26fef0ab1b0d295661d7b311af2688727da4fd62
[linux-2.6-microblaze.git] / drivers / net / wireless / marvell / mwifiex / main.c
1 /*
2  * NXP Wireless LAN device driver: major functions
3  *
4  * Copyright 2011-2020 NXP
5  *
6  * This software file (the "File") is distributed by NXP
7  * 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 <linux/suspend.h>
21
22 #include "main.h"
23 #include "wmm.h"
24 #include "cfg80211.h"
25 #include "11n.h"
26
27 #define VERSION "1.0"
28 #define MFG_FIRMWARE    "mwifiex_mfg.bin"
29
30 static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
31 module_param(debug_mask, uint, 0);
32 MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
33
34 const char driver_version[] = "mwifiex " VERSION " (%s) ";
35 static char *cal_data_cfg;
36 module_param(cal_data_cfg, charp, 0);
37
38 static unsigned short driver_mode;
39 module_param(driver_mode, ushort, 0);
40 MODULE_PARM_DESC(driver_mode,
41                  "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
42
43 bool mfg_mode;
44 module_param(mfg_mode, bool, 0);
45 MODULE_PARM_DESC(mfg_mode, "manufacturing mode enable:1, disable:0");
46
47 bool aggr_ctrl;
48 module_param(aggr_ctrl, bool, 0000);
49 MODULE_PARM_DESC(aggr_ctrl, "usb tx aggregation enable:1, disable:0");
50
51 const u16 mwifiex_1d_to_wmm_queue[8] = { 1, 0, 0, 1, 2, 2, 3, 3 };
52
53 /*
54  * This function registers the device and performs all the necessary
55  * initializations.
56  *
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
63  *      - Initialize locks
64  *
65  * In case of any errors during inittialization, this function also ensures
66  * proper cleanup before exiting.
67  */
68 static int mwifiex_register(void *card, struct device *dev,
69                             struct mwifiex_if_ops *if_ops, void **padapter)
70 {
71         struct mwifiex_adapter *adapter;
72         int i;
73
74         adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
75         if (!adapter)
76                 return -ENOMEM;
77
78         *padapter = adapter;
79         adapter->dev = dev;
80         adapter->card = card;
81
82         /* Save interface specific operations in adapter */
83         memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
84         adapter->debug_mask = debug_mask;
85
86         /* card specific initialization has been deferred until now .. */
87         if (adapter->if_ops.init_if)
88                 if (adapter->if_ops.init_if(adapter))
89                         goto error;
90
91         adapter->priv_num = 0;
92
93         for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
94                 /* Allocate memory for private structure */
95                 adapter->priv[i] =
96                         kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
97                 if (!adapter->priv[i])
98                         goto error;
99
100                 adapter->priv[i]->adapter = adapter;
101                 adapter->priv_num++;
102         }
103         mwifiex_init_lock_list(adapter);
104
105         timer_setup(&adapter->cmd_timer, mwifiex_cmd_timeout_func, 0);
106
107         return 0;
108
109 error:
110         mwifiex_dbg(adapter, ERROR,
111                     "info: leave mwifiex_register with error\n");
112
113         for (i = 0; i < adapter->priv_num; i++)
114                 kfree(adapter->priv[i]);
115
116         kfree(adapter);
117
118         return -1;
119 }
120
121 /*
122  * This function unregisters the device and performs all the necessary
123  * cleanups.
124  *
125  * The following cleanup operations are performed -
126  *      - Free the timers
127  *      - Free beacon buffers
128  *      - Free private structures
129  *      - Free adapter structure
130  */
131 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
132 {
133         s32 i;
134
135         if (adapter->if_ops.cleanup_if)
136                 adapter->if_ops.cleanup_if(adapter);
137
138         del_timer_sync(&adapter->cmd_timer);
139
140         /* Free private structures */
141         for (i = 0; i < adapter->priv_num; i++) {
142                 if (adapter->priv[i]) {
143                         mwifiex_free_curr_bcn(adapter->priv[i]);
144                         kfree(adapter->priv[i]);
145                 }
146         }
147
148         if (adapter->nd_info) {
149                 for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
150                         kfree(adapter->nd_info->matches[i]);
151                 kfree(adapter->nd_info);
152                 adapter->nd_info = NULL;
153         }
154
155         kfree(adapter->regd);
156
157         kfree(adapter);
158         return 0;
159 }
160
161 void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
162 {
163         unsigned long flags;
164
165         spin_lock_irqsave(&adapter->main_proc_lock, flags);
166         if (adapter->mwifiex_processing) {
167                 adapter->more_task_flag = true;
168                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
169         } else {
170                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
171                 queue_work(adapter->workqueue, &adapter->main_work);
172         }
173 }
174 EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
175
176 static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
177 {
178         spin_lock_bh(&adapter->rx_proc_lock);
179         if (adapter->rx_processing) {
180                 spin_unlock_bh(&adapter->rx_proc_lock);
181         } else {
182                 spin_unlock_bh(&adapter->rx_proc_lock);
183                 queue_work(adapter->rx_workqueue, &adapter->rx_work);
184         }
185 }
186
187 static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
188 {
189         struct sk_buff *skb;
190         struct mwifiex_rxinfo *rx_info;
191
192         spin_lock_bh(&adapter->rx_proc_lock);
193         if (adapter->rx_processing || adapter->rx_locked) {
194                 spin_unlock_bh(&adapter->rx_proc_lock);
195                 goto exit_rx_proc;
196         } else {
197                 adapter->rx_processing = true;
198                 spin_unlock_bh(&adapter->rx_proc_lock);
199         }
200
201         /* Check for Rx data */
202         while ((skb = skb_dequeue(&adapter->rx_data_q))) {
203                 atomic_dec(&adapter->rx_pending);
204                 if ((adapter->delay_main_work ||
205                      adapter->iface_type == MWIFIEX_USB) &&
206                     (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
207                         if (adapter->if_ops.submit_rem_rx_urbs)
208                                 adapter->if_ops.submit_rem_rx_urbs(adapter);
209                         adapter->delay_main_work = false;
210                         mwifiex_queue_main_work(adapter);
211                 }
212                 rx_info = MWIFIEX_SKB_RXCB(skb);
213                 if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
214                         if (adapter->if_ops.deaggr_pkt)
215                                 adapter->if_ops.deaggr_pkt(adapter, skb);
216                         dev_kfree_skb_any(skb);
217                 } else {
218                         mwifiex_handle_rx_packet(adapter, skb);
219                 }
220         }
221         spin_lock_bh(&adapter->rx_proc_lock);
222         adapter->rx_processing = false;
223         spin_unlock_bh(&adapter->rx_proc_lock);
224
225 exit_rx_proc:
226         return 0;
227 }
228
229 static void maybe_quirk_fw_disable_ds(struct mwifiex_adapter *adapter)
230 {
231         struct mwifiex_private *priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
232         struct mwifiex_ver_ext ver_ext;
233
234         if (test_and_set_bit(MWIFIEX_IS_REQUESTING_FW_VEREXT, &adapter->work_flags))
235                 return;
236
237         memset(&ver_ext, 0, sizeof(ver_ext));
238         ver_ext.version_str_sel = 1;
239         if (mwifiex_send_cmd(priv, HostCmd_CMD_VERSION_EXT,
240                              HostCmd_ACT_GEN_GET, 0, &ver_ext, false)) {
241                 mwifiex_dbg(priv->adapter, MSG,
242                             "Checking hardware revision failed.\n");
243         }
244 }
245
246 /*
247  * The main process.
248  *
249  * This function is the main procedure of the driver and handles various driver
250  * operations. It runs in a loop and provides the core functionalities.
251  *
252  * The main responsibilities of this function are -
253  *      - Ensure concurrency control
254  *      - Handle pending interrupts and call interrupt handlers
255  *      - Wake up the card if required
256  *      - Handle command responses and call response handlers
257  *      - Handle events and call event handlers
258  *      - Execute pending commands
259  *      - Transmit pending data packets
260  */
261 int mwifiex_main_process(struct mwifiex_adapter *adapter)
262 {
263         int ret = 0;
264         unsigned long flags;
265
266         spin_lock_irqsave(&adapter->main_proc_lock, flags);
267
268         /* Check if already processing */
269         if (adapter->mwifiex_processing || adapter->main_locked) {
270                 adapter->more_task_flag = true;
271                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
272                 return 0;
273         } else {
274                 adapter->mwifiex_processing = true;
275                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
276         }
277 process_start:
278         do {
279                 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
280                         break;
281
282                 /* For non-USB interfaces, If we process interrupts first, it
283                  * would increase RX pending even further. Avoid this by
284                  * checking if rx_pending has crossed high threshold and
285                  * schedule rx work queue and then process interrupts.
286                  * For USB interface, there are no interrupts. We already have
287                  * HIGH_RX_PENDING check in usb.c
288                  */
289                 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
290                     adapter->iface_type != MWIFIEX_USB) {
291                         adapter->delay_main_work = true;
292                         mwifiex_queue_rx_work(adapter);
293                         break;
294                 }
295
296                 /* Handle pending interrupt if any */
297                 if (adapter->int_status) {
298                         if (adapter->hs_activated)
299                                 mwifiex_process_hs_config(adapter);
300                         if (adapter->if_ops.process_int_status)
301                                 adapter->if_ops.process_int_status(adapter);
302                 }
303
304                 if (adapter->rx_work_enabled && adapter->data_received)
305                         mwifiex_queue_rx_work(adapter);
306
307                 /* Need to wake up the card ? */
308                 if ((adapter->ps_state == PS_STATE_SLEEP) &&
309                     (adapter->pm_wakeup_card_req &&
310                      !adapter->pm_wakeup_fw_try) &&
311                     (is_command_pending(adapter) ||
312                      !skb_queue_empty(&adapter->tx_data_q) ||
313                      !mwifiex_bypass_txlist_empty(adapter) ||
314                      !mwifiex_wmm_lists_empty(adapter))) {
315                         adapter->pm_wakeup_fw_try = true;
316                         mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
317                         adapter->if_ops.wakeup(adapter);
318                         continue;
319                 }
320
321                 if (IS_CARD_RX_RCVD(adapter)) {
322                         adapter->data_received = false;
323                         adapter->pm_wakeup_fw_try = false;
324                         del_timer(&adapter->wakeup_timer);
325                         if (adapter->ps_state == PS_STATE_SLEEP)
326                                 adapter->ps_state = PS_STATE_AWAKE;
327                 } else {
328                         /* We have tried to wakeup the card already */
329                         if (adapter->pm_wakeup_fw_try)
330                                 break;
331                         if (adapter->ps_state == PS_STATE_PRE_SLEEP)
332                                 mwifiex_check_ps_cond(adapter);
333
334                         if (adapter->ps_state != PS_STATE_AWAKE)
335                                 break;
336                         if (adapter->tx_lock_flag) {
337                                 if (adapter->iface_type == MWIFIEX_USB) {
338                                         if (!adapter->usb_mc_setup)
339                                                 break;
340                                 } else
341                                         break;
342                         }
343
344                         if ((!adapter->scan_chan_gap_enabled &&
345                              adapter->scan_processing) || adapter->data_sent ||
346                              mwifiex_is_tdls_chan_switching
347                              (mwifiex_get_priv(adapter,
348                                                MWIFIEX_BSS_ROLE_STA)) ||
349                             (mwifiex_wmm_lists_empty(adapter) &&
350                              mwifiex_bypass_txlist_empty(adapter) &&
351                              skb_queue_empty(&adapter->tx_data_q))) {
352                                 if (adapter->cmd_sent || adapter->curr_cmd ||
353                                         !mwifiex_is_send_cmd_allowed
354                                                 (mwifiex_get_priv(adapter,
355                                                 MWIFIEX_BSS_ROLE_STA)) ||
356                                     (!is_command_pending(adapter)))
357                                         break;
358                         }
359                 }
360
361                 /* Check for event */
362                 if (adapter->event_received) {
363                         adapter->event_received = false;
364                         mwifiex_process_event(adapter);
365                 }
366
367                 /* Check for Cmd Resp */
368                 if (adapter->cmd_resp_received) {
369                         adapter->cmd_resp_received = false;
370                         mwifiex_process_cmdresp(adapter);
371
372                         /* call mwifiex back when init_fw is done */
373                         if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
374                                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
375                                 mwifiex_init_fw_complete(adapter);
376                                 maybe_quirk_fw_disable_ds(adapter);
377                         }
378                 }
379
380                 /* Check if we need to confirm Sleep Request
381                    received previously */
382                 if (adapter->ps_state == PS_STATE_PRE_SLEEP)
383                         mwifiex_check_ps_cond(adapter);
384
385                 /* * The ps_state may have been changed during processing of
386                  * Sleep Request event.
387                  */
388                 if ((adapter->ps_state == PS_STATE_SLEEP) ||
389                     (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
390                     (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
391                         continue;
392                 }
393
394                 if (adapter->tx_lock_flag) {
395                         if (adapter->iface_type == MWIFIEX_USB) {
396                                 if (!adapter->usb_mc_setup)
397                                         continue;
398                         } else
399                                 continue;
400                 }
401
402                 if (!adapter->cmd_sent && !adapter->curr_cmd &&
403                     mwifiex_is_send_cmd_allowed
404                     (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
405                         if (mwifiex_exec_next_cmd(adapter) == -1) {
406                                 ret = -1;
407                                 break;
408                         }
409                 }
410
411                 /** If USB Multi channel setup ongoing,
412                  *  wait for ready to tx data.
413                  */
414                 if (adapter->iface_type == MWIFIEX_USB &&
415                     adapter->usb_mc_setup)
416                         continue;
417
418                 if ((adapter->scan_chan_gap_enabled ||
419                      !adapter->scan_processing) &&
420                     !adapter->data_sent &&
421                     !skb_queue_empty(&adapter->tx_data_q)) {
422                         if (adapter->hs_activated_manually) {
423                                 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
424                                                   MWIFIEX_ASYNC_CMD);
425                                 adapter->hs_activated_manually = false;
426                         }
427
428                         mwifiex_process_tx_queue(adapter);
429                         if (adapter->hs_activated) {
430                                 clear_bit(MWIFIEX_IS_HS_CONFIGURED,
431                                           &adapter->work_flags);
432                                 mwifiex_hs_activated_event
433                                         (mwifiex_get_priv
434                                         (adapter, MWIFIEX_BSS_ROLE_ANY),
435                                         false);
436                         }
437                 }
438
439                 if ((adapter->scan_chan_gap_enabled ||
440                      !adapter->scan_processing) &&
441                     !adapter->data_sent &&
442                     !mwifiex_bypass_txlist_empty(adapter) &&
443                     !mwifiex_is_tdls_chan_switching
444                         (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
445                         if (adapter->hs_activated_manually) {
446                                 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
447                                                   MWIFIEX_ASYNC_CMD);
448                                 adapter->hs_activated_manually = false;
449                         }
450
451                         mwifiex_process_bypass_tx(adapter);
452                         if (adapter->hs_activated) {
453                                 clear_bit(MWIFIEX_IS_HS_CONFIGURED,
454                                           &adapter->work_flags);
455                                 mwifiex_hs_activated_event
456                                         (mwifiex_get_priv
457                                          (adapter, MWIFIEX_BSS_ROLE_ANY),
458                                          false);
459                         }
460                 }
461
462                 if ((adapter->scan_chan_gap_enabled ||
463                      !adapter->scan_processing) &&
464                     !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
465                     !mwifiex_is_tdls_chan_switching
466                         (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
467                         if (adapter->hs_activated_manually) {
468                                 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
469                                                   MWIFIEX_ASYNC_CMD);
470                                 adapter->hs_activated_manually = false;
471                         }
472
473                         mwifiex_wmm_process_tx(adapter);
474                         if (adapter->hs_activated) {
475                                 clear_bit(MWIFIEX_IS_HS_CONFIGURED,
476                                           &adapter->work_flags);
477                                 mwifiex_hs_activated_event
478                                         (mwifiex_get_priv
479                                          (adapter, MWIFIEX_BSS_ROLE_ANY),
480                                          false);
481                         }
482                 }
483
484                 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
485                     !adapter->curr_cmd && !is_command_pending(adapter) &&
486                     (mwifiex_wmm_lists_empty(adapter) &&
487                      mwifiex_bypass_txlist_empty(adapter) &&
488                      skb_queue_empty(&adapter->tx_data_q))) {
489                         if (!mwifiex_send_null_packet
490                             (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
491                              MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
492                              MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
493                                 adapter->delay_null_pkt = false;
494                                 adapter->ps_state = PS_STATE_SLEEP;
495                         }
496                         break;
497                 }
498         } while (true);
499
500         spin_lock_irqsave(&adapter->main_proc_lock, flags);
501         if (adapter->more_task_flag) {
502                 adapter->more_task_flag = false;
503                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
504                 goto process_start;
505         }
506         adapter->mwifiex_processing = false;
507         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
508
509         return ret;
510 }
511 EXPORT_SYMBOL_GPL(mwifiex_main_process);
512
513 /*
514  * This function frees the adapter structure.
515  *
516  * Additionally, this closes the netlink socket, frees the timers
517  * and private structures.
518  */
519 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
520 {
521         if (!adapter) {
522                 pr_err("%s: adapter is NULL\n", __func__);
523                 return;
524         }
525
526         mwifiex_unregister(adapter);
527         pr_debug("info: %s: free adapter\n", __func__);
528 }
529
530 /*
531  * This function cancels all works in the queue and destroys
532  * the main workqueue.
533  */
534 static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
535 {
536         if (adapter->workqueue) {
537                 destroy_workqueue(adapter->workqueue);
538                 adapter->workqueue = NULL;
539         }
540
541         if (adapter->rx_workqueue) {
542                 destroy_workqueue(adapter->rx_workqueue);
543                 adapter->rx_workqueue = NULL;
544         }
545 }
546
547 /*
548  * This function gets firmware and initializes it.
549  *
550  * The main initialization steps followed are -
551  *      - Download the correct firmware to card
552  *      - Issue the init commands to firmware
553  */
554 static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context)
555 {
556         int ret;
557         char fmt[64];
558         struct mwifiex_adapter *adapter = context;
559         struct mwifiex_fw_image fw;
560         bool init_failed = false;
561         struct wireless_dev *wdev;
562         struct completion *fw_done = adapter->fw_done;
563
564         if (!firmware) {
565                 mwifiex_dbg(adapter, ERROR,
566                             "Failed to get firmware %s\n", adapter->fw_name);
567                 goto err_dnld_fw;
568         }
569
570         memset(&fw, 0, sizeof(struct mwifiex_fw_image));
571         adapter->firmware = firmware;
572         fw.fw_buf = (u8 *) adapter->firmware->data;
573         fw.fw_len = adapter->firmware->size;
574
575         if (adapter->if_ops.dnld_fw) {
576                 ret = adapter->if_ops.dnld_fw(adapter, &fw);
577         } else {
578                 ret = mwifiex_dnld_fw(adapter, &fw);
579         }
580
581         if (ret == -1)
582                 goto err_dnld_fw;
583
584         mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
585
586         if (cal_data_cfg) {
587                 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
588                                       adapter->dev)) < 0)
589                         mwifiex_dbg(adapter, ERROR,
590                                     "Cal data request_firmware() failed\n");
591         }
592
593         /* enable host interrupt after fw dnld is successful */
594         if (adapter->if_ops.enable_int) {
595                 if (adapter->if_ops.enable_int(adapter))
596                         goto err_dnld_fw;
597         }
598
599         adapter->init_wait_q_woken = false;
600         ret = mwifiex_init_fw(adapter);
601         if (ret == -1) {
602                 goto err_init_fw;
603         } else if (!ret) {
604                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
605                 goto done;
606         }
607         /* Wait for mwifiex_init to complete */
608         if (!adapter->mfg_mode) {
609                 wait_event_interruptible(adapter->init_wait_q,
610                                          adapter->init_wait_q_woken);
611                 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
612                         goto err_init_fw;
613         }
614
615         if (!adapter->wiphy) {
616                 if (mwifiex_register_cfg80211(adapter)) {
617                         mwifiex_dbg(adapter, ERROR,
618                                     "cannot register with cfg80211\n");
619                         goto err_init_fw;
620                 }
621         }
622
623         if (mwifiex_init_channel_scan_gap(adapter)) {
624                 mwifiex_dbg(adapter, ERROR,
625                             "could not init channel stats table\n");
626                 goto err_init_chan_scan;
627         }
628
629         if (driver_mode) {
630                 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
631                 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
632         }
633
634         rtnl_lock();
635         wiphy_lock(adapter->wiphy);
636         /* Create station interface by default */
637         wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
638                                         NL80211_IFTYPE_STATION, NULL);
639         if (IS_ERR(wdev)) {
640                 mwifiex_dbg(adapter, ERROR,
641                             "cannot create default STA interface\n");
642                 wiphy_unlock(adapter->wiphy);
643                 rtnl_unlock();
644                 goto err_add_intf;
645         }
646
647         if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
648                 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
649                                                 NL80211_IFTYPE_AP, NULL);
650                 if (IS_ERR(wdev)) {
651                         mwifiex_dbg(adapter, ERROR,
652                                     "cannot create AP interface\n");
653                         wiphy_unlock(adapter->wiphy);
654                         rtnl_unlock();
655                         goto err_add_intf;
656                 }
657         }
658
659         if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
660                 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
661                                                 NL80211_IFTYPE_P2P_CLIENT, NULL);
662                 if (IS_ERR(wdev)) {
663                         mwifiex_dbg(adapter, ERROR,
664                                     "cannot create p2p client interface\n");
665                         wiphy_unlock(adapter->wiphy);
666                         rtnl_unlock();
667                         goto err_add_intf;
668                 }
669         }
670         wiphy_unlock(adapter->wiphy);
671         rtnl_unlock();
672
673         mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
674         mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
675         adapter->is_up = true;
676         goto done;
677
678 err_add_intf:
679         vfree(adapter->chan_stats);
680 err_init_chan_scan:
681         wiphy_unregister(adapter->wiphy);
682         wiphy_free(adapter->wiphy);
683 err_init_fw:
684         if (adapter->if_ops.disable_int)
685                 adapter->if_ops.disable_int(adapter);
686 err_dnld_fw:
687         mwifiex_dbg(adapter, ERROR,
688                     "info: %s: unregister device\n", __func__);
689         if (adapter->if_ops.unregister_dev)
690                 adapter->if_ops.unregister_dev(adapter);
691
692         set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
693         mwifiex_terminate_workqueue(adapter);
694
695         if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
696                 pr_debug("info: %s: shutdown mwifiex\n", __func__);
697                 mwifiex_shutdown_drv(adapter);
698                 mwifiex_free_cmd_buffers(adapter);
699         }
700
701         init_failed = true;
702 done:
703         if (adapter->cal_data) {
704                 release_firmware(adapter->cal_data);
705                 adapter->cal_data = NULL;
706         }
707         if (adapter->firmware) {
708                 release_firmware(adapter->firmware);
709                 adapter->firmware = NULL;
710         }
711         if (init_failed) {
712                 if (adapter->irq_wakeup >= 0)
713                         device_init_wakeup(adapter->dev, false);
714                 mwifiex_free_adapter(adapter);
715         }
716         /* Tell all current and future waiters we're finished */
717         complete_all(fw_done);
718
719         return init_failed ? -EIO : 0;
720 }
721
722 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
723 {
724         _mwifiex_fw_dpc(firmware, context);
725 }
726
727 /*
728  * This function gets the firmware and (if called asynchronously) kicks off the
729  * HW init when done.
730  */
731 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter,
732                               bool req_fw_nowait)
733 {
734         int ret;
735
736         /* Override default firmware with manufacturing one if
737          * manufacturing mode is enabled
738          */
739         if (mfg_mode) {
740                 if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
741                             sizeof(adapter->fw_name)) >=
742                             sizeof(adapter->fw_name)) {
743                         pr_err("%s: fw_name too long!\n", __func__);
744                         return -1;
745                 }
746         }
747
748         if (req_fw_nowait) {
749                 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
750                                               adapter->dev, GFP_KERNEL, adapter,
751                                               mwifiex_fw_dpc);
752         } else {
753                 ret = request_firmware(&adapter->firmware,
754                                        adapter->fw_name,
755                                        adapter->dev);
756         }
757
758         if (ret < 0)
759                 mwifiex_dbg(adapter, ERROR, "request_firmware%s error %d\n",
760                             req_fw_nowait ? "_nowait" : "", ret);
761         return ret;
762 }
763
764 /*
765  * CFG802.11 network device handler for open.
766  *
767  * Starts the data queue.
768  */
769 static int
770 mwifiex_open(struct net_device *dev)
771 {
772         netif_carrier_off(dev);
773
774         return 0;
775 }
776
777 /*
778  * CFG802.11 network device handler for close.
779  */
780 static int
781 mwifiex_close(struct net_device *dev)
782 {
783         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
784
785         if (priv->scan_request) {
786                 struct cfg80211_scan_info info = {
787                         .aborted = true,
788                 };
789
790                 mwifiex_dbg(priv->adapter, INFO,
791                             "aborting scan on ndo_stop\n");
792                 cfg80211_scan_done(priv->scan_request, &info);
793                 priv->scan_request = NULL;
794                 priv->scan_aborting = true;
795         }
796
797         if (priv->sched_scanning) {
798                 mwifiex_dbg(priv->adapter, INFO,
799                             "aborting bgscan on ndo_stop\n");
800                 mwifiex_stop_bg_scan(priv);
801                 cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
802         }
803
804         return 0;
805 }
806
807 static bool
808 mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
809                         struct sk_buff *skb)
810 {
811         struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
812
813         if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
814             mwifiex_is_skb_mgmt_frame(skb) ||
815             (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
816              ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
817              (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
818                 mwifiex_dbg(priv->adapter, DATA,
819                             "bypass txqueue; eth type %#x, mgmt %d\n",
820                              ntohs(eth_hdr->h_proto),
821                              mwifiex_is_skb_mgmt_frame(skb));
822                 return true;
823         }
824
825         return false;
826 }
827 /*
828  * Add buffer into wmm tx queue and queue work to transmit it.
829  */
830 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
831 {
832         struct netdev_queue *txq;
833         int index = mwifiex_1d_to_wmm_queue[skb->priority];
834
835         if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
836                 txq = netdev_get_tx_queue(priv->netdev, index);
837                 if (!netif_tx_queue_stopped(txq)) {
838                         netif_tx_stop_queue(txq);
839                         mwifiex_dbg(priv->adapter, DATA,
840                                     "stop queue: %d\n", index);
841                 }
842         }
843
844         if (mwifiex_bypass_tx_queue(priv, skb)) {
845                 atomic_inc(&priv->adapter->tx_pending);
846                 atomic_inc(&priv->adapter->bypass_tx_pending);
847                 mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
848          } else {
849                 atomic_inc(&priv->adapter->tx_pending);
850                 mwifiex_wmm_add_buf_txqueue(priv, skb);
851          }
852
853         mwifiex_queue_main_work(priv->adapter);
854
855         return 0;
856 }
857
858 struct sk_buff *
859 mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
860                                 struct sk_buff *skb, u8 flag, u64 *cookie)
861 {
862         struct sk_buff *orig_skb = skb;
863         struct mwifiex_txinfo *tx_info, *orig_tx_info;
864
865         skb = skb_clone(skb, GFP_ATOMIC);
866         if (skb) {
867                 int id;
868
869                 spin_lock_bh(&priv->ack_status_lock);
870                 id = idr_alloc(&priv->ack_status_frames, orig_skb,
871                                1, 0x10, GFP_ATOMIC);
872                 spin_unlock_bh(&priv->ack_status_lock);
873
874                 if (id >= 0) {
875                         tx_info = MWIFIEX_SKB_TXCB(skb);
876                         tx_info->ack_frame_id = id;
877                         tx_info->flags |= flag;
878                         orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
879                         orig_tx_info->ack_frame_id = id;
880                         orig_tx_info->flags |= flag;
881
882                         if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
883                                 orig_tx_info->cookie = *cookie;
884
885                 } else if (skb_shared(skb)) {
886                         kfree_skb(orig_skb);
887                 } else {
888                         kfree_skb(skb);
889                         skb = orig_skb;
890                 }
891         } else {
892                 /* couldn't clone -- lose tx status ... */
893                 skb = orig_skb;
894         }
895
896         return skb;
897 }
898
899 /*
900  * CFG802.11 network device handler for data transmission.
901  */
902 static netdev_tx_t
903 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
904 {
905         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
906         struct sk_buff *new_skb;
907         struct mwifiex_txinfo *tx_info;
908         bool multicast;
909
910         mwifiex_dbg(priv->adapter, DATA,
911                     "data: %lu BSS(%d-%d): Data <= kernel\n",
912                     jiffies, priv->bss_type, priv->bss_num);
913
914         if (test_bit(MWIFIEX_SURPRISE_REMOVED, &priv->adapter->work_flags)) {
915                 kfree_skb(skb);
916                 priv->stats.tx_dropped++;
917                 return 0;
918         }
919         if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
920                 mwifiex_dbg(priv->adapter, ERROR,
921                             "Tx: bad skb len %d\n", skb->len);
922                 kfree_skb(skb);
923                 priv->stats.tx_dropped++;
924                 return 0;
925         }
926         if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
927                 mwifiex_dbg(priv->adapter, DATA,
928                             "data: Tx: insufficient skb headroom %d\n",
929                             skb_headroom(skb));
930                 /* Insufficient skb headroom - allocate a new skb */
931                 new_skb =
932                         skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
933                 if (unlikely(!new_skb)) {
934                         mwifiex_dbg(priv->adapter, ERROR,
935                                     "Tx: cannot alloca new_skb\n");
936                         kfree_skb(skb);
937                         priv->stats.tx_dropped++;
938                         return 0;
939                 }
940                 kfree_skb(skb);
941                 skb = new_skb;
942                 mwifiex_dbg(priv->adapter, INFO,
943                             "info: new skb headroomd %d\n",
944                             skb_headroom(skb));
945         }
946
947         tx_info = MWIFIEX_SKB_TXCB(skb);
948         memset(tx_info, 0, sizeof(*tx_info));
949         tx_info->bss_num = priv->bss_num;
950         tx_info->bss_type = priv->bss_type;
951         tx_info->pkt_len = skb->len;
952
953         multicast = is_multicast_ether_addr(skb->data);
954
955         if (unlikely(!multicast && skb->sk &&
956                      skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
957                      priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
958                 skb = mwifiex_clone_skb_for_tx_status(priv,
959                                                       skb,
960                                         MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
961
962         /* Record the current time the packet was queued; used to
963          * determine the amount of time the packet was queued in
964          * the driver before it was sent to the firmware.
965          * The delay is then sent along with the packet to the
966          * firmware for aggregate delay calculation for stats and
967          * MSDU lifetime expiry.
968          */
969         __net_timestamp(skb);
970
971         if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
972             priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
973             !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
974                 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
975                         mwifiex_tdls_check_tx(priv, skb);
976         }
977
978         mwifiex_queue_tx_pkt(priv, skb);
979
980         return 0;
981 }
982
983 int mwifiex_set_mac_address(struct mwifiex_private *priv,
984                             struct net_device *dev, bool external,
985                             u8 *new_mac)
986 {
987         int ret;
988         u64 mac_addr, old_mac_addr;
989
990         old_mac_addr = ether_addr_to_u64(priv->curr_addr);
991
992         if (external) {
993                 mac_addr = ether_addr_to_u64(new_mac);
994         } else {
995                 /* Internal mac address change */
996                 if (priv->bss_type == MWIFIEX_BSS_TYPE_ANY)
997                         return -EOPNOTSUPP;
998
999                 mac_addr = old_mac_addr;
1000
1001                 if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P) {
1002                         mac_addr |= BIT_ULL(MWIFIEX_MAC_LOCAL_ADMIN_BIT);
1003                         mac_addr += priv->bss_num;
1004                 } else if (priv->adapter->priv[0] != priv) {
1005                         /* Set mac address based on bss_type/bss_num */
1006                         mac_addr ^= BIT_ULL(priv->bss_type + 8);
1007                         mac_addr += priv->bss_num;
1008                 }
1009         }
1010
1011         u64_to_ether_addr(mac_addr, priv->curr_addr);
1012
1013         /* Send request to firmware */
1014         ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
1015                                HostCmd_ACT_GEN_SET, 0, NULL, true);
1016
1017         if (ret) {
1018                 u64_to_ether_addr(old_mac_addr, priv->curr_addr);
1019                 mwifiex_dbg(priv->adapter, ERROR,
1020                             "set mac address failed: ret=%d\n", ret);
1021                 return ret;
1022         }
1023
1024         eth_hw_addr_set(dev, priv->curr_addr);
1025         return 0;
1026 }
1027
1028 /* CFG802.11 network device handler for setting MAC address.
1029  */
1030 static int
1031 mwifiex_ndo_set_mac_address(struct net_device *dev, void *addr)
1032 {
1033         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1034         struct sockaddr *hw_addr = addr;
1035
1036         return mwifiex_set_mac_address(priv, dev, true, hw_addr->sa_data);
1037 }
1038
1039 /*
1040  * CFG802.11 network device handler for setting multicast list.
1041  */
1042 static void mwifiex_set_multicast_list(struct net_device *dev)
1043 {
1044         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1045         struct mwifiex_multicast_list mcast_list;
1046
1047         if (dev->flags & IFF_PROMISC) {
1048                 mcast_list.mode = MWIFIEX_PROMISC_MODE;
1049         } else if (dev->flags & IFF_ALLMULTI ||
1050                    netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
1051                 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
1052         } else {
1053                 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
1054                 mcast_list.num_multicast_addr =
1055                         mwifiex_copy_mcast_addr(&mcast_list, dev);
1056         }
1057         mwifiex_request_set_multicast_list(priv, &mcast_list);
1058 }
1059
1060 /*
1061  * CFG802.11 network device handler for transmission timeout.
1062  */
1063 static void
1064 mwifiex_tx_timeout(struct net_device *dev, unsigned int txqueue)
1065 {
1066         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1067
1068         priv->num_tx_timeout++;
1069         priv->tx_timeout_cnt++;
1070         mwifiex_dbg(priv->adapter, ERROR,
1071                     "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
1072                     jiffies, priv->tx_timeout_cnt, priv->bss_type,
1073                     priv->bss_num);
1074         mwifiex_set_trans_start(dev);
1075
1076         if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
1077             priv->adapter->if_ops.card_reset) {
1078                 mwifiex_dbg(priv->adapter, ERROR,
1079                             "tx_timeout_cnt exceeds threshold.\t"
1080                             "Triggering card reset!\n");
1081                 priv->adapter->if_ops.card_reset(priv->adapter);
1082         }
1083 }
1084
1085 void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
1086 {
1087         struct usb_card_rec *card = adapter->card;
1088         struct mwifiex_private *priv;
1089         u16 tx_buf_size;
1090         int i, ret;
1091
1092         card->mc_resync_flag = true;
1093         for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1094                 if (atomic_read(&card->port[i].tx_data_urb_pending)) {
1095                         mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
1096                         return;
1097                 }
1098         }
1099
1100         card->mc_resync_flag = false;
1101         tx_buf_size = 0xffff;
1102         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1103         ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
1104                                HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
1105         if (ret)
1106                 mwifiex_dbg(adapter, ERROR,
1107                             "send reconfig tx buf size cmd err\n");
1108 }
1109 EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
1110
1111 void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
1112 {
1113         /* Dump all the memory data into single file, a userspace script will
1114          * be used to split all the memory data to multiple files
1115          */
1116         mwifiex_dbg(adapter, MSG,
1117                     "== mwifiex dump information to /sys/class/devcoredump start\n");
1118         dev_coredumpv(adapter->dev, adapter->devdump_data, adapter->devdump_len);
1119         mwifiex_dbg(adapter, MSG,
1120                     "== mwifiex dump information to /sys/class/devcoredump end\n");
1121
1122         /* Device dump data will be freed in device coredump release function
1123          * after 5 min. Here reset adapter->devdump_data and ->devdump_len
1124          * to avoid it been accidentally reused.
1125          */
1126         adapter->devdump_data = NULL;
1127         adapter->devdump_len = 0;
1128 }
1129 EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1130
1131 void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
1132 {
1133         char *p;
1134         char drv_version[64];
1135         struct usb_card_rec *cardp;
1136         struct sdio_mmc_card *sdio_card;
1137         struct mwifiex_private *priv;
1138         int i, idx;
1139         struct netdev_queue *txq;
1140         struct mwifiex_debug_info *debug_info;
1141
1142         mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
1143
1144         p = adapter->devdump_data;
1145         strcpy(p, "========Start dump driverinfo========\n");
1146         p += strlen("========Start dump driverinfo========\n");
1147         p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1148
1149         mwifiex_drv_get_driver_version(adapter, drv_version,
1150                                        sizeof(drv_version) - 1);
1151         p += sprintf(p, "driver_version = %s\n", drv_version);
1152
1153         if (adapter->iface_type == MWIFIEX_USB) {
1154                 cardp = (struct usb_card_rec *)adapter->card;
1155                 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1156                              atomic_read(&cardp->tx_cmd_urb_pending));
1157                 p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1158                              atomic_read(&cardp->port[0].tx_data_urb_pending));
1159                 p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1160                              atomic_read(&cardp->port[1].tx_data_urb_pending));
1161                 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1162                              atomic_read(&cardp->rx_cmd_urb_pending));
1163                 p += sprintf(p, "rx_data_urb_pending = %d\n",
1164                              atomic_read(&cardp->rx_data_urb_pending));
1165         }
1166
1167         p += sprintf(p, "tx_pending = %d\n",
1168                      atomic_read(&adapter->tx_pending));
1169         p += sprintf(p, "rx_pending = %d\n",
1170                      atomic_read(&adapter->rx_pending));
1171
1172         if (adapter->iface_type == MWIFIEX_SDIO) {
1173                 sdio_card = (struct sdio_mmc_card *)adapter->card;
1174                 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1175                              sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1176                 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1177                              sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1178         }
1179
1180         for (i = 0; i < adapter->priv_num; i++) {
1181                 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1182                         continue;
1183                 priv = adapter->priv[i];
1184                 p += sprintf(p, "\n[interface  : \"%s\"]\n",
1185                              priv->netdev->name);
1186                 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1187                              atomic_read(&priv->wmm_tx_pending[0]));
1188                 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1189                              atomic_read(&priv->wmm_tx_pending[1]));
1190                 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1191                              atomic_read(&priv->wmm_tx_pending[2]));
1192                 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1193                              atomic_read(&priv->wmm_tx_pending[3]));
1194                 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1195                              "Disconnected" : "Connected");
1196                 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1197                              ? "on" : "off"));
1198                 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1199                         txq = netdev_get_tx_queue(priv->netdev, idx);
1200                         p += sprintf(p, "tx queue %d:%s  ", idx,
1201                                      netif_tx_queue_stopped(txq) ?
1202                                      "stopped" : "started");
1203                 }
1204                 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1205                              priv->netdev->name, priv->num_tx_timeout);
1206         }
1207
1208         if (adapter->iface_type == MWIFIEX_SDIO ||
1209             adapter->iface_type == MWIFIEX_PCIE) {
1210                 p += sprintf(p, "\n=== %s register dump===\n",
1211                              adapter->iface_type == MWIFIEX_SDIO ?
1212                                                         "SDIO" : "PCIE");
1213                 if (adapter->if_ops.reg_dump)
1214                         p += adapter->if_ops.reg_dump(adapter, p);
1215         }
1216         p += sprintf(p, "\n=== more debug information\n");
1217         debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1218         if (debug_info) {
1219                 for (i = 0; i < adapter->priv_num; i++) {
1220                         if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1221                                 continue;
1222                         priv = adapter->priv[i];
1223                         mwifiex_get_debug_info(priv, debug_info);
1224                         p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1225                         break;
1226                 }
1227                 kfree(debug_info);
1228         }
1229
1230         strcpy(p, "\n========End dump========\n");
1231         p += strlen("\n========End dump========\n");
1232         mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
1233         adapter->devdump_len = p - (char *)adapter->devdump_data;
1234 }
1235 EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
1236
1237 void mwifiex_prepare_fw_dump_info(struct mwifiex_adapter *adapter)
1238 {
1239         u8 idx;
1240         char *fw_dump_ptr;
1241         u32 dump_len = 0;
1242
1243         for (idx = 0; idx < adapter->num_mem_types; idx++) {
1244                 struct memory_type_mapping *entry =
1245                                 &adapter->mem_type_mapping_tbl[idx];
1246
1247                 if (entry->mem_ptr) {
1248                         dump_len += (strlen("========Start dump ") +
1249                                         strlen(entry->mem_name) +
1250                                         strlen("========\n") +
1251                                         (entry->mem_size + 1) +
1252                                         strlen("\n========End dump========\n"));
1253                 }
1254         }
1255
1256         if (dump_len + 1 + adapter->devdump_len > MWIFIEX_FW_DUMP_SIZE) {
1257                 /* Realloc in case buffer overflow */
1258                 fw_dump_ptr = vzalloc(dump_len + 1 + adapter->devdump_len);
1259                 mwifiex_dbg(adapter, MSG, "Realloc device dump data.\n");
1260                 if (!fw_dump_ptr) {
1261                         vfree(adapter->devdump_data);
1262                         mwifiex_dbg(adapter, ERROR,
1263                                     "vzalloc devdump data failure!\n");
1264                         return;
1265                 }
1266
1267                 memmove(fw_dump_ptr, adapter->devdump_data,
1268                         adapter->devdump_len);
1269                 vfree(adapter->devdump_data);
1270                 adapter->devdump_data = fw_dump_ptr;
1271         }
1272
1273         fw_dump_ptr = (char *)adapter->devdump_data + adapter->devdump_len;
1274
1275         for (idx = 0; idx < adapter->num_mem_types; idx++) {
1276                 struct memory_type_mapping *entry =
1277                                         &adapter->mem_type_mapping_tbl[idx];
1278
1279                 if (entry->mem_ptr) {
1280                         strcpy(fw_dump_ptr, "========Start dump ");
1281                         fw_dump_ptr += strlen("========Start dump ");
1282
1283                         strcpy(fw_dump_ptr, entry->mem_name);
1284                         fw_dump_ptr += strlen(entry->mem_name);
1285
1286                         strcpy(fw_dump_ptr, "========\n");
1287                         fw_dump_ptr += strlen("========\n");
1288
1289                         memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1290                         fw_dump_ptr += entry->mem_size;
1291
1292                         strcpy(fw_dump_ptr, "\n========End dump========\n");
1293                         fw_dump_ptr += strlen("\n========End dump========\n");
1294                 }
1295         }
1296
1297         adapter->devdump_len = fw_dump_ptr - (char *)adapter->devdump_data;
1298
1299         for (idx = 0; idx < adapter->num_mem_types; idx++) {
1300                 struct memory_type_mapping *entry =
1301                         &adapter->mem_type_mapping_tbl[idx];
1302
1303                 vfree(entry->mem_ptr);
1304                 entry->mem_ptr = NULL;
1305                 entry->mem_size = 0;
1306         }
1307 }
1308 EXPORT_SYMBOL_GPL(mwifiex_prepare_fw_dump_info);
1309
1310 /*
1311  * CFG802.11 network device handler for statistics retrieval.
1312  */
1313 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1314 {
1315         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1316
1317         return &priv->stats;
1318 }
1319
1320 static u16
1321 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
1322                                 struct net_device *sb_dev)
1323 {
1324         skb->priority = cfg80211_classify8021d(skb, NULL);
1325         return mwifiex_1d_to_wmm_queue[skb->priority];
1326 }
1327
1328 /* Network device handlers */
1329 static const struct net_device_ops mwifiex_netdev_ops = {
1330         .ndo_open = mwifiex_open,
1331         .ndo_stop = mwifiex_close,
1332         .ndo_start_xmit = mwifiex_hard_start_xmit,
1333         .ndo_set_mac_address = mwifiex_ndo_set_mac_address,
1334         .ndo_validate_addr = eth_validate_addr,
1335         .ndo_tx_timeout = mwifiex_tx_timeout,
1336         .ndo_get_stats = mwifiex_get_stats,
1337         .ndo_set_rx_mode = mwifiex_set_multicast_list,
1338         .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
1339 };
1340
1341 /*
1342  * This function initializes the private structure parameters.
1343  *
1344  * The following wait queues are initialized -
1345  *      - IOCTL wait queue
1346  *      - Command wait queue
1347  *      - Statistics wait queue
1348  *
1349  * ...and the following default parameters are set -
1350  *      - Current key index     : Set to 0
1351  *      - Rate index            : Set to auto
1352  *      - Media connected       : Set to disconnected
1353  *      - Adhoc link sensed     : Set to false
1354  *      - Nick name             : Set to null
1355  *      - Number of Tx timeout  : Set to 0
1356  *      - Device address        : Set to current address
1357  *      - Rx histogram statistc : Set to 0
1358  *
1359  * In addition, the CFG80211 work queue is also created.
1360  */
1361 void mwifiex_init_priv_params(struct mwifiex_private *priv,
1362                               struct net_device *dev)
1363 {
1364         dev->netdev_ops = &mwifiex_netdev_ops;
1365         dev->needs_free_netdev = true;
1366         /* Initialize private structure */
1367         priv->current_key_index = 0;
1368         priv->media_connected = false;
1369         memset(priv->mgmt_ie, 0,
1370                sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
1371         priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1372         priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1373         priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
1374         priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
1375         priv->num_tx_timeout = 0;
1376         if (is_valid_ether_addr(dev->dev_addr))
1377                 ether_addr_copy(priv->curr_addr, dev->dev_addr);
1378         else
1379                 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
1380
1381         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1382             GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1383                 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1384                 if (priv->hist_data)
1385                         mwifiex_hist_data_reset(priv);
1386         }
1387 }
1388
1389 /*
1390  * This function check if command is pending.
1391  */
1392 int is_command_pending(struct mwifiex_adapter *adapter)
1393 {
1394         int is_cmd_pend_q_empty;
1395
1396         spin_lock_bh(&adapter->cmd_pending_q_lock);
1397         is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1398         spin_unlock_bh(&adapter->cmd_pending_q_lock);
1399
1400         return !is_cmd_pend_q_empty;
1401 }
1402
1403 /*
1404  * This is the RX work queue function.
1405  *
1406  * It handles the RX operations.
1407  */
1408 static void mwifiex_rx_work_queue(struct work_struct *work)
1409 {
1410         struct mwifiex_adapter *adapter =
1411                 container_of(work, struct mwifiex_adapter, rx_work);
1412
1413         if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1414                 return;
1415         mwifiex_process_rx(adapter);
1416 }
1417
1418 /*
1419  * This is the main work queue function.
1420  *
1421  * It handles the main process, which in turn handles the complete
1422  * driver operations.
1423  */
1424 static void mwifiex_main_work_queue(struct work_struct *work)
1425 {
1426         struct mwifiex_adapter *adapter =
1427                 container_of(work, struct mwifiex_adapter, main_work);
1428
1429         if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1430                 return;
1431         mwifiex_main_process(adapter);
1432 }
1433
1434 /* Common teardown code used for both device removal and reset */
1435 static void mwifiex_uninit_sw(struct mwifiex_adapter *adapter)
1436 {
1437         struct mwifiex_private *priv;
1438         int i;
1439
1440         /* We can no longer handle interrupts once we start doing the teardown
1441          * below.
1442          */
1443         if (adapter->if_ops.disable_int)
1444                 adapter->if_ops.disable_int(adapter);
1445
1446         set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1447         mwifiex_terminate_workqueue(adapter);
1448         adapter->int_status = 0;
1449
1450         /* Stop data */
1451         for (i = 0; i < adapter->priv_num; i++) {
1452                 priv = adapter->priv[i];
1453                 if (priv && priv->netdev) {
1454                         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1455                         if (netif_carrier_ok(priv->netdev))
1456                                 netif_carrier_off(priv->netdev);
1457                         netif_device_detach(priv->netdev);
1458                 }
1459         }
1460
1461         mwifiex_dbg(adapter, CMD, "cmd: calling mwifiex_shutdown_drv...\n");
1462         mwifiex_shutdown_drv(adapter);
1463         mwifiex_dbg(adapter, CMD, "cmd: mwifiex_shutdown_drv done\n");
1464
1465         if (atomic_read(&adapter->rx_pending) ||
1466             atomic_read(&adapter->tx_pending) ||
1467             atomic_read(&adapter->cmd_pending)) {
1468                 mwifiex_dbg(adapter, ERROR,
1469                             "rx_pending=%d, tx_pending=%d,\t"
1470                             "cmd_pending=%d\n",
1471                             atomic_read(&adapter->rx_pending),
1472                             atomic_read(&adapter->tx_pending),
1473                             atomic_read(&adapter->cmd_pending));
1474         }
1475
1476         for (i = 0; i < adapter->priv_num; i++) {
1477                 priv = adapter->priv[i];
1478                 if (!priv)
1479                         continue;
1480                 rtnl_lock();
1481                 if (priv->netdev &&
1482                     priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
1483                         /*
1484                          * Close the netdev now, because if we do it later, the
1485                          * netdev notifiers will need to acquire the wiphy lock
1486                          * again --> deadlock.
1487                          */
1488                         dev_close(priv->wdev.netdev);
1489                         wiphy_lock(adapter->wiphy);
1490                         mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
1491                         wiphy_unlock(adapter->wiphy);
1492                 }
1493                 rtnl_unlock();
1494         }
1495
1496         wiphy_unregister(adapter->wiphy);
1497         wiphy_free(adapter->wiphy);
1498         adapter->wiphy = NULL;
1499
1500         vfree(adapter->chan_stats);
1501         mwifiex_free_cmd_buffers(adapter);
1502 }
1503
1504 /*
1505  * This function can be used for shutting down the adapter SW.
1506  */
1507 int mwifiex_shutdown_sw(struct mwifiex_adapter *adapter)
1508 {
1509         struct mwifiex_private *priv;
1510
1511         if (!adapter)
1512                 return 0;
1513
1514         wait_for_completion(adapter->fw_done);
1515         /* Caller should ensure we aren't suspending while this happens */
1516         reinit_completion(adapter->fw_done);
1517
1518         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1519         mwifiex_deauthenticate(priv, NULL);
1520
1521         mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
1522
1523         mwifiex_uninit_sw(adapter);
1524         adapter->is_up = false;
1525
1526         if (adapter->if_ops.down_dev)
1527                 adapter->if_ops.down_dev(adapter);
1528
1529         return 0;
1530 }
1531 EXPORT_SYMBOL_GPL(mwifiex_shutdown_sw);
1532
1533 /* This function can be used for reinitting the adapter SW. Required
1534  * code is extracted from mwifiex_add_card()
1535  */
1536 int
1537 mwifiex_reinit_sw(struct mwifiex_adapter *adapter)
1538 {
1539         int ret;
1540
1541         mwifiex_init_lock_list(adapter);
1542         if (adapter->if_ops.up_dev)
1543                 adapter->if_ops.up_dev(adapter);
1544
1545         adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1546         clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1547         init_waitqueue_head(&adapter->init_wait_q);
1548         clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1549         adapter->hs_activated = false;
1550         clear_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags);
1551         init_waitqueue_head(&adapter->hs_activate_wait_q);
1552         init_waitqueue_head(&adapter->cmd_wait_q.wait);
1553         adapter->cmd_wait_q.status = 0;
1554         adapter->scan_wait_q_woken = false;
1555
1556         if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1557                 adapter->rx_work_enabled = true;
1558
1559         adapter->workqueue =
1560                 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1561                                 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1562         if (!adapter->workqueue)
1563                 goto err_kmalloc;
1564
1565         INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1566
1567         if (adapter->rx_work_enabled) {
1568                 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1569                                                         WQ_HIGHPRI |
1570                                                         WQ_MEM_RECLAIM |
1571                                                         WQ_UNBOUND, 1);
1572                 if (!adapter->rx_workqueue)
1573                         goto err_kmalloc;
1574                 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1575         }
1576
1577         /* Register the device. Fill up the private data structure with
1578          * relevant information from the card. Some code extracted from
1579          * mwifiex_register_dev()
1580          */
1581         mwifiex_dbg(adapter, INFO, "%s, mwifiex_init_hw_fw()...\n", __func__);
1582
1583         if (mwifiex_init_hw_fw(adapter, false)) {
1584                 mwifiex_dbg(adapter, ERROR,
1585                             "%s: firmware init failed\n", __func__);
1586                 goto err_init_fw;
1587         }
1588
1589         /* _mwifiex_fw_dpc() does its own cleanup */
1590         ret = _mwifiex_fw_dpc(adapter->firmware, adapter);
1591         if (ret) {
1592                 pr_err("Failed to bring up adapter: %d\n", ret);
1593                 return ret;
1594         }
1595         mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
1596
1597         return 0;
1598
1599 err_init_fw:
1600         mwifiex_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
1601         if (adapter->if_ops.unregister_dev)
1602                 adapter->if_ops.unregister_dev(adapter);
1603
1604 err_kmalloc:
1605         set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1606         mwifiex_terminate_workqueue(adapter);
1607         if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1608                 mwifiex_dbg(adapter, ERROR,
1609                             "info: %s: shutdown mwifiex\n", __func__);
1610                 mwifiex_shutdown_drv(adapter);
1611                 mwifiex_free_cmd_buffers(adapter);
1612         }
1613
1614         complete_all(adapter->fw_done);
1615         mwifiex_dbg(adapter, INFO, "%s, error\n", __func__);
1616
1617         return -1;
1618 }
1619 EXPORT_SYMBOL_GPL(mwifiex_reinit_sw);
1620
1621 static irqreturn_t mwifiex_irq_wakeup_handler(int irq, void *priv)
1622 {
1623         struct mwifiex_adapter *adapter = priv;
1624
1625         dev_dbg(adapter->dev, "%s: wake by wifi", __func__);
1626         adapter->wake_by_wifi = true;
1627         disable_irq_nosync(irq);
1628
1629         /* Notify PM core we are wakeup source */
1630         pm_wakeup_event(adapter->dev, 0);
1631         pm_system_wakeup();
1632
1633         return IRQ_HANDLED;
1634 }
1635
1636 static void mwifiex_probe_of(struct mwifiex_adapter *adapter)
1637 {
1638         int ret;
1639         struct device *dev = adapter->dev;
1640
1641         if (!dev->of_node)
1642                 goto err_exit;
1643
1644         adapter->dt_node = dev->of_node;
1645         adapter->irq_wakeup = irq_of_parse_and_map(adapter->dt_node, 0);
1646         if (!adapter->irq_wakeup) {
1647                 dev_dbg(dev, "fail to parse irq_wakeup from device tree\n");
1648                 goto err_exit;
1649         }
1650
1651         ret = devm_request_irq(dev, adapter->irq_wakeup,
1652                                mwifiex_irq_wakeup_handler, IRQF_TRIGGER_LOW,
1653                                "wifi_wake", adapter);
1654         if (ret) {
1655                 dev_err(dev, "Failed to request irq_wakeup %d (%d)\n",
1656                         adapter->irq_wakeup, ret);
1657                 goto err_exit;
1658         }
1659
1660         disable_irq(adapter->irq_wakeup);
1661         if (device_init_wakeup(dev, true)) {
1662                 dev_err(dev, "fail to init wakeup for mwifiex\n");
1663                 goto err_exit;
1664         }
1665         return;
1666
1667 err_exit:
1668         adapter->irq_wakeup = -1;
1669 }
1670
1671 /*
1672  * This function adds the card.
1673  *
1674  * This function follows the following major steps to set up the device -
1675  *      - Initialize software. This includes probing the card, registering
1676  *        the interface operations table, and allocating/initializing the
1677  *        adapter structure
1678  *      - Set up the netlink socket
1679  *      - Create and start the main work queue
1680  *      - Register the device
1681  *      - Initialize firmware and hardware
1682  *      - Add logical interfaces
1683  */
1684 int
1685 mwifiex_add_card(void *card, struct completion *fw_done,
1686                  struct mwifiex_if_ops *if_ops, u8 iface_type,
1687                  struct device *dev)
1688 {
1689         struct mwifiex_adapter *adapter;
1690
1691         if (mwifiex_register(card, dev, if_ops, (void **)&adapter)) {
1692                 pr_err("%s: software init failed\n", __func__);
1693                 goto err_init_sw;
1694         }
1695
1696         mwifiex_probe_of(adapter);
1697
1698         adapter->iface_type = iface_type;
1699         adapter->fw_done = fw_done;
1700
1701         adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1702         clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1703         init_waitqueue_head(&adapter->init_wait_q);
1704         clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1705         adapter->hs_activated = false;
1706         init_waitqueue_head(&adapter->hs_activate_wait_q);
1707         init_waitqueue_head(&adapter->cmd_wait_q.wait);
1708         adapter->cmd_wait_q.status = 0;
1709         adapter->scan_wait_q_woken = false;
1710
1711         if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1712                 adapter->rx_work_enabled = true;
1713
1714         adapter->workqueue =
1715                 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1716                                 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1717         if (!adapter->workqueue)
1718                 goto err_kmalloc;
1719
1720         INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1721
1722         if (adapter->rx_work_enabled) {
1723                 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1724                                                         WQ_HIGHPRI |
1725                                                         WQ_MEM_RECLAIM |
1726                                                         WQ_UNBOUND, 1);
1727                 if (!adapter->rx_workqueue)
1728                         goto err_kmalloc;
1729
1730                 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1731         }
1732
1733         /* Register the device. Fill up the private data structure with relevant
1734            information from the card. */
1735         if (adapter->if_ops.register_dev(adapter)) {
1736                 pr_err("%s: failed to register mwifiex device\n", __func__);
1737                 goto err_registerdev;
1738         }
1739
1740         if (mwifiex_init_hw_fw(adapter, true)) {
1741                 pr_err("%s: firmware init failed\n", __func__);
1742                 goto err_init_fw;
1743         }
1744
1745         return 0;
1746
1747 err_init_fw:
1748         pr_debug("info: %s: unregister device\n", __func__);
1749         if (adapter->if_ops.unregister_dev)
1750                 adapter->if_ops.unregister_dev(adapter);
1751 err_registerdev:
1752         set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1753         mwifiex_terminate_workqueue(adapter);
1754         if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1755                 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1756                 mwifiex_shutdown_drv(adapter);
1757                 mwifiex_free_cmd_buffers(adapter);
1758         }
1759 err_kmalloc:
1760         if (adapter->irq_wakeup >= 0)
1761                 device_init_wakeup(adapter->dev, false);
1762         mwifiex_free_adapter(adapter);
1763
1764 err_init_sw:
1765
1766         return -1;
1767 }
1768 EXPORT_SYMBOL_GPL(mwifiex_add_card);
1769
1770 /*
1771  * This function removes the card.
1772  *
1773  * This function follows the following major steps to remove the device -
1774  *      - Stop data traffic
1775  *      - Shutdown firmware
1776  *      - Remove the logical interfaces
1777  *      - Terminate the work queue
1778  *      - Unregister the device
1779  *      - Free the adapter structure
1780  */
1781 int mwifiex_remove_card(struct mwifiex_adapter *adapter)
1782 {
1783         if (!adapter)
1784                 return 0;
1785
1786         if (adapter->is_up)
1787                 mwifiex_uninit_sw(adapter);
1788
1789         if (adapter->irq_wakeup >= 0)
1790                 device_init_wakeup(adapter->dev, false);
1791
1792         /* Unregister device */
1793         mwifiex_dbg(adapter, INFO,
1794                     "info: unregister device\n");
1795         if (adapter->if_ops.unregister_dev)
1796                 adapter->if_ops.unregister_dev(adapter);
1797         /* Free adapter structure */
1798         mwifiex_dbg(adapter, INFO,
1799                     "info: free adapter\n");
1800         mwifiex_free_adapter(adapter);
1801
1802         return 0;
1803 }
1804 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1805
1806 void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1807                   const char *fmt, ...)
1808 {
1809         struct va_format vaf;
1810         va_list args;
1811
1812         if (!(adapter->debug_mask & mask))
1813                 return;
1814
1815         va_start(args, fmt);
1816
1817         vaf.fmt = fmt;
1818         vaf.va = &args;
1819
1820         if (adapter->dev)
1821                 dev_info(adapter->dev, "%pV", &vaf);
1822         else
1823                 pr_info("%pV", &vaf);
1824
1825         va_end(args);
1826 }
1827 EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1828
1829 /*
1830  * This function initializes the module.
1831  *
1832  * The debug FS is also initialized if configured.
1833  */
1834 static int
1835 mwifiex_init_module(void)
1836 {
1837 #ifdef CONFIG_DEBUG_FS
1838         mwifiex_debugfs_init();
1839 #endif
1840         return 0;
1841 }
1842
1843 /*
1844  * This function cleans up the module.
1845  *
1846  * The debug FS is removed if available.
1847  */
1848 static void
1849 mwifiex_cleanup_module(void)
1850 {
1851 #ifdef CONFIG_DEBUG_FS
1852         mwifiex_debugfs_remove();
1853 #endif
1854 }
1855
1856 module_init(mwifiex_init_module);
1857 module_exit(mwifiex_cleanup_module);
1858
1859 MODULE_AUTHOR("Marvell International Ltd.");
1860 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1861 MODULE_VERSION(VERSION);
1862 MODULE_LICENSE("GPL v2");