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