]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/brcm80211/brcmfmac/dhd_linux.c
brcmfmac: check bus state to be data before sending data.
[karo-tx-linux.git] / drivers / net / wireless / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/kthread.h>
22 #include <linux/slab.h>
23 #include <linux/skbuff.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/random.h>
28 #include <linux/spinlock.h>
29 #include <linux/ethtool.h>
30 #include <linux/fcntl.h>
31 #include <linux/fs.h>
32 #include <linux/uaccess.h>
33 #include <linux/hardirq.h>
34 #include <linux/mutex.h>
35 #include <linux/wait.h>
36 #include <linux/module.h>
37 #include <net/cfg80211.h>
38 #include <net/rtnetlink.h>
39 #include <defs.h>
40 #include <brcmu_utils.h>
41 #include <brcmu_wifi.h>
42
43 #include "dhd.h"
44 #include "dhd_bus.h"
45 #include "dhd_proto.h"
46 #include "dhd_dbg.h"
47 #include "wl_cfg80211.h"
48 #include "fwil.h"
49
50 MODULE_AUTHOR("Broadcom Corporation");
51 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
52 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
53 MODULE_LICENSE("Dual BSD/GPL");
54
55 #define MAX_WAIT_FOR_8021X_TX           50      /* msecs */
56
57 /* Error bits */
58 int brcmf_msg_level = BRCMF_ERROR_VAL;
59 module_param(brcmf_msg_level, int, 0);
60
61 int brcmf_ifname2idx(struct brcmf_pub *drvr, char *name)
62 {
63         int i = BRCMF_MAX_IFS;
64         struct brcmf_if *ifp;
65
66         if (name == NULL || *name == '\0')
67                 return 0;
68
69         while (--i > 0) {
70                 ifp = drvr->iflist[i];
71                 if (ifp && !strncmp(ifp->ndev->name, name, IFNAMSIZ))
72                         break;
73         }
74
75         brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
76
77         return i;               /* default - the primary interface */
78 }
79
80 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
81 {
82         if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
83                 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
84                 return "<if_bad>";
85         }
86
87         if (drvr->iflist[ifidx] == NULL) {
88                 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
89                 return "<if_null>";
90         }
91
92         if (drvr->iflist[ifidx]->ndev)
93                 return drvr->iflist[ifidx]->ndev->name;
94
95         return "<if_none>";
96 }
97
98 static void _brcmf_set_multicast_list(struct work_struct *work)
99 {
100         struct brcmf_if *ifp;
101         struct net_device *ndev;
102         struct netdev_hw_addr *ha;
103         u32 cmd_value, cnt;
104         __le32 cnt_le;
105         char *buf, *bufp;
106         u32 buflen;
107         s32 err;
108         struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
109                                                     multicast_work);
110
111         brcmf_dbg(TRACE, "enter\n");
112
113         ifp = drvr->iflist[0];
114         ndev = ifp->ndev;
115
116         /* Determine initial value of allmulti flag */
117         cmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
118
119         /* Send down the multicast list first. */
120         cnt = netdev_mc_count(ndev);
121         buflen = sizeof(cnt) + (cnt * ETH_ALEN);
122         buf = kmalloc(buflen, GFP_ATOMIC);
123         if (!buf)
124                 return;
125         bufp = buf;
126
127         cnt_le = cpu_to_le32(cnt);
128         memcpy(bufp, &cnt_le, sizeof(cnt_le));
129         bufp += sizeof(cnt_le);
130
131         netdev_for_each_mc_addr(ha, ndev) {
132                 if (!cnt)
133                         break;
134                 memcpy(bufp, ha->addr, ETH_ALEN);
135                 bufp += ETH_ALEN;
136                 cnt--;
137         }
138
139         err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
140         if (err < 0) {
141                 brcmf_dbg(ERROR, "Setting mcast_list failed, %d\n", err);
142                 cmd_value = cnt ? true : cmd_value;
143         }
144
145         kfree(buf);
146
147         /*
148          * Now send the allmulti setting.  This is based on the setting in the
149          * net_device flags, but might be modified above to be turned on if we
150          * were trying to set some addresses and dongle rejected it...
151          */
152         err = brcmf_fil_iovar_int_set(ifp, "allmulti", cmd_value);
153         if (err < 0)
154                 brcmf_dbg(ERROR, "Setting allmulti failed, %d\n", err);
155
156         /*Finally, pick up the PROMISC flag */
157         cmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
158         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PROMISC, cmd_value);
159         if (err < 0)
160                 brcmf_dbg(ERROR, "Setting BRCMF_C_SET_PROMISC failed, %d\n",
161                           err);
162 }
163
164 static void
165 _brcmf_set_mac_address(struct work_struct *work)
166 {
167         struct brcmf_if *ifp;
168         struct net_device *ndev;
169         s32 err;
170
171         struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
172                                                     setmacaddr_work);
173
174         brcmf_dbg(TRACE, "enter\n");
175
176         ifp = drvr->iflist[0];
177         ndev = ifp->ndev;
178
179         err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", drvr->macvalue,
180                                        ETH_ALEN);
181         if (err < 0) {
182                 brcmf_dbg(ERROR, "Setting cur_etheraddr failed, %d\n", err);
183         } else {
184                 brcmf_dbg(TRACE, "MAC address updated to %pM\n",
185                           drvr->macvalue);
186                 memcpy(ndev->dev_addr, drvr->macvalue, ETH_ALEN);
187         }
188 }
189
190 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
191 {
192         struct brcmf_if *ifp = netdev_priv(ndev);
193         struct brcmf_pub *drvr = ifp->drvr;
194         struct sockaddr *sa = (struct sockaddr *)addr;
195
196         memcpy(&drvr->macvalue, sa->sa_data, ETH_ALEN);
197         schedule_work(&drvr->setmacaddr_work);
198         return 0;
199 }
200
201 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
202 {
203         struct brcmf_if *ifp = netdev_priv(ndev);
204         struct brcmf_pub *drvr = ifp->drvr;
205
206         schedule_work(&drvr->multicast_work);
207 }
208
209 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
210 {
211         int ret;
212         struct brcmf_if *ifp = netdev_priv(ndev);
213         struct brcmf_pub *drvr = ifp->drvr;
214
215         brcmf_dbg(TRACE, "Enter\n");
216
217         /* Reject if down */
218         if (!drvr->bus_if->drvr_up ||
219             (drvr->bus_if->state != BRCMF_BUS_DATA)) {
220                 brcmf_dbg(ERROR, "xmit rejected drvup=%d state=%d\n",
221                           drvr->bus_if->drvr_up,
222                           drvr->bus_if->state);
223                 netif_stop_queue(ndev);
224                 return -ENODEV;
225         }
226
227         if (!drvr->iflist[ifp->idx]) {
228                 brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
229                 netif_stop_queue(ndev);
230                 return -ENODEV;
231         }
232
233         /* Make sure there's enough room for any header */
234         if (skb_headroom(skb) < drvr->hdrlen) {
235                 struct sk_buff *skb2;
236
237                 brcmf_dbg(INFO, "%s: insufficient headroom\n",
238                           brcmf_ifname(drvr, ifp->idx));
239                 drvr->bus_if->tx_realloc++;
240                 skb2 = skb_realloc_headroom(skb, drvr->hdrlen);
241                 dev_kfree_skb(skb);
242                 skb = skb2;
243                 if (skb == NULL) {
244                         brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
245                                   brcmf_ifname(drvr, ifp->idx));
246                         ret = -ENOMEM;
247                         goto done;
248                 }
249         }
250
251         /* Update multicast statistic */
252         if (skb->len >= ETH_ALEN) {
253                 u8 *pktdata = (u8 *)(skb->data);
254                 struct ethhdr *eh = (struct ethhdr *)pktdata;
255
256                 if (is_multicast_ether_addr(eh->h_dest))
257                         drvr->tx_multicast++;
258                 if (ntohs(eh->h_proto) == ETH_P_PAE)
259                         atomic_inc(&drvr->pend_8021x_cnt);
260         }
261
262         /* If the protocol uses a data header, apply it */
263         brcmf_proto_hdrpush(drvr, ifp->idx, skb);
264
265         /* Use bus module to send data frame */
266         ret =  drvr->bus_if->brcmf_bus_txdata(drvr->dev, skb);
267
268 done:
269         if (ret)
270                 drvr->bus_if->dstats.tx_dropped++;
271         else
272                 drvr->bus_if->dstats.tx_packets++;
273
274         /* Return ok: we always eat the packet */
275         return 0;
276 }
277
278 void brcmf_txflowblock(struct device *dev, bool state)
279 {
280         struct net_device *ndev;
281         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
282         struct brcmf_pub *drvr = bus_if->drvr;
283         int i;
284
285         brcmf_dbg(TRACE, "Enter\n");
286
287         for (i = 0; i < BRCMF_MAX_IFS; i++)
288                 if (drvr->iflist[i]) {
289                         ndev = drvr->iflist[i]->ndev;
290                         if (state)
291                                 netif_stop_queue(ndev);
292                         else
293                                 netif_wake_queue(ndev);
294                 }
295 }
296
297 static int brcmf_host_event(struct brcmf_pub *drvr, int *ifidx,
298                             void *pktdata, struct brcmf_event_msg *event,
299                             void **data)
300 {
301         int bcmerror = 0;
302
303         bcmerror = brcmf_c_host_event(drvr, ifidx, pktdata, event, data);
304         if (bcmerror != 0)
305                 return bcmerror;
306
307         if (drvr->iflist[*ifidx]->ndev)
308                 brcmf_cfg80211_event(drvr->iflist[*ifidx]->ndev,
309                                      event, *data);
310
311         return bcmerror;
312 }
313
314 void brcmf_rx_frame(struct device *dev, int ifidx,
315                     struct sk_buff_head *skb_list)
316 {
317         unsigned char *eth;
318         uint len;
319         void *data;
320         struct sk_buff *skb, *pnext;
321         struct brcmf_if *ifp;
322         struct brcmf_event_msg event;
323         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
324         struct brcmf_pub *drvr = bus_if->drvr;
325
326         brcmf_dbg(TRACE, "Enter\n");
327
328         skb_queue_walk_safe(skb_list, skb, pnext) {
329                 skb_unlink(skb, skb_list);
330
331                 /* Get the protocol, maintain skb around eth_type_trans()
332                  * The main reason for this hack is for the limitation of
333                  * Linux 2.4 where 'eth_type_trans' uses the
334                  * 'net->hard_header_len'
335                  * to perform skb_pull inside vs ETH_HLEN. Since to avoid
336                  * coping of the packet coming from the network stack to add
337                  * BDC, Hardware header etc, during network interface
338                  * registration
339                  * we set the 'net->hard_header_len' to ETH_HLEN + extra space
340                  * required
341                  * for BDC, Hardware header etc. and not just the ETH_HLEN
342                  */
343                 eth = skb->data;
344                 len = skb->len;
345
346                 ifp = drvr->iflist[ifidx];
347                 if (ifp == NULL)
348                         ifp = drvr->iflist[0];
349
350                 if (!ifp || !ifp->ndev ||
351                     ifp->ndev->reg_state != NETREG_REGISTERED) {
352                         brcmu_pkt_buf_free_skb(skb);
353                         continue;
354                 }
355
356                 skb->dev = ifp->ndev;
357                 skb->protocol = eth_type_trans(skb, skb->dev);
358
359                 if (skb->pkt_type == PACKET_MULTICAST)
360                         bus_if->dstats.multicast++;
361
362                 skb->data = eth;
363                 skb->len = len;
364
365                 /* Strip header, count, deliver upward */
366                 skb_pull(skb, ETH_HLEN);
367
368                 /* Process special event packets and then discard them */
369                 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
370                         brcmf_host_event(drvr, &ifidx,
371                                           skb_mac_header(skb),
372                                           &event, &data);
373
374                 if (drvr->iflist[ifidx]) {
375                         ifp = drvr->iflist[ifidx];
376                         ifp->ndev->last_rx = jiffies;
377                 }
378
379                 bus_if->dstats.rx_bytes += skb->len;
380                 bus_if->dstats.rx_packets++;    /* Local count */
381
382                 if (in_interrupt())
383                         netif_rx(skb);
384                 else
385                         /* If the receive is not processed inside an ISR,
386                          * the softirqd must be woken explicitly to service
387                          * the NET_RX_SOFTIRQ.  In 2.6 kernels, this is handled
388                          * by netif_rx_ni(), but in earlier kernels, we need
389                          * to do it manually.
390                          */
391                         netif_rx_ni(skb);
392         }
393 }
394
395 void brcmf_txcomplete(struct device *dev, struct sk_buff *txp, bool success)
396 {
397         uint ifidx;
398         struct ethhdr *eh;
399         u16 type;
400         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
401         struct brcmf_pub *drvr = bus_if->drvr;
402
403         brcmf_proto_hdrpull(dev, &ifidx, txp);
404
405         eh = (struct ethhdr *)(txp->data);
406         type = ntohs(eh->h_proto);
407
408         if (type == ETH_P_PAE) {
409                 atomic_dec(&drvr->pend_8021x_cnt);
410                 if (waitqueue_active(&drvr->pend_8021x_wait))
411                         wake_up(&drvr->pend_8021x_wait);
412         }
413 }
414
415 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
416 {
417         struct brcmf_if *ifp = netdev_priv(ndev);
418         struct brcmf_bus *bus_if = ifp->drvr->bus_if;
419
420         brcmf_dbg(TRACE, "Enter\n");
421
422         /* Copy dongle stats to net device stats */
423         ifp->stats.rx_packets = bus_if->dstats.rx_packets;
424         ifp->stats.tx_packets = bus_if->dstats.tx_packets;
425         ifp->stats.rx_bytes = bus_if->dstats.rx_bytes;
426         ifp->stats.tx_bytes = bus_if->dstats.tx_bytes;
427         ifp->stats.rx_errors = bus_if->dstats.rx_errors;
428         ifp->stats.tx_errors = bus_if->dstats.tx_errors;
429         ifp->stats.rx_dropped = bus_if->dstats.rx_dropped;
430         ifp->stats.tx_dropped = bus_if->dstats.tx_dropped;
431         ifp->stats.multicast = bus_if->dstats.multicast;
432
433         return &ifp->stats;
434 }
435
436 /*
437  * Set current toe component enables in toe_ol iovar,
438  * and set toe global enable iovar
439  */
440 static int brcmf_toe_set(struct brcmf_if *ifp, u32 toe_ol)
441 {
442         s32 err;
443
444         err = brcmf_fil_iovar_int_set(ifp, "toe_ol", toe_ol);
445         if (err < 0) {
446                 brcmf_dbg(ERROR, "Setting toe_ol failed, %d\n", err);
447                 return err;
448         }
449
450         err = brcmf_fil_iovar_int_set(ifp, "toe", (toe_ol != 0));
451         if (err < 0)
452                 brcmf_dbg(ERROR, "Setting toe failed, %d\n", err);
453
454         return err;
455
456 }
457
458 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
459                                     struct ethtool_drvinfo *info)
460 {
461         struct brcmf_if *ifp = netdev_priv(ndev);
462         struct brcmf_pub *drvr = ifp->drvr;
463
464         sprintf(info->driver, KBUILD_MODNAME);
465         sprintf(info->version, "%lu", drvr->drv_version);
466         sprintf(info->bus_info, "%s", dev_name(drvr->dev));
467 }
468
469 static const struct ethtool_ops brcmf_ethtool_ops = {
470         .get_drvinfo = brcmf_ethtool_get_drvinfo,
471 };
472
473 static int brcmf_ethtool(struct brcmf_if *ifp, void __user *uaddr)
474 {
475         struct brcmf_pub *drvr = ifp->drvr;
476         struct ethtool_drvinfo info;
477         char drvname[sizeof(info.driver)];
478         u32 cmd;
479         struct ethtool_value edata;
480         u32 toe_cmpnt, csum_dir;
481         int ret;
482
483         brcmf_dbg(TRACE, "Enter\n");
484
485         /* all ethtool calls start with a cmd word */
486         if (copy_from_user(&cmd, uaddr, sizeof(u32)))
487                 return -EFAULT;
488
489         switch (cmd) {
490         case ETHTOOL_GDRVINFO:
491                 /* Copy out any request driver name */
492                 if (copy_from_user(&info, uaddr, sizeof(info)))
493                         return -EFAULT;
494                 strncpy(drvname, info.driver, sizeof(info.driver));
495                 drvname[sizeof(info.driver) - 1] = '\0';
496
497                 /* clear struct for return */
498                 memset(&info, 0, sizeof(info));
499                 info.cmd = cmd;
500
501                 /* if requested, identify ourselves */
502                 if (strcmp(drvname, "?dhd") == 0) {
503                         sprintf(info.driver, "dhd");
504                         strcpy(info.version, BRCMF_VERSION_STR);
505                 }
506
507                 /* otherwise, require dongle to be up */
508                 else if (!drvr->bus_if->drvr_up) {
509                         brcmf_dbg(ERROR, "dongle is not up\n");
510                         return -ENODEV;
511                 }
512                 /* finally, report dongle driver type */
513                 else
514                         sprintf(info.driver, "wl");
515
516                 sprintf(info.version, "%lu", drvr->drv_version);
517                 if (copy_to_user(uaddr, &info, sizeof(info)))
518                         return -EFAULT;
519                 brcmf_dbg(CTL, "given %*s, returning %s\n",
520                           (int)sizeof(drvname), drvname, info.driver);
521                 break;
522
523                 /* Get toe offload components from dongle */
524         case ETHTOOL_GRXCSUM:
525         case ETHTOOL_GTXCSUM:
526                 ret = brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_cmpnt);
527                 if (ret < 0)
528                         return ret;
529
530                 csum_dir =
531                     (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
532
533                 edata.cmd = cmd;
534                 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
535
536                 if (copy_to_user(uaddr, &edata, sizeof(edata)))
537                         return -EFAULT;
538                 break;
539
540                 /* Set toe offload components in dongle */
541         case ETHTOOL_SRXCSUM:
542         case ETHTOOL_STXCSUM:
543                 if (copy_from_user(&edata, uaddr, sizeof(edata)))
544                         return -EFAULT;
545
546                 /* Read the current settings, update and write back */
547                 ret = brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_cmpnt);
548                 if (ret < 0)
549                         return ret;
550
551                 csum_dir =
552                     (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
553
554                 if (edata.data != 0)
555                         toe_cmpnt |= csum_dir;
556                 else
557                         toe_cmpnt &= ~csum_dir;
558
559                 ret = brcmf_toe_set(ifp, toe_cmpnt);
560                 if (ret < 0)
561                         return ret;
562
563                 /* If setting TX checksum mode, tell Linux the new mode */
564                 if (cmd == ETHTOOL_STXCSUM) {
565                         if (edata.data)
566                                 ifp->ndev->features |= NETIF_F_IP_CSUM;
567                         else
568                                 ifp->ndev->features &= ~NETIF_F_IP_CSUM;
569                 }
570
571                 break;
572
573         default:
574                 return -EOPNOTSUPP;
575         }
576
577         return 0;
578 }
579
580 static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
581                                     int cmd)
582 {
583         struct brcmf_if *ifp = netdev_priv(ndev);
584         struct brcmf_pub *drvr = ifp->drvr;
585
586         brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
587
588         if (!drvr->iflist[ifp->idx])
589                 return -1;
590
591         if (cmd == SIOCETHTOOL)
592                 return brcmf_ethtool(ifp, ifr->ifr_data);
593
594         return -EOPNOTSUPP;
595 }
596
597 static int brcmf_netdev_stop(struct net_device *ndev)
598 {
599         struct brcmf_if *ifp = netdev_priv(ndev);
600         struct brcmf_pub *drvr = ifp->drvr;
601
602         brcmf_dbg(TRACE, "Enter\n");
603         brcmf_cfg80211_down(drvr->config);
604         if (drvr->bus_if->drvr_up == 0)
605                 return 0;
606
607         /* Set state and stop OS transmissions */
608         drvr->bus_if->drvr_up = false;
609         netif_stop_queue(ndev);
610
611         return 0;
612 }
613
614 static int brcmf_netdev_open(struct net_device *ndev)
615 {
616         struct brcmf_if *ifp = netdev_priv(ndev);
617         struct brcmf_pub *drvr = ifp->drvr;
618         struct brcmf_bus *bus_if = drvr->bus_if;
619         u32 toe_ol;
620         s32 ret = 0;
621
622         brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
623
624         if (ifp->idx == 0) {    /* do it only for primary eth0 */
625                 /* If bus is not ready, can't continue */
626                 if (bus_if->state != BRCMF_BUS_DATA) {
627                         brcmf_dbg(ERROR, "failed bus is not ready\n");
628                         return -EAGAIN;
629                 }
630
631                 atomic_set(&drvr->pend_8021x_cnt, 0);
632
633                 memcpy(ndev->dev_addr, drvr->mac, ETH_ALEN);
634
635                 /* Get current TOE mode from dongle */
636                 if (brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_ol) >= 0
637                     && (toe_ol & TOE_TX_CSUM_OL) != 0)
638                         drvr->iflist[ifp->idx]->ndev->features |=
639                                 NETIF_F_IP_CSUM;
640                 else
641                         drvr->iflist[ifp->idx]->ndev->features &=
642                                 ~NETIF_F_IP_CSUM;
643         }
644
645         /* make sure RF is ready for work */
646         brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
647
648         /* Allow transmit calls */
649         netif_start_queue(ndev);
650         drvr->bus_if->drvr_up = true;
651         if (brcmf_cfg80211_up(drvr->config)) {
652                 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
653                 return -1;
654         }
655
656         return ret;
657 }
658
659 static const struct net_device_ops brcmf_netdev_ops_pri = {
660         .ndo_open = brcmf_netdev_open,
661         .ndo_stop = brcmf_netdev_stop,
662         .ndo_get_stats = brcmf_netdev_get_stats,
663         .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
664         .ndo_start_xmit = brcmf_netdev_start_xmit,
665         .ndo_set_mac_address = brcmf_netdev_set_mac_address,
666         .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
667 };
668
669 int brcmf_net_attach(struct brcmf_if *ifp)
670 {
671         struct brcmf_pub *drvr = ifp->drvr;
672         struct net_device *ndev;
673         u8 temp_addr[ETH_ALEN];
674
675         brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
676
677         ndev = drvr->iflist[ifp->idx]->ndev;
678         ndev->netdev_ops = &brcmf_netdev_ops_pri;
679
680         /*
681          * determine mac address to use
682          */
683         if (is_valid_ether_addr(ifp->mac_addr))
684                 memcpy(temp_addr, ifp->mac_addr, ETH_ALEN);
685         else
686                 memcpy(temp_addr, drvr->mac, ETH_ALEN);
687
688         if (ifp->idx == 1) {
689                 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
690                 /*  ACCESSPOINT INTERFACE CASE */
691                 temp_addr[0] |= 0X02;   /* set bit 2 ,
692                          - Locally Administered address  */
693
694         }
695         ndev->hard_header_len = ETH_HLEN + drvr->hdrlen;
696         ndev->ethtool_ops = &brcmf_ethtool_ops;
697
698         drvr->rxsz = ndev->mtu + ndev->hard_header_len +
699                               drvr->hdrlen;
700
701         memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
702
703         if (register_netdev(ndev) != 0) {
704                 brcmf_dbg(ERROR, "couldn't register the net device\n");
705                 goto fail;
706         }
707
708         brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
709
710         return 0;
711
712 fail:
713         ndev->netdev_ops = NULL;
714         return -EBADE;
715 }
716
717 struct brcmf_if *brcmf_add_if(struct device *dev, int ifidx, s32 bssidx,
718                               char *name, u8 *mac_addr)
719 {
720         struct brcmf_if *ifp;
721         struct net_device *ndev;
722         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
723         struct brcmf_pub *drvr = bus_if->drvr;
724
725         brcmf_dbg(TRACE, "idx %d\n", ifidx);
726
727         ifp = drvr->iflist[ifidx];
728         /*
729          * Delete the existing interface before overwriting it
730          * in case we missed the BRCMF_E_IF_DEL event.
731          */
732         if (ifp) {
733                 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
734                           ifp->ndev->name);
735                 netif_stop_queue(ifp->ndev);
736                 unregister_netdev(ifp->ndev);
737                 free_netdev(ifp->ndev);
738                 drvr->iflist[ifidx] = NULL;
739         }
740
741         /* Allocate netdev, including space for private structure */
742         ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
743         if (!ndev) {
744                 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
745                 return ERR_PTR(-ENOMEM);
746         }
747
748         ifp = netdev_priv(ndev);
749         ifp->ndev = ndev;
750         ifp->drvr = drvr;
751         drvr->iflist[ifidx] = ifp;
752         ifp->idx = ifidx;
753         ifp->bssidx = bssidx;
754         if (mac_addr != NULL)
755                 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
756
757         brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
758                   current->pid, ifp->ndev->name);
759
760         return ifp;
761 }
762
763 void brcmf_del_if(struct brcmf_pub *drvr, int ifidx)
764 {
765         struct brcmf_if *ifp;
766
767         brcmf_dbg(TRACE, "idx %d\n", ifidx);
768
769         ifp = drvr->iflist[ifidx];
770         if (!ifp) {
771                 brcmf_dbg(ERROR, "Null interface\n");
772                 return;
773         }
774         if (ifp->ndev) {
775                 if (ifidx == 0) {
776                         if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
777                                 rtnl_lock();
778                                 brcmf_netdev_stop(ifp->ndev);
779                                 rtnl_unlock();
780                         }
781                 } else {
782                         netif_stop_queue(ifp->ndev);
783                 }
784
785                 unregister_netdev(ifp->ndev);
786                 drvr->iflist[ifidx] = NULL;
787                 if (ifidx == 0)
788                         brcmf_cfg80211_detach(drvr->config);
789                 free_netdev(ifp->ndev);
790         }
791 }
792
793 int brcmf_attach(uint bus_hdrlen, struct device *dev)
794 {
795         struct brcmf_pub *drvr = NULL;
796         int ret = 0;
797
798         brcmf_dbg(TRACE, "Enter\n");
799
800         /* Allocate primary brcmf_info */
801         drvr = kzalloc(sizeof(struct brcmf_pub), GFP_ATOMIC);
802         if (!drvr)
803                 return -ENOMEM;
804
805         mutex_init(&drvr->proto_block);
806
807         /* Link to bus module */
808         drvr->hdrlen = bus_hdrlen;
809         drvr->bus_if = dev_get_drvdata(dev);
810         drvr->bus_if->drvr = drvr;
811         drvr->dev = dev;
812
813         /* create device debugfs folder */
814         brcmf_debugfs_attach(drvr);
815
816         /* Attach and link in the protocol */
817         ret = brcmf_proto_attach(drvr);
818         if (ret != 0) {
819                 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
820                 goto fail;
821         }
822
823         INIT_WORK(&drvr->setmacaddr_work, _brcmf_set_mac_address);
824         INIT_WORK(&drvr->multicast_work, _brcmf_set_multicast_list);
825
826         INIT_LIST_HEAD(&drvr->bus_if->dcmd_list);
827
828         init_waitqueue_head(&drvr->pend_8021x_wait);
829
830         return ret;
831
832 fail:
833         brcmf_detach(dev);
834
835         return ret;
836 }
837
838 int brcmf_bus_start(struct device *dev)
839 {
840         int ret = -1;
841         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
842         struct brcmf_pub *drvr = bus_if->drvr;
843         struct brcmf_if *ifp;
844
845         brcmf_dbg(TRACE, "\n");
846
847         /* Bring up the bus */
848         ret = bus_if->brcmf_bus_init(dev);
849         if (ret != 0) {
850                 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
851                 return ret;
852         }
853
854         /* add primary networking interface */
855         ifp = brcmf_add_if(dev, 0, 0, "wlan%d", NULL);
856         if (IS_ERR(ifp))
857                 return PTR_ERR(ifp);
858
859         /* signal bus ready */
860         bus_if->state = BRCMF_BUS_DATA;
861
862         /* Bus is ready, do any initialization */
863         ret = brcmf_c_preinit_dcmds(ifp);
864         if (ret < 0)
865                 goto fail;
866
867         drvr->config = brcmf_cfg80211_attach(drvr);
868         if (drvr->config == NULL) {
869                 ret = -ENOMEM;
870                 goto fail;
871         }
872
873         ret = brcmf_net_attach(ifp);
874 fail:
875         if (ret < 0) {
876                 brcmf_dbg(ERROR, "brcmf_net_attach failed");
877                 if (drvr->config)
878                         brcmf_cfg80211_detach(drvr->config);
879                 free_netdev(drvr->iflist[0]->ndev);
880                 drvr->iflist[0] = NULL;
881                 return ret;
882         }
883
884         return 0;
885 }
886
887 static void brcmf_bus_detach(struct brcmf_pub *drvr)
888 {
889         brcmf_dbg(TRACE, "Enter\n");
890
891         if (drvr) {
892                 /* Stop the protocol module */
893                 brcmf_proto_stop(drvr);
894
895                 /* Stop the bus module */
896                 drvr->bus_if->brcmf_bus_stop(drvr->dev);
897         }
898 }
899
900 void brcmf_detach(struct device *dev)
901 {
902         int i;
903         struct brcmf_bus *bus_if = dev_get_drvdata(dev);
904         struct brcmf_pub *drvr = bus_if->drvr;
905
906         brcmf_dbg(TRACE, "Enter\n");
907
908
909         /* make sure primary interface removed last */
910         for (i = BRCMF_MAX_IFS-1; i > -1; i--)
911                 if (drvr->iflist[i])
912                         brcmf_del_if(drvr, i);
913
914         brcmf_bus_detach(drvr);
915
916         if (drvr->prot) {
917                 cancel_work_sync(&drvr->setmacaddr_work);
918                 cancel_work_sync(&drvr->multicast_work);
919                 brcmf_proto_detach(drvr);
920         }
921
922         brcmf_debugfs_detach(drvr);
923         bus_if->drvr = NULL;
924         kfree(drvr);
925 }
926
927 static int brcmf_get_pend_8021x_cnt(struct brcmf_pub *drvr)
928 {
929         return atomic_read(&drvr->pend_8021x_cnt);
930 }
931
932 int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
933 {
934         struct brcmf_if *ifp = netdev_priv(ndev);
935         struct brcmf_pub *drvr = ifp->drvr;
936         int err;
937
938         err = wait_event_timeout(drvr->pend_8021x_wait,
939                                  !brcmf_get_pend_8021x_cnt(drvr),
940                                  msecs_to_jiffies(MAX_WAIT_FOR_8021X_TX));
941
942         WARN_ON(!err);
943
944         return !err;
945 }
946
947 static void brcmf_driver_init(struct work_struct *work)
948 {
949         brcmf_debugfs_init();
950
951 #ifdef CONFIG_BRCMFMAC_SDIO
952         brcmf_sdio_init();
953 #endif
954 #ifdef CONFIG_BRCMFMAC_USB
955         brcmf_usb_init();
956 #endif
957 }
958 static DECLARE_WORK(brcmf_driver_work, brcmf_driver_init);
959
960 static int __init brcmfmac_module_init(void)
961 {
962         if (!schedule_work(&brcmf_driver_work))
963                 return -EBUSY;
964
965         return 0;
966 }
967
968 static void __exit brcmfmac_module_exit(void)
969 {
970         cancel_work_sync(&brcmf_driver_work);
971
972 #ifdef CONFIG_BRCMFMAC_SDIO
973         brcmf_sdio_exit();
974 #endif
975 #ifdef CONFIG_BRCMFMAC_USB
976         brcmf_usb_exit();
977 #endif
978         brcmf_debugfs_exit();
979 }
980
981 module_init(brcmfmac_module_init);
982 module_exit(brcmfmac_module_exit);