OSDN Git Service

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