]> rtime.felk.cvut.cz Git - linux-imx.git/blob - drivers/net/wireless/mwifiex/init.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-imx.git] / drivers / net / wireless / mwifiex / init.c
1 /*
2  * Marvell Wireless LAN device driver: HW/FW Initialization
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
20 #include "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27
28 /*
29  * This function adds a BSS priority table to the table list.
30  *
31  * The function allocates a new BSS priority table node and adds it to
32  * the end of BSS priority table list, kept in driver memory.
33  */
34 static int mwifiex_add_bss_prio_tbl(struct mwifiex_private *priv)
35 {
36         struct mwifiex_adapter *adapter = priv->adapter;
37         struct mwifiex_bss_prio_node *bss_prio;
38         struct mwifiex_bss_prio_tbl *tbl = adapter->bss_prio_tbl;
39         unsigned long flags;
40
41         bss_prio = kzalloc(sizeof(struct mwifiex_bss_prio_node), GFP_KERNEL);
42         if (!bss_prio)
43                 return -ENOMEM;
44
45         bss_prio->priv = priv;
46         INIT_LIST_HEAD(&bss_prio->list);
47
48         spin_lock_irqsave(&tbl[priv->bss_priority].bss_prio_lock, flags);
49         list_add_tail(&bss_prio->list, &tbl[priv->bss_priority].bss_prio_head);
50         spin_unlock_irqrestore(&tbl[priv->bss_priority].bss_prio_lock, flags);
51
52         return 0;
53 }
54
55 /*
56  * This function initializes the private structure and sets default
57  * values to the members.
58  *
59  * Additionally, it also initializes all the locks and sets up all the
60  * lists.
61  */
62 int mwifiex_init_priv(struct mwifiex_private *priv)
63 {
64         u32 i;
65
66         priv->media_connected = false;
67         memset(priv->curr_addr, 0xff, ETH_ALEN);
68
69         priv->pkt_tx_ctrl = 0;
70         priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
71         priv->data_rate = 0;    /* Initially indicate the rate as auto */
72         priv->is_data_rate_auto = true;
73         priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
74         priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
75
76         priv->sec_info.wep_enabled = 0;
77         priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
78         priv->sec_info.encryption_mode = 0;
79         for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
80                 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
81         priv->wep_key_curr_index = 0;
82         priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
83                                 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
84
85         priv->beacon_period = 100; /* beacon interval */ ;
86         priv->attempted_bss_desc = NULL;
87         memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
88         priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
89
90         memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
91         memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
92         memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
93         priv->assoc_rsp_size = 0;
94         priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
95         priv->atim_window = 0;
96         priv->adhoc_state = ADHOC_IDLE;
97         priv->tx_power_level = 0;
98         priv->max_tx_power_level = 0;
99         priv->min_tx_power_level = 0;
100         priv->tx_rate = 0;
101         priv->rxpd_htinfo = 0;
102         priv->rxpd_rate = 0;
103         priv->rate_bitmap = 0;
104         priv->data_rssi_last = 0;
105         priv->data_rssi_avg = 0;
106         priv->data_nf_avg = 0;
107         priv->data_nf_last = 0;
108         priv->bcn_rssi_last = 0;
109         priv->bcn_rssi_avg = 0;
110         priv->bcn_nf_avg = 0;
111         priv->bcn_nf_last = 0;
112         memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
113         memset(&priv->aes_key, 0, sizeof(priv->aes_key));
114         priv->wpa_ie_len = 0;
115         priv->wpa_is_gtk_set = false;
116
117         memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
118         priv->assoc_tlv_buf_len = 0;
119         memset(&priv->wps, 0, sizeof(priv->wps));
120         memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
121         priv->gen_ie_buf_len = 0;
122         memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
123
124         priv->wmm_required = true;
125         priv->wmm_enabled = false;
126         priv->wmm_qosinfo = 0;
127         priv->curr_bcn_buf = NULL;
128         priv->curr_bcn_size = 0;
129         priv->wps_ie = NULL;
130         priv->wps_ie_len = 0;
131         priv->ap_11n_enabled = 0;
132         memset(&priv->roc_cfg, 0, sizeof(priv->roc_cfg));
133
134         priv->scan_block = false;
135
136         priv->csa_chan = 0;
137         priv->csa_expire_time = 0;
138
139         return mwifiex_add_bss_prio_tbl(priv);
140 }
141
142 /*
143  * This function allocates buffers for members of the adapter
144  * structure.
145  *
146  * The memory allocated includes scan table, command buffers, and
147  * sleep confirm command buffer. In addition, the queues are
148  * also initialized.
149  */
150 static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
151 {
152         int ret;
153
154         /* Allocate command buffer */
155         ret = mwifiex_alloc_cmd_buffer(adapter);
156         if (ret) {
157                 dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
158                         __func__);
159                 return -1;
160         }
161
162         adapter->sleep_cfm =
163                 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
164                               + INTF_HEADER_LEN);
165
166         if (!adapter->sleep_cfm) {
167                 dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
168                         " cmd buffer\n", __func__);
169                 return -1;
170         }
171         skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
172
173         return 0;
174 }
175
176 /*
177  * This function initializes the adapter structure and sets default
178  * values to the members of adapter.
179  *
180  * This also initializes the WMM related parameters in the driver private
181  * structures.
182  */
183 static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
184 {
185         struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
186
187         skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
188
189         adapter->cmd_sent = false;
190
191         if (adapter->iface_type == MWIFIEX_SDIO)
192                 adapter->data_sent = true;
193         else
194                 adapter->data_sent = false;
195
196         adapter->cmd_resp_received = false;
197         adapter->event_received = false;
198         adapter->data_received = false;
199
200         adapter->surprise_removed = false;
201
202         adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
203
204         adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
205         adapter->ps_state = PS_STATE_AWAKE;
206         adapter->need_to_wakeup = false;
207
208         adapter->scan_mode = HostCmd_BSS_MODE_ANY;
209         adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
210         adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
211         adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
212
213         adapter->scan_probes = 1;
214
215         adapter->multiple_dtim = 1;
216
217         adapter->local_listen_interval = 0;     /* default value in firmware
218                                                    will be used */
219
220         adapter->is_deep_sleep = false;
221
222         adapter->delay_null_pkt = false;
223         adapter->delay_to_ps = 1000;
224         adapter->enhanced_ps_mode = PS_MODE_AUTO;
225
226         adapter->gen_null_pkt = false;  /* Disable NULL Pkg generation by
227                                            default */
228         adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
229                                            default */
230         adapter->pm_wakeup_card_req = false;
231
232         adapter->pm_wakeup_fw_try = false;
233
234         adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
235         adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
236
237         adapter->is_hs_configured = false;
238         adapter->hs_cfg.conditions = cpu_to_le32(HS_CFG_COND_DEF);
239         adapter->hs_cfg.gpio = HS_CFG_GPIO_DEF;
240         adapter->hs_cfg.gap = HS_CFG_GAP_DEF;
241         adapter->hs_activated = false;
242
243         memset(adapter->event_body, 0, sizeof(adapter->event_body));
244         adapter->hw_dot_11n_dev_cap = 0;
245         adapter->hw_dev_mcs_support = 0;
246         adapter->sec_chan_offset = 0;
247         adapter->adhoc_11n_enabled = false;
248
249         mwifiex_wmm_init(adapter);
250
251         if (adapter->sleep_cfm) {
252                 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
253                                                 adapter->sleep_cfm->data;
254                 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
255                 sleep_cfm_buf->command =
256                                 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
257                 sleep_cfm_buf->size =
258                                 cpu_to_le16(adapter->sleep_cfm->len);
259                 sleep_cfm_buf->result = 0;
260                 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
261                 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
262         }
263         memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
264         memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
265         adapter->tx_lock_flag = false;
266         adapter->null_pkt_interval = 0;
267         adapter->fw_bands = 0;
268         adapter->config_bands = 0;
269         adapter->adhoc_start_band = 0;
270         adapter->scan_channels = NULL;
271         adapter->fw_release_number = 0;
272         adapter->fw_cap_info = 0;
273         memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
274         adapter->event_cause = 0;
275         adapter->region_code = 0;
276         adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
277         adapter->adhoc_awake_period = 0;
278         memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
279         adapter->arp_filter_size = 0;
280         adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
281         adapter->empty_tx_q_cnt = 0;
282 }
283
284 /*
285  * This function sets trans_start per tx_queue
286  */
287 void mwifiex_set_trans_start(struct net_device *dev)
288 {
289         int i;
290
291         for (i = 0; i < dev->num_tx_queues; i++)
292                 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
293
294         dev->trans_start = jiffies;
295 }
296
297 /*
298  * This function wakes up all queues in net_device
299  */
300 void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
301                                         struct mwifiex_adapter *adapter)
302 {
303         unsigned long dev_queue_flags;
304         unsigned int i;
305
306         spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
307
308         for (i = 0; i < netdev->num_tx_queues; i++) {
309                 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
310
311                 if (netif_tx_queue_stopped(txq))
312                         netif_tx_wake_queue(txq);
313         }
314
315         spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
316 }
317
318 /*
319  * This function stops all queues in net_device
320  */
321 void mwifiex_stop_net_dev_queue(struct net_device *netdev,
322                                         struct mwifiex_adapter *adapter)
323 {
324         unsigned long dev_queue_flags;
325         unsigned int i;
326
327         spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
328
329         for (i = 0; i < netdev->num_tx_queues; i++) {
330                 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
331
332                 if (!netif_tx_queue_stopped(txq))
333                         netif_tx_stop_queue(txq);
334         }
335
336         spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
337 }
338
339 /*
340  *  This function releases the lock variables and frees the locks and
341  *  associated locks.
342  */
343 static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
344 {
345         struct mwifiex_private *priv;
346         s32 i, j;
347
348         /* Free lists */
349         list_del(&adapter->cmd_free_q);
350         list_del(&adapter->cmd_pending_q);
351         list_del(&adapter->scan_pending_q);
352
353         for (i = 0; i < adapter->priv_num; i++)
354                 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
355
356         for (i = 0; i < adapter->priv_num; i++) {
357                 if (adapter->priv[i]) {
358                         priv = adapter->priv[i];
359                         for (j = 0; j < MAX_NUM_TID; ++j)
360                                 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
361                         list_del(&priv->tx_ba_stream_tbl_ptr);
362                         list_del(&priv->rx_reorder_tbl_ptr);
363                         list_del(&priv->sta_list);
364                 }
365         }
366 }
367
368 /*
369  * This function performs cleanup for adapter structure.
370  *
371  * The cleanup is done recursively, by canceling all pending
372  * commands, freeing the member buffers previously allocated
373  * (command buffers, scan table buffer, sleep confirm command
374  * buffer), stopping the timers and calling the cleanup routines
375  * for every interface.
376  */
377 static void
378 mwifiex_adapter_cleanup(struct mwifiex_adapter *adapter)
379 {
380         int i;
381
382         if (!adapter) {
383                 pr_err("%s: adapter is NULL\n", __func__);
384                 return;
385         }
386
387         for (i = 0; i < adapter->priv_num; i++) {
388                 if (adapter->priv[i])
389                         del_timer_sync(&adapter->priv[i]->scan_delay_timer);
390         }
391
392         mwifiex_cancel_all_pending_cmd(adapter);
393
394         /* Free lock variables */
395         mwifiex_free_lock_list(adapter);
396
397         /* Free command buffer */
398         dev_dbg(adapter->dev, "info: free cmd buffer\n");
399         mwifiex_free_cmd_buffer(adapter);
400
401         del_timer(&adapter->cmd_timer);
402
403         dev_dbg(adapter->dev, "info: free scan table\n");
404
405         if (adapter->if_ops.cleanup_if)
406                 adapter->if_ops.cleanup_if(adapter);
407
408         if (adapter->sleep_cfm)
409                 dev_kfree_skb_any(adapter->sleep_cfm);
410 }
411
412 /*
413  *  This function intializes the lock variables and
414  *  the list heads.
415  */
416 int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
417 {
418         struct mwifiex_private *priv;
419         s32 i, j;
420
421         spin_lock_init(&adapter->mwifiex_lock);
422         spin_lock_init(&adapter->int_lock);
423         spin_lock_init(&adapter->main_proc_lock);
424         spin_lock_init(&adapter->mwifiex_cmd_lock);
425         spin_lock_init(&adapter->queue_lock);
426         for (i = 0; i < adapter->priv_num; i++) {
427                 if (adapter->priv[i]) {
428                         priv = adapter->priv[i];
429                         spin_lock_init(&priv->rx_pkt_lock);
430                         spin_lock_init(&priv->wmm.ra_list_spinlock);
431                         spin_lock_init(&priv->curr_bcn_buf_lock);
432                         spin_lock_init(&priv->sta_list_spinlock);
433                 }
434         }
435
436         /* Initialize cmd_free_q */
437         INIT_LIST_HEAD(&adapter->cmd_free_q);
438         /* Initialize cmd_pending_q */
439         INIT_LIST_HEAD(&adapter->cmd_pending_q);
440         /* Initialize scan_pending_q */
441         INIT_LIST_HEAD(&adapter->scan_pending_q);
442
443         spin_lock_init(&adapter->cmd_free_q_lock);
444         spin_lock_init(&adapter->cmd_pending_q_lock);
445         spin_lock_init(&adapter->scan_pending_q_lock);
446
447         skb_queue_head_init(&adapter->usb_rx_data_q);
448
449         for (i = 0; i < adapter->priv_num; ++i) {
450                 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
451                 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
452         }
453
454         for (i = 0; i < adapter->priv_num; i++) {
455                 if (!adapter->priv[i])
456                         continue;
457                 priv = adapter->priv[i];
458                 for (j = 0; j < MAX_NUM_TID; ++j)
459                         INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
460                 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
461                 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
462                 INIT_LIST_HEAD(&priv->sta_list);
463
464                 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
465                 spin_lock_init(&priv->rx_reorder_tbl_lock);
466         }
467
468         return 0;
469 }
470
471 /*
472  * This function initializes the firmware.
473  *
474  * The following operations are performed sequentially -
475  *      - Allocate adapter structure
476  *      - Initialize the adapter structure
477  *      - Initialize the private structure
478  *      - Add BSS priority tables to the adapter structure
479  *      - For each interface, send the init commands to firmware
480  *      - Send the first command in command pending queue, if available
481  */
482 int mwifiex_init_fw(struct mwifiex_adapter *adapter)
483 {
484         int ret;
485         struct mwifiex_private *priv;
486         u8 i, first_sta = true;
487         int is_cmd_pend_q_empty;
488         unsigned long flags;
489
490         adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
491
492         /* Allocate memory for member of adapter structure */
493         ret = mwifiex_allocate_adapter(adapter);
494         if (ret)
495                 return -1;
496
497         /* Initialize adapter structure */
498         mwifiex_init_adapter(adapter);
499
500         for (i = 0; i < adapter->priv_num; i++) {
501                 if (adapter->priv[i]) {
502                         priv = adapter->priv[i];
503
504                         /* Initialize private structure */
505                         ret = mwifiex_init_priv(priv);
506                         if (ret)
507                                 return -1;
508                 }
509         }
510
511         if (adapter->if_ops.init_fw_port) {
512                 if (adapter->if_ops.init_fw_port(adapter))
513                         return -1;
514         }
515
516         for (i = 0; i < adapter->priv_num; i++) {
517                 if (adapter->priv[i]) {
518                         ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
519                         if (ret == -1)
520                                 return -1;
521
522                         first_sta = false;
523                 }
524         }
525
526         spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
527         is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
528         spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
529         if (!is_cmd_pend_q_empty) {
530                 /* Send the first command in queue and return */
531                 if (mwifiex_main_process(adapter) != -1)
532                         ret = -EINPROGRESS;
533         } else {
534                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
535         }
536
537         return ret;
538 }
539
540 /*
541  * This function deletes the BSS priority tables.
542  *
543  * The function traverses through all the allocated BSS priority nodes
544  * in every BSS priority table and frees them.
545  */
546 static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
547 {
548         int i;
549         struct mwifiex_adapter *adapter = priv->adapter;
550         struct mwifiex_bss_prio_node *bssprio_node, *tmp_node;
551         struct list_head *head;
552         spinlock_t *lock; /* bss priority lock */
553         unsigned long flags;
554
555         for (i = 0; i < adapter->priv_num; ++i) {
556                 head = &adapter->bss_prio_tbl[i].bss_prio_head;
557                 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
558                 dev_dbg(adapter->dev, "info: delete BSS priority table,"
559                                 " bss_type = %d, bss_num = %d, i = %d,"
560                                 " head = %p\n",
561                               priv->bss_type, priv->bss_num, i, head);
562
563                 {
564                         spin_lock_irqsave(lock, flags);
565                         if (list_empty(head)) {
566                                 spin_unlock_irqrestore(lock, flags);
567                                 continue;
568                         }
569                         list_for_each_entry_safe(bssprio_node, tmp_node, head,
570                                                  list) {
571                                 if (bssprio_node->priv == priv) {
572                                         dev_dbg(adapter->dev, "info: Delete "
573                                                 "node %p, next = %p\n",
574                                                 bssprio_node, tmp_node);
575                                         list_del(&bssprio_node->list);
576                                         kfree(bssprio_node);
577                                 }
578                         }
579                         spin_unlock_irqrestore(lock, flags);
580                 }
581         }
582 }
583
584 /*
585  * This function frees the private structure, including cleans
586  * up the TX and RX queues and frees the BSS priority tables.
587  */
588 void mwifiex_free_priv(struct mwifiex_private *priv)
589 {
590         mwifiex_clean_txrx(priv);
591         mwifiex_delete_bss_prio_tbl(priv);
592         mwifiex_free_curr_bcn(priv);
593 }
594
595 /*
596  * This function is used to shutdown the driver.
597  *
598  * The following operations are performed sequentially -
599  *      - Check if already shut down
600  *      - Make sure the main process has stopped
601  *      - Clean up the Tx and Rx queues
602  *      - Delete BSS priority tables
603  *      - Free the adapter
604  *      - Notify completion
605  */
606 int
607 mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
608 {
609         int ret = -EINPROGRESS;
610         struct mwifiex_private *priv;
611         s32 i;
612         struct sk_buff *skb;
613
614         /* mwifiex already shutdown */
615         if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
616                 return 0;
617
618         adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
619         /* wait for mwifiex_process to complete */
620         if (adapter->mwifiex_processing) {
621                 dev_warn(adapter->dev, "main process is still running\n");
622                 return ret;
623         }
624
625         /* cancel current command */
626         if (adapter->curr_cmd) {
627                 dev_warn(adapter->dev, "curr_cmd is still in processing\n");
628                 del_timer(&adapter->cmd_timer);
629                 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
630                 adapter->curr_cmd = NULL;
631         }
632
633         /* shut down mwifiex */
634         dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
635
636         /* Clean up Tx/Rx queues and delete BSS priority table */
637         for (i = 0; i < adapter->priv_num; i++) {
638                 if (adapter->priv[i]) {
639                         priv = adapter->priv[i];
640
641                         mwifiex_clean_txrx(priv);
642                         mwifiex_delete_bss_prio_tbl(priv);
643                 }
644         }
645
646         spin_lock(&adapter->mwifiex_lock);
647
648         if (adapter->if_ops.data_complete) {
649                 while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
650                         struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
651
652                         priv = adapter->priv[rx_info->bss_num];
653                         if (priv)
654                                 priv->stats.rx_dropped++;
655
656                         adapter->if_ops.data_complete(adapter, skb);
657                 }
658         }
659
660         mwifiex_adapter_cleanup(adapter);
661
662         spin_unlock(&adapter->mwifiex_lock);
663
664         /* Notify completion */
665         ret = mwifiex_shutdown_fw_complete(adapter);
666
667         return ret;
668 }
669
670 /*
671  * This function downloads the firmware to the card.
672  *
673  * The actual download is preceded by two sanity checks -
674  *      - Check if firmware is already running
675  *      - Check if the interface is the winner to download the firmware
676  *
677  * ...and followed by another -
678  *      - Check if the firmware is downloaded successfully
679  *
680  * After download is successfully completed, the host interrupts are enabled.
681  */
682 int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
683                     struct mwifiex_fw_image *pmfw)
684 {
685         int ret;
686         u32 poll_num = 1;
687
688         if (adapter->if_ops.check_fw_status) {
689                 adapter->winner = 0;
690
691                 /* check if firmware is already running */
692                 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
693                 if (!ret) {
694                         dev_notice(adapter->dev,
695                                    "WLAN FW already running! Skip FW dnld\n");
696                         return 0;
697                 }
698
699                 poll_num = MAX_FIRMWARE_POLL_TRIES;
700
701                 /* check if we are the winner for downloading FW */
702                 if (!adapter->winner) {
703                         dev_notice(adapter->dev,
704                                    "FW already running! Skip FW dnld\n");
705                         poll_num = MAX_MULTI_INTERFACE_POLL_TRIES;
706                         goto poll_fw;
707                 }
708         }
709
710         if (pmfw) {
711                 /* Download firmware with helper */
712                 ret = adapter->if_ops.prog_fw(adapter, pmfw);
713                 if (ret) {
714                         dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
715                         return ret;
716                 }
717         }
718
719 poll_fw:
720         /* Check if the firmware is downloaded successfully or not */
721         ret = adapter->if_ops.check_fw_status(adapter, poll_num);
722         if (ret)
723                 dev_err(adapter->dev, "FW failed to be active in time\n");
724
725         return ret;
726 }