main.c 23 KB

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