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