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