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