[PATCH] libertas: move generic firmware reset command to common code
[linux-2.6.git] / drivers / net / wireless / libertas / main.c
blob589c583ddbebda0fcd08b337e1dab317c062db83
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/freezer.h>
10 #include <linux/etherdevice.h>
11 #include <linux/netdevice.h>
12 #include <linux/if_arp.h>
13 #include <linux/kthread.h>
15 #include <net/iw_handler.h>
16 #include <net/ieee80211.h>
18 #include "host.h"
19 #include "decl.h"
20 #include "dev.h"
21 #include "wext.h"
22 #include "debugfs.h"
23 #include "assoc.h"
25 #define DRIVER_RELEASE_VERSION "322.p1"
26 const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
27 #ifdef DEBUG
28 "-dbg"
29 #endif
30 "";
33 /* Module parameters */
34 unsigned int libertas_debug = 0;
35 module_param(libertas_debug, int, 0644);
36 EXPORT_SYMBOL_GPL(libertas_debug);
39 #define WLAN_TX_PWR_DEFAULT 20 /*100mW */
40 #define WLAN_TX_PWR_US_DEFAULT 20 /*100mW */
41 #define WLAN_TX_PWR_JP_DEFAULT 16 /*50mW */
42 #define WLAN_TX_PWR_FR_DEFAULT 20 /*100mW */
43 #define WLAN_TX_PWR_EMEA_DEFAULT 20 /*100mW */
45 /* Format { channel, frequency (MHz), maxtxpower } */
46 /* band: 'B/G', region: USA FCC/Canada IC */
47 static struct chan_freq_power channel_freq_power_US_BG[] = {
48 {1, 2412, WLAN_TX_PWR_US_DEFAULT},
49 {2, 2417, WLAN_TX_PWR_US_DEFAULT},
50 {3, 2422, WLAN_TX_PWR_US_DEFAULT},
51 {4, 2427, WLAN_TX_PWR_US_DEFAULT},
52 {5, 2432, WLAN_TX_PWR_US_DEFAULT},
53 {6, 2437, WLAN_TX_PWR_US_DEFAULT},
54 {7, 2442, WLAN_TX_PWR_US_DEFAULT},
55 {8, 2447, WLAN_TX_PWR_US_DEFAULT},
56 {9, 2452, WLAN_TX_PWR_US_DEFAULT},
57 {10, 2457, WLAN_TX_PWR_US_DEFAULT},
58 {11, 2462, WLAN_TX_PWR_US_DEFAULT}
61 /* band: 'B/G', region: Europe ETSI */
62 static struct chan_freq_power channel_freq_power_EU_BG[] = {
63 {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
64 {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
65 {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
66 {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
67 {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
68 {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
69 {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
70 {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
71 {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
72 {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
73 {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
74 {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
75 {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
78 /* band: 'B/G', region: Spain */
79 static struct chan_freq_power channel_freq_power_SPN_BG[] = {
80 {10, 2457, WLAN_TX_PWR_DEFAULT},
81 {11, 2462, WLAN_TX_PWR_DEFAULT}
84 /* band: 'B/G', region: France */
85 static struct chan_freq_power channel_freq_power_FR_BG[] = {
86 {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
87 {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
88 {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
89 {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
92 /* band: 'B/G', region: Japan */
93 static struct chan_freq_power channel_freq_power_JPN_BG[] = {
94 {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
95 {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
96 {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
97 {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
98 {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
99 {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
100 {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
101 {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
102 {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
103 {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
104 {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
105 {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
106 {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
107 {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
111 * the structure for channel, frequency and power
113 struct region_cfp_table {
114 u8 region;
115 struct chan_freq_power *cfp_BG;
116 int cfp_no_BG;
120 * the structure for the mapping between region and CFP
122 static struct region_cfp_table region_cfp_table[] = {
123 {0x10, /*US FCC */
124 channel_freq_power_US_BG,
125 sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
128 {0x20, /*CANADA IC */
129 channel_freq_power_US_BG,
130 sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
133 {0x30, /*EU*/ channel_freq_power_EU_BG,
134 sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
137 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
138 sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
141 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
142 sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
145 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
146 sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
149 /*Add new region here */
153 * the rates supported
155 u8 libertas_supported_rates[G_SUPPORTED_RATES] =
156 { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
157 0 };
160 * the rates supported for ad-hoc G mode
162 u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
163 { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
164 0 };
167 * the rates supported for ad-hoc B mode
169 u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
172 * the table to keep region code
174 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
175 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
178 * Attributes exported through sysfs
182 * @brief Get function for sysfs attribute anycast_mask
184 static ssize_t libertas_anycast_get(struct device * dev,
185 struct device_attribute *attr, char * buf)
187 struct cmd_ds_mesh_access mesh_access;
189 memset(&mesh_access, 0, sizeof(mesh_access));
190 libertas_prepare_and_send_command(to_net_dev(dev)->priv,
191 CMD_MESH_ACCESS,
192 CMD_ACT_MESH_GET_ANYCAST,
193 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
195 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
199 * @brief Set function for sysfs attribute anycast_mask
201 static ssize_t libertas_anycast_set(struct device * dev,
202 struct device_attribute *attr, const char * buf, size_t count)
204 struct cmd_ds_mesh_access mesh_access;
205 uint32_t datum;
207 memset(&mesh_access, 0, sizeof(mesh_access));
208 sscanf(buf, "%x", &datum);
209 mesh_access.data[0] = cpu_to_le32(datum);
211 libertas_prepare_and_send_command((to_net_dev(dev))->priv,
212 CMD_MESH_ACCESS,
213 CMD_ACT_MESH_SET_ANYCAST,
214 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
215 return strlen(buf);
219 * anycast_mask attribute to be exported per mshX interface
220 * through sysfs (/sys/class/net/mshX/anycast_mask)
222 static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
225 * @brief Check if the device can be open and wait if necessary.
227 * @param dev A pointer to net_device structure
228 * @return 0
230 * For USB adapter, on some systems the device open handler will be
231 * called before FW ready. Use the following flag check and wait
232 * function to work around the issue.
235 static int pre_open_check(struct net_device *dev)
237 wlan_private *priv = (wlan_private *) dev->priv;
238 wlan_adapter *adapter = priv->adapter;
239 int i = 0;
241 while (!adapter->fw_ready && i < 20) {
242 i++;
243 msleep_interruptible(100);
245 if (!adapter->fw_ready) {
246 lbs_pr_err("firmware not ready\n");
247 return -1;
250 return 0;
254 * @brief This function opens the device
256 * @param dev A pointer to net_device structure
257 * @return 0
259 static int wlan_dev_open(struct net_device *dev)
261 wlan_private *priv = (wlan_private *) dev->priv;
262 wlan_adapter *adapter = priv->adapter;
264 lbs_deb_enter(LBS_DEB_NET);
266 priv->open = 1;
268 if (adapter->connect_status == LIBERTAS_CONNECTED) {
269 netif_carrier_on(priv->dev);
270 netif_carrier_on(priv->mesh_dev);
271 } else {
272 netif_carrier_off(priv->dev);
273 netif_carrier_off(priv->mesh_dev);
276 lbs_deb_leave(LBS_DEB_NET);
277 return 0;
280 * @brief This function opens the mshX interface
282 * @param dev A pointer to net_device structure
283 * @return 0
285 static int mesh_open(struct net_device *dev)
287 wlan_private *priv = (wlan_private *) dev->priv ;
289 if (pre_open_check(dev) == -1)
290 return -1;
291 priv->mesh_open = 1 ;
292 netif_wake_queue(priv->mesh_dev);
293 if (priv->infra_open == 0)
294 return wlan_dev_open(priv->dev) ;
295 return 0;
299 * @brief This function opens the ethX interface
301 * @param dev A pointer to net_device structure
302 * @return 0
304 static int wlan_open(struct net_device *dev)
306 wlan_private *priv = (wlan_private *) dev->priv ;
308 if(pre_open_check(dev) == -1)
309 return -1;
310 priv->infra_open = 1 ;
311 netif_wake_queue(priv->dev);
312 if (priv->open == 0)
313 return wlan_dev_open(priv->dev) ;
314 return 0;
317 static int wlan_dev_close(struct net_device *dev)
319 wlan_private *priv = dev->priv;
321 lbs_deb_enter(LBS_DEB_NET);
323 netif_carrier_off(priv->dev);
324 priv->open = 0;
326 lbs_deb_leave(LBS_DEB_NET);
327 return 0;
331 * @brief This function closes the mshX interface
333 * @param dev A pointer to net_device structure
334 * @return 0
336 static int mesh_close(struct net_device *dev)
338 wlan_private *priv = (wlan_private *) (dev->priv);
340 priv->mesh_open = 0;
341 netif_stop_queue(priv->mesh_dev);
342 if (priv->infra_open == 0)
343 return wlan_dev_close(dev);
344 else
345 return 0;
349 * @brief This function closes the ethX interface
351 * @param dev A pointer to net_device structure
352 * @return 0
354 static int wlan_close(struct net_device *dev)
356 wlan_private *priv = (wlan_private *) dev->priv;
358 netif_stop_queue(dev);
359 priv->infra_open = 0;
360 if (priv->mesh_open == 0)
361 return wlan_dev_close(dev);
362 else
363 return 0;
367 static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
369 int ret = 0;
370 wlan_private *priv = dev->priv;
372 lbs_deb_enter(LBS_DEB_NET);
374 if (priv->dnld_sent || priv->adapter->TxLockFlag) {
375 priv->stats.tx_dropped++;
376 goto done;
379 netif_stop_queue(priv->dev);
380 netif_stop_queue(priv->mesh_dev);
382 if (libertas_process_tx(priv, skb) == 0)
383 dev->trans_start = jiffies;
384 done:
385 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
386 return ret;
390 * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
393 static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
395 wlan_private *priv = dev->priv;
396 int ret;
398 lbs_deb_enter(LBS_DEB_MESH);
400 SET_MESH_FRAME(skb);
402 ret = wlan_hard_start_xmit(skb, priv->dev);
403 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
404 return ret;
408 * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
411 static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
413 int ret;
415 lbs_deb_enter(LBS_DEB_NET);
417 UNSET_MESH_FRAME(skb);
419 ret = wlan_hard_start_xmit(skb, dev);
420 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
421 return ret;
424 static void wlan_tx_timeout(struct net_device *dev)
426 wlan_private *priv = (wlan_private *) dev->priv;
428 lbs_deb_enter(LBS_DEB_TX);
430 lbs_pr_err("tx watch dog timeout\n");
432 priv->dnld_sent = DNLD_RES_RECEIVED;
433 dev->trans_start = jiffies;
435 if (priv->adapter->currenttxskb) {
436 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
437 /* If we are here, we have not received feedback from
438 the previous packet. Assume TX_FAIL and move on. */
439 priv->adapter->eventcause = 0x01000000;
440 libertas_send_tx_feedback(priv);
441 } else
442 wake_up_interruptible(&priv->waitq);
443 } else if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
444 netif_wake_queue(priv->dev);
445 netif_wake_queue(priv->mesh_dev);
448 lbs_deb_leave(LBS_DEB_TX);
452 * @brief This function returns the network statistics
454 * @param dev A pointer to wlan_private structure
455 * @return A pointer to net_device_stats structure
457 static struct net_device_stats *wlan_get_stats(struct net_device *dev)
459 wlan_private *priv = (wlan_private *) dev->priv;
461 return &priv->stats;
464 static int wlan_set_mac_address(struct net_device *dev, void *addr)
466 int ret = 0;
467 wlan_private *priv = (wlan_private *) dev->priv;
468 wlan_adapter *adapter = priv->adapter;
469 struct sockaddr *phwaddr = addr;
471 lbs_deb_enter(LBS_DEB_NET);
473 /* In case it was called from the mesh device */
474 dev = priv->dev ;
476 memset(adapter->current_addr, 0, ETH_ALEN);
478 /* dev->dev_addr is 8 bytes */
479 lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
481 lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
482 memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
484 ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
485 CMD_ACT_SET,
486 CMD_OPTION_WAITFORRSP, 0, NULL);
488 if (ret) {
489 lbs_deb_net("set MAC address failed\n");
490 ret = -1;
491 goto done;
494 lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
495 memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
496 if (priv->mesh_dev)
497 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
499 done:
500 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
501 return ret;
504 static int wlan_copy_multicast_address(wlan_adapter * adapter,
505 struct net_device *dev)
507 int i = 0;
508 struct dev_mc_list *mcptr = dev->mc_list;
510 for (i = 0; i < dev->mc_count; i++) {
511 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
512 mcptr = mcptr->next;
515 return i;
519 static void wlan_set_multicast_list(struct net_device *dev)
521 wlan_private *priv = dev->priv;
522 wlan_adapter *adapter = priv->adapter;
523 int oldpacketfilter;
525 lbs_deb_enter(LBS_DEB_NET);
527 oldpacketfilter = adapter->currentpacketfilter;
529 if (dev->flags & IFF_PROMISC) {
530 lbs_deb_net("enable promiscuous mode\n");
531 adapter->currentpacketfilter |=
532 CMD_ACT_MAC_PROMISCUOUS_ENABLE;
533 adapter->currentpacketfilter &=
534 ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
535 CMD_ACT_MAC_MULTICAST_ENABLE);
536 } else {
537 /* Multicast */
538 adapter->currentpacketfilter &=
539 ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
541 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
542 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
543 lbs_deb_net( "enabling all multicast\n");
544 adapter->currentpacketfilter |=
545 CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
546 adapter->currentpacketfilter &=
547 ~CMD_ACT_MAC_MULTICAST_ENABLE;
548 } else {
549 adapter->currentpacketfilter &=
550 ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
552 if (!dev->mc_count) {
553 lbs_deb_net("no multicast addresses, "
554 "disabling multicast\n");
555 adapter->currentpacketfilter &=
556 ~CMD_ACT_MAC_MULTICAST_ENABLE;
557 } else {
558 int i;
560 adapter->currentpacketfilter |=
561 CMD_ACT_MAC_MULTICAST_ENABLE;
563 adapter->nr_of_multicastmacaddr =
564 wlan_copy_multicast_address(adapter, dev);
566 lbs_deb_net("multicast addresses: %d\n",
567 dev->mc_count);
569 for (i = 0; i < dev->mc_count; i++) {
570 lbs_deb_net("Multicast address %d:"
571 MAC_FMT "\n", i,
572 adapter->multicastlist[i][0],
573 adapter->multicastlist[i][1],
574 adapter->multicastlist[i][2],
575 adapter->multicastlist[i][3],
576 adapter->multicastlist[i][4],
577 adapter->multicastlist[i][5]);
579 /* send multicast addresses to firmware */
580 libertas_prepare_and_send_command(priv,
581 CMD_MAC_MULTICAST_ADR,
582 CMD_ACT_SET, 0, 0,
583 NULL);
588 if (adapter->currentpacketfilter != oldpacketfilter) {
589 libertas_set_mac_packet_filter(priv);
592 lbs_deb_leave(LBS_DEB_NET);
596 * @brief This function handles the major jobs in the WLAN driver.
597 * It handles all events generated by firmware, RX data received
598 * from firmware and TX data sent from kernel.
600 * @param data A pointer to wlan_thread structure
601 * @return 0
603 static int libertas_thread(void *data)
605 struct net_device *dev = data;
606 wlan_private *priv = dev->priv;
607 wlan_adapter *adapter = priv->adapter;
608 wait_queue_t wait;
609 u8 ireg = 0;
611 lbs_deb_enter(LBS_DEB_THREAD);
613 init_waitqueue_entry(&wait, current);
615 for (;;) {
616 lbs_deb_thread( "main-thread 111: intcounter=%d "
617 "currenttxskb=%p dnld_sent=%d\n",
618 adapter->intcounter,
619 adapter->currenttxskb, priv->dnld_sent);
621 add_wait_queue(&priv->waitq, &wait);
622 set_current_state(TASK_INTERRUPTIBLE);
623 spin_lock_irq(&adapter->driver_lock);
624 if ((adapter->psstate == PS_STATE_SLEEP) ||
625 (!adapter->intcounter
626 && (priv->dnld_sent || adapter->cur_cmd ||
627 list_empty(&adapter->cmdpendingq)))) {
628 lbs_deb_thread(
629 "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
630 adapter->connect_status, adapter->intcounter,
631 adapter->psmode, adapter->psstate);
632 spin_unlock_irq(&adapter->driver_lock);
633 schedule();
634 } else
635 spin_unlock_irq(&adapter->driver_lock);
638 lbs_deb_thread(
639 "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
640 "dnld_sent=%d\n", adapter->intcounter,
641 adapter->currenttxskb, priv->dnld_sent);
643 set_current_state(TASK_RUNNING);
644 remove_wait_queue(&priv->waitq, &wait);
645 try_to_freeze();
647 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
648 "dnld_sent=%d\n",
649 adapter->intcounter,
650 adapter->currenttxskb, priv->dnld_sent);
652 if (kthread_should_stop()
653 || adapter->surpriseremoved) {
654 lbs_deb_thread(
655 "main-thread: break from main thread: surpriseremoved=0x%x\n",
656 adapter->surpriseremoved);
657 break;
661 spin_lock_irq(&adapter->driver_lock);
662 if (adapter->intcounter) {
663 u8 int_status;
664 adapter->intcounter = 0;
665 int_status = priv->hw_get_int_status(priv, &ireg);
667 if (int_status) {
668 lbs_deb_thread(
669 "main-thread: reading HOST_INT_STATUS_REG failed\n");
670 spin_unlock_irq(&adapter->driver_lock);
671 continue;
673 adapter->hisregcpy |= ireg;
676 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
677 "dnld_sent=%d\n",
678 adapter->intcounter,
679 adapter->currenttxskb, priv->dnld_sent);
681 /* command response? */
682 if (adapter->hisregcpy & his_cmdupldrdy) {
683 lbs_deb_thread("main-thread: cmd response ready\n");
685 adapter->hisregcpy &= ~his_cmdupldrdy;
686 spin_unlock_irq(&adapter->driver_lock);
687 libertas_process_rx_command(priv);
688 spin_lock_irq(&adapter->driver_lock);
691 /* Any Card Event */
692 if (adapter->hisregcpy & his_cardevent) {
693 lbs_deb_thread("main-thread: Card Event Activity\n");
695 adapter->hisregcpy &= ~his_cardevent;
697 if (priv->hw_read_event_cause(priv)) {
698 lbs_pr_alert(
699 "main-thread: hw_read_event_cause failed\n");
700 spin_unlock_irq(&adapter->driver_lock);
701 continue;
703 spin_unlock_irq(&adapter->driver_lock);
704 libertas_process_event(priv);
705 } else
706 spin_unlock_irq(&adapter->driver_lock);
708 /* Check if we need to confirm Sleep Request received previously */
709 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
710 if (!priv->dnld_sent && !adapter->cur_cmd) {
711 if (adapter->connect_status ==
712 LIBERTAS_CONNECTED) {
713 lbs_deb_thread(
714 "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
715 "dnld_sent=%d cur_cmd=%p, confirm now\n",
716 adapter->intcounter,
717 adapter->currenttxskb,
718 priv->dnld_sent,
719 adapter->cur_cmd);
721 libertas_ps_confirm_sleep(priv,
722 (u16) adapter->psmode);
723 } else {
724 /* workaround for firmware sending
725 * deauth/linkloss event immediately
726 * after sleep request, remove this
727 * after firmware fixes it
729 adapter->psstate = PS_STATE_AWAKE;
730 lbs_pr_alert(
731 "main-thread: ignore PS_SleepConfirm in non-connected state\n");
736 /* The PS state is changed during processing of Sleep Request
737 * event above
739 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
740 (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
741 continue;
743 /* Execute the next command */
744 if (!priv->dnld_sent && !priv->adapter->cur_cmd)
745 libertas_execute_next_command(priv);
747 /* Wake-up command waiters which can't sleep in
748 * libertas_prepare_and_send_command
750 if (!adapter->nr_cmd_pending)
751 wake_up_all(&adapter->cmd_pending);
753 libertas_tx_runqueue(priv);
756 del_timer(&adapter->command_timer);
757 adapter->nr_cmd_pending = 0;
758 wake_up_all(&adapter->cmd_pending);
760 lbs_deb_leave(LBS_DEB_THREAD);
761 return 0;
765 * @brief This function adds the card. it will probe the
766 * card, allocate the wlan_priv and initialize the device.
768 * @param card A pointer to card
769 * @return A pointer to wlan_private structure
771 wlan_private *libertas_add_card(void *card, struct device *dmdev)
773 struct net_device *dev = NULL;
774 wlan_private *priv = NULL;
776 lbs_deb_enter(LBS_DEB_NET);
778 /* Allocate an Ethernet device and register it */
779 if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
780 lbs_pr_err("init ethX device failed\n");
781 return NULL;
783 priv = dev->priv;
785 /* allocate buffer for wlan_adapter */
786 if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
787 lbs_pr_err("allocate buffer for wlan_adapter failed\n");
788 goto err_kzalloc;
791 priv->dev = dev;
792 priv->card = card;
793 priv->mesh_open = 0;
794 priv->infra_open = 0;
796 SET_MODULE_OWNER(dev);
798 /* Setup the OS Interface to our functions */
799 dev->open = wlan_open;
800 dev->hard_start_xmit = wlan_pre_start_xmit;
801 dev->stop = wlan_close;
802 dev->set_mac_address = wlan_set_mac_address;
803 dev->tx_timeout = wlan_tx_timeout;
804 dev->get_stats = wlan_get_stats;
805 dev->watchdog_timeo = 5 * HZ;
806 dev->ethtool_ops = &libertas_ethtool_ops;
807 #ifdef WIRELESS_EXT
808 dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
809 #endif
810 #define NETIF_F_DYNALLOC 16
811 dev->features |= NETIF_F_DYNALLOC;
812 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
813 dev->set_multicast_list = wlan_set_multicast_list;
815 SET_NETDEV_DEV(dev, dmdev);
817 INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
818 INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
820 spin_lock_init(&priv->adapter->driver_lock);
821 init_waitqueue_head(&priv->adapter->cmd_pending);
822 priv->adapter->nr_cmd_pending = 0;
823 goto done;
825 err_kzalloc:
826 free_netdev(dev);
827 priv = NULL;
828 done:
829 lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
830 return priv;
832 EXPORT_SYMBOL_GPL(libertas_add_card);
834 int libertas_activate_card(wlan_private *priv, char *fw_name)
836 struct net_device *dev = priv->dev;
837 int ret = -1;
839 lbs_deb_enter(LBS_DEB_MAIN);
841 lbs_deb_thread("Starting main thread...\n");
842 init_waitqueue_head(&priv->waitq);
843 priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
844 if (IS_ERR(priv->main_thread)) {
845 lbs_deb_thread("Error creating main thread.\n");
846 goto done;
849 priv->assoc_thread =
850 create_singlethread_workqueue("libertas_assoc");
851 INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
852 INIT_WORK(&priv->sync_channel, libertas_sync_channel);
855 * Register the device. Fillup the private data structure with
856 * relevant information from the card and request for the required
857 * IRQ.
859 if (priv->hw_register_dev(priv) < 0) {
860 lbs_pr_err("failed to register WLAN device\n");
861 goto err_registerdev;
864 /* init FW and HW */
865 if (fw_name && libertas_init_fw(priv, fw_name)) {
866 lbs_pr_err("firmware init failed\n");
867 goto err_registerdev;
870 if (register_netdev(dev)) {
871 lbs_pr_err("cannot register ethX device\n");
872 goto err_init_fw;
875 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
877 libertas_debugfs_init_one(priv, dev);
879 ret = 0;
880 goto done;
882 err_init_fw:
883 priv->hw_unregister_dev(priv);
884 err_registerdev:
885 destroy_workqueue(priv->assoc_thread);
886 /* Stop the thread servicing the interrupts */
887 wake_up_interruptible(&priv->waitq);
888 kthread_stop(priv->main_thread);
889 done:
890 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
891 return ret;
893 EXPORT_SYMBOL_GPL(libertas_activate_card);
897 * @brief This function adds mshX interface
899 * @param priv A pointer to the wlan_private structure
900 * @return 0 if successful, -X otherwise
902 int libertas_add_mesh(wlan_private *priv, struct device *dev)
904 struct net_device *mesh_dev = NULL;
905 int ret = 0;
907 lbs_deb_enter(LBS_DEB_MESH);
909 /* Allocate a virtual mesh device */
910 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
911 lbs_deb_mesh("init mshX device failed\n");
912 ret = -ENOMEM;
913 goto done;
915 mesh_dev->priv = priv;
916 priv->mesh_dev = mesh_dev;
918 SET_MODULE_OWNER(mesh_dev);
920 mesh_dev->open = mesh_open;
921 mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
922 mesh_dev->stop = mesh_close;
923 mesh_dev->get_stats = wlan_get_stats;
924 mesh_dev->set_mac_address = wlan_set_mac_address;
925 mesh_dev->ethtool_ops = &libertas_ethtool_ops;
926 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
927 sizeof(priv->dev->dev_addr));
929 SET_NETDEV_DEV(priv->mesh_dev, dev);
931 #ifdef WIRELESS_EXT
932 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
933 #endif
934 #define NETIF_F_DYNALLOC 16
936 /* Register virtual mesh interface */
937 ret = register_netdev(mesh_dev);
938 if (ret) {
939 lbs_pr_err("cannot register mshX virtual interface\n");
940 goto err_free;
943 ret = device_create_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
944 if (ret)
945 goto err_unregister;
947 /* Everything successful */
948 ret = 0;
949 goto done;
952 err_unregister:
953 unregister_netdev(mesh_dev);
955 err_free:
956 free_netdev(mesh_dev);
958 done:
959 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
960 return ret;
962 EXPORT_SYMBOL_GPL(libertas_add_mesh);
964 static void wake_pending_cmdnodes(wlan_private *priv)
966 struct cmd_ctrl_node *cmdnode;
967 unsigned long flags;
969 lbs_deb_enter(LBS_DEB_CMD);
971 spin_lock_irqsave(&priv->adapter->driver_lock, flags);
972 list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
973 cmdnode->cmdwaitqwoken = 1;
974 wake_up_interruptible(&cmdnode->cmdwait_q);
976 spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
980 int libertas_remove_card(wlan_private *priv)
982 wlan_adapter *adapter;
983 struct net_device *dev;
984 union iwreq_data wrqu;
986 lbs_deb_enter(LBS_DEB_NET);
988 if (!priv)
989 goto out;
991 adapter = priv->adapter;
993 if (!adapter)
994 goto out;
996 dev = priv->dev;
998 netif_stop_queue(priv->dev);
999 netif_carrier_off(priv->dev);
1001 wake_pending_cmdnodes(priv);
1003 unregister_netdev(dev);
1005 cancel_delayed_work(&priv->assoc_work);
1006 destroy_workqueue(priv->assoc_thread);
1008 if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
1009 adapter->psmode = WLAN802_11POWERMODECAM;
1010 libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1013 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1014 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1015 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1017 adapter->surpriseremoved = 1;
1019 /* Stop the thread servicing the interrupts */
1020 kthread_stop(priv->main_thread);
1022 libertas_debugfs_remove_one(priv);
1024 lbs_deb_net("free adapter\n");
1025 libertas_free_adapter(priv);
1027 lbs_deb_net("unregister finish\n");
1029 priv->dev = NULL;
1030 free_netdev(dev);
1032 out:
1033 lbs_deb_leave(LBS_DEB_NET);
1034 return 0;
1036 EXPORT_SYMBOL_GPL(libertas_remove_card);
1039 void libertas_remove_mesh(wlan_private *priv)
1041 struct net_device *mesh_dev;
1043 lbs_deb_enter(LBS_DEB_NET);
1045 if (!priv)
1046 goto out;
1048 mesh_dev = priv->mesh_dev;
1050 netif_stop_queue(mesh_dev);
1051 netif_carrier_off(priv->mesh_dev);
1053 device_remove_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
1054 unregister_netdev(mesh_dev);
1056 priv->mesh_dev = NULL ;
1057 free_netdev(mesh_dev);
1059 out:
1060 lbs_deb_leave(LBS_DEB_NET);
1062 EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1065 * @brief This function finds the CFP in
1066 * region_cfp_table based on region and band parameter.
1068 * @param region The region code
1069 * @param band The band
1070 * @param cfp_no A pointer to CFP number
1071 * @return A pointer to CFP
1073 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1075 int i, end;
1077 lbs_deb_enter(LBS_DEB_MAIN);
1079 end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1081 for (i = 0; i < end ; i++) {
1082 lbs_deb_main("region_cfp_table[i].region=%d\n",
1083 region_cfp_table[i].region);
1084 if (region_cfp_table[i].region == region) {
1085 *cfp_no = region_cfp_table[i].cfp_no_BG;
1086 lbs_deb_leave(LBS_DEB_MAIN);
1087 return region_cfp_table[i].cfp_BG;
1091 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1092 return NULL;
1095 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1097 wlan_adapter *adapter = priv->adapter;
1098 int ret = 0;
1099 int i = 0;
1101 struct chan_freq_power *cfp;
1102 int cfp_no;
1104 lbs_deb_enter(LBS_DEB_MAIN);
1106 memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1109 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1110 if (cfp != NULL) {
1111 adapter->region_channel[i].nrcfp = cfp_no;
1112 adapter->region_channel[i].CFP = cfp;
1113 } else {
1114 lbs_deb_main("wrong region code %#x in band B/G\n",
1115 region);
1116 ret = -1;
1117 goto out;
1119 adapter->region_channel[i].valid = 1;
1120 adapter->region_channel[i].region = region;
1121 adapter->region_channel[i].band = band;
1122 i++;
1124 out:
1125 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1126 return ret;
1130 * @brief This function handles the interrupt. it will change PS
1131 * state if applicable. it will wake up main_thread to handle
1132 * the interrupt event as well.
1134 * @param dev A pointer to net_device structure
1135 * @return n/a
1137 void libertas_interrupt(struct net_device *dev)
1139 wlan_private *priv = dev->priv;
1141 lbs_deb_enter(LBS_DEB_THREAD);
1143 lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1144 priv->adapter->intcounter);
1146 priv->adapter->intcounter++;
1148 if (priv->adapter->psstate == PS_STATE_SLEEP) {
1149 priv->adapter->psstate = PS_STATE_AWAKE;
1150 netif_wake_queue(dev);
1151 netif_wake_queue(priv->mesh_dev);
1154 wake_up_interruptible(&priv->waitq);
1156 lbs_deb_leave(LBS_DEB_THREAD);
1158 EXPORT_SYMBOL_GPL(libertas_interrupt);
1160 int libertas_reset_device(wlan_private *priv)
1162 int ret;
1164 lbs_deb_enter(LBS_DEB_MAIN);
1165 ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
1166 CMD_ACT_HALT, 0, 0, NULL);
1167 msleep_interruptible(10);
1169 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1170 return ret;
1172 EXPORT_SYMBOL_GPL(libertas_reset_device);
1174 static int libertas_init_module(void)
1176 lbs_deb_enter(LBS_DEB_MAIN);
1177 libertas_debugfs_init();
1178 lbs_deb_leave(LBS_DEB_MAIN);
1179 return 0;
1182 static void libertas_exit_module(void)
1184 lbs_deb_enter(LBS_DEB_MAIN);
1186 libertas_debugfs_remove();
1188 lbs_deb_leave(LBS_DEB_MAIN);
1191 module_init(libertas_init_module);
1192 module_exit(libertas_exit_module);
1194 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1195 MODULE_AUTHOR("Marvell International Ltd.");
1196 MODULE_LICENSE("GPL");