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