OSDN Git Service

staging: brcm80211: rename macro definitions in dhd.h
[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 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/mmc/sdio_func.h>
25 #include <linux/random.h>
26 #include <linux/spinlock.h>
27 #include <linux/ethtool.h>
28 #include <linux/fcntl.h>
29 #include <linux/fs.h>
30 #include <linux/uaccess.h>
31 #include <net/cfg80211.h>
32 #if defined(CONFIG_HAS_EARLYSUSPEND)
33 #include <linux/earlysuspend.h>
34 #endif
35 #include <defs.h>
36 #include <brcmu_utils.h>
37 #include <brcmu_wifi.h>
38
39 #include "dngl_stats.h"
40 #include "dhd.h"
41 #include "dhd_bus.h"
42 #include "dhd_proto.h"
43 #include "dhd_dbg.h"
44 #include "wl_cfg80211.h"
45 #include "bcmchip.h"
46
47 /* Global ASSERT type flag */
48 u32 g_assert_type;
49
50 #if defined(CONFIG_PM_SLEEP)
51 #include <linux/suspend.h>
52 atomic_t brcmf_mmc_suspend;
53 DECLARE_WAIT_QUEUE_HEAD(dhd_dpc_wait);
54 #endif  /*  defined(CONFIG_PM_SLEEP) */
55
56 MODULE_AUTHOR("Broadcom Corporation");
57 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
58 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
59 MODULE_LICENSE("Dual BSD/GPL");
60
61
62 /* Interface control information */
63 typedef struct dhd_if {
64         struct dhd_info *info;  /* back pointer to dhd_info */
65         /* OS/stack specifics */
66         struct net_device *net;
67         struct net_device_stats stats;
68         int idx;                /* iface idx in dongle */
69         int state;              /* interface state */
70         uint subunit;           /* subunit */
71         u8 mac_addr[ETH_ALEN];  /* assigned MAC address */
72         bool attached;          /* Delayed attachment when unset */
73         bool txflowcontrol;     /* Per interface flow control indicator */
74         char name[IFNAMSIZ];    /* linux interface name */
75 } dhd_if_t;
76
77 /* Local private structure (extension of pub) */
78 typedef struct dhd_info {
79         dhd_pub_t pub;
80
81         /* OS/stack specifics */
82         dhd_if_t *iflist[BRCMF_MAX_IFS];
83
84         struct semaphore proto_sem;
85         wait_queue_head_t ioctl_resp_wait;
86
87         /* Thread to issue ioctl for multicast */
88         struct task_struct *sysioc_tsk;
89         struct semaphore sysioc_sem;
90         bool set_multicast;
91         bool set_macaddress;
92         u8 macvalue[ETH_ALEN];
93         atomic_t pend_8021x_cnt;
94
95 #ifdef CONFIG_HAS_EARLYSUSPEND
96         struct early_suspend early_suspend;
97 #endif                          /* CONFIG_HAS_EARLYSUSPEND */
98 } dhd_info_t;
99
100 /* Error bits */
101 module_param(brcmf_msg_level, int, 0);
102
103 /* Spawn a thread for system ioctls (set mac, set mcast) */
104 uint brcmf_sysioc = true;
105 module_param(brcmf_sysioc, uint, 0);
106
107 /* ARP offload agent mode : Enable ARP Host Auto-Reply
108 and ARP Peer Auto-Reply */
109 uint brcmf_arp_mode = 0xb;
110 module_param(brcmf_arp_mode, uint, 0);
111
112 /* ARP offload enable */
113 uint brcmf_arp_enable = true;
114 module_param(brcmf_arp_enable, uint, 0);
115
116 /* Global Pkt filter enable control */
117 uint brcmf_pkt_filter_enable = true;
118 module_param(brcmf_pkt_filter_enable, uint, 0);
119
120 /*  Pkt filter init setup */
121 uint brcmf_pkt_filter_init;
122 module_param(brcmf_pkt_filter_init, uint, 0);
123
124 /* Pkt filter mode control */
125 uint brcmf_master_mode = true;
126 module_param(brcmf_master_mode, uint, 1);
127
128 extern int brcmf_dongle_memsize;
129 module_param(brcmf_dongle_memsize, int, 0);
130
131 /* Contorl fw roaming */
132 uint brcmf_roam = 1;
133
134 /* Control radio state */
135 uint brcmf_radio_up = 1;
136
137 /* Network inteface name */
138 char iface_name[IFNAMSIZ] = "wlan";
139 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
140
141 /* The following are specific to the SDIO dongle */
142
143 /* IOCTL response timeout */
144 int brcmf_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
145
146 /* Idle timeout for backplane clock */
147 int brcmf_idletime = BRCMF_IDLETIME_TICKS;
148 module_param(brcmf_idletime, int, 0);
149
150 /* Use polling */
151 uint brcmf_poll;
152 module_param(brcmf_poll, uint, 0);
153
154 /* Use interrupts */
155 uint brcmf_intr = true;
156 module_param(brcmf_intr, uint, 0);
157
158 /* SDIO Drive Strength (in milliamps) */
159 uint brcmf_sdiod_drive_strength = 6;
160 module_param(brcmf_sdiod_drive_strength, uint, 0);
161
162 /* Tx/Rx bounds */
163 extern uint brcmf_txbound;
164 extern uint brcmf_rxbound;
165 module_param(brcmf_txbound, uint, 0);
166 module_param(brcmf_rxbound, uint, 0);
167
168 #ifdef SDTEST
169 /* Echo packet generator (pkts/s) */
170 uint brcmf_pktgen;
171 module_param(brcmf_pktgen, uint, 0);
172
173 /* Echo packet len (0 => sawtooth, max 2040) */
174 uint brcmf_pktgen_len;
175 module_param(brcmf_pktgen_len, uint, 0);
176 #endif
177
178 /* Version string to report */
179 #ifdef BCMDBG
180 #define DHD_COMPILED "\nCompiled in " SRCBASE
181 #else
182 #define DHD_COMPILED
183 #endif
184
185 static int brcmf_toe_get(dhd_info_t *drvr_priv, int idx, u32 *toe_ol);
186 static int brcmf_toe_set(dhd_info_t *drvr_priv, int idx, u32 toe_ol);
187 static int brcmf_host_event(dhd_info_t *drvr_priv, int *ifidx, void *pktdata,
188                              brcmf_event_msg_t *event_ptr, void **data_ptr);
189
190 static void brcmf_set_packet_filter(int value, dhd_pub_t *drvr)
191 {
192         DHD_TRACE(("%s: %d\n", __func__, value));
193         /* 1 - Enable packet filter, only allow unicast packet to send up */
194         /* 0 - Disable packet filter */
195         if (brcmf_pkt_filter_enable) {
196                 int i;
197
198                 for (i = 0; i < drvr->pktfilter_count; i++) {
199                         brcmf_c_pktfilter_offload_set(drvr, drvr->pktfilter[i]);
200                         brcmf_c_pktfilter_offload_enable(drvr,
201                                                          drvr->pktfilter[i],
202                                                          value,
203                                                          brcmf_master_mode);
204                 }
205         }
206 }
207
208 #if defined(CONFIG_HAS_EARLYSUSPEND)
209 static int brcmf_set_suspend(int value, dhd_pub_t *drvr)
210 {
211         int power_mode = PM_MAX;
212         /* struct wl_pkt_filter_enable       enable_parm; */
213         char iovbuf[32];
214         int bcn_li_dtim = 3;
215
216         DHD_TRACE(("%s: enter, value = %d in_suspend=%d\n",
217                    __func__, value, drvr->in_suspend));
218
219         if (drvr && drvr->up) {
220                 if (value && drvr->in_suspend) {
221
222                         /* Kernel suspended */
223                         DHD_TRACE(("%s: force extra Suspend setting\n",
224                                    __func__));
225
226                         brcmf_proto_cdc_set_ioctl(drvr, 0, BRCMF_C_SET_PM,
227                                          (char *)&power_mode,
228                                          sizeof(power_mode));
229
230                         /* Enable packet filter, only allow unicast
231                                  packet to send up */
232                         brcmf_set_packet_filter(1, drvr);
233
234                         /* if dtim skip setup as default force it
235                          * to wake each third dtim
236                          * for better power saving.
237                          * Note that side effect is chance to miss BC/MC
238                          * packet
239                          */
240                         if ((drvr->dtim_skip == 0) || (drvr->dtim_skip == 1))
241                                 bcn_li_dtim = 3;
242                         else
243                                 bcn_li_dtim = drvr->dtim_skip;
244                         brcmu_mkiovar("bcn_li_dtim", (char *)&bcn_li_dtim,
245                                     4, iovbuf, sizeof(iovbuf));
246                         brcmf_proto_cdc_set_ioctl(drvr, 0, BRCMF_C_SET_VAR,
247                                                   iovbuf, sizeof(iovbuf));
248                 } else {
249
250                         /* Kernel resumed  */
251                         DHD_TRACE(("%s: Remove extra suspend setting\n",
252                                    __func__));
253
254                         power_mode = PM_FAST;
255                         brcmf_proto_cdc_set_ioctl(drvr, 0, BRCMF_C_SET_PM,
256                                                   (char *)&power_mode,
257                                                   sizeof(power_mode));
258
259                         /* disable pkt filter */
260                         brcmf_set_packet_filter(0, drvr);
261
262                         /* restore pre-suspend setting for dtim_skip */
263                         brcmu_mkiovar("bcn_li_dtim", (char *)&drvr->dtim_skip,
264                                     4, iovbuf, sizeof(iovbuf));
265
266                         brcmf_proto_cdc_set_ioctl(drvr, 0, BRCMF_C_SET_VAR,
267                                                   iovbuf, sizeof(iovbuf));
268                 }
269         }
270
271         return 0;
272 }
273
274 static void brcmf_suspend_resume_helper(struct dhd_info *drvr_priv, int val)
275 {
276         dhd_pub_t *drvr = &drvr_priv->pub;
277
278         brcmf_os_proto_block(drvr);
279         /* Set flag when early suspend was called */
280         drvr->in_suspend = val;
281         if (!drvr->suspend_disable_flag)
282                 brcmf_set_suspend(val, drvr);
283         brcmf_os_proto_unblock(drvr);
284 }
285
286 static void brcmf_early_suspend(struct early_suspend *h)
287 {
288         struct dhd_info *drvr_priv =
289                         container_of(h, struct dhd_info, early_suspend);
290
291         DHD_TRACE(("%s: enter\n", __func__));
292
293         if (drvr_priv)
294                 dhd_suspend_resume_helper(drvr_priv, 1);
295
296 }
297
298 static void brcmf_late_resume(struct early_suspend *h)
299 {
300         struct dhd_info *drvr_priv =
301                         container_of(h, struct dhd_info, early_suspend);
302
303         DHD_TRACE(("%s: enter\n", __func__));
304
305         if (drvr_priv)
306                 dhd_suspend_resume_helper(drvr_priv, 0);
307 }
308 #endif                          /* defined(CONFIG_HAS_EARLYSUSPEND) */
309
310 /*
311  * Generalized timeout mechanism.  Uses spin sleep with exponential
312  * back-off until
313  * the sleep time reaches one jiffy, then switches over to task delay.  Usage:
314  *
315  *      brcmf_timeout_start(&tmo, usec);
316  *      while (!brcmf_timeout_expired(&tmo))
317  *              if (poll_something())
318  *                      break;
319  *      if (brcmf_timeout_expired(&tmo))
320  *              fatal();
321  */
322
323 void brcmf_timeout_start(dhd_timeout_t *tmo, uint usec)
324 {
325         tmo->limit = usec;
326         tmo->increment = 0;
327         tmo->elapsed = 0;
328         tmo->tick = 1000000 / HZ;
329 }
330
331 int brcmf_timeout_expired(dhd_timeout_t *tmo)
332 {
333         /* Does nothing the first call */
334         if (tmo->increment == 0) {
335                 tmo->increment = 1;
336                 return 0;
337         }
338
339         if (tmo->elapsed >= tmo->limit)
340                 return 1;
341
342         /* Add the delay that's about to take place */
343         tmo->elapsed += tmo->increment;
344
345         if (tmo->increment < tmo->tick) {
346                 udelay(tmo->increment);
347                 tmo->increment *= 2;
348                 if (tmo->increment > tmo->tick)
349                         tmo->increment = tmo->tick;
350         } else {
351                 wait_queue_head_t delay_wait;
352                 DECLARE_WAITQUEUE(wait, current);
353                 int pending;
354                 init_waitqueue_head(&delay_wait);
355                 add_wait_queue(&delay_wait, &wait);
356                 set_current_state(TASK_INTERRUPTIBLE);
357                 schedule_timeout(1);
358                 pending = signal_pending(current);
359                 remove_wait_queue(&delay_wait, &wait);
360                 set_current_state(TASK_RUNNING);
361                 if (pending)
362                         return 1;       /* Interrupted */
363         }
364
365         return 0;
366 }
367
368 static int brcmf_net2idx(dhd_info_t *drvr_priv, struct net_device *net)
369 {
370         int i = 0;
371
372         ASSERT(drvr_priv);
373         while (i < BRCMF_MAX_IFS) {
374                 if (drvr_priv->iflist[i] && (drvr_priv->iflist[i]->net == net))
375                         return i;
376                 i++;
377         }
378
379         return BRCMF_BAD_IF;
380 }
381
382 int brcmf_ifname2idx(dhd_info_t *drvr_priv, char *name)
383 {
384         int i = BRCMF_MAX_IFS;
385
386         ASSERT(drvr_priv);
387
388         if (name == NULL || *name == '\0')
389                 return 0;
390
391         while (--i > 0)
392                 if (drvr_priv->iflist[i]
393                     && !strncmp(drvr_priv->iflist[i]->name, name, IFNAMSIZ))
394                         break;
395
396         DHD_TRACE(("%s: return idx %d for \"%s\"\n", __func__, i, name));
397
398         return i;               /* default - the primary interface */
399 }
400
401 char *brcmf_ifname(dhd_pub_t *drvr, int ifidx)
402 {
403         dhd_info_t *drvr_priv = drvr->info;
404
405         ASSERT(drvr_priv);
406
407         if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
408                 DHD_ERROR(("%s: ifidx %d out of range\n", __func__, ifidx));
409                 return "<if_bad>";
410         }
411
412         if (drvr_priv->iflist[ifidx] == NULL) {
413                 DHD_ERROR(("%s: null i/f %d\n", __func__, ifidx));
414                 return "<if_null>";
415         }
416
417         if (drvr_priv->iflist[ifidx]->net)
418                 return drvr_priv->iflist[ifidx]->net->name;
419
420         return "<if_none>";
421 }
422
423 static void _brcmf_set_multicast_list(dhd_info_t *drvr_priv, int ifidx)
424 {
425         struct net_device *dev;
426         struct netdev_hw_addr *ha;
427         u32 allmulti, cnt;
428
429         struct brcmf_ioctl ioc;
430         char *buf, *bufp;
431         uint buflen;
432         int ret;
433
434         ASSERT(drvr_priv && drvr_priv->iflist[ifidx]);
435         dev = drvr_priv->iflist[ifidx]->net;
436         cnt = netdev_mc_count(dev);
437
438         /* Determine initial value of allmulti flag */
439         allmulti = (dev->flags & IFF_ALLMULTI) ? true : false;
440
441         /* Send down the multicast list first. */
442
443         buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
444         bufp = buf = kmalloc(buflen, GFP_ATOMIC);
445         if (!bufp) {
446                 DHD_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
447                            brcmf_ifname(&drvr_priv->pub, ifidx), cnt));
448                 return;
449         }
450
451         strcpy(bufp, "mcast_list");
452         bufp += strlen("mcast_list") + 1;
453
454         cnt = cpu_to_le32(cnt);
455         memcpy(bufp, &cnt, sizeof(cnt));
456         bufp += sizeof(cnt);
457
458         netdev_for_each_mc_addr(ha, dev) {
459                 if (!cnt)
460                         break;
461                 memcpy(bufp, ha->addr, ETH_ALEN);
462                 bufp += ETH_ALEN;
463                 cnt--;
464         }
465
466         memset(&ioc, 0, sizeof(ioc));
467         ioc.cmd = BRCMF_C_SET_VAR;
468         ioc.buf = buf;
469         ioc.len = buflen;
470         ioc.set = true;
471
472         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
473         if (ret < 0) {
474                 DHD_ERROR(("%s: set mcast_list failed, cnt %d\n",
475                            brcmf_ifname(&drvr_priv->pub, ifidx), cnt));
476                 allmulti = cnt ? true : allmulti;
477         }
478
479         kfree(buf);
480
481         /* Now send the allmulti setting.  This is based on the setting in the
482          * net_device flags, but might be modified above to be turned on if we
483          * were trying to set some addresses and dongle rejected it...
484          */
485
486         buflen = sizeof("allmulti") + sizeof(allmulti);
487         buf = kmalloc(buflen, GFP_ATOMIC);
488         if (!buf) {
489                 DHD_ERROR(("%s: out of memory for allmulti\n",
490                            brcmf_ifname(&drvr_priv->pub, ifidx)));
491                 return;
492         }
493         allmulti = cpu_to_le32(allmulti);
494
495         if (!brcmu_mkiovar
496             ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
497                 DHD_ERROR(("%s: mkiovar failed for allmulti, datalen %d "
498                         "buflen %u\n", brcmf_ifname(&drvr_priv->pub, ifidx),
499                         (int)sizeof(allmulti), buflen));
500                 kfree(buf);
501                 return;
502         }
503
504         memset(&ioc, 0, sizeof(ioc));
505         ioc.cmd = BRCMF_C_SET_VAR;
506         ioc.buf = buf;
507         ioc.len = buflen;
508         ioc.set = true;
509
510         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
511         if (ret < 0) {
512                 DHD_ERROR(("%s: set allmulti %d failed\n",
513                            brcmf_ifname(&drvr_priv->pub, ifidx),
514                            le32_to_cpu(allmulti)));
515         }
516
517         kfree(buf);
518
519         /* Finally, pick up the PROMISC flag as well, like the NIC
520                  driver does */
521
522         allmulti = (dev->flags & IFF_PROMISC) ? true : false;
523         allmulti = cpu_to_le32(allmulti);
524
525         memset(&ioc, 0, sizeof(ioc));
526         ioc.cmd = BRCMF_C_SET_PROMISC;
527         ioc.buf = &allmulti;
528         ioc.len = sizeof(allmulti);
529         ioc.set = true;
530
531         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
532         if (ret < 0) {
533                 DHD_ERROR(("%s: set promisc %d failed\n",
534                            brcmf_ifname(&drvr_priv->pub, ifidx),
535                            le32_to_cpu(allmulti)));
536         }
537 }
538
539 static int _brcmf_set_mac_address(dhd_info_t *drvr_priv, int ifidx, u8 *addr)
540 {
541         char buf[32];
542         struct brcmf_ioctl ioc;
543         int ret;
544
545         DHD_TRACE(("%s enter\n", __func__));
546         if (!brcmu_mkiovar
547             ("cur_etheraddr", (char *)addr, ETH_ALEN, buf, 32)) {
548                 DHD_ERROR(("%s: mkiovar failed for cur_etheraddr\n",
549                            brcmf_ifname(&drvr_priv->pub, ifidx)));
550                 return -1;
551         }
552         memset(&ioc, 0, sizeof(ioc));
553         ioc.cmd = BRCMF_C_SET_VAR;
554         ioc.buf = buf;
555         ioc.len = 32;
556         ioc.set = true;
557
558         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
559         if (ret < 0) {
560                 DHD_ERROR(("%s: set cur_etheraddr failed\n",
561                            brcmf_ifname(&drvr_priv->pub, ifidx)));
562         } else {
563                 memcpy(drvr_priv->iflist[ifidx]->net->dev_addr, addr, ETH_ALEN);
564         }
565
566         return ret;
567 }
568
569 #ifdef SOFTAP
570 extern struct net_device *ap_net_dev;
571 #endif
572
573 static void brcmf_op_if(dhd_if_t *ifp)
574 {
575         dhd_info_t *drvr_priv;
576         int ret = 0, err = 0;
577
578         ASSERT(ifp && ifp->info && ifp->idx);   /* Virtual interfaces only */
579
580         drvr_priv = ifp->info;
581
582         DHD_TRACE(("%s: idx %d, state %d\n", __func__, ifp->idx, ifp->state));
583
584         switch (ifp->state) {
585         case BRCMF_E_IF_ADD:
586                 /*
587                  * Delete the existing interface before overwriting it
588                  * in case we missed the BRCMF_E_IF_DEL event.
589                  */
590                 if (ifp->net != NULL) {
591                         DHD_ERROR(("%s: ERROR: netdev:%s already exists, "
592                         "try free & unregister\n",
593                         __func__, ifp->net->name));
594                         netif_stop_queue(ifp->net);
595                         unregister_netdev(ifp->net);
596                         free_netdev(ifp->net);
597                 }
598                 /* Allocate etherdev, including space for private structure */
599                 ifp->net = alloc_etherdev(sizeof(drvr_priv));
600                 if (!ifp->net) {
601                         DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
602                         ret = -ENOMEM;
603                 }
604                 if (ret == 0) {
605                         strcpy(ifp->net->name, ifp->name);
606                         memcpy(netdev_priv(ifp->net), &drvr_priv, sizeof(drvr_priv));
607                         err = brcmf_net_attach(&drvr_priv->pub, ifp->idx);
608                         if (err != 0) {
609                                 DHD_ERROR(("%s: dhd_net_attach failed, "
610                                         "err %d\n",
611                                         __func__, err));
612                                 ret = -EOPNOTSUPP;
613                         } else {
614 #ifdef SOFTAP
615                                 /* semaphore that the soft AP CODE
616                                          waits on */
617                                 extern struct semaphore ap_eth_sema;
618
619                                 /* save ptr to wl0.1 netdev for use
620                                          in wl_iw.c  */
621                                 ap_net_dev = ifp->net;
622                                 /* signal to the SOFTAP 'sleeper' thread,
623                                          wl0.1 is ready */
624                                 up(&ap_eth_sema);
625 #endif
626                                 DHD_TRACE(("\n ==== pid:%x, net_device for "
627                                         "if:%s created ===\n\n",
628                                         current->pid, ifp->net->name));
629                                 ifp->state = 0;
630                         }
631                 }
632                 break;
633         case BRCMF_E_IF_DEL:
634                 if (ifp->net != NULL) {
635                         DHD_TRACE(("\n%s: got 'WLC_E_IF_DEL' state\n",
636                                    __func__));
637                         netif_stop_queue(ifp->net);
638                         unregister_netdev(ifp->net);
639                         ret = BRCMF_DEL_IF;     /* Make sure the free_netdev()
640                                                          is called */
641                 }
642                 break;
643         default:
644                 DHD_ERROR(("%s: bad op %d\n", __func__, ifp->state));
645                 ASSERT(!ifp->state);
646                 break;
647         }
648
649         if (ret < 0) {
650                 if (ifp->net)
651                         free_netdev(ifp->net);
652
653                 drvr_priv->iflist[ifp->idx] = NULL;
654                 kfree(ifp);
655 #ifdef SOFTAP
656                 if (ifp->net == ap_net_dev)
657                         ap_net_dev = NULL;      /*  NULL  SOFTAP global
658                                                          wl0.1 as well */
659 #endif                          /*  SOFTAP */
660         }
661 }
662
663 static int _brcmf_sysioc_thread(void *data)
664 {
665         dhd_info_t *drvr_priv = (dhd_info_t *) data;
666         int i;
667 #ifdef SOFTAP
668         bool in_ap = false;
669 #endif
670
671         allow_signal(SIGTERM);
672
673         while (down_interruptible(&drvr_priv->sysioc_sem) == 0) {
674                 if (kthread_should_stop())
675                         break;
676                 for (i = 0; i < BRCMF_MAX_IFS; i++) {
677                         if (drvr_priv->iflist[i]) {
678 #ifdef SOFTAP
679                                 in_ap = (ap_net_dev != NULL);
680 #endif                          /* SOFTAP */
681                                 if (drvr_priv->iflist[i]->state)
682                                         brcmf_op_if(drvr_priv->iflist[i]);
683 #ifdef SOFTAP
684                                 if (drvr_priv->iflist[i] == NULL) {
685                                         DHD_TRACE(("\n\n %s: interface %d "
686                                                 "removed!\n", __func__, i));
687                                         continue;
688                                 }
689
690                                 if (in_ap && drvr_priv->set_macaddress) {
691                                         DHD_TRACE(("attempt to set MAC for %s "
692                                                 "in AP Mode," "blocked.\n",
693                                                 drvr_priv->iflist[i]->net->name));
694                                         drvr_priv->set_macaddress = false;
695                                         continue;
696                                 }
697
698                                 if (in_ap && drvr_priv->set_multicast) {
699                                         DHD_TRACE(("attempt to set MULTICAST "
700                                                    "list for %s in AP Mode, "
701                                                    "blocked.\n",
702                                                    drvr_priv->iflist[i]->net->name));
703                                         drvr_priv->set_multicast = false;
704                                         continue;
705                                 }
706 #endif                          /* SOFTAP */
707                                 if (drvr_priv->set_multicast) {
708                                         drvr_priv->set_multicast = false;
709                                         _brcmf_set_multicast_list(drvr_priv, i);
710                                 }
711                                 if (drvr_priv->set_macaddress) {
712                                         drvr_priv->set_macaddress = false;
713                                         _brcmf_set_mac_address(drvr_priv, i,
714                                                              drvr_priv->macvalue);
715                                 }
716                         }
717                 }
718         }
719         return 0;
720 }
721
722 static int brcmf_netdev_set_mac_address(struct net_device *dev, void *addr)
723 {
724         int ret = 0;
725
726         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(dev);
727         struct sockaddr *sa = (struct sockaddr *)addr;
728         int ifidx;
729
730         ifidx = brcmf_net2idx(drvr_priv, dev);
731         if (ifidx == BRCMF_BAD_IF)
732                 return -1;
733
734         ASSERT(drvr_priv->sysioc_tsk);
735         memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
736         drvr_priv->set_macaddress = true;
737         up(&drvr_priv->sysioc_sem);
738
739         return ret;
740 }
741
742 static void brcmf_netdev_set_multicast_list(struct net_device *dev)
743 {
744         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(dev);
745         int ifidx;
746
747         ifidx = brcmf_net2idx(drvr_priv, dev);
748         if (ifidx == BRCMF_BAD_IF)
749                 return;
750
751         ASSERT(drvr_priv->sysioc_tsk);
752         drvr_priv->set_multicast = true;
753         up(&drvr_priv->sysioc_sem);
754 }
755
756 int brcmf_sendpkt(dhd_pub_t *drvr, int ifidx, struct sk_buff *pktbuf)
757 {
758         dhd_info_t *drvr_priv = drvr->info;
759
760         /* Reject if down */
761         if (!drvr->up || (drvr->busstate == DHD_BUS_DOWN))
762                 return -ENODEV;
763
764         /* Update multicast statistic */
765         if (pktbuf->len >= ETH_ALEN) {
766                 u8 *pktdata = (u8 *) (pktbuf->data);
767                 struct ethhdr *eh = (struct ethhdr *)pktdata;
768
769                 if (is_multicast_ether_addr(eh->h_dest))
770                         drvr->tx_multicast++;
771                 if (ntohs(eh->h_proto) == ETH_P_PAE)
772                         atomic_inc(&drvr_priv->pend_8021x_cnt);
773         }
774
775         /* If the protocol uses a data header, apply it */
776         brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
777
778         /* Use bus module to send data frame */
779         return brcmf_sdbrcm_bus_txdata(drvr->bus, pktbuf);
780 }
781
782 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *net)
783 {
784         int ret;
785         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(net);
786         int ifidx;
787
788         DHD_TRACE(("%s: Enter\n", __func__));
789
790         /* Reject if down */
791         if (!drvr_priv->pub.up || (drvr_priv->pub.busstate == DHD_BUS_DOWN)) {
792                 DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d\n",
793                            __func__, drvr_priv->pub.up, drvr_priv->pub.busstate));
794                 netif_stop_queue(net);
795                 return -ENODEV;
796         }
797
798         ifidx = brcmf_net2idx(drvr_priv, net);
799         if (ifidx == BRCMF_BAD_IF) {
800                 DHD_ERROR(("%s: bad ifidx %d\n", __func__, ifidx));
801                 netif_stop_queue(net);
802                 return -ENODEV;
803         }
804
805         /* Make sure there's enough room for any header */
806         if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
807                 struct sk_buff *skb2;
808
809                 DHD_INFO(("%s: insufficient headroom\n",
810                           brcmf_ifname(&drvr_priv->pub, ifidx)));
811                 drvr_priv->pub.tx_realloc++;
812                 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
813                 dev_kfree_skb(skb);
814                 skb = skb2;
815                 if (skb == NULL) {
816                         DHD_ERROR(("%s: skb_realloc_headroom failed\n",
817                                    brcmf_ifname(&drvr_priv->pub, ifidx)));
818                         ret = -ENOMEM;
819                         goto done;
820                 }
821         }
822
823         ret = brcmf_sendpkt(&drvr_priv->pub, ifidx, skb);
824
825 done:
826         if (ret)
827                 drvr_priv->pub.dstats.tx_dropped++;
828         else
829                 drvr_priv->pub.tx_packets++;
830
831         /* Return ok: we always eat the packet */
832         return 0;
833 }
834
835 void brcmf_txflowcontrol(dhd_pub_t *drvr, int ifidx, bool state)
836 {
837         struct net_device *net;
838         dhd_info_t *drvr_priv = drvr->info;
839
840         DHD_TRACE(("%s: Enter\n", __func__));
841
842         drvr->txoff = state;
843         ASSERT(drvr_priv && drvr_priv->iflist[ifidx]);
844         net = drvr_priv->iflist[ifidx]->net;
845         if (state == ON)
846                 netif_stop_queue(net);
847         else
848                 netif_wake_queue(net);
849 }
850
851 void brcmf_rx_frame(dhd_pub_t *drvr, int ifidx, struct sk_buff *skb,
852                   int numpkt)
853 {
854         dhd_info_t *drvr_priv = drvr->info;
855         unsigned char *eth;
856         uint len;
857         void *data;
858         struct sk_buff *pnext, *save_pktbuf;
859         int i;
860         dhd_if_t *ifp;
861         brcmf_event_msg_t event;
862
863         DHD_TRACE(("%s: Enter\n", __func__));
864
865         save_pktbuf = skb;
866
867         for (i = 0; skb && i < numpkt; i++, skb = pnext) {
868
869                 pnext = skb->next;
870                 skb->next = NULL;
871
872                 /* Get the protocol, maintain skb around eth_type_trans()
873                  * The main reason for this hack is for the limitation of
874                  * Linux 2.4 where 'eth_type_trans' uses the
875                  * 'net->hard_header_len'
876                  * to perform skb_pull inside vs ETH_HLEN. Since to avoid
877                  * coping of the packet coming from the network stack to add
878                  * BDC, Hardware header etc, during network interface
879                  * registration
880                  * we set the 'net->hard_header_len' to ETH_HLEN + extra space
881                  * required
882                  * for BDC, Hardware header etc. and not just the ETH_HLEN
883                  */
884                 eth = skb->data;
885                 len = skb->len;
886
887                 ifp = drvr_priv->iflist[ifidx];
888                 if (ifp == NULL)
889                         ifp = drvr_priv->iflist[0];
890
891                 ASSERT(ifp);
892                 skb->dev = ifp->net;
893                 skb->protocol = eth_type_trans(skb, skb->dev);
894
895                 if (skb->pkt_type == PACKET_MULTICAST)
896                         drvr_priv->pub.rx_multicast++;
897
898                 skb->data = eth;
899                 skb->len = len;
900
901                 /* Strip header, count, deliver upward */
902                 skb_pull(skb, ETH_HLEN);
903
904                 /* Process special event packets and then discard them */
905                 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
906                         brcmf_host_event(drvr_priv, &ifidx,
907                                           skb_mac_header(skb),
908                                           &event, &data);
909
910                 ASSERT(ifidx < BRCMF_MAX_IFS && drvr_priv->iflist[ifidx]);
911                 if (drvr_priv->iflist[ifidx] &&
912                     !drvr_priv->iflist[ifidx]->state)
913                         ifp = drvr_priv->iflist[ifidx];
914
915                 if (ifp->net)
916                         ifp->net->last_rx = jiffies;
917
918                 drvr->dstats.rx_bytes += skb->len;
919                 drvr->rx_packets++;     /* Local count */
920
921                 if (in_interrupt()) {
922                         netif_rx(skb);
923                 } else {
924                         /* If the receive is not processed inside an ISR,
925                          * the softirqd must be woken explicitly to service
926                          * the NET_RX_SOFTIRQ.  In 2.6 kernels, this is handled
927                          * by netif_rx_ni(), but in earlier kernels, we need
928                          * to do it manually.
929                          */
930                         netif_rx_ni(skb);
931                 }
932         }
933 }
934
935 void brcmf_event(struct dhd_info *drvr_priv, char *evpkt, int evlen, int ifidx)
936 {
937         /* Linux version has nothing to do */
938         return;
939 }
940
941 void brcmf_txcomplete(dhd_pub_t *drvr, struct sk_buff *txp, bool success)
942 {
943         uint ifidx;
944         dhd_info_t *drvr_priv = drvr->info;
945         struct ethhdr *eh;
946         u16 type;
947
948         brcmf_proto_hdrpull(drvr, &ifidx, txp);
949
950         eh = (struct ethhdr *)(txp->data);
951         type = ntohs(eh->h_proto);
952
953         if (type == ETH_P_PAE)
954                 atomic_dec(&drvr_priv->pend_8021x_cnt);
955
956 }
957
958 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *net)
959 {
960         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(net);
961         dhd_if_t *ifp;
962         int ifidx;
963
964         DHD_TRACE(("%s: Enter\n", __func__));
965
966         ifidx = brcmf_net2idx(drvr_priv, net);
967         if (ifidx == BRCMF_BAD_IF)
968                 return NULL;
969
970         ifp = drvr_priv->iflist[ifidx];
971         ASSERT(drvr_priv && ifp);
972
973         if (drvr_priv->pub.up) {
974                 /* Use the protocol to get dongle stats */
975                 brcmf_proto_dstats(&drvr_priv->pub);
976         }
977
978         /* Copy dongle stats to net device stats */
979         ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
980         ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
981         ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
982         ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
983         ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
984         ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
985         ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
986         ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
987         ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
988
989         return &ifp->stats;
990 }
991
992 /* Retrieve current toe component enables, which are kept
993          as a bitmap in toe_ol iovar */
994 static int brcmf_toe_get(dhd_info_t *drvr_priv, int ifidx, u32 *toe_ol)
995 {
996         struct brcmf_ioctl ioc;
997         char buf[32];
998         int ret;
999
1000         memset(&ioc, 0, sizeof(ioc));
1001
1002         ioc.cmd = BRCMF_C_GET_VAR;
1003         ioc.buf = buf;
1004         ioc.len = (uint) sizeof(buf);
1005         ioc.set = false;
1006
1007         strcpy(buf, "toe_ol");
1008         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
1009         if (ret < 0) {
1010                 /* Check for older dongle image that doesn't support toe_ol */
1011                 if (ret == -EIO) {
1012                         DHD_ERROR(("%s: toe not supported by device\n",
1013                                    brcmf_ifname(&drvr_priv->pub, ifidx)));
1014                         return -EOPNOTSUPP;
1015                 }
1016
1017                 DHD_INFO(("%s: could not get toe_ol: ret=%d\n",
1018                           brcmf_ifname(&drvr_priv->pub, ifidx), ret));
1019                 return ret;
1020         }
1021
1022         memcpy(toe_ol, buf, sizeof(u32));
1023         return 0;
1024 }
1025
1026 /* Set current toe component enables in toe_ol iovar,
1027          and set toe global enable iovar */
1028 static int brcmf_toe_set(dhd_info_t *drvr_priv, int ifidx, u32 toe_ol)
1029 {
1030         struct brcmf_ioctl ioc;
1031         char buf[32];
1032         int toe, ret;
1033
1034         memset(&ioc, 0, sizeof(ioc));
1035
1036         ioc.cmd = BRCMF_C_SET_VAR;
1037         ioc.buf = buf;
1038         ioc.len = (uint) sizeof(buf);
1039         ioc.set = true;
1040
1041         /* Set toe_ol as requested */
1042
1043         strcpy(buf, "toe_ol");
1044         memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
1045
1046         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
1047         if (ret < 0) {
1048                 DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
1049                            brcmf_ifname(&drvr_priv->pub, ifidx), ret));
1050                 return ret;
1051         }
1052
1053         /* Enable toe globally only if any components are enabled. */
1054
1055         toe = (toe_ol != 0);
1056
1057         strcpy(buf, "toe");
1058         memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
1059
1060         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
1061         if (ret < 0) {
1062                 DHD_ERROR(("%s: could not set toe: ret=%d\n",
1063                            brcmf_ifname(&drvr_priv->pub, ifidx), ret));
1064                 return ret;
1065         }
1066
1067         return 0;
1068 }
1069
1070 static void brcmf_ethtool_get_drvinfo(struct net_device *net,
1071                                     struct ethtool_drvinfo *info)
1072 {
1073         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(net);
1074
1075         sprintf(info->driver, KBUILD_MODNAME);
1076         sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
1077         sprintf(info->fw_version, "%s", BCM4329_FW_NAME);
1078         sprintf(info->bus_info, "%s", dev_name(&wl_cfg80211_get_sdio_func()->dev));
1079 }
1080
1081 struct ethtool_ops brcmf_ethtool_ops = {
1082         .get_drvinfo = brcmf_ethtool_get_drvinfo
1083 };
1084
1085 static int brcmf_ethtool(dhd_info_t *drvr_priv, void *uaddr)
1086 {
1087         struct ethtool_drvinfo info;
1088         char drvname[sizeof(info.driver)];
1089         u32 cmd;
1090         struct ethtool_value edata;
1091         u32 toe_cmpnt, csum_dir;
1092         int ret;
1093
1094         DHD_TRACE(("%s: Enter\n", __func__));
1095
1096         /* all ethtool calls start with a cmd word */
1097         if (copy_from_user(&cmd, uaddr, sizeof(u32)))
1098                 return -EFAULT;
1099
1100         switch (cmd) {
1101         case ETHTOOL_GDRVINFO:
1102                 /* Copy out any request driver name */
1103                 if (copy_from_user(&info, uaddr, sizeof(info)))
1104                         return -EFAULT;
1105                 strncpy(drvname, info.driver, sizeof(info.driver));
1106                 drvname[sizeof(info.driver) - 1] = '\0';
1107
1108                 /* clear struct for return */
1109                 memset(&info, 0, sizeof(info));
1110                 info.cmd = cmd;
1111
1112                 /* if dhd requested, identify ourselves */
1113                 if (strcmp(drvname, "?dhd") == 0) {
1114                         sprintf(info.driver, "dhd");
1115                         strcpy(info.version, BRCMF_VERSION_STR);
1116                 }
1117
1118                 /* otherwise, require dongle to be up */
1119                 else if (!drvr_priv->pub.up) {
1120                         DHD_ERROR(("%s: dongle is not up\n", __func__));
1121                         return -ENODEV;
1122                 }
1123
1124                 /* finally, report dongle driver type */
1125                 else if (drvr_priv->pub.iswl)
1126                         sprintf(info.driver, "wl");
1127                 else
1128                         sprintf(info.driver, "xx");
1129
1130                 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
1131                 if (copy_to_user(uaddr, &info, sizeof(info)))
1132                         return -EFAULT;
1133                 DHD_CTL(("%s: given %*s, returning %s\n", __func__,
1134                          (int)sizeof(drvname), drvname, info.driver));
1135                 break;
1136
1137                 /* Get toe offload components from dongle */
1138         case ETHTOOL_GRXCSUM:
1139         case ETHTOOL_GTXCSUM:
1140                 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
1141                 if (ret < 0)
1142                         return ret;
1143
1144                 csum_dir =
1145                     (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1146
1147                 edata.cmd = cmd;
1148                 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
1149
1150                 if (copy_to_user(uaddr, &edata, sizeof(edata)))
1151                         return -EFAULT;
1152                 break;
1153
1154                 /* Set toe offload components in dongle */
1155         case ETHTOOL_SRXCSUM:
1156         case ETHTOOL_STXCSUM:
1157                 if (copy_from_user(&edata, uaddr, sizeof(edata)))
1158                         return -EFAULT;
1159
1160                 /* Read the current settings, update and write back */
1161                 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
1162                 if (ret < 0)
1163                         return ret;
1164
1165                 csum_dir =
1166                     (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1167
1168                 if (edata.data != 0)
1169                         toe_cmpnt |= csum_dir;
1170                 else
1171                         toe_cmpnt &= ~csum_dir;
1172
1173                 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
1174                 if (ret < 0)
1175                         return ret;
1176
1177                 /* If setting TX checksum mode, tell Linux the new mode */
1178                 if (cmd == ETHTOOL_STXCSUM) {
1179                         if (edata.data)
1180                                 drvr_priv->iflist[0]->net->features |=
1181                                     NETIF_F_IP_CSUM;
1182                         else
1183                                 drvr_priv->iflist[0]->net->features &=
1184                                     ~NETIF_F_IP_CSUM;
1185                 }
1186
1187                 break;
1188
1189         default:
1190                 return -EOPNOTSUPP;
1191         }
1192
1193         return 0;
1194 }
1195
1196 static int brcmf_netdev_ioctl_entry(struct net_device *net, struct ifreq *ifr,
1197                                     int cmd)
1198 {
1199         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(net);
1200         dhd_ioctl_t ioc;
1201         int bcmerror = 0;
1202         int buflen = 0;
1203         void *buf = NULL;
1204         uint driver = 0;
1205         int ifidx;
1206         bool is_set_key_cmd;
1207
1208         ifidx = brcmf_net2idx(drvr_priv, net);
1209         DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __func__, ifidx, cmd));
1210
1211         if (ifidx == BRCMF_BAD_IF)
1212                 return -1;
1213
1214         if (cmd == SIOCETHTOOL)
1215                 return brcmf_ethtool(drvr_priv, (void *)ifr->ifr_data);
1216
1217         if (cmd != SIOCDEVPRIVATE)
1218                 return -EOPNOTSUPP;
1219
1220         memset(&ioc, 0, sizeof(ioc));
1221
1222         /* Copy the ioc control structure part of ioctl request */
1223         if (copy_from_user(&ioc, ifr->ifr_data, sizeof(struct brcmf_ioctl))) {
1224                 bcmerror = -EINVAL;
1225                 goto done;
1226         }
1227
1228         /* Copy out any buffer passed */
1229         if (ioc.buf) {
1230                 buflen = min_t(int, ioc.len, BRCMF_IOCTL_MAXLEN);
1231                 /* optimization for direct ioctl calls from kernel */
1232                 /*
1233                    if (segment_eq(get_fs(), KERNEL_DS)) {
1234                    buf = ioc.buf;
1235                    } else {
1236                  */
1237                 {
1238                         buf = kmalloc(buflen, GFP_ATOMIC);
1239                         if (!buf) {
1240                                 bcmerror = -ENOMEM;
1241                                 goto done;
1242                         }
1243                         if (copy_from_user(buf, ioc.buf, buflen)) {
1244                                 bcmerror = -EINVAL;
1245                                 goto done;
1246                         }
1247                 }
1248         }
1249
1250         /* To differentiate between wl and dhd read 4 more byes */
1251         if ((copy_from_user(&driver, (char *)ifr->ifr_data +
1252                             sizeof(struct brcmf_ioctl), sizeof(uint)) != 0)) {
1253                 bcmerror = -EINVAL;
1254                 goto done;
1255         }
1256
1257         if (!capable(CAP_NET_ADMIN)) {
1258                 bcmerror = -EPERM;
1259                 goto done;
1260         }
1261
1262         /* check for local dhd ioctl and handle it */
1263         if (driver == BRCMF_IOCTL_MAGIC) {
1264                 bcmerror = brcmf_c_ioctl((void *)&drvr_priv->pub, &ioc, buf, buflen);
1265                 if (bcmerror)
1266                         drvr_priv->pub.bcmerror = bcmerror;
1267                 goto done;
1268         }
1269
1270         /* send to dongle (must be up, and wl) */
1271         if ((drvr_priv->pub.busstate != DHD_BUS_DATA)) {
1272                 DHD_ERROR(("%s DONGLE_DOWN,__func__\n", __func__));
1273                 bcmerror = -EIO;
1274                 goto done;
1275         }
1276
1277         if (!drvr_priv->pub.iswl) {
1278                 bcmerror = -EIO;
1279                 goto done;
1280         }
1281
1282         /*
1283          * Intercept BRCMF_C_SET_KEY IOCTL - serialize M4 send and
1284          * set key IOCTL to prevent M4 encryption.
1285          */
1286         is_set_key_cmd = ((ioc.cmd == BRCMF_C_SET_KEY) ||
1287                           ((ioc.cmd == BRCMF_C_SET_VAR) &&
1288                            !(strncmp("wsec_key", ioc.buf, 9))) ||
1289                           ((ioc.cmd == BRCMF_C_SET_VAR) &&
1290                            !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1291         if (is_set_key_cmd)
1292                 brcmf_netdev_wait_pend8021x(net);
1293
1294         bcmerror =
1295             brcmf_proto_ioctl(&drvr_priv->pub, ifidx, (struct brcmf_ioctl *)&ioc,
1296                               buf, buflen);
1297
1298 done:
1299         if (!bcmerror && buf && ioc.buf) {
1300                 if (copy_to_user(ioc.buf, buf, buflen))
1301                         bcmerror = -EFAULT;
1302         }
1303
1304         kfree(buf);
1305
1306         if (bcmerror > 0)
1307                 bcmerror = 0;
1308
1309         return bcmerror;
1310 }
1311
1312 static int brcmf_netdev_stop(struct net_device *net)
1313 {
1314 #if !defined(IGNORE_ETH0_DOWN)
1315         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(net);
1316
1317         DHD_TRACE(("%s: Enter\n", __func__));
1318         wl_cfg80211_down();
1319         if (drvr_priv->pub.up == 0)
1320                 return 0;
1321
1322         /* Set state and stop OS transmissions */
1323         drvr_priv->pub.up = 0;
1324         netif_stop_queue(net);
1325 #else
1326         DHD_ERROR(("BYPASS %s:due to BRCM compilation : under investigation\n",
1327                 __func__));
1328 #endif                          /* !defined(IGNORE_ETH0_DOWN) */
1329
1330         return 0;
1331 }
1332
1333 static int brcmf_netdev_open(struct net_device *net)
1334 {
1335         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(net);
1336         u32 toe_ol;
1337         int ifidx = brcmf_net2idx(drvr_priv, net);
1338         s32 ret = 0;
1339
1340         DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1341
1342         if (ifidx == 0) {       /* do it only for primary eth0 */
1343
1344                 /* try to bring up bus */
1345                 ret = brcmf_bus_start(&drvr_priv->pub);
1346                 if (ret != 0) {
1347                         DHD_ERROR(("%s: failed with code %d\n", __func__, ret));
1348                         return -1;
1349                 }
1350                 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
1351
1352                 memcpy(net->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
1353
1354                 /* Get current TOE mode from dongle */
1355                 if (brcmf_toe_get(drvr_priv, ifidx, &toe_ol) >= 0
1356                     && (toe_ol & TOE_TX_CSUM_OL) != 0)
1357                         drvr_priv->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
1358                 else
1359                         drvr_priv->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
1360         }
1361         /* Allow transmit calls */
1362         netif_start_queue(net);
1363         drvr_priv->pub.up = 1;
1364         if (unlikely(wl_cfg80211_up())) {
1365                 DHD_ERROR(("%s: failed to bring up cfg80211\n",
1366                            __func__));
1367                 return -1;
1368         }
1369
1370         return ret;
1371 }
1372
1373 int
1374 brcmf_add_if(dhd_info_t *drvr_priv, int ifidx, void *handle, char *name,
1375            u8 *mac_addr, u32 flags, u8 bssidx)
1376 {
1377         dhd_if_t *ifp;
1378
1379         DHD_TRACE(("%s: idx %d, handle->%p\n", __func__, ifidx, handle));
1380
1381         ASSERT(drvr_priv && (ifidx < BRCMF_MAX_IFS));
1382
1383         ifp = drvr_priv->iflist[ifidx];
1384         if (!ifp) {
1385                 ifp = kmalloc(sizeof(dhd_if_t), GFP_ATOMIC);
1386                 if (!ifp) {
1387                         DHD_ERROR(("%s: OOM - dhd_if_t\n", __func__));
1388                         return -ENOMEM;
1389                 }
1390         }
1391
1392         memset(ifp, 0, sizeof(dhd_if_t));
1393         ifp->info = drvr_priv;
1394         drvr_priv->iflist[ifidx] = ifp;
1395         strlcpy(ifp->name, name, IFNAMSIZ);
1396         if (mac_addr != NULL)
1397                 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
1398
1399         if (handle == NULL) {
1400                 ifp->state = BRCMF_E_IF_ADD;
1401                 ifp->idx = ifidx;
1402                 ASSERT(drvr_priv->sysioc_tsk);
1403                 up(&drvr_priv->sysioc_sem);
1404         } else
1405                 ifp->net = (struct net_device *)handle;
1406
1407         return 0;
1408 }
1409
1410 void brcmf_del_if(dhd_info_t *drvr_priv, int ifidx)
1411 {
1412         dhd_if_t *ifp;
1413
1414         DHD_TRACE(("%s: idx %d\n", __func__, ifidx));
1415
1416         ASSERT(drvr_priv && ifidx && (ifidx < BRCMF_MAX_IFS));
1417         ifp = drvr_priv->iflist[ifidx];
1418         if (!ifp) {
1419                 DHD_ERROR(("%s: Null interface\n", __func__));
1420                 return;
1421         }
1422
1423         ifp->state = BRCMF_E_IF_DEL;
1424         ifp->idx = ifidx;
1425         ASSERT(drvr_priv->sysioc_tsk);
1426         up(&drvr_priv->sysioc_sem);
1427 }
1428
1429 dhd_pub_t *brcmf_attach(struct dhd_bus *bus, uint bus_hdrlen)
1430 {
1431         dhd_info_t *drvr_priv = NULL;
1432         struct net_device *net;
1433
1434         DHD_TRACE(("%s: Enter\n", __func__));
1435
1436         /* Allocate etherdev, including space for private structure */
1437         net = alloc_etherdev(sizeof(drvr_priv));
1438         if (!net) {
1439                 DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
1440                 goto fail;
1441         }
1442
1443         /* Allocate primary dhd_info */
1444         drvr_priv = kzalloc(sizeof(dhd_info_t), GFP_ATOMIC);
1445         if (!drvr_priv) {
1446                 DHD_ERROR(("%s: OOM - alloc dhd_info\n", __func__));
1447                 goto fail;
1448         }
1449
1450         /*
1451          * Save the dhd_info into the priv
1452          */
1453         memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1454
1455         /* Set network interface name if it was provided as module parameter */
1456         if (iface_name[0]) {
1457                 int len;
1458                 char ch;
1459                 strncpy(net->name, iface_name, IFNAMSIZ);
1460                 net->name[IFNAMSIZ - 1] = 0;
1461                 len = strlen(net->name);
1462                 ch = net->name[len - 1];
1463                 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1464                         strcat(net->name, "%d");
1465         }
1466
1467         if (brcmf_add_if(drvr_priv, 0, (void *)net, net->name, NULL, 0, 0) ==
1468             BRCMF_BAD_IF)
1469                 goto fail;
1470
1471         net->netdev_ops = NULL;
1472         sema_init(&drvr_priv->proto_sem, 1);
1473         /* Initialize other structure content */
1474         init_waitqueue_head(&drvr_priv->ioctl_resp_wait);
1475
1476         /* Link to info module */
1477         drvr_priv->pub.info = drvr_priv;
1478
1479         /* Link to bus module */
1480         drvr_priv->pub.bus = bus;
1481         drvr_priv->pub.hdrlen = bus_hdrlen;
1482
1483         /* Attach and link in the protocol */
1484         if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
1485                 DHD_ERROR(("dhd_prot_attach failed\n"));
1486                 goto fail;
1487         }
1488
1489         /* Attach and link in the cfg80211 */
1490         if (unlikely(wl_cfg80211_attach(net, &drvr_priv->pub))) {
1491                 DHD_ERROR(("wl_cfg80211_attach failed\n"));
1492                 goto fail;
1493         }
1494
1495         if (brcmf_sysioc) {
1496                 sema_init(&drvr_priv->sysioc_sem, 0);
1497                 drvr_priv->sysioc_tsk = kthread_run(_brcmf_sysioc_thread, drvr_priv,
1498                                                 "_dhd_sysioc");
1499                 if (IS_ERR(drvr_priv->sysioc_tsk)) {
1500                         printk(KERN_WARNING
1501                                 "_dhd_sysioc thread failed to start\n");
1502                         drvr_priv->sysioc_tsk = NULL;
1503                 }
1504         } else
1505                 drvr_priv->sysioc_tsk = NULL;
1506
1507         /*
1508          * Save the dhd_info into the priv
1509          */
1510         memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1511
1512 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
1513         g_bus = bus;
1514 #endif
1515 #if defined(CONFIG_PM_SLEEP)
1516         atomic_set(&brcmf_mmc_suspend, false);
1517 #endif  /* defined(CONFIG_PM_SLEEP) */
1518         /* && defined(DHD_GPL) */
1519         /* Init lock suspend to prevent kernel going to suspend */
1520 #ifdef CONFIG_HAS_EARLYSUSPEND
1521         drvr_priv->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 20;
1522         drvr_priv->early_suspend.suspend = brcmf_early_suspend;
1523         drvr_priv->early_suspend.resume = brcmf_late_resume;
1524         register_early_suspend(&drvr_priv->early_suspend);
1525 #endif
1526
1527         return &drvr_priv->pub;
1528
1529 fail:
1530         if (net)
1531                 free_netdev(net);
1532         if (drvr_priv)
1533                 brcmf_detach(&drvr_priv->pub);
1534
1535         return NULL;
1536 }
1537
1538 int brcmf_bus_start(dhd_pub_t *drvr)
1539 {
1540         int ret = -1;
1541         dhd_info_t *drvr_priv = drvr->info;
1542         /* Room for "event_msgs" + '\0' + bitvec */
1543         char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1544
1545         ASSERT(drvr_priv);
1546
1547         DHD_TRACE(("%s:\n", __func__));
1548
1549         /* Bring up the bus */
1550         ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub, true);
1551         if (ret != 0) {
1552                 DHD_ERROR(("%s, brcmf_sdbrcm_bus_init failed %d\n", __func__,
1553                            ret));
1554                 return ret;
1555         }
1556
1557         /* If bus is not ready, can't come up */
1558         if (drvr_priv->pub.busstate != DHD_BUS_DATA) {
1559                 DHD_ERROR(("%s failed bus is not ready\n", __func__));
1560                 return -ENODEV;
1561         }
1562
1563         brcmu_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1564                       iovbuf, sizeof(iovbuf));
1565         brcmf_proto_cdc_query_ioctl(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1566                                     sizeof(iovbuf));
1567         memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1568
1569         setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1570         setbit(drvr->eventmask, BRCMF_E_PRUNE);
1571         setbit(drvr->eventmask, BRCMF_E_AUTH);
1572         setbit(drvr->eventmask, BRCMF_E_REASSOC);
1573         setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1574         setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1575         setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1576         setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1577         setbit(drvr->eventmask, BRCMF_E_JOIN);
1578         setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1579         setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1580         setbit(drvr->eventmask, BRCMF_E_LINK);
1581         setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1582         setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1583         setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1584         setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1585         setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1586         setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1587
1588 /* enable dongle roaming event */
1589
1590         drvr->pktfilter_count = 1;
1591         /* Setup filter to allow only unicast */
1592         drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1593
1594         /* Bus is ready, do any protocol initialization */
1595         ret = brcmf_proto_init(&drvr_priv->pub);
1596         if (ret < 0)
1597                 return ret;
1598
1599         return 0;
1600 }
1601
1602 int brcmf_iovar(dhd_pub_t *drvr, int ifidx, char *name, char *cmd_buf,
1603           uint cmd_len, int set)
1604 {
1605         char buf[strlen(name) + 1 + cmd_len];
1606         int len = sizeof(buf);
1607         struct brcmf_ioctl ioc;
1608         int ret;
1609
1610         len = brcmu_mkiovar(name, cmd_buf, cmd_len, buf, len);
1611
1612         memset(&ioc, 0, sizeof(ioc));
1613
1614         ioc.cmd = set ? BRCMF_C_SET_VAR : BRCMF_C_GET_VAR;
1615         ioc.buf = buf;
1616         ioc.len = len;
1617         ioc.set = set;
1618
1619         ret = brcmf_proto_ioctl(drvr, ifidx, &ioc, ioc.buf, ioc.len);
1620         if (!set && ret >= 0)
1621                 memcpy(cmd_buf, buf, cmd_len);
1622
1623         return ret;
1624 }
1625
1626 static struct net_device_ops brcmf_netdev_ops_pri = {
1627         .ndo_open = brcmf_netdev_open,
1628         .ndo_stop = brcmf_netdev_stop,
1629         .ndo_get_stats = brcmf_netdev_get_stats,
1630         .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1631         .ndo_start_xmit = brcmf_netdev_start_xmit,
1632         .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1633         .ndo_set_multicast_list = brcmf_netdev_set_multicast_list
1634 };
1635
1636 int brcmf_net_attach(dhd_pub_t *drvr, int ifidx)
1637 {
1638         dhd_info_t *drvr_priv = drvr->info;
1639         struct net_device *net;
1640         u8 temp_addr[ETH_ALEN] = {
1641                 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1642
1643         DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1644
1645         ASSERT(drvr_priv && drvr_priv->iflist[ifidx]);
1646
1647         net = drvr_priv->iflist[ifidx]->net;
1648         ASSERT(net);
1649
1650         ASSERT(!net->netdev_ops);
1651         net->netdev_ops = &brcmf_netdev_ops_pri;
1652
1653         /*
1654          * We have to use the primary MAC for virtual interfaces
1655          */
1656         if (ifidx != 0) {
1657                 /* for virtual interfaces use the primary MAC  */
1658                 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1659
1660         }
1661
1662         if (ifidx == 1) {
1663                 DHD_TRACE(("%s ACCESS POINT MAC:\n", __func__));
1664                 /*  ACCESSPOINT INTERFACE CASE */
1665                 temp_addr[0] |= 0X02;   /* set bit 2 ,
1666                          - Locally Administered address  */
1667
1668         }
1669         net->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1670         net->ethtool_ops = &brcmf_ethtool_ops;
1671
1672         drvr_priv->pub.rxsz = net->mtu + net->hard_header_len +
1673                                 drvr_priv->pub.hdrlen;
1674
1675         memcpy(net->dev_addr, temp_addr, ETH_ALEN);
1676
1677         if (register_netdev(net) != 0) {
1678                 DHD_ERROR(("%s: couldn't register the net device\n",
1679                         __func__));
1680                 goto fail;
1681         }
1682
1683         DHD_INFO(("%s: Broadcom Dongle Host Driver\n", net->name));
1684
1685         return 0;
1686
1687 fail:
1688         net->netdev_ops = NULL;
1689         return -EBADE;
1690 }
1691
1692 static void brcmf_bus_detach(dhd_pub_t *drvr)
1693 {
1694         dhd_info_t *drvr_priv;
1695
1696         DHD_TRACE(("%s: Enter\n", __func__));
1697
1698         if (drvr) {
1699                 drvr_priv = drvr->info;
1700                 if (drvr_priv) {
1701                         /* Stop the protocol module */
1702                         brcmf_proto_stop(&drvr_priv->pub);
1703
1704                         /* Stop the bus module */
1705                         brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus, true);
1706                 }
1707         }
1708 }
1709
1710 void brcmf_detach(dhd_pub_t *drvr)
1711 {
1712         dhd_info_t *drvr_priv;
1713
1714         DHD_TRACE(("%s: Enter\n", __func__));
1715
1716         if (drvr) {
1717                 drvr_priv = drvr->info;
1718                 if (drvr_priv) {
1719                         dhd_if_t *ifp;
1720                         int i;
1721
1722 #if defined(CONFIG_HAS_EARLYSUSPEND)
1723                         if (drvr_priv->early_suspend.suspend)
1724                                 unregister_early_suspend(
1725                                         &drvr_priv->early_suspend);
1726 #endif                          /* defined(CONFIG_HAS_EARLYSUSPEND) */
1727
1728                         for (i = 1; i < BRCMF_MAX_IFS; i++)
1729                                 if (drvr_priv->iflist[i])
1730                                         brcmf_del_if(drvr_priv, i);
1731
1732                         ifp = drvr_priv->iflist[0];
1733                         ASSERT(ifp);
1734                         if (ifp->net->netdev_ops == &brcmf_netdev_ops_pri) {
1735                                 brcmf_netdev_stop(ifp->net);
1736                                 unregister_netdev(ifp->net);
1737                         }
1738
1739                         if (drvr_priv->sysioc_tsk) {
1740                                 send_sig(SIGTERM, drvr_priv->sysioc_tsk, 1);
1741                                 kthread_stop(drvr_priv->sysioc_tsk);
1742                                 drvr_priv->sysioc_tsk = NULL;
1743                         }
1744
1745                         brcmf_bus_detach(drvr);
1746
1747                         if (drvr->prot)
1748                                 brcmf_proto_detach(drvr);
1749
1750                         wl_cfg80211_detach();
1751
1752                         /* && defined(DHD_GPL) */
1753                         free_netdev(ifp->net);
1754                         kfree(ifp);
1755                         kfree(drvr_priv);
1756                 }
1757         }
1758 }
1759
1760 static void __exit brcmf_module_cleanup(void)
1761 {
1762         DHD_TRACE(("%s: Enter\n", __func__));
1763
1764         dhd_bus_unregister();
1765 }
1766
1767 static int __init brcmf_module_init(void)
1768 {
1769         int error;
1770
1771         DHD_TRACE(("%s: Enter\n", __func__));
1772
1773         error = dhd_bus_register();
1774
1775         if (error) {
1776                 DHD_ERROR(("%s: dhd_bus_register failed\n", __func__));
1777                 goto failed;
1778         }
1779         return 0;
1780
1781 failed:
1782         return -EINVAL;
1783 }
1784
1785 module_init(brcmf_module_init);
1786 module_exit(brcmf_module_cleanup);
1787
1788 /*
1789  * OS specific functions required to implement DHD driver in OS independent way
1790  */
1791 int brcmf_os_proto_block(dhd_pub_t *drvr)
1792 {
1793         dhd_info_t *drvr_priv = drvr->info;
1794
1795         if (drvr_priv) {
1796                 down(&drvr_priv->proto_sem);
1797                 return 1;
1798         }
1799         return 0;
1800 }
1801
1802 int brcmf_os_proto_unblock(dhd_pub_t *drvr)
1803 {
1804         dhd_info_t *drvr_priv = drvr->info;
1805
1806         if (drvr_priv) {
1807                 up(&drvr_priv->proto_sem);
1808                 return 1;
1809         }
1810
1811         return 0;
1812 }
1813
1814 unsigned int brcmf_os_get_ioctl_resp_timeout(void)
1815 {
1816         return (unsigned int)brcmf_ioctl_timeout_msec;
1817 }
1818
1819 void brcmf_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
1820 {
1821         brcmf_ioctl_timeout_msec = (int)timeout_msec;
1822 }
1823
1824 int brcmf_os_ioctl_resp_wait(dhd_pub_t *drvr, uint *condition, bool *pending)
1825 {
1826         dhd_info_t *drvr_priv = drvr->info;
1827         DECLARE_WAITQUEUE(wait, current);
1828         int timeout = brcmf_ioctl_timeout_msec;
1829
1830         /* Convert timeout in millsecond to jiffies */
1831         timeout = timeout * HZ / 1000;
1832
1833         /* Wait until control frame is available */
1834         add_wait_queue(&drvr_priv->ioctl_resp_wait, &wait);
1835         set_current_state(TASK_INTERRUPTIBLE);
1836
1837         while (!(*condition) && (!signal_pending(current) && timeout))
1838                 timeout = schedule_timeout(timeout);
1839
1840         if (signal_pending(current))
1841                 *pending = true;
1842
1843         set_current_state(TASK_RUNNING);
1844         remove_wait_queue(&drvr_priv->ioctl_resp_wait, &wait);
1845
1846         return timeout;
1847 }
1848
1849 int brcmf_os_ioctl_resp_wake(dhd_pub_t *drvr)
1850 {
1851         dhd_info_t *drvr_priv = drvr->info;
1852
1853         if (waitqueue_active(&drvr_priv->ioctl_resp_wait))
1854                 wake_up_interruptible(&drvr_priv->ioctl_resp_wait);
1855
1856         return 0;
1857 }
1858
1859 static int brcmf_host_event(dhd_info_t *drvr_priv, int *ifidx, void *pktdata,
1860                             brcmf_event_msg_t *event, void **data)
1861 {
1862         int bcmerror = 0;
1863
1864         ASSERT(drvr_priv != NULL);
1865
1866         bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
1867         if (bcmerror != 0)
1868                 return bcmerror;
1869
1870         ASSERT(drvr_priv->iflist[*ifidx] != NULL);
1871         ASSERT(drvr_priv->iflist[*ifidx]->net != NULL);
1872         if (drvr_priv->iflist[*ifidx]->net)
1873                 wl_cfg80211_event(drvr_priv->iflist[*ifidx]->net, event, *data);
1874
1875         return bcmerror;
1876 }
1877
1878 int brcmf_netdev_reset(struct net_device *dev, u8 flag)
1879 {
1880         dhd_info_t *drvr_priv = *(dhd_info_t **)netdev_priv(dev);
1881
1882         brcmf_bus_devreset(&drvr_priv->pub, flag);
1883
1884         return 1;
1885 }
1886
1887 int brcmf_netdev_set_suspend_disable(struct net_device *dev, int val)
1888 {
1889         dhd_info_t *drvr_priv = *(dhd_info_t **)netdev_priv(dev);
1890         int ret = 0;
1891
1892         if (drvr_priv) {
1893                 ret = drvr_priv->pub.suspend_disable_flag;
1894                 drvr_priv->pub.suspend_disable_flag = val;
1895         }
1896         return ret;
1897 }
1898
1899 int brcmf_netdev_set_suspend(struct net_device *dev, int val)
1900 {
1901         int ret = 0;
1902 #if defined(CONFIG_HAS_EARLYSUSPEND)
1903         dhd_info_t *drvr_priv = *(dhd_info_t **)netdev_priv(dev);
1904
1905         if (drvr_priv) {
1906                 brcmf_os_proto_block(&drvr_priv->pub);
1907                 ret = brcmf_set_suspend(val, &drvr_priv->pub);
1908                 brcmf_os_proto_unblock(&drvr_priv->pub);
1909         }
1910 #endif          /* defined(CONFIG_HAS_EARLYSUSPEND) */
1911         return ret;
1912 }
1913
1914 int brcmf_netdev_set_dtim_skip(struct net_device *dev, int val)
1915 {
1916         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(dev);
1917
1918         if (drvr_priv)
1919                 drvr_priv->pub.dtim_skip = val;
1920
1921         return 0;
1922 }
1923
1924 int brcmf_netdev_set_packet_filter(struct net_device *dev, int val)
1925 {
1926         dhd_info_t *drvr_priv = *(dhd_info_t **) netdev_priv(dev);
1927         int ret = 0;
1928
1929         /* Packet filtering is set only if we still in early-suspend and
1930          * we need either to turn it ON or turn it OFF
1931          * We can always turn it OFF in case of early-suspend, but we turn it
1932          * back ON only if suspend_disable_flag was not set
1933          */
1934         if (drvr_priv && drvr_priv->pub.up) {
1935                 brcmf_os_proto_block(&drvr_priv->pub);
1936                 if (drvr_priv->pub.in_suspend) {
1937                         if (!val || (val && !drvr_priv->pub.suspend_disable_flag))
1938                                 brcmf_set_packet_filter(val, &drvr_priv->pub);
1939                 }
1940                 brcmf_os_proto_unblock(&drvr_priv->pub);
1941         }
1942         return ret;
1943 }
1944
1945 void brcmf_netdev_init_ioctl(struct net_device *dev)
1946 {
1947         dhd_info_t *drvr_priv = *(dhd_info_t **)netdev_priv(dev);
1948
1949         brcmf_c_preinit_ioctls(&drvr_priv->pub);
1950 }
1951
1952 static int brcmf_get_pend_8021x_cnt(dhd_info_t *drvr_priv)
1953 {
1954         return atomic_read(&drvr_priv->pend_8021x_cnt);
1955 }
1956
1957 #define MAX_WAIT_FOR_8021X_TX   10
1958
1959 int brcmf_netdev_wait_pend8021x(struct net_device *dev)
1960 {
1961         dhd_info_t *drvr_priv = *(dhd_info_t **)netdev_priv(dev);
1962         int timeout = 10 * HZ / 1000;
1963         int ntimes = MAX_WAIT_FOR_8021X_TX;
1964         int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1965
1966         while (ntimes && pend) {
1967                 if (pend) {
1968                         set_current_state(TASK_INTERRUPTIBLE);
1969                         schedule_timeout(timeout);
1970                         set_current_state(TASK_RUNNING);
1971                         ntimes--;
1972                 }
1973                 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1974         }
1975         return pend;
1976 }
1977
1978 #ifdef BCMDBG
1979 int brcmf_write_to_file(dhd_pub_t *drvr, u8 *buf, int size)
1980 {
1981         int ret = 0;
1982         struct file *fp;
1983         mm_segment_t old_fs;
1984         loff_t pos = 0;
1985
1986         /* change to KERNEL_DS address limit */
1987         old_fs = get_fs();
1988         set_fs(KERNEL_DS);
1989
1990         /* open file to write */
1991         fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1992         if (!fp) {
1993                 DHD_ERROR(("%s: open file error\n", __func__));
1994                 ret = -1;
1995                 goto exit;
1996         }
1997
1998         /* Write buf to file */
1999         fp->f_op->write(fp, buf, size, &pos);
2000
2001 exit:
2002         /* free buf before return */
2003         kfree(buf);
2004         /* close file before return */
2005         if (fp)
2006                 filp_close(fp, current->files);
2007         /* restore previous address limit */
2008         set_fs(old_fs);
2009
2010         return ret;
2011 }
2012 #endif                          /* BCMDBG */
2013
2014 #if defined(BCMDBG)
2015 void osl_assert(char *exp, char *file, int line)
2016 {
2017         char tempbuf[256];
2018         char *basename;
2019
2020         basename = strrchr(file, '/');
2021         /* skip the '/' */
2022         if (basename)
2023                 basename++;
2024
2025         if (!basename)
2026                 basename = file;
2027
2028         snprintf(tempbuf, 256,
2029                  "assertion \"%s\" failed: file \"%s\", line %d\n", exp,
2030                  basename, line);
2031
2032         /*
2033          * Print assert message and give it time to
2034          * be written to /var/log/messages
2035          */
2036         if (!in_interrupt()) {
2037                 const int delay = 3;
2038                 printk(KERN_ERR "%s", tempbuf);
2039                 printk(KERN_ERR "panic in %d seconds\n", delay);
2040                 set_current_state(TASK_INTERRUPTIBLE);
2041                 schedule_timeout(delay * HZ);
2042         }
2043
2044         switch (g_assert_type) {
2045         case 0:
2046                 panic(KERN_ERR "%s", tempbuf);
2047                 break;
2048         case 1:
2049                 printk(KERN_ERR "%s", tempbuf);
2050                 BUG();
2051                 break;
2052         case 2:
2053                 printk(KERN_ERR "%s", tempbuf);
2054                 break;
2055         default:
2056                 break;
2057         }
2058 }
2059 #endif                          /* defined(BCMDBG) */