1 /*
2  * Copyright (c) 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 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_rts_cts.c
20  *
21  * implementation for creating sysfs file rts_cts
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_rts_cts.h"
28 #include <wlan_mlme_ucfg_api.h>
29 #include <cfg_ucfg_api.h>
30 #include <cfg_mlme_threshold.h>
31 #include <wma_api.h>
32 
33 static ssize_t
__hdd_sysfs_rts_cts_store(struct net_device * net_dev,char const * buf,size_t count)34 __hdd_sysfs_rts_cts_store(struct net_device *net_dev,
35 			  char const *buf, size_t count)
36 {
37 	struct hdd_adapter *adapter = netdev_priv(net_dev);
38 	char buf_local[MAX_SYSFS_USER_COMMAND_SIZE_LENGTH + 1];
39 	struct hdd_context *hdd_ctx;
40 	char *sptr, *token;
41 	uint32_t rts_threshold;
42 	QDF_STATUS status;
43 	int value, ret;
44 
45 	if (hdd_validate_adapter(adapter))
46 		return -EINVAL;
47 
48 	hdd_ctx = WLAN_HDD_GET_CTX(adapter);
49 	ret = wlan_hdd_validate_context(hdd_ctx);
50 	if (ret != 0)
51 		return ret;
52 
53 	if (!wlan_hdd_validate_modules_state(hdd_ctx))
54 		return -EINVAL;
55 
56 	ret = hdd_sysfs_validate_and_copy_buf(buf_local, sizeof(buf_local),
57 					      buf, count);
58 
59 	if (ret) {
60 		hdd_err_rl("invalid input");
61 		return ret;
62 	}
63 
64 	sptr = buf_local;
65 	token = strsep(&sptr, " ");
66 	if (!token)
67 		return -EINVAL;
68 	if (kstrtou32(token, 0, &value))
69 		return -EINVAL;
70 
71 	switch (value) {
72 	case HDD_SYSFS_RTS_CTS_ENABLE:
73 		status = ucfg_mlme_get_rts_threshold(hdd_ctx->psoc,
74 						     &rts_threshold);
75 		if (QDF_IS_STATUS_ERROR(status)) {
76 			hdd_err_rl("Get rts threshold failed, status %d",
77 				   status);
78 			return -EINVAL;
79 		}
80 		break;
81 	case HDD_SYSFS_RTS_CTS_DISABLE:
82 	case HDD_SYSFS_CTS_ENABLE:
83 		rts_threshold = cfg_max(CFG_RTS_THRESHOLD);
84 		break;
85 	default:
86 		hdd_err_rl("invalid value %d", value);
87 		return -EINVAL;
88 	}
89 
90 	ret = wma_cli_set_command(adapter->deflink->vdev_id,
91 				  wmi_vdev_param_enable_rtscts,
92 				  value, VDEV_CMD);
93 	if (ret) {
94 		hdd_err_rl("Failed to set firmware, ret %d", ret);
95 		return ret;
96 	}
97 
98 	status = ucfg_mlme_set_rts_threshold(hdd_ctx->psoc, rts_threshold);
99 	if (QDF_IS_STATUS_ERROR(status)) {
100 		hdd_err_rl("Set rts threshold failed, status %d", status);
101 		return -EINVAL;
102 	}
103 
104 	return count;
105 }
106 
107 static ssize_t
hdd_sysfs_rts_cts_store(struct device * dev,struct device_attribute * attr,char const * buf,size_t count)108 hdd_sysfs_rts_cts_store(struct device *dev,
109 			struct device_attribute *attr,
110 			char const *buf, size_t count)
111 {
112 	struct net_device *net_dev = container_of(dev, struct net_device, dev);
113 	struct osif_vdev_sync *vdev_sync;
114 	ssize_t errno_size;
115 
116 	errno_size = osif_vdev_sync_op_start(net_dev, &vdev_sync);
117 	if (errno_size)
118 		return errno_size;
119 
120 	errno_size = __hdd_sysfs_rts_cts_store(net_dev, buf, count);
121 
122 	osif_vdev_sync_op_stop(vdev_sync);
123 
124 	return errno_size;
125 }
126 
127 static DEVICE_ATTR(rts_cts, 0220,
128 		   NULL, hdd_sysfs_rts_cts_store);
129 
hdd_sysfs_rts_cts_create(struct hdd_adapter * adapter)130 int hdd_sysfs_rts_cts_create(struct hdd_adapter *adapter)
131 {
132 	int error;
133 
134 	error = device_create_file(&adapter->dev->dev, &dev_attr_rts_cts);
135 	if (error)
136 		hdd_err("could not create rts_cts sysfs file");
137 
138 	return error;
139 }
140 
hdd_sysfs_rts_cts_destroy(struct hdd_adapter * adapter)141 void hdd_sysfs_rts_cts_destroy(struct hdd_adapter *adapter)
142 {
143 	device_remove_file(&adapter->dev->dev, &dev_attr_rts_cts);
144 }
145