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 rates supported
155 u8 libertas_supported_rates[G_SUPPORTED_RATES] =
156 { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
160 * the rates supported for ad-hoc G mode
162 u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
163 { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
167 * the rates supported for ad-hoc B mode
169 u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
172 * the table to keep region code
174 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
175 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
178 * Attributes exported through sysfs
182 * @brief Get function for sysfs attribute anycast_mask
184 static ssize_t libertas_anycast_get(struct device * dev,
185 struct device_attribute *attr, char * buf)
187 struct cmd_ds_mesh_access mesh_access;
189 memset(&mesh_access, 0, sizeof(mesh_access));
190 libertas_prepare_and_send_command(to_net_dev(dev)->priv,
192 CMD_ACT_MESH_GET_ANYCAST,
193 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
195 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
199 * @brief Set function for sysfs attribute anycast_mask
201 static ssize_t libertas_anycast_set(struct device * dev,
202 struct device_attribute *attr, const char * buf, size_t count)
204 struct cmd_ds_mesh_access mesh_access;
207 memset(&mesh_access, 0, sizeof(mesh_access));
208 sscanf(buf, "%x", &datum);
209 mesh_access.data[0] = cpu_to_le32(datum);
211 libertas_prepare_and_send_command((to_net_dev(dev))->priv,
213 CMD_ACT_MESH_SET_ANYCAST,
214 CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
219 * anycast_mask attribute to be exported per mshX interface
220 * through sysfs (/sys/class/net/mshX/anycast_mask)
222 static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
225 * @brief Check if the device can be open and wait if necessary.
227 * @param dev A pointer to net_device structure
230 * For USB adapter, on some systems the device open handler will be
231 * called before FW ready. Use the following flag check and wait
232 * function to work around the issue.
235 static int pre_open_check(struct net_device *dev)
237 wlan_private *priv = (wlan_private *) dev->priv;
238 wlan_adapter *adapter = priv->adapter;
241 while (!adapter->fw_ready && i < 20) {
243 msleep_interruptible(100);
245 if (!adapter->fw_ready) {
246 lbs_pr_err("firmware not ready\n");
254 * @brief This function opens the device
256 * @param dev A pointer to net_device structure
259 static int wlan_dev_open(struct net_device *dev)
261 wlan_private *priv = (wlan_private *) dev->priv;
262 wlan_adapter *adapter = priv->adapter;
264 lbs_deb_enter(LBS_DEB_NET);
268 if (adapter->connect_status == LIBERTAS_CONNECTED) {
269 netif_carrier_on(priv->dev);
270 netif_carrier_on(priv->mesh_dev);
272 netif_carrier_off(priv->dev);
273 netif_carrier_off(priv->mesh_dev);
276 lbs_deb_leave(LBS_DEB_NET);
280 * @brief This function opens the mshX interface
282 * @param dev A pointer to net_device structure
285 static int mesh_open(struct net_device *dev)
287 wlan_private *priv = (wlan_private *) dev->priv ;
289 if (pre_open_check(dev) == -1)
291 priv->mesh_open = 1 ;
292 netif_wake_queue(priv->mesh_dev);
293 if (priv->infra_open == 0)
294 return wlan_dev_open(priv->dev) ;
299 * @brief This function opens the ethX interface
301 * @param dev A pointer to net_device structure
304 static int wlan_open(struct net_device *dev)
306 wlan_private *priv = (wlan_private *) dev->priv ;
308 if(pre_open_check(dev) == -1)
310 priv->infra_open = 1 ;
311 netif_wake_queue(priv->dev);
313 return wlan_dev_open(priv->dev) ;
317 static int wlan_dev_close(struct net_device *dev)
319 wlan_private *priv = dev->priv;
321 lbs_deb_enter(LBS_DEB_NET);
323 netif_carrier_off(priv->dev);
326 lbs_deb_leave(LBS_DEB_NET);
331 * @brief This function closes the mshX interface
333 * @param dev A pointer to net_device structure
336 static int mesh_close(struct net_device *dev)
338 wlan_private *priv = (wlan_private *) (dev->priv);
341 netif_stop_queue(priv->mesh_dev);
342 if (priv->infra_open == 0)
343 return wlan_dev_close(dev);
349 * @brief This function closes the ethX interface
351 * @param dev A pointer to net_device structure
354 static int wlan_close(struct net_device *dev)
356 wlan_private *priv = (wlan_private *) dev->priv;
358 netif_stop_queue(dev);
359 priv->infra_open = 0;
360 if (priv->mesh_open == 0)
361 return wlan_dev_close(dev);
367 static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
370 wlan_private *priv = dev->priv;
372 lbs_deb_enter(LBS_DEB_NET);
374 if (priv->dnld_sent || priv->adapter->TxLockFlag) {
375 priv->stats.tx_dropped++;
379 netif_stop_queue(priv->dev);
380 netif_stop_queue(priv->mesh_dev);
382 if (libertas_process_tx(priv, skb) == 0)
383 dev->trans_start = jiffies;
385 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
390 * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
393 static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
395 wlan_private *priv = dev->priv;
398 lbs_deb_enter(LBS_DEB_MESH);
402 ret = wlan_hard_start_xmit(skb, priv->dev);
403 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
408 * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
411 static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
415 lbs_deb_enter(LBS_DEB_NET);
417 UNSET_MESH_FRAME(skb);
419 ret = wlan_hard_start_xmit(skb, dev);
420 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
424 static void wlan_tx_timeout(struct net_device *dev)
426 wlan_private *priv = (wlan_private *) dev->priv;
428 lbs_deb_enter(LBS_DEB_TX);
430 lbs_pr_err("tx watch dog timeout\n");
432 priv->dnld_sent = DNLD_RES_RECEIVED;
433 dev->trans_start = jiffies;
435 if (priv->adapter->currenttxskb) {
436 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
437 /* If we are here, we have not received feedback from
438 the previous packet. Assume TX_FAIL and move on. */
439 priv->adapter->eventcause = 0x01000000;
440 libertas_send_tx_feedback(priv);
442 wake_up_interruptible(&priv->waitq);
443 } else if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
444 netif_wake_queue(priv->dev);
445 netif_wake_queue(priv->mesh_dev);
448 lbs_deb_leave(LBS_DEB_TX);
452 * @brief This function returns the network statistics
454 * @param dev A pointer to wlan_private structure
455 * @return A pointer to net_device_stats structure
457 static struct net_device_stats *wlan_get_stats(struct net_device *dev)
459 wlan_private *priv = (wlan_private *) dev->priv;
464 static int wlan_set_mac_address(struct net_device *dev, void *addr)
467 wlan_private *priv = (wlan_private *) dev->priv;
468 wlan_adapter *adapter = priv->adapter;
469 struct sockaddr *phwaddr = addr;
471 lbs_deb_enter(LBS_DEB_NET);
473 /* In case it was called from the mesh device */
476 memset(adapter->current_addr, 0, ETH_ALEN);
478 /* dev->dev_addr is 8 bytes */
479 lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
481 lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
482 memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
484 ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
486 CMD_OPTION_WAITFORRSP, 0, NULL);
489 lbs_deb_net("set MAC address failed\n");
494 lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
495 memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
497 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
500 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
504 static int wlan_copy_multicast_address(wlan_adapter * adapter,
505 struct net_device *dev)
508 struct dev_mc_list *mcptr = dev->mc_list;
510 for (i = 0; i < dev->mc_count; i++) {
511 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
519 static void wlan_set_multicast_list(struct net_device *dev)
521 wlan_private *priv = dev->priv;
522 wlan_adapter *adapter = priv->adapter;
525 lbs_deb_enter(LBS_DEB_NET);
527 oldpacketfilter = adapter->currentpacketfilter;
529 if (dev->flags & IFF_PROMISC) {
530 lbs_deb_net("enable promiscuous mode\n");
531 adapter->currentpacketfilter |=
532 CMD_ACT_MAC_PROMISCUOUS_ENABLE;
533 adapter->currentpacketfilter &=
534 ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
535 CMD_ACT_MAC_MULTICAST_ENABLE);
538 adapter->currentpacketfilter &=
539 ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
541 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
542 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
543 lbs_deb_net( "enabling all multicast\n");
544 adapter->currentpacketfilter |=
545 CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
546 adapter->currentpacketfilter &=
547 ~CMD_ACT_MAC_MULTICAST_ENABLE;
549 adapter->currentpacketfilter &=
550 ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
552 if (!dev->mc_count) {
553 lbs_deb_net("no multicast addresses, "
554 "disabling multicast\n");
555 adapter->currentpacketfilter &=
556 ~CMD_ACT_MAC_MULTICAST_ENABLE;
560 adapter->currentpacketfilter |=
561 CMD_ACT_MAC_MULTICAST_ENABLE;
563 adapter->nr_of_multicastmacaddr =
564 wlan_copy_multicast_address(adapter, dev);
566 lbs_deb_net("multicast addresses: %d\n",
569 for (i = 0; i < dev->mc_count; i++) {
570 lbs_deb_net("Multicast address %d:"
572 adapter->multicastlist[i][0],
573 adapter->multicastlist[i][1],
574 adapter->multicastlist[i][2],
575 adapter->multicastlist[i][3],
576 adapter->multicastlist[i][4],
577 adapter->multicastlist[i][5]);
579 /* send multicast addresses to firmware */
580 libertas_prepare_and_send_command(priv,
581 CMD_MAC_MULTICAST_ADR,
588 if (adapter->currentpacketfilter != oldpacketfilter) {
589 libertas_set_mac_packet_filter(priv);
592 lbs_deb_leave(LBS_DEB_NET);
596 * @brief This function handles the major jobs in the WLAN driver.
597 * It handles all events generated by firmware, RX data received
598 * from firmware and TX data sent from kernel.
600 * @param data A pointer to wlan_thread structure
603 static int libertas_thread(void *data)
605 struct net_device *dev = data;
606 wlan_private *priv = dev->priv;
607 wlan_adapter *adapter = priv->adapter;
611 lbs_deb_enter(LBS_DEB_THREAD);
613 init_waitqueue_entry(&wait, current);
616 lbs_deb_thread( "main-thread 111: intcounter=%d "
617 "currenttxskb=%p dnld_sent=%d\n",
619 adapter->currenttxskb, priv->dnld_sent);
621 add_wait_queue(&priv->waitq, &wait);
622 set_current_state(TASK_INTERRUPTIBLE);
623 spin_lock_irq(&adapter->driver_lock);
624 if ((adapter->psstate == PS_STATE_SLEEP) ||
625 (!adapter->intcounter
626 && (priv->dnld_sent || adapter->cur_cmd ||
627 list_empty(&adapter->cmdpendingq)))) {
629 "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
630 adapter->connect_status, adapter->intcounter,
631 adapter->psmode, adapter->psstate);
632 spin_unlock_irq(&adapter->driver_lock);
635 spin_unlock_irq(&adapter->driver_lock);
639 "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
640 "dnld_sent=%d\n", adapter->intcounter,
641 adapter->currenttxskb, priv->dnld_sent);
643 set_current_state(TASK_RUNNING);
644 remove_wait_queue(&priv->waitq, &wait);
647 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
650 adapter->currenttxskb, priv->dnld_sent);
652 if (kthread_should_stop()
653 || adapter->surpriseremoved) {
655 "main-thread: break from main thread: surpriseremoved=0x%x\n",
656 adapter->surpriseremoved);
661 spin_lock_irq(&adapter->driver_lock);
662 if (adapter->intcounter) {
664 adapter->intcounter = 0;
665 int_status = priv->hw_get_int_status(priv, &ireg);
669 "main-thread: reading HOST_INT_STATUS_REG failed\n");
670 spin_unlock_irq(&adapter->driver_lock);
673 adapter->hisregcpy |= ireg;
676 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
679 adapter->currenttxskb, priv->dnld_sent);
681 /* command response? */
682 if (adapter->hisregcpy & his_cmdupldrdy) {
683 lbs_deb_thread("main-thread: cmd response ready\n");
685 adapter->hisregcpy &= ~his_cmdupldrdy;
686 spin_unlock_irq(&adapter->driver_lock);
687 libertas_process_rx_command(priv);
688 spin_lock_irq(&adapter->driver_lock);
692 if (adapter->hisregcpy & his_cardevent) {
693 lbs_deb_thread("main-thread: Card Event Activity\n");
695 adapter->hisregcpy &= ~his_cardevent;
697 if (priv->hw_read_event_cause(priv)) {
699 "main-thread: hw_read_event_cause failed\n");
700 spin_unlock_irq(&adapter->driver_lock);
703 spin_unlock_irq(&adapter->driver_lock);
704 libertas_process_event(priv);
706 spin_unlock_irq(&adapter->driver_lock);
708 /* Check if we need to confirm Sleep Request received previously */
709 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
710 if (!priv->dnld_sent && !adapter->cur_cmd) {
711 if (adapter->connect_status ==
712 LIBERTAS_CONNECTED) {
714 "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
715 "dnld_sent=%d cur_cmd=%p, confirm now\n",
717 adapter->currenttxskb,
721 libertas_ps_confirm_sleep(priv,
722 (u16) adapter->psmode);
724 /* workaround for firmware sending
725 * deauth/linkloss event immediately
726 * after sleep request, remove this
727 * after firmware fixes it
729 adapter->psstate = PS_STATE_AWAKE;
731 "main-thread: ignore PS_SleepConfirm in non-connected state\n");
736 /* The PS state is changed during processing of Sleep Request
739 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
740 (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
743 /* Execute the next command */
744 if (!priv->dnld_sent && !priv->adapter->cur_cmd)
745 libertas_execute_next_command(priv);
747 /* Wake-up command waiters which can't sleep in
748 * libertas_prepare_and_send_command
750 if (!adapter->nr_cmd_pending)
751 wake_up_all(&adapter->cmd_pending);
753 libertas_tx_runqueue(priv);
756 del_timer(&adapter->command_timer);
757 adapter->nr_cmd_pending = 0;
758 wake_up_all(&adapter->cmd_pending);
760 lbs_deb_leave(LBS_DEB_THREAD);
765 * @brief This function adds the card. it will probe the
766 * card, allocate the wlan_priv and initialize the device.
768 * @param card A pointer to card
769 * @return A pointer to wlan_private structure
771 wlan_private *libertas_add_card(void *card, struct device *dmdev)
773 struct net_device *dev = NULL;
774 wlan_private *priv = NULL;
776 lbs_deb_enter(LBS_DEB_NET);
778 /* Allocate an Ethernet device and register it */
779 if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
780 lbs_pr_err("init ethX device failed\n");
785 /* allocate buffer for wlan_adapter */
786 if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
787 lbs_pr_err("allocate buffer for wlan_adapter failed\n");
794 priv->infra_open = 0;
796 SET_MODULE_OWNER(dev);
798 /* Setup the OS Interface to our functions */
799 dev->open = wlan_open;
800 dev->hard_start_xmit = wlan_pre_start_xmit;
801 dev->stop = wlan_close;
802 dev->set_mac_address = wlan_set_mac_address;
803 dev->tx_timeout = wlan_tx_timeout;
804 dev->get_stats = wlan_get_stats;
805 dev->watchdog_timeo = 5 * HZ;
806 dev->ethtool_ops = &libertas_ethtool_ops;
808 dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
810 #define NETIF_F_DYNALLOC 16
811 dev->features |= NETIF_F_DYNALLOC;
812 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
813 dev->set_multicast_list = wlan_set_multicast_list;
815 SET_NETDEV_DEV(dev, dmdev);
817 INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
818 INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
820 spin_lock_init(&priv->adapter->driver_lock);
821 init_waitqueue_head(&priv->adapter->cmd_pending);
822 priv->adapter->nr_cmd_pending = 0;
829 lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
832 EXPORT_SYMBOL_GPL(libertas_add_card);
834 int libertas_activate_card(wlan_private *priv, char *fw_name)
836 struct net_device *dev = priv->dev;
839 lbs_deb_enter(LBS_DEB_MAIN);
841 lbs_deb_thread("Starting main thread...\n");
842 init_waitqueue_head(&priv->waitq);
843 priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
844 if (IS_ERR(priv->main_thread)) {
845 lbs_deb_thread("Error creating main thread.\n");
850 create_singlethread_workqueue("libertas_assoc");
851 INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
852 INIT_WORK(&priv->sync_channel, libertas_sync_channel);
855 * Register the device. Fillup the private data structure with
856 * relevant information from the card and request for the required
859 if (priv->hw_register_dev(priv) < 0) {
860 lbs_pr_err("failed to register WLAN device\n");
861 goto err_registerdev;
865 if (fw_name && libertas_init_fw(priv, fw_name)) {
866 lbs_pr_err("firmware init failed\n");
867 goto err_registerdev;
870 if (register_netdev(dev)) {
871 lbs_pr_err("cannot register ethX device\n");
875 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
877 libertas_debugfs_init_one(priv, dev);
883 priv->hw_unregister_dev(priv);
885 destroy_workqueue(priv->assoc_thread);
886 /* Stop the thread servicing the interrupts */
887 wake_up_interruptible(&priv->waitq);
888 kthread_stop(priv->main_thread);
890 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
893 EXPORT_SYMBOL_GPL(libertas_activate_card);
897 * @brief This function adds mshX interface
899 * @param priv A pointer to the wlan_private structure
900 * @return 0 if successful, -X otherwise
902 int libertas_add_mesh(wlan_private *priv, struct device *dev)
904 struct net_device *mesh_dev = NULL;
907 lbs_deb_enter(LBS_DEB_MESH);
909 /* Allocate a virtual mesh device */
910 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
911 lbs_deb_mesh("init mshX device failed\n");
915 mesh_dev->priv = priv;
916 priv->mesh_dev = mesh_dev;
918 SET_MODULE_OWNER(mesh_dev);
920 mesh_dev->open = mesh_open;
921 mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
922 mesh_dev->stop = mesh_close;
923 mesh_dev->get_stats = wlan_get_stats;
924 mesh_dev->set_mac_address = wlan_set_mac_address;
925 mesh_dev->ethtool_ops = &libertas_ethtool_ops;
926 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
927 sizeof(priv->dev->dev_addr));
929 SET_NETDEV_DEV(priv->mesh_dev, dev);
932 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
934 #define NETIF_F_DYNALLOC 16
936 /* Register virtual mesh interface */
937 ret = register_netdev(mesh_dev);
939 lbs_pr_err("cannot register mshX virtual interface\n");
943 ret = device_create_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
947 /* Everything successful */
953 unregister_netdev(mesh_dev);
956 free_netdev(mesh_dev);
959 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
962 EXPORT_SYMBOL_GPL(libertas_add_mesh);
964 static void wake_pending_cmdnodes(wlan_private *priv)
966 struct cmd_ctrl_node *cmdnode;
969 lbs_deb_enter(LBS_DEB_CMD);
971 spin_lock_irqsave(&priv->adapter->driver_lock, flags);
972 list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
973 cmdnode->cmdwaitqwoken = 1;
974 wake_up_interruptible(&cmdnode->cmdwait_q);
976 spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
980 int libertas_remove_card(wlan_private *priv)
982 wlan_adapter *adapter;
983 struct net_device *dev;
984 union iwreq_data wrqu;
986 lbs_deb_enter(LBS_DEB_NET);
991 adapter = priv->adapter;
998 netif_stop_queue(priv->dev);
999 netif_carrier_off(priv->dev);
1001 wake_pending_cmdnodes(priv);
1003 unregister_netdev(dev);
1005 cancel_delayed_work(&priv->assoc_work);
1006 destroy_workqueue(priv->assoc_thread);
1008 if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
1009 adapter->psmode = WLAN802_11POWERMODECAM;
1010 libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1013 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1014 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1015 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1017 adapter->surpriseremoved = 1;
1019 /* Stop the thread servicing the interrupts */
1020 kthread_stop(priv->main_thread);
1022 libertas_debugfs_remove_one(priv);
1024 lbs_deb_net("free adapter\n");
1025 libertas_free_adapter(priv);
1027 lbs_deb_net("unregister finish\n");
1033 lbs_deb_leave(LBS_DEB_NET);
1036 EXPORT_SYMBOL_GPL(libertas_remove_card);
1039 void libertas_remove_mesh(wlan_private *priv)
1041 struct net_device *mesh_dev;
1043 lbs_deb_enter(LBS_DEB_NET);
1048 mesh_dev = priv->mesh_dev;
1050 netif_stop_queue(mesh_dev);
1051 netif_carrier_off(priv->mesh_dev);
1053 device_remove_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
1054 unregister_netdev(mesh_dev);
1056 priv->mesh_dev = NULL ;
1057 free_netdev(mesh_dev);
1060 lbs_deb_leave(LBS_DEB_NET);
1062 EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1065 * @brief This function finds the CFP in
1066 * region_cfp_table based on region and band parameter.
1068 * @param region The region code
1069 * @param band The band
1070 * @param cfp_no A pointer to CFP number
1071 * @return A pointer to CFP
1073 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1077 lbs_deb_enter(LBS_DEB_MAIN);
1079 end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1081 for (i = 0; i < end ; i++) {
1082 lbs_deb_main("region_cfp_table[i].region=%d\n",
1083 region_cfp_table[i].region);
1084 if (region_cfp_table[i].region == region) {
1085 *cfp_no = region_cfp_table[i].cfp_no_BG;
1086 lbs_deb_leave(LBS_DEB_MAIN);
1087 return region_cfp_table[i].cfp_BG;
1091 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1095 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1097 wlan_adapter *adapter = priv->adapter;
1101 struct chan_freq_power *cfp;
1104 lbs_deb_enter(LBS_DEB_MAIN);
1106 memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1109 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1111 adapter->region_channel[i].nrcfp = cfp_no;
1112 adapter->region_channel[i].CFP = cfp;
1114 lbs_deb_main("wrong region code %#x in band B/G\n",
1119 adapter->region_channel[i].valid = 1;
1120 adapter->region_channel[i].region = region;
1121 adapter->region_channel[i].band = band;
1125 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1130 * @brief This function handles the interrupt. it will change PS
1131 * state if applicable. it will wake up main_thread to handle
1132 * the interrupt event as well.
1134 * @param dev A pointer to net_device structure
1137 void libertas_interrupt(struct net_device *dev)
1139 wlan_private *priv = dev->priv;
1141 lbs_deb_enter(LBS_DEB_THREAD);
1143 lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1144 priv->adapter->intcounter);
1146 priv->adapter->intcounter++;
1148 if (priv->adapter->psstate == PS_STATE_SLEEP) {
1149 priv->adapter->psstate = PS_STATE_AWAKE;
1150 netif_wake_queue(dev);
1151 netif_wake_queue(priv->mesh_dev);
1154 wake_up_interruptible(&priv->waitq);
1156 lbs_deb_leave(LBS_DEB_THREAD);
1158 EXPORT_SYMBOL_GPL(libertas_interrupt);
1160 static int libertas_init_module(void)
1162 lbs_deb_enter(LBS_DEB_MAIN);
1163 libertas_debugfs_init();
1164 lbs_deb_leave(LBS_DEB_MAIN);
1168 static void libertas_exit_module(void)
1170 lbs_deb_enter(LBS_DEB_MAIN);
1172 libertas_debugfs_remove();
1174 lbs_deb_leave(LBS_DEB_MAIN);
1177 module_init(libertas_init_module);
1178 module_exit(libertas_exit_module);
1180 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1181 MODULE_AUTHOR("Marvell International Ltd.");
1182 MODULE_LICENSE("GPL");