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 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/mmc/sdio_func.h>
25 #include <linux/random.h>
26 #include <linux/spinlock.h>
27 #include <linux/ethtool.h>
28 #include <linux/fcntl.h>
30 #include <linux/uaccess.h>
31 #include <linux/hardirq.h>
32 #include <linux/mutex.h>
33 #include <linux/wait.h>
34 #include <linux/module.h>
35 #include <net/cfg80211.h>
36 #include <net/rtnetlink.h>
38 #include <brcmu_utils.h>
39 #include <brcmu_wifi.h>
43 #include "dhd_proto.h"
45 #include "wl_cfg80211.h"
48 MODULE_AUTHOR("Broadcom Corporation");
49 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
50 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
51 MODULE_LICENSE("Dual BSD/GPL");
54 /* Interface control information */
56 struct brcmf_info *info; /* back pointer to brcmf_info */
57 /* OS/stack specifics */
58 struct net_device *ndev;
59 struct net_device_stats stats;
60 int idx; /* iface idx in dongle */
61 int state; /* interface state */
62 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
65 /* Local private structure (extension of pub) */
69 /* OS/stack specifics */
70 struct brcmf_if *iflist[BRCMF_MAX_IFS];
72 struct mutex proto_block;
74 struct work_struct setmacaddr_work;
75 struct work_struct multicast_work;
76 u8 macvalue[ETH_ALEN];
77 atomic_t pend_8021x_cnt;
81 module_param(brcmf_msg_level, int, 0);
84 static int brcmf_net2idx(struct brcmf_info *drvr_priv, struct net_device *ndev)
88 while (i < BRCMF_MAX_IFS) {
89 if (drvr_priv->iflist[i] && drvr_priv->iflist[i]->ndev == ndev)
97 int brcmf_ifname2idx(struct brcmf_info *drvr_priv, char *name)
99 int i = BRCMF_MAX_IFS;
100 struct brcmf_if *ifp;
102 if (name == NULL || *name == '\0')
106 ifp = drvr_priv->iflist[i];
107 if (ifp && !strncmp(ifp->ndev->name, name, IFNAMSIZ))
111 brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
113 return i; /* default - the primary interface */
116 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
118 struct brcmf_info *drvr_priv = drvr->info;
120 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
121 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
125 if (drvr_priv->iflist[ifidx] == NULL) {
126 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
130 if (drvr_priv->iflist[ifidx]->ndev)
131 return drvr_priv->iflist[ifidx]->ndev->name;
136 static void _brcmf_set_multicast_list(struct work_struct *work)
138 struct net_device *ndev;
139 struct netdev_hw_addr *ha;
142 __le32 dcmd_le_value;
144 struct brcmf_dcmd dcmd;
149 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
152 ndev = drvr_priv->iflist[0]->ndev;
153 cnt = netdev_mc_count(ndev);
155 /* Determine initial value of allmulti flag */
156 dcmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
158 /* Send down the multicast list first. */
160 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
161 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
165 strcpy(bufp, "mcast_list");
166 bufp += strlen("mcast_list") + 1;
168 cnt_le = cpu_to_le32(cnt);
169 memcpy(bufp, &cnt_le, sizeof(cnt));
170 bufp += sizeof(cnt_le);
172 netdev_for_each_mc_addr(ha, ndev) {
175 memcpy(bufp, ha->addr, ETH_ALEN);
180 memset(&dcmd, 0, sizeof(dcmd));
181 dcmd.cmd = BRCMF_C_SET_VAR;
186 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
188 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
189 brcmf_ifname(&drvr_priv->pub, 0), cnt);
190 dcmd_value = cnt ? true : dcmd_value;
195 /* Now send the allmulti setting. This is based on the setting in the
196 * net_device flags, but might be modified above to be turned on if we
197 * were trying to set some addresses and dongle rejected it...
200 buflen = sizeof("allmulti") + sizeof(dcmd_value);
201 buf = kmalloc(buflen, GFP_ATOMIC);
205 dcmd_le_value = cpu_to_le32(dcmd_value);
208 ("allmulti", (void *)&dcmd_le_value,
209 sizeof(dcmd_le_value), buf, buflen)) {
210 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
211 brcmf_ifname(&drvr_priv->pub, 0),
212 (int)sizeof(dcmd_value), buflen);
217 memset(&dcmd, 0, sizeof(dcmd));
218 dcmd.cmd = BRCMF_C_SET_VAR;
223 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
225 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
226 brcmf_ifname(&drvr_priv->pub, 0),
227 le32_to_cpu(dcmd_le_value));
232 /* Finally, pick up the PROMISC flag as well, like the NIC
235 dcmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
236 dcmd_le_value = cpu_to_le32(dcmd_value);
238 memset(&dcmd, 0, sizeof(dcmd));
239 dcmd.cmd = BRCMF_C_SET_PROMISC;
240 dcmd.buf = &dcmd_le_value;
241 dcmd.len = sizeof(dcmd_le_value);
244 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
246 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
247 brcmf_ifname(&drvr_priv->pub, 0),
248 le32_to_cpu(dcmd_le_value));
253 _brcmf_set_mac_address(struct work_struct *work)
256 struct brcmf_dcmd dcmd;
259 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
262 brcmf_dbg(TRACE, "enter\n");
263 if (!brcmf_c_mkiovar("cur_etheraddr", (char *)drvr_priv->macvalue,
264 ETH_ALEN, buf, 32)) {
265 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
266 brcmf_ifname(&drvr_priv->pub, 0));
269 memset(&dcmd, 0, sizeof(dcmd));
270 dcmd.cmd = BRCMF_C_SET_VAR;
275 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
277 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
278 brcmf_ifname(&drvr_priv->pub, 0));
280 memcpy(drvr_priv->iflist[0]->ndev->dev_addr,
281 drvr_priv->macvalue, ETH_ALEN);
286 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
288 struct brcmf_info *drvr_priv = *(struct brcmf_info **)
290 struct sockaddr *sa = (struct sockaddr *)addr;
293 ifidx = brcmf_net2idx(drvr_priv, ndev);
294 if (ifidx == BRCMF_BAD_IF)
297 memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
298 schedule_work(&drvr_priv->setmacaddr_work);
302 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
304 struct brcmf_info *drvr_priv = *(struct brcmf_info **)
308 ifidx = brcmf_net2idx(drvr_priv, ndev);
309 if (ifidx == BRCMF_BAD_IF)
312 schedule_work(&drvr_priv->multicast_work);
315 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
317 struct brcmf_info *drvr_priv = drvr->info;
320 if (!drvr->up || (drvr->busstate == BRCMF_BUS_DOWN))
323 /* Update multicast statistic */
324 if (pktbuf->len >= ETH_ALEN) {
325 u8 *pktdata = (u8 *) (pktbuf->data);
326 struct ethhdr *eh = (struct ethhdr *)pktdata;
328 if (is_multicast_ether_addr(eh->h_dest))
329 drvr->tx_multicast++;
330 if (ntohs(eh->h_proto) == ETH_P_PAE)
331 atomic_inc(&drvr_priv->pend_8021x_cnt);
334 /* If the protocol uses a data header, apply it */
335 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
337 /* Use bus module to send data frame */
338 return brcmf_sdbrcm_bus_txdata(drvr->bus, pktbuf);
341 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
344 struct brcmf_info *drvr_priv = *(struct brcmf_info **)
348 brcmf_dbg(TRACE, "Enter\n");
351 if (!drvr_priv->pub.up || (drvr_priv->pub.busstate == BRCMF_BUS_DOWN)) {
352 brcmf_dbg(ERROR, "xmit rejected pub.up=%d busstate=%d\n",
353 drvr_priv->pub.up, drvr_priv->pub.busstate);
354 netif_stop_queue(ndev);
358 ifidx = brcmf_net2idx(drvr_priv, ndev);
359 if (ifidx == BRCMF_BAD_IF) {
360 brcmf_dbg(ERROR, "bad ifidx %d\n", ifidx);
361 netif_stop_queue(ndev);
365 /* Make sure there's enough room for any header */
366 if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
367 struct sk_buff *skb2;
369 brcmf_dbg(INFO, "%s: insufficient headroom\n",
370 brcmf_ifname(&drvr_priv->pub, ifidx));
371 drvr_priv->pub.tx_realloc++;
372 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
376 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
377 brcmf_ifname(&drvr_priv->pub, ifidx));
383 ret = brcmf_sendpkt(&drvr_priv->pub, ifidx, skb);
387 drvr_priv->pub.dstats.tx_dropped++;
389 drvr_priv->pub.tx_packets++;
391 /* Return ok: we always eat the packet */
395 void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
397 struct net_device *ndev;
398 struct brcmf_info *drvr_priv = drvr->info;
400 brcmf_dbg(TRACE, "Enter\n");
403 ndev = drvr_priv->iflist[ifidx]->ndev;
405 netif_stop_queue(ndev);
407 netif_wake_queue(ndev);
410 static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx,
411 void *pktdata, struct brcmf_event_msg *event,
416 bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
420 if (drvr_priv->iflist[*ifidx]->ndev)
421 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->ndev,
427 void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx, struct sk_buff *skb,
430 struct brcmf_info *drvr_priv = drvr->info;
434 struct sk_buff *pnext, *save_pktbuf;
436 struct brcmf_if *ifp;
437 struct brcmf_event_msg event;
439 brcmf_dbg(TRACE, "Enter\n");
443 for (i = 0; skb && i < numpkt; i++, skb = pnext) {
448 /* Get the protocol, maintain skb around eth_type_trans()
449 * The main reason for this hack is for the limitation of
450 * Linux 2.4 where 'eth_type_trans' uses the
451 * 'net->hard_header_len'
452 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
453 * coping of the packet coming from the network stack to add
454 * BDC, Hardware header etc, during network interface
456 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
458 * for BDC, Hardware header etc. and not just the ETH_HLEN
463 ifp = drvr_priv->iflist[ifidx];
465 ifp = drvr_priv->iflist[0];
467 skb->dev = ifp->ndev;
468 skb->protocol = eth_type_trans(skb, skb->dev);
470 if (skb->pkt_type == PACKET_MULTICAST)
471 drvr_priv->pub.rx_multicast++;
476 /* Strip header, count, deliver upward */
477 skb_pull(skb, ETH_HLEN);
479 /* Process special event packets and then discard them */
480 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
481 brcmf_host_event(drvr_priv, &ifidx,
485 if (drvr_priv->iflist[ifidx] &&
486 !drvr_priv->iflist[ifidx]->state)
487 ifp = drvr_priv->iflist[ifidx];
490 ifp->ndev->last_rx = jiffies;
492 drvr->dstats.rx_bytes += skb->len;
493 drvr->rx_packets++; /* Local count */
498 /* If the receive is not processed inside an ISR,
499 * the softirqd must be woken explicitly to service
500 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
501 * by netif_rx_ni(), but in earlier kernels, we need
508 void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
511 struct brcmf_info *drvr_priv = drvr->info;
515 brcmf_proto_hdrpull(drvr, &ifidx, txp);
517 eh = (struct ethhdr *)(txp->data);
518 type = ntohs(eh->h_proto);
520 if (type == ETH_P_PAE)
521 atomic_dec(&drvr_priv->pend_8021x_cnt);
525 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
527 struct brcmf_info *drvr_priv = *(struct brcmf_info **)
529 struct brcmf_if *ifp;
532 brcmf_dbg(TRACE, "Enter\n");
534 ifidx = brcmf_net2idx(drvr_priv, ndev);
535 if (ifidx == BRCMF_BAD_IF)
538 ifp = drvr_priv->iflist[ifidx];
540 if (drvr_priv->pub.up)
541 /* Use the protocol to get dongle stats */
542 brcmf_proto_dstats(&drvr_priv->pub);
544 /* Copy dongle stats to net device stats */
545 ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
546 ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
547 ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
548 ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
549 ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
550 ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
551 ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
552 ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
553 ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
558 /* Retrieve current toe component enables, which are kept
559 as a bitmap in toe_ol iovar */
560 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
562 struct brcmf_dcmd dcmd;
567 memset(&dcmd, 0, sizeof(dcmd));
569 dcmd.cmd = BRCMF_C_GET_VAR;
571 dcmd.len = (uint) sizeof(buf);
574 strcpy(buf, "toe_ol");
575 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
577 /* Check for older dongle image that doesn't support toe_ol */
579 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
580 brcmf_ifname(&drvr_priv->pub, ifidx));
584 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
585 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
589 memcpy(&toe_le, buf, sizeof(u32));
590 *toe_ol = le32_to_cpu(toe_le);
594 /* Set current toe component enables in toe_ol iovar,
595 and set toe global enable iovar */
596 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
598 struct brcmf_dcmd dcmd;
601 __le32 toe_le = cpu_to_le32(toe_ol);
603 memset(&dcmd, 0, sizeof(dcmd));
605 dcmd.cmd = BRCMF_C_SET_VAR;
607 dcmd.len = (uint) sizeof(buf);
610 /* Set toe_ol as requested */
611 strcpy(buf, "toe_ol");
612 memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
614 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
616 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
617 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
621 /* Enable toe globally only if any components are enabled. */
622 toe_le = cpu_to_le32(toe_ol != 0);
625 memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
627 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
629 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
630 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
637 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
638 struct ethtool_drvinfo *info)
640 struct brcmf_info *drvr_priv = *(struct brcmf_info **)
643 sprintf(info->driver, KBUILD_MODNAME);
644 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
645 sprintf(info->fw_version, "%s", BCM4329_FW_NAME);
646 sprintf(info->bus_info, "%s",
647 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
650 static struct ethtool_ops brcmf_ethtool_ops = {
651 .get_drvinfo = brcmf_ethtool_get_drvinfo
654 static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
656 struct ethtool_drvinfo info;
657 char drvname[sizeof(info.driver)];
659 struct ethtool_value edata;
660 u32 toe_cmpnt, csum_dir;
663 brcmf_dbg(TRACE, "Enter\n");
665 /* all ethtool calls start with a cmd word */
666 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
670 case ETHTOOL_GDRVINFO:
671 /* Copy out any request driver name */
672 if (copy_from_user(&info, uaddr, sizeof(info)))
674 strncpy(drvname, info.driver, sizeof(info.driver));
675 drvname[sizeof(info.driver) - 1] = '\0';
677 /* clear struct for return */
678 memset(&info, 0, sizeof(info));
681 /* if requested, identify ourselves */
682 if (strcmp(drvname, "?dhd") == 0) {
683 sprintf(info.driver, "dhd");
684 strcpy(info.version, BRCMF_VERSION_STR);
687 /* otherwise, require dongle to be up */
688 else if (!drvr_priv->pub.up) {
689 brcmf_dbg(ERROR, "dongle is not up\n");
693 /* finally, report dongle driver type */
694 else if (drvr_priv->pub.iswl)
695 sprintf(info.driver, "wl");
697 sprintf(info.driver, "xx");
699 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
700 if (copy_to_user(uaddr, &info, sizeof(info)))
702 brcmf_dbg(CTL, "given %*s, returning %s\n",
703 (int)sizeof(drvname), drvname, info.driver);
706 /* Get toe offload components from dongle */
707 case ETHTOOL_GRXCSUM:
708 case ETHTOOL_GTXCSUM:
709 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
714 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
717 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
719 if (copy_to_user(uaddr, &edata, sizeof(edata)))
723 /* Set toe offload components in dongle */
724 case ETHTOOL_SRXCSUM:
725 case ETHTOOL_STXCSUM:
726 if (copy_from_user(&edata, uaddr, sizeof(edata)))
729 /* Read the current settings, update and write back */
730 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
735 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
738 toe_cmpnt |= csum_dir;
740 toe_cmpnt &= ~csum_dir;
742 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
746 /* If setting TX checksum mode, tell Linux the new mode */
747 if (cmd == ETHTOOL_STXCSUM) {
749 drvr_priv->iflist[0]->ndev->features |=
752 drvr_priv->iflist[0]->ndev->features &=
765 static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
768 struct brcmf_info *drvr_priv = *(struct brcmf_info **)
772 ifidx = brcmf_net2idx(drvr_priv, ndev);
773 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifidx, cmd);
775 if (ifidx == BRCMF_BAD_IF)
778 if (cmd == SIOCETHTOOL)
779 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
784 /* called only from within this driver. Sends a command to the dongle. */
785 s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
787 struct brcmf_dcmd dcmd;
791 struct brcmf_info *drvr_priv = *(struct brcmf_info **)
795 memset(&dcmd, 0, sizeof(dcmd));
800 ifidx = brcmf_net2idx(drvr_priv, ndev);
802 if (dcmd.buf != NULL)
803 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
805 /* send to dongle (must be up, and wl) */
806 if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
807 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
812 if (!drvr_priv->pub.iswl) {
818 * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
819 * set key CMD to prevent M4 encryption.
821 is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
822 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
823 !(strncmp("wsec_key", dcmd.buf, 9))) ||
824 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
825 !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
827 brcmf_netdev_wait_pend8021x(ndev);
829 err = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, buflen);
838 static int brcmf_netdev_stop(struct net_device *ndev)
840 struct brcmf_pub *drvr = *(struct brcmf_pub **) netdev_priv(ndev);
842 brcmf_dbg(TRACE, "Enter\n");
843 brcmf_cfg80211_down(drvr->config);
847 /* Set state and stop OS transmissions */
849 netif_stop_queue(ndev);
854 static int brcmf_netdev_open(struct net_device *ndev)
856 struct brcmf_info *drvr_priv = *(struct brcmf_info **)
859 int ifidx = brcmf_net2idx(drvr_priv, ndev);
862 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
864 if (ifidx == 0) { /* do it only for primary eth0 */
866 /* try to bring up bus */
867 ret = brcmf_bus_start(&drvr_priv->pub);
869 brcmf_dbg(ERROR, "failed with code %d\n", ret);
872 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
874 memcpy(ndev->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
876 /* Get current TOE mode from dongle */
877 if (brcmf_toe_get(drvr_priv, ifidx, &toe_ol) >= 0
878 && (toe_ol & TOE_TX_CSUM_OL) != 0)
879 drvr_priv->iflist[ifidx]->ndev->features |=
882 drvr_priv->iflist[ifidx]->ndev->features &=
885 /* Allow transmit calls */
886 netif_start_queue(ndev);
887 drvr_priv->pub.up = 1;
888 if (brcmf_cfg80211_up(drvr_priv->pub.config)) {
889 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
897 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, struct net_device *ndev,
898 char *name, u8 *mac_addr, u32 flags, u8 bssidx)
900 struct brcmf_if *ifp;
901 int ret = 0, err = 0;
903 brcmf_dbg(TRACE, "idx %d, handle->%p\n", ifidx, ndev);
905 ifp = drvr_priv->iflist[ifidx];
907 ifp = kmalloc(sizeof(struct brcmf_if), GFP_ATOMIC);
912 memset(ifp, 0, sizeof(struct brcmf_if));
913 ifp->info = drvr_priv;
914 drvr_priv->iflist[ifidx] = ifp;
915 if (mac_addr != NULL)
916 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
919 ifp->state = BRCMF_E_IF_ADD;
922 * Delete the existing interface before overwriting it
923 * in case we missed the BRCMF_E_IF_DEL event.
925 if (ifp->ndev != NULL) {
926 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
928 netif_stop_queue(ifp->ndev);
929 unregister_netdev(ifp->ndev);
930 free_netdev(ifp->ndev);
933 /* Allocate netdev, including space for private structure */
934 ifp->ndev = alloc_netdev(sizeof(drvr_priv), "wlan%d",
937 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
942 memcpy(netdev_priv(ifp->ndev), &drvr_priv,
944 err = brcmf_net_attach(&drvr_priv->pub, ifp->idx);
946 brcmf_dbg(ERROR, "brcmf_net_attach failed, err %d\n",
950 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
951 current->pid, ifp->ndev->name);
958 free_netdev(ifp->ndev);
960 drvr_priv->iflist[ifp->idx] = NULL;
969 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
971 struct brcmf_if *ifp;
973 brcmf_dbg(TRACE, "idx %d\n", ifidx);
975 ifp = drvr_priv->iflist[ifidx];
977 brcmf_dbg(ERROR, "Null interface\n");
981 ifp->state = BRCMF_E_IF_DEL;
983 if (ifp->ndev != NULL) {
984 netif_stop_queue(ifp->ndev);
985 unregister_netdev(ifp->ndev);
986 free_netdev(ifp->ndev);
987 drvr_priv->iflist[ifidx] = NULL;
992 struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
994 struct brcmf_info *drvr_priv = NULL;
995 struct net_device *ndev;
997 brcmf_dbg(TRACE, "Enter\n");
999 /* Allocate netdev, including space for private structure */
1000 ndev = alloc_netdev(sizeof(drvr_priv), "wlan%d", ether_setup);
1002 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
1006 /* Allocate primary brcmf_info */
1007 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
1012 * Save the brcmf_info into the priv
1014 memcpy(netdev_priv(ndev), &drvr_priv, sizeof(drvr_priv));
1016 if (brcmf_add_if(drvr_priv, 0, ndev, ndev->name, NULL, 0, 0) ==
1020 ndev->netdev_ops = NULL;
1021 mutex_init(&drvr_priv->proto_block);
1023 /* Link to info module */
1024 drvr_priv->pub.info = drvr_priv;
1026 /* Link to bus module */
1027 drvr_priv->pub.bus = bus;
1028 drvr_priv->pub.hdrlen = bus_hdrlen;
1030 /* Attach and link in the protocol */
1031 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
1032 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
1036 /* Attach and link in the cfg80211 */
1037 drvr_priv->pub.config =
1038 brcmf_cfg80211_attach(ndev,
1039 brcmf_bus_get_device(bus),
1041 if (drvr_priv->pub.config == NULL) {
1042 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1046 INIT_WORK(&drvr_priv->setmacaddr_work, _brcmf_set_mac_address);
1047 INIT_WORK(&drvr_priv->multicast_work, _brcmf_set_multicast_list);
1050 * Save the brcmf_info into the priv
1052 memcpy(netdev_priv(ndev), &drvr_priv, sizeof(drvr_priv));
1054 return &drvr_priv->pub;
1060 brcmf_detach(&drvr_priv->pub);
1065 int brcmf_bus_start(struct brcmf_pub *drvr)
1068 struct brcmf_info *drvr_priv = drvr->info;
1069 /* Room for "event_msgs" + '\0' + bitvec */
1070 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1072 brcmf_dbg(TRACE, "\n");
1074 /* Bring up the bus */
1075 ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub);
1077 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
1081 /* If bus is not ready, can't come up */
1082 if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
1083 brcmf_dbg(ERROR, "failed bus is not ready\n");
1087 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1088 iovbuf, sizeof(iovbuf));
1089 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1091 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1093 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1094 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1095 setbit(drvr->eventmask, BRCMF_E_AUTH);
1096 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1097 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1098 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1099 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1100 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1101 setbit(drvr->eventmask, BRCMF_E_JOIN);
1102 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1103 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1104 setbit(drvr->eventmask, BRCMF_E_LINK);
1105 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1106 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1107 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1108 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1109 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1110 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1112 /* enable dongle roaming event */
1114 drvr->pktfilter_count = 1;
1115 /* Setup filter to allow only unicast */
1116 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1118 /* Bus is ready, do any protocol initialization */
1119 ret = brcmf_proto_init(&drvr_priv->pub);
1126 static struct net_device_ops brcmf_netdev_ops_pri = {
1127 .ndo_open = brcmf_netdev_open,
1128 .ndo_stop = brcmf_netdev_stop,
1129 .ndo_get_stats = brcmf_netdev_get_stats,
1130 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1131 .ndo_start_xmit = brcmf_netdev_start_xmit,
1132 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1133 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
1136 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1138 struct brcmf_info *drvr_priv = drvr->info;
1139 struct net_device *ndev;
1140 u8 temp_addr[ETH_ALEN] = {
1141 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1143 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1145 ndev = drvr_priv->iflist[ifidx]->ndev;
1146 ndev->netdev_ops = &brcmf_netdev_ops_pri;
1149 * We have to use the primary MAC for virtual interfaces
1152 /* for virtual interfaces use the primary MAC */
1153 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1158 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1159 /* ACCESSPOINT INTERFACE CASE */
1160 temp_addr[0] |= 0X02; /* set bit 2 ,
1161 - Locally Administered address */
1164 ndev->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1165 ndev->ethtool_ops = &brcmf_ethtool_ops;
1167 drvr_priv->pub.rxsz = ndev->mtu + ndev->hard_header_len +
1168 drvr_priv->pub.hdrlen;
1170 memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
1172 if (register_netdev(ndev) != 0) {
1173 brcmf_dbg(ERROR, "couldn't register the net device\n");
1177 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
1182 ndev->netdev_ops = NULL;
1186 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1188 struct brcmf_info *drvr_priv;
1190 brcmf_dbg(TRACE, "Enter\n");
1193 drvr_priv = drvr->info;
1195 /* Stop the protocol module */
1196 brcmf_proto_stop(&drvr_priv->pub);
1198 /* Stop the bus module */
1199 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus);
1204 void brcmf_detach(struct brcmf_pub *drvr)
1206 struct brcmf_info *drvr_priv;
1208 brcmf_dbg(TRACE, "Enter\n");
1211 drvr_priv = drvr->info;
1213 struct brcmf_if *ifp;
1216 for (i = 1; i < BRCMF_MAX_IFS; i++)
1217 if (drvr_priv->iflist[i])
1218 brcmf_del_if(drvr_priv, i);
1220 ifp = drvr_priv->iflist[0];
1221 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
1223 brcmf_netdev_stop(ifp->ndev);
1225 unregister_netdev(ifp->ndev);
1228 cancel_work_sync(&drvr_priv->setmacaddr_work);
1229 cancel_work_sync(&drvr_priv->multicast_work);
1231 brcmf_bus_detach(drvr);
1234 brcmf_proto_detach(drvr);
1236 brcmf_cfg80211_detach(drvr->config);
1238 free_netdev(ifp->ndev);
1245 static void __exit brcmf_module_cleanup(void)
1247 brcmf_dbg(TRACE, "Enter\n");
1249 brcmf_bus_unregister();
1252 static int __init brcmf_module_init(void)
1256 brcmf_dbg(TRACE, "Enter\n");
1258 error = brcmf_bus_register();
1261 brcmf_dbg(ERROR, "brcmf_bus_register failed\n");
1270 module_init(brcmf_module_init);
1271 module_exit(brcmf_module_cleanup);
1273 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1275 struct brcmf_info *drvr_priv = drvr->info;
1278 mutex_lock(&drvr_priv->proto_block);
1284 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1286 struct brcmf_info *drvr_priv = drvr->info;
1289 mutex_unlock(&drvr_priv->proto_block);
1296 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1298 return atomic_read(&drvr_priv->pend_8021x_cnt);
1301 #define MAX_WAIT_FOR_8021X_TX 10
1303 int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1305 struct brcmf_info *drvr_priv = *(struct brcmf_info **)netdev_priv(ndev);
1306 int timeout = 10 * HZ / 1000;
1307 int ntimes = MAX_WAIT_FOR_8021X_TX;
1308 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1310 while (ntimes && pend) {
1312 set_current_state(TASK_INTERRUPTIBLE);
1313 schedule_timeout(timeout);
1314 set_current_state(TASK_RUNNING);
1317 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1323 int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
1327 mm_segment_t old_fs;
1330 /* change to KERNEL_DS address limit */
1334 /* open file to write */
1335 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1337 brcmf_dbg(ERROR, "open file error\n");
1342 /* Write buf to file */
1343 fp->f_op->write(fp, (char __user *)buf, size, &pos);
1346 /* free buf before return */
1348 /* close file before return */
1350 filp_close(fp, current->files);
1351 /* restore previous address limit */