1 /*
2  * Copyright (c) 2011-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /**
19  * DOC: wlan_hdd_sysfs_radar.c
20  *
21  * Implementation for creating sysfs file radar
22  */
23 
24 #include <wlan_hdd_includes.h>
25 #include "osif_vdev_sync.h"
26 #include <wlan_hdd_sysfs.h>
27 #include <wlan_hdd_sysfs_radar.h>
28 #include "wlan_dfs_tgt_api.h"
29 
30 static ssize_t
__hdd_sysfs_radar_store(struct net_device * net_dev,char const * buf,size_t count)31 __hdd_sysfs_radar_store(struct net_device *net_dev,
32 			char const *buf, size_t count)
33 {
34 	struct hdd_adapter *adapter = netdev_priv(net_dev);
35 	struct hdd_ap_ctx *ap_ctx = WLAN_HDD_GET_AP_CTX_PTR(adapter->deflink);
36 	char buf_local[MAX_SYSFS_USER_COMMAND_SIZE_LENGTH + 1];
37 	struct wlan_objmgr_pdev *pdev;
38 	struct radar_found_info radar;
39 	struct hdd_context *hdd_ctx;
40 	char *sptr, *token;
41 	int set_value;
42 	int ret;
43 
44 	if (hdd_validate_adapter(adapter))
45 		return -EINVAL;
46 
47 	hdd_ctx = WLAN_HDD_GET_CTX(adapter);
48 	ret = wlan_hdd_validate_context(hdd_ctx);
49 	if (ret != 0)
50 		return ret;
51 
52 	if (!wlan_hdd_validate_modules_state(hdd_ctx))
53 		return -EINVAL;
54 
55 	ret = hdd_sysfs_validate_and_copy_buf(buf_local, sizeof(buf_local),
56 					      buf, count);
57 
58 	if (ret) {
59 		hdd_err_rl("invalid input");
60 		return ret;
61 	}
62 
63 	sptr = buf_local;
64 	hdd_debug("set_radar: count %zu buf_local:(%s) net_devname %s",
65 		  count, buf_local, net_dev->name);
66 
67 	/* Get set_value */
68 	token = strsep(&sptr, " ");
69 	if (!token)
70 		return -EINVAL;
71 	if (kstrtou32(token, 0, &set_value))
72 		return -EINVAL;
73 
74 	hdd_debug("Set QCASAP_SET_RADAR_CMD val %d", set_value);
75 
76 	pdev = hdd_ctx->pdev;
77 	if (!pdev) {
78 		hdd_err("null pdev");
79 		return -EINVAL;
80 	}
81 
82 	qdf_mem_zero(&radar, sizeof(radar));
83 	if (policy_mgr_get_dfs_beaconing_session_id(hdd_ctx->psoc) !=
84 	    WLAN_UMAC_VDEV_ID_MAX)
85 		tgt_dfs_process_radar_ind(pdev, &radar);
86 	else
87 		hdd_debug("Ignore set radar, op ch_freq(%d) is not dfs",
88 			  ap_ctx->operating_chan_freq);
89 
90 	return count;
91 }
92 
93 static ssize_t
hdd_sysfs_radar_store(struct device * dev,struct device_attribute * attr,char const * buf,size_t count)94 hdd_sysfs_radar_store(struct device *dev,
95 		      struct device_attribute *attr,
96 		      char const *buf, size_t count)
97 {
98 	struct net_device *net_dev = container_of(dev, struct net_device, dev);
99 	struct osif_vdev_sync *vdev_sync;
100 	ssize_t errno_size;
101 
102 	errno_size = osif_vdev_sync_op_start(net_dev, &vdev_sync);
103 	if (errno_size)
104 		return errno_size;
105 
106 	errno_size = __hdd_sysfs_radar_store(net_dev, buf, count);
107 
108 	osif_vdev_sync_op_stop(vdev_sync);
109 
110 	return errno_size;
111 }
112 
113 static DEVICE_ATTR(radar, 0220,
114 		   NULL, hdd_sysfs_radar_store);
115 
hdd_sysfs_radar_create(struct hdd_adapter * adapter)116 int hdd_sysfs_radar_create(struct hdd_adapter *adapter)
117 {
118 	int error;
119 
120 	error = device_create_file(&adapter->dev->dev, &dev_attr_radar);
121 	if (error)
122 		hdd_err("could not create radar sysfs file");
123 
124 	return error;
125 }
126 
hdd_sysfs_radar_destroy(struct hdd_adapter * adapter)127 void hdd_sysfs_radar_destroy(struct hdd_adapter *adapter)
128 {
129 	device_remove_file(&adapter->dev->dev, &dev_attr_radar);
130 }
131