26a39ee894fe300096470510fd4fea1dea4fb199
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmfmac / dhd_linux.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #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 <linux/hardirq.h>
32 #include <linux/mutex.h>
33 #include <linux/wait.h>
34 #include <net/cfg80211.h>
35 #include <defs.h>
36 #include <brcmu_utils.h>
37 #include <brcmu_wifi.h>
38
39 #include "dhd.h"
40 #include "dhd_bus.h"
41 #include "dhd_proto.h"
42 #include "dhd_dbg.h"
43 #include "wl_cfg80211.h"
44 #include "bcmchip.h"
45
46 MODULE_AUTHOR("Broadcom Corporation");
47 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
48 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
49 MODULE_LICENSE("Dual BSD/GPL");
50
51
52 /* Interface control information */
53 struct brcmf_if {
54         struct brcmf_info *info;        /* back pointer to brcmf_info */
55         /* OS/stack specifics */
56         struct net_device *net;
57         struct net_device_stats stats;
58         int idx;                /* iface idx in dongle */
59         int state;              /* interface state */
60         uint subunit;           /* subunit */
61         u8 mac_addr[ETH_ALEN];  /* assigned MAC address */
62         bool attached;          /* Delayed attachment when unset */
63         bool txflowcontrol;     /* Per interface flow control indicator */
64         char name[IFNAMSIZ];    /* linux interface name */
65 };
66
67 /* Local private structure (extension of pub) */
68 struct brcmf_info {
69         struct brcmf_pub pub;
70
71         /* OS/stack specifics */
72         struct brcmf_if *iflist[BRCMF_MAX_IFS];
73
74         struct mutex proto_block;
75
76         /* Thread to issue ioctl for multicast */
77         struct task_struct *sysioc_tsk;
78         wait_queue_head_t sysioc_waitq;
79         bool set_multicast;
80         bool set_macaddress;
81         u8 macvalue[ETH_ALEN];
82         atomic_t pend_8021x_cnt;
83 };
84
85 /* Error bits */
86 module_param(brcmf_msg_level, int, 0);
87
88 /* Spawn a thread for system ioctls (set mac, set mcast) */
89 uint brcmf_sysioc = true;
90 module_param(brcmf_sysioc, uint, 0);
91
92 /* ARP offload agent mode : Enable ARP Host Auto-Reply
93 and ARP Peer Auto-Reply */
94 uint brcmf_arp_mode = 0xb;
95 module_param(brcmf_arp_mode, uint, 0);
96
97 /* ARP offload enable */
98 uint brcmf_arp_enable = true;
99 module_param(brcmf_arp_enable, uint, 0);
100
101 /* Global Pkt filter enable control */
102 uint brcmf_pkt_filter_enable = true;
103 module_param(brcmf_pkt_filter_enable, uint, 0);
104
105 /*  Pkt filter init setup */
106 uint brcmf_pkt_filter_init;
107 module_param(brcmf_pkt_filter_init, uint, 0);
108
109 /* Pkt filter mode control */
110 uint brcmf_master_mode = true;
111 module_param(brcmf_master_mode, uint, 0);
112
113 /* Contorl fw roaming */
114 uint brcmf_roam = 1;
115
116 /* Control radio state */
117 uint brcmf_radio_up = 1;
118
119 /* Network inteface name */
120 char iface_name[IFNAMSIZ] = "wlan";
121 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
122
123 static int brcmf_net2idx(struct brcmf_info *drvr_priv, struct net_device *net)
124 {
125         int i = 0;
126
127         while (i < BRCMF_MAX_IFS) {
128                 if (drvr_priv->iflist[i] && (drvr_priv->iflist[i]->net == net))
129                         return i;
130                 i++;
131         }
132
133         return BRCMF_BAD_IF;
134 }
135
136 int brcmf_ifname2idx(struct brcmf_info *drvr_priv, char *name)
137 {
138         int i = BRCMF_MAX_IFS;
139
140         if (name == NULL || *name == '\0')
141                 return 0;
142
143         while (--i > 0)
144                 if (drvr_priv->iflist[i]
145                     && !strncmp(drvr_priv->iflist[i]->name, name, IFNAMSIZ))
146                         break;
147
148         brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
149
150         return i;               /* default - the primary interface */
151 }
152
153 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
154 {
155         struct brcmf_info *drvr_priv = drvr->info;
156
157         if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
158                 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
159                 return "<if_bad>";
160         }
161
162         if (drvr_priv->iflist[ifidx] == NULL) {
163                 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
164                 return "<if_null>";
165         }
166
167         if (drvr_priv->iflist[ifidx]->net)
168                 return drvr_priv->iflist[ifidx]->net->name;
169
170         return "<if_none>";
171 }
172
173 static void _brcmf_set_multicast_list(struct brcmf_info *drvr_priv, int ifidx)
174 {
175         struct net_device *dev;
176         struct netdev_hw_addr *ha;
177         u32 allmulti, cnt;
178
179         struct brcmf_ioctl ioc;
180         char *buf, *bufp;
181         uint buflen;
182         int ret;
183
184         dev = drvr_priv->iflist[ifidx]->net;
185         cnt = netdev_mc_count(dev);
186
187         /* Determine initial value of allmulti flag */
188         allmulti = (dev->flags & IFF_ALLMULTI) ? true : false;
189
190         /* Send down the multicast list first. */
191
192         buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
193         bufp = buf = kmalloc(buflen, GFP_ATOMIC);
194         if (!bufp) {
195                 brcmf_dbg(ERROR, "%s: out of memory for mcast_list, cnt %d\n",
196                           brcmf_ifname(&drvr_priv->pub, ifidx), cnt);
197                 return;
198         }
199
200         strcpy(bufp, "mcast_list");
201         bufp += strlen("mcast_list") + 1;
202
203         cnt = cpu_to_le32(cnt);
204         memcpy(bufp, &cnt, sizeof(cnt));
205         bufp += sizeof(cnt);
206
207         netdev_for_each_mc_addr(ha, dev) {
208                 if (!cnt)
209                         break;
210                 memcpy(bufp, ha->addr, ETH_ALEN);
211                 bufp += ETH_ALEN;
212                 cnt--;
213         }
214
215         memset(&ioc, 0, sizeof(ioc));
216         ioc.cmd = BRCMF_C_SET_VAR;
217         ioc.buf = buf;
218         ioc.len = buflen;
219         ioc.set = true;
220
221         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
222         if (ret < 0) {
223                 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
224                           brcmf_ifname(&drvr_priv->pub, ifidx), cnt);
225                 allmulti = cnt ? true : allmulti;
226         }
227
228         kfree(buf);
229
230         /* Now send the allmulti setting.  This is based on the setting in the
231          * net_device flags, but might be modified above to be turned on if we
232          * were trying to set some addresses and dongle rejected it...
233          */
234
235         buflen = sizeof("allmulti") + sizeof(allmulti);
236         buf = kmalloc(buflen, GFP_ATOMIC);
237         if (!buf) {
238                 brcmf_dbg(ERROR, "%s: out of memory for allmulti\n",
239                           brcmf_ifname(&drvr_priv->pub, ifidx));
240                 return;
241         }
242         allmulti = cpu_to_le32(allmulti);
243
244         if (!brcmu_mkiovar
245             ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
246                 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
247                           brcmf_ifname(&drvr_priv->pub, ifidx),
248                           (int)sizeof(allmulti), buflen);
249                 kfree(buf);
250                 return;
251         }
252
253         memset(&ioc, 0, sizeof(ioc));
254         ioc.cmd = BRCMF_C_SET_VAR;
255         ioc.buf = buf;
256         ioc.len = buflen;
257         ioc.set = true;
258
259         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
260         if (ret < 0) {
261                 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
262                           brcmf_ifname(&drvr_priv->pub, ifidx),
263                           le32_to_cpu(allmulti));
264         }
265
266         kfree(buf);
267
268         /* Finally, pick up the PROMISC flag as well, like the NIC
269                  driver does */
270
271         allmulti = (dev->flags & IFF_PROMISC) ? true : false;
272         allmulti = cpu_to_le32(allmulti);
273
274         memset(&ioc, 0, sizeof(ioc));
275         ioc.cmd = BRCMF_C_SET_PROMISC;
276         ioc.buf = &allmulti;
277         ioc.len = sizeof(allmulti);
278         ioc.set = true;
279
280         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
281         if (ret < 0) {
282                 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
283                           brcmf_ifname(&drvr_priv->pub, ifidx),
284                           le32_to_cpu(allmulti));
285         }
286 }
287
288 static int
289 _brcmf_set_mac_address(struct brcmf_info *drvr_priv, int ifidx, u8 *addr)
290 {
291         char buf[32];
292         struct brcmf_ioctl ioc;
293         int ret;
294
295         brcmf_dbg(TRACE, "enter\n");
296         if (!brcmu_mkiovar("cur_etheraddr", (char *)addr, ETH_ALEN, buf, 32)) {
297                 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
298                           brcmf_ifname(&drvr_priv->pub, ifidx));
299                 return -1;
300         }
301         memset(&ioc, 0, sizeof(ioc));
302         ioc.cmd = BRCMF_C_SET_VAR;
303         ioc.buf = buf;
304         ioc.len = 32;
305         ioc.set = true;
306
307         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
308         if (ret < 0)
309                 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
310                           brcmf_ifname(&drvr_priv->pub, ifidx));
311         else
312                 memcpy(drvr_priv->iflist[ifidx]->net->dev_addr, addr, ETH_ALEN);
313
314         return ret;
315 }
316
317 #ifdef SOFTAP
318 static struct net_device *ap_net_dev;
319 #endif
320
321 /* Virtual interfaces only ((ifp && ifp->info && ifp->idx == true) */
322 static void brcmf_op_if(struct brcmf_if *ifp)
323 {
324         struct brcmf_info *drvr_priv;
325         int ret = 0, err = 0;
326
327         drvr_priv = ifp->info;
328
329         brcmf_dbg(TRACE, "idx %d, state %d\n", ifp->idx, ifp->state);
330
331         switch (ifp->state) {
332         case BRCMF_E_IF_ADD:
333                 /*
334                  * Delete the existing interface before overwriting it
335                  * in case we missed the BRCMF_E_IF_DEL event.
336                  */
337                 if (ifp->net != NULL) {
338                         brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
339                                   ifp->net->name);
340                         netif_stop_queue(ifp->net);
341                         unregister_netdev(ifp->net);
342                         free_netdev(ifp->net);
343                 }
344                 /* Allocate etherdev, including space for private structure */
345                 ifp->net = alloc_etherdev(sizeof(drvr_priv));
346                 if (!ifp->net) {
347                         brcmf_dbg(ERROR, "OOM - alloc_etherdev\n");
348                         ret = -ENOMEM;
349                 }
350                 if (ret == 0) {
351                         strcpy(ifp->net->name, ifp->name);
352                         memcpy(netdev_priv(ifp->net), &drvr_priv,
353                                sizeof(drvr_priv));
354                         err = brcmf_net_attach(&drvr_priv->pub, ifp->idx);
355                         if (err != 0) {
356                                 brcmf_dbg(ERROR, "brcmf_net_attach failed, err %d\n",
357                                           err);
358                                 ret = -EOPNOTSUPP;
359                         } else {
360 #ifdef SOFTAP
361                                 /* semaphore that the soft AP CODE
362                                          waits on */
363                                 struct semaphore ap_eth_sema;
364
365                                 /* save ptr to wl0.1 netdev for use
366                                          in wl_iw.c  */
367                                 ap_net_dev = ifp->net;
368                                 /* signal to the SOFTAP 'sleeper' thread,
369                                          wl0.1 is ready */
370                                 up(&ap_eth_sema);
371 #endif
372                                 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
373                                           current->pid, ifp->net->name);
374                                 ifp->state = 0;
375                         }
376                 }
377                 break;
378         case BRCMF_E_IF_DEL:
379                 if (ifp->net != NULL) {
380                         brcmf_dbg(TRACE, "got 'WLC_E_IF_DEL' state\n");
381                         netif_stop_queue(ifp->net);
382                         unregister_netdev(ifp->net);
383                         ret = BRCMF_DEL_IF;     /* Make sure the free_netdev()
384                                                          is called */
385                 }
386                 break;
387         default:
388                 brcmf_dbg(ERROR, "bad op %d\n", ifp->state);
389                 break;
390         }
391
392         if (ret < 0) {
393                 if (ifp->net)
394                         free_netdev(ifp->net);
395
396                 drvr_priv->iflist[ifp->idx] = NULL;
397                 kfree(ifp);
398 #ifdef SOFTAP
399                 if (ifp->net == ap_net_dev)
400                         ap_net_dev = NULL;      /*  NULL  SOFTAP global
401                                                          wl0.1 as well */
402 #endif                          /*  SOFTAP */
403         }
404 }
405
406 static int _brcmf_sysioc_thread(void *data)
407 {
408         struct brcmf_info *drvr_priv = (struct brcmf_info *) data;
409         int i;
410 #ifdef SOFTAP
411         bool in_ap = false;
412 #endif
413         DECLARE_WAITQUEUE(wait, current);
414         allow_signal(SIGTERM);
415
416         add_wait_queue(&drvr_priv->sysioc_waitq, &wait);
417         while (1) {
418                 prepare_to_wait(&drvr_priv->sysioc_waitq, &wait,
419                                 TASK_INTERRUPTIBLE);
420
421                 /* wait for event */
422                 schedule();
423
424                 if (kthread_should_stop())
425                         break;
426
427                 for (i = 0; i < BRCMF_MAX_IFS; i++) {
428                         struct brcmf_if *ifentry = drvr_priv->iflist[i];
429                         if (ifentry) {
430 #ifdef SOFTAP
431                                 in_ap = (ap_net_dev != NULL);
432 #endif                          /* SOFTAP */
433                                 if (ifentry->state)
434                                         brcmf_op_if(ifentry);
435 #ifdef SOFTAP
436                                 if (drvr_priv->iflist[i] == NULL) {
437                                         brcmf_dbg(TRACE, "interface %d removed!\n",
438                                                   i);
439                                         continue;
440                                 }
441
442                                 if (in_ap && drvr_priv->set_macaddress) {
443                                         brcmf_dbg(TRACE, "attempt to set MAC for %s in AP Mode, blocked.\n",
444                                                   ifentry->net->name);
445                                         drvr_priv->set_macaddress = false;
446                                         continue;
447                                 }
448
449                                 if (in_ap && drvr_priv->set_multicast) {
450                                         brcmf_dbg(TRACE, "attempt to set MULTICAST list for %s in AP Mode, blocked.\n",
451                                                   ifentry->net->name);
452                                         drvr_priv->set_multicast = false;
453                                         continue;
454                                 }
455 #endif                          /* SOFTAP */
456                                 if (drvr_priv->set_multicast) {
457                                         drvr_priv->set_multicast = false;
458                                         _brcmf_set_multicast_list(drvr_priv, i);
459                                 }
460                                 if (drvr_priv->set_macaddress) {
461                                         drvr_priv->set_macaddress = false;
462                                         _brcmf_set_mac_address(drvr_priv, i,
463                                                 drvr_priv->macvalue);
464                                 }
465                         }
466                 }
467         }
468         finish_wait(&drvr_priv->sysioc_waitq, &wait);
469         return 0;
470 }
471
472 static int brcmf_netdev_set_mac_address(struct net_device *dev, void *addr)
473 {
474         int ret = 0;
475
476         struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(dev);
477         struct sockaddr *sa = (struct sockaddr *)addr;
478         int ifidx;
479
480         ifidx = brcmf_net2idx(drvr_priv, dev);
481         if (ifidx == BRCMF_BAD_IF)
482                 return -1;
483
484         memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
485         drvr_priv->set_macaddress = true;
486         wake_up(&drvr_priv->sysioc_waitq);
487         return ret;
488 }
489
490 static void brcmf_netdev_set_multicast_list(struct net_device *dev)
491 {
492         struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(dev);
493         int ifidx;
494
495         ifidx = brcmf_net2idx(drvr_priv, dev);
496         if (ifidx == BRCMF_BAD_IF)
497                 return;
498
499         drvr_priv->set_multicast = true;
500         wake_up(&drvr_priv->sysioc_waitq);
501 }
502
503 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
504 {
505         struct brcmf_info *drvr_priv = drvr->info;
506
507         /* Reject if down */
508         if (!drvr->up || (drvr->busstate == BRCMF_BUS_DOWN))
509                 return -ENODEV;
510
511         /* Update multicast statistic */
512         if (pktbuf->len >= ETH_ALEN) {
513                 u8 *pktdata = (u8 *) (pktbuf->data);
514                 struct ethhdr *eh = (struct ethhdr *)pktdata;
515
516                 if (is_multicast_ether_addr(eh->h_dest))
517                         drvr->tx_multicast++;
518                 if (ntohs(eh->h_proto) == ETH_P_PAE)
519                         atomic_inc(&drvr_priv->pend_8021x_cnt);
520         }
521
522         /* If the protocol uses a data header, apply it */
523         brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
524
525         /* Use bus module to send data frame */
526         return brcmf_sdbrcm_bus_txdata(drvr->bus, pktbuf);
527 }
528
529 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *net)
530 {
531         int ret;
532         struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
533         int ifidx;
534
535         brcmf_dbg(TRACE, "Enter\n");
536
537         /* Reject if down */
538         if (!drvr_priv->pub.up || (drvr_priv->pub.busstate == BRCMF_BUS_DOWN)) {
539                 brcmf_dbg(ERROR, "xmit rejected pub.up=%d busstate=%d\n",
540                           drvr_priv->pub.up, drvr_priv->pub.busstate);
541                 netif_stop_queue(net);
542                 return -ENODEV;
543         }
544
545         ifidx = brcmf_net2idx(drvr_priv, net);
546         if (ifidx == BRCMF_BAD_IF) {
547                 brcmf_dbg(ERROR, "bad ifidx %d\n", ifidx);
548                 netif_stop_queue(net);
549                 return -ENODEV;
550         }
551
552         /* Make sure there's enough room for any header */
553         if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
554                 struct sk_buff *skb2;
555
556                 brcmf_dbg(INFO, "%s: insufficient headroom\n",
557                           brcmf_ifname(&drvr_priv->pub, ifidx));
558                 drvr_priv->pub.tx_realloc++;
559                 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
560                 dev_kfree_skb(skb);
561                 skb = skb2;
562                 if (skb == NULL) {
563                         brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
564                                   brcmf_ifname(&drvr_priv->pub, ifidx));
565                         ret = -ENOMEM;
566                         goto done;
567                 }
568         }
569
570         ret = brcmf_sendpkt(&drvr_priv->pub, ifidx, skb);
571
572 done:
573         if (ret)
574                 drvr_priv->pub.dstats.tx_dropped++;
575         else
576                 drvr_priv->pub.tx_packets++;
577
578         /* Return ok: we always eat the packet */
579         return 0;
580 }
581
582 void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
583 {
584         struct net_device *net;
585         struct brcmf_info *drvr_priv = drvr->info;
586
587         brcmf_dbg(TRACE, "Enter\n");
588
589         drvr->txoff = state;
590         net = drvr_priv->iflist[ifidx]->net;
591         if (state == ON)
592                 netif_stop_queue(net);
593         else
594                 netif_wake_queue(net);
595 }
596
597 static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx,
598                             void *pktdata, struct brcmf_event_msg *event,
599                             void **data)
600 {
601         int bcmerror = 0;
602
603         bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
604         if (bcmerror != 0)
605                 return bcmerror;
606
607         if (drvr_priv->iflist[*ifidx]->net)
608                 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->net,
609                                      event, *data);
610
611         return bcmerror;
612 }
613
614 void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx, struct sk_buff *skb,
615                   int numpkt)
616 {
617         struct brcmf_info *drvr_priv = drvr->info;
618         unsigned char *eth;
619         uint len;
620         void *data;
621         struct sk_buff *pnext, *save_pktbuf;
622         int i;
623         struct brcmf_if *ifp;
624         struct brcmf_event_msg event;
625
626         brcmf_dbg(TRACE, "Enter\n");
627
628         save_pktbuf = skb;
629
630         for (i = 0; skb && i < numpkt; i++, skb = pnext) {
631
632                 pnext = skb->next;
633                 skb->next = NULL;
634
635                 /* Get the protocol, maintain skb around eth_type_trans()
636                  * The main reason for this hack is for the limitation of
637                  * Linux 2.4 where 'eth_type_trans' uses the
638                  * 'net->hard_header_len'
639                  * to perform skb_pull inside vs ETH_HLEN. Since to avoid
640                  * coping of the packet coming from the network stack to add
641                  * BDC, Hardware header etc, during network interface
642                  * registration
643                  * we set the 'net->hard_header_len' to ETH_HLEN + extra space
644                  * required
645                  * for BDC, Hardware header etc. and not just the ETH_HLEN
646                  */
647                 eth = skb->data;
648                 len = skb->len;
649
650                 ifp = drvr_priv->iflist[ifidx];
651                 if (ifp == NULL)
652                         ifp = drvr_priv->iflist[0];
653
654                 skb->dev = ifp->net;
655                 skb->protocol = eth_type_trans(skb, skb->dev);
656
657                 if (skb->pkt_type == PACKET_MULTICAST)
658                         drvr_priv->pub.rx_multicast++;
659
660                 skb->data = eth;
661                 skb->len = len;
662
663                 /* Strip header, count, deliver upward */
664                 skb_pull(skb, ETH_HLEN);
665
666                 /* Process special event packets and then discard them */
667                 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
668                         brcmf_host_event(drvr_priv, &ifidx,
669                                           skb_mac_header(skb),
670                                           &event, &data);
671
672                 if (drvr_priv->iflist[ifidx] &&
673                     !drvr_priv->iflist[ifidx]->state)
674                         ifp = drvr_priv->iflist[ifidx];
675
676                 if (ifp->net)
677                         ifp->net->last_rx = jiffies;
678
679                 drvr->dstats.rx_bytes += skb->len;
680                 drvr->rx_packets++;     /* Local count */
681
682                 if (in_interrupt())
683                         netif_rx(skb);
684                 else
685                         /* If the receive is not processed inside an ISR,
686                          * the softirqd must be woken explicitly to service
687                          * the NET_RX_SOFTIRQ.  In 2.6 kernels, this is handled
688                          * by netif_rx_ni(), but in earlier kernels, we need
689                          * to do it manually.
690                          */
691                         netif_rx_ni(skb);
692         }
693 }
694
695 void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
696 {
697         uint ifidx;
698         struct brcmf_info *drvr_priv = drvr->info;
699         struct ethhdr *eh;
700         u16 type;
701
702         brcmf_proto_hdrpull(drvr, &ifidx, txp);
703
704         eh = (struct ethhdr *)(txp->data);
705         type = ntohs(eh->h_proto);
706
707         if (type == ETH_P_PAE)
708                 atomic_dec(&drvr_priv->pend_8021x_cnt);
709
710 }
711
712 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *net)
713 {
714         struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
715         struct brcmf_if *ifp;
716         int ifidx;
717
718         brcmf_dbg(TRACE, "Enter\n");
719
720         ifidx = brcmf_net2idx(drvr_priv, net);
721         if (ifidx == BRCMF_BAD_IF)
722                 return NULL;
723
724         ifp = drvr_priv->iflist[ifidx];
725
726         if (drvr_priv->pub.up)
727                 /* Use the protocol to get dongle stats */
728                 brcmf_proto_dstats(&drvr_priv->pub);
729
730         /* Copy dongle stats to net device stats */
731         ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
732         ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
733         ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
734         ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
735         ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
736         ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
737         ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
738         ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
739         ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
740
741         return &ifp->stats;
742 }
743
744 /* Retrieve current toe component enables, which are kept
745          as a bitmap in toe_ol iovar */
746 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
747 {
748         struct brcmf_ioctl ioc;
749         char buf[32];
750         int ret;
751
752         memset(&ioc, 0, sizeof(ioc));
753
754         ioc.cmd = BRCMF_C_GET_VAR;
755         ioc.buf = buf;
756         ioc.len = (uint) sizeof(buf);
757         ioc.set = false;
758
759         strcpy(buf, "toe_ol");
760         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
761         if (ret < 0) {
762                 /* Check for older dongle image that doesn't support toe_ol */
763                 if (ret == -EIO) {
764                         brcmf_dbg(ERROR, "%s: toe not supported by device\n",
765                                   brcmf_ifname(&drvr_priv->pub, ifidx));
766                         return -EOPNOTSUPP;
767                 }
768
769                 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
770                           brcmf_ifname(&drvr_priv->pub, ifidx), ret);
771                 return ret;
772         }
773
774         memcpy(toe_ol, buf, sizeof(u32));
775         return 0;
776 }
777
778 /* Set current toe component enables in toe_ol iovar,
779          and set toe global enable iovar */
780 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
781 {
782         struct brcmf_ioctl ioc;
783         char buf[32];
784         int toe, ret;
785
786         memset(&ioc, 0, sizeof(ioc));
787
788         ioc.cmd = BRCMF_C_SET_VAR;
789         ioc.buf = buf;
790         ioc.len = (uint) sizeof(buf);
791         ioc.set = true;
792
793         /* Set toe_ol as requested */
794
795         strcpy(buf, "toe_ol");
796         memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
797
798         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
799         if (ret < 0) {
800                 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
801                           brcmf_ifname(&drvr_priv->pub, ifidx), ret);
802                 return ret;
803         }
804
805         /* Enable toe globally only if any components are enabled. */
806
807         toe = (toe_ol != 0);
808
809         strcpy(buf, "toe");
810         memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
811
812         ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
813         if (ret < 0) {
814                 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
815                           brcmf_ifname(&drvr_priv->pub, ifidx), ret);
816                 return ret;
817         }
818
819         return 0;
820 }
821
822 static void brcmf_ethtool_get_drvinfo(struct net_device *net,
823                                     struct ethtool_drvinfo *info)
824 {
825         struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
826
827         sprintf(info->driver, KBUILD_MODNAME);
828         sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
829         sprintf(info->fw_version, "%s", BCM4329_FW_NAME);
830         sprintf(info->bus_info, "%s",
831                 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
832 }
833
834 static struct ethtool_ops brcmf_ethtool_ops = {
835         .get_drvinfo = brcmf_ethtool_get_drvinfo
836 };
837
838 static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
839 {
840         struct ethtool_drvinfo info;
841         char drvname[sizeof(info.driver)];
842         u32 cmd;
843         struct ethtool_value edata;
844         u32 toe_cmpnt, csum_dir;
845         int ret;
846
847         brcmf_dbg(TRACE, "Enter\n");
848
849         /* all ethtool calls start with a cmd word */
850         if (copy_from_user(&cmd, uaddr, sizeof(u32)))
851                 return -EFAULT;
852
853         switch (cmd) {
854         case ETHTOOL_GDRVINFO:
855                 /* Copy out any request driver name */
856                 if (copy_from_user(&info, uaddr, sizeof(info)))
857                         return -EFAULT;
858                 strncpy(drvname, info.driver, sizeof(info.driver));
859                 drvname[sizeof(info.driver) - 1] = '\0';
860
861                 /* clear struct for return */
862                 memset(&info, 0, sizeof(info));
863                 info.cmd = cmd;
864
865                 /* if requested, identify ourselves */
866                 if (strcmp(drvname, "?dhd") == 0) {
867                         sprintf(info.driver, "dhd");
868                         strcpy(info.version, BRCMF_VERSION_STR);
869                 }
870
871                 /* otherwise, require dongle to be up */
872                 else if (!drvr_priv->pub.up) {
873                         brcmf_dbg(ERROR, "dongle is not up\n");
874                         return -ENODEV;
875                 }
876
877                 /* finally, report dongle driver type */
878                 else if (drvr_priv->pub.iswl)
879                         sprintf(info.driver, "wl");
880                 else
881                         sprintf(info.driver, "xx");
882
883                 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
884                 if (copy_to_user(uaddr, &info, sizeof(info)))
885                         return -EFAULT;
886                 brcmf_dbg(CTL, "given %*s, returning %s\n",
887                           (int)sizeof(drvname), drvname, info.driver);
888                 break;
889
890                 /* Get toe offload components from dongle */
891         case ETHTOOL_GRXCSUM:
892         case ETHTOOL_GTXCSUM:
893                 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
894                 if (ret < 0)
895                         return ret;
896
897                 csum_dir =
898                     (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
899
900                 edata.cmd = cmd;
901                 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
902
903                 if (copy_to_user(uaddr, &edata, sizeof(edata)))
904                         return -EFAULT;
905                 break;
906
907                 /* Set toe offload components in dongle */
908         case ETHTOOL_SRXCSUM:
909         case ETHTOOL_STXCSUM:
910                 if (copy_from_user(&edata, uaddr, sizeof(edata)))
911                         return -EFAULT;
912
913                 /* Read the current settings, update and write back */
914                 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
915                 if (ret < 0)
916                         return ret;
917
918                 csum_dir =
919                     (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
920
921                 if (edata.data != 0)
922                         toe_cmpnt |= csum_dir;
923                 else
924                         toe_cmpnt &= ~csum_dir;
925
926                 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
927                 if (ret < 0)
928                         return ret;
929
930                 /* If setting TX checksum mode, tell Linux the new mode */
931                 if (cmd == ETHTOOL_STXCSUM) {
932                         if (edata.data)
933                                 drvr_priv->iflist[0]->net->features |=
934                                     NETIF_F_IP_CSUM;
935                         else
936                                 drvr_priv->iflist[0]->net->features &=
937                                     ~NETIF_F_IP_CSUM;
938                 }
939
940                 break;
941
942         default:
943                 return -EOPNOTSUPP;
944         }
945
946         return 0;
947 }
948
949 static int brcmf_netdev_ioctl_entry(struct net_device *net, struct ifreq *ifr,
950                                     int cmd)
951 {
952         struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
953         int ifidx;
954
955         ifidx = brcmf_net2idx(drvr_priv, net);
956         brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifidx, cmd);
957
958         if (ifidx == BRCMF_BAD_IF)
959                 return -1;
960
961         if (cmd == SIOCETHTOOL)
962                 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
963
964         return -EOPNOTSUPP;
965 }
966
967 /* called only from within this driver */
968 int brcmf_netdev_ioctl_priv(struct net_device *net, struct brcmf_ioctl *ioc)
969 {
970         int bcmerror = 0;
971         int buflen = 0;
972         bool is_set_key_cmd;
973         struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
974         int ifidx;
975
976         ifidx = brcmf_net2idx(drvr_priv, net);
977
978         if (ioc->buf != NULL)
979                 buflen = min_t(uint, ioc->len, BRCMF_IOCTL_MAXLEN);
980
981         /* send to dongle (must be up, and wl) */
982         if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
983                 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
984                 bcmerror = -EIO;
985                 goto done;
986         }
987
988         if (!drvr_priv->pub.iswl) {
989                 bcmerror = -EIO;
990                 goto done;
991         }
992
993         /*
994          * Intercept BRCMF_C_SET_KEY IOCTL - serialize M4 send and
995          * set key IOCTL to prevent M4 encryption.
996          */
997         is_set_key_cmd = ((ioc->cmd == BRCMF_C_SET_KEY) ||
998                           ((ioc->cmd == BRCMF_C_SET_VAR) &&
999                            !(strncmp("wsec_key", ioc->buf, 9))) ||
1000                           ((ioc->cmd == BRCMF_C_SET_VAR) &&
1001                            !(strncmp("bsscfg:wsec_key", ioc->buf, 15))));
1002         if (is_set_key_cmd)
1003                 brcmf_netdev_wait_pend8021x(net);
1004
1005         bcmerror = brcmf_proto_ioctl(&drvr_priv->pub, ifidx,
1006                                      ioc, ioc->buf, buflen);
1007
1008 done:
1009         if (bcmerror > 0)
1010                 bcmerror = 0;
1011
1012         return bcmerror;
1013 }
1014
1015 static int brcmf_netdev_stop(struct net_device *net)
1016 {
1017         struct brcmf_pub *drvr = *(struct brcmf_pub **) netdev_priv(net);
1018
1019         brcmf_dbg(TRACE, "Enter\n");
1020         brcmf_cfg80211_down(drvr->config);
1021         if (drvr->up == 0)
1022                 return 0;
1023
1024         /* Set state and stop OS transmissions */
1025         drvr->up = 0;
1026         netif_stop_queue(net);
1027
1028         return 0;
1029 }
1030
1031 static int brcmf_netdev_open(struct net_device *net)
1032 {
1033         struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
1034         u32 toe_ol;
1035         int ifidx = brcmf_net2idx(drvr_priv, net);
1036         s32 ret = 0;
1037
1038         brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1039
1040         if (ifidx == 0) {       /* do it only for primary eth0 */
1041
1042                 /* try to bring up bus */
1043                 ret = brcmf_bus_start(&drvr_priv->pub);
1044                 if (ret != 0) {
1045                         brcmf_dbg(ERROR, "failed with code %d\n", ret);
1046                         return -1;
1047                 }
1048                 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
1049
1050                 memcpy(net->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
1051
1052                 /* Get current TOE mode from dongle */
1053                 if (brcmf_toe_get(drvr_priv, ifidx, &toe_ol) >= 0
1054                     && (toe_ol & TOE_TX_CSUM_OL) != 0)
1055                         drvr_priv->iflist[ifidx]->net->features |=
1056                                 NETIF_F_IP_CSUM;
1057                 else
1058                         drvr_priv->iflist[ifidx]->net->features &=
1059                                 ~NETIF_F_IP_CSUM;
1060         }
1061         /* Allow transmit calls */
1062         netif_start_queue(net);
1063         drvr_priv->pub.up = 1;
1064         if (unlikely(brcmf_cfg80211_up(drvr_priv->pub.config))) {
1065                 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
1066                 return -1;
1067         }
1068
1069         return ret;
1070 }
1071
1072 int
1073 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, struct net_device *net,
1074              char *name, u8 *mac_addr, u32 flags, u8 bssidx)
1075 {
1076         struct brcmf_if *ifp;
1077
1078         brcmf_dbg(TRACE, "idx %d, handle->%p\n", ifidx, net);
1079
1080         ifp = drvr_priv->iflist[ifidx];
1081         if (!ifp) {
1082                 ifp = kmalloc(sizeof(struct brcmf_if), GFP_ATOMIC);
1083                 if (!ifp) {
1084                         brcmf_dbg(ERROR, "OOM - struct brcmf_if\n");
1085                         return -ENOMEM;
1086                 }
1087         }
1088
1089         memset(ifp, 0, sizeof(struct brcmf_if));
1090         ifp->info = drvr_priv;
1091         drvr_priv->iflist[ifidx] = ifp;
1092         strlcpy(ifp->name, name, IFNAMSIZ);
1093         if (mac_addr != NULL)
1094                 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
1095
1096         if (net == NULL) {
1097                 ifp->state = BRCMF_E_IF_ADD;
1098                 ifp->idx = ifidx;
1099                 wake_up(&drvr_priv->sysioc_waitq);
1100         } else
1101                 ifp->net = net;
1102
1103         return 0;
1104 }
1105
1106 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
1107 {
1108         struct brcmf_if *ifp;
1109
1110         brcmf_dbg(TRACE, "idx %d\n", ifidx);
1111
1112         ifp = drvr_priv->iflist[ifidx];
1113         if (!ifp) {
1114                 brcmf_dbg(ERROR, "Null interface\n");
1115                 return;
1116         }
1117
1118         ifp->state = BRCMF_E_IF_DEL;
1119         ifp->idx = ifidx;
1120         wake_up(&drvr_priv->sysioc_waitq);
1121 }
1122
1123 struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
1124 {
1125         struct brcmf_info *drvr_priv = NULL;
1126         struct net_device *net;
1127
1128         brcmf_dbg(TRACE, "Enter\n");
1129
1130         /* Allocate etherdev, including space for private structure */
1131         net = alloc_etherdev(sizeof(drvr_priv));
1132         if (!net) {
1133                 brcmf_dbg(ERROR, "OOM - alloc_etherdev\n");
1134                 goto fail;
1135         }
1136
1137         /* Allocate primary brcmf_info */
1138         drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
1139         if (!drvr_priv) {
1140                 brcmf_dbg(ERROR, "OOM - alloc brcmf_info\n");
1141                 goto fail;
1142         }
1143
1144         /*
1145          * Save the brcmf_info into the priv
1146          */
1147         memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1148
1149         /* Set network interface name if it was provided as module parameter */
1150         if (iface_name[0]) {
1151                 int len;
1152                 char ch;
1153                 strncpy(net->name, iface_name, IFNAMSIZ);
1154                 net->name[IFNAMSIZ - 1] = 0;
1155                 len = strlen(net->name);
1156                 ch = net->name[len - 1];
1157                 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1158                         strcat(net->name, "%d");
1159         }
1160
1161         if (brcmf_add_if(drvr_priv, 0, net, net->name, NULL, 0, 0) ==
1162             BRCMF_BAD_IF)
1163                 goto fail;
1164
1165         net->netdev_ops = NULL;
1166         mutex_init(&drvr_priv->proto_block);
1167
1168         /* Link to info module */
1169         drvr_priv->pub.info = drvr_priv;
1170
1171         /* Link to bus module */
1172         drvr_priv->pub.bus = bus;
1173         drvr_priv->pub.hdrlen = bus_hdrlen;
1174
1175         /* Attach and link in the protocol */
1176         if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
1177                 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
1178                 goto fail;
1179         }
1180
1181         /* Attach and link in the cfg80211 */
1182         drvr_priv->pub.config =
1183                         brcmf_cfg80211_attach(net,
1184                                               brcmf_bus_get_device(bus),
1185                                               &drvr_priv->pub);
1186         if (unlikely(drvr_priv->pub.config == NULL)) {
1187                 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1188                 goto fail;
1189         }
1190
1191         if (brcmf_sysioc) {
1192                 init_waitqueue_head(&drvr_priv->sysioc_waitq);
1193                 drvr_priv->sysioc_tsk = kthread_run(_brcmf_sysioc_thread,
1194                                                     drvr_priv, "_brcmf_sysioc");
1195                 if (IS_ERR(drvr_priv->sysioc_tsk)) {
1196                         printk(KERN_WARNING
1197                                 "_brcmf_sysioc thread failed to start\n");
1198                         drvr_priv->sysioc_tsk = NULL;
1199                 }
1200         } else
1201                 drvr_priv->sysioc_tsk = NULL;
1202
1203         /*
1204          * Save the brcmf_info into the priv
1205          */
1206         memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1207
1208         return &drvr_priv->pub;
1209
1210 fail:
1211         if (net)
1212                 free_netdev(net);
1213         if (drvr_priv)
1214                 brcmf_detach(&drvr_priv->pub);
1215
1216         return NULL;
1217 }
1218
1219 int brcmf_bus_start(struct brcmf_pub *drvr)
1220 {
1221         int ret = -1;
1222         struct brcmf_info *drvr_priv = drvr->info;
1223         /* Room for "event_msgs" + '\0' + bitvec */
1224         char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1225
1226         brcmf_dbg(TRACE, "\n");
1227
1228         /* Bring up the bus */
1229         ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub, true);
1230         if (ret != 0) {
1231                 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
1232                 return ret;
1233         }
1234
1235         /* If bus is not ready, can't come up */
1236         if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
1237                 brcmf_dbg(ERROR, "failed bus is not ready\n");
1238                 return -ENODEV;
1239         }
1240
1241         brcmu_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1242                       iovbuf, sizeof(iovbuf));
1243         brcmf_proto_cdc_query_ioctl(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1244                                     sizeof(iovbuf));
1245         memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1246
1247         setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1248         setbit(drvr->eventmask, BRCMF_E_PRUNE);
1249         setbit(drvr->eventmask, BRCMF_E_AUTH);
1250         setbit(drvr->eventmask, BRCMF_E_REASSOC);
1251         setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1252         setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1253         setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1254         setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1255         setbit(drvr->eventmask, BRCMF_E_JOIN);
1256         setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1257         setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1258         setbit(drvr->eventmask, BRCMF_E_LINK);
1259         setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1260         setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1261         setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1262         setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1263         setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1264         setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1265
1266 /* enable dongle roaming event */
1267
1268         drvr->pktfilter_count = 1;
1269         /* Setup filter to allow only unicast */
1270         drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1271
1272         /* Bus is ready, do any protocol initialization */
1273         ret = brcmf_proto_init(&drvr_priv->pub);
1274         if (ret < 0)
1275                 return ret;
1276
1277         return 0;
1278 }
1279
1280 static struct net_device_ops brcmf_netdev_ops_pri = {
1281         .ndo_open = brcmf_netdev_open,
1282         .ndo_stop = brcmf_netdev_stop,
1283         .ndo_get_stats = brcmf_netdev_get_stats,
1284         .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1285         .ndo_start_xmit = brcmf_netdev_start_xmit,
1286         .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1287         .ndo_set_multicast_list = brcmf_netdev_set_multicast_list
1288 };
1289
1290 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1291 {
1292         struct brcmf_info *drvr_priv = drvr->info;
1293         struct net_device *net;
1294         u8 temp_addr[ETH_ALEN] = {
1295                 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1296
1297         brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1298
1299         net = drvr_priv->iflist[ifidx]->net;
1300         net->netdev_ops = &brcmf_netdev_ops_pri;
1301
1302         /*
1303          * We have to use the primary MAC for virtual interfaces
1304          */
1305         if (ifidx != 0) {
1306                 /* for virtual interfaces use the primary MAC  */
1307                 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1308
1309         }
1310
1311         if (ifidx == 1) {
1312                 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1313                 /*  ACCESSPOINT INTERFACE CASE */
1314                 temp_addr[0] |= 0X02;   /* set bit 2 ,
1315                          - Locally Administered address  */
1316
1317         }
1318         net->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1319         net->ethtool_ops = &brcmf_ethtool_ops;
1320
1321         drvr_priv->pub.rxsz = net->mtu + net->hard_header_len +
1322                                 drvr_priv->pub.hdrlen;
1323
1324         memcpy(net->dev_addr, temp_addr, ETH_ALEN);
1325
1326         if (register_netdev(net) != 0) {
1327                 brcmf_dbg(ERROR, "couldn't register the net device\n");
1328                 goto fail;
1329         }
1330
1331         brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", net->name);
1332
1333         return 0;
1334
1335 fail:
1336         net->netdev_ops = NULL;
1337         return -EBADE;
1338 }
1339
1340 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1341 {
1342         struct brcmf_info *drvr_priv;
1343
1344         brcmf_dbg(TRACE, "Enter\n");
1345
1346         if (drvr) {
1347                 drvr_priv = drvr->info;
1348                 if (drvr_priv) {
1349                         /* Stop the protocol module */
1350                         brcmf_proto_stop(&drvr_priv->pub);
1351
1352                         /* Stop the bus module */
1353                         brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus, true);
1354                 }
1355         }
1356 }
1357
1358 void brcmf_detach(struct brcmf_pub *drvr)
1359 {
1360         struct brcmf_info *drvr_priv;
1361
1362         brcmf_dbg(TRACE, "Enter\n");
1363
1364         if (drvr) {
1365                 drvr_priv = drvr->info;
1366                 if (drvr_priv) {
1367                         struct brcmf_if *ifp;
1368                         int i;
1369
1370                         for (i = 1; i < BRCMF_MAX_IFS; i++)
1371                                 if (drvr_priv->iflist[i])
1372                                         brcmf_del_if(drvr_priv, i);
1373
1374                         ifp = drvr_priv->iflist[0];
1375                         if (ifp->net->netdev_ops == &brcmf_netdev_ops_pri) {
1376                                 brcmf_netdev_stop(ifp->net);
1377                                 unregister_netdev(ifp->net);
1378                         }
1379
1380                         if (drvr_priv->sysioc_tsk) {
1381                                 send_sig(SIGTERM, drvr_priv->sysioc_tsk, 1);
1382                                 kthread_stop(drvr_priv->sysioc_tsk);
1383                                 drvr_priv->sysioc_tsk = NULL;
1384                         }
1385
1386                         brcmf_bus_detach(drvr);
1387
1388                         if (drvr->prot)
1389                                 brcmf_proto_detach(drvr);
1390
1391                         brcmf_cfg80211_detach(drvr->config);
1392
1393                         free_netdev(ifp->net);
1394                         kfree(ifp);
1395                         kfree(drvr_priv);
1396                 }
1397         }
1398 }
1399
1400 static void __exit brcmf_module_cleanup(void)
1401 {
1402         brcmf_dbg(TRACE, "Enter\n");
1403
1404         brcmf_bus_unregister();
1405 }
1406
1407 static int __init brcmf_module_init(void)
1408 {
1409         int error;
1410
1411         brcmf_dbg(TRACE, "Enter\n");
1412
1413         error = brcmf_bus_register();
1414
1415         if (error) {
1416                 brcmf_dbg(ERROR, "brcmf_bus_register failed\n");
1417                 goto failed;
1418         }
1419         return 0;
1420
1421 failed:
1422         return -EINVAL;
1423 }
1424
1425 module_init(brcmf_module_init);
1426 module_exit(brcmf_module_cleanup);
1427
1428 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1429 {
1430         struct brcmf_info *drvr_priv = drvr->info;
1431
1432         if (drvr_priv) {
1433                 mutex_lock(&drvr_priv->proto_block);
1434                 return 1;
1435         }
1436         return 0;
1437 }
1438
1439 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1440 {
1441         struct brcmf_info *drvr_priv = drvr->info;
1442
1443         if (drvr_priv) {
1444                 mutex_unlock(&drvr_priv->proto_block);
1445                 return 1;
1446         }
1447
1448         return 0;
1449 }
1450
1451 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1452 {
1453         return atomic_read(&drvr_priv->pend_8021x_cnt);
1454 }
1455
1456 #define MAX_WAIT_FOR_8021X_TX   10
1457
1458 int brcmf_netdev_wait_pend8021x(struct net_device *dev)
1459 {
1460         struct brcmf_info *drvr_priv = *(struct brcmf_info **)netdev_priv(dev);
1461         int timeout = 10 * HZ / 1000;
1462         int ntimes = MAX_WAIT_FOR_8021X_TX;
1463         int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1464
1465         while (ntimes && pend) {
1466                 if (pend) {
1467                         set_current_state(TASK_INTERRUPTIBLE);
1468                         schedule_timeout(timeout);
1469                         set_current_state(TASK_RUNNING);
1470                         ntimes--;
1471                 }
1472                 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1473         }
1474         return pend;
1475 }
1476
1477 #ifdef BCMDBG
1478 int brcmf_write_to_file(struct brcmf_pub *drvr, u8 *buf, int size)
1479 {
1480         int ret = 0;
1481         struct file *fp;
1482         mm_segment_t old_fs;
1483         loff_t pos = 0;
1484
1485         /* change to KERNEL_DS address limit */
1486         old_fs = get_fs();
1487         set_fs(KERNEL_DS);
1488
1489         /* open file to write */
1490         fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1491         if (!fp) {
1492                 brcmf_dbg(ERROR, "open file error\n");
1493                 ret = -1;
1494                 goto exit;
1495         }
1496
1497         /* Write buf to file */
1498         fp->f_op->write(fp, buf, size, &pos);
1499
1500 exit:
1501         /* free buf before return */
1502         kfree(buf);
1503         /* close file before return */
1504         if (fp)
1505                 filp_close(fp, current->files);
1506         /* restore previous address limit */
1507         set_fs(old_fs);
1508
1509         return ret;
1510 }
1511 #endif                          /* BCMDBG */