Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * NXP Wireless LAN device driver: major functions
0004  *
0005  * Copyright 2011-2020 NXP
0006  */
0007 
0008 #include <linux/suspend.h>
0009 
0010 #include "main.h"
0011 #include "wmm.h"
0012 #include "cfg80211.h"
0013 #include "11n.h"
0014 
0015 #define VERSION "1.0"
0016 #define MFG_FIRMWARE    "mwifiex_mfg.bin"
0017 
0018 static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
0019 module_param(debug_mask, uint, 0);
0020 MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
0021 
0022 const char driver_version[] = "mwifiex " VERSION " (%s) ";
0023 static char *cal_data_cfg;
0024 module_param(cal_data_cfg, charp, 0);
0025 
0026 static unsigned short driver_mode;
0027 module_param(driver_mode, ushort, 0);
0028 MODULE_PARM_DESC(driver_mode,
0029          "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
0030 
0031 bool mfg_mode;
0032 module_param(mfg_mode, bool, 0);
0033 MODULE_PARM_DESC(mfg_mode, "manufacturing mode enable:1, disable:0");
0034 
0035 bool aggr_ctrl;
0036 module_param(aggr_ctrl, bool, 0000);
0037 MODULE_PARM_DESC(aggr_ctrl, "usb tx aggregation enable:1, disable:0");
0038 
0039 const u16 mwifiex_1d_to_wmm_queue[8] = { 1, 0, 0, 1, 2, 2, 3, 3 };
0040 
0041 /*
0042  * This function registers the device and performs all the necessary
0043  * initializations.
0044  *
0045  * The following initialization operations are performed -
0046  *      - Allocate adapter structure
0047  *      - Save interface specific operations table in adapter
0048  *      - Call interface specific initialization routine
0049  *      - Allocate private structures
0050  *      - Set default adapter structure parameters
0051  *      - Initialize locks
0052  *
0053  * In case of any errors during inittialization, this function also ensures
0054  * proper cleanup before exiting.
0055  */
0056 static int mwifiex_register(void *card, struct device *dev,
0057                 struct mwifiex_if_ops *if_ops, void **padapter)
0058 {
0059     struct mwifiex_adapter *adapter;
0060     int i;
0061 
0062     adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
0063     if (!adapter)
0064         return -ENOMEM;
0065 
0066     *padapter = adapter;
0067     adapter->dev = dev;
0068     adapter->card = card;
0069 
0070     /* Save interface specific operations in adapter */
0071     memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
0072     adapter->debug_mask = debug_mask;
0073 
0074     /* card specific initialization has been deferred until now .. */
0075     if (adapter->if_ops.init_if)
0076         if (adapter->if_ops.init_if(adapter))
0077             goto error;
0078 
0079     adapter->priv_num = 0;
0080 
0081     for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
0082         /* Allocate memory for private structure */
0083         adapter->priv[i] =
0084             kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
0085         if (!adapter->priv[i])
0086             goto error;
0087 
0088         adapter->priv[i]->adapter = adapter;
0089         adapter->priv_num++;
0090     }
0091     mwifiex_init_lock_list(adapter);
0092 
0093     timer_setup(&adapter->cmd_timer, mwifiex_cmd_timeout_func, 0);
0094 
0095     return 0;
0096 
0097 error:
0098     mwifiex_dbg(adapter, ERROR,
0099             "info: leave mwifiex_register with error\n");
0100 
0101     for (i = 0; i < adapter->priv_num; i++)
0102         kfree(adapter->priv[i]);
0103 
0104     kfree(adapter);
0105 
0106     return -1;
0107 }
0108 
0109 /*
0110  * This function unregisters the device and performs all the necessary
0111  * cleanups.
0112  *
0113  * The following cleanup operations are performed -
0114  *      - Free the timers
0115  *      - Free beacon buffers
0116  *      - Free private structures
0117  *      - Free adapter structure
0118  */
0119 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
0120 {
0121     s32 i;
0122 
0123     if (adapter->if_ops.cleanup_if)
0124         adapter->if_ops.cleanup_if(adapter);
0125 
0126     del_timer_sync(&adapter->cmd_timer);
0127 
0128     /* Free private structures */
0129     for (i = 0; i < adapter->priv_num; i++) {
0130         if (adapter->priv[i]) {
0131             mwifiex_free_curr_bcn(adapter->priv[i]);
0132             kfree(adapter->priv[i]);
0133         }
0134     }
0135 
0136     if (adapter->nd_info) {
0137         for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
0138             kfree(adapter->nd_info->matches[i]);
0139         kfree(adapter->nd_info);
0140         adapter->nd_info = NULL;
0141     }
0142 
0143     kfree(adapter->regd);
0144 
0145     kfree(adapter);
0146     return 0;
0147 }
0148 
0149 void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
0150 {
0151     unsigned long flags;
0152 
0153     spin_lock_irqsave(&adapter->main_proc_lock, flags);
0154     if (adapter->mwifiex_processing) {
0155         adapter->more_task_flag = true;
0156         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
0157     } else {
0158         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
0159         queue_work(adapter->workqueue, &adapter->main_work);
0160     }
0161 }
0162 EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
0163 
0164 static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
0165 {
0166     spin_lock_bh(&adapter->rx_proc_lock);
0167     if (adapter->rx_processing) {
0168         spin_unlock_bh(&adapter->rx_proc_lock);
0169     } else {
0170         spin_unlock_bh(&adapter->rx_proc_lock);
0171         queue_work(adapter->rx_workqueue, &adapter->rx_work);
0172     }
0173 }
0174 
0175 static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
0176 {
0177     struct sk_buff *skb;
0178     struct mwifiex_rxinfo *rx_info;
0179 
0180     spin_lock_bh(&adapter->rx_proc_lock);
0181     if (adapter->rx_processing || adapter->rx_locked) {
0182         spin_unlock_bh(&adapter->rx_proc_lock);
0183         goto exit_rx_proc;
0184     } else {
0185         adapter->rx_processing = true;
0186         spin_unlock_bh(&adapter->rx_proc_lock);
0187     }
0188 
0189     /* Check for Rx data */
0190     while ((skb = skb_dequeue(&adapter->rx_data_q))) {
0191         atomic_dec(&adapter->rx_pending);
0192         if ((adapter->delay_main_work ||
0193              adapter->iface_type == MWIFIEX_USB) &&
0194             (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
0195             if (adapter->if_ops.submit_rem_rx_urbs)
0196                 adapter->if_ops.submit_rem_rx_urbs(adapter);
0197             adapter->delay_main_work = false;
0198             mwifiex_queue_main_work(adapter);
0199         }
0200         rx_info = MWIFIEX_SKB_RXCB(skb);
0201         if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
0202             if (adapter->if_ops.deaggr_pkt)
0203                 adapter->if_ops.deaggr_pkt(adapter, skb);
0204             dev_kfree_skb_any(skb);
0205         } else {
0206             mwifiex_handle_rx_packet(adapter, skb);
0207         }
0208     }
0209     spin_lock_bh(&adapter->rx_proc_lock);
0210     adapter->rx_processing = false;
0211     spin_unlock_bh(&adapter->rx_proc_lock);
0212 
0213 exit_rx_proc:
0214     return 0;
0215 }
0216 
0217 static void maybe_quirk_fw_disable_ds(struct mwifiex_adapter *adapter)
0218 {
0219     struct mwifiex_private *priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
0220     struct mwifiex_ver_ext ver_ext;
0221 
0222     if (test_and_set_bit(MWIFIEX_IS_REQUESTING_FW_VEREXT, &adapter->work_flags))
0223         return;
0224 
0225     memset(&ver_ext, 0, sizeof(ver_ext));
0226     ver_ext.version_str_sel = 1;
0227     if (mwifiex_send_cmd(priv, HostCmd_CMD_VERSION_EXT,
0228                  HostCmd_ACT_GEN_GET, 0, &ver_ext, false)) {
0229         mwifiex_dbg(priv->adapter, MSG,
0230                 "Checking hardware revision failed.\n");
0231     }
0232 }
0233 
0234 /*
0235  * The main process.
0236  *
0237  * This function is the main procedure of the driver and handles various driver
0238  * operations. It runs in a loop and provides the core functionalities.
0239  *
0240  * The main responsibilities of this function are -
0241  *      - Ensure concurrency control
0242  *      - Handle pending interrupts and call interrupt handlers
0243  *      - Wake up the card if required
0244  *      - Handle command responses and call response handlers
0245  *      - Handle events and call event handlers
0246  *      - Execute pending commands
0247  *      - Transmit pending data packets
0248  */
0249 int mwifiex_main_process(struct mwifiex_adapter *adapter)
0250 {
0251     int ret = 0;
0252     unsigned long flags;
0253 
0254     spin_lock_irqsave(&adapter->main_proc_lock, flags);
0255 
0256     /* Check if already processing */
0257     if (adapter->mwifiex_processing || adapter->main_locked) {
0258         adapter->more_task_flag = true;
0259         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
0260         return 0;
0261     } else {
0262         adapter->mwifiex_processing = true;
0263         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
0264     }
0265 process_start:
0266     do {
0267         if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
0268             break;
0269 
0270         /* For non-USB interfaces, If we process interrupts first, it
0271          * would increase RX pending even further. Avoid this by
0272          * checking if rx_pending has crossed high threshold and
0273          * schedule rx work queue and then process interrupts.
0274          * For USB interface, there are no interrupts. We already have
0275          * HIGH_RX_PENDING check in usb.c
0276          */
0277         if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
0278             adapter->iface_type != MWIFIEX_USB) {
0279             adapter->delay_main_work = true;
0280             mwifiex_queue_rx_work(adapter);
0281             break;
0282         }
0283 
0284         /* Handle pending interrupt if any */
0285         if (adapter->int_status) {
0286             if (adapter->hs_activated)
0287                 mwifiex_process_hs_config(adapter);
0288             if (adapter->if_ops.process_int_status)
0289                 adapter->if_ops.process_int_status(adapter);
0290         }
0291 
0292         if (adapter->rx_work_enabled && adapter->data_received)
0293             mwifiex_queue_rx_work(adapter);
0294 
0295         /* Need to wake up the card ? */
0296         if ((adapter->ps_state == PS_STATE_SLEEP) &&
0297             (adapter->pm_wakeup_card_req &&
0298              !adapter->pm_wakeup_fw_try) &&
0299             (is_command_pending(adapter) ||
0300              !skb_queue_empty(&adapter->tx_data_q) ||
0301              !mwifiex_bypass_txlist_empty(adapter) ||
0302              !mwifiex_wmm_lists_empty(adapter))) {
0303             adapter->pm_wakeup_fw_try = true;
0304             mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
0305             adapter->if_ops.wakeup(adapter);
0306             continue;
0307         }
0308 
0309         if (IS_CARD_RX_RCVD(adapter)) {
0310             adapter->data_received = false;
0311             adapter->pm_wakeup_fw_try = false;
0312             del_timer(&adapter->wakeup_timer);
0313             if (adapter->ps_state == PS_STATE_SLEEP)
0314                 adapter->ps_state = PS_STATE_AWAKE;
0315         } else {
0316             /* We have tried to wakeup the card already */
0317             if (adapter->pm_wakeup_fw_try)
0318                 break;
0319             if (adapter->ps_state == PS_STATE_PRE_SLEEP)
0320                 mwifiex_check_ps_cond(adapter);
0321 
0322             if (adapter->ps_state != PS_STATE_AWAKE)
0323                 break;
0324             if (adapter->tx_lock_flag) {
0325                 if (adapter->iface_type == MWIFIEX_USB) {
0326                     if (!adapter->usb_mc_setup)
0327                         break;
0328                 } else
0329                     break;
0330             }
0331 
0332             if ((!adapter->scan_chan_gap_enabled &&
0333                  adapter->scan_processing) || adapter->data_sent ||
0334                  mwifiex_is_tdls_chan_switching
0335                  (mwifiex_get_priv(adapter,
0336                            MWIFIEX_BSS_ROLE_STA)) ||
0337                 (mwifiex_wmm_lists_empty(adapter) &&
0338                  mwifiex_bypass_txlist_empty(adapter) &&
0339                  skb_queue_empty(&adapter->tx_data_q))) {
0340                 if (adapter->cmd_sent || adapter->curr_cmd ||
0341                     !mwifiex_is_send_cmd_allowed
0342                         (mwifiex_get_priv(adapter,
0343                         MWIFIEX_BSS_ROLE_STA)) ||
0344                     (!is_command_pending(adapter)))
0345                     break;
0346             }
0347         }
0348 
0349         /* Check for event */
0350         if (adapter->event_received) {
0351             adapter->event_received = false;
0352             mwifiex_process_event(adapter);
0353         }
0354 
0355         /* Check for Cmd Resp */
0356         if (adapter->cmd_resp_received) {
0357             adapter->cmd_resp_received = false;
0358             mwifiex_process_cmdresp(adapter);
0359 
0360             /* call mwifiex back when init_fw is done */
0361             if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
0362                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
0363                 mwifiex_init_fw_complete(adapter);
0364                 maybe_quirk_fw_disable_ds(adapter);
0365             }
0366         }
0367 
0368         /* Check if we need to confirm Sleep Request
0369            received previously */
0370         if (adapter->ps_state == PS_STATE_PRE_SLEEP)
0371             mwifiex_check_ps_cond(adapter);
0372 
0373         /* * The ps_state may have been changed during processing of
0374          * Sleep Request event.
0375          */
0376         if ((adapter->ps_state == PS_STATE_SLEEP) ||
0377             (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
0378             (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
0379             continue;
0380         }
0381 
0382         if (adapter->tx_lock_flag) {
0383             if (adapter->iface_type == MWIFIEX_USB) {
0384                 if (!adapter->usb_mc_setup)
0385                     continue;
0386             } else
0387                 continue;
0388         }
0389 
0390         if (!adapter->cmd_sent && !adapter->curr_cmd &&
0391             mwifiex_is_send_cmd_allowed
0392             (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
0393             if (mwifiex_exec_next_cmd(adapter) == -1) {
0394                 ret = -1;
0395                 break;
0396             }
0397         }
0398 
0399         /** If USB Multi channel setup ongoing,
0400          *  wait for ready to tx data.
0401          */
0402         if (adapter->iface_type == MWIFIEX_USB &&
0403             adapter->usb_mc_setup)
0404             continue;
0405 
0406         if ((adapter->scan_chan_gap_enabled ||
0407              !adapter->scan_processing) &&
0408             !adapter->data_sent &&
0409             !skb_queue_empty(&adapter->tx_data_q)) {
0410             if (adapter->hs_activated_manually) {
0411                 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
0412                           MWIFIEX_ASYNC_CMD);
0413                 adapter->hs_activated_manually = false;
0414             }
0415 
0416             mwifiex_process_tx_queue(adapter);
0417             if (adapter->hs_activated) {
0418                 clear_bit(MWIFIEX_IS_HS_CONFIGURED,
0419                       &adapter->work_flags);
0420                 mwifiex_hs_activated_event
0421                     (mwifiex_get_priv
0422                     (adapter, MWIFIEX_BSS_ROLE_ANY),
0423                     false);
0424             }
0425         }
0426 
0427         if ((adapter->scan_chan_gap_enabled ||
0428              !adapter->scan_processing) &&
0429             !adapter->data_sent &&
0430             !mwifiex_bypass_txlist_empty(adapter) &&
0431             !mwifiex_is_tdls_chan_switching
0432             (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
0433             if (adapter->hs_activated_manually) {
0434                 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
0435                           MWIFIEX_ASYNC_CMD);
0436                 adapter->hs_activated_manually = false;
0437             }
0438 
0439             mwifiex_process_bypass_tx(adapter);
0440             if (adapter->hs_activated) {
0441                 clear_bit(MWIFIEX_IS_HS_CONFIGURED,
0442                       &adapter->work_flags);
0443                 mwifiex_hs_activated_event
0444                     (mwifiex_get_priv
0445                      (adapter, MWIFIEX_BSS_ROLE_ANY),
0446                      false);
0447             }
0448         }
0449 
0450         if ((adapter->scan_chan_gap_enabled ||
0451              !adapter->scan_processing) &&
0452             !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
0453             !mwifiex_is_tdls_chan_switching
0454             (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
0455             if (adapter->hs_activated_manually) {
0456                 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
0457                           MWIFIEX_ASYNC_CMD);
0458                 adapter->hs_activated_manually = false;
0459             }
0460 
0461             mwifiex_wmm_process_tx(adapter);
0462             if (adapter->hs_activated) {
0463                 clear_bit(MWIFIEX_IS_HS_CONFIGURED,
0464                       &adapter->work_flags);
0465                 mwifiex_hs_activated_event
0466                     (mwifiex_get_priv
0467                      (adapter, MWIFIEX_BSS_ROLE_ANY),
0468                      false);
0469             }
0470         }
0471 
0472         if (adapter->delay_null_pkt && !adapter->cmd_sent &&
0473             !adapter->curr_cmd && !is_command_pending(adapter) &&
0474             (mwifiex_wmm_lists_empty(adapter) &&
0475              mwifiex_bypass_txlist_empty(adapter) &&
0476              skb_queue_empty(&adapter->tx_data_q))) {
0477             if (!mwifiex_send_null_packet
0478                 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
0479                  MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
0480                  MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
0481                 adapter->delay_null_pkt = false;
0482                 adapter->ps_state = PS_STATE_SLEEP;
0483             }
0484             break;
0485         }
0486     } while (true);
0487 
0488     spin_lock_irqsave(&adapter->main_proc_lock, flags);
0489     if (adapter->more_task_flag) {
0490         adapter->more_task_flag = false;
0491         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
0492         goto process_start;
0493     }
0494     adapter->mwifiex_processing = false;
0495     spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
0496 
0497     return ret;
0498 }
0499 EXPORT_SYMBOL_GPL(mwifiex_main_process);
0500 
0501 /*
0502  * This function frees the adapter structure.
0503  *
0504  * Additionally, this closes the netlink socket, frees the timers
0505  * and private structures.
0506  */
0507 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
0508 {
0509     if (!adapter) {
0510         pr_err("%s: adapter is NULL\n", __func__);
0511         return;
0512     }
0513 
0514     mwifiex_unregister(adapter);
0515     pr_debug("info: %s: free adapter\n", __func__);
0516 }
0517 
0518 /*
0519  * This function cancels all works in the queue and destroys
0520  * the main workqueue.
0521  */
0522 static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
0523 {
0524     if (adapter->workqueue) {
0525         destroy_workqueue(adapter->workqueue);
0526         adapter->workqueue = NULL;
0527     }
0528 
0529     if (adapter->rx_workqueue) {
0530         destroy_workqueue(adapter->rx_workqueue);
0531         adapter->rx_workqueue = NULL;
0532     }
0533 }
0534 
0535 /*
0536  * This function gets firmware and initializes it.
0537  *
0538  * The main initialization steps followed are -
0539  *      - Download the correct firmware to card
0540  *      - Issue the init commands to firmware
0541  */
0542 static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context)
0543 {
0544     int ret;
0545     char fmt[64];
0546     struct mwifiex_adapter *adapter = context;
0547     struct mwifiex_fw_image fw;
0548     bool init_failed = false;
0549     struct wireless_dev *wdev;
0550     struct completion *fw_done = adapter->fw_done;
0551 
0552     if (!firmware) {
0553         mwifiex_dbg(adapter, ERROR,
0554                 "Failed to get firmware %s\n", adapter->fw_name);
0555         goto err_dnld_fw;
0556     }
0557 
0558     memset(&fw, 0, sizeof(struct mwifiex_fw_image));
0559     adapter->firmware = firmware;
0560     fw.fw_buf = (u8 *) adapter->firmware->data;
0561     fw.fw_len = adapter->firmware->size;
0562 
0563     if (adapter->if_ops.dnld_fw) {
0564         ret = adapter->if_ops.dnld_fw(adapter, &fw);
0565     } else {
0566         ret = mwifiex_dnld_fw(adapter, &fw);
0567     }
0568 
0569     if (ret == -1)
0570         goto err_dnld_fw;
0571 
0572     mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
0573 
0574     if (cal_data_cfg) {
0575         if ((request_firmware(&adapter->cal_data, cal_data_cfg,
0576                       adapter->dev)) < 0)
0577             mwifiex_dbg(adapter, ERROR,
0578                     "Cal data request_firmware() failed\n");
0579     }
0580 
0581     /* enable host interrupt after fw dnld is successful */
0582     if (adapter->if_ops.enable_int) {
0583         if (adapter->if_ops.enable_int(adapter))
0584             goto err_dnld_fw;
0585     }
0586 
0587     adapter->init_wait_q_woken = false;
0588     ret = mwifiex_init_fw(adapter);
0589     if (ret == -1) {
0590         goto err_init_fw;
0591     } else if (!ret) {
0592         adapter->hw_status = MWIFIEX_HW_STATUS_READY;
0593         goto done;
0594     }
0595     /* Wait for mwifiex_init to complete */
0596     if (!adapter->mfg_mode) {
0597         wait_event_interruptible(adapter->init_wait_q,
0598                      adapter->init_wait_q_woken);
0599         if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
0600             goto err_init_fw;
0601     }
0602 
0603     if (!adapter->wiphy) {
0604         if (mwifiex_register_cfg80211(adapter)) {
0605             mwifiex_dbg(adapter, ERROR,
0606                     "cannot register with cfg80211\n");
0607             goto err_init_fw;
0608         }
0609     }
0610 
0611     if (mwifiex_init_channel_scan_gap(adapter)) {
0612         mwifiex_dbg(adapter, ERROR,
0613                 "could not init channel stats table\n");
0614         goto err_init_chan_scan;
0615     }
0616 
0617     if (driver_mode) {
0618         driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
0619         driver_mode |= MWIFIEX_DRIVER_MODE_STA;
0620     }
0621 
0622     rtnl_lock();
0623     wiphy_lock(adapter->wiphy);
0624     /* Create station interface by default */
0625     wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
0626                     NL80211_IFTYPE_STATION, NULL);
0627     if (IS_ERR(wdev)) {
0628         mwifiex_dbg(adapter, ERROR,
0629                 "cannot create default STA interface\n");
0630         wiphy_unlock(adapter->wiphy);
0631         rtnl_unlock();
0632         goto err_add_intf;
0633     }
0634 
0635     if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
0636         wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
0637                         NL80211_IFTYPE_AP, NULL);
0638         if (IS_ERR(wdev)) {
0639             mwifiex_dbg(adapter, ERROR,
0640                     "cannot create AP interface\n");
0641             wiphy_unlock(adapter->wiphy);
0642             rtnl_unlock();
0643             goto err_add_intf;
0644         }
0645     }
0646 
0647     if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
0648         wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
0649                         NL80211_IFTYPE_P2P_CLIENT, NULL);
0650         if (IS_ERR(wdev)) {
0651             mwifiex_dbg(adapter, ERROR,
0652                     "cannot create p2p client interface\n");
0653             wiphy_unlock(adapter->wiphy);
0654             rtnl_unlock();
0655             goto err_add_intf;
0656         }
0657     }
0658     wiphy_unlock(adapter->wiphy);
0659     rtnl_unlock();
0660 
0661     mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
0662     mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
0663     adapter->is_up = true;
0664     goto done;
0665 
0666 err_add_intf:
0667     vfree(adapter->chan_stats);
0668 err_init_chan_scan:
0669     wiphy_unregister(adapter->wiphy);
0670     wiphy_free(adapter->wiphy);
0671 err_init_fw:
0672     if (adapter->if_ops.disable_int)
0673         adapter->if_ops.disable_int(adapter);
0674 err_dnld_fw:
0675     mwifiex_dbg(adapter, ERROR,
0676             "info: %s: unregister device\n", __func__);
0677     if (adapter->if_ops.unregister_dev)
0678         adapter->if_ops.unregister_dev(adapter);
0679 
0680     set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
0681     mwifiex_terminate_workqueue(adapter);
0682 
0683     if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
0684         pr_debug("info: %s: shutdown mwifiex\n", __func__);
0685         mwifiex_shutdown_drv(adapter);
0686         mwifiex_free_cmd_buffers(adapter);
0687     }
0688 
0689     init_failed = true;
0690 done:
0691     if (adapter->cal_data) {
0692         release_firmware(adapter->cal_data);
0693         adapter->cal_data = NULL;
0694     }
0695     if (adapter->firmware) {
0696         release_firmware(adapter->firmware);
0697         adapter->firmware = NULL;
0698     }
0699     if (init_failed) {
0700         if (adapter->irq_wakeup >= 0)
0701             device_init_wakeup(adapter->dev, false);
0702         mwifiex_free_adapter(adapter);
0703     }
0704     /* Tell all current and future waiters we're finished */
0705     complete_all(fw_done);
0706 
0707     return init_failed ? -EIO : 0;
0708 }
0709 
0710 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
0711 {
0712     _mwifiex_fw_dpc(firmware, context);
0713 }
0714 
0715 /*
0716  * This function gets the firmware and (if called asynchronously) kicks off the
0717  * HW init when done.
0718  */
0719 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter,
0720                   bool req_fw_nowait)
0721 {
0722     int ret;
0723 
0724     /* Override default firmware with manufacturing one if
0725      * manufacturing mode is enabled
0726      */
0727     if (mfg_mode) {
0728         if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
0729                 sizeof(adapter->fw_name)) >=
0730                 sizeof(adapter->fw_name)) {
0731             pr_err("%s: fw_name too long!\n", __func__);
0732             return -1;
0733         }
0734     }
0735 
0736     if (req_fw_nowait) {
0737         ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
0738                           adapter->dev, GFP_KERNEL, adapter,
0739                           mwifiex_fw_dpc);
0740     } else {
0741         ret = request_firmware(&adapter->firmware,
0742                        adapter->fw_name,
0743                        adapter->dev);
0744     }
0745 
0746     if (ret < 0)
0747         mwifiex_dbg(adapter, ERROR, "request_firmware%s error %d\n",
0748                 req_fw_nowait ? "_nowait" : "", ret);
0749     return ret;
0750 }
0751 
0752 /*
0753  * CFG802.11 network device handler for open.
0754  *
0755  * Starts the data queue.
0756  */
0757 static int
0758 mwifiex_open(struct net_device *dev)
0759 {
0760     netif_carrier_off(dev);
0761 
0762     return 0;
0763 }
0764 
0765 /*
0766  * CFG802.11 network device handler for close.
0767  */
0768 static int
0769 mwifiex_close(struct net_device *dev)
0770 {
0771     struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
0772 
0773     if (priv->scan_request) {
0774         struct cfg80211_scan_info info = {
0775             .aborted = true,
0776         };
0777 
0778         mwifiex_dbg(priv->adapter, INFO,
0779                 "aborting scan on ndo_stop\n");
0780         cfg80211_scan_done(priv->scan_request, &info);
0781         priv->scan_request = NULL;
0782         priv->scan_aborting = true;
0783     }
0784 
0785     if (priv->sched_scanning) {
0786         mwifiex_dbg(priv->adapter, INFO,
0787                 "aborting bgscan on ndo_stop\n");
0788         mwifiex_stop_bg_scan(priv);
0789         cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
0790     }
0791 
0792     return 0;
0793 }
0794 
0795 static bool
0796 mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
0797             struct sk_buff *skb)
0798 {
0799     struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
0800 
0801     if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
0802         mwifiex_is_skb_mgmt_frame(skb) ||
0803         (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
0804          ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
0805          (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
0806         mwifiex_dbg(priv->adapter, DATA,
0807                 "bypass txqueue; eth type %#x, mgmt %d\n",
0808                  ntohs(eth_hdr->h_proto),
0809                  mwifiex_is_skb_mgmt_frame(skb));
0810         return true;
0811     }
0812 
0813     return false;
0814 }
0815 /*
0816  * Add buffer into wmm tx queue and queue work to transmit it.
0817  */
0818 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
0819 {
0820     struct netdev_queue *txq;
0821     int index = mwifiex_1d_to_wmm_queue[skb->priority];
0822 
0823     if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
0824         txq = netdev_get_tx_queue(priv->netdev, index);
0825         if (!netif_tx_queue_stopped(txq)) {
0826             netif_tx_stop_queue(txq);
0827             mwifiex_dbg(priv->adapter, DATA,
0828                     "stop queue: %d\n", index);
0829         }
0830     }
0831 
0832     if (mwifiex_bypass_tx_queue(priv, skb)) {
0833         atomic_inc(&priv->adapter->tx_pending);
0834         atomic_inc(&priv->adapter->bypass_tx_pending);
0835         mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
0836      } else {
0837         atomic_inc(&priv->adapter->tx_pending);
0838         mwifiex_wmm_add_buf_txqueue(priv, skb);
0839      }
0840 
0841     mwifiex_queue_main_work(priv->adapter);
0842 
0843     return 0;
0844 }
0845 
0846 struct sk_buff *
0847 mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
0848                 struct sk_buff *skb, u8 flag, u64 *cookie)
0849 {
0850     struct sk_buff *orig_skb = skb;
0851     struct mwifiex_txinfo *tx_info, *orig_tx_info;
0852 
0853     skb = skb_clone(skb, GFP_ATOMIC);
0854     if (skb) {
0855         int id;
0856 
0857         spin_lock_bh(&priv->ack_status_lock);
0858         id = idr_alloc(&priv->ack_status_frames, orig_skb,
0859                    1, 0x10, GFP_ATOMIC);
0860         spin_unlock_bh(&priv->ack_status_lock);
0861 
0862         if (id >= 0) {
0863             tx_info = MWIFIEX_SKB_TXCB(skb);
0864             tx_info->ack_frame_id = id;
0865             tx_info->flags |= flag;
0866             orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
0867             orig_tx_info->ack_frame_id = id;
0868             orig_tx_info->flags |= flag;
0869 
0870             if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
0871                 orig_tx_info->cookie = *cookie;
0872 
0873         } else if (skb_shared(skb)) {
0874             kfree_skb(orig_skb);
0875         } else {
0876             kfree_skb(skb);
0877             skb = orig_skb;
0878         }
0879     } else {
0880         /* couldn't clone -- lose tx status ... */
0881         skb = orig_skb;
0882     }
0883 
0884     return skb;
0885 }
0886 
0887 /*
0888  * CFG802.11 network device handler for data transmission.
0889  */
0890 static netdev_tx_t
0891 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
0892 {
0893     struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
0894     struct sk_buff *new_skb;
0895     struct mwifiex_txinfo *tx_info;
0896     bool multicast;
0897 
0898     mwifiex_dbg(priv->adapter, DATA,
0899             "data: %lu BSS(%d-%d): Data <= kernel\n",
0900             jiffies, priv->bss_type, priv->bss_num);
0901 
0902     if (test_bit(MWIFIEX_SURPRISE_REMOVED, &priv->adapter->work_flags)) {
0903         kfree_skb(skb);
0904         priv->stats.tx_dropped++;
0905         return 0;
0906     }
0907     if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
0908         mwifiex_dbg(priv->adapter, ERROR,
0909                 "Tx: bad skb len %d\n", skb->len);
0910         kfree_skb(skb);
0911         priv->stats.tx_dropped++;
0912         return 0;
0913     }
0914     if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
0915         mwifiex_dbg(priv->adapter, DATA,
0916                 "data: Tx: insufficient skb headroom %d\n",
0917                 skb_headroom(skb));
0918         /* Insufficient skb headroom - allocate a new skb */
0919         new_skb =
0920             skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
0921         if (unlikely(!new_skb)) {
0922             mwifiex_dbg(priv->adapter, ERROR,
0923                     "Tx: cannot alloca new_skb\n");
0924             kfree_skb(skb);
0925             priv->stats.tx_dropped++;
0926             return 0;
0927         }
0928         kfree_skb(skb);
0929         skb = new_skb;
0930         mwifiex_dbg(priv->adapter, INFO,
0931                 "info: new skb headroomd %d\n",
0932                 skb_headroom(skb));
0933     }
0934 
0935     tx_info = MWIFIEX_SKB_TXCB(skb);
0936     memset(tx_info, 0, sizeof(*tx_info));
0937     tx_info->bss_num = priv->bss_num;
0938     tx_info->bss_type = priv->bss_type;
0939     tx_info->pkt_len = skb->len;
0940 
0941     multicast = is_multicast_ether_addr(skb->data);
0942 
0943     if (unlikely(!multicast && skb->sk &&
0944              skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
0945              priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
0946         skb = mwifiex_clone_skb_for_tx_status(priv,
0947                               skb,
0948                     MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
0949 
0950     /* Record the current time the packet was queued; used to
0951      * determine the amount of time the packet was queued in
0952      * the driver before it was sent to the firmware.
0953      * The delay is then sent along with the packet to the
0954      * firmware for aggregate delay calculation for stats and
0955      * MSDU lifetime expiry.
0956      */
0957     __net_timestamp(skb);
0958 
0959     if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
0960         priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
0961         !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
0962         if (priv->adapter->auto_tdls && priv->check_tdls_tx)
0963             mwifiex_tdls_check_tx(priv, skb);
0964     }
0965 
0966     mwifiex_queue_tx_pkt(priv, skb);
0967 
0968     return 0;
0969 }
0970 
0971 int mwifiex_set_mac_address(struct mwifiex_private *priv,
0972                 struct net_device *dev, bool external,
0973                 u8 *new_mac)
0974 {
0975     int ret;
0976     u64 mac_addr, old_mac_addr;
0977 
0978     old_mac_addr = ether_addr_to_u64(priv->curr_addr);
0979 
0980     if (external) {
0981         mac_addr = ether_addr_to_u64(new_mac);
0982     } else {
0983         /* Internal mac address change */
0984         if (priv->bss_type == MWIFIEX_BSS_TYPE_ANY)
0985             return -EOPNOTSUPP;
0986 
0987         mac_addr = old_mac_addr;
0988 
0989         if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P) {
0990             mac_addr |= BIT_ULL(MWIFIEX_MAC_LOCAL_ADMIN_BIT);
0991             mac_addr += priv->bss_num;
0992         } else if (priv->adapter->priv[0] != priv) {
0993             /* Set mac address based on bss_type/bss_num */
0994             mac_addr ^= BIT_ULL(priv->bss_type + 8);
0995             mac_addr += priv->bss_num;
0996         }
0997     }
0998 
0999     u64_to_ether_addr(mac_addr, priv->curr_addr);
1000 
1001     /* Send request to firmware */
1002     ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
1003                    HostCmd_ACT_GEN_SET, 0, NULL, true);
1004 
1005     if (ret) {
1006         u64_to_ether_addr(old_mac_addr, priv->curr_addr);
1007         mwifiex_dbg(priv->adapter, ERROR,
1008                 "set mac address failed: ret=%d\n", ret);
1009         return ret;
1010     }
1011 
1012     eth_hw_addr_set(dev, priv->curr_addr);
1013     return 0;
1014 }
1015 
1016 /* CFG802.11 network device handler for setting MAC address.
1017  */
1018 static int
1019 mwifiex_ndo_set_mac_address(struct net_device *dev, void *addr)
1020 {
1021     struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1022     struct sockaddr *hw_addr = addr;
1023 
1024     return mwifiex_set_mac_address(priv, dev, true, hw_addr->sa_data);
1025 }
1026 
1027 /*
1028  * CFG802.11 network device handler for setting multicast list.
1029  */
1030 static void mwifiex_set_multicast_list(struct net_device *dev)
1031 {
1032     struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1033     struct mwifiex_multicast_list mcast_list;
1034 
1035     if (dev->flags & IFF_PROMISC) {
1036         mcast_list.mode = MWIFIEX_PROMISC_MODE;
1037     } else if (dev->flags & IFF_ALLMULTI ||
1038            netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
1039         mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
1040     } else {
1041         mcast_list.mode = MWIFIEX_MULTICAST_MODE;
1042         mcast_list.num_multicast_addr =
1043             mwifiex_copy_mcast_addr(&mcast_list, dev);
1044     }
1045     mwifiex_request_set_multicast_list(priv, &mcast_list);
1046 }
1047 
1048 /*
1049  * CFG802.11 network device handler for transmission timeout.
1050  */
1051 static void
1052 mwifiex_tx_timeout(struct net_device *dev, unsigned int txqueue)
1053 {
1054     struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1055 
1056     priv->num_tx_timeout++;
1057     priv->tx_timeout_cnt++;
1058     mwifiex_dbg(priv->adapter, ERROR,
1059             "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
1060             jiffies, priv->tx_timeout_cnt, priv->bss_type,
1061             priv->bss_num);
1062     mwifiex_set_trans_start(dev);
1063 
1064     if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
1065         priv->adapter->if_ops.card_reset) {
1066         mwifiex_dbg(priv->adapter, ERROR,
1067                 "tx_timeout_cnt exceeds threshold.\t"
1068                 "Triggering card reset!\n");
1069         priv->adapter->if_ops.card_reset(priv->adapter);
1070     }
1071 }
1072 
1073 void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
1074 {
1075     struct usb_card_rec *card = adapter->card;
1076     struct mwifiex_private *priv;
1077     u16 tx_buf_size;
1078     int i, ret;
1079 
1080     card->mc_resync_flag = true;
1081     for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1082         if (atomic_read(&card->port[i].tx_data_urb_pending)) {
1083             mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
1084             return;
1085         }
1086     }
1087 
1088     card->mc_resync_flag = false;
1089     tx_buf_size = 0xffff;
1090     priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1091     ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
1092                    HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
1093     if (ret)
1094         mwifiex_dbg(adapter, ERROR,
1095                 "send reconfig tx buf size cmd err\n");
1096 }
1097 EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
1098 
1099 void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
1100 {
1101     /* Dump all the memory data into single file, a userspace script will
1102      * be used to split all the memory data to multiple files
1103      */
1104     mwifiex_dbg(adapter, MSG,
1105             "== mwifiex dump information to /sys/class/devcoredump start\n");
1106     dev_coredumpv(adapter->dev, adapter->devdump_data, adapter->devdump_len,
1107               GFP_KERNEL);
1108     mwifiex_dbg(adapter, MSG,
1109             "== mwifiex dump information to /sys/class/devcoredump end\n");
1110 
1111     /* Device dump data will be freed in device coredump release function
1112      * after 5 min. Here reset adapter->devdump_data and ->devdump_len
1113      * to avoid it been accidentally reused.
1114      */
1115     adapter->devdump_data = NULL;
1116     adapter->devdump_len = 0;
1117 }
1118 EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1119 
1120 void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
1121 {
1122     char *p;
1123     char drv_version[64];
1124     struct usb_card_rec *cardp;
1125     struct sdio_mmc_card *sdio_card;
1126     struct mwifiex_private *priv;
1127     int i, idx;
1128     struct netdev_queue *txq;
1129     struct mwifiex_debug_info *debug_info;
1130 
1131     mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
1132 
1133     p = adapter->devdump_data;
1134     strcpy(p, "========Start dump driverinfo========\n");
1135     p += strlen("========Start dump driverinfo========\n");
1136     p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1137 
1138     mwifiex_drv_get_driver_version(adapter, drv_version,
1139                        sizeof(drv_version) - 1);
1140     p += sprintf(p, "driver_version = %s\n", drv_version);
1141 
1142     if (adapter->iface_type == MWIFIEX_USB) {
1143         cardp = (struct usb_card_rec *)adapter->card;
1144         p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1145                  atomic_read(&cardp->tx_cmd_urb_pending));
1146         p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1147                  atomic_read(&cardp->port[0].tx_data_urb_pending));
1148         p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1149                  atomic_read(&cardp->port[1].tx_data_urb_pending));
1150         p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1151                  atomic_read(&cardp->rx_cmd_urb_pending));
1152         p += sprintf(p, "rx_data_urb_pending = %d\n",
1153                  atomic_read(&cardp->rx_data_urb_pending));
1154     }
1155 
1156     p += sprintf(p, "tx_pending = %d\n",
1157              atomic_read(&adapter->tx_pending));
1158     p += sprintf(p, "rx_pending = %d\n",
1159              atomic_read(&adapter->rx_pending));
1160 
1161     if (adapter->iface_type == MWIFIEX_SDIO) {
1162         sdio_card = (struct sdio_mmc_card *)adapter->card;
1163         p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1164                  sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1165         p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1166                  sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1167     }
1168 
1169     for (i = 0; i < adapter->priv_num; i++) {
1170         if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1171             continue;
1172         priv = adapter->priv[i];
1173         p += sprintf(p, "\n[interface  : \"%s\"]\n",
1174                  priv->netdev->name);
1175         p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1176                  atomic_read(&priv->wmm_tx_pending[0]));
1177         p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1178                  atomic_read(&priv->wmm_tx_pending[1]));
1179         p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1180                  atomic_read(&priv->wmm_tx_pending[2]));
1181         p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1182                  atomic_read(&priv->wmm_tx_pending[3]));
1183         p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1184                  "Disconnected" : "Connected");
1185         p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1186                  ? "on" : "off"));
1187         for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1188             txq = netdev_get_tx_queue(priv->netdev, idx);
1189             p += sprintf(p, "tx queue %d:%s  ", idx,
1190                      netif_tx_queue_stopped(txq) ?
1191                      "stopped" : "started");
1192         }
1193         p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1194                  priv->netdev->name, priv->num_tx_timeout);
1195     }
1196 
1197     if (adapter->iface_type == MWIFIEX_SDIO ||
1198         adapter->iface_type == MWIFIEX_PCIE) {
1199         p += sprintf(p, "\n=== %s register dump===\n",
1200                  adapter->iface_type == MWIFIEX_SDIO ?
1201                             "SDIO" : "PCIE");
1202         if (adapter->if_ops.reg_dump)
1203             p += adapter->if_ops.reg_dump(adapter, p);
1204     }
1205     p += sprintf(p, "\n=== more debug information\n");
1206     debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1207     if (debug_info) {
1208         for (i = 0; i < adapter->priv_num; i++) {
1209             if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1210                 continue;
1211             priv = adapter->priv[i];
1212             mwifiex_get_debug_info(priv, debug_info);
1213             p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1214             break;
1215         }
1216         kfree(debug_info);
1217     }
1218 
1219     strcpy(p, "\n========End dump========\n");
1220     p += strlen("\n========End dump========\n");
1221     mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
1222     adapter->devdump_len = p - (char *)adapter->devdump_data;
1223 }
1224 EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
1225 
1226 void mwifiex_prepare_fw_dump_info(struct mwifiex_adapter *adapter)
1227 {
1228     u8 idx;
1229     char *fw_dump_ptr;
1230     u32 dump_len = 0;
1231 
1232     for (idx = 0; idx < adapter->num_mem_types; idx++) {
1233         struct memory_type_mapping *entry =
1234                 &adapter->mem_type_mapping_tbl[idx];
1235 
1236         if (entry->mem_ptr) {
1237             dump_len += (strlen("========Start dump ") +
1238                     strlen(entry->mem_name) +
1239                     strlen("========\n") +
1240                     (entry->mem_size + 1) +
1241                     strlen("\n========End dump========\n"));
1242         }
1243     }
1244 
1245     if (dump_len + 1 + adapter->devdump_len > MWIFIEX_FW_DUMP_SIZE) {
1246         /* Realloc in case buffer overflow */
1247         fw_dump_ptr = vzalloc(dump_len + 1 + adapter->devdump_len);
1248         mwifiex_dbg(adapter, MSG, "Realloc device dump data.\n");
1249         if (!fw_dump_ptr) {
1250             vfree(adapter->devdump_data);
1251             mwifiex_dbg(adapter, ERROR,
1252                     "vzalloc devdump data failure!\n");
1253             return;
1254         }
1255 
1256         memmove(fw_dump_ptr, adapter->devdump_data,
1257             adapter->devdump_len);
1258         vfree(adapter->devdump_data);
1259         adapter->devdump_data = fw_dump_ptr;
1260     }
1261 
1262     fw_dump_ptr = (char *)adapter->devdump_data + adapter->devdump_len;
1263 
1264     for (idx = 0; idx < adapter->num_mem_types; idx++) {
1265         struct memory_type_mapping *entry =
1266                     &adapter->mem_type_mapping_tbl[idx];
1267 
1268         if (entry->mem_ptr) {
1269             strcpy(fw_dump_ptr, "========Start dump ");
1270             fw_dump_ptr += strlen("========Start dump ");
1271 
1272             strcpy(fw_dump_ptr, entry->mem_name);
1273             fw_dump_ptr += strlen(entry->mem_name);
1274 
1275             strcpy(fw_dump_ptr, "========\n");
1276             fw_dump_ptr += strlen("========\n");
1277 
1278             memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1279             fw_dump_ptr += entry->mem_size;
1280 
1281             strcpy(fw_dump_ptr, "\n========End dump========\n");
1282             fw_dump_ptr += strlen("\n========End dump========\n");
1283         }
1284     }
1285 
1286     adapter->devdump_len = fw_dump_ptr - (char *)adapter->devdump_data;
1287 
1288     for (idx = 0; idx < adapter->num_mem_types; idx++) {
1289         struct memory_type_mapping *entry =
1290             &adapter->mem_type_mapping_tbl[idx];
1291 
1292         vfree(entry->mem_ptr);
1293         entry->mem_ptr = NULL;
1294         entry->mem_size = 0;
1295     }
1296 }
1297 EXPORT_SYMBOL_GPL(mwifiex_prepare_fw_dump_info);
1298 
1299 /*
1300  * CFG802.11 network device handler for statistics retrieval.
1301  */
1302 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1303 {
1304     struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1305 
1306     return &priv->stats;
1307 }
1308 
1309 static u16
1310 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
1311                 struct net_device *sb_dev)
1312 {
1313     skb->priority = cfg80211_classify8021d(skb, NULL);
1314     return mwifiex_1d_to_wmm_queue[skb->priority];
1315 }
1316 
1317 /* Network device handlers */
1318 static const struct net_device_ops mwifiex_netdev_ops = {
1319     .ndo_open = mwifiex_open,
1320     .ndo_stop = mwifiex_close,
1321     .ndo_start_xmit = mwifiex_hard_start_xmit,
1322     .ndo_set_mac_address = mwifiex_ndo_set_mac_address,
1323     .ndo_validate_addr = eth_validate_addr,
1324     .ndo_tx_timeout = mwifiex_tx_timeout,
1325     .ndo_get_stats = mwifiex_get_stats,
1326     .ndo_set_rx_mode = mwifiex_set_multicast_list,
1327     .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
1328 };
1329 
1330 /*
1331  * This function initializes the private structure parameters.
1332  *
1333  * The following wait queues are initialized -
1334  *      - IOCTL wait queue
1335  *      - Command wait queue
1336  *      - Statistics wait queue
1337  *
1338  * ...and the following default parameters are set -
1339  *      - Current key index     : Set to 0
1340  *      - Rate index            : Set to auto
1341  *      - Media connected       : Set to disconnected
1342  *      - Adhoc link sensed     : Set to false
1343  *      - Nick name             : Set to null
1344  *      - Number of Tx timeout  : Set to 0
1345  *      - Device address        : Set to current address
1346  *      - Rx histogram statistc : Set to 0
1347  *
1348  * In addition, the CFG80211 work queue is also created.
1349  */
1350 void mwifiex_init_priv_params(struct mwifiex_private *priv,
1351                   struct net_device *dev)
1352 {
1353     dev->netdev_ops = &mwifiex_netdev_ops;
1354     dev->needs_free_netdev = true;
1355     /* Initialize private structure */
1356     priv->current_key_index = 0;
1357     priv->media_connected = false;
1358     memset(priv->mgmt_ie, 0,
1359            sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
1360     priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1361     priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1362     priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
1363     priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
1364     priv->num_tx_timeout = 0;
1365     if (is_valid_ether_addr(dev->dev_addr))
1366         ether_addr_copy(priv->curr_addr, dev->dev_addr);
1367     else
1368         ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
1369 
1370     if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1371         GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1372         priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1373         if (priv->hist_data)
1374             mwifiex_hist_data_reset(priv);
1375     }
1376 }
1377 
1378 /*
1379  * This function check if command is pending.
1380  */
1381 int is_command_pending(struct mwifiex_adapter *adapter)
1382 {
1383     int is_cmd_pend_q_empty;
1384 
1385     spin_lock_bh(&adapter->cmd_pending_q_lock);
1386     is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1387     spin_unlock_bh(&adapter->cmd_pending_q_lock);
1388 
1389     return !is_cmd_pend_q_empty;
1390 }
1391 
1392 /*
1393  * This is the RX work queue function.
1394  *
1395  * It handles the RX operations.
1396  */
1397 static void mwifiex_rx_work_queue(struct work_struct *work)
1398 {
1399     struct mwifiex_adapter *adapter =
1400         container_of(work, struct mwifiex_adapter, rx_work);
1401 
1402     if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1403         return;
1404     mwifiex_process_rx(adapter);
1405 }
1406 
1407 /*
1408  * This is the main work queue function.
1409  *
1410  * It handles the main process, which in turn handles the complete
1411  * driver operations.
1412  */
1413 static void mwifiex_main_work_queue(struct work_struct *work)
1414 {
1415     struct mwifiex_adapter *adapter =
1416         container_of(work, struct mwifiex_adapter, main_work);
1417 
1418     if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1419         return;
1420     mwifiex_main_process(adapter);
1421 }
1422 
1423 /* Common teardown code used for both device removal and reset */
1424 static void mwifiex_uninit_sw(struct mwifiex_adapter *adapter)
1425 {
1426     struct mwifiex_private *priv;
1427     int i;
1428 
1429     /* We can no longer handle interrupts once we start doing the teardown
1430      * below.
1431      */
1432     if (adapter->if_ops.disable_int)
1433         adapter->if_ops.disable_int(adapter);
1434 
1435     set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1436     mwifiex_terminate_workqueue(adapter);
1437     adapter->int_status = 0;
1438 
1439     /* Stop data */
1440     for (i = 0; i < adapter->priv_num; i++) {
1441         priv = adapter->priv[i];
1442         if (priv && priv->netdev) {
1443             mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1444             if (netif_carrier_ok(priv->netdev))
1445                 netif_carrier_off(priv->netdev);
1446             netif_device_detach(priv->netdev);
1447         }
1448     }
1449 
1450     mwifiex_dbg(adapter, CMD, "cmd: calling mwifiex_shutdown_drv...\n");
1451     mwifiex_shutdown_drv(adapter);
1452     mwifiex_dbg(adapter, CMD, "cmd: mwifiex_shutdown_drv done\n");
1453 
1454     if (atomic_read(&adapter->rx_pending) ||
1455         atomic_read(&adapter->tx_pending) ||
1456         atomic_read(&adapter->cmd_pending)) {
1457         mwifiex_dbg(adapter, ERROR,
1458                 "rx_pending=%d, tx_pending=%d,\t"
1459                 "cmd_pending=%d\n",
1460                 atomic_read(&adapter->rx_pending),
1461                 atomic_read(&adapter->tx_pending),
1462                 atomic_read(&adapter->cmd_pending));
1463     }
1464 
1465     for (i = 0; i < adapter->priv_num; i++) {
1466         priv = adapter->priv[i];
1467         if (!priv)
1468             continue;
1469         rtnl_lock();
1470         if (priv->netdev &&
1471             priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
1472             /*
1473              * Close the netdev now, because if we do it later, the
1474              * netdev notifiers will need to acquire the wiphy lock
1475              * again --> deadlock.
1476              */
1477             dev_close(priv->wdev.netdev);
1478             wiphy_lock(adapter->wiphy);
1479             mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
1480             wiphy_unlock(adapter->wiphy);
1481         }
1482         rtnl_unlock();
1483     }
1484 
1485     wiphy_unregister(adapter->wiphy);
1486     wiphy_free(adapter->wiphy);
1487     adapter->wiphy = NULL;
1488 
1489     vfree(adapter->chan_stats);
1490     mwifiex_free_cmd_buffers(adapter);
1491 }
1492 
1493 /*
1494  * This function can be used for shutting down the adapter SW.
1495  */
1496 int mwifiex_shutdown_sw(struct mwifiex_adapter *adapter)
1497 {
1498     struct mwifiex_private *priv;
1499 
1500     if (!adapter)
1501         return 0;
1502 
1503     wait_for_completion(adapter->fw_done);
1504     /* Caller should ensure we aren't suspending while this happens */
1505     reinit_completion(adapter->fw_done);
1506 
1507     priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1508     mwifiex_deauthenticate(priv, NULL);
1509 
1510     mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
1511 
1512     mwifiex_uninit_sw(adapter);
1513     adapter->is_up = false;
1514 
1515     if (adapter->if_ops.down_dev)
1516         adapter->if_ops.down_dev(adapter);
1517 
1518     return 0;
1519 }
1520 EXPORT_SYMBOL_GPL(mwifiex_shutdown_sw);
1521 
1522 /* This function can be used for reinitting the adapter SW. Required
1523  * code is extracted from mwifiex_add_card()
1524  */
1525 int
1526 mwifiex_reinit_sw(struct mwifiex_adapter *adapter)
1527 {
1528     int ret;
1529 
1530     mwifiex_init_lock_list(adapter);
1531     if (adapter->if_ops.up_dev)
1532         adapter->if_ops.up_dev(adapter);
1533 
1534     adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1535     clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1536     init_waitqueue_head(&adapter->init_wait_q);
1537     clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1538     adapter->hs_activated = false;
1539     clear_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags);
1540     init_waitqueue_head(&adapter->hs_activate_wait_q);
1541     init_waitqueue_head(&adapter->cmd_wait_q.wait);
1542     adapter->cmd_wait_q.status = 0;
1543     adapter->scan_wait_q_woken = false;
1544 
1545     if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1546         adapter->rx_work_enabled = true;
1547 
1548     adapter->workqueue =
1549         alloc_workqueue("MWIFIEX_WORK_QUEUE",
1550                 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1551     if (!adapter->workqueue)
1552         goto err_kmalloc;
1553 
1554     INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1555 
1556     if (adapter->rx_work_enabled) {
1557         adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1558                             WQ_HIGHPRI |
1559                             WQ_MEM_RECLAIM |
1560                             WQ_UNBOUND, 1);
1561         if (!adapter->rx_workqueue)
1562             goto err_kmalloc;
1563         INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1564     }
1565 
1566     /* Register the device. Fill up the private data structure with
1567      * relevant information from the card. Some code extracted from
1568      * mwifiex_register_dev()
1569      */
1570     mwifiex_dbg(adapter, INFO, "%s, mwifiex_init_hw_fw()...\n", __func__);
1571 
1572     if (mwifiex_init_hw_fw(adapter, false)) {
1573         mwifiex_dbg(adapter, ERROR,
1574                 "%s: firmware init failed\n", __func__);
1575         goto err_init_fw;
1576     }
1577 
1578     /* _mwifiex_fw_dpc() does its own cleanup */
1579     ret = _mwifiex_fw_dpc(adapter->firmware, adapter);
1580     if (ret) {
1581         pr_err("Failed to bring up adapter: %d\n", ret);
1582         return ret;
1583     }
1584     mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
1585 
1586     return 0;
1587 
1588 err_init_fw:
1589     mwifiex_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
1590     if (adapter->if_ops.unregister_dev)
1591         adapter->if_ops.unregister_dev(adapter);
1592 
1593 err_kmalloc:
1594     set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1595     mwifiex_terminate_workqueue(adapter);
1596     if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1597         mwifiex_dbg(adapter, ERROR,
1598                 "info: %s: shutdown mwifiex\n", __func__);
1599         mwifiex_shutdown_drv(adapter);
1600         mwifiex_free_cmd_buffers(adapter);
1601     }
1602 
1603     complete_all(adapter->fw_done);
1604     mwifiex_dbg(adapter, INFO, "%s, error\n", __func__);
1605 
1606     return -1;
1607 }
1608 EXPORT_SYMBOL_GPL(mwifiex_reinit_sw);
1609 
1610 static irqreturn_t mwifiex_irq_wakeup_handler(int irq, void *priv)
1611 {
1612     struct mwifiex_adapter *adapter = priv;
1613 
1614     dev_dbg(adapter->dev, "%s: wake by wifi", __func__);
1615     adapter->wake_by_wifi = true;
1616     disable_irq_nosync(irq);
1617 
1618     /* Notify PM core we are wakeup source */
1619     pm_wakeup_event(adapter->dev, 0);
1620     pm_system_wakeup();
1621 
1622     return IRQ_HANDLED;
1623 }
1624 
1625 static void mwifiex_probe_of(struct mwifiex_adapter *adapter)
1626 {
1627     int ret;
1628     struct device *dev = adapter->dev;
1629 
1630     if (!dev->of_node)
1631         goto err_exit;
1632 
1633     adapter->dt_node = dev->of_node;
1634     adapter->irq_wakeup = irq_of_parse_and_map(adapter->dt_node, 0);
1635     if (!adapter->irq_wakeup) {
1636         dev_dbg(dev, "fail to parse irq_wakeup from device tree\n");
1637         goto err_exit;
1638     }
1639 
1640     ret = devm_request_irq(dev, adapter->irq_wakeup,
1641                    mwifiex_irq_wakeup_handler, IRQF_TRIGGER_LOW,
1642                    "wifi_wake", adapter);
1643     if (ret) {
1644         dev_err(dev, "Failed to request irq_wakeup %d (%d)\n",
1645             adapter->irq_wakeup, ret);
1646         goto err_exit;
1647     }
1648 
1649     disable_irq(adapter->irq_wakeup);
1650     if (device_init_wakeup(dev, true)) {
1651         dev_err(dev, "fail to init wakeup for mwifiex\n");
1652         goto err_exit;
1653     }
1654     return;
1655 
1656 err_exit:
1657     adapter->irq_wakeup = -1;
1658 }
1659 
1660 /*
1661  * This function adds the card.
1662  *
1663  * This function follows the following major steps to set up the device -
1664  *      - Initialize software. This includes probing the card, registering
1665  *        the interface operations table, and allocating/initializing the
1666  *        adapter structure
1667  *      - Set up the netlink socket
1668  *      - Create and start the main work queue
1669  *      - Register the device
1670  *      - Initialize firmware and hardware
1671  *      - Add logical interfaces
1672  */
1673 int
1674 mwifiex_add_card(void *card, struct completion *fw_done,
1675          struct mwifiex_if_ops *if_ops, u8 iface_type,
1676          struct device *dev)
1677 {
1678     struct mwifiex_adapter *adapter;
1679 
1680     if (mwifiex_register(card, dev, if_ops, (void **)&adapter)) {
1681         pr_err("%s: software init failed\n", __func__);
1682         goto err_init_sw;
1683     }
1684 
1685     mwifiex_probe_of(adapter);
1686 
1687     adapter->iface_type = iface_type;
1688     adapter->fw_done = fw_done;
1689 
1690     adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1691     clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1692     init_waitqueue_head(&adapter->init_wait_q);
1693     clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1694     adapter->hs_activated = false;
1695     init_waitqueue_head(&adapter->hs_activate_wait_q);
1696     init_waitqueue_head(&adapter->cmd_wait_q.wait);
1697     adapter->cmd_wait_q.status = 0;
1698     adapter->scan_wait_q_woken = false;
1699 
1700     if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1701         adapter->rx_work_enabled = true;
1702 
1703     adapter->workqueue =
1704         alloc_workqueue("MWIFIEX_WORK_QUEUE",
1705                 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1706     if (!adapter->workqueue)
1707         goto err_kmalloc;
1708 
1709     INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1710 
1711     if (adapter->rx_work_enabled) {
1712         adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1713                             WQ_HIGHPRI |
1714                             WQ_MEM_RECLAIM |
1715                             WQ_UNBOUND, 1);
1716         if (!adapter->rx_workqueue)
1717             goto err_kmalloc;
1718 
1719         INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1720     }
1721 
1722     /* Register the device. Fill up the private data structure with relevant
1723        information from the card. */
1724     if (adapter->if_ops.register_dev(adapter)) {
1725         pr_err("%s: failed to register mwifiex device\n", __func__);
1726         goto err_registerdev;
1727     }
1728 
1729     if (mwifiex_init_hw_fw(adapter, true)) {
1730         pr_err("%s: firmware init failed\n", __func__);
1731         goto err_init_fw;
1732     }
1733 
1734     return 0;
1735 
1736 err_init_fw:
1737     pr_debug("info: %s: unregister device\n", __func__);
1738     if (adapter->if_ops.unregister_dev)
1739         adapter->if_ops.unregister_dev(adapter);
1740 err_registerdev:
1741     set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1742     mwifiex_terminate_workqueue(adapter);
1743     if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1744         pr_debug("info: %s: shutdown mwifiex\n", __func__);
1745         mwifiex_shutdown_drv(adapter);
1746         mwifiex_free_cmd_buffers(adapter);
1747     }
1748 err_kmalloc:
1749     if (adapter->irq_wakeup >= 0)
1750         device_init_wakeup(adapter->dev, false);
1751     mwifiex_free_adapter(adapter);
1752 
1753 err_init_sw:
1754 
1755     return -1;
1756 }
1757 EXPORT_SYMBOL_GPL(mwifiex_add_card);
1758 
1759 /*
1760  * This function removes the card.
1761  *
1762  * This function follows the following major steps to remove the device -
1763  *      - Stop data traffic
1764  *      - Shutdown firmware
1765  *      - Remove the logical interfaces
1766  *      - Terminate the work queue
1767  *      - Unregister the device
1768  *      - Free the adapter structure
1769  */
1770 int mwifiex_remove_card(struct mwifiex_adapter *adapter)
1771 {
1772     if (!adapter)
1773         return 0;
1774 
1775     if (adapter->is_up)
1776         mwifiex_uninit_sw(adapter);
1777 
1778     if (adapter->irq_wakeup >= 0)
1779         device_init_wakeup(adapter->dev, false);
1780 
1781     /* Unregister device */
1782     mwifiex_dbg(adapter, INFO,
1783             "info: unregister device\n");
1784     if (adapter->if_ops.unregister_dev)
1785         adapter->if_ops.unregister_dev(adapter);
1786     /* Free adapter structure */
1787     mwifiex_dbg(adapter, INFO,
1788             "info: free adapter\n");
1789     mwifiex_free_adapter(adapter);
1790 
1791     return 0;
1792 }
1793 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1794 
1795 void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1796           const char *fmt, ...)
1797 {
1798     struct va_format vaf;
1799     va_list args;
1800 
1801     if (!(adapter->debug_mask & mask))
1802         return;
1803 
1804     va_start(args, fmt);
1805 
1806     vaf.fmt = fmt;
1807     vaf.va = &args;
1808 
1809     if (adapter->dev)
1810         dev_info(adapter->dev, "%pV", &vaf);
1811     else
1812         pr_info("%pV", &vaf);
1813 
1814     va_end(args);
1815 }
1816 EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1817 
1818 /*
1819  * This function initializes the module.
1820  *
1821  * The debug FS is also initialized if configured.
1822  */
1823 static int
1824 mwifiex_init_module(void)
1825 {
1826 #ifdef CONFIG_DEBUG_FS
1827     mwifiex_debugfs_init();
1828 #endif
1829     return 0;
1830 }
1831 
1832 /*
1833  * This function cleans up the module.
1834  *
1835  * The debug FS is removed if available.
1836  */
1837 static void
1838 mwifiex_cleanup_module(void)
1839 {
1840 #ifdef CONFIG_DEBUG_FS
1841     mwifiex_debugfs_remove();
1842 #endif
1843 }
1844 
1845 module_init(mwifiex_init_module);
1846 module_exit(mwifiex_cleanup_module);
1847 
1848 MODULE_AUTHOR("Marvell International Ltd.");
1849 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1850 MODULE_VERSION(VERSION);
1851 MODULE_LICENSE("GPL v2");