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 /* Thread to issue ioctl for multicast */
77 struct task_struct *sysioc_tsk;
78 wait_queue_head_t sysioc_waitq;
81 u8 macvalue[ETH_ALEN];
82 atomic_t pend_8021x_cnt;
86 module_param(brcmf_msg_level, int, 0);
88 /* Spawn a thread for system ioctls (set mac, set mcast) */
89 uint brcmf_sysioc = true;
90 module_param(brcmf_sysioc, uint, 0);
92 /* ARP offload agent mode : Enable ARP Host Auto-Reply
93 and ARP Peer Auto-Reply */
94 uint brcmf_arp_mode = 0xb;
95 module_param(brcmf_arp_mode, uint, 0);
97 /* ARP offload enable */
98 uint brcmf_arp_enable = true;
99 module_param(brcmf_arp_enable, uint, 0);
101 /* Global Pkt filter enable control */
102 uint brcmf_pkt_filter_enable = true;
103 module_param(brcmf_pkt_filter_enable, uint, 0);
105 /* Pkt filter init setup */
106 uint brcmf_pkt_filter_init;
107 module_param(brcmf_pkt_filter_init, uint, 0);
109 /* Pkt filter mode control */
110 uint brcmf_master_mode = true;
111 module_param(brcmf_master_mode, uint, 0);
113 /* Contorl fw roaming */
116 /* Control radio state */
117 uint brcmf_radio_up = 1;
119 /* Network inteface name */
120 char iface_name[IFNAMSIZ] = "wlan";
121 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
123 static int brcmf_net2idx(struct brcmf_info *drvr_priv, struct net_device *net)
127 while (i < BRCMF_MAX_IFS) {
128 if (drvr_priv->iflist[i] && (drvr_priv->iflist[i]->net == net))
136 int brcmf_ifname2idx(struct brcmf_info *drvr_priv, char *name)
138 int i = BRCMF_MAX_IFS;
140 if (name == NULL || *name == '\0')
144 if (drvr_priv->iflist[i]
145 && !strncmp(drvr_priv->iflist[i]->name, name, IFNAMSIZ))
148 brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
150 return i; /* default - the primary interface */
153 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
155 struct brcmf_info *drvr_priv = drvr->info;
157 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
158 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
162 if (drvr_priv->iflist[ifidx] == NULL) {
163 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
167 if (drvr_priv->iflist[ifidx]->net)
168 return drvr_priv->iflist[ifidx]->net->name;
173 static void _brcmf_set_multicast_list(struct brcmf_info *drvr_priv, int ifidx)
175 struct net_device *dev;
176 struct netdev_hw_addr *ha;
179 struct brcmf_ioctl ioc;
184 dev = drvr_priv->iflist[ifidx]->net;
185 cnt = netdev_mc_count(dev);
187 /* Determine initial value of allmulti flag */
188 allmulti = (dev->flags & IFF_ALLMULTI) ? true : false;
190 /* Send down the multicast list first. */
192 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
193 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
195 brcmf_dbg(ERROR, "%s: out of memory for mcast_list, cnt %d\n",
196 brcmf_ifname(&drvr_priv->pub, ifidx), cnt);
200 strcpy(bufp, "mcast_list");
201 bufp += strlen("mcast_list") + 1;
203 cnt = cpu_to_le32(cnt);
204 memcpy(bufp, &cnt, sizeof(cnt));
207 netdev_for_each_mc_addr(ha, dev) {
210 memcpy(bufp, ha->addr, ETH_ALEN);
215 memset(&ioc, 0, sizeof(ioc));
216 ioc.cmd = BRCMF_C_SET_VAR;
221 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
223 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
224 brcmf_ifname(&drvr_priv->pub, ifidx), cnt);
225 allmulti = cnt ? true : allmulti;
230 /* Now send the allmulti setting. This is based on the setting in the
231 * net_device flags, but might be modified above to be turned on if we
232 * were trying to set some addresses and dongle rejected it...
235 buflen = sizeof("allmulti") + sizeof(allmulti);
236 buf = kmalloc(buflen, GFP_ATOMIC);
238 brcmf_dbg(ERROR, "%s: out of memory for allmulti\n",
239 brcmf_ifname(&drvr_priv->pub, ifidx));
242 allmulti = cpu_to_le32(allmulti);
245 ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
246 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
247 brcmf_ifname(&drvr_priv->pub, ifidx),
248 (int)sizeof(allmulti), buflen);
253 memset(&ioc, 0, sizeof(ioc));
254 ioc.cmd = BRCMF_C_SET_VAR;
259 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
261 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
262 brcmf_ifname(&drvr_priv->pub, ifidx),
263 le32_to_cpu(allmulti));
268 /* Finally, pick up the PROMISC flag as well, like the NIC
271 allmulti = (dev->flags & IFF_PROMISC) ? true : false;
272 allmulti = cpu_to_le32(allmulti);
274 memset(&ioc, 0, sizeof(ioc));
275 ioc.cmd = BRCMF_C_SET_PROMISC;
277 ioc.len = sizeof(allmulti);
280 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
282 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
283 brcmf_ifname(&drvr_priv->pub, ifidx),
284 le32_to_cpu(allmulti));
289 _brcmf_set_mac_address(struct brcmf_info *drvr_priv, int ifidx, u8 *addr)
292 struct brcmf_ioctl ioc;
295 brcmf_dbg(TRACE, "enter\n");
296 if (!brcmu_mkiovar("cur_etheraddr", (char *)addr, ETH_ALEN, buf, 32)) {
297 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
298 brcmf_ifname(&drvr_priv->pub, ifidx));
301 memset(&ioc, 0, sizeof(ioc));
302 ioc.cmd = BRCMF_C_SET_VAR;
307 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
309 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
310 brcmf_ifname(&drvr_priv->pub, ifidx));
312 memcpy(drvr_priv->iflist[ifidx]->net->dev_addr, addr, ETH_ALEN);
318 static struct net_device *ap_net_dev;
321 /* Virtual interfaces only ((ifp && ifp->info && ifp->idx == true) */
322 static void brcmf_op_if(struct brcmf_if *ifp)
324 struct brcmf_info *drvr_priv;
325 int ret = 0, err = 0;
327 drvr_priv = ifp->info;
329 brcmf_dbg(TRACE, "idx %d, state %d\n", ifp->idx, ifp->state);
331 switch (ifp->state) {
334 * Delete the existing interface before overwriting it
335 * in case we missed the BRCMF_E_IF_DEL event.
337 if (ifp->net != NULL) {
338 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
340 netif_stop_queue(ifp->net);
341 unregister_netdev(ifp->net);
342 free_netdev(ifp->net);
344 /* Allocate etherdev, including space for private structure */
345 ifp->net = alloc_etherdev(sizeof(drvr_priv));
347 brcmf_dbg(ERROR, "OOM - alloc_etherdev\n");
351 strcpy(ifp->net->name, ifp->name);
352 memcpy(netdev_priv(ifp->net), &drvr_priv,
354 err = brcmf_net_attach(&drvr_priv->pub, ifp->idx);
356 brcmf_dbg(ERROR, "brcmf_net_attach failed, err %d\n",
361 /* semaphore that the soft AP CODE
363 struct semaphore ap_eth_sema;
365 /* save ptr to wl0.1 netdev for use
367 ap_net_dev = ifp->net;
368 /* signal to the SOFTAP 'sleeper' thread,
372 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
373 current->pid, ifp->net->name);
379 if (ifp->net != NULL) {
380 brcmf_dbg(TRACE, "got 'WLC_E_IF_DEL' state\n");
381 netif_stop_queue(ifp->net);
382 unregister_netdev(ifp->net);
383 ret = BRCMF_DEL_IF; /* Make sure the free_netdev()
388 brcmf_dbg(ERROR, "bad op %d\n", ifp->state);
394 free_netdev(ifp->net);
396 drvr_priv->iflist[ifp->idx] = NULL;
399 if (ifp->net == ap_net_dev)
400 ap_net_dev = NULL; /* NULL SOFTAP global
406 static int _brcmf_sysioc_thread(void *data)
408 struct brcmf_info *drvr_priv = (struct brcmf_info *) data;
413 DECLARE_WAITQUEUE(wait, current);
414 allow_signal(SIGTERM);
416 add_wait_queue(&drvr_priv->sysioc_waitq, &wait);
418 prepare_to_wait(&drvr_priv->sysioc_waitq, &wait,
424 if (kthread_should_stop())
427 for (i = 0; i < BRCMF_MAX_IFS; i++) {
428 struct brcmf_if *ifentry = drvr_priv->iflist[i];
431 in_ap = (ap_net_dev != NULL);
434 brcmf_op_if(ifentry);
436 if (drvr_priv->iflist[i] == NULL) {
437 brcmf_dbg(TRACE, "interface %d removed!\n",
442 if (in_ap && drvr_priv->set_macaddress) {
443 brcmf_dbg(TRACE, "attempt to set MAC for %s in AP Mode, blocked.\n",
445 drvr_priv->set_macaddress = false;
449 if (in_ap && drvr_priv->set_multicast) {
450 brcmf_dbg(TRACE, "attempt to set MULTICAST list for %s in AP Mode, blocked.\n",
452 drvr_priv->set_multicast = false;
456 if (drvr_priv->set_multicast) {
457 drvr_priv->set_multicast = false;
458 _brcmf_set_multicast_list(drvr_priv, i);
460 if (drvr_priv->set_macaddress) {
461 drvr_priv->set_macaddress = false;
462 _brcmf_set_mac_address(drvr_priv, i,
463 drvr_priv->macvalue);
468 finish_wait(&drvr_priv->sysioc_waitq, &wait);
472 static int brcmf_netdev_set_mac_address(struct net_device *dev, void *addr)
476 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(dev);
477 struct sockaddr *sa = (struct sockaddr *)addr;
480 ifidx = brcmf_net2idx(drvr_priv, dev);
481 if (ifidx == BRCMF_BAD_IF)
484 memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
485 drvr_priv->set_macaddress = true;
486 wake_up(&drvr_priv->sysioc_waitq);
490 static void brcmf_netdev_set_multicast_list(struct net_device *dev)
492 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(dev);
495 ifidx = brcmf_net2idx(drvr_priv, dev);
496 if (ifidx == BRCMF_BAD_IF)
499 drvr_priv->set_multicast = true;
500 wake_up(&drvr_priv->sysioc_waitq);
503 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
505 struct brcmf_info *drvr_priv = drvr->info;
508 if (!drvr->up || (drvr->busstate == BRCMF_BUS_DOWN))
511 /* Update multicast statistic */
512 if (pktbuf->len >= ETH_ALEN) {
513 u8 *pktdata = (u8 *) (pktbuf->data);
514 struct ethhdr *eh = (struct ethhdr *)pktdata;
516 if (is_multicast_ether_addr(eh->h_dest))
517 drvr->tx_multicast++;
518 if (ntohs(eh->h_proto) == ETH_P_PAE)
519 atomic_inc(&drvr_priv->pend_8021x_cnt);
522 /* If the protocol uses a data header, apply it */
523 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
525 /* Use bus module to send data frame */
526 return brcmf_sdbrcm_bus_txdata(drvr->bus, pktbuf);
529 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *net)
532 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
535 brcmf_dbg(TRACE, "Enter\n");
538 if (!drvr_priv->pub.up || (drvr_priv->pub.busstate == BRCMF_BUS_DOWN)) {
539 brcmf_dbg(ERROR, "xmit rejected pub.up=%d busstate=%d\n",
540 drvr_priv->pub.up, drvr_priv->pub.busstate);
541 netif_stop_queue(net);
545 ifidx = brcmf_net2idx(drvr_priv, net);
546 if (ifidx == BRCMF_BAD_IF) {
547 brcmf_dbg(ERROR, "bad ifidx %d\n", ifidx);
548 netif_stop_queue(net);
552 /* Make sure there's enough room for any header */
553 if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
554 struct sk_buff *skb2;
556 brcmf_dbg(INFO, "%s: insufficient headroom\n",
557 brcmf_ifname(&drvr_priv->pub, ifidx));
558 drvr_priv->pub.tx_realloc++;
559 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
563 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
564 brcmf_ifname(&drvr_priv->pub, ifidx));
570 ret = brcmf_sendpkt(&drvr_priv->pub, ifidx, skb);
574 drvr_priv->pub.dstats.tx_dropped++;
576 drvr_priv->pub.tx_packets++;
578 /* Return ok: we always eat the packet */
582 void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
584 struct net_device *net;
585 struct brcmf_info *drvr_priv = drvr->info;
587 brcmf_dbg(TRACE, "Enter\n");
590 net = drvr_priv->iflist[ifidx]->net;
592 netif_stop_queue(net);
594 netif_wake_queue(net);
597 static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx,
598 void *pktdata, struct brcmf_event_msg *event,
603 bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
607 if (drvr_priv->iflist[*ifidx]->net)
608 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->net,
614 void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx, struct sk_buff *skb,
617 struct brcmf_info *drvr_priv = drvr->info;
621 struct sk_buff *pnext, *save_pktbuf;
623 struct brcmf_if *ifp;
624 struct brcmf_event_msg event;
626 brcmf_dbg(TRACE, "Enter\n");
630 for (i = 0; skb && i < numpkt; i++, skb = pnext) {
635 /* Get the protocol, maintain skb around eth_type_trans()
636 * The main reason for this hack is for the limitation of
637 * Linux 2.4 where 'eth_type_trans' uses the
638 * 'net->hard_header_len'
639 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
640 * coping of the packet coming from the network stack to add
641 * BDC, Hardware header etc, during network interface
643 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
645 * for BDC, Hardware header etc. and not just the ETH_HLEN
650 ifp = drvr_priv->iflist[ifidx];
652 ifp = drvr_priv->iflist[0];
655 skb->protocol = eth_type_trans(skb, skb->dev);
657 if (skb->pkt_type == PACKET_MULTICAST)
658 drvr_priv->pub.rx_multicast++;
663 /* Strip header, count, deliver upward */
664 skb_pull(skb, ETH_HLEN);
666 /* Process special event packets and then discard them */
667 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
668 brcmf_host_event(drvr_priv, &ifidx,
672 if (drvr_priv->iflist[ifidx] &&
673 !drvr_priv->iflist[ifidx]->state)
674 ifp = drvr_priv->iflist[ifidx];
677 ifp->net->last_rx = jiffies;
679 drvr->dstats.rx_bytes += skb->len;
680 drvr->rx_packets++; /* Local count */
685 /* If the receive is not processed inside an ISR,
686 * the softirqd must be woken explicitly to service
687 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
688 * by netif_rx_ni(), but in earlier kernels, we need
695 void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
698 struct brcmf_info *drvr_priv = drvr->info;
702 brcmf_proto_hdrpull(drvr, &ifidx, txp);
704 eh = (struct ethhdr *)(txp->data);
705 type = ntohs(eh->h_proto);
707 if (type == ETH_P_PAE)
708 atomic_dec(&drvr_priv->pend_8021x_cnt);
712 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *net)
714 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
715 struct brcmf_if *ifp;
718 brcmf_dbg(TRACE, "Enter\n");
720 ifidx = brcmf_net2idx(drvr_priv, net);
721 if (ifidx == BRCMF_BAD_IF)
724 ifp = drvr_priv->iflist[ifidx];
726 if (drvr_priv->pub.up)
727 /* Use the protocol to get dongle stats */
728 brcmf_proto_dstats(&drvr_priv->pub);
730 /* Copy dongle stats to net device stats */
731 ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
732 ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
733 ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
734 ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
735 ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
736 ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
737 ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
738 ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
739 ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
744 /* Retrieve current toe component enables, which are kept
745 as a bitmap in toe_ol iovar */
746 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
748 struct brcmf_ioctl ioc;
752 memset(&ioc, 0, sizeof(ioc));
754 ioc.cmd = BRCMF_C_GET_VAR;
756 ioc.len = (uint) sizeof(buf);
759 strcpy(buf, "toe_ol");
760 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
762 /* Check for older dongle image that doesn't support toe_ol */
764 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
765 brcmf_ifname(&drvr_priv->pub, ifidx));
769 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
770 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
774 memcpy(toe_ol, buf, sizeof(u32));
778 /* Set current toe component enables in toe_ol iovar,
779 and set toe global enable iovar */
780 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
782 struct brcmf_ioctl ioc;
786 memset(&ioc, 0, sizeof(ioc));
788 ioc.cmd = BRCMF_C_SET_VAR;
790 ioc.len = (uint) sizeof(buf);
793 /* Set toe_ol as requested */
795 strcpy(buf, "toe_ol");
796 memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
798 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
800 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
801 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
805 /* Enable toe globally only if any components are enabled. */
810 memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
812 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
814 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
815 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
822 static void brcmf_ethtool_get_drvinfo(struct net_device *net,
823 struct ethtool_drvinfo *info)
825 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
827 sprintf(info->driver, KBUILD_MODNAME);
828 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
829 sprintf(info->fw_version, "%s", BCM4329_FW_NAME);
830 sprintf(info->bus_info, "%s",
831 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
834 static struct ethtool_ops brcmf_ethtool_ops = {
835 .get_drvinfo = brcmf_ethtool_get_drvinfo
838 static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
840 struct ethtool_drvinfo info;
841 char drvname[sizeof(info.driver)];
843 struct ethtool_value edata;
844 u32 toe_cmpnt, csum_dir;
847 brcmf_dbg(TRACE, "Enter\n");
849 /* all ethtool calls start with a cmd word */
850 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
854 case ETHTOOL_GDRVINFO:
855 /* Copy out any request driver name */
856 if (copy_from_user(&info, uaddr, sizeof(info)))
858 strncpy(drvname, info.driver, sizeof(info.driver));
859 drvname[sizeof(info.driver) - 1] = '\0';
861 /* clear struct for return */
862 memset(&info, 0, sizeof(info));
865 /* if requested, identify ourselves */
866 if (strcmp(drvname, "?dhd") == 0) {
867 sprintf(info.driver, "dhd");
868 strcpy(info.version, BRCMF_VERSION_STR);
871 /* otherwise, require dongle to be up */
872 else if (!drvr_priv->pub.up) {
873 brcmf_dbg(ERROR, "dongle is not up\n");
877 /* finally, report dongle driver type */
878 else if (drvr_priv->pub.iswl)
879 sprintf(info.driver, "wl");
881 sprintf(info.driver, "xx");
883 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
884 if (copy_to_user(uaddr, &info, sizeof(info)))
886 brcmf_dbg(CTL, "given %*s, returning %s\n",
887 (int)sizeof(drvname), drvname, info.driver);
890 /* Get toe offload components from dongle */
891 case ETHTOOL_GRXCSUM:
892 case ETHTOOL_GTXCSUM:
893 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
898 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
901 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
903 if (copy_to_user(uaddr, &edata, sizeof(edata)))
907 /* Set toe offload components in dongle */
908 case ETHTOOL_SRXCSUM:
909 case ETHTOOL_STXCSUM:
910 if (copy_from_user(&edata, uaddr, sizeof(edata)))
913 /* Read the current settings, update and write back */
914 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
919 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
922 toe_cmpnt |= csum_dir;
924 toe_cmpnt &= ~csum_dir;
926 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
930 /* If setting TX checksum mode, tell Linux the new mode */
931 if (cmd == ETHTOOL_STXCSUM) {
933 drvr_priv->iflist[0]->net->features |=
936 drvr_priv->iflist[0]->net->features &=
949 static int brcmf_netdev_ioctl_entry(struct net_device *net, struct ifreq *ifr,
952 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
955 ifidx = brcmf_net2idx(drvr_priv, net);
956 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifidx, cmd);
958 if (ifidx == BRCMF_BAD_IF)
961 if (cmd == SIOCETHTOOL)
962 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
967 /* called only from within this driver */
968 int brcmf_netdev_ioctl_priv(struct net_device *net, struct brcmf_ioctl *ioc)
973 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
976 ifidx = brcmf_net2idx(drvr_priv, net);
978 if (ioc->buf != NULL)
979 buflen = min_t(uint, ioc->len, BRCMF_IOCTL_MAXLEN);
981 /* send to dongle (must be up, and wl) */
982 if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
983 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
988 if (!drvr_priv->pub.iswl) {
994 * Intercept BRCMF_C_SET_KEY IOCTL - serialize M4 send and
995 * set key IOCTL to prevent M4 encryption.
997 is_set_key_cmd = ((ioc->cmd == BRCMF_C_SET_KEY) ||
998 ((ioc->cmd == BRCMF_C_SET_VAR) &&
999 !(strncmp("wsec_key", ioc->buf, 9))) ||
1000 ((ioc->cmd == BRCMF_C_SET_VAR) &&
1001 !(strncmp("bsscfg:wsec_key", ioc->buf, 15))));
1003 brcmf_netdev_wait_pend8021x(net);
1005 bcmerror = brcmf_proto_ioctl(&drvr_priv->pub, ifidx,
1006 ioc, ioc->buf, buflen);
1015 static int brcmf_netdev_stop(struct net_device *net)
1017 struct brcmf_pub *drvr = *(struct brcmf_pub **) netdev_priv(net);
1019 brcmf_dbg(TRACE, "Enter\n");
1020 brcmf_cfg80211_down(drvr->config);
1024 /* Set state and stop OS transmissions */
1026 netif_stop_queue(net);
1031 static int brcmf_netdev_open(struct net_device *net)
1033 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
1035 int ifidx = brcmf_net2idx(drvr_priv, net);
1038 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1040 if (ifidx == 0) { /* do it only for primary eth0 */
1042 /* try to bring up bus */
1043 ret = brcmf_bus_start(&drvr_priv->pub);
1045 brcmf_dbg(ERROR, "failed with code %d\n", ret);
1048 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
1050 memcpy(net->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
1052 /* Get current TOE mode from dongle */
1053 if (brcmf_toe_get(drvr_priv, ifidx, &toe_ol) >= 0
1054 && (toe_ol & TOE_TX_CSUM_OL) != 0)
1055 drvr_priv->iflist[ifidx]->net->features |=
1058 drvr_priv->iflist[ifidx]->net->features &=
1061 /* Allow transmit calls */
1062 netif_start_queue(net);
1063 drvr_priv->pub.up = 1;
1064 if (unlikely(brcmf_cfg80211_up(drvr_priv->pub.config))) {
1065 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
1073 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, struct net_device *net,
1074 char *name, u8 *mac_addr, u32 flags, u8 bssidx)
1076 struct brcmf_if *ifp;
1078 brcmf_dbg(TRACE, "idx %d, handle->%p\n", ifidx, net);
1080 ifp = drvr_priv->iflist[ifidx];
1082 ifp = kmalloc(sizeof(struct brcmf_if), GFP_ATOMIC);
1084 brcmf_dbg(ERROR, "OOM - struct brcmf_if\n");
1089 memset(ifp, 0, sizeof(struct brcmf_if));
1090 ifp->info = drvr_priv;
1091 drvr_priv->iflist[ifidx] = ifp;
1092 strlcpy(ifp->name, name, IFNAMSIZ);
1093 if (mac_addr != NULL)
1094 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
1097 ifp->state = BRCMF_E_IF_ADD;
1099 wake_up(&drvr_priv->sysioc_waitq);
1106 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
1108 struct brcmf_if *ifp;
1110 brcmf_dbg(TRACE, "idx %d\n", ifidx);
1112 ifp = drvr_priv->iflist[ifidx];
1114 brcmf_dbg(ERROR, "Null interface\n");
1118 ifp->state = BRCMF_E_IF_DEL;
1120 wake_up(&drvr_priv->sysioc_waitq);
1123 struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
1125 struct brcmf_info *drvr_priv = NULL;
1126 struct net_device *net;
1128 brcmf_dbg(TRACE, "Enter\n");
1130 /* Allocate etherdev, including space for private structure */
1131 net = alloc_etherdev(sizeof(drvr_priv));
1133 brcmf_dbg(ERROR, "OOM - alloc_etherdev\n");
1137 /* Allocate primary brcmf_info */
1138 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
1140 brcmf_dbg(ERROR, "OOM - alloc brcmf_info\n");
1145 * Save the brcmf_info into the priv
1147 memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1149 /* Set network interface name if it was provided as module parameter */
1150 if (iface_name[0]) {
1153 strncpy(net->name, iface_name, IFNAMSIZ);
1154 net->name[IFNAMSIZ - 1] = 0;
1155 len = strlen(net->name);
1156 ch = net->name[len - 1];
1157 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1158 strcat(net->name, "%d");
1161 if (brcmf_add_if(drvr_priv, 0, net, net->name, NULL, 0, 0) ==
1165 net->netdev_ops = NULL;
1166 mutex_init(&drvr_priv->proto_block);
1168 /* Link to info module */
1169 drvr_priv->pub.info = drvr_priv;
1171 /* Link to bus module */
1172 drvr_priv->pub.bus = bus;
1173 drvr_priv->pub.hdrlen = bus_hdrlen;
1175 /* Attach and link in the protocol */
1176 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
1177 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
1181 /* Attach and link in the cfg80211 */
1182 drvr_priv->pub.config =
1183 brcmf_cfg80211_attach(net,
1184 brcmf_bus_get_device(bus),
1186 if (unlikely(drvr_priv->pub.config == NULL)) {
1187 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1192 init_waitqueue_head(&drvr_priv->sysioc_waitq);
1193 drvr_priv->sysioc_tsk = kthread_run(_brcmf_sysioc_thread,
1194 drvr_priv, "_brcmf_sysioc");
1195 if (IS_ERR(drvr_priv->sysioc_tsk)) {
1197 "_brcmf_sysioc thread failed to start\n");
1198 drvr_priv->sysioc_tsk = NULL;
1201 drvr_priv->sysioc_tsk = NULL;
1204 * Save the brcmf_info into the priv
1206 memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1208 return &drvr_priv->pub;
1214 brcmf_detach(&drvr_priv->pub);
1219 int brcmf_bus_start(struct brcmf_pub *drvr)
1222 struct brcmf_info *drvr_priv = drvr->info;
1223 /* Room for "event_msgs" + '\0' + bitvec */
1224 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1226 brcmf_dbg(TRACE, "\n");
1228 /* Bring up the bus */
1229 ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub, true);
1231 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
1235 /* If bus is not ready, can't come up */
1236 if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
1237 brcmf_dbg(ERROR, "failed bus is not ready\n");
1241 brcmu_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1242 iovbuf, sizeof(iovbuf));
1243 brcmf_proto_cdc_query_ioctl(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1245 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1247 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1248 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1249 setbit(drvr->eventmask, BRCMF_E_AUTH);
1250 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1251 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1252 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1253 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1254 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1255 setbit(drvr->eventmask, BRCMF_E_JOIN);
1256 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1257 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1258 setbit(drvr->eventmask, BRCMF_E_LINK);
1259 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1260 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1261 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1262 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1263 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1264 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1266 /* enable dongle roaming event */
1268 drvr->pktfilter_count = 1;
1269 /* Setup filter to allow only unicast */
1270 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1272 /* Bus is ready, do any protocol initialization */
1273 ret = brcmf_proto_init(&drvr_priv->pub);
1280 static struct net_device_ops brcmf_netdev_ops_pri = {
1281 .ndo_open = brcmf_netdev_open,
1282 .ndo_stop = brcmf_netdev_stop,
1283 .ndo_get_stats = brcmf_netdev_get_stats,
1284 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1285 .ndo_start_xmit = brcmf_netdev_start_xmit,
1286 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1287 .ndo_set_multicast_list = brcmf_netdev_set_multicast_list
1290 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1292 struct brcmf_info *drvr_priv = drvr->info;
1293 struct net_device *net;
1294 u8 temp_addr[ETH_ALEN] = {
1295 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1297 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1299 net = drvr_priv->iflist[ifidx]->net;
1300 net->netdev_ops = &brcmf_netdev_ops_pri;
1303 * We have to use the primary MAC for virtual interfaces
1306 /* for virtual interfaces use the primary MAC */
1307 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1312 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1313 /* ACCESSPOINT INTERFACE CASE */
1314 temp_addr[0] |= 0X02; /* set bit 2 ,
1315 - Locally Administered address */
1318 net->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1319 net->ethtool_ops = &brcmf_ethtool_ops;
1321 drvr_priv->pub.rxsz = net->mtu + net->hard_header_len +
1322 drvr_priv->pub.hdrlen;
1324 memcpy(net->dev_addr, temp_addr, ETH_ALEN);
1326 if (register_netdev(net) != 0) {
1327 brcmf_dbg(ERROR, "couldn't register the net device\n");
1331 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", net->name);
1336 net->netdev_ops = NULL;
1340 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1342 struct brcmf_info *drvr_priv;
1344 brcmf_dbg(TRACE, "Enter\n");
1347 drvr_priv = drvr->info;
1349 /* Stop the protocol module */
1350 brcmf_proto_stop(&drvr_priv->pub);
1352 /* Stop the bus module */
1353 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus, true);
1358 void brcmf_detach(struct brcmf_pub *drvr)
1360 struct brcmf_info *drvr_priv;
1362 brcmf_dbg(TRACE, "Enter\n");
1365 drvr_priv = drvr->info;
1367 struct brcmf_if *ifp;
1370 for (i = 1; i < BRCMF_MAX_IFS; i++)
1371 if (drvr_priv->iflist[i])
1372 brcmf_del_if(drvr_priv, i);
1374 ifp = drvr_priv->iflist[0];
1375 if (ifp->net->netdev_ops == &brcmf_netdev_ops_pri) {
1376 brcmf_netdev_stop(ifp->net);
1377 unregister_netdev(ifp->net);
1380 if (drvr_priv->sysioc_tsk) {
1381 send_sig(SIGTERM, drvr_priv->sysioc_tsk, 1);
1382 kthread_stop(drvr_priv->sysioc_tsk);
1383 drvr_priv->sysioc_tsk = NULL;
1386 brcmf_bus_detach(drvr);
1389 brcmf_proto_detach(drvr);
1391 brcmf_cfg80211_detach(drvr->config);
1393 free_netdev(ifp->net);
1400 static void __exit brcmf_module_cleanup(void)
1402 brcmf_dbg(TRACE, "Enter\n");
1404 brcmf_bus_unregister();
1407 static int __init brcmf_module_init(void)
1411 brcmf_dbg(TRACE, "Enter\n");
1413 error = brcmf_bus_register();
1416 brcmf_dbg(ERROR, "brcmf_bus_register failed\n");
1425 module_init(brcmf_module_init);
1426 module_exit(brcmf_module_cleanup);
1428 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1430 struct brcmf_info *drvr_priv = drvr->info;
1433 mutex_lock(&drvr_priv->proto_block);
1439 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1441 struct brcmf_info *drvr_priv = drvr->info;
1444 mutex_unlock(&drvr_priv->proto_block);
1451 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1453 return atomic_read(&drvr_priv->pend_8021x_cnt);
1456 #define MAX_WAIT_FOR_8021X_TX 10
1458 int brcmf_netdev_wait_pend8021x(struct net_device *dev)
1460 struct brcmf_info *drvr_priv = *(struct brcmf_info **)netdev_priv(dev);
1461 int timeout = 10 * HZ / 1000;
1462 int ntimes = MAX_WAIT_FOR_8021X_TX;
1463 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1465 while (ntimes && pend) {
1467 set_current_state(TASK_INTERRUPTIBLE);
1468 schedule_timeout(timeout);
1469 set_current_state(TASK_RUNNING);
1472 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1478 int brcmf_write_to_file(struct brcmf_pub *drvr, u8 *buf, int size)
1482 mm_segment_t old_fs;
1485 /* change to KERNEL_DS address limit */
1489 /* open file to write */
1490 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1492 brcmf_dbg(ERROR, "open file error\n");
1497 /* Write buf to file */
1498 fp->f_op->write(fp, buf, size, &pos);
1501 /* free buf before return */
1503 /* close file before return */
1505 filp_close(fp, current->files);
1506 /* restore previous address limit */