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