2 * This file contains the major functions in WLAN
3 * driver. It includes init, exit, open, close and main
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>
26 #define DRIVER_RELEASE_VERSION "323.p0"
27 const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
34 /* Module parameters */
35 unsigned int lbs_debug;
36 EXPORT_SYMBOL_GPL(lbs_debug);
37 module_param_named(libertas_debug, lbs_debug, int, 0644);
40 #define LBS_TX_PWR_DEFAULT 20 /*100mW */
41 #define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
42 #define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
43 #define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
44 #define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
46 /* Format { channel, frequency (MHz), maxtxpower } */
47 /* band: 'B/G', region: USA FCC/Canada IC */
48 static struct chan_freq_power channel_freq_power_US_BG[] = {
49 {1, 2412, LBS_TX_PWR_US_DEFAULT},
50 {2, 2417, LBS_TX_PWR_US_DEFAULT},
51 {3, 2422, LBS_TX_PWR_US_DEFAULT},
52 {4, 2427, LBS_TX_PWR_US_DEFAULT},
53 {5, 2432, LBS_TX_PWR_US_DEFAULT},
54 {6, 2437, LBS_TX_PWR_US_DEFAULT},
55 {7, 2442, LBS_TX_PWR_US_DEFAULT},
56 {8, 2447, LBS_TX_PWR_US_DEFAULT},
57 {9, 2452, LBS_TX_PWR_US_DEFAULT},
58 {10, 2457, LBS_TX_PWR_US_DEFAULT},
59 {11, 2462, LBS_TX_PWR_US_DEFAULT}
62 /* band: 'B/G', region: Europe ETSI */
63 static struct chan_freq_power channel_freq_power_EU_BG[] = {
64 {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
65 {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
66 {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
67 {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
68 {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
69 {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
70 {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
71 {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
72 {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
73 {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
74 {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
75 {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
76 {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
79 /* band: 'B/G', region: Spain */
80 static struct chan_freq_power channel_freq_power_SPN_BG[] = {
81 {10, 2457, LBS_TX_PWR_DEFAULT},
82 {11, 2462, LBS_TX_PWR_DEFAULT}
85 /* band: 'B/G', region: France */
86 static struct chan_freq_power channel_freq_power_FR_BG[] = {
87 {10, 2457, LBS_TX_PWR_FR_DEFAULT},
88 {11, 2462, LBS_TX_PWR_FR_DEFAULT},
89 {12, 2467, LBS_TX_PWR_FR_DEFAULT},
90 {13, 2472, LBS_TX_PWR_FR_DEFAULT}
93 /* band: 'B/G', region: Japan */
94 static struct chan_freq_power channel_freq_power_JPN_BG[] = {
95 {1, 2412, LBS_TX_PWR_JP_DEFAULT},
96 {2, 2417, LBS_TX_PWR_JP_DEFAULT},
97 {3, 2422, LBS_TX_PWR_JP_DEFAULT},
98 {4, 2427, LBS_TX_PWR_JP_DEFAULT},
99 {5, 2432, LBS_TX_PWR_JP_DEFAULT},
100 {6, 2437, LBS_TX_PWR_JP_DEFAULT},
101 {7, 2442, LBS_TX_PWR_JP_DEFAULT},
102 {8, 2447, LBS_TX_PWR_JP_DEFAULT},
103 {9, 2452, LBS_TX_PWR_JP_DEFAULT},
104 {10, 2457, LBS_TX_PWR_JP_DEFAULT},
105 {11, 2462, LBS_TX_PWR_JP_DEFAULT},
106 {12, 2467, LBS_TX_PWR_JP_DEFAULT},
107 {13, 2472, LBS_TX_PWR_JP_DEFAULT},
108 {14, 2484, LBS_TX_PWR_JP_DEFAULT}
112 * the structure for channel, frequency and power
114 struct region_cfp_table {
116 struct chan_freq_power *cfp_BG;
121 * the structure for the mapping between region and CFP
123 static struct region_cfp_table region_cfp_table[] = {
125 channel_freq_power_US_BG,
126 ARRAY_SIZE(channel_freq_power_US_BG),
129 {0x20, /*CANADA IC */
130 channel_freq_power_US_BG,
131 ARRAY_SIZE(channel_freq_power_US_BG),
134 {0x30, /*EU*/ channel_freq_power_EU_BG,
135 ARRAY_SIZE(channel_freq_power_EU_BG),
138 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
139 ARRAY_SIZE(channel_freq_power_SPN_BG),
142 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
143 ARRAY_SIZE(channel_freq_power_FR_BG),
146 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
147 ARRAY_SIZE(channel_freq_power_JPN_BG),
150 /*Add new region here */
154 * the table to keep region code
156 u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
157 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
160 * 802.11b/g supported bitrates (in 500Kb/s units)
162 u8 lbs_bg_rates[MAX_RATES] =
163 { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
167 * FW rate table. FW refers to rates by their index in this table, not by the
168 * rate value itself. Values of 0x00 are
169 * reserved positions.
171 static u8 fw_data_rates[MAX_RATES] =
172 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
173 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
177 * @brief use index to get the data rate
179 * @param idx The index of data rate
180 * @return data rate or 0
182 u32 lbs_fw_index_to_data_rate(u8 idx)
184 if (idx >= sizeof(fw_data_rates))
186 return fw_data_rates[idx];
190 * @brief use rate to get the index
192 * @param rate data rate
195 u8 lbs_data_rate_to_fw_index(u32 rate)
202 for (i = 0; i < sizeof(fw_data_rates); i++) {
203 if (rate == fw_data_rates[i])
210 * Attributes exported through sysfs
214 * @brief Get function for sysfs attribute anycast_mask
216 static ssize_t lbs_anycast_get(struct device *dev,
217 struct device_attribute *attr, char * buf)
219 struct cmd_ds_mesh_access mesh_access;
221 memset(&mesh_access, 0, sizeof(mesh_access));
222 lbs_prepare_and_send_command(to_net_dev(dev)->priv,
224 CMD_ACT_MESH_GET_ANYCAST,
225 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
227 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
231 * @brief Set function for sysfs attribute anycast_mask
233 static ssize_t lbs_anycast_set(struct device *dev,
234 struct device_attribute *attr, const char * buf, size_t count)
236 struct cmd_ds_mesh_access mesh_access;
239 memset(&mesh_access, 0, sizeof(mesh_access));
240 sscanf(buf, "%x", &datum);
241 mesh_access.data[0] = cpu_to_le32(datum);
243 lbs_prepare_and_send_command((to_net_dev(dev))->priv,
245 CMD_ACT_MESH_SET_ANYCAST,
246 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
250 int lbs_add_rtap(struct lbs_private *priv);
251 void lbs_remove_rtap(struct lbs_private *priv);
254 * Get function for sysfs attribute rtap
256 static ssize_t lbs_rtap_get(struct device *dev,
257 struct device_attribute *attr, char * buf)
259 struct lbs_private *priv = to_net_dev(dev)->priv;
260 return snprintf(buf, 5, "0x%X\n", priv->monitormode);
264 * Set function for sysfs attribute rtap
266 static ssize_t lbs_rtap_set(struct device *dev,
267 struct device_attribute *attr, const char * buf, size_t count)
270 struct lbs_private *priv = to_net_dev(dev)->priv;
272 sscanf(buf, "%x", &monitor_mode);
273 if (monitor_mode != LBS_MONITOR_OFF) {
274 if(priv->monitormode == monitor_mode)
276 if (priv->monitormode == LBS_MONITOR_OFF) {
277 if (priv->mode == IW_MODE_INFRA)
278 lbs_send_deauthentication(priv);
279 else if (priv->mode == IW_MODE_ADHOC)
280 lbs_stop_adhoc_network(priv);
283 priv->monitormode = monitor_mode;
287 if (priv->monitormode == LBS_MONITOR_OFF)
289 priv->monitormode = LBS_MONITOR_OFF;
290 lbs_remove_rtap(priv);
292 if (priv->currenttxskb) {
293 dev_kfree_skb_any(priv->currenttxskb);
294 priv->currenttxskb = NULL;
297 /* Wake queues, command thread, etc. */
298 lbs_host_to_card_done(priv);
301 lbs_prepare_and_send_command(priv,
302 CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
303 CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
308 * lbs_rtap attribute to be exported per mshX interface
309 * through sysfs (/sys/class/net/mshX/libertas-rtap)
311 static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get,
315 * anycast_mask attribute to be exported per mshX interface
316 * through sysfs (/sys/class/net/mshX/anycast_mask)
318 static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
320 static ssize_t lbs_autostart_enabled_get(struct device *dev,
321 struct device_attribute *attr, char * buf)
323 struct cmd_ds_mesh_access mesh_access;
325 memset(&mesh_access, 0, sizeof(mesh_access));
326 lbs_prepare_and_send_command(to_net_dev(dev)->priv,
328 CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
329 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
331 return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
334 static ssize_t lbs_autostart_enabled_set(struct device *dev,
335 struct device_attribute *attr, const char * buf, size_t count)
337 struct cmd_ds_mesh_access mesh_access;
339 struct lbs_private *priv = (to_net_dev(dev))->priv;
342 memset(&mesh_access, 0, sizeof(mesh_access));
343 sscanf(buf, "%d", &datum);
344 mesh_access.data[0] = cpu_to_le32(datum);
346 ret = lbs_prepare_and_send_command(priv,
348 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
349 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
351 priv->mesh_autostart_enabled = datum ? 1 : 0;
356 static DEVICE_ATTR(autostart_enabled, 0644,
357 lbs_autostart_enabled_get, lbs_autostart_enabled_set);
359 static struct attribute *lbs_mesh_sysfs_entries[] = {
360 &dev_attr_anycast_mask.attr,
361 &dev_attr_autostart_enabled.attr,
365 static struct attribute_group lbs_mesh_attr_group = {
366 .attrs = lbs_mesh_sysfs_entries,
370 * @brief Check if the device can be open and wait if necessary.
372 * @param dev A pointer to net_device structure
375 * For USB adapter, on some systems the device open handler will be
376 * called before FW ready. Use the following flag check and wait
377 * function to work around the issue.
380 static int pre_open_check(struct net_device *dev)
382 struct lbs_private *priv = (struct lbs_private *) dev->priv;
385 while (!priv->fw_ready && i < 20) {
387 msleep_interruptible(100);
389 if (!priv->fw_ready) {
390 lbs_pr_err("firmware not ready\n");
398 * @brief This function opens the device
400 * @param dev A pointer to net_device structure
403 static int lbs_dev_open(struct net_device *dev)
405 struct lbs_private *priv = (struct lbs_private *) dev->priv;
407 lbs_deb_enter(LBS_DEB_NET);
411 if (priv->connect_status == LBS_CONNECTED)
412 netif_carrier_on(priv->dev);
414 netif_carrier_off(priv->dev);
416 if (priv->mesh_dev) {
417 if (priv->mesh_connect_status == LBS_CONNECTED)
418 netif_carrier_on(priv->mesh_dev);
420 netif_carrier_off(priv->mesh_dev);
423 lbs_deb_leave(LBS_DEB_NET);
427 * @brief This function opens the mshX interface
429 * @param dev A pointer to net_device structure
432 static int lbs_mesh_open(struct net_device *dev)
434 struct lbs_private *priv = (struct lbs_private *) dev->priv ;
436 if (pre_open_check(dev) == -1)
438 priv->mesh_open = 1 ;
439 netif_wake_queue(priv->mesh_dev);
441 priv->mesh_connect_status = LBS_CONNECTED;
443 netif_carrier_on(priv->mesh_dev);
444 netif_wake_queue(priv->mesh_dev);
445 if (priv->infra_open == 0)
446 return lbs_dev_open(priv->dev) ;
451 * @brief This function opens the ethX interface
453 * @param dev A pointer to net_device structure
456 static int lbs_open(struct net_device *dev)
458 struct lbs_private *priv = (struct lbs_private *) dev->priv ;
460 if(pre_open_check(dev) == -1)
462 priv->infra_open = 1 ;
463 netif_wake_queue(priv->dev);
465 return lbs_dev_open(priv->dev) ;
469 static int lbs_dev_close(struct net_device *dev)
471 struct lbs_private *priv = dev->priv;
473 lbs_deb_enter(LBS_DEB_NET);
475 netif_carrier_off(priv->dev);
478 lbs_deb_leave(LBS_DEB_NET);
483 * @brief This function closes the mshX interface
485 * @param dev A pointer to net_device structure
488 static int lbs_mesh_close(struct net_device *dev)
490 struct lbs_private *priv = (struct lbs_private *) (dev->priv);
493 netif_stop_queue(priv->mesh_dev);
494 if (priv->infra_open == 0)
495 return lbs_dev_close(dev);
501 * @brief This function closes the ethX interface
503 * @param dev A pointer to net_device structure
506 static int lbs_close(struct net_device *dev)
508 struct lbs_private *priv = (struct lbs_private *) dev->priv;
510 netif_stop_queue(dev);
511 priv->infra_open = 0;
512 if (priv->mesh_open == 0)
513 return lbs_dev_close(dev);
519 static int lbs_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
522 struct lbs_private *priv = dev->priv;
524 lbs_deb_enter(LBS_DEB_TX);
526 /* We could return NETDEV_TX_BUSY here, but I'd actually
527 like to get the point where we can BUG() */
528 if (priv->dnld_sent) {
529 lbs_pr_err("%s while dnld_sent\n", __func__);
530 priv->stats.tx_dropped++;
533 if (priv->currenttxskb) {
534 lbs_pr_err("%s while TX skb pending\n", __func__);
535 priv->stats.tx_dropped++;
539 netif_stop_queue(priv->dev);
541 netif_stop_queue(priv->mesh_dev);
543 if (lbs_process_tx(priv, skb) == 0)
544 dev->trans_start = jiffies;
546 lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
551 * @brief Mark mesh packets and handover them to lbs_hard_start_xmit
554 static int lbs_mesh_pre_start_xmit(struct sk_buff *skb,
555 struct net_device *dev)
557 struct lbs_private *priv = dev->priv;
560 lbs_deb_enter(LBS_DEB_MESH);
561 if (priv->monitormode != LBS_MONITOR_OFF) {
562 netif_stop_queue(dev);
568 ret = lbs_hard_start_xmit(skb, priv->mesh_dev);
569 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
574 * @brief Mark non-mesh packets and handover them to lbs_hard_start_xmit
577 static int lbs_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
579 struct lbs_private *priv = dev->priv;
582 lbs_deb_enter(LBS_DEB_TX);
584 if (priv->monitormode != LBS_MONITOR_OFF) {
585 netif_stop_queue(dev);
589 UNSET_MESH_FRAME(skb);
591 ret = lbs_hard_start_xmit(skb, dev);
592 lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
596 static void lbs_tx_timeout(struct net_device *dev)
598 struct lbs_private *priv = (struct lbs_private *) dev->priv;
600 lbs_deb_enter(LBS_DEB_TX);
602 lbs_pr_err("tx watch dog timeout\n");
604 dev->trans_start = jiffies;
606 if (priv->currenttxskb) {
607 priv->eventcause = 0x01000000;
608 lbs_send_tx_feedback(priv);
610 /* XX: Shouldn't we also call into the hw-specific driver
611 to kick it somehow? */
612 lbs_host_to_card_done(priv);
614 lbs_deb_leave(LBS_DEB_TX);
617 void lbs_host_to_card_done(struct lbs_private *priv)
621 spin_lock_irqsave(&priv->driver_lock, flags);
623 priv->dnld_sent = DNLD_RES_RECEIVED;
625 /* Wake main thread if commands are pending */
627 wake_up_interruptible(&priv->waitq);
629 /* Don't wake netif queues if we're in monitor mode and
630 a TX packet is already pending, or if there are commands
631 queued to be sent. */
632 if (!priv->currenttxskb && list_empty(&priv->cmdpendingq)) {
633 if (priv->dev && priv->connect_status == LBS_CONNECTED)
634 netif_wake_queue(priv->dev);
636 if (priv->mesh_dev && priv->mesh_connect_status == LBS_CONNECTED)
637 netif_wake_queue(priv->mesh_dev);
639 spin_unlock_irqrestore(&priv->driver_lock, flags);
641 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
644 * @brief This function returns the network statistics
646 * @param dev A pointer to struct lbs_private structure
647 * @return A pointer to net_device_stats structure
649 static struct net_device_stats *lbs_get_stats(struct net_device *dev)
651 struct lbs_private *priv = (struct lbs_private *) dev->priv;
656 static int lbs_set_mac_address(struct net_device *dev, void *addr)
659 struct lbs_private *priv = (struct lbs_private *) dev->priv;
660 struct sockaddr *phwaddr = addr;
662 lbs_deb_enter(LBS_DEB_NET);
664 /* In case it was called from the mesh device */
667 memset(priv->current_addr, 0, ETH_ALEN);
669 /* dev->dev_addr is 8 bytes */
670 lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
672 lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
673 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
675 ret = lbs_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
677 CMD_OPTION_WAITFORRSP, 0, NULL);
680 lbs_deb_net("set MAC address failed\n");
685 lbs_deb_hex(LBS_DEB_NET, "priv->macaddr", priv->current_addr, ETH_ALEN);
686 memcpy(dev->dev_addr, priv->current_addr, ETH_ALEN);
688 memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
691 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
695 static int lbs_copy_multicast_address(struct lbs_private *priv,
696 struct net_device *dev)
699 struct dev_mc_list *mcptr = dev->mc_list;
701 for (i = 0; i < dev->mc_count; i++) {
702 memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
710 static void lbs_set_multicast_list(struct net_device *dev)
712 struct lbs_private *priv = dev->priv;
714 DECLARE_MAC_BUF(mac);
716 lbs_deb_enter(LBS_DEB_NET);
718 oldpacketfilter = priv->currentpacketfilter;
720 if (dev->flags & IFF_PROMISC) {
721 lbs_deb_net("enable promiscuous mode\n");
722 priv->currentpacketfilter |=
723 CMD_ACT_MAC_PROMISCUOUS_ENABLE;
724 priv->currentpacketfilter &=
725 ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
726 CMD_ACT_MAC_MULTICAST_ENABLE);
729 priv->currentpacketfilter &=
730 ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
732 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
733 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
734 lbs_deb_net( "enabling all multicast\n");
735 priv->currentpacketfilter |=
736 CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
737 priv->currentpacketfilter &=
738 ~CMD_ACT_MAC_MULTICAST_ENABLE;
740 priv->currentpacketfilter &=
741 ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
743 if (!dev->mc_count) {
744 lbs_deb_net("no multicast addresses, "
745 "disabling multicast\n");
746 priv->currentpacketfilter &=
747 ~CMD_ACT_MAC_MULTICAST_ENABLE;
751 priv->currentpacketfilter |=
752 CMD_ACT_MAC_MULTICAST_ENABLE;
754 priv->nr_of_multicastmacaddr =
755 lbs_copy_multicast_address(priv, dev);
757 lbs_deb_net("multicast addresses: %d\n",
760 for (i = 0; i < dev->mc_count; i++) {
761 lbs_deb_net("Multicast address %d:%s\n",
763 priv->multicastlist[i]));
765 /* send multicast addresses to firmware */
766 lbs_prepare_and_send_command(priv,
767 CMD_MAC_MULTICAST_ADR,
774 if (priv->currentpacketfilter != oldpacketfilter) {
775 lbs_set_mac_packet_filter(priv);
778 lbs_deb_leave(LBS_DEB_NET);
782 * @brief This function handles the major jobs in the LBS driver.
783 * It handles all events generated by firmware, RX data received
784 * from firmware and TX data sent from kernel.
786 * @param data A pointer to lbs_thread structure
789 static int lbs_thread(void *data)
791 struct net_device *dev = data;
792 struct lbs_private *priv = dev->priv;
796 lbs_deb_enter(LBS_DEB_THREAD);
798 init_waitqueue_entry(&wait, current);
803 lbs_deb_thread( "main-thread 111: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
804 priv->intcounter, priv->currenttxskb, priv->dnld_sent);
806 add_wait_queue(&priv->waitq, &wait);
807 set_current_state(TASK_INTERRUPTIBLE);
808 spin_lock_irq(&priv->driver_lock);
810 if ((priv->psstate == PS_STATE_SLEEP) ||
811 (!priv->intcounter && (priv->dnld_sent || priv->cur_cmd || list_empty(&priv->cmdpendingq)))) {
812 lbs_deb_thread("main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
813 priv->connect_status, priv->intcounter,
814 priv->psmode, priv->psstate);
815 spin_unlock_irq(&priv->driver_lock);
818 spin_unlock_irq(&priv->driver_lock);
820 lbs_deb_thread("main-thread 222 (waking up): intcounter=%d currenttxskb=%p dnld_sent=%d\n",
821 priv->intcounter, priv->currenttxskb, priv->dnld_sent);
823 set_current_state(TASK_RUNNING);
824 remove_wait_queue(&priv->waitq, &wait);
827 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
828 priv->intcounter, priv->currenttxskb, priv->dnld_sent);
830 if (kthread_should_stop() || priv->surpriseremoved) {
831 lbs_deb_thread("main-thread: break from main thread: surpriseremoved=0x%x\n",
832 priv->surpriseremoved);
837 spin_lock_irq(&priv->driver_lock);
839 if (priv->intcounter) {
842 priv->intcounter = 0;
843 int_status = priv->hw_get_int_status(priv, &ireg);
846 lbs_deb_thread("main-thread: reading HOST_INT_STATUS_REG failed\n");
847 spin_unlock_irq(&priv->driver_lock);
850 priv->hisregcpy |= ireg;
853 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
854 priv->intcounter, priv->currenttxskb, priv->dnld_sent);
856 /* command response? */
857 if (priv->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
858 lbs_deb_thread("main-thread: cmd response ready\n");
860 priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
861 spin_unlock_irq(&priv->driver_lock);
862 lbs_process_rx_command(priv);
863 spin_lock_irq(&priv->driver_lock);
867 if (priv->hisregcpy & MRVDRV_CARDEVENT) {
868 lbs_deb_thread("main-thread: Card Event Activity\n");
870 priv->hisregcpy &= ~MRVDRV_CARDEVENT;
872 if (priv->hw_read_event_cause(priv)) {
873 lbs_pr_alert("main-thread: hw_read_event_cause failed\n");
874 spin_unlock_irq(&priv->driver_lock);
877 spin_unlock_irq(&priv->driver_lock);
878 lbs_process_event(priv);
880 spin_unlock_irq(&priv->driver_lock);
882 /* Check if we need to confirm Sleep Request received previously */
883 if (priv->psstate == PS_STATE_PRE_SLEEP &&
884 !priv->dnld_sent && !priv->cur_cmd) {
885 if (priv->connect_status == LBS_CONNECTED) {
886 lbs_deb_thread("main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p dnld_sent=%d cur_cmd=%p, confirm now\n",
887 priv->intcounter, priv->currenttxskb, priv->dnld_sent, priv->cur_cmd);
889 lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
891 /* workaround for firmware sending
892 * deauth/linkloss event immediately
893 * after sleep request; remove this
894 * after firmware fixes it
896 priv->psstate = PS_STATE_AWAKE;
897 lbs_pr_alert("main-thread: ignore PS_SleepConfirm in non-connected state\n");
901 /* The PS state is changed during processing of Sleep Request
904 if ((priv->psstate == PS_STATE_SLEEP) ||
905 (priv->psstate == PS_STATE_PRE_SLEEP))
908 /* Execute the next command */
909 if (!priv->dnld_sent && !priv->cur_cmd)
910 lbs_execute_next_command(priv);
912 /* Wake-up command waiters which can't sleep in
913 * lbs_prepare_and_send_command
915 if (!list_empty(&priv->cmdpendingq))
916 wake_up_all(&priv->cmd_pending);
918 lbs_tx_runqueue(priv);
921 del_timer(&priv->command_timer);
922 wake_up_all(&priv->cmd_pending);
924 lbs_deb_leave(LBS_DEB_THREAD);
929 * @brief This function downloads firmware image, gets
930 * HW spec from firmware and set basic parameters to
933 * @param priv A pointer to struct lbs_private structure
936 static int lbs_setup_firmware(struct lbs_private *priv)
939 struct cmd_ds_mesh_access mesh_access;
941 lbs_deb_enter(LBS_DEB_FW);
944 * Read MAC address from HW
946 memset(priv->current_addr, 0xff, ETH_ALEN);
948 ret = lbs_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
949 0, CMD_OPTION_WAITFORRSP, 0, NULL);
956 lbs_set_mac_packet_filter(priv);
958 /* Get the supported Data rates */
959 ret = lbs_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
961 CMD_OPTION_WAITFORRSP, 0, NULL);
968 /* Disable mesh autostart */
969 if (priv->mesh_dev) {
970 memset(&mesh_access, 0, sizeof(mesh_access));
971 mesh_access.data[0] = cpu_to_le32(0);
972 ret = lbs_prepare_and_send_command(priv,
974 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
975 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
980 priv->mesh_autostart_enabled = 0;
985 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
990 * This function handles the timeout of command sending.
991 * It will re-send the same command again.
993 static void command_timer_fn(unsigned long data)
995 struct lbs_private *priv = (struct lbs_private *)data;
996 struct cmd_ctrl_node *ptempnode;
997 struct cmd_ds_command *cmd;
1000 ptempnode = priv->cur_cmd;
1001 if (ptempnode == NULL) {
1002 lbs_deb_fw("ptempnode empty\n");
1006 cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
1008 lbs_deb_fw("cmd is NULL\n");
1012 lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
1014 if (!priv->fw_ready)
1017 spin_lock_irqsave(&priv->driver_lock, flags);
1018 priv->cur_cmd = NULL;
1019 spin_unlock_irqrestore(&priv->driver_lock, flags);
1021 lbs_deb_fw("re-sending same command because of timeout\n");
1022 lbs_queue_cmd(priv, ptempnode, 0);
1024 wake_up_interruptible(&priv->waitq);
1029 static int lbs_init_adapter(struct lbs_private *priv)
1034 /* Allocate buffer to store the BSSID list */
1035 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
1036 priv->networks = kzalloc(bufsize, GFP_KERNEL);
1037 if (!priv->networks) {
1038 lbs_pr_err("Out of memory allocating beacons\n");
1043 /* Initialize scan result lists */
1044 INIT_LIST_HEAD(&priv->network_free_list);
1045 INIT_LIST_HEAD(&priv->network_list);
1046 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
1047 list_add_tail(&priv->networks[i].list,
1048 &priv->network_free_list);
1051 priv->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++priv->seqnum);
1052 priv->lbs_ps_confirm_sleep.command =
1053 cpu_to_le16(CMD_802_11_PS_MODE);
1054 priv->lbs_ps_confirm_sleep.size =
1055 cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
1056 priv->lbs_ps_confirm_sleep.action =
1057 cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
1059 memset(priv->current_addr, 0xff, ETH_ALEN);
1061 priv->connect_status = LBS_DISCONNECTED;
1062 priv->mesh_connect_status = LBS_DISCONNECTED;
1063 priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1064 priv->mode = IW_MODE_INFRA;
1065 priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
1066 priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
1067 priv->radioon = RADIO_ON;
1068 priv->auto_rate = 1;
1069 priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1070 priv->psmode = LBS802_11POWERMODECAM;
1071 priv->psstate = PS_STATE_FULL_POWER;
1073 mutex_init(&priv->lock);
1075 memset(&priv->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
1076 priv->tx_queue_idx = 0;
1077 spin_lock_init(&priv->txqueue_lock);
1079 setup_timer(&priv->command_timer, command_timer_fn,
1080 (unsigned long)priv);
1082 INIT_LIST_HEAD(&priv->cmdfreeq);
1083 INIT_LIST_HEAD(&priv->cmdpendingq);
1085 spin_lock_init(&priv->driver_lock);
1086 init_waitqueue_head(&priv->cmd_pending);
1088 /* Allocate the command buffers */
1089 if (lbs_allocate_cmd_buffer(priv)) {
1090 lbs_pr_err("Out of memory allocating command buffers\n");
1098 static void lbs_free_adapter(struct lbs_private *priv)
1100 lbs_deb_fw("free command buffer\n");
1101 lbs_free_cmd_buffer(priv);
1103 lbs_deb_fw("free command_timer\n");
1104 del_timer(&priv->command_timer);
1106 lbs_deb_fw("free scan results table\n");
1107 kfree(priv->networks);
1108 priv->networks = NULL;
1112 * @brief This function adds the card. it will probe the
1113 * card, allocate the lbs_priv and initialize the device.
1115 * @param card A pointer to card
1116 * @return A pointer to struct lbs_private structure
1118 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
1120 struct net_device *dev = NULL;
1121 struct lbs_private *priv = NULL;
1123 lbs_deb_enter(LBS_DEB_NET);
1125 /* Allocate an Ethernet device and register it */
1126 dev = alloc_etherdev(sizeof(struct lbs_private));
1128 lbs_pr_err("init ethX device failed\n");
1133 if (lbs_init_adapter(priv)) {
1134 lbs_pr_err("failed to initialize adapter structure.\n");
1135 goto err_init_adapter;
1140 priv->mesh_open = 0;
1141 priv->infra_open = 0;
1143 /* Setup the OS Interface to our functions */
1144 dev->open = lbs_open;
1145 dev->hard_start_xmit = lbs_pre_start_xmit;
1146 dev->stop = lbs_close;
1147 dev->set_mac_address = lbs_set_mac_address;
1148 dev->tx_timeout = lbs_tx_timeout;
1149 dev->get_stats = lbs_get_stats;
1150 dev->watchdog_timeo = 5 * HZ;
1151 dev->ethtool_ops = &lbs_ethtool_ops;
1153 dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
1155 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1156 dev->set_multicast_list = lbs_set_multicast_list;
1158 SET_NETDEV_DEV(dev, dmdev);
1160 priv->rtap_net_dev = NULL;
1162 lbs_deb_thread("Starting main thread...\n");
1163 init_waitqueue_head(&priv->waitq);
1164 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
1165 if (IS_ERR(priv->main_thread)) {
1166 lbs_deb_thread("Error creating main thread.\n");
1167 goto err_init_adapter;
1170 priv->work_thread = create_singlethread_workqueue("lbs_worker");
1171 INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
1172 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
1173 INIT_WORK(&priv->sync_channel, lbs_sync_channel);
1178 lbs_free_adapter(priv);
1183 lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
1186 EXPORT_SYMBOL_GPL(lbs_add_card);
1189 int lbs_remove_card(struct lbs_private *priv)
1191 struct net_device *dev = priv->dev;
1192 union iwreq_data wrqu;
1194 lbs_deb_enter(LBS_DEB_MAIN);
1196 lbs_remove_rtap(priv);
1199 device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
1201 cancel_delayed_work(&priv->scan_work);
1202 cancel_delayed_work(&priv->assoc_work);
1203 destroy_workqueue(priv->work_thread);
1205 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1206 priv->psmode = LBS802_11POWERMODECAM;
1207 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1210 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1211 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1212 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1214 /* Stop the thread servicing the interrupts */
1215 priv->surpriseremoved = 1;
1216 kthread_stop(priv->main_thread);
1218 lbs_free_adapter(priv);
1223 lbs_deb_leave(LBS_DEB_MAIN);
1226 EXPORT_SYMBOL_GPL(lbs_remove_card);
1229 int lbs_start_card(struct lbs_private *priv)
1231 struct net_device *dev = priv->dev;
1234 lbs_deb_enter(LBS_DEB_MAIN);
1236 /* poke the firmware */
1237 ret = lbs_setup_firmware(priv);
1244 if (register_netdev(dev)) {
1245 lbs_pr_err("cannot register ethX device\n");
1248 if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1249 lbs_pr_err("cannot register lbs_rtap attribute\n");
1251 lbs_debugfs_init_one(priv, dev);
1253 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1258 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1261 EXPORT_SYMBOL_GPL(lbs_start_card);
1264 int lbs_stop_card(struct lbs_private *priv)
1266 struct net_device *dev = priv->dev;
1268 struct cmd_ctrl_node *cmdnode;
1269 unsigned long flags;
1271 lbs_deb_enter(LBS_DEB_MAIN);
1273 netif_stop_queue(priv->dev);
1274 netif_carrier_off(priv->dev);
1276 lbs_debugfs_remove_one(priv);
1278 /* Flush pending command nodes */
1279 spin_lock_irqsave(&priv->driver_lock, flags);
1280 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
1281 cmdnode->cmdwaitqwoken = 1;
1282 wake_up_interruptible(&cmdnode->cmdwait_q);
1284 spin_unlock_irqrestore(&priv->driver_lock, flags);
1286 unregister_netdev(dev);
1288 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1291 EXPORT_SYMBOL_GPL(lbs_stop_card);
1295 * @brief This function adds mshX interface
1297 * @param priv A pointer to the struct lbs_private structure
1298 * @return 0 if successful, -X otherwise
1300 int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
1302 struct net_device *mesh_dev = NULL;
1305 lbs_deb_enter(LBS_DEB_MESH);
1307 /* Allocate a virtual mesh device */
1308 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1309 lbs_deb_mesh("init mshX device failed\n");
1313 mesh_dev->priv = priv;
1314 priv->mesh_dev = mesh_dev;
1316 mesh_dev->open = lbs_mesh_open;
1317 mesh_dev->hard_start_xmit = lbs_mesh_pre_start_xmit;
1318 mesh_dev->stop = lbs_mesh_close;
1319 mesh_dev->get_stats = lbs_get_stats;
1320 mesh_dev->set_mac_address = lbs_set_mac_address;
1321 mesh_dev->ethtool_ops = &lbs_ethtool_ops;
1322 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1323 sizeof(priv->dev->dev_addr));
1325 SET_NETDEV_DEV(priv->mesh_dev, dev);
1328 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1330 /* Register virtual mesh interface */
1331 ret = register_netdev(mesh_dev);
1333 lbs_pr_err("cannot register mshX virtual interface\n");
1337 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1339 goto err_unregister;
1341 /* Everything successful */
1346 unregister_netdev(mesh_dev);
1349 free_netdev(mesh_dev);
1352 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1355 EXPORT_SYMBOL_GPL(lbs_add_mesh);
1358 void lbs_remove_mesh(struct lbs_private *priv)
1360 struct net_device *mesh_dev;
1362 lbs_deb_enter(LBS_DEB_MAIN);
1367 mesh_dev = priv->mesh_dev;
1369 netif_stop_queue(mesh_dev);
1370 netif_carrier_off(priv->mesh_dev);
1372 sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1373 unregister_netdev(mesh_dev);
1375 priv->mesh_dev = NULL ;
1376 free_netdev(mesh_dev);
1379 lbs_deb_leave(LBS_DEB_MAIN);
1381 EXPORT_SYMBOL_GPL(lbs_remove_mesh);
1384 * @brief This function finds the CFP in
1385 * region_cfp_table based on region and band parameter.
1387 * @param region The region code
1388 * @param band The band
1389 * @param cfp_no A pointer to CFP number
1390 * @return A pointer to CFP
1392 struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1396 lbs_deb_enter(LBS_DEB_MAIN);
1398 end = ARRAY_SIZE(region_cfp_table);
1400 for (i = 0; i < end ; i++) {
1401 lbs_deb_main("region_cfp_table[i].region=%d\n",
1402 region_cfp_table[i].region);
1403 if (region_cfp_table[i].region == region) {
1404 *cfp_no = region_cfp_table[i].cfp_no_BG;
1405 lbs_deb_leave(LBS_DEB_MAIN);
1406 return region_cfp_table[i].cfp_BG;
1410 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1414 int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
1419 struct chan_freq_power *cfp;
1422 lbs_deb_enter(LBS_DEB_MAIN);
1424 memset(priv->region_channel, 0, sizeof(priv->region_channel));
1427 cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
1429 priv->region_channel[i].nrcfp = cfp_no;
1430 priv->region_channel[i].CFP = cfp;
1432 lbs_deb_main("wrong region code %#x in band B/G\n",
1437 priv->region_channel[i].valid = 1;
1438 priv->region_channel[i].region = region;
1439 priv->region_channel[i].band = band;
1443 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1448 * @brief This function handles the interrupt. it will change PS
1449 * state if applicable. it will wake up main_thread to handle
1450 * the interrupt event as well.
1452 * @param dev A pointer to net_device structure
1455 void lbs_interrupt(struct net_device *dev)
1457 struct lbs_private *priv = dev->priv;
1459 lbs_deb_enter(LBS_DEB_THREAD);
1461 lbs_deb_thread("lbs_interrupt: intcounter=%d\n",
1466 if (priv->psstate == PS_STATE_SLEEP) {
1467 priv->psstate = PS_STATE_AWAKE;
1468 netif_wake_queue(dev);
1470 netif_wake_queue(priv->mesh_dev);
1473 wake_up_interruptible(&priv->waitq);
1475 lbs_deb_leave(LBS_DEB_THREAD);
1477 EXPORT_SYMBOL_GPL(lbs_interrupt);
1479 int lbs_reset_device(struct lbs_private *priv)
1483 lbs_deb_enter(LBS_DEB_MAIN);
1484 ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
1485 CMD_ACT_HALT, 0, 0, NULL);
1486 msleep_interruptible(10);
1488 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1491 EXPORT_SYMBOL_GPL(lbs_reset_device);
1493 static int __init lbs_init_module(void)
1495 lbs_deb_enter(LBS_DEB_MAIN);
1497 lbs_deb_leave(LBS_DEB_MAIN);
1501 static void __exit lbs_exit_module(void)
1503 lbs_deb_enter(LBS_DEB_MAIN);
1505 lbs_debugfs_remove();
1507 lbs_deb_leave(LBS_DEB_MAIN);
1511 * rtap interface support fuctions
1514 static int lbs_rtap_open(struct net_device *dev)
1516 netif_carrier_off(dev);
1517 netif_stop_queue(dev);
1521 static int lbs_rtap_stop(struct net_device *dev)
1526 static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1528 netif_stop_queue(dev);
1532 static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
1534 struct lbs_private *priv = dev->priv;
1535 return &priv->ieee->stats;
1539 void lbs_remove_rtap(struct lbs_private *priv)
1541 if (priv->rtap_net_dev == NULL)
1543 unregister_netdev(priv->rtap_net_dev);
1544 free_ieee80211(priv->rtap_net_dev);
1545 priv->rtap_net_dev = NULL;
1548 int lbs_add_rtap(struct lbs_private *priv)
1552 if (priv->rtap_net_dev)
1555 priv->rtap_net_dev = alloc_ieee80211(0);
1556 if (priv->rtap_net_dev == NULL)
1560 priv->ieee = netdev_priv(priv->rtap_net_dev);
1562 strcpy(priv->rtap_net_dev->name, "rtap%d");
1564 priv->rtap_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
1565 priv->rtap_net_dev->open = lbs_rtap_open;
1566 priv->rtap_net_dev->stop = lbs_rtap_stop;
1567 priv->rtap_net_dev->get_stats = lbs_rtap_get_stats;
1568 priv->rtap_net_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
1569 priv->rtap_net_dev->set_multicast_list = lbs_set_multicast_list;
1570 priv->rtap_net_dev->priv = priv;
1572 priv->ieee->iw_mode = IW_MODE_MONITOR;
1574 rc = register_netdev(priv->rtap_net_dev);
1576 free_ieee80211(priv->rtap_net_dev);
1577 priv->rtap_net_dev = NULL;
1585 module_init(lbs_init_module);
1586 module_exit(lbs_exit_module);
1588 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1589 MODULE_AUTHOR("Marvell International Ltd.");
1590 MODULE_LICENSE("GPL");