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