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>
14 #include <net/iw_handler.h>
15 #include <net/ieee80211.h>
26 #define DRIVER_RELEASE_VERSION "320.p0"
27 const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
34 /* Module parameters */
35 unsigned int libertas_debug = 0;
36 module_param(libertas_debug, int, 0644);
40 #define WLAN_TX_PWR_DEFAULT 20 /*100mW */
41 #define WLAN_TX_PWR_US_DEFAULT 20 /*100mW */
42 #define WLAN_TX_PWR_JP_DEFAULT 16 /*50mW */
43 #define WLAN_TX_PWR_FR_DEFAULT 20 /*100mW */
44 #define WLAN_TX_PWR_EMEA_DEFAULT 20 /*100mW */
46 /* Format { channel, frequency (MHz), maxtxpower } */
47 /* band: 'B/G', region: USA FCC/Canada IC */
48 static struct chan_freq_power channel_freq_power_US_BG[] = {
49 {1, 2412, WLAN_TX_PWR_US_DEFAULT},
50 {2, 2417, WLAN_TX_PWR_US_DEFAULT},
51 {3, 2422, WLAN_TX_PWR_US_DEFAULT},
52 {4, 2427, WLAN_TX_PWR_US_DEFAULT},
53 {5, 2432, WLAN_TX_PWR_US_DEFAULT},
54 {6, 2437, WLAN_TX_PWR_US_DEFAULT},
55 {7, 2442, WLAN_TX_PWR_US_DEFAULT},
56 {8, 2447, WLAN_TX_PWR_US_DEFAULT},
57 {9, 2452, WLAN_TX_PWR_US_DEFAULT},
58 {10, 2457, WLAN_TX_PWR_US_DEFAULT},
59 {11, 2462, WLAN_TX_PWR_US_DEFAULT}
62 /* band: 'B/G', region: Europe ETSI */
63 static struct chan_freq_power channel_freq_power_EU_BG[] = {
64 {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
65 {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
66 {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
67 {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
68 {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
69 {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
70 {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
71 {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
72 {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
73 {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
74 {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
75 {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
76 {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
79 /* band: 'B/G', region: Spain */
80 static struct chan_freq_power channel_freq_power_SPN_BG[] = {
81 {10, 2457, WLAN_TX_PWR_DEFAULT},
82 {11, 2462, WLAN_TX_PWR_DEFAULT}
85 /* band: 'B/G', region: France */
86 static struct chan_freq_power channel_freq_power_FR_BG[] = {
87 {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
88 {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
89 {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
90 {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
93 /* band: 'B/G', region: Japan */
94 static struct chan_freq_power channel_freq_power_JPN_BG[] = {
95 {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
96 {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
97 {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
98 {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
99 {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
100 {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
101 {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
102 {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
103 {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
104 {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
105 {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
106 {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
107 {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
108 {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
112 * the structure for channel, frequency and power
114 struct region_cfp_table {
116 struct chan_freq_power *cfp_BG;
121 * the structure for the mapping between region and CFP
123 static struct region_cfp_table region_cfp_table[] = {
125 channel_freq_power_US_BG,
126 sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
129 {0x20, /*CANADA IC */
130 channel_freq_power_US_BG,
131 sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
134 {0x30, /*EU*/ channel_freq_power_EU_BG,
135 sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
138 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
139 sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
142 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
143 sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
146 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
147 sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
150 /*Add new region here */
154 * the rates supported
156 u8 libertas_supported_rates[G_SUPPORTED_RATES] =
157 { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
161 * the rates supported for ad-hoc G mode
163 u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
164 { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
168 * the rates supported for ad-hoc B mode
170 u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
173 static struct net_device *libertas_devs[MAX_DEVS];
174 static int libertas_found = 0;
177 * the table to keep region code
179 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
180 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
182 static u8 *default_fw_name = "usb8388.bin";
185 * Attributes exported through sysfs
189 * @brief Get function for sysfs attribute libertas_mpp
191 static ssize_t libertas_mpp_get(struct device * dev,
192 struct device_attribute *attr, char * buf) {
193 struct cmd_ds_mesh_access mesh_access;
195 memset(&mesh_access, 0, sizeof(mesh_access));
196 libertas_prepare_and_send_command(to_net_dev(dev)->priv,
198 cmd_act_mesh_get_mpp,
199 cmd_option_waitforrsp, 0, (void *)&mesh_access);
201 return snprintf(buf, 3, "%d\n", mesh_access.data[0]);
205 * @brief Set function for sysfs attribute libertas_mpp
207 static ssize_t libertas_mpp_set(struct device * dev,
208 struct device_attribute *attr, const char * buf, size_t count) {
209 struct cmd_ds_mesh_access mesh_access;
211 memset(&mesh_access, 0, sizeof(mesh_access));
212 sscanf(buf, "%d", &(mesh_access.data[0]));
213 libertas_prepare_and_send_command((to_net_dev(dev))->priv,
215 cmd_act_mesh_set_mpp,
216 cmd_option_waitforrsp, 0, (void *)&mesh_access);
221 * libertas_mpp attribute to be exported per mshX interface
222 * through sysfs (/sys/class/net/mshX/libertas-mpp)
224 static DEVICE_ATTR(libertas_mpp, 0644, libertas_mpp_get,
228 * @brief Check if the device can be open and wait if necessary.
230 * @param dev A pointer to net_device structure
233 * For USB adapter, on some systems the device open handler will be
234 * called before FW ready. Use the following flag check and wait
235 * function to work around the issue.
238 static int pre_open_check(struct net_device *dev)
240 wlan_private *priv = (wlan_private *) dev->priv;
241 wlan_adapter *adapter = priv->adapter;
244 while (!adapter->fw_ready && i < 20) {
246 msleep_interruptible(100);
248 if (!adapter->fw_ready) {
249 lbs_pr_err("firmware not ready\n");
257 * @brief This function opens the device
259 * @param dev A pointer to net_device structure
262 static int wlan_dev_open(struct net_device *dev)
264 wlan_private *priv = (wlan_private *) dev->priv;
265 wlan_adapter *adapter = priv->adapter;
267 lbs_deb_enter(LBS_DEB_NET);
271 if (adapter->connect_status == libertas_connected) {
272 netif_carrier_on(priv->wlan_dev.netdev);
274 netif_carrier_off(priv->wlan_dev.netdev);
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_start_queue(priv->mesh_dev);
293 if (priv->infra_open == 0)
294 return wlan_dev_open(priv->wlan_dev.netdev) ;
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->wlan_dev.netdev);
313 return wlan_dev_open(priv->wlan_dev.netdev) ;
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->wlan_dev.netdev);
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( ((wlan_private *) dev->priv)->wlan_dev.netdev) ;
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(priv->wlan_dev.netdev);
359 priv->infra_open = 0;
360 if (priv->mesh_open == 0)
361 return wlan_dev_close( ((wlan_private *) dev->priv)->wlan_dev.netdev) ;
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->wlan_dev.dnld_sent || priv->adapter->TxLockFlag) {
375 priv->stats.tx_dropped++;
379 netif_stop_queue(priv->wlan_dev.netdev);
381 if (libertas_process_tx(priv, skb) == 0)
382 dev->trans_start = jiffies;
384 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
389 * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
392 static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
394 wlan_private *priv = dev->priv;
397 lbs_deb_enter(LBS_DEB_MESH);
401 ret = wlan_hard_start_xmit(skb, priv->wlan_dev.netdev);
402 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
407 * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
410 static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
414 lbs_deb_enter(LBS_DEB_NET);
416 UNSET_MESH_FRAME(skb);
418 ret = wlan_hard_start_xmit(skb, dev);
419 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
423 static void wlan_tx_timeout(struct net_device *dev)
425 wlan_private *priv = (wlan_private *) dev->priv;
427 lbs_deb_enter(LBS_DEB_TX);
429 lbs_pr_err("tx watch dog timeout\n");
431 priv->wlan_dev.dnld_sent = DNLD_RES_RECEIVED;
432 dev->trans_start = jiffies;
434 if (priv->adapter->currenttxskb) {
435 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
436 /* If we are here, we have not received feedback from
437 the previous packet. Assume TX_FAIL and move on. */
438 priv->adapter->eventcause = 0x01000000;
439 libertas_send_tx_feedback(priv);
441 wake_up_interruptible(&priv->mainthread.waitq);
442 } else if (priv->adapter->connect_status == libertas_connected)
443 netif_wake_queue(priv->wlan_dev.netdev);
445 lbs_deb_leave(LBS_DEB_TX);
449 * @brief This function returns the network statistics
451 * @param dev A pointer to wlan_private structure
452 * @return A pointer to net_device_stats structure
454 static struct net_device_stats *wlan_get_stats(struct net_device *dev)
456 wlan_private *priv = (wlan_private *) dev->priv;
461 static int wlan_set_mac_address(struct net_device *dev, void *addr)
464 wlan_private *priv = (wlan_private *) dev->priv;
465 wlan_adapter *adapter = priv->adapter;
466 struct sockaddr *phwaddr = addr;
468 lbs_deb_enter(LBS_DEB_NET);
470 memset(adapter->current_addr, 0, ETH_ALEN);
472 /* dev->dev_addr is 8 bytes */
473 lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
475 lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
476 memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
478 ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
480 cmd_option_waitforrsp, 0, NULL);
483 lbs_deb_net("set MAC address failed\n");
488 lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
489 memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
491 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
494 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
498 static int wlan_copy_multicast_address(wlan_adapter * adapter,
499 struct net_device *dev)
502 struct dev_mc_list *mcptr = dev->mc_list;
504 for (i = 0; i < dev->mc_count; i++) {
505 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
513 static void wlan_set_multicast_list(struct net_device *dev)
515 wlan_private *priv = dev->priv;
516 wlan_adapter *adapter = priv->adapter;
519 lbs_deb_enter(LBS_DEB_NET);
521 oldpacketfilter = adapter->currentpacketfilter;
523 if (dev->flags & IFF_PROMISC) {
524 lbs_deb_net("enable promiscuous mode\n");
525 adapter->currentpacketfilter |=
526 cmd_act_mac_promiscuous_enable;
527 adapter->currentpacketfilter &=
528 ~(cmd_act_mac_all_multicast_enable |
529 cmd_act_mac_multicast_enable);
532 adapter->currentpacketfilter &=
533 ~cmd_act_mac_promiscuous_enable;
535 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
536 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
537 lbs_deb_net( "enabling all multicast\n");
538 adapter->currentpacketfilter |=
539 cmd_act_mac_all_multicast_enable;
540 adapter->currentpacketfilter &=
541 ~cmd_act_mac_multicast_enable;
543 adapter->currentpacketfilter &=
544 ~cmd_act_mac_all_multicast_enable;
546 if (!dev->mc_count) {
547 lbs_deb_net("no multicast addresses, "
548 "disabling multicast\n");
549 adapter->currentpacketfilter &=
550 ~cmd_act_mac_multicast_enable;
554 adapter->currentpacketfilter |=
555 cmd_act_mac_multicast_enable;
557 adapter->nr_of_multicastmacaddr =
558 wlan_copy_multicast_address(adapter, dev);
560 lbs_deb_net("multicast addresses: %d\n",
563 for (i = 0; i < dev->mc_count; i++) {
564 lbs_deb_net("Multicast address %d:"
566 adapter->multicastlist[i][0],
567 adapter->multicastlist[i][1],
568 adapter->multicastlist[i][2],
569 adapter->multicastlist[i][3],
570 adapter->multicastlist[i][4],
571 adapter->multicastlist[i][5]);
573 /* send multicast addresses to firmware */
574 libertas_prepare_and_send_command(priv,
575 cmd_mac_multicast_adr,
582 if (adapter->currentpacketfilter != oldpacketfilter) {
583 libertas_set_mac_packet_filter(priv);
586 lbs_deb_leave(LBS_DEB_NET);
590 * @brief This function handles the major jobs in the WLAN driver.
591 * It handles all events generated by firmware, RX data received
592 * from firmware and TX data sent from kernel.
594 * @param data A pointer to wlan_thread structure
597 static int wlan_service_main_thread(void *data)
599 struct wlan_thread *thread = data;
600 wlan_private *priv = thread->priv;
601 wlan_adapter *adapter = priv->adapter;
605 lbs_deb_enter(LBS_DEB_THREAD);
607 wlan_activate_thread(thread);
609 init_waitqueue_entry(&wait, current);
612 lbs_deb_thread( "main-thread 111: intcounter=%d "
613 "currenttxskb=%p dnld_sent=%d\n",
615 adapter->currenttxskb, priv->wlan_dev.dnld_sent);
617 add_wait_queue(&thread->waitq, &wait);
618 set_current_state(TASK_INTERRUPTIBLE);
619 spin_lock_irq(&adapter->driver_lock);
620 if ((adapter->psstate == PS_STATE_SLEEP) ||
621 (!adapter->intcounter
622 && (priv->wlan_dev.dnld_sent || adapter->cur_cmd ||
623 list_empty(&adapter->cmdpendingq)))) {
625 "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
626 adapter->connect_status, adapter->intcounter,
627 adapter->psmode, adapter->psstate);
628 spin_unlock_irq(&adapter->driver_lock);
631 spin_unlock_irq(&adapter->driver_lock);
635 "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
636 "dnld_sent=%d\n", adapter->intcounter,
637 adapter->currenttxskb, priv->wlan_dev.dnld_sent);
639 set_current_state(TASK_RUNNING);
640 remove_wait_queue(&thread->waitq, &wait);
643 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
646 adapter->currenttxskb, priv->wlan_dev.dnld_sent);
648 if (kthread_should_stop()
649 || adapter->surpriseremoved) {
651 "main-thread: break from main thread: surpriseremoved=0x%x\n",
652 adapter->surpriseremoved);
657 spin_lock_irq(&adapter->driver_lock);
658 if (adapter->intcounter) {
660 adapter->intcounter = 0;
661 int_status = libertas_sbi_get_int_status(priv, &ireg);
665 "main-thread: reading HOST_INT_STATUS_REG failed\n");
666 spin_unlock_irq(&adapter->driver_lock);
669 adapter->hisregcpy |= ireg;
672 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
675 adapter->currenttxskb, priv->wlan_dev.dnld_sent);
677 /* command response? */
678 if (adapter->hisregcpy & his_cmdupldrdy) {
679 lbs_deb_thread("main-thread: cmd response ready\n");
681 adapter->hisregcpy &= ~his_cmdupldrdy;
682 spin_unlock_irq(&adapter->driver_lock);
683 libertas_process_rx_command(priv);
684 spin_lock_irq(&adapter->driver_lock);
688 if (adapter->hisregcpy & his_cardevent) {
689 lbs_deb_thread("main-thread: Card Event Activity\n");
691 adapter->hisregcpy &= ~his_cardevent;
693 if (libertas_sbi_read_event_cause(priv)) {
695 "main-thread: libertas_sbi_read_event_cause failed\n");
696 spin_unlock_irq(&adapter->driver_lock);
699 spin_unlock_irq(&adapter->driver_lock);
700 libertas_process_event(priv);
702 spin_unlock_irq(&adapter->driver_lock);
704 /* Check if we need to confirm Sleep Request received previously */
705 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
706 if (!priv->wlan_dev.dnld_sent && !adapter->cur_cmd) {
707 if (adapter->connect_status ==
708 libertas_connected) {
710 "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
711 "dnld_sent=%d cur_cmd=%p, confirm now\n",
713 adapter->currenttxskb,
714 priv->wlan_dev.dnld_sent,
717 libertas_ps_confirm_sleep(priv,
718 (u16) adapter->psmode);
720 /* workaround for firmware sending
721 * deauth/linkloss event immediately
722 * after sleep request, remove this
723 * after firmware fixes it
725 adapter->psstate = PS_STATE_AWAKE;
727 "main-thread: ignore PS_SleepConfirm in non-connected state\n");
732 /* The PS state is changed during processing of Sleep Request
735 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
736 (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
739 /* Execute the next command */
740 if (!priv->wlan_dev.dnld_sent && !priv->adapter->cur_cmd)
741 libertas_execute_next_command(priv);
743 /* Wake-up command waiters which can't sleep in
744 * libertas_prepare_and_send_command
746 if (!adapter->nr_cmd_pending)
747 wake_up_all(&adapter->cmd_pending);
749 libertas_tx_runqueue(priv);
752 del_timer(&adapter->command_timer);
753 adapter->nr_cmd_pending = 0;
754 wake_up_all(&adapter->cmd_pending);
755 wlan_deactivate_thread(thread);
757 lbs_deb_leave(LBS_DEB_THREAD);
762 * @brief This function adds the card. it will probe the
763 * card, allocate the wlan_priv and initialize the device.
765 * @param card A pointer to card
766 * @return A pointer to wlan_private structure
768 wlan_private *wlan_add_card(void *card)
770 struct net_device *dev = NULL;
771 wlan_private *priv = NULL;
773 lbs_deb_enter(LBS_DEB_NET);
775 /* Allocate an Ethernet device and register it */
776 if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
777 lbs_pr_err("init ethX device failed\n");
783 /* allocate buffer for wlan_adapter */
784 if (!(priv->adapter = kmalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
785 lbs_pr_err("allocate buffer for wlan_adapter failed\n");
789 /* init wlan_adapter */
790 memset(priv->adapter, 0, sizeof(wlan_adapter));
792 priv->wlan_dev.netdev = dev;
793 priv->wlan_dev.card = card;
795 priv->infra_open = 0;
797 SET_MODULE_OWNER(dev);
799 /* Setup the OS Interface to our functions */
800 dev->open = wlan_open;
801 dev->hard_start_xmit = wlan_pre_start_xmit;
802 dev->stop = wlan_close;
803 dev->do_ioctl = libertas_do_ioctl;
804 dev->set_mac_address = wlan_set_mac_address;
806 #define WLAN_WATCHDOG_TIMEOUT (5 * HZ)
808 dev->tx_timeout = wlan_tx_timeout;
809 dev->get_stats = wlan_get_stats;
810 dev->watchdog_timeo = WLAN_WATCHDOG_TIMEOUT;
811 dev->ethtool_ops = &libertas_ethtool_ops;
814 dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
816 #define NETIF_F_DYNALLOC 16
817 dev->features |= NETIF_F_DYNALLOC;
818 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
819 dev->set_multicast_list = wlan_set_multicast_list;
821 INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
822 INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
824 spin_lock_init(&priv->adapter->driver_lock);
825 init_waitqueue_head(&priv->adapter->cmd_pending);
826 priv->adapter->nr_cmd_pending = 0;
828 lbs_deb_thread("Starting kthread...\n");
829 priv->mainthread.priv = priv;
830 wlan_create_thread(wlan_service_main_thread,
831 &priv->mainthread, "wlan_main_service");
834 create_singlethread_workqueue("libertas_assoc");
835 INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
838 * Register the device. Fillup the private data structure with
839 * relevant information from the card and request for the required
842 if (libertas_sbi_register_dev(priv) < 0) {
843 lbs_pr_err("failed to register WLAN device\n");
844 goto err_registerdev;
848 if (libertas_init_fw(priv)) {
849 lbs_pr_err("firmware init failed\n");
850 goto err_registerdev;
853 if (register_netdev(dev)) {
854 lbs_pr_err("cannot register ethX device\n");
858 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
860 libertas_debugfs_init_one(priv, dev);
862 if (libertas_found == MAX_DEVS)
864 libertas_devs[libertas_found] = dev;
867 lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
871 libertas_sbi_unregister_dev(priv);
873 destroy_workqueue(priv->assoc_thread);
874 /* Stop the thread servicing the interrupts */
875 wake_up_interruptible(&priv->mainthread.waitq);
876 wlan_terminate_thread(&priv->mainthread);
877 kfree(priv->adapter);
881 lbs_deb_leave_args(LBS_DEB_NET, "priv NULL");
886 * @brief This function adds mshX interface
888 * @param priv A pointer to the wlan_private structure
889 * @return 0 if successful, -X otherwise
891 int wlan_add_mesh(wlan_private *priv)
893 struct net_device *mesh_dev = NULL;
896 lbs_deb_enter(LBS_DEB_MESH);
898 /* Allocate a virtual mesh device */
899 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
900 lbs_deb_mesh("init mshX device failed\n");
904 mesh_dev->priv = priv;
905 priv->mesh_dev = mesh_dev;
907 SET_MODULE_OWNER(mesh_dev);
909 mesh_dev->open = mesh_open;
910 mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
911 mesh_dev->stop = mesh_close;
912 mesh_dev->do_ioctl = libertas_do_ioctl;
913 mesh_dev->get_stats = wlan_get_stats;
914 mesh_dev->ethtool_ops = &libertas_ethtool_ops;
915 memcpy(mesh_dev->dev_addr, priv->wlan_dev.netdev->dev_addr,
916 sizeof(priv->wlan_dev.netdev->dev_addr));
919 mesh_dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
921 #define NETIF_F_DYNALLOC 16
923 /* Register virtual mesh interface */
924 ret = register_netdev(mesh_dev);
926 lbs_pr_err("cannot register mshX virtual interface\n");
930 ret = device_create_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
934 /* Everything successful */
940 unregister_netdev(mesh_dev);
943 free_netdev(mesh_dev);
946 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
950 static void wake_pending_cmdnodes(wlan_private *priv)
952 struct cmd_ctrl_node *cmdnode;
955 lbs_deb_enter(LBS_DEB_CMD);
957 spin_lock_irqsave(&priv->adapter->driver_lock, flags);
958 list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
959 cmdnode->cmdwaitqwoken = 1;
960 wake_up_interruptible(&cmdnode->cmdwait_q);
962 spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
966 int wlan_remove_card(wlan_private *priv)
968 wlan_adapter *adapter;
969 struct net_device *dev;
970 union iwreq_data wrqu;
973 lbs_deb_enter(LBS_DEB_NET);
978 adapter = priv->adapter;
983 dev = priv->wlan_dev.netdev;
985 netif_stop_queue(priv->wlan_dev.netdev);
986 netif_carrier_off(priv->wlan_dev.netdev);
988 wake_pending_cmdnodes(priv);
990 unregister_netdev(dev);
992 cancel_delayed_work(&priv->assoc_work);
993 destroy_workqueue(priv->assoc_thread);
995 if (adapter->psmode == wlan802_11powermodemax_psp) {
996 adapter->psmode = wlan802_11powermodecam;
997 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
1000 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1001 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1002 wireless_send_event(priv->wlan_dev.netdev, SIOCGIWAP, &wrqu, NULL);
1004 adapter->surpriseremoved = 1;
1006 /* Stop the thread servicing the interrupts */
1007 wlan_terminate_thread(&priv->mainthread);
1009 libertas_debugfs_remove_one(priv);
1011 lbs_deb_net("free adapter\n");
1012 libertas_free_adapter(priv);
1014 for (i = 0; i<libertas_found; i++) {
1015 if (libertas_devs[i]==priv->wlan_dev.netdev) {
1016 libertas_devs[i] = libertas_devs[--libertas_found];
1017 libertas_devs[libertas_found] = NULL ;
1022 lbs_deb_net("unregister finish\n");
1024 priv->wlan_dev.netdev = NULL;
1028 lbs_deb_leave(LBS_DEB_NET);
1032 void wlan_remove_mesh(wlan_private *priv)
1034 struct net_device *mesh_dev;
1036 lbs_deb_enter(LBS_DEB_NET);
1041 mesh_dev = priv->mesh_dev;
1043 netif_stop_queue(mesh_dev);
1045 device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
1046 unregister_netdev(mesh_dev);
1048 priv->mesh_dev = NULL ;
1049 free_netdev(mesh_dev);
1052 lbs_deb_leave(LBS_DEB_NET);
1056 * @brief This function finds the CFP in
1057 * region_cfp_table based on region and band parameter.
1059 * @param region The region code
1060 * @param band The band
1061 * @param cfp_no A pointer to CFP number
1062 * @return A pointer to CFP
1064 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1068 lbs_deb_enter(LBS_DEB_MAIN);
1070 end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1072 for (i = 0; i < end ; i++) {
1073 lbs_deb_main("region_cfp_table[i].region=%d\n",
1074 region_cfp_table[i].region);
1075 if (region_cfp_table[i].region == region) {
1076 *cfp_no = region_cfp_table[i].cfp_no_BG;
1077 lbs_deb_leave(LBS_DEB_MAIN);
1078 return region_cfp_table[i].cfp_BG;
1082 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1086 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1088 wlan_adapter *adapter = priv->adapter;
1092 struct chan_freq_power *cfp;
1095 lbs_deb_enter(LBS_DEB_MAIN);
1097 memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1100 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1102 adapter->region_channel[i].nrcfp = cfp_no;
1103 adapter->region_channel[i].CFP = cfp;
1105 lbs_deb_main("wrong region code %#x in band B/G\n",
1110 adapter->region_channel[i].valid = 1;
1111 adapter->region_channel[i].region = region;
1112 adapter->region_channel[i].band = band;
1116 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1121 * @brief This function handles the interrupt. it will change PS
1122 * state if applicable. it will wake up main_thread to handle
1123 * the interrupt event as well.
1125 * @param dev A pointer to net_device structure
1128 void libertas_interrupt(struct net_device *dev)
1130 wlan_private *priv = dev->priv;
1132 lbs_deb_enter(LBS_DEB_THREAD);
1134 lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1135 priv->adapter->intcounter);
1137 priv->adapter->intcounter++;
1139 if (priv->adapter->psstate == PS_STATE_SLEEP) {
1140 priv->adapter->psstate = PS_STATE_AWAKE;
1141 netif_wake_queue(dev);
1144 wake_up_interruptible(&priv->mainthread.waitq);
1146 lbs_deb_leave(LBS_DEB_THREAD);
1149 static int wlan_init_module(void)
1153 lbs_deb_enter(LBS_DEB_MAIN);
1155 if (libertas_fw_name == NULL) {
1156 libertas_fw_name = default_fw_name;
1159 libertas_debugfs_init();
1161 if (libertas_sbi_register()) {
1163 libertas_debugfs_remove();
1166 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1170 static void wlan_cleanup_module(void)
1174 lbs_deb_enter(LBS_DEB_MAIN);
1176 for (i = 0; i<libertas_found; i++) {
1177 wlan_private *priv = libertas_devs[i]->priv;
1181 libertas_sbi_unregister();
1182 libertas_debugfs_remove();
1184 lbs_deb_leave(LBS_DEB_MAIN);
1187 module_init(wlan_init_module);
1188 module_exit(wlan_cleanup_module);
1190 MODULE_DESCRIPTION("M-WLAN Driver");
1191 MODULE_AUTHOR("Marvell International Ltd.");
1192 MODULE_LICENSE("GPL");