main.c 23 KB

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