1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 
11 #include <rtw_android.h>
12 #include <osdep_service.h>
13 #include <rtw_debug.h>
14 #include <rtw_ioctl_set.h>
15 
16 static const char *android_wifi_cmd_str[ANDROID_WIFI_CMD_MAX] = {
17 	"START",
18 	"STOP",
19 	"SCAN-ACTIVE",
20 	"SCAN-PASSIVE",
21 	"RSSI",
22 	"LINKSPEED",
23 	"RXFILTER-START",
24 	"RXFILTER-STOP",
25 	"RXFILTER-ADD",
26 	"RXFILTER-REMOVE",
27 	"BTCOEXSCAN-START",
28 	"BTCOEXSCAN-STOP",
29 	"BTCOEXMODE",
30 	"SETSUSPENDOPT",
31 	"P2P_DEV_ADDR",
32 	"SETFWPATH",
33 	"SETBAND",
34 	"GETBAND",
35 	"COUNTRY",
36 	"P2P_SET_NOA",
37 	"P2P_GET_NOA",
38 	"P2P_SET_PS",
39 	"SET_AP_WPS_P2P_IE",
40 	"MACADDR",
41 	"BLOCK",
42 	"WFD-ENABLE",
43 	"WFD-DISABLE",
44 	"WFD-SET-TCPPORT",
45 	"WFD-SET-MAXTPUT",
46 	"WFD-SET-DEVTYPE",
47 };
48 
49 struct android_wifi_priv_cmd {
50 	const char __user *buf;
51 	int used_len;
52 	int total_len;
53 };
54 
rtw_android_cmdstr_to_num(char * cmdstr)55 int rtw_android_cmdstr_to_num(char *cmdstr)
56 {
57 	int cmd_num;
58 
59 	for (cmd_num = 0; cmd_num < ANDROID_WIFI_CMD_MAX; cmd_num++)
60 		if (!strncasecmp(cmdstr, android_wifi_cmd_str[cmd_num],
61 				 strlen(android_wifi_cmd_str[cmd_num])))
62 			break;
63 	return cmd_num;
64 }
65 
rtw_android_get_rssi(struct net_device * net,char * command,int total_len)66 static int rtw_android_get_rssi(struct net_device *net, char *command,
67 				int total_len)
68 {
69 	struct adapter *padapter = netdev_priv(net);
70 	struct	mlme_priv	*pmlmepriv = &padapter->mlmepriv;
71 	struct	wlan_network	*pcur_network = &pmlmepriv->cur_network;
72 	int bytes_written = 0;
73 
74 	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
75 		bytes_written += snprintf(&command[bytes_written], total_len,
76 					  "%s rssi %d",
77 					  pcur_network->network.ssid.ssid,
78 					  padapter->recvpriv.rssi);
79 	}
80 	return bytes_written;
81 }
82 
rtw_android_get_link_speed(struct net_device * net,char * command,int total_len)83 static int rtw_android_get_link_speed(struct net_device *net, char *command,
84 				      int total_len)
85 {
86 	struct adapter *padapter = netdev_priv(net);
87 	u16 link_speed;
88 
89 	link_speed = rtw_get_cur_max_rate(padapter) / 10;
90 	return snprintf(command, total_len, "LinkSpeed %d",
91 				 link_speed);
92 }
93 
rtw_android_get_macaddr(struct net_device * net,char * command,int total_len)94 static int rtw_android_get_macaddr(struct net_device *net, char *command,
95 				   int total_len)
96 {
97 	return snprintf(command, total_len, "Macaddr = %pM",
98 				 net->dev_addr);
99 }
100 
android_set_cntry(struct net_device * net,char * command,int total_len)101 static int android_set_cntry(struct net_device *net, char *command,
102 			     int total_len)
103 {
104 	struct adapter *adapter = netdev_priv(net);
105 	char *country_code = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_COUNTRY]) + 1;
106 	int ret;
107 
108 	ret = rtw_set_country(adapter, country_code);
109 	return (ret == _SUCCESS) ? 0 : -1;
110 }
111 
android_get_p2p_addr(struct net_device * net,char * command,int total_len)112 static int android_get_p2p_addr(struct net_device *net, char *command,
113 				int total_len)
114 {
115 	/* We use the same address as our HW MAC address */
116 	memcpy(command, net->dev_addr, ETH_ALEN);
117 	return ETH_ALEN;
118 }
119 
rtw_android_priv_cmd(struct net_device * net,struct ifreq * ifr,int cmd)120 int rtw_android_priv_cmd(struct net_device *net, struct ifreq *ifr, int cmd)
121 {
122 	int ret = 0;
123 	char *command;
124 	int cmd_num;
125 	int bytes_written = 0;
126 	struct android_wifi_priv_cmd priv_cmd;
127 
128 	if (!ifr->ifr_data)
129 		return -EINVAL;
130 	if (copy_from_user(&priv_cmd, ifr->ifr_data, sizeof(priv_cmd)))
131 		return -EFAULT;
132 	if (priv_cmd.total_len < 1)
133 		return -EINVAL;
134 	command = memdup_user(priv_cmd.buf, priv_cmd.total_len);
135 	if (IS_ERR(command))
136 		return PTR_ERR(command);
137 	command[priv_cmd.total_len - 1] = 0;
138 	DBG_88E("%s: Android private cmd \"%s\" on %s\n",
139 		__func__, command, ifr->ifr_name);
140 	cmd_num = rtw_android_cmdstr_to_num(command);
141 	switch (cmd_num) {
142 	case ANDROID_WIFI_CMD_START:
143 		goto response;
144 	case ANDROID_WIFI_CMD_SETFWPATH:
145 		goto response;
146 	}
147 	switch (cmd_num) {
148 	case ANDROID_WIFI_CMD_STOP:
149 		break;
150 	case ANDROID_WIFI_CMD_SCAN_ACTIVE:
151 		break;
152 	case ANDROID_WIFI_CMD_SCAN_PASSIVE:
153 		break;
154 	case ANDROID_WIFI_CMD_RSSI:
155 		bytes_written = rtw_android_get_rssi(net, command,
156 						     priv_cmd.total_len);
157 		break;
158 	case ANDROID_WIFI_CMD_LINKSPEED:
159 		bytes_written = rtw_android_get_link_speed(net, command,
160 							   priv_cmd.total_len);
161 		break;
162 	case ANDROID_WIFI_CMD_MACADDR:
163 		bytes_written = rtw_android_get_macaddr(net, command,
164 							priv_cmd.total_len);
165 		break;
166 	case ANDROID_WIFI_CMD_BLOCK:
167 		break;
168 	case ANDROID_WIFI_CMD_RXFILTER_START:
169 		break;
170 	case ANDROID_WIFI_CMD_RXFILTER_STOP:
171 		break;
172 	case ANDROID_WIFI_CMD_RXFILTER_ADD:
173 		break;
174 	case ANDROID_WIFI_CMD_RXFILTER_REMOVE:
175 		break;
176 	case ANDROID_WIFI_CMD_BTCOEXSCAN_START:
177 		/* TBD: BTCOEXSCAN-START */
178 		break;
179 	case ANDROID_WIFI_CMD_BTCOEXSCAN_STOP:
180 		/* TBD: BTCOEXSCAN-STOP */
181 		break;
182 	case ANDROID_WIFI_CMD_BTCOEXMODE:
183 		break;
184 	case ANDROID_WIFI_CMD_SETSUSPENDOPT:
185 		break;
186 	case ANDROID_WIFI_CMD_SETBAND:
187 		break;
188 	case ANDROID_WIFI_CMD_GETBAND:
189 		break;
190 	case ANDROID_WIFI_CMD_COUNTRY:
191 		bytes_written = android_set_cntry(net, command,
192 						  priv_cmd.total_len);
193 		break;
194 	case ANDROID_WIFI_CMD_P2P_DEV_ADDR:
195 		bytes_written = android_get_p2p_addr(net, command,
196 						     priv_cmd.total_len);
197 		break;
198 	case ANDROID_WIFI_CMD_P2P_SET_NOA:
199 		break;
200 	case ANDROID_WIFI_CMD_P2P_GET_NOA:
201 		break;
202 	case ANDROID_WIFI_CMD_P2P_SET_PS:
203 		break;
204 	default:
205 		DBG_88E("Unknown PRIVATE command %s - ignored\n", command);
206 		snprintf(command, 3, "OK");
207 		bytes_written = strlen("OK");
208 	}
209 
210 response:
211 	if (bytes_written >= 0) {
212 		if ((bytes_written == 0) && (priv_cmd.total_len > 0))
213 			command[0] = '\0';
214 		if (bytes_written >= priv_cmd.total_len) {
215 			DBG_88E("%s: bytes_written = %d\n", __func__,
216 				bytes_written);
217 			bytes_written = priv_cmd.total_len;
218 		} else {
219 			bytes_written++;
220 		}
221 		priv_cmd.used_len = bytes_written;
222 		if (copy_to_user((char __user *)priv_cmd.buf, command,
223 				 bytes_written)) {
224 			DBG_88E("%s: failed to copy data to user buffer\n",
225 				__func__);
226 			ret = -EFAULT;
227 		}
228 	} else {
229 		ret = bytes_written;
230 	}
231 	kfree(command);
232 	return ret;
233 }
234