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_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 */
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
;
72 if (name
== NULL
|| *name
== '\0')
76 ifp
= drvr
->iflist
[i
];
77 if (ifp
&& !strncmp(ifp
->ndev
->name
, name
, IFNAMSIZ
))
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
);
93 if (drvr
->iflist
[ifidx
] == NULL
) {
94 brcmf_dbg(ERROR
, "null i/f %d\n", ifidx
);
98 if (drvr
->iflist
[ifidx
]->ndev
)
99 return drvr
->iflist
[ifidx
]->ndev
->name
;
104 static void _brcmf_set_multicast_list(struct work_struct
*work
)
106 struct net_device
*ndev
;
107 struct netdev_hw_addr
*ha
;
110 __le32 dcmd_le_value
;
112 struct brcmf_dcmd dcmd
;
117 struct brcmf_pub
*drvr
= container_of(work
, struct brcmf_pub
,
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
);
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
) {
143 memcpy(bufp
, ha
->addr
, ETH_ALEN
);
148 memset(&dcmd
, 0, sizeof(dcmd
));
149 dcmd
.cmd
= BRCMF_C_SET_VAR
;
154 ret
= brcmf_proto_dcmd(drvr
, 0, &dcmd
, dcmd
.len
);
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
;
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
);
173 dcmd_le_value
= cpu_to_le32(dcmd_value
);
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
);
185 memset(&dcmd
, 0, sizeof(dcmd
));
186 dcmd
.cmd
= BRCMF_C_SET_VAR
;
191 ret
= brcmf_proto_dcmd(drvr
, 0, &dcmd
, dcmd
.len
);
193 brcmf_dbg(ERROR
, "%s: set allmulti %d failed\n",
194 brcmf_ifname(drvr
, 0),
195 le32_to_cpu(dcmd_le_value
));
200 /* Finally, pick up the PROMISC flag as well, like the NIC
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
);
212 ret
= brcmf_proto_dcmd(drvr
, 0, &dcmd
, dcmd
.len
);
214 brcmf_dbg(ERROR
, "%s: set promisc %d failed\n",
215 brcmf_ifname(drvr
, 0),
216 le32_to_cpu(dcmd_le_value
));
221 _brcmf_set_mac_address(struct work_struct
*work
)
224 struct brcmf_dcmd dcmd
;
227 struct brcmf_pub
*drvr
= container_of(work
, struct brcmf_pub
,
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));
237 memset(&dcmd
, 0, sizeof(dcmd
));
238 dcmd
.cmd
= BRCMF_C_SET_VAR
;
243 ret
= brcmf_proto_dcmd(drvr
, 0, &dcmd
, dcmd
.len
);
245 brcmf_dbg(ERROR
, "%s: set cur_etheraddr failed\n",
246 brcmf_ifname(drvr
, 0));
248 memcpy(drvr
->iflist
[0]->ndev
->dev_addr
,
249 drvr
->macvalue
, ETH_ALEN
);
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
);
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
)
276 if (!drvr
->bus_if
->drvr_up
|| (drvr
->bus_if
->state
== BRCMF_BUS_DOWN
))
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
)
300 struct brcmf_if
*ifp
= netdev_priv(ndev
);
301 struct brcmf_pub
*drvr
= ifp
->drvr
;
303 brcmf_dbg(TRACE
, "Enter\n");
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
);
315 if (!drvr
->iflist
[ifp
->idx
]) {
316 brcmf_dbg(ERROR
, "bad ifidx %d\n", ifp
->idx
);
317 netif_stop_queue(ndev
);
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
);
332 brcmf_dbg(ERROR
, "%s: skb_realloc_headroom failed\n",
333 brcmf_ifname(drvr
, ifp
->idx
));
339 ret
= brcmf_sendpkt(drvr
, ifp
->idx
, skb
);
343 drvr
->bus_if
->dstats
.tx_dropped
++;
345 drvr
->bus_if
->dstats
.tx_packets
++;
347 /* Return ok: we always eat the packet */
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
;
361 netif_stop_queue(ndev
);
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
,
372 bcmerror
= brcmf_c_host_event(drvr
, ifidx
, pktdata
, event
, data
);
376 if (drvr
->iflist
[*ifidx
]->ndev
)
377 brcmf_cfg80211_event(drvr
->iflist
[*ifidx
]->ndev
,
383 void brcmf_rx_frame(struct device
*dev
, int ifidx
,
384 struct sk_buff_head
*skb_list
)
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
408 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
410 * for BDC, Hardware header etc. and not just the ETH_HLEN
415 ifp
= drvr
->iflist
[ifidx
];
417 ifp
= drvr
->iflist
[0];
419 if (!ifp
|| !ifp
->ndev
||
420 ifp
->ndev
->reg_state
!= NETREG_REGISTERED
) {
421 brcmu_pkt_buf_free_skb(skb
);
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
++;
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
,
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 */
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
464 void brcmf_txcomplete(struct device
*dev
, struct sk_buff
*txp
, bool success
)
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
;
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
;
512 memset(&dcmd
, 0, sizeof(dcmd
));
514 dcmd
.cmd
= BRCMF_C_GET_VAR
;
516 dcmd
.len
= (uint
) sizeof(buf
);
519 strcpy(buf
, "toe_ol");
520 ret
= brcmf_proto_dcmd(drvr
, ifidx
, &dcmd
, dcmd
.len
);
522 /* Check for older dongle image that doesn't support toe_ol */
524 brcmf_dbg(ERROR
, "%s: toe not supported by device\n",
525 brcmf_ifname(drvr
, ifidx
));
529 brcmf_dbg(INFO
, "%s: could not get toe_ol: ret=%d\n",
530 brcmf_ifname(drvr
, ifidx
), ret
);
534 memcpy(&toe_le
, buf
, sizeof(u32
));
535 *toe_ol
= le32_to_cpu(toe_le
);
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
;
546 __le32 toe_le
= cpu_to_le32(toe_ol
);
548 memset(&dcmd
, 0, sizeof(dcmd
));
550 dcmd
.cmd
= BRCMF_C_SET_VAR
;
552 dcmd
.len
= (uint
) sizeof(buf
);
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
);
561 brcmf_dbg(ERROR
, "%s: could not set toe_ol: ret=%d\n",
562 brcmf_ifname(drvr
, ifidx
), ret
);
566 /* Enable toe globally only if any components are enabled. */
567 toe_le
= cpu_to_le32(toe_ol
!= 0);
570 memcpy(&buf
[sizeof("toe")], &toe_le
, sizeof(u32
));
572 ret
= brcmf_proto_dcmd(drvr
, ifidx
, &dcmd
, dcmd
.len
);
574 brcmf_dbg(ERROR
, "%s: could not set toe: ret=%d\n",
575 brcmf_ifname(drvr
, ifidx
), ret
);
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
)];
602 struct ethtool_value edata
;
603 u32 toe_cmpnt
, csum_dir
;
606 brcmf_dbg(TRACE
, "Enter\n");
608 /* all ethtool calls start with a cmd word */
609 if (copy_from_user(&cmd
, uaddr
, sizeof(u32
)))
613 case ETHTOOL_GDRVINFO
:
614 /* Copy out any request driver name */
615 if (copy_from_user(&info
, uaddr
, sizeof(info
)))
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
));
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");
636 /* finally, report dongle driver type */
638 sprintf(info
.driver
, "wl");
640 sprintf(info
.driver
, "xx");
642 sprintf(info
.version
, "%lu", drvr
->drv_version
);
643 if (copy_to_user(uaddr
, &info
, sizeof(info
)))
645 brcmf_dbg(CTL
, "given %*s, returning %s\n",
646 (int)sizeof(drvname
), drvname
, info
.driver
);
649 /* Get toe offload components from dongle */
650 case ETHTOOL_GRXCSUM
:
651 case ETHTOOL_GTXCSUM
:
652 ret
= brcmf_toe_get(drvr
, 0, &toe_cmpnt
);
657 (cmd
== ETHTOOL_GTXCSUM
) ? TOE_TX_CSUM_OL
: TOE_RX_CSUM_OL
;
660 edata
.data
= (toe_cmpnt
& csum_dir
) ? 1 : 0;
662 if (copy_to_user(uaddr
, &edata
, sizeof(edata
)))
666 /* Set toe offload components in dongle */
667 case ETHTOOL_SRXCSUM
:
668 case ETHTOOL_STXCSUM
:
669 if (copy_from_user(&edata
, uaddr
, sizeof(edata
)))
672 /* Read the current settings, update and write back */
673 ret
= brcmf_toe_get(drvr
, 0, &toe_cmpnt
);
678 (cmd
== ETHTOOL_STXCSUM
) ? TOE_TX_CSUM_OL
: TOE_RX_CSUM_OL
;
681 toe_cmpnt
|= csum_dir
;
683 toe_cmpnt
&= ~csum_dir
;
685 ret
= brcmf_toe_set(drvr
, 0, toe_cmpnt
);
689 /* If setting TX checksum mode, tell Linux the new mode */
690 if (cmd
== ETHTOOL_STXCSUM
) {
692 drvr
->iflist
[0]->ndev
->features
|=
695 drvr
->iflist
[0]->ndev
->features
&=
708 static int brcmf_netdev_ioctl_entry(struct net_device
*ndev
, struct ifreq
*ifr
,
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
])
719 if (cmd
== SIOCETHTOOL
)
720 return brcmf_ethtool(drvr
, ifr
->ifr_data
);
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
;
732 struct brcmf_if
*ifp
= netdev_priv(ndev
);
733 struct brcmf_pub
*drvr
= ifp
->drvr
;
735 memset(&dcmd
, 0, sizeof(dcmd
));
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");
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))));
765 brcmf_netdev_wait_pend8021x(ndev
);
767 err
= brcmf_proto_dcmd(drvr
, ifp
->idx
, &dcmd
, buflen
);
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)
786 /* Set state and stop OS transmissions */
787 drvr
->bus_if
->drvr_up
= false;
788 netif_stop_queue(ndev
);
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
;
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
);
806 brcmf_dbg(ERROR
, "failed with code %d\n", ret
);
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
|=
819 drvr
->iflist
[ifp
->idx
]->ndev
->features
&=
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");
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.
859 brcmf_dbg(ERROR
, "ERROR: netdev:%s already exists, try free & unregister\n",
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
);
870 brcmf_dbg(ERROR
, "OOM - alloc_netdev\n");
874 ifp
= netdev_priv(ndev
);
877 drvr
->iflist
[ifidx
] = ifp
;
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
;
889 brcmf_dbg(TRACE
, " ==== pid:%x, net_device for if:%s created ===\n",
890 current
->pid
, ifp
->ndev
->name
);
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
];
903 brcmf_dbg(ERROR
, "Null interface\n");
908 if (ifp
->ndev
->netdev_ops
== &brcmf_netdev_ops_pri
) {
910 brcmf_netdev_stop(ifp
->ndev
);
914 netif_stop_queue(ifp
->ndev
);
917 unregister_netdev(ifp
->ndev
);
918 drvr
->iflist
[ifidx
] = NULL
;
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
;
930 brcmf_dbg(TRACE
, "Enter\n");
932 /* Allocate primary brcmf_info */
933 drvr
= kzalloc(sizeof(struct brcmf_pub
), GFP_ATOMIC
);
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
;
945 /* Attach and link in the protocol */
946 ret
= brcmf_proto_attach(drvr
);
948 brcmf_dbg(ERROR
, "brcmf_prot_attach failed\n");
952 INIT_WORK(&drvr
->setmacaddr_work
, _brcmf_set_mac_address
);
953 INIT_WORK(&drvr
->multicast_work
, _brcmf_set_multicast_list
);
963 int brcmf_bus_start(struct device
*dev
)
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
);
976 brcmf_dbg(ERROR
, "brcmf_sdbrcm_bus_init failed %d\n", 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");
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
,
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
);
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
1040 /* for virtual interfaces use the primary MAC */
1041 memcpy(temp_addr
, drvr
->mac
, ETH_ALEN
);
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
+
1058 memcpy(ndev
->dev_addr
, temp_addr
, ETH_ALEN
);
1060 /* attach to cfg80211 for primary interface */
1062 drvr
->config
= brcmf_cfg80211_attach(ndev
, drvr
->dev
, drvr
);
1063 if (drvr
->config
== NULL
) {
1064 brcmf_dbg(ERROR
, "wl_cfg80211_attach failed\n");
1069 if (register_netdev(ndev
) != 0) {
1070 brcmf_dbg(ERROR
, "couldn't register the net device\n");
1074 brcmf_dbg(INFO
, "%s: Broadcom Dongle Host Driver\n", ndev
->name
);
1079 ndev
->netdev_ops
= NULL
;
1083 static void brcmf_bus_detach(struct brcmf_pub
*drvr
)
1085 brcmf_dbg(TRACE
, "Enter\n");
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
)
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
);
1116 brcmf_proto_detach(drvr
);
1118 bus_if
->drvr
= NULL
;
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
) {
1139 set_current_state(TASK_INTERRUPTIBLE
);
1140 schedule_timeout(timeout
);
1141 set_current_state(TASK_RUNNING
);
1144 pend
= brcmf_get_pend_8021x_cnt(drvr
);
1150 int brcmf_write_to_file(struct brcmf_pub
*drvr
, const u8
*buf
, int size
)
1154 mm_segment_t old_fs
;
1157 /* change to KERNEL_DS address limit */
1161 /* open file to write */
1162 fp
= filp_open("/tmp/mem_dump", O_WRONLY
| O_CREAT
, 0640);
1164 brcmf_dbg(ERROR
, "open file error\n");
1169 /* Write buf to file */
1170 fp
->f_op
->write(fp
, (char __user
*)buf
, size
, &pos
);
1173 /* free buf before return */
1175 /* close file before return */
1177 filp_close(fp
, current
->files
);
1178 /* restore previous address limit */