OSDN Git Service

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