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