1 /*
2  * Copyright (c) 2011-2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: wlan_hdd_sysfs_wowl_add_ptrn.c
22  *
23  * implementation for creating sysfs file wowl_add_ptrn
24  */
25 
26 #include <wlan_hdd_includes.h>
27 #include <wlan_hdd_sysfs.h>
28 #include "osif_vdev_sync.h"
29 #include "wlan_hdd_wowl.h"
30 #include <wlan_hdd_sysfs_wowl_add_ptrn.h>
31 
32 static ssize_t
__hdd_sysfs_wowl_add_ptrn_store(struct net_device * net_dev,char const * buf,size_t count)33 __hdd_sysfs_wowl_add_ptrn_store(struct net_device *net_dev,
34 				char const *buf,
35 				size_t count)
36 {
37 	struct hdd_adapter *adapter = netdev_priv(net_dev);
38 	struct hdd_context *hdd_ctx;
39 	char *buf_local = NULL;
40 	int ret;
41 
42 	if (hdd_validate_adapter(adapter))
43 		return -EINVAL;
44 
45 	hdd_ctx = WLAN_HDD_GET_CTX(adapter);
46 	ret = wlan_hdd_validate_context(hdd_ctx);
47 	if (ret)
48 		return ret;
49 
50 	if (!wlan_hdd_validate_modules_state(hdd_ctx))
51 		return -EINVAL;
52 
53 	if (count > MAX_CMD_INPUT)
54 		return -EINVAL;
55 
56 	buf_local = (char *)qdf_mem_malloc(sizeof(char) * count);
57 	if (!buf_local)
58 		return -EINVAL;
59 
60 	strlcpy(buf_local, buf, count);
61 
62 	buf_local[count - 1] = '\0';
63 
64 	hdd_debug("wowl_add_ptrn: count %zu buf_local:(%s)",
65 		  count, buf_local);
66 
67 	if (!hdd_add_wowl_ptrn(adapter, buf_local)) {
68 		hdd_err_rl("Failed to add wowl ptrn");
69 		qdf_mem_free(buf_local);
70 		return -EINVAL;
71 	}
72 
73 	qdf_mem_free(buf_local);
74 	return count;
75 }
76 
77 static ssize_t
hdd_sysfs_wowl_add_ptrn_store(struct device * dev,struct device_attribute * attr,char const * buf,size_t count)78 hdd_sysfs_wowl_add_ptrn_store(struct device *dev,
79 			      struct device_attribute *attr,
80 			      char const *buf, size_t count)
81 {
82 	struct net_device *net_dev = container_of(dev, struct net_device, dev);
83 	struct osif_vdev_sync *vdev_sync;
84 	ssize_t err_size;
85 
86 	err_size = osif_vdev_sync_op_start(net_dev, &vdev_sync);
87 	if (err_size)
88 		return err_size;
89 
90 	err_size = __hdd_sysfs_wowl_add_ptrn_store(net_dev, buf, count);
91 
92 	osif_vdev_sync_op_stop(vdev_sync);
93 
94 	return err_size;
95 }
96 
97 static DEVICE_ATTR(wowl_add_ptrn, 0220,
98 		   NULL, hdd_sysfs_wowl_add_ptrn_store);
99 
hdd_sysfs_wowl_add_ptrn_create(struct hdd_adapter * adapter)100 int hdd_sysfs_wowl_add_ptrn_create(struct hdd_adapter *adapter)
101 {
102 	int error;
103 
104 	error = device_create_file(&adapter->dev->dev,
105 				   &dev_attr_wowl_add_ptrn);
106 	if (error)
107 		hdd_err("could not create wowl_add_ptrn sysfs file");
108 
109 	return error;
110 }
111 
hdd_sysfs_wowl_add_ptrn_destroy(struct hdd_adapter * adapter)112 void hdd_sysfs_wowl_add_ptrn_destroy(struct hdd_adapter *adapter)
113 {
114 	device_remove_file(&adapter->dev->dev, &dev_attr_wowl_add_ptrn);
115 }
116