rtw_android.c 35 KB

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