sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / net / wireless / libertas / main.c
blob8c40949cb0769edef3620cc8ecaa82ce86d8d6d9
1 /*
2 * This file contains the major functions in WLAN
3 * driver. It includes init, exit, open, close and main
4 * thread etc..
5 */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/moduleparam.h>
10 #include <linux/delay.h>
11 #include <linux/etherdevice.h>
12 #include <linux/netdevice.h>
13 #include <linux/if_arp.h>
14 #include <linux/kthread.h>
15 #include <linux/kfifo.h>
16 #include <linux/slab.h>
17 #include <net/cfg80211.h>
19 #include "host.h"
20 #include "decl.h"
21 #include "dev.h"
22 #include "cfg.h"
23 #include "debugfs.h"
24 #include "cmd.h"
26 #define DRIVER_RELEASE_VERSION "323.p0"
27 const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
28 #ifdef DEBUG
29 "-dbg"
30 #endif
31 "";
34 /* Module parameters */
35 unsigned int lbs_debug;
36 EXPORT_SYMBOL_GPL(lbs_debug);
37 module_param_named(libertas_debug, lbs_debug, int, 0644);
39 unsigned int lbs_disablemesh;
40 EXPORT_SYMBOL_GPL(lbs_disablemesh);
41 module_param_named(libertas_disablemesh, lbs_disablemesh, int, 0644);
45 * This global structure is used to send the confirm_sleep command as
46 * fast as possible down to the firmware.
48 struct cmd_confirm_sleep confirm_sleep;
52 * the table to keep region code
54 u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
55 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
58 * FW rate table. FW refers to rates by their index in this table, not by the
59 * rate value itself. Values of 0x00 are
60 * reserved positions.
62 static u8 fw_data_rates[MAX_RATES] =
63 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
64 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
67 /**
68 * lbs_fw_index_to_data_rate - use index to get the data rate
70 * @idx: The index of data rate
71 * returns: data rate or 0
73 u32 lbs_fw_index_to_data_rate(u8 idx)
75 if (idx >= sizeof(fw_data_rates))
76 idx = 0;
77 return fw_data_rates[idx];
80 /**
81 * lbs_data_rate_to_fw_index - use rate to get the index
83 * @rate: data rate
84 * returns: index or 0
86 u8 lbs_data_rate_to_fw_index(u32 rate)
88 u8 i;
90 if (!rate)
91 return 0;
93 for (i = 0; i < sizeof(fw_data_rates); i++) {
94 if (rate == fw_data_rates[i])
95 return i;
97 return 0;
102 * lbs_dev_open - open the ethX interface
104 * @dev: A pointer to &net_device structure
105 * returns: 0 or -EBUSY if monitor mode active
107 static int lbs_dev_open(struct net_device *dev)
109 struct lbs_private *priv = dev->ml_priv;
110 int ret = 0;
112 lbs_deb_enter(LBS_DEB_NET);
114 spin_lock_irq(&priv->driver_lock);
115 priv->stopping = false;
117 if (priv->connect_status == LBS_CONNECTED)
118 netif_carrier_on(dev);
119 else
120 netif_carrier_off(dev);
122 if (!priv->tx_pending_len)
123 netif_wake_queue(dev);
125 spin_unlock_irq(&priv->driver_lock);
126 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
127 return ret;
131 * lbs_eth_stop - close the ethX interface
133 * @dev: A pointer to &net_device structure
134 * returns: 0
136 static int lbs_eth_stop(struct net_device *dev)
138 struct lbs_private *priv = dev->ml_priv;
140 lbs_deb_enter(LBS_DEB_NET);
142 spin_lock_irq(&priv->driver_lock);
143 priv->stopping = true;
144 netif_stop_queue(dev);
145 spin_unlock_irq(&priv->driver_lock);
147 schedule_work(&priv->mcast_work);
148 cancel_delayed_work_sync(&priv->scan_work);
149 if (priv->scan_req) {
150 cfg80211_scan_done(priv->scan_req, false);
151 priv->scan_req = NULL;
154 lbs_deb_leave(LBS_DEB_NET);
155 return 0;
158 void lbs_host_to_card_done(struct lbs_private *priv)
160 unsigned long flags;
162 lbs_deb_enter(LBS_DEB_THREAD);
164 spin_lock_irqsave(&priv->driver_lock, flags);
166 priv->dnld_sent = DNLD_RES_RECEIVED;
168 /* Wake main thread if commands are pending */
169 if (!priv->cur_cmd || priv->tx_pending_len > 0) {
170 if (!priv->wakeup_dev_required)
171 wake_up_interruptible(&priv->waitq);
174 spin_unlock_irqrestore(&priv->driver_lock, flags);
175 lbs_deb_leave(LBS_DEB_THREAD);
177 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
179 int lbs_set_mac_address(struct net_device *dev, void *addr)
181 int ret = 0;
182 struct lbs_private *priv = dev->ml_priv;
183 struct sockaddr *phwaddr = addr;
184 struct cmd_ds_802_11_mac_address cmd;
186 lbs_deb_enter(LBS_DEB_NET);
188 /* In case it was called from the mesh device */
189 dev = priv->dev;
191 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
192 cmd.action = cpu_to_le16(CMD_ACT_SET);
193 memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
195 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
196 if (ret) {
197 lbs_deb_net("set MAC address failed\n");
198 goto done;
201 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
202 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
203 if (priv->mesh_dev)
204 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
206 done:
207 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
208 return ret;
212 static inline int mac_in_list(unsigned char *list, int list_len,
213 unsigned char *mac)
215 while (list_len) {
216 if (!memcmp(list, mac, ETH_ALEN))
217 return 1;
218 list += ETH_ALEN;
219 list_len--;
221 return 0;
225 static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
226 struct net_device *dev, int nr_addrs)
228 int i = nr_addrs;
229 struct netdev_hw_addr *ha;
230 int cnt;
232 if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
233 return nr_addrs;
235 netif_addr_lock_bh(dev);
236 cnt = netdev_mc_count(dev);
237 netdev_for_each_mc_addr(ha, dev) {
238 if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
239 lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
240 ha->addr);
241 cnt--;
242 continue;
245 if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
246 break;
247 memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
248 lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
249 ha->addr);
250 i++;
251 cnt--;
253 netif_addr_unlock_bh(dev);
254 if (cnt)
255 return -EOVERFLOW;
257 return i;
260 static void lbs_set_mcast_worker(struct work_struct *work)
262 struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
263 struct cmd_ds_mac_multicast_adr mcast_cmd;
264 int dev_flags;
265 int nr_addrs;
266 int old_mac_control = priv->mac_control;
268 lbs_deb_enter(LBS_DEB_NET);
270 dev_flags = priv->dev->flags;
271 if (priv->mesh_dev)
272 dev_flags |= priv->mesh_dev->flags;
274 if (dev_flags & IFF_PROMISC) {
275 priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
276 priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
277 CMD_ACT_MAC_MULTICAST_ENABLE);
278 goto out_set_mac_control;
279 } else if (dev_flags & IFF_ALLMULTI) {
280 do_allmulti:
281 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
282 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
283 CMD_ACT_MAC_MULTICAST_ENABLE);
284 goto out_set_mac_control;
287 /* Once for priv->dev, again for priv->mesh_dev if it exists */
288 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
289 if (nr_addrs >= 0 && priv->mesh_dev)
290 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
291 if (nr_addrs < 0)
292 goto do_allmulti;
294 if (nr_addrs) {
295 int size = offsetof(struct cmd_ds_mac_multicast_adr,
296 maclist[6*nr_addrs]);
298 mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
299 mcast_cmd.hdr.size = cpu_to_le16(size);
300 mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
302 lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
304 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
305 } else
306 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
308 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
309 CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
310 out_set_mac_control:
311 if (priv->mac_control != old_mac_control)
312 lbs_set_mac_control(priv);
314 lbs_deb_leave(LBS_DEB_NET);
317 void lbs_set_multicast_list(struct net_device *dev)
319 struct lbs_private *priv = dev->ml_priv;
321 schedule_work(&priv->mcast_work);
325 * lbs_thread - handles the major jobs in the LBS driver.
326 * It handles all events generated by firmware, RX data received
327 * from firmware and TX data sent from kernel.
329 * @data: A pointer to &lbs_thread structure
330 * returns: 0
332 static int lbs_thread(void *data)
334 struct net_device *dev = data;
335 struct lbs_private *priv = dev->ml_priv;
336 wait_queue_t wait;
338 lbs_deb_enter(LBS_DEB_THREAD);
340 init_waitqueue_entry(&wait, current);
342 for (;;) {
343 int shouldsleep;
344 u8 resp_idx;
346 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
347 priv->currenttxskb, priv->dnld_sent);
349 add_wait_queue(&priv->waitq, &wait);
350 set_current_state(TASK_INTERRUPTIBLE);
351 spin_lock_irq(&priv->driver_lock);
353 if (kthread_should_stop())
354 shouldsleep = 0; /* Bye */
355 else if (priv->surpriseremoved)
356 shouldsleep = 1; /* We need to wait until we're _told_ to die */
357 else if (priv->psstate == PS_STATE_SLEEP)
358 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
359 else if (priv->cmd_timed_out)
360 shouldsleep = 0; /* Command timed out. Recover */
361 else if (!priv->fw_ready)
362 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
363 else if (priv->dnld_sent)
364 shouldsleep = 1; /* Something is en route to the device already */
365 else if (priv->tx_pending_len > 0)
366 shouldsleep = 0; /* We've a packet to send */
367 else if (priv->resp_len[priv->resp_idx])
368 shouldsleep = 0; /* We have a command response */
369 else if (priv->cur_cmd)
370 shouldsleep = 1; /* Can't send a command; one already running */
371 else if (!list_empty(&priv->cmdpendingq) &&
372 !(priv->wakeup_dev_required))
373 shouldsleep = 0; /* We have a command to send */
374 else if (kfifo_len(&priv->event_fifo))
375 shouldsleep = 0; /* We have an event to process */
376 else
377 shouldsleep = 1; /* No command */
379 if (shouldsleep) {
380 lbs_deb_thread("sleeping, connect_status %d, "
381 "psmode %d, psstate %d\n",
382 priv->connect_status,
383 priv->psmode, priv->psstate);
384 spin_unlock_irq(&priv->driver_lock);
385 schedule();
386 } else
387 spin_unlock_irq(&priv->driver_lock);
389 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
390 priv->currenttxskb, priv->dnld_sent);
392 set_current_state(TASK_RUNNING);
393 remove_wait_queue(&priv->waitq, &wait);
395 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
396 priv->currenttxskb, priv->dnld_sent);
398 if (kthread_should_stop()) {
399 lbs_deb_thread("break from main thread\n");
400 break;
403 if (priv->surpriseremoved) {
404 lbs_deb_thread("adapter removed; waiting to die...\n");
405 continue;
408 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
409 priv->currenttxskb, priv->dnld_sent);
411 /* Process any pending command response */
412 spin_lock_irq(&priv->driver_lock);
413 resp_idx = priv->resp_idx;
414 if (priv->resp_len[resp_idx]) {
415 spin_unlock_irq(&priv->driver_lock);
416 lbs_process_command_response(priv,
417 priv->resp_buf[resp_idx],
418 priv->resp_len[resp_idx]);
419 spin_lock_irq(&priv->driver_lock);
420 priv->resp_len[resp_idx] = 0;
422 spin_unlock_irq(&priv->driver_lock);
424 /* Process hardware events, e.g. card removed, link lost */
425 spin_lock_irq(&priv->driver_lock);
426 while (kfifo_len(&priv->event_fifo)) {
427 u32 event;
429 if (kfifo_out(&priv->event_fifo,
430 (unsigned char *) &event, sizeof(event)) !=
431 sizeof(event))
432 break;
433 spin_unlock_irq(&priv->driver_lock);
434 lbs_process_event(priv, event);
435 spin_lock_irq(&priv->driver_lock);
437 spin_unlock_irq(&priv->driver_lock);
439 if (priv->wakeup_dev_required) {
440 lbs_deb_thread("Waking up device...\n");
441 /* Wake up device */
442 if (priv->exit_deep_sleep(priv))
443 lbs_deb_thread("Wakeup device failed\n");
444 continue;
447 /* command timeout stuff */
448 if (priv->cmd_timed_out && priv->cur_cmd) {
449 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
451 netdev_info(dev, "Timeout submitting command 0x%04x\n",
452 le16_to_cpu(cmdnode->cmdbuf->command));
453 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
454 if (priv->reset_card)
455 priv->reset_card(priv);
457 priv->cmd_timed_out = 0;
459 if (!priv->fw_ready)
460 continue;
462 /* Check if we need to confirm Sleep Request received previously */
463 if (priv->psstate == PS_STATE_PRE_SLEEP &&
464 !priv->dnld_sent && !priv->cur_cmd) {
465 if (priv->connect_status == LBS_CONNECTED) {
466 lbs_deb_thread("pre-sleep, currenttxskb %p, "
467 "dnld_sent %d, cur_cmd %p\n",
468 priv->currenttxskb, priv->dnld_sent,
469 priv->cur_cmd);
471 lbs_ps_confirm_sleep(priv);
472 } else {
473 /* workaround for firmware sending
474 * deauth/linkloss event immediately
475 * after sleep request; remove this
476 * after firmware fixes it
478 priv->psstate = PS_STATE_AWAKE;
479 netdev_alert(dev,
480 "ignore PS_SleepConfirm in non-connected state\n");
484 /* The PS state is changed during processing of Sleep Request
485 * event above
487 if ((priv->psstate == PS_STATE_SLEEP) ||
488 (priv->psstate == PS_STATE_PRE_SLEEP))
489 continue;
491 if (priv->is_deep_sleep)
492 continue;
494 /* Execute the next command */
495 if (!priv->dnld_sent && !priv->cur_cmd)
496 lbs_execute_next_command(priv);
498 spin_lock_irq(&priv->driver_lock);
499 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
500 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
501 priv->tx_pending_buf,
502 priv->tx_pending_len);
503 if (ret) {
504 lbs_deb_tx("host_to_card failed %d\n", ret);
505 priv->dnld_sent = DNLD_RES_RECEIVED;
507 priv->tx_pending_len = 0;
508 if (!priv->currenttxskb) {
509 /* We can wake the queues immediately if we aren't
510 waiting for TX feedback */
511 if (priv->connect_status == LBS_CONNECTED)
512 netif_wake_queue(priv->dev);
513 if (priv->mesh_dev &&
514 lbs_mesh_connected(priv))
515 netif_wake_queue(priv->mesh_dev);
518 spin_unlock_irq(&priv->driver_lock);
521 del_timer(&priv->command_timer);
522 del_timer(&priv->auto_deepsleep_timer);
524 lbs_deb_leave(LBS_DEB_THREAD);
525 return 0;
529 * lbs_setup_firmware - gets the HW spec from the firmware and sets
530 * some basic parameters
532 * @priv: A pointer to &struct lbs_private structure
533 * returns: 0 or -1
535 static int lbs_setup_firmware(struct lbs_private *priv)
537 int ret = -1;
538 s16 curlevel = 0, minlevel = 0, maxlevel = 0;
540 lbs_deb_enter(LBS_DEB_FW);
542 /* Read MAC address from firmware */
543 memset(priv->current_addr, 0xff, ETH_ALEN);
544 ret = lbs_update_hw_spec(priv);
545 if (ret)
546 goto done;
548 /* Read power levels if available */
549 ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
550 if (ret == 0) {
551 priv->txpower_cur = curlevel;
552 priv->txpower_min = minlevel;
553 priv->txpower_max = maxlevel;
556 /* Send cmd to FW to enable 11D function */
557 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
559 lbs_set_mac_control(priv);
560 done:
561 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
562 return ret;
565 int lbs_suspend(struct lbs_private *priv)
567 int ret;
569 lbs_deb_enter(LBS_DEB_FW);
571 if (priv->is_deep_sleep) {
572 ret = lbs_set_deep_sleep(priv, 0);
573 if (ret) {
574 netdev_err(priv->dev,
575 "deep sleep cancellation failed: %d\n", ret);
576 return ret;
578 priv->deep_sleep_required = 1;
581 ret = lbs_set_host_sleep(priv, 1);
583 netif_device_detach(priv->dev);
584 if (priv->mesh_dev)
585 netif_device_detach(priv->mesh_dev);
587 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
588 return ret;
590 EXPORT_SYMBOL_GPL(lbs_suspend);
592 int lbs_resume(struct lbs_private *priv)
594 int ret;
596 lbs_deb_enter(LBS_DEB_FW);
598 ret = lbs_set_host_sleep(priv, 0);
600 netif_device_attach(priv->dev);
601 if (priv->mesh_dev)
602 netif_device_attach(priv->mesh_dev);
604 if (priv->deep_sleep_required) {
605 priv->deep_sleep_required = 0;
606 ret = lbs_set_deep_sleep(priv, 1);
607 if (ret)
608 netdev_err(priv->dev,
609 "deep sleep activation failed: %d\n", ret);
612 if (priv->setup_fw_on_resume)
613 ret = lbs_setup_firmware(priv);
615 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
616 return ret;
618 EXPORT_SYMBOL_GPL(lbs_resume);
621 * lbs_cmd_timeout_handler - handles the timeout of command sending.
622 * It will re-send the same command again.
624 * @data: &struct lbs_private pointer
626 static void lbs_cmd_timeout_handler(unsigned long data)
628 struct lbs_private *priv = (struct lbs_private *)data;
629 unsigned long flags;
631 lbs_deb_enter(LBS_DEB_CMD);
632 spin_lock_irqsave(&priv->driver_lock, flags);
634 if (!priv->cur_cmd)
635 goto out;
637 netdev_info(priv->dev, "command 0x%04x timed out\n",
638 le16_to_cpu(priv->cur_cmd->cmdbuf->command));
640 priv->cmd_timed_out = 1;
641 wake_up_interruptible(&priv->waitq);
642 out:
643 spin_unlock_irqrestore(&priv->driver_lock, flags);
644 lbs_deb_leave(LBS_DEB_CMD);
648 * auto_deepsleep_timer_fn - put the device back to deep sleep mode when
649 * timer expires and no activity (command, event, data etc.) is detected.
650 * @data: &struct lbs_private pointer
651 * returns: N/A
653 static void auto_deepsleep_timer_fn(unsigned long data)
655 struct lbs_private *priv = (struct lbs_private *)data;
657 lbs_deb_enter(LBS_DEB_CMD);
659 if (priv->is_activity_detected) {
660 priv->is_activity_detected = 0;
661 } else {
662 if (priv->is_auto_deep_sleep_enabled &&
663 (!priv->wakeup_dev_required) &&
664 (priv->connect_status != LBS_CONNECTED)) {
665 struct cmd_header cmd;
667 lbs_deb_main("Entering auto deep sleep mode...\n");
668 memset(&cmd, 0, sizeof(cmd));
669 cmd.size = cpu_to_le16(sizeof(cmd));
670 lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
671 sizeof(cmd));
674 mod_timer(&priv->auto_deepsleep_timer , jiffies +
675 (priv->auto_deep_sleep_timeout * HZ)/1000);
676 lbs_deb_leave(LBS_DEB_CMD);
679 int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
681 lbs_deb_enter(LBS_DEB_SDIO);
683 priv->is_auto_deep_sleep_enabled = 1;
684 if (priv->is_deep_sleep)
685 priv->wakeup_dev_required = 1;
686 mod_timer(&priv->auto_deepsleep_timer ,
687 jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
689 lbs_deb_leave(LBS_DEB_SDIO);
690 return 0;
693 int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
695 lbs_deb_enter(LBS_DEB_SDIO);
697 priv->is_auto_deep_sleep_enabled = 0;
698 priv->auto_deep_sleep_timeout = 0;
699 del_timer(&priv->auto_deepsleep_timer);
701 lbs_deb_leave(LBS_DEB_SDIO);
702 return 0;
705 static int lbs_init_adapter(struct lbs_private *priv)
707 int ret;
709 lbs_deb_enter(LBS_DEB_MAIN);
711 memset(priv->current_addr, 0xff, ETH_ALEN);
713 priv->connect_status = LBS_DISCONNECTED;
714 priv->channel = DEFAULT_AD_HOC_CHANNEL;
715 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
716 priv->radio_on = 1;
717 priv->psmode = LBS802_11POWERMODECAM;
718 priv->psstate = PS_STATE_FULL_POWER;
719 priv->is_deep_sleep = 0;
720 priv->is_auto_deep_sleep_enabled = 0;
721 priv->deep_sleep_required = 0;
722 priv->wakeup_dev_required = 0;
723 init_waitqueue_head(&priv->ds_awake_q);
724 init_waitqueue_head(&priv->scan_q);
725 priv->authtype_auto = 1;
726 priv->is_host_sleep_configured = 0;
727 priv->is_host_sleep_activated = 0;
728 init_waitqueue_head(&priv->host_sleep_q);
729 mutex_init(&priv->lock);
731 setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
732 (unsigned long)priv);
733 setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
734 (unsigned long)priv);
736 INIT_LIST_HEAD(&priv->cmdfreeq);
737 INIT_LIST_HEAD(&priv->cmdpendingq);
739 spin_lock_init(&priv->driver_lock);
741 /* Allocate the command buffers */
742 if (lbs_allocate_cmd_buffer(priv)) {
743 pr_err("Out of memory allocating command buffers\n");
744 ret = -ENOMEM;
745 goto out;
747 priv->resp_idx = 0;
748 priv->resp_len[0] = priv->resp_len[1] = 0;
750 /* Create the event FIFO */
751 ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
752 if (ret) {
753 pr_err("Out of memory allocating event FIFO buffer\n");
754 goto out;
757 out:
758 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
760 return ret;
763 static void lbs_free_adapter(struct lbs_private *priv)
765 lbs_deb_enter(LBS_DEB_MAIN);
767 lbs_free_cmd_buffer(priv);
768 kfifo_free(&priv->event_fifo);
769 del_timer(&priv->command_timer);
770 del_timer(&priv->auto_deepsleep_timer);
772 lbs_deb_leave(LBS_DEB_MAIN);
775 static const struct net_device_ops lbs_netdev_ops = {
776 .ndo_open = lbs_dev_open,
777 .ndo_stop = lbs_eth_stop,
778 .ndo_start_xmit = lbs_hard_start_xmit,
779 .ndo_set_mac_address = lbs_set_mac_address,
780 .ndo_set_multicast_list = lbs_set_multicast_list,
781 .ndo_change_mtu = eth_change_mtu,
782 .ndo_validate_addr = eth_validate_addr,
786 * lbs_add_card - adds the card. It will probe the
787 * card, allocate the lbs_priv and initialize the device.
789 * @card: A pointer to card
790 * @dmdev: A pointer to &struct device
791 * returns: A pointer to &struct lbs_private structure
793 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
795 struct net_device *dev;
796 struct wireless_dev *wdev;
797 struct lbs_private *priv = NULL;
799 lbs_deb_enter(LBS_DEB_MAIN);
801 /* Allocate an Ethernet device and register it */
802 wdev = lbs_cfg_alloc(dmdev);
803 if (IS_ERR(wdev)) {
804 pr_err("cfg80211 init failed\n");
805 goto done;
808 wdev->iftype = NL80211_IFTYPE_STATION;
809 priv = wdev_priv(wdev);
810 priv->wdev = wdev;
812 if (lbs_init_adapter(priv)) {
813 pr_err("failed to initialize adapter structure\n");
814 goto err_wdev;
817 dev = alloc_netdev(0, "wlan%d", ether_setup);
818 if (!dev) {
819 dev_err(dmdev, "no memory for network device instance\n");
820 goto err_adapter;
823 dev->ieee80211_ptr = wdev;
824 dev->ml_priv = priv;
825 SET_NETDEV_DEV(dev, dmdev);
826 wdev->netdev = dev;
827 priv->dev = dev;
829 dev->netdev_ops = &lbs_netdev_ops;
830 dev->watchdog_timeo = 5 * HZ;
831 dev->ethtool_ops = &lbs_ethtool_ops;
832 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
834 priv->card = card;
836 strcpy(dev->name, "wlan%d");
838 lbs_deb_thread("Starting main thread...\n");
839 init_waitqueue_head(&priv->waitq);
840 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
841 if (IS_ERR(priv->main_thread)) {
842 lbs_deb_thread("Error creating main thread.\n");
843 goto err_ndev;
846 priv->work_thread = create_singlethread_workqueue("lbs_worker");
847 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
849 priv->wol_criteria = EHS_REMOVE_WAKEUP;
850 priv->wol_gpio = 0xff;
851 priv->wol_gap = 20;
852 priv->ehs_remove_supported = true;
854 goto done;
856 err_ndev:
857 free_netdev(dev);
859 err_adapter:
860 lbs_free_adapter(priv);
862 err_wdev:
863 lbs_cfg_free(priv);
865 priv = NULL;
867 done:
868 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
869 return priv;
871 EXPORT_SYMBOL_GPL(lbs_add_card);
874 void lbs_remove_card(struct lbs_private *priv)
876 struct net_device *dev = priv->dev;
878 lbs_deb_enter(LBS_DEB_MAIN);
880 lbs_remove_mesh(priv);
881 lbs_scan_deinit(priv);
883 dev = priv->dev;
885 cancel_work_sync(&priv->mcast_work);
887 /* worker thread destruction blocks on the in-flight command which
888 * should have been cleared already in lbs_stop_card().
890 lbs_deb_main("destroying worker thread\n");
891 destroy_workqueue(priv->work_thread);
892 lbs_deb_main("done destroying worker thread\n");
894 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
895 priv->psmode = LBS802_11POWERMODECAM;
896 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
899 if (priv->is_deep_sleep) {
900 priv->is_deep_sleep = 0;
901 wake_up_interruptible(&priv->ds_awake_q);
904 priv->is_host_sleep_configured = 0;
905 priv->is_host_sleep_activated = 0;
906 wake_up_interruptible(&priv->host_sleep_q);
908 /* Stop the thread servicing the interrupts */
909 priv->surpriseremoved = 1;
910 kthread_stop(priv->main_thread);
912 lbs_free_adapter(priv);
913 lbs_cfg_free(priv);
914 free_netdev(dev);
916 lbs_deb_leave(LBS_DEB_MAIN);
918 EXPORT_SYMBOL_GPL(lbs_remove_card);
921 int lbs_rtap_supported(struct lbs_private *priv)
923 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
924 return 1;
926 /* newer firmware use a capability mask */
927 return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
928 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
932 int lbs_start_card(struct lbs_private *priv)
934 struct net_device *dev = priv->dev;
935 int ret = -1;
937 lbs_deb_enter(LBS_DEB_MAIN);
939 /* poke the firmware */
940 ret = lbs_setup_firmware(priv);
941 if (ret)
942 goto done;
944 if (lbs_cfg_register(priv)) {
945 pr_err("cannot register device\n");
946 goto done;
949 lbs_update_channel(priv);
951 if (!lbs_disablemesh)
952 lbs_init_mesh(priv);
953 else
954 pr_info("%s: mesh disabled\n", dev->name);
956 lbs_debugfs_init_one(priv, dev);
958 netdev_info(dev, "Marvell WLAN 802.11 adapter\n");
960 ret = 0;
962 done:
963 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
964 return ret;
966 EXPORT_SYMBOL_GPL(lbs_start_card);
969 void lbs_stop_card(struct lbs_private *priv)
971 struct net_device *dev;
972 struct cmd_ctrl_node *cmdnode;
973 unsigned long flags;
975 lbs_deb_enter(LBS_DEB_MAIN);
977 if (!priv)
978 goto out;
979 dev = priv->dev;
981 netif_stop_queue(dev);
982 netif_carrier_off(dev);
984 lbs_debugfs_remove_one(priv);
985 lbs_deinit_mesh(priv);
987 /* Delete the timeout of the currently processing command */
988 del_timer_sync(&priv->command_timer);
989 del_timer_sync(&priv->auto_deepsleep_timer);
991 /* Flush pending command nodes */
992 spin_lock_irqsave(&priv->driver_lock, flags);
993 lbs_deb_main("clearing pending commands\n");
994 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
995 cmdnode->result = -ENOENT;
996 cmdnode->cmdwaitqwoken = 1;
997 wake_up_interruptible(&cmdnode->cmdwait_q);
1000 /* Flush the command the card is currently processing */
1001 if (priv->cur_cmd) {
1002 lbs_deb_main("clearing current command\n");
1003 priv->cur_cmd->result = -ENOENT;
1004 priv->cur_cmd->cmdwaitqwoken = 1;
1005 wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
1007 lbs_deb_main("done clearing commands\n");
1008 spin_unlock_irqrestore(&priv->driver_lock, flags);
1010 unregister_netdev(dev);
1012 out:
1013 lbs_deb_leave(LBS_DEB_MAIN);
1015 EXPORT_SYMBOL_GPL(lbs_stop_card);
1018 void lbs_queue_event(struct lbs_private *priv, u32 event)
1020 unsigned long flags;
1022 lbs_deb_enter(LBS_DEB_THREAD);
1023 spin_lock_irqsave(&priv->driver_lock, flags);
1025 if (priv->psstate == PS_STATE_SLEEP)
1026 priv->psstate = PS_STATE_AWAKE;
1028 kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1030 wake_up_interruptible(&priv->waitq);
1032 spin_unlock_irqrestore(&priv->driver_lock, flags);
1033 lbs_deb_leave(LBS_DEB_THREAD);
1035 EXPORT_SYMBOL_GPL(lbs_queue_event);
1037 void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
1039 lbs_deb_enter(LBS_DEB_THREAD);
1041 if (priv->psstate == PS_STATE_SLEEP)
1042 priv->psstate = PS_STATE_AWAKE;
1044 /* Swap buffers by flipping the response index */
1045 BUG_ON(resp_idx > 1);
1046 priv->resp_idx = resp_idx;
1048 wake_up_interruptible(&priv->waitq);
1050 lbs_deb_leave(LBS_DEB_THREAD);
1052 EXPORT_SYMBOL_GPL(lbs_notify_command_response);
1055 * lbs_get_firmware - Retrieves two-stage firmware
1057 * @dev: A pointer to &device structure
1058 * @user_helper: User-defined helper firmware file
1059 * @user_mainfw: User-defined main firmware file
1060 * @card_model: Bus-specific card model ID used to filter firmware table
1061 * elements
1062 * @fw_table: Table of firmware file names and device model numbers
1063 * terminated by an entry with a NULL helper name
1064 * @helper: On success, the helper firmware; caller must free
1065 * @mainfw: On success, the main firmware; caller must free
1067 * returns: 0 on success, non-zero on failure
1069 int lbs_get_firmware(struct device *dev, const char *user_helper,
1070 const char *user_mainfw, u32 card_model,
1071 const struct lbs_fw_table *fw_table,
1072 const struct firmware **helper,
1073 const struct firmware **mainfw)
1075 const struct lbs_fw_table *iter;
1076 int ret;
1078 BUG_ON(helper == NULL);
1079 BUG_ON(mainfw == NULL);
1081 /* Try user-specified firmware first */
1082 if (user_helper) {
1083 ret = request_firmware(helper, user_helper, dev);
1084 if (ret) {
1085 dev_err(dev, "couldn't find helper firmware %s\n",
1086 user_helper);
1087 goto fail;
1090 if (user_mainfw) {
1091 ret = request_firmware(mainfw, user_mainfw, dev);
1092 if (ret) {
1093 dev_err(dev, "couldn't find main firmware %s\n",
1094 user_mainfw);
1095 goto fail;
1099 if (*helper && *mainfw)
1100 return 0;
1102 /* Otherwise search for firmware to use. If neither the helper or
1103 * the main firmware were specified by the user, then we need to
1104 * make sure that found helper & main are from the same entry in
1105 * fw_table.
1107 iter = fw_table;
1108 while (iter && iter->helper) {
1109 if (iter->model != card_model)
1110 goto next;
1112 if (*helper == NULL) {
1113 ret = request_firmware(helper, iter->helper, dev);
1114 if (ret)
1115 goto next;
1117 /* If the device has one-stage firmware (ie cf8305) and
1118 * we've got it then we don't need to bother with the
1119 * main firmware.
1121 if (iter->fwname == NULL)
1122 return 0;
1125 if (*mainfw == NULL) {
1126 ret = request_firmware(mainfw, iter->fwname, dev);
1127 if (ret && !user_helper) {
1128 /* Clear the helper if it wasn't user-specified
1129 * and the main firmware load failed, to ensure
1130 * we don't have mismatched firmware pairs.
1132 release_firmware(*helper);
1133 *helper = NULL;
1137 if (*helper && *mainfw)
1138 return 0;
1140 next:
1141 iter++;
1144 fail:
1145 /* Failed */
1146 if (*helper) {
1147 release_firmware(*helper);
1148 *helper = NULL;
1150 if (*mainfw) {
1151 release_firmware(*mainfw);
1152 *mainfw = NULL;
1155 return -ENOENT;
1157 EXPORT_SYMBOL_GPL(lbs_get_firmware);
1159 static int __init lbs_init_module(void)
1161 lbs_deb_enter(LBS_DEB_MAIN);
1162 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1163 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1164 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1165 confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
1166 lbs_debugfs_init();
1167 lbs_deb_leave(LBS_DEB_MAIN);
1168 return 0;
1171 static void __exit lbs_exit_module(void)
1173 lbs_deb_enter(LBS_DEB_MAIN);
1174 lbs_debugfs_remove();
1175 lbs_deb_leave(LBS_DEB_MAIN);
1178 module_init(lbs_init_module);
1179 module_exit(lbs_exit_module);
1181 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1182 MODULE_AUTHOR("Marvell International Ltd.");
1183 MODULE_LICENSE("GPL");