blob: 3402bffdd016ca21ff1ce424d98772d9f24ee3d5 [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
41 if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT) {
42 /*
43 * Abort scan operation by cancelling all pending scan
44 * commands
45 */
46 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
47 list_for_each_entry_safe(cmd_node, tmp_node,
48 &adapter->scan_pending_q, list) {
49 list_del(&cmd_node->list);
50 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
51 }
52 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
53
54 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
55 adapter->scan_processing = false;
56 adapter->scan_delay_cnt = 0;
57 adapter->empty_tx_q_cnt = 0;
58 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
59
60 if (priv->scan_request) {
61 dev_dbg(adapter->dev, "info: aborting scan\n");
62 cfg80211_scan_done(priv->scan_request, 1);
63 priv->scan_request = NULL;
64 } else {
65 priv->scan_aborting = false;
66 dev_dbg(adapter->dev, "info: scan already aborted\n");
67 }
68 goto done;
69 }
70
71 if (!atomic_read(&priv->adapter->is_tx_received)) {
72 adapter->empty_tx_q_cnt++;
73 if (adapter->empty_tx_q_cnt == MWIFIEX_MAX_EMPTY_TX_Q_CNT) {
74 /*
75 * No Tx traffic for 200msec. Get scan command from
76 * scan pending queue and put to cmd pending queue to
77 * resume scan operation
78 */
79 adapter->scan_delay_cnt = 0;
80 adapter->empty_tx_q_cnt = 0;
81 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
82 cmd_node = list_first_entry(&adapter->scan_pending_q,
83 struct cmd_ctrl_node, list);
84 list_del(&cmd_node->list);
85 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
86 flags);
87
88 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
89 true);
90 queue_work(adapter->workqueue, &adapter->main_work);
91 goto done;
92 }
93 } else {
94 adapter->empty_tx_q_cnt = 0;
95 }
96
97 /* Delay scan operation further by 20msec */
98 mod_timer(&priv->scan_delay_timer, jiffies +
99 msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
100 adapter->scan_delay_cnt++;
101
102done:
103 if (atomic_read(&priv->adapter->is_tx_received))
104 atomic_set(&priv->adapter->is_tx_received, false);
105
106 return;
107}
108
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700109/*
110 * This function registers the device and performs all the necessary
111 * initializations.
112 *
113 * The following initialization operations are performed -
114 * - Allocate adapter structure
115 * - Save interface specific operations table in adapter
116 * - Call interface specific initialization routine
117 * - Allocate private structures
118 * - Set default adapter structure parameters
119 * - Initialize locks
120 *
121 * In case of any errors during inittialization, this function also ensures
122 * proper cleanup before exiting.
123 */
124static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530125 void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700126{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700127 struct mwifiex_adapter *adapter;
128 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700129
130 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700131 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200132 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700133
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530134 *padapter = adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700135 adapter->card = card;
136
137 /* Save interface specific operations in adapter */
138 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
139
140 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700141 if (adapter->if_ops.init_if)
142 if (adapter->if_ops.init_if(adapter))
143 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700144
145 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700146
Avinash Patil64b05e22012-05-08 18:30:13 -0700147 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
148 /* Allocate memory for private structure */
149 adapter->priv[i] =
150 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
151 if (!adapter->priv[i])
152 goto error;
153
154 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -0700155 adapter->priv_num++;
Amitkumar Karwardf810082013-06-04 16:19:56 -0700156
157 setup_timer(&adapter->priv[i]->scan_delay_timer,
158 scan_delay_timer_fn,
159 (unsigned long)adapter->priv[i]);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700160 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -0700161 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700162
163 init_timer(&adapter->cmd_timer);
164 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
165 adapter->cmd_timer.data = (unsigned long) adapter;
166
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700167 return 0;
168
169error:
170 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
171
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700172 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700173 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700174
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700175 kfree(adapter);
176
177 return -1;
178}
179
180/*
181 * This function unregisters the device and performs all the necessary
182 * cleanups.
183 *
184 * The following cleanup operations are performed -
185 * - Free the timers
186 * - Free beacon buffers
187 * - Free private structures
188 * - Free adapter structure
189 */
190static int mwifiex_unregister(struct mwifiex_adapter *adapter)
191{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700192 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700193
Amitkumar Karwar4cfda672013-07-22 19:17:50 -0700194 if (adapter->if_ops.cleanup_if)
195 adapter->if_ops.cleanup_if(adapter);
196
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700197 del_timer(&adapter->cmd_timer);
198
199 /* Free private structures */
200 for (i = 0; i < adapter->priv_num; i++) {
201 if (adapter->priv[i]) {
202 mwifiex_free_curr_bcn(adapter->priv[i]);
Amitkumar Karwar003d87c2013-07-22 19:17:48 -0700203 del_timer_sync(&adapter->priv[i]->scan_delay_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700204 kfree(adapter->priv[i]);
205 }
206 }
207
208 kfree(adapter);
209 return 0;
210}
211
212/*
213 * The main process.
214 *
215 * This function is the main procedure of the driver and handles various driver
216 * operations. It runs in a loop and provides the core functionalities.
217 *
218 * The main responsibilities of this function are -
219 * - Ensure concurrency control
220 * - Handle pending interrupts and call interrupt handlers
221 * - Wake up the card if required
222 * - Handle command responses and call response handlers
223 * - Handle events and call event handlers
224 * - Execute pending commands
225 * - Transmit pending data packets
226 */
227int mwifiex_main_process(struct mwifiex_adapter *adapter)
228{
229 int ret = 0;
230 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700231 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700232
233 spin_lock_irqsave(&adapter->main_proc_lock, flags);
234
235 /* Check if already processing */
236 if (adapter->mwifiex_processing) {
237 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
238 goto exit_main_proc;
239 } else {
240 adapter->mwifiex_processing = true;
241 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
242 }
243process_start:
244 do {
245 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
246 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
247 break;
248
249 /* Handle pending interrupt if any */
250 if (adapter->int_status) {
251 if (adapter->hs_activated)
252 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700253 if (adapter->if_ops.process_int_status)
254 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700255 }
256
257 /* Need to wake up the card ? */
258 if ((adapter->ps_state == PS_STATE_SLEEP) &&
259 (adapter->pm_wakeup_card_req &&
260 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700261 (is_command_pending(adapter) ||
262 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700263 adapter->pm_wakeup_fw_try = true;
264 adapter->if_ops.wakeup(adapter);
265 continue;
266 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700267
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700268 if (IS_CARD_RX_RCVD(adapter)) {
269 adapter->pm_wakeup_fw_try = false;
270 if (adapter->ps_state == PS_STATE_SLEEP)
271 adapter->ps_state = PS_STATE_AWAKE;
272 } else {
273 /* We have tried to wakeup the card already */
274 if (adapter->pm_wakeup_fw_try)
275 break;
276 if (adapter->ps_state != PS_STATE_AWAKE ||
277 adapter->tx_lock_flag)
278 break;
279
Amitkumar Karwarf931c772012-06-20 19:58:36 -0700280 if ((adapter->scan_processing &&
281 !adapter->scan_delay_cnt) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700282 mwifiex_wmm_lists_empty(adapter)) {
283 if (adapter->cmd_sent || adapter->curr_cmd ||
284 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700285 break;
286 }
287 }
288
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700289 /* Check Rx data for USB */
290 if (adapter->iface_type == MWIFIEX_USB)
291 while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
292 mwifiex_handle_rx_packet(adapter, skb);
293
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700294 /* Check for Cmd Resp */
295 if (adapter->cmd_resp_received) {
296 adapter->cmd_resp_received = false;
297 mwifiex_process_cmdresp(adapter);
298
299 /* call mwifiex back when init_fw is done */
300 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
301 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
302 mwifiex_init_fw_complete(adapter);
303 }
304 }
305
306 /* Check for event */
307 if (adapter->event_received) {
308 adapter->event_received = false;
309 mwifiex_process_event(adapter);
310 }
311
312 /* Check if we need to confirm Sleep Request
313 received previously */
314 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
315 if (!adapter->cmd_sent && !adapter->curr_cmd)
316 mwifiex_check_ps_cond(adapter);
317 }
318
319 /* * The ps_state may have been changed during processing of
320 * Sleep Request event.
321 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700322 if ((adapter->ps_state == PS_STATE_SLEEP) ||
323 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
324 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
325 adapter->tx_lock_flag)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700326 continue;
327
328 if (!adapter->cmd_sent && !adapter->curr_cmd) {
329 if (mwifiex_exec_next_cmd(adapter) == -1) {
330 ret = -1;
331 break;
332 }
333 }
334
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700335 if ((!adapter->scan_processing || adapter->scan_delay_cnt) &&
336 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700337 mwifiex_wmm_process_tx(adapter);
338 if (adapter->hs_activated) {
339 adapter->is_hs_configured = false;
340 mwifiex_hs_activated_event
341 (mwifiex_get_priv
342 (adapter, MWIFIEX_BSS_ROLE_ANY),
343 false);
344 }
345 }
346
347 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700348 !adapter->curr_cmd && !is_command_pending(adapter) &&
349 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700350 if (!mwifiex_send_null_packet
351 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
352 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
353 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
354 adapter->delay_null_pkt = false;
355 adapter->ps_state = PS_STATE_SLEEP;
356 }
357 break;
358 }
359 } while (true);
360
361 if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter))
362 goto process_start;
363
364 spin_lock_irqsave(&adapter->main_proc_lock, flags);
365 adapter->mwifiex_processing = false;
366 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
367
368exit_main_proc:
369 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
370 mwifiex_shutdown_drv(adapter);
371 return ret;
372}
Bing Zhao601216e2012-11-05 16:59:15 -0800373EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700374
375/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700376 * This function frees the adapter structure.
377 *
378 * Additionally, this closes the netlink socket, frees the timers
379 * and private structures.
380 */
381static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
382{
383 if (!adapter) {
384 pr_err("%s: adapter is NULL\n", __func__);
385 return;
386 }
387
388 mwifiex_unregister(adapter);
389 pr_debug("info: %s: free adapter\n", __func__);
390}
391
392/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700393 * This function cancels all works in the queue and destroys
394 * the main workqueue.
395 */
396static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
397{
398 flush_workqueue(adapter->workqueue);
399 destroy_workqueue(adapter->workqueue);
400 adapter->workqueue = NULL;
401}
402
403/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700404 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700405 *
406 * The main initialization steps followed are -
407 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700408 * - Issue the init commands to firmware
409 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700410static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700411{
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700412 int ret, i;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700413 char fmt[64];
414 struct mwifiex_private *priv;
415 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700416 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700417 struct semaphore *sem = adapter->card_sem;
418 bool init_failed = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700419
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700420 if (!firmware) {
421 dev_err(adapter->dev,
422 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700423 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700424 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700425
426 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
427 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700428 fw.fw_buf = (u8 *) adapter->firmware->data;
429 fw.fw_len = adapter->firmware->size;
430
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700431 if (adapter->if_ops.dnld_fw)
432 ret = adapter->if_ops.dnld_fw(adapter, &fw);
433 else
434 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700435 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700436 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700437
438 dev_notice(adapter->dev, "WLAN FW is active\n");
439
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700440 if (cal_data_cfg) {
441 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
442 adapter->dev)) < 0)
443 dev_err(adapter->dev,
444 "Cal data request_firmware() failed\n");
445 }
446
Daniel Drake232fde02013-07-13 10:57:10 -0400447 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700448 if (adapter->if_ops.enable_int) {
449 if (adapter->if_ops.enable_int(adapter))
450 goto err_dnld_fw;
451 }
Daniel Drake232fde02013-07-13 10:57:10 -0400452
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700453 adapter->init_wait_q_woken = false;
454 ret = mwifiex_init_fw(adapter);
455 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700456 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700457 } else if (!ret) {
458 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
459 goto done;
460 }
461 /* Wait for mwifiex_init to complete */
462 wait_event_interruptible(adapter->init_wait_q,
463 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700464 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700465 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700466
Avinash Patild6bffe82012-05-08 18:30:15 -0700467 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
468 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700469 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700470 goto err_register_cfg80211;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700471 }
472
473 rtnl_lock();
474 /* Create station interface by default */
Avinash Patild6bffe82012-05-08 18:30:15 -0700475 if (!mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700476 NL80211_IFTYPE_STATION, NULL, NULL)) {
477 dev_err(adapter->dev, "cannot create default STA interface\n");
478 goto err_add_intf;
479 }
Avinash Patild6bffe82012-05-08 18:30:15 -0700480
481 /* Create AP interface by default */
482 if (!mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
483 NL80211_IFTYPE_AP, NULL, NULL)) {
484 dev_err(adapter->dev, "cannot create default AP interface\n");
485 goto err_add_intf;
486 }
Stone Piao197f4a22012-09-25 20:23:40 -0700487
488 /* Create P2P interface by default */
489 if (!mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
490 NL80211_IFTYPE_P2P_CLIENT, NULL, NULL)) {
491 dev_err(adapter->dev, "cannot create default P2P interface\n");
492 goto err_add_intf;
493 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700494 rtnl_unlock();
495
496 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
497 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
498 goto done;
499
500err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700501 for (i = 0; i < adapter->priv_num; i++) {
502 priv = adapter->priv[i];
503
504 if (!priv)
505 continue;
506
507 if (priv->wdev && priv->netdev)
508 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
509 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700510 rtnl_unlock();
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700511err_register_cfg80211:
512 wiphy_unregister(adapter->wiphy);
513 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700514err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400515 if (adapter->if_ops.disable_int)
516 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700517err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700518 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700519 if (adapter->if_ops.unregister_dev)
520 adapter->if_ops.unregister_dev(adapter);
521
522 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
523 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
524 pr_debug("info: %s: shutdown mwifiex\n", __func__);
525 adapter->init_wait_q_woken = false;
526
527 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
528 wait_event_interruptible(adapter->init_wait_q,
529 adapter->init_wait_q_woken);
530 }
531 adapter->surprise_removed = true;
532 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700533 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700534done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700535 if (adapter->cal_data) {
536 release_firmware(adapter->cal_data);
537 adapter->cal_data = NULL;
538 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700539 if (adapter->firmware) {
540 release_firmware(adapter->firmware);
541 adapter->firmware = NULL;
542 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700543 complete(&adapter->fw_load);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700544 if (init_failed)
545 mwifiex_free_adapter(adapter);
546 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700547 return;
548}
549
550/*
551 * This function initializes the hardware and gets firmware.
552 */
553static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
554{
555 int ret;
556
557 init_completion(&adapter->fw_load);
558 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
559 adapter->dev, GFP_KERNEL, adapter,
560 mwifiex_fw_dpc);
561 if (ret < 0)
562 dev_err(adapter->dev,
563 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700564 return ret;
565}
566
567/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700568 * CFG802.11 network device handler for open.
569 *
570 * Starts the data queue.
571 */
572static int
573mwifiex_open(struct net_device *dev)
574{
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800575 netif_tx_start_all_queues(dev);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700576 return 0;
577}
578
579/*
580 * CFG802.11 network device handler for close.
581 */
582static int
583mwifiex_close(struct net_device *dev)
584{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700585 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
586
587 if (priv->scan_request) {
588 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
589 cfg80211_scan_done(priv->scan_request, 1);
590 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700591 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700592 }
593
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700594 return 0;
595}
596
597/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700598 * Add buffer into wmm tx queue and queue work to transmit it.
599 */
600int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
601{
Avinash Patil47411a02012-11-01 18:44:16 -0700602 struct netdev_queue *txq;
603 int index = mwifiex_1d_to_wmm_queue[skb->priority];
604
605 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
606 txq = netdev_get_tx_queue(priv->netdev, index);
607 if (!netif_tx_queue_stopped(txq)) {
608 netif_tx_stop_queue(txq);
609 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
610 }
611 }
612
Stone Piaoe39faa72012-09-25 20:23:32 -0700613 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700614 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700615
616 if (priv->adapter->scan_delay_cnt)
617 atomic_set(&priv->adapter->is_tx_received, true);
618
Stone Piaoe39faa72012-09-25 20:23:32 -0700619 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
620
621 return 0;
622}
623
624/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700625 * CFG802.11 network device handler for data transmission.
626 */
627static int
628mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
629{
630 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700631 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700632 struct mwifiex_txinfo *tx_info;
Avinash Patil47411a02012-11-01 18:44:16 -0700633 struct timeval tv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700634
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800635 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700636 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700637
638 if (priv->adapter->surprise_removed) {
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 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
644 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200645 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700646 priv->stats.tx_dropped++;
647 return 0;
648 }
649 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
650 dev_dbg(priv->adapter->dev,
651 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700652 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700653 /* Insufficient skb headroom - allocate a new skb */
654 new_skb =
655 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
656 if (unlikely(!new_skb)) {
657 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200658 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700659 priv->stats.tx_dropped++;
660 return 0;
661 }
662 kfree_skb(skb);
663 skb = new_skb;
664 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700665 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700666 }
667
668 tx_info = MWIFIEX_SKB_TXCB(skb);
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800669 tx_info->bss_num = priv->bss_num;
670 tx_info->bss_type = priv->bss_type;
Avinash Patil47411a02012-11-01 18:44:16 -0700671
672 /* Record the current time the packet was queued; used to
673 * determine the amount of time the packet was queued in
674 * the driver before it was sent to the firmware.
675 * The delay is then sent along with the packet to the
676 * firmware for aggregate delay calculation for stats and
677 * MSDU lifetime expiry.
678 */
679 do_gettimeofday(&tv);
680 skb->tstamp = timeval_to_ktime(tv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700681
Stone Piaoe39faa72012-09-25 20:23:32 -0700682 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700683
684 return 0;
685}
686
687/*
688 * CFG802.11 network device handler for setting MAC address.
689 */
690static int
691mwifiex_set_mac_address(struct net_device *dev, void *addr)
692{
693 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700694 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700695 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700696
697 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
698
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700699 /* Send request to firmware */
700 ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
701 HostCmd_ACT_GEN_SET, 0, NULL);
702
703 if (!ret)
704 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
705 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700706 dev_err(priv->adapter->dev,
707 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700708
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700709 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
710
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700711 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700712}
713
714/*
715 * CFG802.11 network device handler for setting multicast list.
716 */
717static void mwifiex_set_multicast_list(struct net_device *dev)
718{
719 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700720 struct mwifiex_multicast_list mcast_list;
721
722 if (dev->flags & IFF_PROMISC) {
723 mcast_list.mode = MWIFIEX_PROMISC_MODE;
724 } else if (dev->flags & IFF_ALLMULTI ||
725 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
726 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
727 } else {
728 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400729 mcast_list.num_multicast_addr =
730 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700731 }
732 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700733}
734
735/*
736 * CFG802.11 network device handler for transmission timeout.
737 */
738static void
739mwifiex_tx_timeout(struct net_device *dev)
740{
741 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
742
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700743 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800744 priv->tx_timeout_cnt++;
745 dev_err(priv->adapter->dev,
746 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
747 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
748 mwifiex_set_trans_start(dev);
749
750 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
751 priv->adapter->if_ops.card_reset) {
752 dev_err(priv->adapter->dev,
753 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
754 priv->adapter->if_ops.card_reset(priv->adapter);
755 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700756}
757
758/*
759 * CFG802.11 network device handler for statistics retrieval.
760 */
761static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
762{
763 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
764
765 return &priv->stats;
766}
767
Avinash Patil47411a02012-11-01 18:44:16 -0700768static u16
769mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb)
770{
771 skb->priority = cfg80211_classify8021d(skb);
772 return mwifiex_1d_to_wmm_queue[skb->priority];
773}
774
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700775/* Network device handlers */
776static const struct net_device_ops mwifiex_netdev_ops = {
777 .ndo_open = mwifiex_open,
778 .ndo_stop = mwifiex_close,
779 .ndo_start_xmit = mwifiex_hard_start_xmit,
780 .ndo_set_mac_address = mwifiex_set_mac_address,
781 .ndo_tx_timeout = mwifiex_tx_timeout,
782 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000783 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700784 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700785};
786
787/*
788 * This function initializes the private structure parameters.
789 *
790 * The following wait queues are initialized -
791 * - IOCTL wait queue
792 * - Command wait queue
793 * - Statistics wait queue
794 *
795 * ...and the following default parameters are set -
796 * - Current key index : Set to 0
797 * - Rate index : Set to auto
798 * - Media connected : Set to disconnected
799 * - Adhoc link sensed : Set to false
800 * - Nick name : Set to null
801 * - Number of Tx timeout : Set to 0
802 * - Device address : Set to current address
803 *
804 * In addition, the CFG80211 work queue is also created.
805 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700806void mwifiex_init_priv_params(struct mwifiex_private *priv,
807 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700808{
809 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700810 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700811 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700812 priv->current_key_index = 0;
813 priv->media_connected = false;
814 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700815 memset(priv->mgmt_ie, 0,
816 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700817 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
818 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
819 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
820 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700821 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700822 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
823}
824
825/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700826 * This function check if command is pending.
827 */
828int is_command_pending(struct mwifiex_adapter *adapter)
829{
830 unsigned long flags;
831 int is_cmd_pend_q_empty;
832
833 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
834 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
835 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
836
837 return !is_cmd_pend_q_empty;
838}
839
840/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700841 * This is the main work queue function.
842 *
843 * It handles the main process, which in turn handles the complete
844 * driver operations.
845 */
846static void mwifiex_main_work_queue(struct work_struct *work)
847{
848 struct mwifiex_adapter *adapter =
849 container_of(work, struct mwifiex_adapter, main_work);
850
851 if (adapter->surprise_removed)
852 return;
853 mwifiex_main_process(adapter);
854}
855
856/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700857 * This function adds the card.
858 *
859 * This function follows the following major steps to set up the device -
860 * - Initialize software. This includes probing the card, registering
861 * the interface operations table, and allocating/initializing the
862 * adapter structure
863 * - Set up the netlink socket
864 * - Create and start the main work queue
865 * - Register the device
866 * - Initialize firmware and hardware
867 * - Add logical interfaces
868 */
869int
870mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700871 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700872{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700873 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700874
875 if (down_interruptible(sem))
876 goto exit_sem_err;
877
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700878 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700879 pr_err("%s: software init failed\n", __func__);
880 goto err_init_sw;
881 }
882
Amitkumar Karward930fae2011-10-11 17:41:21 -0700883 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700884 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700885
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700886 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700887 adapter->surprise_removed = false;
888 init_waitqueue_head(&adapter->init_wait_q);
889 adapter->is_suspended = false;
890 adapter->hs_activated = false;
891 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700892 adapter->cmd_wait_q_required = false;
893 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700894 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700895 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700896
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700897 adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
898 if (!adapter->workqueue)
899 goto err_kmalloc;
900
901 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
902
903 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -0400904 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700905 if (adapter->if_ops.register_dev(adapter)) {
906 pr_err("%s: failed to register mwifiex device\n", __func__);
907 goto err_registerdev;
908 }
909
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700910 if (mwifiex_init_hw_fw(adapter)) {
911 pr_err("%s: firmware init failed\n", __func__);
912 goto err_init_fw;
913 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700914
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700915 return 0;
916
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700917err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700918 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700919 if (adapter->if_ops.unregister_dev)
920 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700921 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
922 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
923 pr_debug("info: %s: shutdown mwifiex\n", __func__);
924 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700925
926 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700927 wait_event_interruptible(adapter->init_wait_q,
928 adapter->init_wait_q_woken);
929 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700930err_registerdev:
931 adapter->surprise_removed = true;
932 mwifiex_terminate_workqueue(adapter);
933err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700934 mwifiex_free_adapter(adapter);
935
936err_init_sw:
937 up(sem);
938
939exit_sem_err:
940 return -1;
941}
942EXPORT_SYMBOL_GPL(mwifiex_add_card);
943
944/*
945 * This function removes the card.
946 *
947 * This function follows the following major steps to remove the device -
948 * - Stop data traffic
949 * - Shutdown firmware
950 * - Remove the logical interfaces
951 * - Terminate the work queue
952 * - Unregister the device
953 * - Free the adapter structure
954 */
955int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
956{
957 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700958 int i;
959
960 if (down_interruptible(sem))
961 goto exit_sem_err;
962
963 if (!adapter)
964 goto exit_remove;
965
Daniel Drake232fde02013-07-13 10:57:10 -0400966 /* We can no longer handle interrupts once we start doing the teardown
967 * below. */
968 if (adapter->if_ops.disable_int)
969 adapter->if_ops.disable_int(adapter);
970
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700971 adapter->surprise_removed = true;
972
973 /* Stop data */
974 for (i = 0; i < adapter->priv_num; i++) {
975 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700976 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -0700977 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700978 if (netif_carrier_ok(priv->netdev))
979 netif_carrier_off(priv->netdev);
980 }
981 }
982
983 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
984 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700985
986 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700987 wait_event_interruptible(adapter->init_wait_q,
988 adapter->init_wait_q_woken);
989 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
990 if (atomic_read(&adapter->rx_pending) ||
991 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700992 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700993 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700994 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700995 atomic_read(&adapter->rx_pending),
996 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700997 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700998 }
999
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001000 for (i = 0; i < adapter->priv_num; i++) {
1001 priv = adapter->priv[i];
1002
1003 if (!priv)
1004 continue;
1005
1006 rtnl_lock();
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -08001007 if (priv->wdev && priv->netdev)
Johannes Berg84efbb82012-06-16 00:00:26 +02001008 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001009 rtnl_unlock();
1010 }
1011
Yogesh Ashok Powar3d82de02011-10-05 14:58:24 -07001012 priv = adapter->priv[0];
Avinash Patil64b05e22012-05-08 18:30:13 -07001013 if (!priv || !priv->wdev)
Yogesh Ashok Powar3d82de02011-10-05 14:58:24 -07001014 goto exit_remove;
1015
Avinash Patil64b05e22012-05-08 18:30:13 -07001016 wiphy_unregister(priv->wdev->wiphy);
1017 wiphy_free(priv->wdev->wiphy);
1018
1019 for (i = 0; i < adapter->priv_num; i++) {
1020 priv = adapter->priv[i];
1021 if (priv)
1022 kfree(priv->wdev);
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -08001023 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001024
1025 mwifiex_terminate_workqueue(adapter);
1026
1027 /* Unregister device */
1028 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001029 if (adapter->if_ops.unregister_dev)
1030 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001031 /* Free adapter structure */
1032 dev_dbg(adapter->dev, "info: free adapter\n");
1033 mwifiex_free_adapter(adapter);
1034
1035exit_remove:
1036 up(sem);
1037exit_sem_err:
1038 return 0;
1039}
1040EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1041
1042/*
1043 * This function initializes the module.
1044 *
1045 * The debug FS is also initialized if configured.
1046 */
1047static int
1048mwifiex_init_module(void)
1049{
1050#ifdef CONFIG_DEBUG_FS
1051 mwifiex_debugfs_init();
1052#endif
1053 return 0;
1054}
1055
1056/*
1057 * This function cleans up the module.
1058 *
1059 * The debug FS is removed if available.
1060 */
1061static void
1062mwifiex_cleanup_module(void)
1063{
1064#ifdef CONFIG_DEBUG_FS
1065 mwifiex_debugfs_remove();
1066#endif
1067}
1068
1069module_init(mwifiex_init_module);
1070module_exit(mwifiex_cleanup_module);
1071
1072MODULE_AUTHOR("Marvell International Ltd.");
1073MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1074MODULE_VERSION(VERSION);
1075MODULE_LICENSE("GPL v2");