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