brcm80211: fmac: move module init/exit to sdio layer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / brcm80211 / brcmfmac / dhd_linux.c
blob73d328027e19785b163fe8cd3e384cf66f316ffc
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_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) */
64 struct brcmf_info {
65 struct brcmf_pub 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;
78 /* Error bits */
79 module_param(brcmf_msg_level, int, 0);
81 int brcmf_ifname2idx(struct brcmf_info *drvr_priv, char *name)
83 int i = BRCMF_MAX_IFS;
84 struct brcmf_if *ifp;
86 if (name == NULL || *name == '\0')
87 return 0;
89 while (--i > 0) {
90 ifp = drvr_priv->iflist[i];
91 if (ifp && !strncmp(ifp->ndev->name, name, IFNAMSIZ))
92 break;
95 brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
97 return i; /* default - the primary interface */
100 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
102 struct brcmf_info *drvr_priv = drvr->info;
104 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
105 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
106 return "<if_bad>";
109 if (drvr_priv->iflist[ifidx] == NULL) {
110 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
111 return "<if_null>";
114 if (drvr_priv->iflist[ifidx]->ndev)
115 return drvr_priv->iflist[ifidx]->ndev->name;
117 return "<if_none>";
120 static void _brcmf_set_multicast_list(struct work_struct *work)
122 struct net_device *ndev;
123 struct netdev_hw_addr *ha;
124 u32 dcmd_value, cnt;
125 __le32 cnt_le;
126 __le32 dcmd_le_value;
128 struct brcmf_dcmd dcmd;
129 char *buf, *bufp;
130 uint buflen;
131 int ret;
133 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
134 multicast_work);
136 ndev = drvr_priv->iflist[0]->ndev;
137 cnt = netdev_mc_count(ndev);
139 /* Determine initial value of allmulti flag */
140 dcmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
142 /* Send down the multicast list first. */
144 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
145 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
146 if (!bufp)
147 return;
149 strcpy(bufp, "mcast_list");
150 bufp += strlen("mcast_list") + 1;
152 cnt_le = cpu_to_le32(cnt);
153 memcpy(bufp, &cnt_le, sizeof(cnt));
154 bufp += sizeof(cnt_le);
156 netdev_for_each_mc_addr(ha, ndev) {
157 if (!cnt)
158 break;
159 memcpy(bufp, ha->addr, ETH_ALEN);
160 bufp += ETH_ALEN;
161 cnt--;
164 memset(&dcmd, 0, sizeof(dcmd));
165 dcmd.cmd = BRCMF_C_SET_VAR;
166 dcmd.buf = buf;
167 dcmd.len = buflen;
168 dcmd.set = true;
170 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
171 if (ret < 0) {
172 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
173 brcmf_ifname(&drvr_priv->pub, 0), cnt);
174 dcmd_value = cnt ? true : dcmd_value;
177 kfree(buf);
179 /* Now send the allmulti setting. This is based on the setting in the
180 * net_device flags, but might be modified above to be turned on if we
181 * were trying to set some addresses and dongle rejected it...
184 buflen = sizeof("allmulti") + sizeof(dcmd_value);
185 buf = kmalloc(buflen, GFP_ATOMIC);
186 if (!buf)
187 return;
189 dcmd_le_value = cpu_to_le32(dcmd_value);
191 if (!brcmf_c_mkiovar
192 ("allmulti", (void *)&dcmd_le_value,
193 sizeof(dcmd_le_value), buf, buflen)) {
194 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
195 brcmf_ifname(&drvr_priv->pub, 0),
196 (int)sizeof(dcmd_value), buflen);
197 kfree(buf);
198 return;
201 memset(&dcmd, 0, sizeof(dcmd));
202 dcmd.cmd = BRCMF_C_SET_VAR;
203 dcmd.buf = buf;
204 dcmd.len = buflen;
205 dcmd.set = true;
207 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
208 if (ret < 0) {
209 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
210 brcmf_ifname(&drvr_priv->pub, 0),
211 le32_to_cpu(dcmd_le_value));
214 kfree(buf);
216 /* Finally, pick up the PROMISC flag as well, like the NIC
217 driver does */
219 dcmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
220 dcmd_le_value = cpu_to_le32(dcmd_value);
222 memset(&dcmd, 0, sizeof(dcmd));
223 dcmd.cmd = BRCMF_C_SET_PROMISC;
224 dcmd.buf = &dcmd_le_value;
225 dcmd.len = sizeof(dcmd_le_value);
226 dcmd.set = true;
228 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
229 if (ret < 0) {
230 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
231 brcmf_ifname(&drvr_priv->pub, 0),
232 le32_to_cpu(dcmd_le_value));
236 static void
237 _brcmf_set_mac_address(struct work_struct *work)
239 char buf[32];
240 struct brcmf_dcmd dcmd;
241 int ret;
243 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
244 setmacaddr_work);
246 brcmf_dbg(TRACE, "enter\n");
247 if (!brcmf_c_mkiovar("cur_etheraddr", (char *)drvr_priv->macvalue,
248 ETH_ALEN, buf, 32)) {
249 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
250 brcmf_ifname(&drvr_priv->pub, 0));
251 return;
253 memset(&dcmd, 0, sizeof(dcmd));
254 dcmd.cmd = BRCMF_C_SET_VAR;
255 dcmd.buf = buf;
256 dcmd.len = 32;
257 dcmd.set = true;
259 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
260 if (ret < 0)
261 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
262 brcmf_ifname(&drvr_priv->pub, 0));
263 else
264 memcpy(drvr_priv->iflist[0]->ndev->dev_addr,
265 drvr_priv->macvalue, ETH_ALEN);
267 return;
270 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
272 struct brcmf_if *ifp = netdev_priv(ndev);
273 struct brcmf_info *drvr_priv = ifp->info;
274 struct sockaddr *sa = (struct sockaddr *)addr;
276 memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
277 schedule_work(&drvr_priv->setmacaddr_work);
278 return 0;
281 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
283 struct brcmf_if *ifp = netdev_priv(ndev);
284 struct brcmf_info *drvr_priv = ifp->info;
286 schedule_work(&drvr_priv->multicast_work);
289 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
291 struct brcmf_info *drvr_priv = drvr->info;
293 /* Reject if down */
294 if (!drvr->up || (drvr->busstate == BRCMF_BUS_DOWN))
295 return -ENODEV;
297 /* Update multicast statistic */
298 if (pktbuf->len >= ETH_ALEN) {
299 u8 *pktdata = (u8 *) (pktbuf->data);
300 struct ethhdr *eh = (struct ethhdr *)pktdata;
302 if (is_multicast_ether_addr(eh->h_dest))
303 drvr->tx_multicast++;
304 if (ntohs(eh->h_proto) == ETH_P_PAE)
305 atomic_inc(&drvr_priv->pend_8021x_cnt);
308 /* If the protocol uses a data header, apply it */
309 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
311 /* Use bus module to send data frame */
312 return brcmf_sdbrcm_bus_txdata(drvr->bus, pktbuf);
315 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
317 int ret;
318 struct brcmf_if *ifp = netdev_priv(ndev);
319 struct brcmf_info *drvr_priv = ifp->info;
321 brcmf_dbg(TRACE, "Enter\n");
323 /* Reject if down */
324 if (!drvr_priv->pub.up || (drvr_priv->pub.busstate == BRCMF_BUS_DOWN)) {
325 brcmf_dbg(ERROR, "xmit rejected pub.up=%d busstate=%d\n",
326 drvr_priv->pub.up, drvr_priv->pub.busstate);
327 netif_stop_queue(ndev);
328 return -ENODEV;
331 if (!drvr_priv->iflist[ifp->idx]) {
332 brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
333 netif_stop_queue(ndev);
334 return -ENODEV;
337 /* Make sure there's enough room for any header */
338 if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
339 struct sk_buff *skb2;
341 brcmf_dbg(INFO, "%s: insufficient headroom\n",
342 brcmf_ifname(&drvr_priv->pub, ifp->idx));
343 drvr_priv->pub.tx_realloc++;
344 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
345 dev_kfree_skb(skb);
346 skb = skb2;
347 if (skb == NULL) {
348 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
349 brcmf_ifname(&drvr_priv->pub, ifp->idx));
350 ret = -ENOMEM;
351 goto done;
355 ret = brcmf_sendpkt(&drvr_priv->pub, ifp->idx, skb);
357 done:
358 if (ret)
359 drvr_priv->pub.dstats.tx_dropped++;
360 else
361 drvr_priv->pub.tx_packets++;
363 /* Return ok: we always eat the packet */
364 return 0;
367 void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
369 struct net_device *ndev;
370 struct brcmf_info *drvr_priv = drvr->info;
372 brcmf_dbg(TRACE, "Enter\n");
374 drvr->txoff = state;
375 ndev = drvr_priv->iflist[ifidx]->ndev;
376 if (state == ON)
377 netif_stop_queue(ndev);
378 else
379 netif_wake_queue(ndev);
382 static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx,
383 void *pktdata, struct brcmf_event_msg *event,
384 void **data)
386 int bcmerror = 0;
388 bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
389 if (bcmerror != 0)
390 return bcmerror;
392 if (drvr_priv->iflist[*ifidx]->ndev)
393 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->ndev,
394 event, *data);
396 return bcmerror;
399 void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx,
400 struct sk_buff_head *skb_list)
402 struct brcmf_info *drvr_priv = drvr->info;
403 unsigned char *eth;
404 uint len;
405 void *data;
406 struct sk_buff *skb, *pnext;
407 struct brcmf_if *ifp;
408 struct brcmf_event_msg event;
410 brcmf_dbg(TRACE, "Enter\n");
412 skb_queue_walk_safe(skb_list, skb, pnext) {
413 skb_unlink(skb, skb_list);
415 /* Get the protocol, maintain skb around eth_type_trans()
416 * The main reason for this hack is for the limitation of
417 * Linux 2.4 where 'eth_type_trans' uses the
418 * 'net->hard_header_len'
419 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
420 * coping of the packet coming from the network stack to add
421 * BDC, Hardware header etc, during network interface
422 * registration
423 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
424 * required
425 * for BDC, Hardware header etc. and not just the ETH_HLEN
427 eth = skb->data;
428 len = skb->len;
430 ifp = drvr_priv->iflist[ifidx];
431 if (ifp == NULL)
432 ifp = drvr_priv->iflist[0];
434 if (!ifp || !ifp->ndev ||
435 ifp->ndev->reg_state != NETREG_REGISTERED) {
436 brcmu_pkt_buf_free_skb(skb);
437 continue;
440 skb->dev = ifp->ndev;
441 skb->protocol = eth_type_trans(skb, skb->dev);
443 if (skb->pkt_type == PACKET_MULTICAST)
444 drvr_priv->pub.rx_multicast++;
446 skb->data = eth;
447 skb->len = len;
449 /* Strip header, count, deliver upward */
450 skb_pull(skb, ETH_HLEN);
452 /* Process special event packets and then discard them */
453 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
454 brcmf_host_event(drvr_priv, &ifidx,
455 skb_mac_header(skb),
456 &event, &data);
458 if (drvr_priv->iflist[ifidx]) {
459 ifp = drvr_priv->iflist[ifidx];
460 ifp->ndev->last_rx = jiffies;
463 drvr->dstats.rx_bytes += skb->len;
464 drvr->rx_packets++; /* Local count */
466 if (in_interrupt())
467 netif_rx(skb);
468 else
469 /* If the receive is not processed inside an ISR,
470 * the softirqd must be woken explicitly to service
471 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
472 * by netif_rx_ni(), but in earlier kernels, we need
473 * to do it manually.
475 netif_rx_ni(skb);
479 void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
481 uint ifidx;
482 struct brcmf_info *drvr_priv = drvr->info;
483 struct ethhdr *eh;
484 u16 type;
486 brcmf_proto_hdrpull(drvr, &ifidx, txp);
488 eh = (struct ethhdr *)(txp->data);
489 type = ntohs(eh->h_proto);
491 if (type == ETH_P_PAE)
492 atomic_dec(&drvr_priv->pend_8021x_cnt);
496 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
498 struct brcmf_if *ifp = netdev_priv(ndev);
499 struct brcmf_info *drvr_priv = ifp->info;
501 brcmf_dbg(TRACE, "Enter\n");
503 if (drvr_priv->pub.up)
504 /* Use the protocol to get dongle stats */
505 brcmf_proto_dstats(&drvr_priv->pub);
507 /* Copy dongle stats to net device stats */
508 ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
509 ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
510 ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
511 ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
512 ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
513 ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
514 ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
515 ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
516 ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
518 return &ifp->stats;
521 /* Retrieve current toe component enables, which are kept
522 as a bitmap in toe_ol iovar */
523 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
525 struct brcmf_dcmd dcmd;
526 __le32 toe_le;
527 char buf[32];
528 int ret;
530 memset(&dcmd, 0, sizeof(dcmd));
532 dcmd.cmd = BRCMF_C_GET_VAR;
533 dcmd.buf = buf;
534 dcmd.len = (uint) sizeof(buf);
535 dcmd.set = false;
537 strcpy(buf, "toe_ol");
538 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
539 if (ret < 0) {
540 /* Check for older dongle image that doesn't support toe_ol */
541 if (ret == -EIO) {
542 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
543 brcmf_ifname(&drvr_priv->pub, ifidx));
544 return -EOPNOTSUPP;
547 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
548 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
549 return ret;
552 memcpy(&toe_le, buf, sizeof(u32));
553 *toe_ol = le32_to_cpu(toe_le);
554 return 0;
557 /* Set current toe component enables in toe_ol iovar,
558 and set toe global enable iovar */
559 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
561 struct brcmf_dcmd dcmd;
562 char buf[32];
563 int ret;
564 __le32 toe_le = cpu_to_le32(toe_ol);
566 memset(&dcmd, 0, sizeof(dcmd));
568 dcmd.cmd = BRCMF_C_SET_VAR;
569 dcmd.buf = buf;
570 dcmd.len = (uint) sizeof(buf);
571 dcmd.set = true;
573 /* Set toe_ol as requested */
574 strcpy(buf, "toe_ol");
575 memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
577 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
578 if (ret < 0) {
579 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
580 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
581 return ret;
584 /* Enable toe globally only if any components are enabled. */
585 toe_le = cpu_to_le32(toe_ol != 0);
587 strcpy(buf, "toe");
588 memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
590 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
591 if (ret < 0) {
592 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
593 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
594 return ret;
597 return 0;
600 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
601 struct ethtool_drvinfo *info)
603 struct brcmf_if *ifp = netdev_priv(ndev);
604 struct brcmf_info *drvr_priv = ifp->info;
606 sprintf(info->driver, KBUILD_MODNAME);
607 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
608 sprintf(info->bus_info, "%s",
609 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
612 static struct ethtool_ops brcmf_ethtool_ops = {
613 .get_drvinfo = brcmf_ethtool_get_drvinfo
616 static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
618 struct ethtool_drvinfo info;
619 char drvname[sizeof(info.driver)];
620 u32 cmd;
621 struct ethtool_value edata;
622 u32 toe_cmpnt, csum_dir;
623 int ret;
625 brcmf_dbg(TRACE, "Enter\n");
627 /* all ethtool calls start with a cmd word */
628 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
629 return -EFAULT;
631 switch (cmd) {
632 case ETHTOOL_GDRVINFO:
633 /* Copy out any request driver name */
634 if (copy_from_user(&info, uaddr, sizeof(info)))
635 return -EFAULT;
636 strncpy(drvname, info.driver, sizeof(info.driver));
637 drvname[sizeof(info.driver) - 1] = '\0';
639 /* clear struct for return */
640 memset(&info, 0, sizeof(info));
641 info.cmd = cmd;
643 /* if requested, identify ourselves */
644 if (strcmp(drvname, "?dhd") == 0) {
645 sprintf(info.driver, "dhd");
646 strcpy(info.version, BRCMF_VERSION_STR);
649 /* otherwise, require dongle to be up */
650 else if (!drvr_priv->pub.up) {
651 brcmf_dbg(ERROR, "dongle is not up\n");
652 return -ENODEV;
655 /* finally, report dongle driver type */
656 else if (drvr_priv->pub.iswl)
657 sprintf(info.driver, "wl");
658 else
659 sprintf(info.driver, "xx");
661 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
662 if (copy_to_user(uaddr, &info, sizeof(info)))
663 return -EFAULT;
664 brcmf_dbg(CTL, "given %*s, returning %s\n",
665 (int)sizeof(drvname), drvname, info.driver);
666 break;
668 /* Get toe offload components from dongle */
669 case ETHTOOL_GRXCSUM:
670 case ETHTOOL_GTXCSUM:
671 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
672 if (ret < 0)
673 return ret;
675 csum_dir =
676 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
678 edata.cmd = cmd;
679 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
681 if (copy_to_user(uaddr, &edata, sizeof(edata)))
682 return -EFAULT;
683 break;
685 /* Set toe offload components in dongle */
686 case ETHTOOL_SRXCSUM:
687 case ETHTOOL_STXCSUM:
688 if (copy_from_user(&edata, uaddr, sizeof(edata)))
689 return -EFAULT;
691 /* Read the current settings, update and write back */
692 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
693 if (ret < 0)
694 return ret;
696 csum_dir =
697 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
699 if (edata.data != 0)
700 toe_cmpnt |= csum_dir;
701 else
702 toe_cmpnt &= ~csum_dir;
704 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
705 if (ret < 0)
706 return ret;
708 /* If setting TX checksum mode, tell Linux the new mode */
709 if (cmd == ETHTOOL_STXCSUM) {
710 if (edata.data)
711 drvr_priv->iflist[0]->ndev->features |=
712 NETIF_F_IP_CSUM;
713 else
714 drvr_priv->iflist[0]->ndev->features &=
715 ~NETIF_F_IP_CSUM;
718 break;
720 default:
721 return -EOPNOTSUPP;
724 return 0;
727 static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
728 int cmd)
730 struct brcmf_if *ifp = netdev_priv(ndev);
731 struct brcmf_info *drvr_priv = ifp->info;
733 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
735 if (!drvr_priv->iflist[ifp->idx])
736 return -1;
738 if (cmd == SIOCETHTOOL)
739 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
741 return -EOPNOTSUPP;
744 /* called only from within this driver. Sends a command to the dongle. */
745 s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
747 struct brcmf_dcmd dcmd;
748 s32 err = 0;
749 int buflen = 0;
750 bool is_set_key_cmd;
751 struct brcmf_if *ifp = netdev_priv(ndev);
752 struct brcmf_info *drvr_priv = ifp->info;
754 memset(&dcmd, 0, sizeof(dcmd));
755 dcmd.cmd = cmd;
756 dcmd.buf = arg;
757 dcmd.len = len;
759 if (dcmd.buf != NULL)
760 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
762 /* send to dongle (must be up, and wl) */
763 if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
764 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
765 err = -EIO;
766 goto done;
769 if (!drvr_priv->pub.iswl) {
770 err = -EIO;
771 goto done;
775 * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
776 * set key CMD to prevent M4 encryption.
778 is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
779 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
780 !(strncmp("wsec_key", dcmd.buf, 9))) ||
781 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
782 !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
783 if (is_set_key_cmd)
784 brcmf_netdev_wait_pend8021x(ndev);
786 err = brcmf_proto_dcmd(&drvr_priv->pub, ifp->idx, &dcmd, buflen);
788 done:
789 if (err > 0)
790 err = 0;
792 return err;
795 static int brcmf_netdev_stop(struct net_device *ndev)
797 struct brcmf_if *ifp = netdev_priv(ndev);
798 struct brcmf_pub *drvr = &ifp->info->pub;
800 brcmf_dbg(TRACE, "Enter\n");
801 brcmf_cfg80211_down(drvr->config);
802 if (drvr->up == 0)
803 return 0;
805 /* Set state and stop OS transmissions */
806 drvr->up = 0;
807 netif_stop_queue(ndev);
809 return 0;
812 static int brcmf_netdev_open(struct net_device *ndev)
814 struct brcmf_if *ifp = netdev_priv(ndev);
815 struct brcmf_info *drvr_priv = ifp->info;
816 u32 toe_ol;
817 s32 ret = 0;
819 brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
821 if (ifp->idx == 0) { /* do it only for primary eth0 */
822 /* try to bring up bus */
823 ret = brcmf_bus_start(&drvr_priv->pub);
824 if (ret != 0) {
825 brcmf_dbg(ERROR, "failed with code %d\n", ret);
826 return -1;
828 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
830 memcpy(ndev->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
832 /* Get current TOE mode from dongle */
833 if (brcmf_toe_get(drvr_priv, ifp->idx, &toe_ol) >= 0
834 && (toe_ol & TOE_TX_CSUM_OL) != 0)
835 drvr_priv->iflist[ifp->idx]->ndev->features |=
836 NETIF_F_IP_CSUM;
837 else
838 drvr_priv->iflist[ifp->idx]->ndev->features &=
839 ~NETIF_F_IP_CSUM;
841 /* Allow transmit calls */
842 netif_start_queue(ndev);
843 drvr_priv->pub.up = 1;
844 if (brcmf_cfg80211_up(drvr_priv->pub.config)) {
845 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
846 return -1;
849 return ret;
852 static const struct net_device_ops brcmf_netdev_ops_pri = {
853 .ndo_open = brcmf_netdev_open,
854 .ndo_stop = brcmf_netdev_stop,
855 .ndo_get_stats = brcmf_netdev_get_stats,
856 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
857 .ndo_start_xmit = brcmf_netdev_start_xmit,
858 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
859 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
863 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, char *name, u8 *mac_addr)
865 struct brcmf_if *ifp;
866 struct net_device *ndev;
868 brcmf_dbg(TRACE, "idx %d\n", ifidx);
870 ifp = drvr_priv->iflist[ifidx];
872 * Delete the existing interface before overwriting it
873 * in case we missed the BRCMF_E_IF_DEL event.
875 if (ifp) {
876 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
877 ifp->ndev->name);
878 netif_stop_queue(ifp->ndev);
879 unregister_netdev(ifp->ndev);
880 free_netdev(ifp->ndev);
881 drvr_priv->iflist[ifidx] = NULL;
884 /* Allocate netdev, including space for private structure */
885 ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
886 if (!ndev) {
887 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
888 return -ENOMEM;
891 ifp = netdev_priv(ndev);
892 ifp->ndev = ndev;
893 ifp->info = drvr_priv;
894 drvr_priv->iflist[ifidx] = ifp;
895 ifp->idx = ifidx;
896 if (mac_addr != NULL)
897 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
899 if (brcmf_net_attach(&drvr_priv->pub, ifp->idx)) {
900 brcmf_dbg(ERROR, "brcmf_net_attach failed");
901 free_netdev(ifp->ndev);
902 drvr_priv->iflist[ifidx] = NULL;
903 return -EOPNOTSUPP;
906 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
907 current->pid, ifp->ndev->name);
909 return 0;
912 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
914 struct brcmf_if *ifp;
916 brcmf_dbg(TRACE, "idx %d\n", ifidx);
918 ifp = drvr_priv->iflist[ifidx];
919 if (!ifp) {
920 brcmf_dbg(ERROR, "Null interface\n");
921 return;
923 if (ifp->ndev) {
924 if (ifidx == 0) {
925 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
926 rtnl_lock();
927 brcmf_netdev_stop(ifp->ndev);
928 rtnl_unlock();
930 } else {
931 netif_stop_queue(ifp->ndev);
934 unregister_netdev(ifp->ndev);
935 drvr_priv->iflist[ifidx] = NULL;
936 if (ifidx == 0)
937 brcmf_cfg80211_detach(drvr_priv->pub.config);
938 free_netdev(ifp->ndev);
942 struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
944 struct brcmf_info *drvr_priv = NULL;
946 brcmf_dbg(TRACE, "Enter\n");
948 /* Allocate primary brcmf_info */
949 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
950 if (!drvr_priv)
951 goto fail;
953 mutex_init(&drvr_priv->proto_block);
955 /* Link to info module */
956 drvr_priv->pub.info = drvr_priv;
958 /* Link to bus module */
959 drvr_priv->pub.bus = bus;
960 drvr_priv->pub.hdrlen = bus_hdrlen;
962 /* Attach and link in the protocol */
963 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
964 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
965 goto fail;
968 INIT_WORK(&drvr_priv->setmacaddr_work, _brcmf_set_mac_address);
969 INIT_WORK(&drvr_priv->multicast_work, _brcmf_set_multicast_list);
971 return &drvr_priv->pub;
973 fail:
974 if (drvr_priv)
975 brcmf_detach(&drvr_priv->pub);
977 return NULL;
980 int brcmf_bus_start(struct brcmf_pub *drvr)
982 int ret = -1;
983 struct brcmf_info *drvr_priv = drvr->info;
984 /* Room for "event_msgs" + '\0' + bitvec */
985 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
987 brcmf_dbg(TRACE, "\n");
989 /* Bring up the bus */
990 ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub);
991 if (ret != 0) {
992 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
993 return ret;
996 /* If bus is not ready, can't come up */
997 if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
998 brcmf_dbg(ERROR, "failed bus is not ready\n");
999 return -ENODEV;
1002 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1003 iovbuf, sizeof(iovbuf));
1004 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1005 sizeof(iovbuf));
1006 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1008 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1009 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1010 setbit(drvr->eventmask, BRCMF_E_AUTH);
1011 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1012 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1013 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1014 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1015 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1016 setbit(drvr->eventmask, BRCMF_E_JOIN);
1017 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1018 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1019 setbit(drvr->eventmask, BRCMF_E_LINK);
1020 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1021 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1022 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1023 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1024 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1025 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1027 /* enable dongle roaming event */
1029 drvr->pktfilter_count = 1;
1030 /* Setup filter to allow only unicast */
1031 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1033 /* Bus is ready, do any protocol initialization */
1034 ret = brcmf_proto_init(&drvr_priv->pub);
1035 if (ret < 0)
1036 return ret;
1038 return 0;
1041 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1043 struct brcmf_info *drvr_priv = drvr->info;
1044 struct net_device *ndev;
1045 u8 temp_addr[ETH_ALEN] = {
1046 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1048 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1050 ndev = drvr_priv->iflist[ifidx]->ndev;
1051 ndev->netdev_ops = &brcmf_netdev_ops_pri;
1054 * We have to use the primary MAC for virtual interfaces
1056 if (ifidx != 0) {
1057 /* for virtual interfaces use the primary MAC */
1058 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1062 if (ifidx == 1) {
1063 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1064 /* ACCESSPOINT INTERFACE CASE */
1065 temp_addr[0] |= 0X02; /* set bit 2 ,
1066 - Locally Administered address */
1069 ndev->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1070 ndev->ethtool_ops = &brcmf_ethtool_ops;
1072 drvr_priv->pub.rxsz = ndev->mtu + ndev->hard_header_len +
1073 drvr_priv->pub.hdrlen;
1075 memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
1077 /* attach to cfg80211 for primary interface */
1078 if (!ifidx) {
1079 drvr->config =
1080 brcmf_cfg80211_attach(ndev,
1081 brcmf_bus_get_device(drvr->bus),
1082 drvr);
1083 if (drvr->config == NULL) {
1084 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1085 goto fail;
1089 if (register_netdev(ndev) != 0) {
1090 brcmf_dbg(ERROR, "couldn't register the net device\n");
1091 goto fail;
1094 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
1096 return 0;
1098 fail:
1099 ndev->netdev_ops = NULL;
1100 return -EBADE;
1103 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1105 struct brcmf_info *drvr_priv;
1107 brcmf_dbg(TRACE, "Enter\n");
1109 if (drvr) {
1110 drvr_priv = drvr->info;
1111 if (drvr_priv) {
1112 /* Stop the protocol module */
1113 brcmf_proto_stop(&drvr_priv->pub);
1115 /* Stop the bus module */
1116 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus);
1121 void brcmf_detach(struct brcmf_pub *drvr)
1123 struct brcmf_info *drvr_priv;
1125 brcmf_dbg(TRACE, "Enter\n");
1127 if (drvr) {
1128 drvr_priv = drvr->info;
1129 if (drvr_priv) {
1130 int i;
1132 /* make sure primary interface removed last */
1133 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
1134 if (drvr_priv->iflist[i])
1135 brcmf_del_if(drvr_priv, i);
1137 cancel_work_sync(&drvr_priv->setmacaddr_work);
1138 cancel_work_sync(&drvr_priv->multicast_work);
1140 brcmf_bus_detach(drvr);
1142 if (drvr->prot)
1143 brcmf_proto_detach(drvr);
1145 kfree(drvr_priv);
1150 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1152 struct brcmf_info *drvr_priv = drvr->info;
1154 if (drvr_priv) {
1155 mutex_lock(&drvr_priv->proto_block);
1156 return 1;
1158 return 0;
1161 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1163 struct brcmf_info *drvr_priv = drvr->info;
1165 if (drvr_priv) {
1166 mutex_unlock(&drvr_priv->proto_block);
1167 return 1;
1170 return 0;
1173 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1175 return atomic_read(&drvr_priv->pend_8021x_cnt);
1178 #define MAX_WAIT_FOR_8021X_TX 10
1180 int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1182 struct brcmf_if *ifp = netdev_priv(ndev);
1183 struct brcmf_info *drvr_priv = ifp->info;
1184 int timeout = 10 * HZ / 1000;
1185 int ntimes = MAX_WAIT_FOR_8021X_TX;
1186 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1188 while (ntimes && pend) {
1189 if (pend) {
1190 set_current_state(TASK_INTERRUPTIBLE);
1191 schedule_timeout(timeout);
1192 set_current_state(TASK_RUNNING);
1193 ntimes--;
1195 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1197 return pend;
1200 #ifdef BCMDBG
1201 int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
1203 int ret = 0;
1204 struct file *fp;
1205 mm_segment_t old_fs;
1206 loff_t pos = 0;
1208 /* change to KERNEL_DS address limit */
1209 old_fs = get_fs();
1210 set_fs(KERNEL_DS);
1212 /* open file to write */
1213 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1214 if (!fp) {
1215 brcmf_dbg(ERROR, "open file error\n");
1216 ret = -1;
1217 goto exit;
1220 /* Write buf to file */
1221 fp->f_op->write(fp, (char __user *)buf, size, &pos);
1223 exit:
1224 /* free buf before return */
1225 kfree(buf);
1226 /* close file before return */
1227 if (fp)
1228 filp_close(fp, current->files);
1229 /* restore previous address limit */
1230 set_fs(old_fs);
1232 return ret;
1234 #endif /* BCMDBG */