• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2017 Realtek Corporation.
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  *****************************************************************************/
15 
16 #ifdef CONFIG_GPIO_WAKEUP
17 #include <linux/gpio.h>
18 #endif
19 
20 #include <drv_types.h>
21 
22 #if defined(RTW_ENABLE_WIFI_CONTROL_FUNC)
23 #include <linux/platform_device.h>
24 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
25 	#include <linux/wlan_plat.h>
26 #else
27 	#include <linux/wifi_tiwlan.h>
28 #endif
29 #endif /* defined(RTW_ENABLE_WIFI_CONTROL_FUNC) */
30 
31 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0))
32 #define strnicmp	strncasecmp
33 #endif /* Linux kernel >= 4.0.0 */
34 
35 #ifdef CONFIG_GPIO_WAKEUP
36 #include <linux/interrupt.h>
37 #include <linux/irq.h>
38 #endif
39 
40 #include "rtw_version.h"
41 
42 extern void macstr2num(u8 *dst, u8 *src);
43 
44 const char *android_wifi_cmd_str[ANDROID_WIFI_CMD_MAX] = {
45 	"START",
46 	"STOP",
47 	"SCAN-ACTIVE",
48 	"SCAN-PASSIVE",
49 	"RSSI",
50 	"LINKSPEED",
51 	"RXFILTER-START",
52 	"RXFILTER-STOP",
53 	"RXFILTER-ADD",
54 	"RXFILTER-REMOVE",
55 	"BTCOEXSCAN-START",
56 	"BTCOEXSCAN-STOP",
57 	"BTCOEXMODE",
58 	"SETSUSPENDMODE",
59 	"SETSUSPENDOPT",
60 	"P2P_DEV_ADDR",
61 	"SETFWPATH",
62 	"SETBAND",
63 	"GETBAND",
64 	"COUNTRY",
65 	"P2P_SET_NOA",
66 	"P2P_GET_NOA",
67 	"P2P_SET_PS",
68 	"SET_AP_WPS_P2P_IE",
69 
70 	"MIRACAST",
71 
72 #ifdef CONFIG_PNO_SUPPORT
73 	"PNOSSIDCLR",
74 	"PNOSETUP",
75 	"PNOFORCE",
76 	"PNODEBUG",
77 #endif
78 
79 	"MACADDR",
80 
81 	"BLOCK_SCAN",
82 	"BLOCK",
83 	"WFD-ENABLE",
84 	"WFD-DISABLE",
85 	"WFD-SET-TCPPORT",
86 	"WFD-SET-MAXTPUT",
87 	"WFD-SET-DEVTYPE",
88 	"SET_DTIM",
89 	"HOSTAPD_SET_MACADDR_ACL",
90 	"HOSTAPD_ACL_ADD_STA",
91 	"HOSTAPD_ACL_REMOVE_STA",
92 #if defined(CONFIG_GTK_OL) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0))
93 	"GTK_REKEY_OFFLOAD",
94 #endif /* CONFIG_GTK_OL */
95 /*	Private command for	P2P disable*/
96 	"P2P_DISABLE",
97 	"SET_AEK",
98 	"EXT_AUTH_STATUS",
99 	"DRIVER_VERSION"
100 };
101 
102 #ifdef CONFIG_PNO_SUPPORT
103 #define PNO_TLV_PREFIX			'S'
104 #define PNO_TLV_VERSION			'1'
105 #define PNO_TLV_SUBVERSION		'2'
106 #define PNO_TLV_RESERVED		'0'
107 #define PNO_TLV_TYPE_SSID_IE	'S'
108 #define PNO_TLV_TYPE_TIME		'T'
109 #define PNO_TLV_FREQ_REPEAT		'R'
110 #define PNO_TLV_FREQ_EXPO_MAX	'M'
111 
112 typedef struct cmd_tlv {
113 	char prefix;
114 	char version;
115 	char subver;
116 	char reserved;
117 } cmd_tlv_t;
118 
119 #ifdef CONFIG_PNO_SET_DEBUG
120 char pno_in_example[] = {
121 	'P', 'N', 'O', 'S', 'E', 'T', 'U', 'P', ' ',
122 	'S', '1', '2', '0',
123 	'S',	/* 1 */
124 	0x05,
125 	'd', 'l', 'i', 'n', 'k',
126 	'S',	/* 2 */
127 	0x06,
128 	'B', 'U', 'F', 'B', 'U', 'F',
129 	'S',	/* 3 */
130 	0x20,
131 	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '!', '@', '#', '$', '%', '^',
132 	'S',	/* 4 */
133 	0x0a,
134 	'!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
135 	'T',
136 	'0', '5',
137 	'R',
138 	'2',
139 	'M',
140 	'2',
141 	0x00
142 };
143 #endif /* CONFIG_PNO_SET_DEBUG */
144 #endif /* PNO_SUPPORT */
145 
146 typedef struct android_wifi_priv_cmd {
147 	char *buf;
148 	int used_len;
149 	int total_len;
150 } android_wifi_priv_cmd;
151 
152 #ifdef CONFIG_COMPAT
153 typedef struct compat_android_wifi_priv_cmd {
154 	compat_uptr_t buf;
155 	int used_len;
156 	int total_len;
157 } compat_android_wifi_priv_cmd;
158 #endif /* CONFIG_COMPAT */
159 
160 /**
161  * Local (static) functions and variables
162  */
163 
164 /* Initialize g_wifi_on to 1 so dhd_bus_start will be called for the first
165  * time (only) in dhd_open, subsequential wifi on will be handled by
166  * wl_android_wifi_on
167  */
168 static int g_wifi_on = _TRUE;
169 
170 unsigned int oob_irq = 0;
171 unsigned int oob_gpio = 0;
172 
173 #ifdef CONFIG_PNO_SUPPORT
174 /*
175  * rtw_android_pno_setup
176  * Description:
177  * This is used for private command.
178  *
179  * Parameter:
180  * net: net_device
181  * command: parameters from private command
182  * total_len: the length of the command.
183  *
184  * */
rtw_android_pno_setup(struct net_device * net,char * command,int total_len)185 static int rtw_android_pno_setup(struct net_device *net, char *command, int total_len)
186 {
187 	pno_ssid_t pno_ssids_local[MAX_PNO_LIST_COUNT];
188 	int res = -1;
189 	int nssid = 0;
190 	cmd_tlv_t *cmd_tlv_temp;
191 	char *str_ptr;
192 	int tlv_size_left;
193 	int pno_time = 0;
194 	int pno_repeat = 0;
195 	int pno_freq_expo_max = 0;
196 	int cmdlen = strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_PNOSETUP_SET]) + 1;
197 
198 #ifdef CONFIG_PNO_SET_DEBUG
199 	int i;
200 	char *p;
201 	p = pno_in_example;
202 
203 	total_len = sizeof(pno_in_example);
204 	str_ptr = p + cmdlen;
205 #else
206 	str_ptr = command + cmdlen;
207 #endif
208 
209 	if (total_len < (cmdlen + sizeof(cmd_tlv_t))) {
210 		RTW_INFO("%s argument=%d less min size\n", __func__, total_len);
211 		goto exit_proc;
212 	}
213 
214 	tlv_size_left = total_len - cmdlen;
215 
216 	cmd_tlv_temp = (cmd_tlv_t *)str_ptr;
217 	memset(pno_ssids_local, 0, sizeof(pno_ssids_local));
218 
219 	if ((cmd_tlv_temp->prefix == PNO_TLV_PREFIX) &&
220 	    (cmd_tlv_temp->version == PNO_TLV_VERSION) &&
221 	    (cmd_tlv_temp->subver == PNO_TLV_SUBVERSION)) {
222 
223 		str_ptr += sizeof(cmd_tlv_t);
224 		tlv_size_left -= sizeof(cmd_tlv_t);
225 
226 		nssid = rtw_parse_ssid_list_tlv(&str_ptr, pno_ssids_local,
227 			     MAX_PNO_LIST_COUNT, &tlv_size_left);
228 		if (nssid <= 0) {
229 			RTW_INFO("SSID is not presented or corrupted ret=%d\n", nssid);
230 			goto exit_proc;
231 		} else {
232 			if ((str_ptr[0] != PNO_TLV_TYPE_TIME) || (tlv_size_left <= 1)) {
233 				RTW_INFO("%s scan duration corrupted field size %d\n",
234 					 __func__, tlv_size_left);
235 				goto exit_proc;
236 			}
237 			str_ptr++;
238 			pno_time = simple_strtoul(str_ptr, &str_ptr, 16);
239 			RTW_INFO("%s: pno_time=%d\n", __func__, pno_time);
240 
241 			if (str_ptr[0] != 0) {
242 				if ((str_ptr[0] != PNO_TLV_FREQ_REPEAT)) {
243 					RTW_INFO("%s pno repeat : corrupted field\n",
244 						 __func__);
245 					goto exit_proc;
246 				}
247 				str_ptr++;
248 				pno_repeat = simple_strtoul(str_ptr, &str_ptr, 16);
249 				RTW_INFO("%s :got pno_repeat=%d\n", __FUNCTION__, pno_repeat);
250 				if (str_ptr[0] != PNO_TLV_FREQ_EXPO_MAX) {
251 					RTW_INFO("%s FREQ_EXPO_MAX corrupted field size\n",
252 						 __func__);
253 					goto exit_proc;
254 				}
255 				str_ptr++;
256 				pno_freq_expo_max = simple_strtoul(str_ptr, &str_ptr, 16);
257 				RTW_INFO("%s: pno_freq_expo_max=%d\n",
258 					 __func__, pno_freq_expo_max);
259 			}
260 		}
261 	} else {
262 		RTW_INFO("%s get wrong TLV command\n", __FUNCTION__);
263 		goto exit_proc;
264 	}
265 
266 	res = rtw_dev_pno_set(net, pno_ssids_local, nssid, pno_time, pno_repeat, pno_freq_expo_max);
267 
268 #ifdef CONFIG_PNO_SET_DEBUG
269 	rtw_dev_pno_debug(net);
270 #endif
271 
272 exit_proc:
273 	return res;
274 }
275 
276 /*
277  * rtw_android_cfg80211_pno_setup
278  * Description:
279  * This is used for cfg80211 sched_scan.
280  *
281  * Parameter:
282  * net: net_device
283  * request: cfg80211_request
284  * */
285 
rtw_android_cfg80211_pno_setup(struct net_device * net,struct cfg80211_ssid * ssids,int n_ssids,int interval)286 int rtw_android_cfg80211_pno_setup(struct net_device *net,
287 		   struct cfg80211_ssid *ssids, int n_ssids, int interval)
288 {
289 	int res = -1;
290 	int nssid = 0;
291 	int pno_time = 0;
292 	int pno_repeat = 0;
293 	int pno_freq_expo_max = 0;
294 	int index = 0;
295 	pno_ssid_t pno_ssids_local[MAX_PNO_LIST_COUNT];
296 
297 	if (n_ssids > MAX_PNO_LIST_COUNT || n_ssids < 0) {
298 		RTW_INFO("%s: nssids(%d) is invalid.\n", __func__, n_ssids);
299 		return -EINVAL;
300 	}
301 
302 	memset(pno_ssids_local, 0, sizeof(pno_ssids_local));
303 
304 	nssid = n_ssids;
305 
306 	for (index = 0 ; index < nssid ; index++) {
307 		pno_ssids_local[index].SSID_len = ssids[index].ssid_len;
308 		memcpy(pno_ssids_local[index].SSID, ssids[index].ssid,
309 		       ssids[index].ssid_len);
310 	}
311 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
312 	if(ssids)
313 		rtw_mfree((u8 *)ssids, (n_ssids * sizeof(struct cfg80211_ssid)));
314 #endif
315 	pno_time = (interval / 1000);
316 
317 	RTW_INFO("%s: nssids: %d, pno_time=%d\n", __func__, nssid, pno_time);
318 
319 	res = rtw_dev_pno_set(net, pno_ssids_local, nssid, pno_time,
320 			      pno_repeat, pno_freq_expo_max);
321 
322 #ifdef CONFIG_PNO_SET_DEBUG
323 	rtw_dev_pno_debug(net);
324 #endif
325 
326 	return res;
327 }
328 
rtw_android_pno_enable(struct net_device * net,int pno_enable)329 int rtw_android_pno_enable(struct net_device *net, int pno_enable)
330 {
331 	_adapter *padapter = (_adapter *)rtw_netdev_priv(net);
332 	struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter);
333 
334 	if (pwrctl) {
335 		pwrctl->wowlan_pno_enable = pno_enable;
336 		RTW_INFO("%s: wowlan_pno_enable: %d\n", __func__, pwrctl->wowlan_pno_enable);
337 		if (pwrctl->wowlan_pno_enable == 0) {
338 			if (pwrctl->pnlo_info != NULL) {
339 				rtw_mfree((u8 *)pwrctl->pnlo_info, sizeof(pno_nlo_info_t));
340 				pwrctl->pnlo_info = NULL;
341 			}
342 			if (pwrctl->pno_ssid_list != NULL) {
343 				rtw_mfree((u8 *)pwrctl->pno_ssid_list, sizeof(pno_ssid_list_t));
344 				pwrctl->pno_ssid_list = NULL;
345 			}
346 			#ifndef RTW_HALMAC
347 			if (pwrctl->pscan_info != NULL) {
348 				rtw_mfree((u8 *)pwrctl->pscan_info, sizeof(pno_scan_info_t));
349 				pwrctl->pscan_info = NULL;
350 			}
351 			#endif
352 		}
353 		return 0;
354 	} else
355 		return -1;
356 }
357 #endif /* CONFIG_PNO_SUPPORT */
358 
rtw_android_cmdstr_to_num(char * cmdstr)359 int rtw_android_cmdstr_to_num(char *cmdstr)
360 {
361 	int cmd_num;
362 	for (cmd_num = 0 ; cmd_num < ANDROID_WIFI_CMD_MAX; cmd_num++)
363 		if (0 == strnicmp(cmdstr , android_wifi_cmd_str[cmd_num], strlen(android_wifi_cmd_str[cmd_num])))
364 			break;
365 
366 	return cmd_num;
367 }
368 
rtw_android_get_rssi(struct net_device * net,char * command,int total_len)369 int rtw_android_get_rssi(struct net_device *net, char *command, int total_len)
370 {
371 	_adapter *padapter = (_adapter *)rtw_netdev_priv(net);
372 	struct	mlme_priv	*pmlmepriv = &(padapter->mlmepriv);
373 	struct	wlan_network	*pcur_network = &pmlmepriv->cur_network;
374 	int bytes_written = 0;
375 
376 	if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) {
377 		bytes_written += snprintf(&command[bytes_written], total_len, "%s rssi %d",
378 			pcur_network->network.Ssid.Ssid, padapter->recvpriv.rssi);
379 	}
380 
381 	return bytes_written;
382 }
383 
rtw_android_get_link_speed(struct net_device * net,char * command,int total_len)384 int rtw_android_get_link_speed(struct net_device *net, char *command, int total_len)
385 {
386 	_adapter *padapter = (_adapter *)rtw_netdev_priv(net);
387 	int bytes_written = 0;
388 	u16 link_speed = 0;
389 
390 	link_speed = rtw_get_cur_max_rate(padapter) / 10;
391 	bytes_written = snprintf(command, total_len, "LinkSpeed %d", link_speed);
392 
393 	return bytes_written;
394 }
395 
rtw_android_get_macaddr(struct net_device * net,char * command,int total_len)396 int rtw_android_get_macaddr(struct net_device *net, char *command, int total_len)
397 {
398 	int bytes_written = 0;
399 
400 	bytes_written = snprintf(command, total_len, "Macaddr = "MAC_FMT, MAC_ARG(net->dev_addr));
401 	return bytes_written;
402 }
403 
rtw_android_set_country(struct net_device * net,char * command,int total_len)404 int rtw_android_set_country(struct net_device *net, char *command, int total_len)
405 {
406 	_adapter *adapter = (_adapter *)rtw_netdev_priv(net);
407 	char *country_code = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_COUNTRY]) + 1;
408 	int ret = _FAIL;
409 
410 	ret = rtw_set_country(adapter, country_code, RTW_REGD_SET_BY_USER);
411 
412 	return (ret == _SUCCESS) ? 0 : -1;
413 }
414 
rtw_android_get_p2p_dev_addr(struct net_device * net,char * command,int total_len)415 int rtw_android_get_p2p_dev_addr(struct net_device *net, char *command, int total_len)
416 {
417 	int bytes_written = 0;
418 
419 	/* We use the same address as our HW MAC address */
420 	_rtw_memcpy(command, net->dev_addr, ETH_ALEN);
421 
422 	bytes_written = ETH_ALEN;
423 	return bytes_written;
424 }
425 
rtw_android_set_block_scan(struct net_device * net,char * command,int total_len)426 int rtw_android_set_block_scan(struct net_device *net, char *command, int total_len)
427 {
428 	_adapter *adapter = (_adapter *)rtw_netdev_priv(net);
429 	char *block_value = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_BLOCK_SCAN]) + 1;
430 
431 #ifdef CONFIG_IOCTL_CFG80211
432 	adapter_wdev_data(adapter)->block_scan = (*block_value == '0') ? _FALSE : _TRUE;
433 #endif
434 
435 	return 0;
436 }
437 
rtw_android_set_block(struct net_device * net,char * command,int total_len)438 int rtw_android_set_block(struct net_device *net, char *command, int total_len)
439 {
440 	_adapter *adapter = (_adapter *)rtw_netdev_priv(net);
441 	char *block_value = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_BLOCK]) + 1;
442 
443 #ifdef CONFIG_IOCTL_CFG80211
444 	adapter_wdev_data(adapter)->block = (*block_value == '0') ? _FALSE : _TRUE;
445 #endif
446 
447 	return 0;
448 }
449 
rtw_android_setband(struct net_device * net,char * command,int total_len)450 int rtw_android_setband(struct net_device *net, char *command, int total_len)
451 {
452 	_adapter *adapter = (_adapter *)rtw_netdev_priv(net);
453 	char *arg = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_SETBAND]) + 1;
454 	u32 band = WIFI_FREQUENCY_BAND_AUTO;
455 	int ret = _FAIL;
456 
457 	if (sscanf(arg, "%u", &band) >= 1)
458 		ret = rtw_set_band(adapter, band);
459 
460 	return (ret == _SUCCESS) ? 0 : -1;
461 }
462 
rtw_android_getband(struct net_device * net,char * command,int total_len)463 int rtw_android_getband(struct net_device *net, char *command, int total_len)
464 {
465 	_adapter *adapter = (_adapter *)rtw_netdev_priv(net);
466 	int bytes_written = 0;
467 
468 	bytes_written = snprintf(command, total_len, "%u", adapter->setband);
469 
470 	return bytes_written;
471 }
472 
473 #ifdef CONFIG_WFD
rtw_android_set_miracast_mode(struct net_device * net,char * command,int total_len)474 int rtw_android_set_miracast_mode(struct net_device *net, char *command, int total_len)
475 {
476 	_adapter *adapter = (_adapter *)rtw_netdev_priv(net);
477 	struct wifi_display_info *wfd_info = &adapter->wfd_info;
478 	char *arg = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_MIRACAST]) + 1;
479 	u8 mode;
480 	int num;
481 	int ret = _FAIL;
482 
483 	num = sscanf(arg, "%hhu", &mode);
484 
485 	if (num < 1)
486 		goto exit;
487 
488 	switch (mode) {
489 	case 1: /* soruce */
490 		mode = MIRACAST_SOURCE;
491 		break;
492 	case 2: /* sink */
493 		mode = MIRACAST_SINK;
494 		break;
495 	case 0: /* disabled */
496 	default:
497 		mode = MIRACAST_DISABLED;
498 		break;
499 	}
500 	wfd_info->stack_wfd_mode = mode;
501 	RTW_INFO("stack miracast mode: %s\n", get_miracast_mode_str(wfd_info->stack_wfd_mode));
502 
503 	ret = _SUCCESS;
504 
505 exit:
506 	return (ret == _SUCCESS) ? 0 : -1;
507 }
508 #endif /* CONFIG_WFD */
509 
get_int_from_command(char * pcmd)510 int get_int_from_command(char *pcmd)
511 {
512 	int i = 0;
513 
514 	for (i = 0; i < strlen(pcmd); i++) {
515 		if (pcmd[i] == '=') {
516 			/*	Skip the '=' and space characters. */
517 			i += 2;
518 			break;
519 		}
520 	}
521 	return rtw_atoi(pcmd + i) ;
522 }
523 
524 #if defined(CONFIG_GTK_OL) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0))
rtw_gtk_offload(struct net_device * net,u8 * cmd_ptr)525 int rtw_gtk_offload(struct net_device *net, u8 *cmd_ptr)
526 {
527 	int i;
528 	/* u8 *cmd_ptr = priv_cmd.buf; */
529 	struct sta_info *psta;
530 	_adapter *padapter = (_adapter *)rtw_netdev_priv(net);
531 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
532 	struct sta_priv *pstapriv = &padapter->stapriv;
533 	struct security_priv *psecuritypriv = &(padapter->securitypriv);
534 	psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv));
535 
536 
537 	if (psta == NULL)
538 		RTW_INFO("%s, : Obtain Sta_info fail\n", __func__);
539 	else {
540 		/* string command length of "GTK_REKEY_OFFLOAD" */
541 		cmd_ptr += 18;
542 
543 		_rtw_memcpy(psta->kek, cmd_ptr, RTW_KEK_LEN);
544 		cmd_ptr += RTW_KEK_LEN;
545 		/*
546 		printk("supplicant KEK: ");
547 		for(i=0;i<RTW_KEK_LEN; i++)
548 			printk(" %02x ", psta->kek[i]);
549 		printk("\n supplicant KCK: ");
550 		*/
551 		_rtw_memcpy(psta->kck, cmd_ptr, RTW_KCK_LEN);
552 		cmd_ptr += RTW_KCK_LEN;
553 		/*
554 		for(i=0;i<RTW_KEK_LEN; i++)
555 			printk(" %02x ", psta->kck[i]);
556 		*/
557 		_rtw_memcpy(psta->replay_ctr, cmd_ptr, RTW_REPLAY_CTR_LEN);
558 		psecuritypriv->binstallKCK_KEK = _TRUE;
559 
560 		/* printk("\nREPLAY_CTR: "); */
561 		/* for(i=0;i<RTW_REPLAY_CTR_LEN; i++) */
562 		/* printk(" %02x ", psta->replay_ctr[i]); */
563 	}
564 
565 	return _SUCCESS;
566 }
567 #endif /* CONFIG_GTK_OL */
568 
569 #ifdef CONFIG_RTW_MESH_AEK
rtw_android_set_aek(struct net_device * ndev,char * command,int total_len)570 static int rtw_android_set_aek(struct net_device *ndev, char *command, int total_len)
571 {
572 #define SET_AEK_DATA_LEN (ETH_ALEN + 32)
573 
574 	_adapter *adapter = (_adapter *)rtw_netdev_priv(ndev);
575 	u8 *addr;
576 	u8 *aek;
577 	int err = 0;
578 
579 	if (total_len - strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_SET_AEK]) - 1 != SET_AEK_DATA_LEN) {
580 		err = -EINVAL;
581 		goto exit;
582 	}
583 
584 	addr = command + strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_SET_AEK]) + 1;
585 	aek = addr + ETH_ALEN;
586 
587 	RTW_PRINT(FUNC_NDEV_FMT" addr="MAC_FMT"\n"
588 		, FUNC_NDEV_ARG(ndev), MAC_ARG(addr));
589 	if (0)
590 		RTW_PRINT(FUNC_NDEV_FMT" aek="KEY_FMT KEY_FMT"\n"
591 			, FUNC_NDEV_ARG(ndev), KEY_ARG(aek), KEY_ARG(aek + 16));
592 
593 	if (rtw_mesh_plink_set_aek(adapter, addr, aek) != _SUCCESS)
594 		err = -ENOENT;
595 
596 exit:
597 	return err;
598 }
599 #endif /* CONFIG_RTW_MESH_AEK */
600 
rtw_android_priv_cmd(struct net_device * net,struct ifreq * ifr,int cmd)601 int rtw_android_priv_cmd(struct net_device *net, struct ifreq *ifr, int cmd)
602 {
603 	#define PRIVATE_COMMAND_MAX_LEN        8192
604 	int ret = 0;
605 	char *command = NULL;
606 	int cmd_num;
607 	int bytes_written = 0;
608 #ifdef CONFIG_PNO_SUPPORT
609 	uint cmdlen = 0;
610 	uint pno_enable = 0;
611 #endif
612 	android_wifi_priv_cmd priv_cmd;
613 	_adapter	*padapter = (_adapter *) rtw_netdev_priv(net);
614 #ifdef CONFIG_WFD
615 	struct wifi_display_info		*pwfd_info;
616 #endif
617 
618 	rtw_lock_suspend();
619 
620 	if (!ifr->ifr_data) {
621 		ret = -EINVAL;
622 		goto exit;
623 	}
624 	if (padapter->registrypriv.mp_mode == 1) {
625 		ret = -EINVAL;
626 		goto exit;
627 	}
628 #ifdef CONFIG_COMPAT
629 #if (KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE)
630 	if (is_compat_task()) {
631 #else
632 	if (in_compat_syscall()) {
633 #endif
634 		/* User space is 32-bit, use compat ioctl */
635 		compat_android_wifi_priv_cmd compat_priv_cmd;
636 
637 		if (copy_from_user(&compat_priv_cmd, ifr->ifr_data, sizeof(compat_android_wifi_priv_cmd))) {
638 			ret = -EFAULT;
639 			goto exit;
640 		}
641 		priv_cmd.buf = compat_ptr(compat_priv_cmd.buf);
642 		priv_cmd.used_len = compat_priv_cmd.used_len;
643 		priv_cmd.total_len = compat_priv_cmd.total_len;
644 	} else
645 #endif /* CONFIG_COMPAT */
646 		if (copy_from_user(&priv_cmd, ifr->ifr_data, sizeof(android_wifi_priv_cmd))) {
647 			ret = -EFAULT;
648 			goto exit;
649 		}
650 	if (padapter->registrypriv.mp_mode == 1) {
651 		ret = -EFAULT;
652 		goto exit;
653 	}
654 	/*RTW_INFO("%s priv_cmd.buf=%p priv_cmd.total_len=%d  priv_cmd.used_len=%d\n",__func__,priv_cmd.buf,priv_cmd.total_len,priv_cmd.used_len);*/
655 	if (priv_cmd.total_len > PRIVATE_COMMAND_MAX_LEN || priv_cmd.total_len < 0) {
656 		RTW_WARN("%s: invalid private command (%d)\n", __FUNCTION__,
657 			priv_cmd.total_len);
658 		ret = -EFAULT;
659 		goto exit;
660 	}
661 
662 	command = rtw_zmalloc(priv_cmd.total_len+1);
663 	if (!command) {
664 		RTW_INFO("%s: failed to allocate memory\n", __FUNCTION__);
665 		ret = -ENOMEM;
666 		goto exit;
667 	}
668 	#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
669 	if (!access_ok(priv_cmd.buf, priv_cmd.total_len)) {
670 	#else
671 	if (!access_ok(VERIFY_READ, priv_cmd.buf, priv_cmd.total_len)) {
672 	#endif
673 		RTW_INFO("%s: failed to access memory\n", __FUNCTION__);
674 		ret = -EFAULT;
675 		goto exit;
676 	}
677 	if (copy_from_user(command, (void *)priv_cmd.buf, priv_cmd.total_len)) {
678 		ret = -EFAULT;
679 		goto exit;
680 	}
681 	command[priv_cmd.total_len] = '\0';
682 	RTW_INFO("%s: Android private cmd \"%s\" on %s\n"
683 		 , __FUNCTION__, command, ifr->ifr_name);
684 
685 	cmd_num = rtw_android_cmdstr_to_num(command);
686 
687 	switch (cmd_num) {
688 	case ANDROID_WIFI_CMD_START:
689 		/* bytes_written = wl_android_wifi_on(net); */
690 		goto response;
691 	case ANDROID_WIFI_CMD_SETFWPATH:
692 		goto response;
693 	}
694 
695 	if (!g_wifi_on) {
696 		RTW_INFO("%s: Ignore private cmd \"%s\" - iface %s is down\n"
697 			 , __FUNCTION__, command, ifr->ifr_name);
698 		ret = 0;
699 		goto exit;
700 	}
701 
702 	if (!hal_chk_wl_func(padapter, WL_FUNC_MIRACAST)) {
703 		switch (cmd_num) {
704 		case ANDROID_WIFI_CMD_WFD_ENABLE:
705 		case ANDROID_WIFI_CMD_WFD_DISABLE:
706 		case ANDROID_WIFI_CMD_WFD_SET_TCPPORT:
707 		case ANDROID_WIFI_CMD_WFD_SET_MAX_TPUT:
708 		case ANDROID_WIFI_CMD_WFD_SET_DEVTYPE:
709 			goto response;
710 		}
711 	}
712 
713 	switch (cmd_num) {
714 
715 	case ANDROID_WIFI_CMD_STOP:
716 		/* bytes_written = wl_android_wifi_off(net); */
717 		break;
718 
719 	case ANDROID_WIFI_CMD_SCAN_ACTIVE:
720 		/* rtw_set_scan_mode((_adapter *)rtw_netdev_priv(net), SCAN_ACTIVE); */
721 #ifdef CONFIG_PLATFORM_MSTAR
722 #ifdef CONFIG_IOCTL_CFG80211
723 		adapter_wdev_data((_adapter *)rtw_netdev_priv(net))->bandroid_scan = _TRUE;
724 #endif /* CONFIG_IOCTL_CFG80211 */
725 #endif /* CONFIG_PLATFORM_MSTAR */
726 		break;
727 	case ANDROID_WIFI_CMD_SCAN_PASSIVE:
728 		/* rtw_set_scan_mode((_adapter *)rtw_netdev_priv(net), SCAN_PASSIVE); */
729 		break;
730 
731 	case ANDROID_WIFI_CMD_RSSI:
732 		bytes_written = rtw_android_get_rssi(net, command, priv_cmd.total_len);
733 		break;
734 	case ANDROID_WIFI_CMD_LINKSPEED:
735 		bytes_written = rtw_android_get_link_speed(net, command, priv_cmd.total_len);
736 		break;
737 
738 	case ANDROID_WIFI_CMD_MACADDR:
739 		bytes_written = rtw_android_get_macaddr(net, command, priv_cmd.total_len);
740 		break;
741 
742 	case ANDROID_WIFI_CMD_BLOCK_SCAN:
743 		bytes_written = rtw_android_set_block_scan(net, command, priv_cmd.total_len);
744 		break;
745 
746 	case ANDROID_WIFI_CMD_BLOCK:
747 		bytes_written = rtw_android_set_block(net, command, priv_cmd.total_len);
748 		break;
749 
750 	case ANDROID_WIFI_CMD_RXFILTER_START:
751 		/* bytes_written = net_os_set_packet_filter(net, 1); */
752 		break;
753 	case ANDROID_WIFI_CMD_RXFILTER_STOP:
754 		/* bytes_written = net_os_set_packet_filter(net, 0); */
755 		break;
756 	case ANDROID_WIFI_CMD_RXFILTER_ADD:
757 		/* int filter_num = *(command + strlen(CMD_RXFILTER_ADD) + 1) - '0'; */
758 		/* bytes_written = net_os_rxfilter_add_remove(net, TRUE, filter_num); */
759 		break;
760 	case ANDROID_WIFI_CMD_RXFILTER_REMOVE:
761 		/* int filter_num = *(command + strlen(CMD_RXFILTER_REMOVE) + 1) - '0'; */
762 		/* bytes_written = net_os_rxfilter_add_remove(net, FALSE, filter_num); */
763 		break;
764 
765 	case ANDROID_WIFI_CMD_BTCOEXSCAN_START:
766 		/* TBD: BTCOEXSCAN-START */
767 		break;
768 	case ANDROID_WIFI_CMD_BTCOEXSCAN_STOP:
769 		/* TBD: BTCOEXSCAN-STOP */
770 		break;
771 	case ANDROID_WIFI_CMD_BTCOEXMODE:
772 #if 0
773 		uint mode = *(command + strlen(CMD_BTCOEXMODE) + 1) - '0';
774 		if (mode == 1)
775 			net_os_set_packet_filter(net, 0); /* DHCP starts */
776 		else
777 			net_os_set_packet_filter(net, 1); /* DHCP ends */
778 #ifdef WL_CFG80211
779 		bytes_written = wl_cfg80211_set_btcoex_dhcp(net, command);
780 #endif
781 #endif
782 		break;
783 
784 	case ANDROID_WIFI_CMD_SETSUSPENDMODE:
785 		break;
786 
787 	case ANDROID_WIFI_CMD_SETSUSPENDOPT:
788 		/* bytes_written = wl_android_set_suspendopt(net, command, priv_cmd.total_len); */
789 		break;
790 
791 	case ANDROID_WIFI_CMD_SETBAND:
792 		bytes_written = rtw_android_setband(net, command, priv_cmd.total_len);
793 		break;
794 
795 	case ANDROID_WIFI_CMD_GETBAND:
796 		bytes_written = rtw_android_getband(net, command, priv_cmd.total_len);
797 		break;
798 
799 	case ANDROID_WIFI_CMD_COUNTRY:
800 		bytes_written = rtw_android_set_country(net, command, priv_cmd.total_len);
801 		break;
802 
803 #ifdef CONFIG_PNO_SUPPORT
804 	case ANDROID_WIFI_CMD_PNOSSIDCLR_SET:
805 		/* bytes_written = dhd_dev_pno_reset(net); */
806 		break;
807 	case ANDROID_WIFI_CMD_PNOSETUP_SET:
808 		bytes_written = rtw_android_pno_setup(net, command, priv_cmd.total_len);
809 		break;
810 	case ANDROID_WIFI_CMD_PNOENABLE_SET:
811 		cmdlen = strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_PNOENABLE_SET]);
812 		pno_enable = *(command + cmdlen + 1) - '0';
813 		bytes_written = rtw_android_pno_enable(net, pno_enable);
814 		break;
815 #endif
816 
817 	case ANDROID_WIFI_CMD_P2P_DEV_ADDR:
818 		bytes_written = rtw_android_get_p2p_dev_addr(net, command, priv_cmd.total_len);
819 		break;
820 	case ANDROID_WIFI_CMD_P2P_SET_NOA:
821 		/* int skip = strlen(CMD_P2P_SET_NOA) + 1; */
822 		/* bytes_written = wl_cfg80211_set_p2p_noa(net, command + skip, priv_cmd.total_len - skip); */
823 		break;
824 	case ANDROID_WIFI_CMD_P2P_GET_NOA:
825 		/* bytes_written = wl_cfg80211_get_p2p_noa(net, command, priv_cmd.total_len); */
826 		break;
827 	case ANDROID_WIFI_CMD_P2P_SET_PS:
828 		/* int skip = strlen(CMD_P2P_SET_PS) + 1; */
829 		/* bytes_written = wl_cfg80211_set_p2p_ps(net, command + skip, priv_cmd.total_len - skip); */
830 		break;
831 
832 #ifdef CONFIG_IOCTL_CFG80211
833 	#ifdef CONFIG_AP_MODE
834 	case ANDROID_WIFI_CMD_SET_AP_WPS_P2P_IE: {
835 		int skip = strlen(android_wifi_cmd_str[ANDROID_WIFI_CMD_SET_AP_WPS_P2P_IE]) + 3;
836 		bytes_written = rtw_cfg80211_set_mgnt_wpsp2pie(net, command + skip, priv_cmd.total_len - skip, *(command + skip - 2) - '0');
837 		adapter_to_dvobj(padapter)->wpas_type = RTW_WPAS_ANDROID;
838 		break;
839 	}
840 	#endif
841 #endif /* CONFIG_IOCTL_CFG80211 */
842 
843 #ifdef CONFIG_WFD
844 
845 	case ANDROID_WIFI_CMD_MIRACAST:
846 		bytes_written = rtw_android_set_miracast_mode(net, command, priv_cmd.total_len);
847 		break;
848 
849 	case ANDROID_WIFI_CMD_WFD_ENABLE: {
850 		/*	Commented by Albert 2012/07/24 */
851 		/*	We can enable the WFD function by using the following command: */
852 		/*	wpa_cli driver wfd-enable */
853 
854 		if (padapter->wdinfo.driver_interface == DRIVER_CFG80211)
855 			rtw_wfd_enable(padapter, 1);
856 		break;
857 	}
858 
859 	case ANDROID_WIFI_CMD_WFD_DISABLE: {
860 		/*	Commented by Albert 2012/07/24 */
861 		/*	We can disable the WFD function by using the following command: */
862 		/*	wpa_cli driver wfd-disable */
863 
864 		if (padapter->wdinfo.driver_interface == DRIVER_CFG80211)
865 			rtw_wfd_enable(padapter, 0);
866 		break;
867 	}
868 	case ANDROID_WIFI_CMD_WFD_SET_TCPPORT: {
869 		/*	Commented by Albert 2012/07/24 */
870 		/*	We can set the tcp port number by using the following command: */
871 		/*	wpa_cli driver wfd-set-tcpport = 554 */
872 
873 		if (padapter->wdinfo.driver_interface == DRIVER_CFG80211)
874 			rtw_wfd_set_ctrl_port(padapter, (u16)get_int_from_command(command));
875 		break;
876 	}
877 	case ANDROID_WIFI_CMD_WFD_SET_MAX_TPUT: {
878 		break;
879 	}
880 	case ANDROID_WIFI_CMD_WFD_SET_DEVTYPE: {
881 		/*	Commented by Albert 2012/08/28 */
882 		/*	Specify the WFD device type ( WFD source/primary sink ) */
883 
884 		pwfd_info = &padapter->wfd_info;
885 		if (padapter->wdinfo.driver_interface == DRIVER_CFG80211) {
886 			pwfd_info->wfd_device_type = (u8) get_int_from_command(command);
887 			pwfd_info->wfd_device_type &= WFD_DEVINFO_DUAL;
888 		}
889 		break;
890 	}
891 #endif
892 	case ANDROID_WIFI_CMD_CHANGE_DTIM: {
893 #ifdef CONFIG_LPS
894 		u8 dtim;
895 		u8 *ptr = (u8 *) command;
896 
897 		ptr += 9;/* string command length of  "SET_DTIM"; */
898 
899 		dtim = rtw_atoi(ptr);
900 
901 		RTW_INFO("DTIM=%d\n", dtim);
902 
903 		rtw_lps_change_dtim_cmd(padapter, dtim);
904 #endif
905 	}
906 	break;
907 
908 #if CONFIG_RTW_MACADDR_ACL
909 	case ANDROID_WIFI_CMD_HOSTAPD_SET_MACADDR_ACL: {
910 		rtw_set_macaddr_acl(padapter, RTW_ACL_PERIOD_BSS, get_int_from_command(command));
911 		break;
912 	}
913 	case ANDROID_WIFI_CMD_HOSTAPD_ACL_ADD_STA: {
914 		u8 addr[ETH_ALEN] = {0x00};
915 		macstr2num(addr, command + strlen("HOSTAPD_ACL_ADD_STA") + 3);	/* 3 is space bar + "=" + space bar these 3 chars */
916 		rtw_acl_add_sta(padapter, RTW_ACL_PERIOD_BSS, addr);
917 		break;
918 	}
919 	case ANDROID_WIFI_CMD_HOSTAPD_ACL_REMOVE_STA: {
920 		u8 addr[ETH_ALEN] = {0x00};
921 		macstr2num(addr, command + strlen("HOSTAPD_ACL_REMOVE_STA") + 3);	/* 3 is space bar + "=" + space bar these 3 chars */
922 		rtw_acl_remove_sta(padapter, RTW_ACL_PERIOD_BSS, addr);
923 		break;
924 	}
925 #endif /* CONFIG_RTW_MACADDR_ACL */
926 #if defined(CONFIG_GTK_OL) && (LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0))
927 	case ANDROID_WIFI_CMD_GTK_REKEY_OFFLOAD:
928 		rtw_gtk_offload(net, (u8 *)command);
929 		break;
930 #endif /* CONFIG_GTK_OL		 */
931 	case ANDROID_WIFI_CMD_P2P_DISABLE: {
932 #ifdef CONFIG_P2P
933 		rtw_p2p_enable(padapter, P2P_ROLE_DISABLE);
934 #endif /* CONFIG_P2P */
935 		break;
936 	}
937 
938 #ifdef CONFIG_RTW_MESH_AEK
939 	case ANDROID_WIFI_CMD_SET_AEK:
940 		bytes_written = rtw_android_set_aek(net, command, priv_cmd.total_len);
941 		break;
942 #endif
943 
944 	case ANDROID_WIFI_CMD_EXT_AUTH_STATUS: {
945 		rtw_set_external_auth_status(padapter,
946 			command + strlen("EXT_AUTH_STATUS "),
947 			priv_cmd.total_len - strlen("EXT_AUTH_STATUS "));
948 		break;
949 	}
950 	case ANDROID_WIFI_CMD_DRIVERVERSION: {
951 		bytes_written = strlen(DRIVERVERSION);
952 		snprintf(command, bytes_written + 1, DRIVERVERSION);
953 		break;
954 	}
955 	default:
956 		RTW_INFO("Unknown PRIVATE command %s - ignored\n", command);
957 		snprintf(command, 3, "OK");
958 		bytes_written = strlen("OK");
959 	}
960 
961 response:
962 	if (bytes_written >= 0) {
963 		if ((bytes_written == 0) && (priv_cmd.total_len > 0))
964 			command[0] = '\0';
965 		if (bytes_written >= priv_cmd.total_len) {
966 			RTW_INFO("%s: bytes_written = %d\n", __FUNCTION__, bytes_written);
967 			bytes_written = priv_cmd.total_len;
968 		} else
969 			bytes_written++;
970 		priv_cmd.used_len = bytes_written;
971 		if (copy_to_user((void *)priv_cmd.buf, command, bytes_written)) {
972 			RTW_INFO("%s: failed to copy data to user buffer\n", __FUNCTION__);
973 			ret = -EFAULT;
974 		}
975 	} else
976 		ret = bytes_written;
977 
978 exit:
979 	rtw_unlock_suspend();
980 	if (command)
981 		rtw_mfree(command, priv_cmd.total_len + 1);
982 
983 	return ret;
984 }
985 
986 
987 /**
988  * Functions for Android WiFi card detection
989  */
990 #if defined(RTW_ENABLE_WIFI_CONTROL_FUNC)
991 
992 static int g_wifidev_registered = 0;
993 static struct semaphore wifi_control_sem;
994 static struct wifi_platform_data *wifi_control_data = NULL;
995 static struct resource *wifi_irqres = NULL;
996 
997 static int wifi_add_dev(void);
998 static void wifi_del_dev(void);
999 
1000 int rtw_android_wifictrl_func_add(void)
1001 {
1002 	int ret = 0;
1003 	sema_init(&wifi_control_sem, 0);
1004 
1005 	ret = wifi_add_dev();
1006 	if (ret) {
1007 		RTW_INFO("%s: platform_driver_register failed\n", __FUNCTION__);
1008 		return ret;
1009 	}
1010 	g_wifidev_registered = 1;
1011 
1012 	/* Waiting callback after platform_driver_register is done or exit with error */
1013 	if (down_timeout(&wifi_control_sem,  msecs_to_jiffies(1000)) != 0) {
1014 		ret = -EINVAL;
1015 		RTW_INFO("%s: platform_driver_register timeout\n", __FUNCTION__);
1016 	}
1017 
1018 	return ret;
1019 }
1020 
1021 void rtw_android_wifictrl_func_del(void)
1022 {
1023 	if (g_wifidev_registered) {
1024 		wifi_del_dev();
1025 		g_wifidev_registered = 0;
1026 	}
1027 }
1028 
1029 void *wl_android_prealloc(int section, unsigned long size)
1030 {
1031 	void *alloc_ptr = NULL;
1032 	if (wifi_control_data && wifi_control_data->mem_prealloc) {
1033 		alloc_ptr = wifi_control_data->mem_prealloc(section, size);
1034 		if (alloc_ptr) {
1035 			RTW_INFO("success alloc section %d\n", section);
1036 			if (size != 0L)
1037 				memset(alloc_ptr, 0, size);
1038 			return alloc_ptr;
1039 		}
1040 	}
1041 
1042 	RTW_INFO("can't alloc section %d\n", section);
1043 	return NULL;
1044 }
1045 
1046 int wifi_get_irq_number(unsigned long *irq_flags_ptr)
1047 {
1048 	if (wifi_irqres) {
1049 		*irq_flags_ptr = wifi_irqres->flags & IRQF_TRIGGER_MASK;
1050 		return (int)wifi_irqres->start;
1051 	}
1052 #ifdef CUSTOM_OOB_GPIO_NUM
1053 	return CUSTOM_OOB_GPIO_NUM;
1054 #else
1055 	return -1;
1056 #endif
1057 }
1058 
1059 int wifi_set_power(int on, unsigned long msec)
1060 {
1061 	RTW_INFO("%s = %d\n", __FUNCTION__, on);
1062 	if (wifi_control_data && wifi_control_data->set_power)
1063 		wifi_control_data->set_power(on);
1064 	if (msec)
1065 		msleep(msec);
1066 	return 0;
1067 }
1068 
1069 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
1070 int wifi_get_mac_addr(unsigned char *buf)
1071 {
1072 	RTW_INFO("%s\n", __FUNCTION__);
1073 	if (!buf)
1074 		return -EINVAL;
1075 	if (wifi_control_data && wifi_control_data->get_mac_addr)
1076 		return wifi_control_data->get_mac_addr(buf);
1077 	return -EOPNOTSUPP;
1078 }
1079 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)) */
1080 
1081 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) || defined(COMPAT_KERNEL_RELEASE)
1082 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
1083 void *wifi_get_country_code(char *ccode, u32 flags)
1084 #else /* Linux kernel < 3.18 */
1085 void *wifi_get_country_code(char *ccode)
1086 #endif /* Linux kernel < 3.18 */
1087 {
1088 	RTW_INFO("%s\n", __FUNCTION__);
1089 	if (!ccode)
1090 		return NULL;
1091 	if (wifi_control_data && wifi_control_data->get_country_code)
1092 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
1093 		return wifi_control_data->get_country_code(ccode, flags);
1094 #else /* Linux kernel < 3.18 */
1095 		return wifi_control_data->get_country_code(ccode);
1096 #endif /* Linux kernel < 3.18 */
1097 	return NULL;
1098 }
1099 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) */
1100 
1101 static int wifi_set_carddetect(int on)
1102 {
1103 	RTW_INFO("%s = %d\n", __FUNCTION__, on);
1104 	if (wifi_control_data && wifi_control_data->set_carddetect)
1105 		wifi_control_data->set_carddetect(on);
1106 	return 0;
1107 }
1108 
1109 static int wifi_probe(struct platform_device *pdev)
1110 {
1111 	struct wifi_platform_data *wifi_ctrl =
1112 		(struct wifi_platform_data *)(pdev->dev.platform_data);
1113 	int wifi_wake_gpio = 0;
1114 
1115 	RTW_INFO("## %s\n", __FUNCTION__);
1116 	wifi_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "bcmdhd_wlan_irq");
1117 
1118 	if (wifi_irqres == NULL)
1119 		wifi_irqres = platform_get_resource_byname(pdev,
1120 				IORESOURCE_IRQ, "bcm4329_wlan_irq");
1121 	else
1122 		wifi_wake_gpio = wifi_irqres->start;
1123 
1124 #ifdef CONFIG_GPIO_WAKEUP
1125 	RTW_INFO("%s: gpio:%d wifi_wake_gpio:%d\n", __func__,
1126 	       (int)wifi_irqres->start, wifi_wake_gpio);
1127 
1128 	if (wifi_wake_gpio > 0) {
1129 #ifdef CONFIG_PLATFORM_INTEL_BYT
1130 		wifi_configure_gpio();
1131 #else /* CONFIG_PLATFORM_INTEL_BYT */
1132 		gpio_request(wifi_wake_gpio, "oob_irq");
1133 		gpio_direction_input(wifi_wake_gpio);
1134 		oob_irq = gpio_to_irq(wifi_wake_gpio);
1135 #endif /* CONFIG_PLATFORM_INTEL_BYT */
1136 		RTW_INFO("%s oob_irq:%d\n", __func__, oob_irq);
1137 	} else if (wifi_irqres) {
1138 		oob_irq = wifi_irqres->start;
1139 		RTW_INFO("%s oob_irq:%d\n", __func__, oob_irq);
1140 	}
1141 #endif
1142 	wifi_control_data = wifi_ctrl;
1143 
1144 	wifi_set_power(1, 0);	/* Power On */
1145 	wifi_set_carddetect(1);	/* CardDetect (0->1) */
1146 
1147 	up(&wifi_control_sem);
1148 	return 0;
1149 }
1150 
1151 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
1152 extern PADAPTER g_test_adapter;
1153 
1154 static void shutdown_card(void)
1155 {
1156 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(g_test_adapter);
1157 	u32 addr;
1158 	u8 tmp8, cnt = 0;
1159 
1160 	if (NULL == g_test_adapter) {
1161 		RTW_INFO("%s: padapter==NULL\n", __FUNCTION__);
1162 		return;
1163 	}
1164 
1165 #ifdef CONFIG_FWLPS_IN_IPS
1166 	LeaveAllPowerSaveMode(g_test_adapter);
1167 #endif /* CONFIG_FWLPS_IN_IPS */
1168 
1169 #ifdef CONFIG_WOWLAN
1170 #ifdef CONFIG_GPIO_WAKEUP
1171 	/*default wake up pin change to BT*/
1172 	RTW_INFO("%s:default wake up pin change to BT\n", __FUNCTION__);
1173 	rtw_hal_switch_gpio_wl_ctrl(g_test_adapter, pwrpriv->wowlan_gpio_index, _FALSE);
1174 #endif /* CONFIG_GPIO_WAKEUP */
1175 #endif /* CONFIG_WOWLAN */
1176 
1177 	/* Leave SDIO HCI Suspend */
1178 	addr = 0x10250086;
1179 	rtw_write8(g_test_adapter, addr, 0);
1180 	do {
1181 		tmp8 = rtw_read8(g_test_adapter, addr);
1182 		cnt++;
1183 		RTW_INFO(FUNC_ADPT_FMT ": polling SDIO_HSUS_CTRL(0x%x)=0x%x, cnt=%d\n",
1184 			 FUNC_ADPT_ARG(g_test_adapter), addr, tmp8, cnt);
1185 
1186 		if (tmp8 & BIT(1))
1187 			break;
1188 
1189 		if (cnt >= 100) {
1190 			RTW_INFO(FUNC_ADPT_FMT ": polling 0x%x[1]==1 FAIL!!\n",
1191 				 FUNC_ADPT_ARG(g_test_adapter), addr);
1192 			break;
1193 		}
1194 
1195 		rtw_mdelay_os(10);
1196 	} while (1);
1197 
1198 	/* unlock register I/O */
1199 	rtw_write8(g_test_adapter, 0x1C, 0);
1200 
1201 	/* enable power down function */
1202 	/* 0x04[4] = 1 */
1203 	/* 0x05[7] = 1 */
1204 	addr = 0x04;
1205 	tmp8 = rtw_read8(g_test_adapter, addr);
1206 	tmp8 |= BIT(4);
1207 	rtw_write8(g_test_adapter, addr, tmp8);
1208 	RTW_INFO(FUNC_ADPT_FMT ": read after write 0x%x=0x%x\n",
1209 		FUNC_ADPT_ARG(g_test_adapter), addr, rtw_read8(g_test_adapter, addr));
1210 
1211 	addr = 0x05;
1212 	tmp8 = rtw_read8(g_test_adapter, addr);
1213 	tmp8 |= BIT(7);
1214 	rtw_write8(g_test_adapter, addr, tmp8);
1215 	RTW_INFO(FUNC_ADPT_FMT ": read after write 0x%x=0x%x\n",
1216 		FUNC_ADPT_ARG(g_test_adapter), addr, rtw_read8(g_test_adapter, addr));
1217 
1218 	/* lock register page0 0x0~0xB read/write */
1219 	rtw_write8(g_test_adapter, 0x1C, 0x0E);
1220 
1221 	rtw_set_surprise_removed(g_test_adapter);
1222 	RTW_INFO(FUNC_ADPT_FMT ": bSurpriseRemoved=%s\n",
1223 		FUNC_ADPT_ARG(g_test_adapter), rtw_is_surprise_removed(g_test_adapter) ? "True" : "False");
1224 }
1225 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
1226 
1227 static int wifi_remove(struct platform_device *pdev)
1228 {
1229 	struct wifi_platform_data *wifi_ctrl =
1230 		(struct wifi_platform_data *)(pdev->dev.platform_data);
1231 
1232 	RTW_INFO("## %s\n", __FUNCTION__);
1233 	wifi_control_data = wifi_ctrl;
1234 
1235 	wifi_set_power(0, 0);	/* Power Off */
1236 	wifi_set_carddetect(0);	/* CardDetect (1->0) */
1237 
1238 	up(&wifi_control_sem);
1239 	return 0;
1240 }
1241 
1242 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
1243 static void wifi_shutdown(struct platform_device *pdev)
1244 {
1245 	struct wifi_platform_data *wifi_ctrl =
1246 		(struct wifi_platform_data *)(pdev->dev.platform_data);
1247 
1248 
1249 	RTW_INFO("## %s\n", __FUNCTION__);
1250 
1251 	wifi_control_data = wifi_ctrl;
1252 
1253 	shutdown_card();
1254 	wifi_set_power(0, 0);	/* Power Off */
1255 	wifi_set_carddetect(0);	/* CardDetect (1->0) */
1256 }
1257 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
1258 
1259 static int wifi_suspend(struct platform_device *pdev, pm_message_t state)
1260 {
1261 	RTW_INFO("##> %s\n", __FUNCTION__);
1262 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY)
1263 	bcmsdh_oob_intr_set(0);
1264 #endif
1265 	return 0;
1266 }
1267 
1268 static int wifi_resume(struct platform_device *pdev)
1269 {
1270 	RTW_INFO("##> %s\n", __FUNCTION__);
1271 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY)
1272 	if (dhd_os_check_if_up(bcmsdh_get_drvdata()))
1273 		bcmsdh_oob_intr_set(1);
1274 #endif
1275 	return 0;
1276 }
1277 
1278 /* temporarily use these two */
1279 static struct platform_driver wifi_device = {
1280 	.probe          = wifi_probe,
1281 	.remove         = wifi_remove,
1282 	.suspend        = wifi_suspend,
1283 	.resume         = wifi_resume,
1284 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
1285 	.shutdown       = wifi_shutdown,
1286 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
1287 	.driver         = {
1288 		.name   = "bcmdhd_wlan",
1289 	}
1290 };
1291 
1292 static struct platform_driver wifi_device_legacy = {
1293 	.probe          = wifi_probe,
1294 	.remove         = wifi_remove,
1295 	.suspend        = wifi_suspend,
1296 	.resume         = wifi_resume,
1297 	.driver         = {
1298 		.name   = "bcm4329_wlan",
1299 	}
1300 };
1301 
1302 static int wifi_add_dev(void)
1303 {
1304 	RTW_INFO("## Calling platform_driver_register\n");
1305 	platform_driver_register(&wifi_device);
1306 	platform_driver_register(&wifi_device_legacy);
1307 	return 0;
1308 }
1309 
1310 static void wifi_del_dev(void)
1311 {
1312 	RTW_INFO("## Unregister platform_driver_register\n");
1313 	platform_driver_unregister(&wifi_device);
1314 	platform_driver_unregister(&wifi_device_legacy);
1315 }
1316 #endif /* defined(RTW_ENABLE_WIFI_CONTROL_FUNC) */
1317 
1318 #ifdef CONFIG_GPIO_WAKEUP
1319 #ifdef CONFIG_PLATFORM_INTEL_BYT
1320 int wifi_configure_gpio(void)
1321 {
1322 	if (gpio_request(oob_gpio, "oob_irq")) {
1323 		RTW_INFO("## %s Cannot request GPIO\n", __FUNCTION__);
1324 		return -1;
1325 	}
1326 	gpio_export(oob_gpio, 0);
1327 	if (gpio_direction_input(oob_gpio)) {
1328 		RTW_INFO("## %s Cannot set GPIO direction input\n", __FUNCTION__);
1329 		return -1;
1330 	}
1331 	oob_irq = gpio_to_irq(oob_gpio);
1332 	if (oob_irq < 0) {
1333 		RTW_INFO("## %s Cannot convert GPIO to IRQ\n", __FUNCTION__);
1334 		return -1;
1335 	}
1336 
1337 	RTW_INFO("## %s OOB_IRQ=%d\n", __FUNCTION__, oob_irq);
1338 
1339 	return 0;
1340 }
1341 #endif /* CONFIG_PLATFORM_INTEL_BYT */
1342 void wifi_free_gpio(unsigned int gpio)
1343 {
1344 #ifdef CONFIG_PLATFORM_INTEL_BYT
1345 	if (gpio)
1346 		gpio_free(gpio);
1347 #endif /* CONFIG_PLATFORM_INTEL_BYT */
1348 }
1349 #endif /* CONFIG_GPIO_WAKEUP */
1350