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