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