blob: 97211745fe36218b1e15e494332364bd61dab764 [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"
46#include "bcmchip.h"
47
48MODULE_AUTHOR("Broadcom Corporation");
49MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
50MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
51MODULE_LICENSE("Dual BSD/GPL");
52
53
54/* Interface control information */
55struct brcmf_if {
56 struct brcmf_info *info; /* back pointer to brcmf_info */
57 /* OS/stack specifics */
58 struct net_device *ndev;
59 struct net_device_stats stats;
60 int idx; /* iface idx in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +020061 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
62};
63
64/* Local private structure (extension of pub) */
65struct brcmf_info {
66 struct brcmf_pub pub;
67
68 /* OS/stack specifics */
69 struct brcmf_if *iflist[BRCMF_MAX_IFS];
70
71 struct mutex proto_block;
72
73 struct work_struct setmacaddr_work;
74 struct work_struct multicast_work;
75 u8 macvalue[ETH_ALEN];
76 atomic_t pend_8021x_cnt;
77};
78
79/* Error bits */
80module_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
435 skb->dev = ifp->ndev;
436 skb->protocol = eth_type_trans(skb, skb->dev);
437
438 if (skb->pkt_type == PACKET_MULTICAST)
439 drvr_priv->pub.rx_multicast++;
440
441 skb->data = eth;
442 skb->len = len;
443
444 /* Strip header, count, deliver upward */
445 skb_pull(skb, ETH_HLEN);
446
447 /* Process special event packets and then discard them */
448 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
449 brcmf_host_event(drvr_priv, &ifidx,
450 skb_mac_header(skb),
451 &event, &data);
452
Franky Lind1a5b6f2011-10-21 16:16:34 +0200453 if (drvr_priv->iflist[ifidx]) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200454 ifp = drvr_priv->iflist[ifidx];
Arend van Spriel5b435de2011-10-05 13:19:03 +0200455 ifp->ndev->last_rx = jiffies;
Franky Lind1a5b6f2011-10-21 16:16:34 +0200456 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200457
458 drvr->dstats.rx_bytes += skb->len;
459 drvr->rx_packets++; /* Local count */
460
461 if (in_interrupt())
462 netif_rx(skb);
463 else
464 /* If the receive is not processed inside an ISR,
465 * the softirqd must be woken explicitly to service
466 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
467 * by netif_rx_ni(), but in earlier kernels, we need
468 * to do it manually.
469 */
470 netif_rx_ni(skb);
471 }
472}
473
474void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
475{
476 uint ifidx;
477 struct brcmf_info *drvr_priv = drvr->info;
478 struct ethhdr *eh;
479 u16 type;
480
481 brcmf_proto_hdrpull(drvr, &ifidx, txp);
482
483 eh = (struct ethhdr *)(txp->data);
484 type = ntohs(eh->h_proto);
485
486 if (type == ETH_P_PAE)
487 atomic_dec(&drvr_priv->pend_8021x_cnt);
488
489}
490
491static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
492{
Franky Line1b83582011-10-21 16:16:33 +0200493 struct brcmf_if *ifp = netdev_priv(ndev);
494 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200495
496 brcmf_dbg(TRACE, "Enter\n");
497
Arend van Spriel5b435de2011-10-05 13:19:03 +0200498 if (drvr_priv->pub.up)
499 /* Use the protocol to get dongle stats */
500 brcmf_proto_dstats(&drvr_priv->pub);
501
502 /* Copy dongle stats to net device stats */
503 ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
504 ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
505 ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
506 ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
507 ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
508 ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
509 ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
510 ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
511 ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
512
513 return &ifp->stats;
514}
515
516/* Retrieve current toe component enables, which are kept
517 as a bitmap in toe_ol iovar */
518static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
519{
520 struct brcmf_dcmd dcmd;
Arend van Spriel10629042011-10-12 20:51:18 +0200521 __le32 toe_le;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200522 char buf[32];
523 int ret;
524
525 memset(&dcmd, 0, sizeof(dcmd));
526
527 dcmd.cmd = BRCMF_C_GET_VAR;
528 dcmd.buf = buf;
529 dcmd.len = (uint) sizeof(buf);
530 dcmd.set = false;
531
532 strcpy(buf, "toe_ol");
533 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
534 if (ret < 0) {
535 /* Check for older dongle image that doesn't support toe_ol */
536 if (ret == -EIO) {
537 brcmf_dbg(ERROR, "%s: toe not supported by device\n",
538 brcmf_ifname(&drvr_priv->pub, ifidx));
539 return -EOPNOTSUPP;
540 }
541
542 brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
543 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
544 return ret;
545 }
546
Arend van Spriel10629042011-10-12 20:51:18 +0200547 memcpy(&toe_le, buf, sizeof(u32));
548 *toe_ol = le32_to_cpu(toe_le);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200549 return 0;
550}
551
552/* Set current toe component enables in toe_ol iovar,
553 and set toe global enable iovar */
554static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
555{
556 struct brcmf_dcmd dcmd;
557 char buf[32];
Arend van Spriel10629042011-10-12 20:51:18 +0200558 int ret;
559 __le32 toe_le = cpu_to_le32(toe_ol);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200560
561 memset(&dcmd, 0, sizeof(dcmd));
562
563 dcmd.cmd = BRCMF_C_SET_VAR;
564 dcmd.buf = buf;
565 dcmd.len = (uint) sizeof(buf);
566 dcmd.set = true;
567
568 /* Set toe_ol as requested */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200569 strcpy(buf, "toe_ol");
Arend van Spriel10629042011-10-12 20:51:18 +0200570 memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200571
572 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
573 if (ret < 0) {
574 brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
575 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
576 return ret;
577 }
578
579 /* Enable toe globally only if any components are enabled. */
Arend van Spriel10629042011-10-12 20:51:18 +0200580 toe_le = cpu_to_le32(toe_ol != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200581
582 strcpy(buf, "toe");
Arend van Spriel10629042011-10-12 20:51:18 +0200583 memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200584
585 ret = brcmf_proto_dcmd(&drvr_priv->pub, ifidx, &dcmd, dcmd.len);
586 if (ret < 0) {
587 brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
588 brcmf_ifname(&drvr_priv->pub, ifidx), ret);
589 return ret;
590 }
591
592 return 0;
593}
594
595static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
596 struct ethtool_drvinfo *info)
597{
Franky Line1b83582011-10-21 16:16:33 +0200598 struct brcmf_if *ifp = netdev_priv(ndev);
599 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200600
601 sprintf(info->driver, KBUILD_MODNAME);
602 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
603 sprintf(info->fw_version, "%s", BCM4329_FW_NAME);
604 sprintf(info->bus_info, "%s",
605 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
606}
607
608static struct ethtool_ops brcmf_ethtool_ops = {
609 .get_drvinfo = brcmf_ethtool_get_drvinfo
610};
611
612static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
613{
614 struct ethtool_drvinfo info;
615 char drvname[sizeof(info.driver)];
616 u32 cmd;
617 struct ethtool_value edata;
618 u32 toe_cmpnt, csum_dir;
619 int ret;
620
621 brcmf_dbg(TRACE, "Enter\n");
622
623 /* all ethtool calls start with a cmd word */
624 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
625 return -EFAULT;
626
627 switch (cmd) {
628 case ETHTOOL_GDRVINFO:
629 /* Copy out any request driver name */
630 if (copy_from_user(&info, uaddr, sizeof(info)))
631 return -EFAULT;
632 strncpy(drvname, info.driver, sizeof(info.driver));
633 drvname[sizeof(info.driver) - 1] = '\0';
634
635 /* clear struct for return */
636 memset(&info, 0, sizeof(info));
637 info.cmd = cmd;
638
639 /* if requested, identify ourselves */
640 if (strcmp(drvname, "?dhd") == 0) {
641 sprintf(info.driver, "dhd");
642 strcpy(info.version, BRCMF_VERSION_STR);
643 }
644
645 /* otherwise, require dongle to be up */
646 else if (!drvr_priv->pub.up) {
647 brcmf_dbg(ERROR, "dongle is not up\n");
648 return -ENODEV;
649 }
650
651 /* finally, report dongle driver type */
652 else if (drvr_priv->pub.iswl)
653 sprintf(info.driver, "wl");
654 else
655 sprintf(info.driver, "xx");
656
657 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
658 if (copy_to_user(uaddr, &info, sizeof(info)))
659 return -EFAULT;
660 brcmf_dbg(CTL, "given %*s, returning %s\n",
661 (int)sizeof(drvname), drvname, info.driver);
662 break;
663
664 /* Get toe offload components from dongle */
665 case ETHTOOL_GRXCSUM:
666 case ETHTOOL_GTXCSUM:
667 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
668 if (ret < 0)
669 return ret;
670
671 csum_dir =
672 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
673
674 edata.cmd = cmd;
675 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
676
677 if (copy_to_user(uaddr, &edata, sizeof(edata)))
678 return -EFAULT;
679 break;
680
681 /* Set toe offload components in dongle */
682 case ETHTOOL_SRXCSUM:
683 case ETHTOOL_STXCSUM:
684 if (copy_from_user(&edata, uaddr, sizeof(edata)))
685 return -EFAULT;
686
687 /* Read the current settings, update and write back */
688 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
689 if (ret < 0)
690 return ret;
691
692 csum_dir =
693 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
694
695 if (edata.data != 0)
696 toe_cmpnt |= csum_dir;
697 else
698 toe_cmpnt &= ~csum_dir;
699
700 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
701 if (ret < 0)
702 return ret;
703
704 /* If setting TX checksum mode, tell Linux the new mode */
705 if (cmd == ETHTOOL_STXCSUM) {
706 if (edata.data)
707 drvr_priv->iflist[0]->ndev->features |=
708 NETIF_F_IP_CSUM;
709 else
710 drvr_priv->iflist[0]->ndev->features &=
711 ~NETIF_F_IP_CSUM;
712 }
713
714 break;
715
716 default:
717 return -EOPNOTSUPP;
718 }
719
720 return 0;
721}
722
723static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
724 int cmd)
725{
Franky Line1b83582011-10-21 16:16:33 +0200726 struct brcmf_if *ifp = netdev_priv(ndev);
727 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200728
Franky Line1b83582011-10-21 16:16:33 +0200729 brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200730
Franky Line1b83582011-10-21 16:16:33 +0200731 if (!drvr_priv->iflist[ifp->idx])
Arend van Spriel5b435de2011-10-05 13:19:03 +0200732 return -1;
733
734 if (cmd == SIOCETHTOOL)
735 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
736
737 return -EOPNOTSUPP;
738}
739
740/* called only from within this driver. Sends a command to the dongle. */
741s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
742{
743 struct brcmf_dcmd dcmd;
744 s32 err = 0;
745 int buflen = 0;
746 bool is_set_key_cmd;
Franky Line1b83582011-10-21 16:16:33 +0200747 struct brcmf_if *ifp = netdev_priv(ndev);
748 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200749
750 memset(&dcmd, 0, sizeof(dcmd));
751 dcmd.cmd = cmd;
752 dcmd.buf = arg;
753 dcmd.len = len;
754
Arend van Spriel5b435de2011-10-05 13:19:03 +0200755 if (dcmd.buf != NULL)
756 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
757
758 /* send to dongle (must be up, and wl) */
759 if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
760 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
761 err = -EIO;
762 goto done;
763 }
764
765 if (!drvr_priv->pub.iswl) {
766 err = -EIO;
767 goto done;
768 }
769
770 /*
771 * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
772 * set key CMD to prevent M4 encryption.
773 */
774 is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
775 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
776 !(strncmp("wsec_key", dcmd.buf, 9))) ||
777 ((dcmd.cmd == BRCMF_C_SET_VAR) &&
778 !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
779 if (is_set_key_cmd)
780 brcmf_netdev_wait_pend8021x(ndev);
781
Franky Line1b83582011-10-21 16:16:33 +0200782 err = brcmf_proto_dcmd(&drvr_priv->pub, ifp->idx, &dcmd, buflen);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200783
784done:
785 if (err > 0)
786 err = 0;
787
788 return err;
789}
790
791static int brcmf_netdev_stop(struct net_device *ndev)
792{
Franky Line1b83582011-10-21 16:16:33 +0200793 struct brcmf_if *ifp = netdev_priv(ndev);
794 struct brcmf_pub *drvr = &ifp->info->pub;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200795
796 brcmf_dbg(TRACE, "Enter\n");
797 brcmf_cfg80211_down(drvr->config);
798 if (drvr->up == 0)
799 return 0;
800
801 /* Set state and stop OS transmissions */
802 drvr->up = 0;
803 netif_stop_queue(ndev);
804
805 return 0;
806}
807
808static int brcmf_netdev_open(struct net_device *ndev)
809{
Franky Line1b83582011-10-21 16:16:33 +0200810 struct brcmf_if *ifp = netdev_priv(ndev);
811 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200812 u32 toe_ol;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200813 s32 ret = 0;
814
Franky Line1b83582011-10-21 16:16:33 +0200815 brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200816
Franky Line1b83582011-10-21 16:16:33 +0200817 if (ifp->idx == 0) { /* do it only for primary eth0 */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200818 /* try to bring up bus */
819 ret = brcmf_bus_start(&drvr_priv->pub);
820 if (ret != 0) {
821 brcmf_dbg(ERROR, "failed with code %d\n", ret);
822 return -1;
823 }
824 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
825
826 memcpy(ndev->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
827
828 /* Get current TOE mode from dongle */
Franky Line1b83582011-10-21 16:16:33 +0200829 if (brcmf_toe_get(drvr_priv, ifp->idx, &toe_ol) >= 0
Arend van Spriel5b435de2011-10-05 13:19:03 +0200830 && (toe_ol & TOE_TX_CSUM_OL) != 0)
Franky Line1b83582011-10-21 16:16:33 +0200831 drvr_priv->iflist[ifp->idx]->ndev->features |=
Arend van Spriel5b435de2011-10-05 13:19:03 +0200832 NETIF_F_IP_CSUM;
833 else
Franky Line1b83582011-10-21 16:16:33 +0200834 drvr_priv->iflist[ifp->idx]->ndev->features &=
Arend van Spriel5b435de2011-10-05 13:19:03 +0200835 ~NETIF_F_IP_CSUM;
836 }
837 /* Allow transmit calls */
838 netif_start_queue(ndev);
839 drvr_priv->pub.up = 1;
840 if (brcmf_cfg80211_up(drvr_priv->pub.config)) {
841 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
842 return -1;
843 }
844
845 return ret;
846}
847
Franky Lindfded552011-10-21 16:16:20 +0200848static const struct net_device_ops brcmf_netdev_ops_pri = {
849 .ndo_open = brcmf_netdev_open,
850 .ndo_stop = brcmf_netdev_stop,
851 .ndo_get_stats = brcmf_netdev_get_stats,
852 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
853 .ndo_start_xmit = brcmf_netdev_start_xmit,
854 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
855 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
856};
857
Arend van Spriel5b435de2011-10-05 13:19:03 +0200858int
Franky Lin15d45b62011-10-21 16:16:32 +0200859brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, char *name, u8 *mac_addr)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200860{
861 struct brcmf_if *ifp;
Franky Line1b83582011-10-21 16:16:33 +0200862 struct net_device *ndev;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200863
Franky Lin15d45b62011-10-21 16:16:32 +0200864 brcmf_dbg(TRACE, "idx %d\n", ifidx);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200865
866 ifp = drvr_priv->iflist[ifidx];
Franky Line1b83582011-10-21 16:16:33 +0200867 /*
868 * Delete the existing interface before overwriting it
869 * in case we missed the BRCMF_E_IF_DEL event.
870 */
871 if (ifp) {
872 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
873 ifp->ndev->name);
874 netif_stop_queue(ifp->ndev);
875 unregister_netdev(ifp->ndev);
876 free_netdev(ifp->ndev);
877 drvr_priv->iflist[ifidx] = NULL;
Franky Lin15d45b62011-10-21 16:16:32 +0200878 }
Franky Line1b83582011-10-21 16:16:33 +0200879
880 /* Allocate netdev, including space for private structure */
881 ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
882 if (!ndev) {
883 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
884 return -ENOMEM;
885 }
886
887 ifp = netdev_priv(ndev);
888 ifp->ndev = ndev;
Franky Lin15d45b62011-10-21 16:16:32 +0200889 ifp->info = drvr_priv;
890 drvr_priv->iflist[ifidx] = ifp;
Franky Lin15d45b62011-10-21 16:16:32 +0200891 ifp->idx = ifidx;
Franky Lin15d45b62011-10-21 16:16:32 +0200892 if (mac_addr != NULL)
893 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200894
Franky Lin15d45b62011-10-21 16:16:32 +0200895 if (brcmf_net_attach(&drvr_priv->pub, ifp->idx)) {
896 brcmf_dbg(ERROR, "brcmf_net_attach failed");
897 free_netdev(ifp->ndev);
Franky Line1b83582011-10-21 16:16:33 +0200898 drvr_priv->iflist[ifidx] = NULL;
899 return -EOPNOTSUPP;
Franky Lin15d45b62011-10-21 16:16:32 +0200900 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200901
Franky Lin15d45b62011-10-21 16:16:32 +0200902 brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
903 current->pid, ifp->ndev->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200904
905 return 0;
906}
907
908void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
909{
910 struct brcmf_if *ifp;
911
912 brcmf_dbg(TRACE, "idx %d\n", ifidx);
913
914 ifp = drvr_priv->iflist[ifidx];
915 if (!ifp) {
916 brcmf_dbg(ERROR, "Null interface\n");
917 return;
918 }
Franky Lindfded552011-10-21 16:16:20 +0200919 if (ifp->ndev) {
920 if (ifidx == 0) {
921 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
922 rtnl_lock();
923 brcmf_netdev_stop(ifp->ndev);
924 rtnl_unlock();
925 }
926 } else {
927 netif_stop_queue(ifp->ndev);
928 }
929
Arend van Spriel5b435de2011-10-05 13:19:03 +0200930 unregister_netdev(ifp->ndev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200931 drvr_priv->iflist[ifidx] = NULL;
Franky Lindfded552011-10-21 16:16:20 +0200932 if (ifidx == 0)
933 brcmf_cfg80211_detach(drvr_priv->pub.config);
934 free_netdev(ifp->ndev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200935 }
936}
937
938struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
939{
940 struct brcmf_info *drvr_priv = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200941
942 brcmf_dbg(TRACE, "Enter\n");
943
Arend van Spriel5b435de2011-10-05 13:19:03 +0200944 /* Allocate primary brcmf_info */
945 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
946 if (!drvr_priv)
947 goto fail;
948
Arend van Spriel5b435de2011-10-05 13:19:03 +0200949 mutex_init(&drvr_priv->proto_block);
950
951 /* Link to info module */
952 drvr_priv->pub.info = drvr_priv;
953
954 /* Link to bus module */
955 drvr_priv->pub.bus = bus;
956 drvr_priv->pub.hdrlen = bus_hdrlen;
957
958 /* Attach and link in the protocol */
959 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
960 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
961 goto fail;
962 }
963
Arend van Spriel5b435de2011-10-05 13:19:03 +0200964 INIT_WORK(&drvr_priv->setmacaddr_work, _brcmf_set_mac_address);
965 INIT_WORK(&drvr_priv->multicast_work, _brcmf_set_multicast_list);
966
Arend van Spriel5b435de2011-10-05 13:19:03 +0200967 return &drvr_priv->pub;
968
969fail:
Arend van Spriel5b435de2011-10-05 13:19:03 +0200970 if (drvr_priv)
971 brcmf_detach(&drvr_priv->pub);
972
973 return NULL;
974}
975
976int brcmf_bus_start(struct brcmf_pub *drvr)
977{
978 int ret = -1;
979 struct brcmf_info *drvr_priv = drvr->info;
980 /* Room for "event_msgs" + '\0' + bitvec */
981 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
982
983 brcmf_dbg(TRACE, "\n");
984
985 /* Bring up the bus */
986 ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub);
987 if (ret != 0) {
988 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
989 return ret;
990 }
991
992 /* If bus is not ready, can't come up */
993 if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
994 brcmf_dbg(ERROR, "failed bus is not ready\n");
995 return -ENODEV;
996 }
997
Alwin Beukers53a22772011-10-12 20:51:30 +0200998 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200999 iovbuf, sizeof(iovbuf));
1000 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1001 sizeof(iovbuf));
1002 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1003
1004 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1005 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1006 setbit(drvr->eventmask, BRCMF_E_AUTH);
1007 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1008 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1009 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1010 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1011 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1012 setbit(drvr->eventmask, BRCMF_E_JOIN);
1013 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1014 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1015 setbit(drvr->eventmask, BRCMF_E_LINK);
1016 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1017 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1018 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1019 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1020 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1021 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1022
1023/* enable dongle roaming event */
1024
1025 drvr->pktfilter_count = 1;
1026 /* Setup filter to allow only unicast */
1027 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1028
1029 /* Bus is ready, do any protocol initialization */
1030 ret = brcmf_proto_init(&drvr_priv->pub);
1031 if (ret < 0)
1032 return ret;
1033
1034 return 0;
1035}
1036
Arend van Spriel5b435de2011-10-05 13:19:03 +02001037int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1038{
1039 struct brcmf_info *drvr_priv = drvr->info;
1040 struct net_device *ndev;
1041 u8 temp_addr[ETH_ALEN] = {
1042 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1043
1044 brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1045
1046 ndev = drvr_priv->iflist[ifidx]->ndev;
1047 ndev->netdev_ops = &brcmf_netdev_ops_pri;
1048
1049 /*
1050 * We have to use the primary MAC for virtual interfaces
1051 */
1052 if (ifidx != 0) {
1053 /* for virtual interfaces use the primary MAC */
1054 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1055
1056 }
1057
1058 if (ifidx == 1) {
1059 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1060 /* ACCESSPOINT INTERFACE CASE */
1061 temp_addr[0] |= 0X02; /* set bit 2 ,
1062 - Locally Administered address */
1063
1064 }
1065 ndev->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1066 ndev->ethtool_ops = &brcmf_ethtool_ops;
1067
1068 drvr_priv->pub.rxsz = ndev->mtu + ndev->hard_header_len +
1069 drvr_priv->pub.hdrlen;
1070
1071 memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
1072
Franky Lin15d45b62011-10-21 16:16:32 +02001073 /* attach to cfg80211 for primary interface */
1074 if (!ifidx) {
1075 drvr->config =
1076 brcmf_cfg80211_attach(ndev,
1077 brcmf_bus_get_device(drvr->bus),
1078 drvr);
1079 if (drvr->config == NULL) {
1080 brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1081 goto fail;
1082 }
1083 }
1084
Arend van Spriel5b435de2011-10-05 13:19:03 +02001085 if (register_netdev(ndev) != 0) {
1086 brcmf_dbg(ERROR, "couldn't register the net device\n");
1087 goto fail;
1088 }
1089
1090 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
1091
1092 return 0;
1093
1094fail:
1095 ndev->netdev_ops = NULL;
1096 return -EBADE;
1097}
1098
1099static void brcmf_bus_detach(struct brcmf_pub *drvr)
1100{
1101 struct brcmf_info *drvr_priv;
1102
1103 brcmf_dbg(TRACE, "Enter\n");
1104
1105 if (drvr) {
1106 drvr_priv = drvr->info;
1107 if (drvr_priv) {
1108 /* Stop the protocol module */
1109 brcmf_proto_stop(&drvr_priv->pub);
1110
1111 /* Stop the bus module */
1112 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus);
1113 }
1114 }
1115}
1116
1117void brcmf_detach(struct brcmf_pub *drvr)
1118{
1119 struct brcmf_info *drvr_priv;
1120
1121 brcmf_dbg(TRACE, "Enter\n");
1122
1123 if (drvr) {
1124 drvr_priv = drvr->info;
1125 if (drvr_priv) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001126 int i;
1127
Franky Lindfded552011-10-21 16:16:20 +02001128 /* make sure primary interface removed last */
1129 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001130 if (drvr_priv->iflist[i])
1131 brcmf_del_if(drvr_priv, i);
1132
Arend van Spriel5b435de2011-10-05 13:19:03 +02001133 cancel_work_sync(&drvr_priv->setmacaddr_work);
1134 cancel_work_sync(&drvr_priv->multicast_work);
1135
1136 brcmf_bus_detach(drvr);
1137
1138 if (drvr->prot)
1139 brcmf_proto_detach(drvr);
1140
Arend van Spriel5b435de2011-10-05 13:19:03 +02001141 kfree(drvr_priv);
1142 }
1143 }
1144}
1145
1146static void __exit brcmf_module_cleanup(void)
1147{
1148 brcmf_dbg(TRACE, "Enter\n");
1149
1150 brcmf_bus_unregister();
1151}
1152
1153static int __init brcmf_module_init(void)
1154{
1155 int error;
1156
1157 brcmf_dbg(TRACE, "Enter\n");
1158
1159 error = brcmf_bus_register();
1160
1161 if (error) {
1162 brcmf_dbg(ERROR, "brcmf_bus_register failed\n");
1163 goto failed;
1164 }
1165 return 0;
1166
1167failed:
1168 return -EINVAL;
1169}
1170
1171module_init(brcmf_module_init);
1172module_exit(brcmf_module_cleanup);
1173
1174int brcmf_os_proto_block(struct brcmf_pub *drvr)
1175{
1176 struct brcmf_info *drvr_priv = drvr->info;
1177
1178 if (drvr_priv) {
1179 mutex_lock(&drvr_priv->proto_block);
1180 return 1;
1181 }
1182 return 0;
1183}
1184
1185int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1186{
1187 struct brcmf_info *drvr_priv = drvr->info;
1188
1189 if (drvr_priv) {
1190 mutex_unlock(&drvr_priv->proto_block);
1191 return 1;
1192 }
1193
1194 return 0;
1195}
1196
1197static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1198{
1199 return atomic_read(&drvr_priv->pend_8021x_cnt);
1200}
1201
1202#define MAX_WAIT_FOR_8021X_TX 10
1203
1204int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1205{
Franky Line1b83582011-10-21 16:16:33 +02001206 struct brcmf_if *ifp = netdev_priv(ndev);
1207 struct brcmf_info *drvr_priv = ifp->info;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001208 int timeout = 10 * HZ / 1000;
1209 int ntimes = MAX_WAIT_FOR_8021X_TX;
1210 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1211
1212 while (ntimes && pend) {
1213 if (pend) {
1214 set_current_state(TASK_INTERRUPTIBLE);
1215 schedule_timeout(timeout);
1216 set_current_state(TASK_RUNNING);
1217 ntimes--;
1218 }
1219 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1220 }
1221 return pend;
1222}
1223
1224#ifdef BCMDBG
Arend van Sprielaf534952011-10-12 20:51:14 +02001225int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001226{
1227 int ret = 0;
1228 struct file *fp;
1229 mm_segment_t old_fs;
1230 loff_t pos = 0;
1231
1232 /* change to KERNEL_DS address limit */
1233 old_fs = get_fs();
1234 set_fs(KERNEL_DS);
1235
1236 /* open file to write */
1237 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1238 if (!fp) {
1239 brcmf_dbg(ERROR, "open file error\n");
1240 ret = -1;
1241 goto exit;
1242 }
1243
1244 /* Write buf to file */
Arend van Sprielaf534952011-10-12 20:51:14 +02001245 fp->f_op->write(fp, (char __user *)buf, size, &pos);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001246
1247exit:
1248 /* free buf before return */
1249 kfree(buf);
1250 /* close file before return */
1251 if (fp)
1252 filp_close(fp, current->files);
1253 /* restore previous address limit */
1254 set_fs(old_fs);
1255
1256 return ret;
1257}
1258#endif /* BCMDBG */