1 /*
2  * NXP Wireless LAN device driver: major functions
3  *
4  * Copyright 2011-2020 NXP
5  *
6  * This software file (the "File") is distributed by NXP
7  * 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 <linux/suspend.h>
21 
22 #include "main.h"
23 #include "wmm.h"
24 #include "cfg80211.h"
25 #include "11n.h"
26 
27 #define VERSION	"1.0"
28 #define MFG_FIRMWARE	"mwifiex_mfg.bin"
29 
30 static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
31 module_param(debug_mask, uint, 0);
32 MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
33 
34 const char driver_version[] = "mwifiex " VERSION " (%s) ";
35 static char *cal_data_cfg;
36 module_param(cal_data_cfg, charp, 0);
37 
38 static unsigned short driver_mode;
39 module_param(driver_mode, ushort, 0);
40 MODULE_PARM_DESC(driver_mode,
41 		 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
42 
43 bool mfg_mode;
44 module_param(mfg_mode, bool, 0);
45 MODULE_PARM_DESC(mfg_mode, "manufacturing mode enable:1, disable:0");
46 
47 bool aggr_ctrl;
48 module_param(aggr_ctrl, bool, 0000);
49 MODULE_PARM_DESC(aggr_ctrl, "usb tx aggregation enable:1, disable:0");
50 
51 const u16 mwifiex_1d_to_wmm_queue[8] = { 1, 0, 0, 1, 2, 2, 3, 3 };
52 
53 /*
54  * This function registers the device and performs all the necessary
55  * initializations.
56  *
57  * The following initialization operations are performed -
58  *      - Allocate adapter structure
59  *      - Save interface specific operations table in adapter
60  *      - Call interface specific initialization routine
61  *      - Allocate private structures
62  *      - Set default adapter structure parameters
63  *      - Initialize locks
64  *
65  * In case of any errors during inittialization, this function also ensures
66  * proper cleanup before exiting.
67  */
68 static int mwifiex_register(void *card, struct device *dev,
69 			    struct mwifiex_if_ops *if_ops, void **padapter)
70 {
71 	struct mwifiex_adapter *adapter;
72 	int i;
73 
74 	adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
75 	if (!adapter)
76 		return -ENOMEM;
77 
78 	*padapter = adapter;
79 	adapter->dev = dev;
80 	adapter->card = card;
81 
82 	/* Save interface specific operations in adapter */
83 	memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
84 	adapter->debug_mask = debug_mask;
85 
86 	/* card specific initialization has been deferred until now .. */
87 	if (adapter->if_ops.init_if)
88 		if (adapter->if_ops.init_if(adapter))
89 			goto error;
90 
91 	adapter->priv_num = 0;
92 
93 	for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
94 		/* Allocate memory for private structure */
95 		adapter->priv[i] =
96 			kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
97 		if (!adapter->priv[i])
98 			goto error;
99 
100 		adapter->priv[i]->adapter = adapter;
101 		adapter->priv_num++;
102 	}
103 	mwifiex_init_lock_list(adapter);
104 
105 	timer_setup(&adapter->cmd_timer, mwifiex_cmd_timeout_func, 0);
106 
107 	return 0;
108 
109 error:
110 	mwifiex_dbg(adapter, ERROR,
111 		    "info: leave mwifiex_register with error\n");
112 
113 	for (i = 0; i < adapter->priv_num; i++)
114 		kfree(adapter->priv[i]);
115 
116 	kfree(adapter);
117 
118 	return -1;
119 }
120 
121 /*
122  * This function unregisters the device and performs all the necessary
123  * cleanups.
124  *
125  * The following cleanup operations are performed -
126  *      - Free the timers
127  *      - Free beacon buffers
128  *      - Free private structures
129  *      - Free adapter structure
130  */
131 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
132 {
133 	s32 i;
134 
135 	if (adapter->if_ops.cleanup_if)
136 		adapter->if_ops.cleanup_if(adapter);
137 
138 	del_timer_sync(&adapter->cmd_timer);
139 
140 	/* Free private structures */
141 	for (i = 0; i < adapter->priv_num; i++) {
142 		if (adapter->priv[i]) {
143 			mwifiex_free_curr_bcn(adapter->priv[i]);
144 			kfree(adapter->priv[i]);
145 		}
146 	}
147 
148 	if (adapter->nd_info) {
149 		for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
150 			kfree(adapter->nd_info->matches[i]);
151 		kfree(adapter->nd_info);
152 		adapter->nd_info = NULL;
153 	}
154 
155 	kfree(adapter->regd);
156 
157 	kfree(adapter);
158 	return 0;
159 }
160 
161 void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
162 {
163 	unsigned long flags;
164 
165 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
166 	if (adapter->mwifiex_processing) {
167 		adapter->more_task_flag = true;
168 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
169 	} else {
170 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
171 		queue_work(adapter->workqueue, &adapter->main_work);
172 	}
173 }
174 EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
175 
176 static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
177 {
178 	spin_lock_bh(&adapter->rx_proc_lock);
179 	if (adapter->rx_processing) {
180 		spin_unlock_bh(&adapter->rx_proc_lock);
181 	} else {
182 		spin_unlock_bh(&adapter->rx_proc_lock);
183 		queue_work(adapter->rx_workqueue, &adapter->rx_work);
184 	}
185 }
186 
187 static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
188 {
189 	struct sk_buff *skb;
190 	struct mwifiex_rxinfo *rx_info;
191 
192 	spin_lock_bh(&adapter->rx_proc_lock);
193 	if (adapter->rx_processing || adapter->rx_locked) {
194 		spin_unlock_bh(&adapter->rx_proc_lock);
195 		goto exit_rx_proc;
196 	} else {
197 		adapter->rx_processing = true;
198 		spin_unlock_bh(&adapter->rx_proc_lock);
199 	}
200 
201 	/* Check for Rx data */
202 	while ((skb = skb_dequeue(&adapter->rx_data_q))) {
203 		atomic_dec(&adapter->rx_pending);
204 		if ((adapter->delay_main_work ||
205 		     adapter->iface_type == MWIFIEX_USB) &&
206 		    (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
207 			if (adapter->if_ops.submit_rem_rx_urbs)
208 				adapter->if_ops.submit_rem_rx_urbs(adapter);
209 			adapter->delay_main_work = false;
210 			mwifiex_queue_main_work(adapter);
211 		}
212 		rx_info = MWIFIEX_SKB_RXCB(skb);
213 		if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
214 			if (adapter->if_ops.deaggr_pkt)
215 				adapter->if_ops.deaggr_pkt(adapter, skb);
216 			dev_kfree_skb_any(skb);
217 		} else {
218 			mwifiex_handle_rx_packet(adapter, skb);
219 		}
220 	}
221 	spin_lock_bh(&adapter->rx_proc_lock);
222 	adapter->rx_processing = false;
223 	spin_unlock_bh(&adapter->rx_proc_lock);
224 
225 exit_rx_proc:
226 	return 0;
227 }
228 
229 /*
230  * The main process.
231  *
232  * This function is the main procedure of the driver and handles various driver
233  * operations. It runs in a loop and provides the core functionalities.
234  *
235  * The main responsibilities of this function are -
236  *      - Ensure concurrency control
237  *      - Handle pending interrupts and call interrupt handlers
238  *      - Wake up the card if required
239  *      - Handle command responses and call response handlers
240  *      - Handle events and call event handlers
241  *      - Execute pending commands
242  *      - Transmit pending data packets
243  */
244 int mwifiex_main_process(struct mwifiex_adapter *adapter)
245 {
246 	int ret = 0;
247 	unsigned long flags;
248 
249 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
250 
251 	/* Check if already processing */
252 	if (adapter->mwifiex_processing || adapter->main_locked) {
253 		adapter->more_task_flag = true;
254 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
255 		return 0;
256 	} else {
257 		adapter->mwifiex_processing = true;
258 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
259 	}
260 process_start:
261 	do {
262 		if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
263 			break;
264 
265 		/* For non-USB interfaces, If we process interrupts first, it
266 		 * would increase RX pending even further. Avoid this by
267 		 * checking if rx_pending has crossed high threshold and
268 		 * schedule rx work queue and then process interrupts.
269 		 * For USB interface, there are no interrupts. We already have
270 		 * HIGH_RX_PENDING check in usb.c
271 		 */
272 		if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
273 		    adapter->iface_type != MWIFIEX_USB) {
274 			adapter->delay_main_work = true;
275 			mwifiex_queue_rx_work(adapter);
276 			break;
277 		}
278 
279 		/* Handle pending interrupt if any */
280 		if (adapter->int_status) {
281 			if (adapter->hs_activated)
282 				mwifiex_process_hs_config(adapter);
283 			if (adapter->if_ops.process_int_status)
284 				adapter->if_ops.process_int_status(adapter);
285 		}
286 
287 		if (adapter->rx_work_enabled && adapter->data_received)
288 			mwifiex_queue_rx_work(adapter);
289 
290 		/* Need to wake up the card ? */
291 		if ((adapter->ps_state == PS_STATE_SLEEP) &&
292 		    (adapter->pm_wakeup_card_req &&
293 		     !adapter->pm_wakeup_fw_try) &&
294 		    (is_command_pending(adapter) ||
295 		     !skb_queue_empty(&adapter->tx_data_q) ||
296 		     !mwifiex_bypass_txlist_empty(adapter) ||
297 		     !mwifiex_wmm_lists_empty(adapter))) {
298 			adapter->pm_wakeup_fw_try = true;
299 			mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
300 			adapter->if_ops.wakeup(adapter);
301 			continue;
302 		}
303 
304 		if (IS_CARD_RX_RCVD(adapter)) {
305 			adapter->data_received = false;
306 			adapter->pm_wakeup_fw_try = false;
307 			del_timer(&adapter->wakeup_timer);
308 			if (adapter->ps_state == PS_STATE_SLEEP)
309 				adapter->ps_state = PS_STATE_AWAKE;
310 		} else {
311 			/* We have tried to wakeup the card already */
312 			if (adapter->pm_wakeup_fw_try)
313 				break;
314 			if (adapter->ps_state == PS_STATE_PRE_SLEEP)
315 				mwifiex_check_ps_cond(adapter);
316 
317 			if (adapter->ps_state != PS_STATE_AWAKE)
318 				break;
319 			if (adapter->tx_lock_flag) {
320 				if (adapter->iface_type == MWIFIEX_USB) {
321 					if (!adapter->usb_mc_setup)
322 						break;
323 				} else
324 					break;
325 			}
326 
327 			if ((!adapter->scan_chan_gap_enabled &&
328 			     adapter->scan_processing) || adapter->data_sent ||
329 			     mwifiex_is_tdls_chan_switching
330 			     (mwifiex_get_priv(adapter,
331 					       MWIFIEX_BSS_ROLE_STA)) ||
332 			    (mwifiex_wmm_lists_empty(adapter) &&
333 			     mwifiex_bypass_txlist_empty(adapter) &&
334 			     skb_queue_empty(&adapter->tx_data_q))) {
335 				if (adapter->cmd_sent || adapter->curr_cmd ||
336 					!mwifiex_is_send_cmd_allowed
337 						(mwifiex_get_priv(adapter,
338 						MWIFIEX_BSS_ROLE_STA)) ||
339 				    (!is_command_pending(adapter)))
340 					break;
341 			}
342 		}
343 
344 		/* Check for event */
345 		if (adapter->event_received) {
346 			adapter->event_received = false;
347 			mwifiex_process_event(adapter);
348 		}
349 
350 		/* Check for Cmd Resp */
351 		if (adapter->cmd_resp_received) {
352 			adapter->cmd_resp_received = false;
353 			mwifiex_process_cmdresp(adapter);
354 
355 			/* call mwifiex back when init_fw is done */
356 			if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
357 				adapter->hw_status = MWIFIEX_HW_STATUS_READY;
358 				mwifiex_init_fw_complete(adapter);
359 			}
360 		}
361 
362 		/* Check if we need to confirm Sleep Request
363 		   received previously */
364 		if (adapter->ps_state == PS_STATE_PRE_SLEEP)
365 			mwifiex_check_ps_cond(adapter);
366 
367 		/* * The ps_state may have been changed during processing of
368 		 * Sleep Request event.
369 		 */
370 		if ((adapter->ps_state == PS_STATE_SLEEP) ||
371 		    (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
372 		    (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
373 			continue;
374 		}
375 
376 		if (adapter->tx_lock_flag) {
377 			if (adapter->iface_type == MWIFIEX_USB) {
378 				if (!adapter->usb_mc_setup)
379 					continue;
380 			} else
381 				continue;
382 		}
383 
384 		if (!adapter->cmd_sent && !adapter->curr_cmd &&
385 		    mwifiex_is_send_cmd_allowed
386 		    (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
387 			if (mwifiex_exec_next_cmd(adapter) == -1) {
388 				ret = -1;
389 				break;
390 			}
391 		}
392 
393 		/** If USB Multi channel setup ongoing,
394 		 *  wait for ready to tx data.
395 		 */
396 		if (adapter->iface_type == MWIFIEX_USB &&
397 		    adapter->usb_mc_setup)
398 			continue;
399 
400 		if ((adapter->scan_chan_gap_enabled ||
401 		     !adapter->scan_processing) &&
402 		    !adapter->data_sent &&
403 		    !skb_queue_empty(&adapter->tx_data_q)) {
404 			if (adapter->hs_activated_manually) {
405 				mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
406 						  MWIFIEX_ASYNC_CMD);
407 				adapter->hs_activated_manually = false;
408 			}
409 
410 			mwifiex_process_tx_queue(adapter);
411 			if (adapter->hs_activated) {
412 				clear_bit(MWIFIEX_IS_HS_CONFIGURED,
413 					  &adapter->work_flags);
414 				mwifiex_hs_activated_event
415 					(mwifiex_get_priv
416 					(adapter, MWIFIEX_BSS_ROLE_ANY),
417 					false);
418 			}
419 		}
420 
421 		if ((adapter->scan_chan_gap_enabled ||
422 		     !adapter->scan_processing) &&
423 		    !adapter->data_sent &&
424 		    !mwifiex_bypass_txlist_empty(adapter) &&
425 		    !mwifiex_is_tdls_chan_switching
426 			(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
427 			if (adapter->hs_activated_manually) {
428 				mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
429 						  MWIFIEX_ASYNC_CMD);
430 				adapter->hs_activated_manually = false;
431 			}
432 
433 			mwifiex_process_bypass_tx(adapter);
434 			if (adapter->hs_activated) {
435 				clear_bit(MWIFIEX_IS_HS_CONFIGURED,
436 					  &adapter->work_flags);
437 				mwifiex_hs_activated_event
438 					(mwifiex_get_priv
439 					 (adapter, MWIFIEX_BSS_ROLE_ANY),
440 					 false);
441 			}
442 		}
443 
444 		if ((adapter->scan_chan_gap_enabled ||
445 		     !adapter->scan_processing) &&
446 		    !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
447 		    !mwifiex_is_tdls_chan_switching
448 			(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
449 			if (adapter->hs_activated_manually) {
450 				mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY),
451 						  MWIFIEX_ASYNC_CMD);
452 				adapter->hs_activated_manually = false;
453 			}
454 
455 			mwifiex_wmm_process_tx(adapter);
456 			if (adapter->hs_activated) {
457 				clear_bit(MWIFIEX_IS_HS_CONFIGURED,
458 					  &adapter->work_flags);
459 				mwifiex_hs_activated_event
460 					(mwifiex_get_priv
461 					 (adapter, MWIFIEX_BSS_ROLE_ANY),
462 					 false);
463 			}
464 		}
465 
466 		if (adapter->delay_null_pkt && !adapter->cmd_sent &&
467 		    !adapter->curr_cmd && !is_command_pending(adapter) &&
468 		    (mwifiex_wmm_lists_empty(adapter) &&
469 		     mwifiex_bypass_txlist_empty(adapter) &&
470 		     skb_queue_empty(&adapter->tx_data_q))) {
471 			if (!mwifiex_send_null_packet
472 			    (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
473 			     MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
474 			     MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
475 				adapter->delay_null_pkt = false;
476 				adapter->ps_state = PS_STATE_SLEEP;
477 			}
478 			break;
479 		}
480 	} while (true);
481 
482 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
483 	if (adapter->more_task_flag) {
484 		adapter->more_task_flag = false;
485 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
486 		goto process_start;
487 	}
488 	adapter->mwifiex_processing = false;
489 	spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
490 
491 	return ret;
492 }
493 EXPORT_SYMBOL_GPL(mwifiex_main_process);
494 
495 /*
496  * This function frees the adapter structure.
497  *
498  * Additionally, this closes the netlink socket, frees the timers
499  * and private structures.
500  */
501 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
502 {
503 	if (!adapter) {
504 		pr_err("%s: adapter is NULL\n", __func__);
505 		return;
506 	}
507 
508 	mwifiex_unregister(adapter);
509 	pr_debug("info: %s: free adapter\n", __func__);
510 }
511 
512 /*
513  * This function cancels all works in the queue and destroys
514  * the main workqueue.
515  */
516 static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
517 {
518 	if (adapter->workqueue) {
519 		destroy_workqueue(adapter->workqueue);
520 		adapter->workqueue = NULL;
521 	}
522 
523 	if (adapter->rx_workqueue) {
524 		destroy_workqueue(adapter->rx_workqueue);
525 		adapter->rx_workqueue = NULL;
526 	}
527 }
528 
529 /*
530  * This function gets firmware and initializes it.
531  *
532  * The main initialization steps followed are -
533  *      - Download the correct firmware to card
534  *      - Issue the init commands to firmware
535  */
536 static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context)
537 {
538 	int ret;
539 	char fmt[64];
540 	struct mwifiex_adapter *adapter = context;
541 	struct mwifiex_fw_image fw;
542 	bool init_failed = false;
543 	struct wireless_dev *wdev;
544 	struct completion *fw_done = adapter->fw_done;
545 
546 	if (!firmware) {
547 		mwifiex_dbg(adapter, ERROR,
548 			    "Failed to get firmware %s\n", adapter->fw_name);
549 		goto err_dnld_fw;
550 	}
551 
552 	memset(&fw, 0, sizeof(struct mwifiex_fw_image));
553 	adapter->firmware = firmware;
554 	fw.fw_buf = (u8 *) adapter->firmware->data;
555 	fw.fw_len = adapter->firmware->size;
556 
557 	if (adapter->if_ops.dnld_fw) {
558 		ret = adapter->if_ops.dnld_fw(adapter, &fw);
559 	} else {
560 		ret = mwifiex_dnld_fw(adapter, &fw);
561 	}
562 
563 	if (ret == -1)
564 		goto err_dnld_fw;
565 
566 	mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
567 
568 	if (cal_data_cfg) {
569 		if ((request_firmware(&adapter->cal_data, cal_data_cfg,
570 				      adapter->dev)) < 0)
571 			mwifiex_dbg(adapter, ERROR,
572 				    "Cal data request_firmware() failed\n");
573 	}
574 
575 	/* enable host interrupt after fw dnld is successful */
576 	if (adapter->if_ops.enable_int) {
577 		if (adapter->if_ops.enable_int(adapter))
578 			goto err_dnld_fw;
579 	}
580 
581 	adapter->init_wait_q_woken = false;
582 	ret = mwifiex_init_fw(adapter);
583 	if (ret == -1) {
584 		goto err_init_fw;
585 	} else if (!ret) {
586 		adapter->hw_status = MWIFIEX_HW_STATUS_READY;
587 		goto done;
588 	}
589 	/* Wait for mwifiex_init to complete */
590 	if (!adapter->mfg_mode) {
591 		wait_event_interruptible(adapter->init_wait_q,
592 					 adapter->init_wait_q_woken);
593 		if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
594 			goto err_init_fw;
595 	}
596 
597 	if (!adapter->wiphy) {
598 		if (mwifiex_register_cfg80211(adapter)) {
599 			mwifiex_dbg(adapter, ERROR,
600 				    "cannot register with cfg80211\n");
601 			goto err_init_fw;
602 		}
603 	}
604 
605 	if (mwifiex_init_channel_scan_gap(adapter)) {
606 		mwifiex_dbg(adapter, ERROR,
607 			    "could not init channel stats table\n");
608 		goto err_init_chan_scan;
609 	}
610 
611 	if (driver_mode) {
612 		driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
613 		driver_mode |= MWIFIEX_DRIVER_MODE_STA;
614 	}
615 
616 	rtnl_lock();
617 	wiphy_lock(adapter->wiphy);
618 	/* Create station interface by default */
619 	wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
620 					NL80211_IFTYPE_STATION, NULL);
621 	if (IS_ERR(wdev)) {
622 		mwifiex_dbg(adapter, ERROR,
623 			    "cannot create default STA interface\n");
624 		wiphy_unlock(adapter->wiphy);
625 		rtnl_unlock();
626 		goto err_add_intf;
627 	}
628 
629 	if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
630 		wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
631 						NL80211_IFTYPE_AP, NULL);
632 		if (IS_ERR(wdev)) {
633 			mwifiex_dbg(adapter, ERROR,
634 				    "cannot create AP interface\n");
635 			wiphy_unlock(adapter->wiphy);
636 			rtnl_unlock();
637 			goto err_add_intf;
638 		}
639 	}
640 
641 	if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
642 		wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
643 						NL80211_IFTYPE_P2P_CLIENT, NULL);
644 		if (IS_ERR(wdev)) {
645 			mwifiex_dbg(adapter, ERROR,
646 				    "cannot create p2p client interface\n");
647 			wiphy_unlock(adapter->wiphy);
648 			rtnl_unlock();
649 			goto err_add_intf;
650 		}
651 	}
652 	wiphy_unlock(adapter->wiphy);
653 	rtnl_unlock();
654 
655 	mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
656 	mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
657 	adapter->is_up = true;
658 	goto done;
659 
660 err_add_intf:
661 	vfree(adapter->chan_stats);
662 err_init_chan_scan:
663 	wiphy_unregister(adapter->wiphy);
664 	wiphy_free(adapter->wiphy);
665 err_init_fw:
666 	if (adapter->if_ops.disable_int)
667 		adapter->if_ops.disable_int(adapter);
668 err_dnld_fw:
669 	mwifiex_dbg(adapter, ERROR,
670 		    "info: %s: unregister device\n", __func__);
671 	if (adapter->if_ops.unregister_dev)
672 		adapter->if_ops.unregister_dev(adapter);
673 
674 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
675 	mwifiex_terminate_workqueue(adapter);
676 
677 	if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
678 		pr_debug("info: %s: shutdown mwifiex\n", __func__);
679 		mwifiex_shutdown_drv(adapter);
680 		mwifiex_free_cmd_buffers(adapter);
681 	}
682 
683 	init_failed = true;
684 done:
685 	if (adapter->cal_data) {
686 		release_firmware(adapter->cal_data);
687 		adapter->cal_data = NULL;
688 	}
689 	if (adapter->firmware) {
690 		release_firmware(adapter->firmware);
691 		adapter->firmware = NULL;
692 	}
693 	if (init_failed) {
694 		if (adapter->irq_wakeup >= 0)
695 			device_init_wakeup(adapter->dev, false);
696 		mwifiex_free_adapter(adapter);
697 	}
698 	/* Tell all current and future waiters we're finished */
699 	complete_all(fw_done);
700 
701 	return init_failed ? -EIO : 0;
702 }
703 
704 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
705 {
706 	_mwifiex_fw_dpc(firmware, context);
707 }
708 
709 /*
710  * This function gets the firmware and (if called asynchronously) kicks off the
711  * HW init when done.
712  */
713 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter,
714 			      bool req_fw_nowait)
715 {
716 	int ret;
717 
718 	/* Override default firmware with manufacturing one if
719 	 * manufacturing mode is enabled
720 	 */
721 	if (mfg_mode) {
722 		if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
723 			    sizeof(adapter->fw_name)) >=
724 			    sizeof(adapter->fw_name)) {
725 			pr_err("%s: fw_name too long!\n", __func__);
726 			return -1;
727 		}
728 	}
729 
730 	if (req_fw_nowait) {
731 		ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
732 					      adapter->dev, GFP_KERNEL, adapter,
733 					      mwifiex_fw_dpc);
734 	} else {
735 		ret = request_firmware(&adapter->firmware,
736 				       adapter->fw_name,
737 				       adapter->dev);
738 	}
739 
740 	if (ret < 0)
741 		mwifiex_dbg(adapter, ERROR, "request_firmware%s error %d\n",
742 			    req_fw_nowait ? "_nowait" : "", ret);
743 	return ret;
744 }
745 
746 /*
747  * CFG802.11 network device handler for open.
748  *
749  * Starts the data queue.
750  */
751 static int
752 mwifiex_open(struct net_device *dev)
753 {
754 	netif_carrier_off(dev);
755 
756 	return 0;
757 }
758 
759 /*
760  * CFG802.11 network device handler for close.
761  */
762 static int
763 mwifiex_close(struct net_device *dev)
764 {
765 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
766 
767 	if (priv->scan_request) {
768 		struct cfg80211_scan_info info = {
769 			.aborted = true,
770 		};
771 
772 		mwifiex_dbg(priv->adapter, INFO,
773 			    "aborting scan on ndo_stop\n");
774 		cfg80211_scan_done(priv->scan_request, &info);
775 		priv->scan_request = NULL;
776 		priv->scan_aborting = true;
777 	}
778 
779 	if (priv->sched_scanning) {
780 		mwifiex_dbg(priv->adapter, INFO,
781 			    "aborting bgscan on ndo_stop\n");
782 		mwifiex_stop_bg_scan(priv);
783 		cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
784 	}
785 
786 	return 0;
787 }
788 
789 static bool
790 mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
791 			struct sk_buff *skb)
792 {
793 	struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
794 
795 	if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
796 	    mwifiex_is_skb_mgmt_frame(skb) ||
797 	    (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
798 	     ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
799 	     (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
800 		mwifiex_dbg(priv->adapter, DATA,
801 			    "bypass txqueue; eth type %#x, mgmt %d\n",
802 			     ntohs(eth_hdr->h_proto),
803 			     mwifiex_is_skb_mgmt_frame(skb));
804 		return true;
805 	}
806 
807 	return false;
808 }
809 /*
810  * Add buffer into wmm tx queue and queue work to transmit it.
811  */
812 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
813 {
814 	struct netdev_queue *txq;
815 	int index = mwifiex_1d_to_wmm_queue[skb->priority];
816 
817 	if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
818 		txq = netdev_get_tx_queue(priv->netdev, index);
819 		if (!netif_tx_queue_stopped(txq)) {
820 			netif_tx_stop_queue(txq);
821 			mwifiex_dbg(priv->adapter, DATA,
822 				    "stop queue: %d\n", index);
823 		}
824 	}
825 
826 	if (mwifiex_bypass_tx_queue(priv, skb)) {
827 		atomic_inc(&priv->adapter->tx_pending);
828 		atomic_inc(&priv->adapter->bypass_tx_pending);
829 		mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
830 	 } else {
831 		atomic_inc(&priv->adapter->tx_pending);
832 		mwifiex_wmm_add_buf_txqueue(priv, skb);
833 	 }
834 
835 	mwifiex_queue_main_work(priv->adapter);
836 
837 	return 0;
838 }
839 
840 struct sk_buff *
841 mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
842 				struct sk_buff *skb, u8 flag, u64 *cookie)
843 {
844 	struct sk_buff *orig_skb = skb;
845 	struct mwifiex_txinfo *tx_info, *orig_tx_info;
846 
847 	skb = skb_clone(skb, GFP_ATOMIC);
848 	if (skb) {
849 		int id;
850 
851 		spin_lock_bh(&priv->ack_status_lock);
852 		id = idr_alloc(&priv->ack_status_frames, orig_skb,
853 			       1, 0x10, GFP_ATOMIC);
854 		spin_unlock_bh(&priv->ack_status_lock);
855 
856 		if (id >= 0) {
857 			tx_info = MWIFIEX_SKB_TXCB(skb);
858 			tx_info->ack_frame_id = id;
859 			tx_info->flags |= flag;
860 			orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
861 			orig_tx_info->ack_frame_id = id;
862 			orig_tx_info->flags |= flag;
863 
864 			if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
865 				orig_tx_info->cookie = *cookie;
866 
867 		} else if (skb_shared(skb)) {
868 			kfree_skb(orig_skb);
869 		} else {
870 			kfree_skb(skb);
871 			skb = orig_skb;
872 		}
873 	} else {
874 		/* couldn't clone -- lose tx status ... */
875 		skb = orig_skb;
876 	}
877 
878 	return skb;
879 }
880 
881 /*
882  * CFG802.11 network device handler for data transmission.
883  */
884 static netdev_tx_t
885 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
886 {
887 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
888 	struct sk_buff *new_skb;
889 	struct mwifiex_txinfo *tx_info;
890 	bool multicast;
891 
892 	mwifiex_dbg(priv->adapter, DATA,
893 		    "data: %lu BSS(%d-%d): Data <= kernel\n",
894 		    jiffies, priv->bss_type, priv->bss_num);
895 
896 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &priv->adapter->work_flags)) {
897 		kfree_skb(skb);
898 		priv->stats.tx_dropped++;
899 		return 0;
900 	}
901 	if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
902 		mwifiex_dbg(priv->adapter, ERROR,
903 			    "Tx: bad skb len %d\n", skb->len);
904 		kfree_skb(skb);
905 		priv->stats.tx_dropped++;
906 		return 0;
907 	}
908 	if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
909 		mwifiex_dbg(priv->adapter, DATA,
910 			    "data: Tx: insufficient skb headroom %d\n",
911 			    skb_headroom(skb));
912 		/* Insufficient skb headroom - allocate a new skb */
913 		new_skb =
914 			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
915 		if (unlikely(!new_skb)) {
916 			mwifiex_dbg(priv->adapter, ERROR,
917 				    "Tx: cannot alloca new_skb\n");
918 			kfree_skb(skb);
919 			priv->stats.tx_dropped++;
920 			return 0;
921 		}
922 		kfree_skb(skb);
923 		skb = new_skb;
924 		mwifiex_dbg(priv->adapter, INFO,
925 			    "info: new skb headroomd %d\n",
926 			    skb_headroom(skb));
927 	}
928 
929 	tx_info = MWIFIEX_SKB_TXCB(skb);
930 	memset(tx_info, 0, sizeof(*tx_info));
931 	tx_info->bss_num = priv->bss_num;
932 	tx_info->bss_type = priv->bss_type;
933 	tx_info->pkt_len = skb->len;
934 
935 	multicast = is_multicast_ether_addr(skb->data);
936 
937 	if (unlikely(!multicast && skb->sk &&
938 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
939 		     priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
940 		skb = mwifiex_clone_skb_for_tx_status(priv,
941 						      skb,
942 					MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
943 
944 	/* Record the current time the packet was queued; used to
945 	 * determine the amount of time the packet was queued in
946 	 * the driver before it was sent to the firmware.
947 	 * The delay is then sent along with the packet to the
948 	 * firmware for aggregate delay calculation for stats and
949 	 * MSDU lifetime expiry.
950 	 */
951 	__net_timestamp(skb);
952 
953 	if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
954 	    priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
955 	    !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
956 		if (priv->adapter->auto_tdls && priv->check_tdls_tx)
957 			mwifiex_tdls_check_tx(priv, skb);
958 	}
959 
960 	mwifiex_queue_tx_pkt(priv, skb);
961 
962 	return 0;
963 }
964 
965 int mwifiex_set_mac_address(struct mwifiex_private *priv,
966 			    struct net_device *dev, bool external,
967 			    u8 *new_mac)
968 {
969 	int ret;
970 	u64 mac_addr, old_mac_addr;
971 
972 	old_mac_addr = ether_addr_to_u64(priv->curr_addr);
973 
974 	if (external) {
975 		mac_addr = ether_addr_to_u64(new_mac);
976 	} else {
977 		/* Internal mac address change */
978 		if (priv->bss_type == MWIFIEX_BSS_TYPE_ANY)
979 			return -EOPNOTSUPP;
980 
981 		mac_addr = old_mac_addr;
982 
983 		if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P) {
984 			mac_addr |= BIT_ULL(MWIFIEX_MAC_LOCAL_ADMIN_BIT);
985 			mac_addr += priv->bss_num;
986 		} else if (priv->adapter->priv[0] != priv) {
987 			/* Set mac address based on bss_type/bss_num */
988 			mac_addr ^= BIT_ULL(priv->bss_type + 8);
989 			mac_addr += priv->bss_num;
990 		}
991 	}
992 
993 	u64_to_ether_addr(mac_addr, priv->curr_addr);
994 
995 	/* Send request to firmware */
996 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
997 			       HostCmd_ACT_GEN_SET, 0, NULL, true);
998 
999 	if (ret) {
1000 		u64_to_ether_addr(old_mac_addr, priv->curr_addr);
1001 		mwifiex_dbg(priv->adapter, ERROR,
1002 			    "set mac address failed: ret=%d\n", ret);
1003 		return ret;
1004 	}
1005 
1006 	eth_hw_addr_set(dev, priv->curr_addr);
1007 	return 0;
1008 }
1009 
1010 /* CFG802.11 network device handler for setting MAC address.
1011  */
1012 static int
1013 mwifiex_ndo_set_mac_address(struct net_device *dev, void *addr)
1014 {
1015 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1016 	struct sockaddr *hw_addr = addr;
1017 
1018 	return mwifiex_set_mac_address(priv, dev, true, hw_addr->sa_data);
1019 }
1020 
1021 /*
1022  * CFG802.11 network device handler for setting multicast list.
1023  */
1024 static void mwifiex_set_multicast_list(struct net_device *dev)
1025 {
1026 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1027 	struct mwifiex_multicast_list mcast_list;
1028 
1029 	if (dev->flags & IFF_PROMISC) {
1030 		mcast_list.mode = MWIFIEX_PROMISC_MODE;
1031 	} else if (dev->flags & IFF_ALLMULTI ||
1032 		   netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
1033 		mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
1034 	} else {
1035 		mcast_list.mode = MWIFIEX_MULTICAST_MODE;
1036 		mcast_list.num_multicast_addr =
1037 			mwifiex_copy_mcast_addr(&mcast_list, dev);
1038 	}
1039 	mwifiex_request_set_multicast_list(priv, &mcast_list);
1040 }
1041 
1042 /*
1043  * CFG802.11 network device handler for transmission timeout.
1044  */
1045 static void
1046 mwifiex_tx_timeout(struct net_device *dev, unsigned int txqueue)
1047 {
1048 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1049 
1050 	priv->num_tx_timeout++;
1051 	priv->tx_timeout_cnt++;
1052 	mwifiex_dbg(priv->adapter, ERROR,
1053 		    "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
1054 		    jiffies, priv->tx_timeout_cnt, priv->bss_type,
1055 		    priv->bss_num);
1056 	mwifiex_set_trans_start(dev);
1057 
1058 	if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
1059 	    priv->adapter->if_ops.card_reset) {
1060 		mwifiex_dbg(priv->adapter, ERROR,
1061 			    "tx_timeout_cnt exceeds threshold.\t"
1062 			    "Triggering card reset!\n");
1063 		priv->adapter->if_ops.card_reset(priv->adapter);
1064 	}
1065 }
1066 
1067 void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
1068 {
1069 	struct usb_card_rec *card = adapter->card;
1070 	struct mwifiex_private *priv;
1071 	u16 tx_buf_size;
1072 	int i, ret;
1073 
1074 	card->mc_resync_flag = true;
1075 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1076 		if (atomic_read(&card->port[i].tx_data_urb_pending)) {
1077 			mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
1078 			return;
1079 		}
1080 	}
1081 
1082 	card->mc_resync_flag = false;
1083 	tx_buf_size = 0xffff;
1084 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1085 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
1086 			       HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
1087 	if (ret)
1088 		mwifiex_dbg(adapter, ERROR,
1089 			    "send reconfig tx buf size cmd err\n");
1090 }
1091 EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
1092 
1093 void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
1094 {
1095 	/* Dump all the memory data into single file, a userspace script will
1096 	 * be used to split all the memory data to multiple files
1097 	 */
1098 	mwifiex_dbg(adapter, MSG,
1099 		    "== mwifiex dump information to /sys/class/devcoredump start\n");
1100 	dev_coredumpv(adapter->dev, adapter->devdump_data, adapter->devdump_len,
1101 		      GFP_KERNEL);
1102 	mwifiex_dbg(adapter, MSG,
1103 		    "== mwifiex dump information to /sys/class/devcoredump end\n");
1104 
1105 	/* Device dump data will be freed in device coredump release function
1106 	 * after 5 min. Here reset adapter->devdump_data and ->devdump_len
1107 	 * to avoid it been accidentally reused.
1108 	 */
1109 	adapter->devdump_data = NULL;
1110 	adapter->devdump_len = 0;
1111 }
1112 EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1113 
1114 void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
1115 {
1116 	char *p;
1117 	char drv_version[64];
1118 	struct usb_card_rec *cardp;
1119 	struct sdio_mmc_card *sdio_card;
1120 	struct mwifiex_private *priv;
1121 	int i, idx;
1122 	struct netdev_queue *txq;
1123 	struct mwifiex_debug_info *debug_info;
1124 
1125 	mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
1126 
1127 	p = adapter->devdump_data;
1128 	strcpy(p, "========Start dump driverinfo========\n");
1129 	p += strlen("========Start dump driverinfo========\n");
1130 	p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1131 
1132 	mwifiex_drv_get_driver_version(adapter, drv_version,
1133 				       sizeof(drv_version) - 1);
1134 	p += sprintf(p, "driver_version = %s\n", drv_version);
1135 
1136 	if (adapter->iface_type == MWIFIEX_USB) {
1137 		cardp = (struct usb_card_rec *)adapter->card;
1138 		p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1139 			     atomic_read(&cardp->tx_cmd_urb_pending));
1140 		p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1141 			     atomic_read(&cardp->port[0].tx_data_urb_pending));
1142 		p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1143 			     atomic_read(&cardp->port[1].tx_data_urb_pending));
1144 		p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1145 			     atomic_read(&cardp->rx_cmd_urb_pending));
1146 		p += sprintf(p, "rx_data_urb_pending = %d\n",
1147 			     atomic_read(&cardp->rx_data_urb_pending));
1148 	}
1149 
1150 	p += sprintf(p, "tx_pending = %d\n",
1151 		     atomic_read(&adapter->tx_pending));
1152 	p += sprintf(p, "rx_pending = %d\n",
1153 		     atomic_read(&adapter->rx_pending));
1154 
1155 	if (adapter->iface_type == MWIFIEX_SDIO) {
1156 		sdio_card = (struct sdio_mmc_card *)adapter->card;
1157 		p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1158 			     sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1159 		p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1160 			     sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1161 	}
1162 
1163 	for (i = 0; i < adapter->priv_num; i++) {
1164 		if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1165 			continue;
1166 		priv = adapter->priv[i];
1167 		p += sprintf(p, "\n[interface  : \"%s\"]\n",
1168 			     priv->netdev->name);
1169 		p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1170 			     atomic_read(&priv->wmm_tx_pending[0]));
1171 		p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1172 			     atomic_read(&priv->wmm_tx_pending[1]));
1173 		p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1174 			     atomic_read(&priv->wmm_tx_pending[2]));
1175 		p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1176 			     atomic_read(&priv->wmm_tx_pending[3]));
1177 		p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1178 			     "Disconnected" : "Connected");
1179 		p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1180 			     ? "on" : "off"));
1181 		for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1182 			txq = netdev_get_tx_queue(priv->netdev, idx);
1183 			p += sprintf(p, "tx queue %d:%s  ", idx,
1184 				     netif_tx_queue_stopped(txq) ?
1185 				     "stopped" : "started");
1186 		}
1187 		p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1188 			     priv->netdev->name, priv->num_tx_timeout);
1189 	}
1190 
1191 	if (adapter->iface_type == MWIFIEX_SDIO ||
1192 	    adapter->iface_type == MWIFIEX_PCIE) {
1193 		p += sprintf(p, "\n=== %s register dump===\n",
1194 			     adapter->iface_type == MWIFIEX_SDIO ?
1195 							"SDIO" : "PCIE");
1196 		if (adapter->if_ops.reg_dump)
1197 			p += adapter->if_ops.reg_dump(adapter, p);
1198 	}
1199 	p += sprintf(p, "\n=== more debug information\n");
1200 	debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1201 	if (debug_info) {
1202 		for (i = 0; i < adapter->priv_num; i++) {
1203 			if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1204 				continue;
1205 			priv = adapter->priv[i];
1206 			mwifiex_get_debug_info(priv, debug_info);
1207 			p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1208 			break;
1209 		}
1210 		kfree(debug_info);
1211 	}
1212 
1213 	strcpy(p, "\n========End dump========\n");
1214 	p += strlen("\n========End dump========\n");
1215 	mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
1216 	adapter->devdump_len = p - (char *)adapter->devdump_data;
1217 }
1218 EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
1219 
1220 void mwifiex_prepare_fw_dump_info(struct mwifiex_adapter *adapter)
1221 {
1222 	u8 idx;
1223 	char *fw_dump_ptr;
1224 	u32 dump_len = 0;
1225 
1226 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1227 		struct memory_type_mapping *entry =
1228 				&adapter->mem_type_mapping_tbl[idx];
1229 
1230 		if (entry->mem_ptr) {
1231 			dump_len += (strlen("========Start dump ") +
1232 					strlen(entry->mem_name) +
1233 					strlen("========\n") +
1234 					(entry->mem_size + 1) +
1235 					strlen("\n========End dump========\n"));
1236 		}
1237 	}
1238 
1239 	if (dump_len + 1 + adapter->devdump_len > MWIFIEX_FW_DUMP_SIZE) {
1240 		/* Realloc in case buffer overflow */
1241 		fw_dump_ptr = vzalloc(dump_len + 1 + adapter->devdump_len);
1242 		mwifiex_dbg(adapter, MSG, "Realloc device dump data.\n");
1243 		if (!fw_dump_ptr) {
1244 			vfree(adapter->devdump_data);
1245 			mwifiex_dbg(adapter, ERROR,
1246 				    "vzalloc devdump data failure!\n");
1247 			return;
1248 		}
1249 
1250 		memmove(fw_dump_ptr, adapter->devdump_data,
1251 			adapter->devdump_len);
1252 		vfree(adapter->devdump_data);
1253 		adapter->devdump_data = fw_dump_ptr;
1254 	}
1255 
1256 	fw_dump_ptr = (char *)adapter->devdump_data + adapter->devdump_len;
1257 
1258 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1259 		struct memory_type_mapping *entry =
1260 					&adapter->mem_type_mapping_tbl[idx];
1261 
1262 		if (entry->mem_ptr) {
1263 			strcpy(fw_dump_ptr, "========Start dump ");
1264 			fw_dump_ptr += strlen("========Start dump ");
1265 
1266 			strcpy(fw_dump_ptr, entry->mem_name);
1267 			fw_dump_ptr += strlen(entry->mem_name);
1268 
1269 			strcpy(fw_dump_ptr, "========\n");
1270 			fw_dump_ptr += strlen("========\n");
1271 
1272 			memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1273 			fw_dump_ptr += entry->mem_size;
1274 
1275 			strcpy(fw_dump_ptr, "\n========End dump========\n");
1276 			fw_dump_ptr += strlen("\n========End dump========\n");
1277 		}
1278 	}
1279 
1280 	adapter->devdump_len = fw_dump_ptr - (char *)adapter->devdump_data;
1281 
1282 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1283 		struct memory_type_mapping *entry =
1284 			&adapter->mem_type_mapping_tbl[idx];
1285 
1286 		vfree(entry->mem_ptr);
1287 		entry->mem_ptr = NULL;
1288 		entry->mem_size = 0;
1289 	}
1290 }
1291 EXPORT_SYMBOL_GPL(mwifiex_prepare_fw_dump_info);
1292 
1293 /*
1294  * CFG802.11 network device handler for statistics retrieval.
1295  */
1296 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1297 {
1298 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1299 
1300 	return &priv->stats;
1301 }
1302 
1303 static u16
1304 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
1305 				struct net_device *sb_dev)
1306 {
1307 	skb->priority = cfg80211_classify8021d(skb, NULL);
1308 	return mwifiex_1d_to_wmm_queue[skb->priority];
1309 }
1310 
1311 /* Network device handlers */
1312 static const struct net_device_ops mwifiex_netdev_ops = {
1313 	.ndo_open = mwifiex_open,
1314 	.ndo_stop = mwifiex_close,
1315 	.ndo_start_xmit = mwifiex_hard_start_xmit,
1316 	.ndo_set_mac_address = mwifiex_ndo_set_mac_address,
1317 	.ndo_validate_addr = eth_validate_addr,
1318 	.ndo_tx_timeout = mwifiex_tx_timeout,
1319 	.ndo_get_stats = mwifiex_get_stats,
1320 	.ndo_set_rx_mode = mwifiex_set_multicast_list,
1321 	.ndo_select_queue = mwifiex_netdev_select_wmm_queue,
1322 };
1323 
1324 /*
1325  * This function initializes the private structure parameters.
1326  *
1327  * The following wait queues are initialized -
1328  *      - IOCTL wait queue
1329  *      - Command wait queue
1330  *      - Statistics wait queue
1331  *
1332  * ...and the following default parameters are set -
1333  *      - Current key index     : Set to 0
1334  *      - Rate index            : Set to auto
1335  *      - Media connected       : Set to disconnected
1336  *      - Adhoc link sensed     : Set to false
1337  *      - Nick name             : Set to null
1338  *      - Number of Tx timeout  : Set to 0
1339  *      - Device address        : Set to current address
1340  *      - Rx histogram statistc : Set to 0
1341  *
1342  * In addition, the CFG80211 work queue is also created.
1343  */
1344 void mwifiex_init_priv_params(struct mwifiex_private *priv,
1345 			      struct net_device *dev)
1346 {
1347 	dev->netdev_ops = &mwifiex_netdev_ops;
1348 	dev->needs_free_netdev = true;
1349 	/* Initialize private structure */
1350 	priv->current_key_index = 0;
1351 	priv->media_connected = false;
1352 	memset(priv->mgmt_ie, 0,
1353 	       sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
1354 	priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1355 	priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1356 	priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
1357 	priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
1358 	priv->num_tx_timeout = 0;
1359 	if (is_valid_ether_addr(dev->dev_addr))
1360 		ether_addr_copy(priv->curr_addr, dev->dev_addr);
1361 	else
1362 		ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
1363 
1364 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1365 	    GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1366 		priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1367 		if (priv->hist_data)
1368 			mwifiex_hist_data_reset(priv);
1369 	}
1370 }
1371 
1372 /*
1373  * This function check if command is pending.
1374  */
1375 int is_command_pending(struct mwifiex_adapter *adapter)
1376 {
1377 	int is_cmd_pend_q_empty;
1378 
1379 	spin_lock_bh(&adapter->cmd_pending_q_lock);
1380 	is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1381 	spin_unlock_bh(&adapter->cmd_pending_q_lock);
1382 
1383 	return !is_cmd_pend_q_empty;
1384 }
1385 
1386 /*
1387  * This is the RX work queue function.
1388  *
1389  * It handles the RX operations.
1390  */
1391 static void mwifiex_rx_work_queue(struct work_struct *work)
1392 {
1393 	struct mwifiex_adapter *adapter =
1394 		container_of(work, struct mwifiex_adapter, rx_work);
1395 
1396 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1397 		return;
1398 	mwifiex_process_rx(adapter);
1399 }
1400 
1401 /*
1402  * This is the main work queue function.
1403  *
1404  * It handles the main process, which in turn handles the complete
1405  * driver operations.
1406  */
1407 static void mwifiex_main_work_queue(struct work_struct *work)
1408 {
1409 	struct mwifiex_adapter *adapter =
1410 		container_of(work, struct mwifiex_adapter, main_work);
1411 
1412 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1413 		return;
1414 	mwifiex_main_process(adapter);
1415 }
1416 
1417 /* Common teardown code used for both device removal and reset */
1418 static void mwifiex_uninit_sw(struct mwifiex_adapter *adapter)
1419 {
1420 	struct mwifiex_private *priv;
1421 	int i;
1422 
1423 	/* We can no longer handle interrupts once we start doing the teardown
1424 	 * below.
1425 	 */
1426 	if (adapter->if_ops.disable_int)
1427 		adapter->if_ops.disable_int(adapter);
1428 
1429 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1430 	mwifiex_terminate_workqueue(adapter);
1431 	adapter->int_status = 0;
1432 
1433 	/* Stop data */
1434 	for (i = 0; i < adapter->priv_num; i++) {
1435 		priv = adapter->priv[i];
1436 		if (priv && priv->netdev) {
1437 			mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1438 			if (netif_carrier_ok(priv->netdev))
1439 				netif_carrier_off(priv->netdev);
1440 			netif_device_detach(priv->netdev);
1441 		}
1442 	}
1443 
1444 	mwifiex_dbg(adapter, CMD, "cmd: calling mwifiex_shutdown_drv...\n");
1445 	mwifiex_shutdown_drv(adapter);
1446 	mwifiex_dbg(adapter, CMD, "cmd: mwifiex_shutdown_drv done\n");
1447 
1448 	if (atomic_read(&adapter->rx_pending) ||
1449 	    atomic_read(&adapter->tx_pending) ||
1450 	    atomic_read(&adapter->cmd_pending)) {
1451 		mwifiex_dbg(adapter, ERROR,
1452 			    "rx_pending=%d, tx_pending=%d,\t"
1453 			    "cmd_pending=%d\n",
1454 			    atomic_read(&adapter->rx_pending),
1455 			    atomic_read(&adapter->tx_pending),
1456 			    atomic_read(&adapter->cmd_pending));
1457 	}
1458 
1459 	for (i = 0; i < adapter->priv_num; i++) {
1460 		priv = adapter->priv[i];
1461 		if (!priv)
1462 			continue;
1463 		rtnl_lock();
1464 		if (priv->netdev &&
1465 		    priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
1466 			/*
1467 			 * Close the netdev now, because if we do it later, the
1468 			 * netdev notifiers will need to acquire the wiphy lock
1469 			 * again --> deadlock.
1470 			 */
1471 			dev_close(priv->wdev.netdev);
1472 			wiphy_lock(adapter->wiphy);
1473 			mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
1474 			wiphy_unlock(adapter->wiphy);
1475 		}
1476 		rtnl_unlock();
1477 	}
1478 
1479 	wiphy_unregister(adapter->wiphy);
1480 	wiphy_free(adapter->wiphy);
1481 	adapter->wiphy = NULL;
1482 
1483 	vfree(adapter->chan_stats);
1484 	mwifiex_free_cmd_buffers(adapter);
1485 }
1486 
1487 /*
1488  * This function can be used for shutting down the adapter SW.
1489  */
1490 int mwifiex_shutdown_sw(struct mwifiex_adapter *adapter)
1491 {
1492 	struct mwifiex_private *priv;
1493 
1494 	if (!adapter)
1495 		return 0;
1496 
1497 	wait_for_completion(adapter->fw_done);
1498 	/* Caller should ensure we aren't suspending while this happens */
1499 	reinit_completion(adapter->fw_done);
1500 
1501 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1502 	mwifiex_deauthenticate(priv, NULL);
1503 
1504 	mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
1505 
1506 	mwifiex_uninit_sw(adapter);
1507 	adapter->is_up = false;
1508 
1509 	if (adapter->if_ops.down_dev)
1510 		adapter->if_ops.down_dev(adapter);
1511 
1512 	return 0;
1513 }
1514 EXPORT_SYMBOL_GPL(mwifiex_shutdown_sw);
1515 
1516 /* This function can be used for reinitting the adapter SW. Required
1517  * code is extracted from mwifiex_add_card()
1518  */
1519 int
1520 mwifiex_reinit_sw(struct mwifiex_adapter *adapter)
1521 {
1522 	int ret;
1523 
1524 	mwifiex_init_lock_list(adapter);
1525 	if (adapter->if_ops.up_dev)
1526 		adapter->if_ops.up_dev(adapter);
1527 
1528 	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1529 	clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1530 	init_waitqueue_head(&adapter->init_wait_q);
1531 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1532 	adapter->hs_activated = false;
1533 	clear_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags);
1534 	init_waitqueue_head(&adapter->hs_activate_wait_q);
1535 	init_waitqueue_head(&adapter->cmd_wait_q.wait);
1536 	adapter->cmd_wait_q.status = 0;
1537 	adapter->scan_wait_q_woken = false;
1538 
1539 	if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1540 		adapter->rx_work_enabled = true;
1541 
1542 	adapter->workqueue =
1543 		alloc_workqueue("MWIFIEX_WORK_QUEUE",
1544 				WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1545 	if (!adapter->workqueue)
1546 		goto err_kmalloc;
1547 
1548 	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1549 
1550 	if (adapter->rx_work_enabled) {
1551 		adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1552 							WQ_HIGHPRI |
1553 							WQ_MEM_RECLAIM |
1554 							WQ_UNBOUND, 1);
1555 		if (!adapter->rx_workqueue)
1556 			goto err_kmalloc;
1557 		INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1558 	}
1559 
1560 	/* Register the device. Fill up the private data structure with
1561 	 * relevant information from the card. Some code extracted from
1562 	 * mwifiex_register_dev()
1563 	 */
1564 	mwifiex_dbg(adapter, INFO, "%s, mwifiex_init_hw_fw()...\n", __func__);
1565 
1566 	if (mwifiex_init_hw_fw(adapter, false)) {
1567 		mwifiex_dbg(adapter, ERROR,
1568 			    "%s: firmware init failed\n", __func__);
1569 		goto err_init_fw;
1570 	}
1571 
1572 	/* _mwifiex_fw_dpc() does its own cleanup */
1573 	ret = _mwifiex_fw_dpc(adapter->firmware, adapter);
1574 	if (ret) {
1575 		pr_err("Failed to bring up adapter: %d\n", ret);
1576 		return ret;
1577 	}
1578 	mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
1579 
1580 	return 0;
1581 
1582 err_init_fw:
1583 	mwifiex_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
1584 	if (adapter->if_ops.unregister_dev)
1585 		adapter->if_ops.unregister_dev(adapter);
1586 
1587 err_kmalloc:
1588 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1589 	mwifiex_terminate_workqueue(adapter);
1590 	if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1591 		mwifiex_dbg(adapter, ERROR,
1592 			    "info: %s: shutdown mwifiex\n", __func__);
1593 		mwifiex_shutdown_drv(adapter);
1594 		mwifiex_free_cmd_buffers(adapter);
1595 	}
1596 
1597 	complete_all(adapter->fw_done);
1598 	mwifiex_dbg(adapter, INFO, "%s, error\n", __func__);
1599 
1600 	return -1;
1601 }
1602 EXPORT_SYMBOL_GPL(mwifiex_reinit_sw);
1603 
1604 static irqreturn_t mwifiex_irq_wakeup_handler(int irq, void *priv)
1605 {
1606 	struct mwifiex_adapter *adapter = priv;
1607 
1608 	dev_dbg(adapter->dev, "%s: wake by wifi", __func__);
1609 	adapter->wake_by_wifi = true;
1610 	disable_irq_nosync(irq);
1611 
1612 	/* Notify PM core we are wakeup source */
1613 	pm_wakeup_event(adapter->dev, 0);
1614 	pm_system_wakeup();
1615 
1616 	return IRQ_HANDLED;
1617 }
1618 
1619 static void mwifiex_probe_of(struct mwifiex_adapter *adapter)
1620 {
1621 	int ret;
1622 	struct device *dev = adapter->dev;
1623 
1624 	if (!dev->of_node)
1625 		goto err_exit;
1626 
1627 	adapter->dt_node = dev->of_node;
1628 	adapter->irq_wakeup = irq_of_parse_and_map(adapter->dt_node, 0);
1629 	if (!adapter->irq_wakeup) {
1630 		dev_dbg(dev, "fail to parse irq_wakeup from device tree\n");
1631 		goto err_exit;
1632 	}
1633 
1634 	ret = devm_request_irq(dev, adapter->irq_wakeup,
1635 			       mwifiex_irq_wakeup_handler, IRQF_TRIGGER_LOW,
1636 			       "wifi_wake", adapter);
1637 	if (ret) {
1638 		dev_err(dev, "Failed to request irq_wakeup %d (%d)\n",
1639 			adapter->irq_wakeup, ret);
1640 		goto err_exit;
1641 	}
1642 
1643 	disable_irq(adapter->irq_wakeup);
1644 	if (device_init_wakeup(dev, true)) {
1645 		dev_err(dev, "fail to init wakeup for mwifiex\n");
1646 		goto err_exit;
1647 	}
1648 	return;
1649 
1650 err_exit:
1651 	adapter->irq_wakeup = -1;
1652 }
1653 
1654 /*
1655  * This function adds the card.
1656  *
1657  * This function follows the following major steps to set up the device -
1658  *      - Initialize software. This includes probing the card, registering
1659  *        the interface operations table, and allocating/initializing the
1660  *        adapter structure
1661  *      - Set up the netlink socket
1662  *      - Create and start the main work queue
1663  *      - Register the device
1664  *      - Initialize firmware and hardware
1665  *      - Add logical interfaces
1666  */
1667 int
1668 mwifiex_add_card(void *card, struct completion *fw_done,
1669 		 struct mwifiex_if_ops *if_ops, u8 iface_type,
1670 		 struct device *dev)
1671 {
1672 	struct mwifiex_adapter *adapter;
1673 
1674 	if (mwifiex_register(card, dev, if_ops, (void **)&adapter)) {
1675 		pr_err("%s: software init failed\n", __func__);
1676 		goto err_init_sw;
1677 	}
1678 
1679 	mwifiex_probe_of(adapter);
1680 
1681 	adapter->iface_type = iface_type;
1682 	adapter->fw_done = fw_done;
1683 
1684 	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1685 	clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1686 	init_waitqueue_head(&adapter->init_wait_q);
1687 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1688 	adapter->hs_activated = false;
1689 	init_waitqueue_head(&adapter->hs_activate_wait_q);
1690 	init_waitqueue_head(&adapter->cmd_wait_q.wait);
1691 	adapter->cmd_wait_q.status = 0;
1692 	adapter->scan_wait_q_woken = false;
1693 
1694 	if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1695 		adapter->rx_work_enabled = true;
1696 
1697 	adapter->workqueue =
1698 		alloc_workqueue("MWIFIEX_WORK_QUEUE",
1699 				WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1700 	if (!adapter->workqueue)
1701 		goto err_kmalloc;
1702 
1703 	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1704 
1705 	if (adapter->rx_work_enabled) {
1706 		adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1707 							WQ_HIGHPRI |
1708 							WQ_MEM_RECLAIM |
1709 							WQ_UNBOUND, 1);
1710 		if (!adapter->rx_workqueue)
1711 			goto err_kmalloc;
1712 
1713 		INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1714 	}
1715 
1716 	/* Register the device. Fill up the private data structure with relevant
1717 	   information from the card. */
1718 	if (adapter->if_ops.register_dev(adapter)) {
1719 		pr_err("%s: failed to register mwifiex device\n", __func__);
1720 		goto err_registerdev;
1721 	}
1722 
1723 	if (mwifiex_init_hw_fw(adapter, true)) {
1724 		pr_err("%s: firmware init failed\n", __func__);
1725 		goto err_init_fw;
1726 	}
1727 
1728 	return 0;
1729 
1730 err_init_fw:
1731 	pr_debug("info: %s: unregister device\n", __func__);
1732 	if (adapter->if_ops.unregister_dev)
1733 		adapter->if_ops.unregister_dev(adapter);
1734 err_registerdev:
1735 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1736 	mwifiex_terminate_workqueue(adapter);
1737 	if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1738 		pr_debug("info: %s: shutdown mwifiex\n", __func__);
1739 		mwifiex_shutdown_drv(adapter);
1740 		mwifiex_free_cmd_buffers(adapter);
1741 	}
1742 err_kmalloc:
1743 	if (adapter->irq_wakeup >= 0)
1744 		device_init_wakeup(adapter->dev, false);
1745 	mwifiex_free_adapter(adapter);
1746 
1747 err_init_sw:
1748 
1749 	return -1;
1750 }
1751 EXPORT_SYMBOL_GPL(mwifiex_add_card);
1752 
1753 /*
1754  * This function removes the card.
1755  *
1756  * This function follows the following major steps to remove the device -
1757  *      - Stop data traffic
1758  *      - Shutdown firmware
1759  *      - Remove the logical interfaces
1760  *      - Terminate the work queue
1761  *      - Unregister the device
1762  *      - Free the adapter structure
1763  */
1764 int mwifiex_remove_card(struct mwifiex_adapter *adapter)
1765 {
1766 	if (!adapter)
1767 		return 0;
1768 
1769 	if (adapter->is_up)
1770 		mwifiex_uninit_sw(adapter);
1771 
1772 	if (adapter->irq_wakeup >= 0)
1773 		device_init_wakeup(adapter->dev, false);
1774 
1775 	/* Unregister device */
1776 	mwifiex_dbg(adapter, INFO,
1777 		    "info: unregister device\n");
1778 	if (adapter->if_ops.unregister_dev)
1779 		adapter->if_ops.unregister_dev(adapter);
1780 	/* Free adapter structure */
1781 	mwifiex_dbg(adapter, INFO,
1782 		    "info: free adapter\n");
1783 	mwifiex_free_adapter(adapter);
1784 
1785 	return 0;
1786 }
1787 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1788 
1789 void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1790 		  const char *fmt, ...)
1791 {
1792 	struct va_format vaf;
1793 	va_list args;
1794 
1795 	if (!(adapter->debug_mask & mask))
1796 		return;
1797 
1798 	va_start(args, fmt);
1799 
1800 	vaf.fmt = fmt;
1801 	vaf.va = &args;
1802 
1803 	if (adapter->dev)
1804 		dev_info(adapter->dev, "%pV", &vaf);
1805 	else
1806 		pr_info("%pV", &vaf);
1807 
1808 	va_end(args);
1809 }
1810 EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1811 
1812 /*
1813  * This function initializes the module.
1814  *
1815  * The debug FS is also initialized if configured.
1816  */
1817 static int
1818 mwifiex_init_module(void)
1819 {
1820 #ifdef CONFIG_DEBUG_FS
1821 	mwifiex_debugfs_init();
1822 #endif
1823 	return 0;
1824 }
1825 
1826 /*
1827  * This function cleans up the module.
1828  *
1829  * The debug FS is removed if available.
1830  */
1831 static void
1832 mwifiex_cleanup_module(void)
1833 {
1834 #ifdef CONFIG_DEBUG_FS
1835 	mwifiex_debugfs_remove();
1836 #endif
1837 }
1838 
1839 module_init(mwifiex_init_module);
1840 module_exit(mwifiex_cleanup_module);
1841 
1842 MODULE_AUTHOR("Marvell International Ltd.");
1843 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1844 MODULE_VERSION(VERSION);
1845 MODULE_LICENSE("GPL v2");
1846