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 <net/cfg80211.h>
36 #include <brcmu_utils.h>
37 #include <brcmu_wifi.h>
41 #include "dhd_proto.h"
43 #include "wl_cfg80211.h"
46 MODULE_AUTHOR("Broadcom Corporation");
47 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
48 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
49 MODULE_LICENSE("Dual BSD/GPL");
52 /* Interface control information */
54 struct brcmf_info *info; /* back pointer to brcmf_info */
55 /* OS/stack specifics */
56 struct net_device *net;
57 struct net_device_stats stats;
58 int idx; /* iface idx in dongle */
59 int state; /* interface state */
60 uint subunit; /* subunit */
61 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
62 bool attached; /* Delayed attachment when unset */
63 bool txflowcontrol; /* Per interface flow control indicator */
64 char name[IFNAMSIZ]; /* linux interface name */
67 /* Local private structure (extension of pub) */
71 /* OS/stack specifics */
72 struct brcmf_if *iflist[BRCMF_MAX_IFS];
74 struct mutex proto_block;
76 struct work_struct setmacaddr_work;
77 struct work_struct multicast_work;
78 u8 macvalue[ETH_ALEN];
79 atomic_t pend_8021x_cnt;
83 module_param(brcmf_msg_level, int, 0);
86 static int brcmf_net2idx(struct brcmf_info *drvr_priv, struct net_device *net)
90 while (i < BRCMF_MAX_IFS) {
91 if (drvr_priv->iflist[i] && (drvr_priv->iflist[i]->net == net))
99 int brcmf_ifname2idx(struct brcmf_info *drvr_priv, char *name)
101 int i = BRCMF_MAX_IFS;
103 if (name == NULL || *name == '\0')
107 if (drvr_priv->iflist[i]
108 && !strncmp(drvr_priv->iflist[i]->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]->net)
131 return drvr_priv->iflist[ifidx]->net->name;
136 static void _brcmf_set_multicast_list(struct work_struct *work)
138 struct net_device *dev;
139 struct netdev_hw_addr *ha;
144 struct brcmf_ioctl ioc;
149 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
152 dev = drvr_priv->iflist[0]->net;
153 cnt = netdev_mc_count(dev);
155 /* Determine initial value of allmulti flag */
156 allmulti = (dev->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);
163 brcmf_dbg(ERROR, "%s: out of memory for mcast_list, cnt %d\n",
164 brcmf_ifname(&drvr_priv->pub, 0), cnt);
168 strcpy(bufp, "mcast_list");
169 bufp += strlen("mcast_list") + 1;
171 cnt_le = cpu_to_le32(cnt);
172 memcpy(bufp, &cnt_le, sizeof(cnt));
173 bufp += sizeof(cnt_le);
175 netdev_for_each_mc_addr(ha, dev) {
178 memcpy(bufp, ha->addr, ETH_ALEN);
183 memset(&ioc, 0, sizeof(ioc));
184 ioc.cmd = BRCMF_C_SET_VAR;
189 ret = brcmf_proto_ioctl(&drvr_priv->pub, 0, &ioc, ioc.len);
191 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
192 brcmf_ifname(&drvr_priv->pub, 0), cnt);
193 allmulti = cnt ? true : allmulti;
198 /* Now send the allmulti setting. This is based on the setting in the
199 * net_device flags, but might be modified above to be turned on if we
200 * were trying to set some addresses and dongle rejected it...
203 buflen = sizeof("allmulti") + sizeof(allmulti);
204 buf = kmalloc(buflen, GFP_ATOMIC);
206 brcmf_dbg(ERROR, "%s: out of memory for allmulti\n",
207 brcmf_ifname(&drvr_priv->pub, 0));
210 allmulti_le = cpu_to_le32(allmulti);
213 ("allmulti", (void *)&allmulti_le,
214 sizeof(allmulti_le), buf, buflen)) {
215 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
216 brcmf_ifname(&drvr_priv->pub, 0),
217 (int)sizeof(allmulti), buflen);
222 memset(&ioc, 0, sizeof(ioc));
223 ioc.cmd = BRCMF_C_SET_VAR;
228 ret = brcmf_proto_ioctl(&drvr_priv->pub, 0, &ioc, ioc.len);
230 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
231 brcmf_ifname(&drvr_priv->pub, 0),
232 le32_to_cpu(allmulti_le));
237 /* Finally, pick up the PROMISC flag as well, like the NIC
240 allmulti = (dev->flags & IFF_PROMISC) ? true : false;
241 allmulti_le = cpu_to_le32(allmulti);
243 memset(&ioc, 0, sizeof(ioc));
244 ioc.cmd = BRCMF_C_SET_PROMISC;
245 ioc.buf = &allmulti_le;
246 ioc.len = sizeof(allmulti_le);
249 ret = brcmf_proto_ioctl(&drvr_priv->pub, 0, &ioc, ioc.len);
251 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
252 brcmf_ifname(&drvr_priv->pub, 0),
253 le32_to_cpu(allmulti_le));
258 _brcmf_set_mac_address(struct work_struct *work)
261 struct brcmf_ioctl ioc;
264 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
267 brcmf_dbg(TRACE, "enter\n");
268 if (!brcmu_mkiovar("cur_etheraddr", (char *)drvr_priv->macvalue,
269 ETH_ALEN, buf, 32)) {
270 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
271 brcmf_ifname(&drvr_priv->pub, 0));
274 memset(&ioc, 0, sizeof(ioc));
275 ioc.cmd = BRCMF_C_SET_VAR;
280 ret = brcmf_proto_ioctl(&drvr_priv->pub, 0, &ioc, ioc.len);
282 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
283 brcmf_ifname(&drvr_priv->pub, 0));
285 memcpy(drvr_priv->iflist[0]->net->dev_addr,
286 drvr_priv->macvalue, ETH_ALEN);
291 /* Virtual interfaces only ((ifp && ifp->info && ifp->idx == true) */
292 static void brcmf_op_if(struct brcmf_if *ifp)
294 struct brcmf_info *drvr_priv;
295 int ret = 0, err = 0;
297 drvr_priv = ifp->info;
299 brcmf_dbg(TRACE, "idx %d, state %d\n", ifp->idx, ifp->state);
301 switch (ifp->state) {
304 * Delete the existing interface before overwriting it
305 * in case we missed the BRCMF_E_IF_DEL event.
307 if (ifp->net != NULL) {
308 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
310 netif_stop_queue(ifp->net);
311 unregister_netdev(ifp->net);
312 free_netdev(ifp->net);
314 /* Allocate netdev, including space for private structure */
315 ifp->net = alloc_netdev(sizeof(drvr_priv), "wlan%d",
318 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
322 strcpy(ifp->net->name, ifp->name);
323 memcpy(netdev_priv(ifp->net), &drvr_priv,
325 err = brcmf_net_attach(&drvr_priv->pub, ifp->idx);
327 brcmf_dbg(ERROR, "brcmf_net_attach failed, err %d\n",
331 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
332 current->pid, ifp->net->name);
338 if (ifp->net != NULL) {
339 brcmf_dbg(TRACE, "got 'WLC_E_IF_DEL' state\n");
340 netif_stop_queue(ifp->net);
341 unregister_netdev(ifp->net);
342 ret = BRCMF_DEL_IF; /* Make sure the free_netdev()
347 brcmf_dbg(ERROR, "bad op %d\n", ifp->state);
353 free_netdev(ifp->net);
355 drvr_priv->iflist[ifp->idx] = NULL;
360 static int brcmf_netdev_set_mac_address(struct net_device *dev, void *addr)
362 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(dev);
363 struct sockaddr *sa = (struct sockaddr *)addr;
366 ifidx = brcmf_net2idx(drvr_priv, dev);
367 if (ifidx == BRCMF_BAD_IF)
370 memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
371 schedule_work(&drvr_priv->setmacaddr_work);
375 static void brcmf_netdev_set_multicast_list(struct net_device *dev)
377 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(dev);
380 ifidx = brcmf_net2idx(drvr_priv, dev);
381 if (ifidx == BRCMF_BAD_IF)
384 schedule_work(&drvr_priv->multicast_work);
387 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
389 struct brcmf_info *drvr_priv = drvr->info;
392 if (!drvr->up || (drvr->busstate == BRCMF_BUS_DOWN))
395 /* Update multicast statistic */
396 if (pktbuf->len >= ETH_ALEN) {
397 u8 *pktdata = (u8 *) (pktbuf->data);
398 struct ethhdr *eh = (struct ethhdr *)pktdata;
400 if (is_multicast_ether_addr(eh->h_dest))
401 drvr->tx_multicast++;
402 if (ntohs(eh->h_proto) == ETH_P_PAE)
403 atomic_inc(&drvr_priv->pend_8021x_cnt);
406 /* If the protocol uses a data header, apply it */
407 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
409 /* Use bus module to send data frame */
410 return brcmf_sdbrcm_bus_txdata(drvr->bus, pktbuf);
413 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *net)
416 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
419 brcmf_dbg(TRACE, "Enter\n");
422 if (!drvr_priv->pub.up || (drvr_priv->pub.busstate == BRCMF_BUS_DOWN)) {
423 brcmf_dbg(ERROR, "xmit rejected pub.up=%d busstate=%d\n",
424 drvr_priv->pub.up, drvr_priv->pub.busstate);
425 netif_stop_queue(net);
429 ifidx = brcmf_net2idx(drvr_priv, net);
430 if (ifidx == BRCMF_BAD_IF) {
431 brcmf_dbg(ERROR, "bad ifidx %d\n", ifidx);
432 netif_stop_queue(net);
436 /* Make sure there's enough room for any header */
437 if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
438 struct sk_buff *skb2;
440 brcmf_dbg(INFO, "%s: insufficient headroom\n",
441 brcmf_ifname(&drvr_priv->pub, ifidx));
442 drvr_priv->pub.tx_realloc++;
443 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
447 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
448 brcmf_ifname(&drvr_priv->pub, ifidx));
454 ret = brcmf_sendpkt(&drvr_priv->pub, ifidx, skb);
458 drvr_priv->pub.dstats.tx_dropped++;
460 drvr_priv->pub.tx_packets++;
462 /* Return ok: we always eat the packet */
466 void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
468 struct net_device *net;
469 struct brcmf_info *drvr_priv = drvr->info;
471 brcmf_dbg(TRACE, "Enter\n");
474 net = drvr_priv->iflist[ifidx]->net;
476 netif_stop_queue(net);
478 netif_wake_queue(net);
481 static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx,
482 void *pktdata, struct brcmf_event_msg *event,
487 bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
491 if (drvr_priv->iflist[*ifidx]->net)
492 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->net,
498 void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx, struct sk_buff *skb,
501 struct brcmf_info *drvr_priv = drvr->info;
505 struct sk_buff *pnext, *save_pktbuf;
507 struct brcmf_if *ifp;
508 struct brcmf_event_msg event;
510 brcmf_dbg(TRACE, "Enter\n");
514 for (i = 0; skb && i < numpkt; i++, skb = pnext) {
519 /* Get the protocol, maintain skb around eth_type_trans()
520 * The main reason for this hack is for the limitation of
521 * Linux 2.4 where 'eth_type_trans' uses the
522 * 'net->hard_header_len'
523 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
524 * coping of the packet coming from the network stack to add
525 * BDC, Hardware header etc, during network interface
527 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
529 * for BDC, Hardware header etc. and not just the ETH_HLEN
534 ifp = drvr_priv->iflist[ifidx];
536 ifp = drvr_priv->iflist[0];
539 skb->protocol = eth_type_trans(skb, skb->dev);
541 if (skb->pkt_type == PACKET_MULTICAST)
542 drvr_priv->pub.rx_multicast++;
547 /* Strip header, count, deliver upward */
548 skb_pull(skb, ETH_HLEN);
550 /* Process special event packets and then discard them */
551 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
552 brcmf_host_event(drvr_priv, &ifidx,
556 if (drvr_priv->iflist[ifidx] &&
557 !drvr_priv->iflist[ifidx]->state)
558 ifp = drvr_priv->iflist[ifidx];
561 ifp->net->last_rx = jiffies;
563 drvr->dstats.rx_bytes += skb->len;
564 drvr->rx_packets++; /* Local count */
569 /* If the receive is not processed inside an ISR,
570 * the softirqd must be woken explicitly to service
571 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
572 * by netif_rx_ni(), but in earlier kernels, we need
579 void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
582 struct brcmf_info *drvr_priv = drvr->info;
586 brcmf_proto_hdrpull(drvr, &ifidx, txp);
588 eh = (struct ethhdr *)(txp->data);
589 type = ntohs(eh->h_proto);
591 if (type == ETH_P_PAE)
592 atomic_dec(&drvr_priv->pend_8021x_cnt);
596 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *net)
598 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
599 struct brcmf_if *ifp;
602 brcmf_dbg(TRACE, "Enter\n");
604 ifidx = brcmf_net2idx(drvr_priv, net);
605 if (ifidx == BRCMF_BAD_IF)
608 ifp = drvr_priv->iflist[ifidx];
610 if (drvr_priv->pub.up)
611 /* Use the protocol to get dongle stats */
612 brcmf_proto_dstats(&drvr_priv->pub);
614 /* Copy dongle stats to net device stats */
615 ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
616 ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
617 ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
618 ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
619 ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
620 ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
621 ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
622 ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
623 ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
628 /* Retrieve current toe component enables, which are kept
629 as a bitmap in toe_ol iovar */
630 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
632 struct brcmf_ioctl ioc;
636 memset(&ioc, 0, sizeof(ioc));
638 ioc.cmd = BRCMF_C_GET_VAR;
640 ioc.len = (uint) sizeof(buf);
643 strcpy(buf, "toe_ol");
644 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.len);
646 /* Check for older dongle image that doesn't support toe_ol */
648 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
649 brcmf_ifname(&drvr_priv->pub, ifidx));
653 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
654 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
658 memcpy(toe_ol, buf, sizeof(u32));
662 /* Set current toe component enables in toe_ol iovar,
663 and set toe global enable iovar */
664 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
666 struct brcmf_ioctl ioc;
670 memset(&ioc, 0, sizeof(ioc));
672 ioc.cmd = BRCMF_C_SET_VAR;
674 ioc.len = (uint) sizeof(buf);
677 /* Set toe_ol as requested */
679 strcpy(buf, "toe_ol");
680 memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
682 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.len);
684 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
685 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
689 /* Enable toe globally only if any components are enabled. */
694 memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
696 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.len);
698 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
699 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
706 static void brcmf_ethtool_get_drvinfo(struct net_device *net,
707 struct ethtool_drvinfo *info)
709 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
711 sprintf(info->driver, KBUILD_MODNAME);
712 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
713 sprintf(info->fw_version, "%s", BCM4329_FW_NAME);
714 sprintf(info->bus_info, "%s",
715 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
718 static struct ethtool_ops brcmf_ethtool_ops = {
719 .get_drvinfo = brcmf_ethtool_get_drvinfo
722 static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
724 struct ethtool_drvinfo info;
725 char drvname[sizeof(info.driver)];
727 struct ethtool_value edata;
728 u32 toe_cmpnt, csum_dir;
731 brcmf_dbg(TRACE, "Enter\n");
733 /* all ethtool calls start with a cmd word */
734 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
738 case ETHTOOL_GDRVINFO:
739 /* Copy out any request driver name */
740 if (copy_from_user(&info, uaddr, sizeof(info)))
742 strncpy(drvname, info.driver, sizeof(info.driver));
743 drvname[sizeof(info.driver) - 1] = '\0';
745 /* clear struct for return */
746 memset(&info, 0, sizeof(info));
749 /* if requested, identify ourselves */
750 if (strcmp(drvname, "?dhd") == 0) {
751 sprintf(info.driver, "dhd");
752 strcpy(info.version, BRCMF_VERSION_STR);
755 /* otherwise, require dongle to be up */
756 else if (!drvr_priv->pub.up) {
757 brcmf_dbg(ERROR, "dongle is not up\n");
761 /* finally, report dongle driver type */
762 else if (drvr_priv->pub.iswl)
763 sprintf(info.driver, "wl");
765 sprintf(info.driver, "xx");
767 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
768 if (copy_to_user(uaddr, &info, sizeof(info)))
770 brcmf_dbg(CTL, "given %*s, returning %s\n",
771 (int)sizeof(drvname), drvname, info.driver);
774 /* Get toe offload components from dongle */
775 case ETHTOOL_GRXCSUM:
776 case ETHTOOL_GTXCSUM:
777 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
782 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
785 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
787 if (copy_to_user(uaddr, &edata, sizeof(edata)))
791 /* Set toe offload components in dongle */
792 case ETHTOOL_SRXCSUM:
793 case ETHTOOL_STXCSUM:
794 if (copy_from_user(&edata, uaddr, sizeof(edata)))
797 /* Read the current settings, update and write back */
798 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
803 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
806 toe_cmpnt |= csum_dir;
808 toe_cmpnt &= ~csum_dir;
810 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
814 /* If setting TX checksum mode, tell Linux the new mode */
815 if (cmd == ETHTOOL_STXCSUM) {
817 drvr_priv->iflist[0]->net->features |=
820 drvr_priv->iflist[0]->net->features &=
833 static int brcmf_netdev_ioctl_entry(struct net_device *net, struct ifreq *ifr,
836 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
839 ifidx = brcmf_net2idx(drvr_priv, net);
840 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifidx, cmd);
842 if (ifidx == BRCMF_BAD_IF)
845 if (cmd == SIOCETHTOOL)
846 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
851 /* called only from within this driver */
852 int brcmf_netdev_ioctl_priv(struct net_device *net, struct brcmf_ioctl *ioc)
857 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
860 ifidx = brcmf_net2idx(drvr_priv, net);
862 if (ioc->buf != NULL)
863 buflen = min_t(uint, ioc->len, BRCMF_IOCTL_MAXLEN);
865 /* send to dongle (must be up, and wl) */
866 if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
867 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
872 if (!drvr_priv->pub.iswl) {
878 * Intercept BRCMF_C_SET_KEY IOCTL - serialize M4 send and
879 * set key IOCTL to prevent M4 encryption.
881 is_set_key_cmd = ((ioc->cmd == BRCMF_C_SET_KEY) ||
882 ((ioc->cmd == BRCMF_C_SET_VAR) &&
883 !(strncmp("wsec_key", ioc->buf, 9))) ||
884 ((ioc->cmd == BRCMF_C_SET_VAR) &&
885 !(strncmp("bsscfg:wsec_key", ioc->buf, 15))));
887 brcmf_netdev_wait_pend8021x(net);
889 bcmerror = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, ioc, buflen);
898 static int brcmf_netdev_stop(struct net_device *net)
900 struct brcmf_pub *drvr = *(struct brcmf_pub **) netdev_priv(net);
902 brcmf_dbg(TRACE, "Enter\n");
903 brcmf_cfg80211_down(drvr->config);
907 /* Set state and stop OS transmissions */
909 netif_stop_queue(net);
914 static int brcmf_netdev_open(struct net_device *net)
916 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
918 int ifidx = brcmf_net2idx(drvr_priv, net);
921 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
923 if (ifidx == 0) { /* do it only for primary eth0 */
925 /* try to bring up bus */
926 ret = brcmf_bus_start(&drvr_priv->pub);
928 brcmf_dbg(ERROR, "failed with code %d\n", ret);
931 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
933 memcpy(net->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
935 /* Get current TOE mode from dongle */
936 if (brcmf_toe_get(drvr_priv, ifidx, &toe_ol) >= 0
937 && (toe_ol & TOE_TX_CSUM_OL) != 0)
938 drvr_priv->iflist[ifidx]->net->features |=
941 drvr_priv->iflist[ifidx]->net->features &=
944 /* Allow transmit calls */
945 netif_start_queue(net);
946 drvr_priv->pub.up = 1;
947 if (brcmf_cfg80211_up(drvr_priv->pub.config)) {
948 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
956 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, struct net_device *net,
957 char *name, u8 *mac_addr, u32 flags, u8 bssidx)
959 struct brcmf_if *ifp;
961 brcmf_dbg(TRACE, "idx %d, handle->%p\n", ifidx, net);
963 ifp = drvr_priv->iflist[ifidx];
965 ifp = kmalloc(sizeof(struct brcmf_if), GFP_ATOMIC);
967 brcmf_dbg(ERROR, "OOM - struct brcmf_if\n");
972 memset(ifp, 0, sizeof(struct brcmf_if));
973 ifp->info = drvr_priv;
974 drvr_priv->iflist[ifidx] = ifp;
975 strlcpy(ifp->name, name, IFNAMSIZ);
976 if (mac_addr != NULL)
977 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
980 ifp->state = BRCMF_E_IF_ADD;
989 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
991 struct brcmf_if *ifp;
993 brcmf_dbg(TRACE, "idx %d\n", ifidx);
995 ifp = drvr_priv->iflist[ifidx];
997 brcmf_dbg(ERROR, "Null interface\n");
1001 ifp->state = BRCMF_E_IF_DEL;
1006 struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
1008 struct brcmf_info *drvr_priv = NULL;
1009 struct net_device *net;
1011 brcmf_dbg(TRACE, "Enter\n");
1013 /* Allocate netdev, including space for private structure */
1014 net = alloc_netdev(sizeof(drvr_priv), "wlan%d", ether_setup);
1016 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
1020 /* Allocate primary brcmf_info */
1021 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
1023 brcmf_dbg(ERROR, "OOM - alloc brcmf_info\n");
1028 * Save the brcmf_info into the priv
1030 memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1032 if (brcmf_add_if(drvr_priv, 0, net, net->name, NULL, 0, 0) ==
1036 net->netdev_ops = NULL;
1037 mutex_init(&drvr_priv->proto_block);
1039 /* Link to info module */
1040 drvr_priv->pub.info = drvr_priv;
1042 /* Link to bus module */
1043 drvr_priv->pub.bus = bus;
1044 drvr_priv->pub.hdrlen = bus_hdrlen;
1046 /* Attach and link in the protocol */
1047 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
1048 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
1052 /* Attach and link in the cfg80211 */
1053 drvr_priv->pub.config =
1054 brcmf_cfg80211_attach(net,
1055 brcmf_bus_get_device(bus),
1057 if (drvr_priv->pub.config == NULL) {
1058 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1062 INIT_WORK(&drvr_priv->setmacaddr_work, _brcmf_set_mac_address);
1063 INIT_WORK(&drvr_priv->multicast_work, _brcmf_set_multicast_list);
1066 * Save the brcmf_info into the priv
1068 memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1070 return &drvr_priv->pub;
1076 brcmf_detach(&drvr_priv->pub);
1081 int brcmf_bus_start(struct brcmf_pub *drvr)
1084 struct brcmf_info *drvr_priv = drvr->info;
1085 /* Room for "event_msgs" + '\0' + bitvec */
1086 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1088 brcmf_dbg(TRACE, "\n");
1090 /* Bring up the bus */
1091 ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub, true);
1093 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
1097 /* If bus is not ready, can't come up */
1098 if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
1099 brcmf_dbg(ERROR, "failed bus is not ready\n");
1103 brcmu_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1104 iovbuf, sizeof(iovbuf));
1105 brcmf_proto_cdc_query_ioctl(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1107 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1109 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1110 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1111 setbit(drvr->eventmask, BRCMF_E_AUTH);
1112 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1113 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1114 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1115 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1116 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1117 setbit(drvr->eventmask, BRCMF_E_JOIN);
1118 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1119 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1120 setbit(drvr->eventmask, BRCMF_E_LINK);
1121 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1122 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1123 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1124 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1125 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1126 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1128 /* enable dongle roaming event */
1130 drvr->pktfilter_count = 1;
1131 /* Setup filter to allow only unicast */
1132 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1134 /* Bus is ready, do any protocol initialization */
1135 ret = brcmf_proto_init(&drvr_priv->pub);
1142 static struct net_device_ops brcmf_netdev_ops_pri = {
1143 .ndo_open = brcmf_netdev_open,
1144 .ndo_stop = brcmf_netdev_stop,
1145 .ndo_get_stats = brcmf_netdev_get_stats,
1146 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1147 .ndo_start_xmit = brcmf_netdev_start_xmit,
1148 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1149 .ndo_set_multicast_list = brcmf_netdev_set_multicast_list
1152 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1154 struct brcmf_info *drvr_priv = drvr->info;
1155 struct net_device *net;
1156 u8 temp_addr[ETH_ALEN] = {
1157 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1159 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1161 net = drvr_priv->iflist[ifidx]->net;
1162 net->netdev_ops = &brcmf_netdev_ops_pri;
1165 * We have to use the primary MAC for virtual interfaces
1168 /* for virtual interfaces use the primary MAC */
1169 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1174 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1175 /* ACCESSPOINT INTERFACE CASE */
1176 temp_addr[0] |= 0X02; /* set bit 2 ,
1177 - Locally Administered address */
1180 net->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1181 net->ethtool_ops = &brcmf_ethtool_ops;
1183 drvr_priv->pub.rxsz = net->mtu + net->hard_header_len +
1184 drvr_priv->pub.hdrlen;
1186 memcpy(net->dev_addr, temp_addr, ETH_ALEN);
1188 if (register_netdev(net) != 0) {
1189 brcmf_dbg(ERROR, "couldn't register the net device\n");
1193 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", net->name);
1198 net->netdev_ops = NULL;
1202 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1204 struct brcmf_info *drvr_priv;
1206 brcmf_dbg(TRACE, "Enter\n");
1209 drvr_priv = drvr->info;
1211 /* Stop the protocol module */
1212 brcmf_proto_stop(&drvr_priv->pub);
1214 /* Stop the bus module */
1215 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus, true);
1220 void brcmf_detach(struct brcmf_pub *drvr)
1222 struct brcmf_info *drvr_priv;
1224 brcmf_dbg(TRACE, "Enter\n");
1227 drvr_priv = drvr->info;
1229 struct brcmf_if *ifp;
1232 for (i = 1; i < BRCMF_MAX_IFS; i++)
1233 if (drvr_priv->iflist[i])
1234 brcmf_del_if(drvr_priv, i);
1236 ifp = drvr_priv->iflist[0];
1237 if (ifp->net->netdev_ops == &brcmf_netdev_ops_pri) {
1238 brcmf_netdev_stop(ifp->net);
1239 unregister_netdev(ifp->net);
1242 cancel_work_sync(&drvr_priv->setmacaddr_work);
1243 cancel_work_sync(&drvr_priv->multicast_work);
1245 brcmf_bus_detach(drvr);
1248 brcmf_proto_detach(drvr);
1250 brcmf_cfg80211_detach(drvr->config);
1252 free_netdev(ifp->net);
1259 static void __exit brcmf_module_cleanup(void)
1261 brcmf_dbg(TRACE, "Enter\n");
1263 brcmf_bus_unregister();
1266 static int __init brcmf_module_init(void)
1270 brcmf_dbg(TRACE, "Enter\n");
1272 error = brcmf_bus_register();
1275 brcmf_dbg(ERROR, "brcmf_bus_register failed\n");
1284 module_init(brcmf_module_init);
1285 module_exit(brcmf_module_cleanup);
1287 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1289 struct brcmf_info *drvr_priv = drvr->info;
1292 mutex_lock(&drvr_priv->proto_block);
1298 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1300 struct brcmf_info *drvr_priv = drvr->info;
1303 mutex_unlock(&drvr_priv->proto_block);
1310 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1312 return atomic_read(&drvr_priv->pend_8021x_cnt);
1315 #define MAX_WAIT_FOR_8021X_TX 10
1317 int brcmf_netdev_wait_pend8021x(struct net_device *dev)
1319 struct brcmf_info *drvr_priv = *(struct brcmf_info **)netdev_priv(dev);
1320 int timeout = 10 * HZ / 1000;
1321 int ntimes = MAX_WAIT_FOR_8021X_TX;
1322 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1324 while (ntimes && pend) {
1326 set_current_state(TASK_INTERRUPTIBLE);
1327 schedule_timeout(timeout);
1328 set_current_state(TASK_RUNNING);
1331 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1337 int brcmf_write_to_file(struct brcmf_pub *drvr, u8 *buf, int size)
1341 mm_segment_t old_fs;
1344 /* change to KERNEL_DS address limit */
1348 /* open file to write */
1349 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1351 brcmf_dbg(ERROR, "open file error\n");
1356 /* Write buf to file */
1357 fp->f_op->write(fp, buf, size, &pos);
1360 /* free buf before return */
1362 /* close file before return */
1364 filp_close(fp, current->files);
1365 /* restore previous address limit */