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