2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
40 #include <brcmu_utils.h>
41 #include <brcmu_wifi.h>
45 #include "dhd_proto.h"
47 #include "wl_cfg80211.h"
49 MODULE_AUTHOR("Broadcom Corporation");
50 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
51 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
52 MODULE_LICENSE("Dual BSD/GPL");
55 /* Interface control information */
57 struct brcmf_pub *drvr; /* back pointer to brcmf_pub */
58 /* OS/stack specifics */
59 struct net_device *ndev;
60 struct net_device_stats stats;
61 int idx; /* iface idx in dongle */
62 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
66 int brcmf_msg_level = BRCMF_ERROR_VAL;
67 module_param(brcmf_msg_level, int, 0);
69 int brcmf_ifname2idx(struct brcmf_pub *drvr, char *name)
71 int i = BRCMF_MAX_IFS;
74 if (name == NULL || *name == '\0')
78 ifp = drvr->iflist[i];
79 if (ifp && !strncmp(ifp->ndev->name, name, IFNAMSIZ))
83 brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
85 return i; /* default - the primary interface */
88 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
90 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
91 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
95 if (drvr->iflist[ifidx] == NULL) {
96 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
100 if (drvr->iflist[ifidx]->ndev)
101 return drvr->iflist[ifidx]->ndev->name;
106 static void _brcmf_set_multicast_list(struct work_struct *work)
108 struct net_device *ndev;
109 struct netdev_hw_addr *ha;
112 __le32 dcmd_le_value;
114 struct brcmf_dcmd dcmd;
119 struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
122 ndev = drvr->iflist[0]->ndev;
123 cnt = netdev_mc_count(ndev);
125 /* Determine initial value of allmulti flag */
126 dcmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
128 /* Send down the multicast list first. */
130 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
131 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
135 strcpy(bufp, "mcast_list");
136 bufp += strlen("mcast_list") + 1;
138 cnt_le = cpu_to_le32(cnt);
139 memcpy(bufp, &cnt_le, sizeof(cnt));
140 bufp += sizeof(cnt_le);
142 netdev_for_each_mc_addr(ha, ndev) {
145 memcpy(bufp, ha->addr, ETH_ALEN);
150 memset(&dcmd, 0, sizeof(dcmd));
151 dcmd.cmd = BRCMF_C_SET_VAR;
156 ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
158 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
159 brcmf_ifname(drvr, 0), cnt);
160 dcmd_value = cnt ? true : dcmd_value;
165 /* Now send the allmulti setting. This is based on the setting in the
166 * net_device flags, but might be modified above to be turned on if we
167 * were trying to set some addresses and dongle rejected it...
170 buflen = sizeof("allmulti") + sizeof(dcmd_value);
171 buf = kmalloc(buflen, GFP_ATOMIC);
175 dcmd_le_value = cpu_to_le32(dcmd_value);
178 ("allmulti", (void *)&dcmd_le_value,
179 sizeof(dcmd_le_value), buf, buflen)) {
180 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
181 brcmf_ifname(drvr, 0),
182 (int)sizeof(dcmd_value), buflen);
187 memset(&dcmd, 0, sizeof(dcmd));
188 dcmd.cmd = BRCMF_C_SET_VAR;
193 ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
195 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
196 brcmf_ifname(drvr, 0),
197 le32_to_cpu(dcmd_le_value));
202 /* Finally, pick up the PROMISC flag as well, like the NIC
205 dcmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
206 dcmd_le_value = cpu_to_le32(dcmd_value);
208 memset(&dcmd, 0, sizeof(dcmd));
209 dcmd.cmd = BRCMF_C_SET_PROMISC;
210 dcmd.buf = &dcmd_le_value;
211 dcmd.len = sizeof(dcmd_le_value);
214 ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
216 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
217 brcmf_ifname(drvr, 0),
218 le32_to_cpu(dcmd_le_value));
223 _brcmf_set_mac_address(struct work_struct *work)
226 struct brcmf_dcmd dcmd;
229 struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
232 brcmf_dbg(TRACE, "enter\n");
233 if (!brcmf_c_mkiovar("cur_etheraddr", (char *)drvr->macvalue,
234 ETH_ALEN, buf, 32)) {
235 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
236 brcmf_ifname(drvr, 0));
239 memset(&dcmd, 0, sizeof(dcmd));
240 dcmd.cmd = BRCMF_C_SET_VAR;
245 ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
247 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
248 brcmf_ifname(drvr, 0));
250 memcpy(drvr->iflist[0]->ndev->dev_addr,
251 drvr->macvalue, ETH_ALEN);
256 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
258 struct brcmf_if *ifp = netdev_priv(ndev);
259 struct brcmf_pub *drvr = ifp->drvr;
260 struct sockaddr *sa = (struct sockaddr *)addr;
262 memcpy(&drvr->macvalue, sa->sa_data, ETH_ALEN);
263 schedule_work(&drvr->setmacaddr_work);
267 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
269 struct brcmf_if *ifp = netdev_priv(ndev);
270 struct brcmf_pub *drvr = ifp->drvr;
272 schedule_work(&drvr->multicast_work);
275 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
278 if (!drvr->bus_if->drvr_up || (drvr->bus_if->state == BRCMF_BUS_DOWN))
281 /* Update multicast statistic */
282 if (pktbuf->len >= ETH_ALEN) {
283 u8 *pktdata = (u8 *) (pktbuf->data);
284 struct ethhdr *eh = (struct ethhdr *)pktdata;
286 if (is_multicast_ether_addr(eh->h_dest))
287 drvr->tx_multicast++;
288 if (ntohs(eh->h_proto) == ETH_P_PAE)
289 atomic_inc(&drvr->pend_8021x_cnt);
292 /* If the protocol uses a data header, apply it */
293 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
295 /* Use bus module to send data frame */
296 return drvr->bus_if->brcmf_bus_txdata(drvr->dev, pktbuf);
299 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
302 struct brcmf_if *ifp = netdev_priv(ndev);
303 struct brcmf_pub *drvr = ifp->drvr;
305 brcmf_dbg(TRACE, "Enter\n");
308 if (!drvr->bus_if->drvr_up ||
309 (drvr->bus_if->state == BRCMF_BUS_DOWN)) {
310 brcmf_dbg(ERROR, "xmit rejected drvup=%d state=%d\n",
311 drvr->bus_if->drvr_up,
312 drvr->bus_if->state);
313 netif_stop_queue(ndev);
317 if (!drvr->iflist[ifp->idx]) {
318 brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
319 netif_stop_queue(ndev);
323 /* Make sure there's enough room for any header */
324 if (skb_headroom(skb) < drvr->hdrlen) {
325 struct sk_buff *skb2;
327 brcmf_dbg(INFO, "%s: insufficient headroom\n",
328 brcmf_ifname(drvr, ifp->idx));
329 drvr->bus_if->tx_realloc++;
330 skb2 = skb_realloc_headroom(skb, drvr->hdrlen);
334 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
335 brcmf_ifname(drvr, ifp->idx));
341 ret = brcmf_sendpkt(drvr, ifp->idx, skb);
345 drvr->bus_if->dstats.tx_dropped++;
347 drvr->bus_if->dstats.tx_packets++;
349 /* Return ok: we always eat the packet */
353 void brcmf_txflowcontrol(struct device *dev, int ifidx, bool state)
355 struct net_device *ndev;
356 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
357 struct brcmf_pub *drvr = bus_if->drvr;
359 brcmf_dbg(TRACE, "Enter\n");
361 ndev = drvr->iflist[ifidx]->ndev;
363 netif_stop_queue(ndev);
365 netif_wake_queue(ndev);
368 static int brcmf_host_event(struct brcmf_pub *drvr, int *ifidx,
369 void *pktdata, struct brcmf_event_msg *event,
374 bcmerror = brcmf_c_host_event(drvr, ifidx, pktdata, event, data);
378 if (drvr->iflist[*ifidx]->ndev)
379 brcmf_cfg80211_event(drvr->iflist[*ifidx]->ndev,
385 void brcmf_rx_frame(struct device *dev, int ifidx,
386 struct sk_buff_head *skb_list)
391 struct sk_buff *skb, *pnext;
392 struct brcmf_if *ifp;
393 struct brcmf_event_msg event;
394 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
395 struct brcmf_pub *drvr = bus_if->drvr;
397 brcmf_dbg(TRACE, "Enter\n");
399 skb_queue_walk_safe(skb_list, skb, pnext) {
400 skb_unlink(skb, skb_list);
402 /* Get the protocol, maintain skb around eth_type_trans()
403 * The main reason for this hack is for the limitation of
404 * Linux 2.4 where 'eth_type_trans' uses the
405 * 'net->hard_header_len'
406 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
407 * coping of the packet coming from the network stack to add
408 * BDC, Hardware header etc, during network interface
410 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
412 * for BDC, Hardware header etc. and not just the ETH_HLEN
417 ifp = drvr->iflist[ifidx];
419 ifp = drvr->iflist[0];
421 if (!ifp || !ifp->ndev ||
422 ifp->ndev->reg_state != NETREG_REGISTERED) {
423 brcmu_pkt_buf_free_skb(skb);
427 skb->dev = ifp->ndev;
428 skb->protocol = eth_type_trans(skb, skb->dev);
430 if (skb->pkt_type == PACKET_MULTICAST)
431 bus_if->dstats.multicast++;
436 /* Strip header, count, deliver upward */
437 skb_pull(skb, ETH_HLEN);
439 /* Process special event packets and then discard them */
440 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
441 brcmf_host_event(drvr, &ifidx,
445 if (drvr->iflist[ifidx]) {
446 ifp = drvr->iflist[ifidx];
447 ifp->ndev->last_rx = jiffies;
450 bus_if->dstats.rx_bytes += skb->len;
451 bus_if->dstats.rx_packets++; /* Local count */
456 /* If the receive is not processed inside an ISR,
457 * the softirqd must be woken explicitly to service
458 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
459 * by netif_rx_ni(), but in earlier kernels, we need
466 void brcmf_txcomplete(struct device *dev, struct sk_buff *txp, bool success)
471 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
472 struct brcmf_pub *drvr = bus_if->drvr;
474 brcmf_proto_hdrpull(dev, &ifidx, txp);
476 eh = (struct ethhdr *)(txp->data);
477 type = ntohs(eh->h_proto);
479 if (type == ETH_P_PAE)
480 atomic_dec(&drvr->pend_8021x_cnt);
484 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
486 struct brcmf_if *ifp = netdev_priv(ndev);
487 struct brcmf_bus *bus_if = ifp->drvr->bus_if;
489 brcmf_dbg(TRACE, "Enter\n");
491 /* Copy dongle stats to net device stats */
492 ifp->stats.rx_packets = bus_if->dstats.rx_packets;
493 ifp->stats.tx_packets = bus_if->dstats.tx_packets;
494 ifp->stats.rx_bytes = bus_if->dstats.rx_bytes;
495 ifp->stats.tx_bytes = bus_if->dstats.tx_bytes;
496 ifp->stats.rx_errors = bus_if->dstats.rx_errors;
497 ifp->stats.tx_errors = bus_if->dstats.tx_errors;
498 ifp->stats.rx_dropped = bus_if->dstats.rx_dropped;
499 ifp->stats.tx_dropped = bus_if->dstats.tx_dropped;
500 ifp->stats.multicast = bus_if->dstats.multicast;
505 /* Retrieve current toe component enables, which are kept
506 as a bitmap in toe_ol iovar */
507 static int brcmf_toe_get(struct brcmf_pub *drvr, int ifidx, u32 *toe_ol)
509 struct brcmf_dcmd dcmd;
514 memset(&dcmd, 0, sizeof(dcmd));
516 dcmd.cmd = BRCMF_C_GET_VAR;
518 dcmd.len = (uint) sizeof(buf);
521 strcpy(buf, "toe_ol");
522 ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
524 /* Check for older dongle image that doesn't support toe_ol */
526 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
527 brcmf_ifname(drvr, ifidx));
531 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
532 brcmf_ifname(drvr, ifidx), ret);
536 memcpy(&toe_le, buf, sizeof(u32));
537 *toe_ol = le32_to_cpu(toe_le);
541 /* Set current toe component enables in toe_ol iovar,
542 and set toe global enable iovar */
543 static int brcmf_toe_set(struct brcmf_pub *drvr, int ifidx, u32 toe_ol)
545 struct brcmf_dcmd dcmd;
548 __le32 toe_le = cpu_to_le32(toe_ol);
550 memset(&dcmd, 0, sizeof(dcmd));
552 dcmd.cmd = BRCMF_C_SET_VAR;
554 dcmd.len = (uint) sizeof(buf);
557 /* Set toe_ol as requested */
558 strcpy(buf, "toe_ol");
559 memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
561 ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
563 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
564 brcmf_ifname(drvr, ifidx), ret);
568 /* Enable toe globally only if any components are enabled. */
569 toe_le = cpu_to_le32(toe_ol != 0);
572 memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
574 ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
576 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
577 brcmf_ifname(drvr, ifidx), ret);
584 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
585 struct ethtool_drvinfo *info)
587 struct brcmf_if *ifp = netdev_priv(ndev);
588 struct brcmf_pub *drvr = ifp->drvr;
590 sprintf(info->driver, KBUILD_MODNAME);
591 sprintf(info->version, "%lu", drvr->drv_version);
592 sprintf(info->bus_info, "%s", dev_name(drvr->dev));
595 static const struct ethtool_ops brcmf_ethtool_ops = {
596 .get_drvinfo = brcmf_ethtool_get_drvinfo,
599 static int brcmf_ethtool(struct brcmf_pub *drvr, void __user *uaddr)
601 struct ethtool_drvinfo info;
602 char drvname[sizeof(info.driver)];
604 struct ethtool_value edata;
605 u32 toe_cmpnt, csum_dir;
608 brcmf_dbg(TRACE, "Enter\n");
610 /* all ethtool calls start with a cmd word */
611 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
615 case ETHTOOL_GDRVINFO:
616 /* Copy out any request driver name */
617 if (copy_from_user(&info, uaddr, sizeof(info)))
619 strncpy(drvname, info.driver, sizeof(info.driver));
620 drvname[sizeof(info.driver) - 1] = '\0';
622 /* clear struct for return */
623 memset(&info, 0, sizeof(info));
626 /* if requested, identify ourselves */
627 if (strcmp(drvname, "?dhd") == 0) {
628 sprintf(info.driver, "dhd");
629 strcpy(info.version, BRCMF_VERSION_STR);
632 /* otherwise, require dongle to be up */
633 else if (!drvr->bus_if->drvr_up) {
634 brcmf_dbg(ERROR, "dongle is not up\n");
638 /* finally, report dongle driver type */
640 sprintf(info.driver, "wl");
642 sprintf(info.driver, "xx");
644 sprintf(info.version, "%lu", drvr->drv_version);
645 if (copy_to_user(uaddr, &info, sizeof(info)))
647 brcmf_dbg(CTL, "given %*s, returning %s\n",
648 (int)sizeof(drvname), drvname, info.driver);
651 /* Get toe offload components from dongle */
652 case ETHTOOL_GRXCSUM:
653 case ETHTOOL_GTXCSUM:
654 ret = brcmf_toe_get(drvr, 0, &toe_cmpnt);
659 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
662 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
664 if (copy_to_user(uaddr, &edata, sizeof(edata)))
668 /* Set toe offload components in dongle */
669 case ETHTOOL_SRXCSUM:
670 case ETHTOOL_STXCSUM:
671 if (copy_from_user(&edata, uaddr, sizeof(edata)))
674 /* Read the current settings, update and write back */
675 ret = brcmf_toe_get(drvr, 0, &toe_cmpnt);
680 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
683 toe_cmpnt |= csum_dir;
685 toe_cmpnt &= ~csum_dir;
687 ret = brcmf_toe_set(drvr, 0, toe_cmpnt);
691 /* If setting TX checksum mode, tell Linux the new mode */
692 if (cmd == ETHTOOL_STXCSUM) {
694 drvr->iflist[0]->ndev->features |=
697 drvr->iflist[0]->ndev->features &=
710 static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
713 struct brcmf_if *ifp = netdev_priv(ndev);
714 struct brcmf_pub *drvr = ifp->drvr;
716 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
718 if (!drvr->iflist[ifp->idx])
721 if (cmd == SIOCETHTOOL)
722 return brcmf_ethtool(drvr, ifr->ifr_data);
727 /* called only from within this driver. Sends a command to the dongle. */
728 s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
730 struct brcmf_dcmd dcmd;
734 struct brcmf_if *ifp = netdev_priv(ndev);
735 struct brcmf_pub *drvr = ifp->drvr;
737 memset(&dcmd, 0, sizeof(dcmd));
742 if (dcmd.buf != NULL)
743 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
745 /* send to dongle (must be up, and wl) */
746 if ((drvr->bus_if->state != BRCMF_BUS_DATA)) {
747 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
758 * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
759 * set key CMD to prevent M4 encryption.
761 is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
762 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
763 !(strncmp("wsec_key", dcmd.buf, 9))) ||
764 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
765 !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
767 brcmf_netdev_wait_pend8021x(ndev);
769 err = brcmf_proto_dcmd(drvr, ifp->idx, &dcmd, buflen);
778 static int brcmf_netdev_stop(struct net_device *ndev)
780 struct brcmf_if *ifp = netdev_priv(ndev);
781 struct brcmf_pub *drvr = ifp->drvr;
783 brcmf_dbg(TRACE, "Enter\n");
784 brcmf_cfg80211_down(drvr->config);
785 if (drvr->bus_if->drvr_up == 0)
788 /* Set state and stop OS transmissions */
789 drvr->bus_if->drvr_up = false;
790 netif_stop_queue(ndev);
795 static int brcmf_netdev_open(struct net_device *ndev)
797 struct brcmf_if *ifp = netdev_priv(ndev);
798 struct brcmf_pub *drvr = ifp->drvr;
799 struct brcmf_bus *bus_if = drvr->bus_if;
804 brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
806 if (ifp->idx == 0) { /* do it only for primary eth0 */
807 /* If bus is not ready, can't continue */
808 if (bus_if->state != BRCMF_BUS_DATA) {
809 brcmf_dbg(ERROR, "failed bus is not ready\n");
813 atomic_set(&drvr->pend_8021x_cnt, 0);
815 memcpy(ndev->dev_addr, drvr->mac, ETH_ALEN);
817 /* Get current TOE mode from dongle */
818 if (brcmf_toe_get(drvr, ifp->idx, &toe_ol) >= 0
819 && (toe_ol & TOE_TX_CSUM_OL) != 0)
820 drvr->iflist[ifp->idx]->ndev->features |=
823 drvr->iflist[ifp->idx]->ndev->features &=
827 /* make sure RF is ready for work */
828 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_UP, (char *)&up, sizeof(up));
830 /* Allow transmit calls */
831 netif_start_queue(ndev);
832 drvr->bus_if->drvr_up = true;
833 if (brcmf_cfg80211_up(drvr->config)) {
834 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
841 static const struct net_device_ops brcmf_netdev_ops_pri = {
842 .ndo_open = brcmf_netdev_open,
843 .ndo_stop = brcmf_netdev_stop,
844 .ndo_get_stats = brcmf_netdev_get_stats,
845 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
846 .ndo_start_xmit = brcmf_netdev_start_xmit,
847 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
848 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
851 static int brcmf_net_attach(struct brcmf_if *ifp)
853 struct brcmf_pub *drvr = ifp->drvr;
854 struct net_device *ndev;
855 u8 temp_addr[ETH_ALEN];
857 brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
859 ndev = drvr->iflist[ifp->idx]->ndev;
860 ndev->netdev_ops = &brcmf_netdev_ops_pri;
863 * determine mac address to use
865 if (is_valid_ether_addr(ifp->mac_addr))
866 memcpy(temp_addr, ifp->mac_addr, ETH_ALEN);
868 memcpy(temp_addr, drvr->mac, ETH_ALEN);
871 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
872 /* ACCESSPOINT INTERFACE CASE */
873 temp_addr[0] |= 0X02; /* set bit 2 ,
874 - Locally Administered address */
877 ndev->hard_header_len = ETH_HLEN + drvr->hdrlen;
878 ndev->ethtool_ops = &brcmf_ethtool_ops;
880 drvr->rxsz = ndev->mtu + ndev->hard_header_len +
883 memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
885 /* attach to cfg80211 for primary interface */
887 drvr->config = brcmf_cfg80211_attach(ndev, drvr->dev, drvr);
888 if (drvr->config == NULL) {
889 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
894 if (register_netdev(ndev) != 0) {
895 brcmf_dbg(ERROR, "couldn't register the net device\n");
899 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
904 ndev->netdev_ops = NULL;
909 brcmf_add_if(struct device *dev, int ifidx, char *name, u8 *mac_addr)
911 struct brcmf_if *ifp;
912 struct net_device *ndev;
913 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
914 struct brcmf_pub *drvr = bus_if->drvr;
916 brcmf_dbg(TRACE, "idx %d\n", ifidx);
918 ifp = drvr->iflist[ifidx];
920 * Delete the existing interface before overwriting it
921 * in case we missed the BRCMF_E_IF_DEL event.
924 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
926 netif_stop_queue(ifp->ndev);
927 unregister_netdev(ifp->ndev);
928 free_netdev(ifp->ndev);
929 drvr->iflist[ifidx] = NULL;
932 /* Allocate netdev, including space for private structure */
933 ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
935 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
939 ifp = netdev_priv(ndev);
942 drvr->iflist[ifidx] = ifp;
944 if (mac_addr != NULL)
945 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
947 if (brcmf_net_attach(ifp)) {
948 brcmf_dbg(ERROR, "brcmf_net_attach failed");
949 free_netdev(ifp->ndev);
950 drvr->iflist[ifidx] = NULL;
954 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
955 current->pid, ifp->ndev->name);
960 void brcmf_del_if(struct brcmf_pub *drvr, int ifidx)
962 struct brcmf_if *ifp;
964 brcmf_dbg(TRACE, "idx %d\n", ifidx);
966 ifp = drvr->iflist[ifidx];
968 brcmf_dbg(ERROR, "Null interface\n");
973 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
975 brcmf_netdev_stop(ifp->ndev);
979 netif_stop_queue(ifp->ndev);
982 unregister_netdev(ifp->ndev);
983 drvr->iflist[ifidx] = NULL;
985 brcmf_cfg80211_detach(drvr->config);
986 free_netdev(ifp->ndev);
990 int brcmf_attach(uint bus_hdrlen, struct device *dev)
992 struct brcmf_pub *drvr = NULL;
995 brcmf_dbg(TRACE, "Enter\n");
997 /* Allocate primary brcmf_info */
998 drvr = kzalloc(sizeof(struct brcmf_pub), GFP_ATOMIC);
1002 mutex_init(&drvr->proto_block);
1004 /* Link to bus module */
1005 drvr->hdrlen = bus_hdrlen;
1006 drvr->bus_if = dev_get_drvdata(dev);
1007 drvr->bus_if->drvr = drvr;
1010 /* Attach and link in the protocol */
1011 ret = brcmf_proto_attach(drvr);
1013 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
1017 INIT_WORK(&drvr->setmacaddr_work, _brcmf_set_mac_address);
1018 INIT_WORK(&drvr->multicast_work, _brcmf_set_multicast_list);
1028 int brcmf_bus_start(struct device *dev)
1031 /* Room for "event_msgs" + '\0' + bitvec */
1032 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1033 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1034 struct brcmf_pub *drvr = bus_if->drvr;
1036 brcmf_dbg(TRACE, "\n");
1038 /* Bring up the bus */
1039 ret = bus_if->brcmf_bus_init(dev);
1041 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
1045 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1046 iovbuf, sizeof(iovbuf));
1047 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1049 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1051 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1052 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1053 setbit(drvr->eventmask, BRCMF_E_AUTH);
1054 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1055 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1056 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1057 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1058 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1059 setbit(drvr->eventmask, BRCMF_E_JOIN);
1060 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1061 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1062 setbit(drvr->eventmask, BRCMF_E_LINK);
1063 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1064 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1065 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1066 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1067 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1068 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1070 /* enable dongle roaming event */
1072 drvr->pktfilter_count = 1;
1073 /* Setup filter to allow only unicast */
1074 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1076 /* Bus is ready, do any protocol initialization */
1077 ret = brcmf_proto_init(drvr);
1081 /* add primary networking interface */
1082 ret = brcmf_add_if(dev, 0, "wlan%d", drvr->mac);
1086 /* signal bus ready */
1087 bus_if->state = BRCMF_BUS_DATA;
1091 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1093 brcmf_dbg(TRACE, "Enter\n");
1096 /* Stop the protocol module */
1097 brcmf_proto_stop(drvr);
1099 /* Stop the bus module */
1100 drvr->bus_if->brcmf_bus_stop(drvr->dev);
1104 void brcmf_detach(struct device *dev)
1107 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1108 struct brcmf_pub *drvr = bus_if->drvr;
1110 brcmf_dbg(TRACE, "Enter\n");
1113 /* make sure primary interface removed last */
1114 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
1115 if (drvr->iflist[i])
1116 brcmf_del_if(drvr, i);
1118 brcmf_bus_detach(drvr);
1121 cancel_work_sync(&drvr->setmacaddr_work);
1122 cancel_work_sync(&drvr->multicast_work);
1123 brcmf_proto_detach(drvr);
1126 bus_if->drvr = NULL;
1130 static int brcmf_get_pend_8021x_cnt(struct brcmf_pub *drvr)
1132 return atomic_read(&drvr->pend_8021x_cnt);
1135 #define MAX_WAIT_FOR_8021X_TX 10
1137 int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1139 struct brcmf_if *ifp = netdev_priv(ndev);
1140 struct brcmf_pub *drvr = ifp->drvr;
1141 int timeout = 10 * HZ / 1000;
1142 int ntimes = MAX_WAIT_FOR_8021X_TX;
1143 int pend = brcmf_get_pend_8021x_cnt(drvr);
1145 while (ntimes && pend) {
1147 set_current_state(TASK_INTERRUPTIBLE);
1148 schedule_timeout(timeout);
1149 set_current_state(TASK_RUNNING);
1152 pend = brcmf_get_pend_8021x_cnt(drvr);
1158 int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
1162 mm_segment_t old_fs;
1165 /* change to KERNEL_DS address limit */
1169 /* open file to write */
1170 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1172 brcmf_dbg(ERROR, "open file error\n");
1177 /* Write buf to file */
1178 fp->f_op->write(fp, (char __user *)buf, size, &pos);
1181 /* free buf before return */
1183 /* close file before return */
1185 filp_close(fp, NULL);
1186 /* restore previous address limit */
1193 static void brcmf_driver_init(struct work_struct *work)
1195 #ifdef CONFIG_BRCMFMAC_SDIO
1198 #ifdef CONFIG_BRCMFMAC_USB
1202 static DECLARE_WORK(brcmf_driver_work, brcmf_driver_init);
1204 static int __init brcmfmac_module_init(void)
1206 if (!schedule_work(&brcmf_driver_work))
1212 static void __exit brcmfmac_module_exit(void)
1214 cancel_work_sync(&brcmf_driver_work);
1216 #ifdef CONFIG_BRCMFMAC_SDIO
1219 #ifdef CONFIG_BRCMFMAC_USB
1224 module_init(brcmfmac_module_init);
1225 module_exit(brcmfmac_module_exit);