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