main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. /*
  246. * This function frees the adapter structure.
  247. *
  248. * Additionally, this closes the netlink socket, frees the timers
  249. * and private structures.
  250. */
  251. static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
  252. {
  253. if (!adapter) {
  254. pr_err("%s: adapter is NULL\n", __func__);
  255. return;
  256. }
  257. mwifiex_unregister(adapter);
  258. pr_debug("info: %s: free adapter\n", __func__);
  259. }
  260. /*
  261. * This function gets firmware and initializes it.
  262. *
  263. * The main initialization steps followed are -
  264. * - Download the correct firmware to card
  265. * - Issue the init commands to firmware
  266. */
  267. static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
  268. {
  269. int ret;
  270. char fmt[64];
  271. struct mwifiex_private *priv;
  272. struct mwifiex_adapter *adapter = context;
  273. struct mwifiex_fw_image fw;
  274. if (!firmware) {
  275. dev_err(adapter->dev,
  276. "Failed to get firmware %s\n", adapter->fw_name);
  277. goto done;
  278. }
  279. memset(&fw, 0, sizeof(struct mwifiex_fw_image));
  280. adapter->firmware = firmware;
  281. fw.fw_buf = (u8 *) adapter->firmware->data;
  282. fw.fw_len = adapter->firmware->size;
  283. if (adapter->if_ops.dnld_fw)
  284. ret = adapter->if_ops.dnld_fw(adapter, &fw);
  285. else
  286. ret = mwifiex_dnld_fw(adapter, &fw);
  287. if (ret == -1)
  288. goto done;
  289. dev_notice(adapter->dev, "WLAN FW is active\n");
  290. adapter->init_wait_q_woken = false;
  291. ret = mwifiex_init_fw(adapter);
  292. if (ret == -1) {
  293. goto done;
  294. } else if (!ret) {
  295. adapter->hw_status = MWIFIEX_HW_STATUS_READY;
  296. goto done;
  297. }
  298. /* Wait for mwifiex_init to complete */
  299. wait_event_interruptible(adapter->init_wait_q,
  300. adapter->init_wait_q_woken);
  301. if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
  302. goto done;
  303. priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
  304. if (mwifiex_register_cfg80211(adapter)) {
  305. dev_err(adapter->dev, "cannot register with cfg80211\n");
  306. goto err_init_fw;
  307. }
  308. rtnl_lock();
  309. /* Create station interface by default */
  310. if (!mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
  311. NL80211_IFTYPE_STATION, NULL, NULL)) {
  312. dev_err(adapter->dev, "cannot create default STA interface\n");
  313. goto err_add_intf;
  314. }
  315. /* Create AP interface by default */
  316. if (!mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
  317. NL80211_IFTYPE_AP, NULL, NULL)) {
  318. dev_err(adapter->dev, "cannot create default AP interface\n");
  319. goto err_add_intf;
  320. }
  321. /* Create P2P interface by default */
  322. if (!mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
  323. NL80211_IFTYPE_P2P_CLIENT, NULL, NULL)) {
  324. dev_err(adapter->dev, "cannot create default P2P interface\n");
  325. goto err_add_intf;
  326. }
  327. rtnl_unlock();
  328. mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
  329. dev_notice(adapter->dev, "driver_version = %s\n", fmt);
  330. goto done;
  331. err_add_intf:
  332. mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
  333. rtnl_unlock();
  334. err_init_fw:
  335. pr_debug("info: %s: unregister device\n", __func__);
  336. adapter->if_ops.unregister_dev(adapter);
  337. done:
  338. release_firmware(adapter->firmware);
  339. complete(&adapter->fw_load);
  340. return;
  341. }
  342. /*
  343. * This function initializes the hardware and gets firmware.
  344. */
  345. static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
  346. {
  347. int ret;
  348. init_completion(&adapter->fw_load);
  349. ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
  350. adapter->dev, GFP_KERNEL, adapter,
  351. mwifiex_fw_dpc);
  352. if (ret < 0)
  353. dev_err(adapter->dev,
  354. "request_firmware_nowait() returned error %d\n", ret);
  355. return ret;
  356. }
  357. /*
  358. * This function fills a driver buffer.
  359. *
  360. * The function associates a given SKB with the provided driver buffer
  361. * and also updates some of the SKB parameters, including IP header,
  362. * priority and timestamp.
  363. */
  364. static void
  365. mwifiex_fill_buffer(struct sk_buff *skb)
  366. {
  367. struct ethhdr *eth;
  368. struct iphdr *iph;
  369. struct timeval tv;
  370. u8 tid = 0;
  371. eth = (struct ethhdr *) skb->data;
  372. switch (eth->h_proto) {
  373. case __constant_htons(ETH_P_IP):
  374. iph = ip_hdr(skb);
  375. tid = IPTOS_PREC(iph->tos);
  376. pr_debug("data: packet type ETH_P_IP: %04x, tid=%#x prio=%#x\n",
  377. eth->h_proto, tid, skb->priority);
  378. break;
  379. case __constant_htons(ETH_P_ARP):
  380. pr_debug("data: ARP packet: %04x\n", eth->h_proto);
  381. default:
  382. break;
  383. }
  384. /* Offset for TOS field in the IP header */
  385. #define IPTOS_OFFSET 5
  386. tid = (tid >> IPTOS_OFFSET);
  387. skb->priority = tid;
  388. /* Record the current time the packet was queued; used to
  389. determine the amount of time the packet was queued in
  390. the driver before it was sent to the firmware.
  391. The delay is then sent along with the packet to the
  392. firmware for aggregate delay calculation for stats and
  393. MSDU lifetime expiry.
  394. */
  395. do_gettimeofday(&tv);
  396. skb->tstamp = timeval_to_ktime(tv);
  397. }
  398. /*
  399. * CFG802.11 network device handler for open.
  400. *
  401. * Starts the data queue.
  402. */
  403. static int
  404. mwifiex_open(struct net_device *dev)
  405. {
  406. netif_tx_start_all_queues(dev);
  407. return 0;
  408. }
  409. /*
  410. * CFG802.11 network device handler for close.
  411. */
  412. static int
  413. mwifiex_close(struct net_device *dev)
  414. {
  415. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  416. if (priv->scan_request) {
  417. dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
  418. cfg80211_scan_done(priv->scan_request, 1);
  419. priv->scan_request = NULL;
  420. }
  421. return 0;
  422. }
  423. /*
  424. * Add buffer into wmm tx queue and queue work to transmit it.
  425. */
  426. int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
  427. {
  428. mwifiex_wmm_add_buf_txqueue(priv, skb);
  429. atomic_inc(&priv->adapter->tx_pending);
  430. if (priv->adapter->scan_delay_cnt)
  431. atomic_set(&priv->adapter->is_tx_received, true);
  432. if (atomic_read(&priv->adapter->tx_pending) >= MAX_TX_PENDING) {
  433. mwifiex_set_trans_start(priv->netdev);
  434. mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
  435. }
  436. queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
  437. return 0;
  438. }
  439. /*
  440. * CFG802.11 network device handler for data transmission.
  441. */
  442. static int
  443. mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  444. {
  445. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  446. struct sk_buff *new_skb;
  447. struct mwifiex_txinfo *tx_info;
  448. dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
  449. jiffies, priv->bss_type, priv->bss_num);
  450. if (priv->adapter->surprise_removed) {
  451. kfree_skb(skb);
  452. priv->stats.tx_dropped++;
  453. return 0;
  454. }
  455. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  456. dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
  457. kfree_skb(skb);
  458. priv->stats.tx_dropped++;
  459. return 0;
  460. }
  461. if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
  462. dev_dbg(priv->adapter->dev,
  463. "data: Tx: insufficient skb headroom %d\n",
  464. skb_headroom(skb));
  465. /* Insufficient skb headroom - allocate a new skb */
  466. new_skb =
  467. skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
  468. if (unlikely(!new_skb)) {
  469. dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
  470. kfree_skb(skb);
  471. priv->stats.tx_dropped++;
  472. return 0;
  473. }
  474. kfree_skb(skb);
  475. skb = new_skb;
  476. dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
  477. skb_headroom(skb));
  478. }
  479. tx_info = MWIFIEX_SKB_TXCB(skb);
  480. tx_info->bss_num = priv->bss_num;
  481. tx_info->bss_type = priv->bss_type;
  482. mwifiex_fill_buffer(skb);
  483. mwifiex_queue_tx_pkt(priv, skb);
  484. return 0;
  485. }
  486. /*
  487. * CFG802.11 network device handler for setting MAC address.
  488. */
  489. static int
  490. mwifiex_set_mac_address(struct net_device *dev, void *addr)
  491. {
  492. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  493. struct sockaddr *hw_addr = addr;
  494. int ret;
  495. memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
  496. /* Send request to firmware */
  497. ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
  498. HostCmd_ACT_GEN_SET, 0, NULL);
  499. if (!ret)
  500. memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
  501. else
  502. dev_err(priv->adapter->dev,
  503. "set mac address failed: ret=%d\n", ret);
  504. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  505. return ret;
  506. }
  507. /*
  508. * CFG802.11 network device handler for setting multicast list.
  509. */
  510. static void mwifiex_set_multicast_list(struct net_device *dev)
  511. {
  512. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  513. struct mwifiex_multicast_list mcast_list;
  514. if (dev->flags & IFF_PROMISC) {
  515. mcast_list.mode = MWIFIEX_PROMISC_MODE;
  516. } else if (dev->flags & IFF_ALLMULTI ||
  517. netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
  518. mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
  519. } else {
  520. mcast_list.mode = MWIFIEX_MULTICAST_MODE;
  521. if (netdev_mc_count(dev))
  522. mcast_list.num_multicast_addr =
  523. mwifiex_copy_mcast_addr(&mcast_list, dev);
  524. }
  525. mwifiex_request_set_multicast_list(priv, &mcast_list);
  526. }
  527. /*
  528. * CFG802.11 network device handler for transmission timeout.
  529. */
  530. static void
  531. mwifiex_tx_timeout(struct net_device *dev)
  532. {
  533. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  534. dev_err(priv->adapter->dev, "%lu : Tx timeout, bss_type-num = %d-%d\n",
  535. jiffies, priv->bss_type, priv->bss_num);
  536. mwifiex_set_trans_start(dev);
  537. priv->num_tx_timeout++;
  538. }
  539. /*
  540. * CFG802.11 network device handler for statistics retrieval.
  541. */
  542. static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
  543. {
  544. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  545. return &priv->stats;
  546. }
  547. /* Network device handlers */
  548. static const struct net_device_ops mwifiex_netdev_ops = {
  549. .ndo_open = mwifiex_open,
  550. .ndo_stop = mwifiex_close,
  551. .ndo_start_xmit = mwifiex_hard_start_xmit,
  552. .ndo_set_mac_address = mwifiex_set_mac_address,
  553. .ndo_tx_timeout = mwifiex_tx_timeout,
  554. .ndo_get_stats = mwifiex_get_stats,
  555. .ndo_set_rx_mode = mwifiex_set_multicast_list,
  556. };
  557. /*
  558. * This function initializes the private structure parameters.
  559. *
  560. * The following wait queues are initialized -
  561. * - IOCTL wait queue
  562. * - Command wait queue
  563. * - Statistics wait queue
  564. *
  565. * ...and the following default parameters are set -
  566. * - Current key index : Set to 0
  567. * - Rate index : Set to auto
  568. * - Media connected : Set to disconnected
  569. * - Adhoc link sensed : Set to false
  570. * - Nick name : Set to null
  571. * - Number of Tx timeout : Set to 0
  572. * - Device address : Set to current address
  573. *
  574. * In addition, the CFG80211 work queue is also created.
  575. */
  576. void mwifiex_init_priv_params(struct mwifiex_private *priv,
  577. struct net_device *dev)
  578. {
  579. dev->netdev_ops = &mwifiex_netdev_ops;
  580. /* Initialize private structure */
  581. priv->current_key_index = 0;
  582. priv->media_connected = false;
  583. memset(&priv->nick_name, 0, sizeof(priv->nick_name));
  584. memset(priv->mgmt_ie, 0,
  585. sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
  586. priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
  587. priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
  588. priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
  589. priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
  590. priv->num_tx_timeout = 0;
  591. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  592. }
  593. /*
  594. * This function check if command is pending.
  595. */
  596. int is_command_pending(struct mwifiex_adapter *adapter)
  597. {
  598. unsigned long flags;
  599. int is_cmd_pend_q_empty;
  600. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  601. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  602. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  603. return !is_cmd_pend_q_empty;
  604. }
  605. /*
  606. * This is the main work queue function.
  607. *
  608. * It handles the main process, which in turn handles the complete
  609. * driver operations.
  610. */
  611. static void mwifiex_main_work_queue(struct work_struct *work)
  612. {
  613. struct mwifiex_adapter *adapter =
  614. container_of(work, struct mwifiex_adapter, main_work);
  615. if (adapter->surprise_removed)
  616. return;
  617. mwifiex_main_process(adapter);
  618. }
  619. /*
  620. * This function cancels all works in the queue and destroys
  621. * the main workqueue.
  622. */
  623. static void
  624. mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
  625. {
  626. flush_workqueue(adapter->workqueue);
  627. destroy_workqueue(adapter->workqueue);
  628. adapter->workqueue = NULL;
  629. }
  630. /*
  631. * This function adds the card.
  632. *
  633. * This function follows the following major steps to set up the device -
  634. * - Initialize software. This includes probing the card, registering
  635. * the interface operations table, and allocating/initializing the
  636. * adapter structure
  637. * - Set up the netlink socket
  638. * - Create and start the main work queue
  639. * - Register the device
  640. * - Initialize firmware and hardware
  641. * - Add logical interfaces
  642. */
  643. int
  644. mwifiex_add_card(void *card, struct semaphore *sem,
  645. struct mwifiex_if_ops *if_ops, u8 iface_type)
  646. {
  647. struct mwifiex_adapter *adapter;
  648. if (down_interruptible(sem))
  649. goto exit_sem_err;
  650. if (mwifiex_register(card, if_ops, (void **)&adapter)) {
  651. pr_err("%s: software init failed\n", __func__);
  652. goto err_init_sw;
  653. }
  654. adapter->iface_type = iface_type;
  655. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  656. adapter->surprise_removed = false;
  657. init_waitqueue_head(&adapter->init_wait_q);
  658. adapter->is_suspended = false;
  659. adapter->hs_activated = false;
  660. init_waitqueue_head(&adapter->hs_activate_wait_q);
  661. adapter->cmd_wait_q_required = false;
  662. init_waitqueue_head(&adapter->cmd_wait_q.wait);
  663. adapter->cmd_wait_q.status = 0;
  664. adapter->scan_wait_q_woken = false;
  665. adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
  666. if (!adapter->workqueue)
  667. goto err_kmalloc;
  668. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  669. /* Register the device. Fill up the private data structure with relevant
  670. information from the card and request for the required IRQ. */
  671. if (adapter->if_ops.register_dev(adapter)) {
  672. pr_err("%s: failed to register mwifiex device\n", __func__);
  673. goto err_registerdev;
  674. }
  675. if (mwifiex_init_hw_fw(adapter)) {
  676. pr_err("%s: firmware init failed\n", __func__);
  677. goto err_init_fw;
  678. }
  679. up(sem);
  680. return 0;
  681. err_init_fw:
  682. pr_debug("info: %s: unregister device\n", __func__);
  683. if (adapter->if_ops.unregister_dev)
  684. adapter->if_ops.unregister_dev(adapter);
  685. err_registerdev:
  686. adapter->surprise_removed = true;
  687. mwifiex_terminate_workqueue(adapter);
  688. err_kmalloc:
  689. if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
  690. (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
  691. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  692. adapter->init_wait_q_woken = false;
  693. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  694. wait_event_interruptible(adapter->init_wait_q,
  695. adapter->init_wait_q_woken);
  696. }
  697. mwifiex_free_adapter(adapter);
  698. err_init_sw:
  699. up(sem);
  700. exit_sem_err:
  701. return -1;
  702. }
  703. EXPORT_SYMBOL_GPL(mwifiex_add_card);
  704. /*
  705. * This function removes the card.
  706. *
  707. * This function follows the following major steps to remove the device -
  708. * - Stop data traffic
  709. * - Shutdown firmware
  710. * - Remove the logical interfaces
  711. * - Terminate the work queue
  712. * - Unregister the device
  713. * - Free the adapter structure
  714. */
  715. int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
  716. {
  717. struct mwifiex_private *priv = NULL;
  718. int i;
  719. if (down_interruptible(sem))
  720. goto exit_sem_err;
  721. if (!adapter)
  722. goto exit_remove;
  723. adapter->surprise_removed = true;
  724. /* Stop data */
  725. for (i = 0; i < adapter->priv_num; i++) {
  726. priv = adapter->priv[i];
  727. if (priv && priv->netdev) {
  728. if (!netif_queue_stopped(priv->netdev))
  729. mwifiex_stop_net_dev_queue(priv->netdev,
  730. adapter);
  731. if (netif_carrier_ok(priv->netdev))
  732. netif_carrier_off(priv->netdev);
  733. }
  734. }
  735. dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
  736. adapter->init_wait_q_woken = false;
  737. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  738. wait_event_interruptible(adapter->init_wait_q,
  739. adapter->init_wait_q_woken);
  740. dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
  741. if (atomic_read(&adapter->rx_pending) ||
  742. atomic_read(&adapter->tx_pending) ||
  743. atomic_read(&adapter->cmd_pending)) {
  744. dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
  745. "cmd_pending=%d\n",
  746. atomic_read(&adapter->rx_pending),
  747. atomic_read(&adapter->tx_pending),
  748. atomic_read(&adapter->cmd_pending));
  749. }
  750. for (i = 0; i < adapter->priv_num; i++) {
  751. priv = adapter->priv[i];
  752. if (!priv)
  753. continue;
  754. rtnl_lock();
  755. if (priv->wdev && priv->netdev)
  756. mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
  757. rtnl_unlock();
  758. }
  759. priv = adapter->priv[0];
  760. if (!priv || !priv->wdev)
  761. goto exit_remove;
  762. wiphy_unregister(priv->wdev->wiphy);
  763. wiphy_free(priv->wdev->wiphy);
  764. for (i = 0; i < adapter->priv_num; i++) {
  765. priv = adapter->priv[i];
  766. if (priv)
  767. kfree(priv->wdev);
  768. }
  769. mwifiex_terminate_workqueue(adapter);
  770. /* Unregister device */
  771. dev_dbg(adapter->dev, "info: unregister device\n");
  772. if (adapter->if_ops.unregister_dev)
  773. adapter->if_ops.unregister_dev(adapter);
  774. /* Free adapter structure */
  775. dev_dbg(adapter->dev, "info: free adapter\n");
  776. mwifiex_free_adapter(adapter);
  777. exit_remove:
  778. up(sem);
  779. exit_sem_err:
  780. return 0;
  781. }
  782. EXPORT_SYMBOL_GPL(mwifiex_remove_card);
  783. /*
  784. * This function initializes the module.
  785. *
  786. * The debug FS is also initialized if configured.
  787. */
  788. static int
  789. mwifiex_init_module(void)
  790. {
  791. #ifdef CONFIG_DEBUG_FS
  792. mwifiex_debugfs_init();
  793. #endif
  794. return 0;
  795. }
  796. /*
  797. * This function cleans up the module.
  798. *
  799. * The debug FS is removed if available.
  800. */
  801. static void
  802. mwifiex_cleanup_module(void)
  803. {
  804. #ifdef CONFIG_DEBUG_FS
  805. mwifiex_debugfs_remove();
  806. #endif
  807. }
  808. module_init(mwifiex_init_module);
  809. module_exit(mwifiex_cleanup_module);
  810. MODULE_AUTHOR("Marvell International Ltd.");
  811. MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
  812. MODULE_VERSION(VERSION);
  813. MODULE_LICENSE("GPL v2");