]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/libertas/main.c
[PATCH] libertas: make libertas_wlan_data_rates static
[~andy/linux] / 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/delay.h>
8 #include <linux/freezer.h>
9 #include <linux/etherdevice.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12
13 #include <net/iw_handler.h>
14
15 #include "host.h"
16 #include "sbi.h"
17 #include "decl.h"
18 #include "dev.h"
19 #include "fw.h"
20 #include "wext.h"
21 #include "debugfs.h"
22 #include "assoc.h"
23
24 #define DRIVER_RELEASE_VERSION "320.p0"
25 const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
26 #ifdef  DEBUG
27     "-dbg"
28 #endif
29     "";
30
31 #define WLAN_TX_PWR_DEFAULT             20      /*100mW */
32 #define WLAN_TX_PWR_US_DEFAULT          20      /*100mW */
33 #define WLAN_TX_PWR_JP_DEFAULT          16      /*50mW */
34 #define WLAN_TX_PWR_FR_DEFAULT          20      /*100mW */
35 #define WLAN_TX_PWR_EMEA_DEFAULT        20      /*100mW */
36
37 /* Format { channel, frequency (MHz), maxtxpower } */
38 /* band: 'B/G', region: USA FCC/Canada IC */
39 static struct chan_freq_power channel_freq_power_US_BG[] = {
40         {1, 2412, WLAN_TX_PWR_US_DEFAULT},
41         {2, 2417, WLAN_TX_PWR_US_DEFAULT},
42         {3, 2422, WLAN_TX_PWR_US_DEFAULT},
43         {4, 2427, WLAN_TX_PWR_US_DEFAULT},
44         {5, 2432, WLAN_TX_PWR_US_DEFAULT},
45         {6, 2437, WLAN_TX_PWR_US_DEFAULT},
46         {7, 2442, WLAN_TX_PWR_US_DEFAULT},
47         {8, 2447, WLAN_TX_PWR_US_DEFAULT},
48         {9, 2452, WLAN_TX_PWR_US_DEFAULT},
49         {10, 2457, WLAN_TX_PWR_US_DEFAULT},
50         {11, 2462, WLAN_TX_PWR_US_DEFAULT}
51 };
52
53 /* band: 'B/G', region: Europe ETSI */
54 static struct chan_freq_power channel_freq_power_EU_BG[] = {
55         {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
56         {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
57         {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
58         {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
59         {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
60         {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
61         {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
62         {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
63         {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
64         {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
65         {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
66         {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
67         {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
68 };
69
70 /* band: 'B/G', region: Spain */
71 static struct chan_freq_power channel_freq_power_SPN_BG[] = {
72         {10, 2457, WLAN_TX_PWR_DEFAULT},
73         {11, 2462, WLAN_TX_PWR_DEFAULT}
74 };
75
76 /* band: 'B/G', region: France */
77 static struct chan_freq_power channel_freq_power_FR_BG[] = {
78         {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
79         {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
80         {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
81         {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
82 };
83
84 /* band: 'B/G', region: Japan */
85 static struct chan_freq_power channel_freq_power_JPN_BG[] = {
86         {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
87         {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
88         {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
89         {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
90         {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
91         {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
92         {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
93         {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
94         {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
95         {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
96         {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
97         {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
98         {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
99         {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
100 };
101
102 /**
103  * the structure for channel, frequency and power
104  */
105 struct region_cfp_table {
106         u8 region;
107         struct chan_freq_power *cfp_BG;
108         int cfp_no_BG;
109 };
110
111 /**
112  * the structure for the mapping between region and CFP
113  */
114 static struct region_cfp_table region_cfp_table[] = {
115         {0x10,                  /*US FCC */
116          channel_freq_power_US_BG,
117          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
118          }
119         ,
120         {0x20,                  /*CANADA IC */
121          channel_freq_power_US_BG,
122          sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
123          }
124         ,
125         {0x30, /*EU*/ channel_freq_power_EU_BG,
126          sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
127          }
128         ,
129         {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
130          sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
131          }
132         ,
133         {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
134          sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
135          }
136         ,
137         {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
138          sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
139          }
140         ,
141 /*Add new region here */
142 };
143
144 /**
145  * the rates supported
146  */
147 u8 libertas_supported_rates[G_SUPPORTED_RATES] =
148     { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
149 0 };
150
151 /**
152  * the rates supported for ad-hoc G mode
153  */
154 u8 libertas_adhoc_rates_g[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 B mode
160  */
161 u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
162
163 #define MAX_DEVS 5
164 static struct net_device *libertas_devs[MAX_DEVS];
165 static int libertas_found = 0;
166
167 /**
168  * the table to keep region code
169  */
170 u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
171     { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
172
173 static u8 *default_fw_name = "usb8388.bin";
174
175 /**
176  * Attributes exported through sysfs
177  */
178
179 /**
180  * @brief Get function for sysfs attribute libertas_mpp
181  */
182 static ssize_t libertas_mpp_get(struct device * dev,
183                 struct device_attribute *attr, char * buf) {
184         struct cmd_ds_mesh_access mesh_access;
185
186         memset(&mesh_access, 0, sizeof(mesh_access));
187         libertas_prepare_and_send_command(to_net_dev(dev)->priv,
188                         cmd_mesh_access,
189                         cmd_act_mesh_get_mpp,
190                         cmd_option_waitforrsp, 0, (void *)&mesh_access);
191
192         return snprintf(buf, 3, "%d\n", mesh_access.data[0]);
193 }
194
195 /**
196  * @brief Set function for sysfs attribute libertas_mpp
197  */
198 static ssize_t libertas_mpp_set(struct device * dev,
199                 struct device_attribute *attr, const char * buf, size_t count) {
200         struct cmd_ds_mesh_access mesh_access;
201
202
203         memset(&mesh_access, 0, sizeof(mesh_access));
204         sscanf(buf, "%d", &(mesh_access.data[0]));
205         libertas_prepare_and_send_command((to_net_dev(dev))->priv,
206                         cmd_mesh_access,
207                         cmd_act_mesh_set_mpp,
208                         cmd_option_waitforrsp, 0, (void *)&mesh_access);
209         return strlen(buf);
210 }
211
212 /**
213  * libertas_mpp attribute to be exported per mshX interface
214  * through sysfs (/sys/class/net/mshX/libertas-mpp)
215  */
216 static DEVICE_ATTR(libertas_mpp, 0644, libertas_mpp_get,
217                 libertas_mpp_set );
218
219 /**
220  *  @brief Check if the device can be open and wait if necessary.
221  *
222  *  @param dev     A pointer to net_device structure
223  *  @return        0
224  *
225  * For USB adapter, on some systems the device open handler will be
226  * called before FW ready. Use the following flag check and wait
227  * function to work around the issue.
228  *
229  */
230 static int pre_open_check(struct net_device *dev) {
231         wlan_private *priv = (wlan_private *) dev->priv;
232         wlan_adapter *adapter = priv->adapter;
233         int i = 0;
234
235         while (!adapter->fw_ready && i < 20) {
236                 i++;
237                 msleep_interruptible(100);
238         }
239         if (!adapter->fw_ready) {
240                 lbs_pr_info("FW not ready, pre_open_check() return failure\n");
241                 LEAVE();
242                 return -1;
243         }
244
245         return 0;
246 }
247
248 /**
249  *  @brief This function opens the device
250  *
251  *  @param dev     A pointer to net_device structure
252  *  @return        0
253  */
254 static int wlan_dev_open(struct net_device *dev)
255 {
256         wlan_private *priv = (wlan_private *) dev->priv;
257         wlan_adapter *adapter = priv->adapter;
258
259         ENTER();
260
261
262         priv->open = 1;
263
264         if (adapter->connect_status == libertas_connected) {
265                 netif_carrier_on(priv->wlan_dev.netdev);
266         } else
267                 netif_carrier_off(priv->wlan_dev.netdev);
268
269         LEAVE();
270         return 0;
271 }
272 /**
273  *  @brief This function opens the mshX interface
274  *
275  *  @param dev     A pointer to net_device structure
276  *  @return        0
277  */
278 static int mesh_open(struct net_device *dev)
279 {
280         wlan_private *priv = (wlan_private *) dev->priv ;
281
282         if(pre_open_check(dev) == -1)
283                 return -1;
284         priv->mesh_open = 1 ;
285         netif_start_queue(priv->mesh_dev);
286         if (priv->infra_open == 0)
287                 return wlan_dev_open(priv->wlan_dev.netdev) ;
288         return 0;
289 }
290
291 /**
292  *  @brief This function opens the ethX interface
293  *
294  *  @param dev     A pointer to net_device structure
295  *  @return        0
296  */
297 static int wlan_open(struct net_device *dev)
298 {
299         wlan_private *priv = (wlan_private *) dev->priv ;
300
301         if(pre_open_check(dev) == -1)
302                 return -1;
303         priv->infra_open = 1 ;
304         netif_wake_queue(priv->wlan_dev.netdev);
305         if (priv->open == 0)
306                 return wlan_dev_open(priv->wlan_dev.netdev) ;
307         return 0;
308 }
309
310 static int wlan_dev_close(struct net_device *dev)
311 {
312         wlan_private *priv = dev->priv;
313
314         ENTER();
315
316         netif_carrier_off(priv->wlan_dev.netdev);
317         priv->open = 0;
318
319         LEAVE();
320         return 0;
321 }
322
323 /**
324  *  @brief This function closes the mshX interface
325  *
326  *  @param dev     A pointer to net_device structure
327  *  @return        0
328  */
329 static int mesh_close(struct net_device *dev)
330 {
331         wlan_private *priv = (wlan_private *) (dev->priv);
332
333         priv->mesh_open = 0;
334         netif_stop_queue(priv->mesh_dev);
335         if (priv->infra_open == 0)
336                 return wlan_dev_close( ((wlan_private *) dev->priv)->wlan_dev.netdev) ;
337         else
338                 return 0;
339 }
340
341 /**
342  *  @brief This function closes the ethX interface
343  *
344  *  @param dev     A pointer to net_device structure
345  *  @return        0
346  */
347 static int wlan_close(struct net_device *dev) {
348         wlan_private *priv = (wlan_private *) dev->priv;
349
350         netif_stop_queue(priv->wlan_dev.netdev);
351         priv->infra_open = 0;
352         if (priv->mesh_open == 0)
353                 return wlan_dev_close( ((wlan_private *) dev->priv)->wlan_dev.netdev) ;
354         else
355                 return 0;
356 }
357
358
359 static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
360 {
361         int ret = 0;
362         wlan_private *priv = dev->priv;
363
364         ENTER();
365
366         if (priv->wlan_dev.dnld_sent || priv->adapter->TxLockFlag) {
367                 priv->stats.tx_dropped++;
368                 goto done;
369         }
370
371         netif_stop_queue(priv->wlan_dev.netdev);
372
373         if (libertas_process_tx(priv, skb) == 0)
374                 dev->trans_start = jiffies;
375 done:
376         LEAVE();
377         return ret;
378 }
379
380 /**
381  * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
382  *
383  */
384 static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
385 {
386         wlan_private *priv = dev->priv;
387         ENTER();
388         SET_MESH_FRAME(skb);
389         LEAVE();
390
391         return wlan_hard_start_xmit(skb, priv->wlan_dev.netdev);
392 }
393
394 /**
395  * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
396  *
397  */
398 static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev) {
399         ENTER();
400         UNSET_MESH_FRAME(skb);
401         LEAVE();
402         return wlan_hard_start_xmit(skb, dev);
403 }
404
405 static void wlan_tx_timeout(struct net_device *dev)
406 {
407         wlan_private *priv = (wlan_private *) dev->priv;
408
409         ENTER();
410
411         lbs_pr_err("tx watch dog timeout!\n");
412
413         priv->wlan_dev.dnld_sent = DNLD_RES_RECEIVED;
414         dev->trans_start = jiffies;
415
416         if (priv->adapter->currenttxskb) {
417                 if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
418                         /* If we are here, we have not received feedback from
419                            the previous packet.  Assume TX_FAIL and move on. */
420                         priv->adapter->eventcause = 0x01000000;
421                         libertas_send_tx_feedback(priv);
422                 } else
423                         wake_up_interruptible(&priv->mainthread.waitq);
424         } else if (priv->adapter->connect_status == libertas_connected)
425                 netif_wake_queue(priv->wlan_dev.netdev);
426
427         LEAVE();
428 }
429
430 /**
431  *  @brief This function returns the network statistics
432  *
433  *  @param dev     A pointer to wlan_private structure
434  *  @return        A pointer to net_device_stats structure
435  */
436 static struct net_device_stats *wlan_get_stats(struct net_device *dev)
437 {
438         wlan_private *priv = (wlan_private *) dev->priv;
439
440         return &priv->stats;
441 }
442
443 static int wlan_set_mac_address(struct net_device *dev, void *addr)
444 {
445         int ret = 0;
446         wlan_private *priv = (wlan_private *) dev->priv;
447         wlan_adapter *adapter = priv->adapter;
448         struct sockaddr *phwaddr = addr;
449
450         ENTER();
451
452         memset(adapter->current_addr, 0, ETH_ALEN);
453
454         /* dev->dev_addr is 8 bytes */
455         lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
456
457         lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
458         memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
459
460         ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
461                                     cmd_act_set,
462                                     cmd_option_waitforrsp, 0, NULL);
463
464         if (ret) {
465                 lbs_pr_debug(1, "set mac address failed.\n");
466                 ret = -1;
467                 goto done;
468         }
469
470         lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
471         memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
472         memcpy(((wlan_private *) dev->priv)->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
473
474 done:
475         LEAVE();
476         return ret;
477 }
478
479 static int wlan_copy_multicast_address(wlan_adapter * adapter,
480                                      struct net_device *dev)
481 {
482         int i = 0;
483         struct dev_mc_list *mcptr = dev->mc_list;
484
485         for (i = 0; i < dev->mc_count; i++) {
486                 memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
487                 mcptr = mcptr->next;
488         }
489
490         return i;
491
492 }
493
494 static void wlan_set_multicast_list(struct net_device *dev)
495 {
496         wlan_private *priv = dev->priv;
497         wlan_adapter *adapter = priv->adapter;
498         int oldpacketfilter;
499
500         ENTER();
501
502         oldpacketfilter = adapter->currentpacketfilter;
503
504         if (dev->flags & IFF_PROMISC) {
505                 lbs_pr_debug(1, "enable Promiscuous mode\n");
506                 adapter->currentpacketfilter |=
507                     cmd_act_mac_promiscuous_enable;
508                 adapter->currentpacketfilter &=
509                     ~(cmd_act_mac_all_multicast_enable |
510                       cmd_act_mac_multicast_enable);
511         } else {
512                 /* Multicast */
513                 adapter->currentpacketfilter &=
514                     ~cmd_act_mac_promiscuous_enable;
515
516                 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
517                     MRVDRV_MAX_MULTICAST_LIST_SIZE) {
518                         lbs_pr_debug(1, "Enabling All Multicast!\n");
519                         adapter->currentpacketfilter |=
520                             cmd_act_mac_all_multicast_enable;
521                         adapter->currentpacketfilter &=
522                             ~cmd_act_mac_multicast_enable;
523                 } else {
524                         adapter->currentpacketfilter &=
525                             ~cmd_act_mac_all_multicast_enable;
526
527                         if (!dev->mc_count) {
528                                 lbs_pr_debug(1, "No multicast addresses - "
529                                        "disabling multicast!\n");
530                                 adapter->currentpacketfilter &=
531                                     ~cmd_act_mac_multicast_enable;
532                         } else {
533                                 int i;
534
535                                 adapter->currentpacketfilter |=
536                                     cmd_act_mac_multicast_enable;
537
538                                 adapter->nr_of_multicastmacaddr =
539                                     wlan_copy_multicast_address(adapter, dev);
540
541                                 lbs_pr_debug(1, "Multicast addresses: %d\n",
542                                        dev->mc_count);
543
544                                 for (i = 0; i < dev->mc_count; i++) {
545                                         lbs_pr_debug(1, "Multicast address %d:"
546                                                "%x %x %x %x %x %x\n", i,
547                                                adapter->multicastlist[i][0],
548                                                adapter->multicastlist[i][1],
549                                                adapter->multicastlist[i][2],
550                                                adapter->multicastlist[i][3],
551                                                adapter->multicastlist[i][4],
552                                                adapter->multicastlist[i][5]);
553                                 }
554                                 /* set multicast addresses to firmware */
555                                 libertas_prepare_and_send_command(priv,
556                                                       cmd_mac_multicast_adr,
557                                                       cmd_act_set, 0, 0,
558                                                       NULL);
559                         }
560                 }
561         }
562
563         if (adapter->currentpacketfilter != oldpacketfilter) {
564                 libertas_set_mac_packet_filter(priv);
565         }
566
567         LEAVE();
568 }
569
570 /**
571  *  @brief This function hanldes the major job in WLAN driver.
572  *  it handles the event generated by firmware, rx data received
573  *  from firmware and tx data sent from kernel.
574  *
575  *  @param data    A pointer to wlan_thread structure
576  *  @return        0
577  */
578 static int wlan_service_main_thread(void *data)
579 {
580         struct wlan_thread *thread = data;
581         wlan_private *priv = thread->priv;
582         wlan_adapter *adapter = priv->adapter;
583         wait_queue_t wait;
584         u8 ireg = 0;
585
586         ENTER();
587
588         wlan_activate_thread(thread);
589
590         init_waitqueue_entry(&wait, current);
591
592         for (;;) {
593                 lbs_pr_debug(1, "main-thread 111: intcounter=%d "
594                        "currenttxskb=%p dnld_sent=%d\n",
595                        adapter->intcounter,
596                        adapter->currenttxskb, priv->wlan_dev.dnld_sent);
597
598                 add_wait_queue(&thread->waitq, &wait);
599                 set_current_state(TASK_INTERRUPTIBLE);
600                 spin_lock_irq(&adapter->driver_lock);
601                 if ((adapter->psstate == PS_STATE_SLEEP) ||
602                     (!adapter->intcounter
603                      && (priv->wlan_dev.dnld_sent || adapter->cur_cmd ||
604                          list_empty(&adapter->cmdpendingq)))) {
605                         lbs_pr_debug(1,
606                                "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
607                                adapter->connect_status, adapter->intcounter,
608                                adapter->psmode, adapter->psstate);
609                         spin_unlock_irq(&adapter->driver_lock);
610                         schedule();
611                 } else
612                         spin_unlock_irq(&adapter->driver_lock);
613
614
615                 lbs_pr_debug(1,
616                        "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
617                        "dnld_sent=%d\n", adapter->intcounter,
618                        adapter->currenttxskb, priv->wlan_dev.dnld_sent);
619
620                 set_current_state(TASK_RUNNING);
621                 remove_wait_queue(&thread->waitq, &wait);
622                 try_to_freeze();
623
624                 lbs_pr_debug(1, "main-thread 333: intcounter=%d currenttxskb=%p "
625                        "dnld_sent=%d\n",
626                        adapter->intcounter,
627                        adapter->currenttxskb, priv->wlan_dev.dnld_sent);
628
629                 if (kthread_should_stop()
630                     || adapter->surpriseremoved) {
631                         lbs_pr_debug(1,
632                                "main-thread: break from main thread: surpriseremoved=0x%x\n",
633                                adapter->surpriseremoved);
634                         break;
635                 }
636
637
638                 spin_lock_irq(&adapter->driver_lock);
639                 if (adapter->intcounter) {
640                         u8 int_status;
641                         adapter->intcounter = 0;
642                         int_status = libertas_sbi_get_int_status(priv, &ireg);
643
644                         if (int_status) {
645                                 lbs_pr_debug(1,
646                                        "main-thread: reading HOST_INT_STATUS_REG failed\n");
647                                 spin_unlock_irq(&adapter->driver_lock);
648                                 continue;
649                         }
650                         adapter->hisregcpy |= ireg;
651                 }
652
653                 lbs_pr_debug(1, "main-thread 444: intcounter=%d currenttxskb=%p "
654                        "dnld_sent=%d\n",
655                        adapter->intcounter,
656                        adapter->currenttxskb, priv->wlan_dev.dnld_sent);
657
658                 /* command response? */
659                 if (adapter->hisregcpy & his_cmdupldrdy) {
660                         lbs_pr_debug(1, "main-thread: cmd response ready.\n");
661
662                         adapter->hisregcpy &= ~his_cmdupldrdy;
663                         spin_unlock_irq(&adapter->driver_lock);
664                         libertas_process_rx_command(priv);
665                         spin_lock_irq(&adapter->driver_lock);
666                 }
667
668                 /* Any Card Event */
669                 if (adapter->hisregcpy & his_cardevent) {
670                         lbs_pr_debug(1, "main-thread: Card Event Activity.\n");
671
672                         adapter->hisregcpy &= ~his_cardevent;
673
674                         if (libertas_sbi_read_event_cause(priv)) {
675                                 lbs_pr_alert(
676                                        "main-thread: libertas_sbi_read_event_cause failed.\n");
677                                 spin_unlock_irq(&adapter->driver_lock);
678                                 continue;
679                         }
680                         spin_unlock_irq(&adapter->driver_lock);
681                         libertas_process_event(priv);
682                 } else
683                         spin_unlock_irq(&adapter->driver_lock);
684
685                 /* Check if we need to confirm Sleep Request received previously */
686                 if (adapter->psstate == PS_STATE_PRE_SLEEP) {
687                         if (!priv->wlan_dev.dnld_sent && !adapter->cur_cmd) {
688                                 if (adapter->connect_status ==
689                                     libertas_connected) {
690                                         lbs_pr_debug(1,
691                                                "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
692                                                "dnld_sent=%d cur_cmd=%p, confirm now\n",
693                                                adapter->intcounter,
694                                                adapter->currenttxskb,
695                                                priv->wlan_dev.dnld_sent,
696                                                adapter->cur_cmd);
697
698                                         libertas_ps_confirm_sleep(priv,
699                                                        (u16) adapter->psmode);
700                                 } else {
701                                         /* workaround for firmware sending
702                                          * deauth/linkloss event immediately
703                                          * after sleep request, remove this
704                                          * after firmware fixes it
705                                          */
706                                         adapter->psstate = PS_STATE_AWAKE;
707                                         lbs_pr_alert(
708                                                "main-thread: ignore PS_SleepConfirm in non-connected state\n");
709                                 }
710                         }
711                 }
712
713                 /* The PS state is changed during processing of Sleep Request
714                  * event above
715                  */
716                 if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
717                     (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
718                         continue;
719
720                 /* Execute the next command */
721                 if (!priv->wlan_dev.dnld_sent && !priv->adapter->cur_cmd)
722                         libertas_execute_next_command(priv);
723
724                 /* Wake-up command waiters which can't sleep in
725                  * libertas_prepare_and_send_command
726                  */
727                 if (!adapter->nr_cmd_pending)
728                         wake_up_all(&adapter->cmd_pending);
729
730                 libertas_tx_runqueue(priv);
731         }
732
733         del_timer(&adapter->command_timer);
734         adapter->nr_cmd_pending = 0;
735         wake_up_all(&adapter->cmd_pending);
736         wlan_deactivate_thread(thread);
737
738         LEAVE();
739         return 0;
740 }
741
742 /**
743  * @brief This function adds the card. it will probe the
744  * card, allocate the wlan_priv and initialize the device.
745  *
746  *  @param card    A pointer to card
747  *  @return        A pointer to wlan_private structure
748  */
749 wlan_private *wlan_add_card(void *card)
750 {
751         struct net_device *dev = NULL;
752         struct net_device *mesh_dev = NULL;
753         wlan_private *priv = NULL;
754
755         ENTER();
756
757         /* Allocate an Ethernet device and register it */
758         if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
759                 lbs_pr_alert( "Init ethernet device failed!\n");
760                 return NULL;
761         }
762
763         priv = dev->priv;
764
765         /* allocate buffer for wlan_adapter */
766         if (!(priv->adapter = kmalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
767                 lbs_pr_alert( "Allocate buffer for wlan_adapter failed!\n");
768                 goto err_kmalloc;
769         }
770
771         /* Allocate a virtual mesh device */
772         if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
773                 lbs_pr_debug(1, "Init ethernet device failed!\n");
774                 return NULL;
775         }
776
777         /* Both intervaces share the priv structure */
778         mesh_dev->priv = priv;
779
780         /* init wlan_adapter */
781         memset(priv->adapter, 0, sizeof(wlan_adapter));
782
783         priv->wlan_dev.netdev = dev;
784         priv->wlan_dev.card = card;
785         priv->mesh_open = 0;
786         priv->infra_open = 0;
787         priv->mesh_dev = mesh_dev;
788
789         SET_MODULE_OWNER(dev);
790         SET_MODULE_OWNER(mesh_dev);
791
792         /* Setup the OS Interface to our functions */
793         dev->open = wlan_open;
794         dev->hard_start_xmit = wlan_pre_start_xmit;
795         dev->stop = wlan_close;
796         dev->do_ioctl = libertas_do_ioctl;
797         dev->set_mac_address = wlan_set_mac_address;
798         mesh_dev->open = mesh_open;
799         mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
800         mesh_dev->stop = mesh_close;
801         mesh_dev->do_ioctl = libertas_do_ioctl;
802         memcpy(mesh_dev->dev_addr, priv->wlan_dev.netdev->dev_addr,
803                         sizeof(priv->wlan_dev.netdev->dev_addr));
804
805 #define WLAN_WATCHDOG_TIMEOUT   (5 * HZ)
806
807         dev->tx_timeout = wlan_tx_timeout;
808         dev->get_stats = wlan_get_stats;
809         dev->watchdog_timeo = WLAN_WATCHDOG_TIMEOUT;
810         dev->ethtool_ops = &libertas_ethtool_ops;
811         mesh_dev->get_stats = wlan_get_stats;
812         mesh_dev->ethtool_ops = &libertas_ethtool_ops;
813
814 #ifdef  WIRELESS_EXT
815         dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
816         mesh_dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
817 #endif
818 #define NETIF_F_DYNALLOC 16
819         dev->features |= NETIF_F_DYNALLOC;
820         dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
821         dev->set_multicast_list = wlan_set_multicast_list;
822
823         INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
824         INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
825
826         spin_lock_init(&priv->adapter->driver_lock);
827         init_waitqueue_head(&priv->adapter->cmd_pending);
828         priv->adapter->nr_cmd_pending = 0;
829
830         lbs_pr_debug(1, "Starting kthread...\n");
831         priv->mainthread.priv = priv;
832         wlan_create_thread(wlan_service_main_thread,
833                            &priv->mainthread, "wlan_main_service");
834
835         priv->assoc_thread =
836                 create_singlethread_workqueue("libertas_assoc");
837         INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
838
839         /*
840          * Register the device. Fillup the private data structure with
841          * relevant information from the card and request for the required
842          * IRQ.
843          */
844         if (libertas_sbi_register_dev(priv) < 0) {
845                 lbs_pr_info("failed to register wlan device!\n");
846                 goto err_registerdev;
847         }
848
849         /* init FW and HW */
850         if (libertas_init_fw(priv)) {
851                 lbs_pr_debug(1, "Firmware Init failed\n");
852                 goto err_registerdev;
853         }
854
855         if (register_netdev(dev)) {
856                 lbs_pr_err("Cannot register network device!\n");
857                 goto err_init_fw;
858         }
859
860         /* Register virtual mesh interface */
861         if (register_netdev(mesh_dev)) {
862                 lbs_pr_info("Cannot register mesh virtual interface!\n");
863                 goto err_init_fw;
864         }
865
866         lbs_pr_info("%s: Marvell Wlan 802.11 adapter\n", dev->name);
867
868         libertas_debugfs_init_one(priv, dev);
869
870         if (libertas_found == MAX_DEVS)
871                 goto err_init_fw;
872         libertas_devs[libertas_found] = dev;
873         libertas_found++;
874         if (device_create_file(&(mesh_dev->dev), &dev_attr_libertas_mpp))
875                 goto err_create_file;
876
877         LEAVE();
878         return priv;
879
880 err_create_file:
881         device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
882 err_init_fw:
883         libertas_sbi_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         kfree(priv->adapter);
890 err_kmalloc:
891         free_netdev(dev);
892         free_netdev(mesh_dev);
893
894         LEAVE();
895         return NULL;
896 }
897
898 static void wake_pending_cmdnodes(wlan_private *priv)
899 {
900         struct cmd_ctrl_node *cmdnode;
901         unsigned long flags;
902
903         spin_lock_irqsave(&priv->adapter->driver_lock, flags);
904         list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
905                 cmdnode->cmdwaitqwoken = 1;
906                 wake_up_interruptible(&cmdnode->cmdwait_q);
907         }
908         spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
909 }
910
911
912 int wlan_remove_card(void *card)
913 {
914         wlan_private *priv = libertas_sbi_get_priv(card);
915         wlan_adapter *adapter;
916         struct net_device *dev;
917         struct net_device *mesh_dev;
918         union iwreq_data wrqu;
919         int i;
920
921         ENTER();
922
923         if (!priv) {
924                 LEAVE();
925                 return 0;
926         }
927
928         adapter = priv->adapter;
929
930         if (!adapter) {
931                 LEAVE();
932                 return 0;
933         }
934
935         dev = priv->wlan_dev.netdev;
936         mesh_dev = priv->mesh_dev;
937
938         netif_stop_queue(mesh_dev);
939         netif_stop_queue(priv->wlan_dev.netdev);
940         netif_carrier_off(priv->wlan_dev.netdev);
941
942         wake_pending_cmdnodes(priv);
943
944         device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
945         unregister_netdev(mesh_dev);
946         unregister_netdev(dev);
947
948         cancel_delayed_work(&priv->assoc_work);
949         destroy_workqueue(priv->assoc_thread);
950
951         if (adapter->psmode == wlan802_11powermodemax_psp) {
952                 adapter->psmode = wlan802_11powermodecam;
953                 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
954         }
955
956         memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
957         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
958         wireless_send_event(priv->wlan_dev.netdev, SIOCGIWAP, &wrqu, NULL);
959
960         adapter->surpriseremoved = 1;
961
962         /* Stop the thread servicing the interrupts */
963         wlan_terminate_thread(&priv->mainthread);
964
965         libertas_debugfs_remove_one(priv);
966
967         lbs_pr_debug(1, "Free adapter\n");
968         libertas_free_adapter(priv);
969
970         for (i = 0; i<libertas_found; i++) {
971                 if (libertas_devs[i]==priv->wlan_dev.netdev) {
972                         libertas_devs[i] = libertas_devs[--libertas_found];
973                         libertas_devs[libertas_found] = NULL ;
974                         break ;
975                 }
976         }
977
978         lbs_pr_debug(1, "Unregister finish\n");
979
980         priv->wlan_dev.netdev = NULL;
981         priv->mesh_dev = NULL ;
982         free_netdev(mesh_dev);
983         free_netdev(dev);
984
985         LEAVE();
986         return 0;
987 }
988
989 /**
990  *  @brief This function finds the CFP in
991  *  region_cfp_table based on region and band parameter.
992  *
993  *  @param region  The region code
994  *  @param band    The band
995  *  @param cfp_no  A pointer to CFP number
996  *  @return        A pointer to CFP
997  */
998 struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
999 {
1000         int i, end;
1001
1002         ENTER();
1003
1004         end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
1005
1006         for (i = 0; i < end ; i++) {
1007                 lbs_pr_debug(1, "region_cfp_table[i].region=%d\n",
1008                         region_cfp_table[i].region);
1009                 if (region_cfp_table[i].region == region) {
1010                         *cfp_no = region_cfp_table[i].cfp_no_BG;
1011                         LEAVE();
1012                         return region_cfp_table[i].cfp_BG;
1013                 }
1014         }
1015
1016         LEAVE();
1017         return NULL;
1018 }
1019
1020 int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
1021 {
1022         wlan_adapter *adapter = priv->adapter;
1023         int i = 0;
1024
1025         struct chan_freq_power *cfp;
1026         int cfp_no;
1027
1028         ENTER();
1029
1030         memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
1031
1032         {
1033                 cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
1034                 if (cfp != NULL) {
1035                         adapter->region_channel[i].nrcfp = cfp_no;
1036                         adapter->region_channel[i].CFP = cfp;
1037                 } else {
1038                         lbs_pr_debug(1, "wrong region code %#x in band B-G\n",
1039                                region);
1040                         return -1;
1041                 }
1042                 adapter->region_channel[i].valid = 1;
1043                 adapter->region_channel[i].region = region;
1044                 adapter->region_channel[i].band = band;
1045                 i++;
1046         }
1047         LEAVE();
1048         return 0;
1049 }
1050
1051 /**
1052  *  @brief This function handles the interrupt. it will change PS
1053  *  state if applicable. it will wake up main_thread to handle
1054  *  the interrupt event as well.
1055  *
1056  *  @param dev     A pointer to net_device structure
1057  *  @return        n/a
1058  */
1059 void libertas_interrupt(struct net_device *dev)
1060 {
1061         wlan_private *priv = dev->priv;
1062
1063         ENTER();
1064
1065         lbs_pr_debug(1, "libertas_interrupt: intcounter=%d\n",
1066                priv->adapter->intcounter);
1067
1068         priv->adapter->intcounter++;
1069
1070         if (priv->adapter->psstate == PS_STATE_SLEEP) {
1071                 priv->adapter->psstate = PS_STATE_AWAKE;
1072                 netif_wake_queue(dev);
1073         }
1074
1075         wake_up_interruptible(&priv->mainthread.waitq);
1076
1077         LEAVE();
1078 }
1079
1080 static int wlan_init_module(void)
1081 {
1082         int ret = 0;
1083
1084         ENTER();
1085
1086         if (libertas_fw_name == NULL) {
1087                 libertas_fw_name = default_fw_name;
1088         }
1089
1090         libertas_debugfs_init();
1091
1092         if (libertas_sbi_register()) {
1093                 ret = -1;
1094                 libertas_debugfs_remove();
1095                 goto done;
1096         }
1097
1098 done:
1099         LEAVE();
1100         return ret;
1101 }
1102
1103 static void wlan_cleanup_module(void)
1104 {
1105         int i;
1106
1107         ENTER();
1108
1109         for (i = 0; i<libertas_found; i++) {
1110                 wlan_private *priv = libertas_devs[i]->priv;
1111                 reset_device(priv);
1112         }
1113
1114         libertas_sbi_unregister();
1115         libertas_debugfs_remove();
1116
1117         LEAVE();
1118 }
1119
1120 module_init(wlan_init_module);
1121 module_exit(wlan_cleanup_module);
1122
1123 MODULE_DESCRIPTION("M-WLAN Driver");
1124 MODULE_AUTHOR("Marvell International Ltd.");
1125 MODULE_LICENSE("GPL");