1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
23 
24 #include <rtw_android.h>
25 #include <osdep_service.h>
26 #include <rtw_debug.h>
27 #include <rtw_ioctl_set.h>
28 
29 static const char *android_wifi_cmd_str[ANDROID_WIFI_CMD_MAX] = {
30 	"START",
31 	"STOP",
32 	"SCAN-ACTIVE",
33 	"SCAN-PASSIVE",
34 	"RSSI",
35 	"LINKSPEED",
36 	"RXFILTER-START",
37 	"RXFILTER-STOP",
38 	"RXFILTER-ADD",
39 	"RXFILTER-REMOVE",
40 	"BTCOEXSCAN-START",
41 	"BTCOEXSCAN-STOP",
42 	"BTCOEXMODE",
43 	"SETSUSPENDOPT",
44 	"P2P_DEV_ADDR",
45 	"SETFWPATH",
46 	"SETBAND",
47 	"GETBAND",
48 	"COUNTRY",
49 	"P2P_SET_NOA",
50 	"P2P_GET_NOA",
51 	"P2P_SET_PS",
52 	"SET_AP_WPS_P2P_IE",
53 	"MACADDR",
54 	"BLOCK",
55 	"WFD-ENABLE",
56 	"WFD-DISABLE",
57 	"WFD-SET-TCPPORT",
58 	"WFD-SET-MAXTPUT",
59 	"WFD-SET-DEVTYPE",
60 };
61 
62 struct android_wifi_priv_cmd {
63 	const char __user *buf;
64 	int used_len;
65 	int total_len;
66 };
67 
68 /**
69  * Local (static) functions and variables
70  */
71 
72 /* Initialize g_wifi_on to 1 so dhd_bus_start will be called for the first
73  * time (only) in dhd_open, subsequential wifi on will be handled by
74  * wl_android_wifi_on
75  */
76 static int g_wifi_on = true;
77 
rtw_android_cmdstr_to_num(char * cmdstr)78 int rtw_android_cmdstr_to_num(char *cmdstr)
79 {
80 	int cmd_num;
81 	for (cmd_num = 0; cmd_num < ANDROID_WIFI_CMD_MAX; cmd_num++)
82 		if (0 == strncasecmp(cmdstr, android_wifi_cmd_str[cmd_num],
83 				  strlen(android_wifi_cmd_str[cmd_num])))
84 			break;
85 	return cmd_num;
86 }
87 
rtw_android_get_rssi(struct net_device * net,char * command,int total_len)88 static int rtw_android_get_rssi(struct net_device *net, char *command,
89 				int total_len)
90 {
91 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(net);
92 	struct	mlme_priv	*pmlmepriv = &(padapter->mlmepriv);
93 	struct	wlan_network	*pcur_network = &pmlmepriv->cur_network;
94 	int bytes_written = 0;
95 
96 	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
97 		bytes_written += snprintf(&command[bytes_written], total_len,
98 					  "%s rssi %d",
99 					  pcur_network->network.Ssid.Ssid,
100 					  padapter->recvpriv.rssi);
101 	}
102 	return bytes_written;
103 }
104 
rtw_android_get_link_speed(struct net_device * net,char * command,int total_len)105 static int rtw_android_get_link_speed(struct net_device *net, char *command,
106 				      int total_len)
107 {
108 	struct adapter *padapter = (struct adapter *)rtw_netdev_priv(net);
109 	u16 link_speed;
110 
111 	link_speed = rtw_get_cur_max_rate(padapter) / 10;
112 	return snprintf(command, total_len, "LinkSpeed %d",
113 				 link_speed);
114 }
115 
rtw_android_get_macaddr(struct net_device * net,char * command,int total_len)116 static int rtw_android_get_macaddr(struct net_device *net, char *command,
117 				   int total_len)
118 {
119 	return snprintf(command, total_len, "Macaddr = %pM",
120 				 net->dev_addr);
121 }
122 
android_set_cntry(struct net_device * net,char * command,int total_len)123 static int android_set_cntry(struct net_device *net, char *command,
124 			     int total_len)
125 {
126 	struct adapter *adapter = (struct adapter *)rtw_netdev_priv(net);
127 	char *country_code = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_COUNTRY]) + 1;
128 	int ret;
129 
130 	ret = rtw_set_country(adapter, country_code);
131 	return (ret == _SUCCESS) ? 0 : -1;
132 }
133 
android_get_p2p_addr(struct net_device * net,char * command,int total_len)134 static int android_get_p2p_addr(struct net_device *net, char *command,
135 					int total_len)
136 {
137 	/* We use the same address as our HW MAC address */
138 	memcpy(command, net->dev_addr, ETH_ALEN);
139 	return ETH_ALEN;
140 }
141 
rtw_android_set_block(struct net_device * net,char * command,int total_len)142 static int rtw_android_set_block(struct net_device *net, char *command,
143 				 int total_len)
144 {
145 	return 0;
146 }
147 
rtw_android_priv_cmd(struct net_device * net,struct ifreq * ifr,int cmd)148 int rtw_android_priv_cmd(struct net_device *net, struct ifreq *ifr, int cmd)
149 {
150 	int ret = 0;
151 	char *command;
152 	int cmd_num;
153 	int bytes_written = 0;
154 	struct android_wifi_priv_cmd priv_cmd;
155 
156 	if (!ifr->ifr_data)
157 		return -EINVAL;
158 	if (copy_from_user(&priv_cmd, ifr->ifr_data, sizeof(priv_cmd)))
159 		return -EFAULT;
160 	if (priv_cmd.total_len < 1)
161 		return -EINVAL;
162 	command = memdup_user(priv_cmd.buf, priv_cmd.total_len);
163 	if (IS_ERR(command))
164 		return PTR_ERR(command);
165 	command[priv_cmd.total_len - 1] = 0;
166 	DBG_88E("%s: Android private cmd \"%s\" on %s\n",
167 		__func__, command, ifr->ifr_name);
168 	cmd_num = rtw_android_cmdstr_to_num(command);
169 	switch (cmd_num) {
170 	case ANDROID_WIFI_CMD_START:
171 		goto response;
172 	case ANDROID_WIFI_CMD_SETFWPATH:
173 		goto response;
174 	}
175 	if (!g_wifi_on) {
176 		DBG_88E("%s: Ignore private cmd \"%s\" - iface %s is down\n",
177 			__func__, command, ifr->ifr_name);
178 		ret = 0;
179 		goto free;
180 	}
181 	switch (cmd_num) {
182 	case ANDROID_WIFI_CMD_STOP:
183 		break;
184 	case ANDROID_WIFI_CMD_SCAN_ACTIVE:
185 		break;
186 	case ANDROID_WIFI_CMD_SCAN_PASSIVE:
187 		break;
188 	case ANDROID_WIFI_CMD_RSSI:
189 		bytes_written = rtw_android_get_rssi(net, command,
190 						     priv_cmd.total_len);
191 		break;
192 	case ANDROID_WIFI_CMD_LINKSPEED:
193 		bytes_written = rtw_android_get_link_speed(net, command,
194 							   priv_cmd.total_len);
195 		break;
196 	case ANDROID_WIFI_CMD_MACADDR:
197 		bytes_written = rtw_android_get_macaddr(net, command,
198 							priv_cmd.total_len);
199 		break;
200 	case ANDROID_WIFI_CMD_BLOCK:
201 		bytes_written = rtw_android_set_block(net, command,
202 						      priv_cmd.total_len);
203 		break;
204 	case ANDROID_WIFI_CMD_RXFILTER_START:
205 		break;
206 	case ANDROID_WIFI_CMD_RXFILTER_STOP:
207 		break;
208 	case ANDROID_WIFI_CMD_RXFILTER_ADD:
209 		break;
210 	case ANDROID_WIFI_CMD_RXFILTER_REMOVE:
211 		break;
212 	case ANDROID_WIFI_CMD_BTCOEXSCAN_START:
213 		/* TBD: BTCOEXSCAN-START */
214 		break;
215 	case ANDROID_WIFI_CMD_BTCOEXSCAN_STOP:
216 		/* TBD: BTCOEXSCAN-STOP */
217 		break;
218 	case ANDROID_WIFI_CMD_BTCOEXMODE:
219 		break;
220 	case ANDROID_WIFI_CMD_SETSUSPENDOPT:
221 		break;
222 	case ANDROID_WIFI_CMD_SETBAND:
223 		break;
224 	case ANDROID_WIFI_CMD_GETBAND:
225 		break;
226 	case ANDROID_WIFI_CMD_COUNTRY:
227 		bytes_written = android_set_cntry(net, command,
228 						  priv_cmd.total_len);
229 		break;
230 	case ANDROID_WIFI_CMD_P2P_DEV_ADDR:
231 		bytes_written = android_get_p2p_addr(net, command,
232 						     priv_cmd.total_len);
233 		break;
234 	case ANDROID_WIFI_CMD_P2P_SET_NOA:
235 		break;
236 	case ANDROID_WIFI_CMD_P2P_GET_NOA:
237 		break;
238 	case ANDROID_WIFI_CMD_P2P_SET_PS:
239 		break;
240 	default:
241 		DBG_88E("Unknown PRIVATE command %s - ignored\n", command);
242 		snprintf(command, 3, "OK");
243 		bytes_written = strlen("OK");
244 	}
245 
246 response:
247 	if (bytes_written >= 0) {
248 		if ((bytes_written == 0) && (priv_cmd.total_len > 0))
249 			command[0] = '\0';
250 		if (bytes_written >= priv_cmd.total_len) {
251 			DBG_88E("%s: bytes_written = %d\n", __func__,
252 				bytes_written);
253 			bytes_written = priv_cmd.total_len;
254 		} else {
255 			bytes_written++;
256 		}
257 		priv_cmd.used_len = bytes_written;
258 		if (copy_to_user((char __user *)priv_cmd.buf, command,
259 				 bytes_written)) {
260 			DBG_88E("%s: failed to copy data to user buffer\n",
261 				__func__);
262 			ret = -EFAULT;
263 		}
264 	} else {
265 		ret = bytes_written;
266 	}
267 free:
268 	kfree(command);
269 	return ret;
270 }
271