blob: b522f7c3690118720ba01b7206a487fbdfab9ebe [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);
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700333 if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter)) {
334 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
335 goto process_start;
336 }
337
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700338 adapter->mwifiex_processing = false;
339 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
340
341exit_main_proc:
342 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
343 mwifiex_shutdown_drv(adapter);
344 return ret;
345}
Bing Zhao601216e2012-11-05 16:59:15 -0800346EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700347
348/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700349 * This function frees the adapter structure.
350 *
351 * Additionally, this closes the netlink socket, frees the timers
352 * and private structures.
353 */
354static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
355{
356 if (!adapter) {
357 pr_err("%s: adapter is NULL\n", __func__);
358 return;
359 }
360
361 mwifiex_unregister(adapter);
362 pr_debug("info: %s: free adapter\n", __func__);
363}
364
365/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700366 * This function cancels all works in the queue and destroys
367 * the main workqueue.
368 */
369static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
370{
371 flush_workqueue(adapter->workqueue);
372 destroy_workqueue(adapter->workqueue);
373 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530374
375 if (adapter->rx_workqueue) {
376 flush_workqueue(adapter->rx_workqueue);
377 destroy_workqueue(adapter->rx_workqueue);
378 adapter->rx_workqueue = NULL;
379 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700380}
381
382/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700383 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700384 *
385 * The main initialization steps followed are -
386 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700387 * - Issue the init commands to firmware
388 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700389static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700390{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800391 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700392 char fmt[64];
393 struct mwifiex_private *priv;
394 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700395 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700396 struct semaphore *sem = adapter->card_sem;
397 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800398 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700399
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700400 if (!firmware) {
401 dev_err(adapter->dev,
402 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700403 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700404 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700405
406 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
407 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700408 fw.fw_buf = (u8 *) adapter->firmware->data;
409 fw.fw_len = adapter->firmware->size;
410
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700411 if (adapter->if_ops.dnld_fw)
412 ret = adapter->if_ops.dnld_fw(adapter, &fw);
413 else
414 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700415 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700416 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700417
418 dev_notice(adapter->dev, "WLAN FW is active\n");
419
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700420 if (cal_data_cfg) {
421 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
422 adapter->dev)) < 0)
423 dev_err(adapter->dev,
424 "Cal data request_firmware() failed\n");
425 }
426
Daniel Drake232fde02013-07-13 10:57:10 -0400427 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700428 if (adapter->if_ops.enable_int) {
429 if (adapter->if_ops.enable_int(adapter))
430 goto err_dnld_fw;
431 }
Daniel Drake232fde02013-07-13 10:57:10 -0400432
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700433 adapter->init_wait_q_woken = false;
434 ret = mwifiex_init_fw(adapter);
435 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700436 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700437 } else if (!ret) {
438 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
439 goto done;
440 }
441 /* Wait for mwifiex_init to complete */
442 wait_event_interruptible(adapter->init_wait_q,
443 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700444 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700445 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700446
Avinash Patild6bffe82012-05-08 18:30:15 -0700447 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
448 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700449 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800450 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700451 }
452
453 rtnl_lock();
454 /* Create station interface by default */
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800455 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
456 NL80211_IFTYPE_STATION, NULL, NULL);
457 if (IS_ERR(wdev)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700458 dev_err(adapter->dev, "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800459 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700460 goto err_add_intf;
461 }
462 rtnl_unlock();
463
464 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
465 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
466 goto done;
467
468err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700469 wiphy_unregister(adapter->wiphy);
470 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700471err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400472 if (adapter->if_ops.disable_int)
473 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700474err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700475 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700476 if (adapter->if_ops.unregister_dev)
477 adapter->if_ops.unregister_dev(adapter);
478
479 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
480 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
481 pr_debug("info: %s: shutdown mwifiex\n", __func__);
482 adapter->init_wait_q_woken = false;
483
484 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
485 wait_event_interruptible(adapter->init_wait_q,
486 adapter->init_wait_q_woken);
487 }
488 adapter->surprise_removed = true;
489 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700490 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700491done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700492 if (adapter->cal_data) {
493 release_firmware(adapter->cal_data);
494 adapter->cal_data = NULL;
495 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700496 if (adapter->firmware) {
497 release_firmware(adapter->firmware);
498 adapter->firmware = NULL;
499 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700500 if (init_failed)
501 mwifiex_free_adapter(adapter);
502 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700503 return;
504}
505
506/*
507 * This function initializes the hardware and gets firmware.
508 */
509static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
510{
511 int ret;
512
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700513 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
514 adapter->dev, GFP_KERNEL, adapter,
515 mwifiex_fw_dpc);
516 if (ret < 0)
517 dev_err(adapter->dev,
518 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700519 return ret;
520}
521
522/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700523 * CFG802.11 network device handler for open.
524 *
525 * Starts the data queue.
526 */
527static int
528mwifiex_open(struct net_device *dev)
529{
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800530 netif_tx_start_all_queues(dev);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700531 return 0;
532}
533
534/*
535 * CFG802.11 network device handler for close.
536 */
537static int
538mwifiex_close(struct net_device *dev)
539{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700540 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
541
542 if (priv->scan_request) {
543 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
544 cfg80211_scan_done(priv->scan_request, 1);
545 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700546 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700547 }
548
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700549 return 0;
550}
551
552/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700553 * Add buffer into wmm tx queue and queue work to transmit it.
554 */
555int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
556{
Avinash Patil47411a02012-11-01 18:44:16 -0700557 struct netdev_queue *txq;
558 int index = mwifiex_1d_to_wmm_queue[skb->priority];
559
560 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
561 txq = netdev_get_tx_queue(priv->netdev, index);
562 if (!netif_tx_queue_stopped(txq)) {
563 netif_tx_stop_queue(txq);
564 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
565 }
566 }
567
Stone Piaoe39faa72012-09-25 20:23:32 -0700568 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700569 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700570
Stone Piaoe39faa72012-09-25 20:23:32 -0700571 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
572
573 return 0;
574}
575
576/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700577 * CFG802.11 network device handler for data transmission.
578 */
579static int
580mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
581{
582 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700583 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700584 struct mwifiex_txinfo *tx_info;
585
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800586 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700587 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700588
589 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200590 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700591 priv->stats.tx_dropped++;
592 return 0;
593 }
594 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
595 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200596 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700597 priv->stats.tx_dropped++;
598 return 0;
599 }
600 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
601 dev_dbg(priv->adapter->dev,
602 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700603 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700604 /* Insufficient skb headroom - allocate a new skb */
605 new_skb =
606 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
607 if (unlikely(!new_skb)) {
608 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200609 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700610 priv->stats.tx_dropped++;
611 return 0;
612 }
613 kfree_skb(skb);
614 skb = new_skb;
615 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700616 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700617 }
618
619 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700620 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800621 tx_info->bss_num = priv->bss_num;
622 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800623 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700624
625 /* Record the current time the packet was queued; used to
626 * determine the amount of time the packet was queued in
627 * the driver before it was sent to the firmware.
628 * The delay is then sent along with the packet to the
629 * firmware for aggregate delay calculation for stats and
630 * MSDU lifetime expiry.
631 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100632 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700633
Stone Piaoe39faa72012-09-25 20:23:32 -0700634 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700635
636 return 0;
637}
638
639/*
640 * CFG802.11 network device handler for setting MAC address.
641 */
642static int
643mwifiex_set_mac_address(struct net_device *dev, void *addr)
644{
645 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700646 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700647 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700648
649 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
650
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700651 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800652 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
653 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700654
655 if (!ret)
656 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
657 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700658 dev_err(priv->adapter->dev,
659 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700660
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700661 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
662
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700663 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700664}
665
666/*
667 * CFG802.11 network device handler for setting multicast list.
668 */
669static void mwifiex_set_multicast_list(struct net_device *dev)
670{
671 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700672 struct mwifiex_multicast_list mcast_list;
673
674 if (dev->flags & IFF_PROMISC) {
675 mcast_list.mode = MWIFIEX_PROMISC_MODE;
676 } else if (dev->flags & IFF_ALLMULTI ||
677 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
678 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
679 } else {
680 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400681 mcast_list.num_multicast_addr =
682 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700683 }
684 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700685}
686
687/*
688 * CFG802.11 network device handler for transmission timeout.
689 */
690static void
691mwifiex_tx_timeout(struct net_device *dev)
692{
693 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
694
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700695 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800696 priv->tx_timeout_cnt++;
697 dev_err(priv->adapter->dev,
698 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
699 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
700 mwifiex_set_trans_start(dev);
701
702 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
703 priv->adapter->if_ops.card_reset) {
704 dev_err(priv->adapter->dev,
705 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
706 priv->adapter->if_ops.card_reset(priv->adapter);
707 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700708}
709
710/*
711 * CFG802.11 network device handler for statistics retrieval.
712 */
713static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
714{
715 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
716
717 return &priv->stats;
718}
719
Avinash Patil47411a02012-11-01 18:44:16 -0700720static u16
Jason Wangf663dd92014-01-10 16:18:26 +0800721mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100722 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -0700723{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800724 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -0700725 return mwifiex_1d_to_wmm_queue[skb->priority];
726}
727
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700728/* Network device handlers */
729static const struct net_device_ops mwifiex_netdev_ops = {
730 .ndo_open = mwifiex_open,
731 .ndo_stop = mwifiex_close,
732 .ndo_start_xmit = mwifiex_hard_start_xmit,
733 .ndo_set_mac_address = mwifiex_set_mac_address,
734 .ndo_tx_timeout = mwifiex_tx_timeout,
735 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000736 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700737 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700738};
739
740/*
741 * This function initializes the private structure parameters.
742 *
743 * The following wait queues are initialized -
744 * - IOCTL wait queue
745 * - Command wait queue
746 * - Statistics wait queue
747 *
748 * ...and the following default parameters are set -
749 * - Current key index : Set to 0
750 * - Rate index : Set to auto
751 * - Media connected : Set to disconnected
752 * - Adhoc link sensed : Set to false
753 * - Nick name : Set to null
754 * - Number of Tx timeout : Set to 0
755 * - Device address : Set to current address
756 *
757 * In addition, the CFG80211 work queue is also created.
758 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700759void mwifiex_init_priv_params(struct mwifiex_private *priv,
760 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700761{
762 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700763 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700764 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700765 priv->current_key_index = 0;
766 priv->media_connected = false;
767 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700768 memset(priv->mgmt_ie, 0,
769 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700770 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
771 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
772 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
773 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700774 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700775 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
776}
777
778/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700779 * This function check if command is pending.
780 */
781int is_command_pending(struct mwifiex_adapter *adapter)
782{
783 unsigned long flags;
784 int is_cmd_pend_q_empty;
785
786 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
787 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
788 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
789
790 return !is_cmd_pend_q_empty;
791}
792
793/*
Avinash Patil6e251172014-09-12 20:08:59 +0530794 * This is the RX work queue function.
795 *
796 * It handles the RX operations.
797 */
798static void mwifiex_rx_work_queue(struct work_struct *work)
799{
800 struct mwifiex_adapter *adapter =
801 container_of(work, struct mwifiex_adapter, rx_work);
802
803 if (adapter->surprise_removed)
804 return;
805 mwifiex_process_rx(adapter);
806}
807
808/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700809 * This is the main work queue function.
810 *
811 * It handles the main process, which in turn handles the complete
812 * driver operations.
813 */
814static void mwifiex_main_work_queue(struct work_struct *work)
815{
816 struct mwifiex_adapter *adapter =
817 container_of(work, struct mwifiex_adapter, main_work);
818
819 if (adapter->surprise_removed)
820 return;
821 mwifiex_main_process(adapter);
822}
823
824/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700825 * This function adds the card.
826 *
827 * This function follows the following major steps to set up the device -
828 * - Initialize software. This includes probing the card, registering
829 * the interface operations table, and allocating/initializing the
830 * adapter structure
831 * - Set up the netlink socket
832 * - Create and start the main work queue
833 * - Register the device
834 * - Initialize firmware and hardware
835 * - Add logical interfaces
836 */
837int
838mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700839 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700840{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700841 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700842
843 if (down_interruptible(sem))
844 goto exit_sem_err;
845
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700846 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700847 pr_err("%s: software init failed\n", __func__);
848 goto err_init_sw;
849 }
850
Amitkumar Karward930fae2011-10-11 17:41:21 -0700851 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700852 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700853
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700854 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700855 adapter->surprise_removed = false;
856 init_waitqueue_head(&adapter->init_wait_q);
857 adapter->is_suspended = false;
858 adapter->hs_activated = false;
859 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700860 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700861 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700862 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700863
Avinash Patil6e251172014-09-12 20:08:59 +0530864 if (num_possible_cpus() > 1) {
865 adapter->rx_work_enabled = true;
866 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
867 }
868
Amitkumar Karwar448a71c2013-10-11 18:33:04 -0700869 adapter->workqueue =
870 alloc_workqueue("MWIFIEX_WORK_QUEUE",
871 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700872 if (!adapter->workqueue)
873 goto err_kmalloc;
874
875 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +0530876
877 if (adapter->rx_work_enabled) {
878 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
879 WQ_HIGHPRI |
880 WQ_MEM_RECLAIM |
881 WQ_UNBOUND, 1);
882 if (!adapter->rx_workqueue)
883 goto err_kmalloc;
884
885 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
886 }
887
Amitkumar Karwar92c25382014-06-19 21:38:52 -0700888 if (adapter->if_ops.iface_work)
889 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700890
891 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -0400892 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700893 if (adapter->if_ops.register_dev(adapter)) {
894 pr_err("%s: failed to register mwifiex device\n", __func__);
895 goto err_registerdev;
896 }
897
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700898 if (mwifiex_init_hw_fw(adapter)) {
899 pr_err("%s: firmware init failed\n", __func__);
900 goto err_init_fw;
901 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700902
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700903 return 0;
904
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700905err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700906 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700907 if (adapter->if_ops.unregister_dev)
908 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700909 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
910 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
911 pr_debug("info: %s: shutdown mwifiex\n", __func__);
912 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700913
914 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700915 wait_event_interruptible(adapter->init_wait_q,
916 adapter->init_wait_q_woken);
917 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700918err_registerdev:
919 adapter->surprise_removed = true;
920 mwifiex_terminate_workqueue(adapter);
921err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700922 mwifiex_free_adapter(adapter);
923
924err_init_sw:
925 up(sem);
926
927exit_sem_err:
928 return -1;
929}
930EXPORT_SYMBOL_GPL(mwifiex_add_card);
931
932/*
933 * This function removes the card.
934 *
935 * This function follows the following major steps to remove the device -
936 * - Stop data traffic
937 * - Shutdown firmware
938 * - Remove the logical interfaces
939 * - Terminate the work queue
940 * - Unregister the device
941 * - Free the adapter structure
942 */
943int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
944{
945 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700946 int i;
947
948 if (down_interruptible(sem))
949 goto exit_sem_err;
950
951 if (!adapter)
952 goto exit_remove;
953
Daniel Drake232fde02013-07-13 10:57:10 -0400954 /* We can no longer handle interrupts once we start doing the teardown
955 * below. */
956 if (adapter->if_ops.disable_int)
957 adapter->if_ops.disable_int(adapter);
958
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700959 adapter->surprise_removed = true;
960
961 /* Stop data */
962 for (i = 0; i < adapter->priv_num; i++) {
963 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700964 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -0700965 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700966 if (netif_carrier_ok(priv->netdev))
967 netif_carrier_off(priv->netdev);
968 }
969 }
970
971 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
972 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700973
974 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700975 wait_event_interruptible(adapter->init_wait_q,
976 adapter->init_wait_q_woken);
977 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
978 if (atomic_read(&adapter->rx_pending) ||
979 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700980 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700981 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700982 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700983 atomic_read(&adapter->rx_pending),
984 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700985 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700986 }
987
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700988 for (i = 0; i < adapter->priv_num; i++) {
989 priv = adapter->priv[i];
990
991 if (!priv)
992 continue;
993
994 rtnl_lock();
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800995 if (priv->wdev && priv->netdev)
Johannes Berg84efbb82012-06-16 00:00:26 +0200996 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700997 rtnl_unlock();
998 }
999
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001000 wiphy_unregister(adapter->wiphy);
1001 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001002
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001003 mwifiex_terminate_workqueue(adapter);
1004
1005 /* Unregister device */
1006 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001007 if (adapter->if_ops.unregister_dev)
1008 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001009 /* Free adapter structure */
1010 dev_dbg(adapter->dev, "info: free adapter\n");
1011 mwifiex_free_adapter(adapter);
1012
1013exit_remove:
1014 up(sem);
1015exit_sem_err:
1016 return 0;
1017}
1018EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1019
1020/*
1021 * This function initializes the module.
1022 *
1023 * The debug FS is also initialized if configured.
1024 */
1025static int
1026mwifiex_init_module(void)
1027{
1028#ifdef CONFIG_DEBUG_FS
1029 mwifiex_debugfs_init();
1030#endif
1031 return 0;
1032}
1033
1034/*
1035 * This function cleans up the module.
1036 *
1037 * The debug FS is removed if available.
1038 */
1039static void
1040mwifiex_cleanup_module(void)
1041{
1042#ifdef CONFIG_DEBUG_FS
1043 mwifiex_debugfs_remove();
1044#endif
1045}
1046
1047module_init(mwifiex_init_module);
1048module_exit(mwifiex_cleanup_module);
1049
1050MODULE_AUTHOR("Marvell International Ltd.");
1051MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1052MODULE_VERSION(VERSION);
1053MODULE_LICENSE("GPL v2");