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