blob: 2c3a99d6c9a6d40981e2fb369ad54f2f336dd411 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
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.
7 *
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.
15 */
16
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>
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020034#include <linux/module.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020035#include <net/cfg80211.h>
36#include <net/rtnetlink.h>
37#include <defs.h>
38#include <brcmu_utils.h>
39#include <brcmu_wifi.h>
40
41#include "dhd.h"
42#include "dhd_bus.h"
43#include "dhd_proto.h"
44#include "dhd_dbg.h"
45#include "wl_cfg80211.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020046
47MODULE_AUTHOR("Broadcom Corporation");
48MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
49MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
50MODULE_LICENSE("Dual BSD/GPL");
51
52
53/* Interface control information */
54struct 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 */
Arend van Spriel5b435de2011-10-05 13:19:03 +020060 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
61};
62
63/* Local private structure (extension of pub) */
64struct brcmf_info {
65 struct brcmf_pub pub;
66
67 /* OS/stack specifics */
68 struct brcmf_if *iflist[BRCMF_MAX_IFS];
69
70 struct mutex proto_block;
71
72 struct work_struct setmacaddr_work;
73 struct work_struct multicast_work;
74 u8 macvalue[ETH_ALEN];
75 atomic_t pend_8021x_cnt;
76};
77
78/* Error bits */
Franky Lin4f96bf12011-11-22 17:21:48 -080079int brcmf_msg_level = BRCMF_ERROR_VAL;
Arend van Spriel5b435de2011-10-05 13:19:03 +020080module_param(brcmf_msg_level, int, 0);
81
Arend van Spriel5b435de2011-10-05 13:19:03 +020082int brcmf_ifname2idx(struct brcmf_info *drvr_priv, char *name)
83{
84 int i = BRCMF_MAX_IFS;
85 struct brcmf_if *ifp;
86
87 if (name == NULL || *name == '\0')
88 return 0;
89
90 while (--i > 0) {
91 ifp = drvr_priv->iflist[i];
92 if (ifp && !strncmp(ifp->ndev->name, name, IFNAMSIZ))
93 break;
94 }
95
96 brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
97
98 return i; /* default - the primary interface */
99}
100
101char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
102{
103 struct brcmf_info *drvr_priv = drvr->info;
104
105 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
106 brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
107 return "<if_bad>";
108 }
109
110 if (drvr_priv->iflist[ifidx] == NULL) {
111 brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
112 return "<if_null>";
113 }
114
115 if (drvr_priv->iflist[ifidx]->ndev)
116 return drvr_priv->iflist[ifidx]->ndev->name;
117
118 return "<if_none>";
119}
120
121static void _brcmf_set_multicast_list(struct work_struct *work)
122{
123 struct net_device *ndev;
124 struct netdev_hw_addr *ha;
Arend van Sprielb5036242011-10-12 20:51:17 +0200125 u32 dcmd_value, cnt;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200126 __le32 cnt_le;
Arend van Sprielb5036242011-10-12 20:51:17 +0200127 __le32 dcmd_le_value;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200128
129 struct brcmf_dcmd dcmd;
130 char *buf, *bufp;
131 uint buflen;
132 int ret;
133
134 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
135 multicast_work);
136
137 ndev = drvr_priv->iflist[0]->ndev;
138 cnt = netdev_mc_count(ndev);
139
140 /* Determine initial value of allmulti flag */
Arend van Sprielb5036242011-10-12 20:51:17 +0200141 dcmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200142
143 /* Send down the multicast list first. */
144
145 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
146 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
147 if (!bufp)
148 return;
149
150 strcpy(bufp, "mcast_list");
151 bufp += strlen("mcast_list") + 1;
152
153 cnt_le = cpu_to_le32(cnt);
154 memcpy(bufp, &cnt_le, sizeof(cnt));
155 bufp += sizeof(cnt_le);
156
157 netdev_for_each_mc_addr(ha, ndev) {
158 if (!cnt)
159 break;
160 memcpy(bufp, ha->addr, ETH_ALEN);
161 bufp += ETH_ALEN;
162 cnt--;
163 }
164
165 memset(&dcmd, 0, sizeof(dcmd));
166 dcmd.cmd = BRCMF_C_SET_VAR;
167 dcmd.buf = buf;
168 dcmd.len = buflen;
169 dcmd.set = true;
170
171 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
172 if (ret < 0) {
173 brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
174 brcmf_ifname(&drvr_priv->pub, 0), cnt);
Arend van Sprielb5036242011-10-12 20:51:17 +0200175 dcmd_value = cnt ? true : dcmd_value;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200176 }
177
178 kfree(buf);
179
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...
183 */
184
Arend van Sprielb5036242011-10-12 20:51:17 +0200185 buflen = sizeof("allmulti") + sizeof(dcmd_value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200186 buf = kmalloc(buflen, GFP_ATOMIC);
187 if (!buf)
188 return;
189
Arend van Sprielb5036242011-10-12 20:51:17 +0200190 dcmd_le_value = cpu_to_le32(dcmd_value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200191
Alwin Beukers53a22772011-10-12 20:51:30 +0200192 if (!brcmf_c_mkiovar
Arend van Sprielb5036242011-10-12 20:51:17 +0200193 ("allmulti", (void *)&dcmd_le_value,
194 sizeof(dcmd_le_value), buf, buflen)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200195 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
196 brcmf_ifname(&drvr_priv->pub, 0),
Arend van Sprielb5036242011-10-12 20:51:17 +0200197 (int)sizeof(dcmd_value), buflen);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200198 kfree(buf);
199 return;
200 }
201
202 memset(&dcmd, 0, sizeof(dcmd));
203 dcmd.cmd = BRCMF_C_SET_VAR;
204 dcmd.buf = buf;
205 dcmd.len = buflen;
206 dcmd.set = true;
207
208 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
209 if (ret < 0) {
210 brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
211 brcmf_ifname(&drvr_priv->pub, 0),
Arend van Sprielb5036242011-10-12 20:51:17 +0200212 le32_to_cpu(dcmd_le_value));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200213 }
214
215 kfree(buf);
216
217 /* Finally, pick up the PROMISC flag as well, like the NIC
218 driver does */
219
Arend van Sprielb5036242011-10-12 20:51:17 +0200220 dcmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
221 dcmd_le_value = cpu_to_le32(dcmd_value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200222
223 memset(&dcmd, 0, sizeof(dcmd));
224 dcmd.cmd = BRCMF_C_SET_PROMISC;
Arend van Sprielb5036242011-10-12 20:51:17 +0200225 dcmd.buf = &dcmd_le_value;
226 dcmd.len = sizeof(dcmd_le_value);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200227 dcmd.set = true;
228
229 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
230 if (ret < 0) {
231 brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
232 brcmf_ifname(&drvr_priv->pub, 0),
Arend van Sprielb5036242011-10-12 20:51:17 +0200233 le32_to_cpu(dcmd_le_value));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200234 }
235}
236
237static void
238_brcmf_set_mac_address(struct work_struct *work)
239{
240 char buf[32];
241 struct brcmf_dcmd dcmd;
242 int ret;
243
244 struct brcmf_info *drvr_priv = container_of(work, struct brcmf_info,
245 setmacaddr_work);
246
247 brcmf_dbg(TRACE, "enter\n");
Alwin Beukers53a22772011-10-12 20:51:30 +0200248 if (!brcmf_c_mkiovar("cur_etheraddr", (char *)drvr_priv->macvalue,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200249 ETH_ALEN, buf, 32)) {
250 brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
251 brcmf_ifname(&drvr_priv->pub, 0));
252 return;
253 }
254 memset(&dcmd, 0, sizeof(dcmd));
255 dcmd.cmd = BRCMF_C_SET_VAR;
256 dcmd.buf = buf;
257 dcmd.len = 32;
258 dcmd.set = true;
259
260 ret = brcmf_proto_dcmd(&drvr_priv->pub, 0, &dcmd, dcmd.len);
261 if (ret < 0)
262 brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
263 brcmf_ifname(&drvr_priv->pub, 0));
264 else
265 memcpy(drvr_priv->iflist[0]->ndev->dev_addr,
266 drvr_priv->macvalue, ETH_ALEN);
267
268 return;
269}
270
271static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
272{
Franky Line1b83582011-10-21 16:16:33 +0200273 struct brcmf_if *ifp = netdev_priv(ndev);
274 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200275 struct sockaddr *sa = (struct sockaddr *)addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200276
277 memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
278 schedule_work(&drvr_priv->setmacaddr_work);
279 return 0;
280}
281
282static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
283{
Franky Line1b83582011-10-21 16:16:33 +0200284 struct brcmf_if *ifp = netdev_priv(ndev);
285 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200286
287 schedule_work(&drvr_priv->multicast_work);
288}
289
290int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
291{
292 struct brcmf_info *drvr_priv = drvr->info;
293
294 /* Reject if down */
Franky Lin8d169aa2011-11-22 17:21:52 -0800295 if (!drvr->up || (drvr->bus_if->state == BRCMF_BUS_DOWN))
Arend van Spriel5b435de2011-10-05 13:19:03 +0200296 return -ENODEV;
297
298 /* Update multicast statistic */
299 if (pktbuf->len >= ETH_ALEN) {
300 u8 *pktdata = (u8 *) (pktbuf->data);
301 struct ethhdr *eh = (struct ethhdr *)pktdata;
302
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);
307 }
308
309 /* If the protocol uses a data header, apply it */
310 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
311
312 /* Use bus module to send data frame */
Franky Linbf347bb2011-11-22 17:21:56 -0800313 return brcmf_sdbrcm_bus_txdata(drvr->dev, pktbuf);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200314}
315
316static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
317{
318 int ret;
Franky Line1b83582011-10-21 16:16:33 +0200319 struct brcmf_if *ifp = netdev_priv(ndev);
320 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200321
322 brcmf_dbg(TRACE, "Enter\n");
323
324 /* Reject if down */
Franky Lin8d169aa2011-11-22 17:21:52 -0800325 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",
328 drvr_priv->pub.up,
329 drvr_priv->pub.bus_if->state);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200330 netif_stop_queue(ndev);
331 return -ENODEV;
332 }
333
Franky Line1b83582011-10-21 16:16:33 +0200334 if (!drvr_priv->iflist[ifp->idx]) {
335 brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200336 netif_stop_queue(ndev);
337 return -ENODEV;
338 }
339
340 /* Make sure there's enough room for any header */
341 if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
342 struct sk_buff *skb2;
343
344 brcmf_dbg(INFO, "%s: insufficient headroom\n",
Franky Line1b83582011-10-21 16:16:33 +0200345 brcmf_ifname(&drvr_priv->pub, ifp->idx));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200346 drvr_priv->pub.tx_realloc++;
347 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
348 dev_kfree_skb(skb);
349 skb = skb2;
350 if (skb == NULL) {
351 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
Franky Line1b83582011-10-21 16:16:33 +0200352 brcmf_ifname(&drvr_priv->pub, ifp->idx));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200353 ret = -ENOMEM;
354 goto done;
355 }
356 }
357
Franky Line1b83582011-10-21 16:16:33 +0200358 ret = brcmf_sendpkt(&drvr_priv->pub, ifp->idx, skb);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200359
360done:
361 if (ret)
362 drvr_priv->pub.dstats.tx_dropped++;
363 else
364 drvr_priv->pub.tx_packets++;
365
366 /* Return ok: we always eat the packet */
367 return 0;
368}
369
370void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
371{
372 struct net_device *ndev;
373 struct brcmf_info *drvr_priv = drvr->info;
374
375 brcmf_dbg(TRACE, "Enter\n");
376
377 drvr->txoff = state;
378 ndev = drvr_priv->iflist[ifidx]->ndev;
379 if (state == ON)
380 netif_stop_queue(ndev);
381 else
382 netif_wake_queue(ndev);
383}
384
385static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx,
386 void *pktdata, struct brcmf_event_msg *event,
387 void **data)
388{
389 int bcmerror = 0;
390
391 bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
392 if (bcmerror != 0)
393 return bcmerror;
394
395 if (drvr_priv->iflist[*ifidx]->ndev)
396 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->ndev,
397 event, *data);
398
399 return bcmerror;
400}
401
Arend van Spriel0b45bf72011-11-22 17:21:36 -0800402void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx,
403 struct sk_buff_head *skb_list)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200404{
405 struct brcmf_info *drvr_priv = drvr->info;
406 unsigned char *eth;
407 uint len;
408 void *data;
Arend van Spriel0b45bf72011-11-22 17:21:36 -0800409 struct sk_buff *skb, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200410 struct brcmf_if *ifp;
411 struct brcmf_event_msg event;
412
413 brcmf_dbg(TRACE, "Enter\n");
414
Arend van Spriel0b45bf72011-11-22 17:21:36 -0800415 skb_queue_walk_safe(skb_list, skb, pnext) {
416 skb_unlink(skb, skb_list);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200417
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
425 * registration
426 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
427 * required
428 * for BDC, Hardware header etc. and not just the ETH_HLEN
429 */
430 eth = skb->data;
431 len = skb->len;
432
433 ifp = drvr_priv->iflist[ifidx];
434 if (ifp == NULL)
435 ifp = drvr_priv->iflist[0];
436
Franky Lin0c094c72011-11-22 17:21:46 -0800437 if (!ifp || !ifp->ndev ||
438 ifp->ndev->reg_state != NETREG_REGISTERED) {
439 brcmu_pkt_buf_free_skb(skb);
440 continue;
441 }
442
Arend van Spriel5b435de2011-10-05 13:19:03 +0200443 skb->dev = ifp->ndev;
444 skb->protocol = eth_type_trans(skb, skb->dev);
445
446 if (skb->pkt_type == PACKET_MULTICAST)
447 drvr_priv->pub.rx_multicast++;
448
449 skb->data = eth;
450 skb->len = len;
451
452 /* Strip header, count, deliver upward */
453 skb_pull(skb, ETH_HLEN);
454
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,
458 skb_mac_header(skb),
459 &event, &data);
460
Franky Lind1a5b6f2011-10-21 16:16:34 +0200461 if (drvr_priv->iflist[ifidx]) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200462 ifp = drvr_priv->iflist[ifidx];
Arend van Spriel5b435de2011-10-05 13:19:03 +0200463 ifp->ndev->last_rx = jiffies;
Franky Lind1a5b6f2011-10-21 16:16:34 +0200464 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200465
466 drvr->dstats.rx_bytes += skb->len;
467 drvr->rx_packets++; /* Local count */
468
469 if (in_interrupt())
470 netif_rx(skb);
471 else
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
476 * to do it manually.
477 */
478 netif_rx_ni(skb);
479 }
480}
481
482void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
483{
484 uint ifidx;
485 struct brcmf_info *drvr_priv = drvr->info;
486 struct ethhdr *eh;
487 u16 type;
488
489 brcmf_proto_hdrpull(drvr, &ifidx, txp);
490
491 eh = (struct ethhdr *)(txp->data);
492 type = ntohs(eh->h_proto);
493
494 if (type == ETH_P_PAE)
495 atomic_dec(&drvr_priv->pend_8021x_cnt);
496
497}
498
499static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
500{
Franky Line1b83582011-10-21 16:16:33 +0200501 struct brcmf_if *ifp = netdev_priv(ndev);
502 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200503
504 brcmf_dbg(TRACE, "Enter\n");
505
Arend van Spriel5b435de2011-10-05 13:19:03 +0200506 if (drvr_priv->pub.up)
507 /* Use the protocol to get dongle stats */
508 brcmf_proto_dstats(&drvr_priv->pub);
509
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;
520
521 return &ifp->stats;
522}
523
524/* Retrieve current toe component enables, which are kept
525 as a bitmap in toe_ol iovar */
526static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
527{
528 struct brcmf_dcmd dcmd;
Arend van Spriel10629042011-10-12 20:51:18 +0200529 __le32 toe_le;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200530 char buf[32];
531 int ret;
532
533 memset(&dcmd, 0, sizeof(dcmd));
534
535 dcmd.cmd = BRCMF_C_GET_VAR;
536 dcmd.buf = buf;
537 dcmd.len = (uint) sizeof(buf);
538 dcmd.set = false;
539
540 strcpy(buf, "toe_ol");
541 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
542 if (ret < 0) {
543 /* Check for older dongle image that doesn't support toe_ol */
544 if (ret == -EIO) {
545 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
546 brcmf_ifname(&drvr_priv->pub, ifidx));
547 return -EOPNOTSUPP;
548 }
549
550 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
551 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
552 return ret;
553 }
554
Arend van Spriel10629042011-10-12 20:51:18 +0200555 memcpy(&toe_le, buf, sizeof(u32));
556 *toe_ol = le32_to_cpu(toe_le);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200557 return 0;
558}
559
560/* Set current toe component enables in toe_ol iovar,
561 and set toe global enable iovar */
562static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
563{
564 struct brcmf_dcmd dcmd;
565 char buf[32];
Arend van Spriel10629042011-10-12 20:51:18 +0200566 int ret;
567 __le32 toe_le = cpu_to_le32(toe_ol);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200568
569 memset(&dcmd, 0, sizeof(dcmd));
570
571 dcmd.cmd = BRCMF_C_SET_VAR;
572 dcmd.buf = buf;
573 dcmd.len = (uint) sizeof(buf);
574 dcmd.set = true;
575
576 /* Set toe_ol as requested */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200577 strcpy(buf, "toe_ol");
Arend van Spriel10629042011-10-12 20:51:18 +0200578 memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200579
580 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
581 if (ret < 0) {
582 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
583 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
584 return ret;
585 }
586
587 /* Enable toe globally only if any components are enabled. */
Arend van Spriel10629042011-10-12 20:51:18 +0200588 toe_le = cpu_to_le32(toe_ol != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200589
590 strcpy(buf, "toe");
Arend van Spriel10629042011-10-12 20:51:18 +0200591 memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200592
593 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
594 if (ret < 0) {
595 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
596 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
597 return ret;
598 }
599
600 return 0;
601}
602
603static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
604 struct ethtool_drvinfo *info)
605{
Franky Line1b83582011-10-21 16:16:33 +0200606 struct brcmf_if *ifp = netdev_priv(ndev);
607 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200608
609 sprintf(info->driver, KBUILD_MODNAME);
610 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
Franky Linc0a79622011-11-22 17:21:58 -0800611 sprintf(info->bus_info, "%s", dev_name(drvr_priv->pub.dev));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200612}
613
614static struct ethtool_ops brcmf_ethtool_ops = {
615 .get_drvinfo = brcmf_ethtool_get_drvinfo
616};
617
618static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
619{
620 struct ethtool_drvinfo info;
621 char drvname[sizeof(info.driver)];
622 u32 cmd;
623 struct ethtool_value edata;
624 u32 toe_cmpnt, csum_dir;
625 int ret;
626
627 brcmf_dbg(TRACE, "Enter\n");
628
629 /* all ethtool calls start with a cmd word */
630 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
631 return -EFAULT;
632
633 switch (cmd) {
634 case ETHTOOL_GDRVINFO:
635 /* Copy out any request driver name */
636 if (copy_from_user(&info, uaddr, sizeof(info)))
637 return -EFAULT;
638 strncpy(drvname, info.driver, sizeof(info.driver));
639 drvname[sizeof(info.driver) - 1] = '\0';
640
641 /* clear struct for return */
642 memset(&info, 0, sizeof(info));
643 info.cmd = cmd;
644
645 /* if requested, identify ourselves */
646 if (strcmp(drvname, "?dhd") == 0) {
647 sprintf(info.driver, "dhd");
648 strcpy(info.version, BRCMF_VERSION_STR);
649 }
650
651 /* otherwise, require dongle to be up */
652 else if (!drvr_priv->pub.up) {
653 brcmf_dbg(ERROR, "dongle is not up\n");
654 return -ENODEV;
655 }
656
657 /* finally, report dongle driver type */
658 else if (drvr_priv->pub.iswl)
659 sprintf(info.driver, "wl");
660 else
661 sprintf(info.driver, "xx");
662
663 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
664 if (copy_to_user(uaddr, &info, sizeof(info)))
665 return -EFAULT;
666 brcmf_dbg(CTL, "given %*s, returning %s\n",
667 (int)sizeof(drvname), drvname, info.driver);
668 break;
669
670 /* Get toe offload components from dongle */
671 case ETHTOOL_GRXCSUM:
672 case ETHTOOL_GTXCSUM:
673 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
674 if (ret < 0)
675 return ret;
676
677 csum_dir =
678 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
679
680 edata.cmd = cmd;
681 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
682
683 if (copy_to_user(uaddr, &edata, sizeof(edata)))
684 return -EFAULT;
685 break;
686
687 /* Set toe offload components in dongle */
688 case ETHTOOL_SRXCSUM:
689 case ETHTOOL_STXCSUM:
690 if (copy_from_user(&edata, uaddr, sizeof(edata)))
691 return -EFAULT;
692
693 /* Read the current settings, update and write back */
694 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
695 if (ret < 0)
696 return ret;
697
698 csum_dir =
699 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
700
701 if (edata.data != 0)
702 toe_cmpnt |= csum_dir;
703 else
704 toe_cmpnt &= ~csum_dir;
705
706 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
707 if (ret < 0)
708 return ret;
709
710 /* If setting TX checksum mode, tell Linux the new mode */
711 if (cmd == ETHTOOL_STXCSUM) {
712 if (edata.data)
713 drvr_priv->iflist[0]->ndev->features |=
714 NETIF_F_IP_CSUM;
715 else
716 drvr_priv->iflist[0]->ndev->features &=
717 ~NETIF_F_IP_CSUM;
718 }
719
720 break;
721
722 default:
723 return -EOPNOTSUPP;
724 }
725
726 return 0;
727}
728
729static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
730 int cmd)
731{
Franky Line1b83582011-10-21 16:16:33 +0200732 struct brcmf_if *ifp = netdev_priv(ndev);
733 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200734
Franky Line1b83582011-10-21 16:16:33 +0200735 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200736
Franky Line1b83582011-10-21 16:16:33 +0200737 if (!drvr_priv->iflist[ifp->idx])
Arend van Spriel5b435de2011-10-05 13:19:03 +0200738 return -1;
739
740 if (cmd == SIOCETHTOOL)
741 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
742
743 return -EOPNOTSUPP;
744}
745
746/* called only from within this driver. Sends a command to the dongle. */
747s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
748{
749 struct brcmf_dcmd dcmd;
750 s32 err = 0;
751 int buflen = 0;
752 bool is_set_key_cmd;
Franky Line1b83582011-10-21 16:16:33 +0200753 struct brcmf_if *ifp = netdev_priv(ndev);
754 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200755
756 memset(&dcmd, 0, sizeof(dcmd));
757 dcmd.cmd = cmd;
758 dcmd.buf = arg;
759 dcmd.len = len;
760
Arend van Spriel5b435de2011-10-05 13:19:03 +0200761 if (dcmd.buf != NULL)
762 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
763
764 /* send to dongle (must be up, and wl) */
Franky Lin8d169aa2011-11-22 17:21:52 -0800765 if ((drvr_priv->pub.bus_if->state != BRCMF_BUS_DATA)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200766 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
767 err = -EIO;
768 goto done;
769 }
770
771 if (!drvr_priv->pub.iswl) {
772 err = -EIO;
773 goto done;
774 }
775
776 /*
777 * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
778 * set key CMD to prevent M4 encryption.
779 */
780 is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
781 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
782 !(strncmp("wsec_key", dcmd.buf, 9))) ||
783 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
784 !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
785 if (is_set_key_cmd)
786 brcmf_netdev_wait_pend8021x(ndev);
787
Franky Line1b83582011-10-21 16:16:33 +0200788 err = brcmf_proto_dcmd(&drvr_priv->pub, ifp->idx, &dcmd, buflen);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200789
790done:
791 if (err > 0)
792 err = 0;
793
794 return err;
795}
796
797static int brcmf_netdev_stop(struct net_device *ndev)
798{
Franky Line1b83582011-10-21 16:16:33 +0200799 struct brcmf_if *ifp = netdev_priv(ndev);
800 struct brcmf_pub *drvr = &ifp->info->pub;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200801
802 brcmf_dbg(TRACE, "Enter\n");
803 brcmf_cfg80211_down(drvr->config);
804 if (drvr->up == 0)
805 return 0;
806
807 /* Set state and stop OS transmissions */
Rusty Russell3db1cd52011-12-19 13:56:45 +0000808 drvr->up = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200809 netif_stop_queue(ndev);
810
811 return 0;
812}
813
814static int brcmf_netdev_open(struct net_device *ndev)
815{
Franky Line1b83582011-10-21 16:16:33 +0200816 struct brcmf_if *ifp = netdev_priv(ndev);
817 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200818 u32 toe_ol;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200819 s32 ret = 0;
820
Franky Line1b83582011-10-21 16:16:33 +0200821 brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200822
Franky Line1b83582011-10-21 16:16:33 +0200823 if (ifp->idx == 0) { /* do it only for primary eth0 */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200824 /* try to bring up bus */
825 ret = brcmf_bus_start(&drvr_priv->pub);
826 if (ret != 0) {
827 brcmf_dbg(ERROR, "failed with code %d\n", ret);
828 return -1;
829 }
830 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
831
832 memcpy(ndev->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
833
834 /* Get current TOE mode from dongle */
Franky Line1b83582011-10-21 16:16:33 +0200835 if (brcmf_toe_get(drvr_priv, ifp->idx, &toe_ol) >= 0
Arend van Spriel5b435de2011-10-05 13:19:03 +0200836 && (toe_ol & TOE_TX_CSUM_OL) != 0)
Franky Line1b83582011-10-21 16:16:33 +0200837 drvr_priv->iflist[ifp->idx]->ndev->features |=
Arend van Spriel5b435de2011-10-05 13:19:03 +0200838 NETIF_F_IP_CSUM;
839 else
Franky Line1b83582011-10-21 16:16:33 +0200840 drvr_priv->iflist[ifp->idx]->ndev->features &=
Arend van Spriel5b435de2011-10-05 13:19:03 +0200841 ~NETIF_F_IP_CSUM;
842 }
843 /* Allow transmit calls */
844 netif_start_queue(ndev);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000845 drvr_priv->pub.up = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200846 if (brcmf_cfg80211_up(drvr_priv->pub.config)) {
847 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
848 return -1;
849 }
850
851 return ret;
852}
853
Franky Lindfded552011-10-21 16:16:20 +0200854static const struct net_device_ops brcmf_netdev_ops_pri = {
855 .ndo_open = brcmf_netdev_open,
856 .ndo_stop = brcmf_netdev_stop,
857 .ndo_get_stats = brcmf_netdev_get_stats,
858 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
859 .ndo_start_xmit = brcmf_netdev_start_xmit,
860 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
861 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
862};
863
Arend van Spriel5b435de2011-10-05 13:19:03 +0200864int
Franky Lin15d45b62011-10-21 16:16:32 +0200865brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, char *name, u8 *mac_addr)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200866{
867 struct brcmf_if *ifp;
Franky Line1b83582011-10-21 16:16:33 +0200868 struct net_device *ndev;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200869
Franky Lin15d45b62011-10-21 16:16:32 +0200870 brcmf_dbg(TRACE, "idx %d\n", ifidx);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200871
872 ifp = drvr_priv->iflist[ifidx];
Franky Line1b83582011-10-21 16:16:33 +0200873 /*
874 * Delete the existing interface before overwriting it
875 * in case we missed the BRCMF_E_IF_DEL event.
876 */
877 if (ifp) {
878 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
879 ifp->ndev->name);
880 netif_stop_queue(ifp->ndev);
881 unregister_netdev(ifp->ndev);
882 free_netdev(ifp->ndev);
883 drvr_priv->iflist[ifidx] = NULL;
Franky Lin15d45b62011-10-21 16:16:32 +0200884 }
Franky Line1b83582011-10-21 16:16:33 +0200885
886 /* Allocate netdev, including space for private structure */
887 ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
888 if (!ndev) {
889 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
890 return -ENOMEM;
891 }
892
893 ifp = netdev_priv(ndev);
894 ifp->ndev = ndev;
Franky Lin15d45b62011-10-21 16:16:32 +0200895 ifp->info = drvr_priv;
896 drvr_priv->iflist[ifidx] = ifp;
Franky Lin15d45b62011-10-21 16:16:32 +0200897 ifp->idx = ifidx;
Franky Lin15d45b62011-10-21 16:16:32 +0200898 if (mac_addr != NULL)
899 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200900
Franky Lin15d45b62011-10-21 16:16:32 +0200901 if (brcmf_net_attach(&drvr_priv->pub, ifp->idx)) {
902 brcmf_dbg(ERROR, "brcmf_net_attach failed");
903 free_netdev(ifp->ndev);
Franky Line1b83582011-10-21 16:16:33 +0200904 drvr_priv->iflist[ifidx] = NULL;
905 return -EOPNOTSUPP;
Franky Lin15d45b62011-10-21 16:16:32 +0200906 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200907
Franky Lin15d45b62011-10-21 16:16:32 +0200908 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
909 current->pid, ifp->ndev->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200910
911 return 0;
912}
913
914void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
915{
916 struct brcmf_if *ifp;
917
918 brcmf_dbg(TRACE, "idx %d\n", ifidx);
919
920 ifp = drvr_priv->iflist[ifidx];
921 if (!ifp) {
922 brcmf_dbg(ERROR, "Null interface\n");
923 return;
924 }
Franky Lindfded552011-10-21 16:16:20 +0200925 if (ifp->ndev) {
926 if (ifidx == 0) {
927 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
928 rtnl_lock();
929 brcmf_netdev_stop(ifp->ndev);
930 rtnl_unlock();
931 }
932 } else {
933 netif_stop_queue(ifp->ndev);
934 }
935
Arend van Spriel5b435de2011-10-05 13:19:03 +0200936 unregister_netdev(ifp->ndev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200937 drvr_priv->iflist[ifidx] = NULL;
Franky Lindfded552011-10-21 16:16:20 +0200938 if (ifidx == 0)
939 brcmf_cfg80211_detach(drvr_priv->pub.config);
940 free_netdev(ifp->ndev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200941 }
942}
943
Franky Lin8d169aa2011-11-22 17:21:52 -0800944struct brcmf_pub *brcmf_attach(struct brcmf_sdio *bus, uint bus_hdrlen,
945 struct device *dev)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200946{
947 struct brcmf_info *drvr_priv = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200948
949 brcmf_dbg(TRACE, "Enter\n");
950
Arend van Spriel5b435de2011-10-05 13:19:03 +0200951 /* Allocate primary brcmf_info */
952 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
953 if (!drvr_priv)
954 goto fail;
955
Arend van Spriel5b435de2011-10-05 13:19:03 +0200956 mutex_init(&drvr_priv->proto_block);
957
958 /* Link to info module */
959 drvr_priv->pub.info = drvr_priv;
960
961 /* Link to bus module */
962 drvr_priv->pub.bus = bus;
963 drvr_priv->pub.hdrlen = bus_hdrlen;
Franky Lin8d169aa2011-11-22 17:21:52 -0800964 drvr_priv->pub.bus_if = dev_get_drvdata(dev);
Franky Lin532cdd32011-11-22 17:21:54 -0800965 drvr_priv->pub.dev = dev;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200966
967 /* Attach and link in the protocol */
968 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
969 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
970 goto fail;
971 }
972
Arend van Spriel5b435de2011-10-05 13:19:03 +0200973 INIT_WORK(&drvr_priv->setmacaddr_work, _brcmf_set_mac_address);
974 INIT_WORK(&drvr_priv->multicast_work, _brcmf_set_multicast_list);
975
Arend van Spriel5b435de2011-10-05 13:19:03 +0200976 return &drvr_priv->pub;
977
978fail:
Arend van Spriel5b435de2011-10-05 13:19:03 +0200979 if (drvr_priv)
980 brcmf_detach(&drvr_priv->pub);
981
982 return NULL;
983}
984
985int brcmf_bus_start(struct brcmf_pub *drvr)
986{
987 int ret = -1;
988 struct brcmf_info *drvr_priv = drvr->info;
989 /* Room for "event_msgs" + '\0' + bitvec */
990 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
991
992 brcmf_dbg(TRACE, "\n");
993
994 /* Bring up the bus */
Franky Linfa20b912011-11-22 17:21:57 -0800995 ret = brcmf_sdbrcm_bus_init(drvr_priv->pub.dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200996 if (ret != 0) {
997 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
998 return ret;
999 }
1000
1001 /* If bus is not ready, can't come up */
Franky Lin8d169aa2011-11-22 17:21:52 -08001002 if (drvr_priv->pub.bus_if->state != BRCMF_BUS_DATA) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001003 brcmf_dbg(ERROR, "failed bus is not ready\n");
1004 return -ENODEV;
1005 }
1006
Alwin Beukers53a22772011-10-12 20:51:30 +02001007 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001008 iovbuf, sizeof(iovbuf));
1009 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1010 sizeof(iovbuf));
1011 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1012
1013 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1014 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1015 setbit(drvr->eventmask, BRCMF_E_AUTH);
1016 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1017 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1018 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1019 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1020 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1021 setbit(drvr->eventmask, BRCMF_E_JOIN);
1022 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1023 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1024 setbit(drvr->eventmask, BRCMF_E_LINK);
1025 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1026 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1027 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1028 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1029 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1030 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1031
1032/* enable dongle roaming event */
1033
1034 drvr->pktfilter_count = 1;
1035 /* Setup filter to allow only unicast */
1036 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1037
1038 /* Bus is ready, do any protocol initialization */
1039 ret = brcmf_proto_init(&drvr_priv->pub);
1040 if (ret < 0)
1041 return ret;
1042
1043 return 0;
1044}
1045
Arend van Spriel5b435de2011-10-05 13:19:03 +02001046int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1047{
1048 struct brcmf_info *drvr_priv = drvr->info;
1049 struct net_device *ndev;
1050 u8 temp_addr[ETH_ALEN] = {
1051 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1052
1053 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1054
1055 ndev = drvr_priv->iflist[ifidx]->ndev;
1056 ndev->netdev_ops = &brcmf_netdev_ops_pri;
1057
1058 /*
1059 * We have to use the primary MAC for virtual interfaces
1060 */
1061 if (ifidx != 0) {
1062 /* for virtual interfaces use the primary MAC */
1063 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1064
1065 }
1066
1067 if (ifidx == 1) {
1068 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1069 /* ACCESSPOINT INTERFACE CASE */
1070 temp_addr[0] |= 0X02; /* set bit 2 ,
1071 - Locally Administered address */
1072
1073 }
1074 ndev->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1075 ndev->ethtool_ops = &brcmf_ethtool_ops;
1076
1077 drvr_priv->pub.rxsz = ndev->mtu + ndev->hard_header_len +
1078 drvr_priv->pub.hdrlen;
1079
1080 memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
1081
Franky Lin15d45b62011-10-21 16:16:32 +02001082 /* attach to cfg80211 for primary interface */
1083 if (!ifidx) {
Franky Linc0a79622011-11-22 17:21:58 -08001084 drvr->config = brcmf_cfg80211_attach(ndev, drvr->dev, drvr);
Franky Lin15d45b62011-10-21 16:16:32 +02001085 if (drvr->config == NULL) {
1086 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1087 goto fail;
1088 }
1089 }
1090
Arend van Spriel5b435de2011-10-05 13:19:03 +02001091 if (register_netdev(ndev) != 0) {
1092 brcmf_dbg(ERROR, "couldn't register the net device\n");
1093 goto fail;
1094 }
1095
1096 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
1097
1098 return 0;
1099
1100fail:
1101 ndev->netdev_ops = NULL;
1102 return -EBADE;
1103}
1104
1105static void brcmf_bus_detach(struct brcmf_pub *drvr)
1106{
1107 struct brcmf_info *drvr_priv;
1108
1109 brcmf_dbg(TRACE, "Enter\n");
1110
1111 if (drvr) {
1112 drvr_priv = drvr->info;
1113 if (drvr_priv) {
1114 /* Stop the protocol module */
1115 brcmf_proto_stop(&drvr_priv->pub);
1116
1117 /* Stop the bus module */
Franky Lin94c2fb82011-11-22 17:21:59 -08001118 brcmf_sdbrcm_bus_stop(drvr_priv->pub.dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001119 }
1120 }
1121}
1122
1123void brcmf_detach(struct brcmf_pub *drvr)
1124{
1125 struct brcmf_info *drvr_priv;
1126
1127 brcmf_dbg(TRACE, "Enter\n");
1128
1129 if (drvr) {
1130 drvr_priv = drvr->info;
1131 if (drvr_priv) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001132 int i;
1133
Franky Lindfded552011-10-21 16:16:20 +02001134 /* make sure primary interface removed last */
1135 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001136 if (drvr_priv->iflist[i])
1137 brcmf_del_if(drvr_priv, i);
1138
Arend van Spriel5b435de2011-10-05 13:19:03 +02001139 cancel_work_sync(&drvr_priv->setmacaddr_work);
1140 cancel_work_sync(&drvr_priv->multicast_work);
1141
1142 brcmf_bus_detach(drvr);
1143
1144 if (drvr->prot)
1145 brcmf_proto_detach(drvr);
1146
Arend van Spriel5b435de2011-10-05 13:19:03 +02001147 kfree(drvr_priv);
1148 }
1149 }
1150}
1151
Arend van Spriel5b435de2011-10-05 13:19:03 +02001152int brcmf_os_proto_block(struct brcmf_pub *drvr)
1153{
1154 struct brcmf_info *drvr_priv = drvr->info;
1155
1156 if (drvr_priv) {
1157 mutex_lock(&drvr_priv->proto_block);
1158 return 1;
1159 }
1160 return 0;
1161}
1162
1163int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1164{
1165 struct brcmf_info *drvr_priv = drvr->info;
1166
1167 if (drvr_priv) {
1168 mutex_unlock(&drvr_priv->proto_block);
1169 return 1;
1170 }
1171
1172 return 0;
1173}
1174
1175static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1176{
1177 return atomic_read(&drvr_priv->pend_8021x_cnt);
1178}
1179
1180#define MAX_WAIT_FOR_8021X_TX 10
1181
1182int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1183{
Franky Line1b83582011-10-21 16:16:33 +02001184 struct brcmf_if *ifp = netdev_priv(ndev);
1185 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001186 int timeout = 10 * HZ / 1000;
1187 int ntimes = MAX_WAIT_FOR_8021X_TX;
1188 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1189
1190 while (ntimes && pend) {
1191 if (pend) {
1192 set_current_state(TASK_INTERRUPTIBLE);
1193 schedule_timeout(timeout);
1194 set_current_state(TASK_RUNNING);
1195 ntimes--;
1196 }
1197 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1198 }
1199 return pend;
1200}
1201
1202#ifdef BCMDBG
Arend van Sprielaf534952011-10-12 20:51:14 +02001203int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001204{
1205 int ret = 0;
1206 struct file *fp;
1207 mm_segment_t old_fs;
1208 loff_t pos = 0;
1209
1210 /* change to KERNEL_DS address limit */
1211 old_fs = get_fs();
1212 set_fs(KERNEL_DS);
1213
1214 /* open file to write */
1215 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1216 if (!fp) {
1217 brcmf_dbg(ERROR, "open file error\n");
1218 ret = -1;
1219 goto exit;
1220 }
1221
1222 /* Write buf to file */
Arend van Sprielaf534952011-10-12 20:51:14 +02001223 fp->f_op->write(fp, (char __user *)buf, size, &pos);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001224
1225exit:
1226 /* free buf before return */
1227 kfree(buf);
1228 /* close file before return */
1229 if (fp)
1230 filp_close(fp, current->files);
1231 /* restore previous address limit */
1232 set_fs(old_fs);
1233
1234 return ret;
1235}
1236#endif /* BCMDBG */