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