1 /*
2  * Copyright (c) 2020-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2021, 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_unit_test.c
20  *
21  * WLAN Host Device Driver implementation to create sysfs
22  * unit_test_target
23  */
24 #include "wlan_hdd_main.h"
25 #include "osif_psoc_sync.h"
26 #include "osif_vdev_sync.h"
27 #include "wlan_dsc_test.h"
28 #include "wlan_hdd_sysfs.h"
29 #include "wlan_hdd_sysfs_unit_test.h"
30 #include "wlan_module_ids.h"
31 #include "wma.h"
32 
33 #define MAX_USER_COMMAND_SIZE_UNIT_TEST_TARGET 256
34 
__hdd_sysfs_unit_test_target_store(struct net_device * net_dev,const char __user * buf,size_t count)35 static ssize_t __hdd_sysfs_unit_test_target_store(
36 		struct net_device *net_dev,
37 		const char __user *buf, size_t count)
38 {
39 	struct hdd_adapter *adapter = WLAN_HDD_GET_PRIV_PTR(net_dev);
40 	struct hdd_context *hdd_ctx;
41 	char buf_local[MAX_USER_COMMAND_SIZE_UNIT_TEST_TARGET + 1];
42 	char *sptr, *token;
43 	uint32_t apps_args[WMI_UNIT_TEST_MAX_NUM_ARGS];
44 	int module_id, args_num, ret, i;
45 	uint8_t vdev_id = 0;
46 	QDF_STATUS status;
47 
48 	if (!adapter) {
49 		hdd_err("Adapter is null");
50 		return -EINVAL;
51 	} else if (adapter->device_mode != QDF_FTM_MODE &&
52 		   hdd_validate_adapter(adapter)) {
53 		return -EINVAL;
54 	}
55 
56 	hdd_ctx = WLAN_HDD_GET_CTX(adapter);
57 	ret = wlan_hdd_validate_context(hdd_ctx);
58 	if (ret != 0)
59 		return ret;
60 
61 	if (!wlan_hdd_validate_modules_state(hdd_ctx))
62 		return -EINVAL;
63 
64 	if (!hdd_ctx->config->is_unit_test_framework_enabled) {
65 		hdd_warn_rl("UT framework is disabled");
66 		return -EINVAL;
67 	}
68 
69 	ret = hdd_sysfs_validate_and_copy_buf(buf_local, sizeof(buf_local),
70 					      buf, count);
71 	if (ret) {
72 		hdd_err_rl("invalid input");
73 		return ret;
74 	}
75 
76 	hdd_nofl_info("unit_test_target: count %zu buf_local:(%s) net_devname %s",
77 		      count, buf_local, net_dev->name);
78 
79 	sptr = buf_local;
80 	/* Get module_id */
81 	token = strsep(&sptr, " ");
82 	if (!token)
83 		return -EINVAL;
84 	if (kstrtou32(token, 0, &module_id))
85 		return -EINVAL;
86 
87 	/* Get args_num */
88 	token = strsep(&sptr, " ");
89 	if (!token)
90 		return -EINVAL;
91 	if (kstrtou32(token, 0, &args_num))
92 		return -EINVAL;
93 
94 	if (module_id < WLAN_MODULE_ID_MIN ||
95 	    module_id >= WLAN_MODULE_ID_MAX) {
96 		hdd_err_rl("Invalid MODULE ID %d", module_id);
97 		return -EINVAL;
98 	}
99 	if (args_num > WMI_UNIT_TEST_MAX_NUM_ARGS) {
100 		hdd_err_rl("Too many args %d", args_num);
101 		return -EINVAL;
102 	}
103 
104 	for (i = 0; i < args_num; i++) {
105 		token = strsep(&sptr, " ");
106 		if (!token) {
107 			hdd_err_rl("not enough args(%d), expected args_num:%d",
108 				   i, args_num);
109 			return -EINVAL;
110 		}
111 		if (kstrtou32(token, 0, &apps_args[i]))
112 			return -EINVAL;
113 	}
114 
115 	if (QDF_GLOBAL_FTM_MODE == hdd_get_conparam())
116 		vdev_id = 0;
117 	else
118 		vdev_id = adapter->deflink->vdev_id;
119 
120 	status = sme_send_unit_test_cmd(vdev_id,
121 					module_id,
122 					args_num,
123 					&apps_args[0]);
124 	if (status != QDF_STATUS_SUCCESS) {
125 		hdd_err_rl("sme_send_unit_test_cmd returned %d", status);
126 		return -EINVAL;
127 	}
128 
129 	return count;
130 }
131 
hdd_sysfs_unit_test_target_store(struct device * dev,struct device_attribute * attr,char const * buf,size_t count)132 static ssize_t hdd_sysfs_unit_test_target_store(struct device *dev,
133 						struct device_attribute *attr,
134 						char const *buf, size_t count)
135 {
136 	struct net_device *net_dev = container_of(dev, struct net_device, dev);
137 	struct osif_vdev_sync *vdev_sync;
138 	ssize_t errno_size;
139 
140 	errno_size = osif_vdev_sync_op_start(net_dev, &vdev_sync);
141 	if (errno_size)
142 		return errno_size;
143 
144 	errno_size = __hdd_sysfs_unit_test_target_store(
145 				net_dev, buf, count);
146 	if (errno_size < 0)
147 		hdd_err_rl("errno_size %zd", errno_size);
148 
149 	osif_vdev_sync_op_stop(vdev_sync);
150 
151 	return errno_size;
152 }
153 
154 static DEVICE_ATTR(unit_test_target, 0220,
155 		   NULL, hdd_sysfs_unit_test_target_store);
156 
hdd_sysfs_unit_test_target_create(struct hdd_adapter * adapter)157 int hdd_sysfs_unit_test_target_create(struct hdd_adapter *adapter)
158 {
159 	int error;
160 
161 	error = device_create_file(&adapter->dev->dev,
162 				   &dev_attr_unit_test_target);
163 	if (error)
164 		hdd_err("could not create unit_test_target sysfs file");
165 
166 	return error;
167 }
168 
hdd_sysfs_unit_test_target_destroy(struct hdd_adapter * adapter)169 void hdd_sysfs_unit_test_target_destroy(struct hdd_adapter *adapter)
170 {
171 	device_remove_file(&adapter->dev->dev, &dev_attr_unit_test_target);
172 }
173 
174