Staging: brcm80211: s/ulong/unsigned long/
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmfmac / dhd_linux.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #ifdef CONFIG_WIFI_CONTROL_FUNC
18 #include <linux/platform_device.h>
19 #endif
20 #include <typedefs.h>
21 #include <linuxver.h>
22 #include <osl.h>
23
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/skbuff.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/random.h>
31 #include <linux/spinlock.h>
32 #include <linux/ethtool.h>
33 #include <linux/fcntl.h>
34 #include <linux/fs.h>
35
36 #include <asm/uaccess.h>
37 #include <asm/unaligned.h>
38 #include <bcmutils.h>
39 #include <bcmendian.h>
40
41 #include <proto/ethernet.h>
42 #include <dngl_stats.h>
43 #include <dhd.h>
44 #include <dhd_bus.h>
45 #include <dhd_proto.h>
46 #include <dhd_dbg.h>
47
48 #ifdef CONFIG_CFG80211
49 #include <wl_cfg80211.h>
50 #endif
51
52 #define EPI_VERSION_STR         "4.218.248.5"
53
54 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
55 #include <linux/wifi_tiwlan.h>
56
57 struct semaphore wifi_control_sem;
58
59 struct dhd_bus *g_bus;
60
61 static struct wifi_platform_data *wifi_control_data = NULL;
62 static struct resource *wifi_irqres = NULL;
63
64 int wifi_get_irq_number(unsigned long *irq_flags_ptr)
65 {
66         if (wifi_irqres) {
67                 *irq_flags_ptr = wifi_irqres->flags & IRQF_TRIGGER_MASK;
68                 return (int)wifi_irqres->start;
69         }
70 #ifdef CUSTOM_OOB_GPIO_NUM
71         return CUSTOM_OOB_GPIO_NUM;
72 #else
73         return -1;
74 #endif
75 }
76
77 int wifi_set_carddetect(int on)
78 {
79         printk(KERN_ERR "%s = %d\n", __func__, on);
80         if (wifi_control_data && wifi_control_data->set_carddetect)
81                 wifi_control_data->set_carddetect(on);
82         return 0;
83 }
84
85 int wifi_set_power(int on, unsigned long msec)
86 {
87         printk(KERN_ERR "%s = %d\n", __func__, on);
88         if (wifi_control_data && wifi_control_data->set_power)
89                 wifi_control_data->set_power(on);
90         if (msec)
91                 mdelay(msec);
92         return 0;
93 }
94
95 int wifi_set_reset(int on, unsigned long msec)
96 {
97         printk(KERN_ERR "%s = %d\n", __func__, on);
98         if (wifi_control_data && wifi_control_data->set_reset)
99                 wifi_control_data->set_reset(on);
100         if (msec)
101                 mdelay(msec);
102         return 0;
103 }
104
105 static int wifi_probe(struct platform_device *pdev)
106 {
107         struct wifi_platform_data *wifi_ctrl =
108             (struct wifi_platform_data *)(pdev->dev.platform_data);
109
110         printk(KERN_ERR "## %s\n", __func__);
111         wifi_irqres =
112             platform_get_resource_byname(pdev, IORESOURCE_IRQ,
113                                          "bcm4329_wlan_irq");
114         wifi_control_data = wifi_ctrl;
115
116         wifi_set_power(1, 0);   /* Power On */
117         wifi_set_carddetect(1); /* CardDetect (0->1) */
118
119         up(&wifi_control_sem);
120         return 0;
121 }
122
123 static int wifi_remove(struct platform_device *pdev)
124 {
125         struct wifi_platform_data *wifi_ctrl =
126             (struct wifi_platform_data *)(pdev->dev.platform_data);
127
128         printk(KERN_ERR "## %s\n", __func__);
129         wifi_control_data = wifi_ctrl;
130
131         wifi_set_carddetect(0); /* CardDetect (1->0) */
132         wifi_set_power(0, 0);   /* Power Off */
133
134         up(&wifi_control_sem);
135         return 0;
136 }
137
138 static int wifi_suspend(struct platform_device *pdev, pm_message_t state)
139 {
140         DHD_TRACE(("##> %s\n", __func__));
141         return 0;
142 }
143
144 static int wifi_resume(struct platform_device *pdev)
145 {
146         DHD_TRACE(("##> %s\n", __func__));
147         return 0;
148 }
149
150 static struct platform_driver wifi_device = {
151         .probe = wifi_probe,
152         .remove = wifi_remove,
153         .suspend = wifi_suspend,
154         .resume = wifi_resume,
155         .driver = {
156                    .name = "bcm4329_wlan",
157                    }
158 };
159
160 int wifi_add_dev(void)
161 {
162         DHD_TRACE(("## Calling platform_driver_register\n"));
163         return platform_driver_register(&wifi_device);
164 }
165
166 void wifi_del_dev(void)
167 {
168         DHD_TRACE(("## Unregister platform_driver_register\n"));
169         platform_driver_unregister(&wifi_device);
170 }
171 #endif  /* defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC) */
172
173 #if defined(CONFIG_PM_SLEEP)
174 #include <linux/suspend.h>
175 volatile bool dhd_mmc_suspend = FALSE;
176 DECLARE_WAIT_QUEUE_HEAD(dhd_dpc_wait);
177 #endif  /*  defined(CONFIG_PM_SLEEP) */
178
179 #if defined(OOB_INTR_ONLY)
180 extern void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable);
181 #endif  /* defined(OOB_INTR_ONLY) */
182
183 MODULE_AUTHOR("Broadcom Corporation");
184 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
185 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
186 MODULE_LICENSE("Dual BSD/GPL");
187
188 /* Linux wireless extension support */
189 #if defined(CONFIG_WIRELESS_EXT)
190 #include <wl_iw.h>
191 extern wl_iw_extra_params_t g_wl_iw_params;
192 #endif          /* defined(CONFIG_WIRELESS_EXT) */
193
194 #if defined(CONFIG_HAS_EARLYSUSPEND)
195 #include <linux/earlysuspend.h>
196 extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
197                             uint len);
198 #endif          /* defined(CONFIG_HAS_EARLYSUSPEND) */
199
200 #ifdef PKT_FILTER_SUPPORT
201 extern void dhd_pktfilter_offload_set(dhd_pub_t *dhd, char *arg);
202 extern void dhd_pktfilter_offload_enable(dhd_pub_t *dhd, char *arg, int enable,
203                                          int master_mode);
204 #endif
205
206 /* Interface control information */
207 typedef struct dhd_if {
208         struct dhd_info *info;  /* back pointer to dhd_info */
209         /* OS/stack specifics */
210         struct net_device *net;
211         struct net_device_stats stats;
212         int idx;                /* iface idx in dongle */
213         int state;              /* interface state */
214         uint subunit;           /* subunit */
215         u8 mac_addr[ETHER_ADDR_LEN];    /* assigned MAC address */
216         bool attached;          /* Delayed attachment when unset */
217         bool txflowcontrol;     /* Per interface flow control indicator */
218         char name[IFNAMSIZ + 1];        /* linux interface name */
219 } dhd_if_t;
220
221 /* Local private structure (extension of pub) */
222 typedef struct dhd_info {
223 #if defined(CONFIG_WIRELESS_EXT)
224         wl_iw_t iw;             /* wireless extensions state (must be first) */
225 #endif                          /* defined(CONFIG_WIRELESS_EXT) */
226
227         dhd_pub_t pub;
228
229         /* OS/stack specifics */
230         dhd_if_t *iflist[DHD_MAX_IFS];
231
232         struct semaphore proto_sem;
233         wait_queue_head_t ioctl_resp_wait;
234         struct timer_list timer;
235         bool wd_timer_valid;
236         struct tasklet_struct tasklet;
237         spinlock_t sdlock;
238         spinlock_t txqlock;
239         /* Thread based operation */
240         bool threads_only;
241         struct semaphore sdsem;
242         long watchdog_pid;
243         struct semaphore watchdog_sem;
244         struct completion watchdog_exited;
245         long dpc_pid;
246         struct semaphore dpc_sem;
247         struct completion dpc_exited;
248
249         /* Thread to issue ioctl for multicast */
250         long sysioc_pid;
251         struct semaphore sysioc_sem;
252         struct completion sysioc_exited;
253         bool set_multicast;
254         bool set_macaddress;
255         struct ether_addr macvalue;
256         wait_queue_head_t ctrl_wait;
257         atomic_t pend_8021x_cnt;
258
259 #ifdef CONFIG_HAS_EARLYSUSPEND
260         struct early_suspend early_suspend;
261 #endif                          /* CONFIG_HAS_EARLYSUSPEND */
262 } dhd_info_t;
263
264 /* Definitions to provide path to the firmware and nvram
265  * example nvram_path[MOD_PARAM_PATHLEN]="/projects/wlan/nvram.txt"
266  */
267 char firmware_path[MOD_PARAM_PATHLEN];
268 char nvram_path[MOD_PARAM_PATHLEN];
269
270 /* load firmware and/or nvram values from the filesystem */
271 module_param_string(firmware_path, firmware_path, MOD_PARAM_PATHLEN, 0);
272 module_param_string(nvram_path, nvram_path, MOD_PARAM_PATHLEN, 0);
273
274 /* Error bits */
275 module_param(dhd_msg_level, int, 0);
276
277 /* Spawn a thread for system ioctls (set mac, set mcast) */
278 uint dhd_sysioc = TRUE;
279 module_param(dhd_sysioc, uint, 0);
280
281 /* Watchdog interval */
282 uint dhd_watchdog_ms = 10;
283 module_param(dhd_watchdog_ms, uint, 0);
284
285 #ifdef DHD_DEBUG
286 /* Console poll interval */
287 uint dhd_console_ms = 0;
288 module_param(dhd_console_ms, uint, 0);
289 #endif                          /* DHD_DEBUG */
290
291 /* ARP offload agent mode : Enable ARP Host Auto-Reply
292 and ARP Peer Auto-Reply */
293 uint dhd_arp_mode = 0xb;
294 module_param(dhd_arp_mode, uint, 0);
295
296 /* ARP offload enable */
297 uint dhd_arp_enable = TRUE;
298 module_param(dhd_arp_enable, uint, 0);
299
300 /* Global Pkt filter enable control */
301 uint dhd_pkt_filter_enable = TRUE;
302 module_param(dhd_pkt_filter_enable, uint, 0);
303
304 /*  Pkt filter init setup */
305 uint dhd_pkt_filter_init = 0;
306 module_param(dhd_pkt_filter_init, uint, 0);
307
308 /* Pkt filter mode control */
309 uint dhd_master_mode = TRUE;
310 module_param(dhd_master_mode, uint, 1);
311
312 /* Watchdog thread priority, -1 to use kernel timer */
313 int dhd_watchdog_prio = 97;
314 module_param(dhd_watchdog_prio, int, 0);
315
316 /* DPC thread priority, -1 to use tasklet */
317 int dhd_dpc_prio = 98;
318 module_param(dhd_dpc_prio, int, 0);
319
320 /* DPC thread priority, -1 to use tasklet */
321 extern int dhd_dongle_memsize;
322 module_param(dhd_dongle_memsize, int, 0);
323
324 /* Contorl fw roaming */
325 #ifdef CUSTOMER_HW2
326 uint dhd_roam = 0;
327 #else
328 uint dhd_roam = 1;
329 #endif
330
331 /* Control radio state */
332 uint dhd_radio_up = 1;
333
334 /* Network inteface name */
335 char iface_name[IFNAMSIZ];
336 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
337
338 #define DAEMONIZE(a) daemonize(a); \
339         allow_signal(SIGKILL); \
340         allow_signal(SIGTERM);
341
342 #define BLOCKABLE()     (!in_atomic())
343
344 /* The following are specific to the SDIO dongle */
345
346 /* IOCTL response timeout */
347 int dhd_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
348
349 /* Idle timeout for backplane clock */
350 int dhd_idletime = DHD_IDLETIME_TICKS;
351 module_param(dhd_idletime, int, 0);
352
353 /* Use polling */
354 uint dhd_poll = FALSE;
355 module_param(dhd_poll, uint, 0);
356
357 #ifdef CONFIG_CFG80211
358 /* Use cfg80211 */
359 uint dhd_cfg80211 = TRUE;
360 module_param(dhd_cfg80211, uint, 0);
361 #endif
362
363 /* Use interrupts */
364 uint dhd_intr = TRUE;
365 module_param(dhd_intr, uint, 0);
366
367 /* SDIO Drive Strength (in milliamps) */
368 uint dhd_sdiod_drive_strength = 6;
369 module_param(dhd_sdiod_drive_strength, uint, 0);
370
371 /* Tx/Rx bounds */
372 extern uint dhd_txbound;
373 extern uint dhd_rxbound;
374 module_param(dhd_txbound, uint, 0);
375 module_param(dhd_rxbound, uint, 0);
376
377 /* Deferred transmits */
378 extern uint dhd_deferred_tx;
379 module_param(dhd_deferred_tx, uint, 0);
380
381 #ifdef SDTEST
382 /* Echo packet generator (pkts/s) */
383 uint dhd_pktgen = 0;
384 module_param(dhd_pktgen, uint, 0);
385
386 /* Echo packet len (0 => sawtooth, max 2040) */
387 uint dhd_pktgen_len = 0;
388 module_param(dhd_pktgen_len, uint, 0);
389 #endif
390
391 #ifdef CONFIG_CFG80211
392 #define FAVORITE_WIFI_CP        (!!dhd_cfg80211)
393 #define IS_CFG80211_FAVORITE() FAVORITE_WIFI_CP
394 #define DBG_CFG80211_GET() ((dhd_cfg80211 & WL_DBG_MASK) >> 1)
395 #define NO_FW_REQ() (dhd_cfg80211 & 0x80)
396 #endif
397
398 /* Version string to report */
399 #ifdef DHD_DEBUG
400 #define DHD_COMPILED "\nCompiled in " SRCBASE
401 #else
402 #define DHD_COMPILED
403 #endif
404
405 static char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR
406 #ifdef DHD_DEBUG
407 "\nCompiled in " " on " __DATE__ " at " __TIME__
408 #endif
409 ;
410
411 #if defined(CONFIG_WIRELESS_EXT)
412 struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev);
413 #endif                          /* defined(CONFIG_WIRELESS_EXT) */
414
415 static void dhd_dpc(unsigned long data);
416 /* forward decl */
417 extern int dhd_wait_pend8021x(struct net_device *dev);
418
419 #ifdef TOE
420 #ifndef BDC
421 #error TOE requires BDC
422 #endif                          /* !BDC */
423 static int dhd_toe_get(dhd_info_t *dhd, int idx, uint32 *toe_ol);
424 static int dhd_toe_set(dhd_info_t *dhd, int idx, uint32 toe_ol);
425 #endif                          /* TOE */
426
427 static int dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
428                              wl_event_msg_t *event_ptr, void **data_ptr);
429
430 #if defined(CONFIG_PM_SLEEP)
431 static int dhd_sleep_pm_callback(struct notifier_block *nfb,
432                                  unsigned long action, void *ignored)
433 {
434         switch (action) {
435         case PM_HIBERNATION_PREPARE:
436         case PM_SUSPEND_PREPARE:
437                 dhd_mmc_suspend = TRUE;
438                 return NOTIFY_OK;
439         case PM_POST_HIBERNATION:
440         case PM_POST_SUSPEND:
441                 dhd_mmc_suspend = FALSE;
442                 return NOTIFY_OK;
443         }
444         return 0;
445 }
446
447 static struct notifier_block dhd_sleep_pm_notifier = {
448         .notifier_call = dhd_sleep_pm_callback,
449         .priority = 0
450 };
451
452 extern int register_pm_notifier(struct notifier_block *nb);
453 extern int unregister_pm_notifier(struct notifier_block *nb);
454 #endif  /* defined(CONFIG_PM_SLEEP) */
455         /* && defined(DHD_GPL) */
456 static void dhd_set_packet_filter(int value, dhd_pub_t *dhd)
457 {
458 #ifdef PKT_FILTER_SUPPORT
459         DHD_TRACE(("%s: %d\n", __func__, value));
460         /* 1 - Enable packet filter, only allow unicast packet to send up */
461         /* 0 - Disable packet filter */
462         if (dhd_pkt_filter_enable) {
463                 int i;
464
465                 for (i = 0; i < dhd->pktfilter_count; i++) {
466                         dhd_pktfilter_offload_set(dhd, dhd->pktfilter[i]);
467                         dhd_pktfilter_offload_enable(dhd, dhd->pktfilter[i],
468                                                      value, dhd_master_mode);
469                 }
470         }
471 #endif
472 }
473
474 #if defined(CONFIG_HAS_EARLYSUSPEND)
475 static int dhd_set_suspend(int value, dhd_pub_t *dhd)
476 {
477         int power_mode = PM_MAX;
478         /* wl_pkt_filter_enable_t       enable_parm; */
479         char iovbuf[32];
480         int bcn_li_dtim = 3;
481 #ifdef CUSTOMER_HW2
482         uint roamvar = 1;
483 #endif                          /* CUSTOMER_HW2 */
484
485         DHD_TRACE(("%s: enter, value = %d in_suspend=%d\n",
486                    __func__, value, dhd->in_suspend));
487
488         if (dhd && dhd->up) {
489                 if (value && dhd->in_suspend) {
490
491                         /* Kernel suspended */
492                         DHD_TRACE(("%s: force extra Suspend setting\n",
493                                    __func__));
494
495                         dhdcdc_set_ioctl(dhd, 0, WLC_SET_PM,
496                                          (char *)&power_mode,
497                                          sizeof(power_mode));
498
499                         /* Enable packet filter, only allow unicast
500                                  packet to send up */
501                         dhd_set_packet_filter(1, dhd);
502
503                         /* if dtim skip setup as default force it
504                          * to wake each thrid dtim
505                          * for better power saving.
506                          * Note that side effect is chance to miss BC/MC
507                          * packet
508                          */
509                         if ((dhd->dtim_skip == 0) || (dhd->dtim_skip == 1))
510                                 bcn_li_dtim = 3;
511                         else
512                                 bcn_li_dtim = dhd->dtim_skip;
513                         bcm_mkiovar("bcn_li_dtim", (char *)&bcn_li_dtim,
514                                     4, iovbuf, sizeof(iovbuf));
515                         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
516                                          sizeof(iovbuf));
517 #ifdef CUSTOMER_HW2
518                         /* Disable build-in roaming to allowed \
519                          * supplicant to take of romaing
520                          */
521                         bcm_mkiovar("roam_off", (char *)&roamvar, 4,
522                                     iovbuf, sizeof(iovbuf));
523                         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
524                                          sizeof(iovbuf));
525 #endif                          /* CUSTOMER_HW2 */
526                 } else {
527
528                         /* Kernel resumed  */
529                         DHD_TRACE(("%s: Remove extra suspend setting\n",
530                                    __func__));
531
532                         power_mode = PM_FAST;
533                         dhdcdc_set_ioctl(dhd, 0, WLC_SET_PM,
534                                          (char *)&power_mode,
535                                          sizeof(power_mode));
536
537                         /* disable pkt filter */
538                         dhd_set_packet_filter(0, dhd);
539
540                         /* restore pre-suspend setting for dtim_skip */
541                         bcm_mkiovar("bcn_li_dtim", (char *)&dhd->dtim_skip,
542                                     4, iovbuf, sizeof(iovbuf));
543
544                         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
545                                          sizeof(iovbuf));
546 #ifdef CUSTOMER_HW2
547                         roamvar = 0;
548                         bcm_mkiovar("roam_off", (char *)&roamvar, 4, iovbuf,
549                                     sizeof(iovbuf));
550                         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
551                                          sizeof(iovbuf));
552 #endif                          /* CUSTOMER_HW2 */
553                 }
554         }
555
556         return 0;
557 }
558
559 static void dhd_suspend_resume_helper(struct dhd_info *dhd, int val)
560 {
561         dhd_pub_t *dhdp = &dhd->pub;
562
563         dhd_os_proto_block(dhdp);
564         /* Set flag when early suspend was called */
565         dhdp->in_suspend = val;
566         if (!dhdp->suspend_disable_flag)
567                 dhd_set_suspend(val, dhdp);
568         dhd_os_proto_unblock(dhdp);
569 }
570
571 static void dhd_early_suspend(struct early_suspend *h)
572 {
573         struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
574
575         DHD_TRACE(("%s: enter\n", __func__));
576
577         if (dhd)
578                 dhd_suspend_resume_helper(dhd, 1);
579
580 }
581
582 static void dhd_late_resume(struct early_suspend *h)
583 {
584         struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
585
586         DHD_TRACE(("%s: enter\n", __func__));
587
588         if (dhd)
589                 dhd_suspend_resume_helper(dhd, 0);
590 }
591 #endif                          /* defined(CONFIG_HAS_EARLYSUSPEND) */
592
593 /*
594  * Generalized timeout mechanism.  Uses spin sleep with exponential
595  * back-off until
596  * the sleep time reaches one jiffy, then switches over to task delay.  Usage:
597  *
598  *      dhd_timeout_start(&tmo, usec);
599  *      while (!dhd_timeout_expired(&tmo))
600  *              if (poll_something())
601  *                      break;
602  *      if (dhd_timeout_expired(&tmo))
603  *              fatal();
604  */
605
606 void dhd_timeout_start(dhd_timeout_t *tmo, uint usec)
607 {
608         tmo->limit = usec;
609         tmo->increment = 0;
610         tmo->elapsed = 0;
611         tmo->tick = 1000000 / HZ;
612 }
613
614 int dhd_timeout_expired(dhd_timeout_t *tmo)
615 {
616         /* Does nothing the first call */
617         if (tmo->increment == 0) {
618                 tmo->increment = 1;
619                 return 0;
620         }
621
622         if (tmo->elapsed >= tmo->limit)
623                 return 1;
624
625         /* Add the delay that's about to take place */
626         tmo->elapsed += tmo->increment;
627
628         if (tmo->increment < tmo->tick) {
629                 OSL_DELAY(tmo->increment);
630                 tmo->increment *= 2;
631                 if (tmo->increment > tmo->tick)
632                         tmo->increment = tmo->tick;
633         } else {
634                 wait_queue_head_t delay_wait;
635                 DECLARE_WAITQUEUE(wait, current);
636                 int pending;
637                 init_waitqueue_head(&delay_wait);
638                 add_wait_queue(&delay_wait, &wait);
639                 set_current_state(TASK_INTERRUPTIBLE);
640                 schedule_timeout(1);
641                 pending = signal_pending(current);
642                 remove_wait_queue(&delay_wait, &wait);
643                 set_current_state(TASK_RUNNING);
644                 if (pending)
645                         return 1;       /* Interrupted */
646         }
647
648         return 0;
649 }
650
651 static int dhd_net2idx(dhd_info_t *dhd, struct net_device *net)
652 {
653         int i = 0;
654
655         ASSERT(dhd);
656         while (i < DHD_MAX_IFS) {
657                 if (dhd->iflist[i] && (dhd->iflist[i]->net == net))
658                         return i;
659                 i++;
660         }
661
662         return DHD_BAD_IF;
663 }
664
665 int dhd_ifname2idx(dhd_info_t *dhd, char *name)
666 {
667         int i = DHD_MAX_IFS;
668
669         ASSERT(dhd);
670
671         if (name == NULL || *name == '\0')
672                 return 0;
673
674         while (--i > 0)
675                 if (dhd->iflist[i]
676                     && !strncmp(dhd->iflist[i]->name, name, IFNAMSIZ))
677                         break;
678
679         DHD_TRACE(("%s: return idx %d for \"%s\"\n", __func__, i, name));
680
681         return i;               /* default - the primary interface */
682 }
683
684 char *dhd_ifname(dhd_pub_t *dhdp, int ifidx)
685 {
686         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
687
688         ASSERT(dhd);
689
690         if (ifidx < 0 || ifidx >= DHD_MAX_IFS) {
691                 DHD_ERROR(("%s: ifidx %d out of range\n", __func__, ifidx));
692                 return "<if_bad>";
693         }
694
695         if (dhd->iflist[ifidx] == NULL) {
696                 DHD_ERROR(("%s: null i/f %d\n", __func__, ifidx));
697                 return "<if_null>";
698         }
699
700         if (dhd->iflist[ifidx]->net)
701                 return dhd->iflist[ifidx]->net->name;
702
703         return "<if_none>";
704 }
705
706 static void _dhd_set_multicast_list(dhd_info_t *dhd, int ifidx)
707 {
708         struct net_device *dev;
709         struct netdev_hw_addr *ha;
710         uint32 allmulti, cnt;
711
712         wl_ioctl_t ioc;
713         char *buf, *bufp;
714         uint buflen;
715         int ret;
716
717         ASSERT(dhd && dhd->iflist[ifidx]);
718         dev = dhd->iflist[ifidx]->net;
719         cnt = netdev_mc_count(dev);
720
721         /* Determine initial value of allmulti flag */
722         allmulti = (dev->flags & IFF_ALLMULTI) ? TRUE : FALSE;
723
724         /* Send down the multicast list first. */
725
726         buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETHER_ADDR_LEN);
727         bufp = buf = MALLOC(dhd->pub.osh, buflen);
728         if (!bufp) {
729                 DHD_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
730                            dhd_ifname(&dhd->pub, ifidx), cnt));
731                 return;
732         }
733
734         strcpy(bufp, "mcast_list");
735         bufp += strlen("mcast_list") + 1;
736
737         cnt = htol32(cnt);
738         memcpy(bufp, &cnt, sizeof(cnt));
739         bufp += sizeof(cnt);
740
741         netdev_for_each_mc_addr(ha, dev) {
742                 if (!cnt)
743                         break;
744                 memcpy(bufp, ha->addr, ETHER_ADDR_LEN);
745                 bufp += ETHER_ADDR_LEN;
746                 cnt--;
747         }
748
749         memset(&ioc, 0, sizeof(ioc));
750         ioc.cmd = WLC_SET_VAR;
751         ioc.buf = buf;
752         ioc.len = buflen;
753         ioc.set = TRUE;
754
755         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
756         if (ret < 0) {
757                 DHD_ERROR(("%s: set mcast_list failed, cnt %d\n",
758                            dhd_ifname(&dhd->pub, ifidx), cnt));
759                 allmulti = cnt ? TRUE : allmulti;
760         }
761
762         MFREE(dhd->pub.osh, buf, buflen);
763
764         /* Now send the allmulti setting.  This is based on the setting in the
765          * net_device flags, but might be modified above to be turned on if we
766          * were trying to set some addresses and dongle rejected it...
767          */
768
769         buflen = sizeof("allmulti") + sizeof(allmulti);
770         buf = MALLOC(dhd->pub.osh, buflen);
771         if (!buf) {
772                 DHD_ERROR(("%s: out of memory for allmulti\n",
773                            dhd_ifname(&dhd->pub, ifidx)));
774                 return;
775         }
776         allmulti = htol32(allmulti);
777
778         if (!bcm_mkiovar
779             ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
780                 DHD_ERROR(("%s: mkiovar failed for allmulti, datalen %d "
781                         "buflen %u\n", dhd_ifname(&dhd->pub, ifidx),
782                         (int)sizeof(allmulti), buflen));
783                 MFREE(dhd->pub.osh, buf, buflen);
784                 return;
785         }
786
787         memset(&ioc, 0, sizeof(ioc));
788         ioc.cmd = WLC_SET_VAR;
789         ioc.buf = buf;
790         ioc.len = buflen;
791         ioc.set = TRUE;
792
793         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
794         if (ret < 0) {
795                 DHD_ERROR(("%s: set allmulti %d failed\n",
796                            dhd_ifname(&dhd->pub, ifidx), ltoh32(allmulti)));
797         }
798
799         MFREE(dhd->pub.osh, buf, buflen);
800
801         /* Finally, pick up the PROMISC flag as well, like the NIC
802                  driver does */
803
804         allmulti = (dev->flags & IFF_PROMISC) ? TRUE : FALSE;
805         allmulti = htol32(allmulti);
806
807         memset(&ioc, 0, sizeof(ioc));
808         ioc.cmd = WLC_SET_PROMISC;
809         ioc.buf = &allmulti;
810         ioc.len = sizeof(allmulti);
811         ioc.set = TRUE;
812
813         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
814         if (ret < 0) {
815                 DHD_ERROR(("%s: set promisc %d failed\n",
816                            dhd_ifname(&dhd->pub, ifidx), ltoh32(allmulti)));
817         }
818 }
819
820 static int
821 _dhd_set_mac_address(dhd_info_t *dhd, int ifidx, struct ether_addr *addr)
822 {
823         char buf[32];
824         wl_ioctl_t ioc;
825         int ret;
826
827         DHD_TRACE(("%s enter\n", __func__));
828         if (!bcm_mkiovar
829             ("cur_etheraddr", (char *)addr, ETHER_ADDR_LEN, buf, 32)) {
830                 DHD_ERROR(("%s: mkiovar failed for cur_etheraddr\n",
831                            dhd_ifname(&dhd->pub, ifidx)));
832                 return -1;
833         }
834         memset(&ioc, 0, sizeof(ioc));
835         ioc.cmd = WLC_SET_VAR;
836         ioc.buf = buf;
837         ioc.len = 32;
838         ioc.set = TRUE;
839
840         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
841         if (ret < 0) {
842                 DHD_ERROR(("%s: set cur_etheraddr failed\n",
843                            dhd_ifname(&dhd->pub, ifidx)));
844         } else {
845                 memcpy(dhd->iflist[ifidx]->net->dev_addr, addr, ETHER_ADDR_LEN);
846         }
847
848         return ret;
849 }
850
851 #ifdef SOFTAP
852 extern struct net_device *ap_net_dev;
853 #endif
854
855 static void dhd_op_if(dhd_if_t *ifp)
856 {
857         dhd_info_t *dhd;
858         int ret = 0, err = 0;
859
860         ASSERT(ifp && ifp->info && ifp->idx);   /* Virtual interfaces only */
861
862         dhd = ifp->info;
863
864         DHD_TRACE(("%s: idx %d, state %d\n", __func__, ifp->idx, ifp->state));
865
866         switch (ifp->state) {
867         case WLC_E_IF_ADD:
868                 /*
869                  * Delete the existing interface before overwriting it
870                  * in case we missed the WLC_E_IF_DEL event.
871                  */
872                 if (ifp->net != NULL) {
873                         DHD_ERROR(("%s: ERROR: netdev:%s already exists, "
874                         "try free & unregister\n",
875                         __func__, ifp->net->name));
876                         netif_stop_queue(ifp->net);
877                         unregister_netdev(ifp->net);
878                         free_netdev(ifp->net);
879                 }
880                 /* Allocate etherdev, including space for private structure */
881                 ifp->net = alloc_etherdev(sizeof(dhd));
882                 if (!ifp->net) {
883                         DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
884                         ret = -ENOMEM;
885                 }
886                 if (ret == 0) {
887                         strcpy(ifp->net->name, ifp->name);
888                         memcpy(netdev_priv(ifp->net), &dhd, sizeof(dhd));
889                         err = dhd_net_attach(&dhd->pub, ifp->idx);
890                         if (err != 0) {
891                                 DHD_ERROR(("%s: dhd_net_attach failed, "
892                                         "err %d\n",
893                                         __func__, err));
894                                 ret = -EOPNOTSUPP;
895                         } else {
896 #ifdef SOFTAP
897                                 /* semaphore that the soft AP CODE
898                                          waits on */
899                                 extern struct semaphore ap_eth_sema;
900
901                                 /* save ptr to wl0.1 netdev for use
902                                          in wl_iw.c  */
903                                 ap_net_dev = ifp->net;
904                                 /* signal to the SOFTAP 'sleeper' thread,
905                                          wl0.1 is ready */
906                                 up(&ap_eth_sema);
907 #endif
908                                 DHD_TRACE(("\n ==== pid:%x, net_device for "
909                                         "if:%s created ===\n\n",
910                                         current->pid, ifp->net->name));
911                                 ifp->state = 0;
912                         }
913                 }
914                 break;
915         case WLC_E_IF_DEL:
916                 if (ifp->net != NULL) {
917                         DHD_TRACE(("\n%s: got 'WLC_E_IF_DEL' state\n",
918                                    __func__));
919                         netif_stop_queue(ifp->net);
920                         unregister_netdev(ifp->net);
921                         ret = DHD_DEL_IF;       /* Make sure the free_netdev()
922                                                          is called */
923                 }
924                 break;
925         default:
926                 DHD_ERROR(("%s: bad op %d\n", __func__, ifp->state));
927                 ASSERT(!ifp->state);
928                 break;
929         }
930
931         if (ret < 0) {
932                 if (ifp->net)
933                         free_netdev(ifp->net);
934
935                 dhd->iflist[ifp->idx] = NULL;
936                 MFREE(dhd->pub.osh, ifp, sizeof(*ifp));
937 #ifdef SOFTAP
938                 if (ifp->net == ap_net_dev)
939                         ap_net_dev = NULL;      /*  NULL  SOFTAP global
940                                                          wl0.1 as well */
941 #endif                          /*  SOFTAP */
942         }
943 }
944
945 static int _dhd_sysioc_thread(void *data)
946 {
947         dhd_info_t *dhd = (dhd_info_t *) data;
948         int i;
949 #ifdef SOFTAP
950         bool in_ap = FALSE;
951 #endif
952
953         DAEMONIZE("dhd_sysioc");
954
955         while (down_interruptible(&dhd->sysioc_sem) == 0) {
956                 for (i = 0; i < DHD_MAX_IFS; i++) {
957                         if (dhd->iflist[i]) {
958 #ifdef SOFTAP
959                                 in_ap = (ap_net_dev != NULL);
960 #endif                          /* SOFTAP */
961                                 if (dhd->iflist[i]->state)
962                                         dhd_op_if(dhd->iflist[i]);
963 #ifdef SOFTAP
964                                 if (dhd->iflist[i] == NULL) {
965                                         DHD_TRACE(("\n\n %s: interface %d "
966                                                 "removed!\n", __func__, i));
967                                         continue;
968                                 }
969
970                                 if (in_ap && dhd->set_macaddress) {
971                                         DHD_TRACE(("attempt to set MAC for %s "
972                                                 "in AP Mode," "blocked. \n",
973                                                 dhd->iflist[i]->net->name));
974                                         dhd->set_macaddress = FALSE;
975                                         continue;
976                                 }
977
978                                 if (in_ap && dhd->set_multicast) {
979                                         DHD_TRACE(("attempt to set MULTICAST list for %s" "in AP Mode, blocked. \n",
980                                                 dhd->iflist[i]->net->name));
981                                         dhd->set_multicast = FALSE;
982                                         continue;
983                                 }
984 #endif                          /* SOFTAP */
985                                 if (dhd->set_multicast) {
986                                         dhd->set_multicast = FALSE;
987                                         _dhd_set_multicast_list(dhd, i);
988                                 }
989                                 if (dhd->set_macaddress) {
990                                         dhd->set_macaddress = FALSE;
991                                         _dhd_set_mac_address(dhd, i,
992                                                              &dhd->macvalue);
993                                 }
994                         }
995                 }
996         }
997         complete_and_exit(&dhd->sysioc_exited, 0);
998 }
999
1000 static int dhd_set_mac_address(struct net_device *dev, void *addr)
1001 {
1002         int ret = 0;
1003
1004         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
1005         struct sockaddr *sa = (struct sockaddr *)addr;
1006         int ifidx;
1007
1008         ifidx = dhd_net2idx(dhd, dev);
1009         if (ifidx == DHD_BAD_IF)
1010                 return -1;
1011
1012         ASSERT(dhd->sysioc_pid >= 0);
1013         memcpy(&dhd->macvalue, sa->sa_data, ETHER_ADDR_LEN);
1014         dhd->set_macaddress = TRUE;
1015         up(&dhd->sysioc_sem);
1016
1017         return ret;
1018 }
1019
1020 static void dhd_set_multicast_list(struct net_device *dev)
1021 {
1022         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
1023         int ifidx;
1024
1025         ifidx = dhd_net2idx(dhd, dev);
1026         if (ifidx == DHD_BAD_IF)
1027                 return;
1028
1029         ASSERT(dhd->sysioc_pid >= 0);
1030         dhd->set_multicast = TRUE;
1031         up(&dhd->sysioc_sem);
1032 }
1033
1034 int dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, void *pktbuf)
1035 {
1036         int ret;
1037         dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
1038
1039         /* Reject if down */
1040         if (!dhdp->up || (dhdp->busstate == DHD_BUS_DOWN))
1041                 return -ENODEV;
1042
1043         /* Update multicast statistic */
1044         if (PKTLEN(pktbuf) >= ETHER_ADDR_LEN) {
1045                 u8 *pktdata = (u8 *) PKTDATA(pktbuf);
1046                 struct ether_header *eh = (struct ether_header *)pktdata;
1047
1048                 if (ETHER_ISMULTI(eh->ether_dhost))
1049                         dhdp->tx_multicast++;
1050                 if (ntoh16(eh->ether_type) == ETHER_TYPE_802_1X)
1051                         atomic_inc(&dhd->pend_8021x_cnt);
1052         }
1053
1054         /* Look into the packet and update the packet priority */
1055         if ((PKTPRIO(pktbuf) == 0))
1056                 pktsetprio(pktbuf, FALSE);
1057
1058         /* If the protocol uses a data header, apply it */
1059         dhd_prot_hdrpush(dhdp, ifidx, pktbuf);
1060
1061         /* Use bus module to send data frame */
1062 #ifdef BCMDBUS
1063         ret = dbus_send_pkt(dhdp->dbus, pktbuf, NULL /* pktinfo */);
1064 #else
1065         WAKE_LOCK_TIMEOUT(dhdp, WAKE_LOCK_TMOUT, 25);
1066         ret = dhd_bus_txdata(dhdp->bus, pktbuf);
1067 #endif                          /* BCMDBUS */
1068
1069         return ret;
1070 }
1071
1072 static int dhd_start_xmit(struct sk_buff *skb, struct net_device *net)
1073 {
1074         int ret;
1075         void *pktbuf;
1076         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1077         int ifidx;
1078
1079         DHD_TRACE(("%s: Enter\n", __func__));
1080
1081         /* Reject if down */
1082         if (!dhd->pub.up || (dhd->pub.busstate == DHD_BUS_DOWN)) {
1083                 DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d\n",
1084                            __func__, dhd->pub.up, dhd->pub.busstate));
1085                 netif_stop_queue(net);
1086                 return -ENODEV;
1087         }
1088
1089         ifidx = dhd_net2idx(dhd, net);
1090         if (ifidx == DHD_BAD_IF) {
1091                 DHD_ERROR(("%s: bad ifidx %d\n", __func__, ifidx));
1092                 netif_stop_queue(net);
1093                 return -ENODEV;
1094         }
1095
1096         /* Make sure there's enough room for any header */
1097         if (skb_headroom(skb) < dhd->pub.hdrlen) {
1098                 struct sk_buff *skb2;
1099
1100                 DHD_INFO(("%s: insufficient headroom\n",
1101                           dhd_ifname(&dhd->pub, ifidx)));
1102                 dhd->pub.tx_realloc++;
1103                 skb2 = skb_realloc_headroom(skb, dhd->pub.hdrlen);
1104                 dev_kfree_skb(skb);
1105                 skb = skb2;
1106                 if (skb == NULL) {
1107                         DHD_ERROR(("%s: skb_realloc_headroom failed\n",
1108                                    dhd_ifname(&dhd->pub, ifidx)));
1109                         ret = -ENOMEM;
1110                         goto done;
1111                 }
1112         }
1113
1114         /* Convert to packet */
1115         pktbuf = PKTFRMNATIVE(dhd->pub.osh, skb);
1116         if (!pktbuf) {
1117                 DHD_ERROR(("%s: PKTFRMNATIVE failed\n",
1118                            dhd_ifname(&dhd->pub, ifidx)));
1119                 dev_kfree_skb_any(skb);
1120                 ret = -ENOMEM;
1121                 goto done;
1122         }
1123
1124         ret = dhd_sendpkt(&dhd->pub, ifidx, pktbuf);
1125
1126 done:
1127         if (ret)
1128                 dhd->pub.dstats.tx_dropped++;
1129         else
1130                 dhd->pub.tx_packets++;
1131
1132         /* Return ok: we always eat the packet */
1133         return 0;
1134 }
1135
1136 void dhd_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool state)
1137 {
1138         struct net_device *net;
1139         dhd_info_t *dhd = dhdp->info;
1140
1141         DHD_TRACE(("%s: Enter\n", __func__));
1142
1143         dhdp->txoff = state;
1144         ASSERT(dhd && dhd->iflist[ifidx]);
1145         net = dhd->iflist[ifidx]->net;
1146         if (state == ON)
1147                 netif_stop_queue(net);
1148         else
1149                 netif_wake_queue(net);
1150 }
1151
1152 void dhd_rx_frame(dhd_pub_t *dhdp, int ifidx, void *pktbuf, int numpkt)
1153 {
1154         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1155         struct sk_buff *skb;
1156         unsigned char *eth;
1157         uint len;
1158         void *data, *pnext, *save_pktbuf;
1159         int i;
1160         dhd_if_t *ifp;
1161         wl_event_msg_t event;
1162
1163         DHD_TRACE(("%s: Enter\n", __func__));
1164
1165         save_pktbuf = pktbuf;
1166
1167         for (i = 0; pktbuf && i < numpkt; i++, pktbuf = pnext) {
1168
1169                 pnext = PKTNEXT(pktbuf);
1170                 PKTSETNEXT(pktbuf, NULL);
1171
1172                 skb = PKTTONATIVE(dhdp->osh, pktbuf);
1173
1174                 /* Get the protocol, maintain skb around eth_type_trans()
1175                  * The main reason for this hack is for the limitation of
1176                  * Linux 2.4 where 'eth_type_trans' uses the
1177                  * 'net->hard_header_len'
1178                  * to perform skb_pull inside vs ETH_HLEN. Since to avoid
1179                  * coping of the packet coming from the network stack to add
1180                  * BDC, Hardware header etc, during network interface
1181                  * registration
1182                  * we set the 'net->hard_header_len' to ETH_HLEN + extra space
1183                  * required
1184                  * for BDC, Hardware header etc. and not just the ETH_HLEN
1185                  */
1186                 eth = skb->data;
1187                 len = skb->len;
1188
1189                 ifp = dhd->iflist[ifidx];
1190                 if (ifp == NULL)
1191                         ifp = dhd->iflist[0];
1192
1193                 ASSERT(ifp);
1194                 skb->dev = ifp->net;
1195                 skb->protocol = eth_type_trans(skb, skb->dev);
1196
1197                 if (skb->pkt_type == PACKET_MULTICAST)
1198                         dhd->pub.rx_multicast++;
1199
1200                 skb->data = eth;
1201                 skb->len = len;
1202
1203                 /* Strip header, count, deliver upward */
1204                 skb_pull(skb, ETH_HLEN);
1205
1206                 /* Process special event packets and then discard them */
1207                 if (ntoh16(skb->protocol) == ETHER_TYPE_BRCM)
1208                         dhd_wl_host_event(dhd, &ifidx,
1209                                           skb->mac_header,
1210                                           &event, &data);
1211
1212                 ASSERT(ifidx < DHD_MAX_IFS && dhd->iflist[ifidx]);
1213                 if (dhd->iflist[ifidx] && !dhd->iflist[ifidx]->state)
1214                         ifp = dhd->iflist[ifidx];
1215
1216                 if (ifp->net)
1217                         ifp->net->last_rx = jiffies;
1218
1219                 dhdp->dstats.rx_bytes += skb->len;
1220                 dhdp->rx_packets++;     /* Local count */
1221
1222                 if (in_interrupt()) {
1223                         netif_rx(skb);
1224                 } else {
1225                         /* If the receive is not processed inside an ISR,
1226                          * the softirqd must be woken explicitly to service
1227                          * the NET_RX_SOFTIRQ.  In 2.6 kernels, this is handled
1228                          * by netif_rx_ni(), but in earlier kernels, we need
1229                          * to do it manually.
1230                          */
1231                         netif_rx_ni(skb);
1232                 }
1233         }
1234 }
1235
1236 void dhd_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx)
1237 {
1238         /* Linux version has nothing to do */
1239         return;
1240 }
1241
1242 void dhd_txcomplete(dhd_pub_t *dhdp, void *txp, bool success)
1243 {
1244         uint ifidx;
1245         dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
1246         struct ether_header *eh;
1247         uint16 type;
1248
1249         dhd_prot_hdrpull(dhdp, &ifidx, txp);
1250
1251         eh = (struct ether_header *)PKTDATA(txp);
1252         type = ntoh16(eh->ether_type);
1253
1254         if (type == ETHER_TYPE_802_1X)
1255                 atomic_dec(&dhd->pend_8021x_cnt);
1256
1257 }
1258
1259 static struct net_device_stats *dhd_get_stats(struct net_device *net)
1260 {
1261         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1262         dhd_if_t *ifp;
1263         int ifidx;
1264
1265         DHD_TRACE(("%s: Enter\n", __func__));
1266
1267         ifidx = dhd_net2idx(dhd, net);
1268         if (ifidx == DHD_BAD_IF)
1269                 return NULL;
1270
1271         ifp = dhd->iflist[ifidx];
1272         ASSERT(dhd && ifp);
1273
1274         if (dhd->pub.up) {
1275                 /* Use the protocol to get dongle stats */
1276                 dhd_prot_dstats(&dhd->pub);
1277         }
1278
1279         /* Copy dongle stats to net device stats */
1280         ifp->stats.rx_packets = dhd->pub.dstats.rx_packets;
1281         ifp->stats.tx_packets = dhd->pub.dstats.tx_packets;
1282         ifp->stats.rx_bytes = dhd->pub.dstats.rx_bytes;
1283         ifp->stats.tx_bytes = dhd->pub.dstats.tx_bytes;
1284         ifp->stats.rx_errors = dhd->pub.dstats.rx_errors;
1285         ifp->stats.tx_errors = dhd->pub.dstats.tx_errors;
1286         ifp->stats.rx_dropped = dhd->pub.dstats.rx_dropped;
1287         ifp->stats.tx_dropped = dhd->pub.dstats.tx_dropped;
1288         ifp->stats.multicast = dhd->pub.dstats.multicast;
1289
1290         return &ifp->stats;
1291 }
1292
1293 static int dhd_watchdog_thread(void *data)
1294 {
1295         dhd_info_t *dhd = (dhd_info_t *) data;
1296         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_WATCHDOG, "dhd_watchdog_thread");
1297
1298         /* This thread doesn't need any user-level access,
1299          * so get rid of all our resources
1300          */
1301 #ifdef DHD_SCHED
1302         if (dhd_watchdog_prio > 0) {
1303                 struct sched_param param;
1304                 param.sched_priority = (dhd_watchdog_prio < MAX_RT_PRIO) ?
1305                     dhd_watchdog_prio : (MAX_RT_PRIO - 1);
1306                 setScheduler(current, SCHED_FIFO, &param);
1307         }
1308 #endif                          /* DHD_SCHED */
1309
1310         DAEMONIZE("dhd_watchdog");
1311
1312         /* Run until signal received */
1313         while (1) {
1314                 if (down_interruptible(&dhd->watchdog_sem) == 0) {
1315                         if (dhd->pub.dongle_reset == FALSE) {
1316                                 WAKE_LOCK(&dhd->pub, WAKE_LOCK_WATCHDOG);
1317                                 /* Call the bus module watchdog */
1318                                 dhd_bus_watchdog(&dhd->pub);
1319                                 WAKE_UNLOCK(&dhd->pub, WAKE_LOCK_WATCHDOG);
1320                         }
1321                         /* Count the tick for reference */
1322                         dhd->pub.tickcnt++;
1323                 } else
1324                         break;
1325         }
1326
1327         WAKE_LOCK_DESTROY(&dhd->pub, WAKE_LOCK_WATCHDOG);
1328         complete_and_exit(&dhd->watchdog_exited, 0);
1329 }
1330
1331 static void dhd_watchdog(unsigned long data)
1332 {
1333         dhd_info_t *dhd = (dhd_info_t *) data;
1334
1335         if (dhd->watchdog_pid >= 0) {
1336                 up(&dhd->watchdog_sem);
1337
1338                 /* Reschedule the watchdog */
1339                 if (dhd->wd_timer_valid) {
1340                         mod_timer(&dhd->timer,
1341                                   jiffies + dhd_watchdog_ms * HZ / 1000);
1342                 }
1343                 return;
1344         }
1345
1346         /* Call the bus module watchdog */
1347         dhd_bus_watchdog(&dhd->pub);
1348
1349         /* Count the tick for reference */
1350         dhd->pub.tickcnt++;
1351
1352         /* Reschedule the watchdog */
1353         if (dhd->wd_timer_valid)
1354                 mod_timer(&dhd->timer, jiffies + dhd_watchdog_ms * HZ / 1000);
1355 }
1356
1357 static int dhd_dpc_thread(void *data)
1358 {
1359         dhd_info_t *dhd = (dhd_info_t *) data;
1360
1361         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_DPC, "dhd_dpc_thread");
1362         /* This thread doesn't need any user-level access,
1363          * so get rid of all our resources
1364          */
1365 #ifdef DHD_SCHED
1366         if (dhd_dpc_prio > 0) {
1367                 struct sched_param param;
1368                 param.sched_priority =
1369                     (dhd_dpc_prio <
1370                      MAX_RT_PRIO) ? dhd_dpc_prio : (MAX_RT_PRIO - 1);
1371                 setScheduler(current, SCHED_FIFO, &param);
1372         }
1373 #endif                          /* DHD_SCHED */
1374
1375         DAEMONIZE("dhd_dpc");
1376
1377         /* Run until signal received */
1378         while (1) {
1379                 if (down_interruptible(&dhd->dpc_sem) == 0) {
1380                         /* Call bus dpc unless it indicated down
1381                                  (then clean stop) */
1382                         if (dhd->pub.busstate != DHD_BUS_DOWN) {
1383                                 WAKE_LOCK(&dhd->pub, WAKE_LOCK_DPC);
1384                                 if (dhd_bus_dpc(dhd->pub.bus)) {
1385                                         up(&dhd->dpc_sem);
1386                                         WAKE_LOCK_TIMEOUT(&dhd->pub,
1387                                                           WAKE_LOCK_TMOUT, 25);
1388                                 }
1389                                 WAKE_UNLOCK(&dhd->pub, WAKE_LOCK_DPC);
1390                         } else {
1391                                 dhd_bus_stop(dhd->pub.bus, TRUE);
1392                         }
1393                 } else
1394                         break;
1395         }
1396
1397         WAKE_LOCK_DESTROY(&dhd->pub, WAKE_LOCK_DPC);
1398
1399         complete_and_exit(&dhd->dpc_exited, 0);
1400 }
1401
1402 static void dhd_dpc(unsigned long data)
1403 {
1404         dhd_info_t *dhd;
1405
1406         dhd = (dhd_info_t *) data;
1407
1408         /* Call bus dpc unless it indicated down (then clean stop) */
1409         if (dhd->pub.busstate != DHD_BUS_DOWN) {
1410                 if (dhd_bus_dpc(dhd->pub.bus))
1411                         tasklet_schedule(&dhd->tasklet);
1412         } else {
1413                 dhd_bus_stop(dhd->pub.bus, TRUE);
1414         }
1415 }
1416
1417 void dhd_sched_dpc(dhd_pub_t *dhdp)
1418 {
1419         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1420
1421         if (dhd->dpc_pid >= 0) {
1422                 up(&dhd->dpc_sem);
1423                 return;
1424         }
1425
1426         tasklet_schedule(&dhd->tasklet);
1427 }
1428
1429 #ifdef TOE
1430 /* Retrieve current toe component enables, which are kept
1431          as a bitmap in toe_ol iovar */
1432 static int dhd_toe_get(dhd_info_t *dhd, int ifidx, uint32 *toe_ol)
1433 {
1434         wl_ioctl_t ioc;
1435         char buf[32];
1436         int ret;
1437
1438         memset(&ioc, 0, sizeof(ioc));
1439
1440         ioc.cmd = WLC_GET_VAR;
1441         ioc.buf = buf;
1442         ioc.len = (uint) sizeof(buf);
1443         ioc.set = FALSE;
1444
1445         strcpy(buf, "toe_ol");
1446         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1447         if (ret < 0) {
1448                 /* Check for older dongle image that doesn't support toe_ol */
1449                 if (ret == -EIO) {
1450                         DHD_ERROR(("%s: toe not supported by device\n",
1451                                    dhd_ifname(&dhd->pub, ifidx)));
1452                         return -EOPNOTSUPP;
1453                 }
1454
1455                 DHD_INFO(("%s: could not get toe_ol: ret=%d\n",
1456                           dhd_ifname(&dhd->pub, ifidx), ret));
1457                 return ret;
1458         }
1459
1460         memcpy(toe_ol, buf, sizeof(uint32));
1461         return 0;
1462 }
1463
1464 /* Set current toe component enables in toe_ol iovar,
1465          and set toe global enable iovar */
1466 static int dhd_toe_set(dhd_info_t *dhd, int ifidx, uint32 toe_ol)
1467 {
1468         wl_ioctl_t ioc;
1469         char buf[32];
1470         int toe, ret;
1471
1472         memset(&ioc, 0, sizeof(ioc));
1473
1474         ioc.cmd = WLC_SET_VAR;
1475         ioc.buf = buf;
1476         ioc.len = (uint) sizeof(buf);
1477         ioc.set = TRUE;
1478
1479         /* Set toe_ol as requested */
1480
1481         strcpy(buf, "toe_ol");
1482         memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(uint32));
1483
1484         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1485         if (ret < 0) {
1486                 DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
1487                            dhd_ifname(&dhd->pub, ifidx), ret));
1488                 return ret;
1489         }
1490
1491         /* Enable toe globally only if any components are enabled. */
1492
1493         toe = (toe_ol != 0);
1494
1495         strcpy(buf, "toe");
1496         memcpy(&buf[sizeof("toe")], &toe, sizeof(uint32));
1497
1498         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1499         if (ret < 0) {
1500                 DHD_ERROR(("%s: could not set toe: ret=%d\n",
1501                            dhd_ifname(&dhd->pub, ifidx), ret));
1502                 return ret;
1503         }
1504
1505         return 0;
1506 }
1507 #endif                          /* TOE */
1508
1509 static void dhd_ethtool_get_drvinfo(struct net_device *net,
1510                                     struct ethtool_drvinfo *info)
1511 {
1512         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1513
1514         sprintf(info->driver, "wl");
1515         sprintf(info->version, "%lu", dhd->pub.drv_version);
1516 }
1517
1518 struct ethtool_ops dhd_ethtool_ops = {
1519         .get_drvinfo = dhd_ethtool_get_drvinfo
1520 };
1521
1522 static int dhd_ethtool(dhd_info_t *dhd, void *uaddr)
1523 {
1524         struct ethtool_drvinfo info;
1525         char drvname[sizeof(info.driver)];
1526         uint32 cmd;
1527 #ifdef TOE
1528         struct ethtool_value edata;
1529         uint32 toe_cmpnt, csum_dir;
1530         int ret;
1531 #endif
1532
1533         DHD_TRACE(("%s: Enter\n", __func__));
1534
1535         /* all ethtool calls start with a cmd word */
1536         if (copy_from_user(&cmd, uaddr, sizeof(uint32)))
1537                 return -EFAULT;
1538
1539         switch (cmd) {
1540         case ETHTOOL_GDRVINFO:
1541                 /* Copy out any request driver name */
1542                 if (copy_from_user(&info, uaddr, sizeof(info)))
1543                         return -EFAULT;
1544                 strncpy(drvname, info.driver, sizeof(info.driver));
1545                 drvname[sizeof(info.driver) - 1] = '\0';
1546
1547                 /* clear struct for return */
1548                 memset(&info, 0, sizeof(info));
1549                 info.cmd = cmd;
1550
1551                 /* if dhd requested, identify ourselves */
1552                 if (strcmp(drvname, "?dhd") == 0) {
1553                         sprintf(info.driver, "dhd");
1554                         strcpy(info.version, EPI_VERSION_STR);
1555                 }
1556
1557                 /* otherwise, require dongle to be up */
1558                 else if (!dhd->pub.up) {
1559                         DHD_ERROR(("%s: dongle is not up\n", __func__));
1560                         return -ENODEV;
1561                 }
1562
1563                 /* finally, report dongle driver type */
1564                 else if (dhd->pub.iswl)
1565                         sprintf(info.driver, "wl");
1566                 else
1567                         sprintf(info.driver, "xx");
1568
1569                 sprintf(info.version, "%lu", dhd->pub.drv_version);
1570                 if (copy_to_user(uaddr, &info, sizeof(info)))
1571                         return -EFAULT;
1572                 DHD_CTL(("%s: given %*s, returning %s\n", __func__,
1573                          (int)sizeof(drvname), drvname, info.driver));
1574                 break;
1575
1576 #ifdef TOE
1577                 /* Get toe offload components from dongle */
1578         case ETHTOOL_GRXCSUM:
1579         case ETHTOOL_GTXCSUM:
1580                 ret = dhd_toe_get(dhd, 0, &toe_cmpnt);
1581                 if (ret < 0)
1582                         return ret;
1583
1584                 csum_dir =
1585                     (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1586
1587                 edata.cmd = cmd;
1588                 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
1589
1590                 if (copy_to_user(uaddr, &edata, sizeof(edata)))
1591                         return -EFAULT;
1592                 break;
1593
1594                 /* Set toe offload components in dongle */
1595         case ETHTOOL_SRXCSUM:
1596         case ETHTOOL_STXCSUM:
1597                 if (copy_from_user(&edata, uaddr, sizeof(edata)))
1598                         return -EFAULT;
1599
1600                 /* Read the current settings, update and write back */
1601                 ret = dhd_toe_get(dhd, 0, &toe_cmpnt);
1602                 if (ret < 0)
1603                         return ret;
1604
1605                 csum_dir =
1606                     (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1607
1608                 if (edata.data != 0)
1609                         toe_cmpnt |= csum_dir;
1610                 else
1611                         toe_cmpnt &= ~csum_dir;
1612
1613                 ret = dhd_toe_set(dhd, 0, toe_cmpnt);
1614                 if (ret < 0)
1615                         return ret;
1616
1617                 /* If setting TX checksum mode, tell Linux the new mode */
1618                 if (cmd == ETHTOOL_STXCSUM) {
1619                         if (edata.data)
1620                                 dhd->iflist[0]->net->features |=
1621                                     NETIF_F_IP_CSUM;
1622                         else
1623                                 dhd->iflist[0]->net->features &=
1624                                     ~NETIF_F_IP_CSUM;
1625                 }
1626
1627                 break;
1628 #endif                          /* TOE */
1629
1630         default:
1631                 return -EOPNOTSUPP;
1632         }
1633
1634         return 0;
1635 }
1636
1637 static int dhd_ioctl_entry(struct net_device *net, struct ifreq *ifr, int cmd)
1638 {
1639         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1640         dhd_ioctl_t ioc;
1641         int bcmerror = 0;
1642         int buflen = 0;
1643         void *buf = NULL;
1644         uint driver = 0;
1645         int ifidx;
1646         bool is_set_key_cmd;
1647
1648         ifidx = dhd_net2idx(dhd, net);
1649         DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __func__, ifidx, cmd));
1650
1651         if (ifidx == DHD_BAD_IF)
1652                 return -1;
1653
1654 #if defined(CONFIG_WIRELESS_EXT)
1655         /* linux wireless extensions */
1656         if ((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST)) {
1657                 /* may recurse, do NOT lock */
1658                 return wl_iw_ioctl(net, ifr, cmd);
1659         }
1660 #endif                          /* defined(CONFIG_WIRELESS_EXT) */
1661
1662         if (cmd == SIOCETHTOOL)
1663                 return dhd_ethtool(dhd, (void *)ifr->ifr_data);
1664
1665         if (cmd != SIOCDEVPRIVATE)
1666                 return -EOPNOTSUPP;
1667
1668         memset(&ioc, 0, sizeof(ioc));
1669
1670         /* Copy the ioc control structure part of ioctl request */
1671         if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
1672                 bcmerror = -BCME_BADADDR;
1673                 goto done;
1674         }
1675
1676         /* Copy out any buffer passed */
1677         if (ioc.buf) {
1678                 buflen = MIN(ioc.len, DHD_IOCTL_MAXLEN);
1679                 /* optimization for direct ioctl calls from kernel */
1680                 /*
1681                    if (segment_eq(get_fs(), KERNEL_DS)) {
1682                    buf = ioc.buf;
1683                    } else {
1684                  */
1685                 {
1686                         buf = (char *)MALLOC(dhd->pub.osh, buflen);
1687                         if (!buf) {
1688                                 bcmerror = -BCME_NOMEM;
1689                                 goto done;
1690                         }
1691                         if (copy_from_user(buf, ioc.buf, buflen)) {
1692                                 bcmerror = -BCME_BADADDR;
1693                                 goto done;
1694                         }
1695                 }
1696         }
1697
1698         /* To differentiate between wl and dhd read 4 more byes */
1699         if ((copy_from_user(&driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
1700                             sizeof(uint)) != 0)) {
1701                 bcmerror = -BCME_BADADDR;
1702                 goto done;
1703         }
1704
1705         if (!capable(CAP_NET_ADMIN)) {
1706                 bcmerror = -BCME_EPERM;
1707                 goto done;
1708         }
1709
1710         /* check for local dhd ioctl and handle it */
1711         if (driver == DHD_IOCTL_MAGIC) {
1712                 bcmerror = dhd_ioctl((void *)&dhd->pub, &ioc, buf, buflen);
1713                 if (bcmerror)
1714                         dhd->pub.bcmerror = bcmerror;
1715                 goto done;
1716         }
1717
1718         /* send to dongle (must be up, and wl) */
1719         if ((dhd->pub.busstate != DHD_BUS_DATA)) {
1720                 DHD_ERROR(("%s DONGLE_DOWN,__func__\n", __func__));
1721                 bcmerror = BCME_DONGLE_DOWN;
1722                 goto done;
1723         }
1724
1725         if (!dhd->pub.iswl) {
1726                 bcmerror = BCME_DONGLE_DOWN;
1727                 goto done;
1728         }
1729
1730         /* Intercept WLC_SET_KEY IOCTL - serialize M4 send and set key IOCTL to
1731          * prevent M4 encryption.
1732          */
1733         is_set_key_cmd = ((ioc.cmd == WLC_SET_KEY) ||
1734                           ((ioc.cmd == WLC_SET_VAR) &&
1735                            !(strncmp("wsec_key", ioc.buf, 9))) ||
1736                           ((ioc.cmd == WLC_SET_VAR) &&
1737                            !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1738         if (is_set_key_cmd)
1739                 dhd_wait_pend8021x(net);
1740
1741         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_IOCTL, "dhd_ioctl_entry");
1742         WAKE_LOCK(&dhd->pub, WAKE_LOCK_IOCTL);
1743
1744         bcmerror =
1745             dhd_prot_ioctl(&dhd->pub, ifidx, (wl_ioctl_t *)&ioc, buf, buflen);
1746
1747         WAKE_UNLOCK(&dhd->pub, WAKE_LOCK_IOCTL);
1748         WAKE_LOCK_DESTROY(&dhd->pub, WAKE_LOCK_IOCTL);
1749 done:
1750         if (!bcmerror && buf && ioc.buf) {
1751                 if (copy_to_user(ioc.buf, buf, buflen))
1752                         bcmerror = -EFAULT;
1753         }
1754
1755         if (buf)
1756                 MFREE(dhd->pub.osh, buf, buflen);
1757
1758         return OSL_ERROR(bcmerror);
1759 }
1760
1761 static int dhd_stop(struct net_device *net)
1762 {
1763 #if !defined(IGNORE_ETH0_DOWN)
1764         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1765
1766         DHD_TRACE(("%s: Enter\n", __func__));
1767 #ifdef CONFIG_CFG80211
1768         if (IS_CFG80211_FAVORITE()) {
1769                 wl_cfg80211_down();
1770         }
1771 #endif
1772         if (dhd->pub.up == 0)
1773                 return 0;
1774
1775         /* Set state and stop OS transmissions */
1776         dhd->pub.up = 0;
1777         netif_stop_queue(net);
1778 #else
1779         DHD_ERROR(("BYPASS %s:due to BRCM compilation : under investigation\n",
1780                 __func__));
1781 #endif                          /* !defined(IGNORE_ETH0_DOWN) */
1782
1783         OLD_MOD_DEC_USE_COUNT;
1784         return 0;
1785 }
1786
1787 static int dhd_open(struct net_device *net)
1788 {
1789         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1790 #ifdef TOE
1791         uint32 toe_ol;
1792 #endif
1793         int ifidx = dhd_net2idx(dhd, net);
1794         int32 ret = 0;
1795
1796         DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1797
1798         if (ifidx == 0) {       /* do it only for primary eth0 */
1799
1800                 /* try to bring up bus */
1801                 ret = dhd_bus_start(&dhd->pub);
1802                 if (ret != 0) {
1803                         DHD_ERROR(("%s: failed with code %d\n", __func__, ret));
1804                         return -1;
1805                 }
1806                 atomic_set(&dhd->pend_8021x_cnt, 0);
1807
1808                 memcpy(net->dev_addr, dhd->pub.mac.octet, ETHER_ADDR_LEN);
1809
1810 #ifdef TOE
1811                 /* Get current TOE mode from dongle */
1812                 if (dhd_toe_get(dhd, ifidx, &toe_ol) >= 0
1813                     && (toe_ol & TOE_TX_CSUM_OL) != 0)
1814                         dhd->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
1815                 else
1816                         dhd->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
1817 #endif
1818         }
1819         /* Allow transmit calls */
1820         netif_start_queue(net);
1821         dhd->pub.up = 1;
1822 #ifdef CONFIG_CFG80211
1823         if (IS_CFG80211_FAVORITE()) {
1824                 if (unlikely(wl_cfg80211_up())) {
1825                         DHD_ERROR(("%s: failed to bring up cfg80211\n",
1826                                    __func__));
1827                         return -1;
1828                 }
1829         }
1830 #endif
1831
1832         OLD_MOD_INC_USE_COUNT;
1833         return ret;
1834 }
1835
1836 osl_t *dhd_osl_attach(void *pdev, uint bustype)
1837 {
1838         return osl_attach(pdev, bustype, TRUE);
1839 }
1840
1841 void dhd_osl_detach(osl_t *osh)
1842 {
1843         if (MALLOCED(osh)) {
1844                 DHD_ERROR(("%s: MEMORY LEAK %d bytes\n", __func__,
1845                            MALLOCED(osh)));
1846         }
1847         osl_detach(osh);
1848 }
1849
1850 int
1851 dhd_add_if(dhd_info_t *dhd, int ifidx, void *handle, char *name,
1852            u8 *mac_addr, uint32 flags, u8 bssidx)
1853 {
1854         dhd_if_t *ifp;
1855
1856         DHD_TRACE(("%s: idx %d, handle->%p\n", __func__, ifidx, handle));
1857
1858         ASSERT(dhd && (ifidx < DHD_MAX_IFS));
1859
1860         ifp = dhd->iflist[ifidx];
1861         if (!ifp && !(ifp = MALLOC(dhd->pub.osh, sizeof(dhd_if_t)))) {
1862                 DHD_ERROR(("%s: OOM - dhd_if_t\n", __func__));
1863                 return -ENOMEM;
1864         }
1865
1866         memset(ifp, 0, sizeof(dhd_if_t));
1867         ifp->info = dhd;
1868         dhd->iflist[ifidx] = ifp;
1869         strncpy(ifp->name, name, IFNAMSIZ);
1870         ifp->name[IFNAMSIZ] = '\0';
1871         if (mac_addr != NULL)
1872                 memcpy(&ifp->mac_addr, mac_addr, ETHER_ADDR_LEN);
1873
1874         if (handle == NULL) {
1875                 ifp->state = WLC_E_IF_ADD;
1876                 ifp->idx = ifidx;
1877                 ASSERT(dhd->sysioc_pid >= 0);
1878                 up(&dhd->sysioc_sem);
1879         } else
1880                 ifp->net = (struct net_device *)handle;
1881
1882         return 0;
1883 }
1884
1885 void dhd_del_if(dhd_info_t *dhd, int ifidx)
1886 {
1887         dhd_if_t *ifp;
1888
1889         DHD_TRACE(("%s: idx %d\n", __func__, ifidx));
1890
1891         ASSERT(dhd && ifidx && (ifidx < DHD_MAX_IFS));
1892         ifp = dhd->iflist[ifidx];
1893         if (!ifp) {
1894                 DHD_ERROR(("%s: Null interface\n", __func__));
1895                 return;
1896         }
1897
1898         ifp->state = WLC_E_IF_DEL;
1899         ifp->idx = ifidx;
1900         ASSERT(dhd->sysioc_pid >= 0);
1901         up(&dhd->sysioc_sem);
1902 }
1903
1904 dhd_pub_t *dhd_attach(osl_t *osh, struct dhd_bus *bus, uint bus_hdrlen)
1905 {
1906         dhd_info_t *dhd = NULL;
1907         struct net_device *net;
1908
1909         DHD_TRACE(("%s: Enter\n", __func__));
1910         /* updates firmware nvram path if it was provided as module
1911                  paramters */
1912         if ((firmware_path != NULL) && (firmware_path[0] != '\0'))
1913                 strcpy(fw_path, firmware_path);
1914         if ((nvram_path != NULL) && (nvram_path[0] != '\0'))
1915                 strcpy(nv_path, nvram_path);
1916
1917         /* Allocate etherdev, including space for private structure */
1918         net = alloc_etherdev(sizeof(dhd));
1919         if (!net) {
1920                 DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
1921                 goto fail;
1922         }
1923
1924         /* Allocate primary dhd_info */
1925         dhd = MALLOC(osh, sizeof(dhd_info_t));
1926         if (!dhd) {
1927                 DHD_ERROR(("%s: OOM - alloc dhd_info\n", __func__));
1928                 goto fail;
1929         }
1930
1931         memset(dhd, 0, sizeof(dhd_info_t));
1932
1933         /*
1934          * Save the dhd_info into the priv
1935          */
1936         memcpy(netdev_priv(net), &dhd, sizeof(dhd));
1937         dhd->pub.osh = osh;
1938
1939         /* Set network interface name if it was provided as module parameter */
1940         if (iface_name[0]) {
1941                 int len;
1942                 char ch;
1943                 strncpy(net->name, iface_name, IFNAMSIZ);
1944                 net->name[IFNAMSIZ - 1] = 0;
1945                 len = strlen(net->name);
1946                 ch = net->name[len - 1];
1947                 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1948                         strcat(net->name, "%d");
1949         }
1950
1951         if (dhd_add_if(dhd, 0, (void *)net, net->name, NULL, 0, 0) ==
1952             DHD_BAD_IF)
1953                 goto fail;
1954
1955         net->netdev_ops = NULL;
1956         init_MUTEX(&dhd->proto_sem);
1957         /* Initialize other structure content */
1958         init_waitqueue_head(&dhd->ioctl_resp_wait);
1959         init_waitqueue_head(&dhd->ctrl_wait);
1960
1961         /* Initialize the spinlocks */
1962         spin_lock_init(&dhd->sdlock);
1963         spin_lock_init(&dhd->txqlock);
1964
1965         /* Link to info module */
1966         dhd->pub.info = dhd;
1967
1968         /* Link to bus module */
1969         dhd->pub.bus = bus;
1970         dhd->pub.hdrlen = bus_hdrlen;
1971
1972         /* Attach and link in the protocol */
1973         if (dhd_prot_attach(&dhd->pub) != 0) {
1974                 DHD_ERROR(("dhd_prot_attach failed\n"));
1975                 goto fail;
1976         }
1977 #if defined(CONFIG_WIRELESS_EXT)
1978         /* Attach and link in the iw */
1979         if (wl_iw_attach(net, (void *)&dhd->pub) != 0) {
1980                 DHD_ERROR(("wl_iw_attach failed\n"));
1981                 goto fail;
1982         }
1983 #endif                          /* defined(CONFIG_WIRELESS_EXT) */
1984
1985 #ifdef CONFIG_CFG80211
1986         /* Attach and link in the cfg80211 */
1987         if (IS_CFG80211_FAVORITE()) {
1988                 if (unlikely(wl_cfg80211_attach(net, &dhd->pub))) {
1989                         DHD_ERROR(("wl_cfg80211_attach failed\n"));
1990                         goto fail;
1991                 }
1992                 if (!NO_FW_REQ()) {
1993                         strcpy(fw_path, wl_cfg80211_get_fwname());
1994                         strcpy(nv_path, wl_cfg80211_get_nvramname());
1995                 }
1996                 wl_cfg80211_dbg_level(DBG_CFG80211_GET());
1997         }
1998 #endif
1999
2000         /* Set up the watchdog timer */
2001         init_timer(&dhd->timer);
2002         dhd->timer.data = (unsigned long) dhd;
2003         dhd->timer.function = dhd_watchdog;
2004
2005         /* Initialize thread based operation and lock */
2006         init_MUTEX(&dhd->sdsem);
2007         if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0))
2008                 dhd->threads_only = TRUE;
2009         else
2010                 dhd->threads_only = FALSE;
2011
2012         if (dhd_dpc_prio >= 0) {
2013                 /* Initialize watchdog thread */
2014                 sema_init(&dhd->watchdog_sem, 0);
2015                 init_completion(&dhd->watchdog_exited);
2016                 dhd->watchdog_pid = kernel_thread(dhd_watchdog_thread, dhd, 0);
2017         } else {
2018                 dhd->watchdog_pid = -1;
2019         }
2020
2021         /* Set up the bottom half handler */
2022         if (dhd_dpc_prio >= 0) {
2023                 /* Initialize DPC thread */
2024                 sema_init(&dhd->dpc_sem, 0);
2025                 init_completion(&dhd->dpc_exited);
2026                 dhd->dpc_pid = kernel_thread(dhd_dpc_thread, dhd, 0);
2027         } else {
2028                 tasklet_init(&dhd->tasklet, dhd_dpc, (unsigned long) dhd);
2029                 dhd->dpc_pid = -1;
2030         }
2031
2032         if (dhd_sysioc) {
2033                 sema_init(&dhd->sysioc_sem, 0);
2034                 init_completion(&dhd->sysioc_exited);
2035                 dhd->sysioc_pid = kernel_thread(_dhd_sysioc_thread, dhd, 0);
2036         } else {
2037                 dhd->sysioc_pid = -1;
2038         }
2039
2040         /*
2041          * Save the dhd_info into the priv
2042          */
2043         memcpy(netdev_priv(net), &dhd, sizeof(dhd));
2044
2045 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2046         g_bus = bus;
2047 #endif
2048 #if defined(CONFIG_PM_SLEEP)
2049         register_pm_notifier(&dhd_sleep_pm_notifier);
2050 #endif  /* defined(CONFIG_PM_SLEEP) */
2051         /* && defined(DHD_GPL) */
2052         /* Init lock suspend to prevent kernel going to suspend */
2053         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_TMOUT, "dhd_wake_lock");
2054         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_LINK_DOWN_TMOUT,
2055                        "dhd_wake_lock_link_dw_event");
2056         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_PNO_FIND_TMOUT,
2057                        "dhd_wake_lock_link_pno_find_event");
2058 #ifdef CONFIG_HAS_EARLYSUSPEND
2059         dhd->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 20;
2060         dhd->early_suspend.suspend = dhd_early_suspend;
2061         dhd->early_suspend.resume = dhd_late_resume;
2062         register_early_suspend(&dhd->early_suspend);
2063 #endif
2064
2065         return &dhd->pub;
2066
2067 fail:
2068         if (net)
2069                 free_netdev(net);
2070         if (dhd)
2071                 dhd_detach(&dhd->pub);
2072
2073         return NULL;
2074 }
2075
2076 int dhd_bus_start(dhd_pub_t *dhdp)
2077 {
2078         int ret = -1;
2079         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
2080 #ifdef EMBEDDED_PLATFORM
2081         char iovbuf[WL_EVENTING_MASK_LEN + 12]; /*  Room for "event_msgs" +
2082                                                  '\0' + bitvec  */
2083 #endif                          /* EMBEDDED_PLATFORM */
2084
2085         ASSERT(dhd);
2086
2087         DHD_TRACE(("%s:\n", __func__));
2088
2089         /* try to download image and nvram to the dongle */
2090         if (dhd->pub.busstate == DHD_BUS_DOWN) {
2091                 WAKE_LOCK_INIT(dhdp, WAKE_LOCK_DOWNLOAD, "dhd_bus_start");
2092                 WAKE_LOCK(dhdp, WAKE_LOCK_DOWNLOAD);
2093                 if (!(dhd_bus_download_firmware(dhd->pub.bus, dhd->pub.osh,
2094                                                 fw_path, nv_path))) {
2095                         DHD_ERROR(("%s: dhdsdio_probe_download failed. "
2096                                 "firmware = %s nvram = %s\n",
2097                                 __func__, fw_path, nv_path));
2098                         WAKE_UNLOCK(dhdp, WAKE_LOCK_DOWNLOAD);
2099                         WAKE_LOCK_DESTROY(dhdp, WAKE_LOCK_DOWNLOAD);
2100                         return -1;
2101                 }
2102
2103                 WAKE_UNLOCK(dhdp, WAKE_LOCK_DOWNLOAD);
2104                 WAKE_LOCK_DESTROY(dhdp, WAKE_LOCK_DOWNLOAD);
2105         }
2106
2107         /* Start the watchdog timer */
2108         dhd->pub.tickcnt = 0;
2109         dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
2110
2111         /* Bring up the bus */
2112         ret = dhd_bus_init(&dhd->pub, TRUE);
2113         if (ret != 0) {
2114                 DHD_ERROR(("%s, dhd_bus_init failed %d\n", __func__, ret));
2115                 return ret;
2116         }
2117 #if defined(OOB_INTR_ONLY)
2118         /* Host registration for OOB interrupt */
2119         if (bcmsdh_register_oob_intr(dhdp)) {
2120                 del_timer_sync(&dhd->timer);
2121                 dhd->wd_timer_valid = FALSE;
2122                 DHD_ERROR(("%s Host failed to resgister for OOB\n", __func__));
2123                 return -ENODEV;
2124         }
2125
2126         /* Enable oob at firmware */
2127         dhd_enable_oob_intr(dhd->pub.bus, TRUE);
2128 #endif                          /* defined(OOB_INTR_ONLY) */
2129
2130         /* If bus is not ready, can't come up */
2131         if (dhd->pub.busstate != DHD_BUS_DATA) {
2132                 del_timer_sync(&dhd->timer);
2133                 dhd->wd_timer_valid = FALSE;
2134                 DHD_ERROR(("%s failed bus is not ready\n", __func__));
2135                 return -ENODEV;
2136         }
2137 #ifdef EMBEDDED_PLATFORM
2138         bcm_mkiovar("event_msgs", dhdp->eventmask, WL_EVENTING_MASK_LEN, iovbuf,
2139                     sizeof(iovbuf));
2140         dhdcdc_query_ioctl(dhdp, 0, WLC_GET_VAR, iovbuf, sizeof(iovbuf));
2141         bcopy(iovbuf, dhdp->eventmask, WL_EVENTING_MASK_LEN);
2142
2143         setbit(dhdp->eventmask, WLC_E_SET_SSID);
2144         setbit(dhdp->eventmask, WLC_E_PRUNE);
2145         setbit(dhdp->eventmask, WLC_E_AUTH);
2146         setbit(dhdp->eventmask, WLC_E_REASSOC);
2147         setbit(dhdp->eventmask, WLC_E_REASSOC_IND);
2148         setbit(dhdp->eventmask, WLC_E_DEAUTH_IND);
2149         setbit(dhdp->eventmask, WLC_E_DISASSOC_IND);
2150         setbit(dhdp->eventmask, WLC_E_DISASSOC);
2151         setbit(dhdp->eventmask, WLC_E_JOIN);
2152         setbit(dhdp->eventmask, WLC_E_ASSOC_IND);
2153         setbit(dhdp->eventmask, WLC_E_PSK_SUP);
2154         setbit(dhdp->eventmask, WLC_E_LINK);
2155         setbit(dhdp->eventmask, WLC_E_NDIS_LINK);
2156         setbit(dhdp->eventmask, WLC_E_MIC_ERROR);
2157         setbit(dhdp->eventmask, WLC_E_PMKID_CACHE);
2158         setbit(dhdp->eventmask, WLC_E_TXFAIL);
2159         setbit(dhdp->eventmask, WLC_E_JOIN_START);
2160         setbit(dhdp->eventmask, WLC_E_SCAN_COMPLETE);
2161 #ifdef PNO_SUPPORT
2162         setbit(dhdp->eventmask, WLC_E_PFN_NET_FOUND);
2163 #endif                          /* PNO_SUPPORT */
2164
2165 /* enable dongle roaming event */
2166
2167         dhdp->pktfilter_count = 1;
2168         /* Setup filter to allow only unicast */
2169         dhdp->pktfilter[0] = "100 0 0 0 0x01 0x00";
2170 #endif                          /* EMBEDDED_PLATFORM */
2171
2172         /* Bus is ready, do any protocol initialization */
2173         ret = dhd_prot_init(&dhd->pub);
2174         if (ret < 0)
2175                 return ret;
2176
2177         return 0;
2178 }
2179
2180 int
2181 dhd_iovar(dhd_pub_t *pub, int ifidx, char *name, char *cmd_buf, uint cmd_len,
2182           int set)
2183 {
2184         char buf[strlen(name) + 1 + cmd_len];
2185         int len = sizeof(buf);
2186         wl_ioctl_t ioc;
2187         int ret;
2188
2189         len = bcm_mkiovar(name, cmd_buf, cmd_len, buf, len);
2190
2191         memset(&ioc, 0, sizeof(ioc));
2192
2193         ioc.cmd = set ? WLC_SET_VAR : WLC_GET_VAR;
2194         ioc.buf = buf;
2195         ioc.len = len;
2196         ioc.set = set;
2197
2198         ret = dhd_prot_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
2199         if (!set && ret >= 0)
2200                 memcpy(cmd_buf, buf, cmd_len);
2201
2202         return ret;
2203 }
2204
2205 static struct net_device_ops dhd_ops_pri = {
2206         .ndo_open = dhd_open,
2207         .ndo_stop = dhd_stop,
2208         .ndo_get_stats = dhd_get_stats,
2209         .ndo_do_ioctl = dhd_ioctl_entry,
2210         .ndo_start_xmit = dhd_start_xmit,
2211         .ndo_set_mac_address = dhd_set_mac_address,
2212         .ndo_set_multicast_list = dhd_set_multicast_list
2213 };
2214
2215 static struct net_device_ops dhd_ops_virt = {
2216         .ndo_get_stats = dhd_get_stats,
2217         .ndo_do_ioctl = dhd_ioctl_entry,
2218         .ndo_start_xmit = dhd_start_xmit,
2219         .ndo_set_mac_address = dhd_set_mac_address,
2220         .ndo_set_multicast_list = dhd_set_multicast_list
2221 };
2222
2223 int dhd_net_attach(dhd_pub_t *dhdp, int ifidx)
2224 {
2225         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
2226         struct net_device *net;
2227         u8 temp_addr[ETHER_ADDR_LEN] = {
2228                 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
2229
2230         DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
2231
2232         ASSERT(dhd && dhd->iflist[ifidx]);
2233
2234         net = dhd->iflist[ifidx]->net;
2235         ASSERT(net);
2236
2237         ASSERT(!net->netdev_ops);
2238         net->netdev_ops = &dhd_ops_virt;
2239
2240         net->netdev_ops = &dhd_ops_pri;
2241
2242         /*
2243          * We have to use the primary MAC for virtual interfaces
2244          */
2245         if (ifidx != 0) {
2246                 /* for virtual interfaces use the primary MAC  */
2247                 memcpy(temp_addr, dhd->pub.mac.octet, ETHER_ADDR_LEN);
2248
2249         }
2250
2251         if (ifidx == 1) {
2252                 DHD_TRACE(("%s ACCESS POINT MAC: \n", __func__));
2253                 /*  ACCESSPOINT INTERFACE CASE */
2254                 temp_addr[0] |= 0X02;   /* set bit 2 ,
2255                          - Locally Administered address  */
2256
2257         }
2258         net->hard_header_len = ETH_HLEN + dhd->pub.hdrlen;
2259         net->ethtool_ops = &dhd_ethtool_ops;
2260
2261 #if defined(CONFIG_WIRELESS_EXT)
2262 #if defined(CONFIG_CFG80211)
2263         if (!IS_CFG80211_FAVORITE()) {
2264 #endif
2265 #if WIRELESS_EXT < 19
2266                 net->get_wireless_stats = dhd_get_wireless_stats;
2267 #endif                          /* WIRELESS_EXT < 19 */
2268 #if WIRELESS_EXT > 12
2269                 net->wireless_handlers =
2270                     (struct iw_handler_def *)&wl_iw_handler_def;
2271 #endif                          /* WIRELESS_EXT > 12 */
2272 #if defined(CONFIG_CFG80211)
2273         }
2274 #endif
2275 #endif                          /* defined(CONFIG_WIRELESS_EXT) */
2276
2277         dhd->pub.rxsz = net->mtu + net->hard_header_len + dhd->pub.hdrlen;
2278
2279         memcpy(net->dev_addr, temp_addr, ETHER_ADDR_LEN);
2280
2281         if (register_netdev(net) != 0) {
2282                 DHD_ERROR(("%s: couldn't register the net device\n",
2283                         __func__));
2284                 goto fail;
2285         }
2286
2287         printf("%s: Broadcom Dongle Host Driver\n", net->name);
2288
2289         return 0;
2290
2291 fail:
2292         net->netdev_ops = NULL;
2293         return BCME_ERROR;
2294 }
2295
2296 void dhd_bus_detach(dhd_pub_t *dhdp)
2297 {
2298         dhd_info_t *dhd;
2299
2300         DHD_TRACE(("%s: Enter\n", __func__));
2301
2302         if (dhdp) {
2303                 dhd = (dhd_info_t *) dhdp->info;
2304                 if (dhd) {
2305                         /* Stop the protocol module */
2306                         dhd_prot_stop(&dhd->pub);
2307
2308                         /* Stop the bus module */
2309                         dhd_bus_stop(dhd->pub.bus, TRUE);
2310 #if defined(OOB_INTR_ONLY)
2311                         bcmsdh_unregister_oob_intr();
2312 #endif                          /* defined(OOB_INTR_ONLY) */
2313
2314                         /* Clear the watchdog timer */
2315                         del_timer_sync(&dhd->timer);
2316                         dhd->wd_timer_valid = FALSE;
2317                 }
2318         }
2319 }
2320
2321 void dhd_detach(dhd_pub_t *dhdp)
2322 {
2323         dhd_info_t *dhd;
2324
2325         DHD_TRACE(("%s: Enter\n", __func__));
2326
2327         if (dhdp) {
2328                 dhd = (dhd_info_t *) dhdp->info;
2329                 if (dhd) {
2330                         dhd_if_t *ifp;
2331                         int i;
2332
2333 #if defined(CONFIG_HAS_EARLYSUSPEND)
2334                         if (dhd->early_suspend.suspend)
2335                                 unregister_early_suspend(&dhd->early_suspend);
2336 #endif                          /* defined(CONFIG_HAS_EARLYSUSPEND) */
2337
2338                         for (i = 1; i < DHD_MAX_IFS; i++)
2339                                 if (dhd->iflist[i])
2340                                         dhd_del_if(dhd, i);
2341
2342                         ifp = dhd->iflist[0];
2343                         ASSERT(ifp);
2344                         if (ifp->net->netdev_ops == &dhd_ops_pri) {
2345                                 dhd_stop(ifp->net);
2346                                 unregister_netdev(ifp->net);
2347                         }
2348
2349                         if (dhd->watchdog_pid >= 0) {
2350                                 KILL_PROC(dhd->watchdog_pid, SIGTERM);
2351                                 wait_for_completion(&dhd->watchdog_exited);
2352                         }
2353
2354                         if (dhd->dpc_pid >= 0) {
2355                                 KILL_PROC(dhd->dpc_pid, SIGTERM);
2356                                 wait_for_completion(&dhd->dpc_exited);
2357                         } else
2358                                 tasklet_kill(&dhd->tasklet);
2359
2360                         if (dhd->sysioc_pid >= 0) {
2361                                 KILL_PROC(dhd->sysioc_pid, SIGTERM);
2362                                 wait_for_completion(&dhd->sysioc_exited);
2363                         }
2364
2365                         dhd_bus_detach(dhdp);
2366
2367                         if (dhdp->prot)
2368                                 dhd_prot_detach(dhdp);
2369
2370 #if defined(CONFIG_WIRELESS_EXT)
2371                         wl_iw_detach();
2372 #endif                          /* (CONFIG_WIRELESS_EXT) */
2373
2374 #ifdef CONFIG_CFG80211
2375                         if (IS_CFG80211_FAVORITE())
2376                                 wl_cfg80211_detach();
2377 #endif                          /* CONFIG_CFG80211 */
2378
2379 #if defined(CONFIG_PM_SLEEP)
2380                         unregister_pm_notifier(&dhd_sleep_pm_notifier);
2381 #endif  /* defined(CONFIG_PM_SLEEP) */
2382                         /* && defined(DHD_GPL) */
2383                         WAKE_LOCK_DESTROY(dhdp, WAKE_LOCK_TMOUT);
2384                         WAKE_LOCK_DESTROY(dhdp, WAKE_LOCK_LINK_DOWN_TMOUT);
2385                         WAKE_LOCK_DESTROY(dhdp, WAKE_LOCK_PNO_FIND_TMOUT);
2386                         free_netdev(ifp->net);
2387                         MFREE(dhd->pub.osh, ifp, sizeof(*ifp));
2388                         MFREE(dhd->pub.osh, dhd, sizeof(*dhd));
2389                 }
2390         }
2391 }
2392
2393 static void __exit dhd_module_cleanup(void)
2394 {
2395         DHD_TRACE(("%s: Enter\n", __func__));
2396
2397         dhd_bus_unregister();
2398 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2399         wifi_del_dev();
2400 #endif
2401         /* Call customer gpio to turn off power with WL_REG_ON signal */
2402         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
2403 }
2404
2405 static int __init dhd_module_init(void)
2406 {
2407         int error;
2408
2409         DHD_TRACE(("%s: Enter\n", __func__));
2410
2411         /* Sanity check on the module parameters */
2412         do {
2413                 /* Both watchdog and DPC as tasklets are ok */
2414                 if ((dhd_watchdog_prio < 0) && (dhd_dpc_prio < 0))
2415                         break;
2416
2417                 /* If both watchdog and DPC are threads, TX must be deferred */
2418                 if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0)
2419                     && dhd_deferred_tx)
2420                         break;
2421
2422                 DHD_ERROR(("Invalid module parameters.\n"));
2423                 return -EINVAL;
2424         } while (0);
2425         /* Call customer gpio to turn on power with WL_REG_ON signal */
2426         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_ON);
2427
2428 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2429         sema_init(&wifi_control_sem, 0);
2430
2431         error = wifi_add_dev();
2432         if (error) {
2433                 DHD_ERROR(("%s: platform_driver_register failed\n", __func__));
2434                 goto faild;
2435         }
2436
2437         /* Waiting callback after platform_driver_register is done or
2438                  exit with error */
2439         if (down_timeout(&wifi_control_sem, msecs_to_jiffies(1000)) != 0) {
2440                 printk(KERN_ERR "%s: platform_driver_register timeout\n",
2441                         __func__);
2442                 /* remove device */
2443                 wifi_del_dev();
2444                 goto faild;
2445         }
2446 #endif  /* #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC) */
2447
2448         error = dhd_bus_register();
2449
2450         if (!error)
2451                 printf("\n%s\n", dhd_version);
2452         else {
2453                 DHD_ERROR(("%s: sdio_register_driver failed\n", __func__));
2454                 goto faild;
2455         }
2456         return error;
2457
2458 faild:
2459         /* turn off power and exit */
2460         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
2461         return -EINVAL;
2462 }
2463
2464 module_init(dhd_module_init);
2465 module_exit(dhd_module_cleanup);
2466
2467 /*
2468  * OS specific functions required to implement DHD driver in OS independent way
2469  */
2470 int dhd_os_proto_block(dhd_pub_t *pub)
2471 {
2472         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2473
2474         if (dhd) {
2475                 down(&dhd->proto_sem);
2476                 return 1;
2477         }
2478         return 0;
2479 }
2480
2481 int dhd_os_proto_unblock(dhd_pub_t *pub)
2482 {
2483         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2484
2485         if (dhd) {
2486                 up(&dhd->proto_sem);
2487                 return 1;
2488         }
2489
2490         return 0;
2491 }
2492
2493 unsigned int dhd_os_get_ioctl_resp_timeout(void)
2494 {
2495         return (unsigned int)dhd_ioctl_timeout_msec;
2496 }
2497
2498 void dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
2499 {
2500         dhd_ioctl_timeout_msec = (int)timeout_msec;
2501 }
2502
2503 int dhd_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition, bool *pending)
2504 {
2505         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2506         DECLARE_WAITQUEUE(wait, current);
2507         int timeout = dhd_ioctl_timeout_msec;
2508
2509         /* Convert timeout in millsecond to jiffies */
2510         timeout = timeout * HZ / 1000;
2511
2512         /* Wait until control frame is available */
2513         add_wait_queue(&dhd->ioctl_resp_wait, &wait);
2514         set_current_state(TASK_INTERRUPTIBLE);
2515
2516         while (!(*condition) && (!signal_pending(current) && timeout))
2517                 timeout = schedule_timeout(timeout);
2518
2519         if (signal_pending(current))
2520                 *pending = TRUE;
2521
2522         set_current_state(TASK_RUNNING);
2523         remove_wait_queue(&dhd->ioctl_resp_wait, &wait);
2524
2525         return timeout;
2526 }
2527
2528 int dhd_os_ioctl_resp_wake(dhd_pub_t *pub)
2529 {
2530         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2531
2532         if (waitqueue_active(&dhd->ioctl_resp_wait))
2533                 wake_up_interruptible(&dhd->ioctl_resp_wait);
2534
2535         return 0;
2536 }
2537
2538 void dhd_os_wd_timer(void *bus, uint wdtick)
2539 {
2540         dhd_pub_t *pub = bus;
2541         static uint save_dhd_watchdog_ms = 0;
2542         dhd_info_t *dhd = (dhd_info_t *) pub->info;
2543
2544         /* don't start the wd until fw is loaded */
2545         if (pub->busstate == DHD_BUS_DOWN)
2546                 return;
2547
2548         /* Totally stop the timer */
2549         if (!wdtick && dhd->wd_timer_valid == TRUE) {
2550                 del_timer_sync(&dhd->timer);
2551                 dhd->wd_timer_valid = FALSE;
2552                 save_dhd_watchdog_ms = wdtick;
2553                 return;
2554         }
2555
2556         if (wdtick) {
2557                 dhd_watchdog_ms = (uint) wdtick;
2558
2559                 if (save_dhd_watchdog_ms != dhd_watchdog_ms) {
2560
2561                         if (dhd->wd_timer_valid == TRUE)
2562                                 /* Stop timer and restart at new value */
2563                                 del_timer_sync(&dhd->timer);
2564
2565                         /* Create timer again when watchdog period is
2566                            dynamically changed or in the first instance
2567                          */
2568                         dhd->timer.expires =
2569                             jiffies + dhd_watchdog_ms * HZ / 1000;
2570                         add_timer(&dhd->timer);
2571
2572                 } else {
2573                         /* Re arm the timer, at last watchdog period */
2574                         mod_timer(&dhd->timer,
2575                                   jiffies + dhd_watchdog_ms * HZ / 1000);
2576                 }
2577
2578                 dhd->wd_timer_valid = TRUE;
2579                 save_dhd_watchdog_ms = wdtick;
2580         }
2581 }
2582
2583 void *dhd_os_open_image(char *filename)
2584 {
2585         struct file *fp;
2586
2587 #ifdef CONFIG_CFG80211
2588         if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2589                 return wl_cfg80211_request_fw(filename);
2590 #endif
2591
2592         fp = filp_open(filename, O_RDONLY, 0);
2593         /*
2594          * 2.6.11 (FC4) supports filp_open() but later revs don't?
2595          * Alternative:
2596          * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
2597          * ???
2598          */
2599         if (IS_ERR(fp))
2600                 fp = NULL;
2601
2602         return fp;
2603 }
2604
2605 int dhd_os_get_image_block(char *buf, int len, void *image)
2606 {
2607         struct file *fp = (struct file *)image;
2608         int rdlen;
2609
2610 #ifdef CONFIG_CFG80211
2611         if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2612                 return wl_cfg80211_read_fw(buf, len);
2613 #endif
2614
2615         if (!image)
2616                 return 0;
2617
2618         rdlen = kernel_read(fp, fp->f_pos, buf, len);
2619         if (rdlen > 0)
2620                 fp->f_pos += rdlen;
2621
2622         return rdlen;
2623 }
2624
2625 void dhd_os_close_image(void *image)
2626 {
2627 #ifdef CONFIG_CFG80211
2628         if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2629                 return wl_cfg80211_release_fw();
2630 #endif
2631         if (image)
2632                 filp_close((struct file *)image, NULL);
2633 }
2634
2635 void dhd_os_sdlock(dhd_pub_t *pub)
2636 {
2637         dhd_info_t *dhd;
2638
2639         dhd = (dhd_info_t *) (pub->info);
2640
2641         if (dhd->threads_only)
2642                 down(&dhd->sdsem);
2643         else
2644                 spin_lock_bh(&dhd->sdlock);
2645 }
2646
2647 void dhd_os_sdunlock(dhd_pub_t *pub)
2648 {
2649         dhd_info_t *dhd;
2650
2651         dhd = (dhd_info_t *) (pub->info);
2652
2653         if (dhd->threads_only)
2654                 up(&dhd->sdsem);
2655         else
2656                 spin_unlock_bh(&dhd->sdlock);
2657 }
2658
2659 void dhd_os_sdlock_txq(dhd_pub_t *pub)
2660 {
2661         dhd_info_t *dhd;
2662
2663         dhd = (dhd_info_t *) (pub->info);
2664         spin_lock_bh(&dhd->txqlock);
2665 }
2666
2667 void dhd_os_sdunlock_txq(dhd_pub_t *pub)
2668 {
2669         dhd_info_t *dhd;
2670
2671         dhd = (dhd_info_t *) (pub->info);
2672         spin_unlock_bh(&dhd->txqlock);
2673 }
2674
2675 void dhd_os_sdlock_rxq(dhd_pub_t *pub)
2676 {
2677 }
2678
2679 void dhd_os_sdunlock_rxq(dhd_pub_t *pub)
2680 {
2681 }
2682
2683 void dhd_os_sdtxlock(dhd_pub_t *pub)
2684 {
2685         dhd_os_sdlock(pub);
2686 }
2687
2688 void dhd_os_sdtxunlock(dhd_pub_t *pub)
2689 {
2690         dhd_os_sdunlock(pub);
2691 }
2692
2693 #ifdef DHD_USE_STATIC_BUF
2694 void *dhd_os_prealloc(int section, unsigned long size)
2695 {
2696 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2697         void *alloc_ptr = NULL;
2698         if (wifi_control_data && wifi_control_data->mem_prealloc) {
2699                 alloc_ptr = wifi_control_data->mem_prealloc(section, size);
2700                 if (alloc_ptr) {
2701                         DHD_INFO(("success alloc section %d\n", section));
2702                         bzero(alloc_ptr, size);
2703                         return alloc_ptr;
2704                 }
2705         }
2706
2707         DHD_ERROR(("can't alloc section %d\n", section));
2708         return 0;
2709 #else
2710         return MALLOC(0, size);
2711 #endif  /* #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC) */
2712 }
2713 #endif  /* DHD_USE_STATIC_BUF */
2714 #if defined(CONFIG_WIRELESS_EXT)
2715 struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev)
2716 {
2717         int res = 0;
2718         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2719
2720         res = wl_iw_get_wireless_stats(dev, &dhd->iw.wstats);
2721
2722         if (res == 0)
2723                 return &dhd->iw.wstats;
2724         else
2725                 return NULL;
2726 }
2727 #endif  /* defined(CONFIG_WIRELESS_EXT) */
2728
2729 static int
2730 dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
2731                   wl_event_msg_t *event, void **data)
2732 {
2733         int bcmerror = 0;
2734
2735         ASSERT(dhd != NULL);
2736
2737         bcmerror = wl_host_event(dhd, ifidx, pktdata, event, data);
2738         if (bcmerror != BCME_OK)
2739                 return bcmerror;
2740
2741 #if defined(CONFIG_WIRELESS_EXT)
2742 #if defined(CONFIG_CFG80211)
2743         if (!IS_CFG80211_FAVORITE()) {
2744 #endif
2745                 if ((dhd->iflist[*ifidx] == NULL)
2746                     || (dhd->iflist[*ifidx]->net == NULL)) {
2747                         DHD_ERROR(("%s Exit null pointer\n", __func__));
2748                         return bcmerror;
2749                 }
2750
2751                 if (dhd->iflist[*ifidx]->net)
2752                         wl_iw_event(dhd->iflist[*ifidx]->net, event, *data);
2753 #if defined(CONFIG_CFG80211)
2754         }
2755 #endif
2756 #endif                          /* defined(CONFIG_WIRELESS_EXT)  */
2757
2758 #ifdef CONFIG_CFG80211
2759         if (IS_CFG80211_FAVORITE()) {
2760                 ASSERT(dhd->iflist[*ifidx] != NULL);
2761                 ASSERT(dhd->iflist[*ifidx]->net != NULL);
2762                 if (dhd->iflist[*ifidx]->net)
2763                         wl_cfg80211_event(dhd->iflist[*ifidx]->net, event,
2764                                           *data);
2765         }
2766 #endif
2767
2768         return bcmerror;
2769 }
2770
2771 /* send up locally generated event */
2772 void dhd_sendup_event(dhd_pub_t *dhdp, wl_event_msg_t *event, void *data)
2773 {
2774         switch (ntoh32(event->event_type)) {
2775         default:
2776                 break;
2777         }
2778 }
2779
2780 void dhd_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
2781 {
2782         struct dhd_info *dhdinfo = dhd->info;
2783         dhd_os_sdunlock(dhd);
2784         wait_event_interruptible_timeout(dhdinfo->ctrl_wait,
2785                                          (*lockvar == FALSE), HZ * 2);
2786         dhd_os_sdlock(dhd);
2787         return;
2788 }
2789
2790 void dhd_wait_event_wakeup(dhd_pub_t *dhd)
2791 {
2792         struct dhd_info *dhdinfo = dhd->info;
2793         if (waitqueue_active(&dhdinfo->ctrl_wait))
2794                 wake_up_interruptible(&dhdinfo->ctrl_wait);
2795         return;
2796 }
2797
2798 int dhd_dev_reset(struct net_device *dev, u8 flag)
2799 {
2800         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2801
2802         /* Turning off watchdog */
2803         if (flag)
2804                 dhd_os_wd_timer(&dhd->pub, 0);
2805
2806         dhd_bus_devreset(&dhd->pub, flag);
2807
2808         /* Turning on watchdog back */
2809         if (!flag)
2810                 dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
2811         DHD_ERROR(("%s:  WLAN OFF DONE\n", __func__));
2812
2813         return 1;
2814 }
2815
2816 int net_os_set_suspend_disable(struct net_device *dev, int val)
2817 {
2818         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2819         int ret = 0;
2820
2821         if (dhd) {
2822                 ret = dhd->pub.suspend_disable_flag;
2823                 dhd->pub.suspend_disable_flag = val;
2824         }
2825         return ret;
2826 }
2827
2828 int net_os_set_suspend(struct net_device *dev, int val)
2829 {
2830         int ret = 0;
2831 #if defined(CONFIG_HAS_EARLYSUSPEND)
2832         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2833
2834         if (dhd) {
2835                 dhd_os_proto_block(&dhd->pub);
2836                 ret = dhd_set_suspend(val, &dhd->pub);
2837                 dhd_os_proto_unblock(&dhd->pub);
2838         }
2839 #endif          /* defined(CONFIG_HAS_EARLYSUSPEND) */
2840         return ret;
2841 }
2842
2843 int net_os_set_dtim_skip(struct net_device *dev, int val)
2844 {
2845         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2846
2847         if (dhd)
2848                 dhd->pub.dtim_skip = val;
2849
2850         return 0;
2851 }
2852
2853 int net_os_set_packet_filter(struct net_device *dev, int val)
2854 {
2855         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2856         int ret = 0;
2857
2858         /* Packet filtering is set only if we still in early-suspend and
2859          * we need either to turn it ON or turn it OFF
2860          * We can always turn it OFF in case of early-suspend, but we turn it
2861          * back ON only if suspend_disable_flag was not set
2862          */
2863         if (dhd && dhd->pub.up) {
2864                 dhd_os_proto_block(&dhd->pub);
2865                 if (dhd->pub.in_suspend) {
2866                         if (!val || (val && !dhd->pub.suspend_disable_flag))
2867                                 dhd_set_packet_filter(val, &dhd->pub);
2868                 }
2869                 dhd_os_proto_unblock(&dhd->pub);
2870         }
2871         return ret;
2872 }
2873
2874 void dhd_dev_init_ioctl(struct net_device *dev)
2875 {
2876         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2877
2878         dhd_preinit_ioctls(&dhd->pub);
2879 }
2880
2881 #ifdef PNO_SUPPORT
2882 /* Linux wrapper to call common dhd_pno_clean */
2883 int dhd_dev_pno_reset(struct net_device *dev)
2884 {
2885         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2886
2887         return dhd_pno_clean(&dhd->pub);
2888 }
2889
2890 /* Linux wrapper to call common dhd_pno_enable */
2891 int dhd_dev_pno_enable(struct net_device *dev, int pfn_enabled)
2892 {
2893         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2894
2895         return dhd_pno_enable(&dhd->pub, pfn_enabled);
2896 }
2897
2898 /* Linux wrapper to call common dhd_pno_set */
2899 int
2900 dhd_dev_pno_set(struct net_device *dev, wlc_ssid_t *ssids_local, int nssid,
2901                 unsigned char scan_fr)
2902 {
2903         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2904
2905         return dhd_pno_set(&dhd->pub, ssids_local, nssid, scan_fr);
2906 }
2907
2908 /* Linux wrapper to get  pno status */
2909 int dhd_dev_get_pno_status(struct net_device *dev)
2910 {
2911         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2912
2913         return dhd_pno_get_status(&dhd->pub);
2914 }
2915
2916 #endif                          /* PNO_SUPPORT */
2917
2918 static int dhd_get_pend_8021x_cnt(dhd_info_t *dhd)
2919 {
2920         return atomic_read(&dhd->pend_8021x_cnt);
2921 }
2922
2923 #define MAX_WAIT_FOR_8021X_TX   10
2924
2925 int dhd_wait_pend8021x(struct net_device *dev)
2926 {
2927         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2928         int timeout = 10 * HZ / 1000;
2929         int ntimes = MAX_WAIT_FOR_8021X_TX;
2930         int pend = dhd_get_pend_8021x_cnt(dhd);
2931
2932         while (ntimes && pend) {
2933                 if (pend) {
2934                         set_current_state(TASK_INTERRUPTIBLE);
2935                         schedule_timeout(timeout);
2936                         set_current_state(TASK_RUNNING);
2937                         ntimes--;
2938                 }
2939                 pend = dhd_get_pend_8021x_cnt(dhd);
2940         }
2941         return pend;
2942 }
2943
2944 #ifdef DHD_DEBUG
2945 int write_to_file(dhd_pub_t *dhd, u8 *buf, int size)
2946 {
2947         int ret = 0;
2948         struct file *fp;
2949         mm_segment_t old_fs;
2950         loff_t pos = 0;
2951
2952         /* change to KERNEL_DS address limit */
2953         old_fs = get_fs();
2954         set_fs(KERNEL_DS);
2955
2956         /* open file to write */
2957         fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
2958         if (!fp) {
2959                 printf("%s: open file error\n", __func__);
2960                 ret = -1;
2961                 goto exit;
2962         }
2963
2964         /* Write buf to file */
2965         fp->f_op->write(fp, buf, size, &pos);
2966
2967 exit:
2968         /* free buf before return */
2969         MFREE(dhd->osh, buf, size);
2970         /* close file before return */
2971         if (fp)
2972                 filp_close(fp, current->files);
2973         /* restore previous address limit */
2974         set_fs(old_fs);
2975
2976         return ret;
2977 }
2978 #endif                          /* DHD_DEBUG */