blob: 77db0886c6e2e9fa764f2a0add8876b477972915 [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: major functions
3 *
4 * Copyright (C) 2011, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20#include "main.h"
21#include "wmm.h"
22#include "cfg80211.h"
23#include "11n.h"
24
25#define VERSION "1.0"
26
27const char driver_version[] = "mwifiex " VERSION " (%s) ";
Amitkumar Karwar388ec382013-05-17 17:50:25 -070028static char *cal_data_cfg;
29module_param(cal_data_cfg, charp, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070030
Amitkumar Karwardf810082013-06-04 16:19:56 -070031static void scan_delay_timer_fn(unsigned long data)
32{
33 struct mwifiex_private *priv = (struct mwifiex_private *)data;
34 struct mwifiex_adapter *adapter = priv->adapter;
35 struct cmd_ctrl_node *cmd_node, *tmp_node;
36 unsigned long flags;
37
38 if (adapter->surprise_removed)
39 return;
40
Amitkumar Karwarf5e8560e2014-03-25 19:01:18 -070041 if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT ||
42 !adapter->scan_processing) {
Amitkumar Karwardf810082013-06-04 16:19:56 -070043 /*
44 * Abort scan operation by cancelling all pending scan
45 * commands
46 */
47 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
48 list_for_each_entry_safe(cmd_node, tmp_node,
49 &adapter->scan_pending_q, list) {
50 list_del(&cmd_node->list);
51 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
52 }
53 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
54
55 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
56 adapter->scan_processing = false;
57 adapter->scan_delay_cnt = 0;
58 adapter->empty_tx_q_cnt = 0;
59 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
60
61 if (priv->scan_request) {
62 dev_dbg(adapter->dev, "info: aborting scan\n");
63 cfg80211_scan_done(priv->scan_request, 1);
64 priv->scan_request = NULL;
65 } else {
66 priv->scan_aborting = false;
67 dev_dbg(adapter->dev, "info: scan already aborted\n");
68 }
69 goto done;
70 }
71
72 if (!atomic_read(&priv->adapter->is_tx_received)) {
73 adapter->empty_tx_q_cnt++;
74 if (adapter->empty_tx_q_cnt == MWIFIEX_MAX_EMPTY_TX_Q_CNT) {
75 /*
76 * No Tx traffic for 200msec. Get scan command from
77 * scan pending queue and put to cmd pending queue to
78 * resume scan operation
79 */
80 adapter->scan_delay_cnt = 0;
81 adapter->empty_tx_q_cnt = 0;
82 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
83 cmd_node = list_first_entry(&adapter->scan_pending_q,
84 struct cmd_ctrl_node, list);
85 list_del(&cmd_node->list);
86 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
87 flags);
88
89 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
90 true);
91 queue_work(adapter->workqueue, &adapter->main_work);
92 goto done;
93 }
94 } else {
95 adapter->empty_tx_q_cnt = 0;
96 }
97
98 /* Delay scan operation further by 20msec */
99 mod_timer(&priv->scan_delay_timer, jiffies +
100 msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
101 adapter->scan_delay_cnt++;
102
103done:
104 if (atomic_read(&priv->adapter->is_tx_received))
105 atomic_set(&priv->adapter->is_tx_received, false);
106
107 return;
108}
109
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700110/*
111 * This function registers the device and performs all the necessary
112 * initializations.
113 *
114 * The following initialization operations are performed -
115 * - Allocate adapter structure
116 * - Save interface specific operations table in adapter
117 * - Call interface specific initialization routine
118 * - Allocate private structures
119 * - Set default adapter structure parameters
120 * - Initialize locks
121 *
122 * In case of any errors during inittialization, this function also ensures
123 * proper cleanup before exiting.
124 */
125static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530126 void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700127{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700128 struct mwifiex_adapter *adapter;
129 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700130
131 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700132 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200133 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700134
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530135 *padapter = adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700136 adapter->card = card;
137
138 /* Save interface specific operations in adapter */
139 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
140
141 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700142 if (adapter->if_ops.init_if)
143 if (adapter->if_ops.init_if(adapter))
144 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700145
146 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700147
Avinash Patil64b05e22012-05-08 18:30:13 -0700148 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
149 /* Allocate memory for private structure */
150 adapter->priv[i] =
151 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
152 if (!adapter->priv[i])
153 goto error;
154
155 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -0700156 adapter->priv_num++;
Amitkumar Karwardf810082013-06-04 16:19:56 -0700157
158 setup_timer(&adapter->priv[i]->scan_delay_timer,
159 scan_delay_timer_fn,
160 (unsigned long)adapter->priv[i]);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700161 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -0700162 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700163
164 init_timer(&adapter->cmd_timer);
165 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
166 adapter->cmd_timer.data = (unsigned long) adapter;
167
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700168 return 0;
169
170error:
171 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
172
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700173 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700174 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700175
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700176 kfree(adapter);
177
178 return -1;
179}
180
181/*
182 * This function unregisters the device and performs all the necessary
183 * cleanups.
184 *
185 * The following cleanup operations are performed -
186 * - Free the timers
187 * - Free beacon buffers
188 * - Free private structures
189 * - Free adapter structure
190 */
191static int mwifiex_unregister(struct mwifiex_adapter *adapter)
192{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700193 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700194
Amitkumar Karwar4cfda672013-07-22 19:17:50 -0700195 if (adapter->if_ops.cleanup_if)
196 adapter->if_ops.cleanup_if(adapter);
197
Avinash Patil629873f2014-02-18 15:47:55 -0800198 del_timer_sync(&adapter->cmd_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700199
200 /* Free private structures */
201 for (i = 0; i < adapter->priv_num; i++) {
202 if (adapter->priv[i]) {
203 mwifiex_free_curr_bcn(adapter->priv[i]);
Amitkumar Karwar003d87c2013-07-22 19:17:48 -0700204 del_timer_sync(&adapter->priv[i]->scan_delay_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700205 kfree(adapter->priv[i]);
206 }
207 }
208
209 kfree(adapter);
210 return 0;
211}
212
213/*
214 * The main process.
215 *
216 * This function is the main procedure of the driver and handles various driver
217 * operations. It runs in a loop and provides the core functionalities.
218 *
219 * The main responsibilities of this function are -
220 * - Ensure concurrency control
221 * - Handle pending interrupts and call interrupt handlers
222 * - Wake up the card if required
223 * - Handle command responses and call response handlers
224 * - Handle events and call event handlers
225 * - Execute pending commands
226 * - Transmit pending data packets
227 */
228int mwifiex_main_process(struct mwifiex_adapter *adapter)
229{
230 int ret = 0;
231 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700232 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700233
234 spin_lock_irqsave(&adapter->main_proc_lock, flags);
235
236 /* Check if already processing */
237 if (adapter->mwifiex_processing) {
238 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
239 goto exit_main_proc;
240 } else {
241 adapter->mwifiex_processing = true;
242 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
243 }
244process_start:
245 do {
246 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
247 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
248 break;
249
250 /* Handle pending interrupt if any */
251 if (adapter->int_status) {
252 if (adapter->hs_activated)
253 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700254 if (adapter->if_ops.process_int_status)
255 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700256 }
257
258 /* Need to wake up the card ? */
259 if ((adapter->ps_state == PS_STATE_SLEEP) &&
260 (adapter->pm_wakeup_card_req &&
261 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700262 (is_command_pending(adapter) ||
263 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700264 adapter->pm_wakeup_fw_try = true;
265 adapter->if_ops.wakeup(adapter);
266 continue;
267 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700268
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700269 if (IS_CARD_RX_RCVD(adapter)) {
270 adapter->pm_wakeup_fw_try = false;
271 if (adapter->ps_state == PS_STATE_SLEEP)
272 adapter->ps_state = PS_STATE_AWAKE;
273 } else {
274 /* We have tried to wakeup the card already */
275 if (adapter->pm_wakeup_fw_try)
276 break;
277 if (adapter->ps_state != PS_STATE_AWAKE ||
278 adapter->tx_lock_flag)
279 break;
280
Amitkumar Karwarf931c7702012-06-20 19:58:36 -0700281 if ((adapter->scan_processing &&
282 !adapter->scan_delay_cnt) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700283 mwifiex_wmm_lists_empty(adapter)) {
284 if (adapter->cmd_sent || adapter->curr_cmd ||
285 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700286 break;
287 }
288 }
289
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700290 /* Check Rx data for USB */
291 if (adapter->iface_type == MWIFIEX_USB)
292 while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
293 mwifiex_handle_rx_packet(adapter, skb);
294
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700295 /* Check for Cmd Resp */
296 if (adapter->cmd_resp_received) {
297 adapter->cmd_resp_received = false;
298 mwifiex_process_cmdresp(adapter);
299
300 /* call mwifiex back when init_fw is done */
301 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
302 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
303 mwifiex_init_fw_complete(adapter);
304 }
305 }
306
307 /* Check for event */
308 if (adapter->event_received) {
309 adapter->event_received = false;
310 mwifiex_process_event(adapter);
311 }
312
313 /* Check if we need to confirm Sleep Request
314 received previously */
315 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
316 if (!adapter->cmd_sent && !adapter->curr_cmd)
317 mwifiex_check_ps_cond(adapter);
318 }
319
320 /* * The ps_state may have been changed during processing of
321 * Sleep Request event.
322 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700323 if ((adapter->ps_state == PS_STATE_SLEEP) ||
324 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
325 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
326 adapter->tx_lock_flag)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700327 continue;
328
329 if (!adapter->cmd_sent && !adapter->curr_cmd) {
330 if (mwifiex_exec_next_cmd(adapter) == -1) {
331 ret = -1;
332 break;
333 }
334 }
335
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700336 if ((!adapter->scan_processing || adapter->scan_delay_cnt) &&
337 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700338 mwifiex_wmm_process_tx(adapter);
339 if (adapter->hs_activated) {
340 adapter->is_hs_configured = false;
341 mwifiex_hs_activated_event
342 (mwifiex_get_priv
343 (adapter, MWIFIEX_BSS_ROLE_ANY),
344 false);
345 }
346 }
347
348 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700349 !adapter->curr_cmd && !is_command_pending(adapter) &&
350 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700351 if (!mwifiex_send_null_packet
352 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
353 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
354 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
355 adapter->delay_null_pkt = false;
356 adapter->ps_state = PS_STATE_SLEEP;
357 }
358 break;
359 }
360 } while (true);
361
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700362 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700363 if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter)) {
364 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
365 goto process_start;
366 }
367
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700368 adapter->mwifiex_processing = false;
369 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
370
371exit_main_proc:
372 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
373 mwifiex_shutdown_drv(adapter);
374 return ret;
375}
Bing Zhao601216e2012-11-05 16:59:15 -0800376EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700377
378/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700379 * This function frees the adapter structure.
380 *
381 * Additionally, this closes the netlink socket, frees the timers
382 * and private structures.
383 */
384static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
385{
386 if (!adapter) {
387 pr_err("%s: adapter is NULL\n", __func__);
388 return;
389 }
390
391 mwifiex_unregister(adapter);
392 pr_debug("info: %s: free adapter\n", __func__);
393}
394
395/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700396 * This function cancels all works in the queue and destroys
397 * the main workqueue.
398 */
399static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
400{
401 flush_workqueue(adapter->workqueue);
402 destroy_workqueue(adapter->workqueue);
403 adapter->workqueue = NULL;
404}
405
406/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700407 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700408 *
409 * The main initialization steps followed are -
410 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700411 * - Issue the init commands to firmware
412 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700413static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700414{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800415 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700416 char fmt[64];
417 struct mwifiex_private *priv;
418 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700419 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700420 struct semaphore *sem = adapter->card_sem;
421 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800422 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700423
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700424 if (!firmware) {
425 dev_err(adapter->dev,
426 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700427 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700428 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700429
430 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
431 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700432 fw.fw_buf = (u8 *) adapter->firmware->data;
433 fw.fw_len = adapter->firmware->size;
434
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700435 if (adapter->if_ops.dnld_fw)
436 ret = adapter->if_ops.dnld_fw(adapter, &fw);
437 else
438 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700439 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700440 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700441
442 dev_notice(adapter->dev, "WLAN FW is active\n");
443
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700444 if (cal_data_cfg) {
445 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
446 adapter->dev)) < 0)
447 dev_err(adapter->dev,
448 "Cal data request_firmware() failed\n");
449 }
450
Daniel Drake232fde02013-07-13 10:57:10 -0400451 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700452 if (adapter->if_ops.enable_int) {
453 if (adapter->if_ops.enable_int(adapter))
454 goto err_dnld_fw;
455 }
Daniel Drake232fde02013-07-13 10:57:10 -0400456
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700457 adapter->init_wait_q_woken = false;
458 ret = mwifiex_init_fw(adapter);
459 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700460 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700461 } else if (!ret) {
462 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
463 goto done;
464 }
465 /* Wait for mwifiex_init to complete */
466 wait_event_interruptible(adapter->init_wait_q,
467 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700468 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700469 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700470
Avinash Patild6bffe82012-05-08 18:30:15 -0700471 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
472 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700473 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800474 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700475 }
476
477 rtnl_lock();
478 /* Create station interface by default */
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800479 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
480 NL80211_IFTYPE_STATION, NULL, NULL);
481 if (IS_ERR(wdev)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700482 dev_err(adapter->dev, "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800483 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700484 goto err_add_intf;
485 }
486 rtnl_unlock();
487
488 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
489 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
490 goto done;
491
492err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700493 wiphy_unregister(adapter->wiphy);
494 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700495err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400496 if (adapter->if_ops.disable_int)
497 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700498err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700499 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700500 if (adapter->if_ops.unregister_dev)
501 adapter->if_ops.unregister_dev(adapter);
502
503 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
504 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
505 pr_debug("info: %s: shutdown mwifiex\n", __func__);
506 adapter->init_wait_q_woken = false;
507
508 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
509 wait_event_interruptible(adapter->init_wait_q,
510 adapter->init_wait_q_woken);
511 }
512 adapter->surprise_removed = true;
513 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700514 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700515done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700516 if (adapter->cal_data) {
517 release_firmware(adapter->cal_data);
518 adapter->cal_data = NULL;
519 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700520 if (adapter->firmware) {
521 release_firmware(adapter->firmware);
522 adapter->firmware = NULL;
523 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700524 complete(&adapter->fw_load);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700525 if (init_failed)
526 mwifiex_free_adapter(adapter);
527 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700528 return;
529}
530
531/*
532 * This function initializes the hardware and gets firmware.
533 */
534static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
535{
536 int ret;
537
538 init_completion(&adapter->fw_load);
539 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
540 adapter->dev, GFP_KERNEL, adapter,
541 mwifiex_fw_dpc);
542 if (ret < 0)
543 dev_err(adapter->dev,
544 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700545 return ret;
546}
547
548/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700549 * CFG802.11 network device handler for open.
550 *
551 * Starts the data queue.
552 */
553static int
554mwifiex_open(struct net_device *dev)
555{
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800556 netif_tx_start_all_queues(dev);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700557 return 0;
558}
559
560/*
561 * CFG802.11 network device handler for close.
562 */
563static int
564mwifiex_close(struct net_device *dev)
565{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700566 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
567
568 if (priv->scan_request) {
569 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
570 cfg80211_scan_done(priv->scan_request, 1);
571 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700572 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700573 }
574
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700575 return 0;
576}
577
578/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700579 * Add buffer into wmm tx queue and queue work to transmit it.
580 */
581int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
582{
Avinash Patil47411a02012-11-01 18:44:16 -0700583 struct netdev_queue *txq;
584 int index = mwifiex_1d_to_wmm_queue[skb->priority];
585
586 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
587 txq = netdev_get_tx_queue(priv->netdev, index);
588 if (!netif_tx_queue_stopped(txq)) {
589 netif_tx_stop_queue(txq);
590 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
591 }
592 }
593
Stone Piaoe39faa72012-09-25 20:23:32 -0700594 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700595 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700596
597 if (priv->adapter->scan_delay_cnt)
598 atomic_set(&priv->adapter->is_tx_received, true);
599
Stone Piaoe39faa72012-09-25 20:23:32 -0700600 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
601
602 return 0;
603}
604
605/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700606 * CFG802.11 network device handler for data transmission.
607 */
608static int
609mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
610{
611 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700612 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700613 struct mwifiex_txinfo *tx_info;
Avinash Patil47411a02012-11-01 18:44:16 -0700614 struct timeval tv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700615
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800616 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700617 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700618
619 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200620 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700621 priv->stats.tx_dropped++;
622 return 0;
623 }
624 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
625 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200626 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700627 priv->stats.tx_dropped++;
628 return 0;
629 }
630 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
631 dev_dbg(priv->adapter->dev,
632 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700633 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700634 /* Insufficient skb headroom - allocate a new skb */
635 new_skb =
636 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
637 if (unlikely(!new_skb)) {
638 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200639 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700640 priv->stats.tx_dropped++;
641 return 0;
642 }
643 kfree_skb(skb);
644 skb = new_skb;
645 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700646 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700647 }
648
649 tx_info = MWIFIEX_SKB_TXCB(skb);
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800650 tx_info->bss_num = priv->bss_num;
651 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800652 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700653
654 /* Record the current time the packet was queued; used to
655 * determine the amount of time the packet was queued in
656 * the driver before it was sent to the firmware.
657 * The delay is then sent along with the packet to the
658 * firmware for aggregate delay calculation for stats and
659 * MSDU lifetime expiry.
660 */
661 do_gettimeofday(&tv);
662 skb->tstamp = timeval_to_ktime(tv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700663
Stone Piaoe39faa72012-09-25 20:23:32 -0700664 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700665
666 return 0;
667}
668
669/*
670 * CFG802.11 network device handler for setting MAC address.
671 */
672static int
673mwifiex_set_mac_address(struct net_device *dev, void *addr)
674{
675 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700676 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700677 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700678
679 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
680
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700681 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800682 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
683 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700684
685 if (!ret)
686 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
687 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700688 dev_err(priv->adapter->dev,
689 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700690
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700691 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
692
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700693 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700694}
695
696/*
697 * CFG802.11 network device handler for setting multicast list.
698 */
699static void mwifiex_set_multicast_list(struct net_device *dev)
700{
701 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700702 struct mwifiex_multicast_list mcast_list;
703
704 if (dev->flags & IFF_PROMISC) {
705 mcast_list.mode = MWIFIEX_PROMISC_MODE;
706 } else if (dev->flags & IFF_ALLMULTI ||
707 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
708 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
709 } else {
710 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400711 mcast_list.num_multicast_addr =
712 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700713 }
714 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700715}
716
717/*
718 * CFG802.11 network device handler for transmission timeout.
719 */
720static void
721mwifiex_tx_timeout(struct net_device *dev)
722{
723 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
724
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700725 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800726 priv->tx_timeout_cnt++;
727 dev_err(priv->adapter->dev,
728 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
729 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
730 mwifiex_set_trans_start(dev);
731
732 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
733 priv->adapter->if_ops.card_reset) {
734 dev_err(priv->adapter->dev,
735 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
736 priv->adapter->if_ops.card_reset(priv->adapter);
737 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700738}
739
740/*
741 * CFG802.11 network device handler for statistics retrieval.
742 */
743static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
744{
745 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
746
747 return &priv->stats;
748}
749
Avinash Patil47411a02012-11-01 18:44:16 -0700750static u16
Jason Wangf663dd92014-01-10 16:18:26 +0800751mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100752 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -0700753{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800754 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -0700755 return mwifiex_1d_to_wmm_queue[skb->priority];
756}
757
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700758/* Network device handlers */
759static const struct net_device_ops mwifiex_netdev_ops = {
760 .ndo_open = mwifiex_open,
761 .ndo_stop = mwifiex_close,
762 .ndo_start_xmit = mwifiex_hard_start_xmit,
763 .ndo_set_mac_address = mwifiex_set_mac_address,
764 .ndo_tx_timeout = mwifiex_tx_timeout,
765 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000766 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700767 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700768};
769
770/*
771 * This function initializes the private structure parameters.
772 *
773 * The following wait queues are initialized -
774 * - IOCTL wait queue
775 * - Command wait queue
776 * - Statistics wait queue
777 *
778 * ...and the following default parameters are set -
779 * - Current key index : Set to 0
780 * - Rate index : Set to auto
781 * - Media connected : Set to disconnected
782 * - Adhoc link sensed : Set to false
783 * - Nick name : Set to null
784 * - Number of Tx timeout : Set to 0
785 * - Device address : Set to current address
786 *
787 * In addition, the CFG80211 work queue is also created.
788 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700789void mwifiex_init_priv_params(struct mwifiex_private *priv,
790 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700791{
792 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700793 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700794 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700795 priv->current_key_index = 0;
796 priv->media_connected = false;
797 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700798 memset(priv->mgmt_ie, 0,
799 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700800 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
801 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
802 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
803 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700804 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700805 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
806}
807
808/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700809 * This function check if command is pending.
810 */
811int is_command_pending(struct mwifiex_adapter *adapter)
812{
813 unsigned long flags;
814 int is_cmd_pend_q_empty;
815
816 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
817 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
818 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
819
820 return !is_cmd_pend_q_empty;
821}
822
823/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700824 * This is the main work queue function.
825 *
826 * It handles the main process, which in turn handles the complete
827 * driver operations.
828 */
829static void mwifiex_main_work_queue(struct work_struct *work)
830{
831 struct mwifiex_adapter *adapter =
832 container_of(work, struct mwifiex_adapter, main_work);
833
834 if (adapter->surprise_removed)
835 return;
836 mwifiex_main_process(adapter);
837}
838
839/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700840 * This function adds the card.
841 *
842 * This function follows the following major steps to set up the device -
843 * - Initialize software. This includes probing the card, registering
844 * the interface operations table, and allocating/initializing the
845 * adapter structure
846 * - Set up the netlink socket
847 * - Create and start the main work queue
848 * - Register the device
849 * - Initialize firmware and hardware
850 * - Add logical interfaces
851 */
852int
853mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700854 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700855{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700856 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700857
858 if (down_interruptible(sem))
859 goto exit_sem_err;
860
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700861 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700862 pr_err("%s: software init failed\n", __func__);
863 goto err_init_sw;
864 }
865
Amitkumar Karward930fae2011-10-11 17:41:21 -0700866 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700867 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700868
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700869 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700870 adapter->surprise_removed = false;
871 init_waitqueue_head(&adapter->init_wait_q);
872 adapter->is_suspended = false;
873 adapter->hs_activated = false;
874 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700875 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700876 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700877 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700878
Amitkumar Karwar448a71c2013-10-11 18:33:04 -0700879 adapter->workqueue =
880 alloc_workqueue("MWIFIEX_WORK_QUEUE",
881 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700882 if (!adapter->workqueue)
883 goto err_kmalloc;
884
885 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
886
887 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -0400888 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700889 if (adapter->if_ops.register_dev(adapter)) {
890 pr_err("%s: failed to register mwifiex device\n", __func__);
891 goto err_registerdev;
892 }
893
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894 if (mwifiex_init_hw_fw(adapter)) {
895 pr_err("%s: firmware init failed\n", __func__);
896 goto err_init_fw;
897 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700898
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700899 return 0;
900
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700901err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700902 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700903 if (adapter->if_ops.unregister_dev)
904 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700905 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
906 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
907 pr_debug("info: %s: shutdown mwifiex\n", __func__);
908 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700909
910 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700911 wait_event_interruptible(adapter->init_wait_q,
912 adapter->init_wait_q_woken);
913 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700914err_registerdev:
915 adapter->surprise_removed = true;
916 mwifiex_terminate_workqueue(adapter);
917err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700918 mwifiex_free_adapter(adapter);
919
920err_init_sw:
921 up(sem);
922
923exit_sem_err:
924 return -1;
925}
926EXPORT_SYMBOL_GPL(mwifiex_add_card);
927
928/*
929 * This function removes the card.
930 *
931 * This function follows the following major steps to remove the device -
932 * - Stop data traffic
933 * - Shutdown firmware
934 * - Remove the logical interfaces
935 * - Terminate the work queue
936 * - Unregister the device
937 * - Free the adapter structure
938 */
939int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
940{
941 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700942 int i;
943
944 if (down_interruptible(sem))
945 goto exit_sem_err;
946
947 if (!adapter)
948 goto exit_remove;
949
Daniel Drake232fde02013-07-13 10:57:10 -0400950 /* We can no longer handle interrupts once we start doing the teardown
951 * below. */
952 if (adapter->if_ops.disable_int)
953 adapter->if_ops.disable_int(adapter);
954
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700955 adapter->surprise_removed = true;
956
957 /* Stop data */
958 for (i = 0; i < adapter->priv_num; i++) {
959 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700960 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -0700961 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700962 if (netif_carrier_ok(priv->netdev))
963 netif_carrier_off(priv->netdev);
964 }
965 }
966
967 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
968 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700969
970 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700971 wait_event_interruptible(adapter->init_wait_q,
972 adapter->init_wait_q_woken);
973 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
974 if (atomic_read(&adapter->rx_pending) ||
975 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700976 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700977 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700978 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700979 atomic_read(&adapter->rx_pending),
980 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700981 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700982 }
983
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700984 for (i = 0; i < adapter->priv_num; i++) {
985 priv = adapter->priv[i];
986
987 if (!priv)
988 continue;
989
990 rtnl_lock();
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800991 if (priv->wdev && priv->netdev)
Johannes Berg84efbb82012-06-16 00:00:26 +0200992 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700993 rtnl_unlock();
994 }
995
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -0800996 wiphy_unregister(adapter->wiphy);
997 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -0700998
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700999 mwifiex_terminate_workqueue(adapter);
1000
1001 /* Unregister device */
1002 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001003 if (adapter->if_ops.unregister_dev)
1004 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001005 /* Free adapter structure */
1006 dev_dbg(adapter->dev, "info: free adapter\n");
1007 mwifiex_free_adapter(adapter);
1008
1009exit_remove:
1010 up(sem);
1011exit_sem_err:
1012 return 0;
1013}
1014EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1015
1016/*
1017 * This function initializes the module.
1018 *
1019 * The debug FS is also initialized if configured.
1020 */
1021static int
1022mwifiex_init_module(void)
1023{
1024#ifdef CONFIG_DEBUG_FS
1025 mwifiex_debugfs_init();
1026#endif
1027 return 0;
1028}
1029
1030/*
1031 * This function cleans up the module.
1032 *
1033 * The debug FS is removed if available.
1034 */
1035static void
1036mwifiex_cleanup_module(void)
1037{
1038#ifdef CONFIG_DEBUG_FS
1039 mwifiex_debugfs_remove();
1040#endif
1041}
1042
1043module_init(mwifiex_init_module);
1044module_exit(mwifiex_cleanup_module);
1045
1046MODULE_AUTHOR("Marvell International Ltd.");
1047MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1048MODULE_VERSION(VERSION);
1049MODULE_LICENSE("GPL v2");