blob: dfa37eadc4db566ecc75db890c838d8d04600917 [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: major functions
3 *
Xinming Hu65da33f2014-06-19 21:38:57 -07004 * Copyright (C) 2011-2014, Marvell International Ltd.
Bing Zhao5e6e3a92011-03-21 18:00:50 -07005 *
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;
Amitkumar Karward5343f02014-07-11 20:53:14 -070036 spinlock_t *scan_q_lock = &adapter->scan_pending_q_lock;
Amitkumar Karwardf810082013-06-04 16:19:56 -070037 unsigned long flags;
38
39 if (adapter->surprise_removed)
40 return;
41
Amitkumar Karwarf5e8560e2014-03-25 19:01:18 -070042 if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT ||
43 !adapter->scan_processing) {
Amitkumar Karwardf810082013-06-04 16:19:56 -070044 /*
45 * Abort scan operation by cancelling all pending scan
46 * commands
47 */
Amitkumar Karward5343f02014-07-11 20:53:14 -070048 spin_lock_irqsave(scan_q_lock, flags);
Amitkumar Karwardf810082013-06-04 16:19:56 -070049 list_for_each_entry_safe(cmd_node, tmp_node,
50 &adapter->scan_pending_q, list) {
51 list_del(&cmd_node->list);
52 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
53 }
Amitkumar Karward5343f02014-07-11 20:53:14 -070054 spin_unlock_irqrestore(scan_q_lock, flags);
Amitkumar Karwardf810082013-06-04 16:19:56 -070055
56 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
57 adapter->scan_processing = false;
58 adapter->scan_delay_cnt = 0;
59 adapter->empty_tx_q_cnt = 0;
60 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
61
62 if (priv->scan_request) {
63 dev_dbg(adapter->dev, "info: aborting scan\n");
64 cfg80211_scan_done(priv->scan_request, 1);
65 priv->scan_request = NULL;
66 } else {
67 priv->scan_aborting = false;
68 dev_dbg(adapter->dev, "info: scan already aborted\n");
69 }
70 goto done;
71 }
72
73 if (!atomic_read(&priv->adapter->is_tx_received)) {
74 adapter->empty_tx_q_cnt++;
75 if (adapter->empty_tx_q_cnt == MWIFIEX_MAX_EMPTY_TX_Q_CNT) {
76 /*
77 * No Tx traffic for 200msec. Get scan command from
78 * scan pending queue and put to cmd pending queue to
79 * resume scan operation
80 */
81 adapter->scan_delay_cnt = 0;
82 adapter->empty_tx_q_cnt = 0;
Amitkumar Karward5343f02014-07-11 20:53:14 -070083 spin_lock_irqsave(scan_q_lock, flags);
84
85 if (list_empty(&adapter->scan_pending_q)) {
86 spin_unlock_irqrestore(scan_q_lock, flags);
87 goto done;
88 }
89
Amitkumar Karwardf810082013-06-04 16:19:56 -070090 cmd_node = list_first_entry(&adapter->scan_pending_q,
91 struct cmd_ctrl_node, list);
92 list_del(&cmd_node->list);
Amitkumar Karward5343f02014-07-11 20:53:14 -070093 spin_unlock_irqrestore(scan_q_lock, flags);
Amitkumar Karwardf810082013-06-04 16:19:56 -070094
95 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
96 true);
97 queue_work(adapter->workqueue, &adapter->main_work);
98 goto done;
99 }
100 } else {
101 adapter->empty_tx_q_cnt = 0;
102 }
103
104 /* Delay scan operation further by 20msec */
105 mod_timer(&priv->scan_delay_timer, jiffies +
106 msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
107 adapter->scan_delay_cnt++;
108
109done:
110 if (atomic_read(&priv->adapter->is_tx_received))
111 atomic_set(&priv->adapter->is_tx_received, false);
112
113 return;
114}
115
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700116/*
117 * This function registers the device and performs all the necessary
118 * initializations.
119 *
120 * The following initialization operations are performed -
121 * - Allocate adapter structure
122 * - Save interface specific operations table in adapter
123 * - Call interface specific initialization routine
124 * - Allocate private structures
125 * - Set default adapter structure parameters
126 * - Initialize locks
127 *
128 * In case of any errors during inittialization, this function also ensures
129 * proper cleanup before exiting.
130 */
131static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530132 void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700133{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700134 struct mwifiex_adapter *adapter;
135 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700136
137 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700138 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200139 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700140
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530141 *padapter = adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700142 adapter->card = card;
143
144 /* Save interface specific operations in adapter */
145 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
146
147 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700148 if (adapter->if_ops.init_if)
149 if (adapter->if_ops.init_if(adapter))
150 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700151
152 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700153
Avinash Patil64b05e22012-05-08 18:30:13 -0700154 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
155 /* Allocate memory for private structure */
156 adapter->priv[i] =
157 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
158 if (!adapter->priv[i])
159 goto error;
160
161 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -0700162 adapter->priv_num++;
Amitkumar Karwardf810082013-06-04 16:19:56 -0700163
164 setup_timer(&adapter->priv[i]->scan_delay_timer,
165 scan_delay_timer_fn,
166 (unsigned long)adapter->priv[i]);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700167 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -0700168 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700169
170 init_timer(&adapter->cmd_timer);
171 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
172 adapter->cmd_timer.data = (unsigned long) adapter;
173
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700174 return 0;
175
176error:
177 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
178
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700179 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700180 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700181
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700182 kfree(adapter);
183
184 return -1;
185}
186
187/*
188 * This function unregisters the device and performs all the necessary
189 * cleanups.
190 *
191 * The following cleanup operations are performed -
192 * - Free the timers
193 * - Free beacon buffers
194 * - Free private structures
195 * - Free adapter structure
196 */
197static int mwifiex_unregister(struct mwifiex_adapter *adapter)
198{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700199 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700200
Amitkumar Karwar4cfda672013-07-22 19:17:50 -0700201 if (adapter->if_ops.cleanup_if)
202 adapter->if_ops.cleanup_if(adapter);
203
Avinash Patil629873f2014-02-18 15:47:55 -0800204 del_timer_sync(&adapter->cmd_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700205
206 /* Free private structures */
207 for (i = 0; i < adapter->priv_num; i++) {
208 if (adapter->priv[i]) {
209 mwifiex_free_curr_bcn(adapter->priv[i]);
Amitkumar Karwar003d87c2013-07-22 19:17:48 -0700210 del_timer_sync(&adapter->priv[i]->scan_delay_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700211 kfree(adapter->priv[i]);
212 }
213 }
214
215 kfree(adapter);
216 return 0;
217}
218
219/*
220 * The main process.
221 *
222 * This function is the main procedure of the driver and handles various driver
223 * operations. It runs in a loop and provides the core functionalities.
224 *
225 * The main responsibilities of this function are -
226 * - Ensure concurrency control
227 * - Handle pending interrupts and call interrupt handlers
228 * - Wake up the card if required
229 * - Handle command responses and call response handlers
230 * - Handle events and call event handlers
231 * - Execute pending commands
232 * - Transmit pending data packets
233 */
234int mwifiex_main_process(struct mwifiex_adapter *adapter)
235{
236 int ret = 0;
237 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700238 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700239
240 spin_lock_irqsave(&adapter->main_proc_lock, flags);
241
242 /* Check if already processing */
243 if (adapter->mwifiex_processing) {
244 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
245 goto exit_main_proc;
246 } else {
247 adapter->mwifiex_processing = true;
248 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
249 }
250process_start:
251 do {
252 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
253 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
254 break;
255
256 /* Handle pending interrupt if any */
257 if (adapter->int_status) {
258 if (adapter->hs_activated)
259 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700260 if (adapter->if_ops.process_int_status)
261 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700262 }
263
264 /* Need to wake up the card ? */
265 if ((adapter->ps_state == PS_STATE_SLEEP) &&
266 (adapter->pm_wakeup_card_req &&
267 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700268 (is_command_pending(adapter) ||
269 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700270 adapter->pm_wakeup_fw_try = true;
271 adapter->if_ops.wakeup(adapter);
272 continue;
273 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700274
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700275 if (IS_CARD_RX_RCVD(adapter)) {
276 adapter->pm_wakeup_fw_try = false;
277 if (adapter->ps_state == PS_STATE_SLEEP)
278 adapter->ps_state = PS_STATE_AWAKE;
279 } else {
280 /* We have tried to wakeup the card already */
281 if (adapter->pm_wakeup_fw_try)
282 break;
283 if (adapter->ps_state != PS_STATE_AWAKE ||
284 adapter->tx_lock_flag)
285 break;
286
Amitkumar Karwarf931c7702012-06-20 19:58:36 -0700287 if ((adapter->scan_processing &&
288 !adapter->scan_delay_cnt) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700289 mwifiex_wmm_lists_empty(adapter)) {
290 if (adapter->cmd_sent || adapter->curr_cmd ||
291 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700292 break;
293 }
294 }
295
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700296 /* Check Rx data for USB */
297 if (adapter->iface_type == MWIFIEX_USB)
298 while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
299 mwifiex_handle_rx_packet(adapter, skb);
300
Amitkumar Karwar20474122014-04-14 15:31:05 -0700301 /* Check for event */
302 if (adapter->event_received) {
303 adapter->event_received = false;
304 mwifiex_process_event(adapter);
305 }
306
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700307 /* Check for Cmd Resp */
308 if (adapter->cmd_resp_received) {
309 adapter->cmd_resp_received = false;
310 mwifiex_process_cmdresp(adapter);
311
312 /* call mwifiex back when init_fw is done */
313 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
314 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
315 mwifiex_init_fw_complete(adapter);
316 }
317 }
318
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700319 /* Check if we need to confirm Sleep Request
320 received previously */
321 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
322 if (!adapter->cmd_sent && !adapter->curr_cmd)
323 mwifiex_check_ps_cond(adapter);
324 }
325
326 /* * The ps_state may have been changed during processing of
327 * Sleep Request event.
328 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700329 if ((adapter->ps_state == PS_STATE_SLEEP) ||
330 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
331 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
332 adapter->tx_lock_flag)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700333 continue;
334
335 if (!adapter->cmd_sent && !adapter->curr_cmd) {
336 if (mwifiex_exec_next_cmd(adapter) == -1) {
337 ret = -1;
338 break;
339 }
340 }
341
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700342 if ((!adapter->scan_processing || adapter->scan_delay_cnt) &&
343 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700344 mwifiex_wmm_process_tx(adapter);
345 if (adapter->hs_activated) {
346 adapter->is_hs_configured = false;
347 mwifiex_hs_activated_event
348 (mwifiex_get_priv
349 (adapter, MWIFIEX_BSS_ROLE_ANY),
350 false);
351 }
352 }
353
354 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700355 !adapter->curr_cmd && !is_command_pending(adapter) &&
356 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700357 if (!mwifiex_send_null_packet
358 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
359 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
360 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
361 adapter->delay_null_pkt = false;
362 adapter->ps_state = PS_STATE_SLEEP;
363 }
364 break;
365 }
366 } while (true);
367
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700368 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700369 if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter)) {
370 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
371 goto process_start;
372 }
373
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700374 adapter->mwifiex_processing = false;
375 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
376
377exit_main_proc:
378 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
379 mwifiex_shutdown_drv(adapter);
380 return ret;
381}
Bing Zhao601216e2012-11-05 16:59:15 -0800382EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700383
384/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700385 * This function frees the adapter structure.
386 *
387 * Additionally, this closes the netlink socket, frees the timers
388 * and private structures.
389 */
390static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
391{
392 if (!adapter) {
393 pr_err("%s: adapter is NULL\n", __func__);
394 return;
395 }
396
397 mwifiex_unregister(adapter);
398 pr_debug("info: %s: free adapter\n", __func__);
399}
400
401/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700402 * This function cancels all works in the queue and destroys
403 * the main workqueue.
404 */
405static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
406{
407 flush_workqueue(adapter->workqueue);
408 destroy_workqueue(adapter->workqueue);
409 adapter->workqueue = NULL;
410}
411
412/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700413 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700414 *
415 * The main initialization steps followed are -
416 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700417 * - Issue the init commands to firmware
418 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700419static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700420{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800421 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700422 char fmt[64];
423 struct mwifiex_private *priv;
424 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700425 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700426 struct semaphore *sem = adapter->card_sem;
427 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800428 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700429
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700430 if (!firmware) {
431 dev_err(adapter->dev,
432 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700433 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700434 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700435
436 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
437 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700438 fw.fw_buf = (u8 *) adapter->firmware->data;
439 fw.fw_len = adapter->firmware->size;
440
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700441 if (adapter->if_ops.dnld_fw)
442 ret = adapter->if_ops.dnld_fw(adapter, &fw);
443 else
444 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700445 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700446 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700447
448 dev_notice(adapter->dev, "WLAN FW is active\n");
449
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700450 if (cal_data_cfg) {
451 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
452 adapter->dev)) < 0)
453 dev_err(adapter->dev,
454 "Cal data request_firmware() failed\n");
455 }
456
Daniel Drake232fde02013-07-13 10:57:10 -0400457 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700458 if (adapter->if_ops.enable_int) {
459 if (adapter->if_ops.enable_int(adapter))
460 goto err_dnld_fw;
461 }
Daniel Drake232fde02013-07-13 10:57:10 -0400462
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700463 adapter->init_wait_q_woken = false;
464 ret = mwifiex_init_fw(adapter);
465 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700466 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467 } else if (!ret) {
468 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
469 goto done;
470 }
471 /* Wait for mwifiex_init to complete */
472 wait_event_interruptible(adapter->init_wait_q,
473 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700474 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700475 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700476
Avinash Patild6bffe82012-05-08 18:30:15 -0700477 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
478 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700479 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800480 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700481 }
482
483 rtnl_lock();
484 /* Create station interface by default */
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800485 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
486 NL80211_IFTYPE_STATION, NULL, NULL);
487 if (IS_ERR(wdev)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700488 dev_err(adapter->dev, "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800489 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700490 goto err_add_intf;
491 }
492 rtnl_unlock();
493
494 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
495 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
496 goto done;
497
498err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700499 wiphy_unregister(adapter->wiphy);
500 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700501err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400502 if (adapter->if_ops.disable_int)
503 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700504err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700505 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700506 if (adapter->if_ops.unregister_dev)
507 adapter->if_ops.unregister_dev(adapter);
508
509 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
510 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
511 pr_debug("info: %s: shutdown mwifiex\n", __func__);
512 adapter->init_wait_q_woken = false;
513
514 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
515 wait_event_interruptible(adapter->init_wait_q,
516 adapter->init_wait_q_woken);
517 }
518 adapter->surprise_removed = true;
519 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700520 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700521done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700522 if (adapter->cal_data) {
523 release_firmware(adapter->cal_data);
524 adapter->cal_data = NULL;
525 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700526 if (adapter->firmware) {
527 release_firmware(adapter->firmware);
528 adapter->firmware = NULL;
529 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700530 if (init_failed)
531 mwifiex_free_adapter(adapter);
532 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700533 return;
534}
535
536/*
537 * This function initializes the hardware and gets firmware.
538 */
539static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
540{
541 int ret;
542
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700543 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
544 adapter->dev, GFP_KERNEL, adapter,
545 mwifiex_fw_dpc);
546 if (ret < 0)
547 dev_err(adapter->dev,
548 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700549 return ret;
550}
551
552/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700553 * CFG802.11 network device handler for open.
554 *
555 * Starts the data queue.
556 */
557static int
558mwifiex_open(struct net_device *dev)
559{
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800560 netif_tx_start_all_queues(dev);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700561 return 0;
562}
563
564/*
565 * CFG802.11 network device handler for close.
566 */
567static int
568mwifiex_close(struct net_device *dev)
569{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700570 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
571
572 if (priv->scan_request) {
573 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
574 cfg80211_scan_done(priv->scan_request, 1);
575 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700576 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700577 }
578
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700579 return 0;
580}
581
582/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700583 * Add buffer into wmm tx queue and queue work to transmit it.
584 */
585int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
586{
Avinash Patil47411a02012-11-01 18:44:16 -0700587 struct netdev_queue *txq;
588 int index = mwifiex_1d_to_wmm_queue[skb->priority];
589
590 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
591 txq = netdev_get_tx_queue(priv->netdev, index);
592 if (!netif_tx_queue_stopped(txq)) {
593 netif_tx_stop_queue(txq);
594 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
595 }
596 }
597
Stone Piaoe39faa72012-09-25 20:23:32 -0700598 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700599 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700600
601 if (priv->adapter->scan_delay_cnt)
602 atomic_set(&priv->adapter->is_tx_received, true);
603
Stone Piaoe39faa72012-09-25 20:23:32 -0700604 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
605
606 return 0;
607}
608
609/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700610 * CFG802.11 network device handler for data transmission.
611 */
612static int
613mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
614{
615 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700616 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700617 struct mwifiex_txinfo *tx_info;
618
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800619 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700620 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700621
622 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200623 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700624 priv->stats.tx_dropped++;
625 return 0;
626 }
627 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
628 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200629 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700630 priv->stats.tx_dropped++;
631 return 0;
632 }
633 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
634 dev_dbg(priv->adapter->dev,
635 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700636 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700637 /* Insufficient skb headroom - allocate a new skb */
638 new_skb =
639 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
640 if (unlikely(!new_skb)) {
641 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200642 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700643 priv->stats.tx_dropped++;
644 return 0;
645 }
646 kfree_skb(skb);
647 skb = new_skb;
648 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700649 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700650 }
651
652 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700653 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800654 tx_info->bss_num = priv->bss_num;
655 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800656 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700657
658 /* Record the current time the packet was queued; used to
659 * determine the amount of time the packet was queued in
660 * the driver before it was sent to the firmware.
661 * The delay is then sent along with the packet to the
662 * firmware for aggregate delay calculation for stats and
663 * MSDU lifetime expiry.
664 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100665 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700666
Stone Piaoe39faa72012-09-25 20:23:32 -0700667 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700668
669 return 0;
670}
671
672/*
673 * CFG802.11 network device handler for setting MAC address.
674 */
675static int
676mwifiex_set_mac_address(struct net_device *dev, void *addr)
677{
678 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700679 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700680 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700681
682 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
683
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700684 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800685 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
686 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700687
688 if (!ret)
689 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
690 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700691 dev_err(priv->adapter->dev,
692 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700693
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700694 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
695
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700696 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700697}
698
699/*
700 * CFG802.11 network device handler for setting multicast list.
701 */
702static void mwifiex_set_multicast_list(struct net_device *dev)
703{
704 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700705 struct mwifiex_multicast_list mcast_list;
706
707 if (dev->flags & IFF_PROMISC) {
708 mcast_list.mode = MWIFIEX_PROMISC_MODE;
709 } else if (dev->flags & IFF_ALLMULTI ||
710 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
711 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
712 } else {
713 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400714 mcast_list.num_multicast_addr =
715 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700716 }
717 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700718}
719
720/*
721 * CFG802.11 network device handler for transmission timeout.
722 */
723static void
724mwifiex_tx_timeout(struct net_device *dev)
725{
726 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
727
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700728 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800729 priv->tx_timeout_cnt++;
730 dev_err(priv->adapter->dev,
731 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
732 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
733 mwifiex_set_trans_start(dev);
734
735 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
736 priv->adapter->if_ops.card_reset) {
737 dev_err(priv->adapter->dev,
738 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
739 priv->adapter->if_ops.card_reset(priv->adapter);
740 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700741}
742
743/*
744 * CFG802.11 network device handler for statistics retrieval.
745 */
746static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
747{
748 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
749
750 return &priv->stats;
751}
752
Avinash Patil47411a02012-11-01 18:44:16 -0700753static u16
Jason Wangf663dd92014-01-10 16:18:26 +0800754mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100755 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -0700756{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800757 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -0700758 return mwifiex_1d_to_wmm_queue[skb->priority];
759}
760
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700761/* Network device handlers */
762static const struct net_device_ops mwifiex_netdev_ops = {
763 .ndo_open = mwifiex_open,
764 .ndo_stop = mwifiex_close,
765 .ndo_start_xmit = mwifiex_hard_start_xmit,
766 .ndo_set_mac_address = mwifiex_set_mac_address,
767 .ndo_tx_timeout = mwifiex_tx_timeout,
768 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000769 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700770 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700771};
772
773/*
774 * This function initializes the private structure parameters.
775 *
776 * The following wait queues are initialized -
777 * - IOCTL wait queue
778 * - Command wait queue
779 * - Statistics wait queue
780 *
781 * ...and the following default parameters are set -
782 * - Current key index : Set to 0
783 * - Rate index : Set to auto
784 * - Media connected : Set to disconnected
785 * - Adhoc link sensed : Set to false
786 * - Nick name : Set to null
787 * - Number of Tx timeout : Set to 0
788 * - Device address : Set to current address
789 *
790 * In addition, the CFG80211 work queue is also created.
791 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700792void mwifiex_init_priv_params(struct mwifiex_private *priv,
793 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700794{
795 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700796 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700797 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700798 priv->current_key_index = 0;
799 priv->media_connected = false;
800 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700801 memset(priv->mgmt_ie, 0,
802 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700803 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
804 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
805 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
806 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700807 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700808 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
809}
810
811/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700812 * This function check if command is pending.
813 */
814int is_command_pending(struct mwifiex_adapter *adapter)
815{
816 unsigned long flags;
817 int is_cmd_pend_q_empty;
818
819 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
820 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
821 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
822
823 return !is_cmd_pend_q_empty;
824}
825
826/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700827 * This is the main work queue function.
828 *
829 * It handles the main process, which in turn handles the complete
830 * driver operations.
831 */
832static void mwifiex_main_work_queue(struct work_struct *work)
833{
834 struct mwifiex_adapter *adapter =
835 container_of(work, struct mwifiex_adapter, main_work);
836
837 if (adapter->surprise_removed)
838 return;
839 mwifiex_main_process(adapter);
840}
841
842/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700843 * This function adds the card.
844 *
845 * This function follows the following major steps to set up the device -
846 * - Initialize software. This includes probing the card, registering
847 * the interface operations table, and allocating/initializing the
848 * adapter structure
849 * - Set up the netlink socket
850 * - Create and start the main work queue
851 * - Register the device
852 * - Initialize firmware and hardware
853 * - Add logical interfaces
854 */
855int
856mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700857 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700858{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700859 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700860
861 if (down_interruptible(sem))
862 goto exit_sem_err;
863
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700864 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700865 pr_err("%s: software init failed\n", __func__);
866 goto err_init_sw;
867 }
868
Amitkumar Karward930fae2011-10-11 17:41:21 -0700869 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700870 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700871
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700872 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700873 adapter->surprise_removed = false;
874 init_waitqueue_head(&adapter->init_wait_q);
875 adapter->is_suspended = false;
876 adapter->hs_activated = false;
877 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700878 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700879 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700880 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700881
Amitkumar Karwar448a71c2013-10-11 18:33:04 -0700882 adapter->workqueue =
883 alloc_workqueue("MWIFIEX_WORK_QUEUE",
884 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700885 if (!adapter->workqueue)
886 goto err_kmalloc;
887
888 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Amitkumar Karwar92c25382014-06-19 21:38:52 -0700889 if (adapter->if_ops.iface_work)
890 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700891
892 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -0400893 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894 if (adapter->if_ops.register_dev(adapter)) {
895 pr_err("%s: failed to register mwifiex device\n", __func__);
896 goto err_registerdev;
897 }
898
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700899 if (mwifiex_init_hw_fw(adapter)) {
900 pr_err("%s: firmware init failed\n", __func__);
901 goto err_init_fw;
902 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700903
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700904 return 0;
905
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700906err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700907 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700908 if (adapter->if_ops.unregister_dev)
909 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700910 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
911 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
912 pr_debug("info: %s: shutdown mwifiex\n", __func__);
913 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700914
915 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700916 wait_event_interruptible(adapter->init_wait_q,
917 adapter->init_wait_q_woken);
918 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700919err_registerdev:
920 adapter->surprise_removed = true;
921 mwifiex_terminate_workqueue(adapter);
922err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700923 mwifiex_free_adapter(adapter);
924
925err_init_sw:
926 up(sem);
927
928exit_sem_err:
929 return -1;
930}
931EXPORT_SYMBOL_GPL(mwifiex_add_card);
932
933/*
934 * This function removes the card.
935 *
936 * This function follows the following major steps to remove the device -
937 * - Stop data traffic
938 * - Shutdown firmware
939 * - Remove the logical interfaces
940 * - Terminate the work queue
941 * - Unregister the device
942 * - Free the adapter structure
943 */
944int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
945{
946 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700947 int i;
948
949 if (down_interruptible(sem))
950 goto exit_sem_err;
951
952 if (!adapter)
953 goto exit_remove;
954
Daniel Drake232fde02013-07-13 10:57:10 -0400955 /* We can no longer handle interrupts once we start doing the teardown
956 * below. */
957 if (adapter->if_ops.disable_int)
958 adapter->if_ops.disable_int(adapter);
959
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700960 adapter->surprise_removed = true;
961
962 /* Stop data */
963 for (i = 0; i < adapter->priv_num; i++) {
964 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700965 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -0700966 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700967 if (netif_carrier_ok(priv->netdev))
968 netif_carrier_off(priv->netdev);
969 }
970 }
971
972 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
973 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700974
975 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700976 wait_event_interruptible(adapter->init_wait_q,
977 adapter->init_wait_q_woken);
978 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
979 if (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 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700983 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700984 atomic_read(&adapter->rx_pending),
985 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700986 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700987 }
988
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700989 for (i = 0; i < adapter->priv_num; i++) {
990 priv = adapter->priv[i];
991
992 if (!priv)
993 continue;
994
995 rtnl_lock();
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800996 if (priv->wdev && priv->netdev)
Johannes Berg84efbb82012-06-16 00:00:26 +0200997 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700998 rtnl_unlock();
999 }
1000
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001001 wiphy_unregister(adapter->wiphy);
1002 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001003
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001004 mwifiex_terminate_workqueue(adapter);
1005
1006 /* Unregister device */
1007 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001008 if (adapter->if_ops.unregister_dev)
1009 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001010 /* Free adapter structure */
1011 dev_dbg(adapter->dev, "info: free adapter\n");
1012 mwifiex_free_adapter(adapter);
1013
1014exit_remove:
1015 up(sem);
1016exit_sem_err:
1017 return 0;
1018}
1019EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1020
1021/*
1022 * This function initializes the module.
1023 *
1024 * The debug FS is also initialized if configured.
1025 */
1026static int
1027mwifiex_init_module(void)
1028{
1029#ifdef CONFIG_DEBUG_FS
1030 mwifiex_debugfs_init();
1031#endif
1032 return 0;
1033}
1034
1035/*
1036 * This function cleans up the module.
1037 *
1038 * The debug FS is removed if available.
1039 */
1040static void
1041mwifiex_cleanup_module(void)
1042{
1043#ifdef CONFIG_DEBUG_FS
1044 mwifiex_debugfs_remove();
1045#endif
1046}
1047
1048module_init(mwifiex_init_module);
1049module_exit(mwifiex_cleanup_module);
1050
1051MODULE_AUTHOR("Marvell International Ltd.");
1052MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1053MODULE_VERSION(VERSION);
1054MODULE_LICENSE("GPL v2");