main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. /*
  26. * This function registers the device and performs all the necessary
  27. * initializations.
  28. *
  29. * The following initialization operations are performed -
  30. * - Allocate adapter structure
  31. * - Save interface specific operations table in adapter
  32. * - Call interface specific initialization routine
  33. * - Allocate private structures
  34. * - Set default adapter structure parameters
  35. * - Initialize locks
  36. *
  37. * In case of any errors during inittialization, this function also ensures
  38. * proper cleanup before exiting.
  39. */
  40. static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
  41. void **padapter)
  42. {
  43. struct mwifiex_adapter *adapter;
  44. int i;
  45. adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
  46. if (!adapter)
  47. return -ENOMEM;
  48. *padapter = adapter;
  49. adapter->card = card;
  50. /* Save interface specific operations in adapter */
  51. memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
  52. /* card specific initialization has been deferred until now .. */
  53. if (adapter->if_ops.init_if)
  54. if (adapter->if_ops.init_if(adapter))
  55. goto error;
  56. adapter->priv_num = 0;
  57. for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
  58. /* Allocate memory for private structure */
  59. adapter->priv[i] =
  60. kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
  61. if (!adapter->priv[i])
  62. goto error;
  63. adapter->priv[i]->adapter = adapter;
  64. adapter->priv_num++;
  65. }
  66. mwifiex_init_lock_list(adapter);
  67. init_timer(&adapter->cmd_timer);
  68. adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
  69. adapter->cmd_timer.data = (unsigned long) adapter;
  70. return 0;
  71. error:
  72. dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
  73. for (i = 0; i < adapter->priv_num; i++)
  74. kfree(adapter->priv[i]);
  75. kfree(adapter);
  76. return -1;
  77. }
  78. /*
  79. * This function unregisters the device and performs all the necessary
  80. * cleanups.
  81. *
  82. * The following cleanup operations are performed -
  83. * - Free the timers
  84. * - Free beacon buffers
  85. * - Free private structures
  86. * - Free adapter structure
  87. */
  88. static int mwifiex_unregister(struct mwifiex_adapter *adapter)
  89. {
  90. s32 i;
  91. del_timer(&adapter->cmd_timer);
  92. /* Free private structures */
  93. for (i = 0; i < adapter->priv_num; i++) {
  94. if (adapter->priv[i]) {
  95. mwifiex_free_curr_bcn(adapter->priv[i]);
  96. kfree(adapter->priv[i]);
  97. }
  98. }
  99. kfree(adapter);
  100. return 0;
  101. }
  102. /*
  103. * The main process.
  104. *
  105. * This function is the main procedure of the driver and handles various driver
  106. * operations. It runs in a loop and provides the core functionalities.
  107. *
  108. * The main responsibilities of this function are -
  109. * - Ensure concurrency control
  110. * - Handle pending interrupts and call interrupt handlers
  111. * - Wake up the card if required
  112. * - Handle command responses and call response handlers
  113. * - Handle events and call event handlers
  114. * - Execute pending commands
  115. * - Transmit pending data packets
  116. */
  117. int mwifiex_main_process(struct mwifiex_adapter *adapter)
  118. {
  119. int ret = 0;
  120. unsigned long flags;
  121. struct sk_buff *skb;
  122. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  123. /* Check if already processing */
  124. if (adapter->mwifiex_processing) {
  125. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  126. goto exit_main_proc;
  127. } else {
  128. adapter->mwifiex_processing = true;
  129. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  130. }
  131. process_start:
  132. do {
  133. if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
  134. (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
  135. break;
  136. /* Handle pending interrupt if any */
  137. if (adapter->int_status) {
  138. if (adapter->hs_activated)
  139. mwifiex_process_hs_config(adapter);
  140. if (adapter->if_ops.process_int_status)
  141. adapter->if_ops.process_int_status(adapter);
  142. }
  143. /* Need to wake up the card ? */
  144. if ((adapter->ps_state == PS_STATE_SLEEP) &&
  145. (adapter->pm_wakeup_card_req &&
  146. !adapter->pm_wakeup_fw_try) &&
  147. (is_command_pending(adapter) ||
  148. !mwifiex_wmm_lists_empty(adapter))) {
  149. adapter->pm_wakeup_fw_try = true;
  150. adapter->if_ops.wakeup(adapter);
  151. continue;
  152. }
  153. if (IS_CARD_RX_RCVD(adapter)) {
  154. adapter->pm_wakeup_fw_try = false;
  155. if (adapter->ps_state == PS_STATE_SLEEP)
  156. adapter->ps_state = PS_STATE_AWAKE;
  157. } else {
  158. /* We have tried to wakeup the card already */
  159. if (adapter->pm_wakeup_fw_try)
  160. break;
  161. if (adapter->ps_state != PS_STATE_AWAKE ||
  162. adapter->tx_lock_flag)
  163. break;
  164. if ((adapter->scan_processing &&
  165. !adapter->scan_delay_cnt) || adapter->data_sent ||
  166. mwifiex_wmm_lists_empty(adapter)) {
  167. if (adapter->cmd_sent || adapter->curr_cmd ||
  168. (!is_command_pending(adapter)))
  169. break;
  170. }
  171. }
  172. /* Check Rx data for USB */
  173. if (adapter->iface_type == MWIFIEX_USB)
  174. while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
  175. mwifiex_handle_rx_packet(adapter, skb);
  176. /* Check for Cmd Resp */
  177. if (adapter->cmd_resp_received) {
  178. adapter->cmd_resp_received = false;
  179. mwifiex_process_cmdresp(adapter);
  180. /* call mwifiex back when init_fw is done */
  181. if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
  182. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  183. mwifiex_init_fw_complete(adapter);
  184. }
  185. }
  186. /* Check for event */
  187. if (adapter->event_received) {
  188. adapter->event_received = false;
  189. mwifiex_process_event(adapter);
  190. }
  191. /* Check if we need to confirm Sleep Request
  192. received previously */
  193. if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
  194. if (!adapter->cmd_sent && !adapter->curr_cmd)
  195. mwifiex_check_ps_cond(adapter);
  196. }
  197. /* * The ps_state may have been changed during processing of
  198. * Sleep Request event.
  199. */
  200. if ((adapter->ps_state == PS_STATE_SLEEP) ||
  201. (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
  202. (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
  203. adapter->tx_lock_flag)
  204. continue;
  205. if (!adapter->cmd_sent && !adapter->curr_cmd) {
  206. if (mwifiex_exec_next_cmd(adapter) == -1) {
  207. ret = -1;
  208. break;
  209. }
  210. }
  211. if ((!adapter->scan_processing || adapter->scan_delay_cnt) &&
  212. !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
  213. mwifiex_wmm_process_tx(adapter);
  214. if (adapter->hs_activated) {
  215. adapter->is_hs_configured = false;
  216. mwifiex_hs_activated_event
  217. (mwifiex_get_priv
  218. (adapter, MWIFIEX_BSS_ROLE_ANY),
  219. false);
  220. }
  221. }
  222. if (adapter->delay_null_pkt && !adapter->cmd_sent &&
  223. !adapter->curr_cmd && !is_command_pending(adapter) &&
  224. mwifiex_wmm_lists_empty(adapter)) {
  225. if (!mwifiex_send_null_packet
  226. (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  227. MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
  228. MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
  229. adapter->delay_null_pkt = false;
  230. adapter->ps_state = PS_STATE_SLEEP;
  231. }
  232. break;
  233. }
  234. } while (true);
  235. if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter))
  236. goto process_start;
  237. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  238. adapter->mwifiex_processing = false;
  239. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  240. exit_main_proc:
  241. if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
  242. mwifiex_shutdown_drv(adapter);
  243. return ret;
  244. }
  245. EXPORT_SYMBOL_GPL(mwifiex_main_process);
  246. /*
  247. * This function frees the adapter structure.
  248. *
  249. * Additionally, this closes the netlink socket, frees the timers
  250. * and private structures.
  251. */
  252. static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
  253. {
  254. if (!adapter) {
  255. pr_err("%s: adapter is NULL\n", __func__);
  256. return;
  257. }
  258. mwifiex_unregister(adapter);
  259. pr_debug("info: %s: free adapter\n", __func__);
  260. }
  261. /*
  262. * This function gets firmware and initializes it.
  263. *
  264. * The main initialization steps followed are -
  265. * - Download the correct firmware to card
  266. * - Issue the init commands to firmware
  267. */
  268. static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
  269. {
  270. int ret;
  271. char fmt[64];
  272. struct mwifiex_private *priv;
  273. struct mwifiex_adapter *adapter = context;
  274. struct mwifiex_fw_image fw;
  275. if (!firmware) {
  276. dev_err(adapter->dev,
  277. "Failed to get firmware %s\n", adapter->fw_name);
  278. goto done;
  279. }
  280. memset(&fw, 0, sizeof(struct mwifiex_fw_image));
  281. adapter->firmware = firmware;
  282. fw.fw_buf = (u8 *) adapter->firmware->data;
  283. fw.fw_len = adapter->firmware->size;
  284. if (adapter->if_ops.dnld_fw)
  285. ret = adapter->if_ops.dnld_fw(adapter, &fw);
  286. else
  287. ret = mwifiex_dnld_fw(adapter, &fw);
  288. if (ret == -1)
  289. goto done;
  290. dev_notice(adapter->dev, "WLAN FW is active\n");
  291. adapter->init_wait_q_woken = false;
  292. ret = mwifiex_init_fw(adapter);
  293. if (ret == -1) {
  294. goto done;
  295. } else if (!ret) {
  296. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  297. goto done;
  298. }
  299. /* Wait for mwifiex_init to complete */
  300. wait_event_interruptible(adapter->init_wait_q,
  301. adapter->init_wait_q_woken);
  302. if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
  303. goto done;
  304. priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
  305. if (mwifiex_register_cfg80211(adapter)) {
  306. dev_err(adapter->dev, "cannot register with cfg80211\n");
  307. goto err_init_fw;
  308. }
  309. rtnl_lock();
  310. /* Create station interface by default */
  311. if (!mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
  312. NL80211_IFTYPE_STATION, NULL, NULL)) {
  313. dev_err(adapter->dev, "cannot create default STA interface\n");
  314. goto err_add_intf;
  315. }
  316. /* Create AP interface by default */
  317. if (!mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
  318. NL80211_IFTYPE_AP, NULL, NULL)) {
  319. dev_err(adapter->dev, "cannot create default AP interface\n");
  320. goto err_add_intf;
  321. }
  322. /* Create P2P interface by default */
  323. if (!mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
  324. NL80211_IFTYPE_P2P_CLIENT, NULL, NULL)) {
  325. dev_err(adapter->dev, "cannot create default P2P interface\n");
  326. goto err_add_intf;
  327. }
  328. rtnl_unlock();
  329. mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
  330. dev_notice(adapter->dev, "driver_version = %s\n", fmt);
  331. goto done;
  332. err_add_intf:
  333. mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
  334. rtnl_unlock();
  335. err_init_fw:
  336. pr_debug("info: %s: unregister device\n", __func__);
  337. adapter->if_ops.unregister_dev(adapter);
  338. done:
  339. release_firmware(adapter->firmware);
  340. complete(&adapter->fw_load);
  341. return;
  342. }
  343. /*
  344. * This function initializes the hardware and gets firmware.
  345. */
  346. static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
  347. {
  348. int ret;
  349. init_completion(&adapter->fw_load);
  350. ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
  351. adapter->dev, GFP_KERNEL, adapter,
  352. mwifiex_fw_dpc);
  353. if (ret < 0)
  354. dev_err(adapter->dev,
  355. "request_firmware_nowait() returned error %d\n", ret);
  356. return ret;
  357. }
  358. /*
  359. * CFG802.11 network device handler for open.
  360. *
  361. * Starts the data queue.
  362. */
  363. static int
  364. mwifiex_open(struct net_device *dev)
  365. {
  366. netif_tx_start_all_queues(dev);
  367. return 0;
  368. }
  369. /*
  370. * CFG802.11 network device handler for close.
  371. */
  372. static int
  373. mwifiex_close(struct net_device *dev)
  374. {
  375. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  376. if (priv->scan_request) {
  377. dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
  378. cfg80211_scan_done(priv->scan_request, 1);
  379. priv->scan_request = NULL;
  380. }
  381. return 0;
  382. }
  383. /*
  384. * Add buffer into wmm tx queue and queue work to transmit it.
  385. */
  386. int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
  387. {
  388. struct netdev_queue *txq;
  389. int index = mwifiex_1d_to_wmm_queue[skb->priority];
  390. if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
  391. txq = netdev_get_tx_queue(priv->netdev, index);
  392. if (!netif_tx_queue_stopped(txq)) {
  393. netif_tx_stop_queue(txq);
  394. dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
  395. }
  396. }
  397. atomic_inc(&priv->adapter->tx_pending);
  398. mwifiex_wmm_add_buf_txqueue(priv, skb);
  399. if (priv->adapter->scan_delay_cnt)
  400. atomic_set(&priv->adapter->is_tx_received, true);
  401. queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
  402. return 0;
  403. }
  404. /*
  405. * CFG802.11 network device handler for data transmission.
  406. */
  407. static int
  408. mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  409. {
  410. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  411. struct sk_buff *new_skb;
  412. struct mwifiex_txinfo *tx_info;
  413. struct timeval tv;
  414. dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
  415. jiffies, priv->bss_type, priv->bss_num);
  416. if (priv->adapter->surprise_removed) {
  417. kfree_skb(skb);
  418. priv->stats.tx_dropped++;
  419. return 0;
  420. }
  421. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  422. dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
  423. kfree_skb(skb);
  424. priv->stats.tx_dropped++;
  425. return 0;
  426. }
  427. if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
  428. dev_dbg(priv->adapter->dev,
  429. "data: Tx: insufficient skb headroom %d\n",
  430. skb_headroom(skb));
  431. /* Insufficient skb headroom - allocate a new skb */
  432. new_skb =
  433. skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
  434. if (unlikely(!new_skb)) {
  435. dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
  436. kfree_skb(skb);
  437. priv->stats.tx_dropped++;
  438. return 0;
  439. }
  440. kfree_skb(skb);
  441. skb = new_skb;
  442. dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
  443. skb_headroom(skb));
  444. }
  445. tx_info = MWIFIEX_SKB_TXCB(skb);
  446. tx_info->bss_num = priv->bss_num;
  447. tx_info->bss_type = priv->bss_type;
  448. /* Record the current time the packet was queued; used to
  449. * determine the amount of time the packet was queued in
  450. * the driver before it was sent to the firmware.
  451. * The delay is then sent along with the packet to the
  452. * firmware for aggregate delay calculation for stats and
  453. * MSDU lifetime expiry.
  454. */
  455. do_gettimeofday(&tv);
  456. skb->tstamp = timeval_to_ktime(tv);
  457. mwifiex_queue_tx_pkt(priv, skb);
  458. return 0;
  459. }
  460. /*
  461. * CFG802.11 network device handler for setting MAC address.
  462. */
  463. static int
  464. mwifiex_set_mac_address(struct net_device *dev, void *addr)
  465. {
  466. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  467. struct sockaddr *hw_addr = addr;
  468. int ret;
  469. memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
  470. /* Send request to firmware */
  471. ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
  472. HostCmd_ACT_GEN_SET, 0, NULL);
  473. if (!ret)
  474. memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
  475. else
  476. dev_err(priv->adapter->dev,
  477. "set mac address failed: ret=%d\n", ret);
  478. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  479. return ret;
  480. }
  481. /*
  482. * CFG802.11 network device handler for setting multicast list.
  483. */
  484. static void mwifiex_set_multicast_list(struct net_device *dev)
  485. {
  486. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  487. struct mwifiex_multicast_list mcast_list;
  488. if (dev->flags & IFF_PROMISC) {
  489. mcast_list.mode = MWIFIEX_PROMISC_MODE;
  490. } else if (dev->flags & IFF_ALLMULTI ||
  491. netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
  492. mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
  493. } else {
  494. mcast_list.mode = MWIFIEX_MULTICAST_MODE;
  495. if (netdev_mc_count(dev))
  496. mcast_list.num_multicast_addr =
  497. mwifiex_copy_mcast_addr(&mcast_list, dev);
  498. }
  499. mwifiex_request_set_multicast_list(priv, &mcast_list);
  500. }
  501. /*
  502. * CFG802.11 network device handler for transmission timeout.
  503. */
  504. static void
  505. mwifiex_tx_timeout(struct net_device *dev)
  506. {
  507. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  508. dev_err(priv->adapter->dev, "%lu : Tx timeout, bss_type-num = %d-%d\n",
  509. jiffies, priv->bss_type, priv->bss_num);
  510. mwifiex_set_trans_start(dev);
  511. priv->num_tx_timeout++;
  512. }
  513. /*
  514. * CFG802.11 network device handler for statistics retrieval.
  515. */
  516. static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
  517. {
  518. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  519. return &priv->stats;
  520. }
  521. static u16
  522. mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb)
  523. {
  524. skb->priority = cfg80211_classify8021d(skb);
  525. return mwifiex_1d_to_wmm_queue[skb->priority];
  526. }
  527. /* Network device handlers */
  528. static const struct net_device_ops mwifiex_netdev_ops = {
  529. .ndo_open = mwifiex_open,
  530. .ndo_stop = mwifiex_close,
  531. .ndo_start_xmit = mwifiex_hard_start_xmit,
  532. .ndo_set_mac_address = mwifiex_set_mac_address,
  533. .ndo_tx_timeout = mwifiex_tx_timeout,
  534. .ndo_get_stats = mwifiex_get_stats,
  535. .ndo_set_rx_mode = mwifiex_set_multicast_list,
  536. .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
  537. };
  538. /*
  539. * This function initializes the private structure parameters.
  540. *
  541. * The following wait queues are initialized -
  542. * - IOCTL wait queue
  543. * - Command wait queue
  544. * - Statistics wait queue
  545. *
  546. * ...and the following default parameters are set -
  547. * - Current key index : Set to 0
  548. * - Rate index : Set to auto
  549. * - Media connected : Set to disconnected
  550. * - Adhoc link sensed : Set to false
  551. * - Nick name : Set to null
  552. * - Number of Tx timeout : Set to 0
  553. * - Device address : Set to current address
  554. *
  555. * In addition, the CFG80211 work queue is also created.
  556. */
  557. void mwifiex_init_priv_params(struct mwifiex_private *priv,
  558. struct net_device *dev)
  559. {
  560. dev->netdev_ops = &mwifiex_netdev_ops;
  561. /* Initialize private structure */
  562. priv->current_key_index = 0;
  563. priv->media_connected = false;
  564. memset(&priv->nick_name, 0, sizeof(priv->nick_name));
  565. memset(priv->mgmt_ie, 0,
  566. sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
  567. priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
  568. priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
  569. priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
  570. priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
  571. priv->num_tx_timeout = 0;
  572. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  573. }
  574. /*
  575. * This function check if command is pending.
  576. */
  577. int is_command_pending(struct mwifiex_adapter *adapter)
  578. {
  579. unsigned long flags;
  580. int is_cmd_pend_q_empty;
  581. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  582. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  583. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  584. return !is_cmd_pend_q_empty;
  585. }
  586. /*
  587. * This is the main work queue function.
  588. *
  589. * It handles the main process, which in turn handles the complete
  590. * driver operations.
  591. */
  592. static void mwifiex_main_work_queue(struct work_struct *work)
  593. {
  594. struct mwifiex_adapter *adapter =
  595. container_of(work, struct mwifiex_adapter, main_work);
  596. if (adapter->surprise_removed)
  597. return;
  598. mwifiex_main_process(adapter);
  599. }
  600. /*
  601. * This function cancels all works in the queue and destroys
  602. * the main workqueue.
  603. */
  604. static void
  605. mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
  606. {
  607. flush_workqueue(adapter->workqueue);
  608. destroy_workqueue(adapter->workqueue);
  609. adapter->workqueue = NULL;
  610. }
  611. /*
  612. * This function adds the card.
  613. *
  614. * This function follows the following major steps to set up the device -
  615. * - Initialize software. This includes probing the card, registering
  616. * the interface operations table, and allocating/initializing the
  617. * adapter structure
  618. * - Set up the netlink socket
  619. * - Create and start the main work queue
  620. * - Register the device
  621. * - Initialize firmware and hardware
  622. * - Add logical interfaces
  623. */
  624. int
  625. mwifiex_add_card(void *card, struct semaphore *sem,
  626. struct mwifiex_if_ops *if_ops, u8 iface_type)
  627. {
  628. struct mwifiex_adapter *adapter;
  629. if (down_interruptible(sem))
  630. goto exit_sem_err;
  631. if (mwifiex_register(card, if_ops, (void **)&adapter)) {
  632. pr_err("%s: software init failed\n", __func__);
  633. goto err_init_sw;
  634. }
  635. adapter->iface_type = iface_type;
  636. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  637. adapter->surprise_removed = false;
  638. init_waitqueue_head(&adapter->init_wait_q);
  639. adapter->is_suspended = false;
  640. adapter->hs_activated = false;
  641. init_waitqueue_head(&adapter->hs_activate_wait_q);
  642. adapter->cmd_wait_q_required = false;
  643. init_waitqueue_head(&adapter->cmd_wait_q.wait);
  644. adapter->cmd_wait_q.status = 0;
  645. adapter->scan_wait_q_woken = false;
  646. adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
  647. if (!adapter->workqueue)
  648. goto err_kmalloc;
  649. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  650. /* Register the device. Fill up the private data structure with relevant
  651. information from the card and request for the required IRQ. */
  652. if (adapter->if_ops.register_dev(adapter)) {
  653. pr_err("%s: failed to register mwifiex device\n", __func__);
  654. goto err_registerdev;
  655. }
  656. if (mwifiex_init_hw_fw(adapter)) {
  657. pr_err("%s: firmware init failed\n", __func__);
  658. goto err_init_fw;
  659. }
  660. up(sem);
  661. return 0;
  662. err_init_fw:
  663. pr_debug("info: %s: unregister device\n", __func__);
  664. if (adapter->if_ops.unregister_dev)
  665. adapter->if_ops.unregister_dev(adapter);
  666. err_registerdev:
  667. adapter->surprise_removed = true;
  668. mwifiex_terminate_workqueue(adapter);
  669. err_kmalloc:
  670. if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
  671. (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
  672. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  673. adapter->init_wait_q_woken = false;
  674. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  675. wait_event_interruptible(adapter->init_wait_q,
  676. adapter->init_wait_q_woken);
  677. }
  678. mwifiex_free_adapter(adapter);
  679. err_init_sw:
  680. up(sem);
  681. exit_sem_err:
  682. return -1;
  683. }
  684. EXPORT_SYMBOL_GPL(mwifiex_add_card);
  685. /*
  686. * This function removes the card.
  687. *
  688. * This function follows the following major steps to remove the device -
  689. * - Stop data traffic
  690. * - Shutdown firmware
  691. * - Remove the logical interfaces
  692. * - Terminate the work queue
  693. * - Unregister the device
  694. * - Free the adapter structure
  695. */
  696. int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
  697. {
  698. struct mwifiex_private *priv = NULL;
  699. int i;
  700. if (down_interruptible(sem))
  701. goto exit_sem_err;
  702. if (!adapter)
  703. goto exit_remove;
  704. adapter->surprise_removed = true;
  705. /* Stop data */
  706. for (i = 0; i < adapter->priv_num; i++) {
  707. priv = adapter->priv[i];
  708. if (priv && priv->netdev) {
  709. mwifiex_stop_net_dev_queue(priv->netdev, adapter);
  710. if (netif_carrier_ok(priv->netdev))
  711. netif_carrier_off(priv->netdev);
  712. }
  713. }
  714. dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
  715. adapter->init_wait_q_woken = false;
  716. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  717. wait_event_interruptible(adapter->init_wait_q,
  718. adapter->init_wait_q_woken);
  719. dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
  720. if (atomic_read(&adapter->rx_pending) ||
  721. atomic_read(&adapter->tx_pending) ||
  722. atomic_read(&adapter->cmd_pending)) {
  723. dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
  724. "cmd_pending=%d\n",
  725. atomic_read(&adapter->rx_pending),
  726. atomic_read(&adapter->tx_pending),
  727. atomic_read(&adapter->cmd_pending));
  728. }
  729. for (i = 0; i < adapter->priv_num; i++) {
  730. priv = adapter->priv[i];
  731. if (!priv)
  732. continue;
  733. rtnl_lock();
  734. if (priv->wdev && priv->netdev)
  735. mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
  736. rtnl_unlock();
  737. }
  738. priv = adapter->priv[0];
  739. if (!priv || !priv->wdev)
  740. goto exit_remove;
  741. wiphy_unregister(priv->wdev->wiphy);
  742. wiphy_free(priv->wdev->wiphy);
  743. for (i = 0; i < adapter->priv_num; i++) {
  744. priv = adapter->priv[i];
  745. if (priv)
  746. kfree(priv->wdev);
  747. }
  748. mwifiex_terminate_workqueue(adapter);
  749. /* Unregister device */
  750. dev_dbg(adapter->dev, "info: unregister device\n");
  751. if (adapter->if_ops.unregister_dev)
  752. adapter->if_ops.unregister_dev(adapter);
  753. /* Free adapter structure */
  754. dev_dbg(adapter->dev, "info: free adapter\n");
  755. mwifiex_free_adapter(adapter);
  756. exit_remove:
  757. up(sem);
  758. exit_sem_err:
  759. return 0;
  760. }
  761. EXPORT_SYMBOL_GPL(mwifiex_remove_card);
  762. /*
  763. * This function initializes the module.
  764. *
  765. * The debug FS is also initialized if configured.
  766. */
  767. static int
  768. mwifiex_init_module(void)
  769. {
  770. #ifdef CONFIG_DEBUG_FS
  771. mwifiex_debugfs_init();
  772. #endif
  773. return 0;
  774. }
  775. /*
  776. * This function cleans up the module.
  777. *
  778. * The debug FS is removed if available.
  779. */
  780. static void
  781. mwifiex_cleanup_module(void)
  782. {
  783. #ifdef CONFIG_DEBUG_FS
  784. mwifiex_debugfs_remove();
  785. #endif
  786. }
  787. module_init(mwifiex_init_module);
  788. module_exit(mwifiex_cleanup_module);
  789. MODULE_AUTHOR("Marvell International Ltd.");
  790. MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
  791. MODULE_VERSION(VERSION);
  792. MODULE_LICENSE("GPL v2");