UPSTREAM mwifiex: fix memory leak issue when driver unload
[cascardo/linux.git] / drivers / net / wireless / mwifiex / main.c
1 /*
2  * Marvell Wireless LAN device driver: major functions
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "main.h"
21 #include "wmm.h"
22 #include "cfg80211.h"
23 #include "11n.h"
24
25 #define VERSION "1.0"
26
27 const char driver_version[] = "mwifiex " VERSION " (%s) ";
28
29 /*
30  * This function registers the device and performs all the necessary
31  * initializations.
32  *
33  * The following initialization operations are performed -
34  *      - Allocate adapter structure
35  *      - Save interface specific operations table in adapter
36  *      - Call interface specific initialization routine
37  *      - Allocate private structures
38  *      - Set default adapter structure parameters
39  *      - Initialize locks
40  *
41  * In case of any errors during inittialization, this function also ensures
42  * proper cleanup before exiting.
43  */
44 static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
45                             void **padapter)
46 {
47         struct mwifiex_adapter *adapter;
48         int i;
49
50         adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
51         if (!adapter)
52                 return -ENOMEM;
53
54         *padapter = adapter;
55         adapter->card = card;
56
57         /* Save interface specific operations in adapter */
58         memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
59
60         /* card specific initialization has been deferred until now .. */
61         if (adapter->if_ops.init_if(adapter))
62                 goto error;
63
64         adapter->priv_num = 0;
65
66         /* Allocate memory for private structure */
67         adapter->priv[0] = kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
68         if (!adapter->priv[0]) {
69                 dev_err(adapter->dev,
70                         "%s: failed to alloc priv[0]\n", __func__);
71                 goto error;
72         }
73
74         adapter->priv_num++;
75
76         adapter->priv[0]->adapter = adapter;
77         mwifiex_init_lock_list(adapter);
78
79         init_timer(&adapter->cmd_timer);
80         adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
81         adapter->cmd_timer.data = (unsigned long) adapter;
82
83         return 0;
84
85 error:
86         dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
87
88         for (i = 0; i < adapter->priv_num; i++)
89                 kfree(adapter->priv[i]);
90
91         kfree(adapter);
92
93         return -1;
94 }
95
96 /*
97  * This function unregisters the device and performs all the necessary
98  * cleanups.
99  *
100  * The following cleanup operations are performed -
101  *      - Free the timers
102  *      - Free beacon buffers
103  *      - Free private structures
104  *      - Free adapter structure
105  */
106 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
107 {
108         s32 i;
109
110         del_timer(&adapter->cmd_timer);
111
112         /* Free private structures */
113         for (i = 0; i < adapter->priv_num; i++) {
114                 if (adapter->priv[i]) {
115                         mwifiex_free_curr_bcn(adapter->priv[i]);
116                         kfree(adapter->priv[i]);
117                 }
118         }
119
120         kfree(adapter);
121         return 0;
122 }
123
124 /*
125  * The main process.
126  *
127  * This function is the main procedure of the driver and handles various driver
128  * operations. It runs in a loop and provides the core functionalities.
129  *
130  * The main responsibilities of this function are -
131  *      - Ensure concurrency control
132  *      - Handle pending interrupts and call interrupt handlers
133  *      - Wake up the card if required
134  *      - Handle command responses and call response handlers
135  *      - Handle events and call event handlers
136  *      - Execute pending commands
137  *      - Transmit pending data packets
138  */
139 int mwifiex_main_process(struct mwifiex_adapter *adapter)
140 {
141         int ret = 0;
142         unsigned long flags;
143
144         spin_lock_irqsave(&adapter->main_proc_lock, flags);
145
146         /* Check if already processing */
147         if (adapter->mwifiex_processing) {
148                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
149                 goto exit_main_proc;
150         } else {
151                 adapter->mwifiex_processing = true;
152                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
153         }
154 process_start:
155         do {
156                 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
157                     (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
158                         break;
159
160                 /* Handle pending interrupt if any */
161                 if (adapter->int_status) {
162                         if (adapter->hs_activated)
163                                 mwifiex_process_hs_config(adapter);
164                         adapter->if_ops.process_int_status(adapter);
165                 }
166
167                 /* Need to wake up the card ? */
168                 if ((adapter->ps_state == PS_STATE_SLEEP) &&
169                     (adapter->pm_wakeup_card_req &&
170                      !adapter->pm_wakeup_fw_try) &&
171                     (is_command_pending(adapter) ||
172                      !mwifiex_wmm_lists_empty(adapter))) {
173                         adapter->pm_wakeup_fw_try = true;
174                         adapter->if_ops.wakeup(adapter);
175                         continue;
176                 }
177                 if (IS_CARD_RX_RCVD(adapter)) {
178                         adapter->pm_wakeup_fw_try = false;
179                         if (adapter->ps_state == PS_STATE_SLEEP)
180                                 adapter->ps_state = PS_STATE_AWAKE;
181                 } else {
182                         /* We have tried to wakeup the card already */
183                         if (adapter->pm_wakeup_fw_try)
184                                 break;
185                         if (adapter->ps_state != PS_STATE_AWAKE ||
186                             adapter->tx_lock_flag)
187                                 break;
188
189                         if ((adapter->scan_processing &&
190                              !adapter->scan_delay_cnt) || adapter->data_sent ||
191                             mwifiex_wmm_lists_empty(adapter)) {
192                                 if (adapter->cmd_sent || adapter->curr_cmd ||
193                                     (!is_command_pending(adapter)))
194                                         break;
195                         }
196                 }
197
198                 /* Check for Cmd Resp */
199                 if (adapter->cmd_resp_received) {
200                         adapter->cmd_resp_received = false;
201                         mwifiex_process_cmdresp(adapter);
202
203                         /* call mwifiex back when init_fw is done */
204                         if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
205                                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
206                                 mwifiex_init_fw_complete(adapter);
207                         }
208                 }
209
210                 /* Check for event */
211                 if (adapter->event_received) {
212                         adapter->event_received = false;
213                         mwifiex_process_event(adapter);
214                 }
215
216                 /* Check if we need to confirm Sleep Request
217                    received previously */
218                 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
219                         if (!adapter->cmd_sent && !adapter->curr_cmd)
220                                 mwifiex_check_ps_cond(adapter);
221                 }
222
223                 /* * The ps_state may have been changed during processing of
224                  * Sleep Request event.
225                  */
226                 if ((adapter->ps_state == PS_STATE_SLEEP) ||
227                     (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
228                     (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
229                     adapter->tx_lock_flag)
230                         continue;
231
232                 if (!adapter->cmd_sent && !adapter->curr_cmd) {
233                         if (mwifiex_exec_next_cmd(adapter) == -1) {
234                                 ret = -1;
235                                 break;
236                         }
237                 }
238
239                 if ((!adapter->scan_processing || adapter->scan_delay_cnt) &&
240                     !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
241                         mwifiex_wmm_process_tx(adapter);
242                         if (adapter->hs_activated) {
243                                 adapter->is_hs_configured = false;
244                                 mwifiex_hs_activated_event
245                                         (mwifiex_get_priv
246                                          (adapter, MWIFIEX_BSS_ROLE_ANY),
247                                          false);
248                         }
249                 }
250
251                 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
252                     !adapter->curr_cmd && !is_command_pending(adapter) &&
253                     mwifiex_wmm_lists_empty(adapter)) {
254                         if (!mwifiex_send_null_packet
255                             (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
256                              MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
257                              MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
258                                 adapter->delay_null_pkt = false;
259                                 adapter->ps_state = PS_STATE_SLEEP;
260                         }
261                         break;
262                 }
263         } while (true);
264
265         if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter))
266                 goto process_start;
267
268         spin_lock_irqsave(&adapter->main_proc_lock, flags);
269         adapter->mwifiex_processing = false;
270         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
271
272 exit_main_proc:
273         if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
274                 mwifiex_shutdown_drv(adapter);
275         return ret;
276 }
277 EXPORT_SYMBOL_GPL(mwifiex_main_process);
278
279 /*
280  * This function frees the adapter structure.
281  *
282  * Additionally, this closes the netlink socket, frees the timers
283  * and private structures.
284  */
285 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
286 {
287         if (!adapter) {
288                 pr_err("%s: adapter is NULL\n", __func__);
289                 return;
290         }
291
292         mwifiex_unregister(adapter);
293         pr_debug("info: %s: free adapter\n", __func__);
294 }
295
296 /*
297  * This function gets firmware and initializes it.
298  *
299  * The main initialization steps followed are -
300  *      - Download the correct firmware to card
301  *      - Issue the init commands to firmware
302  */
303 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
304 {
305         int ret;
306         char fmt[64];
307         struct mwifiex_private *priv;
308         struct mwifiex_adapter *adapter = context;
309         struct mwifiex_fw_image fw;
310
311         if (!firmware) {
312                 dev_err(adapter->dev,
313                         "Failed to get firmware %s\n", adapter->fw_name);
314                 goto done;
315         }
316
317         memset(&fw, 0, sizeof(struct mwifiex_fw_image));
318         adapter->firmware = firmware;
319         fw.fw_buf = (u8 *) adapter->firmware->data;
320         fw.fw_len = adapter->firmware->size;
321
322         ret = mwifiex_dnld_fw(adapter, &fw);
323         if (ret == -1)
324                 goto done;
325
326         dev_notice(adapter->dev, "WLAN FW is active\n");
327
328         adapter->init_wait_q_woken = false;
329         ret = mwifiex_init_fw(adapter);
330         if (ret == -1) {
331                 goto done;
332         } else if (!ret) {
333                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
334                 goto done;
335         }
336         /* Wait for mwifiex_init to complete */
337         wait_event_interruptible(adapter->init_wait_q,
338                                  adapter->init_wait_q_woken);
339         if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
340                 goto done;
341
342         priv = adapter->priv[0];
343         if (mwifiex_register_cfg80211(priv) != 0) {
344                 dev_err(adapter->dev, "cannot register with cfg80211\n");
345                 goto err_init_fw;
346         }
347
348         rtnl_lock();
349         /* Create station interface by default */
350         if (!mwifiex_add_virtual_intf(priv->wdev->wiphy, "mlan%d",
351                                       NL80211_IFTYPE_STATION, NULL, NULL)) {
352                 dev_err(adapter->dev, "cannot create default STA interface\n");
353                 goto err_add_intf;
354         }
355         rtnl_unlock();
356
357         mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
358         dev_notice(adapter->dev, "driver_version = %s\n", fmt);
359         goto done;
360
361 err_add_intf:
362         mwifiex_del_virtual_intf(priv->wdev->wiphy, priv->netdev);
363         rtnl_unlock();
364 err_init_fw:
365         pr_debug("info: %s: unregister device\n", __func__);
366         adapter->if_ops.unregister_dev(adapter);
367 done:
368         release_firmware(adapter->firmware);
369         complete(&adapter->fw_load);
370         return;
371 }
372
373 /*
374  * This function initializes the hardware and gets firmware.
375  */
376 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
377 {
378         int ret;
379
380         init_completion(&adapter->fw_load);
381         ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
382                                       adapter->dev, GFP_KERNEL, adapter,
383                                       mwifiex_fw_dpc);
384         if (ret < 0)
385                 dev_err(adapter->dev,
386                         "request_firmware_nowait() returned error %d\n", ret);
387         return ret;
388 }
389
390 /*
391  * CFG802.11 network device handler for open.
392  *
393  * Starts the data queue.
394  */
395 static int
396 mwifiex_open(struct net_device *dev)
397 {
398         netif_tx_start_all_queues(dev);
399         return 0;
400 }
401
402 /*
403  * CFG802.11 network device handler for close.
404  */
405 static int
406 mwifiex_close(struct net_device *dev)
407 {
408         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
409
410         if (priv->scan_request) {
411                 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
412                 cfg80211_scan_done(priv->scan_request, 1);
413                 priv->scan_request = NULL;
414         }
415
416         return 0;
417 }
418
419 /*
420  * Add buffer into wmm tx queue and queue work to transmit it.
421  */
422 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
423 {
424         struct netdev_queue *txq;
425         int index = mwifiex_1d_to_wmm_queue[skb->priority];
426
427         if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
428                 txq = netdev_get_tx_queue(priv->netdev, index);
429                 if (!netif_tx_queue_stopped(txq)) {
430                         netif_tx_stop_queue(txq);
431                         dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
432                 }
433         }
434
435         atomic_inc(&priv->adapter->tx_pending);
436         mwifiex_wmm_add_buf_txqueue(priv, skb);
437
438         if (priv->adapter->scan_delay_cnt)
439                 atomic_set(&priv->adapter->is_tx_received, true);
440
441         queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
442
443         return 0;
444 }
445
446 /*
447  * CFG802.11 network device handler for data transmission.
448  */
449 static int
450 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
451 {
452         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
453         struct sk_buff *new_skb;
454         struct mwifiex_txinfo *tx_info;
455         struct timeval tv;
456
457         dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
458                 jiffies, priv->bss_type, priv->bss_num);
459
460         if (priv->adapter->surprise_removed) {
461                 kfree_skb(skb);
462                 priv->stats.tx_dropped++;
463                 return 0;
464         }
465         if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
466                 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
467                 kfree_skb(skb);
468                 priv->stats.tx_dropped++;
469                 return 0;
470         }
471         if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
472                 dev_dbg(priv->adapter->dev,
473                         "data: Tx: insufficient skb headroom %d\n",
474                         skb_headroom(skb));
475                 /* Insufficient skb headroom - allocate a new skb */
476                 new_skb =
477                         skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
478                 if (unlikely(!new_skb)) {
479                         dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
480                         kfree_skb(skb);
481                         priv->stats.tx_dropped++;
482                         return 0;
483                 }
484                 kfree_skb(skb);
485                 skb = new_skb;
486                 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
487                         skb_headroom(skb));
488         }
489
490         tx_info = MWIFIEX_SKB_TXCB(skb);
491         tx_info->bss_num = priv->bss_num;
492         tx_info->bss_type = priv->bss_type;
493
494         /* Record the current time the packet was queued; used to
495          * determine the amount of time the packet was queued in
496          * the driver before it was sent to the firmware.
497          * The delay is then sent along with the packet to the
498          * firmware for aggregate delay calculation for stats and
499          * MSDU lifetime expiry.
500          */
501         do_gettimeofday(&tv);
502         skb->tstamp = timeval_to_ktime(tv);
503
504         mwifiex_queue_tx_pkt(priv, skb);
505
506         return 0;
507 }
508
509 /*
510  * CFG802.11 network device handler for setting MAC address.
511  */
512 static int
513 mwifiex_set_mac_address(struct net_device *dev, void *addr)
514 {
515         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
516         struct sockaddr *hw_addr = addr;
517         int ret;
518
519         memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
520
521         /* Send request to firmware */
522         ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
523                                     HostCmd_ACT_GEN_SET, 0, NULL);
524
525         if (!ret)
526                 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
527         else
528                 dev_err(priv->adapter->dev,
529                         "set mac address failed: ret=%d\n", ret);
530
531         memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
532
533         return ret;
534 }
535
536 /*
537  * CFG802.11 network device handler for setting multicast list.
538  */
539 static void mwifiex_set_multicast_list(struct net_device *dev)
540 {
541         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
542         struct mwifiex_multicast_list mcast_list;
543
544         if (dev->flags & IFF_PROMISC) {
545                 mcast_list.mode = MWIFIEX_PROMISC_MODE;
546         } else if (dev->flags & IFF_ALLMULTI ||
547                    netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
548                 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
549         } else {
550                 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
551                 if (netdev_mc_count(dev))
552                         mcast_list.num_multicast_addr =
553                                 mwifiex_copy_mcast_addr(&mcast_list, dev);
554         }
555         mwifiex_request_set_multicast_list(priv, &mcast_list);
556 }
557
558 /*
559  * CFG802.11 network device handler for transmission timeout.
560  */
561 static void
562 mwifiex_tx_timeout(struct net_device *dev)
563 {
564         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
565
566         priv->num_tx_timeout++;
567         priv->tx_timeout_cnt++;
568         dev_err(priv->adapter->dev,
569                 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
570                 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
571         mwifiex_set_trans_start(dev);
572
573         if (priv->adapter->if_ops.reg_dbg)
574                 priv->adapter->if_ops.reg_dbg(priv->adapter);
575
576         if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
577             priv->adapter->if_ops.card_reset) {
578                 dev_err(priv->adapter->dev, "tx_timeout_cnt exceeds threshold. "
579                         "Triggering card reset!\n");
580                 priv->adapter->if_ops.card_reset(priv->adapter);
581         }
582 }
583
584 /*
585  * CFG802.11 network device handler for statistics retrieval.
586  */
587 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
588 {
589         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
590
591         return &priv->stats;
592 }
593
594 static u16
595 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb)
596 {
597         skb->priority = cfg80211_classify8021d(skb);
598         return mwifiex_1d_to_wmm_queue[skb->priority];
599 }
600
601 /* Network device handlers */
602 static const struct net_device_ops mwifiex_netdev_ops = {
603         .ndo_open = mwifiex_open,
604         .ndo_stop = mwifiex_close,
605         .ndo_start_xmit = mwifiex_hard_start_xmit,
606         .ndo_set_mac_address = mwifiex_set_mac_address,
607         .ndo_tx_timeout = mwifiex_tx_timeout,
608         .ndo_get_stats = mwifiex_get_stats,
609         .ndo_set_rx_mode = mwifiex_set_multicast_list,
610         .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
611 };
612
613 /*
614  * This function initializes the private structure parameters.
615  *
616  * The following wait queues are initialized -
617  *      - IOCTL wait queue
618  *      - Command wait queue
619  *      - Statistics wait queue
620  *
621  * ...and the following default parameters are set -
622  *      - Current key index     : Set to 0
623  *      - Rate index            : Set to auto
624  *      - Media connected       : Set to disconnected
625  *      - Adhoc link sensed     : Set to false
626  *      - Nick name             : Set to null
627  *      - Number of Tx timeout  : Set to 0
628  *      - Device address        : Set to current address
629  *
630  * In addition, the CFG80211 work queue is also created.
631  */
632 void mwifiex_init_priv_params(struct mwifiex_private *priv,
633                                                 struct net_device *dev)
634 {
635         dev->netdev_ops = &mwifiex_netdev_ops;
636         dev->destructor = free_netdev;
637         /* Initialize private structure */
638         priv->current_key_index = 0;
639         priv->media_connected = false;
640         memset(&priv->nick_name, 0, sizeof(priv->nick_name));
641         priv->num_tx_timeout = 0;
642         memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
643 }
644
645 /*
646  * This function check if command is pending.
647  */
648 int is_command_pending(struct mwifiex_adapter *adapter)
649 {
650         unsigned long flags;
651         int is_cmd_pend_q_empty;
652
653         spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
654         is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
655         spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
656
657         return !is_cmd_pend_q_empty;
658 }
659
660 /*
661  * This is the main work queue function.
662  *
663  * It handles the main process, which in turn handles the complete
664  * driver operations.
665  */
666 static void mwifiex_main_work_queue(struct work_struct *work)
667 {
668         struct mwifiex_adapter *adapter =
669                 container_of(work, struct mwifiex_adapter, main_work);
670
671         if (adapter->surprise_removed)
672                 return;
673         mwifiex_main_process(adapter);
674 }
675
676 /*
677  * This function cancels all works in the queue and destroys
678  * the main workqueue.
679  */
680 static void
681 mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
682 {
683         flush_workqueue(adapter->workqueue);
684         destroy_workqueue(adapter->workqueue);
685         adapter->workqueue = NULL;
686 }
687
688 /*
689  * This function adds the card.
690  *
691  * This function follows the following major steps to set up the device -
692  *      - Initialize software. This includes probing the card, registering
693  *        the interface operations table, and allocating/initializing the
694  *        adapter structure
695  *      - Set up the netlink socket
696  *      - Create and start the main work queue
697  *      - Register the device
698  *      - Initialize firmware and hardware
699  *      - Add logical interfaces
700  */
701 int
702 mwifiex_add_card(void *card, struct semaphore *sem,
703                  struct mwifiex_if_ops *if_ops, u8 iface_type)
704 {
705         struct mwifiex_adapter *adapter;
706
707         if (down_interruptible(sem))
708                 goto exit_sem_err;
709
710         if (mwifiex_register(card, if_ops, (void **)&adapter)) {
711                 pr_err("%s: software init failed\n", __func__);
712                 goto err_init_sw;
713         }
714
715         adapter->iface_type = iface_type;
716
717         adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
718         adapter->surprise_removed = false;
719         init_waitqueue_head(&adapter->init_wait_q);
720         adapter->is_suspended = false;
721         adapter->hs_activated = false;
722         init_waitqueue_head(&adapter->hs_activate_wait_q);
723         adapter->cmd_wait_q_required = false;
724         init_waitqueue_head(&adapter->cmd_wait_q.wait);
725         adapter->cmd_wait_q.status = 0;
726         adapter->scan_wait_q_woken = false;
727
728         adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
729         if (!adapter->workqueue)
730                 goto err_kmalloc;
731
732         INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
733
734         /* Register the device. Fill up the private data structure with relevant
735            information from the card and request for the required IRQ. */
736         if (adapter->if_ops.register_dev(adapter)) {
737                 pr_err("%s: failed to register mwifiex device\n", __func__);
738                 goto err_registerdev;
739         }
740
741         if (mwifiex_init_hw_fw(adapter)) {
742                 pr_err("%s: firmware init failed\n", __func__);
743                 goto err_init_fw;
744         }
745
746         up(sem);
747         return 0;
748
749 err_init_fw:
750         pr_debug("info: %s: unregister device\n", __func__);
751         adapter->if_ops.unregister_dev(adapter);
752 err_registerdev:
753         adapter->surprise_removed = true;
754         mwifiex_terminate_workqueue(adapter);
755 err_kmalloc:
756         if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
757             (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
758                 pr_debug("info: %s: shutdown mwifiex\n", __func__);
759                 adapter->init_wait_q_woken = false;
760
761                 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
762                         wait_event_interruptible(adapter->init_wait_q,
763                                                  adapter->init_wait_q_woken);
764         }
765
766         mwifiex_free_adapter(adapter);
767
768 err_init_sw:
769         up(sem);
770
771 exit_sem_err:
772         return -1;
773 }
774 EXPORT_SYMBOL_GPL(mwifiex_add_card);
775
776 /*
777  * This function removes the card.
778  *
779  * This function follows the following major steps to remove the device -
780  *      - Stop data traffic
781  *      - Shutdown firmware
782  *      - Remove the logical interfaces
783  *      - Terminate the work queue
784  *      - Unregister the device
785  *      - Free the adapter structure
786  */
787 int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
788 {
789         struct mwifiex_private *priv = NULL;
790         int i;
791
792         if (down_interruptible(sem))
793                 goto exit_sem_err;
794
795         if (!adapter)
796                 goto exit_remove;
797
798         adapter->surprise_removed = true;
799
800         /* Stop data */
801         for (i = 0; i < adapter->priv_num; i++) {
802                 priv = adapter->priv[i];
803                 if (priv && priv->netdev) {
804                         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
805                         if (netif_carrier_ok(priv->netdev))
806                                 netif_carrier_off(priv->netdev);
807                 }
808         }
809
810         dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
811         adapter->init_wait_q_woken = false;
812
813         if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
814                 wait_event_interruptible(adapter->init_wait_q,
815                                          adapter->init_wait_q_woken);
816         dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
817         if (atomic_read(&adapter->rx_pending) ||
818             atomic_read(&adapter->tx_pending) ||
819             atomic_read(&adapter->cmd_pending)) {
820                 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
821                        "cmd_pending=%d\n",
822                        atomic_read(&adapter->rx_pending),
823                        atomic_read(&adapter->tx_pending),
824                        atomic_read(&adapter->cmd_pending));
825         }
826
827         for (i = 0; i < adapter->priv_num; i++) {
828                 priv = adapter->priv[i];
829
830                 if (!priv)
831                         continue;
832
833                 rtnl_lock();
834                 if (priv->wdev && priv->netdev)
835                         mwifiex_del_virtual_intf(priv->wdev->wiphy,
836                                                  priv->netdev);
837                 rtnl_unlock();
838         }
839
840         priv = adapter->priv[0];
841         if (!priv)
842                 goto exit_remove;
843
844         if (priv->wdev) {
845                 wiphy_unregister(priv->wdev->wiphy);
846                 wiphy_free(priv->wdev->wiphy);
847                 kfree(priv->wdev);
848         }
849
850         mwifiex_terminate_workqueue(adapter);
851
852         /* Unregister device */
853         dev_dbg(adapter->dev, "info: unregister device\n");
854         adapter->if_ops.unregister_dev(adapter);
855         /* Free adapter structure */
856         dev_dbg(adapter->dev, "info: free adapter\n");
857         mwifiex_free_adapter(adapter);
858
859 exit_remove:
860         up(sem);
861 exit_sem_err:
862         return 0;
863 }
864 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
865
866 /*
867  * This function initializes the module.
868  *
869  * The debug FS is also initialized if configured.
870  */
871 static int
872 mwifiex_init_module(void)
873 {
874 #ifdef CONFIG_DEBUG_FS
875         mwifiex_debugfs_init();
876 #endif
877         return 0;
878 }
879
880 /*
881  * This function cleans up the module.
882  *
883  * The debug FS is removed if available.
884  */
885 static void
886 mwifiex_cleanup_module(void)
887 {
888 #ifdef CONFIG_DEBUG_FS
889         mwifiex_debugfs_remove();
890 #endif
891 }
892
893 module_init(mwifiex_init_module);
894 module_exit(mwifiex_cleanup_module);
895
896 MODULE_AUTHOR("Marvell International Ltd.");
897 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
898 MODULE_VERSION(VERSION);
899 MODULE_LICENSE("GPL v2");