orinoco_usb: Use setup_timer
[deliverable/linux.git] / drivers / net / wireless / mwifiex / main.c
CommitLineData
5e6e3a92
BZ
1/*
2 * Marvell Wireless LAN device driver: major functions
3 *
65da33f5 4 * Copyright (C) 2011-2014, Marvell International Ltd.
5e6e3a92
BZ
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
27const char driver_version[] = "mwifiex " VERSION " (%s) ";
388ec385
AK
28static char *cal_data_cfg;
29module_param(cal_data_cfg, charp, 0);
5e6e3a92 30
0013c7ce
AP
31static unsigned short driver_mode;
32module_param(driver_mode, ushort, 0);
33MODULE_PARM_DESC(driver_mode,
34 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
35
5e6e3a92
BZ
36/*
37 * This function registers the device and performs all the necessary
38 * initializations.
39 *
40 * The following initialization operations are performed -
41 * - Allocate adapter structure
42 * - Save interface specific operations table in adapter
43 * - Call interface specific initialization routine
44 * - Allocate private structures
45 * - Set default adapter structure parameters
46 * - Initialize locks
47 *
48 * In case of any errors during inittialization, this function also ensures
49 * proper cleanup before exiting.
50 */
51static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
287546df 52 void **padapter)
5e6e3a92 53{
2be7859f
AK
54 struct mwifiex_adapter *adapter;
55 int i;
5e6e3a92
BZ
56
57 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
5e6e3a92 58 if (!adapter)
b53575ec 59 return -ENOMEM;
5e6e3a92 60
287546df 61 *padapter = adapter;
5e6e3a92
BZ
62 adapter->card = card;
63
64 /* Save interface specific operations in adapter */
65 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
66
67 /* card specific initialization has been deferred until now .. */
4daffe35
AK
68 if (adapter->if_ops.init_if)
69 if (adapter->if_ops.init_if(adapter))
70 goto error;
5e6e3a92
BZ
71
72 adapter->priv_num = 0;
5e6e3a92 73
64b05e2f
AP
74 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
75 /* Allocate memory for private structure */
76 adapter->priv[i] =
77 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
78 if (!adapter->priv[i])
79 goto error;
93a1df48 80
64b05e2f 81 adapter->priv[i]->adapter = adapter;
64b05e2f
AP
82 adapter->priv_num++;
83 }
44b815c6 84 mwifiex_init_lock_list(adapter);
5e6e3a92
BZ
85
86 init_timer(&adapter->cmd_timer);
87 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
88 adapter->cmd_timer.data = (unsigned long) adapter;
89
5e6e3a92
BZ
90 return 0;
91
92error:
93 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
94
93a1df48 95 for (i = 0; i < adapter->priv_num; i++)
5e6e3a92 96 kfree(adapter->priv[i]);
93a1df48 97
5e6e3a92
BZ
98 kfree(adapter);
99
100 return -1;
101}
102
103/*
104 * This function unregisters the device and performs all the necessary
105 * cleanups.
106 *
107 * The following cleanup operations are performed -
108 * - Free the timers
109 * - Free beacon buffers
110 * - Free private structures
111 * - Free adapter structure
112 */
113static int mwifiex_unregister(struct mwifiex_adapter *adapter)
114{
270e58e8 115 s32 i;
5e6e3a92 116
4cfda67d
AK
117 if (adapter->if_ops.cleanup_if)
118 adapter->if_ops.cleanup_if(adapter);
119
629873f2 120 del_timer_sync(&adapter->cmd_timer);
5e6e3a92
BZ
121
122 /* Free private structures */
123 for (i = 0; i < adapter->priv_num; i++) {
124 if (adapter->priv[i]) {
125 mwifiex_free_curr_bcn(adapter->priv[i]);
126 kfree(adapter->priv[i]);
127 }
128 }
129
bf354433 130 vfree(adapter->chan_stats);
5e6e3a92
BZ
131 kfree(adapter);
132 return 0;
133}
134
6e251174
AP
135static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
136{
137 unsigned long flags;
138 struct sk_buff *skb;
6e251174
AP
139
140 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
141 if (adapter->rx_processing || adapter->rx_locked) {
142 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
143 goto exit_rx_proc;
144 } else {
145 adapter->rx_processing = true;
146 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
147 }
148
149 /* Check for Rx data */
150 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
151 atomic_dec(&adapter->rx_pending);
381e9fff
AK
152 if ((adapter->delay_main_work ||
153 adapter->iface_type == MWIFIEX_USB) &&
b43a0d9d 154 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
cf6a64fd
AK
155 if (adapter->if_ops.submit_rem_rx_urbs)
156 adapter->if_ops.submit_rem_rx_urbs(adapter);
6e251174 157 adapter->delay_main_work = false;
b43a0d9d 158 queue_work(adapter->workqueue, &adapter->main_work);
6e251174
AP
159 }
160 mwifiex_handle_rx_packet(adapter, skb);
161 }
162 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
163 adapter->rx_processing = false;
164 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
165
6e251174
AP
166exit_rx_proc:
167 return 0;
168}
169
5e6e3a92
BZ
170/*
171 * The main process.
172 *
173 * This function is the main procedure of the driver and handles various driver
174 * operations. It runs in a loop and provides the core functionalities.
175 *
176 * The main responsibilities of this function are -
177 * - Ensure concurrency control
178 * - Handle pending interrupts and call interrupt handlers
179 * - Wake up the card if required
180 * - Handle command responses and call response handlers
181 * - Handle events and call event handlers
182 * - Execute pending commands
183 * - Transmit pending data packets
184 */
185int mwifiex_main_process(struct mwifiex_adapter *adapter)
186{
187 int ret = 0;
188 unsigned long flags;
189
190 spin_lock_irqsave(&adapter->main_proc_lock, flags);
191
192 /* Check if already processing */
193 if (adapter->mwifiex_processing) {
194 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
195 goto exit_main_proc;
196 } else {
197 adapter->mwifiex_processing = true;
198 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
199 }
200process_start:
201 do {
202 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
203 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
204 break;
205
381e9fff
AK
206 /* For non-USB interfaces, If we process interrupts first, it
207 * would increase RX pending even further. Avoid this by
208 * checking if rx_pending has crossed high threshold and
209 * schedule rx work queue and then process interrupts.
210 * For USB interface, there are no interrupts. We already have
211 * HIGH_RX_PENDING check in usb.c
6e251174 212 */
381e9fff
AK
213 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
214 adapter->iface_type != MWIFIEX_USB) {
6e251174
AP
215 adapter->delay_main_work = true;
216 if (!adapter->rx_processing)
217 queue_work(adapter->rx_workqueue,
218 &adapter->rx_work);
219 break;
220 }
221
5e6e3a92
BZ
222 /* Handle pending interrupt if any */
223 if (adapter->int_status) {
224 if (adapter->hs_activated)
225 mwifiex_process_hs_config(adapter);
4daffe35
AK
226 if (adapter->if_ops.process_int_status)
227 adapter->if_ops.process_int_status(adapter);
5e6e3a92
BZ
228 }
229
6e251174
AP
230 if (adapter->rx_work_enabled && adapter->data_received)
231 queue_work(adapter->rx_workqueue, &adapter->rx_work);
232
5e6e3a92
BZ
233 /* Need to wake up the card ? */
234 if ((adapter->ps_state == PS_STATE_SLEEP) &&
235 (adapter->pm_wakeup_card_req &&
236 !adapter->pm_wakeup_fw_try) &&
f57c1edc
YAP
237 (is_command_pending(adapter) ||
238 !mwifiex_wmm_lists_empty(adapter))) {
5e6e3a92 239 adapter->pm_wakeup_fw_try = true;
4636187d 240 mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
5e6e3a92
BZ
241 adapter->if_ops.wakeup(adapter);
242 continue;
243 }
4daffe35 244
5e6e3a92 245 if (IS_CARD_RX_RCVD(adapter)) {
6e251174 246 adapter->data_received = false;
5e6e3a92 247 adapter->pm_wakeup_fw_try = false;
4636187d 248 del_timer_sync(&adapter->wakeup_timer);
5e6e3a92
BZ
249 if (adapter->ps_state == PS_STATE_SLEEP)
250 adapter->ps_state = PS_STATE_AWAKE;
251 } else {
252 /* We have tried to wakeup the card already */
253 if (adapter->pm_wakeup_fw_try)
254 break;
255 if (adapter->ps_state != PS_STATE_AWAKE ||
256 adapter->tx_lock_flag)
257 break;
258
5ec39efa 259 if ((!adapter->scan_chan_gap_enabled &&
5ec39efa 260 adapter->scan_processing) || adapter->data_sent ||
f57c1edc
YAP
261 mwifiex_wmm_lists_empty(adapter)) {
262 if (adapter->cmd_sent || adapter->curr_cmd ||
263 (!is_command_pending(adapter)))
5e6e3a92
BZ
264 break;
265 }
266 }
267
20474129
AK
268 /* Check for event */
269 if (adapter->event_received) {
270 adapter->event_received = false;
271 mwifiex_process_event(adapter);
272 }
273
5e6e3a92
BZ
274 /* Check for Cmd Resp */
275 if (adapter->cmd_resp_received) {
276 adapter->cmd_resp_received = false;
277 mwifiex_process_cmdresp(adapter);
278
279 /* call mwifiex back when init_fw is done */
280 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
281 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
282 mwifiex_init_fw_complete(adapter);
283 }
284 }
285
5e6e3a92
BZ
286 /* Check if we need to confirm Sleep Request
287 received previously */
288 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
289 if (!adapter->cmd_sent && !adapter->curr_cmd)
290 mwifiex_check_ps_cond(adapter);
291 }
292
293 /* * The ps_state may have been changed during processing of
294 * Sleep Request event.
295 */
f57c1edc
YAP
296 if ((adapter->ps_state == PS_STATE_SLEEP) ||
297 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
298 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
299 adapter->tx_lock_flag)
5e6e3a92
BZ
300 continue;
301
302 if (!adapter->cmd_sent && !adapter->curr_cmd) {
303 if (mwifiex_exec_next_cmd(adapter) == -1) {
304 ret = -1;
305 break;
306 }
307 }
308
5ec39efa 309 if ((adapter->scan_chan_gap_enabled ||
d8d91253 310 !adapter->scan_processing) &&
3249ba73 311 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
5e6e3a92
BZ
312 mwifiex_wmm_process_tx(adapter);
313 if (adapter->hs_activated) {
314 adapter->is_hs_configured = false;
315 mwifiex_hs_activated_event
316 (mwifiex_get_priv
317 (adapter, MWIFIEX_BSS_ROLE_ANY),
318 false);
319 }
320 }
321
322 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
f57c1edc
YAP
323 !adapter->curr_cmd && !is_command_pending(adapter) &&
324 mwifiex_wmm_lists_empty(adapter)) {
5e6e3a92
BZ
325 if (!mwifiex_send_null_packet
326 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
327 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
328 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
329 adapter->delay_null_pkt = false;
330 adapter->ps_state = PS_STATE_SLEEP;
331 }
332 break;
333 }
334 } while (true);
335
453b0c3f 336 spin_lock_irqsave(&adapter->main_proc_lock, flags);
f73e557b
AP
337 if (!adapter->delay_main_work &&
338 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
453b0c3f 339 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
5e6e3a92 340 goto process_start;
453b0c3f 341 }
5e6e3a92 342
5e6e3a92
BZ
343 adapter->mwifiex_processing = false;
344 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
345
346exit_main_proc:
347 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
348 mwifiex_shutdown_drv(adapter);
349 return ret;
350}
601216e1 351EXPORT_SYMBOL_GPL(mwifiex_main_process);
5e6e3a92 352
5e6e3a92
BZ
353/*
354 * This function frees the adapter structure.
355 *
356 * Additionally, this closes the netlink socket, frees the timers
357 * and private structures.
358 */
359static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
360{
361 if (!adapter) {
362 pr_err("%s: adapter is NULL\n", __func__);
363 return;
364 }
365
366 mwifiex_unregister(adapter);
367 pr_debug("info: %s: free adapter\n", __func__);
368}
369
6b41f941
AK
370/*
371 * This function cancels all works in the queue and destroys
372 * the main workqueue.
373 */
374static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
375{
376 flush_workqueue(adapter->workqueue);
377 destroy_workqueue(adapter->workqueue);
378 adapter->workqueue = NULL;
6e251174
AP
379
380 if (adapter->rx_workqueue) {
381 flush_workqueue(adapter->rx_workqueue);
382 destroy_workqueue(adapter->rx_workqueue);
383 adapter->rx_workqueue = NULL;
384 }
6b41f941
AK
385}
386
5e6e3a92 387/*
59a4cc25 388 * This function gets firmware and initializes it.
5e6e3a92
BZ
389 *
390 * The main initialization steps followed are -
391 * - Download the correct firmware to card
5e6e3a92
BZ
392 * - Issue the init commands to firmware
393 */
59a4cc25 394static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
5e6e3a92 395{
bec568ff 396 int ret;
59a4cc25
AK
397 char fmt[64];
398 struct mwifiex_private *priv;
399 struct mwifiex_adapter *adapter = context;
5e6e3a92 400 struct mwifiex_fw_image fw;
c3afd99f
AK
401 struct semaphore *sem = adapter->card_sem;
402 bool init_failed = false;
68b76e99 403 struct wireless_dev *wdev;
5e6e3a92 404
59a4cc25
AK
405 if (!firmware) {
406 dev_err(adapter->dev,
407 "Failed to get firmware %s\n", adapter->fw_name);
6b41f941 408 goto err_dnld_fw;
5e6e3a92 409 }
59a4cc25
AK
410
411 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
412 adapter->firmware = firmware;
5e6e3a92
BZ
413 fw.fw_buf = (u8 *) adapter->firmware->data;
414 fw.fw_len = adapter->firmware->size;
415
4daffe35
AK
416 if (adapter->if_ops.dnld_fw)
417 ret = adapter->if_ops.dnld_fw(adapter, &fw);
418 else
419 ret = mwifiex_dnld_fw(adapter, &fw);
5e6e3a92 420 if (ret == -1)
6b41f941 421 goto err_dnld_fw;
5e6e3a92
BZ
422
423 dev_notice(adapter->dev, "WLAN FW is active\n");
424
388ec385
AK
425 if (cal_data_cfg) {
426 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
427 adapter->dev)) < 0)
428 dev_err(adapter->dev,
429 "Cal data request_firmware() failed\n");
430 }
431
232fde06 432 /* enable host interrupt after fw dnld is successful */
6b41f941
AK
433 if (adapter->if_ops.enable_int) {
434 if (adapter->if_ops.enable_int(adapter))
435 goto err_dnld_fw;
436 }
232fde06 437
5e6e3a92
BZ
438 adapter->init_wait_q_woken = false;
439 ret = mwifiex_init_fw(adapter);
440 if (ret == -1) {
6b41f941 441 goto err_init_fw;
5e6e3a92
BZ
442 } else if (!ret) {
443 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
444 goto done;
445 }
446 /* Wait for mwifiex_init to complete */
447 wait_event_interruptible(adapter->init_wait_q,
448 adapter->init_wait_q_woken);
59a4cc25 449 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
6b41f941 450 goto err_init_fw;
59a4cc25 451
d6bffe8b
AP
452 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
453 if (mwifiex_register_cfg80211(adapter)) {
59a4cc25 454 dev_err(adapter->dev, "cannot register with cfg80211\n");
d1af2943 455 goto err_init_fw;
59a4cc25
AK
456 }
457
bf354433
AP
458 if (mwifiex_init_channel_scan_gap(adapter)) {
459 dev_err(adapter->dev, "could not init channel stats table\n");
460 goto err_init_fw;
461 }
462
0013c7ce
AP
463 if (driver_mode) {
464 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
465 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
466 }
467
59a4cc25
AK
468 rtnl_lock();
469 /* Create station interface by default */
68b76e99
AK
470 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
471 NL80211_IFTYPE_STATION, NULL, NULL);
472 if (IS_ERR(wdev)) {
59a4cc25 473 dev_err(adapter->dev, "cannot create default STA interface\n");
bec568ff 474 rtnl_unlock();
59a4cc25 475 goto err_add_intf;
5e6e3a92 476 }
0013c7ce
AP
477
478 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
479 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
480 NL80211_IFTYPE_AP, NULL, NULL);
481 if (IS_ERR(wdev)) {
482 dev_err(adapter->dev, "cannot create AP interface\n");
483 rtnl_unlock();
484 goto err_add_intf;
485 }
486 }
487
488 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
489 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
490 NL80211_IFTYPE_P2P_CLIENT, NULL,
491 NULL);
492 if (IS_ERR(wdev)) {
493 dev_err(adapter->dev,
494 "cannot create p2p client interface\n");
495 rtnl_unlock();
496 goto err_add_intf;
497 }
498 }
59a4cc25
AK
499 rtnl_unlock();
500
501 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
502 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
503 goto done;
5e6e3a92 504
59a4cc25 505err_add_intf:
6b41f941
AK
506 wiphy_unregister(adapter->wiphy);
507 wiphy_free(adapter->wiphy);
59a4cc25 508err_init_fw:
232fde06
DD
509 if (adapter->if_ops.disable_int)
510 adapter->if_ops.disable_int(adapter);
6b41f941 511err_dnld_fw:
59a4cc25 512 pr_debug("info: %s: unregister device\n", __func__);
6b41f941
AK
513 if (adapter->if_ops.unregister_dev)
514 adapter->if_ops.unregister_dev(adapter);
515
7197325b 516 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
6b41f941
AK
517 pr_debug("info: %s: shutdown mwifiex\n", __func__);
518 adapter->init_wait_q_woken = false;
519
520 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
521 wait_event_interruptible(adapter->init_wait_q,
522 adapter->init_wait_q_woken);
523 }
524 adapter->surprise_removed = true;
525 mwifiex_terminate_workqueue(adapter);
c3afd99f 526 init_failed = true;
5e6e3a92 527done:
388ec385
AK
528 if (adapter->cal_data) {
529 release_firmware(adapter->cal_data);
530 adapter->cal_data = NULL;
531 }
c3afd99f
AK
532 if (adapter->firmware) {
533 release_firmware(adapter->firmware);
534 adapter->firmware = NULL;
535 }
c3afd99f
AK
536 if (init_failed)
537 mwifiex_free_adapter(adapter);
538 up(sem);
59a4cc25
AK
539 return;
540}
541
542/*
543 * This function initializes the hardware and gets firmware.
544 */
545static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
546{
547 int ret;
548
59a4cc25
AK
549 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
550 adapter->dev, GFP_KERNEL, adapter,
551 mwifiex_fw_dpc);
552 if (ret < 0)
553 dev_err(adapter->dev,
554 "request_firmware_nowait() returned error %d\n", ret);
5e6e3a92
BZ
555 return ret;
556}
557
5e6e3a92
BZ
558/*
559 * CFG802.11 network device handler for open.
560 *
561 * Starts the data queue.
562 */
563static int
564mwifiex_open(struct net_device *dev)
565{
bbea3bc4 566 netif_tx_start_all_queues(dev);
5e6e3a92
BZ
567 return 0;
568}
569
570/*
571 * CFG802.11 network device handler for close.
572 */
573static int
574mwifiex_close(struct net_device *dev)
575{
f162cac8
AK
576 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
577
578 if (priv->scan_request) {
579 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
580 cfg80211_scan_done(priv->scan_request, 1);
581 priv->scan_request = NULL;
75ab753d 582 priv->scan_aborting = true;
f162cac8
AK
583 }
584
5e6e3a92
BZ
585 return 0;
586}
587
e39faa73
SP
588/*
589 * Add buffer into wmm tx queue and queue work to transmit it.
590 */
591int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
592{
47411a06
AP
593 struct netdev_queue *txq;
594 int index = mwifiex_1d_to_wmm_queue[skb->priority];
595
596 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
597 txq = netdev_get_tx_queue(priv->netdev, index);
598 if (!netif_tx_queue_stopped(txq)) {
599 netif_tx_stop_queue(txq);
600 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
601 }
602 }
603
e39faa73 604 atomic_inc(&priv->adapter->tx_pending);
47411a06 605 mwifiex_wmm_add_buf_txqueue(priv, skb);
e39faa73 606
e39faa73
SP
607 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
608
609 return 0;
610}
611
18ca4382 612struct sk_buff *
808bbebc 613mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
18ca4382 614 struct sk_buff *skb, u8 flag, u64 *cookie)
808bbebc
AK
615{
616 struct sk_buff *orig_skb = skb;
617 struct mwifiex_txinfo *tx_info, *orig_tx_info;
618
619 skb = skb_clone(skb, GFP_ATOMIC);
620 if (skb) {
621 unsigned long flags;
622 int id;
623
624 spin_lock_irqsave(&priv->ack_status_lock, flags);
625 id = idr_alloc(&priv->ack_status_frames, orig_skb,
626 1, 0xff, GFP_ATOMIC);
627 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
628
629 if (id >= 0) {
630 tx_info = MWIFIEX_SKB_TXCB(skb);
631 tx_info->ack_frame_id = id;
632 tx_info->flags |= flag;
633 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
634 orig_tx_info->ack_frame_id = id;
635 orig_tx_info->flags |= flag;
18ca4382
AK
636
637 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
638 orig_tx_info->cookie = *cookie;
639
808bbebc
AK
640 } else if (skb_shared(skb)) {
641 kfree_skb(orig_skb);
642 } else {
643 kfree_skb(skb);
644 skb = orig_skb;
645 }
646 } else {
647 /* couldn't clone -- lose tx status ... */
648 skb = orig_skb;
649 }
650
651 return skb;
652}
653
5e6e3a92
BZ
654/*
655 * CFG802.11 network device handler for data transmission.
656 */
657static int
658mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
659{
660 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
270e58e8 661 struct sk_buff *new_skb;
5e6e3a92 662 struct mwifiex_txinfo *tx_info;
808bbebc 663 bool multicast;
5e6e3a92 664
9da9a3b2 665 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
f57c1edc 666 jiffies, priv->bss_type, priv->bss_num);
5e6e3a92
BZ
667
668 if (priv->adapter->surprise_removed) {
b53575ec 669 kfree_skb(skb);
5e6e3a92
BZ
670 priv->stats.tx_dropped++;
671 return 0;
672 }
673 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
674 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
b53575ec 675 kfree_skb(skb);
5e6e3a92
BZ
676 priv->stats.tx_dropped++;
677 return 0;
678 }
679 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
680 dev_dbg(priv->adapter->dev,
681 "data: Tx: insufficient skb headroom %d\n",
f57c1edc 682 skb_headroom(skb));
5e6e3a92
BZ
683 /* Insufficient skb headroom - allocate a new skb */
684 new_skb =
685 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
686 if (unlikely(!new_skb)) {
687 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
b53575ec 688 kfree_skb(skb);
5e6e3a92
BZ
689 priv->stats.tx_dropped++;
690 return 0;
691 }
692 kfree_skb(skb);
693 skb = new_skb;
694 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
f57c1edc 695 skb_headroom(skb));
5e6e3a92
BZ
696 }
697
698 tx_info = MWIFIEX_SKB_TXCB(skb);
d76744a9 699 memset(tx_info, 0, sizeof(*tx_info));
9da9a3b2
YAP
700 tx_info->bss_num = priv->bss_num;
701 tx_info->bss_type = priv->bss_type;
a1ed4849 702 tx_info->pkt_len = skb->len;
47411a06 703
808bbebc
AK
704 multicast = is_multicast_ether_addr(skb->data);
705
706 if (unlikely(!multicast && skb->sk &&
707 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
708 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
709 skb = mwifiex_clone_skb_for_tx_status(priv,
710 skb,
18ca4382 711 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
808bbebc 712
47411a06
AP
713 /* Record the current time the packet was queued; used to
714 * determine the amount of time the packet was queued in
715 * the driver before it was sent to the firmware.
716 * The delay is then sent along with the packet to the
717 * firmware for aggregate delay calculation for stats and
718 * MSDU lifetime expiry.
719 */
c64800e7 720 __net_timestamp(skb);
5e6e3a92 721
9927baa3
AP
722 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
723 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
724 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
725 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
726 mwifiex_tdls_check_tx(priv, skb);
727 }
728
e39faa73 729 mwifiex_queue_tx_pkt(priv, skb);
5e6e3a92
BZ
730
731 return 0;
732}
733
734/*
735 * CFG802.11 network device handler for setting MAC address.
736 */
737static int
738mwifiex_set_mac_address(struct net_device *dev, void *addr)
739{
740 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
a5ffddb7 741 struct sockaddr *hw_addr = addr;
270e58e8 742 int ret;
5e6e3a92
BZ
743
744 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
745
600f5d90 746 /* Send request to firmware */
fa0ecbb9
BZ
747 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
748 HostCmd_ACT_GEN_SET, 0, NULL, true);
600f5d90
AK
749
750 if (!ret)
751 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
752 else
f57c1edc
YAP
753 dev_err(priv->adapter->dev,
754 "set mac address failed: ret=%d\n", ret);
600f5d90 755
5e6e3a92
BZ
756 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
757
600f5d90 758 return ret;
5e6e3a92
BZ
759}
760
761/*
762 * CFG802.11 network device handler for setting multicast list.
763 */
764static void mwifiex_set_multicast_list(struct net_device *dev)
765{
766 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
600f5d90
AK
767 struct mwifiex_multicast_list mcast_list;
768
769 if (dev->flags & IFF_PROMISC) {
770 mcast_list.mode = MWIFIEX_PROMISC_MODE;
771 } else if (dev->flags & IFF_ALLMULTI ||
772 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
773 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
774 } else {
775 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
6390d885
DD
776 mcast_list.num_multicast_addr =
777 mwifiex_copy_mcast_addr(&mcast_list, dev);
600f5d90
AK
778 }
779 mwifiex_request_set_multicast_list(priv, &mcast_list);
5e6e3a92
BZ
780}
781
782/*
783 * CFG802.11 network device handler for transmission timeout.
784 */
785static void
786mwifiex_tx_timeout(struct net_device *dev)
787{
788 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
789
5e6e3a92 790 priv->num_tx_timeout++;
8908c7d5
AN
791 priv->tx_timeout_cnt++;
792 dev_err(priv->adapter->dev,
793 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
794 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
795 mwifiex_set_trans_start(dev);
796
797 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
798 priv->adapter->if_ops.card_reset) {
799 dev_err(priv->adapter->dev,
800 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
801 priv->adapter->if_ops.card_reset(priv->adapter);
802 }
5e6e3a92
BZ
803}
804
11cd07a9
XH
805void mwifiex_dump_drv_info(struct mwifiex_adapter *adapter)
806{
807 void *p;
808 char drv_version[64];
809 struct usb_card_rec *cardp;
810 struct sdio_mmc_card *sdio_card;
811 struct mwifiex_private *priv;
812 int i, idx;
813 struct netdev_queue *txq;
814 struct mwifiex_debug_info *debug_info;
815
816 if (adapter->drv_info_dump) {
817 vfree(adapter->drv_info_dump);
818 adapter->drv_info_size = 0;
819 }
820
821 dev_info(adapter->dev, "=== DRIVER INFO DUMP START===\n");
822
823 adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
824
825 if (!adapter->drv_info_dump)
826 return;
827
828 p = (char *)(adapter->drv_info_dump);
829 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
830
831 mwifiex_drv_get_driver_version(adapter, drv_version,
832 sizeof(drv_version) - 1);
833 p += sprintf(p, "driver_version = %s\n", drv_version);
834
835 if (adapter->iface_type == MWIFIEX_USB) {
836 cardp = (struct usb_card_rec *)adapter->card;
837 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
838 atomic_read(&cardp->tx_cmd_urb_pending));
839 p += sprintf(p, "tx_data_urb_pending = %d\n",
840 atomic_read(&cardp->tx_data_urb_pending));
841 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
842 atomic_read(&cardp->rx_cmd_urb_pending));
843 p += sprintf(p, "rx_data_urb_pending = %d\n",
844 atomic_read(&cardp->rx_data_urb_pending));
845 }
846
847 p += sprintf(p, "tx_pending = %d\n",
848 atomic_read(&adapter->tx_pending));
849 p += sprintf(p, "rx_pending = %d\n",
850 atomic_read(&adapter->rx_pending));
851
852 if (adapter->iface_type == MWIFIEX_SDIO) {
853 sdio_card = (struct sdio_mmc_card *)adapter->card;
854 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
855 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
856 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
857 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
858 }
859
860 for (i = 0; i < adapter->priv_num; i++) {
861 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
862 continue;
863 priv = adapter->priv[i];
864 p += sprintf(p, "\n[interface : \"%s\"]\n",
865 priv->netdev->name);
866 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
867 atomic_read(&priv->wmm_tx_pending[0]));
868 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
869 atomic_read(&priv->wmm_tx_pending[1]));
870 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
871 atomic_read(&priv->wmm_tx_pending[2]));
872 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
873 atomic_read(&priv->wmm_tx_pending[3]));
874 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
875 "Disconnected" : "Connected");
876 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
877 ? "on" : "off"));
878 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
879 txq = netdev_get_tx_queue(priv->netdev, idx);
880 p += sprintf(p, "tx queue %d:%s ", idx,
881 netif_tx_queue_stopped(txq) ?
882 "stopped" : "started");
883 }
884 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
885 priv->netdev->name, priv->num_tx_timeout);
886 }
887
809c6ea8
XH
888 if (adapter->iface_type == MWIFIEX_SDIO) {
889 p += sprintf(p, "\n=== SDIO register DUMP===\n");
890 if (adapter->if_ops.reg_dump)
891 p += adapter->if_ops.reg_dump(adapter, p);
892 }
893
11cd07a9
XH
894 p += sprintf(p, "\n=== MORE DEBUG INFORMATION\n");
895 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
896 if (debug_info) {
897 for (i = 0; i < adapter->priv_num; i++) {
898 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
899 continue;
900 priv = adapter->priv[i];
901 mwifiex_get_debug_info(priv, debug_info);
902 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
903 break;
904 }
905 kfree(debug_info);
906 }
907
908 adapter->drv_info_size = p - adapter->drv_info_dump;
909 dev_info(adapter->dev, "=== DRIVER INFO DUMP END===\n");
910}
911EXPORT_SYMBOL_GPL(mwifiex_dump_drv_info);
912
5e6e3a92
BZ
913/*
914 * CFG802.11 network device handler for statistics retrieval.
915 */
916static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
917{
918 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
919
920 return &priv->stats;
921}
922
47411a06 923static u16
f663dd9a 924mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
99932d4f 925 void *accel_priv, select_queue_fallback_t fallback)
47411a06 926{
fa9ffc74 927 skb->priority = cfg80211_classify8021d(skb, NULL);
47411a06
AP
928 return mwifiex_1d_to_wmm_queue[skb->priority];
929}
930
5e6e3a92
BZ
931/* Network device handlers */
932static const struct net_device_ops mwifiex_netdev_ops = {
933 .ndo_open = mwifiex_open,
934 .ndo_stop = mwifiex_close,
935 .ndo_start_xmit = mwifiex_hard_start_xmit,
936 .ndo_set_mac_address = mwifiex_set_mac_address,
937 .ndo_tx_timeout = mwifiex_tx_timeout,
938 .ndo_get_stats = mwifiex_get_stats,
afc4b13d 939 .ndo_set_rx_mode = mwifiex_set_multicast_list,
47411a06 940 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
5e6e3a92
BZ
941};
942
943/*
944 * This function initializes the private structure parameters.
945 *
946 * The following wait queues are initialized -
947 * - IOCTL wait queue
948 * - Command wait queue
949 * - Statistics wait queue
950 *
951 * ...and the following default parameters are set -
952 * - Current key index : Set to 0
953 * - Rate index : Set to auto
954 * - Media connected : Set to disconnected
955 * - Adhoc link sensed : Set to false
956 * - Nick name : Set to null
957 * - Number of Tx timeout : Set to 0
958 * - Device address : Set to current address
cbf6e055 959 * - Rx histogram statistc : Set to 0
5e6e3a92
BZ
960 *
961 * In addition, the CFG80211 work queue is also created.
962 */
93a1df48
YAP
963void mwifiex_init_priv_params(struct mwifiex_private *priv,
964 struct net_device *dev)
5e6e3a92
BZ
965{
966 dev->netdev_ops = &mwifiex_netdev_ops;
f16fdc9d 967 dev->destructor = free_netdev;
5e6e3a92 968 /* Initialize private structure */
5e6e3a92
BZ
969 priv->current_key_index = 0;
970 priv->media_connected = false;
971 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
ede98bfa
AP
972 memset(priv->mgmt_ie, 0,
973 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
f31acabe
AP
974 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
975 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
976 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
977 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
5e6e3a92 978 priv->num_tx_timeout = 0;
5e6e3a92 979 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
cbf6e055
XH
980
981 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
982 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
983 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
984 if (priv->hist_data)
985 mwifiex_hist_data_reset(priv);
986 }
5e6e3a92
BZ
987}
988
5e6e3a92
BZ
989/*
990 * This function check if command is pending.
991 */
992int is_command_pending(struct mwifiex_adapter *adapter)
993{
994 unsigned long flags;
995 int is_cmd_pend_q_empty;
996
997 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
998 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
999 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1000
1001 return !is_cmd_pend_q_empty;
1002}
1003
6e251174
AP
1004/*
1005 * This is the RX work queue function.
1006 *
1007 * It handles the RX operations.
1008 */
1009static void mwifiex_rx_work_queue(struct work_struct *work)
1010{
1011 struct mwifiex_adapter *adapter =
1012 container_of(work, struct mwifiex_adapter, rx_work);
1013
1014 if (adapter->surprise_removed)
1015 return;
1016 mwifiex_process_rx(adapter);
1017}
1018
5e6e3a92
BZ
1019/*
1020 * This is the main work queue function.
1021 *
1022 * It handles the main process, which in turn handles the complete
1023 * driver operations.
1024 */
1025static void mwifiex_main_work_queue(struct work_struct *work)
1026{
1027 struct mwifiex_adapter *adapter =
1028 container_of(work, struct mwifiex_adapter, main_work);
1029
1030 if (adapter->surprise_removed)
1031 return;
1032 mwifiex_main_process(adapter);
1033}
1034
5e6e3a92
BZ
1035/*
1036 * This function adds the card.
1037 *
1038 * This function follows the following major steps to set up the device -
1039 * - Initialize software. This includes probing the card, registering
1040 * the interface operations table, and allocating/initializing the
1041 * adapter structure
1042 * - Set up the netlink socket
1043 * - Create and start the main work queue
1044 * - Register the device
1045 * - Initialize firmware and hardware
1046 * - Add logical interfaces
1047 */
1048int
1049mwifiex_add_card(void *card, struct semaphore *sem,
d930faee 1050 struct mwifiex_if_ops *if_ops, u8 iface_type)
5e6e3a92 1051{
2be7859f 1052 struct mwifiex_adapter *adapter;
5e6e3a92
BZ
1053
1054 if (down_interruptible(sem))
1055 goto exit_sem_err;
1056
93a1df48 1057 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
5e6e3a92
BZ
1058 pr_err("%s: software init failed\n", __func__);
1059 goto err_init_sw;
1060 }
1061
d930faee 1062 adapter->iface_type = iface_type;
6b41f941 1063 adapter->card_sem = sem;
d930faee 1064
5e6e3a92 1065 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
5e6e3a92
BZ
1066 adapter->surprise_removed = false;
1067 init_waitqueue_head(&adapter->init_wait_q);
1068 adapter->is_suspended = false;
1069 adapter->hs_activated = false;
1070 init_waitqueue_head(&adapter->hs_activate_wait_q);
600f5d90 1071 init_waitqueue_head(&adapter->cmd_wait_q.wait);
600f5d90 1072 adapter->cmd_wait_q.status = 0;
efaaa8b8 1073 adapter->scan_wait_q_woken = false;
5e6e3a92 1074
ec4a16b4 1075 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
6e251174
AP
1076 adapter->rx_work_enabled = true;
1077 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1078 }
1079
448a71cc
AK
1080 adapter->workqueue =
1081 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1082 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
5e6e3a92
BZ
1083 if (!adapter->workqueue)
1084 goto err_kmalloc;
1085
1086 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
6e251174
AP
1087
1088 if (adapter->rx_work_enabled) {
1089 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1090 WQ_HIGHPRI |
1091 WQ_MEM_RECLAIM |
1092 WQ_UNBOUND, 1);
1093 if (!adapter->rx_workqueue)
1094 goto err_kmalloc;
1095
1096 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1097 }
1098
92c2538f
AK
1099 if (adapter->if_ops.iface_work)
1100 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
5e6e3a92
BZ
1101
1102 /* Register the device. Fill up the private data structure with relevant
232fde06 1103 information from the card. */
5e6e3a92
BZ
1104 if (adapter->if_ops.register_dev(adapter)) {
1105 pr_err("%s: failed to register mwifiex device\n", __func__);
1106 goto err_registerdev;
1107 }
1108
5e6e3a92
BZ
1109 if (mwifiex_init_hw_fw(adapter)) {
1110 pr_err("%s: firmware init failed\n", __func__);
1111 goto err_init_fw;
1112 }
2be7859f 1113
5e6e3a92
BZ
1114 return 0;
1115
5e6e3a92 1116err_init_fw:
5e6e3a92 1117 pr_debug("info: %s: unregister device\n", __func__);
4daffe35
AK
1118 if (adapter->if_ops.unregister_dev)
1119 adapter->if_ops.unregister_dev(adapter);
7197325b 1120 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
5e6e3a92
BZ
1121 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1122 adapter->init_wait_q_woken = false;
636c4598
YAP
1123
1124 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
5e6e3a92
BZ
1125 wait_event_interruptible(adapter->init_wait_q,
1126 adapter->init_wait_q_woken);
1127 }
6b41f941
AK
1128err_registerdev:
1129 adapter->surprise_removed = true;
1130 mwifiex_terminate_workqueue(adapter);
1131err_kmalloc:
5e6e3a92
BZ
1132 mwifiex_free_adapter(adapter);
1133
1134err_init_sw:
1135 up(sem);
1136
1137exit_sem_err:
1138 return -1;
1139}
1140EXPORT_SYMBOL_GPL(mwifiex_add_card);
1141
1142/*
1143 * This function removes the card.
1144 *
1145 * This function follows the following major steps to remove the device -
1146 * - Stop data traffic
1147 * - Shutdown firmware
1148 * - Remove the logical interfaces
1149 * - Terminate the work queue
1150 * - Unregister the device
1151 * - Free the adapter structure
1152 */
1153int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1154{
1155 struct mwifiex_private *priv = NULL;
5e6e3a92
BZ
1156 int i;
1157
1158 if (down_interruptible(sem))
1159 goto exit_sem_err;
1160
1161 if (!adapter)
1162 goto exit_remove;
1163
232fde06
DD
1164 /* We can no longer handle interrupts once we start doing the teardown
1165 * below. */
1166 if (adapter->if_ops.disable_int)
1167 adapter->if_ops.disable_int(adapter);
1168
5e6e3a92
BZ
1169 adapter->surprise_removed = true;
1170
9d2e85e0
MY
1171 mwifiex_terminate_workqueue(adapter);
1172
5e6e3a92
BZ
1173 /* Stop data */
1174 for (i = 0; i < adapter->priv_num; i++) {
1175 priv = adapter->priv[i];
93a1df48 1176 if (priv && priv->netdev) {
47411a06 1177 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
5e6e3a92
BZ
1178 if (netif_carrier_ok(priv->netdev))
1179 netif_carrier_off(priv->netdev);
1180 }
1181 }
1182
1183 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
1184 adapter->init_wait_q_woken = false;
636c4598
YAP
1185
1186 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
5e6e3a92
BZ
1187 wait_event_interruptible(adapter->init_wait_q,
1188 adapter->init_wait_q_woken);
1189 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
1190 if (atomic_read(&adapter->rx_pending) ||
1191 atomic_read(&adapter->tx_pending) ||
600f5d90 1192 atomic_read(&adapter->cmd_pending)) {
5e6e3a92 1193 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
600f5d90 1194 "cmd_pending=%d\n",
5e6e3a92
BZ
1195 atomic_read(&adapter->rx_pending),
1196 atomic_read(&adapter->tx_pending),
600f5d90 1197 atomic_read(&adapter->cmd_pending));
5e6e3a92
BZ
1198 }
1199
93a1df48
YAP
1200 for (i = 0; i < adapter->priv_num; i++) {
1201 priv = adapter->priv[i];
1202
1203 if (!priv)
1204 continue;
1205
1206 rtnl_lock();
2da8cbf8 1207 if (priv->wdev && priv->netdev)
84efbb84 1208 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
93a1df48
YAP
1209 rtnl_unlock();
1210 }
1211
c2868ade
AK
1212 wiphy_unregister(adapter->wiphy);
1213 wiphy_free(adapter->wiphy);
64b05e2f 1214
5e6e3a92
BZ
1215 /* Unregister device */
1216 dev_dbg(adapter->dev, "info: unregister device\n");
4daffe35
AK
1217 if (adapter->if_ops.unregister_dev)
1218 adapter->if_ops.unregister_dev(adapter);
5e6e3a92
BZ
1219 /* Free adapter structure */
1220 dev_dbg(adapter->dev, "info: free adapter\n");
1221 mwifiex_free_adapter(adapter);
1222
1223exit_remove:
1224 up(sem);
1225exit_sem_err:
1226 return 0;
1227}
1228EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1229
1230/*
1231 * This function initializes the module.
1232 *
1233 * The debug FS is also initialized if configured.
1234 */
1235static int
1236mwifiex_init_module(void)
1237{
1238#ifdef CONFIG_DEBUG_FS
1239 mwifiex_debugfs_init();
1240#endif
1241 return 0;
1242}
1243
1244/*
1245 * This function cleans up the module.
1246 *
1247 * The debug FS is removed if available.
1248 */
1249static void
1250mwifiex_cleanup_module(void)
1251{
1252#ifdef CONFIG_DEBUG_FS
1253 mwifiex_debugfs_remove();
1254#endif
1255}
1256
1257module_init(mwifiex_init_module);
1258module_exit(mwifiex_cleanup_module);
1259
1260MODULE_AUTHOR("Marvell International Ltd.");
1261MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1262MODULE_VERSION(VERSION);
1263MODULE_LICENSE("GPL v2");
This page took 0.376569 seconds and 5 git commands to generate.