2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/mmc/sdio_func.h>
25 #include <linux/random.h>
26 #include <linux/spinlock.h>
27 #include <linux/ethtool.h>
28 #include <linux/fcntl.h>
30 #include <linux/uaccess.h>
31 #include <linux/hardirq.h>
32 #include <linux/mutex.h>
33 #include <linux/wait.h>
34 #include <linux/module.h>
35 #include <net/cfg80211.h>
36 #include <net/rtnetlink.h>
38 #include <brcmu_utils.h>
39 #include <brcmu_wifi.h>
43 #include "dhd_proto.h"
45 #include "wl_cfg80211.h"
47 MODULE_AUTHOR("Broadcom Corporation");
48 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
49 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
50 MODULE_LICENSE("Dual BSD/GPL");
53 /* Interface control information */
55 struct brcmf_info *info; /* back pointer to brcmf_info */
56 /* OS/stack specifics */
57 struct net_device *ndev;
58 struct net_device_stats stats;
59 int idx; /* iface idx in dongle */
60 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
63 /* Local private structure (extension of pub) */
67 /* OS/stack specifics */
68 struct brcmf_if *iflist[BRCMF_MAX_IFS];
70 struct mutex proto_block;
72 struct work_struct setmacaddr_work;
73 struct work_struct multicast_work;
74 u8 macvalue[ETH_ALEN];
75 atomic_t pend_8021x_cnt;
79 int brcmf_msg_level = BRCMF_ERROR_VAL;
80 module_param(brcmf_msg_level, int, 0);
82 int brcmf_ifname2idx(struct brcmf_info *drvr_priv, char *name)
84 int i = BRCMF_MAX_IFS;
87 if (name == NULL || *name == '\0')
91 ifp = drvr_priv->iflist[i];
92 if (ifp && !strncmp(ifp->ndev->name, name, IFNAMSIZ))
96 brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
98 return i; /* default - the primary interface */
101 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
103 struct brcmf_info *drvr_priv = drvr->info;
105 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
106 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
110 if (drvr_priv->iflist[ifidx] == NULL) {
111 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
115 if (drvr_priv->iflist[ifidx]->ndev)
116 return drvr_priv->iflist[ifidx]->ndev->name;
121 static void _brcmf_set_multicast_list(struct work_struct *work)
123 struct net_device *ndev;
124 struct netdev_hw_addr *ha;
127 __le32 dcmd_le_value;
129 struct brcmf_dcmd dcmd;
134 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
137 ndev = drvr_priv->iflist[0]->ndev;
138 cnt = netdev_mc_count(ndev);
140 /* Determine initial value of allmulti flag */
141 dcmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
143 /* Send down the multicast list first. */
145 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
146 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
150 strcpy(bufp, "mcast_list");
151 bufp += strlen("mcast_list") + 1;
153 cnt_le = cpu_to_le32(cnt);
154 memcpy(bufp, &cnt_le, sizeof(cnt));
155 bufp += sizeof(cnt_le);
157 netdev_for_each_mc_addr(ha, ndev) {
160 memcpy(bufp, ha->addr, ETH_ALEN);
165 memset(&dcmd, 0, sizeof(dcmd));
166 dcmd.cmd = BRCMF_C_SET_VAR;
171 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
173 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
174 brcmf_ifname(&drvr_priv->pub, 0), cnt);
175 dcmd_value = cnt ? true : dcmd_value;
180 /* Now send the allmulti setting. This is based on the setting in the
181 * net_device flags, but might be modified above to be turned on if we
182 * were trying to set some addresses and dongle rejected it...
185 buflen = sizeof("allmulti") + sizeof(dcmd_value);
186 buf = kmalloc(buflen, GFP_ATOMIC);
190 dcmd_le_value = cpu_to_le32(dcmd_value);
193 ("allmulti", (void *)&dcmd_le_value,
194 sizeof(dcmd_le_value), buf, buflen)) {
195 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
196 brcmf_ifname(&drvr_priv->pub, 0),
197 (int)sizeof(dcmd_value), buflen);
202 memset(&dcmd, 0, sizeof(dcmd));
203 dcmd.cmd = BRCMF_C_SET_VAR;
208 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
210 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
211 brcmf_ifname(&drvr_priv->pub, 0),
212 le32_to_cpu(dcmd_le_value));
217 /* Finally, pick up the PROMISC flag as well, like the NIC
220 dcmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
221 dcmd_le_value = cpu_to_le32(dcmd_value);
223 memset(&dcmd, 0, sizeof(dcmd));
224 dcmd.cmd = BRCMF_C_SET_PROMISC;
225 dcmd.buf = &dcmd_le_value;
226 dcmd.len = sizeof(dcmd_le_value);
229 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
231 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
232 brcmf_ifname(&drvr_priv->pub, 0),
233 le32_to_cpu(dcmd_le_value));
238 _brcmf_set_mac_address(struct work_struct *work)
241 struct brcmf_dcmd dcmd;
244 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
247 brcmf_dbg(TRACE, "enter\n");
248 if (!brcmf_c_mkiovar("cur_etheraddr", (char *)drvr_priv->macvalue,
249 ETH_ALEN, buf, 32)) {
250 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
251 brcmf_ifname(&drvr_priv->pub, 0));
254 memset(&dcmd, 0, sizeof(dcmd));
255 dcmd.cmd = BRCMF_C_SET_VAR;
260 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
262 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
263 brcmf_ifname(&drvr_priv->pub, 0));
265 memcpy(drvr_priv->iflist[0]->ndev->dev_addr,
266 drvr_priv->macvalue, ETH_ALEN);
271 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
273 struct brcmf_if *ifp = netdev_priv(ndev);
274 struct brcmf_info *drvr_priv = ifp->info;
275 struct sockaddr *sa = (struct sockaddr *)addr;
277 memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
278 schedule_work(&drvr_priv->setmacaddr_work);
282 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
284 struct brcmf_if *ifp = netdev_priv(ndev);
285 struct brcmf_info *drvr_priv = ifp->info;
287 schedule_work(&drvr_priv->multicast_work);
290 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
292 struct brcmf_info *drvr_priv = drvr->info;
295 if (!drvr->up || (drvr->bus_if->state == BRCMF_BUS_DOWN))
298 /* Update multicast statistic */
299 if (pktbuf->len >= ETH_ALEN) {
300 u8 *pktdata = (u8 *) (pktbuf->data);
301 struct ethhdr *eh = (struct ethhdr *)pktdata;
303 if (is_multicast_ether_addr(eh->h_dest))
304 drvr->tx_multicast++;
305 if (ntohs(eh->h_proto) == ETH_P_PAE)
306 atomic_inc(&drvr_priv->pend_8021x_cnt);
309 /* If the protocol uses a data header, apply it */
310 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
312 /* Use bus module to send data frame */
313 return brcmf_sdbrcm_bus_txdata(drvr->dev, pktbuf);
316 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
319 struct brcmf_if *ifp = netdev_priv(ndev);
320 struct brcmf_info *drvr_priv = ifp->info;
322 brcmf_dbg(TRACE, "Enter\n");
325 if (!drvr_priv->pub.up ||
326 (drvr_priv->pub.bus_if->state == BRCMF_BUS_DOWN)) {
327 brcmf_dbg(ERROR, "xmit rejected pub.up=%d state=%d\n",
329 drvr_priv->pub.bus_if->state);
330 netif_stop_queue(ndev);
334 if (!drvr_priv->iflist[ifp->idx]) {
335 brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
336 netif_stop_queue(ndev);
340 /* Make sure there's enough room for any header */
341 if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
342 struct sk_buff *skb2;
344 brcmf_dbg(INFO, "%s: insufficient headroom\n",
345 brcmf_ifname(&drvr_priv->pub, ifp->idx));
346 drvr_priv->pub.tx_realloc++;
347 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
351 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
352 brcmf_ifname(&drvr_priv->pub, ifp->idx));
358 ret = brcmf_sendpkt(&drvr_priv->pub, ifp->idx, skb);
362 drvr_priv->pub.dstats.tx_dropped++;
364 drvr_priv->pub.tx_packets++;
366 /* Return ok: we always eat the packet */
370 void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
372 struct net_device *ndev;
373 struct brcmf_info *drvr_priv = drvr->info;
375 brcmf_dbg(TRACE, "Enter\n");
378 ndev = drvr_priv->iflist[ifidx]->ndev;
380 netif_stop_queue(ndev);
382 netif_wake_queue(ndev);
385 static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx,
386 void *pktdata, struct brcmf_event_msg *event,
391 bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
395 if (drvr_priv->iflist[*ifidx]->ndev)
396 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->ndev,
402 void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx,
403 struct sk_buff_head *skb_list)
405 struct brcmf_info *drvr_priv = drvr->info;
409 struct sk_buff *skb, *pnext;
410 struct brcmf_if *ifp;
411 struct brcmf_event_msg event;
413 brcmf_dbg(TRACE, "Enter\n");
415 skb_queue_walk_safe(skb_list, skb, pnext) {
416 skb_unlink(skb, skb_list);
418 /* Get the protocol, maintain skb around eth_type_trans()
419 * The main reason for this hack is for the limitation of
420 * Linux 2.4 where 'eth_type_trans' uses the
421 * 'net->hard_header_len'
422 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
423 * coping of the packet coming from the network stack to add
424 * BDC, Hardware header etc, during network interface
426 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
428 * for BDC, Hardware header etc. and not just the ETH_HLEN
433 ifp = drvr_priv->iflist[ifidx];
435 ifp = drvr_priv->iflist[0];
437 if (!ifp || !ifp->ndev ||
438 ifp->ndev->reg_state != NETREG_REGISTERED) {
439 brcmu_pkt_buf_free_skb(skb);
443 skb->dev = ifp->ndev;
444 skb->protocol = eth_type_trans(skb, skb->dev);
446 if (skb->pkt_type == PACKET_MULTICAST)
447 drvr_priv->pub.rx_multicast++;
452 /* Strip header, count, deliver upward */
453 skb_pull(skb, ETH_HLEN);
455 /* Process special event packets and then discard them */
456 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
457 brcmf_host_event(drvr_priv, &ifidx,
461 if (drvr_priv->iflist[ifidx]) {
462 ifp = drvr_priv->iflist[ifidx];
463 ifp->ndev->last_rx = jiffies;
466 drvr->dstats.rx_bytes += skb->len;
467 drvr->rx_packets++; /* Local count */
472 /* If the receive is not processed inside an ISR,
473 * the softirqd must be woken explicitly to service
474 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
475 * by netif_rx_ni(), but in earlier kernels, we need
482 void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
485 struct brcmf_info *drvr_priv = drvr->info;
489 brcmf_proto_hdrpull(drvr, &ifidx, txp);
491 eh = (struct ethhdr *)(txp->data);
492 type = ntohs(eh->h_proto);
494 if (type == ETH_P_PAE)
495 atomic_dec(&drvr_priv->pend_8021x_cnt);
499 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
501 struct brcmf_if *ifp = netdev_priv(ndev);
502 struct brcmf_info *drvr_priv = ifp->info;
504 brcmf_dbg(TRACE, "Enter\n");
506 if (drvr_priv->pub.up)
507 /* Use the protocol to get dongle stats */
508 brcmf_proto_dstats(&drvr_priv->pub);
510 /* Copy dongle stats to net device stats */
511 ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
512 ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
513 ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
514 ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
515 ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
516 ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
517 ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
518 ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
519 ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
524 /* Retrieve current toe component enables, which are kept
525 as a bitmap in toe_ol iovar */
526 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
528 struct brcmf_dcmd dcmd;
533 memset(&dcmd, 0, sizeof(dcmd));
535 dcmd.cmd = BRCMF_C_GET_VAR;
537 dcmd.len = (uint) sizeof(buf);
540 strcpy(buf, "toe_ol");
541 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
543 /* Check for older dongle image that doesn't support toe_ol */
545 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
546 brcmf_ifname(&drvr_priv->pub, ifidx));
550 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
551 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
555 memcpy(&toe_le, buf, sizeof(u32));
556 *toe_ol = le32_to_cpu(toe_le);
560 /* Set current toe component enables in toe_ol iovar,
561 and set toe global enable iovar */
562 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
564 struct brcmf_dcmd dcmd;
567 __le32 toe_le = cpu_to_le32(toe_ol);
569 memset(&dcmd, 0, sizeof(dcmd));
571 dcmd.cmd = BRCMF_C_SET_VAR;
573 dcmd.len = (uint) sizeof(buf);
576 /* Set toe_ol as requested */
577 strcpy(buf, "toe_ol");
578 memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
580 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
582 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
583 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
587 /* Enable toe globally only if any components are enabled. */
588 toe_le = cpu_to_le32(toe_ol != 0);
591 memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
593 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
595 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
596 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
603 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
604 struct ethtool_drvinfo *info)
606 struct brcmf_if *ifp = netdev_priv(ndev);
607 struct brcmf_info *drvr_priv = ifp->info;
609 sprintf(info->driver, KBUILD_MODNAME);
610 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
611 sprintf(info->bus_info, "%s",
612 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
615 static struct ethtool_ops brcmf_ethtool_ops = {
616 .get_drvinfo = brcmf_ethtool_get_drvinfo
619 static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
621 struct ethtool_drvinfo info;
622 char drvname[sizeof(info.driver)];
624 struct ethtool_value edata;
625 u32 toe_cmpnt, csum_dir;
628 brcmf_dbg(TRACE, "Enter\n");
630 /* all ethtool calls start with a cmd word */
631 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
635 case ETHTOOL_GDRVINFO:
636 /* Copy out any request driver name */
637 if (copy_from_user(&info, uaddr, sizeof(info)))
639 strncpy(drvname, info.driver, sizeof(info.driver));
640 drvname[sizeof(info.driver) - 1] = '\0';
642 /* clear struct for return */
643 memset(&info, 0, sizeof(info));
646 /* if requested, identify ourselves */
647 if (strcmp(drvname, "?dhd") == 0) {
648 sprintf(info.driver, "dhd");
649 strcpy(info.version, BRCMF_VERSION_STR);
652 /* otherwise, require dongle to be up */
653 else if (!drvr_priv->pub.up) {
654 brcmf_dbg(ERROR, "dongle is not up\n");
658 /* finally, report dongle driver type */
659 else if (drvr_priv->pub.iswl)
660 sprintf(info.driver, "wl");
662 sprintf(info.driver, "xx");
664 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
665 if (copy_to_user(uaddr, &info, sizeof(info)))
667 brcmf_dbg(CTL, "given %*s, returning %s\n",
668 (int)sizeof(drvname), drvname, info.driver);
671 /* Get toe offload components from dongle */
672 case ETHTOOL_GRXCSUM:
673 case ETHTOOL_GTXCSUM:
674 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
679 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
682 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
684 if (copy_to_user(uaddr, &edata, sizeof(edata)))
688 /* Set toe offload components in dongle */
689 case ETHTOOL_SRXCSUM:
690 case ETHTOOL_STXCSUM:
691 if (copy_from_user(&edata, uaddr, sizeof(edata)))
694 /* Read the current settings, update and write back */
695 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
700 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
703 toe_cmpnt |= csum_dir;
705 toe_cmpnt &= ~csum_dir;
707 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
711 /* If setting TX checksum mode, tell Linux the new mode */
712 if (cmd == ETHTOOL_STXCSUM) {
714 drvr_priv->iflist[0]->ndev->features |=
717 drvr_priv->iflist[0]->ndev->features &=
730 static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
733 struct brcmf_if *ifp = netdev_priv(ndev);
734 struct brcmf_info *drvr_priv = ifp->info;
736 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
738 if (!drvr_priv->iflist[ifp->idx])
741 if (cmd == SIOCETHTOOL)
742 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
747 /* called only from within this driver. Sends a command to the dongle. */
748 s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
750 struct brcmf_dcmd dcmd;
754 struct brcmf_if *ifp = netdev_priv(ndev);
755 struct brcmf_info *drvr_priv = ifp->info;
757 memset(&dcmd, 0, sizeof(dcmd));
762 if (dcmd.buf != NULL)
763 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
765 /* send to dongle (must be up, and wl) */
766 if ((drvr_priv->pub.bus_if->state != BRCMF_BUS_DATA)) {
767 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
772 if (!drvr_priv->pub.iswl) {
778 * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
779 * set key CMD to prevent M4 encryption.
781 is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
782 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
783 !(strncmp("wsec_key", dcmd.buf, 9))) ||
784 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
785 !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
787 brcmf_netdev_wait_pend8021x(ndev);
789 err = brcmf_proto_dcmd(&drvr_priv->pub, ifp->idx, &dcmd, buflen);
798 static int brcmf_netdev_stop(struct net_device *ndev)
800 struct brcmf_if *ifp = netdev_priv(ndev);
801 struct brcmf_pub *drvr = &ifp->info->pub;
803 brcmf_dbg(TRACE, "Enter\n");
804 brcmf_cfg80211_down(drvr->config);
808 /* Set state and stop OS transmissions */
810 netif_stop_queue(ndev);
815 static int brcmf_netdev_open(struct net_device *ndev)
817 struct brcmf_if *ifp = netdev_priv(ndev);
818 struct brcmf_info *drvr_priv = ifp->info;
822 brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
824 if (ifp->idx == 0) { /* do it only for primary eth0 */
825 /* try to bring up bus */
826 ret = brcmf_bus_start(&drvr_priv->pub);
828 brcmf_dbg(ERROR, "failed with code %d\n", ret);
831 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
833 memcpy(ndev->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
835 /* Get current TOE mode from dongle */
836 if (brcmf_toe_get(drvr_priv, ifp->idx, &toe_ol) >= 0
837 && (toe_ol & TOE_TX_CSUM_OL) != 0)
838 drvr_priv->iflist[ifp->idx]->ndev->features |=
841 drvr_priv->iflist[ifp->idx]->ndev->features &=
844 /* Allow transmit calls */
845 netif_start_queue(ndev);
846 drvr_priv->pub.up = 1;
847 if (brcmf_cfg80211_up(drvr_priv->pub.config)) {
848 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
855 static const struct net_device_ops brcmf_netdev_ops_pri = {
856 .ndo_open = brcmf_netdev_open,
857 .ndo_stop = brcmf_netdev_stop,
858 .ndo_get_stats = brcmf_netdev_get_stats,
859 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
860 .ndo_start_xmit = brcmf_netdev_start_xmit,
861 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
862 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
866 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, char *name, u8 *mac_addr)
868 struct brcmf_if *ifp;
869 struct net_device *ndev;
871 brcmf_dbg(TRACE, "idx %d\n", ifidx);
873 ifp = drvr_priv->iflist[ifidx];
875 * Delete the existing interface before overwriting it
876 * in case we missed the BRCMF_E_IF_DEL event.
879 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
881 netif_stop_queue(ifp->ndev);
882 unregister_netdev(ifp->ndev);
883 free_netdev(ifp->ndev);
884 drvr_priv->iflist[ifidx] = NULL;
887 /* Allocate netdev, including space for private structure */
888 ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
890 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
894 ifp = netdev_priv(ndev);
896 ifp->info = drvr_priv;
897 drvr_priv->iflist[ifidx] = ifp;
899 if (mac_addr != NULL)
900 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
902 if (brcmf_net_attach(&drvr_priv->pub, ifp->idx)) {
903 brcmf_dbg(ERROR, "brcmf_net_attach failed");
904 free_netdev(ifp->ndev);
905 drvr_priv->iflist[ifidx] = NULL;
909 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
910 current->pid, ifp->ndev->name);
915 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
917 struct brcmf_if *ifp;
919 brcmf_dbg(TRACE, "idx %d\n", ifidx);
921 ifp = drvr_priv->iflist[ifidx];
923 brcmf_dbg(ERROR, "Null interface\n");
928 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
930 brcmf_netdev_stop(ifp->ndev);
934 netif_stop_queue(ifp->ndev);
937 unregister_netdev(ifp->ndev);
938 drvr_priv->iflist[ifidx] = NULL;
940 brcmf_cfg80211_detach(drvr_priv->pub.config);
941 free_netdev(ifp->ndev);
945 struct brcmf_pub *brcmf_attach(struct brcmf_sdio *bus, uint bus_hdrlen,
948 struct brcmf_info *drvr_priv = NULL;
950 brcmf_dbg(TRACE, "Enter\n");
952 /* Allocate primary brcmf_info */
953 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
957 mutex_init(&drvr_priv->proto_block);
959 /* Link to info module */
960 drvr_priv->pub.info = drvr_priv;
962 /* Link to bus module */
963 drvr_priv->pub.bus = bus;
964 drvr_priv->pub.hdrlen = bus_hdrlen;
965 drvr_priv->pub.bus_if = dev_get_drvdata(dev);
966 drvr_priv->pub.dev = dev;
968 /* Attach and link in the protocol */
969 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
970 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
974 INIT_WORK(&drvr_priv->setmacaddr_work, _brcmf_set_mac_address);
975 INIT_WORK(&drvr_priv->multicast_work, _brcmf_set_multicast_list);
977 return &drvr_priv->pub;
981 brcmf_detach(&drvr_priv->pub);
986 int brcmf_bus_start(struct brcmf_pub *drvr)
989 struct brcmf_info *drvr_priv = drvr->info;
990 /* Room for "event_msgs" + '\0' + bitvec */
991 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
993 brcmf_dbg(TRACE, "\n");
995 /* Bring up the bus */
996 ret = brcmf_sdbrcm_bus_init(drvr_priv->pub.dev);
998 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
1002 /* If bus is not ready, can't come up */
1003 if (drvr_priv->pub.bus_if->state != BRCMF_BUS_DATA) {
1004 brcmf_dbg(ERROR, "failed bus is not ready\n");
1008 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1009 iovbuf, sizeof(iovbuf));
1010 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1012 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1014 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1015 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1016 setbit(drvr->eventmask, BRCMF_E_AUTH);
1017 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1018 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1019 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1020 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1021 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1022 setbit(drvr->eventmask, BRCMF_E_JOIN);
1023 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1024 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1025 setbit(drvr->eventmask, BRCMF_E_LINK);
1026 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1027 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1028 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1029 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1030 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1031 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1033 /* enable dongle roaming event */
1035 drvr->pktfilter_count = 1;
1036 /* Setup filter to allow only unicast */
1037 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1039 /* Bus is ready, do any protocol initialization */
1040 ret = brcmf_proto_init(&drvr_priv->pub);
1047 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1049 struct brcmf_info *drvr_priv = drvr->info;
1050 struct net_device *ndev;
1051 u8 temp_addr[ETH_ALEN] = {
1052 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1054 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1056 ndev = drvr_priv->iflist[ifidx]->ndev;
1057 ndev->netdev_ops = &brcmf_netdev_ops_pri;
1060 * We have to use the primary MAC for virtual interfaces
1063 /* for virtual interfaces use the primary MAC */
1064 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1069 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1070 /* ACCESSPOINT INTERFACE CASE */
1071 temp_addr[0] |= 0X02; /* set bit 2 ,
1072 - Locally Administered address */
1075 ndev->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1076 ndev->ethtool_ops = &brcmf_ethtool_ops;
1078 drvr_priv->pub.rxsz = ndev->mtu + ndev->hard_header_len +
1079 drvr_priv->pub.hdrlen;
1081 memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
1083 /* attach to cfg80211 for primary interface */
1086 brcmf_cfg80211_attach(ndev,
1087 brcmf_bus_get_device(drvr->bus),
1089 if (drvr->config == NULL) {
1090 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1095 if (register_netdev(ndev) != 0) {
1096 brcmf_dbg(ERROR, "couldn't register the net device\n");
1100 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
1105 ndev->netdev_ops = NULL;
1109 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1111 struct brcmf_info *drvr_priv;
1113 brcmf_dbg(TRACE, "Enter\n");
1116 drvr_priv = drvr->info;
1118 /* Stop the protocol module */
1119 brcmf_proto_stop(&drvr_priv->pub);
1121 /* Stop the bus module */
1122 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus);
1127 void brcmf_detach(struct brcmf_pub *drvr)
1129 struct brcmf_info *drvr_priv;
1131 brcmf_dbg(TRACE, "Enter\n");
1134 drvr_priv = drvr->info;
1138 /* make sure primary interface removed last */
1139 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
1140 if (drvr_priv->iflist[i])
1141 brcmf_del_if(drvr_priv, i);
1143 cancel_work_sync(&drvr_priv->setmacaddr_work);
1144 cancel_work_sync(&drvr_priv->multicast_work);
1146 brcmf_bus_detach(drvr);
1149 brcmf_proto_detach(drvr);
1156 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1158 struct brcmf_info *drvr_priv = drvr->info;
1161 mutex_lock(&drvr_priv->proto_block);
1167 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1169 struct brcmf_info *drvr_priv = drvr->info;
1172 mutex_unlock(&drvr_priv->proto_block);
1179 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1181 return atomic_read(&drvr_priv->pend_8021x_cnt);
1184 #define MAX_WAIT_FOR_8021X_TX 10
1186 int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1188 struct brcmf_if *ifp = netdev_priv(ndev);
1189 struct brcmf_info *drvr_priv = ifp->info;
1190 int timeout = 10 * HZ / 1000;
1191 int ntimes = MAX_WAIT_FOR_8021X_TX;
1192 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1194 while (ntimes && pend) {
1196 set_current_state(TASK_INTERRUPTIBLE);
1197 schedule_timeout(timeout);
1198 set_current_state(TASK_RUNNING);
1201 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1207 int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
1211 mm_segment_t old_fs;
1214 /* change to KERNEL_DS address limit */
1218 /* open file to write */
1219 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1221 brcmf_dbg(ERROR, "open file error\n");
1226 /* Write buf to file */
1227 fp->f_op->write(fp, (char __user *)buf, size, &pos);
1230 /* free buf before return */
1232 /* close file before return */
1234 filp_close(fp, current->files);
1235 /* restore previous address limit */