main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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[i]->bss_priority = i;
  65. adapter->priv_num++;
  66. }
  67. mwifiex_init_lock_list(adapter);
  68. init_timer(&adapter->cmd_timer);
  69. adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
  70. adapter->cmd_timer.data = (unsigned long) adapter;
  71. return 0;
  72. error:
  73. dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
  74. for (i = 0; i < adapter->priv_num; i++)
  75. kfree(adapter->priv[i]);
  76. kfree(adapter);
  77. return -1;
  78. }
  79. /*
  80. * This function unregisters the device and performs all the necessary
  81. * cleanups.
  82. *
  83. * The following cleanup operations are performed -
  84. * - Free the timers
  85. * - Free beacon buffers
  86. * - Free private structures
  87. * - Free adapter structure
  88. */
  89. static int mwifiex_unregister(struct mwifiex_adapter *adapter)
  90. {
  91. s32 i;
  92. del_timer(&adapter->cmd_timer);
  93. /* Free private structures */
  94. for (i = 0; i < adapter->priv_num; i++) {
  95. if (adapter->priv[i]) {
  96. mwifiex_free_curr_bcn(adapter->priv[i]);
  97. kfree(adapter->priv[i]);
  98. }
  99. }
  100. kfree(adapter);
  101. return 0;
  102. }
  103. /*
  104. * The main process.
  105. *
  106. * This function is the main procedure of the driver and handles various driver
  107. * operations. It runs in a loop and provides the core functionalities.
  108. *
  109. * The main responsibilities of this function are -
  110. * - Ensure concurrency control
  111. * - Handle pending interrupts and call interrupt handlers
  112. * - Wake up the card if required
  113. * - Handle command responses and call response handlers
  114. * - Handle events and call event handlers
  115. * - Execute pending commands
  116. * - Transmit pending data packets
  117. */
  118. int mwifiex_main_process(struct mwifiex_adapter *adapter)
  119. {
  120. int ret = 0;
  121. unsigned long flags;
  122. struct sk_buff *skb;
  123. spin_lock_irqsave(&adapter->main_proc_lock, flags);
  124. /* Check if already processing */
  125. if (adapter->mwifiex_processing) {
  126. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  127. goto exit_main_proc;
  128. } else {
  129. adapter->mwifiex_processing = true;
  130. spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
  131. }
  132. process_start:
  133. do {
  134. if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
  135. (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
  136. break;
  137. /* Handle pending interrupt if any */
  138. if (adapter->int_status) {
  139. if (adapter->hs_activated)
  140. mwifiex_process_hs_config(adapter);
  141. if (adapter->if_ops.process_int_status)
  142. adapter->if_ops.process_int_status(adapter);
  143. }
  144. /* Need to wake up the card ? */
  145. if ((adapter->ps_state == PS_STATE_SLEEP) &&
  146. (adapter->pm_wakeup_card_req &&
  147. !adapter->pm_wakeup_fw_try) &&
  148. (is_command_pending(adapter) ||
  149. !mwifiex_wmm_lists_empty(adapter))) {
  150. adapter->pm_wakeup_fw_try = true;
  151. adapter->if_ops.wakeup(adapter);
  152. continue;
  153. }
  154. if (IS_CARD_RX_RCVD(adapter)) {
  155. adapter->pm_wakeup_fw_try = false;
  156. if (adapter->ps_state == PS_STATE_SLEEP)
  157. adapter->ps_state = PS_STATE_AWAKE;
  158. } else {
  159. /* We have tried to wakeup the card already */
  160. if (adapter->pm_wakeup_fw_try)
  161. break;
  162. if (adapter->ps_state != PS_STATE_AWAKE ||
  163. adapter->tx_lock_flag)
  164. break;
  165. if (adapter->scan_processing || 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->data_sent &&
  212. !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. rtnl_unlock();
  322. mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
  323. dev_notice(adapter->dev, "driver_version = %s\n", fmt);
  324. goto done;
  325. err_add_intf:
  326. mwifiex_del_virtual_intf(adapter->wiphy, priv->netdev);
  327. rtnl_unlock();
  328. err_init_fw:
  329. pr_debug("info: %s: unregister device\n", __func__);
  330. adapter->if_ops.unregister_dev(adapter);
  331. done:
  332. release_firmware(adapter->firmware);
  333. complete(&adapter->fw_load);
  334. return;
  335. }
  336. /*
  337. * This function initializes the hardware and gets firmware.
  338. */
  339. static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
  340. {
  341. int ret;
  342. init_completion(&adapter->fw_load);
  343. ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
  344. adapter->dev, GFP_KERNEL, adapter,
  345. mwifiex_fw_dpc);
  346. if (ret < 0)
  347. dev_err(adapter->dev,
  348. "request_firmware_nowait() returned error %d\n", ret);
  349. return ret;
  350. }
  351. /*
  352. * This function fills a driver buffer.
  353. *
  354. * The function associates a given SKB with the provided driver buffer
  355. * and also updates some of the SKB parameters, including IP header,
  356. * priority and timestamp.
  357. */
  358. static void
  359. mwifiex_fill_buffer(struct sk_buff *skb)
  360. {
  361. struct ethhdr *eth;
  362. struct iphdr *iph;
  363. struct timeval tv;
  364. u8 tid = 0;
  365. eth = (struct ethhdr *) skb->data;
  366. switch (eth->h_proto) {
  367. case __constant_htons(ETH_P_IP):
  368. iph = ip_hdr(skb);
  369. tid = IPTOS_PREC(iph->tos);
  370. pr_debug("data: packet type ETH_P_IP: %04x, tid=%#x prio=%#x\n",
  371. eth->h_proto, tid, skb->priority);
  372. break;
  373. case __constant_htons(ETH_P_ARP):
  374. pr_debug("data: ARP packet: %04x\n", eth->h_proto);
  375. default:
  376. break;
  377. }
  378. /* Offset for TOS field in the IP header */
  379. #define IPTOS_OFFSET 5
  380. tid = (tid >> IPTOS_OFFSET);
  381. skb->priority = tid;
  382. /* Record the current time the packet was queued; used to
  383. determine the amount of time the packet was queued in
  384. the driver before it was sent to the firmware.
  385. The delay is then sent along with the packet to the
  386. firmware for aggregate delay calculation for stats and
  387. MSDU lifetime expiry.
  388. */
  389. do_gettimeofday(&tv);
  390. skb->tstamp = timeval_to_ktime(tv);
  391. }
  392. /*
  393. * CFG802.11 network device handler for open.
  394. *
  395. * Starts the data queue.
  396. */
  397. static int
  398. mwifiex_open(struct net_device *dev)
  399. {
  400. netif_tx_start_all_queues(dev);
  401. return 0;
  402. }
  403. /*
  404. * CFG802.11 network device handler for close.
  405. */
  406. static int
  407. mwifiex_close(struct net_device *dev)
  408. {
  409. return 0;
  410. }
  411. /*
  412. * CFG802.11 network device handler for data transmission.
  413. */
  414. static int
  415. mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  416. {
  417. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  418. struct sk_buff *new_skb;
  419. struct mwifiex_txinfo *tx_info;
  420. dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
  421. jiffies, priv->bss_type, priv->bss_num);
  422. if (priv->adapter->surprise_removed) {
  423. kfree_skb(skb);
  424. priv->stats.tx_dropped++;
  425. return 0;
  426. }
  427. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  428. dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
  429. kfree_skb(skb);
  430. priv->stats.tx_dropped++;
  431. return 0;
  432. }
  433. if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
  434. dev_dbg(priv->adapter->dev,
  435. "data: Tx: insufficient skb headroom %d\n",
  436. skb_headroom(skb));
  437. /* Insufficient skb headroom - allocate a new skb */
  438. new_skb =
  439. skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
  440. if (unlikely(!new_skb)) {
  441. dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
  442. kfree_skb(skb);
  443. priv->stats.tx_dropped++;
  444. return 0;
  445. }
  446. kfree_skb(skb);
  447. skb = new_skb;
  448. dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
  449. skb_headroom(skb));
  450. }
  451. tx_info = MWIFIEX_SKB_TXCB(skb);
  452. tx_info->bss_num = priv->bss_num;
  453. tx_info->bss_type = priv->bss_type;
  454. mwifiex_fill_buffer(skb);
  455. mwifiex_wmm_add_buf_txqueue(priv, skb);
  456. atomic_inc(&priv->adapter->tx_pending);
  457. if (atomic_read(&priv->adapter->tx_pending) >= MAX_TX_PENDING) {
  458. mwifiex_set_trans_start(dev);
  459. mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
  460. }
  461. queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
  462. return 0;
  463. }
  464. /*
  465. * CFG802.11 network device handler for setting MAC address.
  466. */
  467. static int
  468. mwifiex_set_mac_address(struct net_device *dev, void *addr)
  469. {
  470. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  471. struct sockaddr *hw_addr = addr;
  472. int ret;
  473. memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
  474. /* Send request to firmware */
  475. ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
  476. HostCmd_ACT_GEN_SET, 0, NULL);
  477. if (!ret)
  478. memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
  479. else
  480. dev_err(priv->adapter->dev,
  481. "set mac address failed: ret=%d\n", ret);
  482. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  483. return ret;
  484. }
  485. /*
  486. * CFG802.11 network device handler for setting multicast list.
  487. */
  488. static void mwifiex_set_multicast_list(struct net_device *dev)
  489. {
  490. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  491. struct mwifiex_multicast_list mcast_list;
  492. if (dev->flags & IFF_PROMISC) {
  493. mcast_list.mode = MWIFIEX_PROMISC_MODE;
  494. } else if (dev->flags & IFF_ALLMULTI ||
  495. netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
  496. mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
  497. } else {
  498. mcast_list.mode = MWIFIEX_MULTICAST_MODE;
  499. if (netdev_mc_count(dev))
  500. mcast_list.num_multicast_addr =
  501. mwifiex_copy_mcast_addr(&mcast_list, dev);
  502. }
  503. mwifiex_request_set_multicast_list(priv, &mcast_list);
  504. }
  505. /*
  506. * CFG802.11 network device handler for transmission timeout.
  507. */
  508. static void
  509. mwifiex_tx_timeout(struct net_device *dev)
  510. {
  511. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  512. dev_err(priv->adapter->dev, "%lu : Tx timeout, bss_type-num = %d-%d\n",
  513. jiffies, priv->bss_type, priv->bss_num);
  514. mwifiex_set_trans_start(dev);
  515. priv->num_tx_timeout++;
  516. }
  517. /*
  518. * CFG802.11 network device handler for statistics retrieval.
  519. */
  520. static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
  521. {
  522. struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
  523. return &priv->stats;
  524. }
  525. /* Network device handlers */
  526. static const struct net_device_ops mwifiex_netdev_ops = {
  527. .ndo_open = mwifiex_open,
  528. .ndo_stop = mwifiex_close,
  529. .ndo_start_xmit = mwifiex_hard_start_xmit,
  530. .ndo_set_mac_address = mwifiex_set_mac_address,
  531. .ndo_tx_timeout = mwifiex_tx_timeout,
  532. .ndo_get_stats = mwifiex_get_stats,
  533. .ndo_set_rx_mode = mwifiex_set_multicast_list,
  534. };
  535. /*
  536. * This function initializes the private structure parameters.
  537. *
  538. * The following wait queues are initialized -
  539. * - IOCTL wait queue
  540. * - Command wait queue
  541. * - Statistics wait queue
  542. *
  543. * ...and the following default parameters are set -
  544. * - Current key index : Set to 0
  545. * - Rate index : Set to auto
  546. * - Media connected : Set to disconnected
  547. * - Adhoc link sensed : Set to false
  548. * - Nick name : Set to null
  549. * - Number of Tx timeout : Set to 0
  550. * - Device address : Set to current address
  551. *
  552. * In addition, the CFG80211 work queue is also created.
  553. */
  554. void mwifiex_init_priv_params(struct mwifiex_private *priv,
  555. struct net_device *dev)
  556. {
  557. dev->netdev_ops = &mwifiex_netdev_ops;
  558. /* Initialize private structure */
  559. priv->current_key_index = 0;
  560. priv->media_connected = false;
  561. memset(&priv->nick_name, 0, sizeof(priv->nick_name));
  562. memset(priv->mgmt_ie, 0,
  563. sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
  564. priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
  565. priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
  566. priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
  567. priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
  568. priv->num_tx_timeout = 0;
  569. memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
  570. }
  571. /*
  572. * This function check if command is pending.
  573. */
  574. int is_command_pending(struct mwifiex_adapter *adapter)
  575. {
  576. unsigned long flags;
  577. int is_cmd_pend_q_empty;
  578. spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
  579. is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
  580. spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
  581. return !is_cmd_pend_q_empty;
  582. }
  583. /*
  584. * This is the main work queue function.
  585. *
  586. * It handles the main process, which in turn handles the complete
  587. * driver operations.
  588. */
  589. static void mwifiex_main_work_queue(struct work_struct *work)
  590. {
  591. struct mwifiex_adapter *adapter =
  592. container_of(work, struct mwifiex_adapter, main_work);
  593. if (adapter->surprise_removed)
  594. return;
  595. mwifiex_main_process(adapter);
  596. }
  597. /*
  598. * This function cancels all works in the queue and destroys
  599. * the main workqueue.
  600. */
  601. static void
  602. mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
  603. {
  604. flush_workqueue(adapter->workqueue);
  605. destroy_workqueue(adapter->workqueue);
  606. adapter->workqueue = NULL;
  607. }
  608. /*
  609. * This function adds the card.
  610. *
  611. * This function follows the following major steps to set up the device -
  612. * - Initialize software. This includes probing the card, registering
  613. * the interface operations table, and allocating/initializing the
  614. * adapter structure
  615. * - Set up the netlink socket
  616. * - Create and start the main work queue
  617. * - Register the device
  618. * - Initialize firmware and hardware
  619. * - Add logical interfaces
  620. */
  621. int
  622. mwifiex_add_card(void *card, struct semaphore *sem,
  623. struct mwifiex_if_ops *if_ops, u8 iface_type)
  624. {
  625. struct mwifiex_adapter *adapter;
  626. if (down_interruptible(sem))
  627. goto exit_sem_err;
  628. if (mwifiex_register(card, if_ops, (void **)&adapter)) {
  629. pr_err("%s: software init failed\n", __func__);
  630. goto err_init_sw;
  631. }
  632. adapter->iface_type = iface_type;
  633. adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
  634. adapter->surprise_removed = false;
  635. init_waitqueue_head(&adapter->init_wait_q);
  636. adapter->is_suspended = false;
  637. adapter->hs_activated = false;
  638. init_waitqueue_head(&adapter->hs_activate_wait_q);
  639. adapter->cmd_wait_q_required = false;
  640. init_waitqueue_head(&adapter->cmd_wait_q.wait);
  641. adapter->cmd_wait_q.status = 0;
  642. adapter->scan_wait_q_woken = false;
  643. adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
  644. if (!adapter->workqueue)
  645. goto err_kmalloc;
  646. INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
  647. /* Register the device. Fill up the private data structure with relevant
  648. information from the card and request for the required IRQ. */
  649. if (adapter->if_ops.register_dev(adapter)) {
  650. pr_err("%s: failed to register mwifiex device\n", __func__);
  651. goto err_registerdev;
  652. }
  653. if (mwifiex_init_hw_fw(adapter)) {
  654. pr_err("%s: firmware init failed\n", __func__);
  655. goto err_init_fw;
  656. }
  657. up(sem);
  658. return 0;
  659. err_init_fw:
  660. pr_debug("info: %s: unregister device\n", __func__);
  661. if (adapter->if_ops.unregister_dev)
  662. adapter->if_ops.unregister_dev(adapter);
  663. err_registerdev:
  664. adapter->surprise_removed = true;
  665. mwifiex_terminate_workqueue(adapter);
  666. err_kmalloc:
  667. if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
  668. (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
  669. pr_debug("info: %s: shutdown mwifiex\n", __func__);
  670. adapter->init_wait_q_woken = false;
  671. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  672. wait_event_interruptible(adapter->init_wait_q,
  673. adapter->init_wait_q_woken);
  674. }
  675. mwifiex_free_adapter(adapter);
  676. err_init_sw:
  677. up(sem);
  678. exit_sem_err:
  679. return -1;
  680. }
  681. EXPORT_SYMBOL_GPL(mwifiex_add_card);
  682. /*
  683. * This function removes the card.
  684. *
  685. * This function follows the following major steps to remove the device -
  686. * - Stop data traffic
  687. * - Shutdown firmware
  688. * - Remove the logical interfaces
  689. * - Terminate the work queue
  690. * - Unregister the device
  691. * - Free the adapter structure
  692. */
  693. int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
  694. {
  695. struct mwifiex_private *priv = NULL;
  696. int i;
  697. if (down_interruptible(sem))
  698. goto exit_sem_err;
  699. if (!adapter)
  700. goto exit_remove;
  701. adapter->surprise_removed = true;
  702. /* Stop data */
  703. for (i = 0; i < adapter->priv_num; i++) {
  704. priv = adapter->priv[i];
  705. if (priv && priv->netdev) {
  706. if (!netif_queue_stopped(priv->netdev))
  707. mwifiex_stop_net_dev_queue(priv->netdev,
  708. adapter);
  709. if (netif_carrier_ok(priv->netdev))
  710. netif_carrier_off(priv->netdev);
  711. }
  712. }
  713. dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
  714. adapter->init_wait_q_woken = false;
  715. if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
  716. wait_event_interruptible(adapter->init_wait_q,
  717. adapter->init_wait_q_woken);
  718. dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
  719. if (atomic_read(&adapter->rx_pending) ||
  720. atomic_read(&adapter->tx_pending) ||
  721. atomic_read(&adapter->cmd_pending)) {
  722. dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
  723. "cmd_pending=%d\n",
  724. atomic_read(&adapter->rx_pending),
  725. atomic_read(&adapter->tx_pending),
  726. atomic_read(&adapter->cmd_pending));
  727. }
  728. for (i = 0; i < adapter->priv_num; i++) {
  729. priv = adapter->priv[i];
  730. if (!priv)
  731. continue;
  732. rtnl_lock();
  733. if (priv->wdev && priv->netdev)
  734. mwifiex_del_virtual_intf(adapter->wiphy, priv->netdev);
  735. rtnl_unlock();
  736. }
  737. priv = adapter->priv[0];
  738. if (!priv || !priv->wdev)
  739. goto exit_remove;
  740. wiphy_unregister(priv->wdev->wiphy);
  741. wiphy_free(priv->wdev->wiphy);
  742. for (i = 0; i < adapter->priv_num; i++) {
  743. priv = adapter->priv[i];
  744. if (priv)
  745. kfree(priv->wdev);
  746. }
  747. mwifiex_terminate_workqueue(adapter);
  748. /* Unregister device */
  749. dev_dbg(adapter->dev, "info: unregister device\n");
  750. if (adapter->if_ops.unregister_dev)
  751. adapter->if_ops.unregister_dev(adapter);
  752. /* Free adapter structure */
  753. dev_dbg(adapter->dev, "info: free adapter\n");
  754. mwifiex_free_adapter(adapter);
  755. exit_remove:
  756. up(sem);
  757. exit_sem_err:
  758. return 0;
  759. }
  760. EXPORT_SYMBOL_GPL(mwifiex_remove_card);
  761. /*
  762. * This function initializes the module.
  763. *
  764. * The debug FS is also initialized if configured.
  765. */
  766. static int
  767. mwifiex_init_module(void)
  768. {
  769. #ifdef CONFIG_DEBUG_FS
  770. mwifiex_debugfs_init();
  771. #endif
  772. return 0;
  773. }
  774. /*
  775. * This function cleans up the module.
  776. *
  777. * The debug FS is removed if available.
  778. */
  779. static void
  780. mwifiex_cleanup_module(void)
  781. {
  782. #ifdef CONFIG_DEBUG_FS
  783. mwifiex_debugfs_remove();
  784. #endif
  785. }
  786. module_init(mwifiex_init_module);
  787. module_exit(mwifiex_cleanup_module);
  788. MODULE_AUTHOR("Marvell International Ltd.");
  789. MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
  790. MODULE_VERSION(VERSION);
  791. MODULE_LICENSE("GPL v2");