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);
256 * @brief Check if the device can be open and wait if necessary.
258 * @param dev A pointer to net_device structure
261 * For USB adapter, on some systems the device open handler will be
262 * called before FW ready. Use the following flag check and wait
263 * function to work around the issue.
266 static int pre_open_check(struct net_device *dev)
268 wlan_private *priv = (wlan_private *) dev->priv;
269 wlan_adapter *adapter = priv->adapter;
272 while (!adapter->fw_ready && i < 20) {
274 msleep_interruptible(100);
276 if (!adapter->fw_ready) {
277 lbs_pr_err("firmware not ready\n");
285 * @brief This function opens the device
287 * @param dev A pointer to net_device structure
290 static int libertas_dev_open(struct net_device *dev)
292 wlan_private *priv = (wlan_private *) dev->priv;
293 wlan_adapter *adapter = priv->adapter;
295 lbs_deb_enter(LBS_DEB_NET);
299 if (adapter->connect_status == LIBERTAS_CONNECTED) {
300 netif_carrier_on(priv->dev);
301 netif_carrier_on(priv->mesh_dev);
303 netif_carrier_off(priv->dev);
304 netif_carrier_off(priv->mesh_dev);
307 lbs_deb_leave(LBS_DEB_NET);
311 * @brief This function opens the mshX interface
313 * @param dev A pointer to net_device structure
316 static int libertas_mesh_open(struct net_device *dev)
318 wlan_private *priv = (wlan_private *) dev->priv ;
320 if (pre_open_check(dev) == -1)
322 priv->mesh_open = 1 ;
323 netif_wake_queue(priv->mesh_dev);
324 if (priv->infra_open == 0)
325 return libertas_dev_open(priv->dev) ;
330 * @brief This function opens the ethX interface
332 * @param dev A pointer to net_device structure
335 static int libertas_open(struct net_device *dev)
337 wlan_private *priv = (wlan_private *) dev->priv ;
339 if(pre_open_check(dev) == -1)
341 priv->infra_open = 1 ;
342 netif_wake_queue(priv->dev);
344 return libertas_dev_open(priv->dev) ;
348 static int libertas_dev_close(struct net_device *dev)
350 wlan_private *priv = dev->priv;
352 lbs_deb_enter(LBS_DEB_NET);
354 netif_carrier_off(priv->dev);
357 lbs_deb_leave(LBS_DEB_NET);
362 * @brief This function closes the mshX interface
364 * @param dev A pointer to net_device structure
367 static int libertas_mesh_close(struct net_device *dev)
369 wlan_private *priv = (wlan_private *) (dev->priv);
372 netif_stop_queue(priv->mesh_dev);
373 if (priv->infra_open == 0)
374 return libertas_dev_close(dev);
380 * @brief This function closes the ethX interface
382 * @param dev A pointer to net_device structure
385 static int libertas_close(struct net_device *dev)
387 wlan_private *priv = (wlan_private *) dev->priv;
389 netif_stop_queue(dev);
390 priv->infra_open = 0;
391 if (priv->mesh_open == 0)
392 return libertas_dev_close(dev);
398 static int libertas_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
401 wlan_private *priv = dev->priv;
403 lbs_deb_enter(LBS_DEB_NET);
405 if (priv->dnld_sent || priv->adapter->TxLockFlag) {
406 priv->stats.tx_dropped++;
410 netif_stop_queue(priv->dev);
411 netif_stop_queue(priv->mesh_dev);
413 if (libertas_process_tx(priv, skb) == 0)
414 dev->trans_start = jiffies;
416 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
421 * @brief Mark mesh packets and handover them to libertas_hard_start_xmit
424 static int libertas_mesh_pre_start_xmit(struct sk_buff *skb,
425 struct net_device *dev)
427 wlan_private *priv = dev->priv;
430 lbs_deb_enter(LBS_DEB_MESH);
434 ret = libertas_hard_start_xmit(skb, priv->dev);
435 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
440 * @brief Mark non-mesh packets and handover them to libertas_hard_start_xmit
443 static int libertas_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
447 lbs_deb_enter(LBS_DEB_NET);
449 UNSET_MESH_FRAME(skb);
451 ret = libertas_hard_start_xmit(skb, dev);
452 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
456 static void libertas_tx_timeout(struct net_device *dev)
458 wlan_private *priv = (wlan_private *) dev->priv;
460 lbs_deb_enter(LBS_DEB_TX);
462 lbs_pr_err("tx watch dog timeout\n");
464 priv->dnld_sent = DNLD_RES_RECEIVED;
465 dev->trans_start = jiffies;
467 if (priv->adapter->currenttxskb) {
468 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
469 /* If we are here, we have not received feedback from
470 the previous packet. Assume TX_FAIL and move on. */
471 priv->adapter->eventcause = 0x01000000;
472 libertas_send_tx_feedback(priv);
474 wake_up_interruptible(&priv->waitq);
475 } else if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
476 netif_wake_queue(priv->dev);
477 netif_wake_queue(priv->mesh_dev);
480 lbs_deb_leave(LBS_DEB_TX);
484 * @brief This function returns the network statistics
486 * @param dev A pointer to wlan_private structure
487 * @return A pointer to net_device_stats structure
489 static struct net_device_stats *libertas_get_stats(struct net_device *dev)
491 wlan_private *priv = (wlan_private *) dev->priv;
496 static int libertas_set_mac_address(struct net_device *dev, void *addr)
499 wlan_private *priv = (wlan_private *) dev->priv;
500 wlan_adapter *adapter = priv->adapter;
501 struct sockaddr *phwaddr = addr;
503 lbs_deb_enter(LBS_DEB_NET);
505 /* In case it was called from the mesh device */
508 memset(adapter->current_addr, 0, ETH_ALEN);
510 /* dev->dev_addr is 8 bytes */
511 lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
513 lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
514 memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
516 ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
518 CMD_OPTION_WAITFORRSP, 0, NULL);
521 lbs_deb_net("set MAC address failed\n");
526 lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
527 memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
529 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
532 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
536 static int libertas_copy_multicast_address(wlan_adapter * adapter,
537 struct net_device *dev)
540 struct dev_mc_list *mcptr = dev->mc_list;
542 for (i = 0; i < dev->mc_count; i++) {
543 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
551 static void libertas_set_multicast_list(struct net_device *dev)
553 wlan_private *priv = dev->priv;
554 wlan_adapter *adapter = priv->adapter;
557 lbs_deb_enter(LBS_DEB_NET);
559 oldpacketfilter = adapter->currentpacketfilter;
561 if (dev->flags & IFF_PROMISC) {
562 lbs_deb_net("enable promiscuous mode\n");
563 adapter->currentpacketfilter |=
564 CMD_ACT_MAC_PROMISCUOUS_ENABLE;
565 adapter->currentpacketfilter &=
566 ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
567 CMD_ACT_MAC_MULTICAST_ENABLE);
570 adapter->currentpacketfilter &=
571 ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
573 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
574 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
575 lbs_deb_net( "enabling all multicast\n");
576 adapter->currentpacketfilter |=
577 CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
578 adapter->currentpacketfilter &=
579 ~CMD_ACT_MAC_MULTICAST_ENABLE;
581 adapter->currentpacketfilter &=
582 ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
584 if (!dev->mc_count) {
585 lbs_deb_net("no multicast addresses, "
586 "disabling multicast\n");
587 adapter->currentpacketfilter &=
588 ~CMD_ACT_MAC_MULTICAST_ENABLE;
592 adapter->currentpacketfilter |=
593 CMD_ACT_MAC_MULTICAST_ENABLE;
595 adapter->nr_of_multicastmacaddr =
596 libertas_copy_multicast_address(adapter, dev);
598 lbs_deb_net("multicast addresses: %d\n",
601 for (i = 0; i < dev->mc_count; i++) {
602 lbs_deb_net("Multicast address %d:"
604 adapter->multicastlist[i][0],
605 adapter->multicastlist[i][1],
606 adapter->multicastlist[i][2],
607 adapter->multicastlist[i][3],
608 adapter->multicastlist[i][4],
609 adapter->multicastlist[i][5]);
611 /* send multicast addresses to firmware */
612 libertas_prepare_and_send_command(priv,
613 CMD_MAC_MULTICAST_ADR,
620 if (adapter->currentpacketfilter != oldpacketfilter) {
621 libertas_set_mac_packet_filter(priv);
624 lbs_deb_leave(LBS_DEB_NET);
628 * @brief This function handles the major jobs in the WLAN driver.
629 * It handles all events generated by firmware, RX data received
630 * from firmware and TX data sent from kernel.
632 * @param data A pointer to wlan_thread structure
635 static int libertas_thread(void *data)
637 struct net_device *dev = data;
638 wlan_private *priv = dev->priv;
639 wlan_adapter *adapter = priv->adapter;
643 lbs_deb_enter(LBS_DEB_THREAD);
645 init_waitqueue_entry(&wait, current);
648 lbs_deb_thread( "main-thread 111: intcounter=%d "
649 "currenttxskb=%p dnld_sent=%d\n",
651 adapter->currenttxskb, priv->dnld_sent);
653 add_wait_queue(&priv->waitq, &wait);
654 set_current_state(TASK_INTERRUPTIBLE);
655 spin_lock_irq(&adapter->driver_lock);
656 if ((adapter->psstate == PS_STATE_SLEEP) ||
657 (!adapter->intcounter
658 && (priv->dnld_sent || adapter->cur_cmd ||
659 list_empty(&adapter->cmdpendingq)))) {
661 "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
662 adapter->connect_status, adapter->intcounter,
663 adapter->psmode, adapter->psstate);
664 spin_unlock_irq(&adapter->driver_lock);
667 spin_unlock_irq(&adapter->driver_lock);
671 "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
672 "dnld_sent=%d\n", adapter->intcounter,
673 adapter->currenttxskb, priv->dnld_sent);
675 set_current_state(TASK_RUNNING);
676 remove_wait_queue(&priv->waitq, &wait);
679 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
682 adapter->currenttxskb, priv->dnld_sent);
684 if (kthread_should_stop()
685 || adapter->surpriseremoved) {
687 "main-thread: break from main thread: surpriseremoved=0x%x\n",
688 adapter->surpriseremoved);
693 spin_lock_irq(&adapter->driver_lock);
694 if (adapter->intcounter) {
696 adapter->intcounter = 0;
697 int_status = priv->hw_get_int_status(priv, &ireg);
701 "main-thread: reading HOST_INT_STATUS_REG failed\n");
702 spin_unlock_irq(&adapter->driver_lock);
705 adapter->hisregcpy |= ireg;
708 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
711 adapter->currenttxskb, priv->dnld_sent);
713 /* command response? */
714 if (adapter->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
715 lbs_deb_thread("main-thread: cmd response ready\n");
717 adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
718 spin_unlock_irq(&adapter->driver_lock);
719 libertas_process_rx_command(priv);
720 spin_lock_irq(&adapter->driver_lock);
724 if (adapter->hisregcpy & MRVDRV_CARDEVENT) {
725 lbs_deb_thread("main-thread: Card Event Activity\n");
727 adapter->hisregcpy &= ~MRVDRV_CARDEVENT;
729 if (priv->hw_read_event_cause(priv)) {
731 "main-thread: hw_read_event_cause failed\n");
732 spin_unlock_irq(&adapter->driver_lock);
735 spin_unlock_irq(&adapter->driver_lock);
736 libertas_process_event(priv);
738 spin_unlock_irq(&adapter->driver_lock);
740 /* Check if we need to confirm Sleep Request received previously */
741 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
742 if (!priv->dnld_sent && !adapter->cur_cmd) {
743 if (adapter->connect_status ==
744 LIBERTAS_CONNECTED) {
746 "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
747 "dnld_sent=%d cur_cmd=%p, confirm now\n",
749 adapter->currenttxskb,
753 libertas_ps_confirm_sleep(priv,
754 (u16) adapter->psmode);
756 /* workaround for firmware sending
757 * deauth/linkloss event immediately
758 * after sleep request, remove this
759 * after firmware fixes it
761 adapter->psstate = PS_STATE_AWAKE;
763 "main-thread: ignore PS_SleepConfirm in non-connected state\n");
768 /* The PS state is changed during processing of Sleep Request
771 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
772 (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
775 /* Execute the next command */
776 if (!priv->dnld_sent && !priv->adapter->cur_cmd)
777 libertas_execute_next_command(priv);
779 /* Wake-up command waiters which can't sleep in
780 * libertas_prepare_and_send_command
782 if (!adapter->nr_cmd_pending)
783 wake_up_all(&adapter->cmd_pending);
785 libertas_tx_runqueue(priv);
788 del_timer(&adapter->command_timer);
789 adapter->nr_cmd_pending = 0;
790 wake_up_all(&adapter->cmd_pending);
792 lbs_deb_leave(LBS_DEB_THREAD);
797 * @brief This function downloads firmware image, gets
798 * HW spec from firmware and set basic parameters to
801 * @param priv A pointer to wlan_private structure
804 static int wlan_setup_station_hw(wlan_private * priv)
807 wlan_adapter *adapter = priv->adapter;
809 lbs_deb_enter(LBS_DEB_FW);
811 ret = priv->hw_prog_firmware(priv);
814 lbs_deb_fw("bootloader in invalid state\n");
820 * Read MAC address from HW
822 memset(adapter->current_addr, 0xff, ETH_ALEN);
824 ret = libertas_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
825 0, CMD_OPTION_WAITFORRSP, 0, NULL);
832 libertas_set_mac_packet_filter(priv);
834 /* Get the supported Data rates */
835 ret = libertas_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
837 CMD_OPTION_WAITFORRSP, 0, NULL);
846 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
850 static void command_timer_fn(unsigned long data);
853 * This function handles the timeout of command sending.
854 * It will re-send the same command again.
856 static void command_timer_fn(unsigned long data)
858 wlan_private *priv = (wlan_private *)data;
859 wlan_adapter *adapter = priv->adapter;
860 struct cmd_ctrl_node *ptempnode;
861 struct cmd_ds_command *cmd;
864 ptempnode = adapter->cur_cmd;
865 if (ptempnode == NULL) {
866 lbs_deb_fw("ptempnode empty\n");
870 cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
872 lbs_deb_fw("cmd is NULL\n");
876 lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
878 if (!adapter->fw_ready)
881 spin_lock_irqsave(&adapter->driver_lock, flags);
882 adapter->cur_cmd = NULL;
883 spin_unlock_irqrestore(&adapter->driver_lock, flags);
885 lbs_deb_fw("re-sending same command because of timeout\n");
886 libertas_queue_cmd(adapter, ptempnode, 0);
888 wake_up_interruptible(&priv->waitq);
893 static void libertas_free_adapter(wlan_private * priv)
895 wlan_adapter *adapter = priv->adapter;
898 lbs_deb_fw("why double free adapter?\n");
902 lbs_deb_fw("free command buffer\n");
903 libertas_free_cmd_buffer(priv);
905 lbs_deb_fw("free command_timer\n");
906 del_timer(&adapter->command_timer);
908 lbs_deb_fw("free scan results table\n");
909 kfree(adapter->networks);
910 adapter->networks = NULL;
912 /* Free the adapter object itself */
913 lbs_deb_fw("free adapter\n");
915 priv->adapter = NULL;
918 static int wlan_allocate_adapter(wlan_private * priv)
921 wlan_adapter *adapter = priv->adapter;
923 /* Allocate buffer to store the BSSID list */
924 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
925 adapter->networks = kzalloc(bufsize, GFP_KERNEL);
926 if (!adapter->networks) {
927 lbs_pr_err("Out of memory allocating beacons\n");
928 libertas_free_adapter(priv);
932 /* Allocate the command buffers */
933 libertas_allocate_cmd_buffer(priv);
935 memset(&adapter->libertas_ps_confirm_sleep, 0, sizeof(struct PS_CMD_ConfirmSleep));
936 adapter->libertas_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
937 adapter->libertas_ps_confirm_sleep.command =
938 cpu_to_le16(CMD_802_11_PS_MODE);
939 adapter->libertas_ps_confirm_sleep.size =
940 cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
941 adapter->libertas_ps_confirm_sleep.result = 0;
942 adapter->libertas_ps_confirm_sleep.action =
943 cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
948 static void wlan_init_adapter(wlan_private * priv)
950 wlan_adapter *adapter = priv->adapter;
953 adapter->scanprobes = 0;
955 adapter->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
956 adapter->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
959 adapter->atimwindow = 0;
961 adapter->connect_status = LIBERTAS_DISCONNECTED;
962 memset(adapter->current_addr, 0xff, ETH_ALEN);
965 adapter->scantype = CMD_SCAN_TYPE_ACTIVE;
968 adapter->scanmode = CMD_BSS_TYPE_ANY;
970 /* 802.11 specific */
971 adapter->secinfo.wep_enabled = 0;
972 for (i = 0; i < sizeof(adapter->wep_keys) / sizeof(adapter->wep_keys[0]);
974 memset(&adapter->wep_keys[i], 0, sizeof(struct enc_key));
975 adapter->wep_tx_keyidx = 0;
976 adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
977 adapter->mode = IW_MODE_INFRA;
979 adapter->pending_assoc_req = NULL;
980 adapter->in_progress_assoc_req = NULL;
982 /* Initialize scan result lists */
983 INIT_LIST_HEAD(&adapter->network_free_list);
984 INIT_LIST_HEAD(&adapter->network_list);
985 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
986 list_add_tail(&adapter->networks[i].list,
987 &adapter->network_free_list);
990 mutex_init(&adapter->lock);
992 adapter->prescan = 1;
994 memset(&adapter->curbssparams, 0, sizeof(adapter->curbssparams));
995 adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
997 /* PnP and power profile */
998 adapter->surpriseremoved = 0;
1000 adapter->currentpacketfilter =
1001 CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
1003 adapter->radioon = RADIO_ON;
1004 adapter->txantenna = RF_ANTENNA_2;
1005 adapter->rxantenna = RF_ANTENNA_AUTO;
1007 adapter->auto_rate = 1;
1008 adapter->cur_rate = 0;
1009 adapter->adhoc_grate_enabled = 0;
1011 adapter->beaconperiod = MRVDRV_BEACON_INTERVAL;
1013 // set default capabilities
1014 adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1016 adapter->psmode = WLAN802_11POWERMODECAM;
1017 adapter->multipledtim = MRVDRV_DEFAULT_MULTIPLE_DTIM;
1019 adapter->listeninterval = MRVDRV_DEFAULT_LISTEN_INTERVAL;
1021 adapter->psstate = PS_STATE_FULL_POWER;
1022 adapter->needtowakeup = 0;
1023 adapter->locallisteninterval = 0; /* default value in firmware will be used */
1025 adapter->intcounter = 0;
1027 adapter->currenttxskb = NULL;
1028 adapter->pkttxctrl = 0;
1030 memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
1031 adapter->tx_queue_idx = 0;
1032 spin_lock_init(&adapter->txqueue_lock);
1037 static int libertas_init_fw(wlan_private * priv)
1040 wlan_adapter *adapter = priv->adapter;
1042 lbs_deb_enter(LBS_DEB_FW);
1044 /* Allocate adapter structure */
1045 if ((ret = wlan_allocate_adapter(priv)) != 0)
1048 /* init adapter structure */
1049 wlan_init_adapter(priv);
1051 /* init timer etc. */
1052 setup_timer(&adapter->command_timer, command_timer_fn,
1053 (unsigned long)priv);
1055 /* download fimrware etc. */
1056 if ((ret = wlan_setup_station_hw(priv)) != 0) {
1057 del_timer_sync(&adapter->command_timer);
1062 libertas_init_11d(priv);
1066 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
1071 * @brief This function adds the card. it will probe the
1072 * card, allocate the wlan_priv and initialize the device.
1074 * @param card A pointer to card
1075 * @return A pointer to wlan_private structure
1077 wlan_private *libertas_add_card(void *card, struct device *dmdev)
1079 struct net_device *dev = NULL;
1080 wlan_private *priv = NULL;
1082 lbs_deb_enter(LBS_DEB_NET);
1084 /* Allocate an Ethernet device and register it */
1085 if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
1086 lbs_pr_err("init ethX device failed\n");
1091 /* allocate buffer for wlan_adapter */
1092 if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
1093 lbs_pr_err("allocate buffer for wlan_adapter failed\n");
1099 priv->mesh_open = 0;
1100 priv->infra_open = 0;
1102 SET_MODULE_OWNER(dev);
1104 /* Setup the OS Interface to our functions */
1105 dev->open = libertas_open;
1106 dev->hard_start_xmit = libertas_pre_start_xmit;
1107 dev->stop = libertas_close;
1108 dev->set_mac_address = libertas_set_mac_address;
1109 dev->tx_timeout = libertas_tx_timeout;
1110 dev->get_stats = libertas_get_stats;
1111 dev->watchdog_timeo = 5 * HZ;
1112 dev->ethtool_ops = &libertas_ethtool_ops;
1114 dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
1116 #define NETIF_F_DYNALLOC 16
1117 dev->features |= NETIF_F_DYNALLOC;
1118 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1119 dev->set_multicast_list = libertas_set_multicast_list;
1121 SET_NETDEV_DEV(dev, dmdev);
1123 INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
1124 INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
1126 spin_lock_init(&priv->adapter->driver_lock);
1127 init_waitqueue_head(&priv->adapter->cmd_pending);
1128 priv->adapter->nr_cmd_pending = 0;
1135 lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
1138 EXPORT_SYMBOL_GPL(libertas_add_card);
1140 int libertas_activate_card(wlan_private *priv)
1142 struct net_device *dev = priv->dev;
1145 lbs_deb_enter(LBS_DEB_MAIN);
1147 lbs_deb_thread("Starting main thread...\n");
1148 init_waitqueue_head(&priv->waitq);
1149 priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
1150 if (IS_ERR(priv->main_thread)) {
1151 lbs_deb_thread("Error creating main thread.\n");
1155 priv->assoc_thread =
1156 create_singlethread_workqueue("libertas_assoc");
1157 INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
1158 INIT_WORK(&priv->sync_channel, libertas_sync_channel);
1161 * Register the device. Fillup the private data structure with
1162 * relevant information from the card and request for the required
1165 if (priv->hw_register_dev(priv) < 0) {
1166 lbs_pr_err("failed to register WLAN device\n");
1167 goto err_registerdev;
1170 /* init FW and HW */
1171 if (libertas_init_fw(priv)) {
1172 lbs_pr_err("firmware init failed\n");
1173 goto err_registerdev;
1176 if (register_netdev(dev)) {
1177 lbs_pr_err("cannot register ethX device\n");
1181 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1183 libertas_debugfs_init_one(priv, dev);
1189 priv->hw_unregister_dev(priv);
1191 destroy_workqueue(priv->assoc_thread);
1192 /* Stop the thread servicing the interrupts */
1193 wake_up_interruptible(&priv->waitq);
1194 kthread_stop(priv->main_thread);
1196 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
1199 EXPORT_SYMBOL_GPL(libertas_activate_card);
1203 * @brief This function adds mshX interface
1205 * @param priv A pointer to the wlan_private structure
1206 * @return 0 if successful, -X otherwise
1208 int libertas_add_mesh(wlan_private *priv, struct device *dev)
1210 struct net_device *mesh_dev = NULL;
1213 lbs_deb_enter(LBS_DEB_MESH);
1215 /* Allocate a virtual mesh device */
1216 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1217 lbs_deb_mesh("init mshX device failed\n");
1221 mesh_dev->priv = priv;
1222 priv->mesh_dev = mesh_dev;
1224 SET_MODULE_OWNER(mesh_dev);
1226 mesh_dev->open = libertas_mesh_open;
1227 mesh_dev->hard_start_xmit = libertas_mesh_pre_start_xmit;
1228 mesh_dev->stop = libertas_mesh_close;
1229 mesh_dev->get_stats = libertas_get_stats;
1230 mesh_dev->set_mac_address = libertas_set_mac_address;
1231 mesh_dev->ethtool_ops = &libertas_ethtool_ops;
1232 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1233 sizeof(priv->dev->dev_addr));
1235 SET_NETDEV_DEV(priv->mesh_dev, dev);
1238 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1240 #define NETIF_F_DYNALLOC 16
1242 /* Register virtual mesh interface */
1243 ret = register_netdev(mesh_dev);
1245 lbs_pr_err("cannot register mshX virtual interface\n");
1249 ret = device_create_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
1251 goto err_unregister;
1253 /* Everything successful */
1259 unregister_netdev(mesh_dev);
1262 free_netdev(mesh_dev);
1265 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1268 EXPORT_SYMBOL_GPL(libertas_add_mesh);
1270 static void wake_pending_cmdnodes(wlan_private *priv)
1272 struct cmd_ctrl_node *cmdnode;
1273 unsigned long flags;
1275 lbs_deb_enter(LBS_DEB_CMD);
1277 spin_lock_irqsave(&priv->adapter->driver_lock, flags);
1278 list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
1279 cmdnode->cmdwaitqwoken = 1;
1280 wake_up_interruptible(&cmdnode->cmdwait_q);
1282 spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
1286 int libertas_remove_card(wlan_private *priv)
1288 wlan_adapter *adapter;
1289 struct net_device *dev;
1290 union iwreq_data wrqu;
1292 lbs_deb_enter(LBS_DEB_NET);
1297 adapter = priv->adapter;
1304 netif_stop_queue(priv->dev);
1305 netif_carrier_off(priv->dev);
1307 wake_pending_cmdnodes(priv);
1309 unregister_netdev(dev);
1311 cancel_delayed_work(&priv->assoc_work);
1312 destroy_workqueue(priv->assoc_thread);
1314 if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
1315 adapter->psmode = WLAN802_11POWERMODECAM;
1316 libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1319 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1320 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1321 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1323 adapter->surpriseremoved = 1;
1325 /* Stop the thread servicing the interrupts */
1326 kthread_stop(priv->main_thread);
1328 libertas_debugfs_remove_one(priv);
1330 lbs_deb_net("free adapter\n");
1331 libertas_free_adapter(priv);
1333 lbs_deb_net("unregister finish\n");
1339 lbs_deb_leave(LBS_DEB_NET);
1342 EXPORT_SYMBOL_GPL(libertas_remove_card);
1345 void libertas_remove_mesh(wlan_private *priv)
1347 struct net_device *mesh_dev;
1349 lbs_deb_enter(LBS_DEB_NET);
1354 mesh_dev = priv->mesh_dev;
1356 netif_stop_queue(mesh_dev);
1357 netif_carrier_off(priv->mesh_dev);
1359 device_remove_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
1360 unregister_netdev(mesh_dev);
1362 priv->mesh_dev = NULL ;
1363 free_netdev(mesh_dev);
1366 lbs_deb_leave(LBS_DEB_NET);
1368 EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1371 * @brief This function finds the CFP in
1372 * region_cfp_table based on region and band parameter.
1374 * @param region The region code
1375 * @param band The band
1376 * @param cfp_no A pointer to CFP number
1377 * @return A pointer to CFP
1379 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1383 lbs_deb_enter(LBS_DEB_MAIN);
1385 end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1387 for (i = 0; i < end ; i++) {
1388 lbs_deb_main("region_cfp_table[i].region=%d\n",
1389 region_cfp_table[i].region);
1390 if (region_cfp_table[i].region == region) {
1391 *cfp_no = region_cfp_table[i].cfp_no_BG;
1392 lbs_deb_leave(LBS_DEB_MAIN);
1393 return region_cfp_table[i].cfp_BG;
1397 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1401 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1403 wlan_adapter *adapter = priv->adapter;
1407 struct chan_freq_power *cfp;
1410 lbs_deb_enter(LBS_DEB_MAIN);
1412 memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1415 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1417 adapter->region_channel[i].nrcfp = cfp_no;
1418 adapter->region_channel[i].CFP = cfp;
1420 lbs_deb_main("wrong region code %#x in band B/G\n",
1425 adapter->region_channel[i].valid = 1;
1426 adapter->region_channel[i].region = region;
1427 adapter->region_channel[i].band = band;
1431 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1436 * @brief This function handles the interrupt. it will change PS
1437 * state if applicable. it will wake up main_thread to handle
1438 * the interrupt event as well.
1440 * @param dev A pointer to net_device structure
1443 void libertas_interrupt(struct net_device *dev)
1445 wlan_private *priv = dev->priv;
1447 lbs_deb_enter(LBS_DEB_THREAD);
1449 lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1450 priv->adapter->intcounter);
1452 priv->adapter->intcounter++;
1454 if (priv->adapter->psstate == PS_STATE_SLEEP) {
1455 priv->adapter->psstate = PS_STATE_AWAKE;
1456 netif_wake_queue(dev);
1457 netif_wake_queue(priv->mesh_dev);
1460 wake_up_interruptible(&priv->waitq);
1462 lbs_deb_leave(LBS_DEB_THREAD);
1464 EXPORT_SYMBOL_GPL(libertas_interrupt);
1466 int libertas_reset_device(wlan_private *priv)
1470 lbs_deb_enter(LBS_DEB_MAIN);
1471 ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
1472 CMD_ACT_HALT, 0, 0, NULL);
1473 msleep_interruptible(10);
1475 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1478 EXPORT_SYMBOL_GPL(libertas_reset_device);
1480 static int libertas_init_module(void)
1482 lbs_deb_enter(LBS_DEB_MAIN);
1483 libertas_debugfs_init();
1484 lbs_deb_leave(LBS_DEB_MAIN);
1488 static void libertas_exit_module(void)
1490 lbs_deb_enter(LBS_DEB_MAIN);
1492 libertas_debugfs_remove();
1494 lbs_deb_leave(LBS_DEB_MAIN);
1497 module_init(libertas_init_module);
1498 module_exit(libertas_exit_module);
1500 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1501 MODULE_AUTHOR("Marvell International Ltd.");
1502 MODULE_LICENSE("GPL");