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