main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /*
  2. * Marvell Wireless LAN device driver: major functions
  3. *
  4. * Copyright (C) 2011, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include "main.h"
  20. #include "wmm.h"
  21. #include "cfg80211.h"
  22. #include "11n.h"
  23. #define VERSION "1.0"
  24. const char driver_version[] = "mwifiex " VERSION " (%s) ";
  25. struct mwifiex_adapter *g_adapter;
  26. EXPORT_SYMBOL_GPL(g_adapter);
  27. static struct mwifiex_bss_attr mwifiex_bss_sta[] = {
  28. {MWIFIEX_BSS_TYPE_STA, MWIFIEX_DATA_FRAME_TYPE_ETH_II, true, 0, 0},
  29. };
  30. static int drv_mode = DRV_MODE_STA;
  31. static char fw_name[32] = DEFAULT_FW_NAME;
  32. /* Supported drv_mode table */
  33. static struct mwifiex_drv_mode mwifiex_drv_mode_tbl[] = {
  34. {
  35. /* drv_mode */
  36. .drv_mode = DRV_MODE_STA,
  37. /* intf number */
  38. .intf_num = ARRAY_SIZE(mwifiex_bss_sta),
  39. /* bss_attr */
  40. .bss_attr = mwifiex_bss_sta,
  41. }
  42. ,
  43. };
  44. /*
  45. * This function registers the device and performs all the necessary
  46. * initializations.
  47. *
  48. * The following initialization operations are performed -
  49. * - Allocate adapter structure
  50. * - Save interface specific operations table in adapter
  51. * - Call interface specific initialization routine
  52. * - Allocate private structures
  53. * - Set default adapter structure parameters
  54. * - Initialize locks
  55. *
  56. * In case of any errors during inittialization, this function also ensures
  57. * proper cleanup before exiting.
  58. */
  59. static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
  60. struct mwifiex_device *mdevice, void **padapter)
  61. {
  62. int ret = 0;
  63. struct mwifiex_adapter *adapter = NULL;
  64. u8 i = 0;
  65. adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
  66. /* Allocate memory for adapter structure */
  67. if (!adapter)
  68. return -1;
  69. g_adapter = adapter;
  70. adapter->card = card;
  71. /* Save interface specific operations in adapter */
  72. memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
  73. /* card specific initialization has been deferred until now .. */
  74. ret = adapter->if_ops.init_if(adapter);
  75. if (ret)
  76. goto error;
  77. adapter->priv_num = 0;
  78. for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
  79. adapter->priv[i] = NULL;
  80. if (!mdevice->bss_attr[i].active)
  81. continue;
  82. /* For valid bss_attr,
  83. allocate memory for private structure */
  84. adapter->priv[i] = kzalloc(sizeof(struct mwifiex_private),
  85. GFP_KERNEL);
  86. if (!adapter->priv[i]) {
  87. dev_err(adapter->dev, "%s: failed to alloc priv[%d]\n",
  88. __func__, i);
  89. goto error;
  90. }
  91. adapter->priv_num++;
  92. memset(adapter->priv[i], 0,
  93. sizeof(struct mwifiex_private));
  94. adapter->priv[i]->adapter = adapter;
  95. /* Save bss_type, frame_type & bss_priority */
  96. adapter->priv[i]->bss_type = (u8) mdevice->bss_attr[i].bss_type;
  97. adapter->priv[i]->frame_type =
  98. (u8) mdevice->bss_attr[i].frame_type;
  99. adapter->priv[i]->bss_priority =
  100. (u8) mdevice->bss_attr[i].bss_priority;
  101. if (mdevice->bss_attr[i].bss_type == MWIFIEX_BSS_TYPE_STA)
  102. adapter->priv[i]->bss_role = MWIFIEX_BSS_ROLE_STA;
  103. else if (mdevice->bss_attr[i].bss_type == MWIFIEX_BSS_TYPE_UAP)
  104. adapter->priv[i]->bss_role = MWIFIEX_BSS_ROLE_UAP;
  105. /* Save bss_index & bss_num */
  106. adapter->priv[i]->bss_index = i;
  107. adapter->priv[i]->bss_num = mdevice->bss_attr[i].bss_num;
  108. }
  109. /* Initialize lock variables */
  110. if (mwifiex_init_lock_list(adapter))
  111. goto error;
  112. init_timer(&adapter->cmd_timer);
  113. adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
  114. adapter->cmd_timer.data = (unsigned long) adapter;
  115. /* Return pointer of struct mwifiex_adapter */
  116. *padapter = adapter;
  117. return 0;
  118. error:
  119. dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
  120. /* Free lock variables */
  121. mwifiex_free_lock_list(adapter);
  122. for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++)
  123. kfree(adapter->priv[i]);
  124. kfree(adapter);
  125. return -1;
  126. }
  127. /*
  128. * This function unregisters the device and performs all the necessary
  129. * cleanups.
  130. *
  131. * The following cleanup operations are performed -
  132. * - Free the timers
  133. * - Free beacon buffers
  134. * - Free private structures
  135. * - Free adapter structure
  136. */
  137. static int mwifiex_unregister(struct mwifiex_adapter *adapter)
  138. {
  139. s32 i = 0;
  140. del_timer(&adapter->cmd_timer);
  141. /* Free private structures */
  142. for (i = 0; i < adapter->priv_num; i++) {
  143. if (adapter->priv[i]) {
  144. mwifiex_free_curr_bcn(adapter->priv[i]);
  145. kfree(adapter->priv[i]);
  146. }
  147. }
  148. kfree(adapter);
  149. return 0;
  150. }
  151. /*
  152. * The main process.
  153. *
  154. * This function is the main procedure of the driver and handles various driver
  155. * operations. It runs in a loop and provides the core functionalities.
  156. *
  157. * The main responsibilities of this function are -
  158. * - Ensure concurrency control
  159. * - Handle pending interrupts and call interrupt handlers
  160. * - Wake up the card if required
  161. * - Handle command responses and call response handlers
  162. * - Handle events and call event handlers
  163. * - Execute pending commands
  164. * - Transmit pending data packets
  165. */
  166. int mwifiex_main_process(struct mwifiex_adapter *adapter)
  167. {
  168. int ret = 0;
  169. unsigned long flags;
  170. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  171. /* Check if already processing */
  172. if (adapter->mwifiex_processing) {
  173. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  174. goto exit_main_proc;
  175. } else {
  176. adapter->mwifiex_processing = true;
  177. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  178. }
  179. process_start:
  180. do {
  181. if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
  182. (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
  183. break;
  184. /* Handle pending interrupt if any */
  185. if (adapter->int_status) {
  186. if (adapter->hs_activated)
  187. mwifiex_process_hs_config(adapter);
  188. adapter->if_ops.process_int_status(adapter);
  189. }
  190. /* Need to wake up the card ? */
  191. if ((adapter->ps_state == PS_STATE_SLEEP) &&
  192. (adapter->pm_wakeup_card_req &&
  193. !adapter->pm_wakeup_fw_try) &&
  194. (is_command_pending(adapter)
  195. || !mwifiex_wmm_lists_empty(adapter))) {
  196. adapter->pm_wakeup_fw_try = true;
  197. adapter->if_ops.wakeup(adapter);
  198. continue;
  199. }
  200. if (IS_CARD_RX_RCVD(adapter)) {
  201. adapter->pm_wakeup_fw_try = false;
  202. if (adapter->ps_state == PS_STATE_SLEEP)
  203. adapter->ps_state = PS_STATE_AWAKE;
  204. } else {
  205. /* We have tried to wakeup the card already */
  206. if (adapter->pm_wakeup_fw_try)
  207. break;
  208. if (adapter->ps_state != PS_STATE_AWAKE ||
  209. adapter->tx_lock_flag)
  210. break;
  211. if (adapter->scan_processing || adapter->data_sent
  212. || mwifiex_wmm_lists_empty(adapter)) {
  213. if (adapter->cmd_sent || adapter->curr_cmd
  214. || (!is_command_pending(adapter)))
  215. break;
  216. }
  217. }
  218. /* Check for Cmd Resp */
  219. if (adapter->cmd_resp_received) {
  220. adapter->cmd_resp_received = false;
  221. mwifiex_process_cmdresp(adapter);
  222. /* call mwifiex back when init_fw is done */
  223. if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
  224. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  225. mwifiex_init_fw_complete(adapter);
  226. }
  227. }
  228. /* Check for event */
  229. if (adapter->event_received) {
  230. adapter->event_received = false;
  231. mwifiex_process_event(adapter);
  232. }
  233. /* Check if we need to confirm Sleep Request
  234. received previously */
  235. if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
  236. if (!adapter->cmd_sent && !adapter->curr_cmd)
  237. mwifiex_check_ps_cond(adapter);
  238. }
  239. /* * The ps_state may have been changed during processing of
  240. * Sleep Request event.
  241. */
  242. if ((adapter->ps_state == PS_STATE_SLEEP)
  243. || (adapter->ps_state == PS_STATE_PRE_SLEEP)
  244. || (adapter->ps_state == PS_STATE_SLEEP_CFM)
  245. || adapter->tx_lock_flag)
  246. continue;
  247. if (!adapter->cmd_sent && !adapter->curr_cmd) {
  248. if (mwifiex_exec_next_cmd(adapter) == -1) {
  249. ret = -1;
  250. break;
  251. }
  252. }
  253. if (!adapter->scan_processing && !adapter->data_sent &&
  254. !mwifiex_wmm_lists_empty(adapter)) {
  255. mwifiex_wmm_process_tx(adapter);
  256. if (adapter->hs_activated) {
  257. adapter->is_hs_configured = false;
  258. mwifiex_hs_activated_event
  259. (mwifiex_get_priv
  260. (adapter, MWIFIEX_BSS_ROLE_ANY),
  261. false);
  262. }
  263. }
  264. if (adapter->delay_null_pkt && !adapter->cmd_sent &&
  265. !adapter->curr_cmd && !is_command_pending(adapter)
  266. && mwifiex_wmm_lists_empty(adapter)) {
  267. if (!mwifiex_send_null_packet
  268. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  269. MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
  270. MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
  271. adapter->delay_null_pkt = false;
  272. adapter->ps_state = PS_STATE_SLEEP;
  273. }
  274. break;
  275. }
  276. } while (true);
  277. if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter))
  278. goto process_start;
  279. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  280. adapter->mwifiex_processing = false;
  281. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  282. exit_main_proc:
  283. if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
  284. mwifiex_shutdown_drv(adapter);
  285. return ret;
  286. }
  287. /*
  288. * This function initializes the software.
  289. *
  290. * The main work includes allocating and initializing the adapter structure
  291. * and initializing the private structures.
  292. */
  293. static int
  294. mwifiex_init_sw(void *card, struct mwifiex_if_ops *if_ops, void **pmwifiex)
  295. {
  296. int i;
  297. struct mwifiex_device device;
  298. struct mwifiex_drv_mode *drv_mode_ptr;
  299. /* find mwifiex_drv_mode entry from mwifiex_drv_mode_tbl */
  300. drv_mode_ptr = NULL;
  301. for (i = 0; i < ARRAY_SIZE(mwifiex_drv_mode_tbl); i++) {
  302. if (mwifiex_drv_mode_tbl[i].drv_mode == drv_mode) {
  303. drv_mode_ptr = &mwifiex_drv_mode_tbl[i];
  304. break;
  305. }
  306. }
  307. if (!drv_mode_ptr) {
  308. pr_err("invalid drv_mode=%d\n", drv_mode);
  309. return -1;
  310. }
  311. memset(&device, 0, sizeof(struct mwifiex_device));
  312. for (i = 0; i < drv_mode_ptr->intf_num; i++) {
  313. device.bss_attr[i].bss_type =
  314. drv_mode_ptr->bss_attr[i].bss_type;
  315. device.bss_attr[i].frame_type =
  316. drv_mode_ptr->bss_attr[i].frame_type;
  317. device.bss_attr[i].active = drv_mode_ptr->bss_attr[i].active;
  318. device.bss_attr[i].bss_priority =
  319. drv_mode_ptr->bss_attr[i].bss_priority;
  320. device.bss_attr[i].bss_num = drv_mode_ptr->bss_attr[i].bss_num;
  321. }
  322. if (mwifiex_register(card, if_ops, &device, pmwifiex))
  323. return -1;
  324. return 0;
  325. }
  326. /*
  327. * This function frees the adapter structure.
  328. *
  329. * Additionally, this closes the netlink socket, frees the timers
  330. * and private structures.
  331. */
  332. static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
  333. {
  334. if (!adapter) {
  335. pr_err("%s: adapter is NULL\n", __func__);
  336. return;
  337. }
  338. mwifiex_unregister(adapter);
  339. pr_debug("info: %s: free adapter\n", __func__);
  340. }
  341. /*
  342. * This function initializes the hardware and firmware.
  343. *
  344. * The main initialization steps followed are -
  345. * - Download the correct firmware to card
  346. * - Allocate and initialize the adapter structure
  347. * - Initialize the private structures
  348. * - Issue the init commands to firmware
  349. */
  350. static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
  351. {
  352. int ret = 0;
  353. int err;
  354. struct mwifiex_fw_image fw;
  355. memset(&fw, 0, sizeof(struct mwifiex_fw_image));
  356. switch (adapter->revision_id) {
  357. case SD8787_W0:
  358. case SD8787_W1:
  359. strcpy(fw_name, SD8787_W1_FW_NAME);
  360. break;
  361. case SD8787_A0:
  362. case SD8787_A1:
  363. strcpy(fw_name, SD8787_AX_FW_NAME);
  364. break;
  365. default:
  366. break;
  367. }
  368. err = request_firmware(&adapter->firmware, fw_name, adapter->dev);
  369. if (err < 0) {
  370. dev_err(adapter->dev, "request_firmware() returned"
  371. " error code %#x\n", err);
  372. ret = -1;
  373. goto done;
  374. }
  375. fw.fw_buf = (u8 *) adapter->firmware->data;
  376. fw.fw_len = adapter->firmware->size;
  377. ret = mwifiex_dnld_fw(adapter, &fw);
  378. if (ret == -1)
  379. goto done;
  380. dev_notice(adapter->dev, "WLAN FW is active\n");
  381. adapter->init_wait_q_woken = false;
  382. ret = mwifiex_init_fw(adapter);
  383. if (ret == -1) {
  384. goto done;
  385. } else if (!ret) {
  386. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  387. goto done;
  388. }
  389. /* Wait for mwifiex_init to complete */
  390. wait_event_interruptible(adapter->init_wait_q,
  391. adapter->init_wait_q_woken);
  392. if (adapter->hw_status != MWIFIEX_HW_STATUS_READY) {
  393. ret = -1;
  394. goto done;
  395. }
  396. ret = 0;
  397. done:
  398. if (adapter->firmware)
  399. release_firmware(adapter->firmware);
  400. if (ret)
  401. ret = -1;
  402. return ret;
  403. }
  404. /*
  405. * This function fills a driver buffer.
  406. *
  407. * The function associates a given SKB with the provided driver buffer
  408. * and also updates some of the SKB parameters, including IP header,
  409. * priority and timestamp.
  410. */
  411. static void
  412. mwifiex_fill_buffer(struct sk_buff *skb)
  413. {
  414. struct ethhdr *eth = NULL;
  415. struct iphdr *iph;
  416. struct timeval tv;
  417. u8 tid = 0;
  418. eth = (struct ethhdr *) skb->data;
  419. switch (eth->h_proto) {
  420. case __constant_htons(ETH_P_IP):
  421. iph = ip_hdr(skb);
  422. tid = IPTOS_PREC(iph->tos);
  423. pr_debug("data: packet type ETH_P_IP: %04x, tid=%#x prio=%#x\n",
  424. eth->h_proto, tid, skb->priority);
  425. break;
  426. case __constant_htons(ETH_P_ARP):
  427. pr_debug("data: ARP packet: %04x\n", eth->h_proto);
  428. default:
  429. break;
  430. }
  431. /* Offset for TOS field in the IP header */
  432. #define IPTOS_OFFSET 5
  433. tid = (tid >> IPTOS_OFFSET);
  434. skb->priority = tid;
  435. /* Record the current time the packet was queued; used to
  436. determine the amount of time the packet was queued in
  437. the driver before it was sent to the firmware.
  438. The delay is then sent along with the packet to the
  439. firmware for aggregate delay calculation for stats and
  440. MSDU lifetime expiry.
  441. */
  442. do_gettimeofday(&tv);
  443. skb->tstamp = timeval_to_ktime(tv);
  444. return;
  445. }
  446. /*
  447. * CFG802.11 network device handler for open.
  448. *
  449. * Starts the data queue.
  450. */
  451. static int
  452. mwifiex_open(struct net_device *dev)
  453. {
  454. netif_start_queue(dev);
  455. return 0;
  456. }
  457. /*
  458. * CFG802.11 network device handler for close.
  459. */
  460. static int
  461. mwifiex_close(struct net_device *dev)
  462. {
  463. return 0;
  464. }
  465. /*
  466. * CFG802.11 network device handler for data transmission.
  467. */
  468. static int
  469. mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  470. {
  471. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  472. struct sk_buff *new_skb = NULL;
  473. struct mwifiex_txinfo *tx_info;
  474. dev_dbg(priv->adapter->dev, "data: %lu BSS(%d): Data <= kernel\n",
  475. jiffies, priv->bss_index);
  476. if (priv->adapter->surprise_removed) {
  477. kfree(skb);
  478. priv->stats.tx_dropped++;
  479. return 0;
  480. }
  481. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  482. dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
  483. kfree(skb);
  484. priv->stats.tx_dropped++;
  485. return 0;
  486. }
  487. if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
  488. dev_dbg(priv->adapter->dev,
  489. "data: Tx: insufficient skb headroom %d\n",
  490. skb_headroom(skb));
  491. /* Insufficient skb headroom - allocate a new skb */
  492. new_skb =
  493. skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
  494. if (unlikely(!new_skb)) {
  495. dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
  496. kfree(skb);
  497. priv->stats.tx_dropped++;
  498. return 0;
  499. }
  500. kfree_skb(skb);
  501. skb = new_skb;
  502. dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
  503. skb_headroom(skb));
  504. }
  505. tx_info = MWIFIEX_SKB_TXCB(skb);
  506. tx_info->bss_index = priv->bss_index;
  507. mwifiex_fill_buffer(skb);
  508. mwifiex_wmm_add_buf_txqueue(priv->adapter, skb);
  509. atomic_inc(&priv->adapter->tx_pending);
  510. if (atomic_read(&priv->adapter->tx_pending) >= MAX_TX_PENDING) {
  511. netif_stop_queue(priv->netdev);
  512. dev->trans_start = jiffies;
  513. }
  514. queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
  515. return 0;
  516. }
  517. /*
  518. * CFG802.11 network device handler for setting MAC address.
  519. */
  520. static int
  521. mwifiex_set_mac_address(struct net_device *dev, void *addr)
  522. {
  523. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  524. struct sockaddr *hw_addr = (struct sockaddr *) addr;
  525. memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
  526. if (mwifiex_request_set_mac_address(priv)) {
  527. dev_err(priv->adapter->dev, "set MAC address failed\n");
  528. return -EFAULT;
  529. }
  530. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  531. return 0;
  532. }
  533. /*
  534. * CFG802.11 network device handler for setting multicast list.
  535. */
  536. static void mwifiex_set_multicast_list(struct net_device *dev)
  537. {
  538. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  539. mwifiex_request_set_multicast_list(priv, dev);
  540. }
  541. /*
  542. * CFG802.11 network device handler for transmission timeout.
  543. */
  544. static void
  545. mwifiex_tx_timeout(struct net_device *dev)
  546. {
  547. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  548. dev_err(priv->adapter->dev, "%lu : Tx timeout, bss_index=%d\n",
  549. jiffies, priv->bss_index);
  550. dev->trans_start = jiffies;
  551. priv->num_tx_timeout++;
  552. }
  553. /*
  554. * CFG802.11 network device handler for statistics retrieval.
  555. */
  556. static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
  557. {
  558. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  559. return &priv->stats;
  560. }
  561. /* Network device handlers */
  562. static const struct net_device_ops mwifiex_netdev_ops = {
  563. .ndo_open = mwifiex_open,
  564. .ndo_stop = mwifiex_close,
  565. .ndo_start_xmit = mwifiex_hard_start_xmit,
  566. .ndo_set_mac_address = mwifiex_set_mac_address,
  567. .ndo_tx_timeout = mwifiex_tx_timeout,
  568. .ndo_get_stats = mwifiex_get_stats,
  569. .ndo_set_multicast_list = mwifiex_set_multicast_list,
  570. };
  571. /*
  572. * This function initializes the private structure parameters.
  573. *
  574. * The following wait queues are initialized -
  575. * - IOCTL wait queue
  576. * - Command wait queue
  577. * - Statistics wait queue
  578. *
  579. * ...and the following default parameters are set -
  580. * - Current key index : Set to 0
  581. * - Rate index : Set to auto
  582. * - Media connected : Set to disconnected
  583. * - Adhoc link sensed : Set to false
  584. * - Nick name : Set to null
  585. * - Number of Tx timeout : Set to 0
  586. * - Device address : Set to current address
  587. *
  588. * In addition, the CFG80211 work queue is also created.
  589. */
  590. static void
  591. mwifiex_init_priv_params(struct mwifiex_private *priv, struct net_device *dev)
  592. {
  593. dev->netdev_ops = &mwifiex_netdev_ops;
  594. /* Initialize private structure */
  595. init_waitqueue_head(&priv->ioctl_wait_q);
  596. init_waitqueue_head(&priv->cmd_wait_q);
  597. init_waitqueue_head(&priv->w_stats_wait_q);
  598. priv->current_key_index = 0;
  599. priv->media_connected = false;
  600. memset(&priv->nick_name, 0, sizeof(priv->nick_name));
  601. priv->num_tx_timeout = 0;
  602. priv->workqueue = create_singlethread_workqueue("cfg80211_wq");
  603. INIT_WORK(&priv->cfg_workqueue, mwifiex_cfg80211_results);
  604. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  605. }
  606. /*
  607. * This function adds a new logical interface.
  608. *
  609. * It allocates, initializes and registers the interface by performing
  610. * the following opearations -
  611. * - Allocate a new net device structure
  612. * - Assign device name
  613. * - Register the new device with CFG80211 subsystem
  614. * - Initialize semaphore and private structure
  615. * - Register the new device with kernel
  616. * - Create the complete debug FS structure if configured
  617. */
  618. static struct mwifiex_private *mwifiex_add_interface(
  619. struct mwifiex_adapter *adapter,
  620. u8 bss_index, u8 bss_type)
  621. {
  622. struct net_device *dev = NULL;
  623. struct mwifiex_private *priv = NULL;
  624. void *mdev_priv = NULL;
  625. dev = alloc_netdev_mq(sizeof(struct mwifiex_private *), "mlan%d",
  626. ether_setup, 1);
  627. if (!dev) {
  628. dev_err(adapter->dev, "no memory available for netdevice\n");
  629. goto error;
  630. }
  631. if (dev_alloc_name(dev, dev->name)) {
  632. dev_err(adapter->dev, "unable to alloc name for netdevice\n");
  633. goto error;
  634. }
  635. if (mwifiex_register_cfg80211(dev, adapter->priv[bss_index]->curr_addr,
  636. adapter->priv[bss_index]) != 0) {
  637. dev_err(adapter->dev, "cannot register netdevice with cfg80211\n");
  638. goto error;
  639. }
  640. /* Save the priv pointer in netdev */
  641. priv = adapter->priv[bss_index];
  642. mdev_priv = netdev_priv(dev);
  643. *((unsigned long *) mdev_priv) = (unsigned long) priv;
  644. priv->netdev = dev;
  645. sema_init(&priv->async_sem, 1);
  646. priv->scan_pending_on_block = false;
  647. mwifiex_init_priv_params(priv, dev);
  648. SET_NETDEV_DEV(dev, adapter->dev);
  649. /* Register network device */
  650. if (register_netdev(dev)) {
  651. dev_err(adapter->dev, "cannot register virtual network device\n");
  652. goto error;
  653. }
  654. dev_dbg(adapter->dev, "info: %s: Marvell 802.11 Adapter\n", dev->name);
  655. #ifdef CONFIG_DEBUG_FS
  656. mwifiex_dev_debugfs_init(priv);
  657. #endif
  658. return priv;
  659. error:
  660. if (dev)
  661. free_netdev(dev);
  662. return NULL;
  663. }
  664. /*
  665. * This function removes a logical interface.
  666. *
  667. * It deregisters, resets and frees the interface by performing
  668. * the following operations -
  669. * - Disconnect the device if connected, send wireless event to
  670. * notify applications.
  671. * - Remove the debug FS structure if configured
  672. * - Unregister the device from kernel
  673. * - Free the net device structure
  674. * - Cancel all works and destroy work queue
  675. * - Unregister and free the wireless device from CFG80211 subsystem
  676. */
  677. static void
  678. mwifiex_remove_interface(struct mwifiex_adapter *adapter, u8 bss_index)
  679. {
  680. struct net_device *dev = NULL;
  681. struct mwifiex_private *priv = adapter->priv[bss_index];
  682. if (!priv)
  683. return;
  684. dev = priv->netdev;
  685. if (priv->media_connected)
  686. priv->media_connected = false;
  687. #ifdef CONFIG_DEBUG_FS
  688. mwifiex_dev_debugfs_remove(priv);
  689. #endif
  690. /* Last reference is our one */
  691. dev_dbg(adapter->dev, "info: %s: refcnt = %d\n",
  692. dev->name, netdev_refcnt_read(dev));
  693. if (dev->reg_state == NETREG_REGISTERED)
  694. unregister_netdev(dev);
  695. /* Clear the priv in adapter */
  696. priv->netdev = NULL;
  697. if (dev)
  698. free_netdev(dev);
  699. cancel_work_sync(&priv->cfg_workqueue);
  700. flush_workqueue(priv->workqueue);
  701. destroy_workqueue(priv->workqueue);
  702. wiphy_unregister(priv->wdev->wiphy);
  703. wiphy_free(priv->wdev->wiphy);
  704. kfree(priv->wdev);
  705. return;
  706. }
  707. /*
  708. * Sends IOCTL request to shutdown firmware.
  709. *
  710. * This function allocates the IOCTL request buffer, fills it
  711. * with requisite parameters and calls the IOCTL handler.
  712. */
  713. int mwifiex_shutdown_fw(struct mwifiex_private *priv, u8 wait_option)
  714. {
  715. struct mwifiex_wait_queue *wait = NULL;
  716. int status = 0;
  717. /* Allocate an IOCTL request buffer */
  718. wait = mwifiex_alloc_fill_wait_queue(priv, wait_option);
  719. if (!wait)
  720. return -ENOMEM;
  721. status = mwifiex_misc_ioctl_init_shutdown(priv->adapter, wait,
  722. MWIFIEX_FUNC_SHUTDOWN);
  723. status = mwifiex_request_ioctl(priv, wait, status, wait_option);
  724. kfree(wait);
  725. return status;
  726. }
  727. EXPORT_SYMBOL_GPL(mwifiex_shutdown_fw);
  728. /*
  729. * This function check if command is pending.
  730. */
  731. int is_command_pending(struct mwifiex_adapter *adapter)
  732. {
  733. unsigned long flags;
  734. int is_cmd_pend_q_empty;
  735. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  736. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  737. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  738. return !is_cmd_pend_q_empty;
  739. }
  740. /*
  741. * This function returns the correct private structure pointer based
  742. * upon the BSS number.
  743. */
  744. struct mwifiex_private *
  745. mwifiex_bss_index_to_priv(struct mwifiex_adapter *adapter, u8 bss_index)
  746. {
  747. if (!adapter || (bss_index >= adapter->priv_num))
  748. return NULL;
  749. return adapter->priv[bss_index];
  750. }
  751. /*
  752. * This is the main work queue function.
  753. *
  754. * It handles the main process, which in turn handles the complete
  755. * driver operations.
  756. */
  757. static void mwifiex_main_work_queue(struct work_struct *work)
  758. {
  759. struct mwifiex_adapter *adapter =
  760. container_of(work, struct mwifiex_adapter, main_work);
  761. if (adapter->surprise_removed)
  762. return;
  763. mwifiex_main_process(adapter);
  764. }
  765. /*
  766. * This function cancels all works in the queue and destroys
  767. * the main workqueue.
  768. */
  769. static void
  770. mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
  771. {
  772. flush_workqueue(adapter->workqueue);
  773. destroy_workqueue(adapter->workqueue);
  774. adapter->workqueue = NULL;
  775. }
  776. /*
  777. * This function adds the card.
  778. *
  779. * This function follows the following major steps to set up the device -
  780. * - Initialize software. This includes probing the card, registering
  781. * the interface operations table, and allocating/initializing the
  782. * adapter structure
  783. * - Set up the netlink socket
  784. * - Create and start the main work queue
  785. * - Register the device
  786. * - Initialize firmware and hardware
  787. * - Add logical interfaces
  788. */
  789. int
  790. mwifiex_add_card(void *card, struct semaphore *sem,
  791. struct mwifiex_if_ops *if_ops)
  792. {
  793. int status = 0;
  794. int i;
  795. struct mwifiex_adapter *adapter = NULL;
  796. struct mwifiex_drv_mode *drv_mode_info = &mwifiex_drv_mode_tbl[0];
  797. if (down_interruptible(sem))
  798. goto exit_sem_err;
  799. if (mwifiex_init_sw(card, if_ops, (void **) &adapter)) {
  800. pr_err("%s: software init failed\n", __func__);
  801. goto err_init_sw;
  802. }
  803. adapter->drv_mode = drv_mode_info;
  804. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  805. /* PnP and power profile */
  806. adapter->surprise_removed = false;
  807. init_waitqueue_head(&adapter->init_wait_q);
  808. adapter->is_suspended = false;
  809. adapter->hs_activated = false;
  810. init_waitqueue_head(&adapter->hs_activate_wait_q);
  811. /* Create workqueue */
  812. adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
  813. if (!adapter->workqueue)
  814. goto err_kmalloc;
  815. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  816. /* Register the device. Fill up the private data structure with relevant
  817. information from the card and request for the required IRQ. */
  818. if (adapter->if_ops.register_dev(adapter)) {
  819. pr_err("%s: failed to register mwifiex device\n", __func__);
  820. goto err_registerdev;
  821. }
  822. /* Init FW and HW */
  823. if (mwifiex_init_hw_fw(adapter)) {
  824. pr_err("%s: firmware init failed\n", __func__);
  825. goto err_init_fw;
  826. }
  827. /* Add interfaces */
  828. for (i = 0; i < drv_mode_info->intf_num; i++) {
  829. if (!mwifiex_add_interface(adapter, i,
  830. adapter->drv_mode->bss_attr[i].bss_type)) {
  831. status = -1;
  832. break;
  833. }
  834. }
  835. if (status)
  836. goto err_add_intf;
  837. up(sem);
  838. return 0;
  839. err_add_intf:
  840. for (i = 0; i < adapter->priv_num; i++)
  841. mwifiex_remove_interface(adapter, i);
  842. err_init_fw:
  843. /* Unregister device */
  844. pr_debug("info: %s: unregister device\n", __func__);
  845. adapter->if_ops.unregister_dev(adapter);
  846. err_registerdev:
  847. adapter->surprise_removed = true;
  848. mwifiex_terminate_workqueue(adapter);
  849. err_kmalloc:
  850. if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
  851. (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
  852. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  853. adapter->init_wait_q_woken = false;
  854. status = mwifiex_shutdown_drv(adapter);
  855. if (status == -EINPROGRESS)
  856. wait_event_interruptible(adapter->init_wait_q,
  857. adapter->init_wait_q_woken);
  858. }
  859. mwifiex_free_adapter(adapter);
  860. err_init_sw:
  861. up(sem);
  862. exit_sem_err:
  863. return -1;
  864. }
  865. EXPORT_SYMBOL_GPL(mwifiex_add_card);
  866. /*
  867. * This function removes the card.
  868. *
  869. * This function follows the following major steps to remove the device -
  870. * - Stop data traffic
  871. * - Shutdown firmware
  872. * - Remove the logical interfaces
  873. * - Terminate the work queue
  874. * - Unregister the device
  875. * - Free the adapter structure
  876. */
  877. int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
  878. {
  879. struct mwifiex_private *priv = NULL;
  880. int status;
  881. int i;
  882. if (down_interruptible(sem))
  883. goto exit_sem_err;
  884. if (!adapter)
  885. goto exit_remove;
  886. adapter->surprise_removed = true;
  887. /* Stop data */
  888. for (i = 0; i < adapter->priv_num; i++) {
  889. priv = adapter->priv[i];
  890. if (priv) {
  891. if (!netif_queue_stopped(priv->netdev))
  892. netif_stop_queue(priv->netdev);
  893. if (netif_carrier_ok(priv->netdev))
  894. netif_carrier_off(priv->netdev);
  895. }
  896. }
  897. dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
  898. adapter->init_wait_q_woken = false;
  899. status = mwifiex_shutdown_drv(adapter);
  900. if (status == -EINPROGRESS)
  901. wait_event_interruptible(adapter->init_wait_q,
  902. adapter->init_wait_q_woken);
  903. dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
  904. if (atomic_read(&adapter->rx_pending) ||
  905. atomic_read(&adapter->tx_pending) ||
  906. atomic_read(&adapter->ioctl_pending)) {
  907. dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
  908. "ioctl_pending=%d\n",
  909. atomic_read(&adapter->rx_pending),
  910. atomic_read(&adapter->tx_pending),
  911. atomic_read(&adapter->ioctl_pending));
  912. }
  913. /* Remove interface */
  914. for (i = 0; i < adapter->priv_num; i++)
  915. mwifiex_remove_interface(adapter, i);
  916. mwifiex_terminate_workqueue(adapter);
  917. /* Unregister device */
  918. dev_dbg(adapter->dev, "info: unregister device\n");
  919. adapter->if_ops.unregister_dev(adapter);
  920. /* Free adapter structure */
  921. dev_dbg(adapter->dev, "info: free adapter\n");
  922. mwifiex_free_adapter(adapter);
  923. exit_remove:
  924. up(sem);
  925. exit_sem_err:
  926. return 0;
  927. }
  928. EXPORT_SYMBOL_GPL(mwifiex_remove_card);
  929. /*
  930. * This function initializes the module.
  931. *
  932. * The debug FS is also initialized if configured.
  933. */
  934. static int
  935. mwifiex_init_module(void)
  936. {
  937. #ifdef CONFIG_DEBUG_FS
  938. mwifiex_debugfs_init();
  939. #endif
  940. return 0;
  941. }
  942. /*
  943. * This function cleans up the module.
  944. *
  945. * The debug FS is removed if available.
  946. */
  947. static void
  948. mwifiex_cleanup_module(void)
  949. {
  950. #ifdef CONFIG_DEBUG_FS
  951. mwifiex_debugfs_remove();
  952. #endif
  953. }
  954. module_init(mwifiex_init_module);
  955. module_exit(mwifiex_cleanup_module);
  956. MODULE_AUTHOR("Marvell International Ltd.");
  957. MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
  958. MODULE_VERSION(VERSION);
  959. MODULE_LICENSE("GPL v2");