]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/libertas/main.c
cf2d719438652705315b5642a453a2d135cb9f16
[karo-tx-linux.git] / 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
14 #include <net/iw_handler.h>
15 #include <net/ieee80211.h>
16
17 #include "host.h"
18 #include "decl.h"
19 #include "dev.h"
20 #include "wext.h"
21 #include "debugfs.h"
22 #include "assoc.h"
23
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     "";
30
31
32 /* Module parameters */
33 unsigned int libertas_debug = 0;
34 module_param(libertas_debug, int, 0644);
35 EXPORT_SYMBOL_GPL(libertas_debug);
36
37
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 */
43
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}
58 };
59
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}
75 };
76
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}
81 };
82
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}
89 };
90
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}
107 };
108
109 /**
110  * the structure for channel, frequency and power
111  */
112 struct region_cfp_table {
113         u8 region;
114         struct chan_freq_power *cfp_BG;
115         int cfp_no_BG;
116 };
117
118 /**
119  * the structure for the mapping between region and CFP
120  */
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),
125          }
126         ,
127         {0x20,                  /*CANADA IC */
128          channel_freq_power_US_BG,
129          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
130          }
131         ,
132         {0x30, /*EU*/ channel_freq_power_EU_BG,
133          sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
134          }
135         ,
136         {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
137          sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
138          }
139         ,
140         {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
141          sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
142          }
143         ,
144         {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
145          sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
146          }
147         ,
148 /*Add new region here */
149 };
150
151 /**
152  * the rates supported
153  */
154 u8 libertas_supported_rates[G_SUPPORTED_RATES] =
155     { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
156 0 };
157
158 /**
159  * the rates supported for ad-hoc G mode
160  */
161 u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
162     { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
163 0 };
164
165 /**
166  * the rates supported for ad-hoc B mode
167  */
168 u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
169
170 /**
171  * the table to keep region code
172  */
173 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
174     { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
175
176 /**
177  * Attributes exported through sysfs
178  */
179
180 /**
181  * @brief Get function for sysfs attribute anycast_mask
182  */
183 static ssize_t libertas_anycast_get(struct device * dev,
184                 struct device_attribute *attr, char * buf)
185 {
186         struct cmd_ds_mesh_access mesh_access;
187
188         memset(&mesh_access, 0, sizeof(mesh_access));
189         libertas_prepare_and_send_command(to_net_dev(dev)->priv,
190                         cmd_mesh_access,
191                         cmd_act_mesh_get_anycast,
192                         cmd_option_waitforrsp, 0, (void *)&mesh_access);
193
194         return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
195 }
196
197 /**
198  * @brief Set function for sysfs attribute anycast_mask
199  */
200 static ssize_t libertas_anycast_set(struct device * dev,
201                 struct device_attribute *attr, const char * buf, size_t count)
202 {
203         struct cmd_ds_mesh_access mesh_access;
204         uint32_t datum;
205
206         memset(&mesh_access, 0, sizeof(mesh_access));
207         sscanf(buf, "%x", &datum);
208         mesh_access.data[0] = cpu_to_le32(datum);
209
210         libertas_prepare_and_send_command((to_net_dev(dev))->priv,
211                         cmd_mesh_access,
212                         cmd_act_mesh_set_anycast,
213                         cmd_option_waitforrsp, 0, (void *)&mesh_access);
214         return strlen(buf);
215 }
216
217 /**
218  * anycast_mask attribute to be exported per mshX interface
219  * through sysfs (/sys/class/net/mshX/anycast_mask)
220  */
221 static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
222
223 /**
224  *  @brief Check if the device can be open and wait if necessary.
225  *
226  *  @param dev     A pointer to net_device structure
227  *  @return        0
228  *
229  * For USB adapter, on some systems the device open handler will be
230  * called before FW ready. Use the following flag check and wait
231  * function to work around the issue.
232  *
233  */
234 static int pre_open_check(struct net_device *dev)
235 {
236         wlan_private *priv = (wlan_private *) dev->priv;
237         wlan_adapter *adapter = priv->adapter;
238         int i = 0;
239
240         while (!adapter->fw_ready && i < 20) {
241                 i++;
242                 msleep_interruptible(100);
243         }
244         if (!adapter->fw_ready) {
245                 lbs_pr_err("firmware not ready\n");
246                 return -1;
247         }
248
249         return 0;
250 }
251
252 /**
253  *  @brief This function opens the device
254  *
255  *  @param dev     A pointer to net_device structure
256  *  @return        0
257  */
258 static int wlan_dev_open(struct net_device *dev)
259 {
260         wlan_private *priv = (wlan_private *) dev->priv;
261         wlan_adapter *adapter = priv->adapter;
262
263         lbs_deb_enter(LBS_DEB_NET);
264
265         priv->open = 1;
266
267         if (adapter->connect_status == libertas_connected) {
268                 netif_carrier_on(priv->dev);
269                 netif_carrier_on(priv->mesh_dev);
270         } else {
271                 netif_carrier_off(priv->dev);
272                 netif_carrier_off(priv->mesh_dev);
273         }
274
275         lbs_deb_leave(LBS_DEB_NET);
276         return 0;
277 }
278 /**
279  *  @brief This function opens the mshX interface
280  *
281  *  @param dev     A pointer to net_device structure
282  *  @return        0
283  */
284 static int mesh_open(struct net_device *dev)
285 {
286         wlan_private *priv = (wlan_private *) dev->priv ;
287
288         if (pre_open_check(dev) == -1)
289                 return -1;
290         priv->mesh_open = 1 ;
291         netif_wake_queue(priv->mesh_dev);
292         if (priv->infra_open == 0)
293                 return wlan_dev_open(priv->dev) ;
294         return 0;
295 }
296
297 /**
298  *  @brief This function opens the ethX interface
299  *
300  *  @param dev     A pointer to net_device structure
301  *  @return        0
302  */
303 static int wlan_open(struct net_device *dev)
304 {
305         wlan_private *priv = (wlan_private *) dev->priv ;
306
307         if(pre_open_check(dev) == -1)
308                 return -1;
309         priv->infra_open = 1 ;
310         netif_wake_queue(priv->dev);
311         if (priv->open == 0)
312                 return wlan_dev_open(priv->dev) ;
313         return 0;
314 }
315
316 static int wlan_dev_close(struct net_device *dev)
317 {
318         wlan_private *priv = dev->priv;
319
320         lbs_deb_enter(LBS_DEB_NET);
321
322         netif_carrier_off(priv->dev);
323         priv->open = 0;
324
325         lbs_deb_leave(LBS_DEB_NET);
326         return 0;
327 }
328
329 /**
330  *  @brief This function closes the mshX interface
331  *
332  *  @param dev     A pointer to net_device structure
333  *  @return        0
334  */
335 static int mesh_close(struct net_device *dev)
336 {
337         wlan_private *priv = (wlan_private *) (dev->priv);
338
339         priv->mesh_open = 0;
340         netif_stop_queue(priv->mesh_dev);
341         if (priv->infra_open == 0)
342                 return wlan_dev_close(dev);
343         else
344                 return 0;
345 }
346
347 /**
348  *  @brief This function closes the ethX interface
349  *
350  *  @param dev     A pointer to net_device structure
351  *  @return        0
352  */
353 static int wlan_close(struct net_device *dev)
354 {
355         wlan_private *priv = (wlan_private *) dev->priv;
356
357         netif_stop_queue(dev);
358         priv->infra_open = 0;
359         if (priv->mesh_open == 0)
360                 return wlan_dev_close(dev);
361         else
362                 return 0;
363 }
364
365
366 static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
367 {
368         int ret = 0;
369         wlan_private *priv = dev->priv;
370
371         lbs_deb_enter(LBS_DEB_NET);
372
373         if (priv->dnld_sent || priv->adapter->TxLockFlag) {
374                 priv->stats.tx_dropped++;
375                 goto done;
376         }
377
378         netif_stop_queue(priv->dev);
379         netif_stop_queue(priv->mesh_dev);
380
381         if (libertas_process_tx(priv, skb) == 0)
382                 dev->trans_start = jiffies;
383 done:
384         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
385         return ret;
386 }
387
388 /**
389  * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
390  *
391  */
392 static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
393 {
394         wlan_private *priv = dev->priv;
395         int ret;
396
397         lbs_deb_enter(LBS_DEB_MESH);
398
399         SET_MESH_FRAME(skb);
400
401         ret = wlan_hard_start_xmit(skb, priv->dev);
402         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
403         return ret;
404 }
405
406 /**
407  * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
408  *
409  */
410 static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
411 {
412         int ret;
413
414         lbs_deb_enter(LBS_DEB_NET);
415
416         UNSET_MESH_FRAME(skb);
417
418         ret = wlan_hard_start_xmit(skb, dev);
419         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
420         return ret;
421 }
422
423 static void wlan_tx_timeout(struct net_device *dev)
424 {
425         wlan_private *priv = (wlan_private *) dev->priv;
426
427         lbs_deb_enter(LBS_DEB_TX);
428
429         lbs_pr_err("tx watch dog timeout\n");
430
431         priv->dnld_sent = DNLD_RES_RECEIVED;
432         dev->trans_start = jiffies;
433
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);
440                 } else
441                         wake_up_interruptible(&priv->mainthread.waitq);
442         } else if (priv->adapter->connect_status == libertas_connected) {
443                 netif_wake_queue(priv->dev);
444                 netif_wake_queue(priv->mesh_dev);
445         }
446
447         lbs_deb_leave(LBS_DEB_TX);
448 }
449
450 /**
451  *  @brief This function returns the network statistics
452  *
453  *  @param dev     A pointer to wlan_private structure
454  *  @return        A pointer to net_device_stats structure
455  */
456 static struct net_device_stats *wlan_get_stats(struct net_device *dev)
457 {
458         wlan_private *priv = (wlan_private *) dev->priv;
459
460         return &priv->stats;
461 }
462
463 static int wlan_set_mac_address(struct net_device *dev, void *addr)
464 {
465         int ret = 0;
466         wlan_private *priv = (wlan_private *) dev->priv;
467         wlan_adapter *adapter = priv->adapter;
468         struct sockaddr *phwaddr = addr;
469
470         lbs_deb_enter(LBS_DEB_NET);
471
472         /* In case it was called from the mesh device */
473         dev = priv->dev ;
474
475         memset(adapter->current_addr, 0, ETH_ALEN);
476
477         /* dev->dev_addr is 8 bytes */
478         lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
479
480         lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
481         memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
482
483         ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
484                                     cmd_act_set,
485                                     cmd_option_waitforrsp, 0, NULL);
486
487         if (ret) {
488                 lbs_deb_net("set MAC address failed\n");
489                 ret = -1;
490                 goto done;
491         }
492
493         lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
494         memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
495         if (priv->mesh_dev)
496                 memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
497
498 done:
499         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
500         return ret;
501 }
502
503 static int wlan_copy_multicast_address(wlan_adapter * adapter,
504                                      struct net_device *dev)
505 {
506         int i = 0;
507         struct dev_mc_list *mcptr = dev->mc_list;
508
509         for (i = 0; i < dev->mc_count; i++) {
510                 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
511                 mcptr = mcptr->next;
512         }
513
514         return i;
515
516 }
517
518 static void wlan_set_multicast_list(struct net_device *dev)
519 {
520         wlan_private *priv = dev->priv;
521         wlan_adapter *adapter = priv->adapter;
522         int oldpacketfilter;
523
524         lbs_deb_enter(LBS_DEB_NET);
525
526         oldpacketfilter = adapter->currentpacketfilter;
527
528         if (dev->flags & IFF_PROMISC) {
529                 lbs_deb_net("enable promiscuous mode\n");
530                 adapter->currentpacketfilter |=
531                     cmd_act_mac_promiscuous_enable;
532                 adapter->currentpacketfilter &=
533                     ~(cmd_act_mac_all_multicast_enable |
534                       cmd_act_mac_multicast_enable);
535         } else {
536                 /* Multicast */
537                 adapter->currentpacketfilter &=
538                     ~cmd_act_mac_promiscuous_enable;
539
540                 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
541                     MRVDRV_MAX_MULTICAST_LIST_SIZE) {
542                         lbs_deb_net( "enabling all multicast\n");
543                         adapter->currentpacketfilter |=
544                             cmd_act_mac_all_multicast_enable;
545                         adapter->currentpacketfilter &=
546                             ~cmd_act_mac_multicast_enable;
547                 } else {
548                         adapter->currentpacketfilter &=
549                             ~cmd_act_mac_all_multicast_enable;
550
551                         if (!dev->mc_count) {
552                                 lbs_deb_net("no multicast addresses, "
553                                        "disabling multicast\n");
554                                 adapter->currentpacketfilter &=
555                                     ~cmd_act_mac_multicast_enable;
556                         } else {
557                                 int i;
558
559                                 adapter->currentpacketfilter |=
560                                     cmd_act_mac_multicast_enable;
561
562                                 adapter->nr_of_multicastmacaddr =
563                                     wlan_copy_multicast_address(adapter, dev);
564
565                                 lbs_deb_net("multicast addresses: %d\n",
566                                        dev->mc_count);
567
568                                 for (i = 0; i < dev->mc_count; i++) {
569                                         lbs_deb_net("Multicast address %d:"
570                                                MAC_FMT "\n", i,
571                                                adapter->multicastlist[i][0],
572                                                adapter->multicastlist[i][1],
573                                                adapter->multicastlist[i][2],
574                                                adapter->multicastlist[i][3],
575                                                adapter->multicastlist[i][4],
576                                                adapter->multicastlist[i][5]);
577                                 }
578                                 /* send multicast addresses to firmware */
579                                 libertas_prepare_and_send_command(priv,
580                                                       cmd_mac_multicast_adr,
581                                                       cmd_act_set, 0, 0,
582                                                       NULL);
583                         }
584                 }
585         }
586
587         if (adapter->currentpacketfilter != oldpacketfilter) {
588                 libertas_set_mac_packet_filter(priv);
589         }
590
591         lbs_deb_leave(LBS_DEB_NET);
592 }
593
594 /**
595  *  @brief This function handles the major jobs in the WLAN driver.
596  *  It handles all events generated by firmware, RX data received
597  *  from firmware and TX data sent from kernel.
598  *
599  *  @param data    A pointer to wlan_thread structure
600  *  @return        0
601  */
602 static int wlan_service_main_thread(void *data)
603 {
604         struct wlan_thread *thread = data;
605         wlan_private *priv = thread->priv;
606         wlan_adapter *adapter = priv->adapter;
607         wait_queue_t wait;
608         u8 ireg = 0;
609
610         lbs_deb_enter(LBS_DEB_THREAD);
611
612         wlan_activate_thread(thread);
613
614         init_waitqueue_entry(&wait, current);
615
616         for (;;) {
617                 lbs_deb_thread( "main-thread 111: intcounter=%d "
618                        "currenttxskb=%p dnld_sent=%d\n",
619                        adapter->intcounter,
620                        adapter->currenttxskb, priv->dnld_sent);
621
622                 add_wait_queue(&thread->waitq, &wait);
623                 set_current_state(TASK_INTERRUPTIBLE);
624                 spin_lock_irq(&adapter->driver_lock);
625                 if ((adapter->psstate == PS_STATE_SLEEP) ||
626                     (!adapter->intcounter
627                      && (priv->dnld_sent || adapter->cur_cmd ||
628                          list_empty(&adapter->cmdpendingq)))) {
629                         lbs_deb_thread(
630                                "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
631                                adapter->connect_status, adapter->intcounter,
632                                adapter->psmode, adapter->psstate);
633                         spin_unlock_irq(&adapter->driver_lock);
634                         schedule();
635                 } else
636                         spin_unlock_irq(&adapter->driver_lock);
637
638
639                 lbs_deb_thread(
640                        "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
641                        "dnld_sent=%d\n", adapter->intcounter,
642                        adapter->currenttxskb, priv->dnld_sent);
643
644                 set_current_state(TASK_RUNNING);
645                 remove_wait_queue(&thread->waitq, &wait);
646                 try_to_freeze();
647
648                 lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
649                        "dnld_sent=%d\n",
650                        adapter->intcounter,
651                        adapter->currenttxskb, priv->dnld_sent);
652
653                 if (kthread_should_stop()
654                     || adapter->surpriseremoved) {
655                         lbs_deb_thread(
656                                "main-thread: break from main thread: surpriseremoved=0x%x\n",
657                                adapter->surpriseremoved);
658                         break;
659                 }
660
661
662                 spin_lock_irq(&adapter->driver_lock);
663                 if (adapter->intcounter) {
664                         u8 int_status;
665                         adapter->intcounter = 0;
666                         int_status = priv->hw_get_int_status(priv, &ireg);
667
668                         if (int_status) {
669                                 lbs_deb_thread(
670                                        "main-thread: reading HOST_INT_STATUS_REG failed\n");
671                                 spin_unlock_irq(&adapter->driver_lock);
672                                 continue;
673                         }
674                         adapter->hisregcpy |= ireg;
675                 }
676
677                 lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
678                        "dnld_sent=%d\n",
679                        adapter->intcounter,
680                        adapter->currenttxskb, priv->dnld_sent);
681
682                 /* command response? */
683                 if (adapter->hisregcpy & his_cmdupldrdy) {
684                         lbs_deb_thread("main-thread: cmd response ready\n");
685
686                         adapter->hisregcpy &= ~his_cmdupldrdy;
687                         spin_unlock_irq(&adapter->driver_lock);
688                         libertas_process_rx_command(priv);
689                         spin_lock_irq(&adapter->driver_lock);
690                 }
691
692                 /* Any Card Event */
693                 if (adapter->hisregcpy & his_cardevent) {
694                         lbs_deb_thread("main-thread: Card Event Activity\n");
695
696                         adapter->hisregcpy &= ~his_cardevent;
697
698                         if (priv->hw_read_event_cause(priv)) {
699                                 lbs_pr_alert(
700                                        "main-thread: hw_read_event_cause failed\n");
701                                 spin_unlock_irq(&adapter->driver_lock);
702                                 continue;
703                         }
704                         spin_unlock_irq(&adapter->driver_lock);
705                         libertas_process_event(priv);
706                 } else
707                         spin_unlock_irq(&adapter->driver_lock);
708
709                 /* Check if we need to confirm Sleep Request received previously */
710                 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
711                         if (!priv->dnld_sent && !adapter->cur_cmd) {
712                                 if (adapter->connect_status ==
713                                     libertas_connected) {
714                                         lbs_deb_thread(
715                                                "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
716                                                "dnld_sent=%d cur_cmd=%p, confirm now\n",
717                                                adapter->intcounter,
718                                                adapter->currenttxskb,
719                                                priv->dnld_sent,
720                                                adapter->cur_cmd);
721
722                                         libertas_ps_confirm_sleep(priv,
723                                                        (u16) adapter->psmode);
724                                 } else {
725                                         /* workaround for firmware sending
726                                          * deauth/linkloss event immediately
727                                          * after sleep request, remove this
728                                          * after firmware fixes it
729                                          */
730                                         adapter->psstate = PS_STATE_AWAKE;
731                                         lbs_pr_alert(
732                                                "main-thread: ignore PS_SleepConfirm in non-connected state\n");
733                                 }
734                         }
735                 }
736
737                 /* The PS state is changed during processing of Sleep Request
738                  * event above
739                  */
740                 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
741                     (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
742                         continue;
743
744                 /* Execute the next command */
745                 if (!priv->dnld_sent && !priv->adapter->cur_cmd)
746                         libertas_execute_next_command(priv);
747
748                 /* Wake-up command waiters which can't sleep in
749                  * libertas_prepare_and_send_command
750                  */
751                 if (!adapter->nr_cmd_pending)
752                         wake_up_all(&adapter->cmd_pending);
753
754                 libertas_tx_runqueue(priv);
755         }
756
757         del_timer(&adapter->command_timer);
758         adapter->nr_cmd_pending = 0;
759         wake_up_all(&adapter->cmd_pending);
760         wlan_deactivate_thread(thread);
761
762         lbs_deb_leave(LBS_DEB_THREAD);
763         return 0;
764 }
765
766 /**
767  * @brief This function adds the card. it will probe the
768  * card, allocate the wlan_priv and initialize the device.
769  *
770  *  @param card    A pointer to card
771  *  @return        A pointer to wlan_private structure
772  */
773 wlan_private *libertas_add_card(void *card, struct device *dmdev)
774 {
775         struct net_device *dev = NULL;
776         wlan_private *priv = NULL;
777
778         lbs_deb_enter(LBS_DEB_NET);
779
780         /* Allocate an Ethernet device and register it */
781         if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
782                 lbs_pr_err("init ethX device failed\n");
783                 return NULL;
784         }
785         priv = dev->priv;
786
787         /* allocate buffer for wlan_adapter */
788         if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
789                 lbs_pr_err("allocate buffer for wlan_adapter failed\n");
790                 goto err_kzalloc;
791         }
792
793         priv->dev = dev;
794         priv->card = card;
795         priv->mesh_open = 0;
796         priv->infra_open = 0;
797
798         SET_MODULE_OWNER(dev);
799
800         /* Setup the OS Interface to our functions */
801         dev->open = wlan_open;
802         dev->hard_start_xmit = wlan_pre_start_xmit;
803         dev->stop = wlan_close;
804         dev->do_ioctl = libertas_do_ioctl;
805         dev->set_mac_address = wlan_set_mac_address;
806         dev->tx_timeout = wlan_tx_timeout;
807         dev->get_stats = wlan_get_stats;
808         dev->watchdog_timeo = 5 * HZ;
809         dev->ethtool_ops = &libertas_ethtool_ops;
810 #ifdef  WIRELESS_EXT
811         dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
812 #endif
813 #define NETIF_F_DYNALLOC 16
814         dev->features |= NETIF_F_DYNALLOC;
815         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
816         dev->set_multicast_list = wlan_set_multicast_list;
817
818         SET_NETDEV_DEV(dev, dmdev);
819
820         INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
821         INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
822
823         spin_lock_init(&priv->adapter->driver_lock);
824         init_waitqueue_head(&priv->adapter->cmd_pending);
825         priv->adapter->nr_cmd_pending = 0;
826         goto done;
827
828 err_kzalloc:
829         free_netdev(dev);
830         priv = NULL;
831 done:
832         lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
833         return priv;
834 }
835 EXPORT_SYMBOL_GPL(libertas_add_card);
836
837 int libertas_activate_card(wlan_private *priv, char *fw_name)
838 {
839         struct net_device *dev = priv->dev;
840         int ret = -1;
841
842         lbs_deb_enter(LBS_DEB_MAIN);
843
844         lbs_deb_thread("Starting kthread...\n");
845         priv->mainthread.priv = priv;
846         wlan_create_thread(wlan_service_main_thread,
847                            &priv->mainthread, "wlan_main_service");
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->mainthread.waitq);
888         wlan_terminate_thread(&priv->mainthread);
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->do_ioctl = libertas_do_ioctl;
924         mesh_dev->get_stats = wlan_get_stats;
925         mesh_dev->set_mac_address = wlan_set_mac_address;
926         mesh_dev->ethtool_ops = &libertas_ethtool_ops;
927         memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
928                         sizeof(priv->dev->dev_addr));
929
930         SET_NETDEV_DEV(priv->mesh_dev, dev);
931
932 #ifdef  WIRELESS_EXT
933         mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
934 #endif
935 #define NETIF_F_DYNALLOC 16
936
937         /* Register virtual mesh interface */
938         ret = register_netdev(mesh_dev);
939         if (ret) {
940                 lbs_pr_err("cannot register mshX virtual interface\n");
941                 goto err_free;
942         }
943
944         ret = device_create_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
945         if (ret)
946                 goto err_unregister;
947
948         /* Everything successful */
949         ret = 0;
950         goto done;
951
952
953 err_unregister:
954         unregister_netdev(mesh_dev);
955
956 err_free:
957         free_netdev(mesh_dev);
958
959 done:
960         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
961         return ret;
962 }
963 EXPORT_SYMBOL_GPL(libertas_add_mesh);
964
965 static void wake_pending_cmdnodes(wlan_private *priv)
966 {
967         struct cmd_ctrl_node *cmdnode;
968         unsigned long flags;
969
970         lbs_deb_enter(LBS_DEB_CMD);
971
972         spin_lock_irqsave(&priv->adapter->driver_lock, flags);
973         list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
974                 cmdnode->cmdwaitqwoken = 1;
975                 wake_up_interruptible(&cmdnode->cmdwait_q);
976         }
977         spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
978 }
979
980
981 int libertas_remove_card(wlan_private *priv)
982 {
983         wlan_adapter *adapter;
984         struct net_device *dev;
985         union iwreq_data wrqu;
986
987         lbs_deb_enter(LBS_DEB_NET);
988
989         if (!priv)
990                 goto out;
991
992         adapter = priv->adapter;
993
994         if (!adapter)
995                 goto out;
996
997         dev = priv->dev;
998
999         netif_stop_queue(priv->dev);
1000         netif_carrier_off(priv->dev);
1001
1002         wake_pending_cmdnodes(priv);
1003
1004         unregister_netdev(dev);
1005
1006         cancel_delayed_work(&priv->assoc_work);
1007         destroy_workqueue(priv->assoc_thread);
1008
1009         if (adapter->psmode == wlan802_11powermodemax_psp) {
1010                 adapter->psmode = wlan802_11powermodecam;
1011                 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
1012         }
1013
1014         memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1015         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1016         wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1017
1018         adapter->surpriseremoved = 1;
1019
1020         /* Stop the thread servicing the interrupts */
1021         wlan_terminate_thread(&priv->mainthread);
1022
1023         libertas_debugfs_remove_one(priv);
1024
1025         lbs_deb_net("free adapter\n");
1026         libertas_free_adapter(priv);
1027
1028         lbs_deb_net("unregister finish\n");
1029
1030         priv->dev = NULL;
1031         free_netdev(dev);
1032
1033 out:
1034         lbs_deb_leave(LBS_DEB_NET);
1035         return 0;
1036 }
1037 EXPORT_SYMBOL_GPL(libertas_remove_card);
1038
1039
1040 void libertas_remove_mesh(wlan_private *priv)
1041 {
1042         struct net_device *mesh_dev;
1043
1044         lbs_deb_enter(LBS_DEB_NET);
1045
1046         if (!priv)
1047                 goto out;
1048
1049         mesh_dev = priv->mesh_dev;
1050
1051         netif_stop_queue(mesh_dev);
1052         netif_carrier_off(priv->mesh_dev);
1053
1054         device_remove_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
1055         unregister_netdev(mesh_dev);
1056
1057         priv->mesh_dev = NULL ;
1058         free_netdev(mesh_dev);
1059
1060 out:
1061         lbs_deb_leave(LBS_DEB_NET);
1062 }
1063 EXPORT_SYMBOL_GPL(libertas_remove_mesh);
1064
1065 /**
1066  *  @brief This function finds the CFP in
1067  *  region_cfp_table based on region and band parameter.
1068  *
1069  *  @param region  The region code
1070  *  @param band    The band
1071  *  @param cfp_no  A pointer to CFP number
1072  *  @return        A pointer to CFP
1073  */
1074 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
1075 {
1076         int i, end;
1077
1078         lbs_deb_enter(LBS_DEB_MAIN);
1079
1080         end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1081
1082         for (i = 0; i < end ; i++) {
1083                 lbs_deb_main("region_cfp_table[i].region=%d\n",
1084                         region_cfp_table[i].region);
1085                 if (region_cfp_table[i].region == region) {
1086                         *cfp_no = region_cfp_table[i].cfp_no_BG;
1087                         lbs_deb_leave(LBS_DEB_MAIN);
1088                         return region_cfp_table[i].cfp_BG;
1089                 }
1090         }
1091
1092         lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1093         return NULL;
1094 }
1095
1096 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1097 {
1098         wlan_adapter *adapter = priv->adapter;
1099         int ret = 0;
1100         int i = 0;
1101
1102         struct chan_freq_power *cfp;
1103         int cfp_no;
1104
1105         lbs_deb_enter(LBS_DEB_MAIN);
1106
1107         memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1108
1109         {
1110                 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1111                 if (cfp != NULL) {
1112                         adapter->region_channel[i].nrcfp = cfp_no;
1113                         adapter->region_channel[i].CFP = cfp;
1114                 } else {
1115                         lbs_deb_main("wrong region code %#x in band B/G\n",
1116                                region);
1117                         ret = -1;
1118                         goto out;
1119                 }
1120                 adapter->region_channel[i].valid = 1;
1121                 adapter->region_channel[i].region = region;
1122                 adapter->region_channel[i].band = band;
1123                 i++;
1124         }
1125 out:
1126         lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1127         return ret;
1128 }
1129
1130 /**
1131  *  @brief This function handles the interrupt. it will change PS
1132  *  state if applicable. it will wake up main_thread to handle
1133  *  the interrupt event as well.
1134  *
1135  *  @param dev     A pointer to net_device structure
1136  *  @return        n/a
1137  */
1138 void libertas_interrupt(struct net_device *dev)
1139 {
1140         wlan_private *priv = dev->priv;
1141
1142         lbs_deb_enter(LBS_DEB_THREAD);
1143
1144         lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
1145                priv->adapter->intcounter);
1146
1147         priv->adapter->intcounter++;
1148
1149         if (priv->adapter->psstate == PS_STATE_SLEEP) {
1150                 priv->adapter->psstate = PS_STATE_AWAKE;
1151                 netif_wake_queue(dev);
1152                 netif_wake_queue(priv->mesh_dev);
1153         }
1154
1155         wake_up_interruptible(&priv->mainthread.waitq);
1156
1157         lbs_deb_leave(LBS_DEB_THREAD);
1158 }
1159 EXPORT_SYMBOL_GPL(libertas_interrupt);
1160
1161 static int libertas_init_module(void)
1162 {
1163         lbs_deb_enter(LBS_DEB_MAIN);
1164         libertas_debugfs_init();
1165         lbs_deb_leave(LBS_DEB_MAIN);
1166         return 0;
1167 }
1168
1169 static void libertas_exit_module(void)
1170 {
1171         lbs_deb_enter(LBS_DEB_MAIN);
1172
1173         libertas_debugfs_remove();
1174
1175         lbs_deb_leave(LBS_DEB_MAIN);
1176 }
1177
1178 module_init(libertas_init_module);
1179 module_exit(libertas_exit_module);
1180
1181 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1182 MODULE_AUTHOR("Marvell International Ltd.");
1183 MODULE_LICENSE("GPL");