[PATCH] libertas: first pass at fixing up endianness issues
[linux-2.6/x86.git] / drivers / net / wireless / libertas / main.c
blobec9be0c51c698dbb13688fc127ea6cbbd03dba08
1 /**
2 * This file contains the major functions in WLAN
3 * driver. It includes init, exit, open, close and main
4 * thread etc..
5 */
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>
17 #include "host.h"
18 #include "decl.h"
19 #include "dev.h"
20 #include "wext.h"
21 #include "debugfs.h"
22 #include "assoc.h"
24 #define DRIVER_RELEASE_VERSION "322.p0"
25 const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
26 #ifdef DEBUG
27 "-dbg"
28 #endif
29 "";
32 /* Module parameters */
33 unsigned int libertas_debug = 0;
34 module_param(libertas_debug, int, 0644);
35 EXPORT_SYMBOL_GPL(libertas_debug);
38 #define WLAN_TX_PWR_DEFAULT 20 /*100mW */
39 #define WLAN_TX_PWR_US_DEFAULT 20 /*100mW */
40 #define WLAN_TX_PWR_JP_DEFAULT 16 /*50mW */
41 #define WLAN_TX_PWR_FR_DEFAULT 20 /*100mW */
42 #define WLAN_TX_PWR_EMEA_DEFAULT 20 /*100mW */
44 /* Format { channel, frequency (MHz), maxtxpower } */
45 /* band: 'B/G', region: USA FCC/Canada IC */
46 static struct chan_freq_power channel_freq_power_US_BG[] = {
47 {1, 2412, WLAN_TX_PWR_US_DEFAULT},
48 {2, 2417, WLAN_TX_PWR_US_DEFAULT},
49 {3, 2422, WLAN_TX_PWR_US_DEFAULT},
50 {4, 2427, WLAN_TX_PWR_US_DEFAULT},
51 {5, 2432, WLAN_TX_PWR_US_DEFAULT},
52 {6, 2437, WLAN_TX_PWR_US_DEFAULT},
53 {7, 2442, WLAN_TX_PWR_US_DEFAULT},
54 {8, 2447, WLAN_TX_PWR_US_DEFAULT},
55 {9, 2452, WLAN_TX_PWR_US_DEFAULT},
56 {10, 2457, WLAN_TX_PWR_US_DEFAULT},
57 {11, 2462, WLAN_TX_PWR_US_DEFAULT}
60 /* band: 'B/G', region: Europe ETSI */
61 static struct chan_freq_power channel_freq_power_EU_BG[] = {
62 {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
63 {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
64 {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
65 {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
66 {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
67 {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
68 {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
69 {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
70 {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
71 {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
72 {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
73 {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
74 {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
77 /* band: 'B/G', region: Spain */
78 static struct chan_freq_power channel_freq_power_SPN_BG[] = {
79 {10, 2457, WLAN_TX_PWR_DEFAULT},
80 {11, 2462, WLAN_TX_PWR_DEFAULT}
83 /* band: 'B/G', region: France */
84 static struct chan_freq_power channel_freq_power_FR_BG[] = {
85 {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
86 {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
87 {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
88 {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
91 /* band: 'B/G', region: Japan */
92 static struct chan_freq_power channel_freq_power_JPN_BG[] = {
93 {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
94 {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
95 {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
96 {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
97 {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
98 {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
99 {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
100 {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
101 {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
102 {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
103 {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
104 {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
105 {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
106 {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
110 * the structure for channel, frequency and power
112 struct region_cfp_table {
113 u8 region;
114 struct chan_freq_power *cfp_BG;
115 int cfp_no_BG;
119 * the structure for the mapping between region and CFP
121 static struct region_cfp_table region_cfp_table[] = {
122 {0x10, /*US FCC */
123 channel_freq_power_US_BG,
124 sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
127 {0x20, /*CANADA IC */
128 channel_freq_power_US_BG,
129 sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
132 {0x30, /*EU*/ channel_freq_power_EU_BG,
133 sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
136 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
137 sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
140 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
141 sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
144 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
145 sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
148 /*Add new region here */
152 * the rates supported
154 u8 libertas_supported_rates[G_SUPPORTED_RATES] =
155 { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
156 0 };
159 * the rates supported for ad-hoc G mode
161 u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
162 { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
163 0 };
166 * the rates supported for ad-hoc B mode
168 u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
171 * the table to keep region code
173 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
174 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
177 * Attributes exported through sysfs
181 * @brief Get function for sysfs attribute libertas_mpp
183 static ssize_t libertas_mpp_get(struct device * dev,
184 struct device_attribute *attr, char * buf) {
185 struct cmd_ds_mesh_access mesh_access;
187 memset(&mesh_access, 0, sizeof(mesh_access));
188 libertas_prepare_and_send_command(to_net_dev(dev)->priv,
189 cmd_mesh_access,
190 cmd_act_mesh_get_mpp,
191 cmd_option_waitforrsp, 0, (void *)&mesh_access);
193 return snprintf(buf, 3, "%d\n", le32_to_cpu(mesh_access.data[0]));
197 * @brief Set function for sysfs attribute libertas_mpp
199 static ssize_t libertas_mpp_set(struct device * dev,
200 struct device_attribute *attr, const char * buf, size_t count) {
201 struct cmd_ds_mesh_access mesh_access;
202 uint32_t datum;
204 memset(&mesh_access, 0, sizeof(mesh_access));
205 sscanf(buf, "%d", &datum);
206 mesh_access.data[0] = cpu_to_le32(datum);
208 libertas_prepare_and_send_command((to_net_dev(dev))->priv,
209 cmd_mesh_access,
210 cmd_act_mesh_set_mpp,
211 cmd_option_waitforrsp, 0, (void *)&mesh_access);
212 return strlen(buf);
216 * libertas_mpp attribute to be exported per mshX interface
217 * through sysfs (/sys/class/net/mshX/libertas-mpp)
219 static DEVICE_ATTR(libertas_mpp, 0644, libertas_mpp_get, libertas_mpp_set );
222 * @brief Check if the device can be open and wait if necessary.
224 * @param dev A pointer to net_device structure
225 * @return 0
227 * For USB adapter, on some systems the device open handler will be
228 * called before FW ready. Use the following flag check and wait
229 * function to work around the issue.
232 static int pre_open_check(struct net_device *dev)
234 wlan_private *priv = (wlan_private *) dev->priv;
235 wlan_adapter *adapter = priv->adapter;
236 int i = 0;
238 while (!adapter->fw_ready && i < 20) {
239 i++;
240 msleep_interruptible(100);
242 if (!adapter->fw_ready) {
243 lbs_pr_err("firmware not ready\n");
244 return -1;
247 return 0;
251 * @brief This function opens the device
253 * @param dev A pointer to net_device structure
254 * @return 0
256 static int wlan_dev_open(struct net_device *dev)
258 wlan_private *priv = (wlan_private *) dev->priv;
259 wlan_adapter *adapter = priv->adapter;
261 lbs_deb_enter(LBS_DEB_NET);
263 priv->open = 1;
265 if (adapter->connect_status == libertas_connected) {
266 netif_carrier_on(priv->dev);
267 netif_carrier_on(priv->mesh_dev);
268 } else {
269 netif_carrier_off(priv->dev);
270 netif_carrier_off(priv->mesh_dev);
273 lbs_deb_leave(LBS_DEB_NET);
274 return 0;
277 * @brief This function opens the mshX interface
279 * @param dev A pointer to net_device structure
280 * @return 0
282 static int mesh_open(struct net_device *dev)
284 wlan_private *priv = (wlan_private *) dev->priv ;
286 if (pre_open_check(dev) == -1)
287 return -1;
288 priv->mesh_open = 1 ;
289 netif_wake_queue(priv->mesh_dev);
290 if (priv->infra_open == 0)
291 return wlan_dev_open(priv->dev) ;
292 return 0;
296 * @brief This function opens the ethX interface
298 * @param dev A pointer to net_device structure
299 * @return 0
301 static int wlan_open(struct net_device *dev)
303 wlan_private *priv = (wlan_private *) dev->priv ;
305 if(pre_open_check(dev) == -1)
306 return -1;
307 priv->infra_open = 1 ;
308 netif_wake_queue(priv->dev);
309 if (priv->open == 0)
310 return wlan_dev_open(priv->dev) ;
311 return 0;
314 static int wlan_dev_close(struct net_device *dev)
316 wlan_private *priv = dev->priv;
318 lbs_deb_enter(LBS_DEB_NET);
320 netif_carrier_off(priv->dev);
321 priv->open = 0;
323 lbs_deb_leave(LBS_DEB_NET);
324 return 0;
328 * @brief This function closes the mshX interface
330 * @param dev A pointer to net_device structure
331 * @return 0
333 static int mesh_close(struct net_device *dev)
335 wlan_private *priv = (wlan_private *) (dev->priv);
337 priv->mesh_open = 0;
338 netif_stop_queue(priv->mesh_dev);
339 if (priv->infra_open == 0)
340 return wlan_dev_close(dev);
341 else
342 return 0;
346 * @brief This function closes the ethX interface
348 * @param dev A pointer to net_device structure
349 * @return 0
351 static int wlan_close(struct net_device *dev)
353 wlan_private *priv = (wlan_private *) dev->priv;
355 netif_stop_queue(dev);
356 priv->infra_open = 0;
357 if (priv->mesh_open == 0)
358 return wlan_dev_close(dev);
359 else
360 return 0;
364 static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
366 int ret = 0;
367 wlan_private *priv = dev->priv;
369 lbs_deb_enter(LBS_DEB_NET);
371 if (priv->dnld_sent || priv->adapter->TxLockFlag) {
372 priv->stats.tx_dropped++;
373 goto done;
376 netif_stop_queue(priv->dev);
377 netif_stop_queue(priv->mesh_dev);
379 if (libertas_process_tx(priv, skb) == 0)
380 dev->trans_start = jiffies;
381 done:
382 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
383 return ret;
387 * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
390 static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
392 wlan_private *priv = dev->priv;
393 int ret;
395 lbs_deb_enter(LBS_DEB_MESH);
397 SET_MESH_FRAME(skb);
399 ret = wlan_hard_start_xmit(skb, priv->dev);
400 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
401 return ret;
405 * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
408 static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
410 int ret;
412 lbs_deb_enter(LBS_DEB_NET);
414 UNSET_MESH_FRAME(skb);
416 ret = wlan_hard_start_xmit(skb, dev);
417 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
418 return ret;
421 static void wlan_tx_timeout(struct net_device *dev)
423 wlan_private *priv = (wlan_private *) dev->priv;
425 lbs_deb_enter(LBS_DEB_TX);
427 lbs_pr_err("tx watch dog timeout\n");
429 priv->dnld_sent = DNLD_RES_RECEIVED;
430 dev->trans_start = jiffies;
432 if (priv->adapter->currenttxskb) {
433 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
434 /* If we are here, we have not received feedback from
435 the previous packet. Assume TX_FAIL and move on. */
436 priv->adapter->eventcause = 0x01000000;
437 libertas_send_tx_feedback(priv);
438 } else
439 wake_up_interruptible(&priv->mainthread.waitq);
440 } else if (priv->adapter->connect_status == libertas_connected) {
441 netif_wake_queue(priv->dev);
442 netif_wake_queue(priv->mesh_dev);
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;
458 return &priv->stats;
461 static int wlan_set_mac_address(struct net_device *dev, void *addr)
463 int ret = 0;
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 /* In case it was called from the mesh device */
471 dev = priv->dev ;
473 memset(adapter->current_addr, 0, ETH_ALEN);
475 /* dev->dev_addr is 8 bytes */
476 lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
478 lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
479 memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
481 ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
482 cmd_act_set,
483 cmd_option_waitforrsp, 0, NULL);
485 if (ret) {
486 lbs_deb_net("set MAC address failed\n");
487 ret = -1;
488 goto done;
491 lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
492 memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
493 if (priv->mesh_dev)
494 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
496 done:
497 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
498 return ret;
501 static int wlan_copy_multicast_address(wlan_adapter * adapter,
502 struct net_device *dev)
504 int i = 0;
505 struct dev_mc_list *mcptr = dev->mc_list;
507 for (i = 0; i < dev->mc_count; i++) {
508 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
509 mcptr = mcptr->next;
512 return i;
516 static void wlan_set_multicast_list(struct net_device *dev)
518 wlan_private *priv = dev->priv;
519 wlan_adapter *adapter = priv->adapter;
520 int oldpacketfilter;
522 lbs_deb_enter(LBS_DEB_NET);
524 oldpacketfilter = adapter->currentpacketfilter;
526 if (dev->flags & IFF_PROMISC) {
527 lbs_deb_net("enable promiscuous mode\n");
528 adapter->currentpacketfilter |=
529 cmd_act_mac_promiscuous_enable;
530 adapter->currentpacketfilter &=
531 ~(cmd_act_mac_all_multicast_enable |
532 cmd_act_mac_multicast_enable);
533 } else {
534 /* Multicast */
535 adapter->currentpacketfilter &=
536 ~cmd_act_mac_promiscuous_enable;
538 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
539 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
540 lbs_deb_net( "enabling all multicast\n");
541 adapter->currentpacketfilter |=
542 cmd_act_mac_all_multicast_enable;
543 adapter->currentpacketfilter &=
544 ~cmd_act_mac_multicast_enable;
545 } else {
546 adapter->currentpacketfilter &=
547 ~cmd_act_mac_all_multicast_enable;
549 if (!dev->mc_count) {
550 lbs_deb_net("no multicast addresses, "
551 "disabling multicast\n");
552 adapter->currentpacketfilter &=
553 ~cmd_act_mac_multicast_enable;
554 } else {
555 int i;
557 adapter->currentpacketfilter |=
558 cmd_act_mac_multicast_enable;
560 adapter->nr_of_multicastmacaddr =
561 wlan_copy_multicast_address(adapter, dev);
563 lbs_deb_net("multicast addresses: %d\n",
564 dev->mc_count);
566 for (i = 0; i < dev->mc_count; i++) {
567 lbs_deb_net("Multicast address %d:"
568 MAC_FMT "\n", i,
569 adapter->multicastlist[i][0],
570 adapter->multicastlist[i][1],
571 adapter->multicastlist[i][2],
572 adapter->multicastlist[i][3],
573 adapter->multicastlist[i][4],
574 adapter->multicastlist[i][5]);
576 /* send multicast addresses to firmware */
577 libertas_prepare_and_send_command(priv,
578 cmd_mac_multicast_adr,
579 cmd_act_set, 0, 0,
580 NULL);
585 if (adapter->currentpacketfilter != oldpacketfilter) {
586 libertas_set_mac_packet_filter(priv);
589 lbs_deb_leave(LBS_DEB_NET);
593 * @brief This function handles the major jobs in the WLAN driver.
594 * It handles all events generated by firmware, RX data received
595 * from firmware and TX data sent from kernel.
597 * @param data A pointer to wlan_thread structure
598 * @return 0
600 static int wlan_service_main_thread(void *data)
602 struct wlan_thread *thread = data;
603 wlan_private *priv = thread->priv;
604 wlan_adapter *adapter = priv->adapter;
605 wait_queue_t wait;
606 u8 ireg = 0;
608 lbs_deb_enter(LBS_DEB_THREAD);
610 wlan_activate_thread(thread);
612 init_waitqueue_entry(&wait, current);
614 for (;;) {
615 lbs_deb_thread( "main-thread 111: intcounter=%d "
616 "currenttxskb=%p dnld_sent=%d\n",
617 adapter->intcounter,
618 adapter->currenttxskb, priv->dnld_sent);
620 add_wait_queue(&thread->waitq, &wait);
621 set_current_state(TASK_INTERRUPTIBLE);
622 spin_lock_irq(&adapter->driver_lock);
623 if ((adapter->psstate == PS_STATE_SLEEP) ||
624 (!adapter->intcounter
625 && (priv->dnld_sent || adapter->cur_cmd ||
626 list_empty(&adapter->cmdpendingq)))) {
627 lbs_deb_thread(
628 "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
629 adapter->connect_status, adapter->intcounter,
630 adapter->psmode, adapter->psstate);
631 spin_unlock_irq(&adapter->driver_lock);
632 schedule();
633 } else
634 spin_unlock_irq(&adapter->driver_lock);
637 lbs_deb_thread(
638 "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
639 "dnld_sent=%d\n", adapter->intcounter,
640 adapter->currenttxskb, priv->dnld_sent);
642 set_current_state(TASK_RUNNING);
643 remove_wait_queue(&thread->waitq, &wait);
644 try_to_freeze();
646 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
647 "dnld_sent=%d\n",
648 adapter->intcounter,
649 adapter->currenttxskb, priv->dnld_sent);
651 if (kthread_should_stop()
652 || adapter->surpriseremoved) {
653 lbs_deb_thread(
654 "main-thread: break from main thread: surpriseremoved=0x%x\n",
655 adapter->surpriseremoved);
656 break;
660 spin_lock_irq(&adapter->driver_lock);
661 if (adapter->intcounter) {
662 u8 int_status;
663 adapter->intcounter = 0;
664 int_status = priv->hw_get_int_status(priv, &ireg);
666 if (int_status) {
667 lbs_deb_thread(
668 "main-thread: reading HOST_INT_STATUS_REG failed\n");
669 spin_unlock_irq(&adapter->driver_lock);
670 continue;
672 adapter->hisregcpy |= ireg;
675 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
676 "dnld_sent=%d\n",
677 adapter->intcounter,
678 adapter->currenttxskb, priv->dnld_sent);
680 /* command response? */
681 if (adapter->hisregcpy & his_cmdupldrdy) {
682 lbs_deb_thread("main-thread: cmd response ready\n");
684 adapter->hisregcpy &= ~his_cmdupldrdy;
685 spin_unlock_irq(&adapter->driver_lock);
686 libertas_process_rx_command(priv);
687 spin_lock_irq(&adapter->driver_lock);
690 /* Any Card Event */
691 if (adapter->hisregcpy & his_cardevent) {
692 lbs_deb_thread("main-thread: Card Event Activity\n");
694 adapter->hisregcpy &= ~his_cardevent;
696 if (priv->hw_read_event_cause(priv)) {
697 lbs_pr_alert(
698 "main-thread: hw_read_event_cause failed\n");
699 spin_unlock_irq(&adapter->driver_lock);
700 continue;
702 spin_unlock_irq(&adapter->driver_lock);
703 libertas_process_event(priv);
704 } else
705 spin_unlock_irq(&adapter->driver_lock);
707 /* Check if we need to confirm Sleep Request received previously */
708 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
709 if (!priv->dnld_sent && !adapter->cur_cmd) {
710 if (adapter->connect_status ==
711 libertas_connected) {
712 lbs_deb_thread(
713 "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
714 "dnld_sent=%d cur_cmd=%p, confirm now\n",
715 adapter->intcounter,
716 adapter->currenttxskb,
717 priv->dnld_sent,
718 adapter->cur_cmd);
720 libertas_ps_confirm_sleep(priv,
721 (u16) adapter->psmode);
722 } else {
723 /* workaround for firmware sending
724 * deauth/linkloss event immediately
725 * after sleep request, remove this
726 * after firmware fixes it
728 adapter->psstate = PS_STATE_AWAKE;
729 lbs_pr_alert(
730 "main-thread: ignore PS_SleepConfirm in non-connected state\n");
735 /* The PS state is changed during processing of Sleep Request
736 * event above
738 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
739 (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
740 continue;
742 /* Execute the next command */
743 if (!priv->dnld_sent && !priv->adapter->cur_cmd)
744 libertas_execute_next_command(priv);
746 /* Wake-up command waiters which can't sleep in
747 * libertas_prepare_and_send_command
749 if (!adapter->nr_cmd_pending)
750 wake_up_all(&adapter->cmd_pending);
752 libertas_tx_runqueue(priv);
755 del_timer(&adapter->command_timer);
756 adapter->nr_cmd_pending = 0;
757 wake_up_all(&adapter->cmd_pending);
758 wlan_deactivate_thread(thread);
760 lbs_deb_leave(LBS_DEB_THREAD);
761 return 0;
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");
781 return NULL;
783 priv = dev->priv;
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");
788 goto err_kzalloc;
791 priv->dev = dev;
792 priv->card = card;
793 priv->mesh_open = 0;
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->do_ioctl = libertas_do_ioctl;
803 dev->set_mac_address = wlan_set_mac_address;
804 dev->tx_timeout = wlan_tx_timeout;
805 dev->get_stats = wlan_get_stats;
806 dev->watchdog_timeo = 5 * HZ;
807 dev->ethtool_ops = &libertas_ethtool_ops;
808 #ifdef WIRELESS_EXT
809 dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
810 #endif
811 #define NETIF_F_DYNALLOC 16
812 dev->features |= NETIF_F_DYNALLOC;
813 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
814 dev->set_multicast_list = wlan_set_multicast_list;
816 SET_NETDEV_DEV(dev, dmdev);
818 INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
819 INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
821 spin_lock_init(&priv->adapter->driver_lock);
822 init_waitqueue_head(&priv->adapter->cmd_pending);
823 priv->adapter->nr_cmd_pending = 0;
824 goto done;
826 err_kzalloc:
827 free_netdev(dev);
828 priv = NULL;
829 done:
830 lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
831 return priv;
833 EXPORT_SYMBOL_GPL(libertas_add_card);
835 int libertas_activate_card(wlan_private *priv, char *fw_name)
837 struct net_device *dev = priv->dev;
838 int ret = -1;
840 lbs_deb_enter(LBS_DEB_MAIN);
842 lbs_deb_thread("Starting kthread...\n");
843 priv->mainthread.priv = priv;
844 wlan_create_thread(wlan_service_main_thread,
845 &priv->mainthread, "wlan_main_service");
847 priv->assoc_thread =
848 create_singlethread_workqueue("libertas_assoc");
849 INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
852 * Register the device. Fillup the private data structure with
853 * relevant information from the card and request for the required
854 * IRQ.
856 if (priv->hw_register_dev(priv) < 0) {
857 lbs_pr_err("failed to register WLAN device\n");
858 goto err_registerdev;
861 /* init FW and HW */
862 if (fw_name && libertas_init_fw(priv, fw_name)) {
863 lbs_pr_err("firmware init failed\n");
864 goto err_registerdev;
867 if (register_netdev(dev)) {
868 lbs_pr_err("cannot register ethX device\n");
869 goto err_init_fw;
872 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
874 libertas_debugfs_init_one(priv, dev);
876 ret = 0;
877 goto done;
879 err_init_fw:
880 priv->hw_unregister_dev(priv);
881 err_registerdev:
882 destroy_workqueue(priv->assoc_thread);
883 /* Stop the thread servicing the interrupts */
884 wake_up_interruptible(&priv->mainthread.waitq);
885 wlan_terminate_thread(&priv->mainthread);
886 done:
887 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
888 return ret;
890 EXPORT_SYMBOL_GPL(libertas_activate_card);
894 * @brief This function adds mshX interface
896 * @param priv A pointer to the wlan_private structure
897 * @return 0 if successful, -X otherwise
899 int libertas_add_mesh(wlan_private *priv, struct device *dev)
901 struct net_device *mesh_dev = NULL;
902 int ret = 0;
904 lbs_deb_enter(LBS_DEB_MESH);
906 /* Allocate a virtual mesh device */
907 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
908 lbs_deb_mesh("init mshX device failed\n");
909 ret = -ENOMEM;
910 goto done;
912 mesh_dev->priv = priv;
913 priv->mesh_dev = mesh_dev;
915 SET_MODULE_OWNER(mesh_dev);
917 mesh_dev->open = mesh_open;
918 mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
919 mesh_dev->stop = mesh_close;
920 mesh_dev->do_ioctl = libertas_do_ioctl;
921 mesh_dev->get_stats = wlan_get_stats;
922 mesh_dev->set_mac_address = wlan_set_mac_address;
923 mesh_dev->ethtool_ops = &libertas_ethtool_ops;
924 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
925 sizeof(priv->dev->dev_addr));
927 SET_NETDEV_DEV(priv->mesh_dev, dev);
929 #ifdef WIRELESS_EXT
930 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
931 #endif
932 #define NETIF_F_DYNALLOC 16
934 /* Register virtual mesh interface */
935 ret = register_netdev(mesh_dev);
936 if (ret) {
937 lbs_pr_err("cannot register mshX virtual interface\n");
938 goto err_free;
941 ret = device_create_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
942 if (ret)
943 goto err_unregister;
945 /* Everything successful */
946 ret = 0;
947 goto done;
950 err_unregister:
951 unregister_netdev(mesh_dev);
953 err_free:
954 free_netdev(mesh_dev);
956 done:
957 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
958 return ret;
960 EXPORT_SYMBOL_GPL(libertas_add_mesh);
962 static void wake_pending_cmdnodes(wlan_private *priv)
964 struct cmd_ctrl_node *cmdnode;
965 unsigned long flags;
967 lbs_deb_enter(LBS_DEB_CMD);
969 spin_lock_irqsave(&priv->adapter->driver_lock, flags);
970 list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
971 cmdnode->cmdwaitqwoken = 1;
972 wake_up_interruptible(&cmdnode->cmdwait_q);
974 spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
978 int libertas_remove_card(wlan_private *priv)
980 wlan_adapter *adapter;
981 struct net_device *dev;
982 union iwreq_data wrqu;
984 lbs_deb_enter(LBS_DEB_NET);
986 if (!priv)
987 goto out;
989 adapter = priv->adapter;
991 if (!adapter)
992 goto out;
994 dev = priv->dev;
996 netif_stop_queue(priv->dev);
997 netif_carrier_off(priv->dev);
999 wake_pending_cmdnodes(priv);
1001 unregister_netdev(dev);
1003 cancel_delayed_work(&priv->assoc_work);
1004 destroy_workqueue(priv->assoc_thread);
1006 if (adapter->psmode == wlan802_11powermodemax_psp) {
1007 adapter->psmode = wlan802_11powermodecam;
1008 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
1011 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1012 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1013 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1015 adapter->surpriseremoved = 1;
1017 /* Stop the thread servicing the interrupts */
1018 wlan_terminate_thread(&priv->mainthread);
1020 libertas_debugfs_remove_one(priv);
1022 lbs_deb_net("free adapter\n");
1023 libertas_free_adapter(priv);
1025 lbs_deb_net("unregister finish\n");
1027 priv->dev = NULL;
1028 free_netdev(dev);
1030 out:
1031 lbs_deb_leave(LBS_DEB_NET);
1032 return 0;
1034 EXPORT_SYMBOL_GPL(libertas_remove_card);
1037 void libertas_remove_mesh(wlan_private *priv)
1039 struct net_device *mesh_dev;
1041 lbs_deb_enter(LBS_DEB_NET);
1043 if (!priv)
1044 goto out;
1046 mesh_dev = priv->mesh_dev;
1048 netif_stop_queue(mesh_dev);
1049 netif_carrier_off(priv->mesh_dev);
1051 device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
1052 unregister_netdev(mesh_dev);
1054 priv->mesh_dev = NULL ;
1055 free_netdev(mesh_dev);
1057 out:
1058 lbs_deb_leave(LBS_DEB_NET);
1060 EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1063 * @brief This function finds the CFP in
1064 * region_cfp_table based on region and band parameter.
1066 * @param region The region code
1067 * @param band The band
1068 * @param cfp_no A pointer to CFP number
1069 * @return A pointer to CFP
1071 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1073 int i, end;
1075 lbs_deb_enter(LBS_DEB_MAIN);
1077 end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1079 for (i = 0; i < end ; i++) {
1080 lbs_deb_main("region_cfp_table[i].region=%d\n",
1081 region_cfp_table[i].region);
1082 if (region_cfp_table[i].region == region) {
1083 *cfp_no = region_cfp_table[i].cfp_no_BG;
1084 lbs_deb_leave(LBS_DEB_MAIN);
1085 return region_cfp_table[i].cfp_BG;
1089 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1090 return NULL;
1093 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1095 wlan_adapter *adapter = priv->adapter;
1096 int ret = 0;
1097 int i = 0;
1099 struct chan_freq_power *cfp;
1100 int cfp_no;
1102 lbs_deb_enter(LBS_DEB_MAIN);
1104 memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1107 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1108 if (cfp != NULL) {
1109 adapter->region_channel[i].nrcfp = cfp_no;
1110 adapter->region_channel[i].CFP = cfp;
1111 } else {
1112 lbs_deb_main("wrong region code %#x in band B/G\n",
1113 region);
1114 ret = -1;
1115 goto out;
1117 adapter->region_channel[i].valid = 1;
1118 adapter->region_channel[i].region = region;
1119 adapter->region_channel[i].band = band;
1120 i++;
1122 out:
1123 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1124 return ret;
1128 * @brief This function handles the interrupt. it will change PS
1129 * state if applicable. it will wake up main_thread to handle
1130 * the interrupt event as well.
1132 * @param dev A pointer to net_device structure
1133 * @return n/a
1135 void libertas_interrupt(struct net_device *dev)
1137 wlan_private *priv = dev->priv;
1139 lbs_deb_enter(LBS_DEB_THREAD);
1141 lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1142 priv->adapter->intcounter);
1144 priv->adapter->intcounter++;
1146 if (priv->adapter->psstate == PS_STATE_SLEEP) {
1147 priv->adapter->psstate = PS_STATE_AWAKE;
1148 netif_wake_queue(dev);
1149 netif_wake_queue(priv->mesh_dev);
1152 wake_up_interruptible(&priv->mainthread.waitq);
1154 lbs_deb_leave(LBS_DEB_THREAD);
1156 EXPORT_SYMBOL_GPL(libertas_interrupt);
1158 static int libertas_init_module(void)
1160 lbs_deb_enter(LBS_DEB_MAIN);
1161 libertas_debugfs_init();
1162 lbs_deb_leave(LBS_DEB_MAIN);
1163 return 0;
1166 static void libertas_exit_module(void)
1168 lbs_deb_enter(LBS_DEB_MAIN);
1170 libertas_debugfs_remove();
1172 lbs_deb_leave(LBS_DEB_MAIN);
1175 module_init(libertas_init_module);
1176 module_exit(libertas_exit_module);
1178 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1179 MODULE_AUTHOR("Marvell International Ltd.");
1180 MODULE_LICENSE("GPL");