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>
25 #define DRIVER_RELEASE_VERSION "322.p1"
26 const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
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 {
115 struct chan_freq_power *cfp_BG;
120 * the structure for the mapping between region and CFP
122 static struct region_cfp_table region_cfp_table[] = {
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 table to keep region code
155 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
156 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
159 * 802.11b/g supported bitrates (in 500Kb/s units)
161 u8 libertas_bg_rates[MAX_RATES] =
162 { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
166 * FW rate table. FW refers to rates by their index in this table, not by the
167 * rate value itself. Values of 0x00 are
168 * reserved positions.
170 static u8 fw_data_rates[MAX_RATES] =
171 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
172 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
176 * @brief use index to get the data rate
178 * @param idx The index of data rate
179 * @return data rate or 0
181 u32 libertas_fw_index_to_data_rate(u8 idx)
183 if (idx >= sizeof(fw_data_rates))
185 return fw_data_rates[idx];
189 * @brief use rate to get the index
191 * @param rate data rate
194 u8 libertas_data_rate_to_fw_index(u32 rate)
201 for (i = 0; i < sizeof(fw_data_rates); i++) {
202 if (rate == fw_data_rates[i])
209 * Attributes exported through sysfs
213 * @brief Get function for sysfs attribute anycast_mask
215 static ssize_t libertas_anycast_get(struct device * dev,
216 struct device_attribute *attr, char * buf)
218 struct cmd_ds_mesh_access mesh_access;
220 memset(&mesh_access, 0, sizeof(mesh_access));
221 libertas_prepare_and_send_command(to_net_dev(dev)->priv,
223 CMD_ACT_MESH_GET_ANYCAST,
224 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
226 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
230 * @brief Set function for sysfs attribute anycast_mask
232 static ssize_t libertas_anycast_set(struct device * dev,
233 struct device_attribute *attr, const char * buf, size_t count)
235 struct cmd_ds_mesh_access mesh_access;
238 memset(&mesh_access, 0, sizeof(mesh_access));
239 sscanf(buf, "%x", &datum);
240 mesh_access.data[0] = cpu_to_le32(datum);
242 libertas_prepare_and_send_command((to_net_dev(dev))->priv,
244 CMD_ACT_MESH_SET_ANYCAST,
245 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
250 * anycast_mask attribute to be exported per mshX interface
251 * through sysfs (/sys/class/net/mshX/anycast_mask)
253 static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
255 static ssize_t libertas_autostart_enabled_get(struct device * dev,
256 struct device_attribute *attr, char * buf)
258 struct cmd_ds_mesh_access mesh_access;
260 memset(&mesh_access, 0, sizeof(mesh_access));
261 libertas_prepare_and_send_command(to_net_dev(dev)->priv,
263 CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
264 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
266 return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
269 static ssize_t libertas_autostart_enabled_set(struct device * dev,
270 struct device_attribute *attr, const char * buf, size_t count)
272 struct cmd_ds_mesh_access mesh_access;
275 memset(&mesh_access, 0, sizeof(mesh_access));
276 sscanf(buf, "%d", &datum);
277 mesh_access.data[0] = cpu_to_le32(datum);
279 libertas_prepare_and_send_command((to_net_dev(dev))->priv,
281 CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
282 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
286 static DEVICE_ATTR(autostart_enabled, 0644,
287 libertas_autostart_enabled_get, libertas_autostart_enabled_set);
289 static struct attribute *libertas_mesh_sysfs_entries[] = {
290 &dev_attr_anycast_mask.attr,
291 &dev_attr_autostart_enabled.attr,
295 static struct attribute_group libertas_mesh_attr_group = {
296 .attrs = libertas_mesh_sysfs_entries,
300 * @brief Check if the device can be open and wait if necessary.
302 * @param dev A pointer to net_device structure
305 * For USB adapter, on some systems the device open handler will be
306 * called before FW ready. Use the following flag check and wait
307 * function to work around the issue.
310 static int pre_open_check(struct net_device *dev)
312 wlan_private *priv = (wlan_private *) dev->priv;
313 wlan_adapter *adapter = priv->adapter;
316 while (!adapter->fw_ready && i < 20) {
318 msleep_interruptible(100);
320 if (!adapter->fw_ready) {
321 lbs_pr_err("firmware not ready\n");
329 * @brief This function opens the device
331 * @param dev A pointer to net_device structure
334 static int libertas_dev_open(struct net_device *dev)
336 wlan_private *priv = (wlan_private *) dev->priv;
337 wlan_adapter *adapter = priv->adapter;
339 lbs_deb_enter(LBS_DEB_NET);
343 if (adapter->connect_status == LIBERTAS_CONNECTED) {
344 netif_carrier_on(priv->dev);
346 netif_carrier_on(priv->mesh_dev);
348 netif_carrier_off(priv->dev);
350 netif_carrier_off(priv->mesh_dev);
353 lbs_deb_leave(LBS_DEB_NET);
357 * @brief This function opens the mshX interface
359 * @param dev A pointer to net_device structure
362 static int libertas_mesh_open(struct net_device *dev)
364 wlan_private *priv = (wlan_private *) dev->priv ;
366 if (pre_open_check(dev) == -1)
368 priv->mesh_open = 1 ;
369 netif_wake_queue(priv->mesh_dev);
370 if (priv->infra_open == 0)
371 return libertas_dev_open(priv->dev) ;
376 * @brief This function opens the ethX interface
378 * @param dev A pointer to net_device structure
381 static int libertas_open(struct net_device *dev)
383 wlan_private *priv = (wlan_private *) dev->priv ;
385 if(pre_open_check(dev) == -1)
387 priv->infra_open = 1 ;
388 netif_wake_queue(priv->dev);
390 return libertas_dev_open(priv->dev) ;
394 static int libertas_dev_close(struct net_device *dev)
396 wlan_private *priv = dev->priv;
398 lbs_deb_enter(LBS_DEB_NET);
400 netif_carrier_off(priv->dev);
403 lbs_deb_leave(LBS_DEB_NET);
408 * @brief This function closes the mshX interface
410 * @param dev A pointer to net_device structure
413 static int libertas_mesh_close(struct net_device *dev)
415 wlan_private *priv = (wlan_private *) (dev->priv);
418 netif_stop_queue(priv->mesh_dev);
419 if (priv->infra_open == 0)
420 return libertas_dev_close(dev);
426 * @brief This function closes the ethX interface
428 * @param dev A pointer to net_device structure
431 static int libertas_close(struct net_device *dev)
433 wlan_private *priv = (wlan_private *) dev->priv;
435 netif_stop_queue(dev);
436 priv->infra_open = 0;
437 if (priv->mesh_open == 0)
438 return libertas_dev_close(dev);
444 static int libertas_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
447 wlan_private *priv = dev->priv;
449 lbs_deb_enter(LBS_DEB_NET);
451 if (priv->dnld_sent || priv->adapter->TxLockFlag) {
452 priv->stats.tx_dropped++;
456 netif_stop_queue(priv->dev);
458 netif_stop_queue(priv->mesh_dev);
460 if (libertas_process_tx(priv, skb) == 0)
461 dev->trans_start = jiffies;
463 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
468 * @brief Mark mesh packets and handover them to libertas_hard_start_xmit
471 static int libertas_mesh_pre_start_xmit(struct sk_buff *skb,
472 struct net_device *dev)
474 wlan_private *priv = dev->priv;
477 lbs_deb_enter(LBS_DEB_MESH);
481 ret = libertas_hard_start_xmit(skb, priv->dev);
482 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
487 * @brief Mark non-mesh packets and handover them to libertas_hard_start_xmit
490 static int libertas_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
494 lbs_deb_enter(LBS_DEB_NET);
496 UNSET_MESH_FRAME(skb);
498 ret = libertas_hard_start_xmit(skb, dev);
499 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
503 static void libertas_tx_timeout(struct net_device *dev)
505 wlan_private *priv = (wlan_private *) dev->priv;
507 lbs_deb_enter(LBS_DEB_TX);
509 lbs_pr_err("tx watch dog timeout\n");
511 priv->dnld_sent = DNLD_RES_RECEIVED;
512 dev->trans_start = jiffies;
514 if (priv->adapter->currenttxskb) {
515 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
516 /* If we are here, we have not received feedback from
517 the previous packet. Assume TX_FAIL and move on. */
518 priv->adapter->eventcause = 0x01000000;
519 libertas_send_tx_feedback(priv);
521 wake_up_interruptible(&priv->waitq);
522 } else if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
523 netif_wake_queue(priv->dev);
525 netif_wake_queue(priv->mesh_dev);
528 lbs_deb_leave(LBS_DEB_TX);
532 * @brief This function returns the network statistics
534 * @param dev A pointer to wlan_private structure
535 * @return A pointer to net_device_stats structure
537 static struct net_device_stats *libertas_get_stats(struct net_device *dev)
539 wlan_private *priv = (wlan_private *) dev->priv;
544 static int libertas_set_mac_address(struct net_device *dev, void *addr)
547 wlan_private *priv = (wlan_private *) dev->priv;
548 wlan_adapter *adapter = priv->adapter;
549 struct sockaddr *phwaddr = addr;
551 lbs_deb_enter(LBS_DEB_NET);
553 /* In case it was called from the mesh device */
556 memset(adapter->current_addr, 0, ETH_ALEN);
558 /* dev->dev_addr is 8 bytes */
559 lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
561 lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
562 memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
564 ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
566 CMD_OPTION_WAITFORRSP, 0, NULL);
569 lbs_deb_net("set MAC address failed\n");
574 lbs_deb_hex(LBS_DEB_NET, "adapter->macaddr", adapter->current_addr, ETH_ALEN);
575 memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
577 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
580 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
584 static int libertas_copy_multicast_address(wlan_adapter * adapter,
585 struct net_device *dev)
588 struct dev_mc_list *mcptr = dev->mc_list;
590 for (i = 0; i < dev->mc_count; i++) {
591 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
599 static void libertas_set_multicast_list(struct net_device *dev)
601 wlan_private *priv = dev->priv;
602 wlan_adapter *adapter = priv->adapter;
605 lbs_deb_enter(LBS_DEB_NET);
607 oldpacketfilter = adapter->currentpacketfilter;
609 if (dev->flags & IFF_PROMISC) {
610 lbs_deb_net("enable promiscuous mode\n");
611 adapter->currentpacketfilter |=
612 CMD_ACT_MAC_PROMISCUOUS_ENABLE;
613 adapter->currentpacketfilter &=
614 ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
615 CMD_ACT_MAC_MULTICAST_ENABLE);
618 adapter->currentpacketfilter &=
619 ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
621 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
622 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
623 lbs_deb_net( "enabling all multicast\n");
624 adapter->currentpacketfilter |=
625 CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
626 adapter->currentpacketfilter &=
627 ~CMD_ACT_MAC_MULTICAST_ENABLE;
629 adapter->currentpacketfilter &=
630 ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
632 if (!dev->mc_count) {
633 lbs_deb_net("no multicast addresses, "
634 "disabling multicast\n");
635 adapter->currentpacketfilter &=
636 ~CMD_ACT_MAC_MULTICAST_ENABLE;
640 adapter->currentpacketfilter |=
641 CMD_ACT_MAC_MULTICAST_ENABLE;
643 adapter->nr_of_multicastmacaddr =
644 libertas_copy_multicast_address(adapter, dev);
646 lbs_deb_net("multicast addresses: %d\n",
649 for (i = 0; i < dev->mc_count; i++) {
650 lbs_deb_net("Multicast address %d:"
652 adapter->multicastlist[i][0],
653 adapter->multicastlist[i][1],
654 adapter->multicastlist[i][2],
655 adapter->multicastlist[i][3],
656 adapter->multicastlist[i][4],
657 adapter->multicastlist[i][5]);
659 /* send multicast addresses to firmware */
660 libertas_prepare_and_send_command(priv,
661 CMD_MAC_MULTICAST_ADR,
668 if (adapter->currentpacketfilter != oldpacketfilter) {
669 libertas_set_mac_packet_filter(priv);
672 lbs_deb_leave(LBS_DEB_NET);
676 * @brief This function handles the major jobs in the WLAN driver.
677 * It handles all events generated by firmware, RX data received
678 * from firmware and TX data sent from kernel.
680 * @param data A pointer to wlan_thread structure
683 static int libertas_thread(void *data)
685 struct net_device *dev = data;
686 wlan_private *priv = dev->priv;
687 wlan_adapter *adapter = priv->adapter;
691 lbs_deb_enter(LBS_DEB_THREAD);
693 init_waitqueue_entry(&wait, current);
696 lbs_deb_thread( "main-thread 111: intcounter=%d "
697 "currenttxskb=%p dnld_sent=%d\n",
699 adapter->currenttxskb, priv->dnld_sent);
701 add_wait_queue(&priv->waitq, &wait);
702 set_current_state(TASK_INTERRUPTIBLE);
703 spin_lock_irq(&adapter->driver_lock);
704 if ((adapter->psstate == PS_STATE_SLEEP) ||
705 (!adapter->intcounter
706 && (priv->dnld_sent || adapter->cur_cmd ||
707 list_empty(&adapter->cmdpendingq)))) {
709 "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
710 adapter->connect_status, adapter->intcounter,
711 adapter->psmode, adapter->psstate);
712 spin_unlock_irq(&adapter->driver_lock);
715 spin_unlock_irq(&adapter->driver_lock);
719 "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
720 "dnld_sent=%d\n", adapter->intcounter,
721 adapter->currenttxskb, priv->dnld_sent);
723 set_current_state(TASK_RUNNING);
724 remove_wait_queue(&priv->waitq, &wait);
727 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
730 adapter->currenttxskb, priv->dnld_sent);
732 if (kthread_should_stop()
733 || adapter->surpriseremoved) {
735 "main-thread: break from main thread: surpriseremoved=0x%x\n",
736 adapter->surpriseremoved);
741 spin_lock_irq(&adapter->driver_lock);
742 if (adapter->intcounter) {
744 adapter->intcounter = 0;
745 int_status = priv->hw_get_int_status(priv, &ireg);
749 "main-thread: reading HOST_INT_STATUS_REG failed\n");
750 spin_unlock_irq(&adapter->driver_lock);
753 adapter->hisregcpy |= ireg;
756 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
759 adapter->currenttxskb, priv->dnld_sent);
761 /* command response? */
762 if (adapter->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
763 lbs_deb_thread("main-thread: cmd response ready\n");
765 adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
766 spin_unlock_irq(&adapter->driver_lock);
767 libertas_process_rx_command(priv);
768 spin_lock_irq(&adapter->driver_lock);
772 if (adapter->hisregcpy & MRVDRV_CARDEVENT) {
773 lbs_deb_thread("main-thread: Card Event Activity\n");
775 adapter->hisregcpy &= ~MRVDRV_CARDEVENT;
777 if (priv->hw_read_event_cause(priv)) {
779 "main-thread: hw_read_event_cause failed\n");
780 spin_unlock_irq(&adapter->driver_lock);
783 spin_unlock_irq(&adapter->driver_lock);
784 libertas_process_event(priv);
786 spin_unlock_irq(&adapter->driver_lock);
788 /* Check if we need to confirm Sleep Request received previously */
789 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
790 if (!priv->dnld_sent && !adapter->cur_cmd) {
791 if (adapter->connect_status ==
792 LIBERTAS_CONNECTED) {
794 "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
795 "dnld_sent=%d cur_cmd=%p, confirm now\n",
797 adapter->currenttxskb,
801 libertas_ps_confirm_sleep(priv,
802 (u16) adapter->psmode);
804 /* workaround for firmware sending
805 * deauth/linkloss event immediately
806 * after sleep request, remove this
807 * after firmware fixes it
809 adapter->psstate = PS_STATE_AWAKE;
811 "main-thread: ignore PS_SleepConfirm in non-connected state\n");
816 /* The PS state is changed during processing of Sleep Request
819 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
820 (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
823 /* Execute the next command */
824 if (!priv->dnld_sent && !priv->adapter->cur_cmd)
825 libertas_execute_next_command(priv);
827 /* Wake-up command waiters which can't sleep in
828 * libertas_prepare_and_send_command
830 if (!adapter->nr_cmd_pending)
831 wake_up_all(&adapter->cmd_pending);
833 libertas_tx_runqueue(priv);
836 del_timer(&adapter->command_timer);
837 adapter->nr_cmd_pending = 0;
838 wake_up_all(&adapter->cmd_pending);
840 lbs_deb_leave(LBS_DEB_THREAD);
845 * @brief This function downloads firmware image, gets
846 * HW spec from firmware and set basic parameters to
849 * @param priv A pointer to wlan_private structure
852 static int wlan_setup_station_hw(wlan_private * priv)
855 wlan_adapter *adapter = priv->adapter;
857 lbs_deb_enter(LBS_DEB_FW);
859 ret = priv->hw_prog_firmware(priv);
862 lbs_deb_fw("bootloader in invalid state\n");
868 * Read MAC address from HW
870 memset(adapter->current_addr, 0xff, ETH_ALEN);
872 ret = libertas_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
873 0, CMD_OPTION_WAITFORRSP, 0, NULL);
880 libertas_set_mac_packet_filter(priv);
882 /* Get the supported Data rates */
883 ret = libertas_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
885 CMD_OPTION_WAITFORRSP, 0, NULL);
894 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
898 static void command_timer_fn(unsigned long data);
901 * This function handles the timeout of command sending.
902 * It will re-send the same command again.
904 static void command_timer_fn(unsigned long data)
906 wlan_private *priv = (wlan_private *)data;
907 wlan_adapter *adapter = priv->adapter;
908 struct cmd_ctrl_node *ptempnode;
909 struct cmd_ds_command *cmd;
912 ptempnode = adapter->cur_cmd;
913 if (ptempnode == NULL) {
914 lbs_deb_fw("ptempnode empty\n");
918 cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
920 lbs_deb_fw("cmd is NULL\n");
924 lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
926 if (!adapter->fw_ready)
929 spin_lock_irqsave(&adapter->driver_lock, flags);
930 adapter->cur_cmd = NULL;
931 spin_unlock_irqrestore(&adapter->driver_lock, flags);
933 lbs_deb_fw("re-sending same command because of timeout\n");
934 libertas_queue_cmd(adapter, ptempnode, 0);
936 wake_up_interruptible(&priv->waitq);
941 static void libertas_free_adapter(wlan_private * priv)
943 wlan_adapter *adapter = priv->adapter;
946 lbs_deb_fw("why double free adapter?\n");
950 lbs_deb_fw("free command buffer\n");
951 libertas_free_cmd_buffer(priv);
953 lbs_deb_fw("free command_timer\n");
954 del_timer(&adapter->command_timer);
956 lbs_deb_fw("free scan results table\n");
957 kfree(adapter->networks);
958 adapter->networks = NULL;
960 /* Free the adapter object itself */
961 lbs_deb_fw("free adapter\n");
963 priv->adapter = NULL;
966 static int wlan_allocate_adapter(wlan_private * priv)
969 wlan_adapter *adapter = priv->adapter;
971 /* Allocate buffer to store the BSSID list */
972 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
973 adapter->networks = kzalloc(bufsize, GFP_KERNEL);
974 if (!adapter->networks) {
975 lbs_pr_err("Out of memory allocating beacons\n");
976 libertas_free_adapter(priv);
980 /* Allocate the command buffers */
981 libertas_allocate_cmd_buffer(priv);
983 memset(&adapter->libertas_ps_confirm_sleep, 0, sizeof(struct PS_CMD_ConfirmSleep));
984 adapter->libertas_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
985 adapter->libertas_ps_confirm_sleep.command =
986 cpu_to_le16(CMD_802_11_PS_MODE);
987 adapter->libertas_ps_confirm_sleep.size =
988 cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
989 adapter->libertas_ps_confirm_sleep.result = 0;
990 adapter->libertas_ps_confirm_sleep.action =
991 cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
996 static void wlan_init_adapter(wlan_private * priv)
998 wlan_adapter *adapter = priv->adapter;
1001 adapter->connect_status = LIBERTAS_DISCONNECTED;
1002 memset(adapter->current_addr, 0xff, ETH_ALEN);
1005 adapter->scantype = CMD_SCAN_TYPE_ACTIVE;
1008 adapter->scanmode = CMD_BSS_TYPE_ANY;
1010 /* 802.11 specific */
1011 adapter->secinfo.wep_enabled = 0;
1012 for (i = 0; i < sizeof(adapter->wep_keys) / sizeof(adapter->wep_keys[0]);
1014 memset(&adapter->wep_keys[i], 0, sizeof(struct enc_key));
1015 adapter->wep_tx_keyidx = 0;
1016 adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1017 adapter->mode = IW_MODE_INFRA;
1019 adapter->pending_assoc_req = NULL;
1020 adapter->in_progress_assoc_req = NULL;
1022 /* Initialize scan result lists */
1023 INIT_LIST_HEAD(&adapter->network_free_list);
1024 INIT_LIST_HEAD(&adapter->network_list);
1025 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
1026 list_add_tail(&adapter->networks[i].list,
1027 &adapter->network_free_list);
1030 mutex_init(&adapter->lock);
1032 memset(&adapter->curbssparams, 0, sizeof(adapter->curbssparams));
1033 adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
1035 /* PnP and power profile */
1036 adapter->surpriseremoved = 0;
1038 adapter->currentpacketfilter =
1039 CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
1041 adapter->radioon = RADIO_ON;
1043 adapter->auto_rate = 1;
1044 adapter->cur_rate = 0;
1045 adapter->adhoc_grate_enabled = 0;
1047 adapter->beaconperiod = MRVDRV_BEACON_INTERVAL;
1049 // set default capabilities
1050 adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1052 adapter->psmode = WLAN802_11POWERMODECAM;
1054 adapter->listeninterval = MRVDRV_DEFAULT_LISTEN_INTERVAL;
1056 adapter->psstate = PS_STATE_FULL_POWER;
1057 adapter->needtowakeup = 0;
1059 adapter->intcounter = 0;
1061 adapter->currenttxskb = NULL;
1063 memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
1064 adapter->tx_queue_idx = 0;
1065 spin_lock_init(&adapter->txqueue_lock);
1070 static int libertas_init_fw(wlan_private * priv)
1073 wlan_adapter *adapter = priv->adapter;
1075 lbs_deb_enter(LBS_DEB_FW);
1077 /* Allocate adapter structure */
1078 if ((ret = wlan_allocate_adapter(priv)) != 0)
1081 /* init adapter structure */
1082 wlan_init_adapter(priv);
1084 /* init timer etc. */
1085 setup_timer(&adapter->command_timer, command_timer_fn,
1086 (unsigned long)priv);
1088 /* download fimrware etc. */
1089 if ((ret = wlan_setup_station_hw(priv)) != 0) {
1090 del_timer_sync(&adapter->command_timer);
1095 libertas_init_11d(priv);
1099 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
1104 * @brief This function adds the card. it will probe the
1105 * card, allocate the wlan_priv and initialize the device.
1107 * @param card A pointer to card
1108 * @return A pointer to wlan_private structure
1110 wlan_private *libertas_add_card(void *card, struct device *dmdev)
1112 struct net_device *dev = NULL;
1113 wlan_private *priv = NULL;
1115 lbs_deb_enter(LBS_DEB_NET);
1117 /* Allocate an Ethernet device and register it */
1118 if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
1119 lbs_pr_err("init ethX device failed\n");
1124 /* allocate buffer for wlan_adapter */
1125 if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
1126 lbs_pr_err("allocate buffer for wlan_adapter failed\n");
1132 priv->mesh_open = 0;
1133 priv->infra_open = 0;
1135 SET_MODULE_OWNER(dev);
1137 /* Setup the OS Interface to our functions */
1138 dev->open = libertas_open;
1139 dev->hard_start_xmit = libertas_pre_start_xmit;
1140 dev->stop = libertas_close;
1141 dev->set_mac_address = libertas_set_mac_address;
1142 dev->tx_timeout = libertas_tx_timeout;
1143 dev->get_stats = libertas_get_stats;
1144 dev->watchdog_timeo = 5 * HZ;
1145 dev->ethtool_ops = &libertas_ethtool_ops;
1147 dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
1149 #define NETIF_F_DYNALLOC 16
1150 dev->features |= NETIF_F_DYNALLOC;
1151 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1152 dev->set_multicast_list = libertas_set_multicast_list;
1154 SET_NETDEV_DEV(dev, dmdev);
1156 INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
1157 INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
1159 spin_lock_init(&priv->adapter->driver_lock);
1160 init_waitqueue_head(&priv->adapter->cmd_pending);
1161 priv->adapter->nr_cmd_pending = 0;
1168 lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
1171 EXPORT_SYMBOL_GPL(libertas_add_card);
1173 int libertas_activate_card(wlan_private *priv)
1175 struct net_device *dev = priv->dev;
1178 lbs_deb_enter(LBS_DEB_MAIN);
1180 lbs_deb_thread("Starting main thread...\n");
1181 init_waitqueue_head(&priv->waitq);
1182 priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
1183 if (IS_ERR(priv->main_thread)) {
1184 lbs_deb_thread("Error creating main thread.\n");
1188 priv->assoc_thread =
1189 create_singlethread_workqueue("libertas_assoc");
1190 INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
1191 INIT_WORK(&priv->sync_channel, libertas_sync_channel);
1194 * Register the device. Fillup the private data structure with
1195 * relevant information from the card and request for the required
1198 if (priv->hw_register_dev(priv) < 0) {
1199 lbs_pr_err("failed to register WLAN device\n");
1200 goto err_registerdev;
1203 /* init FW and HW */
1204 if (libertas_init_fw(priv)) {
1205 lbs_pr_err("firmware init failed\n");
1206 goto err_registerdev;
1209 if (register_netdev(dev)) {
1210 lbs_pr_err("cannot register ethX device\n");
1214 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1216 libertas_debugfs_init_one(priv, dev);
1222 priv->hw_unregister_dev(priv);
1224 destroy_workqueue(priv->assoc_thread);
1225 /* Stop the thread servicing the interrupts */
1226 wake_up_interruptible(&priv->waitq);
1227 kthread_stop(priv->main_thread);
1229 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
1232 EXPORT_SYMBOL_GPL(libertas_activate_card);
1236 * @brief This function adds mshX interface
1238 * @param priv A pointer to the wlan_private structure
1239 * @return 0 if successful, -X otherwise
1241 int libertas_add_mesh(wlan_private *priv, struct device *dev)
1243 struct net_device *mesh_dev = NULL;
1246 lbs_deb_enter(LBS_DEB_MESH);
1248 /* Allocate a virtual mesh device */
1249 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1250 lbs_deb_mesh("init mshX device failed\n");
1254 mesh_dev->priv = priv;
1255 priv->mesh_dev = mesh_dev;
1257 SET_MODULE_OWNER(mesh_dev);
1259 mesh_dev->open = libertas_mesh_open;
1260 mesh_dev->hard_start_xmit = libertas_mesh_pre_start_xmit;
1261 mesh_dev->stop = libertas_mesh_close;
1262 mesh_dev->get_stats = libertas_get_stats;
1263 mesh_dev->set_mac_address = libertas_set_mac_address;
1264 mesh_dev->ethtool_ops = &libertas_ethtool_ops;
1265 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1266 sizeof(priv->dev->dev_addr));
1268 SET_NETDEV_DEV(priv->mesh_dev, dev);
1271 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1273 #define NETIF_F_DYNALLOC 16
1275 /* Register virtual mesh interface */
1276 ret = register_netdev(mesh_dev);
1278 lbs_pr_err("cannot register mshX virtual interface\n");
1282 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
1284 goto err_unregister;
1286 /* Everything successful */
1291 unregister_netdev(mesh_dev);
1294 free_netdev(mesh_dev);
1297 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1300 EXPORT_SYMBOL_GPL(libertas_add_mesh);
1302 static void wake_pending_cmdnodes(wlan_private *priv)
1304 struct cmd_ctrl_node *cmdnode;
1305 unsigned long flags;
1307 lbs_deb_enter(LBS_DEB_HOST);
1309 spin_lock_irqsave(&priv->adapter->driver_lock, flags);
1310 list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
1311 cmdnode->cmdwaitqwoken = 1;
1312 wake_up_interruptible(&cmdnode->cmdwait_q);
1314 spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
1318 int libertas_remove_card(wlan_private *priv)
1320 wlan_adapter *adapter;
1321 struct net_device *dev;
1322 union iwreq_data wrqu;
1324 lbs_deb_enter(LBS_DEB_NET);
1329 adapter = priv->adapter;
1336 netif_stop_queue(priv->dev);
1337 netif_carrier_off(priv->dev);
1339 wake_pending_cmdnodes(priv);
1341 unregister_netdev(dev);
1343 cancel_delayed_work(&priv->assoc_work);
1344 destroy_workqueue(priv->assoc_thread);
1346 if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
1347 adapter->psmode = WLAN802_11POWERMODECAM;
1348 libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1351 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1352 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1353 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1355 adapter->surpriseremoved = 1;
1357 /* Stop the thread servicing the interrupts */
1358 kthread_stop(priv->main_thread);
1360 libertas_debugfs_remove_one(priv);
1362 lbs_deb_net("free adapter\n");
1363 libertas_free_adapter(priv);
1365 lbs_deb_net("unregister finish\n");
1371 lbs_deb_leave(LBS_DEB_NET);
1374 EXPORT_SYMBOL_GPL(libertas_remove_card);
1377 void libertas_remove_mesh(wlan_private *priv)
1379 struct net_device *mesh_dev;
1381 lbs_deb_enter(LBS_DEB_NET);
1386 mesh_dev = priv->mesh_dev;
1388 netif_stop_queue(mesh_dev);
1389 netif_carrier_off(priv->mesh_dev);
1391 sysfs_remove_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
1392 unregister_netdev(mesh_dev);
1394 priv->mesh_dev = NULL ;
1395 free_netdev(mesh_dev);
1398 lbs_deb_leave(LBS_DEB_NET);
1400 EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1403 * @brief This function finds the CFP in
1404 * region_cfp_table based on region and band parameter.
1406 * @param region The region code
1407 * @param band The band
1408 * @param cfp_no A pointer to CFP number
1409 * @return A pointer to CFP
1411 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1415 lbs_deb_enter(LBS_DEB_MAIN);
1417 end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1419 for (i = 0; i < end ; i++) {
1420 lbs_deb_main("region_cfp_table[i].region=%d\n",
1421 region_cfp_table[i].region);
1422 if (region_cfp_table[i].region == region) {
1423 *cfp_no = region_cfp_table[i].cfp_no_BG;
1424 lbs_deb_leave(LBS_DEB_MAIN);
1425 return region_cfp_table[i].cfp_BG;
1429 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1433 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1435 wlan_adapter *adapter = priv->adapter;
1439 struct chan_freq_power *cfp;
1442 lbs_deb_enter(LBS_DEB_MAIN);
1444 memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1447 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1449 adapter->region_channel[i].nrcfp = cfp_no;
1450 adapter->region_channel[i].CFP = cfp;
1452 lbs_deb_main("wrong region code %#x in band B/G\n",
1457 adapter->region_channel[i].valid = 1;
1458 adapter->region_channel[i].region = region;
1459 adapter->region_channel[i].band = band;
1463 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1468 * @brief This function handles the interrupt. it will change PS
1469 * state if applicable. it will wake up main_thread to handle
1470 * the interrupt event as well.
1472 * @param dev A pointer to net_device structure
1475 void libertas_interrupt(struct net_device *dev)
1477 wlan_private *priv = dev->priv;
1479 lbs_deb_enter(LBS_DEB_THREAD);
1481 lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1482 priv->adapter->intcounter);
1484 priv->adapter->intcounter++;
1486 if (priv->adapter->psstate == PS_STATE_SLEEP) {
1487 priv->adapter->psstate = PS_STATE_AWAKE;
1488 netif_wake_queue(dev);
1490 netif_wake_queue(priv->mesh_dev);
1493 wake_up_interruptible(&priv->waitq);
1495 lbs_deb_leave(LBS_DEB_THREAD);
1497 EXPORT_SYMBOL_GPL(libertas_interrupt);
1499 int libertas_reset_device(wlan_private *priv)
1503 lbs_deb_enter(LBS_DEB_MAIN);
1504 ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
1505 CMD_ACT_HALT, 0, 0, NULL);
1506 msleep_interruptible(10);
1508 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1511 EXPORT_SYMBOL_GPL(libertas_reset_device);
1513 static int libertas_init_module(void)
1515 lbs_deb_enter(LBS_DEB_MAIN);
1516 libertas_debugfs_init();
1517 lbs_deb_leave(LBS_DEB_MAIN);
1521 static void libertas_exit_module(void)
1523 lbs_deb_enter(LBS_DEB_MAIN);
1525 libertas_debugfs_remove();
1527 lbs_deb_leave(LBS_DEB_MAIN);
1530 module_init(libertas_init_module);
1531 module_exit(libertas_exit_module);
1533 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1534 MODULE_AUTHOR("Marvell International Ltd.");
1535 MODULE_LICENSE("GPL");