staging: brcm80211: replace MFREE with kfree
[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                 udelay(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 = kmalloc(buflen, GFP_ATOMIC);
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         kfree(buf);
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 = kmalloc(buflen, GFP_ATOMIC);
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                 kfree(buf);
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         kfree(buf);
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                 kfree(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         allow_signal(SIGTERM);
942
943         while (down_interruptible(&dhd->sysioc_sem) == 0) {
944                 if (kthread_should_stop())
945                         break;
946                 for (i = 0; i < DHD_MAX_IFS; i++) {
947                         if (dhd->iflist[i]) {
948 #ifdef SOFTAP
949                                 in_ap = (ap_net_dev != NULL);
950 #endif                          /* SOFTAP */
951                                 if (dhd->iflist[i]->state)
952                                         dhd_op_if(dhd->iflist[i]);
953 #ifdef SOFTAP
954                                 if (dhd->iflist[i] == NULL) {
955                                         DHD_TRACE(("\n\n %s: interface %d "
956                                                 "removed!\n", __func__, i));
957                                         continue;
958                                 }
959
960                                 if (in_ap && dhd->set_macaddress) {
961                                         DHD_TRACE(("attempt to set MAC for %s "
962                                                 "in AP Mode," "blocked. \n",
963                                                 dhd->iflist[i]->net->name));
964                                         dhd->set_macaddress = false;
965                                         continue;
966                                 }
967
968                                 if (in_ap && dhd->set_multicast) {
969                                         DHD_TRACE(("attempt to set MULTICAST list for %s" "in AP Mode, blocked. \n",
970                                                 dhd->iflist[i]->net->name));
971                                         dhd->set_multicast = false;
972                                         continue;
973                                 }
974 #endif                          /* SOFTAP */
975                                 if (dhd->set_multicast) {
976                                         dhd->set_multicast = false;
977                                         _dhd_set_multicast_list(dhd, i);
978                                 }
979                                 if (dhd->set_macaddress) {
980                                         dhd->set_macaddress = false;
981                                         _dhd_set_mac_address(dhd, i,
982                                                              &dhd->macvalue);
983                                 }
984                         }
985                 }
986         }
987         return 0;
988 }
989
990 static int dhd_set_mac_address(struct net_device *dev, void *addr)
991 {
992         int ret = 0;
993
994         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
995         struct sockaddr *sa = (struct sockaddr *)addr;
996         int ifidx;
997
998         ifidx = dhd_net2idx(dhd, dev);
999         if (ifidx == DHD_BAD_IF)
1000                 return -1;
1001
1002         ASSERT(dhd->sysioc_tsk);
1003         memcpy(&dhd->macvalue, sa->sa_data, ETHER_ADDR_LEN);
1004         dhd->set_macaddress = true;
1005         up(&dhd->sysioc_sem);
1006
1007         return ret;
1008 }
1009
1010 static void dhd_set_multicast_list(struct net_device *dev)
1011 {
1012         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
1013         int ifidx;
1014
1015         ifidx = dhd_net2idx(dhd, dev);
1016         if (ifidx == DHD_BAD_IF)
1017                 return;
1018
1019         ASSERT(dhd->sysioc_tsk);
1020         dhd->set_multicast = true;
1021         up(&dhd->sysioc_sem);
1022 }
1023
1024 int dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, void *pktbuf)
1025 {
1026         int ret;
1027         dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
1028
1029         /* Reject if down */
1030         if (!dhdp->up || (dhdp->busstate == DHD_BUS_DOWN))
1031                 return -ENODEV;
1032
1033         /* Update multicast statistic */
1034         if (PKTLEN(pktbuf) >= ETHER_ADDR_LEN) {
1035                 u8 *pktdata = (u8 *) PKTDATA(pktbuf);
1036                 struct ether_header *eh = (struct ether_header *)pktdata;
1037
1038                 if (ETHER_ISMULTI(eh->ether_dhost))
1039                         dhdp->tx_multicast++;
1040                 if (ntoh16(eh->ether_type) == ETHER_TYPE_802_1X)
1041                         atomic_inc(&dhd->pend_8021x_cnt);
1042         }
1043
1044         /* Look into the packet and update the packet priority */
1045         if ((PKTPRIO(pktbuf) == 0))
1046                 pktsetprio(pktbuf, false);
1047
1048         /* If the protocol uses a data header, apply it */
1049         dhd_prot_hdrpush(dhdp, ifidx, pktbuf);
1050
1051         /* Use bus module to send data frame */
1052 #ifdef BCMDBUS
1053         ret = dbus_send_pkt(dhdp->dbus, pktbuf, NULL /* pktinfo */);
1054 #else
1055         WAKE_LOCK_TIMEOUT(dhdp, WAKE_LOCK_TMOUT, 25);
1056         ret = dhd_bus_txdata(dhdp->bus, pktbuf);
1057 #endif                          /* BCMDBUS */
1058
1059         return ret;
1060 }
1061
1062 static int dhd_start_xmit(struct sk_buff *skb, struct net_device *net)
1063 {
1064         int ret;
1065         void *pktbuf;
1066         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1067         int ifidx;
1068
1069         DHD_TRACE(("%s: Enter\n", __func__));
1070
1071         /* Reject if down */
1072         if (!dhd->pub.up || (dhd->pub.busstate == DHD_BUS_DOWN)) {
1073                 DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d\n",
1074                            __func__, dhd->pub.up, dhd->pub.busstate));
1075                 netif_stop_queue(net);
1076                 return -ENODEV;
1077         }
1078
1079         ifidx = dhd_net2idx(dhd, net);
1080         if (ifidx == DHD_BAD_IF) {
1081                 DHD_ERROR(("%s: bad ifidx %d\n", __func__, ifidx));
1082                 netif_stop_queue(net);
1083                 return -ENODEV;
1084         }
1085
1086         /* Make sure there's enough room for any header */
1087         if (skb_headroom(skb) < dhd->pub.hdrlen) {
1088                 struct sk_buff *skb2;
1089
1090                 DHD_INFO(("%s: insufficient headroom\n",
1091                           dhd_ifname(&dhd->pub, ifidx)));
1092                 dhd->pub.tx_realloc++;
1093                 skb2 = skb_realloc_headroom(skb, dhd->pub.hdrlen);
1094                 dev_kfree_skb(skb);
1095                 skb = skb2;
1096                 if (skb == NULL) {
1097                         DHD_ERROR(("%s: skb_realloc_headroom failed\n",
1098                                    dhd_ifname(&dhd->pub, ifidx)));
1099                         ret = -ENOMEM;
1100                         goto done;
1101                 }
1102         }
1103
1104         /* Convert to packet */
1105         pktbuf = PKTFRMNATIVE(dhd->pub.osh, skb);
1106         if (!pktbuf) {
1107                 DHD_ERROR(("%s: PKTFRMNATIVE failed\n",
1108                            dhd_ifname(&dhd->pub, ifidx)));
1109                 dev_kfree_skb_any(skb);
1110                 ret = -ENOMEM;
1111                 goto done;
1112         }
1113
1114         ret = dhd_sendpkt(&dhd->pub, ifidx, pktbuf);
1115
1116 done:
1117         if (ret)
1118                 dhd->pub.dstats.tx_dropped++;
1119         else
1120                 dhd->pub.tx_packets++;
1121
1122         /* Return ok: we always eat the packet */
1123         return 0;
1124 }
1125
1126 void dhd_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool state)
1127 {
1128         struct net_device *net;
1129         dhd_info_t *dhd = dhdp->info;
1130
1131         DHD_TRACE(("%s: Enter\n", __func__));
1132
1133         dhdp->txoff = state;
1134         ASSERT(dhd && dhd->iflist[ifidx]);
1135         net = dhd->iflist[ifidx]->net;
1136         if (state == ON)
1137                 netif_stop_queue(net);
1138         else
1139                 netif_wake_queue(net);
1140 }
1141
1142 void dhd_rx_frame(dhd_pub_t *dhdp, int ifidx, void *pktbuf, int numpkt)
1143 {
1144         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1145         struct sk_buff *skb;
1146         unsigned char *eth;
1147         uint len;
1148         void *data, *pnext, *save_pktbuf;
1149         int i;
1150         dhd_if_t *ifp;
1151         wl_event_msg_t event;
1152
1153         DHD_TRACE(("%s: Enter\n", __func__));
1154
1155         save_pktbuf = pktbuf;
1156
1157         for (i = 0; pktbuf && i < numpkt; i++, pktbuf = pnext) {
1158
1159                 pnext = PKTNEXT(pktbuf);
1160                 PKTSETNEXT(pktbuf, NULL);
1161
1162                 skb = PKTTONATIVE(dhdp->osh, pktbuf);
1163
1164                 /* Get the protocol, maintain skb around eth_type_trans()
1165                  * The main reason for this hack is for the limitation of
1166                  * Linux 2.4 where 'eth_type_trans' uses the
1167                  * 'net->hard_header_len'
1168                  * to perform skb_pull inside vs ETH_HLEN. Since to avoid
1169                  * coping of the packet coming from the network stack to add
1170                  * BDC, Hardware header etc, during network interface
1171                  * registration
1172                  * we set the 'net->hard_header_len' to ETH_HLEN + extra space
1173                  * required
1174                  * for BDC, Hardware header etc. and not just the ETH_HLEN
1175                  */
1176                 eth = skb->data;
1177                 len = skb->len;
1178
1179                 ifp = dhd->iflist[ifidx];
1180                 if (ifp == NULL)
1181                         ifp = dhd->iflist[0];
1182
1183                 ASSERT(ifp);
1184                 skb->dev = ifp->net;
1185                 skb->protocol = eth_type_trans(skb, skb->dev);
1186
1187                 if (skb->pkt_type == PACKET_MULTICAST)
1188                         dhd->pub.rx_multicast++;
1189
1190                 skb->data = eth;
1191                 skb->len = len;
1192
1193                 /* Strip header, count, deliver upward */
1194                 skb_pull(skb, ETH_HLEN);
1195
1196                 /* Process special event packets and then discard them */
1197                 if (ntoh16(skb->protocol) == ETHER_TYPE_BRCM)
1198                         dhd_wl_host_event(dhd, &ifidx,
1199                                           skb->mac_header,
1200                                           &event, &data);
1201
1202                 ASSERT(ifidx < DHD_MAX_IFS && dhd->iflist[ifidx]);
1203                 if (dhd->iflist[ifidx] && !dhd->iflist[ifidx]->state)
1204                         ifp = dhd->iflist[ifidx];
1205
1206                 if (ifp->net)
1207                         ifp->net->last_rx = jiffies;
1208
1209                 dhdp->dstats.rx_bytes += skb->len;
1210                 dhdp->rx_packets++;     /* Local count */
1211
1212                 if (in_interrupt()) {
1213                         netif_rx(skb);
1214                 } else {
1215                         /* If the receive is not processed inside an ISR,
1216                          * the softirqd must be woken explicitly to service
1217                          * the NET_RX_SOFTIRQ.  In 2.6 kernels, this is handled
1218                          * by netif_rx_ni(), but in earlier kernels, we need
1219                          * to do it manually.
1220                          */
1221                         netif_rx_ni(skb);
1222                 }
1223         }
1224 }
1225
1226 void dhd_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx)
1227 {
1228         /* Linux version has nothing to do */
1229         return;
1230 }
1231
1232 void dhd_txcomplete(dhd_pub_t *dhdp, void *txp, bool success)
1233 {
1234         uint ifidx;
1235         dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
1236         struct ether_header *eh;
1237         u16 type;
1238
1239         dhd_prot_hdrpull(dhdp, &ifidx, txp);
1240
1241         eh = (struct ether_header *)PKTDATA(txp);
1242         type = ntoh16(eh->ether_type);
1243
1244         if (type == ETHER_TYPE_802_1X)
1245                 atomic_dec(&dhd->pend_8021x_cnt);
1246
1247 }
1248
1249 static struct net_device_stats *dhd_get_stats(struct net_device *net)
1250 {
1251         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1252         dhd_if_t *ifp;
1253         int ifidx;
1254
1255         DHD_TRACE(("%s: Enter\n", __func__));
1256
1257         ifidx = dhd_net2idx(dhd, net);
1258         if (ifidx == DHD_BAD_IF)
1259                 return NULL;
1260
1261         ifp = dhd->iflist[ifidx];
1262         ASSERT(dhd && ifp);
1263
1264         if (dhd->pub.up) {
1265                 /* Use the protocol to get dongle stats */
1266                 dhd_prot_dstats(&dhd->pub);
1267         }
1268
1269         /* Copy dongle stats to net device stats */
1270         ifp->stats.rx_packets = dhd->pub.dstats.rx_packets;
1271         ifp->stats.tx_packets = dhd->pub.dstats.tx_packets;
1272         ifp->stats.rx_bytes = dhd->pub.dstats.rx_bytes;
1273         ifp->stats.tx_bytes = dhd->pub.dstats.tx_bytes;
1274         ifp->stats.rx_errors = dhd->pub.dstats.rx_errors;
1275         ifp->stats.tx_errors = dhd->pub.dstats.tx_errors;
1276         ifp->stats.rx_dropped = dhd->pub.dstats.rx_dropped;
1277         ifp->stats.tx_dropped = dhd->pub.dstats.tx_dropped;
1278         ifp->stats.multicast = dhd->pub.dstats.multicast;
1279
1280         return &ifp->stats;
1281 }
1282
1283 static int dhd_watchdog_thread(void *data)
1284 {
1285         dhd_info_t *dhd = (dhd_info_t *) data;
1286         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_WATCHDOG, "dhd_watchdog_thread");
1287
1288         /* This thread doesn't need any user-level access,
1289          * so get rid of all our resources
1290          */
1291 #ifdef DHD_SCHED
1292         if (dhd_watchdog_prio > 0) {
1293                 struct sched_param param;
1294                 param.sched_priority = (dhd_watchdog_prio < MAX_RT_PRIO) ?
1295                     dhd_watchdog_prio : (MAX_RT_PRIO - 1);
1296                 setScheduler(current, SCHED_FIFO, &param);
1297         }
1298 #endif                          /* DHD_SCHED */
1299
1300         allow_signal(SIGTERM);
1301         /* Run until signal received */
1302         while (1) {
1303                 if (kthread_should_stop())
1304                         break;
1305                 if (down_interruptible(&dhd->watchdog_sem) == 0) {
1306                         if (dhd->pub.dongle_reset == false) {
1307                                 WAKE_LOCK(&dhd->pub, WAKE_LOCK_WATCHDOG);
1308                                 /* Call the bus module watchdog */
1309                                 dhd_bus_watchdog(&dhd->pub);
1310                                 WAKE_UNLOCK(&dhd->pub, WAKE_LOCK_WATCHDOG);
1311                         }
1312                         /* Count the tick for reference */
1313                         dhd->pub.tickcnt++;
1314                 } else
1315                         break;
1316         }
1317
1318         WAKE_LOCK_DESTROY(&dhd->pub, WAKE_LOCK_WATCHDOG);
1319         return 0;
1320 }
1321
1322 static void dhd_watchdog(unsigned long data)
1323 {
1324         dhd_info_t *dhd = (dhd_info_t *) data;
1325
1326         if (dhd->watchdog_tsk) {
1327                 up(&dhd->watchdog_sem);
1328
1329                 /* Reschedule the watchdog */
1330                 if (dhd->wd_timer_valid) {
1331                         mod_timer(&dhd->timer,
1332                                   jiffies + dhd_watchdog_ms * HZ / 1000);
1333                 }
1334                 return;
1335         }
1336
1337         /* Call the bus module watchdog */
1338         dhd_bus_watchdog(&dhd->pub);
1339
1340         /* Count the tick for reference */
1341         dhd->pub.tickcnt++;
1342
1343         /* Reschedule the watchdog */
1344         if (dhd->wd_timer_valid)
1345                 mod_timer(&dhd->timer, jiffies + dhd_watchdog_ms * HZ / 1000);
1346 }
1347
1348 static int dhd_dpc_thread(void *data)
1349 {
1350         dhd_info_t *dhd = (dhd_info_t *) data;
1351
1352         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_DPC, "dhd_dpc_thread");
1353         /* This thread doesn't need any user-level access,
1354          * so get rid of all our resources
1355          */
1356 #ifdef DHD_SCHED
1357         if (dhd_dpc_prio > 0) {
1358                 struct sched_param param;
1359                 param.sched_priority =
1360                     (dhd_dpc_prio <
1361                      MAX_RT_PRIO) ? dhd_dpc_prio : (MAX_RT_PRIO - 1);
1362                 setScheduler(current, SCHED_FIFO, &param);
1363         }
1364 #endif                          /* DHD_SCHED */
1365
1366         allow_signal(SIGTERM);
1367         /* Run until signal received */
1368         while (1) {
1369                 if (kthread_should_stop())
1370                         break;
1371                 if (down_interruptible(&dhd->dpc_sem) == 0) {
1372                         /* Call bus dpc unless it indicated down
1373                                  (then clean stop) */
1374                         if (dhd->pub.busstate != DHD_BUS_DOWN) {
1375                                 WAKE_LOCK(&dhd->pub, WAKE_LOCK_DPC);
1376                                 if (dhd_bus_dpc(dhd->pub.bus)) {
1377                                         up(&dhd->dpc_sem);
1378                                         WAKE_LOCK_TIMEOUT(&dhd->pub,
1379                                                           WAKE_LOCK_TMOUT, 25);
1380                                 }
1381                                 WAKE_UNLOCK(&dhd->pub, WAKE_LOCK_DPC);
1382                         } else {
1383                                 dhd_bus_stop(dhd->pub.bus, true);
1384                         }
1385                 } else
1386                         break;
1387         }
1388
1389         WAKE_LOCK_DESTROY(&dhd->pub, WAKE_LOCK_DPC);
1390         return 0;
1391 }
1392
1393 static void dhd_dpc(unsigned long data)
1394 {
1395         dhd_info_t *dhd;
1396
1397         dhd = (dhd_info_t *) data;
1398
1399         /* Call bus dpc unless it indicated down (then clean stop) */
1400         if (dhd->pub.busstate != DHD_BUS_DOWN) {
1401                 if (dhd_bus_dpc(dhd->pub.bus))
1402                         tasklet_schedule(&dhd->tasklet);
1403         } else {
1404                 dhd_bus_stop(dhd->pub.bus, true);
1405         }
1406 }
1407
1408 void dhd_sched_dpc(dhd_pub_t *dhdp)
1409 {
1410         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1411
1412         if (dhd->dpc_tsk) {
1413                 up(&dhd->dpc_sem);
1414                 return;
1415         }
1416
1417         tasklet_schedule(&dhd->tasklet);
1418 }
1419
1420 #ifdef TOE
1421 /* Retrieve current toe component enables, which are kept
1422          as a bitmap in toe_ol iovar */
1423 static int dhd_toe_get(dhd_info_t *dhd, int ifidx, u32 *toe_ol)
1424 {
1425         wl_ioctl_t ioc;
1426         char buf[32];
1427         int ret;
1428
1429         memset(&ioc, 0, sizeof(ioc));
1430
1431         ioc.cmd = WLC_GET_VAR;
1432         ioc.buf = buf;
1433         ioc.len = (uint) sizeof(buf);
1434         ioc.set = false;
1435
1436         strcpy(buf, "toe_ol");
1437         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1438         if (ret < 0) {
1439                 /* Check for older dongle image that doesn't support toe_ol */
1440                 if (ret == -EIO) {
1441                         DHD_ERROR(("%s: toe not supported by device\n",
1442                                    dhd_ifname(&dhd->pub, ifidx)));
1443                         return -EOPNOTSUPP;
1444                 }
1445
1446                 DHD_INFO(("%s: could not get toe_ol: ret=%d\n",
1447                           dhd_ifname(&dhd->pub, ifidx), ret));
1448                 return ret;
1449         }
1450
1451         memcpy(toe_ol, buf, sizeof(u32));
1452         return 0;
1453 }
1454
1455 /* Set current toe component enables in toe_ol iovar,
1456          and set toe global enable iovar */
1457 static int dhd_toe_set(dhd_info_t *dhd, int ifidx, u32 toe_ol)
1458 {
1459         wl_ioctl_t ioc;
1460         char buf[32];
1461         int toe, ret;
1462
1463         memset(&ioc, 0, sizeof(ioc));
1464
1465         ioc.cmd = WLC_SET_VAR;
1466         ioc.buf = buf;
1467         ioc.len = (uint) sizeof(buf);
1468         ioc.set = true;
1469
1470         /* Set toe_ol as requested */
1471
1472         strcpy(buf, "toe_ol");
1473         memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
1474
1475         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1476         if (ret < 0) {
1477                 DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
1478                            dhd_ifname(&dhd->pub, ifidx), ret));
1479                 return ret;
1480         }
1481
1482         /* Enable toe globally only if any components are enabled. */
1483
1484         toe = (toe_ol != 0);
1485
1486         strcpy(buf, "toe");
1487         memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
1488
1489         ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1490         if (ret < 0) {
1491                 DHD_ERROR(("%s: could not set toe: ret=%d\n",
1492                            dhd_ifname(&dhd->pub, ifidx), ret));
1493                 return ret;
1494         }
1495
1496         return 0;
1497 }
1498 #endif                          /* TOE */
1499
1500 static void dhd_ethtool_get_drvinfo(struct net_device *net,
1501                                     struct ethtool_drvinfo *info)
1502 {
1503         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1504
1505         sprintf(info->driver, DRV_MODULE_NAME);
1506         sprintf(info->version, "%lu", dhd->pub.drv_version);
1507         sprintf(info->fw_version, "%s", wl_cfg80211_get_fwname());
1508         sprintf(info->bus_info, "%s", dev_name(&wl_cfg80211_get_sdio_func()->dev));
1509 }
1510
1511 struct ethtool_ops dhd_ethtool_ops = {
1512         .get_drvinfo = dhd_ethtool_get_drvinfo
1513 };
1514
1515 static int dhd_ethtool(dhd_info_t *dhd, void *uaddr)
1516 {
1517         struct ethtool_drvinfo info;
1518         char drvname[sizeof(info.driver)];
1519         u32 cmd;
1520 #ifdef TOE
1521         struct ethtool_value edata;
1522         u32 toe_cmpnt, csum_dir;
1523         int ret;
1524 #endif
1525
1526         DHD_TRACE(("%s: Enter\n", __func__));
1527
1528         /* all ethtool calls start with a cmd word */
1529         if (copy_from_user(&cmd, uaddr, sizeof(u32)))
1530                 return -EFAULT;
1531
1532         switch (cmd) {
1533         case ETHTOOL_GDRVINFO:
1534                 /* Copy out any request driver name */
1535                 if (copy_from_user(&info, uaddr, sizeof(info)))
1536                         return -EFAULT;
1537                 strncpy(drvname, info.driver, sizeof(info.driver));
1538                 drvname[sizeof(info.driver) - 1] = '\0';
1539
1540                 /* clear struct for return */
1541                 memset(&info, 0, sizeof(info));
1542                 info.cmd = cmd;
1543
1544                 /* if dhd requested, identify ourselves */
1545                 if (strcmp(drvname, "?dhd") == 0) {
1546                         sprintf(info.driver, "dhd");
1547                         strcpy(info.version, EPI_VERSION_STR);
1548                 }
1549
1550                 /* otherwise, require dongle to be up */
1551                 else if (!dhd->pub.up) {
1552                         DHD_ERROR(("%s: dongle is not up\n", __func__));
1553                         return -ENODEV;
1554                 }
1555
1556                 /* finally, report dongle driver type */
1557                 else if (dhd->pub.iswl)
1558                         sprintf(info.driver, "wl");
1559                 else
1560                         sprintf(info.driver, "xx");
1561
1562                 sprintf(info.version, "%lu", dhd->pub.drv_version);
1563                 if (copy_to_user(uaddr, &info, sizeof(info)))
1564                         return -EFAULT;
1565                 DHD_CTL(("%s: given %*s, returning %s\n", __func__,
1566                          (int)sizeof(drvname), drvname, info.driver));
1567                 break;
1568
1569 #ifdef TOE
1570                 /* Get toe offload components from dongle */
1571         case ETHTOOL_GRXCSUM:
1572         case ETHTOOL_GTXCSUM:
1573                 ret = dhd_toe_get(dhd, 0, &toe_cmpnt);
1574                 if (ret < 0)
1575                         return ret;
1576
1577                 csum_dir =
1578                     (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1579
1580                 edata.cmd = cmd;
1581                 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
1582
1583                 if (copy_to_user(uaddr, &edata, sizeof(edata)))
1584                         return -EFAULT;
1585                 break;
1586
1587                 /* Set toe offload components in dongle */
1588         case ETHTOOL_SRXCSUM:
1589         case ETHTOOL_STXCSUM:
1590                 if (copy_from_user(&edata, uaddr, sizeof(edata)))
1591                         return -EFAULT;
1592
1593                 /* Read the current settings, update and write back */
1594                 ret = dhd_toe_get(dhd, 0, &toe_cmpnt);
1595                 if (ret < 0)
1596                         return ret;
1597
1598                 csum_dir =
1599                     (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1600
1601                 if (edata.data != 0)
1602                         toe_cmpnt |= csum_dir;
1603                 else
1604                         toe_cmpnt &= ~csum_dir;
1605
1606                 ret = dhd_toe_set(dhd, 0, toe_cmpnt);
1607                 if (ret < 0)
1608                         return ret;
1609
1610                 /* If setting TX checksum mode, tell Linux the new mode */
1611                 if (cmd == ETHTOOL_STXCSUM) {
1612                         if (edata.data)
1613                                 dhd->iflist[0]->net->features |=
1614                                     NETIF_F_IP_CSUM;
1615                         else
1616                                 dhd->iflist[0]->net->features &=
1617                                     ~NETIF_F_IP_CSUM;
1618                 }
1619
1620                 break;
1621 #endif                          /* TOE */
1622
1623         default:
1624                 return -EOPNOTSUPP;
1625         }
1626
1627         return 0;
1628 }
1629
1630 static int dhd_ioctl_entry(struct net_device *net, struct ifreq *ifr, int cmd)
1631 {
1632         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1633         dhd_ioctl_t ioc;
1634         int bcmerror = 0;
1635         int buflen = 0;
1636         void *buf = NULL;
1637         uint driver = 0;
1638         int ifidx;
1639         bool is_set_key_cmd;
1640
1641         ifidx = dhd_net2idx(dhd, net);
1642         DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __func__, ifidx, cmd));
1643
1644         if (ifidx == DHD_BAD_IF)
1645                 return -1;
1646
1647 #if defined(CONFIG_WIRELESS_EXT)
1648         /* linux wireless extensions */
1649         if ((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST)) {
1650                 /* may recurse, do NOT lock */
1651                 return wl_iw_ioctl(net, ifr, cmd);
1652         }
1653 #endif                          /* defined(CONFIG_WIRELESS_EXT) */
1654
1655         if (cmd == SIOCETHTOOL)
1656                 return dhd_ethtool(dhd, (void *)ifr->ifr_data);
1657
1658         if (cmd != SIOCDEVPRIVATE)
1659                 return -EOPNOTSUPP;
1660
1661         memset(&ioc, 0, sizeof(ioc));
1662
1663         /* Copy the ioc control structure part of ioctl request */
1664         if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
1665                 bcmerror = -BCME_BADADDR;
1666                 goto done;
1667         }
1668
1669         /* Copy out any buffer passed */
1670         if (ioc.buf) {
1671                 buflen = min_t(int, ioc.len, DHD_IOCTL_MAXLEN);
1672                 /* optimization for direct ioctl calls from kernel */
1673                 /*
1674                    if (segment_eq(get_fs(), KERNEL_DS)) {
1675                    buf = ioc.buf;
1676                    } else {
1677                  */
1678                 {
1679                         buf = kmalloc(buflen, GFP_ATOMIC);
1680                         if (!buf) {
1681                                 bcmerror = -BCME_NOMEM;
1682                                 goto done;
1683                         }
1684                         if (copy_from_user(buf, ioc.buf, buflen)) {
1685                                 bcmerror = -BCME_BADADDR;
1686                                 goto done;
1687                         }
1688                 }
1689         }
1690
1691         /* To differentiate between wl and dhd read 4 more byes */
1692         if ((copy_from_user(&driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
1693                             sizeof(uint)) != 0)) {
1694                 bcmerror = -BCME_BADADDR;
1695                 goto done;
1696         }
1697
1698         if (!capable(CAP_NET_ADMIN)) {
1699                 bcmerror = -BCME_EPERM;
1700                 goto done;
1701         }
1702
1703         /* check for local dhd ioctl and handle it */
1704         if (driver == DHD_IOCTL_MAGIC) {
1705                 bcmerror = dhd_ioctl((void *)&dhd->pub, &ioc, buf, buflen);
1706                 if (bcmerror)
1707                         dhd->pub.bcmerror = bcmerror;
1708                 goto done;
1709         }
1710
1711         /* send to dongle (must be up, and wl) */
1712         if ((dhd->pub.busstate != DHD_BUS_DATA)) {
1713                 DHD_ERROR(("%s DONGLE_DOWN,__func__\n", __func__));
1714                 bcmerror = BCME_DONGLE_DOWN;
1715                 goto done;
1716         }
1717
1718         if (!dhd->pub.iswl) {
1719                 bcmerror = BCME_DONGLE_DOWN;
1720                 goto done;
1721         }
1722
1723         /* Intercept WLC_SET_KEY IOCTL - serialize M4 send and set key IOCTL to
1724          * prevent M4 encryption.
1725          */
1726         is_set_key_cmd = ((ioc.cmd == WLC_SET_KEY) ||
1727                           ((ioc.cmd == WLC_SET_VAR) &&
1728                            !(strncmp("wsec_key", ioc.buf, 9))) ||
1729                           ((ioc.cmd == WLC_SET_VAR) &&
1730                            !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1731         if (is_set_key_cmd)
1732                 dhd_wait_pend8021x(net);
1733
1734         WAKE_LOCK_INIT(&dhd->pub, WAKE_LOCK_IOCTL, "dhd_ioctl_entry");
1735         WAKE_LOCK(&dhd->pub, WAKE_LOCK_IOCTL);
1736
1737         bcmerror =
1738             dhd_prot_ioctl(&dhd->pub, ifidx, (wl_ioctl_t *)&ioc, buf, buflen);
1739
1740         WAKE_UNLOCK(&dhd->pub, WAKE_LOCK_IOCTL);
1741         WAKE_LOCK_DESTROY(&dhd->pub, WAKE_LOCK_IOCTL);
1742 done:
1743         if (!bcmerror && buf && ioc.buf) {
1744                 if (copy_to_user(ioc.buf, buf, buflen))
1745                         bcmerror = -EFAULT;
1746         }
1747
1748         if (buf)
1749                 kfree(buf);
1750
1751         return OSL_ERROR(bcmerror);
1752 }
1753
1754 static int dhd_stop(struct net_device *net)
1755 {
1756 #if !defined(IGNORE_ETH0_DOWN)
1757         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1758
1759         DHD_TRACE(("%s: Enter\n", __func__));
1760         if (IS_CFG80211_FAVORITE()) {
1761                 wl_cfg80211_down();
1762         }
1763         if (dhd->pub.up == 0)
1764                 return 0;
1765
1766         /* Set state and stop OS transmissions */
1767         dhd->pub.up = 0;
1768         netif_stop_queue(net);
1769 #else
1770         DHD_ERROR(("BYPASS %s:due to BRCM compilation : under investigation\n",
1771                 __func__));
1772 #endif                          /* !defined(IGNORE_ETH0_DOWN) */
1773
1774         return 0;
1775 }
1776
1777 static int dhd_open(struct net_device *net)
1778 {
1779         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1780 #ifdef TOE
1781         u32 toe_ol;
1782 #endif
1783         int ifidx = dhd_net2idx(dhd, net);
1784         s32 ret = 0;
1785
1786         DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1787
1788         if (ifidx == 0) {       /* do it only for primary eth0 */
1789
1790                 /* try to bring up bus */
1791                 ret = dhd_bus_start(&dhd->pub);
1792                 if (ret != 0) {
1793                         DHD_ERROR(("%s: failed with code %d\n", __func__, ret));
1794                         return -1;
1795                 }
1796                 atomic_set(&dhd->pend_8021x_cnt, 0);
1797
1798                 memcpy(net->dev_addr, dhd->pub.mac.octet, ETHER_ADDR_LEN);
1799
1800 #ifdef TOE
1801                 /* Get current TOE mode from dongle */
1802                 if (dhd_toe_get(dhd, ifidx, &toe_ol) >= 0
1803                     && (toe_ol & TOE_TX_CSUM_OL) != 0)
1804                         dhd->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
1805                 else
1806                         dhd->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
1807 #endif
1808         }
1809         /* Allow transmit calls */
1810         netif_start_queue(net);
1811         dhd->pub.up = 1;
1812         if (IS_CFG80211_FAVORITE()) {
1813                 if (unlikely(wl_cfg80211_up())) {
1814                         DHD_ERROR(("%s: failed to bring up cfg80211\n",
1815                                    __func__));
1816                         return -1;
1817                 }
1818         }
1819
1820         return ret;
1821 }
1822
1823 osl_t *dhd_osl_attach(void *pdev, uint bustype)
1824 {
1825         return osl_attach(pdev, bustype, true);
1826 }
1827
1828 void dhd_osl_detach(osl_t *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 = kmalloc(sizeof(dhd_if_t), GFP_ATOMIC))) {
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 = kmalloc(sizeof(dhd_info_t), GFP_ATOMIC);
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                                 send_sig(SIGTERM, dhd->watchdog_tsk, 1);
2340                                 kthread_stop(dhd->watchdog_tsk);
2341                                 dhd->watchdog_tsk = NULL;
2342                         }
2343
2344                         if (dhd->dpc_tsk) {
2345                                 send_sig(SIGTERM, dhd->dpc_tsk, 1);
2346                                 kthread_stop(dhd->dpc_tsk);
2347                                 dhd->dpc_tsk = NULL;
2348                         } else
2349                                 tasklet_kill(&dhd->tasklet);
2350
2351                         if (dhd->sysioc_tsk) {
2352                                 send_sig(SIGTERM, dhd->sysioc_tsk, 1);
2353                                 kthread_stop(dhd->sysioc_tsk);
2354                                 dhd->sysioc_tsk = NULL;
2355                         }
2356
2357                         dhd_bus_detach(dhdp);
2358
2359                         if (dhdp->prot)
2360                                 dhd_prot_detach(dhdp);
2361
2362 #if defined(CONFIG_WIRELESS_EXT)
2363                         wl_iw_detach();
2364 #endif                          /* (CONFIG_WIRELESS_EXT) */
2365
2366                         if (IS_CFG80211_FAVORITE())
2367                                 wl_cfg80211_detach();
2368
2369 #if defined(CONFIG_PM_SLEEP)
2370                         unregister_pm_notifier(&dhd_sleep_pm_notifier);
2371 #endif  /* defined(CONFIG_PM_SLEEP) */
2372                         /* && defined(DHD_GPL) */
2373                         WAKE_LOCK_DESTROY(dhdp, WAKE_LOCK_TMOUT);
2374                         WAKE_LOCK_DESTROY(dhdp, WAKE_LOCK_LINK_DOWN_TMOUT);
2375                         WAKE_LOCK_DESTROY(dhdp, WAKE_LOCK_PNO_FIND_TMOUT);
2376                         free_netdev(ifp->net);
2377                         kfree(ifp);
2378                         kfree(dhd);
2379                 }
2380         }
2381 }
2382
2383 static void __exit dhd_module_cleanup(void)
2384 {
2385         DHD_TRACE(("%s: Enter\n", __func__));
2386
2387         dhd_bus_unregister();
2388 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2389         wifi_del_dev();
2390 #endif
2391         /* Call customer gpio to turn off power with WL_REG_ON signal */
2392         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
2393 }
2394
2395 static int __init dhd_module_init(void)
2396 {
2397         int error;
2398
2399         DHD_TRACE(("%s: Enter\n", __func__));
2400
2401         /* Sanity check on the module parameters */
2402         do {
2403                 /* Both watchdog and DPC as tasklets are ok */
2404                 if ((dhd_watchdog_prio < 0) && (dhd_dpc_prio < 0))
2405                         break;
2406
2407                 /* If both watchdog and DPC are threads, TX must be deferred */
2408                 if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0)
2409                     && dhd_deferred_tx)
2410                         break;
2411
2412                 DHD_ERROR(("Invalid module parameters.\n"));
2413                 return -EINVAL;
2414         } while (0);
2415         /* Call customer gpio to turn on power with WL_REG_ON signal */
2416         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_ON);
2417
2418 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2419         sema_init(&wifi_control_sem, 0);
2420
2421         error = wifi_add_dev();
2422         if (error) {
2423                 DHD_ERROR(("%s: platform_driver_register failed\n", __func__));
2424                 goto faild;
2425         }
2426
2427         /* Waiting callback after platform_driver_register is done or
2428                  exit with error */
2429         if (down_timeout(&wifi_control_sem, msecs_to_jiffies(1000)) != 0) {
2430                 printk(KERN_ERR "%s: platform_driver_register timeout\n",
2431                         __func__);
2432                 /* remove device */
2433                 wifi_del_dev();
2434                 goto faild;
2435         }
2436 #endif  /* #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC) */
2437
2438         error = dhd_bus_register();
2439
2440         if (!error)
2441                 printf("\n%s\n", dhd_version);
2442         else {
2443                 DHD_ERROR(("%s: sdio_register_driver failed\n", __func__));
2444                 goto faild;
2445         }
2446         return error;
2447
2448 faild:
2449         /* turn off power and exit */
2450         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
2451         return -EINVAL;
2452 }
2453
2454 module_init(dhd_module_init);
2455 module_exit(dhd_module_cleanup);
2456
2457 /*
2458  * OS specific functions required to implement DHD driver in OS independent way
2459  */
2460 int dhd_os_proto_block(dhd_pub_t *pub)
2461 {
2462         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2463
2464         if (dhd) {
2465                 down(&dhd->proto_sem);
2466                 return 1;
2467         }
2468         return 0;
2469 }
2470
2471 int dhd_os_proto_unblock(dhd_pub_t *pub)
2472 {
2473         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2474
2475         if (dhd) {
2476                 up(&dhd->proto_sem);
2477                 return 1;
2478         }
2479
2480         return 0;
2481 }
2482
2483 unsigned int dhd_os_get_ioctl_resp_timeout(void)
2484 {
2485         return (unsigned int)dhd_ioctl_timeout_msec;
2486 }
2487
2488 void dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
2489 {
2490         dhd_ioctl_timeout_msec = (int)timeout_msec;
2491 }
2492
2493 int dhd_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition, bool *pending)
2494 {
2495         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2496         DECLARE_WAITQUEUE(wait, current);
2497         int timeout = dhd_ioctl_timeout_msec;
2498
2499         /* Convert timeout in millsecond to jiffies */
2500         timeout = timeout * HZ / 1000;
2501
2502         /* Wait until control frame is available */
2503         add_wait_queue(&dhd->ioctl_resp_wait, &wait);
2504         set_current_state(TASK_INTERRUPTIBLE);
2505
2506         while (!(*condition) && (!signal_pending(current) && timeout))
2507                 timeout = schedule_timeout(timeout);
2508
2509         if (signal_pending(current))
2510                 *pending = true;
2511
2512         set_current_state(TASK_RUNNING);
2513         remove_wait_queue(&dhd->ioctl_resp_wait, &wait);
2514
2515         return timeout;
2516 }
2517
2518 int dhd_os_ioctl_resp_wake(dhd_pub_t *pub)
2519 {
2520         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2521
2522         if (waitqueue_active(&dhd->ioctl_resp_wait))
2523                 wake_up_interruptible(&dhd->ioctl_resp_wait);
2524
2525         return 0;
2526 }
2527
2528 void dhd_os_wd_timer(void *bus, uint wdtick)
2529 {
2530         dhd_pub_t *pub = bus;
2531         static uint save_dhd_watchdog_ms;
2532         dhd_info_t *dhd = (dhd_info_t *) pub->info;
2533
2534         /* don't start the wd until fw is loaded */
2535         if (pub->busstate == DHD_BUS_DOWN)
2536                 return;
2537
2538         /* Totally stop the timer */
2539         if (!wdtick && dhd->wd_timer_valid == true) {
2540                 del_timer_sync(&dhd->timer);
2541                 dhd->wd_timer_valid = false;
2542                 save_dhd_watchdog_ms = wdtick;
2543                 return;
2544         }
2545
2546         if (wdtick) {
2547                 dhd_watchdog_ms = (uint) wdtick;
2548
2549                 if (save_dhd_watchdog_ms != dhd_watchdog_ms) {
2550
2551                         if (dhd->wd_timer_valid == true)
2552                                 /* Stop timer and restart at new value */
2553                                 del_timer_sync(&dhd->timer);
2554
2555                         /* Create timer again when watchdog period is
2556                            dynamically changed or in the first instance
2557                          */
2558                         dhd->timer.expires =
2559                             jiffies + dhd_watchdog_ms * HZ / 1000;
2560                         add_timer(&dhd->timer);
2561
2562                 } else {
2563                         /* Re arm the timer, at last watchdog period */
2564                         mod_timer(&dhd->timer,
2565                                   jiffies + dhd_watchdog_ms * HZ / 1000);
2566                 }
2567
2568                 dhd->wd_timer_valid = true;
2569                 save_dhd_watchdog_ms = wdtick;
2570         }
2571 }
2572
2573 void *dhd_os_open_image(char *filename)
2574 {
2575         struct file *fp;
2576
2577         if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2578                 return wl_cfg80211_request_fw(filename);
2579
2580         fp = filp_open(filename, O_RDONLY, 0);
2581         /*
2582          * 2.6.11 (FC4) supports filp_open() but later revs don't?
2583          * Alternative:
2584          * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
2585          * ???
2586          */
2587         if (IS_ERR(fp))
2588                 fp = NULL;
2589
2590         return fp;
2591 }
2592
2593 int dhd_os_get_image_block(char *buf, int len, void *image)
2594 {
2595         struct file *fp = (struct file *)image;
2596         int rdlen;
2597
2598         if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2599                 return wl_cfg80211_read_fw(buf, len);
2600
2601         if (!image)
2602                 return 0;
2603
2604         rdlen = kernel_read(fp, fp->f_pos, buf, len);
2605         if (rdlen > 0)
2606                 fp->f_pos += rdlen;
2607
2608         return rdlen;
2609 }
2610
2611 void dhd_os_close_image(void *image)
2612 {
2613         if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2614                 return wl_cfg80211_release_fw();
2615         if (image)
2616                 filp_close((struct file *)image, NULL);
2617 }
2618
2619 void dhd_os_sdlock(dhd_pub_t *pub)
2620 {
2621         dhd_info_t *dhd;
2622
2623         dhd = (dhd_info_t *) (pub->info);
2624
2625         if (dhd->threads_only)
2626                 down(&dhd->sdsem);
2627         else
2628                 spin_lock_bh(&dhd->sdlock);
2629 }
2630
2631 void dhd_os_sdunlock(dhd_pub_t *pub)
2632 {
2633         dhd_info_t *dhd;
2634
2635         dhd = (dhd_info_t *) (pub->info);
2636
2637         if (dhd->threads_only)
2638                 up(&dhd->sdsem);
2639         else
2640                 spin_unlock_bh(&dhd->sdlock);
2641 }
2642
2643 void dhd_os_sdlock_txq(dhd_pub_t *pub)
2644 {
2645         dhd_info_t *dhd;
2646
2647         dhd = (dhd_info_t *) (pub->info);
2648         spin_lock_bh(&dhd->txqlock);
2649 }
2650
2651 void dhd_os_sdunlock_txq(dhd_pub_t *pub)
2652 {
2653         dhd_info_t *dhd;
2654
2655         dhd = (dhd_info_t *) (pub->info);
2656         spin_unlock_bh(&dhd->txqlock);
2657 }
2658
2659 void dhd_os_sdlock_rxq(dhd_pub_t *pub)
2660 {
2661 }
2662
2663 void dhd_os_sdunlock_rxq(dhd_pub_t *pub)
2664 {
2665 }
2666
2667 void dhd_os_sdtxlock(dhd_pub_t *pub)
2668 {
2669         dhd_os_sdlock(pub);
2670 }
2671
2672 void dhd_os_sdtxunlock(dhd_pub_t *pub)
2673 {
2674         dhd_os_sdunlock(pub);
2675 }
2676
2677 #if defined(CONFIG_WIRELESS_EXT)
2678 struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev)
2679 {
2680         int res = 0;
2681         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2682
2683         res = wl_iw_get_wireless_stats(dev, &dhd->iw.wstats);
2684
2685         if (res == 0)
2686                 return &dhd->iw.wstats;
2687         else
2688                 return NULL;
2689 }
2690 #endif  /* defined(CONFIG_WIRELESS_EXT) */
2691
2692 static int
2693 dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
2694                   wl_event_msg_t *event, void **data)
2695 {
2696         int bcmerror = 0;
2697
2698         ASSERT(dhd != NULL);
2699
2700         bcmerror = wl_host_event(dhd, ifidx, pktdata, event, data);
2701         if (bcmerror != BCME_OK)
2702                 return bcmerror;
2703
2704 #if defined(CONFIG_WIRELESS_EXT)
2705         if (!IS_CFG80211_FAVORITE()) {
2706                 if ((dhd->iflist[*ifidx] == NULL)
2707                     || (dhd->iflist[*ifidx]->net == NULL)) {
2708                         DHD_ERROR(("%s Exit null pointer\n", __func__));
2709                         return bcmerror;
2710                 }
2711
2712                 if (dhd->iflist[*ifidx]->net)
2713                         wl_iw_event(dhd->iflist[*ifidx]->net, event, *data);
2714         }
2715 #endif                          /* defined(CONFIG_WIRELESS_EXT)  */
2716
2717         if (IS_CFG80211_FAVORITE()) {
2718                 ASSERT(dhd->iflist[*ifidx] != NULL);
2719                 ASSERT(dhd->iflist[*ifidx]->net != NULL);
2720                 if (dhd->iflist[*ifidx]->net)
2721                         wl_cfg80211_event(dhd->iflist[*ifidx]->net, event,
2722                                           *data);
2723         }
2724
2725         return bcmerror;
2726 }
2727
2728 /* send up locally generated event */
2729 void dhd_sendup_event(dhd_pub_t *dhdp, wl_event_msg_t *event, void *data)
2730 {
2731         switch (ntoh32(event->event_type)) {
2732         default:
2733                 break;
2734         }
2735 }
2736
2737 void dhd_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
2738 {
2739         struct dhd_info *dhdinfo = dhd->info;
2740         dhd_os_sdunlock(dhd);
2741         wait_event_interruptible_timeout(dhdinfo->ctrl_wait,
2742                                          (*lockvar == false), HZ * 2);
2743         dhd_os_sdlock(dhd);
2744         return;
2745 }
2746
2747 void dhd_wait_event_wakeup(dhd_pub_t *dhd)
2748 {
2749         struct dhd_info *dhdinfo = dhd->info;
2750         if (waitqueue_active(&dhdinfo->ctrl_wait))
2751                 wake_up_interruptible(&dhdinfo->ctrl_wait);
2752         return;
2753 }
2754
2755 int dhd_dev_reset(struct net_device *dev, u8 flag)
2756 {
2757         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2758
2759         /* Turning off watchdog */
2760         if (flag)
2761                 dhd_os_wd_timer(&dhd->pub, 0);
2762
2763         dhd_bus_devreset(&dhd->pub, flag);
2764
2765         /* Turning on watchdog back */
2766         if (!flag)
2767                 dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
2768         DHD_ERROR(("%s:  WLAN OFF DONE\n", __func__));
2769
2770         return 1;
2771 }
2772
2773 int net_os_set_suspend_disable(struct net_device *dev, int val)
2774 {
2775         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2776         int ret = 0;
2777
2778         if (dhd) {
2779                 ret = dhd->pub.suspend_disable_flag;
2780                 dhd->pub.suspend_disable_flag = val;
2781         }
2782         return ret;
2783 }
2784
2785 int net_os_set_suspend(struct net_device *dev, int val)
2786 {
2787         int ret = 0;
2788 #if defined(CONFIG_HAS_EARLYSUSPEND)
2789         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2790
2791         if (dhd) {
2792                 dhd_os_proto_block(&dhd->pub);
2793                 ret = dhd_set_suspend(val, &dhd->pub);
2794                 dhd_os_proto_unblock(&dhd->pub);
2795         }
2796 #endif          /* defined(CONFIG_HAS_EARLYSUSPEND) */
2797         return ret;
2798 }
2799
2800 int net_os_set_dtim_skip(struct net_device *dev, int val)
2801 {
2802         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2803
2804         if (dhd)
2805                 dhd->pub.dtim_skip = val;
2806
2807         return 0;
2808 }
2809
2810 int net_os_set_packet_filter(struct net_device *dev, int val)
2811 {
2812         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2813         int ret = 0;
2814
2815         /* Packet filtering is set only if we still in early-suspend and
2816          * we need either to turn it ON or turn it OFF
2817          * We can always turn it OFF in case of early-suspend, but we turn it
2818          * back ON only if suspend_disable_flag was not set
2819          */
2820         if (dhd && dhd->pub.up) {
2821                 dhd_os_proto_block(&dhd->pub);
2822                 if (dhd->pub.in_suspend) {
2823                         if (!val || (val && !dhd->pub.suspend_disable_flag))
2824                                 dhd_set_packet_filter(val, &dhd->pub);
2825                 }
2826                 dhd_os_proto_unblock(&dhd->pub);
2827         }
2828         return ret;
2829 }
2830
2831 void dhd_dev_init_ioctl(struct net_device *dev)
2832 {
2833         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2834
2835         dhd_preinit_ioctls(&dhd->pub);
2836 }
2837
2838 #ifdef PNO_SUPPORT
2839 /* Linux wrapper to call common dhd_pno_clean */
2840 int dhd_dev_pno_reset(struct net_device *dev)
2841 {
2842         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2843
2844         return dhd_pno_clean(&dhd->pub);
2845 }
2846
2847 /* Linux wrapper to call common dhd_pno_enable */
2848 int dhd_dev_pno_enable(struct net_device *dev, int pfn_enabled)
2849 {
2850         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2851
2852         return dhd_pno_enable(&dhd->pub, pfn_enabled);
2853 }
2854
2855 /* Linux wrapper to call common dhd_pno_set */
2856 int
2857 dhd_dev_pno_set(struct net_device *dev, wlc_ssid_t *ssids_local, int nssid,
2858                 unsigned char scan_fr)
2859 {
2860         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2861
2862         return dhd_pno_set(&dhd->pub, ssids_local, nssid, scan_fr);
2863 }
2864
2865 /* Linux wrapper to get  pno status */
2866 int dhd_dev_get_pno_status(struct net_device *dev)
2867 {
2868         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2869
2870         return dhd_pno_get_status(&dhd->pub);
2871 }
2872
2873 #endif                          /* PNO_SUPPORT */
2874
2875 static int dhd_get_pend_8021x_cnt(dhd_info_t *dhd)
2876 {
2877         return atomic_read(&dhd->pend_8021x_cnt);
2878 }
2879
2880 #define MAX_WAIT_FOR_8021X_TX   10
2881
2882 int dhd_wait_pend8021x(struct net_device *dev)
2883 {
2884         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2885         int timeout = 10 * HZ / 1000;
2886         int ntimes = MAX_WAIT_FOR_8021X_TX;
2887         int pend = dhd_get_pend_8021x_cnt(dhd);
2888
2889         while (ntimes && pend) {
2890                 if (pend) {
2891                         set_current_state(TASK_INTERRUPTIBLE);
2892                         schedule_timeout(timeout);
2893                         set_current_state(TASK_RUNNING);
2894                         ntimes--;
2895                 }
2896                 pend = dhd_get_pend_8021x_cnt(dhd);
2897         }
2898         return pend;
2899 }
2900
2901 #ifdef DHD_DEBUG
2902 int write_to_file(dhd_pub_t *dhd, u8 *buf, int size)
2903 {
2904         int ret = 0;
2905         struct file *fp;
2906         mm_segment_t old_fs;
2907         loff_t pos = 0;
2908
2909         /* change to KERNEL_DS address limit */
2910         old_fs = get_fs();
2911         set_fs(KERNEL_DS);
2912
2913         /* open file to write */
2914         fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
2915         if (!fp) {
2916                 printf("%s: open file error\n", __func__);
2917                 ret = -1;
2918                 goto exit;
2919         }
2920
2921         /* Write buf to file */
2922         fp->f_op->write(fp, buf, size, &pos);
2923
2924 exit:
2925         /* free buf before return */
2926         kfree(buf);
2927         /* close file before return */
2928         if (fp)
2929                 filp_close(fp, current->files);
2930         /* restore previous address limit */
2931         set_fs(old_fs);
2932
2933         return ret;
2934 }
2935 #endif                          /* DHD_DEBUG */