PageRenderTime 59ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/wireless/bcmdhd/dhd_common.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 2451 lines | 1878 code | 381 blank | 192 comment | 381 complexity | b6c74bc5217f61cccb221d0bccae9147 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Broadcom Dongle Host Driver (DHD), common DHD core.
  3. *
  4. * Copyright (C) 1999-2011, Broadcom Corporation
  5. *
  6. * Unless you and Broadcom execute a separate written software license
  7. * agreement governing use of this software, this software is licensed to you
  8. * under the terms of the GNU General Public License version 2 (the "GPL"),
  9. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  10. * following added to such license:
  11. *
  12. * As a special exception, the copyright holders of this software give you
  13. * permission to link this software with independent modules, and to copy and
  14. * distribute the resulting executable under terms of your choice, provided that
  15. * you also meet, for each linked independent module, the terms and conditions of
  16. * the license of that module. An independent module is a module which is not
  17. * derived from this software. The special exception does not apply to any
  18. * modifications of the software.
  19. *
  20. * Notwithstanding the above, under no circumstances may you combine this
  21. * software in any way with any other Broadcom software provided under a license
  22. * other than the GPL, without Broadcom's express prior written consent.
  23. *
  24. * $Id: dhd_common.c 331276 2012-05-04 08:05:57Z $
  25. */
  26. #include <typedefs.h>
  27. #include <osl.h>
  28. #include <epivers.h>
  29. #include <bcmutils.h>
  30. #include <bcmendian.h>
  31. #include <dngl_stats.h>
  32. #include <wlioctl.h>
  33. #include <dhd.h>
  34. #include <proto/bcmevent.h>
  35. #include <dhd_bus.h>
  36. #include <dhd_proto.h>
  37. #include <dhd_dbg.h>
  38. #include <msgtrace.h>
  39. #ifdef WL_CFG80211
  40. #include <wl_cfg80211.h>
  41. #endif
  42. #include <proto/bt_amp_hci.h>
  43. #include <dhd_bta.h>
  44. #ifdef SET_RANDOM_MAC_SOFTAP
  45. #include <linux/random.h>
  46. #include <linux/jiffies.h>
  47. #endif
  48. #ifdef PROP_TXSTATUS
  49. #include <wlfc_proto.h>
  50. #include <dhd_wlfc.h>
  51. #endif
  52. #ifdef WLMEDIA_HTSF
  53. extern void htsf_update(struct dhd_info *dhd, void *data);
  54. #endif
  55. int dhd_msg_level = DHD_ERROR_VAL;
  56. #include <wl_iw.h>
  57. char fw_path[MOD_PARAM_PATHLEN];
  58. char nv_path[MOD_PARAM_PATHLEN];
  59. #ifdef SOFTAP
  60. char fw_path2[MOD_PARAM_PATHLEN];
  61. extern bool softap_enabled;
  62. #endif
  63. /* Last connection success/failure status */
  64. uint32 dhd_conn_event;
  65. uint32 dhd_conn_status;
  66. uint32 dhd_conn_reason;
  67. #define htod32(i) i
  68. #define htod16(i) i
  69. #define dtoh32(i) i
  70. #define dtoh16(i) i
  71. extern int dhd_iscan_request(void * dhdp, uint16 action);
  72. extern void dhd_ind_scan_confirm(void *h, bool status);
  73. extern int dhd_iscan_in_progress(void *h);
  74. void dhd_iscan_lock(void);
  75. void dhd_iscan_unlock(void);
  76. extern int dhd_change_mtu(dhd_pub_t *dhd, int new_mtu, int ifidx);
  77. bool ap_cfg_running = FALSE;
  78. bool ap_fw_loaded = FALSE;
  79. #ifdef DHD_DEBUG
  80. const char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR "\nCompiled on "
  81. __DATE__ " at " __TIME__;
  82. #else
  83. const char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR;
  84. #endif
  85. void dhd_set_timer(void *bus, uint wdtick);
  86. /* IOVar table */
  87. enum {
  88. IOV_VERSION = 1,
  89. IOV_MSGLEVEL,
  90. IOV_BCMERRORSTR,
  91. IOV_BCMERROR,
  92. IOV_WDTICK,
  93. IOV_DUMP,
  94. IOV_CLEARCOUNTS,
  95. IOV_LOGDUMP,
  96. IOV_LOGCAL,
  97. IOV_LOGSTAMP,
  98. IOV_GPIOOB,
  99. IOV_IOCTLTIMEOUT,
  100. IOV_HCI_CMD, /* HCI command */
  101. IOV_HCI_ACL_DATA, /* HCI data packet */
  102. #if defined(DHD_DEBUG)
  103. IOV_CONS,
  104. IOV_DCONSOLE_POLL,
  105. #endif /* defined(DHD_DEBUG) */
  106. #ifdef PROP_TXSTATUS
  107. IOV_PROPTXSTATUS_ENABLE,
  108. IOV_PROPTXSTATUS_MODE,
  109. #endif
  110. IOV_BUS_TYPE,
  111. #ifdef WLMEDIA_HTSF
  112. IOV_WLPKTDLYSTAT_SZ,
  113. #endif
  114. IOV_CHANGEMTU,
  115. IOV_LAST
  116. };
  117. const bcm_iovar_t dhd_iovars[] = {
  118. {"version", IOV_VERSION, 0, IOVT_BUFFER, sizeof(dhd_version) },
  119. #ifdef DHD_DEBUG
  120. {"msglevel", IOV_MSGLEVEL, 0, IOVT_UINT32, 0 },
  121. #endif /* DHD_DEBUG */
  122. {"bcmerrorstr", IOV_BCMERRORSTR, 0, IOVT_BUFFER, BCME_STRLEN },
  123. {"bcmerror", IOV_BCMERROR, 0, IOVT_INT8, 0 },
  124. {"wdtick", IOV_WDTICK, 0, IOVT_UINT32, 0 },
  125. {"dump", IOV_DUMP, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN },
  126. #ifdef DHD_DEBUG
  127. {"cons", IOV_CONS, 0, IOVT_BUFFER, 0 },
  128. {"dconpoll", IOV_DCONSOLE_POLL, 0, IOVT_UINT32, 0 },
  129. #endif
  130. {"clearcounts", IOV_CLEARCOUNTS, 0, IOVT_VOID, 0 },
  131. {"gpioob", IOV_GPIOOB, 0, IOVT_UINT32, 0 },
  132. {"ioctl_timeout", IOV_IOCTLTIMEOUT, 0, IOVT_UINT32, 0 },
  133. {"HCI_cmd", IOV_HCI_CMD, 0, IOVT_BUFFER, 0},
  134. {"HCI_ACL_data", IOV_HCI_ACL_DATA, 0, IOVT_BUFFER, 0},
  135. #ifdef PROP_TXSTATUS
  136. {"proptx", IOV_PROPTXSTATUS_ENABLE, 0, IOVT_UINT32, 0 },
  137. /*
  138. set the proptxtstatus operation mode:
  139. 0 - Do not do any proptxtstatus flow control
  140. 1 - Use implied credit from a packet status
  141. 2 - Use explicit credit
  142. */
  143. {"ptxmode", IOV_PROPTXSTATUS_MODE, 0, IOVT_UINT32, 0 },
  144. #endif
  145. {"bustype", IOV_BUS_TYPE, 0, IOVT_UINT32, 0},
  146. #ifdef WLMEDIA_HTSF
  147. {"pktdlystatsz", IOV_WLPKTDLYSTAT_SZ, 0, IOVT_UINT8, 0 },
  148. #endif
  149. {"changemtu", IOV_CHANGEMTU, 0, IOVT_UINT32, 0 },
  150. {NULL, 0, 0, 0, 0 }
  151. };
  152. struct dhd_cmn *
  153. dhd_common_init(osl_t *osh)
  154. {
  155. dhd_cmn_t *cmn;
  156. /* Init global variables at run-time, not as part of the declaration.
  157. * This is required to support init/de-init of the driver. Initialization
  158. * of globals as part of the declaration results in non-deterministic
  159. * behavior since the value of the globals may be different on the
  160. * first time that the driver is initialized vs subsequent initializations.
  161. */
  162. /* Allocate private bus interface state */
  163. if (!(cmn = MALLOC(osh, sizeof(dhd_cmn_t)))) {
  164. DHD_ERROR(("%s: MALLOC failed\n", __FUNCTION__));
  165. return NULL;
  166. }
  167. memset(cmn, 0, sizeof(dhd_cmn_t));
  168. cmn->osh = osh;
  169. #ifdef CONFIG_BCMDHD_FW_PATH
  170. bcm_strncpy_s(fw_path, sizeof(fw_path), CONFIG_BCMDHD_FW_PATH, MOD_PARAM_PATHLEN-1);
  171. #else /* CONFIG_BCMDHD_FW_PATH */
  172. fw_path[0] = '\0';
  173. #endif /* CONFIG_BCMDHD_FW_PATH */
  174. #ifdef CONFIG_BCMDHD_NVRAM_PATH
  175. bcm_strncpy_s(nv_path, sizeof(nv_path), CONFIG_BCMDHD_NVRAM_PATH, MOD_PARAM_PATHLEN-1);
  176. #else /* CONFIG_BCMDHD_NVRAM_PATH */
  177. nv_path[0] = '\0';
  178. #endif /* CONFIG_BCMDHD_NVRAM_PATH */
  179. #ifdef SOFTAP
  180. fw_path2[0] = '\0';
  181. #endif
  182. return cmn;
  183. }
  184. void
  185. dhd_common_deinit(dhd_pub_t *dhd_pub, dhd_cmn_t *sa_cmn)
  186. {
  187. osl_t *osh;
  188. dhd_cmn_t *cmn;
  189. if (dhd_pub != NULL)
  190. cmn = dhd_pub->cmn;
  191. else
  192. cmn = sa_cmn;
  193. if (!cmn)
  194. return;
  195. osh = cmn->osh;
  196. if (dhd_pub != NULL)
  197. dhd_pub->cmn = NULL;
  198. MFREE(osh, cmn, sizeof(dhd_cmn_t));
  199. }
  200. static int
  201. dhd_dump(dhd_pub_t *dhdp, char *buf, int buflen)
  202. {
  203. char eabuf[ETHER_ADDR_STR_LEN];
  204. struct bcmstrbuf b;
  205. struct bcmstrbuf *strbuf = &b;
  206. bcm_binit(strbuf, buf, buflen);
  207. /* Base DHD info */
  208. bcm_bprintf(strbuf, "%s\n", dhd_version);
  209. bcm_bprintf(strbuf, "\n");
  210. bcm_bprintf(strbuf, "pub.up %d pub.txoff %d pub.busstate %d\n",
  211. dhdp->up, dhdp->txoff, dhdp->busstate);
  212. bcm_bprintf(strbuf, "pub.hdrlen %d pub.maxctl %d pub.rxsz %d\n",
  213. dhdp->hdrlen, dhdp->maxctl, dhdp->rxsz);
  214. bcm_bprintf(strbuf, "pub.iswl %d pub.drv_version %ld pub.mac %s\n",
  215. dhdp->iswl, dhdp->drv_version, bcm_ether_ntoa(&dhdp->mac, eabuf));
  216. bcm_bprintf(strbuf, "pub.bcmerror %d tickcnt %d\n", dhdp->bcmerror, dhdp->tickcnt);
  217. bcm_bprintf(strbuf, "dongle stats:\n");
  218. bcm_bprintf(strbuf, "tx_packets %ld tx_bytes %ld tx_errors %ld tx_dropped %ld\n",
  219. dhdp->dstats.tx_packets, dhdp->dstats.tx_bytes,
  220. dhdp->dstats.tx_errors, dhdp->dstats.tx_dropped);
  221. bcm_bprintf(strbuf, "rx_packets %ld rx_bytes %ld rx_errors %ld rx_dropped %ld\n",
  222. dhdp->dstats.rx_packets, dhdp->dstats.rx_bytes,
  223. dhdp->dstats.rx_errors, dhdp->dstats.rx_dropped);
  224. bcm_bprintf(strbuf, "multicast %ld\n", dhdp->dstats.multicast);
  225. bcm_bprintf(strbuf, "bus stats:\n");
  226. bcm_bprintf(strbuf, "tx_packets %ld tx_multicast %ld tx_errors %ld\n",
  227. dhdp->tx_packets, dhdp->tx_multicast, dhdp->tx_errors);
  228. bcm_bprintf(strbuf, "tx_ctlpkts %ld tx_ctlerrs %ld\n",
  229. dhdp->tx_ctlpkts, dhdp->tx_ctlerrs);
  230. bcm_bprintf(strbuf, "rx_packets %ld rx_multicast %ld rx_errors %ld \n",
  231. dhdp->rx_packets, dhdp->rx_multicast, dhdp->rx_errors);
  232. bcm_bprintf(strbuf, "rx_ctlpkts %ld rx_ctlerrs %ld rx_dropped %ld\n",
  233. dhdp->rx_ctlpkts, dhdp->rx_ctlerrs, dhdp->rx_dropped);
  234. bcm_bprintf(strbuf, "rx_readahead_cnt %ld tx_realloc %ld\n",
  235. dhdp->rx_readahead_cnt, dhdp->tx_realloc);
  236. bcm_bprintf(strbuf, "\n");
  237. /* Add any prot info */
  238. dhd_prot_dump(dhdp, strbuf);
  239. bcm_bprintf(strbuf, "\n");
  240. /* Add any bus info */
  241. dhd_bus_dump(dhdp, strbuf);
  242. return (!strbuf->size ? BCME_BUFTOOSHORT : 0);
  243. }
  244. int
  245. dhd_wl_ioctl_cmd(dhd_pub_t *dhd_pub, int cmd, void *arg, int len, uint8 set, int ifindex)
  246. {
  247. wl_ioctl_t ioc;
  248. ioc.cmd = cmd;
  249. ioc.buf = arg;
  250. ioc.len = len;
  251. ioc.set = set;
  252. return dhd_wl_ioctl(dhd_pub, ifindex, &ioc, arg, len);
  253. }
  254. int
  255. dhd_wl_ioctl(dhd_pub_t *dhd_pub, int ifindex, wl_ioctl_t *ioc, void *buf, int len)
  256. {
  257. int ret;
  258. dhd_os_proto_block(dhd_pub);
  259. ret = dhd_prot_ioctl(dhd_pub, ifindex, ioc, buf, len);
  260. if (ret)
  261. dhd_os_check_hang(dhd_pub, ifindex, ret);
  262. dhd_os_proto_unblock(dhd_pub);
  263. return ret;
  264. }
  265. static int
  266. dhd_doiovar(dhd_pub_t *dhd_pub, const bcm_iovar_t *vi, uint32 actionid, const char *name,
  267. void *params, int plen, void *arg, int len, int val_size)
  268. {
  269. int bcmerror = 0;
  270. int32 int_val = 0;
  271. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  272. DHD_TRACE(("%s: actionid = %d; name %s\n", __FUNCTION__, actionid, name));
  273. if ((bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid))) != 0)
  274. goto exit;
  275. if (plen >= (int)sizeof(int_val))
  276. bcopy(params, &int_val, sizeof(int_val));
  277. switch (actionid) {
  278. case IOV_GVAL(IOV_VERSION):
  279. /* Need to have checked buffer length */
  280. bcm_strncpy_s((char*)arg, len, dhd_version, len);
  281. break;
  282. case IOV_GVAL(IOV_MSGLEVEL):
  283. int_val = (int32)dhd_msg_level;
  284. bcopy(&int_val, arg, val_size);
  285. break;
  286. case IOV_SVAL(IOV_MSGLEVEL):
  287. dhd_msg_level = int_val;
  288. #ifdef WL_CFG80211
  289. /* Enable DHD and WL logs in oneshot */
  290. if (dhd_msg_level & DHD_WL_VAL)
  291. wl_cfg80211_enable_trace(dhd_msg_level);
  292. #endif
  293. break;
  294. case IOV_GVAL(IOV_BCMERRORSTR):
  295. bcm_strncpy_s((char *)arg, len, bcmerrorstr(dhd_pub->bcmerror), BCME_STRLEN);
  296. ((char *)arg)[BCME_STRLEN - 1] = 0x00;
  297. break;
  298. case IOV_GVAL(IOV_BCMERROR):
  299. int_val = (int32)dhd_pub->bcmerror;
  300. bcopy(&int_val, arg, val_size);
  301. break;
  302. case IOV_GVAL(IOV_WDTICK):
  303. int_val = (int32)dhd_watchdog_ms;
  304. bcopy(&int_val, arg, val_size);
  305. break;
  306. case IOV_SVAL(IOV_WDTICK):
  307. if (!dhd_pub->up) {
  308. bcmerror = BCME_NOTUP;
  309. break;
  310. }
  311. dhd_os_wd_timer(dhd_pub, (uint)int_val);
  312. break;
  313. case IOV_GVAL(IOV_DUMP):
  314. bcmerror = dhd_dump(dhd_pub, arg, len);
  315. break;
  316. #ifdef DHD_DEBUG
  317. case IOV_GVAL(IOV_DCONSOLE_POLL):
  318. int_val = (int32)dhd_console_ms;
  319. bcopy(&int_val, arg, val_size);
  320. break;
  321. case IOV_SVAL(IOV_DCONSOLE_POLL):
  322. dhd_console_ms = (uint)int_val;
  323. break;
  324. case IOV_SVAL(IOV_CONS):
  325. if (len > 0)
  326. bcmerror = dhd_bus_console_in(dhd_pub, arg, len - 1);
  327. break;
  328. #endif /* DHD_DEBUG */
  329. case IOV_SVAL(IOV_CLEARCOUNTS):
  330. dhd_pub->tx_packets = dhd_pub->rx_packets = 0;
  331. dhd_pub->tx_errors = dhd_pub->rx_errors = 0;
  332. dhd_pub->tx_ctlpkts = dhd_pub->rx_ctlpkts = 0;
  333. dhd_pub->tx_ctlerrs = dhd_pub->rx_ctlerrs = 0;
  334. dhd_pub->rx_dropped = 0;
  335. dhd_pub->rx_readahead_cnt = 0;
  336. dhd_pub->tx_realloc = 0;
  337. dhd_pub->wd_dpc_sched = 0;
  338. memset(&dhd_pub->dstats, 0, sizeof(dhd_pub->dstats));
  339. dhd_bus_clearcounts(dhd_pub);
  340. #ifdef PROP_TXSTATUS
  341. /* clear proptxstatus related counters */
  342. if (dhd_pub->wlfc_state) {
  343. athost_wl_status_info_t *wlfc =
  344. (athost_wl_status_info_t*)dhd_pub->wlfc_state;
  345. wlfc_hanger_t* hanger;
  346. memset(&wlfc->stats, 0, sizeof(athost_wl_stat_counters_t));
  347. hanger = (wlfc_hanger_t*)wlfc->hanger;
  348. hanger->pushed = 0;
  349. hanger->popped = 0;
  350. hanger->failed_slotfind = 0;
  351. hanger->failed_to_pop = 0;
  352. hanger->failed_to_push = 0;
  353. }
  354. #endif /* PROP_TXSTATUS */
  355. break;
  356. case IOV_GVAL(IOV_IOCTLTIMEOUT): {
  357. int_val = (int32)dhd_os_get_ioctl_resp_timeout();
  358. bcopy(&int_val, arg, sizeof(int_val));
  359. break;
  360. }
  361. case IOV_SVAL(IOV_IOCTLTIMEOUT): {
  362. if (int_val <= 0)
  363. bcmerror = BCME_BADARG;
  364. else
  365. dhd_os_set_ioctl_resp_timeout((unsigned int)int_val);
  366. break;
  367. }
  368. case IOV_SVAL(IOV_HCI_CMD): {
  369. amp_hci_cmd_t *cmd = (amp_hci_cmd_t *)arg;
  370. /* sanity check: command preamble present */
  371. if (len < HCI_CMD_PREAMBLE_SIZE)
  372. return BCME_BUFTOOSHORT;
  373. /* sanity check: command parameters are present */
  374. if (len < (int)(HCI_CMD_PREAMBLE_SIZE + cmd->plen))
  375. return BCME_BUFTOOSHORT;
  376. dhd_bta_docmd(dhd_pub, cmd, len);
  377. break;
  378. }
  379. case IOV_SVAL(IOV_HCI_ACL_DATA): {
  380. amp_hci_ACL_data_t *ACL_data = (amp_hci_ACL_data_t *)arg;
  381. /* sanity check: HCI header present */
  382. if (len < HCI_ACL_DATA_PREAMBLE_SIZE)
  383. return BCME_BUFTOOSHORT;
  384. /* sanity check: ACL data is present */
  385. if (len < (int)(HCI_ACL_DATA_PREAMBLE_SIZE + ACL_data->dlen))
  386. return BCME_BUFTOOSHORT;
  387. dhd_bta_tx_hcidata(dhd_pub, ACL_data, len);
  388. break;
  389. }
  390. #ifdef PROP_TXSTATUS
  391. case IOV_GVAL(IOV_PROPTXSTATUS_ENABLE):
  392. int_val = dhd_pub->wlfc_enabled? 1 : 0;
  393. bcopy(&int_val, arg, val_size);
  394. break;
  395. case IOV_SVAL(IOV_PROPTXSTATUS_ENABLE):
  396. dhd_pub->wlfc_enabled = int_val? 1 : 0;
  397. break;
  398. case IOV_GVAL(IOV_PROPTXSTATUS_MODE): {
  399. athost_wl_status_info_t *wlfc =
  400. (athost_wl_status_info_t*)dhd_pub->wlfc_state;
  401. int_val = dhd_pub->wlfc_state ? (int32)wlfc->proptxstatus_mode : 0;
  402. bcopy(&int_val, arg, val_size);
  403. break;
  404. }
  405. case IOV_SVAL(IOV_PROPTXSTATUS_MODE):
  406. if (dhd_pub->wlfc_state) {
  407. athost_wl_status_info_t *wlfc =
  408. (athost_wl_status_info_t*)dhd_pub->wlfc_state;
  409. wlfc->proptxstatus_mode = int_val & 0xff;
  410. }
  411. break;
  412. #endif /* PROP_TXSTATUS */
  413. case IOV_GVAL(IOV_BUS_TYPE):
  414. /* The dhd application queries the driver to check if its usb or sdio. */
  415. #ifdef BCMDHDUSB
  416. int_val = BUS_TYPE_USB;
  417. #endif
  418. int_val = BUS_TYPE_SDIO;
  419. bcopy(&int_val, arg, val_size);
  420. break;
  421. #ifdef WLMEDIA_HTSF
  422. case IOV_GVAL(IOV_WLPKTDLYSTAT_SZ):
  423. int_val = dhd_pub->htsfdlystat_sz;
  424. bcopy(&int_val, arg, val_size);
  425. break;
  426. case IOV_SVAL(IOV_WLPKTDLYSTAT_SZ):
  427. dhd_pub->htsfdlystat_sz = int_val & 0xff;
  428. printf("Setting tsfdlystat_sz:%d\n", dhd_pub->htsfdlystat_sz);
  429. break;
  430. #endif
  431. case IOV_SVAL(IOV_CHANGEMTU):
  432. int_val &= 0xffff;
  433. bcmerror = dhd_change_mtu(dhd_pub, int_val, 0);
  434. break;
  435. default:
  436. bcmerror = BCME_UNSUPPORTED;
  437. break;
  438. }
  439. exit:
  440. DHD_TRACE(("%s: actionid %d, bcmerror %d\n", __FUNCTION__, actionid, bcmerror));
  441. return bcmerror;
  442. }
  443. /* Store the status of a connection attempt for later retrieval by an iovar */
  444. void
  445. dhd_store_conn_status(uint32 event, uint32 status, uint32 reason)
  446. {
  447. /* Do not overwrite a WLC_E_PRUNE with a WLC_E_SET_SSID
  448. * because an encryption/rsn mismatch results in both events, and
  449. * the important information is in the WLC_E_PRUNE.
  450. */
  451. if (!(event == WLC_E_SET_SSID && status == WLC_E_STATUS_FAIL &&
  452. dhd_conn_event == WLC_E_PRUNE)) {
  453. dhd_conn_event = event;
  454. dhd_conn_status = status;
  455. dhd_conn_reason = reason;
  456. }
  457. }
  458. bool
  459. dhd_prec_enq(dhd_pub_t *dhdp, struct pktq *q, void *pkt, int prec)
  460. {
  461. void *p;
  462. int eprec = -1; /* precedence to evict from */
  463. bool discard_oldest;
  464. /* Fast case, precedence queue is not full and we are also not
  465. * exceeding total queue length
  466. */
  467. if (!pktq_pfull(q, prec) && !pktq_full(q)) {
  468. pktq_penq(q, prec, pkt);
  469. return TRUE;
  470. }
  471. /* Determine precedence from which to evict packet, if any */
  472. if (pktq_pfull(q, prec))
  473. eprec = prec;
  474. else if (pktq_full(q)) {
  475. p = pktq_peek_tail(q, &eprec);
  476. ASSERT(p);
  477. if (eprec > prec || eprec < 0)
  478. return FALSE;
  479. }
  480. /* Evict if needed */
  481. if (eprec >= 0) {
  482. /* Detect queueing to unconfigured precedence */
  483. ASSERT(!pktq_pempty(q, eprec));
  484. discard_oldest = AC_BITMAP_TST(dhdp->wme_dp, eprec);
  485. if (eprec == prec && !discard_oldest)
  486. return FALSE; /* refuse newer (incoming) packet */
  487. /* Evict packet according to discard policy */
  488. p = discard_oldest ? pktq_pdeq(q, eprec) : pktq_pdeq_tail(q, eprec);
  489. ASSERT(p);
  490. PKTFREE(dhdp->osh, p, TRUE);
  491. }
  492. /* Enqueue */
  493. p = pktq_penq(q, prec, pkt);
  494. ASSERT(p);
  495. return TRUE;
  496. }
  497. static int
  498. dhd_iovar_op(dhd_pub_t *dhd_pub, const char *name,
  499. void *params, int plen, void *arg, int len, bool set)
  500. {
  501. int bcmerror = 0;
  502. int val_size;
  503. const bcm_iovar_t *vi = NULL;
  504. uint32 actionid;
  505. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  506. ASSERT(name);
  507. ASSERT(len >= 0);
  508. /* Get MUST have return space */
  509. ASSERT(set || (arg && len));
  510. /* Set does NOT take qualifiers */
  511. ASSERT(!set || (!params && !plen));
  512. if ((vi = bcm_iovar_lookup(dhd_iovars, name)) == NULL) {
  513. bcmerror = BCME_UNSUPPORTED;
  514. goto exit;
  515. }
  516. DHD_CTL(("%s: %s %s, len %d plen %d\n", __FUNCTION__,
  517. name, (set ? "set" : "get"), len, plen));
  518. /* set up 'params' pointer in case this is a set command so that
  519. * the convenience int and bool code can be common to set and get
  520. */
  521. if (params == NULL) {
  522. params = arg;
  523. plen = len;
  524. }
  525. if (vi->type == IOVT_VOID)
  526. val_size = 0;
  527. else if (vi->type == IOVT_BUFFER)
  528. val_size = len;
  529. else
  530. /* all other types are integer sized */
  531. val_size = sizeof(int);
  532. actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
  533. bcmerror = dhd_doiovar(dhd_pub, vi, actionid, name, params, plen, arg, len, val_size);
  534. exit:
  535. return bcmerror;
  536. }
  537. int
  538. dhd_ioctl(dhd_pub_t * dhd_pub, dhd_ioctl_t *ioc, void * buf, uint buflen)
  539. {
  540. int bcmerror = 0;
  541. DHD_TRACE(("%s: Enter\n", __FUNCTION__));
  542. if (!buf) {
  543. return BCME_BADARG;
  544. }
  545. switch (ioc->cmd) {
  546. case DHD_GET_MAGIC:
  547. if (buflen < sizeof(int))
  548. bcmerror = BCME_BUFTOOSHORT;
  549. else
  550. *(int*)buf = DHD_IOCTL_MAGIC;
  551. break;
  552. case DHD_GET_VERSION:
  553. if (buflen < sizeof(int))
  554. bcmerror = -BCME_BUFTOOSHORT;
  555. else
  556. *(int*)buf = DHD_IOCTL_VERSION;
  557. break;
  558. case DHD_GET_VAR:
  559. case DHD_SET_VAR: {
  560. char *arg;
  561. uint arglen;
  562. /* scan past the name to any arguments */
  563. for (arg = buf, arglen = buflen; *arg && arglen; arg++, arglen--)
  564. ;
  565. if (*arg) {
  566. bcmerror = BCME_BUFTOOSHORT;
  567. break;
  568. }
  569. /* account for the NUL terminator */
  570. arg++, arglen--;
  571. /* call with the appropriate arguments */
  572. if (ioc->cmd == DHD_GET_VAR)
  573. bcmerror = dhd_iovar_op(dhd_pub, buf, arg, arglen,
  574. buf, buflen, IOV_GET);
  575. else
  576. bcmerror = dhd_iovar_op(dhd_pub, buf, NULL, 0, arg, arglen, IOV_SET);
  577. if (bcmerror != BCME_UNSUPPORTED)
  578. break;
  579. /* not in generic table, try protocol module */
  580. if (ioc->cmd == DHD_GET_VAR)
  581. bcmerror = dhd_prot_iovar_op(dhd_pub, buf, arg,
  582. arglen, buf, buflen, IOV_GET);
  583. else
  584. bcmerror = dhd_prot_iovar_op(dhd_pub, buf,
  585. NULL, 0, arg, arglen, IOV_SET);
  586. if (bcmerror != BCME_UNSUPPORTED)
  587. break;
  588. /* if still not found, try bus module */
  589. if (ioc->cmd == DHD_GET_VAR) {
  590. bcmerror = dhd_bus_iovar_op(dhd_pub, buf,
  591. arg, arglen, buf, buflen, IOV_GET);
  592. } else {
  593. bcmerror = dhd_bus_iovar_op(dhd_pub, buf,
  594. NULL, 0, arg, arglen, IOV_SET);
  595. }
  596. break;
  597. }
  598. default:
  599. bcmerror = BCME_UNSUPPORTED;
  600. }
  601. return bcmerror;
  602. }
  603. #ifdef SHOW_EVENTS
  604. static void
  605. wl_show_host_event(wl_event_msg_t *event, void *event_data)
  606. {
  607. uint i, status, reason;
  608. bool group = FALSE, flush_txq = FALSE, link = FALSE;
  609. const char *auth_str;
  610. const char *event_name;
  611. uchar *buf;
  612. char err_msg[256], eabuf[ETHER_ADDR_STR_LEN];
  613. uint event_type, flags, auth_type, datalen;
  614. event_type = ntoh32(event->event_type);
  615. flags = ntoh16(event->flags);
  616. status = ntoh32(event->status);
  617. reason = ntoh32(event->reason);
  618. auth_type = ntoh32(event->auth_type);
  619. datalen = ntoh32(event->datalen);
  620. /* debug dump of event messages */
  621. sprintf(eabuf, "%02x:%02x:%02x:%02x:%02x:%02x",
  622. (uchar)event->addr.octet[0]&0xff,
  623. (uchar)event->addr.octet[1]&0xff,
  624. (uchar)event->addr.octet[2]&0xff,
  625. (uchar)event->addr.octet[3]&0xff,
  626. (uchar)event->addr.octet[4]&0xff,
  627. (uchar)event->addr.octet[5]&0xff);
  628. event_name = "UNKNOWN";
  629. for (i = 0; i < (uint)bcmevent_names_size; i++)
  630. if (bcmevent_names[i].event == event_type)
  631. event_name = bcmevent_names[i].name;
  632. if (flags & WLC_EVENT_MSG_LINK)
  633. link = TRUE;
  634. if (flags & WLC_EVENT_MSG_GROUP)
  635. group = TRUE;
  636. if (flags & WLC_EVENT_MSG_FLUSHTXQ)
  637. flush_txq = TRUE;
  638. switch (event_type) {
  639. case WLC_E_START:
  640. case WLC_E_DEAUTH:
  641. case WLC_E_DISASSOC:
  642. DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
  643. break;
  644. case WLC_E_ASSOC_IND:
  645. case WLC_E_REASSOC_IND:
  646. DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
  647. break;
  648. case WLC_E_ASSOC:
  649. case WLC_E_REASSOC:
  650. if (status == WLC_E_STATUS_SUCCESS) {
  651. DHD_EVENT(("MACEVENT: %s, MAC %s, SUCCESS\n", event_name, eabuf));
  652. } else if (status == WLC_E_STATUS_TIMEOUT) {
  653. DHD_EVENT(("MACEVENT: %s, MAC %s, TIMEOUT\n", event_name, eabuf));
  654. } else if (status == WLC_E_STATUS_FAIL) {
  655. DHD_EVENT(("MACEVENT: %s, MAC %s, FAILURE, reason %d\n",
  656. event_name, eabuf, (int)reason));
  657. } else {
  658. DHD_EVENT(("MACEVENT: %s, MAC %s, unexpected status %d\n",
  659. event_name, eabuf, (int)status));
  660. }
  661. break;
  662. case WLC_E_DEAUTH_IND:
  663. case WLC_E_DISASSOC_IND:
  664. DHD_EVENT(("MACEVENT: %s, MAC %s, reason %d\n", event_name, eabuf, (int)reason));
  665. break;
  666. case WLC_E_AUTH:
  667. case WLC_E_AUTH_IND:
  668. if (auth_type == DOT11_OPEN_SYSTEM)
  669. auth_str = "Open System";
  670. else if (auth_type == DOT11_SHARED_KEY)
  671. auth_str = "Shared Key";
  672. else {
  673. sprintf(err_msg, "AUTH unknown: %d", (int)auth_type);
  674. auth_str = err_msg;
  675. }
  676. if (event_type == WLC_E_AUTH_IND) {
  677. DHD_EVENT(("MACEVENT: %s, MAC %s, %s\n", event_name, eabuf, auth_str));
  678. } else if (status == WLC_E_STATUS_SUCCESS) {
  679. DHD_EVENT(("MACEVENT: %s, MAC %s, %s, SUCCESS\n",
  680. event_name, eabuf, auth_str));
  681. } else if (status == WLC_E_STATUS_TIMEOUT) {
  682. DHD_EVENT(("MACEVENT: %s, MAC %s, %s, TIMEOUT\n",
  683. event_name, eabuf, auth_str));
  684. } else if (status == WLC_E_STATUS_FAIL) {
  685. DHD_EVENT(("MACEVENT: %s, MAC %s, %s, FAILURE, reason %d\n",
  686. event_name, eabuf, auth_str, (int)reason));
  687. }
  688. break;
  689. case WLC_E_JOIN:
  690. case WLC_E_ROAM:
  691. case WLC_E_SET_SSID:
  692. if (status == WLC_E_STATUS_SUCCESS) {
  693. DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
  694. } else if (status == WLC_E_STATUS_FAIL) {
  695. DHD_EVENT(("MACEVENT: %s, failed\n", event_name));
  696. } else if (status == WLC_E_STATUS_NO_NETWORKS) {
  697. DHD_EVENT(("MACEVENT: %s, no networks found\n", event_name));
  698. } else {
  699. DHD_EVENT(("MACEVENT: %s, unexpected status %d\n",
  700. event_name, (int)status));
  701. }
  702. break;
  703. case WLC_E_BEACON_RX:
  704. if (status == WLC_E_STATUS_SUCCESS) {
  705. DHD_EVENT(("MACEVENT: %s, SUCCESS\n", event_name));
  706. } else if (status == WLC_E_STATUS_FAIL) {
  707. DHD_EVENT(("MACEVENT: %s, FAIL\n", event_name));
  708. } else {
  709. DHD_EVENT(("MACEVENT: %s, status %d\n", event_name, status));
  710. }
  711. break;
  712. case WLC_E_LINK:
  713. DHD_EVENT(("MACEVENT: %s %s\n", event_name, link?"UP":"DOWN"));
  714. break;
  715. case WLC_E_MIC_ERROR:
  716. DHD_EVENT(("MACEVENT: %s, MAC %s, Group %d, Flush %d\n",
  717. event_name, eabuf, group, flush_txq));
  718. break;
  719. case WLC_E_ICV_ERROR:
  720. case WLC_E_UNICAST_DECODE_ERROR:
  721. case WLC_E_MULTICAST_DECODE_ERROR:
  722. DHD_EVENT(("MACEVENT: %s, MAC %s\n",
  723. event_name, eabuf));
  724. break;
  725. case WLC_E_TXFAIL:
  726. DHD_EVENT(("MACEVENT: %s, RA %s\n", event_name, eabuf));
  727. break;
  728. case WLC_E_SCAN_COMPLETE:
  729. case WLC_E_ASSOC_REQ_IE:
  730. case WLC_E_ASSOC_RESP_IE:
  731. case WLC_E_PMKID_CACHE:
  732. DHD_EVENT(("MACEVENT: %s\n", event_name));
  733. break;
  734. case WLC_E_PFN_NET_FOUND:
  735. case WLC_E_PFN_NET_LOST:
  736. case WLC_E_PFN_SCAN_COMPLETE:
  737. case WLC_E_PFN_SCAN_NONE:
  738. case WLC_E_PFN_SCAN_ALLGONE:
  739. DHD_EVENT(("PNOEVENT: %s\n", event_name));
  740. break;
  741. case WLC_E_PSK_SUP:
  742. case WLC_E_PRUNE:
  743. DHD_EVENT(("MACEVENT: %s, status %d, reason %d\n",
  744. event_name, (int)status, (int)reason));
  745. break;
  746. #ifdef WIFI_ACT_FRAME
  747. case WLC_E_ACTION_FRAME:
  748. DHD_TRACE(("MACEVENT: %s Bssid %s\n", event_name, eabuf));
  749. break;
  750. #endif /* WIFI_ACT_FRAME */
  751. case WLC_E_TRACE: {
  752. static uint32 seqnum_prev = 0;
  753. msgtrace_hdr_t hdr;
  754. uint32 nblost;
  755. char *s, *p;
  756. buf = (uchar *) event_data;
  757. memcpy(&hdr, buf, MSGTRACE_HDRLEN);
  758. if (hdr.version != MSGTRACE_VERSION) {
  759. printf("\nMACEVENT: %s [unsupported version --> "
  760. "dhd version:%d dongle version:%d]\n",
  761. event_name, MSGTRACE_VERSION, hdr.version);
  762. /* Reset datalen to avoid display below */
  763. datalen = 0;
  764. break;
  765. }
  766. /* There are 2 bytes available at the end of data */
  767. buf[MSGTRACE_HDRLEN + ntoh16(hdr.len)] = '\0';
  768. if (ntoh32(hdr.discarded_bytes) || ntoh32(hdr.discarded_printf)) {
  769. printf("\nWLC_E_TRACE: [Discarded traces in dongle -->"
  770. "discarded_bytes %d discarded_printf %d]\n",
  771. ntoh32(hdr.discarded_bytes), ntoh32(hdr.discarded_printf));
  772. }
  773. nblost = ntoh32(hdr.seqnum) - seqnum_prev - 1;
  774. if (nblost > 0) {
  775. printf("\nWLC_E_TRACE: [Event lost --> seqnum %d nblost %d\n",
  776. ntoh32(hdr.seqnum), nblost);
  777. }
  778. seqnum_prev = ntoh32(hdr.seqnum);
  779. /* Display the trace buffer. Advance from \n to \n to avoid display big
  780. * printf (issue with Linux printk )
  781. */
  782. p = (char *)&buf[MSGTRACE_HDRLEN];
  783. while ((s = strstr(p, "\n")) != NULL) {
  784. *s = '\0';
  785. printf("%s\n", p);
  786. p = s+1;
  787. }
  788. printf("%s\n", p);
  789. /* Reset datalen to avoid display below */
  790. datalen = 0;
  791. break;
  792. }
  793. case WLC_E_RSSI:
  794. DHD_EVENT(("MACEVENT: %s %d\n", event_name, ntoh32(*((int *)event_data))));
  795. break;
  796. default:
  797. DHD_EVENT(("MACEVENT: %s %d, MAC %s, status %d, reason %d, auth %d\n",
  798. event_name, event_type, eabuf, (int)status, (int)reason,
  799. (int)auth_type));
  800. break;
  801. }
  802. /* show any appended data */
  803. if (datalen) {
  804. buf = (uchar *) event_data;
  805. DHD_EVENT((" data (%d) : ", datalen));
  806. for (i = 0; i < datalen; i++)
  807. DHD_EVENT((" 0x%02x ", *buf++));
  808. DHD_EVENT(("\n"));
  809. }
  810. }
  811. #endif /* SHOW_EVENTS */
  812. int
  813. wl_host_event(dhd_pub_t *dhd_pub, int *ifidx, void *pktdata,
  814. wl_event_msg_t *event, void **data_ptr)
  815. {
  816. /* check whether packet is a BRCM event pkt */
  817. bcm_event_t *pvt_data = (bcm_event_t *)pktdata;
  818. uint8 *event_data;
  819. uint32 type, status, reason, datalen;
  820. uint16 flags;
  821. int evlen;
  822. if (bcmp(BRCM_OUI, &pvt_data->bcm_hdr.oui[0], DOT11_OUI_LEN)) {
  823. DHD_ERROR(("%s: mismatched OUI, bailing\n", __FUNCTION__));
  824. return (BCME_ERROR);
  825. }
  826. /* BRCM event pkt may be unaligned - use xxx_ua to load user_subtype. */
  827. if (ntoh16_ua((void *)&pvt_data->bcm_hdr.usr_subtype) != BCMILCP_BCM_SUBTYPE_EVENT) {
  828. DHD_ERROR(("%s: mismatched subtype, bailing\n", __FUNCTION__));
  829. return (BCME_ERROR);
  830. }
  831. *data_ptr = &pvt_data[1];
  832. event_data = *data_ptr;
  833. /* memcpy since BRCM event pkt may be unaligned. */
  834. memcpy(event, &pvt_data->event, sizeof(wl_event_msg_t));
  835. type = ntoh32_ua((void *)&event->event_type);
  836. flags = ntoh16_ua((void *)&event->flags);
  837. status = ntoh32_ua((void *)&event->status);
  838. reason = ntoh32_ua((void *)&event->reason);
  839. datalen = ntoh32_ua((void *)&event->datalen);
  840. evlen = datalen + sizeof(bcm_event_t);
  841. DHD_TRACE(("RX: event_type:%d flags:%d status:%d reason:%d \n",
  842. type, flags, status, reason));
  843. switch (type) {
  844. #ifdef PROP_TXSTATUS
  845. case WLC_E_FIFO_CREDIT_MAP:
  846. dhd_wlfc_event(dhd_pub->info);
  847. dhd_wlfc_FIFOcreditmap_event(dhd_pub->info, event_data);
  848. WLFC_DBGMESG(("WLC_E_FIFO_CREDIT_MAP:(AC0,AC1,AC2,AC3),(BC_MC),(OTHER): "
  849. "(%d,%d,%d,%d),(%d),(%d)\n", event_data[0], event_data[1],
  850. event_data[2],
  851. event_data[3], event_data[4], event_data[5]));
  852. break;
  853. #endif
  854. case WLC_E_IF:
  855. {
  856. dhd_if_event_t *ifevent = (dhd_if_event_t *)event_data;
  857. #ifdef PROP_TXSTATUS
  858. {
  859. uint8* ea = pvt_data->eth.ether_dhost;
  860. WLFC_DBGMESG(("WLC_E_IF: idx:%d, action:%s, iftype:%s, "
  861. "[%02x:%02x:%02x:%02x:%02x:%02x]\n",
  862. ifevent->ifidx,
  863. ((ifevent->action == WLC_E_IF_ADD) ? "ADD":"DEL"),
  864. ((ifevent->is_AP == 0) ? "STA":"AP "),
  865. ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]));
  866. (void)ea;
  867. dhd_wlfc_interface_event(dhd_pub->info,
  868. ((ifevent->action == WLC_E_IF_ADD) ?
  869. eWLFC_MAC_ENTRY_ACTION_ADD : eWLFC_MAC_ENTRY_ACTION_DEL),
  870. ifevent->ifidx, ifevent->is_AP, ea);
  871. /* dhd already has created an interface by default, for 0 */
  872. if (ifevent->ifidx == 0)
  873. break;
  874. }
  875. #endif /* PROP_TXSTATUS */
  876. #ifdef WL_CFG80211
  877. if (wl_cfg80211_is_progress_ifchange()) {
  878. DHD_ERROR(("%s: ifidx %d for %s action %d\n",
  879. __FUNCTION__, ifevent->ifidx,
  880. event->ifname, ifevent->action));
  881. if (ifevent->action == WLC_E_IF_ADD)
  882. wl_cfg80211_notify_ifchange();
  883. return (BCME_OK);
  884. }
  885. #endif /* WL_CFG80211 */
  886. if (ifevent->ifidx > 0 && ifevent->ifidx < DHD_MAX_IFS) {
  887. if (ifevent->action == WLC_E_IF_ADD) {
  888. if (dhd_add_if(dhd_pub->info, ifevent->ifidx,
  889. NULL, event->ifname,
  890. event->addr.octet,
  891. ifevent->flags, ifevent->bssidx)) {
  892. DHD_ERROR(("%s: dhd_add_if failed!!"
  893. " ifidx: %d for %s\n",
  894. __FUNCTION__,
  895. ifevent->ifidx,
  896. event->ifname));
  897. return (BCME_ERROR);
  898. }
  899. }
  900. else
  901. dhd_del_if(dhd_pub->info, ifevent->ifidx);
  902. } else {
  903. #ifndef PROP_TXSTATUS
  904. DHD_ERROR(("%s: Invalid ifidx %d for %s\n",
  905. __FUNCTION__, ifevent->ifidx, event->ifname));
  906. #endif /* !PROP_TXSTATUS */
  907. }
  908. }
  909. /* send up the if event: btamp user needs it */
  910. *ifidx = dhd_ifname2idx(dhd_pub->info, event->ifname);
  911. /* push up to external supp/auth */
  912. dhd_event(dhd_pub->info, (char *)pvt_data, evlen, *ifidx);
  913. break;
  914. #ifdef WLMEDIA_HTSF
  915. case WLC_E_HTSFSYNC:
  916. htsf_update(dhd_pub->info, event_data);
  917. break;
  918. #endif /* WLMEDIA_HTSF */
  919. case WLC_E_NDIS_LINK: {
  920. uint32 temp = hton32(WLC_E_LINK);
  921. memcpy((void *)(&pvt_data->event.event_type), &temp,
  922. sizeof(pvt_data->event.event_type));
  923. }
  924. /* These are what external supplicant/authenticator wants */
  925. /* fall through */
  926. case WLC_E_LINK:
  927. case WLC_E_DEAUTH:
  928. case WLC_E_DEAUTH_IND:
  929. case WLC_E_DISASSOC:
  930. case WLC_E_DISASSOC_IND:
  931. DHD_EVENT(("%s: Link event %d, flags %x, status %x\n",
  932. __FUNCTION__, type, flags, status));
  933. /* fall through */
  934. default:
  935. *ifidx = dhd_ifname2idx(dhd_pub->info, event->ifname);
  936. /* push up to external supp/auth */
  937. dhd_event(dhd_pub->info, (char *)pvt_data, evlen, *ifidx);
  938. DHD_TRACE(("%s: MAC event %d, flags %x, status %x\n",
  939. __FUNCTION__, type, flags, status));
  940. /* put it back to WLC_E_NDIS_LINK */
  941. if (type == WLC_E_NDIS_LINK) {
  942. uint32 temp;
  943. temp = ntoh32_ua((void *)&event->event_type);
  944. DHD_TRACE(("Converted to WLC_E_LINK type %d\n", temp));
  945. temp = ntoh32(WLC_E_NDIS_LINK);
  946. memcpy((void *)(&pvt_data->event.event_type), &temp,
  947. sizeof(pvt_data->event.event_type));
  948. }
  949. break;
  950. }
  951. #ifdef SHOW_EVENTS
  952. wl_show_host_event(event, (void *)event_data);
  953. #endif /* SHOW_EVENTS */
  954. return (BCME_OK);
  955. }
  956. void
  957. wl_event_to_host_order(wl_event_msg_t * evt)
  958. {
  959. /* Event struct members passed from dongle to host are stored in network
  960. * byte order. Convert all members to host-order.
  961. */
  962. evt->event_type = ntoh32(evt->event_type);
  963. evt->flags = ntoh16(evt->flags);
  964. evt->status = ntoh32(evt->status);
  965. evt->reason = ntoh32(evt->reason);
  966. evt->auth_type = ntoh32(evt->auth_type);
  967. evt->datalen = ntoh32(evt->datalen);
  968. evt->version = ntoh16(evt->version);
  969. }
  970. void
  971. dhd_print_buf(void *pbuf, int len, int bytes_per_line)
  972. {
  973. #ifdef DHD_DEBUG
  974. int i, j = 0;
  975. unsigned char *buf = pbuf;
  976. if (bytes_per_line == 0) {
  977. bytes_per_line = len;
  978. }
  979. for (i = 0; i < len; i++) {
  980. printf("%2.2x", *buf++);
  981. j++;
  982. if (j == bytes_per_line) {
  983. printf("\n");
  984. j = 0;
  985. } else {
  986. printf(":");
  987. }
  988. }
  989. printf("\n");
  990. #endif /* DHD_DEBUG */
  991. }
  992. #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
  993. /* Convert user's input in hex pattern to byte-size mask */
  994. static int
  995. wl_pattern_atoh(char *src, char *dst)
  996. {
  997. int i;
  998. if (strncmp(src, "0x", 2) != 0 &&
  999. strncmp(src, "0X", 2) != 0) {
  1000. DHD_ERROR(("Mask invalid format. Needs to start with 0x\n"));
  1001. return -1;
  1002. }
  1003. src = src + 2; /* Skip past 0x */
  1004. if (strlen(src) % 2 != 0) {
  1005. DHD_ERROR(("Mask invalid format. Needs to be of even length\n"));
  1006. return -1;
  1007. }
  1008. for (i = 0; *src != '\0'; i++) {
  1009. char num[3];
  1010. bcm_strncpy_s(num, sizeof(num), src, 2);
  1011. num[2] = '\0';
  1012. dst[i] = (uint8)strtoul(num, NULL, 16);
  1013. src += 2;
  1014. }
  1015. return i;
  1016. }
  1017. void
  1018. dhd_pktfilter_offload_enable(dhd_pub_t * dhd, char *arg, int enable, int master_mode)
  1019. {
  1020. char *argv[8];
  1021. int i = 0;
  1022. const char *str;
  1023. int buf_len;
  1024. int str_len;
  1025. char *arg_save = 0, *arg_org = 0;
  1026. int rc;
  1027. char buf[128];
  1028. wl_pkt_filter_enable_t enable_parm;
  1029. wl_pkt_filter_enable_t * pkt_filterp;
  1030. if (!arg)
  1031. return;
  1032. if (!(arg_save = MALLOC(dhd->osh, strlen(arg) + 1))) {
  1033. DHD_ERROR(("%s: kmalloc failed\n", __FUNCTION__));
  1034. goto fail;
  1035. }
  1036. arg_org = arg_save;
  1037. memcpy(arg_save, arg, strlen(arg) + 1);
  1038. argv[i] = bcmstrtok(&arg_save, " ", 0);
  1039. i = 0;
  1040. if (argv[i] == NULL) {
  1041. DHD_ERROR(("No args provided\n"));
  1042. goto fail;
  1043. }
  1044. str = "pkt_filter_enable";
  1045. str_len = strlen(str);
  1046. bcm_strncpy_s(buf, sizeof(buf), str, str_len);
  1047. buf[str_len] = '\0';
  1048. buf_len = str_len + 1;
  1049. pkt_filterp = (wl_pkt_filter_enable_t *)(buf + str_len + 1);
  1050. /* Parse packet filter id. */
  1051. enable_parm.id = htod32(strtoul(argv[i], NULL, 0));
  1052. /* Parse enable/disable value. */
  1053. enable_parm.enable = htod32(enable);
  1054. buf_len += sizeof(enable_parm);
  1055. memcpy((char *)pkt_filterp,
  1056. &enable_parm,
  1057. sizeof(enable_parm));
  1058. /* Enable/disable the specified filter. */
  1059. rc = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, buf_len, TRUE, 0);
  1060. rc = rc >= 0 ? 0 : rc;
  1061. if (rc)
  1062. DHD_TRACE(("%s: failed to add pktfilter %s, retcode = %d\n",
  1063. __FUNCTION__, arg, rc));
  1064. else
  1065. DHD_TRACE(("%s: successfully added pktfilter %s\n",
  1066. __FUNCTION__, arg));
  1067. /* Contorl the master mode */
  1068. bcm_mkiovar("pkt_filter_mode", (char *)&master_mode, 4, buf, sizeof(buf));
  1069. rc = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, sizeof(buf), TRUE, 0);
  1070. rc = rc >= 0 ? 0 : rc;
  1071. if (rc)
  1072. DHD_TRACE(("%s: failed to add pktfilter %s, retcode = %d\n",
  1073. __FUNCTION__, arg, rc));
  1074. fail:
  1075. if (arg_org)
  1076. MFREE(dhd->osh, arg_org, strlen(arg) + 1);
  1077. }
  1078. void
  1079. dhd_pktfilter_offload_set(dhd_pub_t * dhd, char *arg)
  1080. {
  1081. const char *str;
  1082. wl_pkt_filter_t pkt_filter;
  1083. wl_pkt_filter_t *pkt_filterp;
  1084. int buf_len;
  1085. int str_len;
  1086. int rc;
  1087. uint32 mask_size;
  1088. uint32 pattern_size;
  1089. char *argv[8], * buf = 0;
  1090. int i = 0;
  1091. char *arg_save = 0, *arg_org = 0;
  1092. #define BUF_SIZE 2048
  1093. if (!arg)
  1094. return;
  1095. if (!(arg_save = MALLOC(dhd->osh, strlen(arg) + 1))) {
  1096. DHD_ERROR(("%s: kmalloc failed\n", __FUNCTION__));
  1097. goto fail;
  1098. }
  1099. arg_org = arg_save;
  1100. if (!(buf = MALLOC(dhd->osh, BUF_SIZE))) {
  1101. DHD_ERROR(("%s: kmalloc failed\n", __FUNCTION__));
  1102. goto fail;
  1103. }
  1104. memcpy(arg_save, arg, strlen(arg) + 1);
  1105. if (strlen(arg) > BUF_SIZE) {
  1106. DHD_ERROR(("Not enough buffer %d < %d\n", (int)strlen(arg), (int)sizeof(buf)));
  1107. goto fail;
  1108. }
  1109. argv[i] = bcmstrtok(&arg_save, " ", 0);
  1110. while (argv[i++])
  1111. argv[i] = bcmstrtok(&arg_save, " ", 0);
  1112. i = 0;
  1113. if (argv[i] == NULL) {
  1114. DHD_ERROR(("No args provided\n"));
  1115. goto fail;
  1116. }
  1117. str = "pkt_filter_add";
  1118. str_len = strlen(str);
  1119. bcm_strncpy_s(buf, BUF_SIZE, str, str_len);
  1120. buf[ str_len ] = '\0';
  1121. buf_len = str_len + 1;
  1122. pkt_filterp = (wl_pkt_filter_t *) (buf + str_len + 1);
  1123. /* Parse packet filter id. */
  1124. pkt_filter.id = htod32(strtoul(argv[i], NULL, 0));
  1125. if (argv[++i] == NULL) {
  1126. DHD_ERROR(("Polarity not provided\n"));
  1127. goto fail;
  1128. }
  1129. /* Parse filter polarity. */
  1130. pkt_filter.negate_match = htod32(strtoul(argv[i], NULL, 0));
  1131. if (argv[++i] == NULL) {
  1132. DHD_ERROR(("Filter type not provided\n"));
  1133. goto fail;
  1134. }
  1135. /* Parse filter type. */
  1136. pkt_filter.type = htod32(strtoul(argv[i], NULL, 0));
  1137. if (argv[++i] == NULL) {
  1138. DHD_ERROR(("Offset not provided\n"));
  1139. goto fail;
  1140. }
  1141. /* Parse pattern filter offset. */
  1142. pkt_filter.u.pattern.offset = htod32(strtoul(argv[i], NULL, 0));
  1143. if (argv[++i] == NULL) {
  1144. DHD_ERROR(("Bitmask not provided\n"));
  1145. goto fail;
  1146. }
  1147. /* Parse pattern filter mask. */
  1148. mask_size =
  1149. htod32(wl_pattern_atoh(argv[i], (char *) pkt_filterp->u.pattern.mask_and_pattern));
  1150. if (argv[++i] == NULL) {
  1151. DHD_ERROR(("Pattern not provided\n"));
  1152. goto fail;
  1153. }
  1154. /* Parse pattern filter pattern. */
  1155. pattern_size =
  1156. htod32(wl_pattern_atoh(argv[i],
  1157. (char *) &pkt_filterp->u.pattern.mask_and_pattern[mask_size]));
  1158. if (mask_size != pattern_size) {
  1159. DHD_ERROR(("Mask and pattern not the same size\n"));
  1160. goto fail;
  1161. }
  1162. pkt_filter.u.pattern.size_bytes = mask_size;
  1163. buf_len += WL_PKT_FILTER_FIXED_LEN;
  1164. buf_len += (WL_PKT_FILTER_PATTERN_FIXED_LEN + 2 * mask_size);
  1165. /* Keep-alive attributes are set in local variable (keep_alive_pkt), and
  1166. ** then memcpy'ed into buffer (keep_alive_pktp) since there is no
  1167. ** guarantee that the buffer is properly aligned.
  1168. */
  1169. memcpy((char *)pkt_filterp,
  1170. &pkt_filter,
  1171. WL_PKT_FILTER_FIXED_LEN + WL_PKT_FILTER_PATTERN_FIXED_LEN);
  1172. rc = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, buf_len, TRUE, 0);
  1173. rc = rc >= 0 ? 0 : rc;
  1174. if (rc)
  1175. DHD_TRACE(("%s: failed to add pktfilter %s, retcode = %d\n",
  1176. __FUNCTION__, arg, rc));
  1177. else
  1178. DHD_TRACE(("%s: successfully added pktfilter %s\n",
  1179. __FUNCTION__, arg));
  1180. fail:
  1181. if (arg_org)
  1182. MFREE(dhd->osh, arg_org, strlen(arg) + 1);
  1183. if (buf)
  1184. MFREE(dhd->osh, buf, BUF_SIZE);
  1185. }
  1186. /* ========================== */
  1187. /* ==== ARP OFFLOAD SUPPORT = */
  1188. /* ========================== */
  1189. #ifdef ARP_OFFLOAD_SUPPORT
  1190. void
  1191. dhd_arp_offload_set(dhd_pub_t * dhd, int arp_mode)
  1192. {
  1193. char iovbuf[32];
  1194. int retcode;
  1195. bcm_mkiovar("arp_ol", (char *)&arp_mode, 4, iovbuf, sizeof(iovbuf));
  1196. retcode = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
  1197. retcode = retcode >= 0 ? 0 : retcode;
  1198. if (retcode)
  1199. DHD_TRACE(("%s: failed to set ARP offload mode to 0x%x, retcode = %d\n",
  1200. __FUNCTION__, arp_mode, retcode));
  1201. else
  1202. DHD_TRACE(("%s: successfully set ARP offload mode to 0x%x\n",
  1203. __FUNCTION__, arp_mode));
  1204. }
  1205. void
  1206. dhd_arp_offload_enable(dhd_pub_t * dhd, int arp_enable)
  1207. {
  1208. char iovbuf[32];
  1209. int retcode;
  1210. bcm_mkiovar("arpoe", (char *)&arp_enable, 4, iovbuf, sizeof(iovbuf));
  1211. retcode = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
  1212. retcode = retcode >= 0 ? 0 : retcode;
  1213. if (retcode)
  1214. DHD_TRACE(("%s: failed to enabe ARP offload to %d, retcode = %d\n",
  1215. __FUNCTION__, arp_enable, retcode));
  1216. else
  1217. DHD_TRACE(("%s: successfully enabed ARP offload to %d\n",
  1218. __FUNCTION__, arp_enable));
  1219. }
  1220. void
  1221. dhd_aoe_arp_clr(dhd_pub_t *dhd)
  1222. {
  1223. int ret = 0;
  1224. int iov_len = 0;
  1225. char iovbuf[128];
  1226. if (dhd == NULL) return;
  1227. iov_len = bcm_mkiovar("arp_table_clear", 0, 0, iovbuf, sizeof(iovbuf));
  1228. if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, iov_len, TRUE, 0) < 0))
  1229. DHD_ERROR(("%s failed code %d\n", __FUNCTION__, ret));
  1230. }
  1231. void
  1232. dhd_aoe_hostip_clr(dhd_pub_t *dhd)
  1233. {
  1234. int ret = 0;
  1235. int iov_len = 0;
  1236. char iovbuf[128];
  1237. if (dhd == NULL) return;
  1238. iov_len = bcm_mkiovar("arp_hostip_clear", 0, 0, iovbuf, sizeof(iovbuf));
  1239. if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, iov_len, TRUE, 0)) < 0)
  1240. DHD_ERROR(("%s failed code %d\n", __FUNCTION__, ret));
  1241. }
  1242. void
  1243. dhd_arp_offload_add_ip(dhd_pub_t *dhd, uint32 ipaddr)
  1244. {
  1245. int iov_len = 0;
  1246. char iovbuf[32];
  1247. int retcode;
  1248. iov_len = bcm_mkiovar("arp_hostip", (char *)&ipaddr, 4, iovbuf, sizeof(iovbuf));
  1249. retcode = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, iov_len, TRUE, 0);
  1250. if (retcode)
  1251. DHD_TRACE(("%s: ARP ip addr add failed, retcode = %d\n",
  1252. __FUNCTION__, retcode));
  1253. else
  1254. DHD_TRACE(("%s: sARP H ipaddr entry added \n",
  1255. __FUNCTION__));
  1256. }
  1257. int
  1258. dhd_arp_get_arp_hostip_table(dhd_pub_t *dhd, void *buf, int buflen)
  1259. {
  1260. int retcode, i;
  1261. int iov_len = 0;
  1262. uint32 *ptr32 = buf;
  1263. bool clr_bottom = FALSE;
  1264. if (!buf)
  1265. return -1;
  1266. iov_len = bcm_mkiovar("arp_hostip", 0, 0, buf, buflen);
  1267. retcode = dhd_wl_ioctl_cmd(dhd, WLC_GET_VAR, buf, buflen, FALSE, 0);
  1268. if (retcode) {
  1269. DHD_TRACE(("%s: ioctl WLC_GET_VAR error %d\n",
  1270. __FUNCTION__, retcode));
  1271. return -1;
  1272. }
  1273. /* clean up the buf, ascii reminder */
  1274. for (i = 0; i < MAX_IPV4_ENTRIES; i++) {
  1275. if (!clr_bottom) {
  1276. if (*ptr32 == 0)
  1277. clr_bottom = TRUE;
  1278. } else {
  1279. *ptr32 = 0;
  1280. }
  1281. ptr32++;
  1282. }
  1283. return 0;
  1284. }
  1285. #endif /* ARP_OFFLOAD_SUPPORT */
  1286. /* send up locally generated event */
  1287. void
  1288. dhd_sendup_event_common(dhd_pub_t *dhdp, wl_event_msg_t *event, void *data)
  1289. {
  1290. switch (ntoh32(event->event_type)) {
  1291. case WLC_E_BTA_HCI_EVENT:
  1292. break;
  1293. default:
  1294. break;
  1295. }
  1296. /* Call per-port handler. */
  1297. dhd_sendup_event(dhdp, event, data);
  1298. }
  1299. #ifdef SIMPLE_ISCAN
  1300. uint iscan_thread_id = 0;
  1301. iscan_buf_t * iscan_chain = 0;
  1302. iscan_buf_t *
  1303. dhd_iscan_allocate_buf(dhd_pub_t *dhd, iscan_buf_t **iscanbuf)
  1304. {
  1305. iscan_buf_t *iscanbuf_alloc = 0;
  1306. iscan_buf_t *iscanbuf_head;
  1307. DHD_ISCAN(("%s: Entered\n", __FUNCTION__));
  1308. dhd_iscan_lock();
  1309. iscanbuf_alloc = (iscan_buf_t*)MALLOC(dhd->osh, sizeof(iscan_buf_t));
  1310. if (iscanbuf_alloc == NULL)
  1311. goto fail;
  1312. iscanbuf_alloc->next = NULL;
  1313. iscanbuf_head = *iscanbuf;
  1314. DHD_ISCAN(("%s: addr of allocated node = 0x%X"
  1315. "addr of iscanbuf_head = 0x%X dhd = 0x%X\n",
  1316. __FUNCTION__, iscanbuf_alloc, iscanbuf_head, dhd));
  1317. if (iscanbuf_head == NULL) {
  1318. *iscanbuf = iscanbuf_alloc;
  1319. DHD_ISCAN(("%s: Head is allocated\n", __FUNCTION__));
  1320. goto fail;
  1321. }
  1322. while (iscanbuf_head->next)
  1323. iscanbuf_head = iscanbuf_head->next;
  1324. iscanbuf_head->next = iscanbuf_alloc;
  1325. fail:
  1326. dhd_iscan_unlock();
  1327. return iscanbuf_alloc;
  1328. }
  1329. void
  1330. dhd_iscan_free_buf(void *dhdp, iscan_buf_t *iscan_delete)
  1331. {
  1332. iscan_buf_t *iscanbuf_free = 0;
  1333. iscan_buf_t *iscanbuf_prv = 0;
  1334. iscan_buf_t *iscanbuf_cur;
  1335. dhd_pub_t *dhd = dhd_bus_pub(dhdp);
  1336. DHD_ISCAN(("%s: Entered\n", __FUNCTION__));
  1337. dhd_iscan_lock();
  1338. iscanbuf_cur = iscan_chain;
  1339. /* If iscan_delete is null then delete the entire
  1340. * chain or else delete specific one provided
  1341. */
  1342. if (!iscan_delete) {
  1343. while (iscanbuf_cur) {
  1344. iscanbuf_free = iscanbuf_cur;
  1345. iscanbuf_cur = iscanbuf_cur->next;
  1346. iscanbuf_free->next = 0;
  1347. MFREE(dhd->osh, iscanbuf_free, sizeof(iscan_buf_t));
  1348. }
  1349. iscan_chain = 0;
  1350. } else {
  1351. while (iscanbuf_cur) {
  1352. if (iscanbuf_cur == iscan_delete)
  1353. break;
  1354. iscanbuf_prv = iscanbuf_cur;
  1355. iscanbuf_cur = iscanbuf_cur->next;
  1356. }
  1357. if (iscanbuf_prv)
  1358. iscanbuf_prv->next = iscan_delete->next;
  1359. iscan_delete->next = 0;
  1360. MFREE(dhd->osh, iscan_delete, sizeof(iscan_buf_t));
  1361. if (!iscanbuf_prv)
  1362. iscan_chain = 0;
  1363. }
  1364. dhd_iscan_unlock();
  1365. }
  1366. iscan_buf_t *
  1367. dhd_iscan_result_buf(void)
  1368. {
  1369. return iscan_chain;
  1370. }
  1371. int
  1372. dhd_iscan_issue_request(void * dhdp, wl_iscan_params_t *pParams, uint32 size)
  1373. {
  1374. int rc = -1;
  1375. dhd_pub_t *dhd = dhd_bus_pub(dhdp);
  1376. char *buf;
  1377. char iovar[] = "iscan";
  1378. uint32 allocSize = 0;
  1379. wl_ioctl_t ioctl;
  1380. if (pParams) {
  1381. allocSize = (size + strlen(iovar) + 1);
  1382. if ((allocSize < size) || (allocSize < strlen(iovar)))
  1383. {
  1384. DHD_ERROR(("%s: overflow - allocation size too large %d < %d + %d!\n",
  1385. __FUNCTION__, allocSize, size, strlen(iovar)));
  1386. goto cleanUp;
  1387. }
  1388. buf = MALLOC(dhd->osh, allocSize);
  1389. if (buf == NULL)
  1390. {
  1391. DHD_ERROR(("%s: malloc of size %d failed!\n", __FUNCTION__, allocSize));
  1392. goto cleanUp;
  1393. }
  1394. ioctl.cmd = WLC_SET_VAR;
  1395. bcm_mkiovar(iovar, (char *)pParams, size, buf, allocSize);
  1396. rc = dhd_wl_ioctl(dhd, 0, &ioctl, buf, allocSize);
  1397. }
  1398. cleanUp:
  1399. if (buf) {
  1400. MFREE(dhd->osh, buf, allocSize);
  1401. }
  1402. return rc;
  1403. }
  1404. static int
  1405. dhd_iscan_get_partial_result(void *dhdp, uint *scan_count)
  1406. {
  1407. wl_iscan_results_t *list_buf;
  1408. wl_iscan_results_t list;
  1409. wl_scan_results_t *results;
  1410. iscan_buf_t *iscan_cur;
  1411. int status = -1;
  1412. dhd_pub_t *dhd = dhd_bus_pub(dhdp);
  1413. int rc;
  1414. wl_ioctl_t ioctl;
  1415. DHD_ISCAN(("%s: Enter\n", __FUNCTION__));
  1416. iscan_cur = dhd_iscan_allocate_buf(dhd, &iscan_chain);
  1417. if (!iscan_cur) {
  1418. DHD_ERROR(("%s: Failed to allocate node\n", __FUNCTION__));
  1419. dhd_iscan_free_buf(dhdp, 0);
  1420. dhd_iscan_request(dhdp, WL_SCAN_ACTION_ABORT);
  1421. dhd_ind_scan_confirm(dhdp, FALSE);
  1422. goto fail;
  1423. }
  1424. dhd_iscan_lock();
  1425. memset(iscan_cur->iscan_buf, 0, WLC_IW_ISCAN_MAXLEN);
  1426. list_buf = (wl_iscan_results_t*)iscan_cur->iscan_buf;
  1427. results = &list_buf->results;
  1428. results->buflen = WL_ISCAN_RESULTS_FIXED_SIZE;
  1429. results->version = 0;
  1430. results->count = 0;
  1431. memset(&list, 0, sizeof(list));
  1432. list.results.buflen = htod32(WLC_IW_ISCAN_MAXLEN);
  1433. bcm_mkiovar("iscanresults", (char *)&list, WL_ISCAN_RESULTS_FIXED_SIZE,
  1434. iscan_cur->iscan_buf, WLC_IW_ISCAN_MAXLEN);
  1435. ioctl.cmd = WLC_GET_VAR;
  1436. ioctl.set = FALSE;
  1437. rc = dhd_wl_ioctl(dhd, 0, &ioctl, iscan_cur->iscan_buf, WLC_IW_ISCAN_MAXLEN);
  1438. results->buflen = dtoh32(results->buflen);
  1439. results->version = dtoh32(results->version);
  1440. *scan_count = results->count = dtoh32(results->count);
  1441. status = dtoh32(list_buf->status);
  1442. DHD_ISCAN(("%s: Got %d resuls status = (%x)\n", __FUNCTION__, results->count, status));
  1443. dhd_iscan_unlock();
  1444. if (!(*scan_count)) {
  1445. /* TODO: race condition when FLUSH already called */
  1446. dhd_iscan_free_buf(dhdp, 0);
  1447. }
  1448. fail:
  1449. return status;
  1450. }
  1451. #endif /* SIMPLE_ISCAN */
  1452. /*
  1453. * returns = TRUE if associated, FALSE if not associated
  1454. * third paramter retval can return error from error
  1455. */
  1456. bool dhd_is_associated(dhd_pub_t *dhd, void *bss_buf, int *retval)
  1457. {
  1458. char bssid[6], zbuf[6];
  1459. int ret;
  1460. bzero(bssid, 6);
  1461. bzero(zbuf, 6);
  1462. ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_BSSID, (char *)&bssid, ETHER_ADDR_LEN, FALSE, 0);
  1463. DHD_TRACE((" %s WLC_GET_BSSID ioctl res = %d\n", __FUNCTION__, ret));
  1464. if (retval)
  1465. *retval = ret;
  1466. if (ret == BCME_NOTASSOCIATED) {
  1467. DHD_TRACE(("%s: not associated! res:%d\n", __FUNCTION__, ret));
  1468. }
  1469. if (ret < 0)
  1470. return FALSE;
  1471. if ((memcmp(bssid, zbuf, ETHER_ADDR_LEN) != 0)) {
  1472. /* STA is assocoated BSSID is non zero */
  1473. if (bss_buf) {
  1474. /* return bss if caller provided buf */
  1475. memcpy(bss_buf, bssid, ETHER_ADDR_LEN);
  1476. }
  1477. return TRUE;
  1478. } else {
  1479. DHD_TRACE(("%s: WLC_GET_BSSID ioctl returned zero bssid\n", __FUNCTION__));
  1480. return FALSE;
  1481. }
  1482. }
  1483. /* Function to estimate possible DTIM_SKIP value */
  1484. int
  1485. dhd_get_dtim_skip(dhd_pub_t *dhd)
  1486. {
  1487. int bcn_li_dtim;
  1488. int ret = -1;
  1489. int dtim_assoc = 0;
  1490. if ((dhd->dtim_skip == 0) || (dhd->dtim_skip == 1))
  1491. bcn_li_dtim = 3;
  1492. else
  1493. bcn_li_dtim = dhd->dtim_skip;
  1494. /* Check if associated */
  1495. if (dhd_is_associated(dhd, NULL, NULL) == FALSE) {
  1496. DHD_TRACE(("%s NOT assoc ret %d\n", __FUNCTION__, ret));
  1497. goto exit;
  1498. }
  1499. /* if assoc grab ap's dtim value */
  1500. if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_DTIMPRD,
  1501. &dtim_assoc, sizeof(dtim_assoc), FALSE, 0)) < 0) {
  1502. DHD_ERROR(("%s failed code %d\n", __FUNCTION__, ret));
  1503. goto exit;
  1504. }
  1505. DHD_ERROR(("%s bcn_li_dtim=%d DTIM=%d Listen=%d\n",
  1506. __FUNCTION__, bcn_li_dtim, dtim_assoc, LISTEN_INTERVAL));
  1507. /* if not assocated just eixt */
  1508. if (dtim_assoc == 0) {
  1509. goto exit;
  1510. }
  1511. /* check if sta listen interval fits into AP dtim */
  1512. if (dtim_assoc > LISTEN_INTERVAL) {
  1513. /* AP DTIM to big for our Listen Interval : no dtim skiping */
  1514. bcn_li_dtim = 1;
  1515. DHD_ERROR(("%s DTIM=%d > Listen=%d : too big ...\n",
  1516. __FUNCTION__, dtim_assoc, LISTEN_INTERVAL));
  1517. goto exit;
  1518. }
  1519. if ((bcn_li_dtim * dtim_assoc) > LISTEN_INTERVAL) {
  1520. /* Round up dtim_skip to fit into STAs Listen Interval */
  1521. bcn_li_dtim = (int)(LISTEN_INTERVAL / dtim_assoc);
  1522. DHD_TRACE(("%s agjust dtim_skip as %d\n", __FUNCTION__, bcn_li_dtim));
  1523. }
  1524. exit:
  1525. return bcn_li_dtim;
  1526. }
  1527. /* Check if HostAPD or WFD mode setup */
  1528. bool dhd_check_ap_wfd_mode_set(dhd_pub_t *dhd)
  1529. {
  1530. #ifdef WL_CFG80211
  1531. #ifndef WL_ENABLE_P2P_IF
  1532. /* To be back compatble with ICS MR1 release where p2p interface
  1533. * disable but wlan0 used for p2p
  1534. */
  1535. if (((dhd->op_mode & HOSTAPD_MASK) == HOSTAPD_MASK) ||
  1536. ((dhd->op_mode & WFD_MASK) == WFD_MASK)) {
  1537. return TRUE;
  1538. }
  1539. else
  1540. #else
  1541. /* concurent mode with p2p interface for wfd and wlan0 for sta */
  1542. if (((dhd->op_mode & P2P_GO_ENABLED) == P2P_GO_ENABLED) ||
  1543. ((dhd->op_mode & P2P_GC_ENABLED) == P2P_GC_ENABLED)) {
  1544. DHD_ERROR(("%s P2P enabled for mode=%d\n", __FUNCTION__, dhd->op_mode));
  1545. return TRUE;
  1546. }
  1547. else
  1548. #endif /* WL_ENABLE_P2P_IF */
  1549. #endif /* WL_CFG80211 */
  1550. return FALSE;
  1551. }
  1552. #ifdef PNO_SUPPORT
  1553. int
  1554. dhd_pno_clean(dhd_pub_t *dhd)
  1555. {
  1556. char iovbuf[128];
  1557. int pfn_enabled = 0;
  1558. int iov_len = 0;
  1559. int ret;
  1560. /* Disable pfn */
  1561. iov_len = bcm_mkiovar("pfn", (char *)&pfn_enabled, 4, iovbuf, sizeof(iovbuf));
  1562. if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0)) >= 0) {
  1563. /* clear pfn */
  1564. iov_len = bcm_mkiovar("pfnclear", 0, 0, iovbuf, sizeof(iovbuf));
  1565. if (iov_len) {
  1566. if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf,
  1567. iov_len, TRUE, 0)) < 0) {
  1568. DHD_ERROR(("%s failed code %d\n", __FUNCTION__, ret));
  1569. }
  1570. }
  1571. else {
  1572. ret = -1;
  1573. DHD_ERROR(("%s failed code %d\n", __FUNCTION__, iov_len));
  1574. }
  1575. }
  1576. else
  1577. DHD_ERROR(("%s failed code %d\n", __FUNCTION__, ret));
  1578. return ret;
  1579. }
  1580. int
  1581. dhd_pno_enable(dhd_pub_t *dhd, int pfn_enabled)
  1582. {
  1583. char iovbuf[128];
  1584. int ret = -1;
  1585. if ((!dhd) && ((pfn_enabled != 0) || (pfn_enabled != 1))) {
  1586. DHD_ERROR(("%s error exit\n", __FUNCTION__));
  1587. return ret;
  1588. }
  1589. memset(iovbuf, 0, sizeof(iovbuf));
  1590. #ifndef WL_SCHED_SCAN
  1591. if (dhd_check_ap_wfd_mode_set(dhd) == TRUE)
  1592. return (ret);
  1593. if ((pfn_enabled) && (dhd_is_associated(dhd, NULL, NULL) == TRUE)) {
  1594. DHD_ERROR(("%s pno is NOT enable : called in assoc mode , ignore\n", __FUNCTION__));
  1595. return ret;
  1596. }
  1597. #endif /* !WL_SCHED_SCAN */
  1598. /* Enable/disable PNO */
  1599. if ((ret = bcm_mkiovar("pfn", (char *)&pfn_enabled, 4, iovbuf, sizeof(iovbuf))) > 0) {
  1600. if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR,
  1601. iovbuf, sizeof(iovbuf), TRUE, 0)) < 0) {
  1602. DHD_ERROR(("%s failed for error=%d\n", __FUNCTION__, ret));
  1603. return ret;
  1604. }
  1605. else {
  1606. dhd->pno_enable = pfn_enabled;
  1607. DHD_TRACE(("%s set pno as %s\n",
  1608. __FUNCTION__, dhd->pno_enable ? "Enable" : "Disable"));
  1609. }
  1610. }
  1611. else DHD_ERROR(("%s failed err=%d\n", __FUNCTION__, ret));
  1612. return ret;
  1613. }
  1614. /* Function to execute combined scan */
  1615. int
  1616. dhd_pno_set(dhd_pub_t *dhd, wlc_ssid_t* ssids_local, int nssid, ushort scan_fr,
  1617. int pno_repeat, int pno_freq_expo_max)
  1618. {
  1619. int err = -1;
  1620. char iovbuf[128];
  1621. int k, i;
  1622. wl_pfn_param_t pfn_param;
  1623. wl_pfn_t pfn_element;
  1624. uint len = 0;
  1625. DHD_TRACE(("%s nssid=%d nchan=%d\n", __FUNCTION__, nssid, scan_fr));
  1626. if ((!dhd) && (!ssids_local)) {
  1627. DHD_ERROR(("%s error exit\n", __FUNCTION__));
  1628. err = -1;
  1629. return err;
  1630. }
  1631. #ifndef WL_SCHED_SCAN
  1632. if (dhd_check_ap_wfd_mode_set(dhd) == TRUE)
  1633. return (err);
  1634. #endif /* !WL_SCHED_SCAN */
  1635. /* Check for broadcast ssid */
  1636. for (k = 0; k < nssid; k++) {
  1637. if (!ssids_local[k].SSID_len) {
  1638. DHD_ERROR(("%d: Broadcast SSID is ilegal for PNO setting\n", k));
  1639. return err;
  1640. }
  1641. }
  1642. /* #define PNO_DUMP 1 */
  1643. #ifdef PNO_DUMP
  1644. {
  1645. int j;
  1646. for (j = 0; j < nssid; j++) {
  1647. DHD_ERROR(("%d: scan for %s size =%d\n", j,
  1648. ssids_local[j].SSID, ssids_local[j].SSID_len));
  1649. }
  1650. }
  1651. #endif /* PNO_DUMP */
  1652. /* clean up everything */
  1653. if ((err = dhd_pno_clean(dhd)) < 0) {
  1654. DHD_ERROR(("%s failed error=%d\n", __FUNCTION__, err));
  1655. return err;
  1656. }
  1657. memset(iovbuf, 0, sizeof(iovbuf));
  1658. memset(&pfn_param, 0, sizeof(pfn_param));
  1659. memset(&pfn_element, 0, sizeof(pfn_element));
  1660. /* set pfn parameters */
  1661. pfn_param.version = htod32(PFN_VERSION);
  1662. pfn_param.flags = htod16((PFN_LIST_ORDER << SORT_CRITERIA_BIT));
  1663. /* check and set extra pno params */
  1664. if ((pno_repeat != 0) || (pno_freq_expo_max != 0)) {
  1665. pfn_param.flags |= htod16(ENABLE << ENABLE_ADAPTSCAN_BIT);
  1666. pfn_param.repeat = (uchar) (pno_repeat);
  1667. pfn_param.exp = (uchar) (pno_freq_expo_max);
  1668. }
  1669. /* set up pno scan fr */
  1670. if (scan_fr != 0)
  1671. pfn_param.scan_freq = htod32(scan_fr);
  1672. if (pfn_param.scan_freq > PNO_SCAN_MAX_FW_SEC) {
  1673. DHD_ERROR(("%s pno freq above %d sec\n", __FUNCTION__, PNO_SCAN_MAX_FW_SEC));
  1674. return err;
  1675. }
  1676. if (pfn_param.scan_freq < PNO_SCAN_MIN_FW_SEC) {
  1677. DHD_ERROR(("%s pno freq less %d sec\n", __FUNCTION__, PNO_SCAN_MIN_FW_SEC));
  1678. return err;
  1679. }
  1680. len = bcm_mkiovar("pfn_set", (char *)&pfn_param, sizeof(pfn_param), iovbuf, sizeof(iovbuf));
  1681. if ((err = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, len, TRUE, 0)) < 0) {
  1682. DHD_ERROR(("%s pfn_set failed for error=%d\n",
  1683. __FUNCTION__, err));
  1684. return err;
  1685. }
  1686. /* set all pfn ssid */
  1687. for (i = 0; i < nssid; i++) {
  1688. pfn_element.infra = htod32(DOT11_BSSTYPE_INFRASTRUCTURE);
  1689. pfn_element.auth = (DOT11_OPEN_SYSTEM);
  1690. pfn_element.wpa_auth = htod32(WPA_AUTH_PFN_ANY);
  1691. pfn_element.wsec = htod32(0);
  1692. pfn_element.infra = htod32(1);
  1693. pfn_element.flags = htod32(ENABLE << WL_PFN_HIDDEN_BIT);
  1694. memcpy((char *)pfn_element.ssid.SSID, ssids_local[i].SSID, ssids_local[i].SSID_len);
  1695. pfn_element.ssid.SSID_len = ssids_local[i].SSID_len;
  1696. if ((len =
  1697. bcm_mkiovar("pfn_add", (char *)&pfn_element,
  1698. sizeof(pfn_element), iovbuf, sizeof(iovbuf))) > 0) {
  1699. if ((err =
  1700. dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, len, TRUE, 0)) < 0) {
  1701. DHD_ERROR(("%s failed for i=%d error=%d\n",
  1702. __FUNCTION__, i, err));
  1703. return err;
  1704. }
  1705. else
  1706. DHD_TRACE(("%s set OK with PNO time=%d repeat=%d max_adjust=%d\n",
  1707. __FUNCTION__, pfn_param.scan_freq,
  1708. pfn_param.repeat, pfn_param.exp));
  1709. }
  1710. else DHD_ERROR(("%s failed err=%d\n", __FUNCTION__, err));
  1711. }
  1712. /* Enable PNO */
  1713. /* dhd_pno_enable(dhd, 1); */
  1714. return err;
  1715. }
  1716. int
  1717. dhd_pno_set_ex(dhd_pub_t *dhd, wl_pfn_t* ssidnet, int nssid, ushort pno_interval,
  1718. int pno_repeat, int pno_expo_max, int pno_lost_time)
  1719. {
  1720. int err = -1;
  1721. char iovbuf[128];
  1722. int k, i;
  1723. wl_pfn_param_t pfn_param;
  1724. wl_pfn_t pfn_element;
  1725. uint len = 0;
  1726. DHD_TRACE(("%s nssid=%d pno_interval=%d\n", __FUNCTION__, nssid, pno_interval));
  1727. if ((!dhd) && (!ssidnet)) {
  1728. DHD_ERROR(("%s error exit\n", __FUNCTION__));
  1729. err = -1;
  1730. return err;
  1731. }
  1732. if (dhd_check_ap_wfd_mode_set(dhd) == TRUE)
  1733. return (err);
  1734. /* Check for broadcast ssid */
  1735. for (k = 0; k < nssid; k++) {
  1736. if (!ssidnet[k].ssid.SSID_len) {
  1737. DHD_ERROR(("%d: Broadcast SSID is ilegal for PNO setting\n", k));
  1738. return err;
  1739. }
  1740. }
  1741. /* #define PNO_DUMP 1 */
  1742. #ifdef PNO_DUMP
  1743. {
  1744. int j;
  1745. for (j = 0; j < nssid; j++) {
  1746. DHD_ERROR(("%d: scan for %s size =%d\n", j,
  1747. ssidnet[j].ssid.SSID, ssidnet[j].ssid.SSID_len));
  1748. }
  1749. }
  1750. #endif /* PNO_DUMP */
  1751. /* clean up everything */
  1752. if ((err = dhd_pno_clean(dhd)) < 0) {
  1753. DHD_ERROR(("%s failed error=%d\n", __FUNCTION__, err));
  1754. return err;
  1755. }
  1756. memset(iovbuf, 0, sizeof(iovbuf));
  1757. memset(&pfn_param, 0, sizeof(pfn_param));
  1758. memset(&pfn_element, 0, sizeof(pfn_element));
  1759. /* set pfn parameters */
  1760. pfn_param.version = htod32(PFN_VERSION);
  1761. pfn_param.flags = htod16((PFN_LIST_ORDER << SORT_CRITERIA_BIT));
  1762. /* check and set extra pno params */
  1763. if ((pno_repeat != 0) || (pno_expo_max != 0)) {
  1764. pfn_param.flags |= htod16(ENABLE << ENABLE_ADAPTSCAN_BIT);
  1765. pfn_param.repeat = (uchar) (pno_repeat);
  1766. pfn_param.exp = (uchar) (pno_expo_max);
  1767. }
  1768. /* set up pno scan fr */
  1769. if (pno_interval != 0)
  1770. pfn_param.scan_freq = htod32(pno_interval);
  1771. if (pfn_param.scan_freq > PNO_SCAN_MAX_FW_SEC) {
  1772. DHD_ERROR(("%s pno freq above %d sec\n", __FUNCTION__, PNO_SCAN_MAX_FW_SEC));
  1773. return err;
  1774. }
  1775. if (pfn_param.scan_freq < PNO_SCAN_MIN_FW_SEC) {
  1776. DHD_ERROR(("%s pno freq less %d sec\n", __FUNCTION__, PNO_SCAN_MIN_FW_SEC));
  1777. return err;
  1778. }
  1779. /* network lost time */
  1780. pfn_param.lost_network_timeout = htod32(pno_lost_time);
  1781. len = bcm_mkiovar("pfn_set", (char *)&pfn_param, sizeof(pfn_param), iovbuf, sizeof(iovbuf));
  1782. if ((err = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, len, TRUE, 0)) < 0) {
  1783. DHD_ERROR(("%s pfn_set failed for error=%d\n",
  1784. __FUNCTION__, err));
  1785. return err;
  1786. } else {
  1787. DHD_TRACE(("%s pfn_set OK with PNO time=%d repeat=%d max_adjust=%d\n",
  1788. __FUNCTION__, pfn_param.scan_freq,
  1789. pfn_param.repeat, pfn_param.exp));
  1790. }
  1791. /* set all pfn ssid */
  1792. for (i = 0; i < nssid; i++) {
  1793. pfn_element.flags = htod32(ssidnet[i].flags);
  1794. pfn_element.infra = htod32(ssidnet[i].infra);
  1795. pfn_element.auth = htod32(ssidnet[i].auth);
  1796. pfn_element.wpa_auth = htod32(ssidnet[i].wpa_auth);
  1797. pfn_element.wsec = htod32(ssidnet[i].wsec);
  1798. memcpy((char *)pfn_element.ssid.SSID, ssidnet[i].ssid.SSID, ssidnet[i].ssid.SSID_len);
  1799. pfn_element.ssid.SSID_len = htod32(ssidnet[i].ssid.SSID_len);
  1800. if ((len =
  1801. bcm_mkiovar("pfn_add", (char *)&pfn_element,
  1802. sizeof(pfn_element), iovbuf, sizeof(iovbuf))) > 0) {
  1803. if ((err =
  1804. dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, len, TRUE, 0)) < 0) {
  1805. DHD_ERROR(("%s pfn_add failed with ssidnet[%d] error=%d\n",
  1806. __FUNCTION__, i, err));
  1807. return err;
  1808. } else {
  1809. DHD_TRACE(("%s pfn_add OK with ssidnet[%d]\n", __FUNCTION__, i));
  1810. }
  1811. } else {
  1812. DHD_ERROR(("%s bcm_mkiovar failed with ssidnet[%d]\n", __FUNCTION__, i));
  1813. }
  1814. }
  1815. return err;
  1816. }
  1817. int
  1818. dhd_pno_get_status(dhd_pub_t *dhd)
  1819. {
  1820. int ret = -1;
  1821. if (!dhd)
  1822. return ret;
  1823. else
  1824. return (dhd->pno_enable);
  1825. }
  1826. #endif /* PNO_SUPPORT */
  1827. #if defined(KEEP_ALIVE)
  1828. int dhd_keep_alive_onoff(dhd_pub_t *dhd)
  1829. {
  1830. char buf[256];
  1831. const char *str;
  1832. wl_mkeep_alive_pkt_t mkeep_alive_pkt;
  1833. wl_mkeep_alive_pkt_t *mkeep_alive_pktp;
  1834. int buf_len;
  1835. int str_len;
  1836. int res = -1;
  1837. if (dhd_check_ap_wfd_mode_set(dhd) == TRUE)
  1838. return (res);
  1839. DHD_TRACE(("%s execution\n", __FUNCTION__));
  1840. str = "mkeep_alive";
  1841. str_len = strlen(str);
  1842. strncpy(buf, str, str_len);
  1843. buf[ str_len ] = '\0';
  1844. mkeep_alive_pktp = (wl_mkeep_alive_pkt_t *) (buf + str_len + 1);
  1845. mkeep_alive_pkt.period_msec = KEEP_ALIVE_PERIOD;
  1846. buf_len = str_len + 1;
  1847. mkeep_alive_pkt.version = htod16(WL_MKEEP_ALIVE_VERSION);
  1848. mkeep_alive_pkt.length = htod16(WL_MKEEP_ALIVE_FIXED_LEN);
  1849. /* Setup keep alive zero for null packet generation */
  1850. mkeep_alive_pkt.keep_alive_id = 0;
  1851. mkeep_alive_pkt.len_bytes = 0;
  1852. buf_len += WL_MKEEP_ALIVE_FIXED_LEN;
  1853. /* Keep-alive attributes are set in local variable (mkeep_alive_pkt), and
  1854. * then memcpy'ed into buffer (mkeep_alive_pktp) since there is no
  1855. * guarantee that the buffer is properly aligned.
  1856. */
  1857. memcpy((char *)mkeep_alive_pktp, &mkeep_alive_pkt, WL_MKEEP_ALIVE_FIXED_LEN);
  1858. res = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, buf_len, TRUE, 0);
  1859. return res;
  1860. }
  1861. #endif /* defined(KEEP_ALIVE) */
  1862. /* Android ComboSCAN support */
  1863. /*
  1864. * data parsing from ComboScan tlv list
  1865. */
  1866. int
  1867. wl_iw_parse_data_tlv(char** list_str, void *dst, int dst_size, const char token,
  1868. int input_size, int *bytes_left)
  1869. {
  1870. char* str = *list_str;
  1871. uint16 short_temp;
  1872. uint32 int_temp;
  1873. if ((list_str == NULL) || (*list_str == NULL) ||(bytes_left == NULL) || (*bytes_left < 0)) {
  1874. DHD_ERROR(("%s error paramters\n", __FUNCTION__));
  1875. return -1;
  1876. }
  1877. /* Clean all dest bytes */
  1878. memset(dst, 0, dst_size);
  1879. while (*bytes_left > 0) {
  1880. if (str[0] != token) {
  1881. DHD_TRACE(("%s NOT Type=%d get=%d left_parse=%d \n",
  1882. __FUNCTION__, token, str[0], *bytes_left));
  1883. return -1;
  1884. }
  1885. *bytes_left -= 1;
  1886. str += 1;
  1887. if (input_size == 1) {
  1888. memcpy(dst, str, input_size);
  1889. }
  1890. else if (input_size == 2) {
  1891. memcpy(dst, (char *)htod16(memcpy(&short_temp, str, input_size)),
  1892. input_size);
  1893. }
  1894. else if (input_size == 4) {
  1895. memcpy(dst, (char *)htod32(memcpy(&int_temp, str, input_size)),
  1896. input_size);
  1897. }
  1898. *bytes_left -= input_size;
  1899. str += input_size;
  1900. *list_str = str;
  1901. return 1;
  1902. }
  1903. return 1;
  1904. }
  1905. /*
  1906. * channel list parsing from cscan tlv list
  1907. */
  1908. int
  1909. wl_iw_parse_channel_list_tlv(char** list_str, uint16* channel_list,
  1910. int channel_num, int *bytes_left)
  1911. {
  1912. char* str = *list_str;
  1913. int idx = 0;
  1914. if ((list_str == NULL) || (*list_str == NULL) ||(bytes_left == NULL) || (*bytes_left < 0)) {
  1915. DHD_ERROR(("%s error paramters\n", __FUNCTION__));
  1916. return -1;
  1917. }
  1918. while (*bytes_left > 0) {
  1919. if (str[0] != CSCAN_TLV_TYPE_CHANNEL_IE) {
  1920. *list_str = str;
  1921. DHD_TRACE(("End channel=%d left_parse=%d %d\n", idx, *bytes_left, str[0]));
  1922. return idx;
  1923. }
  1924. /* Get proper CSCAN_TLV_TYPE_CHANNEL_IE */
  1925. *bytes_left -= 1;
  1926. str += 1;
  1927. if (str[0] == 0) {
  1928. /* All channels */
  1929. channel_list[idx] = 0x0;
  1930. }
  1931. else {
  1932. channel_list[idx] = (uint16)str[0];
  1933. DHD_TRACE(("%s channel=%d \n", __FUNCTION__, channel_list[idx]));
  1934. }
  1935. *bytes_left -= 1;
  1936. str += 1;
  1937. if (idx++ > 255) {
  1938. DHD_ERROR(("%s Too many channels \n", __FUNCTION__));
  1939. return -1;
  1940. }
  1941. }
  1942. *list_str = str;
  1943. return idx;
  1944. }
  1945. /*
  1946. * SSIDs list parsing from cscan tlv list
  1947. */
  1948. int
  1949. wl_iw_parse_ssid_list_tlv(char** list_str, wlc_ssid_t* ssid, int max, int *bytes_left)
  1950. {
  1951. char* str;
  1952. int idx = 0;
  1953. if ((list_str == NULL) || (*list_str == NULL) || (*bytes_left < 0)) {
  1954. DHD_ERROR(("%s error paramters\n", __FUNCTION__));
  1955. return -1;
  1956. }
  1957. str = *list_str;
  1958. while (*bytes_left > 0) {
  1959. if (str[0] != CSCAN_TLV_TYPE_SSID_IE) {
  1960. *list_str = str;
  1961. DHD_TRACE(("nssid=%d left_parse=%d %d\n", idx, *bytes_left, str[0]));
  1962. return idx;
  1963. }
  1964. /* Get proper CSCAN_TLV_TYPE_SSID_IE */
  1965. *bytes_left -= 1;
  1966. str += 1;
  1967. if (str[0] == 0) {
  1968. /* Broadcast SSID */
  1969. ssid[idx].SSID_len = 0;
  1970. memset((char*)ssid[idx].SSID, 0x0, DOT11_MAX_SSID_LEN);
  1971. *bytes_left -= 1;
  1972. str += 1;
  1973. DHD_TRACE(("BROADCAST SCAN left=%d\n", *bytes_left));
  1974. }
  1975. else if (str[0] <= DOT11_MAX_SSID_LEN) {
  1976. /* Get proper SSID size */
  1977. ssid[idx].SSID_len = str[0];
  1978. *bytes_left -= 1;
  1979. str += 1;
  1980. /* Get SSID */
  1981. if (ssid[idx].SSID_len > *bytes_left) {
  1982. DHD_ERROR(("%s out of memory range len=%d but left=%d\n",
  1983. __FUNCTION__, ssid[idx].SSID_len, *bytes_left));
  1984. return -1;
  1985. }
  1986. memcpy((char*)ssid[idx].SSID, str, ssid[idx].SSID_len);
  1987. *bytes_left -= ssid[idx].SSID_len;
  1988. str += ssid[idx].SSID_len;
  1989. DHD_TRACE(("%s :size=%d left=%d\n",
  1990. (char*)ssid[idx].SSID, ssid[idx].SSID_len, *bytes_left));
  1991. }
  1992. else {
  1993. DHD_ERROR(("### SSID size more that %d\n", str[0]));
  1994. return -1;
  1995. }
  1996. if (idx++ > max) {
  1997. DHD_ERROR(("%s number of SSIDs more that %d\n", __FUNCTION__, idx));
  1998. return -1;
  1999. }
  2000. }
  2001. *list_str = str;
  2002. return idx;
  2003. }
  2004. /* Parse a comma-separated list from list_str into ssid array, starting
  2005. * at index idx. Max specifies size of the ssid array. Parses ssids
  2006. * and returns updated idx; if idx >= max not all fit, the excess have
  2007. * not been copied. Returns -1 on empty string, or on ssid too long.
  2008. */
  2009. int
  2010. wl_iw_parse_ssid_list(char** list_str, wlc_ssid_t* ssid, int idx, int max)
  2011. {
  2012. char* str, *ptr;
  2013. if ((list_str == NULL) || (*list_str == NULL))
  2014. return -1;
  2015. for (str = *list_str; str != NULL; str = ptr) {
  2016. /* check for next TAG */
  2017. if (!strncmp(str, GET_CHANNEL, strlen(GET_CHANNEL))) {
  2018. *list_str = str + strlen(GET_CHANNEL);
  2019. return idx;
  2020. }
  2021. if ((ptr = strchr(str, ',')) != NULL) {
  2022. *ptr++ = '\0';
  2023. }
  2024. if (strlen(str) > DOT11_MAX_SSID_LEN) {
  2025. DHD_ERROR(("ssid <%s> exceeds %d\n", str, DOT11_MAX_SSID_LEN));
  2026. return -1;
  2027. }
  2028. if (strlen(str) == 0)
  2029. ssid[idx].SSID_len = 0;
  2030. if (idx < max) {
  2031. bcm_strcpy_s((char*)ssid[idx].SSID, sizeof(ssid[idx].SSID), str);
  2032. ssid[idx].SSID_len = strlen(str);
  2033. }
  2034. idx++;
  2035. }
  2036. return idx;
  2037. }
  2038. /*
  2039. * Parse channel list from iwpriv CSCAN
  2040. */
  2041. int
  2042. wl_iw_parse_channel_list(char** list_str, uint16* channel_list, int channel_num)
  2043. {
  2044. int num;
  2045. int val;
  2046. char* str;
  2047. char* endptr = NULL;
  2048. if ((list_str == NULL)||(*list_str == NULL))
  2049. return -1;
  2050. str = *list_str;
  2051. num = 0;
  2052. while (strncmp(str, GET_NPROBE, strlen(GET_NPROBE))) {
  2053. val = (int)strtoul(str, &endptr, 0);
  2054. if (endptr == str) {
  2055. printf("could not parse channel number starting at"
  2056. " substring \"%s\" in list:\n%s\n",
  2057. str, *list_str);
  2058. return -1;
  2059. }
  2060. str = endptr + strspn(endptr, " ,");
  2061. if (num == channel_num) {
  2062. DHD_ERROR(("too many channels (more than %d) in channel list:\n%s\n",
  2063. channel_num, *list_str));
  2064. return -1;
  2065. }
  2066. channel_list[num++] = (uint16)val;
  2067. }
  2068. *list_str = str;
  2069. return num;
  2070. }