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_cfg80211_get_sdio_func()->dev));
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 *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);
953 struct brcmf_c_ioctl ioc;
961 ifidx = brcmf_net2idx(drvr_priv, net);
962 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifidx, cmd);
964 if (ifidx == BRCMF_BAD_IF)
967 if (cmd == SIOCETHTOOL)
968 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
970 if (cmd != SIOCDEVPRIVATE)
973 memset(&ioc, 0, sizeof(ioc));
975 /* Copy the ioc control structure part of ioctl request */
976 if (copy_from_user(&ioc, ifr->ifr_data, sizeof(struct brcmf_ioctl))) {
981 /* Copy out any buffer passed */
983 buflen = min_t(int, ioc.len, BRCMF_IOCTL_MAXLEN);
984 /* optimization for direct ioctl calls from kernel */
986 if (segment_eq(get_fs(), KERNEL_DS)) {
991 buf = kmalloc(buflen, GFP_ATOMIC);
996 if (copy_from_user(buf, ioc.buf, buflen)) {
1003 /* To differentiate read 4 more byes */
1004 if ((copy_from_user(&driver, (char *)ifr->ifr_data +
1005 sizeof(struct brcmf_ioctl), sizeof(uint)) != 0)) {
1010 if (!capable(CAP_NET_ADMIN)) {
1015 /* check for local brcmf ioctl and handle it */
1016 if (driver == BRCMF_IOCTL_MAGIC) {
1017 bcmerror = brcmf_c_ioctl((void *)&drvr_priv->pub, &ioc,
1020 drvr_priv->pub.bcmerror = bcmerror;
1024 /* send to dongle (must be up, and wl) */
1025 if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
1026 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
1031 if (!drvr_priv->pub.iswl) {
1037 * Intercept BRCMF_C_SET_KEY IOCTL - serialize M4 send and
1038 * set key IOCTL to prevent M4 encryption.
1040 is_set_key_cmd = ((ioc.cmd == BRCMF_C_SET_KEY) ||
1041 ((ioc.cmd == BRCMF_C_SET_VAR) &&
1042 !(strncmp("wsec_key", ioc.buf, 9))) ||
1043 ((ioc.cmd == BRCMF_C_SET_VAR) &&
1044 !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1046 brcmf_netdev_wait_pend8021x(net);
1048 bcmerror = brcmf_proto_ioctl(&drvr_priv->pub, ifidx,
1049 (struct brcmf_ioctl *)&ioc, buf, buflen);
1052 if (!bcmerror && buf && ioc.buf) {
1053 if (copy_to_user(ioc.buf, buf, buflen))
1065 static int brcmf_netdev_stop(struct net_device *net)
1067 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
1069 brcmf_dbg(TRACE, "Enter\n");
1070 brcmf_cfg80211_down();
1071 if (drvr_priv->pub.up == 0)
1074 /* Set state and stop OS transmissions */
1075 drvr_priv->pub.up = 0;
1076 netif_stop_queue(net);
1081 static int brcmf_netdev_open(struct net_device *net)
1083 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
1085 int ifidx = brcmf_net2idx(drvr_priv, net);
1088 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1090 if (ifidx == 0) { /* do it only for primary eth0 */
1092 /* try to bring up bus */
1093 ret = brcmf_bus_start(&drvr_priv->pub);
1095 brcmf_dbg(ERROR, "failed with code %d\n", ret);
1098 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
1100 memcpy(net->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
1102 /* Get current TOE mode from dongle */
1103 if (brcmf_toe_get(drvr_priv, ifidx, &toe_ol) >= 0
1104 && (toe_ol & TOE_TX_CSUM_OL) != 0)
1105 drvr_priv->iflist[ifidx]->net->features |=
1108 drvr_priv->iflist[ifidx]->net->features &=
1111 /* Allow transmit calls */
1112 netif_start_queue(net);
1113 drvr_priv->pub.up = 1;
1114 if (unlikely(brcmf_cfg80211_up())) {
1115 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
1123 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, struct net_device *net,
1124 char *name, u8 *mac_addr, u32 flags, u8 bssidx)
1126 struct brcmf_if *ifp;
1128 brcmf_dbg(TRACE, "idx %d, handle->%p\n", ifidx, net);
1130 ifp = drvr_priv->iflist[ifidx];
1132 ifp = kmalloc(sizeof(struct brcmf_if), GFP_ATOMIC);
1134 brcmf_dbg(ERROR, "OOM - struct brcmf_if\n");
1139 memset(ifp, 0, sizeof(struct brcmf_if));
1140 ifp->info = drvr_priv;
1141 drvr_priv->iflist[ifidx] = ifp;
1142 strlcpy(ifp->name, name, IFNAMSIZ);
1143 if (mac_addr != NULL)
1144 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
1147 ifp->state = BRCMF_E_IF_ADD;
1149 wake_up(&drvr_priv->sysioc_waitq);
1156 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
1158 struct brcmf_if *ifp;
1160 brcmf_dbg(TRACE, "idx %d\n", ifidx);
1162 ifp = drvr_priv->iflist[ifidx];
1164 brcmf_dbg(ERROR, "Null interface\n");
1168 ifp->state = BRCMF_E_IF_DEL;
1170 wake_up(&drvr_priv->sysioc_waitq);
1173 struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
1175 struct brcmf_info *drvr_priv = NULL;
1176 struct net_device *net;
1178 brcmf_dbg(TRACE, "Enter\n");
1180 /* Allocate etherdev, including space for private structure */
1181 net = alloc_etherdev(sizeof(drvr_priv));
1183 brcmf_dbg(ERROR, "OOM - alloc_etherdev\n");
1187 /* Allocate primary brcmf_info */
1188 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
1190 brcmf_dbg(ERROR, "OOM - alloc brcmf_info\n");
1195 * Save the brcmf_info into the priv
1197 memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1199 /* Set network interface name if it was provided as module parameter */
1200 if (iface_name[0]) {
1203 strncpy(net->name, iface_name, IFNAMSIZ);
1204 net->name[IFNAMSIZ - 1] = 0;
1205 len = strlen(net->name);
1206 ch = net->name[len - 1];
1207 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1208 strcat(net->name, "%d");
1211 if (brcmf_add_if(drvr_priv, 0, net, net->name, NULL, 0, 0) ==
1215 net->netdev_ops = NULL;
1216 mutex_init(&drvr_priv->proto_block);
1218 /* Link to info module */
1219 drvr_priv->pub.info = drvr_priv;
1221 /* Link to bus module */
1222 drvr_priv->pub.bus = bus;
1223 drvr_priv->pub.hdrlen = bus_hdrlen;
1225 /* Attach and link in the protocol */
1226 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
1227 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
1231 /* Attach and link in the cfg80211 */
1232 if (unlikely(brcmf_cfg80211_attach(net, &drvr_priv->pub))) {
1233 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1238 init_waitqueue_head(&drvr_priv->sysioc_waitq);
1239 drvr_priv->sysioc_tsk = kthread_run(_brcmf_sysioc_thread,
1240 drvr_priv, "_brcmf_sysioc");
1241 if (IS_ERR(drvr_priv->sysioc_tsk)) {
1243 "_brcmf_sysioc thread failed to start\n");
1244 drvr_priv->sysioc_tsk = NULL;
1247 drvr_priv->sysioc_tsk = NULL;
1250 * Save the brcmf_info into the priv
1252 memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1254 return &drvr_priv->pub;
1260 brcmf_detach(&drvr_priv->pub);
1265 int brcmf_bus_start(struct brcmf_pub *drvr)
1268 struct brcmf_info *drvr_priv = drvr->info;
1269 /* Room for "event_msgs" + '\0' + bitvec */
1270 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1272 brcmf_dbg(TRACE, "\n");
1274 /* Bring up the bus */
1275 ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub, true);
1277 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
1281 /* If bus is not ready, can't come up */
1282 if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
1283 brcmf_dbg(ERROR, "failed bus is not ready\n");
1287 brcmu_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1288 iovbuf, sizeof(iovbuf));
1289 brcmf_proto_cdc_query_ioctl(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1291 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1293 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1294 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1295 setbit(drvr->eventmask, BRCMF_E_AUTH);
1296 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1297 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1298 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1299 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1300 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1301 setbit(drvr->eventmask, BRCMF_E_JOIN);
1302 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1303 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1304 setbit(drvr->eventmask, BRCMF_E_LINK);
1305 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1306 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1307 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1308 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1309 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1310 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1312 /* enable dongle roaming event */
1314 drvr->pktfilter_count = 1;
1315 /* Setup filter to allow only unicast */
1316 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1318 /* Bus is ready, do any protocol initialization */
1319 ret = brcmf_proto_init(&drvr_priv->pub);
1326 static struct net_device_ops brcmf_netdev_ops_pri = {
1327 .ndo_open = brcmf_netdev_open,
1328 .ndo_stop = brcmf_netdev_stop,
1329 .ndo_get_stats = brcmf_netdev_get_stats,
1330 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1331 .ndo_start_xmit = brcmf_netdev_start_xmit,
1332 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1333 .ndo_set_multicast_list = brcmf_netdev_set_multicast_list
1336 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1338 struct brcmf_info *drvr_priv = drvr->info;
1339 struct net_device *net;
1340 u8 temp_addr[ETH_ALEN] = {
1341 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1343 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1345 net = drvr_priv->iflist[ifidx]->net;
1346 net->netdev_ops = &brcmf_netdev_ops_pri;
1349 * We have to use the primary MAC for virtual interfaces
1352 /* for virtual interfaces use the primary MAC */
1353 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1358 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1359 /* ACCESSPOINT INTERFACE CASE */
1360 temp_addr[0] |= 0X02; /* set bit 2 ,
1361 - Locally Administered address */
1364 net->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1365 net->ethtool_ops = &brcmf_ethtool_ops;
1367 drvr_priv->pub.rxsz = net->mtu + net->hard_header_len +
1368 drvr_priv->pub.hdrlen;
1370 memcpy(net->dev_addr, temp_addr, ETH_ALEN);
1372 if (register_netdev(net) != 0) {
1373 brcmf_dbg(ERROR, "couldn't register the net device\n");
1377 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", net->name);
1382 net->netdev_ops = NULL;
1386 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1388 struct brcmf_info *drvr_priv;
1390 brcmf_dbg(TRACE, "Enter\n");
1393 drvr_priv = drvr->info;
1395 /* Stop the protocol module */
1396 brcmf_proto_stop(&drvr_priv->pub);
1398 /* Stop the bus module */
1399 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus, true);
1404 void brcmf_detach(struct brcmf_pub *drvr)
1406 struct brcmf_info *drvr_priv;
1408 brcmf_dbg(TRACE, "Enter\n");
1411 drvr_priv = drvr->info;
1413 struct brcmf_if *ifp;
1416 for (i = 1; i < BRCMF_MAX_IFS; i++)
1417 if (drvr_priv->iflist[i])
1418 brcmf_del_if(drvr_priv, i);
1420 ifp = drvr_priv->iflist[0];
1421 if (ifp->net->netdev_ops == &brcmf_netdev_ops_pri) {
1422 brcmf_netdev_stop(ifp->net);
1423 unregister_netdev(ifp->net);
1426 if (drvr_priv->sysioc_tsk) {
1427 send_sig(SIGTERM, drvr_priv->sysioc_tsk, 1);
1428 kthread_stop(drvr_priv->sysioc_tsk);
1429 drvr_priv->sysioc_tsk = NULL;
1432 brcmf_bus_detach(drvr);
1435 brcmf_proto_detach(drvr);
1437 brcmf_cfg80211_detach();
1439 free_netdev(ifp->net);
1446 static void __exit brcmf_module_cleanup(void)
1448 brcmf_dbg(TRACE, "Enter\n");
1450 brcmf_bus_unregister();
1453 static int __init brcmf_module_init(void)
1457 brcmf_dbg(TRACE, "Enter\n");
1459 error = brcmf_bus_register();
1462 brcmf_dbg(ERROR, "brcmf_bus_register failed\n");
1471 module_init(brcmf_module_init);
1472 module_exit(brcmf_module_cleanup);
1474 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1476 struct brcmf_info *drvr_priv = drvr->info;
1479 mutex_lock(&drvr_priv->proto_block);
1485 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1487 struct brcmf_info *drvr_priv = drvr->info;
1490 mutex_unlock(&drvr_priv->proto_block);
1497 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1499 return atomic_read(&drvr_priv->pend_8021x_cnt);
1502 #define MAX_WAIT_FOR_8021X_TX 10
1504 int brcmf_netdev_wait_pend8021x(struct net_device *dev)
1506 struct brcmf_info *drvr_priv = *(struct brcmf_info **)netdev_priv(dev);
1507 int timeout = 10 * HZ / 1000;
1508 int ntimes = MAX_WAIT_FOR_8021X_TX;
1509 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1511 while (ntimes && pend) {
1513 set_current_state(TASK_INTERRUPTIBLE);
1514 schedule_timeout(timeout);
1515 set_current_state(TASK_RUNNING);
1518 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1524 int brcmf_write_to_file(struct brcmf_pub *drvr, u8 *buf, int size)
1528 mm_segment_t old_fs;
1531 /* change to KERNEL_DS address limit */
1535 /* open file to write */
1536 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1538 brcmf_dbg(ERROR, "open file error\n");
1543 /* Write buf to file */
1544 fp->f_op->write(fp, buf, size, &pos);
1547 /* free buf before return */
1549 /* close file before return */
1551 filp_close(fp, current->files);
1552 /* restore previous address limit */