spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / net / wireless / brcm80211 / brcmfmac / dhd_linux.c
blobeb9eb766ac270d5e3011b98568343eeba12c5833
1 /*
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>
29 #include <linux/fs.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>
37 #include <defs.h>
38 #include <brcmu_utils.h>
39 #include <brcmu_wifi.h>
41 #include "dhd.h"
42 #include "dhd_bus.h"
43 #include "dhd_proto.h"
44 #include "dhd_dbg.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 */
54 struct brcmf_if {
55 struct brcmf_pub *drvr; /* back pointer to brcmf_pub */
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 /* Error bits */
64 int brcmf_msg_level = BRCMF_ERROR_VAL;
65 module_param(brcmf_msg_level, int, 0);
67 int brcmf_ifname2idx(struct brcmf_pub *drvr, char *name)
69 int i = BRCMF_MAX_IFS;
70 struct brcmf_if *ifp;
72 if (name == NULL || *name == '\0')
73 return 0;
75 while (--i > 0) {
76 ifp = drvr->iflist[i];
77 if (ifp && !strncmp(ifp->ndev->name, name, IFNAMSIZ))
78 break;
81 brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
83 return i; /* default - the primary interface */
86 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
88 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
89 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
90 return "<if_bad>";
93 if (drvr->iflist[ifidx] == NULL) {
94 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
95 return "<if_null>";
98 if (drvr->iflist[ifidx]->ndev)
99 return drvr->iflist[ifidx]->ndev->name;
101 return "<if_none>";
104 static void _brcmf_set_multicast_list(struct work_struct *work)
106 struct net_device *ndev;
107 struct netdev_hw_addr *ha;
108 u32 dcmd_value, cnt;
109 __le32 cnt_le;
110 __le32 dcmd_le_value;
112 struct brcmf_dcmd dcmd;
113 char *buf, *bufp;
114 uint buflen;
115 int ret;
117 struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
118 multicast_work);
120 ndev = drvr->iflist[0]->ndev;
121 cnt = netdev_mc_count(ndev);
123 /* Determine initial value of allmulti flag */
124 dcmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
126 /* Send down the multicast list first. */
128 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
129 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
130 if (!bufp)
131 return;
133 strcpy(bufp, "mcast_list");
134 bufp += strlen("mcast_list") + 1;
136 cnt_le = cpu_to_le32(cnt);
137 memcpy(bufp, &cnt_le, sizeof(cnt));
138 bufp += sizeof(cnt_le);
140 netdev_for_each_mc_addr(ha, ndev) {
141 if (!cnt)
142 break;
143 memcpy(bufp, ha->addr, ETH_ALEN);
144 bufp += ETH_ALEN;
145 cnt--;
148 memset(&dcmd, 0, sizeof(dcmd));
149 dcmd.cmd = BRCMF_C_SET_VAR;
150 dcmd.buf = buf;
151 dcmd.len = buflen;
152 dcmd.set = true;
154 ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
155 if (ret < 0) {
156 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
157 brcmf_ifname(drvr, 0), cnt);
158 dcmd_value = cnt ? true : dcmd_value;
161 kfree(buf);
163 /* Now send the allmulti setting. This is based on the setting in the
164 * net_device flags, but might be modified above to be turned on if we
165 * were trying to set some addresses and dongle rejected it...
168 buflen = sizeof("allmulti") + sizeof(dcmd_value);
169 buf = kmalloc(buflen, GFP_ATOMIC);
170 if (!buf)
171 return;
173 dcmd_le_value = cpu_to_le32(dcmd_value);
175 if (!brcmf_c_mkiovar
176 ("allmulti", (void *)&dcmd_le_value,
177 sizeof(dcmd_le_value), buf, buflen)) {
178 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
179 brcmf_ifname(drvr, 0),
180 (int)sizeof(dcmd_value), buflen);
181 kfree(buf);
182 return;
185 memset(&dcmd, 0, sizeof(dcmd));
186 dcmd.cmd = BRCMF_C_SET_VAR;
187 dcmd.buf = buf;
188 dcmd.len = buflen;
189 dcmd.set = true;
191 ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
192 if (ret < 0) {
193 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
194 brcmf_ifname(drvr, 0),
195 le32_to_cpu(dcmd_le_value));
198 kfree(buf);
200 /* Finally, pick up the PROMISC flag as well, like the NIC
201 driver does */
203 dcmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
204 dcmd_le_value = cpu_to_le32(dcmd_value);
206 memset(&dcmd, 0, sizeof(dcmd));
207 dcmd.cmd = BRCMF_C_SET_PROMISC;
208 dcmd.buf = &dcmd_le_value;
209 dcmd.len = sizeof(dcmd_le_value);
210 dcmd.set = true;
212 ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
213 if (ret < 0) {
214 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
215 brcmf_ifname(drvr, 0),
216 le32_to_cpu(dcmd_le_value));
220 static void
221 _brcmf_set_mac_address(struct work_struct *work)
223 char buf[32];
224 struct brcmf_dcmd dcmd;
225 int ret;
227 struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
228 setmacaddr_work);
230 brcmf_dbg(TRACE, "enter\n");
231 if (!brcmf_c_mkiovar("cur_etheraddr", (char *)drvr->macvalue,
232 ETH_ALEN, buf, 32)) {
233 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
234 brcmf_ifname(drvr, 0));
235 return;
237 memset(&dcmd, 0, sizeof(dcmd));
238 dcmd.cmd = BRCMF_C_SET_VAR;
239 dcmd.buf = buf;
240 dcmd.len = 32;
241 dcmd.set = true;
243 ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
244 if (ret < 0)
245 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
246 brcmf_ifname(drvr, 0));
247 else
248 memcpy(drvr->iflist[0]->ndev->dev_addr,
249 drvr->macvalue, ETH_ALEN);
251 return;
254 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
256 struct brcmf_if *ifp = netdev_priv(ndev);
257 struct brcmf_pub *drvr = ifp->drvr;
258 struct sockaddr *sa = (struct sockaddr *)addr;
260 memcpy(&drvr->macvalue, sa->sa_data, ETH_ALEN);
261 schedule_work(&drvr->setmacaddr_work);
262 return 0;
265 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
267 struct brcmf_if *ifp = netdev_priv(ndev);
268 struct brcmf_pub *drvr = ifp->drvr;
270 schedule_work(&drvr->multicast_work);
273 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
275 /* Reject if down */
276 if (!drvr->bus_if->drvr_up || (drvr->bus_if->state == BRCMF_BUS_DOWN))
277 return -ENODEV;
279 /* Update multicast statistic */
280 if (pktbuf->len >= ETH_ALEN) {
281 u8 *pktdata = (u8 *) (pktbuf->data);
282 struct ethhdr *eh = (struct ethhdr *)pktdata;
284 if (is_multicast_ether_addr(eh->h_dest))
285 drvr->tx_multicast++;
286 if (ntohs(eh->h_proto) == ETH_P_PAE)
287 atomic_inc(&drvr->pend_8021x_cnt);
290 /* If the protocol uses a data header, apply it */
291 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
293 /* Use bus module to send data frame */
294 return drvr->bus_if->brcmf_bus_txdata(drvr->dev, pktbuf);
297 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
299 int ret;
300 struct brcmf_if *ifp = netdev_priv(ndev);
301 struct brcmf_pub *drvr = ifp->drvr;
303 brcmf_dbg(TRACE, "Enter\n");
305 /* Reject if down */
306 if (!drvr->bus_if->drvr_up ||
307 (drvr->bus_if->state == BRCMF_BUS_DOWN)) {
308 brcmf_dbg(ERROR, "xmit rejected drvup=%d state=%d\n",
309 drvr->bus_if->drvr_up,
310 drvr->bus_if->state);
311 netif_stop_queue(ndev);
312 return -ENODEV;
315 if (!drvr->iflist[ifp->idx]) {
316 brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
317 netif_stop_queue(ndev);
318 return -ENODEV;
321 /* Make sure there's enough room for any header */
322 if (skb_headroom(skb) < drvr->hdrlen) {
323 struct sk_buff *skb2;
325 brcmf_dbg(INFO, "%s: insufficient headroom\n",
326 brcmf_ifname(drvr, ifp->idx));
327 drvr->bus_if->tx_realloc++;
328 skb2 = skb_realloc_headroom(skb, drvr->hdrlen);
329 dev_kfree_skb(skb);
330 skb = skb2;
331 if (skb == NULL) {
332 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
333 brcmf_ifname(drvr, ifp->idx));
334 ret = -ENOMEM;
335 goto done;
339 ret = brcmf_sendpkt(drvr, ifp->idx, skb);
341 done:
342 if (ret)
343 drvr->bus_if->dstats.tx_dropped++;
344 else
345 drvr->bus_if->dstats.tx_packets++;
347 /* Return ok: we always eat the packet */
348 return 0;
351 void brcmf_txflowcontrol(struct device *dev, int ifidx, bool state)
353 struct net_device *ndev;
354 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
355 struct brcmf_pub *drvr = bus_if->drvr;
357 brcmf_dbg(TRACE, "Enter\n");
359 ndev = drvr->iflist[ifidx]->ndev;
360 if (state == ON)
361 netif_stop_queue(ndev);
362 else
363 netif_wake_queue(ndev);
366 static int brcmf_host_event(struct brcmf_pub *drvr, int *ifidx,
367 void *pktdata, struct brcmf_event_msg *event,
368 void **data)
370 int bcmerror = 0;
372 bcmerror = brcmf_c_host_event(drvr, ifidx, pktdata, event, data);
373 if (bcmerror != 0)
374 return bcmerror;
376 if (drvr->iflist[*ifidx]->ndev)
377 brcmf_cfg80211_event(drvr->iflist[*ifidx]->ndev,
378 event, *data);
380 return bcmerror;
383 void brcmf_rx_frame(struct device *dev, int ifidx,
384 struct sk_buff_head *skb_list)
386 unsigned char *eth;
387 uint len;
388 void *data;
389 struct sk_buff *skb, *pnext;
390 struct brcmf_if *ifp;
391 struct brcmf_event_msg event;
392 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
393 struct brcmf_pub *drvr = bus_if->drvr;
395 brcmf_dbg(TRACE, "Enter\n");
397 skb_queue_walk_safe(skb_list, skb, pnext) {
398 skb_unlink(skb, skb_list);
400 /* Get the protocol, maintain skb around eth_type_trans()
401 * The main reason for this hack is for the limitation of
402 * Linux 2.4 where 'eth_type_trans' uses the
403 * 'net->hard_header_len'
404 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
405 * coping of the packet coming from the network stack to add
406 * BDC, Hardware header etc, during network interface
407 * registration
408 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
409 * required
410 * for BDC, Hardware header etc. and not just the ETH_HLEN
412 eth = skb->data;
413 len = skb->len;
415 ifp = drvr->iflist[ifidx];
416 if (ifp == NULL)
417 ifp = drvr->iflist[0];
419 if (!ifp || !ifp->ndev ||
420 ifp->ndev->reg_state != NETREG_REGISTERED) {
421 brcmu_pkt_buf_free_skb(skb);
422 continue;
425 skb->dev = ifp->ndev;
426 skb->protocol = eth_type_trans(skb, skb->dev);
428 if (skb->pkt_type == PACKET_MULTICAST)
429 bus_if->dstats.multicast++;
431 skb->data = eth;
432 skb->len = len;
434 /* Strip header, count, deliver upward */
435 skb_pull(skb, ETH_HLEN);
437 /* Process special event packets and then discard them */
438 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
439 brcmf_host_event(drvr, &ifidx,
440 skb_mac_header(skb),
441 &event, &data);
443 if (drvr->iflist[ifidx]) {
444 ifp = drvr->iflist[ifidx];
445 ifp->ndev->last_rx = jiffies;
448 bus_if->dstats.rx_bytes += skb->len;
449 bus_if->dstats.rx_packets++; /* Local count */
451 if (in_interrupt())
452 netif_rx(skb);
453 else
454 /* If the receive is not processed inside an ISR,
455 * the softirqd must be woken explicitly to service
456 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
457 * by netif_rx_ni(), but in earlier kernels, we need
458 * to do it manually.
460 netif_rx_ni(skb);
464 void brcmf_txcomplete(struct device *dev, struct sk_buff *txp, bool success)
466 uint ifidx;
467 struct ethhdr *eh;
468 u16 type;
469 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
470 struct brcmf_pub *drvr = bus_if->drvr;
472 brcmf_proto_hdrpull(dev, &ifidx, txp);
474 eh = (struct ethhdr *)(txp->data);
475 type = ntohs(eh->h_proto);
477 if (type == ETH_P_PAE)
478 atomic_dec(&drvr->pend_8021x_cnt);
482 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
484 struct brcmf_if *ifp = netdev_priv(ndev);
485 struct brcmf_bus *bus_if = ifp->drvr->bus_if;
487 brcmf_dbg(TRACE, "Enter\n");
489 /* Copy dongle stats to net device stats */
490 ifp->stats.rx_packets = bus_if->dstats.rx_packets;
491 ifp->stats.tx_packets = bus_if->dstats.tx_packets;
492 ifp->stats.rx_bytes = bus_if->dstats.rx_bytes;
493 ifp->stats.tx_bytes = bus_if->dstats.tx_bytes;
494 ifp->stats.rx_errors = bus_if->dstats.rx_errors;
495 ifp->stats.tx_errors = bus_if->dstats.tx_errors;
496 ifp->stats.rx_dropped = bus_if->dstats.rx_dropped;
497 ifp->stats.tx_dropped = bus_if->dstats.tx_dropped;
498 ifp->stats.multicast = bus_if->dstats.multicast;
500 return &ifp->stats;
503 /* Retrieve current toe component enables, which are kept
504 as a bitmap in toe_ol iovar */
505 static int brcmf_toe_get(struct brcmf_pub *drvr, int ifidx, u32 *toe_ol)
507 struct brcmf_dcmd dcmd;
508 __le32 toe_le;
509 char buf[32];
510 int ret;
512 memset(&dcmd, 0, sizeof(dcmd));
514 dcmd.cmd = BRCMF_C_GET_VAR;
515 dcmd.buf = buf;
516 dcmd.len = (uint) sizeof(buf);
517 dcmd.set = false;
519 strcpy(buf, "toe_ol");
520 ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
521 if (ret < 0) {
522 /* Check for older dongle image that doesn't support toe_ol */
523 if (ret == -EIO) {
524 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
525 brcmf_ifname(drvr, ifidx));
526 return -EOPNOTSUPP;
529 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
530 brcmf_ifname(drvr, ifidx), ret);
531 return ret;
534 memcpy(&toe_le, buf, sizeof(u32));
535 *toe_ol = le32_to_cpu(toe_le);
536 return 0;
539 /* Set current toe component enables in toe_ol iovar,
540 and set toe global enable iovar */
541 static int brcmf_toe_set(struct brcmf_pub *drvr, int ifidx, u32 toe_ol)
543 struct brcmf_dcmd dcmd;
544 char buf[32];
545 int ret;
546 __le32 toe_le = cpu_to_le32(toe_ol);
548 memset(&dcmd, 0, sizeof(dcmd));
550 dcmd.cmd = BRCMF_C_SET_VAR;
551 dcmd.buf = buf;
552 dcmd.len = (uint) sizeof(buf);
553 dcmd.set = true;
555 /* Set toe_ol as requested */
556 strcpy(buf, "toe_ol");
557 memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
559 ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
560 if (ret < 0) {
561 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
562 brcmf_ifname(drvr, ifidx), ret);
563 return ret;
566 /* Enable toe globally only if any components are enabled. */
567 toe_le = cpu_to_le32(toe_ol != 0);
569 strcpy(buf, "toe");
570 memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
572 ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
573 if (ret < 0) {
574 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
575 brcmf_ifname(drvr, ifidx), ret);
576 return ret;
579 return 0;
582 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
583 struct ethtool_drvinfo *info)
585 struct brcmf_if *ifp = netdev_priv(ndev);
586 struct brcmf_pub *drvr = ifp->drvr;
588 sprintf(info->driver, KBUILD_MODNAME);
589 sprintf(info->version, "%lu", drvr->drv_version);
590 sprintf(info->bus_info, "%s", dev_name(drvr->dev));
593 static struct ethtool_ops brcmf_ethtool_ops = {
594 .get_drvinfo = brcmf_ethtool_get_drvinfo
597 static int brcmf_ethtool(struct brcmf_pub *drvr, void __user *uaddr)
599 struct ethtool_drvinfo info;
600 char drvname[sizeof(info.driver)];
601 u32 cmd;
602 struct ethtool_value edata;
603 u32 toe_cmpnt, csum_dir;
604 int ret;
606 brcmf_dbg(TRACE, "Enter\n");
608 /* all ethtool calls start with a cmd word */
609 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
610 return -EFAULT;
612 switch (cmd) {
613 case ETHTOOL_GDRVINFO:
614 /* Copy out any request driver name */
615 if (copy_from_user(&info, uaddr, sizeof(info)))
616 return -EFAULT;
617 strncpy(drvname, info.driver, sizeof(info.driver));
618 drvname[sizeof(info.driver) - 1] = '\0';
620 /* clear struct for return */
621 memset(&info, 0, sizeof(info));
622 info.cmd = cmd;
624 /* if requested, identify ourselves */
625 if (strcmp(drvname, "?dhd") == 0) {
626 sprintf(info.driver, "dhd");
627 strcpy(info.version, BRCMF_VERSION_STR);
630 /* otherwise, require dongle to be up */
631 else if (!drvr->bus_if->drvr_up) {
632 brcmf_dbg(ERROR, "dongle is not up\n");
633 return -ENODEV;
636 /* finally, report dongle driver type */
637 else if (drvr->iswl)
638 sprintf(info.driver, "wl");
639 else
640 sprintf(info.driver, "xx");
642 sprintf(info.version, "%lu", drvr->drv_version);
643 if (copy_to_user(uaddr, &info, sizeof(info)))
644 return -EFAULT;
645 brcmf_dbg(CTL, "given %*s, returning %s\n",
646 (int)sizeof(drvname), drvname, info.driver);
647 break;
649 /* Get toe offload components from dongle */
650 case ETHTOOL_GRXCSUM:
651 case ETHTOOL_GTXCSUM:
652 ret = brcmf_toe_get(drvr, 0, &toe_cmpnt);
653 if (ret < 0)
654 return ret;
656 csum_dir =
657 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
659 edata.cmd = cmd;
660 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
662 if (copy_to_user(uaddr, &edata, sizeof(edata)))
663 return -EFAULT;
664 break;
666 /* Set toe offload components in dongle */
667 case ETHTOOL_SRXCSUM:
668 case ETHTOOL_STXCSUM:
669 if (copy_from_user(&edata, uaddr, sizeof(edata)))
670 return -EFAULT;
672 /* Read the current settings, update and write back */
673 ret = brcmf_toe_get(drvr, 0, &toe_cmpnt);
674 if (ret < 0)
675 return ret;
677 csum_dir =
678 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
680 if (edata.data != 0)
681 toe_cmpnt |= csum_dir;
682 else
683 toe_cmpnt &= ~csum_dir;
685 ret = brcmf_toe_set(drvr, 0, toe_cmpnt);
686 if (ret < 0)
687 return ret;
689 /* If setting TX checksum mode, tell Linux the new mode */
690 if (cmd == ETHTOOL_STXCSUM) {
691 if (edata.data)
692 drvr->iflist[0]->ndev->features |=
693 NETIF_F_IP_CSUM;
694 else
695 drvr->iflist[0]->ndev->features &=
696 ~NETIF_F_IP_CSUM;
699 break;
701 default:
702 return -EOPNOTSUPP;
705 return 0;
708 static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
709 int cmd)
711 struct brcmf_if *ifp = netdev_priv(ndev);
712 struct brcmf_pub *drvr = ifp->drvr;
714 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
716 if (!drvr->iflist[ifp->idx])
717 return -1;
719 if (cmd == SIOCETHTOOL)
720 return brcmf_ethtool(drvr, ifr->ifr_data);
722 return -EOPNOTSUPP;
725 /* called only from within this driver. Sends a command to the dongle. */
726 s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
728 struct brcmf_dcmd dcmd;
729 s32 err = 0;
730 int buflen = 0;
731 bool is_set_key_cmd;
732 struct brcmf_if *ifp = netdev_priv(ndev);
733 struct brcmf_pub *drvr = ifp->drvr;
735 memset(&dcmd, 0, sizeof(dcmd));
736 dcmd.cmd = cmd;
737 dcmd.buf = arg;
738 dcmd.len = len;
740 if (dcmd.buf != NULL)
741 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
743 /* send to dongle (must be up, and wl) */
744 if ((drvr->bus_if->state != BRCMF_BUS_DATA)) {
745 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
746 err = -EIO;
747 goto done;
750 if (!drvr->iswl) {
751 err = -EIO;
752 goto done;
756 * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
757 * set key CMD to prevent M4 encryption.
759 is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
760 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
761 !(strncmp("wsec_key", dcmd.buf, 9))) ||
762 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
763 !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
764 if (is_set_key_cmd)
765 brcmf_netdev_wait_pend8021x(ndev);
767 err = brcmf_proto_dcmd(drvr, ifp->idx, &dcmd, buflen);
769 done:
770 if (err > 0)
771 err = 0;
773 return err;
776 static int brcmf_netdev_stop(struct net_device *ndev)
778 struct brcmf_if *ifp = netdev_priv(ndev);
779 struct brcmf_pub *drvr = ifp->drvr;
781 brcmf_dbg(TRACE, "Enter\n");
782 brcmf_cfg80211_down(drvr->config);
783 if (drvr->bus_if->drvr_up == 0)
784 return 0;
786 /* Set state and stop OS transmissions */
787 drvr->bus_if->drvr_up = false;
788 netif_stop_queue(ndev);
790 return 0;
793 static int brcmf_netdev_open(struct net_device *ndev)
795 struct brcmf_if *ifp = netdev_priv(ndev);
796 struct brcmf_pub *drvr = ifp->drvr;
797 u32 toe_ol;
798 s32 ret = 0;
800 brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
802 if (ifp->idx == 0) { /* do it only for primary eth0 */
803 /* try to bring up bus */
804 ret = brcmf_bus_start(drvr->dev);
805 if (ret != 0) {
806 brcmf_dbg(ERROR, "failed with code %d\n", ret);
807 return -1;
809 atomic_set(&drvr->pend_8021x_cnt, 0);
811 memcpy(ndev->dev_addr, drvr->mac, ETH_ALEN);
813 /* Get current TOE mode from dongle */
814 if (brcmf_toe_get(drvr, ifp->idx, &toe_ol) >= 0
815 && (toe_ol & TOE_TX_CSUM_OL) != 0)
816 drvr->iflist[ifp->idx]->ndev->features |=
817 NETIF_F_IP_CSUM;
818 else
819 drvr->iflist[ifp->idx]->ndev->features &=
820 ~NETIF_F_IP_CSUM;
822 /* Allow transmit calls */
823 netif_start_queue(ndev);
824 drvr->bus_if->drvr_up = true;
825 if (brcmf_cfg80211_up(drvr->config)) {
826 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
827 return -1;
830 return ret;
833 static const struct net_device_ops brcmf_netdev_ops_pri = {
834 .ndo_open = brcmf_netdev_open,
835 .ndo_stop = brcmf_netdev_stop,
836 .ndo_get_stats = brcmf_netdev_get_stats,
837 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
838 .ndo_start_xmit = brcmf_netdev_start_xmit,
839 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
840 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
844 brcmf_add_if(struct device *dev, int ifidx, char *name, u8 *mac_addr)
846 struct brcmf_if *ifp;
847 struct net_device *ndev;
848 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
849 struct brcmf_pub *drvr = bus_if->drvr;
851 brcmf_dbg(TRACE, "idx %d\n", ifidx);
853 ifp = drvr->iflist[ifidx];
855 * Delete the existing interface before overwriting it
856 * in case we missed the BRCMF_E_IF_DEL event.
858 if (ifp) {
859 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
860 ifp->ndev->name);
861 netif_stop_queue(ifp->ndev);
862 unregister_netdev(ifp->ndev);
863 free_netdev(ifp->ndev);
864 drvr->iflist[ifidx] = NULL;
867 /* Allocate netdev, including space for private structure */
868 ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
869 if (!ndev) {
870 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
871 return -ENOMEM;
874 ifp = netdev_priv(ndev);
875 ifp->ndev = ndev;
876 ifp->drvr = drvr;
877 drvr->iflist[ifidx] = ifp;
878 ifp->idx = ifidx;
879 if (mac_addr != NULL)
880 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
882 if (brcmf_net_attach(drvr, ifp->idx)) {
883 brcmf_dbg(ERROR, "brcmf_net_attach failed");
884 free_netdev(ifp->ndev);
885 drvr->iflist[ifidx] = NULL;
886 return -EOPNOTSUPP;
889 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
890 current->pid, ifp->ndev->name);
892 return 0;
895 void brcmf_del_if(struct brcmf_pub *drvr, int ifidx)
897 struct brcmf_if *ifp;
899 brcmf_dbg(TRACE, "idx %d\n", ifidx);
901 ifp = drvr->iflist[ifidx];
902 if (!ifp) {
903 brcmf_dbg(ERROR, "Null interface\n");
904 return;
906 if (ifp->ndev) {
907 if (ifidx == 0) {
908 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
909 rtnl_lock();
910 brcmf_netdev_stop(ifp->ndev);
911 rtnl_unlock();
913 } else {
914 netif_stop_queue(ifp->ndev);
917 unregister_netdev(ifp->ndev);
918 drvr->iflist[ifidx] = NULL;
919 if (ifidx == 0)
920 brcmf_cfg80211_detach(drvr->config);
921 free_netdev(ifp->ndev);
925 int brcmf_attach(uint bus_hdrlen, struct device *dev)
927 struct brcmf_pub *drvr = NULL;
928 int ret = 0;
930 brcmf_dbg(TRACE, "Enter\n");
932 /* Allocate primary brcmf_info */
933 drvr = kzalloc(sizeof(struct brcmf_pub), GFP_ATOMIC);
934 if (!drvr)
935 return -ENOMEM;
937 mutex_init(&drvr->proto_block);
939 /* Link to bus module */
940 drvr->hdrlen = bus_hdrlen;
941 drvr->bus_if = dev_get_drvdata(dev);
942 drvr->bus_if->drvr = drvr;
943 drvr->dev = dev;
945 /* Attach and link in the protocol */
946 ret = brcmf_proto_attach(drvr);
947 if (ret != 0) {
948 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
949 goto fail;
952 INIT_WORK(&drvr->setmacaddr_work, _brcmf_set_mac_address);
953 INIT_WORK(&drvr->multicast_work, _brcmf_set_multicast_list);
955 return ret;
957 fail:
958 brcmf_detach(dev);
960 return ret;
963 int brcmf_bus_start(struct device *dev)
965 int ret = -1;
966 /* Room for "event_msgs" + '\0' + bitvec */
967 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
968 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
969 struct brcmf_pub *drvr = bus_if->drvr;
971 brcmf_dbg(TRACE, "\n");
973 /* Bring up the bus */
974 ret = bus_if->brcmf_bus_init(dev);
975 if (ret != 0) {
976 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
977 return ret;
980 /* If bus is not ready, can't come up */
981 if (bus_if->state != BRCMF_BUS_DATA) {
982 brcmf_dbg(ERROR, "failed bus is not ready\n");
983 return -ENODEV;
986 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
987 iovbuf, sizeof(iovbuf));
988 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
989 sizeof(iovbuf));
990 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
992 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
993 setbit(drvr->eventmask, BRCMF_E_PRUNE);
994 setbit(drvr->eventmask, BRCMF_E_AUTH);
995 setbit(drvr->eventmask, BRCMF_E_REASSOC);
996 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
997 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
998 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
999 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1000 setbit(drvr->eventmask, BRCMF_E_JOIN);
1001 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1002 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1003 setbit(drvr->eventmask, BRCMF_E_LINK);
1004 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1005 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1006 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1007 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1008 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1009 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1011 /* enable dongle roaming event */
1013 drvr->pktfilter_count = 1;
1014 /* Setup filter to allow only unicast */
1015 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1017 /* Bus is ready, do any protocol initialization */
1018 ret = brcmf_proto_init(drvr);
1019 if (ret < 0)
1020 return ret;
1022 return 0;
1025 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1027 struct net_device *ndev;
1028 u8 temp_addr[ETH_ALEN] = {
1029 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1031 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1033 ndev = drvr->iflist[ifidx]->ndev;
1034 ndev->netdev_ops = &brcmf_netdev_ops_pri;
1037 * We have to use the primary MAC for virtual interfaces
1039 if (ifidx != 0) {
1040 /* for virtual interfaces use the primary MAC */
1041 memcpy(temp_addr, drvr->mac, ETH_ALEN);
1045 if (ifidx == 1) {
1046 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1047 /* ACCESSPOINT INTERFACE CASE */
1048 temp_addr[0] |= 0X02; /* set bit 2 ,
1049 - Locally Administered address */
1052 ndev->hard_header_len = ETH_HLEN + drvr->hdrlen;
1053 ndev->ethtool_ops = &brcmf_ethtool_ops;
1055 drvr->rxsz = ndev->mtu + ndev->hard_header_len +
1056 drvr->hdrlen;
1058 memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
1060 /* attach to cfg80211 for primary interface */
1061 if (!ifidx) {
1062 drvr->config = brcmf_cfg80211_attach(ndev, drvr->dev, drvr);
1063 if (drvr->config == NULL) {
1064 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1065 goto fail;
1069 if (register_netdev(ndev) != 0) {
1070 brcmf_dbg(ERROR, "couldn't register the net device\n");
1071 goto fail;
1074 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
1076 return 0;
1078 fail:
1079 ndev->netdev_ops = NULL;
1080 return -EBADE;
1083 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1085 brcmf_dbg(TRACE, "Enter\n");
1087 if (drvr) {
1088 /* Stop the protocol module */
1089 brcmf_proto_stop(drvr);
1091 /* Stop the bus module */
1092 drvr->bus_if->brcmf_bus_stop(drvr->dev);
1096 void brcmf_detach(struct device *dev)
1098 int i;
1099 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1100 struct brcmf_pub *drvr = bus_if->drvr;
1102 brcmf_dbg(TRACE, "Enter\n");
1105 /* make sure primary interface removed last */
1106 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
1107 if (drvr->iflist[i])
1108 brcmf_del_if(drvr, i);
1110 cancel_work_sync(&drvr->setmacaddr_work);
1111 cancel_work_sync(&drvr->multicast_work);
1113 brcmf_bus_detach(drvr);
1115 if (drvr->prot)
1116 brcmf_proto_detach(drvr);
1118 bus_if->drvr = NULL;
1119 kfree(drvr);
1122 static int brcmf_get_pend_8021x_cnt(struct brcmf_pub *drvr)
1124 return atomic_read(&drvr->pend_8021x_cnt);
1127 #define MAX_WAIT_FOR_8021X_TX 10
1129 int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1131 struct brcmf_if *ifp = netdev_priv(ndev);
1132 struct brcmf_pub *drvr = ifp->drvr;
1133 int timeout = 10 * HZ / 1000;
1134 int ntimes = MAX_WAIT_FOR_8021X_TX;
1135 int pend = brcmf_get_pend_8021x_cnt(drvr);
1137 while (ntimes && pend) {
1138 if (pend) {
1139 set_current_state(TASK_INTERRUPTIBLE);
1140 schedule_timeout(timeout);
1141 set_current_state(TASK_RUNNING);
1142 ntimes--;
1144 pend = brcmf_get_pend_8021x_cnt(drvr);
1146 return pend;
1149 #ifdef BCMDBG
1150 int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
1152 int ret = 0;
1153 struct file *fp;
1154 mm_segment_t old_fs;
1155 loff_t pos = 0;
1157 /* change to KERNEL_DS address limit */
1158 old_fs = get_fs();
1159 set_fs(KERNEL_DS);
1161 /* open file to write */
1162 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1163 if (!fp) {
1164 brcmf_dbg(ERROR, "open file error\n");
1165 ret = -1;
1166 goto exit;
1169 /* Write buf to file */
1170 fp->f_op->write(fp, (char __user *)buf, size, &pos);
1172 exit:
1173 /* free buf before return */
1174 kfree(buf);
1175 /* close file before return */
1176 if (fp)
1177 filp_close(fp, current->files);
1178 /* restore previous address limit */
1179 set_fs(old_fs);
1181 return ret;
1183 #endif /* BCMDBG */