]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/libertas/main.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / libertas / main.c
CommitLineData
876c9d3a
MT
1/**
2 * This file contains the major functions in WLAN
3 * driver. It includes init, exit, open, close and main
4 * thread etc..
5 */
6
a46c6410 7#include <linux/moduleparam.h>
876c9d3a 8#include <linux/delay.h>
876c9d3a
MT
9#include <linux/etherdevice.h>
10#include <linux/netdevice.h>
11#include <linux/if_arp.h>
fe336150 12#include <linux/kthread.h>
7919b89c 13#include <linux/kfifo.h>
75bf45a7 14#include <linux/stddef.h>
2c706002 15#include <linux/ieee80211.h>
5a0e3ad6 16#include <linux/slab.h>
876c9d3a 17#include <net/iw_handler.h>
ff9fc791 18#include <net/cfg80211.h>
876c9d3a
MT
19
20#include "host.h"
876c9d3a
MT
21#include "decl.h"
22#include "dev.h"
876c9d3a 23#include "wext.h"
ff9fc791 24#include "cfg.h"
876c9d3a 25#include "debugfs.h"
245bf20f 26#include "scan.h"
876c9d3a 27#include "assoc.h"
6e66f03f 28#include "cmd.h"
876c9d3a 29
7856a541 30#define DRIVER_RELEASE_VERSION "323.p0"
10078321 31const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
3ce40232
DW
32#ifdef DEBUG
33 "-dbg"
34#endif
35 "";
36
a46c6410
HS
37
38/* Module parameters */
10078321
HS
39unsigned int lbs_debug;
40EXPORT_SYMBOL_GPL(lbs_debug);
41module_param_named(libertas_debug, lbs_debug, int, 0644);
a46c6410
HS
42
43
f539f2ef
HS
44/* This global structure is used to send the confirm_sleep command as
45 * fast as possible down to the firmware. */
46struct cmd_confirm_sleep confirm_sleep;
47
48
876c9d3a 49/**
8c512765 50 * the table to keep region code
876c9d3a 51 */
10078321 52u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
8c512765 53 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
876c9d3a 54
876c9d3a 55/**
8c512765
DW
56 * FW rate table. FW refers to rates by their index in this table, not by the
57 * rate value itself. Values of 0x00 are
58 * reserved positions.
876c9d3a 59 */
8c512765
DW
60static u8 fw_data_rates[MAX_RATES] =
61 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
62 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
63};
876c9d3a 64
876c9d3a 65/**
8c512765
DW
66 * @brief use index to get the data rate
67 *
68 * @param idx The index of data rate
69 * @return data rate or 0
876c9d3a 70 */
10078321 71u32 lbs_fw_index_to_data_rate(u8 idx)
8c512765
DW
72{
73 if (idx >= sizeof(fw_data_rates))
74 idx = 0;
75 return fw_data_rates[idx];
76}
77
78/**
79 * @brief use rate to get the index
80 *
81 * @param rate data rate
82 * @return index or 0
83 */
10078321 84u8 lbs_data_rate_to_fw_index(u32 rate)
8c512765
DW
85{
86 u8 i;
87
88 if (!rate)
89 return 0;
90
91 for (i = 0; i < sizeof(fw_data_rates); i++) {
92 if (rate == fw_data_rates[i])
93 return i;
94 }
95 return 0;
96}
876c9d3a 97
6fb53252 98
2fd6cfe3
DW
99static int lbs_add_rtap(struct lbs_private *priv);
100static void lbs_remove_rtap(struct lbs_private *priv);
7e226272 101
965f8bbc
LCC
102
103/**
104 * Get function for sysfs attribute rtap
105 */
10078321 106static ssize_t lbs_rtap_get(struct device *dev,
965f8bbc
LCC
107 struct device_attribute *attr, char * buf)
108{
ab65f649 109 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
aa21c004 110 return snprintf(buf, 5, "0x%X\n", priv->monitormode);
965f8bbc
LCC
111}
112
113/**
114 * Set function for sysfs attribute rtap
115 */
10078321 116static ssize_t lbs_rtap_set(struct device *dev,
965f8bbc
LCC
117 struct device_attribute *attr, const char * buf, size_t count)
118{
119 int monitor_mode;
ab65f649 120 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
965f8bbc
LCC
121
122 sscanf(buf, "%x", &monitor_mode);
c2b310a7
HS
123 if (monitor_mode) {
124 if (priv->monitormode == monitor_mode)
965f8bbc 125 return strlen(buf);
c2b310a7 126 if (!priv->monitormode) {
602114ae 127 if (priv->infra_open || lbs_mesh_open(priv))
8552855f 128 return -EBUSY;
aa21c004 129 if (priv->mode == IW_MODE_INFRA)
191bb40e
DW
130 lbs_cmd_80211_deauthenticate(priv,
131 priv->curbssparams.bssid,
132 WLAN_REASON_DEAUTH_LEAVING);
aa21c004 133 else if (priv->mode == IW_MODE_ADHOC)
f5fe1fda 134 lbs_adhoc_stop(priv);
10078321 135 lbs_add_rtap(priv);
965f8bbc 136 }
aa21c004 137 priv->monitormode = monitor_mode;
3b72b01d 138 } else {
c2b310a7 139 if (!priv->monitormode)
965f8bbc 140 return strlen(buf);
c2b310a7 141 priv->monitormode = 0;
10078321 142 lbs_remove_rtap(priv);
f86a93e1 143
aa21c004
DW
144 if (priv->currenttxskb) {
145 dev_kfree_skb_any(priv->currenttxskb);
146 priv->currenttxskb = NULL;
f86a93e1
DW
147 }
148
149 /* Wake queues, command thread, etc. */
150 lbs_host_to_card_done(priv);
965f8bbc
LCC
151 }
152
10078321 153 lbs_prepare_and_send_command(priv,
965f8bbc 154 CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
aa21c004 155 CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
965f8bbc
LCC
156 return strlen(buf);
157}
158
159/**
23a397ac
DW
160 * lbs_rtap attribute to be exported per ethX interface
161 * through sysfs (/sys/class/net/ethX/lbs_rtap)
965f8bbc 162 */
23a397ac
DW
163static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
164
165/**
e0e42da3 166 * @brief This function opens the ethX interface
876c9d3a
MT
167 *
168 * @param dev A pointer to net_device structure
8552855f 169 * @return 0 or -EBUSY if monitor mode active
876c9d3a 170 */
10078321 171static int lbs_dev_open(struct net_device *dev)
876c9d3a 172{
ab65f649 173 struct lbs_private *priv = dev->ml_priv;
8552855f 174 int ret = 0;
876c9d3a 175
61d30020
HS
176 lbs_deb_enter(LBS_DEB_NET);
177
8552855f 178 spin_lock_irq(&priv->driver_lock);
876c9d3a 179
c2b310a7 180 if (priv->monitormode) {
8552855f
DW
181 ret = -EBUSY;
182 goto out;
183 }
01d77d8d 184
e0e42da3 185 priv->infra_open = 1;
7e226272 186
e0e42da3
HS
187 if (priv->connect_status == LBS_CONNECTED)
188 netif_carrier_on(dev);
189 else
190 netif_carrier_off(dev);
876c9d3a 191
8552855f
DW
192 if (!priv->tx_pending_len)
193 netif_wake_queue(dev);
194 out:
876c9d3a 195
8552855f 196 spin_unlock_irq(&priv->driver_lock);
61d30020 197 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
8552855f 198 return ret;
876c9d3a
MT
199}
200
876c9d3a
MT
201/**
202 * @brief This function closes the ethX interface
203 *
204 * @param dev A pointer to net_device structure
205 * @return 0
206 */
8552855f 207static int lbs_eth_stop(struct net_device *dev)
78523daa 208{
ab65f649 209 struct lbs_private *priv = dev->ml_priv;
876c9d3a 210
61d30020 211 lbs_deb_enter(LBS_DEB_NET);
8552855f 212
61d30020 213 spin_lock_irq(&priv->driver_lock);
876c9d3a 214 priv->infra_open = 0;
8552855f 215 netif_stop_queue(dev);
8552855f 216 spin_unlock_irq(&priv->driver_lock);
61d30020 217
75bf45a7
DW
218 schedule_work(&priv->mcast_work);
219
61d30020 220 lbs_deb_leave(LBS_DEB_NET);
8552855f 221 return 0;
876c9d3a
MT
222}
223
10078321 224static void lbs_tx_timeout(struct net_device *dev)
876c9d3a 225{
ab65f649 226 struct lbs_private *priv = dev->ml_priv;
876c9d3a 227
9012b28a 228 lbs_deb_enter(LBS_DEB_TX);
876c9d3a 229
9012b28a 230 lbs_pr_err("tx watch dog timeout\n");
876c9d3a 231
876c9d3a
MT
232 dev->trans_start = jiffies;
233
7919b89c
HS
234 if (priv->currenttxskb)
235 lbs_send_tx_feedback(priv, 0);
236
a63b22bb
DW
237 /* XX: Shouldn't we also call into the hw-specific driver
238 to kick it somehow? */
239 lbs_host_to_card_done(priv);
876c9d3a 240
354eca98
DW
241 /* More often than not, this actually happens because the
242 firmware has crapped itself -- rather than just a very
243 busy medium. So send a harmless command, and if/when
244 _that_ times out, we'll kick it in the head. */
245 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
246 0, 0, NULL);
247
9012b28a 248 lbs_deb_leave(LBS_DEB_TX);
876c9d3a
MT
249}
250
e775ed7c
DW
251void lbs_host_to_card_done(struct lbs_private *priv)
252{
a63b22bb
DW
253 unsigned long flags;
254
61d30020
HS
255 lbs_deb_enter(LBS_DEB_THREAD);
256
a63b22bb 257 spin_lock_irqsave(&priv->driver_lock, flags);
e775ed7c
DW
258
259 priv->dnld_sent = DNLD_RES_RECEIVED;
260
261 /* Wake main thread if commands are pending */
49125454
AK
262 if (!priv->cur_cmd || priv->tx_pending_len > 0) {
263 if (!priv->wakeup_dev_required)
264 wake_up_interruptible(&priv->waitq);
265 }
e775ed7c 266
a63b22bb 267 spin_unlock_irqrestore(&priv->driver_lock, flags);
61d30020 268 lbs_deb_leave(LBS_DEB_THREAD);
e775ed7c
DW
269}
270EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
271
e0e42da3 272int lbs_set_mac_address(struct net_device *dev, void *addr)
876c9d3a
MT
273{
274 int ret = 0;
ab65f649 275 struct lbs_private *priv = dev->ml_priv;
876c9d3a 276 struct sockaddr *phwaddr = addr;
2af9f039 277 struct cmd_ds_802_11_mac_address cmd;
876c9d3a 278
9012b28a 279 lbs_deb_enter(LBS_DEB_NET);
876c9d3a 280
0a0d08ac 281 /* In case it was called from the mesh device */
2af9f039 282 dev = priv->dev;
0a0d08ac 283
2af9f039
HS
284 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
285 cmd.action = cpu_to_le16(CMD_ACT_SET);
286 memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
876c9d3a 287
2af9f039 288 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
876c9d3a 289 if (ret) {
9012b28a 290 lbs_deb_net("set MAC address failed\n");
876c9d3a
MT
291 goto done;
292 }
293
2af9f039
HS
294 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
295 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
78523daa 296 if (priv->mesh_dev)
2af9f039 297 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
876c9d3a
MT
298
299done:
9012b28a 300 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
876c9d3a
MT
301 return ret;
302}
303
75bf45a7
DW
304
305static inline int mac_in_list(unsigned char *list, int list_len,
306 unsigned char *mac)
876c9d3a 307{
75bf45a7
DW
308 while (list_len) {
309 if (!memcmp(list, mac, ETH_ALEN))
310 return 1;
311 list += ETH_ALEN;
312 list_len--;
313 }
314 return 0;
315}
316
876c9d3a 317
75bf45a7
DW
318static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
319 struct net_device *dev, int nr_addrs)
320{
321 int i = nr_addrs;
322 struct dev_mc_list *mc_list;
655ffee2 323 int cnt;
75bf45a7
DW
324
325 if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
326 return nr_addrs;
327
b9e40857 328 netif_addr_lock_bh(dev);
655ffee2
JP
329 cnt = netdev_mc_count(dev);
330 netdev_for_each_mc_addr(mc_list, dev) {
75bf45a7 331 if (mac_in_list(cmd->maclist, nr_addrs, mc_list->dmi_addr)) {
e174961c
JB
332 lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
333 mc_list->dmi_addr);
655ffee2 334 cnt--;
75bf45a7
DW
335 continue;
336 }
876c9d3a 337
75bf45a7
DW
338 if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
339 break;
340 memcpy(&cmd->maclist[6*i], mc_list->dmi_addr, ETH_ALEN);
e174961c
JB
341 lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
342 mc_list->dmi_addr);
75bf45a7 343 i++;
655ffee2 344 cnt--;
876c9d3a 345 }
b9e40857 346 netif_addr_unlock_bh(dev);
655ffee2 347 if (cnt)
75bf45a7
DW
348 return -EOVERFLOW;
349
876c9d3a 350 return i;
876c9d3a
MT
351}
352
75bf45a7 353static void lbs_set_mcast_worker(struct work_struct *work)
876c9d3a 354{
75bf45a7
DW
355 struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
356 struct cmd_ds_mac_multicast_adr mcast_cmd;
357 int dev_flags;
358 int nr_addrs;
359 int old_mac_control = priv->mac_control;
876c9d3a 360
9012b28a 361 lbs_deb_enter(LBS_DEB_NET);
876c9d3a 362
75bf45a7
DW
363 dev_flags = priv->dev->flags;
364 if (priv->mesh_dev)
365 dev_flags |= priv->mesh_dev->flags;
366
367 if (dev_flags & IFF_PROMISC) {
368 priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
369 priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
370 CMD_ACT_MAC_MULTICAST_ENABLE);
371 goto out_set_mac_control;
372 } else if (dev_flags & IFF_ALLMULTI) {
373 do_allmulti:
374 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
375 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
376 CMD_ACT_MAC_MULTICAST_ENABLE);
377 goto out_set_mac_control;
876c9d3a
MT
378 }
379
75bf45a7
DW
380 /* Once for priv->dev, again for priv->mesh_dev if it exists */
381 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
382 if (nr_addrs >= 0 && priv->mesh_dev)
383 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
384 if (nr_addrs < 0)
385 goto do_allmulti;
386
387 if (nr_addrs) {
388 int size = offsetof(struct cmd_ds_mac_multicast_adr,
389 maclist[6*nr_addrs]);
390
391 mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
392 mcast_cmd.hdr.size = cpu_to_le16(size);
393 mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
394
395 lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
396
397 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
398 } else
399 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
400
401 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
402 CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
403 out_set_mac_control:
d9e9778c
HS
404 if (priv->mac_control != old_mac_control)
405 lbs_set_mac_control(priv);
876c9d3a 406
9012b28a 407 lbs_deb_leave(LBS_DEB_NET);
876c9d3a
MT
408}
409
e0e42da3 410void lbs_set_multicast_list(struct net_device *dev)
75bf45a7 411{
ab65f649 412 struct lbs_private *priv = dev->ml_priv;
75bf45a7
DW
413
414 schedule_work(&priv->mcast_work);
415}
416
876c9d3a 417/**
10078321 418 * @brief This function handles the major jobs in the LBS driver.
9012b28a
HS
419 * It handles all events generated by firmware, RX data received
420 * from firmware and TX data sent from kernel.
876c9d3a 421 *
10078321 422 * @param data A pointer to lbs_thread structure
876c9d3a
MT
423 * @return 0
424 */
10078321 425static int lbs_thread(void *data)
876c9d3a 426{
fe336150 427 struct net_device *dev = data;
ab65f649 428 struct lbs_private *priv = dev->ml_priv;
876c9d3a 429 wait_queue_t wait;
876c9d3a 430
9012b28a 431 lbs_deb_enter(LBS_DEB_THREAD);
876c9d3a 432
876c9d3a
MT
433 init_waitqueue_entry(&wait, current);
434
435 for (;;) {
b8d40bc9 436 int shouldsleep;
7919b89c 437 u8 resp_idx;
b8d40bc9 438
7919b89c
HS
439 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
440 priv->currenttxskb, priv->dnld_sent);
876c9d3a 441
fe336150 442 add_wait_queue(&priv->waitq, &wait);
876c9d3a 443 set_current_state(TASK_INTERRUPTIBLE);
aa21c004 444 spin_lock_irq(&priv->driver_lock);
59f3e4bf 445
d9f88705 446 if (kthread_should_stop())
b8d40bc9 447 shouldsleep = 0; /* Bye */
d9f88705
DW
448 else if (priv->surpriseremoved)
449 shouldsleep = 1; /* We need to wait until we're _told_ to die */
b8d40bc9
DW
450 else if (priv->psstate == PS_STATE_SLEEP)
451 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
2a345099
DW
452 else if (priv->cmd_timed_out)
453 shouldsleep = 0; /* Command timed out. Recover */
b15152a4
DW
454 else if (!priv->fw_ready)
455 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
b8d40bc9
DW
456 else if (priv->dnld_sent)
457 shouldsleep = 1; /* Something is en route to the device already */
2eb188a1
DW
458 else if (priv->tx_pending_len > 0)
459 shouldsleep = 0; /* We've a packet to send */
ef707b83
HS
460 else if (priv->resp_len[priv->resp_idx])
461 shouldsleep = 0; /* We have a command response */
b8d40bc9
DW
462 else if (priv->cur_cmd)
463 shouldsleep = 1; /* Can't send a command; one already running */
49125454
AK
464 else if (!list_empty(&priv->cmdpendingq) &&
465 !(priv->wakeup_dev_required))
b8d40bc9 466 shouldsleep = 0; /* We have a command to send */
e64c026d 467 else if (kfifo_len(&priv->event_fifo))
7919b89c 468 shouldsleep = 0; /* We have an event to process */
b8d40bc9
DW
469 else
470 shouldsleep = 1; /* No command */
471
472 if (shouldsleep) {
7919b89c 473 lbs_deb_thread("sleeping, connect_status %d, "
5f505d90 474 "psmode %d, psstate %d\n",
7919b89c
HS
475 priv->connect_status,
476 priv->psmode, priv->psstate);
aa21c004 477 spin_unlock_irq(&priv->driver_lock);
876c9d3a
MT
478 schedule();
479 } else
aa21c004 480 spin_unlock_irq(&priv->driver_lock);
876c9d3a 481
7919b89c
HS
482 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
483 priv->currenttxskb, priv->dnld_sent);
876c9d3a
MT
484
485 set_current_state(TASK_RUNNING);
fe336150 486 remove_wait_queue(&priv->waitq, &wait);
876c9d3a 487
7919b89c
HS
488 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
489 priv->currenttxskb, priv->dnld_sent);
876c9d3a 490
d9f88705 491 if (kthread_should_stop()) {
7919b89c 492 lbs_deb_thread("break from main thread\n");
876c9d3a
MT
493 break;
494 }
495
d9f88705
DW
496 if (priv->surpriseremoved) {
497 lbs_deb_thread("adapter removed; waiting to die...\n");
498 continue;
499 }
876c9d3a 500
7919b89c
HS
501 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
502 priv->currenttxskb, priv->dnld_sent);
59f3e4bf 503
7919b89c 504 /* Process any pending command response */
a01f5450 505 spin_lock_irq(&priv->driver_lock);
7919b89c
HS
506 resp_idx = priv->resp_idx;
507 if (priv->resp_len[resp_idx]) {
aa21c004 508 spin_unlock_irq(&priv->driver_lock);
7919b89c
HS
509 lbs_process_command_response(priv,
510 priv->resp_buf[resp_idx],
511 priv->resp_len[resp_idx]);
aa21c004 512 spin_lock_irq(&priv->driver_lock);
7919b89c 513 priv->resp_len[resp_idx] = 0;
876c9d3a 514 }
7919b89c 515 spin_unlock_irq(&priv->driver_lock);
876c9d3a 516
49125454
AK
517 /* Process hardware events, e.g. card removed, link lost */
518 spin_lock_irq(&priv->driver_lock);
e64c026d 519 while (kfifo_len(&priv->event_fifo)) {
49125454 520 u32 event;
7acd72eb 521
9842c38e
SS
522 if (kfifo_out(&priv->event_fifo,
523 (unsigned char *) &event, sizeof(event)) !=
524 sizeof(event))
525 break;
49125454
AK
526 spin_unlock_irq(&priv->driver_lock);
527 lbs_process_event(priv, event);
528 spin_lock_irq(&priv->driver_lock);
529 }
530 spin_unlock_irq(&priv->driver_lock);
531
532 if (priv->wakeup_dev_required) {
533 lbs_deb_thread("Waking up device...\n");
534 /* Wake up device */
535 if (priv->exit_deep_sleep(priv))
536 lbs_deb_thread("Wakeup device failed\n");
537 continue;
538 }
539
7919b89c 540 /* command timeout stuff */
2a345099
DW
541 if (priv->cmd_timed_out && priv->cur_cmd) {
542 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
543
40e6fa82
HS
544 lbs_pr_info("Timeout submitting command 0x%04x\n",
545 le16_to_cpu(cmdnode->cmdbuf->command));
546 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
547 if (priv->reset_card)
548 priv->reset_card(priv);
2a345099
DW
549 }
550 priv->cmd_timed_out = 0;
551
b15152a4
DW
552 if (!priv->fw_ready)
553 continue;
554
876c9d3a 555 /* Check if we need to confirm Sleep Request received previously */
aa21c004
DW
556 if (priv->psstate == PS_STATE_PRE_SLEEP &&
557 !priv->dnld_sent && !priv->cur_cmd) {
558 if (priv->connect_status == LBS_CONNECTED) {
7919b89c
HS
559 lbs_deb_thread("pre-sleep, currenttxskb %p, "
560 "dnld_sent %d, cur_cmd %p\n",
561 priv->currenttxskb, priv->dnld_sent,
562 priv->cur_cmd);
59f3e4bf 563
d4ff0ef6 564 lbs_ps_confirm_sleep(priv);
59f3e4bf
DW
565 } else {
566 /* workaround for firmware sending
567 * deauth/linkloss event immediately
568 * after sleep request; remove this
569 * after firmware fixes it
570 */
aa21c004 571 priv->psstate = PS_STATE_AWAKE;
7919b89c
HS
572 lbs_pr_alert("ignore PS_SleepConfirm in "
573 "non-connected state\n");
876c9d3a
MT
574 }
575 }
576
577 /* The PS state is changed during processing of Sleep Request
578 * event above
579 */
aa21c004
DW
580 if ((priv->psstate == PS_STATE_SLEEP) ||
581 (priv->psstate == PS_STATE_PRE_SLEEP))
876c9d3a
MT
582 continue;
583
49125454
AK
584 if (priv->is_deep_sleep)
585 continue;
586
876c9d3a 587 /* Execute the next command */
aa21c004 588 if (!priv->dnld_sent && !priv->cur_cmd)
10078321 589 lbs_execute_next_command(priv);
876c9d3a
MT
590
591 /* Wake-up command waiters which can't sleep in
10078321 592 * lbs_prepare_and_send_command
876c9d3a 593 */
aa21c004
DW
594 if (!list_empty(&priv->cmdpendingq))
595 wake_up_all(&priv->cmd_pending);
2eb188a1
DW
596
597 spin_lock_irq(&priv->driver_lock);
598 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
599 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
600 priv->tx_pending_buf,
601 priv->tx_pending_len);
602 if (ret) {
603 lbs_deb_tx("host_to_card failed %d\n", ret);
604 priv->dnld_sent = DNLD_RES_RECEIVED;
605 }
606 priv->tx_pending_len = 0;
607 if (!priv->currenttxskb) {
608 /* We can wake the queues immediately if we aren't
609 waiting for TX feedback */
610 if (priv->connect_status == LBS_CONNECTED)
611 netif_wake_queue(priv->dev);
612 if (priv->mesh_dev &&
602114ae 613 lbs_mesh_connected(priv))
2eb188a1
DW
614 netif_wake_queue(priv->mesh_dev);
615 }
616 }
617 spin_unlock_irq(&priv->driver_lock);
876c9d3a
MT
618 }
619
aa21c004 620 del_timer(&priv->command_timer);
49125454 621 del_timer(&priv->auto_deepsleep_timer);
aa21c004 622 wake_up_all(&priv->cmd_pending);
876c9d3a 623
9012b28a 624 lbs_deb_leave(LBS_DEB_THREAD);
876c9d3a
MT
625 return 0;
626}
627
ab25ecae
DW
628static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
629 struct cmd_header *cmd)
630{
61d30020 631 lbs_deb_enter(LBS_DEB_FW);
ab25ecae
DW
632
633 netif_device_detach(priv->dev);
634 if (priv->mesh_dev)
635 netif_device_detach(priv->mesh_dev);
636
637 priv->fw_ready = 0;
61d30020 638 lbs_deb_leave(LBS_DEB_FW);
ab25ecae
DW
639 return 0;
640}
641
ab25ecae
DW
642int lbs_suspend(struct lbs_private *priv)
643{
644 struct cmd_header cmd;
645 int ret;
646
61d30020
HS
647 lbs_deb_enter(LBS_DEB_FW);
648
506e9025
DW
649 if (priv->wol_criteria == 0xffffffff) {
650 lbs_pr_info("Suspend attempt without configuring wake params!\n");
651 return -EINVAL;
652 }
653
ab25ecae 654 memset(&cmd, 0, sizeof(cmd));
7e226272 655
ab25ecae
DW
656 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
657 sizeof(cmd), lbs_suspend_callback, 0);
658 if (ret)
659 lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
660
61d30020 661 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
ab25ecae
DW
662 return ret;
663}
664EXPORT_SYMBOL_GPL(lbs_suspend);
665
a63e5cb2 666void lbs_resume(struct lbs_private *priv)
ab25ecae 667{
61d30020
HS
668 lbs_deb_enter(LBS_DEB_FW);
669
ab25ecae
DW
670 priv->fw_ready = 1;
671
672 /* Firmware doesn't seem to give us RX packets any more
673 until we send it some command. Might as well update */
674 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
675 0, 0, NULL);
676
677 netif_device_attach(priv->dev);
678 if (priv->mesh_dev)
679 netif_device_attach(priv->mesh_dev);
680
61d30020 681 lbs_deb_leave(LBS_DEB_FW);
ab25ecae
DW
682}
683EXPORT_SYMBOL_GPL(lbs_resume);
684
1df4e8fe 685/**
b4836599
CM
686 * @brief This function gets the HW spec from the firmware and sets
687 * some basic parameters.
1df4e8fe 688 *
69f9032d 689 * @param priv A pointer to struct lbs_private structure
1df4e8fe
HS
690 * @return 0 or -1
691 */
69f9032d 692static int lbs_setup_firmware(struct lbs_private *priv)
1df4e8fe
HS
693{
694 int ret = -1;
87c8c72d 695 s16 curlevel = 0, minlevel = 0, maxlevel = 0;
1df4e8fe
HS
696
697 lbs_deb_enter(LBS_DEB_FW);
698
87c8c72d 699 /* Read MAC address from firmware */
aa21c004 700 memset(priv->current_addr, 0xff, ETH_ALEN);
6e66f03f 701 ret = lbs_update_hw_spec(priv);
ef85ad54 702 if (ret)
1df4e8fe 703 goto done;
1df4e8fe 704
87c8c72d
DW
705 /* Read power levels if available */
706 ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
707 if (ret == 0) {
708 priv->txpower_cur = curlevel;
709 priv->txpower_min = minlevel;
710 priv->txpower_max = maxlevel;
711 }
712
d9e9778c 713 lbs_set_mac_control(priv);
1df4e8fe
HS
714done:
715 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
716 return ret;
717}
718
1df4e8fe
HS
719/**
720 * This function handles the timeout of command sending.
721 * It will re-send the same command again.
722 */
40e6fa82 723static void lbs_cmd_timeout_handler(unsigned long data)
1df4e8fe 724{
69f9032d 725 struct lbs_private *priv = (struct lbs_private *)data;
1df4e8fe
HS
726 unsigned long flags;
727
61d30020 728 lbs_deb_enter(LBS_DEB_CMD);
2a345099 729 spin_lock_irqsave(&priv->driver_lock, flags);
1df4e8fe 730
57962f0b 731 if (!priv->cur_cmd)
2a345099 732 goto out;
1df4e8fe 733
57962f0b
HS
734 lbs_pr_info("command 0x%04x timed out\n",
735 le16_to_cpu(priv->cur_cmd->cmdbuf->command));
1df4e8fe 736
2a345099 737 priv->cmd_timed_out = 1;
1df4e8fe 738 wake_up_interruptible(&priv->waitq);
61d30020 739out:
2a345099 740 spin_unlock_irqrestore(&priv->driver_lock, flags);
61d30020 741 lbs_deb_leave(LBS_DEB_CMD);
1df4e8fe
HS
742}
743
49125454
AK
744/**
745 * This function put the device back to deep sleep mode when timer expires
746 * and no activity (command, event, data etc.) is detected.
747 */
748static void auto_deepsleep_timer_fn(unsigned long data)
749{
750 struct lbs_private *priv = (struct lbs_private *)data;
751 int ret;
752
753 lbs_deb_enter(LBS_DEB_CMD);
754
755 if (priv->is_activity_detected) {
756 priv->is_activity_detected = 0;
757 } else {
758 if (priv->is_auto_deep_sleep_enabled &&
759 (!priv->wakeup_dev_required) &&
760 (priv->connect_status != LBS_CONNECTED)) {
761 lbs_deb_main("Entering auto deep sleep mode...\n");
762 ret = lbs_prepare_and_send_command(priv,
763 CMD_802_11_DEEP_SLEEP, 0,
764 0, 0, NULL);
c0bbd576
AK
765 if (ret)
766 lbs_pr_err("Enter Deep Sleep command failed\n");
49125454
AK
767 }
768 }
769 mod_timer(&priv->auto_deepsleep_timer , jiffies +
770 (priv->auto_deep_sleep_timeout * HZ)/1000);
771 lbs_deb_leave(LBS_DEB_CMD);
772}
773
774int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
775{
776 lbs_deb_enter(LBS_DEB_SDIO);
777
778 priv->is_auto_deep_sleep_enabled = 1;
779 if (priv->is_deep_sleep)
780 priv->wakeup_dev_required = 1;
781 mod_timer(&priv->auto_deepsleep_timer ,
782 jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
783
784 lbs_deb_leave(LBS_DEB_SDIO);
785 return 0;
786}
787
788int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
789{
790 lbs_deb_enter(LBS_DEB_SDIO);
791
792 priv->is_auto_deep_sleep_enabled = 0;
793 priv->auto_deep_sleep_timeout = 0;
794 del_timer(&priv->auto_deepsleep_timer);
795
796 lbs_deb_leave(LBS_DEB_SDIO);
797 return 0;
798}
799
69f9032d 800static int lbs_init_adapter(struct lbs_private *priv)
ac558ca2 801{
1df4e8fe 802 size_t bufsize;
954ee164 803 int i, ret = 0;
1df4e8fe 804
61d30020
HS
805 lbs_deb_enter(LBS_DEB_MAIN);
806
1df4e8fe
HS
807 /* Allocate buffer to store the BSSID list */
808 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
aa21c004
DW
809 priv->networks = kzalloc(bufsize, GFP_KERNEL);
810 if (!priv->networks) {
1df4e8fe 811 lbs_pr_err("Out of memory allocating beacons\n");
954ee164
DW
812 ret = -1;
813 goto out;
1df4e8fe
HS
814 }
815
954ee164 816 /* Initialize scan result lists */
aa21c004
DW
817 INIT_LIST_HEAD(&priv->network_free_list);
818 INIT_LIST_HEAD(&priv->network_list);
954ee164 819 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
aa21c004
DW
820 list_add_tail(&priv->networks[i].list,
821 &priv->network_free_list);
954ee164 822 }
1df4e8fe 823
aa21c004 824 memset(priv->current_addr, 0xff, ETH_ALEN);
1df4e8fe 825
aa21c004 826 priv->connect_status = LBS_DISCONNECTED;
aa21c004
DW
827 priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
828 priv->mode = IW_MODE_INFRA;
c14951fe 829 priv->channel = DEFAULT_AD_HOC_CHANNEL;
d9e9778c 830 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
d5db2dfa 831 priv->radio_on = 1;
85319f93 832 priv->enablehwauto = 1;
aa21c004
DW
833 priv->psmode = LBS802_11POWERMODECAM;
834 priv->psstate = PS_STATE_FULL_POWER;
49125454
AK
835 priv->is_deep_sleep = 0;
836 priv->is_auto_deep_sleep_enabled = 0;
837 priv->wakeup_dev_required = 0;
838 init_waitqueue_head(&priv->ds_awake_q);
1df4e8fe 839
aa21c004 840 mutex_init(&priv->lock);
1df4e8fe 841
40e6fa82 842 setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
61d30020 843 (unsigned long)priv);
49125454
AK
844 setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
845 (unsigned long)priv);
1df4e8fe 846
aa21c004
DW
847 INIT_LIST_HEAD(&priv->cmdfreeq);
848 INIT_LIST_HEAD(&priv->cmdpendingq);
1df4e8fe 849
aa21c004
DW
850 spin_lock_init(&priv->driver_lock);
851 init_waitqueue_head(&priv->cmd_pending);
1df4e8fe 852
954ee164 853 /* Allocate the command buffers */
10078321 854 if (lbs_allocate_cmd_buffer(priv)) {
954ee164 855 lbs_pr_err("Out of memory allocating command buffers\n");
7919b89c
HS
856 ret = -ENOMEM;
857 goto out;
858 }
859 priv->resp_idx = 0;
860 priv->resp_len[0] = priv->resp_len[1] = 0;
861
862 /* Create the event FIFO */
c1e13f25 863 ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
45465487 864 if (ret) {
7919b89c 865 lbs_pr_err("Out of memory allocating event FIFO buffer\n");
7919b89c 866 goto out;
954ee164 867 }
1df4e8fe 868
954ee164 869out:
61d30020
HS
870 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
871
954ee164
DW
872 return ret;
873}
1df4e8fe 874
69f9032d 875static void lbs_free_adapter(struct lbs_private *priv)
954ee164 876{
61d30020 877 lbs_deb_enter(LBS_DEB_MAIN);
1df4e8fe 878
61d30020 879 lbs_free_cmd_buffer(priv);
45465487 880 kfifo_free(&priv->event_fifo);
aa21c004 881 del_timer(&priv->command_timer);
49125454 882 del_timer(&priv->auto_deepsleep_timer);
aa21c004
DW
883 kfree(priv->networks);
884 priv->networks = NULL;
61d30020
HS
885
886 lbs_deb_leave(LBS_DEB_MAIN);
1df4e8fe
HS
887}
888
f02abf10
SH
889static const struct net_device_ops lbs_netdev_ops = {
890 .ndo_open = lbs_dev_open,
891 .ndo_stop = lbs_eth_stop,
892 .ndo_start_xmit = lbs_hard_start_xmit,
893 .ndo_set_mac_address = lbs_set_mac_address,
894 .ndo_tx_timeout = lbs_tx_timeout,
895 .ndo_set_multicast_list = lbs_set_multicast_list,
896 .ndo_change_mtu = eth_change_mtu,
897 .ndo_validate_addr = eth_validate_addr,
898};
899
876c9d3a
MT
900/**
901 * @brief This function adds the card. it will probe the
10078321 902 * card, allocate the lbs_priv and initialize the device.
876c9d3a
MT
903 *
904 * @param card A pointer to card
69f9032d 905 * @return A pointer to struct lbs_private structure
876c9d3a 906 */
69f9032d 907struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
876c9d3a 908{
ff9fc791
HS
909 struct net_device *dev;
910 struct wireless_dev *wdev;
69f9032d 911 struct lbs_private *priv = NULL;
876c9d3a 912
61d30020 913 lbs_deb_enter(LBS_DEB_MAIN);
876c9d3a
MT
914
915 /* Allocate an Ethernet device and register it */
ff9fc791
HS
916 wdev = lbs_cfg_alloc(dmdev);
917 if (IS_ERR(wdev)) {
918 lbs_pr_err("cfg80211 init failed\n");
954ee164 919 goto done;
876c9d3a 920 }
ff9fc791
HS
921 /* TODO? */
922 wdev->iftype = NL80211_IFTYPE_STATION;
923 priv = wdev_priv(wdev);
924 priv->wdev = wdev;
876c9d3a 925
10078321 926 if (lbs_init_adapter(priv)) {
954ee164 927 lbs_pr_err("failed to initialize adapter structure.\n");
ff9fc791
HS
928 goto err_wdev;
929 }
930
931 //TODO? dev = alloc_netdev_mq(0, "wlan%d", ether_setup, IWM_TX_QUEUES);
932 dev = alloc_netdev(0, "wlan%d", ether_setup);
933 if (!dev) {
934 dev_err(dmdev, "no memory for network device instance\n");
935 goto err_adapter;
954ee164
DW
936 }
937
ff9fc791
HS
938 dev->ieee80211_ptr = wdev;
939 dev->ml_priv = priv;
940 SET_NETDEV_DEV(dev, dmdev);
941 wdev->netdev = dev;
634b8f49 942 priv->dev = dev;
876c9d3a 943
f02abf10 944 dev->netdev_ops = &lbs_netdev_ops;
fb3dddf2 945 dev->watchdog_timeo = 5 * HZ;
10078321 946 dev->ethtool_ops = &lbs_ethtool_ops;
876c9d3a 947#ifdef WIRELESS_EXT
f02abf10 948 dev->wireless_handlers = &lbs_handler_def;
876c9d3a 949#endif
876c9d3a 950 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
876c9d3a 951
ff9fc791
HS
952
953 // TODO: kzalloc + iwm_init_default_profile(iwm, iwm->umac_profile); ??
954
955
956 priv->card = card;
ff9fc791
HS
957 priv->infra_open = 0;
958
7732ca45 959
965f8bbc 960 priv->rtap_net_dev = NULL;
c00552c6 961 strcpy(dev->name, "wlan%d");
954ee164
DW
962
963 lbs_deb_thread("Starting main thread...\n");
964 init_waitqueue_head(&priv->waitq);
10078321 965 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
954ee164
DW
966 if (IS_ERR(priv->main_thread)) {
967 lbs_deb_thread("Error creating main thread.\n");
ff9fc791 968 goto err_ndev;
954ee164
DW
969 }
970
10078321
HS
971 priv->work_thread = create_singlethread_workqueue("lbs_worker");
972 INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
973 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
75bf45a7 974 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
23a397ac 975
506e9025
DW
976 priv->wol_criteria = 0xffffffff;
977 priv->wol_gpio = 0xff;
978
32a74b7c
HS
979 goto done;
980
ff9fc791 981 err_ndev:
32a74b7c 982 free_netdev(dev);
ff9fc791
HS
983
984 err_adapter:
985 lbs_free_adapter(priv);
986
987 err_wdev:
988 lbs_cfg_free(priv);
989
6a812157 990 priv = NULL;
954ee164 991
32a74b7c 992done:
61d30020 993 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
32a74b7c
HS
994 return priv;
995}
10078321 996EXPORT_SYMBOL_GPL(lbs_add_card);
32a74b7c 997
954ee164 998
a63e5cb2 999void lbs_remove_card(struct lbs_private *priv)
32a74b7c 1000{
634b8f49 1001 struct net_device *dev = priv->dev;
32a74b7c
HS
1002
1003 lbs_deb_enter(LBS_DEB_MAIN);
876c9d3a 1004
23a397ac 1005 lbs_remove_mesh(priv);
10078321 1006 lbs_remove_rtap(priv);
876c9d3a 1007
954ee164 1008 dev = priv->dev;
2afc0c5d 1009
652e3f20
HS
1010 cancel_delayed_work_sync(&priv->scan_work);
1011 cancel_delayed_work_sync(&priv->assoc_work);
75bf45a7 1012 cancel_work_sync(&priv->mcast_work);
71b35f3a
DW
1013
1014 /* worker thread destruction blocks on the in-flight command which
1015 * should have been cleared already in lbs_stop_card().
1016 */
1017 lbs_deb_main("destroying worker thread\n");
954ee164 1018 destroy_workqueue(priv->work_thread);
71b35f3a 1019 lbs_deb_main("done destroying worker thread\n");
876c9d3a 1020
aa21c004
DW
1021 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1022 priv->psmode = LBS802_11POWERMODECAM;
10078321 1023 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
876c9d3a
MT
1024 }
1025
fea2b8ec 1026 lbs_send_disconnect_notification(priv);
954ee164 1027
49125454
AK
1028 if (priv->is_deep_sleep) {
1029 priv->is_deep_sleep = 0;
1030 wake_up_interruptible(&priv->ds_awake_q);
1031 }
1032
954ee164 1033 /* Stop the thread servicing the interrupts */
aa21c004 1034 priv->surpriseremoved = 1;
954ee164
DW
1035 kthread_stop(priv->main_thread);
1036
10078321 1037 lbs_free_adapter(priv);
ff9fc791 1038 lbs_cfg_free(priv);
954ee164
DW
1039
1040 priv->dev = NULL;
1041 free_netdev(dev);
1042
1043 lbs_deb_leave(LBS_DEB_MAIN);
954ee164 1044}
10078321 1045EXPORT_SYMBOL_GPL(lbs_remove_card);
954ee164
DW
1046
1047
cd74468b
HS
1048static int lbs_rtap_supported(struct lbs_private *priv)
1049{
1050 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
1051 return 1;
1052
1053 /* newer firmware use a capability mask */
1054 return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
1055 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
1056}
1057
1058
69f9032d 1059int lbs_start_card(struct lbs_private *priv)
954ee164
DW
1060{
1061 struct net_device *dev = priv->dev;
1062 int ret = -1;
1063
1064 lbs_deb_enter(LBS_DEB_MAIN);
1065
1066 /* poke the firmware */
10078321 1067 ret = lbs_setup_firmware(priv);
954ee164
DW
1068 if (ret)
1069 goto done;
1070
ff9fc791
HS
1071 if (lbs_cfg_register(priv)) {
1072 lbs_pr_err("cannot register device\n");
954ee164 1073 goto done;
876c9d3a 1074 }
020f3d00 1075
020f3d00 1076 lbs_update_channel(priv);
020f3d00 1077
cd74468b
HS
1078 lbs_init_mesh(priv);
1079
e0e42da3
HS
1080 /*
1081 * While rtap isn't related to mesh, only mesh-enabled
1082 * firmware implements the rtap functionality via
1083 * CMD_802_11_MONITOR_MODE.
684d6b36 1084 */
cd74468b 1085 if (lbs_rtap_supported(priv)) {
684d6b36
BZ
1086 if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1087 lbs_pr_err("cannot register lbs_rtap attribute\n");
020f3d00 1088 }
876c9d3a 1089
10078321 1090 lbs_debugfs_init_one(priv, dev);
876c9d3a 1091
954ee164
DW
1092 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1093
32a74b7c 1094 ret = 0;
876c9d3a 1095
32a74b7c 1096done:
954ee164
DW
1097 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1098 return ret;
1099}
10078321 1100EXPORT_SYMBOL_GPL(lbs_start_card);
954ee164
DW
1101
1102
a63e5cb2 1103void lbs_stop_card(struct lbs_private *priv)
954ee164 1104{
43baa5bb 1105 struct net_device *dev;
954ee164
DW
1106 struct cmd_ctrl_node *cmdnode;
1107 unsigned long flags;
1108
1109 lbs_deb_enter(LBS_DEB_MAIN);
1110
652e3f20
HS
1111 if (!priv)
1112 goto out;
43baa5bb 1113 dev = priv->dev;
652e3f20 1114
43baa5bb
JL
1115 netif_stop_queue(dev);
1116 netif_carrier_off(dev);
954ee164 1117
10078321 1118 lbs_debugfs_remove_one(priv);
cd74468b
HS
1119 lbs_deinit_mesh(priv);
1120
1121 if (lbs_rtap_supported(priv))
3b72b01d 1122 device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
954ee164 1123
71b35f3a 1124 /* Delete the timeout of the currently processing command */
652e3f20 1125 del_timer_sync(&priv->command_timer);
49125454 1126 del_timer_sync(&priv->auto_deepsleep_timer);
71b35f3a
DW
1127
1128 /* Flush pending command nodes */
aa21c004 1129 spin_lock_irqsave(&priv->driver_lock, flags);
71b35f3a 1130 lbs_deb_main("clearing pending commands\n");
aa21c004 1131 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
ae125bf8 1132 cmdnode->result = -ENOENT;
954ee164
DW
1133 cmdnode->cmdwaitqwoken = 1;
1134 wake_up_interruptible(&cmdnode->cmdwait_q);
1135 }
71b35f3a
DW
1136
1137 /* Flush the command the card is currently processing */
1138 if (priv->cur_cmd) {
1139 lbs_deb_main("clearing current command\n");
1140 priv->cur_cmd->result = -ENOENT;
1141 priv->cur_cmd->cmdwaitqwoken = 1;
1142 wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
1143 }
1144 lbs_deb_main("done clearing commands\n");
aa21c004 1145 spin_unlock_irqrestore(&priv->driver_lock, flags);
954ee164
DW
1146
1147 unregister_netdev(dev);
1148
652e3f20 1149out:
a63e5cb2 1150 lbs_deb_leave(LBS_DEB_MAIN);
876c9d3a 1151}
10078321 1152EXPORT_SYMBOL_GPL(lbs_stop_card);
084708b6 1153
876c9d3a 1154
7919b89c
HS
1155void lbs_queue_event(struct lbs_private *priv, u32 event)
1156{
1157 unsigned long flags;
1158
1159 lbs_deb_enter(LBS_DEB_THREAD);
1160 spin_lock_irqsave(&priv->driver_lock, flags);
1161
1162 if (priv->psstate == PS_STATE_SLEEP)
1163 priv->psstate = PS_STATE_AWAKE;
1164
7acd72eb 1165 kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
7919b89c
HS
1166
1167 wake_up_interruptible(&priv->waitq);
1168
1169 spin_unlock_irqrestore(&priv->driver_lock, flags);
1170 lbs_deb_leave(LBS_DEB_THREAD);
1171}
1172EXPORT_SYMBOL_GPL(lbs_queue_event);
1173
1174void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
876c9d3a 1175{
ed37b516 1176 lbs_deb_enter(LBS_DEB_THREAD);
876c9d3a 1177
4f679496 1178 if (priv->psstate == PS_STATE_SLEEP)
aa21c004 1179 priv->psstate = PS_STATE_AWAKE;
7919b89c
HS
1180
1181 /* Swap buffers by flipping the response index */
1182 BUG_ON(resp_idx > 1);
1183 priv->resp_idx = resp_idx;
1184
fe336150 1185 wake_up_interruptible(&priv->waitq);
876c9d3a 1186
ed37b516 1187 lbs_deb_leave(LBS_DEB_THREAD);
876c9d3a 1188}
7919b89c 1189EXPORT_SYMBOL_GPL(lbs_notify_command_response);
876c9d3a 1190
4fb910fd 1191static int __init lbs_init_module(void)
876c9d3a 1192{
9012b28a 1193 lbs_deb_enter(LBS_DEB_MAIN);
f539f2ef
HS
1194 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1195 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1196 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1197 confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
10078321 1198 lbs_debugfs_init();
084708b6
HS
1199 lbs_deb_leave(LBS_DEB_MAIN);
1200 return 0;
876c9d3a
MT
1201}
1202
4fb910fd 1203static void __exit lbs_exit_module(void)
876c9d3a 1204{
9012b28a 1205 lbs_deb_enter(LBS_DEB_MAIN);
10078321 1206 lbs_debugfs_remove();
9012b28a 1207 lbs_deb_leave(LBS_DEB_MAIN);
876c9d3a
MT
1208}
1209
965f8bbc
LCC
1210/*
1211 * rtap interface support fuctions
1212 */
1213
10078321 1214static int lbs_rtap_open(struct net_device *dev)
965f8bbc 1215{
8552855f 1216 /* Yes, _stop_ the queue. Because we don't support injection */
61d30020
HS
1217 lbs_deb_enter(LBS_DEB_MAIN);
1218 netif_carrier_off(dev);
1219 netif_stop_queue(dev);
1220 lbs_deb_leave(LBS_DEB_LEAVE);
1221 return 0;
965f8bbc
LCC
1222}
1223
10078321 1224static int lbs_rtap_stop(struct net_device *dev)
965f8bbc 1225{
61d30020
HS
1226 lbs_deb_enter(LBS_DEB_MAIN);
1227 lbs_deb_leave(LBS_DEB_MAIN);
1228 return 0;
965f8bbc
LCC
1229}
1230
d0cf9c0d
SH
1231static netdev_tx_t lbs_rtap_hard_start_xmit(struct sk_buff *skb,
1232 struct net_device *dev)
965f8bbc 1233{
61d30020
HS
1234 netif_stop_queue(dev);
1235 return NETDEV_TX_BUSY;
965f8bbc
LCC
1236}
1237
2fd6cfe3 1238static void lbs_remove_rtap(struct lbs_private *priv)
965f8bbc 1239{
61d30020 1240 lbs_deb_enter(LBS_DEB_MAIN);
965f8bbc 1241 if (priv->rtap_net_dev == NULL)
5f505d90 1242 goto out;
965f8bbc 1243 unregister_netdev(priv->rtap_net_dev);
d9268fb9 1244 free_netdev(priv->rtap_net_dev);
965f8bbc 1245 priv->rtap_net_dev = NULL;
5f505d90 1246out:
61d30020 1247 lbs_deb_leave(LBS_DEB_MAIN);
965f8bbc
LCC
1248}
1249
f02abf10
SH
1250static const struct net_device_ops rtap_netdev_ops = {
1251 .ndo_open = lbs_rtap_open,
1252 .ndo_stop = lbs_rtap_stop,
1253 .ndo_start_xmit = lbs_rtap_hard_start_xmit,
1254};
1255
2fd6cfe3 1256static int lbs_add_rtap(struct lbs_private *priv)
965f8bbc 1257{
61d30020 1258 int ret = 0;
d9268fb9 1259 struct net_device *rtap_dev;
965f8bbc 1260
61d30020
HS
1261 lbs_deb_enter(LBS_DEB_MAIN);
1262 if (priv->rtap_net_dev) {
1263 ret = -EPERM;
1264 goto out;
1265 }
965f8bbc 1266
d9268fb9 1267 rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
61d30020
HS
1268 if (rtap_dev == NULL) {
1269 ret = -ENOMEM;
1270 goto out;
1271 }
965f8bbc 1272
121947c6 1273 memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
d9268fb9 1274 rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
f02abf10 1275 rtap_dev->netdev_ops = &rtap_netdev_ops;
4ceb7b6a 1276 rtap_dev->ml_priv = priv;
229ce3ab 1277 SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
965f8bbc 1278
61d30020
HS
1279 ret = register_netdev(rtap_dev);
1280 if (ret) {
d9268fb9 1281 free_netdev(rtap_dev);
61d30020 1282 goto out;
965f8bbc 1283 }
d9268fb9 1284 priv->rtap_net_dev = rtap_dev;
965f8bbc 1285
61d30020
HS
1286out:
1287 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1288 return ret;
965f8bbc
LCC
1289}
1290
10078321
HS
1291module_init(lbs_init_module);
1292module_exit(lbs_exit_module);
876c9d3a 1293
084708b6 1294MODULE_DESCRIPTION("Libertas WLAN Driver Library");
876c9d3a
MT
1295MODULE_AUTHOR("Marvell International Ltd.");
1296MODULE_LICENSE("GPL");