blob: 2a5a59bec1240997ff89a306a96edb237ed3d8df [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
Bing Zhao5e6e3a92011-03-21 18:00:50 -070031/*
32 * This function registers the device and performs all the necessary
33 * initializations.
34 *
35 * The following initialization operations are performed -
36 * - Allocate adapter structure
37 * - Save interface specific operations table in adapter
38 * - Call interface specific initialization routine
39 * - Allocate private structures
40 * - Set default adapter structure parameters
41 * - Initialize locks
42 *
43 * In case of any errors during inittialization, this function also ensures
44 * proper cleanup before exiting.
45 */
46static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
Amitkumar Karwar287546d2011-06-08 20:39:20 +053047 void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070048{
Amitkumar Karwar2be78592011-04-15 20:50:42 -070049 struct mwifiex_adapter *adapter;
50 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070051
52 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070053 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +020054 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070055
Amitkumar Karwar287546d2011-06-08 20:39:20 +053056 *padapter = adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070057 adapter->card = card;
58
59 /* Save interface specific operations in adapter */
60 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
61
62 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070063 if (adapter->if_ops.init_if)
64 if (adapter->if_ops.init_if(adapter))
65 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070066
67 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070068
Avinash Patil64b05e22012-05-08 18:30:13 -070069 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
70 /* Allocate memory for private structure */
71 adapter->priv[i] =
72 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
73 if (!adapter->priv[i])
74 goto error;
75
76 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -070077 adapter->priv_num++;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070078 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -070079 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070080
81 init_timer(&adapter->cmd_timer);
82 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
83 adapter->cmd_timer.data = (unsigned long) adapter;
84
Bing Zhao5e6e3a92011-03-21 18:00:50 -070085 return 0;
86
87error:
88 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
89
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070090 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070091 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070092
Bing Zhao5e6e3a92011-03-21 18:00:50 -070093 kfree(adapter);
94
95 return -1;
96}
97
98/*
99 * This function unregisters the device and performs all the necessary
100 * cleanups.
101 *
102 * The following cleanup operations are performed -
103 * - Free the timers
104 * - Free beacon buffers
105 * - Free private structures
106 * - Free adapter structure
107 */
108static int mwifiex_unregister(struct mwifiex_adapter *adapter)
109{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700110 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700111
Amitkumar Karwar4cfda672013-07-22 19:17:50 -0700112 if (adapter->if_ops.cleanup_if)
113 adapter->if_ops.cleanup_if(adapter);
114
Avinash Patil629873f2014-02-18 15:47:55 -0800115 del_timer_sync(&adapter->cmd_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700116
117 /* Free private structures */
118 for (i = 0; i < adapter->priv_num; i++) {
119 if (adapter->priv[i]) {
120 mwifiex_free_curr_bcn(adapter->priv[i]);
121 kfree(adapter->priv[i]);
122 }
123 }
124
Avinash Patilbf354432014-10-31 16:08:26 +0530125 vfree(adapter->chan_stats);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700126 kfree(adapter);
127 return 0;
128}
129
Avinash Patil6e251172014-09-12 20:08:59 +0530130static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
131{
132 unsigned long flags;
133 struct sk_buff *skb;
Avinash Patil6e251172014-09-12 20:08:59 +0530134
135 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
136 if (adapter->rx_processing || adapter->rx_locked) {
137 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
138 goto exit_rx_proc;
139 } else {
140 adapter->rx_processing = true;
141 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
142 }
143
144 /* Check for Rx data */
145 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
146 atomic_dec(&adapter->rx_pending);
147 if (adapter->delay_main_work &&
Avinash Patilb43a0d92014-09-29 21:44:14 +0530148 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530149 if (adapter->if_ops.submit_rem_rx_urbs)
150 adapter->if_ops.submit_rem_rx_urbs(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530151 adapter->delay_main_work = false;
Avinash Patilb43a0d92014-09-29 21:44:14 +0530152 queue_work(adapter->workqueue, &adapter->main_work);
Avinash Patil6e251172014-09-12 20:08:59 +0530153 }
154 mwifiex_handle_rx_packet(adapter, skb);
155 }
156 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
157 adapter->rx_processing = false;
158 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
159
Avinash Patil6e251172014-09-12 20:08:59 +0530160exit_rx_proc:
161 return 0;
162}
163
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700164/*
165 * The main process.
166 *
167 * This function is the main procedure of the driver and handles various driver
168 * operations. It runs in a loop and provides the core functionalities.
169 *
170 * The main responsibilities of this function are -
171 * - Ensure concurrency control
172 * - Handle pending interrupts and call interrupt handlers
173 * - Wake up the card if required
174 * - Handle command responses and call response handlers
175 * - Handle events and call event handlers
176 * - Execute pending commands
177 * - Transmit pending data packets
178 */
179int mwifiex_main_process(struct mwifiex_adapter *adapter)
180{
181 int ret = 0;
182 unsigned long flags;
183
184 spin_lock_irqsave(&adapter->main_proc_lock, flags);
185
186 /* Check if already processing */
187 if (adapter->mwifiex_processing) {
188 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
189 goto exit_main_proc;
190 } else {
191 adapter->mwifiex_processing = true;
192 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
193 }
194process_start:
195 do {
196 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
197 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
198 break;
199
Avinash Patil6e251172014-09-12 20:08:59 +0530200 /* If we process interrupts first, it would increase RX pending
201 * even further. Avoid this by checking if rx_pending has
202 * crossed high threshold and schedule rx work queue
203 * and then process interrupts
204 */
205 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING) {
206 adapter->delay_main_work = true;
207 if (!adapter->rx_processing)
208 queue_work(adapter->rx_workqueue,
209 &adapter->rx_work);
210 break;
211 }
212
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700213 /* Handle pending interrupt if any */
214 if (adapter->int_status) {
215 if (adapter->hs_activated)
216 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700217 if (adapter->if_ops.process_int_status)
218 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700219 }
220
Avinash Patil6e251172014-09-12 20:08:59 +0530221 if (adapter->rx_work_enabled && adapter->data_received)
222 queue_work(adapter->rx_workqueue, &adapter->rx_work);
223
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700224 /* Need to wake up the card ? */
225 if ((adapter->ps_state == PS_STATE_SLEEP) &&
226 (adapter->pm_wakeup_card_req &&
227 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700228 (is_command_pending(adapter) ||
229 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700230 adapter->pm_wakeup_fw_try = true;
231 adapter->if_ops.wakeup(adapter);
232 continue;
233 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700234
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700235 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530236 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700237 adapter->pm_wakeup_fw_try = false;
238 if (adapter->ps_state == PS_STATE_SLEEP)
239 adapter->ps_state = PS_STATE_AWAKE;
240 } else {
241 /* We have tried to wakeup the card already */
242 if (adapter->pm_wakeup_fw_try)
243 break;
244 if (adapter->ps_state != PS_STATE_AWAKE ||
245 adapter->tx_lock_flag)
246 break;
247
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530248 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530249 adapter->scan_processing) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700250 mwifiex_wmm_lists_empty(adapter)) {
251 if (adapter->cmd_sent || adapter->curr_cmd ||
252 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700253 break;
254 }
255 }
256
Amitkumar Karwar20474122014-04-14 15:31:05 -0700257 /* Check for event */
258 if (adapter->event_received) {
259 adapter->event_received = false;
260 mwifiex_process_event(adapter);
261 }
262
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700263 /* Check for Cmd Resp */
264 if (adapter->cmd_resp_received) {
265 adapter->cmd_resp_received = false;
266 mwifiex_process_cmdresp(adapter);
267
268 /* call mwifiex back when init_fw is done */
269 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
270 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
271 mwifiex_init_fw_complete(adapter);
272 }
273 }
274
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700275 /* Check if we need to confirm Sleep Request
276 received previously */
277 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
278 if (!adapter->cmd_sent && !adapter->curr_cmd)
279 mwifiex_check_ps_cond(adapter);
280 }
281
282 /* * The ps_state may have been changed during processing of
283 * Sleep Request event.
284 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700285 if ((adapter->ps_state == PS_STATE_SLEEP) ||
286 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
287 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
288 adapter->tx_lock_flag)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700289 continue;
290
291 if (!adapter->cmd_sent && !adapter->curr_cmd) {
292 if (mwifiex_exec_next_cmd(adapter) == -1) {
293 ret = -1;
294 break;
295 }
296 }
297
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530298 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530299 !adapter->scan_processing) &&
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700300 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700301 mwifiex_wmm_process_tx(adapter);
302 if (adapter->hs_activated) {
303 adapter->is_hs_configured = false;
304 mwifiex_hs_activated_event
305 (mwifiex_get_priv
306 (adapter, MWIFIEX_BSS_ROLE_ANY),
307 false);
308 }
309 }
310
311 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700312 !adapter->curr_cmd && !is_command_pending(adapter) &&
313 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700314 if (!mwifiex_send_null_packet
315 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
316 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
317 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
318 adapter->delay_null_pkt = false;
319 adapter->ps_state = PS_STATE_SLEEP;
320 }
321 break;
322 }
323 } while (true);
324
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700325 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Avinash Patilf73e5572014-09-29 21:44:13 +0530326 if (!adapter->delay_main_work &&
327 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700328 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
329 goto process_start;
330 }
331
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700332 adapter->mwifiex_processing = false;
333 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
334
335exit_main_proc:
336 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
337 mwifiex_shutdown_drv(adapter);
338 return ret;
339}
Bing Zhao601216e2012-11-05 16:59:15 -0800340EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700341
342/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700343 * This function frees the adapter structure.
344 *
345 * Additionally, this closes the netlink socket, frees the timers
346 * and private structures.
347 */
348static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
349{
350 if (!adapter) {
351 pr_err("%s: adapter is NULL\n", __func__);
352 return;
353 }
354
355 mwifiex_unregister(adapter);
356 pr_debug("info: %s: free adapter\n", __func__);
357}
358
359/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700360 * This function cancels all works in the queue and destroys
361 * the main workqueue.
362 */
363static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
364{
365 flush_workqueue(adapter->workqueue);
366 destroy_workqueue(adapter->workqueue);
367 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530368
369 if (adapter->rx_workqueue) {
370 flush_workqueue(adapter->rx_workqueue);
371 destroy_workqueue(adapter->rx_workqueue);
372 adapter->rx_workqueue = NULL;
373 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700374}
375
376/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700377 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700378 *
379 * The main initialization steps followed are -
380 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700381 * - Issue the init commands to firmware
382 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700383static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700384{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800385 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700386 char fmt[64];
387 struct mwifiex_private *priv;
388 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700389 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700390 struct semaphore *sem = adapter->card_sem;
391 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800392 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700393
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700394 if (!firmware) {
395 dev_err(adapter->dev,
396 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700397 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700398 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700399
400 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
401 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700402 fw.fw_buf = (u8 *) adapter->firmware->data;
403 fw.fw_len = adapter->firmware->size;
404
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700405 if (adapter->if_ops.dnld_fw)
406 ret = adapter->if_ops.dnld_fw(adapter, &fw);
407 else
408 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700409 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700410 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700411
412 dev_notice(adapter->dev, "WLAN FW is active\n");
413
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700414 if (cal_data_cfg) {
415 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
416 adapter->dev)) < 0)
417 dev_err(adapter->dev,
418 "Cal data request_firmware() failed\n");
419 }
420
Daniel Drake232fde02013-07-13 10:57:10 -0400421 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700422 if (adapter->if_ops.enable_int) {
423 if (adapter->if_ops.enable_int(adapter))
424 goto err_dnld_fw;
425 }
Daniel Drake232fde02013-07-13 10:57:10 -0400426
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700427 adapter->init_wait_q_woken = false;
428 ret = mwifiex_init_fw(adapter);
429 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700430 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700431 } else if (!ret) {
432 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
433 goto done;
434 }
435 /* Wait for mwifiex_init to complete */
436 wait_event_interruptible(adapter->init_wait_q,
437 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700438 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700439 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700440
Avinash Patild6bffe82012-05-08 18:30:15 -0700441 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
442 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700443 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800444 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700445 }
446
Avinash Patilbf354432014-10-31 16:08:26 +0530447 if (mwifiex_init_channel_scan_gap(adapter)) {
448 dev_err(adapter->dev, "could not init channel stats table\n");
449 goto err_init_fw;
450 }
451
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700452 rtnl_lock();
453 /* Create station interface by default */
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800454 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
455 NL80211_IFTYPE_STATION, NULL, NULL);
456 if (IS_ERR(wdev)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700457 dev_err(adapter->dev, "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800458 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700459 goto err_add_intf;
460 }
461 rtnl_unlock();
462
463 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
464 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
465 goto done;
466
467err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700468 wiphy_unregister(adapter->wiphy);
469 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700470err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400471 if (adapter->if_ops.disable_int)
472 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700473err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700474 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700475 if (adapter->if_ops.unregister_dev)
476 adapter->if_ops.unregister_dev(adapter);
477
478 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
479 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
480 pr_debug("info: %s: shutdown mwifiex\n", __func__);
481 adapter->init_wait_q_woken = false;
482
483 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
484 wait_event_interruptible(adapter->init_wait_q,
485 adapter->init_wait_q_woken);
486 }
487 adapter->surprise_removed = true;
488 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700489 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700490done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700491 if (adapter->cal_data) {
492 release_firmware(adapter->cal_data);
493 adapter->cal_data = NULL;
494 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700495 if (adapter->firmware) {
496 release_firmware(adapter->firmware);
497 adapter->firmware = NULL;
498 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700499 if (init_failed)
500 mwifiex_free_adapter(adapter);
501 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700502 return;
503}
504
505/*
506 * This function initializes the hardware and gets firmware.
507 */
508static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
509{
510 int ret;
511
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700512 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
513 adapter->dev, GFP_KERNEL, adapter,
514 mwifiex_fw_dpc);
515 if (ret < 0)
516 dev_err(adapter->dev,
517 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700518 return ret;
519}
520
521/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700522 * CFG802.11 network device handler for open.
523 *
524 * Starts the data queue.
525 */
526static int
527mwifiex_open(struct net_device *dev)
528{
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800529 netif_tx_start_all_queues(dev);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700530 return 0;
531}
532
533/*
534 * CFG802.11 network device handler for close.
535 */
536static int
537mwifiex_close(struct net_device *dev)
538{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700539 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
540
541 if (priv->scan_request) {
542 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
543 cfg80211_scan_done(priv->scan_request, 1);
544 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700545 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700546 }
547
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700548 return 0;
549}
550
551/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700552 * Add buffer into wmm tx queue and queue work to transmit it.
553 */
554int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
555{
Avinash Patil47411a02012-11-01 18:44:16 -0700556 struct netdev_queue *txq;
557 int index = mwifiex_1d_to_wmm_queue[skb->priority];
558
559 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
560 txq = netdev_get_tx_queue(priv->netdev, index);
561 if (!netif_tx_queue_stopped(txq)) {
562 netif_tx_stop_queue(txq);
563 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
564 }
565 }
566
Stone Piaoe39faa72012-09-25 20:23:32 -0700567 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700568 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700569
Stone Piaoe39faa72012-09-25 20:23:32 -0700570 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
571
572 return 0;
573}
574
575/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700576 * CFG802.11 network device handler for data transmission.
577 */
578static int
579mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
580{
581 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700582 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700583 struct mwifiex_txinfo *tx_info;
584
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800585 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700586 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700587
588 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200589 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700590 priv->stats.tx_dropped++;
591 return 0;
592 }
593 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
594 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200595 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700596 priv->stats.tx_dropped++;
597 return 0;
598 }
599 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
600 dev_dbg(priv->adapter->dev,
601 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700602 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700603 /* Insufficient skb headroom - allocate a new skb */
604 new_skb =
605 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
606 if (unlikely(!new_skb)) {
607 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200608 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700609 priv->stats.tx_dropped++;
610 return 0;
611 }
612 kfree_skb(skb);
613 skb = new_skb;
614 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700615 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700616 }
617
618 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700619 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800620 tx_info->bss_num = priv->bss_num;
621 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800622 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700623
624 /* Record the current time the packet was queued; used to
625 * determine the amount of time the packet was queued in
626 * the driver before it was sent to the firmware.
627 * The delay is then sent along with the packet to the
628 * firmware for aggregate delay calculation for stats and
629 * MSDU lifetime expiry.
630 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100631 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700632
Stone Piaoe39faa72012-09-25 20:23:32 -0700633 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700634
635 return 0;
636}
637
638/*
639 * CFG802.11 network device handler for setting MAC address.
640 */
641static int
642mwifiex_set_mac_address(struct net_device *dev, void *addr)
643{
644 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700645 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700646 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700647
648 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
649
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700650 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800651 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
652 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700653
654 if (!ret)
655 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
656 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700657 dev_err(priv->adapter->dev,
658 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700659
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700660 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
661
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700662 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700663}
664
665/*
666 * CFG802.11 network device handler for setting multicast list.
667 */
668static void mwifiex_set_multicast_list(struct net_device *dev)
669{
670 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700671 struct mwifiex_multicast_list mcast_list;
672
673 if (dev->flags & IFF_PROMISC) {
674 mcast_list.mode = MWIFIEX_PROMISC_MODE;
675 } else if (dev->flags & IFF_ALLMULTI ||
676 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
677 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
678 } else {
679 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400680 mcast_list.num_multicast_addr =
681 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700682 }
683 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700684}
685
686/*
687 * CFG802.11 network device handler for transmission timeout.
688 */
689static void
690mwifiex_tx_timeout(struct net_device *dev)
691{
692 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
693
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700694 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800695 priv->tx_timeout_cnt++;
696 dev_err(priv->adapter->dev,
697 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
698 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
699 mwifiex_set_trans_start(dev);
700
701 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
702 priv->adapter->if_ops.card_reset) {
703 dev_err(priv->adapter->dev,
704 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
705 priv->adapter->if_ops.card_reset(priv->adapter);
706 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700707}
708
709/*
710 * CFG802.11 network device handler for statistics retrieval.
711 */
712static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
713{
714 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
715
716 return &priv->stats;
717}
718
Avinash Patil47411a02012-11-01 18:44:16 -0700719static u16
Jason Wangf663dd92014-01-10 16:18:26 +0800720mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100721 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -0700722{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800723 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -0700724 return mwifiex_1d_to_wmm_queue[skb->priority];
725}
726
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700727/* Network device handlers */
728static const struct net_device_ops mwifiex_netdev_ops = {
729 .ndo_open = mwifiex_open,
730 .ndo_stop = mwifiex_close,
731 .ndo_start_xmit = mwifiex_hard_start_xmit,
732 .ndo_set_mac_address = mwifiex_set_mac_address,
733 .ndo_tx_timeout = mwifiex_tx_timeout,
734 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000735 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700736 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700737};
738
739/*
740 * This function initializes the private structure parameters.
741 *
742 * The following wait queues are initialized -
743 * - IOCTL wait queue
744 * - Command wait queue
745 * - Statistics wait queue
746 *
747 * ...and the following default parameters are set -
748 * - Current key index : Set to 0
749 * - Rate index : Set to auto
750 * - Media connected : Set to disconnected
751 * - Adhoc link sensed : Set to false
752 * - Nick name : Set to null
753 * - Number of Tx timeout : Set to 0
754 * - Device address : Set to current address
755 *
756 * In addition, the CFG80211 work queue is also created.
757 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700758void mwifiex_init_priv_params(struct mwifiex_private *priv,
759 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700760{
761 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700762 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700763 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700764 priv->current_key_index = 0;
765 priv->media_connected = false;
766 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700767 memset(priv->mgmt_ie, 0,
768 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700769 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
770 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
771 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
772 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700773 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700774 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
775}
776
777/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700778 * This function check if command is pending.
779 */
780int is_command_pending(struct mwifiex_adapter *adapter)
781{
782 unsigned long flags;
783 int is_cmd_pend_q_empty;
784
785 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
786 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
787 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
788
789 return !is_cmd_pend_q_empty;
790}
791
792/*
Avinash Patil6e251172014-09-12 20:08:59 +0530793 * This is the RX work queue function.
794 *
795 * It handles the RX operations.
796 */
797static void mwifiex_rx_work_queue(struct work_struct *work)
798{
799 struct mwifiex_adapter *adapter =
800 container_of(work, struct mwifiex_adapter, rx_work);
801
802 if (adapter->surprise_removed)
803 return;
804 mwifiex_process_rx(adapter);
805}
806
807/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700808 * This is the main work queue function.
809 *
810 * It handles the main process, which in turn handles the complete
811 * driver operations.
812 */
813static void mwifiex_main_work_queue(struct work_struct *work)
814{
815 struct mwifiex_adapter *adapter =
816 container_of(work, struct mwifiex_adapter, main_work);
817
818 if (adapter->surprise_removed)
819 return;
820 mwifiex_main_process(adapter);
821}
822
823/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700824 * This function adds the card.
825 *
826 * This function follows the following major steps to set up the device -
827 * - Initialize software. This includes probing the card, registering
828 * the interface operations table, and allocating/initializing the
829 * adapter structure
830 * - Set up the netlink socket
831 * - Create and start the main work queue
832 * - Register the device
833 * - Initialize firmware and hardware
834 * - Add logical interfaces
835 */
836int
837mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700838 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700839{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700840 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700841
842 if (down_interruptible(sem))
843 goto exit_sem_err;
844
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700845 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700846 pr_err("%s: software init failed\n", __func__);
847 goto err_init_sw;
848 }
849
Amitkumar Karward930fae2011-10-11 17:41:21 -0700850 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700851 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700852
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700853 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700854 adapter->surprise_removed = false;
855 init_waitqueue_head(&adapter->init_wait_q);
856 adapter->is_suspended = false;
857 adapter->hs_activated = false;
858 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700859 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700860 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700861 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700862
Avinash Patilec4a16b2014-11-05 17:04:27 +0530863 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +0530864 adapter->rx_work_enabled = true;
865 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
866 }
867
Amitkumar Karwar448a71c2013-10-11 18:33:04 -0700868 adapter->workqueue =
869 alloc_workqueue("MWIFIEX_WORK_QUEUE",
870 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700871 if (!adapter->workqueue)
872 goto err_kmalloc;
873
874 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +0530875
876 if (adapter->rx_work_enabled) {
877 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
878 WQ_HIGHPRI |
879 WQ_MEM_RECLAIM |
880 WQ_UNBOUND, 1);
881 if (!adapter->rx_workqueue)
882 goto err_kmalloc;
883
884 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
885 }
886
Amitkumar Karwar92c25382014-06-19 21:38:52 -0700887 if (adapter->if_ops.iface_work)
888 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700889
890 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -0400891 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700892 if (adapter->if_ops.register_dev(adapter)) {
893 pr_err("%s: failed to register mwifiex device\n", __func__);
894 goto err_registerdev;
895 }
896
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700897 if (mwifiex_init_hw_fw(adapter)) {
898 pr_err("%s: firmware init failed\n", __func__);
899 goto err_init_fw;
900 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700901
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700902 return 0;
903
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700904err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700905 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700906 if (adapter->if_ops.unregister_dev)
907 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700908 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
909 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
910 pr_debug("info: %s: shutdown mwifiex\n", __func__);
911 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700912
913 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700914 wait_event_interruptible(adapter->init_wait_q,
915 adapter->init_wait_q_woken);
916 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700917err_registerdev:
918 adapter->surprise_removed = true;
919 mwifiex_terminate_workqueue(adapter);
920err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700921 mwifiex_free_adapter(adapter);
922
923err_init_sw:
924 up(sem);
925
926exit_sem_err:
927 return -1;
928}
929EXPORT_SYMBOL_GPL(mwifiex_add_card);
930
931/*
932 * This function removes the card.
933 *
934 * This function follows the following major steps to remove the device -
935 * - Stop data traffic
936 * - Shutdown firmware
937 * - Remove the logical interfaces
938 * - Terminate the work queue
939 * - Unregister the device
940 * - Free the adapter structure
941 */
942int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
943{
944 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700945 int i;
946
947 if (down_interruptible(sem))
948 goto exit_sem_err;
949
950 if (!adapter)
951 goto exit_remove;
952
Daniel Drake232fde02013-07-13 10:57:10 -0400953 /* We can no longer handle interrupts once we start doing the teardown
954 * below. */
955 if (adapter->if_ops.disable_int)
956 adapter->if_ops.disable_int(adapter);
957
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700958 adapter->surprise_removed = true;
959
960 /* Stop data */
961 for (i = 0; i < adapter->priv_num; i++) {
962 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700963 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -0700964 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700965 if (netif_carrier_ok(priv->netdev))
966 netif_carrier_off(priv->netdev);
967 }
968 }
969
970 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
971 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700972
973 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700974 wait_event_interruptible(adapter->init_wait_q,
975 adapter->init_wait_q_woken);
976 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
977 if (atomic_read(&adapter->rx_pending) ||
978 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700979 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700980 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700981 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700982 atomic_read(&adapter->rx_pending),
983 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700984 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700985 }
986
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700987 for (i = 0; i < adapter->priv_num; i++) {
988 priv = adapter->priv[i];
989
990 if (!priv)
991 continue;
992
993 rtnl_lock();
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800994 if (priv->wdev && priv->netdev)
Johannes Berg84efbb82012-06-16 00:00:26 +0200995 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700996 rtnl_unlock();
997 }
998
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -0800999 wiphy_unregister(adapter->wiphy);
1000 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001001
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001002 mwifiex_terminate_workqueue(adapter);
1003
1004 /* Unregister device */
1005 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001006 if (adapter->if_ops.unregister_dev)
1007 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001008 /* Free adapter structure */
1009 dev_dbg(adapter->dev, "info: free adapter\n");
1010 mwifiex_free_adapter(adapter);
1011
1012exit_remove:
1013 up(sem);
1014exit_sem_err:
1015 return 0;
1016}
1017EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1018
1019/*
1020 * This function initializes the module.
1021 *
1022 * The debug FS is also initialized if configured.
1023 */
1024static int
1025mwifiex_init_module(void)
1026{
1027#ifdef CONFIG_DEBUG_FS
1028 mwifiex_debugfs_init();
1029#endif
1030 return 0;
1031}
1032
1033/*
1034 * This function cleans up the module.
1035 *
1036 * The debug FS is removed if available.
1037 */
1038static void
1039mwifiex_cleanup_module(void)
1040{
1041#ifdef CONFIG_DEBUG_FS
1042 mwifiex_debugfs_remove();
1043#endif
1044}
1045
1046module_init(mwifiex_init_module);
1047module_exit(mwifiex_cleanup_module);
1048
1049MODULE_AUTHOR("Marvell International Ltd.");
1050MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1051MODULE_VERSION(VERSION);
1052MODULE_LICENSE("GPL v2");