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