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