[PATCH] libertas: fix more mixed-case abuse
[linux-2.6] / drivers / net / wireless / libertas / main.c
1 /**
2   * This file contains the major functions in WLAN
3   * driver. It includes init, exit, open, close and main
4   * thread etc..
5   */
6
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>
14
15 #include <net/iw_handler.h>
16 #include <net/ieee80211.h>
17
18 #include "host.h"
19 #include "decl.h"
20 #include "dev.h"
21 #include "wext.h"
22 #include "debugfs.h"
23 #include "assoc.h"
24
25 #define DRIVER_RELEASE_VERSION "322.p1"
26 const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
27 #ifdef  DEBUG
28     "-dbg"
29 #endif
30     "";
31
32
33 /* Module parameters */
34 unsigned int libertas_debug = 0;
35 module_param(libertas_debug, int, 0644);
36 EXPORT_SYMBOL_GPL(libertas_debug);
37
38
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 */
44
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}
59 };
60
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}
76 };
77
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}
82 };
83
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}
90 };
91
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}
108 };
109
110 /**
111  * the structure for channel, frequency and power
112  */
113 struct region_cfp_table {
114         u8 region;
115         struct chan_freq_power *cfp_BG;
116         int cfp_no_BG;
117 };
118
119 /**
120  * the structure for the mapping between region and CFP
121  */
122 static struct region_cfp_table region_cfp_table[] = {
123         {0x10,                  /*US FCC */
124          channel_freq_power_US_BG,
125          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
126          }
127         ,
128         {0x20,                  /*CANADA IC */
129          channel_freq_power_US_BG,
130          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
131          }
132         ,
133         {0x30, /*EU*/ channel_freq_power_EU_BG,
134          sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
135          }
136         ,
137         {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
138          sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
139          }
140         ,
141         {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
142          sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
143          }
144         ,
145         {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
146          sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
147          }
148         ,
149 /*Add new region here */
150 };
151
152 /**
153  * the rates supported
154  */
155 u8 libertas_supported_rates[G_SUPPORTED_RATES] =
156     { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
157 0 };
158
159 /**
160  * the rates supported for ad-hoc G mode
161  */
162 u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
163     { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
164 0 };
165
166 /**
167  * the rates supported for ad-hoc B mode
168  */
169 u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
170
171 /**
172  * the table to keep region code
173  */
174 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
175     { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
176
177 /**
178  * Attributes exported through sysfs
179  */
180
181 /**
182  * @brief Get function for sysfs attribute anycast_mask
183  */
184 static ssize_t libertas_anycast_get(struct device * dev,
185                 struct device_attribute *attr, char * buf)
186 {
187         struct cmd_ds_mesh_access mesh_access;
188
189         memset(&mesh_access, 0, sizeof(mesh_access));
190         libertas_prepare_and_send_command(to_net_dev(dev)->priv,
191                         CMD_MESH_ACCESS,
192                         CMD_ACT_MESH_GET_ANYCAST,
193                         CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
194
195         return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
196 }
197
198 /**
199  * @brief Set function for sysfs attribute anycast_mask
200  */
201 static ssize_t libertas_anycast_set(struct device * dev,
202                 struct device_attribute *attr, const char * buf, size_t count)
203 {
204         struct cmd_ds_mesh_access mesh_access;
205         uint32_t datum;
206
207         memset(&mesh_access, 0, sizeof(mesh_access));
208         sscanf(buf, "%x", &datum);
209         mesh_access.data[0] = cpu_to_le32(datum);
210
211         libertas_prepare_and_send_command((to_net_dev(dev))->priv,
212                         CMD_MESH_ACCESS,
213                         CMD_ACT_MESH_SET_ANYCAST,
214                         CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
215         return strlen(buf);
216 }
217
218 /**
219  * anycast_mask attribute to be exported per mshX interface
220  * through sysfs (/sys/class/net/mshX/anycast_mask)
221  */
222 static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
223
224 /**
225  *  @brief Check if the device can be open and wait if necessary.
226  *
227  *  @param dev     A pointer to net_device structure
228  *  @return        0
229  *
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.
233  *
234  */
235 static int pre_open_check(struct net_device *dev)
236 {
237         wlan_private *priv = (wlan_private *) dev->priv;
238         wlan_adapter *adapter = priv->adapter;
239         int i = 0;
240
241         while (!adapter->fw_ready && i < 20) {
242                 i++;
243                 msleep_interruptible(100);
244         }
245         if (!adapter->fw_ready) {
246                 lbs_pr_err("firmware not ready\n");
247                 return -1;
248         }
249
250         return 0;
251 }
252
253 /**
254  *  @brief This function opens the device
255  *
256  *  @param dev     A pointer to net_device structure
257  *  @return        0
258  */
259 static int wlan_dev_open(struct net_device *dev)
260 {
261         wlan_private *priv = (wlan_private *) dev->priv;
262         wlan_adapter *adapter = priv->adapter;
263
264         lbs_deb_enter(LBS_DEB_NET);
265
266         priv->open = 1;
267
268         if (adapter->connect_status == LIBERTAS_CONNECTED) {
269                 netif_carrier_on(priv->dev);
270                 netif_carrier_on(priv->mesh_dev);
271         } else {
272                 netif_carrier_off(priv->dev);
273                 netif_carrier_off(priv->mesh_dev);
274         }
275
276         lbs_deb_leave(LBS_DEB_NET);
277         return 0;
278 }
279 /**
280  *  @brief This function opens the mshX interface
281  *
282  *  @param dev     A pointer to net_device structure
283  *  @return        0
284  */
285 static int mesh_open(struct net_device *dev)
286 {
287         wlan_private *priv = (wlan_private *) dev->priv ;
288
289         if (pre_open_check(dev) == -1)
290                 return -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) ;
295         return 0;
296 }
297
298 /**
299  *  @brief This function opens the ethX interface
300  *
301  *  @param dev     A pointer to net_device structure
302  *  @return        0
303  */
304 static int wlan_open(struct net_device *dev)
305 {
306         wlan_private *priv = (wlan_private *) dev->priv ;
307
308         if(pre_open_check(dev) == -1)
309                 return -1;
310         priv->infra_open = 1 ;
311         netif_wake_queue(priv->dev);
312         if (priv->open == 0)
313                 return wlan_dev_open(priv->dev) ;
314         return 0;
315 }
316
317 static int wlan_dev_close(struct net_device *dev)
318 {
319         wlan_private *priv = dev->priv;
320
321         lbs_deb_enter(LBS_DEB_NET);
322
323         netif_carrier_off(priv->dev);
324         priv->open = 0;
325
326         lbs_deb_leave(LBS_DEB_NET);
327         return 0;
328 }
329
330 /**
331  *  @brief This function closes the mshX interface
332  *
333  *  @param dev     A pointer to net_device structure
334  *  @return        0
335  */
336 static int mesh_close(struct net_device *dev)
337 {
338         wlan_private *priv = (wlan_private *) (dev->priv);
339
340         priv->mesh_open = 0;
341         netif_stop_queue(priv->mesh_dev);
342         if (priv->infra_open == 0)
343                 return wlan_dev_close(dev);
344         else
345                 return 0;
346 }
347
348 /**
349  *  @brief This function closes the ethX interface
350  *
351  *  @param dev     A pointer to net_device structure
352  *  @return        0
353  */
354 static int wlan_close(struct net_device *dev)
355 {
356         wlan_private *priv = (wlan_private *) dev->priv;
357
358         netif_stop_queue(dev);
359         priv->infra_open = 0;
360         if (priv->mesh_open == 0)
361                 return wlan_dev_close(dev);
362         else
363                 return 0;
364 }
365
366
367 static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
368 {
369         int ret = 0;
370         wlan_private *priv = dev->priv;
371
372         lbs_deb_enter(LBS_DEB_NET);
373
374         if (priv->dnld_sent || priv->adapter->TxLockFlag) {
375                 priv->stats.tx_dropped++;
376                 goto done;
377         }
378
379         netif_stop_queue(priv->dev);
380         netif_stop_queue(priv->mesh_dev);
381
382         if (libertas_process_tx(priv, skb) == 0)
383                 dev->trans_start = jiffies;
384 done:
385         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
386         return ret;
387 }
388
389 /**
390  * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
391  *
392  */
393 static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
394 {
395         wlan_private *priv = dev->priv;
396         int ret;
397
398         lbs_deb_enter(LBS_DEB_MESH);
399
400         SET_MESH_FRAME(skb);
401
402         ret = wlan_hard_start_xmit(skb, priv->dev);
403         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
404         return ret;
405 }
406
407 /**
408  * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
409  *
410  */
411 static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
412 {
413         int ret;
414
415         lbs_deb_enter(LBS_DEB_NET);
416
417         UNSET_MESH_FRAME(skb);
418
419         ret = wlan_hard_start_xmit(skb, dev);
420         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
421         return ret;
422 }
423
424 static void wlan_tx_timeout(struct net_device *dev)
425 {
426         wlan_private *priv = (wlan_private *) dev->priv;
427
428         lbs_deb_enter(LBS_DEB_TX);
429
430         lbs_pr_err("tx watch dog timeout\n");
431
432         priv->dnld_sent = DNLD_RES_RECEIVED;
433         dev->trans_start = jiffies;
434
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);
441                 } else
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);
446         }
447
448         lbs_deb_leave(LBS_DEB_TX);
449 }
450
451 /**
452  *  @brief This function returns the network statistics
453  *
454  *  @param dev     A pointer to wlan_private structure
455  *  @return        A pointer to net_device_stats structure
456  */
457 static struct net_device_stats *wlan_get_stats(struct net_device *dev)
458 {
459         wlan_private *priv = (wlan_private *) dev->priv;
460
461         return &priv->stats;
462 }
463
464 static int wlan_set_mac_address(struct net_device *dev, void *addr)
465 {
466         int ret = 0;
467         wlan_private *priv = (wlan_private *) dev->priv;
468         wlan_adapter *adapter = priv->adapter;
469         struct sockaddr *phwaddr = addr;
470
471         lbs_deb_enter(LBS_DEB_NET);
472
473         /* In case it was called from the mesh device */
474         dev = priv->dev ;
475
476         memset(adapter->current_addr, 0, ETH_ALEN);
477
478         /* dev->dev_addr is 8 bytes */
479         lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
480
481         lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
482         memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
483
484         ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
485                                     CMD_ACT_SET,
486                                     CMD_OPTION_WAITFORRSP, 0, NULL);
487
488         if (ret) {
489                 lbs_deb_net("set MAC address failed\n");
490                 ret = -1;
491                 goto done;
492         }
493
494         lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
495         memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
496         if (priv->mesh_dev)
497                 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
498
499 done:
500         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
501         return ret;
502 }
503
504 static int wlan_copy_multicast_address(wlan_adapter * adapter,
505                                      struct net_device *dev)
506 {
507         int i = 0;
508         struct dev_mc_list *mcptr = dev->mc_list;
509
510         for (i = 0; i < dev->mc_count; i++) {
511                 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
512                 mcptr = mcptr->next;
513         }
514
515         return i;
516
517 }
518
519 static void wlan_set_multicast_list(struct net_device *dev)
520 {
521         wlan_private *priv = dev->priv;
522         wlan_adapter *adapter = priv->adapter;
523         int oldpacketfilter;
524
525         lbs_deb_enter(LBS_DEB_NET);
526
527         oldpacketfilter = adapter->currentpacketfilter;
528
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);
536         } else {
537                 /* Multicast */
538                 adapter->currentpacketfilter &=
539                     ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
540
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;
548                 } else {
549                         adapter->currentpacketfilter &=
550                             ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
551
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;
557                         } else {
558                                 int i;
559
560                                 adapter->currentpacketfilter |=
561                                     CMD_ACT_MAC_MULTICAST_ENABLE;
562
563                                 adapter->nr_of_multicastmacaddr =
564                                     wlan_copy_multicast_address(adapter, dev);
565
566                                 lbs_deb_net("multicast addresses: %d\n",
567                                        dev->mc_count);
568
569                                 for (i = 0; i < dev->mc_count; i++) {
570                                         lbs_deb_net("Multicast address %d:"
571                                                MAC_FMT "\n", i,
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]);
578                                 }
579                                 /* send multicast addresses to firmware */
580                                 libertas_prepare_and_send_command(priv,
581                                                       CMD_MAC_MULTICAST_ADR,
582                                                       CMD_ACT_SET, 0, 0,
583                                                       NULL);
584                         }
585                 }
586         }
587
588         if (adapter->currentpacketfilter != oldpacketfilter) {
589                 libertas_set_mac_packet_filter(priv);
590         }
591
592         lbs_deb_leave(LBS_DEB_NET);
593 }
594
595 /**
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.
599  *
600  *  @param data    A pointer to wlan_thread structure
601  *  @return        0
602  */
603 static int libertas_thread(void *data)
604 {
605         struct net_device *dev = data;
606         wlan_private *priv = dev->priv;
607         wlan_adapter *adapter = priv->adapter;
608         wait_queue_t wait;
609         u8 ireg = 0;
610
611         lbs_deb_enter(LBS_DEB_THREAD);
612
613         init_waitqueue_entry(&wait, current);
614
615         for (;;) {
616                 lbs_deb_thread( "main-thread 111: intcounter=%d "
617                        "currenttxskb=%p dnld_sent=%d\n",
618                        adapter->intcounter,
619                        adapter->currenttxskb, priv->dnld_sent);
620
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)))) {
628                         lbs_deb_thread(
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);
633                         schedule();
634                 } else
635                         spin_unlock_irq(&adapter->driver_lock);
636
637
638                 lbs_deb_thread(
639                        "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
640                        "dnld_sent=%d\n", adapter->intcounter,
641                        adapter->currenttxskb, priv->dnld_sent);
642
643                 set_current_state(TASK_RUNNING);
644                 remove_wait_queue(&priv->waitq, &wait);
645                 try_to_freeze();
646
647                 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
648                        "dnld_sent=%d\n",
649                        adapter->intcounter,
650                        adapter->currenttxskb, priv->dnld_sent);
651
652                 if (kthread_should_stop()
653                     || adapter->surpriseremoved) {
654                         lbs_deb_thread(
655                                "main-thread: break from main thread: surpriseremoved=0x%x\n",
656                                adapter->surpriseremoved);
657                         break;
658                 }
659
660
661                 spin_lock_irq(&adapter->driver_lock);
662                 if (adapter->intcounter) {
663                         u8 int_status;
664                         adapter->intcounter = 0;
665                         int_status = priv->hw_get_int_status(priv, &ireg);
666
667                         if (int_status) {
668                                 lbs_deb_thread(
669                                        "main-thread: reading HOST_INT_STATUS_REG failed\n");
670                                 spin_unlock_irq(&adapter->driver_lock);
671                                 continue;
672                         }
673                         adapter->hisregcpy |= ireg;
674                 }
675
676                 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
677                        "dnld_sent=%d\n",
678                        adapter->intcounter,
679                        adapter->currenttxskb, priv->dnld_sent);
680
681                 /* command response? */
682                 if (adapter->hisregcpy & his_cmdupldrdy) {
683                         lbs_deb_thread("main-thread: cmd response ready\n");
684
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);
689                 }
690
691                 /* Any Card Event */
692                 if (adapter->hisregcpy & his_cardevent) {
693                         lbs_deb_thread("main-thread: Card Event Activity\n");
694
695                         adapter->hisregcpy &= ~his_cardevent;
696
697                         if (priv->hw_read_event_cause(priv)) {
698                                 lbs_pr_alert(
699                                        "main-thread: hw_read_event_cause failed\n");
700                                 spin_unlock_irq(&adapter->driver_lock);
701                                 continue;
702                         }
703                         spin_unlock_irq(&adapter->driver_lock);
704                         libertas_process_event(priv);
705                 } else
706                         spin_unlock_irq(&adapter->driver_lock);
707
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) {
713                                         lbs_deb_thread(
714                                                "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
715                                                "dnld_sent=%d cur_cmd=%p, confirm now\n",
716                                                adapter->intcounter,
717                                                adapter->currenttxskb,
718                                                priv->dnld_sent,
719                                                adapter->cur_cmd);
720
721                                         libertas_ps_confirm_sleep(priv,
722                                                        (u16) adapter->psmode);
723                                 } else {
724                                         /* workaround for firmware sending
725                                          * deauth/linkloss event immediately
726                                          * after sleep request, remove this
727                                          * after firmware fixes it
728                                          */
729                                         adapter->psstate = PS_STATE_AWAKE;
730                                         lbs_pr_alert(
731                                                "main-thread: ignore PS_SleepConfirm in non-connected state\n");
732                                 }
733                         }
734                 }
735
736                 /* The PS state is changed during processing of Sleep Request
737                  * event above
738                  */
739                 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
740                     (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
741                         continue;
742
743                 /* Execute the next command */
744                 if (!priv->dnld_sent && !priv->adapter->cur_cmd)
745                         libertas_execute_next_command(priv);
746
747                 /* Wake-up command waiters which can't sleep in
748                  * libertas_prepare_and_send_command
749                  */
750                 if (!adapter->nr_cmd_pending)
751                         wake_up_all(&adapter->cmd_pending);
752
753                 libertas_tx_runqueue(priv);
754         }
755
756         del_timer(&adapter->command_timer);
757         adapter->nr_cmd_pending = 0;
758         wake_up_all(&adapter->cmd_pending);
759
760         lbs_deb_leave(LBS_DEB_THREAD);
761         return 0;
762 }
763
764 /**
765  * @brief This function adds the card. it will probe the
766  * card, allocate the wlan_priv and initialize the device.
767  *
768  *  @param card    A pointer to card
769  *  @return        A pointer to wlan_private structure
770  */
771 wlan_private *libertas_add_card(void *card, struct device *dmdev)
772 {
773         struct net_device *dev = NULL;
774         wlan_private *priv = NULL;
775
776         lbs_deb_enter(LBS_DEB_NET);
777
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;
782         }
783         priv = dev->priv;
784
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;
789         }
790
791         priv->dev = dev;
792         priv->card = card;
793         priv->mesh_open = 0;
794         priv->infra_open = 0;
795
796         SET_MODULE_OWNER(dev);
797
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;
807 #ifdef  WIRELESS_EXT
808         dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
809 #endif
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;
814
815         SET_NETDEV_DEV(dev, dmdev);
816
817         INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
818         INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
819
820         spin_lock_init(&priv->adapter->driver_lock);
821         init_waitqueue_head(&priv->adapter->cmd_pending);
822         priv->adapter->nr_cmd_pending = 0;
823         goto done;
824
825 err_kzalloc:
826         free_netdev(dev);
827         priv = NULL;
828 done:
829         lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
830         return priv;
831 }
832 EXPORT_SYMBOL_GPL(libertas_add_card);
833
834 int libertas_activate_card(wlan_private *priv, char *fw_name)
835 {
836         struct net_device *dev = priv->dev;
837         int ret = -1;
838
839         lbs_deb_enter(LBS_DEB_MAIN);
840
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");
846                 goto done;
847         }
848
849         priv->assoc_thread =
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);
853
854         /*
855          * Register the device. Fillup the private data structure with
856          * relevant information from the card and request for the required
857          * IRQ.
858          */
859         if (priv->hw_register_dev(priv) < 0) {
860                 lbs_pr_err("failed to register WLAN device\n");
861                 goto err_registerdev;
862         }
863
864         /* init FW and HW */
865         if (fw_name && libertas_init_fw(priv, fw_name)) {
866                 lbs_pr_err("firmware init failed\n");
867                 goto err_registerdev;
868         }
869
870         if (register_netdev(dev)) {
871                 lbs_pr_err("cannot register ethX device\n");
872                 goto err_init_fw;
873         }
874
875         lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
876
877         libertas_debugfs_init_one(priv, dev);
878
879         ret = 0;
880         goto done;
881
882 err_init_fw:
883         priv->hw_unregister_dev(priv);
884 err_registerdev:
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);
889 done:
890         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
891         return ret;
892 }
893 EXPORT_SYMBOL_GPL(libertas_activate_card);
894
895
896 /**
897  * @brief This function adds mshX interface
898  *
899  *  @param priv    A pointer to the wlan_private structure
900  *  @return        0 if successful, -X otherwise
901  */
902 int libertas_add_mesh(wlan_private *priv, struct device *dev)
903 {
904         struct net_device *mesh_dev = NULL;
905         int ret = 0;
906
907         lbs_deb_enter(LBS_DEB_MESH);
908
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");
912                 ret = -ENOMEM;
913                 goto done;
914         }
915         mesh_dev->priv = priv;
916         priv->mesh_dev = mesh_dev;
917
918         SET_MODULE_OWNER(mesh_dev);
919
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));
928
929         SET_NETDEV_DEV(priv->mesh_dev, dev);
930
931 #ifdef  WIRELESS_EXT
932         mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
933 #endif
934 #define NETIF_F_DYNALLOC 16
935
936         /* Register virtual mesh interface */
937         ret = register_netdev(mesh_dev);
938         if (ret) {
939                 lbs_pr_err("cannot register mshX virtual interface\n");
940                 goto err_free;
941         }
942
943         ret = device_create_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
944         if (ret)
945                 goto err_unregister;
946
947         /* Everything successful */
948         ret = 0;
949         goto done;
950
951
952 err_unregister:
953         unregister_netdev(mesh_dev);
954
955 err_free:
956         free_netdev(mesh_dev);
957
958 done:
959         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
960         return ret;
961 }
962 EXPORT_SYMBOL_GPL(libertas_add_mesh);
963
964 static void wake_pending_cmdnodes(wlan_private *priv)
965 {
966         struct cmd_ctrl_node *cmdnode;
967         unsigned long flags;
968
969         lbs_deb_enter(LBS_DEB_CMD);
970
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);
975         }
976         spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
977 }
978
979
980 int libertas_remove_card(wlan_private *priv)
981 {
982         wlan_adapter *adapter;
983         struct net_device *dev;
984         union iwreq_data wrqu;
985
986         lbs_deb_enter(LBS_DEB_NET);
987
988         if (!priv)
989                 goto out;
990
991         adapter = priv->adapter;
992
993         if (!adapter)
994                 goto out;
995
996         dev = priv->dev;
997
998         netif_stop_queue(priv->dev);
999         netif_carrier_off(priv->dev);
1000
1001         wake_pending_cmdnodes(priv);
1002
1003         unregister_netdev(dev);
1004
1005         cancel_delayed_work(&priv->assoc_work);
1006         destroy_workqueue(priv->assoc_thread);
1007
1008         if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
1009                 adapter->psmode = WLAN802_11POWERMODECAM;
1010                 libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1011         }
1012
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);
1016
1017         adapter->surpriseremoved = 1;
1018
1019         /* Stop the thread servicing the interrupts */
1020         kthread_stop(priv->main_thread);
1021
1022         libertas_debugfs_remove_one(priv);
1023
1024         lbs_deb_net("free adapter\n");
1025         libertas_free_adapter(priv);
1026
1027         lbs_deb_net("unregister finish\n");
1028
1029         priv->dev = NULL;
1030         free_netdev(dev);
1031
1032 out:
1033         lbs_deb_leave(LBS_DEB_NET);
1034         return 0;
1035 }
1036 EXPORT_SYMBOL_GPL(libertas_remove_card);
1037
1038
1039 void libertas_remove_mesh(wlan_private *priv)
1040 {
1041         struct net_device *mesh_dev;
1042
1043         lbs_deb_enter(LBS_DEB_NET);
1044
1045         if (!priv)
1046                 goto out;
1047
1048         mesh_dev = priv->mesh_dev;
1049
1050         netif_stop_queue(mesh_dev);
1051         netif_carrier_off(priv->mesh_dev);
1052
1053         device_remove_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
1054         unregister_netdev(mesh_dev);
1055
1056         priv->mesh_dev = NULL ;
1057         free_netdev(mesh_dev);
1058
1059 out:
1060         lbs_deb_leave(LBS_DEB_NET);
1061 }
1062 EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1063
1064 /**
1065  *  @brief This function finds the CFP in
1066  *  region_cfp_table based on region and band parameter.
1067  *
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
1072  */
1073 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1074 {
1075         int i, end;
1076
1077         lbs_deb_enter(LBS_DEB_MAIN);
1078
1079         end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1080
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;
1088                 }
1089         }
1090
1091         lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1092         return NULL;
1093 }
1094
1095 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1096 {
1097         wlan_adapter *adapter = priv->adapter;
1098         int ret = 0;
1099         int i = 0;
1100
1101         struct chan_freq_power *cfp;
1102         int cfp_no;
1103
1104         lbs_deb_enter(LBS_DEB_MAIN);
1105
1106         memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1107
1108         {
1109                 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1110                 if (cfp != NULL) {
1111                         adapter->region_channel[i].nrcfp = cfp_no;
1112                         adapter->region_channel[i].CFP = cfp;
1113                 } else {
1114                         lbs_deb_main("wrong region code %#x in band B/G\n",
1115                                region);
1116                         ret = -1;
1117                         goto out;
1118                 }
1119                 adapter->region_channel[i].valid = 1;
1120                 adapter->region_channel[i].region = region;
1121                 adapter->region_channel[i].band = band;
1122                 i++;
1123         }
1124 out:
1125         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1126         return ret;
1127 }
1128
1129 /**
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.
1133  *
1134  *  @param dev     A pointer to net_device structure
1135  *  @return        n/a
1136  */
1137 void libertas_interrupt(struct net_device *dev)
1138 {
1139         wlan_private *priv = dev->priv;
1140
1141         lbs_deb_enter(LBS_DEB_THREAD);
1142
1143         lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1144                priv->adapter->intcounter);
1145
1146         priv->adapter->intcounter++;
1147
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);
1152         }
1153
1154         wake_up_interruptible(&priv->waitq);
1155
1156         lbs_deb_leave(LBS_DEB_THREAD);
1157 }
1158 EXPORT_SYMBOL_GPL(libertas_interrupt);
1159
1160 static int libertas_init_module(void)
1161 {
1162         lbs_deb_enter(LBS_DEB_MAIN);
1163         libertas_debugfs_init();
1164         lbs_deb_leave(LBS_DEB_MAIN);
1165         return 0;
1166 }
1167
1168 static void libertas_exit_module(void)
1169 {
1170         lbs_deb_enter(LBS_DEB_MAIN);
1171
1172         libertas_debugfs_remove();
1173
1174         lbs_deb_leave(LBS_DEB_MAIN);
1175 }
1176
1177 module_init(libertas_init_module);
1178 module_exit(libertas_exit_module);
1179
1180 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1181 MODULE_AUTHOR("Marvell International Ltd.");
1182 MODULE_LICENSE("GPL");