Revert "Merge branch 'ath6kl-next' of master.kernel.org:/pub/scm/linux/kernel/git...
[linux-2.6/next.git] / drivers / net / wireless / libertas / main.c
blobd62d1fb4177f677c318d8040e7ba3542ee1e29f4
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/hardirq.h>
13 #include <linux/netdevice.h>
14 #include <linux/if_arp.h>
15 #include <linux/kthread.h>
16 #include <linux/kfifo.h>
17 #include <linux/slab.h>
18 #include <net/cfg80211.h>
20 #include "host.h"
21 #include "decl.h"
22 #include "dev.h"
23 #include "cfg.h"
24 #include "debugfs.h"
25 #include "cmd.h"
26 #include "mesh.h"
28 #define DRIVER_RELEASE_VERSION "323.p0"
29 const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
30 #ifdef DEBUG
31 "-dbg"
32 #endif
33 "";
36 /* Module parameters */
37 unsigned int lbs_debug;
38 EXPORT_SYMBOL_GPL(lbs_debug);
39 module_param_named(libertas_debug, lbs_debug, int, 0644);
41 unsigned int lbs_disablemesh;
42 EXPORT_SYMBOL_GPL(lbs_disablemesh);
43 module_param_named(libertas_disablemesh, lbs_disablemesh, int, 0644);
47 * This global structure is used to send the confirm_sleep command as
48 * fast as possible down to the firmware.
50 struct cmd_confirm_sleep confirm_sleep;
54 * the table to keep region code
56 u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
57 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
60 * FW rate table. FW refers to rates by their index in this table, not by the
61 * rate value itself. Values of 0x00 are
62 * reserved positions.
64 static u8 fw_data_rates[MAX_RATES] =
65 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
66 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
69 /**
70 * lbs_fw_index_to_data_rate - use index to get the data rate
72 * @idx: The index of data rate
73 * returns: data rate or 0
75 u32 lbs_fw_index_to_data_rate(u8 idx)
77 if (idx >= sizeof(fw_data_rates))
78 idx = 0;
79 return fw_data_rates[idx];
82 /**
83 * lbs_data_rate_to_fw_index - use rate to get the index
85 * @rate: data rate
86 * returns: index or 0
88 u8 lbs_data_rate_to_fw_index(u32 rate)
90 u8 i;
92 if (!rate)
93 return 0;
95 for (i = 0; i < sizeof(fw_data_rates); i++) {
96 if (rate == fw_data_rates[i])
97 return i;
99 return 0;
102 int lbs_start_iface(struct lbs_private *priv)
104 struct cmd_ds_802_11_mac_address cmd;
105 int ret;
107 if (priv->power_restore) {
108 ret = priv->power_restore(priv);
109 if (ret)
110 return ret;
113 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
114 cmd.action = cpu_to_le16(CMD_ACT_SET);
115 memcpy(cmd.macadd, priv->current_addr, ETH_ALEN);
117 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
118 if (ret) {
119 lbs_deb_net("set MAC address failed\n");
120 goto err;
123 lbs_update_channel(priv);
125 priv->iface_running = true;
126 return 0;
128 err:
129 if (priv->power_save)
130 priv->power_save(priv);
131 return ret;
135 * lbs_dev_open - open the ethX interface
137 * @dev: A pointer to &net_device structure
138 * returns: 0 or -EBUSY if monitor mode active
140 static int lbs_dev_open(struct net_device *dev)
142 struct lbs_private *priv = dev->ml_priv;
143 int ret = 0;
145 lbs_deb_enter(LBS_DEB_NET);
146 if (!priv->iface_running) {
147 ret = lbs_start_iface(priv);
148 if (ret)
149 goto out;
152 spin_lock_irq(&priv->driver_lock);
154 netif_carrier_off(dev);
156 if (!priv->tx_pending_len)
157 netif_wake_queue(dev);
159 spin_unlock_irq(&priv->driver_lock);
161 out:
162 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
163 return ret;
166 static bool lbs_command_queue_empty(struct lbs_private *priv)
168 unsigned long flags;
169 bool ret;
170 spin_lock_irqsave(&priv->driver_lock, flags);
171 ret = priv->cur_cmd == NULL && list_empty(&priv->cmdpendingq);
172 spin_unlock_irqrestore(&priv->driver_lock, flags);
173 return ret;
176 int lbs_stop_iface(struct lbs_private *priv)
178 unsigned long flags;
179 int ret = 0;
181 lbs_deb_enter(LBS_DEB_MAIN);
183 spin_lock_irqsave(&priv->driver_lock, flags);
184 priv->iface_running = false;
185 kfree_skb(priv->currenttxskb);
186 priv->currenttxskb = NULL;
187 priv->tx_pending_len = 0;
188 spin_unlock_irqrestore(&priv->driver_lock, flags);
190 cancel_work_sync(&priv->mcast_work);
192 /* Disable command processing, and wait for all commands to complete */
193 lbs_deb_main("waiting for commands to complete\n");
194 wait_event(priv->waitq, lbs_command_queue_empty(priv));
195 lbs_deb_main("all commands completed\n");
197 if (priv->power_save)
198 ret = priv->power_save(priv);
200 lbs_deb_leave(LBS_DEB_MAIN);
201 return ret;
205 * lbs_eth_stop - close the ethX interface
207 * @dev: A pointer to &net_device structure
208 * returns: 0
210 static int lbs_eth_stop(struct net_device *dev)
212 struct lbs_private *priv = dev->ml_priv;
214 lbs_deb_enter(LBS_DEB_NET);
216 if (priv->connect_status == LBS_CONNECTED)
217 lbs_disconnect(priv, WLAN_REASON_DEAUTH_LEAVING);
219 spin_lock_irq(&priv->driver_lock);
220 netif_stop_queue(dev);
221 spin_unlock_irq(&priv->driver_lock);
223 lbs_update_mcast(priv);
224 cancel_delayed_work_sync(&priv->scan_work);
225 if (priv->scan_req) {
226 cfg80211_scan_done(priv->scan_req, false);
227 priv->scan_req = NULL;
230 netif_carrier_off(priv->dev);
232 if (!lbs_iface_active(priv))
233 lbs_stop_iface(priv);
235 lbs_deb_leave(LBS_DEB_NET);
236 return 0;
239 void lbs_host_to_card_done(struct lbs_private *priv)
241 unsigned long flags;
243 lbs_deb_enter(LBS_DEB_THREAD);
245 spin_lock_irqsave(&priv->driver_lock, flags);
247 priv->dnld_sent = DNLD_RES_RECEIVED;
249 /* Wake main thread if commands are pending */
250 if (!priv->cur_cmd || priv->tx_pending_len > 0) {
251 if (!priv->wakeup_dev_required)
252 wake_up(&priv->waitq);
255 spin_unlock_irqrestore(&priv->driver_lock, flags);
256 lbs_deb_leave(LBS_DEB_THREAD);
258 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
260 int lbs_set_mac_address(struct net_device *dev, void *addr)
262 int ret = 0;
263 struct lbs_private *priv = dev->ml_priv;
264 struct sockaddr *phwaddr = addr;
266 lbs_deb_enter(LBS_DEB_NET);
269 * Can only set MAC address when all interfaces are down, to be written
270 * to the hardware when one of them is brought up.
272 if (lbs_iface_active(priv))
273 return -EBUSY;
275 /* In case it was called from the mesh device */
276 dev = priv->dev;
278 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
279 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
280 if (priv->mesh_dev)
281 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
283 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
284 return ret;
288 static inline int mac_in_list(unsigned char *list, int list_len,
289 unsigned char *mac)
291 while (list_len) {
292 if (!memcmp(list, mac, ETH_ALEN))
293 return 1;
294 list += ETH_ALEN;
295 list_len--;
297 return 0;
301 static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
302 struct net_device *dev, int nr_addrs)
304 int i = nr_addrs;
305 struct netdev_hw_addr *ha;
306 int cnt;
308 if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
309 return nr_addrs;
311 netif_addr_lock_bh(dev);
312 cnt = netdev_mc_count(dev);
313 netdev_for_each_mc_addr(ha, dev) {
314 if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
315 lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
316 ha->addr);
317 cnt--;
318 continue;
321 if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
322 break;
323 memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
324 lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
325 ha->addr);
326 i++;
327 cnt--;
329 netif_addr_unlock_bh(dev);
330 if (cnt)
331 return -EOVERFLOW;
333 return i;
336 void lbs_update_mcast(struct lbs_private *priv)
338 struct cmd_ds_mac_multicast_adr mcast_cmd;
339 int dev_flags = 0;
340 int nr_addrs;
341 int old_mac_control = priv->mac_control;
343 lbs_deb_enter(LBS_DEB_NET);
345 if (netif_running(priv->dev))
346 dev_flags |= priv->dev->flags;
347 if (priv->mesh_dev && netif_running(priv->mesh_dev))
348 dev_flags |= priv->mesh_dev->flags;
350 if (dev_flags & IFF_PROMISC) {
351 priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
352 priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
353 CMD_ACT_MAC_MULTICAST_ENABLE);
354 goto out_set_mac_control;
355 } else if (dev_flags & IFF_ALLMULTI) {
356 do_allmulti:
357 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
358 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
359 CMD_ACT_MAC_MULTICAST_ENABLE);
360 goto out_set_mac_control;
363 /* Once for priv->dev, again for priv->mesh_dev if it exists */
364 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
365 if (nr_addrs >= 0 && priv->mesh_dev)
366 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
367 if (nr_addrs < 0)
368 goto do_allmulti;
370 if (nr_addrs) {
371 int size = offsetof(struct cmd_ds_mac_multicast_adr,
372 maclist[6*nr_addrs]);
374 mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
375 mcast_cmd.hdr.size = cpu_to_le16(size);
376 mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
378 lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
380 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
381 } else
382 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
384 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
385 CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
386 out_set_mac_control:
387 if (priv->mac_control != old_mac_control)
388 lbs_set_mac_control(priv);
390 lbs_deb_leave(LBS_DEB_NET);
393 static void lbs_set_mcast_worker(struct work_struct *work)
395 struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
396 lbs_update_mcast(priv);
399 void lbs_set_multicast_list(struct net_device *dev)
401 struct lbs_private *priv = dev->ml_priv;
403 schedule_work(&priv->mcast_work);
407 * lbs_thread - handles the major jobs in the LBS driver.
408 * It handles all events generated by firmware, RX data received
409 * from firmware and TX data sent from kernel.
411 * @data: A pointer to &lbs_thread structure
412 * returns: 0
414 static int lbs_thread(void *data)
416 struct net_device *dev = data;
417 struct lbs_private *priv = dev->ml_priv;
418 wait_queue_t wait;
420 lbs_deb_enter(LBS_DEB_THREAD);
422 init_waitqueue_entry(&wait, current);
424 for (;;) {
425 int shouldsleep;
426 u8 resp_idx;
428 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
429 priv->currenttxskb, priv->dnld_sent);
431 add_wait_queue(&priv->waitq, &wait);
432 set_current_state(TASK_INTERRUPTIBLE);
433 spin_lock_irq(&priv->driver_lock);
435 if (kthread_should_stop())
436 shouldsleep = 0; /* Bye */
437 else if (priv->surpriseremoved)
438 shouldsleep = 1; /* We need to wait until we're _told_ to die */
439 else if (priv->psstate == PS_STATE_SLEEP)
440 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
441 else if (priv->cmd_timed_out)
442 shouldsleep = 0; /* Command timed out. Recover */
443 else if (!priv->fw_ready)
444 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
445 else if (priv->dnld_sent)
446 shouldsleep = 1; /* Something is en route to the device already */
447 else if (priv->tx_pending_len > 0)
448 shouldsleep = 0; /* We've a packet to send */
449 else if (priv->resp_len[priv->resp_idx])
450 shouldsleep = 0; /* We have a command response */
451 else if (priv->cur_cmd)
452 shouldsleep = 1; /* Can't send a command; one already running */
453 else if (!list_empty(&priv->cmdpendingq) &&
454 !(priv->wakeup_dev_required))
455 shouldsleep = 0; /* We have a command to send */
456 else if (kfifo_len(&priv->event_fifo))
457 shouldsleep = 0; /* We have an event to process */
458 else
459 shouldsleep = 1; /* No command */
461 if (shouldsleep) {
462 lbs_deb_thread("sleeping, connect_status %d, "
463 "psmode %d, psstate %d\n",
464 priv->connect_status,
465 priv->psmode, priv->psstate);
466 spin_unlock_irq(&priv->driver_lock);
467 schedule();
468 } else
469 spin_unlock_irq(&priv->driver_lock);
471 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
472 priv->currenttxskb, priv->dnld_sent);
474 set_current_state(TASK_RUNNING);
475 remove_wait_queue(&priv->waitq, &wait);
477 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
478 priv->currenttxskb, priv->dnld_sent);
480 if (kthread_should_stop()) {
481 lbs_deb_thread("break from main thread\n");
482 break;
485 if (priv->surpriseremoved) {
486 lbs_deb_thread("adapter removed; waiting to die...\n");
487 continue;
490 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
491 priv->currenttxskb, priv->dnld_sent);
493 /* Process any pending command response */
494 spin_lock_irq(&priv->driver_lock);
495 resp_idx = priv->resp_idx;
496 if (priv->resp_len[resp_idx]) {
497 spin_unlock_irq(&priv->driver_lock);
498 lbs_process_command_response(priv,
499 priv->resp_buf[resp_idx],
500 priv->resp_len[resp_idx]);
501 spin_lock_irq(&priv->driver_lock);
502 priv->resp_len[resp_idx] = 0;
504 spin_unlock_irq(&priv->driver_lock);
506 /* Process hardware events, e.g. card removed, link lost */
507 spin_lock_irq(&priv->driver_lock);
508 while (kfifo_len(&priv->event_fifo)) {
509 u32 event;
511 if (kfifo_out(&priv->event_fifo,
512 (unsigned char *) &event, sizeof(event)) !=
513 sizeof(event))
514 break;
515 spin_unlock_irq(&priv->driver_lock);
516 lbs_process_event(priv, event);
517 spin_lock_irq(&priv->driver_lock);
519 spin_unlock_irq(&priv->driver_lock);
521 if (priv->wakeup_dev_required) {
522 lbs_deb_thread("Waking up device...\n");
523 /* Wake up device */
524 if (priv->exit_deep_sleep(priv))
525 lbs_deb_thread("Wakeup device failed\n");
526 continue;
529 /* command timeout stuff */
530 if (priv->cmd_timed_out && priv->cur_cmd) {
531 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
533 netdev_info(dev, "Timeout submitting command 0x%04x\n",
534 le16_to_cpu(cmdnode->cmdbuf->command));
535 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
536 if (priv->reset_card)
537 priv->reset_card(priv);
539 priv->cmd_timed_out = 0;
541 if (!priv->fw_ready)
542 continue;
544 /* Check if we need to confirm Sleep Request received previously */
545 if (priv->psstate == PS_STATE_PRE_SLEEP &&
546 !priv->dnld_sent && !priv->cur_cmd) {
547 if (priv->connect_status == LBS_CONNECTED) {
548 lbs_deb_thread("pre-sleep, currenttxskb %p, "
549 "dnld_sent %d, cur_cmd %p\n",
550 priv->currenttxskb, priv->dnld_sent,
551 priv->cur_cmd);
553 lbs_ps_confirm_sleep(priv);
554 } else {
555 /* workaround for firmware sending
556 * deauth/linkloss event immediately
557 * after sleep request; remove this
558 * after firmware fixes it
560 priv->psstate = PS_STATE_AWAKE;
561 netdev_alert(dev,
562 "ignore PS_SleepConfirm in non-connected state\n");
566 /* The PS state is changed during processing of Sleep Request
567 * event above
569 if ((priv->psstate == PS_STATE_SLEEP) ||
570 (priv->psstate == PS_STATE_PRE_SLEEP))
571 continue;
573 if (priv->is_deep_sleep)
574 continue;
576 /* Execute the next command */
577 if (!priv->dnld_sent && !priv->cur_cmd)
578 lbs_execute_next_command(priv);
580 spin_lock_irq(&priv->driver_lock);
581 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
582 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
583 priv->tx_pending_buf,
584 priv->tx_pending_len);
585 if (ret) {
586 lbs_deb_tx("host_to_card failed %d\n", ret);
587 priv->dnld_sent = DNLD_RES_RECEIVED;
589 priv->tx_pending_len = 0;
590 if (!priv->currenttxskb) {
591 /* We can wake the queues immediately if we aren't
592 waiting for TX feedback */
593 if (priv->connect_status == LBS_CONNECTED)
594 netif_wake_queue(priv->dev);
595 if (priv->mesh_dev &&
596 netif_running(priv->mesh_dev))
597 netif_wake_queue(priv->mesh_dev);
600 spin_unlock_irq(&priv->driver_lock);
603 del_timer(&priv->command_timer);
604 del_timer(&priv->auto_deepsleep_timer);
606 lbs_deb_leave(LBS_DEB_THREAD);
607 return 0;
611 * lbs_setup_firmware - gets the HW spec from the firmware and sets
612 * some basic parameters
614 * @priv: A pointer to &struct lbs_private structure
615 * returns: 0 or -1
617 static int lbs_setup_firmware(struct lbs_private *priv)
619 int ret = -1;
620 s16 curlevel = 0, minlevel = 0, maxlevel = 0;
622 lbs_deb_enter(LBS_DEB_FW);
624 /* Read MAC address from firmware */
625 memset(priv->current_addr, 0xff, ETH_ALEN);
626 ret = lbs_update_hw_spec(priv);
627 if (ret)
628 goto done;
630 /* Read power levels if available */
631 ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
632 if (ret == 0) {
633 priv->txpower_cur = curlevel;
634 priv->txpower_min = minlevel;
635 priv->txpower_max = maxlevel;
638 /* Send cmd to FW to enable 11D function */
639 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
641 lbs_set_mac_control(priv);
642 done:
643 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
644 return ret;
647 int lbs_suspend(struct lbs_private *priv)
649 int ret;
651 lbs_deb_enter(LBS_DEB_FW);
653 if (priv->is_deep_sleep) {
654 ret = lbs_set_deep_sleep(priv, 0);
655 if (ret) {
656 netdev_err(priv->dev,
657 "deep sleep cancellation failed: %d\n", ret);
658 return ret;
660 priv->deep_sleep_required = 1;
663 ret = lbs_set_host_sleep(priv, 1);
665 netif_device_detach(priv->dev);
666 if (priv->mesh_dev)
667 netif_device_detach(priv->mesh_dev);
669 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
670 return ret;
672 EXPORT_SYMBOL_GPL(lbs_suspend);
674 int lbs_resume(struct lbs_private *priv)
676 int ret;
678 lbs_deb_enter(LBS_DEB_FW);
680 ret = lbs_set_host_sleep(priv, 0);
682 netif_device_attach(priv->dev);
683 if (priv->mesh_dev)
684 netif_device_attach(priv->mesh_dev);
686 if (priv->deep_sleep_required) {
687 priv->deep_sleep_required = 0;
688 ret = lbs_set_deep_sleep(priv, 1);
689 if (ret)
690 netdev_err(priv->dev,
691 "deep sleep activation failed: %d\n", ret);
694 if (priv->setup_fw_on_resume)
695 ret = lbs_setup_firmware(priv);
697 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
698 return ret;
700 EXPORT_SYMBOL_GPL(lbs_resume);
703 * lbs_cmd_timeout_handler - handles the timeout of command sending.
704 * It will re-send the same command again.
706 * @data: &struct lbs_private pointer
708 static void lbs_cmd_timeout_handler(unsigned long data)
710 struct lbs_private *priv = (struct lbs_private *)data;
711 unsigned long flags;
713 lbs_deb_enter(LBS_DEB_CMD);
714 spin_lock_irqsave(&priv->driver_lock, flags);
716 if (!priv->cur_cmd)
717 goto out;
719 netdev_info(priv->dev, "command 0x%04x timed out\n",
720 le16_to_cpu(priv->cur_cmd->cmdbuf->command));
722 priv->cmd_timed_out = 1;
725 * If the device didn't even acknowledge the command, reset the state
726 * so that we don't block all future commands due to this one timeout.
728 if (priv->dnld_sent == DNLD_CMD_SENT)
729 priv->dnld_sent = DNLD_RES_RECEIVED;
731 wake_up(&priv->waitq);
732 out:
733 spin_unlock_irqrestore(&priv->driver_lock, flags);
734 lbs_deb_leave(LBS_DEB_CMD);
738 * auto_deepsleep_timer_fn - put the device back to deep sleep mode when
739 * timer expires and no activity (command, event, data etc.) is detected.
740 * @data: &struct lbs_private pointer
741 * returns: N/A
743 static void auto_deepsleep_timer_fn(unsigned long data)
745 struct lbs_private *priv = (struct lbs_private *)data;
747 lbs_deb_enter(LBS_DEB_CMD);
749 if (priv->is_activity_detected) {
750 priv->is_activity_detected = 0;
751 } else {
752 if (priv->is_auto_deep_sleep_enabled &&
753 (!priv->wakeup_dev_required) &&
754 (priv->connect_status != LBS_CONNECTED)) {
755 struct cmd_header cmd;
757 lbs_deb_main("Entering auto deep sleep mode...\n");
758 memset(&cmd, 0, sizeof(cmd));
759 cmd.size = cpu_to_le16(sizeof(cmd));
760 lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
761 sizeof(cmd));
764 mod_timer(&priv->auto_deepsleep_timer , jiffies +
765 (priv->auto_deep_sleep_timeout * HZ)/1000);
766 lbs_deb_leave(LBS_DEB_CMD);
769 int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
771 lbs_deb_enter(LBS_DEB_SDIO);
773 priv->is_auto_deep_sleep_enabled = 1;
774 if (priv->is_deep_sleep)
775 priv->wakeup_dev_required = 1;
776 mod_timer(&priv->auto_deepsleep_timer ,
777 jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
779 lbs_deb_leave(LBS_DEB_SDIO);
780 return 0;
783 int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
785 lbs_deb_enter(LBS_DEB_SDIO);
787 priv->is_auto_deep_sleep_enabled = 0;
788 priv->auto_deep_sleep_timeout = 0;
789 del_timer(&priv->auto_deepsleep_timer);
791 lbs_deb_leave(LBS_DEB_SDIO);
792 return 0;
795 static int lbs_init_adapter(struct lbs_private *priv)
797 int ret;
799 lbs_deb_enter(LBS_DEB_MAIN);
801 memset(priv->current_addr, 0xff, ETH_ALEN);
803 priv->connect_status = LBS_DISCONNECTED;
804 priv->channel = DEFAULT_AD_HOC_CHANNEL;
805 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
806 priv->radio_on = 1;
807 priv->psmode = LBS802_11POWERMODECAM;
808 priv->psstate = PS_STATE_FULL_POWER;
809 priv->is_deep_sleep = 0;
810 priv->is_auto_deep_sleep_enabled = 0;
811 priv->deep_sleep_required = 0;
812 priv->wakeup_dev_required = 0;
813 init_waitqueue_head(&priv->ds_awake_q);
814 init_waitqueue_head(&priv->scan_q);
815 priv->authtype_auto = 1;
816 priv->is_host_sleep_configured = 0;
817 priv->is_host_sleep_activated = 0;
818 init_waitqueue_head(&priv->host_sleep_q);
819 mutex_init(&priv->lock);
821 setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
822 (unsigned long)priv);
823 setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
824 (unsigned long)priv);
826 INIT_LIST_HEAD(&priv->cmdfreeq);
827 INIT_LIST_HEAD(&priv->cmdpendingq);
829 spin_lock_init(&priv->driver_lock);
831 /* Allocate the command buffers */
832 if (lbs_allocate_cmd_buffer(priv)) {
833 pr_err("Out of memory allocating command buffers\n");
834 ret = -ENOMEM;
835 goto out;
837 priv->resp_idx = 0;
838 priv->resp_len[0] = priv->resp_len[1] = 0;
840 /* Create the event FIFO */
841 ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
842 if (ret) {
843 pr_err("Out of memory allocating event FIFO buffer\n");
844 goto out;
847 out:
848 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
850 return ret;
853 static void lbs_free_adapter(struct lbs_private *priv)
855 lbs_deb_enter(LBS_DEB_MAIN);
857 lbs_free_cmd_buffer(priv);
858 kfifo_free(&priv->event_fifo);
859 del_timer(&priv->command_timer);
860 del_timer(&priv->auto_deepsleep_timer);
862 lbs_deb_leave(LBS_DEB_MAIN);
865 static const struct net_device_ops lbs_netdev_ops = {
866 .ndo_open = lbs_dev_open,
867 .ndo_stop = lbs_eth_stop,
868 .ndo_start_xmit = lbs_hard_start_xmit,
869 .ndo_set_mac_address = lbs_set_mac_address,
870 .ndo_set_multicast_list = lbs_set_multicast_list,
871 .ndo_change_mtu = eth_change_mtu,
872 .ndo_validate_addr = eth_validate_addr,
876 * lbs_add_card - adds the card. It will probe the
877 * card, allocate the lbs_priv and initialize the device.
879 * @card: A pointer to card
880 * @dmdev: A pointer to &struct device
881 * returns: A pointer to &struct lbs_private structure
883 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
885 struct net_device *dev;
886 struct wireless_dev *wdev;
887 struct lbs_private *priv = NULL;
889 lbs_deb_enter(LBS_DEB_MAIN);
891 /* Allocate an Ethernet device and register it */
892 wdev = lbs_cfg_alloc(dmdev);
893 if (IS_ERR(wdev)) {
894 pr_err("cfg80211 init failed\n");
895 goto done;
898 wdev->iftype = NL80211_IFTYPE_STATION;
899 priv = wdev_priv(wdev);
900 priv->wdev = wdev;
902 if (lbs_init_adapter(priv)) {
903 pr_err("failed to initialize adapter structure\n");
904 goto err_wdev;
907 dev = alloc_netdev(0, "wlan%d", ether_setup);
908 if (!dev) {
909 dev_err(dmdev, "no memory for network device instance\n");
910 goto err_adapter;
913 dev->ieee80211_ptr = wdev;
914 dev->ml_priv = priv;
915 SET_NETDEV_DEV(dev, dmdev);
916 wdev->netdev = dev;
917 priv->dev = dev;
919 dev->netdev_ops = &lbs_netdev_ops;
920 dev->watchdog_timeo = 5 * HZ;
921 dev->ethtool_ops = &lbs_ethtool_ops;
922 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
924 priv->card = card;
926 strcpy(dev->name, "wlan%d");
928 lbs_deb_thread("Starting main thread...\n");
929 init_waitqueue_head(&priv->waitq);
930 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
931 if (IS_ERR(priv->main_thread)) {
932 lbs_deb_thread("Error creating main thread.\n");
933 goto err_ndev;
936 priv->work_thread = create_singlethread_workqueue("lbs_worker");
937 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
939 priv->wol_criteria = EHS_REMOVE_WAKEUP;
940 priv->wol_gpio = 0xff;
941 priv->wol_gap = 20;
942 priv->ehs_remove_supported = true;
944 goto done;
946 err_ndev:
947 free_netdev(dev);
949 err_adapter:
950 lbs_free_adapter(priv);
952 err_wdev:
953 lbs_cfg_free(priv);
955 priv = NULL;
957 done:
958 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
959 return priv;
961 EXPORT_SYMBOL_GPL(lbs_add_card);
964 void lbs_remove_card(struct lbs_private *priv)
966 struct net_device *dev = priv->dev;
968 lbs_deb_enter(LBS_DEB_MAIN);
970 lbs_remove_mesh(priv);
971 lbs_scan_deinit(priv);
973 /* worker thread destruction blocks on the in-flight command which
974 * should have been cleared already in lbs_stop_card().
976 lbs_deb_main("destroying worker thread\n");
977 destroy_workqueue(priv->work_thread);
978 lbs_deb_main("done destroying worker thread\n");
980 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
981 priv->psmode = LBS802_11POWERMODECAM;
982 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
985 if (priv->is_deep_sleep) {
986 priv->is_deep_sleep = 0;
987 wake_up_interruptible(&priv->ds_awake_q);
990 priv->is_host_sleep_configured = 0;
991 priv->is_host_sleep_activated = 0;
992 wake_up_interruptible(&priv->host_sleep_q);
994 /* Stop the thread servicing the interrupts */
995 priv->surpriseremoved = 1;
996 kthread_stop(priv->main_thread);
998 lbs_free_adapter(priv);
999 lbs_cfg_free(priv);
1000 free_netdev(dev);
1002 lbs_deb_leave(LBS_DEB_MAIN);
1004 EXPORT_SYMBOL_GPL(lbs_remove_card);
1007 int lbs_rtap_supported(struct lbs_private *priv)
1009 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
1010 return 1;
1012 /* newer firmware use a capability mask */
1013 return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
1014 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
1018 int lbs_start_card(struct lbs_private *priv)
1020 struct net_device *dev = priv->dev;
1021 int ret = -1;
1023 lbs_deb_enter(LBS_DEB_MAIN);
1025 /* poke the firmware */
1026 ret = lbs_setup_firmware(priv);
1027 if (ret)
1028 goto done;
1030 if (!lbs_disablemesh)
1031 lbs_init_mesh(priv);
1032 else
1033 pr_info("%s: mesh disabled\n", dev->name);
1035 if (lbs_cfg_register(priv)) {
1036 pr_err("cannot register device\n");
1037 goto done;
1040 if (lbs_mesh_activated(priv))
1041 lbs_start_mesh(priv);
1043 lbs_debugfs_init_one(priv, dev);
1045 netdev_info(dev, "Marvell WLAN 802.11 adapter\n");
1047 ret = 0;
1049 done:
1050 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1051 return ret;
1053 EXPORT_SYMBOL_GPL(lbs_start_card);
1056 void lbs_stop_card(struct lbs_private *priv)
1058 struct net_device *dev;
1060 lbs_deb_enter(LBS_DEB_MAIN);
1062 if (!priv)
1063 goto out;
1064 dev = priv->dev;
1066 netif_stop_queue(dev);
1067 netif_carrier_off(dev);
1069 lbs_debugfs_remove_one(priv);
1070 lbs_deinit_mesh(priv);
1071 unregister_netdev(dev);
1073 out:
1074 lbs_deb_leave(LBS_DEB_MAIN);
1076 EXPORT_SYMBOL_GPL(lbs_stop_card);
1079 void lbs_queue_event(struct lbs_private *priv, u32 event)
1081 unsigned long flags;
1083 lbs_deb_enter(LBS_DEB_THREAD);
1084 spin_lock_irqsave(&priv->driver_lock, flags);
1086 if (priv->psstate == PS_STATE_SLEEP)
1087 priv->psstate = PS_STATE_AWAKE;
1089 kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1091 wake_up(&priv->waitq);
1093 spin_unlock_irqrestore(&priv->driver_lock, flags);
1094 lbs_deb_leave(LBS_DEB_THREAD);
1096 EXPORT_SYMBOL_GPL(lbs_queue_event);
1098 void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
1100 lbs_deb_enter(LBS_DEB_THREAD);
1102 if (priv->psstate == PS_STATE_SLEEP)
1103 priv->psstate = PS_STATE_AWAKE;
1105 /* Swap buffers by flipping the response index */
1106 BUG_ON(resp_idx > 1);
1107 priv->resp_idx = resp_idx;
1109 wake_up(&priv->waitq);
1111 lbs_deb_leave(LBS_DEB_THREAD);
1113 EXPORT_SYMBOL_GPL(lbs_notify_command_response);
1116 * lbs_get_firmware - Retrieves two-stage firmware
1118 * @dev: A pointer to &device structure
1119 * @user_helper: User-defined helper firmware file
1120 * @user_mainfw: User-defined main firmware file
1121 * @card_model: Bus-specific card model ID used to filter firmware table
1122 * elements
1123 * @fw_table: Table of firmware file names and device model numbers
1124 * terminated by an entry with a NULL helper name
1125 * @helper: On success, the helper firmware; caller must free
1126 * @mainfw: On success, the main firmware; caller must free
1128 * returns: 0 on success, non-zero on failure
1130 int lbs_get_firmware(struct device *dev, const char *user_helper,
1131 const char *user_mainfw, u32 card_model,
1132 const struct lbs_fw_table *fw_table,
1133 const struct firmware **helper,
1134 const struct firmware **mainfw)
1136 const struct lbs_fw_table *iter;
1137 int ret;
1139 BUG_ON(helper == NULL);
1140 BUG_ON(mainfw == NULL);
1142 /* Try user-specified firmware first */
1143 if (user_helper) {
1144 ret = request_firmware(helper, user_helper, dev);
1145 if (ret) {
1146 dev_err(dev, "couldn't find helper firmware %s\n",
1147 user_helper);
1148 goto fail;
1151 if (user_mainfw) {
1152 ret = request_firmware(mainfw, user_mainfw, dev);
1153 if (ret) {
1154 dev_err(dev, "couldn't find main firmware %s\n",
1155 user_mainfw);
1156 goto fail;
1160 if (*helper && *mainfw)
1161 return 0;
1163 /* Otherwise search for firmware to use. If neither the helper or
1164 * the main firmware were specified by the user, then we need to
1165 * make sure that found helper & main are from the same entry in
1166 * fw_table.
1168 iter = fw_table;
1169 while (iter && iter->helper) {
1170 if (iter->model != card_model)
1171 goto next;
1173 if (*helper == NULL) {
1174 ret = request_firmware(helper, iter->helper, dev);
1175 if (ret)
1176 goto next;
1178 /* If the device has one-stage firmware (ie cf8305) and
1179 * we've got it then we don't need to bother with the
1180 * main firmware.
1182 if (iter->fwname == NULL)
1183 return 0;
1186 if (*mainfw == NULL) {
1187 ret = request_firmware(mainfw, iter->fwname, dev);
1188 if (ret && !user_helper) {
1189 /* Clear the helper if it wasn't user-specified
1190 * and the main firmware load failed, to ensure
1191 * we don't have mismatched firmware pairs.
1193 release_firmware(*helper);
1194 *helper = NULL;
1198 if (*helper && *mainfw)
1199 return 0;
1201 next:
1202 iter++;
1205 fail:
1206 /* Failed */
1207 if (*helper) {
1208 release_firmware(*helper);
1209 *helper = NULL;
1211 if (*mainfw) {
1212 release_firmware(*mainfw);
1213 *mainfw = NULL;
1216 return -ENOENT;
1218 EXPORT_SYMBOL_GPL(lbs_get_firmware);
1220 static int __init lbs_init_module(void)
1222 lbs_deb_enter(LBS_DEB_MAIN);
1223 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1224 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1225 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1226 confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
1227 lbs_debugfs_init();
1228 lbs_deb_leave(LBS_DEB_MAIN);
1229 return 0;
1232 static void __exit lbs_exit_module(void)
1234 lbs_deb_enter(LBS_DEB_MAIN);
1235 lbs_debugfs_remove();
1236 lbs_deb_leave(LBS_DEB_MAIN);
1239 module_init(lbs_init_module);
1240 module_exit(lbs_exit_module);
1242 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1243 MODULE_AUTHOR("Marvell International Ltd.");
1244 MODULE_LICENSE("GPL");