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