2 * Marvell Wireless LAN device driver: major functions
4 * Copyright (C) 2011, Marvell International Ltd.
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.
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.
27 const char driver_version[] = "mwifiex " VERSION " (%s) ";
29 struct mwifiex_adapter *g_adapter;
30 EXPORT_SYMBOL_GPL(g_adapter);
32 static struct mwifiex_bss_attr mwifiex_bss_sta[] = {
33 {MWIFIEX_BSS_TYPE_STA, MWIFIEX_DATA_FRAME_TYPE_ETH_II, true, 0, 0},
36 static int drv_mode = DRV_MODE_STA;
38 static char fw_name[32] = DEFAULT_FW_NAME;
40 /* Supported drv_mode table */
41 static struct mwifiex_drv_mode mwifiex_drv_mode_tbl[] = {
44 .drv_mode = DRV_MODE_STA,
46 .intf_num = ARRAY_SIZE(mwifiex_bss_sta),
48 .bss_attr = mwifiex_bss_sta,
54 * This function registers the device and performs all the necessary
57 * The following initialization operations are performed -
58 * - Allocate adapter structure
59 * - Save interface specific operations table in adapter
60 * - Call interface specific initialization routine
61 * - Allocate private structures
62 * - Set default adapter structure parameters
65 * In case of any errors during inittialization, this function also ensures
66 * proper cleanup before exiting.
68 static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
69 struct mwifiex_device *mdevice, void **padapter)
72 struct mwifiex_adapter *adapter = NULL;
75 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
76 /* Allocate memory for adapter structure */
83 /* Save interface specific operations in adapter */
84 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
86 /* card specific initialization has been deferred until now .. */
87 ret = adapter->if_ops.init_if(adapter);
91 adapter->priv_num = 0;
92 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
93 adapter->priv[i] = NULL;
95 if (!mdevice->bss_attr[i].active)
98 /* For valid bss_attr,
99 allocate memory for private structure */
100 adapter->priv[i] = kzalloc(sizeof(struct mwifiex_private),
102 if (!adapter->priv[i]) {
103 dev_err(adapter->dev, "%s: failed to alloc priv[%d]\n",
109 memset(adapter->priv[i], 0,
110 sizeof(struct mwifiex_private));
111 adapter->priv[i]->adapter = adapter;
112 /* Save bss_type, frame_type & bss_priority */
113 adapter->priv[i]->bss_type = (u8) mdevice->bss_attr[i].bss_type;
114 adapter->priv[i]->frame_type =
115 (u8) mdevice->bss_attr[i].frame_type;
116 adapter->priv[i]->bss_priority =
117 (u8) mdevice->bss_attr[i].bss_priority;
118 if (mdevice->bss_attr[i].bss_type == MWIFIEX_BSS_TYPE_STA)
119 adapter->priv[i]->bss_role = MWIFIEX_BSS_ROLE_STA;
120 else if (mdevice->bss_attr[i].bss_type == MWIFIEX_BSS_TYPE_UAP)
121 adapter->priv[i]->bss_role = MWIFIEX_BSS_ROLE_UAP;
123 /* Save bss_index & bss_num */
124 adapter->priv[i]->bss_index = i;
125 adapter->priv[i]->bss_num = mdevice->bss_attr[i].bss_num;
128 /* Initialize lock variables */
129 if (mwifiex_init_lock_list(adapter))
132 init_timer(&adapter->cmd_timer);
133 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
134 adapter->cmd_timer.data = (unsigned long) adapter;
136 /* Return pointer of struct mwifiex_adapter */
141 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
143 /* Free lock variables */
144 mwifiex_free_lock_list(adapter);
145 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++)
146 kfree(adapter->priv[i]);
153 * This function unregisters the device and performs all the necessary
156 * The following cleanup operations are performed -
158 * - Free beacon buffers
159 * - Free private structures
160 * - Free adapter structure
162 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
166 del_timer(&adapter->cmd_timer);
168 /* Free private structures */
169 for (i = 0; i < adapter->priv_num; i++) {
170 if (adapter->priv[i]) {
171 mwifiex_free_curr_bcn(adapter->priv[i]);
172 kfree(adapter->priv[i]);
183 * This function is the main procedure of the driver and handles various driver
184 * operations. It runs in a loop and provides the core functionalities.
186 * The main responsibilities of this function are -
187 * - Ensure concurrency control
188 * - Handle pending interrupts and call interrupt handlers
189 * - Wake up the card if required
190 * - Handle command responses and call response handlers
191 * - Handle events and call event handlers
192 * - Execute pending commands
193 * - Transmit pending data packets
195 int mwifiex_main_process(struct mwifiex_adapter *adapter)
200 spin_lock_irqsave(&adapter->main_proc_lock, flags);
202 /* Check if already processing */
203 if (adapter->mwifiex_processing) {
204 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
207 adapter->mwifiex_processing = true;
208 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
212 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
213 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
216 /* Handle pending interrupt if any */
217 if (adapter->int_status) {
218 if (adapter->hs_activated)
219 mwifiex_process_hs_config(adapter);
220 adapter->if_ops.process_int_status(adapter);
223 /* Need to wake up the card ? */
224 if ((adapter->ps_state == PS_STATE_SLEEP) &&
225 (adapter->pm_wakeup_card_req &&
226 !adapter->pm_wakeup_fw_try) &&
227 (is_command_pending(adapter)
228 || !mwifiex_wmm_lists_empty(adapter))) {
229 adapter->pm_wakeup_fw_try = true;
230 adapter->if_ops.wakeup(adapter);
233 if (IS_CARD_RX_RCVD(adapter)) {
234 adapter->pm_wakeup_fw_try = false;
235 if (adapter->ps_state == PS_STATE_SLEEP)
236 adapter->ps_state = PS_STATE_AWAKE;
238 /* We have tried to wakeup the card already */
239 if (adapter->pm_wakeup_fw_try)
241 if (adapter->ps_state != PS_STATE_AWAKE ||
242 adapter->tx_lock_flag)
245 if (adapter->scan_processing || adapter->data_sent
246 || mwifiex_wmm_lists_empty(adapter)) {
247 if (adapter->cmd_sent || adapter->curr_cmd
248 || (!is_command_pending(adapter)))
253 /* Check for Cmd Resp */
254 if (adapter->cmd_resp_received) {
255 adapter->cmd_resp_received = false;
256 mwifiex_process_cmdresp(adapter);
258 /* call mwifiex back when init_fw is done */
259 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
260 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
261 mwifiex_init_fw_complete(adapter);
265 /* Check for event */
266 if (adapter->event_received) {
267 adapter->event_received = false;
268 mwifiex_process_event(adapter);
271 /* Check if we need to confirm Sleep Request
272 received previously */
273 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
274 if (!adapter->cmd_sent && !adapter->curr_cmd)
275 mwifiex_check_ps_cond(adapter);
278 /* * The ps_state may have been changed during processing of
279 * Sleep Request event.
281 if ((adapter->ps_state == PS_STATE_SLEEP)
282 || (adapter->ps_state == PS_STATE_PRE_SLEEP)
283 || (adapter->ps_state == PS_STATE_SLEEP_CFM)
284 || adapter->tx_lock_flag)
287 if (!adapter->cmd_sent && !adapter->curr_cmd) {
288 if (mwifiex_exec_next_cmd(adapter) == -1) {
294 if (!adapter->scan_processing && !adapter->data_sent &&
295 !mwifiex_wmm_lists_empty(adapter)) {
296 mwifiex_wmm_process_tx(adapter);
297 if (adapter->hs_activated) {
298 adapter->is_hs_configured = false;
299 mwifiex_hs_activated_event
301 (adapter, MWIFIEX_BSS_ROLE_ANY),
306 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
307 !adapter->curr_cmd && !is_command_pending(adapter)
308 && mwifiex_wmm_lists_empty(adapter)) {
309 if (!mwifiex_send_null_packet
310 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
311 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
312 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
313 adapter->delay_null_pkt = false;
314 adapter->ps_state = PS_STATE_SLEEP;
320 if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter))
323 spin_lock_irqsave(&adapter->main_proc_lock, flags);
324 adapter->mwifiex_processing = false;
325 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
328 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
329 mwifiex_shutdown_drv(adapter);
334 * This function initializes the software.
336 * The main work includes allocating and initializing the adapter structure
337 * and initializing the private structures.
340 mwifiex_init_sw(void *card, struct mwifiex_if_ops *if_ops, void **pmwifiex)
343 struct mwifiex_device device;
344 struct mwifiex_drv_mode *drv_mode_ptr;
346 /* find mwifiex_drv_mode entry from mwifiex_drv_mode_tbl */
348 for (i = 0; i < ARRAY_SIZE(mwifiex_drv_mode_tbl); i++) {
349 if (mwifiex_drv_mode_tbl[i].drv_mode == drv_mode) {
350 drv_mode_ptr = &mwifiex_drv_mode_tbl[i];
356 pr_err("invalid drv_mode=%d\n", drv_mode);
360 memset(&device, 0, sizeof(struct mwifiex_device));
362 for (i = 0; i < drv_mode_ptr->intf_num; i++) {
363 device.bss_attr[i].bss_type =
364 drv_mode_ptr->bss_attr[i].bss_type;
365 device.bss_attr[i].frame_type =
366 drv_mode_ptr->bss_attr[i].frame_type;
367 device.bss_attr[i].active = drv_mode_ptr->bss_attr[i].active;
368 device.bss_attr[i].bss_priority =
369 drv_mode_ptr->bss_attr[i].bss_priority;
370 device.bss_attr[i].bss_num = drv_mode_ptr->bss_attr[i].bss_num;
373 if (mwifiex_register(card, if_ops, &device, pmwifiex))
380 * This function frees the adapter structure.
382 * Additionally, this closes the netlink socket, frees the timers
383 * and private structures.
385 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
388 pr_err("%s: adapter is NULL\n", __func__);
392 mwifiex_unregister(adapter);
393 pr_debug("info: %s: free adapter\n", __func__);
397 * This function initializes the hardware and firmware.
399 * The main initialization steps followed are -
400 * - Download the correct firmware to card
401 * - Allocate and initialize the adapter structure
402 * - Initialize the private structures
403 * - Issue the init commands to firmware
405 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
409 struct mwifiex_fw_image fw;
411 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
413 switch (adapter->revision_id) {
416 strcpy(fw_name, SD8787_W1_FW_NAME);
420 strcpy(fw_name, SD8787_AX_FW_NAME);
426 err = request_firmware(&adapter->firmware, fw_name, adapter->dev);
428 dev_err(adapter->dev, "request_firmware() returned"
429 " error code %#x\n", err);
433 fw.fw_buf = (u8 *) adapter->firmware->data;
434 fw.fw_len = adapter->firmware->size;
436 ret = mwifiex_dnld_fw(adapter, &fw);
440 dev_notice(adapter->dev, "WLAN FW is active\n");
442 adapter->init_wait_q_woken = false;
443 ret = mwifiex_init_fw(adapter);
447 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
450 /* Wait for mwifiex_init to complete */
451 wait_event_interruptible(adapter->init_wait_q,
452 adapter->init_wait_q_woken);
453 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY) {
460 if (adapter->firmware)
461 release_firmware(adapter->firmware);
468 * This function fills a driver buffer.
470 * The function associates a given SKB with the provided driver buffer
471 * and also updates some of the SKB parameters, including IP header,
472 * priority and timestamp.
475 mwifiex_fill_buffer(struct sk_buff *skb)
477 struct ethhdr *eth = NULL;
482 eth = (struct ethhdr *) skb->data;
483 switch (eth->h_proto) {
484 case __constant_htons(ETH_P_IP):
486 tid = IPTOS_PREC(iph->tos);
487 pr_debug("data: packet type ETH_P_IP: %04x, tid=%#x prio=%#x\n",
488 eth->h_proto, tid, skb->priority);
490 case __constant_htons(ETH_P_ARP):
491 pr_debug("data: ARP packet: %04x\n", eth->h_proto);
495 /* Offset for TOS field in the IP header */
496 #define IPTOS_OFFSET 5
497 tid = (tid >> IPTOS_OFFSET);
499 /* Record the current time the packet was queued; used to
500 determine the amount of time the packet was queued in
501 the driver before it was sent to the firmware.
502 The delay is then sent along with the packet to the
503 firmware for aggregate delay calculation for stats and
504 MSDU lifetime expiry.
506 do_gettimeofday(&tv);
507 skb->tstamp = timeval_to_ktime(tv);
512 * CFG802.11 network device handler for open.
514 * Starts the data queue.
517 mwifiex_open(struct net_device *dev)
519 netif_start_queue(dev);
524 * CFG802.11 network device handler for close.
527 mwifiex_close(struct net_device *dev)
533 * CFG802.11 network device handler for data transmission.
536 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
538 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
539 struct sk_buff *new_skb = NULL;
540 struct mwifiex_txinfo *tx_info;
542 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d): Data <= kernel\n",
543 jiffies, priv->bss_index);
545 if (priv->adapter->surprise_removed) {
547 priv->stats.tx_dropped++;
550 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
551 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
553 priv->stats.tx_dropped++;
556 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
557 dev_dbg(priv->adapter->dev,
558 "data: Tx: insufficient skb headroom %d\n",
560 /* Insufficient skb headroom - allocate a new skb */
562 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
563 if (unlikely(!new_skb)) {
564 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
566 priv->stats.tx_dropped++;
571 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
575 tx_info = MWIFIEX_SKB_TXCB(skb);
576 tx_info->bss_index = priv->bss_index;
577 mwifiex_fill_buffer(skb);
579 mwifiex_wmm_add_buf_txqueue(priv->adapter, skb);
580 atomic_inc(&priv->adapter->tx_pending);
582 if (atomic_read(&priv->adapter->tx_pending) >= MAX_TX_PENDING) {
583 netif_stop_queue(priv->netdev);
584 dev->trans_start = jiffies;
587 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
593 * CFG802.11 network device handler for setting MAC address.
596 mwifiex_set_mac_address(struct net_device *dev, void *addr)
598 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
599 struct sockaddr *hw_addr = (struct sockaddr *) addr;
602 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
604 /* Send request to firmware */
605 ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
606 HostCmd_ACT_GEN_SET, 0, NULL);
609 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
611 dev_err(priv->adapter->dev, "set mac address failed: ret=%d"
614 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
620 * CFG802.11 network device handler for setting multicast list.
622 static void mwifiex_set_multicast_list(struct net_device *dev)
624 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
625 struct mwifiex_multicast_list mcast_list;
627 if (dev->flags & IFF_PROMISC) {
628 mcast_list.mode = MWIFIEX_PROMISC_MODE;
629 } else if (dev->flags & IFF_ALLMULTI ||
630 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
631 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
633 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
634 if (netdev_mc_count(dev))
635 mcast_list.num_multicast_addr =
636 mwifiex_copy_mcast_addr(&mcast_list, dev);
638 mwifiex_request_set_multicast_list(priv, &mcast_list);
642 * CFG802.11 network device handler for transmission timeout.
645 mwifiex_tx_timeout(struct net_device *dev)
647 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
649 dev_err(priv->adapter->dev, "%lu : Tx timeout, bss_index=%d\n",
650 jiffies, priv->bss_index);
651 dev->trans_start = jiffies;
652 priv->num_tx_timeout++;
656 * CFG802.11 network device handler for statistics retrieval.
658 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
660 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
665 /* Network device handlers */
666 static const struct net_device_ops mwifiex_netdev_ops = {
667 .ndo_open = mwifiex_open,
668 .ndo_stop = mwifiex_close,
669 .ndo_start_xmit = mwifiex_hard_start_xmit,
670 .ndo_set_mac_address = mwifiex_set_mac_address,
671 .ndo_tx_timeout = mwifiex_tx_timeout,
672 .ndo_get_stats = mwifiex_get_stats,
673 .ndo_set_multicast_list = mwifiex_set_multicast_list,
677 * This function initializes the private structure parameters.
679 * The following wait queues are initialized -
681 * - Command wait queue
682 * - Statistics wait queue
684 * ...and the following default parameters are set -
685 * - Current key index : Set to 0
686 * - Rate index : Set to auto
687 * - Media connected : Set to disconnected
688 * - Adhoc link sensed : Set to false
689 * - Nick name : Set to null
690 * - Number of Tx timeout : Set to 0
691 * - Device address : Set to current address
693 * In addition, the CFG80211 work queue is also created.
696 mwifiex_init_priv_params(struct mwifiex_private *priv, struct net_device *dev)
698 dev->netdev_ops = &mwifiex_netdev_ops;
699 /* Initialize private structure */
700 priv->current_key_index = 0;
701 priv->media_connected = false;
702 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
703 priv->num_tx_timeout = 0;
704 priv->workqueue = create_singlethread_workqueue("cfg80211_wq");
705 INIT_WORK(&priv->cfg_workqueue, mwifiex_cfg80211_results);
706 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
710 * This function adds a new logical interface.
712 * It allocates, initializes and registers the interface by performing
713 * the following opearations -
714 * - Allocate a new net device structure
715 * - Assign device name
716 * - Register the new device with CFG80211 subsystem
717 * - Initialize semaphore and private structure
718 * - Register the new device with kernel
719 * - Create the complete debug FS structure if configured
721 static struct mwifiex_private *mwifiex_add_interface(
722 struct mwifiex_adapter *adapter,
723 u8 bss_index, u8 bss_type)
725 struct net_device *dev = NULL;
726 struct mwifiex_private *priv = NULL;
727 void *mdev_priv = NULL;
729 dev = alloc_netdev_mq(sizeof(struct mwifiex_private *), "mlan%d",
732 dev_err(adapter->dev, "no memory available for netdevice\n");
735 if (dev_alloc_name(dev, dev->name)) {
736 dev_err(adapter->dev, "unable to alloc name for netdevice\n");
740 if (mwifiex_register_cfg80211(dev, adapter->priv[bss_index]->curr_addr,
741 adapter->priv[bss_index]) != 0) {
742 dev_err(adapter->dev, "cannot register netdevice with cfg80211\n");
745 /* Save the priv pointer in netdev */
746 priv = adapter->priv[bss_index];
747 mdev_priv = netdev_priv(dev);
748 *((unsigned long *) mdev_priv) = (unsigned long) priv;
752 sema_init(&priv->async_sem, 1);
753 priv->scan_pending_on_block = false;
755 mwifiex_init_priv_params(priv, dev);
757 SET_NETDEV_DEV(dev, adapter->dev);
759 /* Register network device */
760 if (register_netdev(dev)) {
761 dev_err(adapter->dev, "cannot register virtual network device\n");
765 dev_dbg(adapter->dev, "info: %s: Marvell 802.11 Adapter\n", dev->name);
766 #ifdef CONFIG_DEBUG_FS
767 mwifiex_dev_debugfs_init(priv);
777 * This function removes a logical interface.
779 * It deregisters, resets and frees the interface by performing
780 * the following operations -
781 * - Disconnect the device if connected, send wireless event to
782 * notify applications.
783 * - Remove the debug FS structure if configured
784 * - Unregister the device from kernel
785 * - Free the net device structure
786 * - Cancel all works and destroy work queue
787 * - Unregister and free the wireless device from CFG80211 subsystem
790 mwifiex_remove_interface(struct mwifiex_adapter *adapter, u8 bss_index)
792 struct net_device *dev = NULL;
793 struct mwifiex_private *priv = adapter->priv[bss_index];
799 if (priv->media_connected)
800 priv->media_connected = false;
802 #ifdef CONFIG_DEBUG_FS
803 mwifiex_dev_debugfs_remove(priv);
805 /* Last reference is our one */
806 dev_dbg(adapter->dev, "info: %s: refcnt = %d\n",
807 dev->name, netdev_refcnt_read(dev));
809 if (dev->reg_state == NETREG_REGISTERED)
810 unregister_netdev(dev);
812 /* Clear the priv in adapter */
817 cancel_work_sync(&priv->cfg_workqueue);
818 flush_workqueue(priv->workqueue);
819 destroy_workqueue(priv->workqueue);
820 wiphy_unregister(priv->wdev->wiphy);
821 wiphy_free(priv->wdev->wiphy);
828 * This function check if command is pending.
830 int is_command_pending(struct mwifiex_adapter *adapter)
833 int is_cmd_pend_q_empty;
835 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
836 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
837 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
839 return !is_cmd_pend_q_empty;
843 * This function returns the correct private structure pointer based
844 * upon the BSS number.
846 struct mwifiex_private *
847 mwifiex_bss_index_to_priv(struct mwifiex_adapter *adapter, u8 bss_index)
849 if (!adapter || (bss_index >= adapter->priv_num))
851 return adapter->priv[bss_index];
855 * This is the main work queue function.
857 * It handles the main process, which in turn handles the complete
860 static void mwifiex_main_work_queue(struct work_struct *work)
862 struct mwifiex_adapter *adapter =
863 container_of(work, struct mwifiex_adapter, main_work);
865 if (adapter->surprise_removed)
867 mwifiex_main_process(adapter);
871 * This function cancels all works in the queue and destroys
872 * the main workqueue.
875 mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
877 flush_workqueue(adapter->workqueue);
878 destroy_workqueue(adapter->workqueue);
879 adapter->workqueue = NULL;
883 * This function adds the card.
885 * This function follows the following major steps to set up the device -
886 * - Initialize software. This includes probing the card, registering
887 * the interface operations table, and allocating/initializing the
889 * - Set up the netlink socket
890 * - Create and start the main work queue
891 * - Register the device
892 * - Initialize firmware and hardware
893 * - Add logical interfaces
896 mwifiex_add_card(void *card, struct semaphore *sem,
897 struct mwifiex_if_ops *if_ops)
901 struct mwifiex_adapter *adapter = NULL;
902 struct mwifiex_drv_mode *drv_mode_info = &mwifiex_drv_mode_tbl[0];
904 if (down_interruptible(sem))
907 if (mwifiex_init_sw(card, if_ops, (void **) &adapter)) {
908 pr_err("%s: software init failed\n", __func__);
912 adapter->drv_mode = drv_mode_info;
914 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
915 /* PnP and power profile */
916 adapter->surprise_removed = false;
917 init_waitqueue_head(&adapter->init_wait_q);
918 adapter->is_suspended = false;
919 adapter->hs_activated = false;
920 init_waitqueue_head(&adapter->hs_activate_wait_q);
921 adapter->cmd_wait_q_required = false;
922 init_waitqueue_head(&adapter->cmd_wait_q.wait);
923 adapter->cmd_wait_q.condition = false;
924 adapter->cmd_wait_q.status = 0;
926 /* Create workqueue */
927 adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
928 if (!adapter->workqueue)
931 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
933 /* Register the device. Fill up the private data structure with relevant
934 information from the card and request for the required IRQ. */
935 if (adapter->if_ops.register_dev(adapter)) {
936 pr_err("%s: failed to register mwifiex device\n", __func__);
937 goto err_registerdev;
941 if (mwifiex_init_hw_fw(adapter)) {
942 pr_err("%s: firmware init failed\n", __func__);
946 for (i = 0; i < drv_mode_info->intf_num; i++) {
947 if (!mwifiex_add_interface(adapter, i,
948 adapter->drv_mode->bss_attr[i].bss_type)) {
961 for (i = 0; i < adapter->priv_num; i++)
962 mwifiex_remove_interface(adapter, i);
964 /* Unregister device */
965 pr_debug("info: %s: unregister device\n", __func__);
966 adapter->if_ops.unregister_dev(adapter);
968 adapter->surprise_removed = true;
969 mwifiex_terminate_workqueue(adapter);
971 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
972 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
973 pr_debug("info: %s: shutdown mwifiex\n", __func__);
974 adapter->init_wait_q_woken = false;
975 status = mwifiex_shutdown_drv(adapter);
976 if (status == -EINPROGRESS)
977 wait_event_interruptible(adapter->init_wait_q,
978 adapter->init_wait_q_woken);
981 mwifiex_free_adapter(adapter);
989 EXPORT_SYMBOL_GPL(mwifiex_add_card);
992 * This function removes the card.
994 * This function follows the following major steps to remove the device -
995 * - Stop data traffic
996 * - Shutdown firmware
997 * - Remove the logical interfaces
998 * - Terminate the work queue
999 * - Unregister the device
1000 * - Free the adapter structure
1002 int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1004 struct mwifiex_private *priv = NULL;
1008 if (down_interruptible(sem))
1014 adapter->surprise_removed = true;
1017 for (i = 0; i < adapter->priv_num; i++) {
1018 priv = adapter->priv[i];
1020 if (!netif_queue_stopped(priv->netdev))
1021 netif_stop_queue(priv->netdev);
1022 if (netif_carrier_ok(priv->netdev))
1023 netif_carrier_off(priv->netdev);
1027 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
1028 adapter->init_wait_q_woken = false;
1029 status = mwifiex_shutdown_drv(adapter);
1030 if (status == -EINPROGRESS)
1031 wait_event_interruptible(adapter->init_wait_q,
1032 adapter->init_wait_q_woken);
1033 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
1034 if (atomic_read(&adapter->rx_pending) ||
1035 atomic_read(&adapter->tx_pending) ||
1036 atomic_read(&adapter->cmd_pending)) {
1037 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
1039 atomic_read(&adapter->rx_pending),
1040 atomic_read(&adapter->tx_pending),
1041 atomic_read(&adapter->cmd_pending));
1044 /* Remove interface */
1045 for (i = 0; i < adapter->priv_num; i++)
1046 mwifiex_remove_interface(adapter, i);
1048 mwifiex_terminate_workqueue(adapter);
1050 /* Unregister device */
1051 dev_dbg(adapter->dev, "info: unregister device\n");
1052 adapter->if_ops.unregister_dev(adapter);
1053 /* Free adapter structure */
1054 dev_dbg(adapter->dev, "info: free adapter\n");
1055 mwifiex_free_adapter(adapter);
1062 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1065 * This function initializes the module.
1067 * The debug FS is also initialized if configured.
1070 mwifiex_init_module(void)
1072 #ifdef CONFIG_DEBUG_FS
1073 mwifiex_debugfs_init();
1079 * This function cleans up the module.
1081 * The debug FS is removed if available.
1084 mwifiex_cleanup_module(void)
1086 #ifdef CONFIG_DEBUG_FS
1087 mwifiex_debugfs_remove();
1091 module_init(mwifiex_init_module);
1092 module_exit(mwifiex_cleanup_module);
1094 MODULE_AUTHOR("Marvell International Ltd.");
1095 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1096 MODULE_VERSION(VERSION);
1097 MODULE_LICENSE("GPL v2");