blob: 683a7ed2571401d388dfa156a2ef2271b749c7b1 [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 */
295 if (!drvr->up || (drvr->busstate == BRCMF_BUS_DOWN))
296 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 */
313 return brcmf_sdbrcm_bus_txdata(drvr->bus, pktbuf);
314}
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 */
325 if (!drvr_priv->pub.up || (drvr_priv->pub.busstate == BRCMF_BUS_DOWN)) {
326 brcmf_dbg(ERROR, "xmit rejected pub.up=%d busstate=%d\n",
327 drvr_priv->pub.up, drvr_priv->pub.busstate);
328 netif_stop_queue(ndev);
329 return -ENODEV;
330 }
331
Franky Line1b83582011-10-21 16:16:33 +0200332 if (!drvr_priv->iflist[ifp->idx]) {
333 brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200334 netif_stop_queue(ndev);
335 return -ENODEV;
336 }
337
338 /* Make sure there's enough room for any header */
339 if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
340 struct sk_buff *skb2;
341
342 brcmf_dbg(INFO, "%s: insufficient headroom\n",
Franky Line1b83582011-10-21 16:16:33 +0200343 brcmf_ifname(&drvr_priv->pub, ifp->idx));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200344 drvr_priv->pub.tx_realloc++;
345 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
346 dev_kfree_skb(skb);
347 skb = skb2;
348 if (skb == NULL) {
349 brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
Franky Line1b83582011-10-21 16:16:33 +0200350 brcmf_ifname(&drvr_priv->pub, ifp->idx));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200351 ret = -ENOMEM;
352 goto done;
353 }
354 }
355
Franky Line1b83582011-10-21 16:16:33 +0200356 ret = brcmf_sendpkt(&drvr_priv->pub, ifp->idx, skb);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200357
358done:
359 if (ret)
360 drvr_priv->pub.dstats.tx_dropped++;
361 else
362 drvr_priv->pub.tx_packets++;
363
364 /* Return ok: we always eat the packet */
365 return 0;
366}
367
368void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
369{
370 struct net_device *ndev;
371 struct brcmf_info *drvr_priv = drvr->info;
372
373 brcmf_dbg(TRACE, "Enter\n");
374
375 drvr->txoff = state;
376 ndev = drvr_priv->iflist[ifidx]->ndev;
377 if (state == ON)
378 netif_stop_queue(ndev);
379 else
380 netif_wake_queue(ndev);
381}
382
383static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx,
384 void *pktdata, struct brcmf_event_msg *event,
385 void **data)
386{
387 int bcmerror = 0;
388
389 bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
390 if (bcmerror != 0)
391 return bcmerror;
392
393 if (drvr_priv->iflist[*ifidx]->ndev)
394 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->ndev,
395 event, *data);
396
397 return bcmerror;
398}
399
Arend van Spriel0b45bf72011-11-22 17:21:36 -0800400void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx,
401 struct sk_buff_head *skb_list)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200402{
403 struct brcmf_info *drvr_priv = drvr->info;
404 unsigned char *eth;
405 uint len;
406 void *data;
Arend van Spriel0b45bf72011-11-22 17:21:36 -0800407 struct sk_buff *skb, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200408 struct brcmf_if *ifp;
409 struct brcmf_event_msg event;
410
411 brcmf_dbg(TRACE, "Enter\n");
412
Arend van Spriel0b45bf72011-11-22 17:21:36 -0800413 skb_queue_walk_safe(skb_list, skb, pnext) {
414 skb_unlink(skb, skb_list);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200415
416 /* Get the protocol, maintain skb around eth_type_trans()
417 * The main reason for this hack is for the limitation of
418 * Linux 2.4 where 'eth_type_trans' uses the
419 * 'net->hard_header_len'
420 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
421 * coping of the packet coming from the network stack to add
422 * BDC, Hardware header etc, during network interface
423 * registration
424 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
425 * required
426 * for BDC, Hardware header etc. and not just the ETH_HLEN
427 */
428 eth = skb->data;
429 len = skb->len;
430
431 ifp = drvr_priv->iflist[ifidx];
432 if (ifp == NULL)
433 ifp = drvr_priv->iflist[0];
434
Franky Lin0c094c72011-11-22 17:21:46 -0800435 if (!ifp || !ifp->ndev ||
436 ifp->ndev->reg_state != NETREG_REGISTERED) {
437 brcmu_pkt_buf_free_skb(skb);
438 continue;
439 }
440
Arend van Spriel5b435de2011-10-05 13:19:03 +0200441 skb->dev = ifp->ndev;
442 skb->protocol = eth_type_trans(skb, skb->dev);
443
444 if (skb->pkt_type == PACKET_MULTICAST)
445 drvr_priv->pub.rx_multicast++;
446
447 skb->data = eth;
448 skb->len = len;
449
450 /* Strip header, count, deliver upward */
451 skb_pull(skb, ETH_HLEN);
452
453 /* Process special event packets and then discard them */
454 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
455 brcmf_host_event(drvr_priv, &ifidx,
456 skb_mac_header(skb),
457 &event, &data);
458
Franky Lind1a5b6f2011-10-21 16:16:34 +0200459 if (drvr_priv->iflist[ifidx]) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200460 ifp = drvr_priv->iflist[ifidx];
Arend van Spriel5b435de2011-10-05 13:19:03 +0200461 ifp->ndev->last_rx = jiffies;
Franky Lind1a5b6f2011-10-21 16:16:34 +0200462 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200463
464 drvr->dstats.rx_bytes += skb->len;
465 drvr->rx_packets++; /* Local count */
466
467 if (in_interrupt())
468 netif_rx(skb);
469 else
470 /* If the receive is not processed inside an ISR,
471 * the softirqd must be woken explicitly to service
472 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
473 * by netif_rx_ni(), but in earlier kernels, we need
474 * to do it manually.
475 */
476 netif_rx_ni(skb);
477 }
478}
479
480void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
481{
482 uint ifidx;
483 struct brcmf_info *drvr_priv = drvr->info;
484 struct ethhdr *eh;
485 u16 type;
486
487 brcmf_proto_hdrpull(drvr, &ifidx, txp);
488
489 eh = (struct ethhdr *)(txp->data);
490 type = ntohs(eh->h_proto);
491
492 if (type == ETH_P_PAE)
493 atomic_dec(&drvr_priv->pend_8021x_cnt);
494
495}
496
497static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
498{
Franky Line1b83582011-10-21 16:16:33 +0200499 struct brcmf_if *ifp = netdev_priv(ndev);
500 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200501
502 brcmf_dbg(TRACE, "Enter\n");
503
Arend van Spriel5b435de2011-10-05 13:19:03 +0200504 if (drvr_priv->pub.up)
505 /* Use the protocol to get dongle stats */
506 brcmf_proto_dstats(&drvr_priv->pub);
507
508 /* Copy dongle stats to net device stats */
509 ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
510 ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
511 ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
512 ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
513 ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
514 ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
515 ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
516 ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
517 ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
518
519 return &ifp->stats;
520}
521
522/* Retrieve current toe component enables, which are kept
523 as a bitmap in toe_ol iovar */
524static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
525{
526 struct brcmf_dcmd dcmd;
Arend van Spriel10629042011-10-12 20:51:18 +0200527 __le32 toe_le;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200528 char buf[32];
529 int ret;
530
531 memset(&dcmd, 0, sizeof(dcmd));
532
533 dcmd.cmd = BRCMF_C_GET_VAR;
534 dcmd.buf = buf;
535 dcmd.len = (uint) sizeof(buf);
536 dcmd.set = false;
537
538 strcpy(buf, "toe_ol");
539 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
540 if (ret < 0) {
541 /* Check for older dongle image that doesn't support toe_ol */
542 if (ret == -EIO) {
543 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
544 brcmf_ifname(&drvr_priv->pub, ifidx));
545 return -EOPNOTSUPP;
546 }
547
548 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
549 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
550 return ret;
551 }
552
Arend van Spriel10629042011-10-12 20:51:18 +0200553 memcpy(&toe_le, buf, sizeof(u32));
554 *toe_ol = le32_to_cpu(toe_le);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200555 return 0;
556}
557
558/* Set current toe component enables in toe_ol iovar,
559 and set toe global enable iovar */
560static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
561{
562 struct brcmf_dcmd dcmd;
563 char buf[32];
Arend van Spriel10629042011-10-12 20:51:18 +0200564 int ret;
565 __le32 toe_le = cpu_to_le32(toe_ol);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200566
567 memset(&dcmd, 0, sizeof(dcmd));
568
569 dcmd.cmd = BRCMF_C_SET_VAR;
570 dcmd.buf = buf;
571 dcmd.len = (uint) sizeof(buf);
572 dcmd.set = true;
573
574 /* Set toe_ol as requested */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200575 strcpy(buf, "toe_ol");
Arend van Spriel10629042011-10-12 20:51:18 +0200576 memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200577
578 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
579 if (ret < 0) {
580 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
581 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
582 return ret;
583 }
584
585 /* Enable toe globally only if any components are enabled. */
Arend van Spriel10629042011-10-12 20:51:18 +0200586 toe_le = cpu_to_le32(toe_ol != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200587
588 strcpy(buf, "toe");
Arend van Spriel10629042011-10-12 20:51:18 +0200589 memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200590
591 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
592 if (ret < 0) {
593 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
594 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
595 return ret;
596 }
597
598 return 0;
599}
600
601static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
602 struct ethtool_drvinfo *info)
603{
Franky Line1b83582011-10-21 16:16:33 +0200604 struct brcmf_if *ifp = netdev_priv(ndev);
605 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200606
607 sprintf(info->driver, KBUILD_MODNAME);
608 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200609 sprintf(info->bus_info, "%s",
610 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
611}
612
613static struct ethtool_ops brcmf_ethtool_ops = {
614 .get_drvinfo = brcmf_ethtool_get_drvinfo
615};
616
617static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
618{
619 struct ethtool_drvinfo info;
620 char drvname[sizeof(info.driver)];
621 u32 cmd;
622 struct ethtool_value edata;
623 u32 toe_cmpnt, csum_dir;
624 int ret;
625
626 brcmf_dbg(TRACE, "Enter\n");
627
628 /* all ethtool calls start with a cmd word */
629 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
630 return -EFAULT;
631
632 switch (cmd) {
633 case ETHTOOL_GDRVINFO:
634 /* Copy out any request driver name */
635 if (copy_from_user(&info, uaddr, sizeof(info)))
636 return -EFAULT;
637 strncpy(drvname, info.driver, sizeof(info.driver));
638 drvname[sizeof(info.driver) - 1] = '\0';
639
640 /* clear struct for return */
641 memset(&info, 0, sizeof(info));
642 info.cmd = cmd;
643
644 /* if requested, identify ourselves */
645 if (strcmp(drvname, "?dhd") == 0) {
646 sprintf(info.driver, "dhd");
647 strcpy(info.version, BRCMF_VERSION_STR);
648 }
649
650 /* otherwise, require dongle to be up */
651 else if (!drvr_priv->pub.up) {
652 brcmf_dbg(ERROR, "dongle is not up\n");
653 return -ENODEV;
654 }
655
656 /* finally, report dongle driver type */
657 else if (drvr_priv->pub.iswl)
658 sprintf(info.driver, "wl");
659 else
660 sprintf(info.driver, "xx");
661
662 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
663 if (copy_to_user(uaddr, &info, sizeof(info)))
664 return -EFAULT;
665 brcmf_dbg(CTL, "given %*s, returning %s\n",
666 (int)sizeof(drvname), drvname, info.driver);
667 break;
668
669 /* Get toe offload components from dongle */
670 case ETHTOOL_GRXCSUM:
671 case ETHTOOL_GTXCSUM:
672 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
673 if (ret < 0)
674 return ret;
675
676 csum_dir =
677 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
678
679 edata.cmd = cmd;
680 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
681
682 if (copy_to_user(uaddr, &edata, sizeof(edata)))
683 return -EFAULT;
684 break;
685
686 /* Set toe offload components in dongle */
687 case ETHTOOL_SRXCSUM:
688 case ETHTOOL_STXCSUM:
689 if (copy_from_user(&edata, uaddr, sizeof(edata)))
690 return -EFAULT;
691
692 /* Read the current settings, update and write back */
693 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
694 if (ret < 0)
695 return ret;
696
697 csum_dir =
698 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
699
700 if (edata.data != 0)
701 toe_cmpnt |= csum_dir;
702 else
703 toe_cmpnt &= ~csum_dir;
704
705 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
706 if (ret < 0)
707 return ret;
708
709 /* If setting TX checksum mode, tell Linux the new mode */
710 if (cmd == ETHTOOL_STXCSUM) {
711 if (edata.data)
712 drvr_priv->iflist[0]->ndev->features |=
713 NETIF_F_IP_CSUM;
714 else
715 drvr_priv->iflist[0]->ndev->features &=
716 ~NETIF_F_IP_CSUM;
717 }
718
719 break;
720
721 default:
722 return -EOPNOTSUPP;
723 }
724
725 return 0;
726}
727
728static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
729 int cmd)
730{
Franky Line1b83582011-10-21 16:16:33 +0200731 struct brcmf_if *ifp = netdev_priv(ndev);
732 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200733
Franky Line1b83582011-10-21 16:16:33 +0200734 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200735
Franky Line1b83582011-10-21 16:16:33 +0200736 if (!drvr_priv->iflist[ifp->idx])
Arend van Spriel5b435de2011-10-05 13:19:03 +0200737 return -1;
738
739 if (cmd == SIOCETHTOOL)
740 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
741
742 return -EOPNOTSUPP;
743}
744
745/* called only from within this driver. Sends a command to the dongle. */
746s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
747{
748 struct brcmf_dcmd dcmd;
749 s32 err = 0;
750 int buflen = 0;
751 bool is_set_key_cmd;
Franky Line1b83582011-10-21 16:16:33 +0200752 struct brcmf_if *ifp = netdev_priv(ndev);
753 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200754
755 memset(&dcmd, 0, sizeof(dcmd));
756 dcmd.cmd = cmd;
757 dcmd.buf = arg;
758 dcmd.len = len;
759
Arend van Spriel5b435de2011-10-05 13:19:03 +0200760 if (dcmd.buf != NULL)
761 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
762
763 /* send to dongle (must be up, and wl) */
764 if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
765 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
766 err = -EIO;
767 goto done;
768 }
769
770 if (!drvr_priv->pub.iswl) {
771 err = -EIO;
772 goto done;
773 }
774
775 /*
776 * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
777 * set key CMD to prevent M4 encryption.
778 */
779 is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
780 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
781 !(strncmp("wsec_key", dcmd.buf, 9))) ||
782 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
783 !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
784 if (is_set_key_cmd)
785 brcmf_netdev_wait_pend8021x(ndev);
786
Franky Line1b83582011-10-21 16:16:33 +0200787 err = brcmf_proto_dcmd(&drvr_priv->pub, ifp->idx, &dcmd, buflen);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200788
789done:
790 if (err > 0)
791 err = 0;
792
793 return err;
794}
795
796static int brcmf_netdev_stop(struct net_device *ndev)
797{
Franky Line1b83582011-10-21 16:16:33 +0200798 struct brcmf_if *ifp = netdev_priv(ndev);
799 struct brcmf_pub *drvr = &ifp->info->pub;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200800
801 brcmf_dbg(TRACE, "Enter\n");
802 brcmf_cfg80211_down(drvr->config);
803 if (drvr->up == 0)
804 return 0;
805
806 /* Set state and stop OS transmissions */
807 drvr->up = 0;
808 netif_stop_queue(ndev);
809
810 return 0;
811}
812
813static int brcmf_netdev_open(struct net_device *ndev)
814{
Franky Line1b83582011-10-21 16:16:33 +0200815 struct brcmf_if *ifp = netdev_priv(ndev);
816 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200817 u32 toe_ol;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200818 s32 ret = 0;
819
Franky Line1b83582011-10-21 16:16:33 +0200820 brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200821
Franky Line1b83582011-10-21 16:16:33 +0200822 if (ifp->idx == 0) { /* do it only for primary eth0 */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200823 /* try to bring up bus */
824 ret = brcmf_bus_start(&drvr_priv->pub);
825 if (ret != 0) {
826 brcmf_dbg(ERROR, "failed with code %d\n", ret);
827 return -1;
828 }
829 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
830
831 memcpy(ndev->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
832
833 /* Get current TOE mode from dongle */
Franky Line1b83582011-10-21 16:16:33 +0200834 if (brcmf_toe_get(drvr_priv, ifp->idx, &toe_ol) >= 0
Arend van Spriel5b435de2011-10-05 13:19:03 +0200835 && (toe_ol & TOE_TX_CSUM_OL) != 0)
Franky Line1b83582011-10-21 16:16:33 +0200836 drvr_priv->iflist[ifp->idx]->ndev->features |=
Arend van Spriel5b435de2011-10-05 13:19:03 +0200837 NETIF_F_IP_CSUM;
838 else
Franky Line1b83582011-10-21 16:16:33 +0200839 drvr_priv->iflist[ifp->idx]->ndev->features &=
Arend van Spriel5b435de2011-10-05 13:19:03 +0200840 ~NETIF_F_IP_CSUM;
841 }
842 /* Allow transmit calls */
843 netif_start_queue(ndev);
844 drvr_priv->pub.up = 1;
845 if (brcmf_cfg80211_up(drvr_priv->pub.config)) {
846 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
847 return -1;
848 }
849
850 return ret;
851}
852
Franky Lindfded552011-10-21 16:16:20 +0200853static const struct net_device_ops brcmf_netdev_ops_pri = {
854 .ndo_open = brcmf_netdev_open,
855 .ndo_stop = brcmf_netdev_stop,
856 .ndo_get_stats = brcmf_netdev_get_stats,
857 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
858 .ndo_start_xmit = brcmf_netdev_start_xmit,
859 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
860 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
861};
862
Arend van Spriel5b435de2011-10-05 13:19:03 +0200863int
Franky Lin15d45b62011-10-21 16:16:32 +0200864brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, char *name, u8 *mac_addr)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200865{
866 struct brcmf_if *ifp;
Franky Line1b83582011-10-21 16:16:33 +0200867 struct net_device *ndev;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200868
Franky Lin15d45b62011-10-21 16:16:32 +0200869 brcmf_dbg(TRACE, "idx %d\n", ifidx);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200870
871 ifp = drvr_priv->iflist[ifidx];
Franky Line1b83582011-10-21 16:16:33 +0200872 /*
873 * Delete the existing interface before overwriting it
874 * in case we missed the BRCMF_E_IF_DEL event.
875 */
876 if (ifp) {
877 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
878 ifp->ndev->name);
879 netif_stop_queue(ifp->ndev);
880 unregister_netdev(ifp->ndev);
881 free_netdev(ifp->ndev);
882 drvr_priv->iflist[ifidx] = NULL;
Franky Lin15d45b62011-10-21 16:16:32 +0200883 }
Franky Line1b83582011-10-21 16:16:33 +0200884
885 /* Allocate netdev, including space for private structure */
886 ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
887 if (!ndev) {
888 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
889 return -ENOMEM;
890 }
891
892 ifp = netdev_priv(ndev);
893 ifp->ndev = ndev;
Franky Lin15d45b62011-10-21 16:16:32 +0200894 ifp->info = drvr_priv;
895 drvr_priv->iflist[ifidx] = ifp;
Franky Lin15d45b62011-10-21 16:16:32 +0200896 ifp->idx = ifidx;
Franky Lin15d45b62011-10-21 16:16:32 +0200897 if (mac_addr != NULL)
898 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200899
Franky Lin15d45b62011-10-21 16:16:32 +0200900 if (brcmf_net_attach(&drvr_priv->pub, ifp->idx)) {
901 brcmf_dbg(ERROR, "brcmf_net_attach failed");
902 free_netdev(ifp->ndev);
Franky Line1b83582011-10-21 16:16:33 +0200903 drvr_priv->iflist[ifidx] = NULL;
904 return -EOPNOTSUPP;
Franky Lin15d45b62011-10-21 16:16:32 +0200905 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200906
Franky Lin15d45b62011-10-21 16:16:32 +0200907 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
908 current->pid, ifp->ndev->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200909
910 return 0;
911}
912
913void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
914{
915 struct brcmf_if *ifp;
916
917 brcmf_dbg(TRACE, "idx %d\n", ifidx);
918
919 ifp = drvr_priv->iflist[ifidx];
920 if (!ifp) {
921 brcmf_dbg(ERROR, "Null interface\n");
922 return;
923 }
Franky Lindfded552011-10-21 16:16:20 +0200924 if (ifp->ndev) {
925 if (ifidx == 0) {
926 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
927 rtnl_lock();
928 brcmf_netdev_stop(ifp->ndev);
929 rtnl_unlock();
930 }
931 } else {
932 netif_stop_queue(ifp->ndev);
933 }
934
Arend van Spriel5b435de2011-10-05 13:19:03 +0200935 unregister_netdev(ifp->ndev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200936 drvr_priv->iflist[ifidx] = NULL;
Franky Lindfded552011-10-21 16:16:20 +0200937 if (ifidx == 0)
938 brcmf_cfg80211_detach(drvr_priv->pub.config);
939 free_netdev(ifp->ndev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200940 }
941}
942
943struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
944{
945 struct brcmf_info *drvr_priv = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200946
947 brcmf_dbg(TRACE, "Enter\n");
948
Arend van Spriel5b435de2011-10-05 13:19:03 +0200949 /* Allocate primary brcmf_info */
950 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
951 if (!drvr_priv)
952 goto fail;
953
Arend van Spriel5b435de2011-10-05 13:19:03 +0200954 mutex_init(&drvr_priv->proto_block);
955
956 /* Link to info module */
957 drvr_priv->pub.info = drvr_priv;
958
959 /* Link to bus module */
960 drvr_priv->pub.bus = bus;
961 drvr_priv->pub.hdrlen = bus_hdrlen;
962
963 /* Attach and link in the protocol */
964 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
965 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
966 goto fail;
967 }
968
Arend van Spriel5b435de2011-10-05 13:19:03 +0200969 INIT_WORK(&drvr_priv->setmacaddr_work, _brcmf_set_mac_address);
970 INIT_WORK(&drvr_priv->multicast_work, _brcmf_set_multicast_list);
971
Arend van Spriel5b435de2011-10-05 13:19:03 +0200972 return &drvr_priv->pub;
973
974fail:
Arend van Spriel5b435de2011-10-05 13:19:03 +0200975 if (drvr_priv)
976 brcmf_detach(&drvr_priv->pub);
977
978 return NULL;
979}
980
981int brcmf_bus_start(struct brcmf_pub *drvr)
982{
983 int ret = -1;
984 struct brcmf_info *drvr_priv = drvr->info;
985 /* Room for "event_msgs" + '\0' + bitvec */
986 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
987
988 brcmf_dbg(TRACE, "\n");
989
990 /* Bring up the bus */
991 ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub);
992 if (ret != 0) {
993 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
994 return ret;
995 }
996
997 /* If bus is not ready, can't come up */
998 if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
999 brcmf_dbg(ERROR, "failed bus is not ready\n");
1000 return -ENODEV;
1001 }
1002
Alwin Beukers53a22772011-10-12 20:51:30 +02001003 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001004 iovbuf, sizeof(iovbuf));
1005 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1006 sizeof(iovbuf));
1007 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1008
1009 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1010 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1011 setbit(drvr->eventmask, BRCMF_E_AUTH);
1012 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1013 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1014 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1015 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1016 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1017 setbit(drvr->eventmask, BRCMF_E_JOIN);
1018 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1019 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1020 setbit(drvr->eventmask, BRCMF_E_LINK);
1021 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1022 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1023 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1024 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1025 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1026 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1027
1028/* enable dongle roaming event */
1029
1030 drvr->pktfilter_count = 1;
1031 /* Setup filter to allow only unicast */
1032 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1033
1034 /* Bus is ready, do any protocol initialization */
1035 ret = brcmf_proto_init(&drvr_priv->pub);
1036 if (ret < 0)
1037 return ret;
1038
1039 return 0;
1040}
1041
Arend van Spriel5b435de2011-10-05 13:19:03 +02001042int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1043{
1044 struct brcmf_info *drvr_priv = drvr->info;
1045 struct net_device *ndev;
1046 u8 temp_addr[ETH_ALEN] = {
1047 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1048
1049 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1050
1051 ndev = drvr_priv->iflist[ifidx]->ndev;
1052 ndev->netdev_ops = &brcmf_netdev_ops_pri;
1053
1054 /*
1055 * We have to use the primary MAC for virtual interfaces
1056 */
1057 if (ifidx != 0) {
1058 /* for virtual interfaces use the primary MAC */
1059 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1060
1061 }
1062
1063 if (ifidx == 1) {
1064 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1065 /* ACCESSPOINT INTERFACE CASE */
1066 temp_addr[0] |= 0X02; /* set bit 2 ,
1067 - Locally Administered address */
1068
1069 }
1070 ndev->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1071 ndev->ethtool_ops = &brcmf_ethtool_ops;
1072
1073 drvr_priv->pub.rxsz = ndev->mtu + ndev->hard_header_len +
1074 drvr_priv->pub.hdrlen;
1075
1076 memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
1077
Franky Lin15d45b62011-10-21 16:16:32 +02001078 /* attach to cfg80211 for primary interface */
1079 if (!ifidx) {
1080 drvr->config =
1081 brcmf_cfg80211_attach(ndev,
1082 brcmf_bus_get_device(drvr->bus),
1083 drvr);
1084 if (drvr->config == NULL) {
1085 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1086 goto fail;
1087 }
1088 }
1089
Arend van Spriel5b435de2011-10-05 13:19:03 +02001090 if (register_netdev(ndev) != 0) {
1091 brcmf_dbg(ERROR, "couldn't register the net device\n");
1092 goto fail;
1093 }
1094
1095 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
1096
1097 return 0;
1098
1099fail:
1100 ndev->netdev_ops = NULL;
1101 return -EBADE;
1102}
1103
1104static void brcmf_bus_detach(struct brcmf_pub *drvr)
1105{
1106 struct brcmf_info *drvr_priv;
1107
1108 brcmf_dbg(TRACE, "Enter\n");
1109
1110 if (drvr) {
1111 drvr_priv = drvr->info;
1112 if (drvr_priv) {
1113 /* Stop the protocol module */
1114 brcmf_proto_stop(&drvr_priv->pub);
1115
1116 /* Stop the bus module */
1117 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus);
1118 }
1119 }
1120}
1121
1122void brcmf_detach(struct brcmf_pub *drvr)
1123{
1124 struct brcmf_info *drvr_priv;
1125
1126 brcmf_dbg(TRACE, "Enter\n");
1127
1128 if (drvr) {
1129 drvr_priv = drvr->info;
1130 if (drvr_priv) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001131 int i;
1132
Franky Lindfded552011-10-21 16:16:20 +02001133 /* make sure primary interface removed last */
1134 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001135 if (drvr_priv->iflist[i])
1136 brcmf_del_if(drvr_priv, i);
1137
Arend van Spriel5b435de2011-10-05 13:19:03 +02001138 cancel_work_sync(&drvr_priv->setmacaddr_work);
1139 cancel_work_sync(&drvr_priv->multicast_work);
1140
1141 brcmf_bus_detach(drvr);
1142
1143 if (drvr->prot)
1144 brcmf_proto_detach(drvr);
1145
Arend van Spriel5b435de2011-10-05 13:19:03 +02001146 kfree(drvr_priv);
1147 }
1148 }
1149}
1150
Arend van Spriel5b435de2011-10-05 13:19:03 +02001151int brcmf_os_proto_block(struct brcmf_pub *drvr)
1152{
1153 struct brcmf_info *drvr_priv = drvr->info;
1154
1155 if (drvr_priv) {
1156 mutex_lock(&drvr_priv->proto_block);
1157 return 1;
1158 }
1159 return 0;
1160}
1161
1162int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1163{
1164 struct brcmf_info *drvr_priv = drvr->info;
1165
1166 if (drvr_priv) {
1167 mutex_unlock(&drvr_priv->proto_block);
1168 return 1;
1169 }
1170
1171 return 0;
1172}
1173
1174static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1175{
1176 return atomic_read(&drvr_priv->pend_8021x_cnt);
1177}
1178
1179#define MAX_WAIT_FOR_8021X_TX 10
1180
1181int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1182{
Franky Line1b83582011-10-21 16:16:33 +02001183 struct brcmf_if *ifp = netdev_priv(ndev);
1184 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001185 int timeout = 10 * HZ / 1000;
1186 int ntimes = MAX_WAIT_FOR_8021X_TX;
1187 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1188
1189 while (ntimes && pend) {
1190 if (pend) {
1191 set_current_state(TASK_INTERRUPTIBLE);
1192 schedule_timeout(timeout);
1193 set_current_state(TASK_RUNNING);
1194 ntimes--;
1195 }
1196 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1197 }
1198 return pend;
1199}
1200
1201#ifdef BCMDBG
Arend van Sprielaf534952011-10-12 20:51:14 +02001202int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001203{
1204 int ret = 0;
1205 struct file *fp;
1206 mm_segment_t old_fs;
1207 loff_t pos = 0;
1208
1209 /* change to KERNEL_DS address limit */
1210 old_fs = get_fs();
1211 set_fs(KERNEL_DS);
1212
1213 /* open file to write */
1214 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1215 if (!fp) {
1216 brcmf_dbg(ERROR, "open file error\n");
1217 ret = -1;
1218 goto exit;
1219 }
1220
1221 /* Write buf to file */
Arend van Sprielaf534952011-10-12 20:51:14 +02001222 fp->f_op->write(fp, (char __user *)buf, size, &pos);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001223
1224exit:
1225 /* free buf before return */
1226 kfree(buf);
1227 /* close file before return */
1228 if (fp)
1229 filp_close(fp, current->files);
1230 /* restore previous address limit */
1231 set_fs(old_fs);
1232
1233 return ret;
1234}
1235#endif /* BCMDBG */