GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / wireless / libertas / main.c
blob7622924670426b4d88e68f29fcdb5c224c4e20fb
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 #include <linux/moduleparam.h>
8 #include <linux/delay.h>
9 #include <linux/etherdevice.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/kthread.h>
13 #include <linux/kfifo.h>
14 #include <linux/slab.h>
15 #include <net/cfg80211.h>
17 #include "host.h"
18 #include "decl.h"
19 #include "dev.h"
20 #include "cfg.h"
21 #include "debugfs.h"
22 #include "cmd.h"
24 #define DRIVER_RELEASE_VERSION "323.p0"
25 const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
26 #ifdef DEBUG
27 "-dbg"
28 #endif
29 "";
32 /* Module parameters */
33 unsigned int lbs_debug;
34 EXPORT_SYMBOL_GPL(lbs_debug);
35 module_param_named(libertas_debug, lbs_debug, int, 0644);
38 /* This global structure is used to send the confirm_sleep command as
39 * fast as possible down to the firmware. */
40 struct cmd_confirm_sleep confirm_sleep;
43 /**
44 * the table to keep region code
46 u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
47 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
49 /**
50 * FW rate table. FW refers to rates by their index in this table, not by the
51 * rate value itself. Values of 0x00 are
52 * reserved positions.
54 static u8 fw_data_rates[MAX_RATES] =
55 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
56 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
59 /**
60 * @brief use index to get the data rate
62 * @param idx The index of data rate
63 * @return data rate or 0
65 u32 lbs_fw_index_to_data_rate(u8 idx)
67 if (idx >= sizeof(fw_data_rates))
68 idx = 0;
69 return fw_data_rates[idx];
72 /**
73 * @brief use rate to get the index
75 * @param rate data rate
76 * @return index or 0
78 u8 lbs_data_rate_to_fw_index(u32 rate)
80 u8 i;
82 if (!rate)
83 return 0;
85 for (i = 0; i < sizeof(fw_data_rates); i++) {
86 if (rate == fw_data_rates[i])
87 return i;
89 return 0;
93 /**
94 * @brief This function opens the ethX interface
96 * @param dev A pointer to net_device structure
97 * @return 0 or -EBUSY if monitor mode active
99 static int lbs_dev_open(struct net_device *dev)
101 struct lbs_private *priv = dev->ml_priv;
102 int ret = 0;
104 lbs_deb_enter(LBS_DEB_NET);
106 spin_lock_irq(&priv->driver_lock);
108 if (priv->connect_status == LBS_CONNECTED)
109 netif_carrier_on(dev);
110 else
111 netif_carrier_off(dev);
113 if (!priv->tx_pending_len)
114 netif_wake_queue(dev);
116 spin_unlock_irq(&priv->driver_lock);
117 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
118 return ret;
122 * @brief This function closes the ethX interface
124 * @param dev A pointer to net_device structure
125 * @return 0
127 static int lbs_eth_stop(struct net_device *dev)
129 struct lbs_private *priv = dev->ml_priv;
131 lbs_deb_enter(LBS_DEB_NET);
133 spin_lock_irq(&priv->driver_lock);
134 netif_stop_queue(dev);
135 spin_unlock_irq(&priv->driver_lock);
137 schedule_work(&priv->mcast_work);
139 lbs_deb_leave(LBS_DEB_NET);
140 return 0;
143 static void lbs_tx_timeout(struct net_device *dev)
145 struct lbs_private *priv = dev->ml_priv;
147 lbs_deb_enter(LBS_DEB_TX);
149 lbs_pr_err("tx watch dog timeout\n");
151 dev->trans_start = jiffies; /* prevent tx timeout */
153 if (priv->currenttxskb)
154 lbs_send_tx_feedback(priv, 0);
156 /* XX: Shouldn't we also call into the hw-specific driver
157 to kick it somehow? */
158 lbs_host_to_card_done(priv);
161 lbs_deb_leave(LBS_DEB_TX);
164 void lbs_host_to_card_done(struct lbs_private *priv)
166 unsigned long flags;
168 lbs_deb_enter(LBS_DEB_THREAD);
170 spin_lock_irqsave(&priv->driver_lock, flags);
172 priv->dnld_sent = DNLD_RES_RECEIVED;
174 /* Wake main thread if commands are pending */
175 if (!priv->cur_cmd || priv->tx_pending_len > 0) {
176 if (!priv->wakeup_dev_required)
177 wake_up_interruptible(&priv->waitq);
180 spin_unlock_irqrestore(&priv->driver_lock, flags);
181 lbs_deb_leave(LBS_DEB_THREAD);
183 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
185 int lbs_set_mac_address(struct net_device *dev, void *addr)
187 int ret = 0;
188 struct lbs_private *priv = dev->ml_priv;
189 struct sockaddr *phwaddr = addr;
190 struct cmd_ds_802_11_mac_address cmd;
192 lbs_deb_enter(LBS_DEB_NET);
194 /* In case it was called from the mesh device */
195 dev = priv->dev;
197 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
198 cmd.action = cpu_to_le16(CMD_ACT_SET);
199 memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
201 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
202 if (ret) {
203 lbs_deb_net("set MAC address failed\n");
204 goto done;
207 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
208 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
209 if (priv->mesh_dev)
210 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
212 done:
213 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
214 return ret;
218 static inline int mac_in_list(unsigned char *list, int list_len,
219 unsigned char *mac)
221 while (list_len) {
222 if (!memcmp(list, mac, ETH_ALEN))
223 return 1;
224 list += ETH_ALEN;
225 list_len--;
227 return 0;
231 static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
232 struct net_device *dev, int nr_addrs)
234 int i = nr_addrs;
235 struct netdev_hw_addr *ha;
236 int cnt;
238 if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
239 return nr_addrs;
241 netif_addr_lock_bh(dev);
242 cnt = netdev_mc_count(dev);
243 netdev_for_each_mc_addr(ha, dev) {
244 if (mac_in_list(cmd->maclist, nr_addrs, ha->addr)) {
245 lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
246 ha->addr);
247 cnt--;
248 continue;
251 if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
252 break;
253 memcpy(&cmd->maclist[6*i], ha->addr, ETH_ALEN);
254 lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
255 ha->addr);
256 i++;
257 cnt--;
259 netif_addr_unlock_bh(dev);
260 if (cnt)
261 return -EOVERFLOW;
263 return i;
266 static void lbs_set_mcast_worker(struct work_struct *work)
268 struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
269 struct cmd_ds_mac_multicast_adr mcast_cmd;
270 int dev_flags;
271 int nr_addrs;
272 int old_mac_control = priv->mac_control;
274 lbs_deb_enter(LBS_DEB_NET);
276 dev_flags = priv->dev->flags;
277 if (priv->mesh_dev)
278 dev_flags |= priv->mesh_dev->flags;
280 if (dev_flags & IFF_PROMISC) {
281 priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
282 priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
283 CMD_ACT_MAC_MULTICAST_ENABLE);
284 goto out_set_mac_control;
285 } else if (dev_flags & IFF_ALLMULTI) {
286 do_allmulti:
287 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
288 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
289 CMD_ACT_MAC_MULTICAST_ENABLE);
290 goto out_set_mac_control;
293 /* Once for priv->dev, again for priv->mesh_dev if it exists */
294 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
295 if (nr_addrs >= 0 && priv->mesh_dev)
296 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
297 if (nr_addrs < 0)
298 goto do_allmulti;
300 if (nr_addrs) {
301 int size = offsetof(struct cmd_ds_mac_multicast_adr,
302 maclist[6*nr_addrs]);
304 mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
305 mcast_cmd.hdr.size = cpu_to_le16(size);
306 mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
308 lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
310 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
311 } else
312 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
314 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
315 CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
316 out_set_mac_control:
317 if (priv->mac_control != old_mac_control)
318 lbs_set_mac_control(priv);
320 lbs_deb_leave(LBS_DEB_NET);
323 void lbs_set_multicast_list(struct net_device *dev)
325 struct lbs_private *priv = dev->ml_priv;
327 schedule_work(&priv->mcast_work);
331 * @brief This function handles the major jobs in the LBS driver.
332 * It handles all events generated by firmware, RX data received
333 * from firmware and TX data sent from kernel.
335 * @param data A pointer to lbs_thread structure
336 * @return 0
338 static int lbs_thread(void *data)
340 struct net_device *dev = data;
341 struct lbs_private *priv = dev->ml_priv;
342 wait_queue_t wait;
344 lbs_deb_enter(LBS_DEB_THREAD);
346 init_waitqueue_entry(&wait, current);
348 for (;;) {
349 int shouldsleep;
350 u8 resp_idx;
352 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
353 priv->currenttxskb, priv->dnld_sent);
355 add_wait_queue(&priv->waitq, &wait);
356 set_current_state(TASK_INTERRUPTIBLE);
357 spin_lock_irq(&priv->driver_lock);
359 if (kthread_should_stop())
360 shouldsleep = 0; /* Bye */
361 else if (priv->surpriseremoved)
362 shouldsleep = 1; /* We need to wait until we're _told_ to die */
363 else if (priv->psstate == PS_STATE_SLEEP)
364 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
365 else if (priv->cmd_timed_out)
366 shouldsleep = 0; /* Command timed out. Recover */
367 else if (!priv->fw_ready)
368 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
369 else if (priv->dnld_sent)
370 shouldsleep = 1; /* Something is en route to the device already */
371 else if (priv->tx_pending_len > 0)
372 shouldsleep = 0; /* We've a packet to send */
373 else if (priv->resp_len[priv->resp_idx])
374 shouldsleep = 0; /* We have a command response */
375 else if (priv->cur_cmd)
376 shouldsleep = 1; /* Can't send a command; one already running */
377 else if (!list_empty(&priv->cmdpendingq) &&
378 !(priv->wakeup_dev_required))
379 shouldsleep = 0; /* We have a command to send */
380 else if (kfifo_len(&priv->event_fifo))
381 shouldsleep = 0; /* We have an event to process */
382 else
383 shouldsleep = 1; /* No command */
385 if (shouldsleep) {
386 lbs_deb_thread("sleeping, connect_status %d, "
387 "psmode %d, psstate %d\n",
388 priv->connect_status,
389 priv->psmode, priv->psstate);
390 spin_unlock_irq(&priv->driver_lock);
391 schedule();
392 } else
393 spin_unlock_irq(&priv->driver_lock);
395 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
396 priv->currenttxskb, priv->dnld_sent);
398 set_current_state(TASK_RUNNING);
399 remove_wait_queue(&priv->waitq, &wait);
401 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
402 priv->currenttxskb, priv->dnld_sent);
404 if (kthread_should_stop()) {
405 lbs_deb_thread("break from main thread\n");
406 break;
409 if (priv->surpriseremoved) {
410 lbs_deb_thread("adapter removed; waiting to die...\n");
411 continue;
414 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
415 priv->currenttxskb, priv->dnld_sent);
417 /* Process any pending command response */
418 spin_lock_irq(&priv->driver_lock);
419 resp_idx = priv->resp_idx;
420 if (priv->resp_len[resp_idx]) {
421 spin_unlock_irq(&priv->driver_lock);
422 lbs_process_command_response(priv,
423 priv->resp_buf[resp_idx],
424 priv->resp_len[resp_idx]);
425 spin_lock_irq(&priv->driver_lock);
426 priv->resp_len[resp_idx] = 0;
428 spin_unlock_irq(&priv->driver_lock);
430 /* Process hardware events, e.g. card removed, link lost */
431 spin_lock_irq(&priv->driver_lock);
432 while (kfifo_len(&priv->event_fifo)) {
433 u32 event;
435 if (kfifo_out(&priv->event_fifo,
436 (unsigned char *) &event, sizeof(event)) !=
437 sizeof(event))
438 break;
439 spin_unlock_irq(&priv->driver_lock);
440 lbs_process_event(priv, event);
441 spin_lock_irq(&priv->driver_lock);
443 spin_unlock_irq(&priv->driver_lock);
445 if (priv->wakeup_dev_required) {
446 lbs_deb_thread("Waking up device...\n");
447 /* Wake up device */
448 if (priv->exit_deep_sleep(priv))
449 lbs_deb_thread("Wakeup device failed\n");
450 continue;
453 /* command timeout stuff */
454 if (priv->cmd_timed_out && priv->cur_cmd) {
455 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
457 lbs_pr_info("Timeout submitting command 0x%04x\n",
458 le16_to_cpu(cmdnode->cmdbuf->command));
459 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
460 if (priv->reset_card)
461 priv->reset_card(priv);
463 priv->cmd_timed_out = 0;
465 if (!priv->fw_ready)
466 continue;
468 /* Check if we need to confirm Sleep Request received previously */
469 if (priv->psstate == PS_STATE_PRE_SLEEP &&
470 !priv->dnld_sent && !priv->cur_cmd) {
471 if (priv->connect_status == LBS_CONNECTED) {
472 lbs_deb_thread("pre-sleep, currenttxskb %p, "
473 "dnld_sent %d, cur_cmd %p\n",
474 priv->currenttxskb, priv->dnld_sent,
475 priv->cur_cmd);
477 lbs_ps_confirm_sleep(priv);
478 } else {
479 priv->psstate = PS_STATE_AWAKE;
480 lbs_pr_alert("ignore PS_SleepConfirm in "
481 "non-connected state\n");
485 /* The PS state is changed during processing of Sleep Request
486 * event above
488 if ((priv->psstate == PS_STATE_SLEEP) ||
489 (priv->psstate == PS_STATE_PRE_SLEEP))
490 continue;
492 if (priv->is_deep_sleep)
493 continue;
495 /* Execute the next command */
496 if (!priv->dnld_sent && !priv->cur_cmd)
497 lbs_execute_next_command(priv);
499 spin_lock_irq(&priv->driver_lock);
500 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
501 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
502 priv->tx_pending_buf,
503 priv->tx_pending_len);
504 if (ret) {
505 lbs_deb_tx("host_to_card failed %d\n", ret);
506 priv->dnld_sent = DNLD_RES_RECEIVED;
508 priv->tx_pending_len = 0;
509 if (!priv->currenttxskb) {
510 /* We can wake the queues immediately if we aren't
511 waiting for TX feedback */
512 if (priv->connect_status == LBS_CONNECTED)
513 netif_wake_queue(priv->dev);
514 if (priv->mesh_dev &&
515 lbs_mesh_connected(priv))
516 netif_wake_queue(priv->mesh_dev);
519 spin_unlock_irq(&priv->driver_lock);
522 del_timer(&priv->command_timer);
523 del_timer(&priv->auto_deepsleep_timer);
525 lbs_deb_leave(LBS_DEB_THREAD);
526 return 0;
529 int lbs_suspend(struct lbs_private *priv)
531 int ret;
533 lbs_deb_enter(LBS_DEB_FW);
535 if (priv->is_deep_sleep) {
536 ret = lbs_set_deep_sleep(priv, 0);
537 if (ret) {
538 lbs_pr_err("deep sleep cancellation failed: %d\n", ret);
539 return ret;
541 priv->deep_sleep_required = 1;
544 ret = lbs_set_host_sleep(priv, 1);
546 netif_device_detach(priv->dev);
547 if (priv->mesh_dev)
548 netif_device_detach(priv->mesh_dev);
550 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
551 return ret;
553 EXPORT_SYMBOL_GPL(lbs_suspend);
555 int lbs_resume(struct lbs_private *priv)
557 int ret;
559 lbs_deb_enter(LBS_DEB_FW);
561 ret = lbs_set_host_sleep(priv, 0);
563 netif_device_attach(priv->dev);
564 if (priv->mesh_dev)
565 netif_device_attach(priv->mesh_dev);
567 if (priv->deep_sleep_required) {
568 priv->deep_sleep_required = 0;
569 ret = lbs_set_deep_sleep(priv, 1);
570 if (ret)
571 lbs_pr_err("deep sleep activation failed: %d\n", ret);
574 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
575 return ret;
577 EXPORT_SYMBOL_GPL(lbs_resume);
580 * @brief This function gets the HW spec from the firmware and sets
581 * some basic parameters.
583 * @param priv A pointer to struct lbs_private structure
584 * @return 0 or -1
586 static int lbs_setup_firmware(struct lbs_private *priv)
588 int ret = -1;
589 s16 curlevel = 0, minlevel = 0, maxlevel = 0;
591 lbs_deb_enter(LBS_DEB_FW);
593 /* Read MAC address from firmware */
594 memset(priv->current_addr, 0xff, ETH_ALEN);
595 ret = lbs_update_hw_spec(priv);
596 if (ret)
597 goto done;
599 /* Read power levels if available */
600 ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
601 if (ret == 0) {
602 priv->txpower_cur = curlevel;
603 priv->txpower_min = minlevel;
604 priv->txpower_max = maxlevel;
607 /* Send cmd to FW to enable 11D function */
608 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_11D_ENABLE, 1);
610 lbs_set_mac_control(priv);
611 done:
612 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
613 return ret;
617 * This function handles the timeout of command sending.
618 * It will re-send the same command again.
620 static void lbs_cmd_timeout_handler(unsigned long data)
622 struct lbs_private *priv = (struct lbs_private *)data;
623 unsigned long flags;
625 lbs_deb_enter(LBS_DEB_CMD);
626 spin_lock_irqsave(&priv->driver_lock, flags);
628 if (!priv->cur_cmd)
629 goto out;
631 lbs_pr_info("command 0x%04x timed out\n",
632 le16_to_cpu(priv->cur_cmd->cmdbuf->command));
634 priv->cmd_timed_out = 1;
635 wake_up_interruptible(&priv->waitq);
636 out:
637 spin_unlock_irqrestore(&priv->driver_lock, flags);
638 lbs_deb_leave(LBS_DEB_CMD);
642 * This function put the device back to deep sleep mode when timer expires
643 * and no activity (command, event, data etc.) is detected.
645 static void auto_deepsleep_timer_fn(unsigned long data)
647 struct lbs_private *priv = (struct lbs_private *)data;
649 lbs_deb_enter(LBS_DEB_CMD);
651 if (priv->is_activity_detected) {
652 priv->is_activity_detected = 0;
653 } else {
654 if (priv->is_auto_deep_sleep_enabled &&
655 (!priv->wakeup_dev_required) &&
656 (priv->connect_status != LBS_CONNECTED)) {
657 struct cmd_header cmd;
659 lbs_deb_main("Entering auto deep sleep mode...\n");
660 memset(&cmd, 0, sizeof(cmd));
661 cmd.size = cpu_to_le16(sizeof(cmd));
662 lbs_cmd_async(priv, CMD_802_11_DEEP_SLEEP, &cmd,
663 sizeof(cmd));
666 mod_timer(&priv->auto_deepsleep_timer , jiffies +
667 (priv->auto_deep_sleep_timeout * HZ)/1000);
668 lbs_deb_leave(LBS_DEB_CMD);
671 int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
673 lbs_deb_enter(LBS_DEB_SDIO);
675 priv->is_auto_deep_sleep_enabled = 1;
676 if (priv->is_deep_sleep)
677 priv->wakeup_dev_required = 1;
678 mod_timer(&priv->auto_deepsleep_timer ,
679 jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
681 lbs_deb_leave(LBS_DEB_SDIO);
682 return 0;
685 int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
687 lbs_deb_enter(LBS_DEB_SDIO);
689 priv->is_auto_deep_sleep_enabled = 0;
690 priv->auto_deep_sleep_timeout = 0;
691 del_timer(&priv->auto_deepsleep_timer);
693 lbs_deb_leave(LBS_DEB_SDIO);
694 return 0;
697 static int lbs_init_adapter(struct lbs_private *priv)
699 int ret;
701 lbs_deb_enter(LBS_DEB_MAIN);
703 memset(priv->current_addr, 0xff, ETH_ALEN);
705 priv->connect_status = LBS_DISCONNECTED;
706 priv->channel = DEFAULT_AD_HOC_CHANNEL;
707 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
708 priv->radio_on = 1;
709 priv->psmode = LBS802_11POWERMODECAM;
710 priv->psstate = PS_STATE_FULL_POWER;
711 priv->is_deep_sleep = 0;
712 priv->is_auto_deep_sleep_enabled = 0;
713 priv->deep_sleep_required = 0;
714 priv->wakeup_dev_required = 0;
715 init_waitqueue_head(&priv->ds_awake_q);
716 init_waitqueue_head(&priv->scan_q);
717 priv->authtype_auto = 1;
718 priv->is_host_sleep_configured = 0;
719 priv->is_host_sleep_activated = 0;
720 init_waitqueue_head(&priv->host_sleep_q);
721 mutex_init(&priv->lock);
723 setup_timer(&priv->command_timer, lbs_cmd_timeout_handler,
724 (unsigned long)priv);
725 setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
726 (unsigned long)priv);
728 INIT_LIST_HEAD(&priv->cmdfreeq);
729 INIT_LIST_HEAD(&priv->cmdpendingq);
731 spin_lock_init(&priv->driver_lock);
733 /* Allocate the command buffers */
734 if (lbs_allocate_cmd_buffer(priv)) {
735 lbs_pr_err("Out of memory allocating command buffers\n");
736 ret = -ENOMEM;
737 goto out;
739 priv->resp_idx = 0;
740 priv->resp_len[0] = priv->resp_len[1] = 0;
742 /* Create the event FIFO */
743 ret = kfifo_alloc(&priv->event_fifo, sizeof(u32) * 16, GFP_KERNEL);
744 if (ret) {
745 lbs_pr_err("Out of memory allocating event FIFO buffer\n");
746 goto out;
749 out:
750 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
752 return ret;
755 static void lbs_free_adapter(struct lbs_private *priv)
757 lbs_deb_enter(LBS_DEB_MAIN);
759 lbs_free_cmd_buffer(priv);
760 kfifo_free(&priv->event_fifo);
761 del_timer(&priv->command_timer);
762 del_timer(&priv->auto_deepsleep_timer);
764 lbs_deb_leave(LBS_DEB_MAIN);
767 static const struct net_device_ops lbs_netdev_ops = {
768 .ndo_open = lbs_dev_open,
769 .ndo_stop = lbs_eth_stop,
770 .ndo_start_xmit = lbs_hard_start_xmit,
771 .ndo_set_mac_address = lbs_set_mac_address,
772 .ndo_tx_timeout = lbs_tx_timeout,
773 .ndo_set_multicast_list = lbs_set_multicast_list,
774 .ndo_change_mtu = eth_change_mtu,
775 .ndo_validate_addr = eth_validate_addr,
779 * @brief This function adds the card. it will probe the
780 * card, allocate the lbs_priv and initialize the device.
782 * @param card A pointer to card
783 * @return A pointer to struct lbs_private structure
785 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
787 struct net_device *dev;
788 struct wireless_dev *wdev;
789 struct lbs_private *priv = NULL;
791 lbs_deb_enter(LBS_DEB_MAIN);
793 /* Allocate an Ethernet device and register it */
794 wdev = lbs_cfg_alloc(dmdev);
795 if (IS_ERR(wdev)) {
796 lbs_pr_err("cfg80211 init failed\n");
797 goto done;
800 wdev->iftype = NL80211_IFTYPE_STATION;
801 priv = wdev_priv(wdev);
802 priv->wdev = wdev;
804 if (lbs_init_adapter(priv)) {
805 lbs_pr_err("failed to initialize adapter structure.\n");
806 goto err_wdev;
809 dev = alloc_netdev(0, "wlan%d", ether_setup);
810 if (!dev) {
811 dev_err(dmdev, "no memory for network device instance\n");
812 goto err_adapter;
815 dev->ieee80211_ptr = wdev;
816 dev->ml_priv = priv;
817 SET_NETDEV_DEV(dev, dmdev);
818 wdev->netdev = dev;
819 priv->dev = dev;
821 dev->netdev_ops = &lbs_netdev_ops;
822 dev->watchdog_timeo = 5 * HZ;
823 dev->ethtool_ops = &lbs_ethtool_ops;
824 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
826 priv->card = card;
828 strcpy(dev->name, "wlan%d");
830 lbs_deb_thread("Starting main thread...\n");
831 init_waitqueue_head(&priv->waitq);
832 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
833 if (IS_ERR(priv->main_thread)) {
834 lbs_deb_thread("Error creating main thread.\n");
835 goto err_ndev;
838 priv->work_thread = create_singlethread_workqueue("lbs_worker");
839 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
841 priv->wol_criteria = 0xffffffff;
842 priv->wol_gpio = 0xff;
843 priv->wol_gap = 20;
845 goto done;
847 err_ndev:
848 free_netdev(dev);
850 err_adapter:
851 lbs_free_adapter(priv);
853 err_wdev:
854 lbs_cfg_free(priv);
856 priv = NULL;
858 done:
859 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
860 return priv;
862 EXPORT_SYMBOL_GPL(lbs_add_card);
865 void lbs_remove_card(struct lbs_private *priv)
867 struct net_device *dev = priv->dev;
869 lbs_deb_enter(LBS_DEB_MAIN);
871 lbs_remove_mesh(priv);
872 lbs_scan_deinit(priv);
874 dev = priv->dev;
876 cancel_work_sync(&priv->mcast_work);
878 /* worker thread destruction blocks on the in-flight command which
879 * should have been cleared already in lbs_stop_card().
881 lbs_deb_main("destroying worker thread\n");
882 destroy_workqueue(priv->work_thread);
883 lbs_deb_main("done destroying worker thread\n");
885 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
886 priv->psmode = LBS802_11POWERMODECAM;
887 lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, true);
890 if (priv->is_deep_sleep) {
891 priv->is_deep_sleep = 0;
892 wake_up_interruptible(&priv->ds_awake_q);
895 priv->is_host_sleep_configured = 0;
896 priv->is_host_sleep_activated = 0;
897 wake_up_interruptible(&priv->host_sleep_q);
899 /* Stop the thread servicing the interrupts */
900 priv->surpriseremoved = 1;
901 kthread_stop(priv->main_thread);
903 lbs_free_adapter(priv);
904 lbs_cfg_free(priv);
906 priv->dev = NULL;
907 free_netdev(dev);
909 lbs_deb_leave(LBS_DEB_MAIN);
911 EXPORT_SYMBOL_GPL(lbs_remove_card);
914 int lbs_rtap_supported(struct lbs_private *priv)
916 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5)
917 return 1;
919 /* newer firmware use a capability mask */
920 return ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
921 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK));
925 int lbs_start_card(struct lbs_private *priv)
927 struct net_device *dev = priv->dev;
928 int ret = -1;
930 lbs_deb_enter(LBS_DEB_MAIN);
932 /* poke the firmware */
933 ret = lbs_setup_firmware(priv);
934 if (ret)
935 goto done;
937 if (lbs_cfg_register(priv)) {
938 lbs_pr_err("cannot register device\n");
939 goto done;
942 lbs_update_channel(priv);
944 lbs_init_mesh(priv);
946 lbs_debugfs_init_one(priv, dev);
948 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
950 ret = 0;
952 done:
953 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
954 return ret;
956 EXPORT_SYMBOL_GPL(lbs_start_card);
959 void lbs_stop_card(struct lbs_private *priv)
961 struct net_device *dev;
962 struct cmd_ctrl_node *cmdnode;
963 unsigned long flags;
965 lbs_deb_enter(LBS_DEB_MAIN);
967 if (!priv)
968 goto out;
969 dev = priv->dev;
971 netif_stop_queue(dev);
972 netif_carrier_off(dev);
974 lbs_debugfs_remove_one(priv);
975 lbs_deinit_mesh(priv);
977 /* Delete the timeout of the currently processing command */
978 del_timer_sync(&priv->command_timer);
979 del_timer_sync(&priv->auto_deepsleep_timer);
981 /* Flush pending command nodes */
982 spin_lock_irqsave(&priv->driver_lock, flags);
983 lbs_deb_main("clearing pending commands\n");
984 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
985 cmdnode->result = -ENOENT;
986 cmdnode->cmdwaitqwoken = 1;
987 wake_up_interruptible(&cmdnode->cmdwait_q);
990 /* Flush the command the card is currently processing */
991 if (priv->cur_cmd) {
992 lbs_deb_main("clearing current command\n");
993 priv->cur_cmd->result = -ENOENT;
994 priv->cur_cmd->cmdwaitqwoken = 1;
995 wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
997 lbs_deb_main("done clearing commands\n");
998 spin_unlock_irqrestore(&priv->driver_lock, flags);
1000 unregister_netdev(dev);
1002 out:
1003 lbs_deb_leave(LBS_DEB_MAIN);
1005 EXPORT_SYMBOL_GPL(lbs_stop_card);
1008 void lbs_queue_event(struct lbs_private *priv, u32 event)
1010 unsigned long flags;
1012 lbs_deb_enter(LBS_DEB_THREAD);
1013 spin_lock_irqsave(&priv->driver_lock, flags);
1015 if (priv->psstate == PS_STATE_SLEEP)
1016 priv->psstate = PS_STATE_AWAKE;
1018 kfifo_in(&priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1020 wake_up_interruptible(&priv->waitq);
1022 spin_unlock_irqrestore(&priv->driver_lock, flags);
1023 lbs_deb_leave(LBS_DEB_THREAD);
1025 EXPORT_SYMBOL_GPL(lbs_queue_event);
1027 void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
1029 lbs_deb_enter(LBS_DEB_THREAD);
1031 if (priv->psstate == PS_STATE_SLEEP)
1032 priv->psstate = PS_STATE_AWAKE;
1034 /* Swap buffers by flipping the response index */
1035 BUG_ON(resp_idx > 1);
1036 priv->resp_idx = resp_idx;
1038 wake_up_interruptible(&priv->waitq);
1040 lbs_deb_leave(LBS_DEB_THREAD);
1042 EXPORT_SYMBOL_GPL(lbs_notify_command_response);
1044 static int __init lbs_init_module(void)
1046 lbs_deb_enter(LBS_DEB_MAIN);
1047 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1048 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1049 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1050 confirm_sleep.action = cpu_to_le16(PS_MODE_ACTION_SLEEP_CONFIRMED);
1051 lbs_debugfs_init();
1052 lbs_deb_leave(LBS_DEB_MAIN);
1053 return 0;
1056 static void __exit lbs_exit_module(void)
1058 lbs_deb_enter(LBS_DEB_MAIN);
1059 lbs_debugfs_remove();
1060 lbs_deb_leave(LBS_DEB_MAIN);
1063 module_init(lbs_init_module);
1064 module_exit(lbs_exit_module);
1066 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1067 MODULE_AUTHOR("Marvell International Ltd.");
1068 MODULE_LICENSE("GPL");