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