blob: be5218987101c3beac428ddd131be7191147c6a6 [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
125 kfree(adapter);
126 return 0;
127}
128
Avinash Patil6e251172014-09-12 20:08:59 +0530129static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
130{
131 unsigned long flags;
132 struct sk_buff *skb;
133 bool delay_main_work = adapter->delay_main_work;
134
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 &&
148 (atomic_dec_return(&adapter->rx_pending) <
149 LOW_RX_PENDING)) {
150 adapter->delay_main_work = false;
151 queue_work(adapter->rx_workqueue, &adapter->rx_work);
152 }
153 mwifiex_handle_rx_packet(adapter, skb);
154 }
155 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
156 adapter->rx_processing = false;
157 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
158
159 if (delay_main_work)
160 queue_work(adapter->workqueue, &adapter->main_work);
161exit_rx_proc:
162 return 0;
163}
164
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700165/*
166 * The main process.
167 *
168 * This function is the main procedure of the driver and handles various driver
169 * operations. It runs in a loop and provides the core functionalities.
170 *
171 * The main responsibilities of this function are -
172 * - Ensure concurrency control
173 * - Handle pending interrupts and call interrupt handlers
174 * - Wake up the card if required
175 * - Handle command responses and call response handlers
176 * - Handle events and call event handlers
177 * - Execute pending commands
178 * - Transmit pending data packets
179 */
180int mwifiex_main_process(struct mwifiex_adapter *adapter)
181{
182 int ret = 0;
183 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700184 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700185
186 spin_lock_irqsave(&adapter->main_proc_lock, flags);
187
188 /* Check if already processing */
189 if (adapter->mwifiex_processing) {
190 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
191 goto exit_main_proc;
192 } else {
193 adapter->mwifiex_processing = true;
194 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
195 }
196process_start:
197 do {
198 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
199 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
200 break;
201
Avinash Patil6e251172014-09-12 20:08:59 +0530202 /* If we process interrupts first, it would increase RX pending
203 * even further. Avoid this by checking if rx_pending has
204 * crossed high threshold and schedule rx work queue
205 * and then process interrupts
206 */
207 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING) {
208 adapter->delay_main_work = true;
209 if (!adapter->rx_processing)
210 queue_work(adapter->rx_workqueue,
211 &adapter->rx_work);
212 break;
213 }
214
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700215 /* Handle pending interrupt if any */
216 if (adapter->int_status) {
217 if (adapter->hs_activated)
218 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700219 if (adapter->if_ops.process_int_status)
220 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700221 }
222
Avinash Patil6e251172014-09-12 20:08:59 +0530223 if (adapter->rx_work_enabled && adapter->data_received)
224 queue_work(adapter->rx_workqueue, &adapter->rx_work);
225
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700226 /* Need to wake up the card ? */
227 if ((adapter->ps_state == PS_STATE_SLEEP) &&
228 (adapter->pm_wakeup_card_req &&
229 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700230 (is_command_pending(adapter) ||
231 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700232 adapter->pm_wakeup_fw_try = true;
233 adapter->if_ops.wakeup(adapter);
234 continue;
235 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700236
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700237 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530238 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700239 adapter->pm_wakeup_fw_try = false;
240 if (adapter->ps_state == PS_STATE_SLEEP)
241 adapter->ps_state = PS_STATE_AWAKE;
242 } else {
243 /* We have tried to wakeup the card already */
244 if (adapter->pm_wakeup_fw_try)
245 break;
246 if (adapter->ps_state != PS_STATE_AWAKE ||
247 adapter->tx_lock_flag)
248 break;
249
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530250 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530251 adapter->scan_processing) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700252 mwifiex_wmm_lists_empty(adapter)) {
253 if (adapter->cmd_sent || adapter->curr_cmd ||
254 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700255 break;
256 }
257 }
258
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700259 /* Check Rx data for USB */
260 if (adapter->iface_type == MWIFIEX_USB)
261 while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
262 mwifiex_handle_rx_packet(adapter, skb);
263
Amitkumar Karwar20474122014-04-14 15:31:05 -0700264 /* Check for event */
265 if (adapter->event_received) {
266 adapter->event_received = false;
267 mwifiex_process_event(adapter);
268 }
269
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700270 /* Check for Cmd Resp */
271 if (adapter->cmd_resp_received) {
272 adapter->cmd_resp_received = false;
273 mwifiex_process_cmdresp(adapter);
274
275 /* call mwifiex back when init_fw is done */
276 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
277 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
278 mwifiex_init_fw_complete(adapter);
279 }
280 }
281
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700282 /* Check if we need to confirm Sleep Request
283 received previously */
284 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
285 if (!adapter->cmd_sent && !adapter->curr_cmd)
286 mwifiex_check_ps_cond(adapter);
287 }
288
289 /* * The ps_state may have been changed during processing of
290 * Sleep Request event.
291 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700292 if ((adapter->ps_state == PS_STATE_SLEEP) ||
293 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
294 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
295 adapter->tx_lock_flag)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700296 continue;
297
298 if (!adapter->cmd_sent && !adapter->curr_cmd) {
299 if (mwifiex_exec_next_cmd(adapter) == -1) {
300 ret = -1;
301 break;
302 }
303 }
304
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530305 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530306 !adapter->scan_processing) &&
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700307 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700308 mwifiex_wmm_process_tx(adapter);
309 if (adapter->hs_activated) {
310 adapter->is_hs_configured = false;
311 mwifiex_hs_activated_event
312 (mwifiex_get_priv
313 (adapter, MWIFIEX_BSS_ROLE_ANY),
314 false);
315 }
316 }
317
318 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700319 !adapter->curr_cmd && !is_command_pending(adapter) &&
320 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700321 if (!mwifiex_send_null_packet
322 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
323 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
324 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
325 adapter->delay_null_pkt = false;
326 adapter->ps_state = PS_STATE_SLEEP;
327 }
328 break;
329 }
330 } while (true);
331
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700332 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Avinash Patilf73e5572014-09-29 21:44:13 +0530333 if (!adapter->delay_main_work &&
334 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700335 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
336 goto process_start;
337 }
338
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700339 adapter->mwifiex_processing = false;
340 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
341
342exit_main_proc:
343 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
344 mwifiex_shutdown_drv(adapter);
345 return ret;
346}
Bing Zhao601216e2012-11-05 16:59:15 -0800347EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700348
349/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700350 * This function frees the adapter structure.
351 *
352 * Additionally, this closes the netlink socket, frees the timers
353 * and private structures.
354 */
355static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
356{
357 if (!adapter) {
358 pr_err("%s: adapter is NULL\n", __func__);
359 return;
360 }
361
362 mwifiex_unregister(adapter);
363 pr_debug("info: %s: free adapter\n", __func__);
364}
365
366/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700367 * This function cancels all works in the queue and destroys
368 * the main workqueue.
369 */
370static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
371{
372 flush_workqueue(adapter->workqueue);
373 destroy_workqueue(adapter->workqueue);
374 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530375
376 if (adapter->rx_workqueue) {
377 flush_workqueue(adapter->rx_workqueue);
378 destroy_workqueue(adapter->rx_workqueue);
379 adapter->rx_workqueue = NULL;
380 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700381}
382
383/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700384 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700385 *
386 * The main initialization steps followed are -
387 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700388 * - Issue the init commands to firmware
389 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700390static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700391{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800392 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700393 char fmt[64];
394 struct mwifiex_private *priv;
395 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700396 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700397 struct semaphore *sem = adapter->card_sem;
398 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800399 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700400
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700401 if (!firmware) {
402 dev_err(adapter->dev,
403 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700404 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700405 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700406
407 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
408 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700409 fw.fw_buf = (u8 *) adapter->firmware->data;
410 fw.fw_len = adapter->firmware->size;
411
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700412 if (adapter->if_ops.dnld_fw)
413 ret = adapter->if_ops.dnld_fw(adapter, &fw);
414 else
415 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700416 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700417 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700418
419 dev_notice(adapter->dev, "WLAN FW is active\n");
420
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700421 if (cal_data_cfg) {
422 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
423 adapter->dev)) < 0)
424 dev_err(adapter->dev,
425 "Cal data request_firmware() failed\n");
426 }
427
Daniel Drake232fde02013-07-13 10:57:10 -0400428 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700429 if (adapter->if_ops.enable_int) {
430 if (adapter->if_ops.enable_int(adapter))
431 goto err_dnld_fw;
432 }
Daniel Drake232fde02013-07-13 10:57:10 -0400433
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700434 adapter->init_wait_q_woken = false;
435 ret = mwifiex_init_fw(adapter);
436 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700437 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700438 } else if (!ret) {
439 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
440 goto done;
441 }
442 /* Wait for mwifiex_init to complete */
443 wait_event_interruptible(adapter->init_wait_q,
444 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700445 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700446 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700447
Avinash Patild6bffe82012-05-08 18:30:15 -0700448 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
449 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700450 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800451 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700452 }
453
454 rtnl_lock();
455 /* Create station interface by default */
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800456 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
457 NL80211_IFTYPE_STATION, NULL, NULL);
458 if (IS_ERR(wdev)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700459 dev_err(adapter->dev, "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800460 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700461 goto err_add_intf;
462 }
463 rtnl_unlock();
464
465 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
466 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
467 goto done;
468
469err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700470 wiphy_unregister(adapter->wiphy);
471 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700472err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400473 if (adapter->if_ops.disable_int)
474 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700475err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700476 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700477 if (adapter->if_ops.unregister_dev)
478 adapter->if_ops.unregister_dev(adapter);
479
480 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
481 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
482 pr_debug("info: %s: shutdown mwifiex\n", __func__);
483 adapter->init_wait_q_woken = false;
484
485 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
486 wait_event_interruptible(adapter->init_wait_q,
487 adapter->init_wait_q_woken);
488 }
489 adapter->surprise_removed = true;
490 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700491 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700492done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700493 if (adapter->cal_data) {
494 release_firmware(adapter->cal_data);
495 adapter->cal_data = NULL;
496 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700497 if (adapter->firmware) {
498 release_firmware(adapter->firmware);
499 adapter->firmware = NULL;
500 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700501 if (init_failed)
502 mwifiex_free_adapter(adapter);
503 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700504 return;
505}
506
507/*
508 * This function initializes the hardware and gets firmware.
509 */
510static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
511{
512 int ret;
513
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700514 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
515 adapter->dev, GFP_KERNEL, adapter,
516 mwifiex_fw_dpc);
517 if (ret < 0)
518 dev_err(adapter->dev,
519 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700520 return ret;
521}
522
523/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700524 * CFG802.11 network device handler for open.
525 *
526 * Starts the data queue.
527 */
528static int
529mwifiex_open(struct net_device *dev)
530{
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800531 netif_tx_start_all_queues(dev);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700532 return 0;
533}
534
535/*
536 * CFG802.11 network device handler for close.
537 */
538static int
539mwifiex_close(struct net_device *dev)
540{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700541 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
542
543 if (priv->scan_request) {
544 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
545 cfg80211_scan_done(priv->scan_request, 1);
546 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700547 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700548 }
549
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700550 return 0;
551}
552
553/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700554 * Add buffer into wmm tx queue and queue work to transmit it.
555 */
556int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
557{
Avinash Patil47411a02012-11-01 18:44:16 -0700558 struct netdev_queue *txq;
559 int index = mwifiex_1d_to_wmm_queue[skb->priority];
560
561 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
562 txq = netdev_get_tx_queue(priv->netdev, index);
563 if (!netif_tx_queue_stopped(txq)) {
564 netif_tx_stop_queue(txq);
565 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
566 }
567 }
568
Stone Piaoe39faa72012-09-25 20:23:32 -0700569 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700570 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700571
Stone Piaoe39faa72012-09-25 20:23:32 -0700572 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
573
574 return 0;
575}
576
577/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700578 * CFG802.11 network device handler for data transmission.
579 */
580static int
581mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
582{
583 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700584 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700585 struct mwifiex_txinfo *tx_info;
586
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800587 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700588 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700589
590 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200591 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700592 priv->stats.tx_dropped++;
593 return 0;
594 }
595 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
596 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200597 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700598 priv->stats.tx_dropped++;
599 return 0;
600 }
601 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
602 dev_dbg(priv->adapter->dev,
603 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700604 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700605 /* Insufficient skb headroom - allocate a new skb */
606 new_skb =
607 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
608 if (unlikely(!new_skb)) {
609 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200610 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700611 priv->stats.tx_dropped++;
612 return 0;
613 }
614 kfree_skb(skb);
615 skb = new_skb;
616 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700617 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700618 }
619
620 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700621 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800622 tx_info->bss_num = priv->bss_num;
623 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800624 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700625
626 /* Record the current time the packet was queued; used to
627 * determine the amount of time the packet was queued in
628 * the driver before it was sent to the firmware.
629 * The delay is then sent along with the packet to the
630 * firmware for aggregate delay calculation for stats and
631 * MSDU lifetime expiry.
632 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100633 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700634
Stone Piaoe39faa72012-09-25 20:23:32 -0700635 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700636
637 return 0;
638}
639
640/*
641 * CFG802.11 network device handler for setting MAC address.
642 */
643static int
644mwifiex_set_mac_address(struct net_device *dev, void *addr)
645{
646 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700647 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700648 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700649
650 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
651
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700652 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800653 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
654 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700655
656 if (!ret)
657 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
658 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700659 dev_err(priv->adapter->dev,
660 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700661
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700662 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
663
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700664 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700665}
666
667/*
668 * CFG802.11 network device handler for setting multicast list.
669 */
670static void mwifiex_set_multicast_list(struct net_device *dev)
671{
672 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700673 struct mwifiex_multicast_list mcast_list;
674
675 if (dev->flags & IFF_PROMISC) {
676 mcast_list.mode = MWIFIEX_PROMISC_MODE;
677 } else if (dev->flags & IFF_ALLMULTI ||
678 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
679 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
680 } else {
681 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400682 mcast_list.num_multicast_addr =
683 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700684 }
685 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700686}
687
688/*
689 * CFG802.11 network device handler for transmission timeout.
690 */
691static void
692mwifiex_tx_timeout(struct net_device *dev)
693{
694 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
695
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700696 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800697 priv->tx_timeout_cnt++;
698 dev_err(priv->adapter->dev,
699 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
700 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
701 mwifiex_set_trans_start(dev);
702
703 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
704 priv->adapter->if_ops.card_reset) {
705 dev_err(priv->adapter->dev,
706 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
707 priv->adapter->if_ops.card_reset(priv->adapter);
708 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700709}
710
711/*
712 * CFG802.11 network device handler for statistics retrieval.
713 */
714static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
715{
716 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
717
718 return &priv->stats;
719}
720
Avinash Patil47411a02012-11-01 18:44:16 -0700721static u16
Jason Wangf663dd92014-01-10 16:18:26 +0800722mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100723 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -0700724{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800725 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -0700726 return mwifiex_1d_to_wmm_queue[skb->priority];
727}
728
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700729/* Network device handlers */
730static const struct net_device_ops mwifiex_netdev_ops = {
731 .ndo_open = mwifiex_open,
732 .ndo_stop = mwifiex_close,
733 .ndo_start_xmit = mwifiex_hard_start_xmit,
734 .ndo_set_mac_address = mwifiex_set_mac_address,
735 .ndo_tx_timeout = mwifiex_tx_timeout,
736 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000737 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700738 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700739};
740
741/*
742 * This function initializes the private structure parameters.
743 *
744 * The following wait queues are initialized -
745 * - IOCTL wait queue
746 * - Command wait queue
747 * - Statistics wait queue
748 *
749 * ...and the following default parameters are set -
750 * - Current key index : Set to 0
751 * - Rate index : Set to auto
752 * - Media connected : Set to disconnected
753 * - Adhoc link sensed : Set to false
754 * - Nick name : Set to null
755 * - Number of Tx timeout : Set to 0
756 * - Device address : Set to current address
757 *
758 * In addition, the CFG80211 work queue is also created.
759 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700760void mwifiex_init_priv_params(struct mwifiex_private *priv,
761 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700762{
763 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700764 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700765 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700766 priv->current_key_index = 0;
767 priv->media_connected = false;
768 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700769 memset(priv->mgmt_ie, 0,
770 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700771 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
772 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
773 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
774 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700775 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700776 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
777}
778
779/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700780 * This function check if command is pending.
781 */
782int is_command_pending(struct mwifiex_adapter *adapter)
783{
784 unsigned long flags;
785 int is_cmd_pend_q_empty;
786
787 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
788 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
789 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
790
791 return !is_cmd_pend_q_empty;
792}
793
794/*
Avinash Patil6e251172014-09-12 20:08:59 +0530795 * This is the RX work queue function.
796 *
797 * It handles the RX operations.
798 */
799static void mwifiex_rx_work_queue(struct work_struct *work)
800{
801 struct mwifiex_adapter *adapter =
802 container_of(work, struct mwifiex_adapter, rx_work);
803
804 if (adapter->surprise_removed)
805 return;
806 mwifiex_process_rx(adapter);
807}
808
809/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700810 * This is the main work queue function.
811 *
812 * It handles the main process, which in turn handles the complete
813 * driver operations.
814 */
815static void mwifiex_main_work_queue(struct work_struct *work)
816{
817 struct mwifiex_adapter *adapter =
818 container_of(work, struct mwifiex_adapter, main_work);
819
820 if (adapter->surprise_removed)
821 return;
822 mwifiex_main_process(adapter);
823}
824
825/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700826 * This function adds the card.
827 *
828 * This function follows the following major steps to set up the device -
829 * - Initialize software. This includes probing the card, registering
830 * the interface operations table, and allocating/initializing the
831 * adapter structure
832 * - Set up the netlink socket
833 * - Create and start the main work queue
834 * - Register the device
835 * - Initialize firmware and hardware
836 * - Add logical interfaces
837 */
838int
839mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700840 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700841{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700842 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700843
844 if (down_interruptible(sem))
845 goto exit_sem_err;
846
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700847 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700848 pr_err("%s: software init failed\n", __func__);
849 goto err_init_sw;
850 }
851
Amitkumar Karward930fae2011-10-11 17:41:21 -0700852 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700853 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700854
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700855 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700856 adapter->surprise_removed = false;
857 init_waitqueue_head(&adapter->init_wait_q);
858 adapter->is_suspended = false;
859 adapter->hs_activated = false;
860 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700861 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700862 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700863 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700864
Avinash Patil6e251172014-09-12 20:08:59 +0530865 if (num_possible_cpus() > 1) {
866 adapter->rx_work_enabled = true;
867 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
868 }
869
Amitkumar Karwar448a71c2013-10-11 18:33:04 -0700870 adapter->workqueue =
871 alloc_workqueue("MWIFIEX_WORK_QUEUE",
872 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700873 if (!adapter->workqueue)
874 goto err_kmalloc;
875
876 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +0530877
878 if (adapter->rx_work_enabled) {
879 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
880 WQ_HIGHPRI |
881 WQ_MEM_RECLAIM |
882 WQ_UNBOUND, 1);
883 if (!adapter->rx_workqueue)
884 goto err_kmalloc;
885
886 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
887 }
888
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");