brcm80211: fmac: change function bus_init parameter
[pandora-kernel.git] / drivers / net / wireless / brcm80211 / brcmfmac / dhd_linux.c
1 /*
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>
34 #include <linux/module.h>
35 #include <net/cfg80211.h>
36 #include <net/rtnetlink.h>
37 #include <defs.h>
38 #include <brcmu_utils.h>
39 #include <brcmu_wifi.h>
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
47 MODULE_AUTHOR("Broadcom Corporation");
48 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
49 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
50 MODULE_LICENSE("Dual BSD/GPL");
51
52
53 /* Interface control information */
54 struct brcmf_if {
55         struct brcmf_info *info;        /* back pointer to brcmf_info */
56         /* OS/stack specifics */
57         struct net_device *ndev;
58         struct net_device_stats stats;
59         int idx;                /* iface idx in dongle */
60         u8 mac_addr[ETH_ALEN];  /* assigned MAC address */
61 };
62
63 /* Local private structure (extension of pub) */
64 struct 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 */
79 int brcmf_msg_level = BRCMF_ERROR_VAL;
80 module_param(brcmf_msg_level, int, 0);
81
82 int 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
101 char *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
121 static void _brcmf_set_multicast_list(struct work_struct *work)
122 {
123         struct net_device *ndev;
124         struct netdev_hw_addr *ha;
125         u32 dcmd_value, cnt;
126         __le32 cnt_le;
127         __le32 dcmd_le_value;
128
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 */
141         dcmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
142
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);
175                 dcmd_value = cnt ? true : dcmd_value;
176         }
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
185         buflen = sizeof("allmulti") + sizeof(dcmd_value);
186         buf = kmalloc(buflen, GFP_ATOMIC);
187         if (!buf)
188                 return;
189
190         dcmd_le_value = cpu_to_le32(dcmd_value);
191
192         if (!brcmf_c_mkiovar
193             ("allmulti", (void *)&dcmd_le_value,
194             sizeof(dcmd_le_value), buf, buflen)) {
195                 brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
196                           brcmf_ifname(&drvr_priv->pub, 0),
197                           (int)sizeof(dcmd_value), buflen);
198                 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),
212                           le32_to_cpu(dcmd_le_value));
213         }
214
215         kfree(buf);
216
217         /* Finally, pick up the PROMISC flag as well, like the NIC
218                  driver does */
219
220         dcmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
221         dcmd_le_value = cpu_to_le32(dcmd_value);
222
223         memset(&dcmd, 0, sizeof(dcmd));
224         dcmd.cmd = BRCMF_C_SET_PROMISC;
225         dcmd.buf = &dcmd_le_value;
226         dcmd.len = sizeof(dcmd_le_value);
227         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),
233                           le32_to_cpu(dcmd_le_value));
234         }
235 }
236
237 static 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");
248         if (!brcmf_c_mkiovar("cur_etheraddr", (char *)drvr_priv->macvalue,
249                            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
271 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
272 {
273         struct brcmf_if *ifp = netdev_priv(ndev);
274         struct brcmf_info *drvr_priv = ifp->info;
275         struct sockaddr *sa = (struct sockaddr *)addr;
276
277         memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
278         schedule_work(&drvr_priv->setmacaddr_work);
279         return 0;
280 }
281
282 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
283 {
284         struct brcmf_if *ifp = netdev_priv(ndev);
285         struct brcmf_info *drvr_priv = ifp->info;
286
287         schedule_work(&drvr_priv->multicast_work);
288 }
289
290 int 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->bus_if->state == 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->dev, pktbuf);
314 }
315
316 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
317 {
318         int ret;
319         struct brcmf_if *ifp = netdev_priv(ndev);
320         struct brcmf_info *drvr_priv = ifp->info;
321
322         brcmf_dbg(TRACE, "Enter\n");
323
324         /* Reject if down */
325         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);
330                 netif_stop_queue(ndev);
331                 return -ENODEV;
332         }
333
334         if (!drvr_priv->iflist[ifp->idx]) {
335                 brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
336                 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",
345                           brcmf_ifname(&drvr_priv->pub, ifp->idx));
346                 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",
352                                   brcmf_ifname(&drvr_priv->pub, ifp->idx));
353                         ret = -ENOMEM;
354                         goto done;
355                 }
356         }
357
358         ret = brcmf_sendpkt(&drvr_priv->pub, ifp->idx, skb);
359
360 done:
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
370 void 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
385 static 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
402 void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx,
403                     struct sk_buff_head *skb_list)
404 {
405         struct brcmf_info *drvr_priv = drvr->info;
406         unsigned char *eth;
407         uint len;
408         void *data;
409         struct sk_buff *skb, *pnext;
410         struct brcmf_if *ifp;
411         struct brcmf_event_msg event;
412
413         brcmf_dbg(TRACE, "Enter\n");
414
415         skb_queue_walk_safe(skb_list, skb, pnext) {
416                 skb_unlink(skb, skb_list);
417
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
437                 if (!ifp || !ifp->ndev ||
438                     ifp->ndev->reg_state != NETREG_REGISTERED) {
439                         brcmu_pkt_buf_free_skb(skb);
440                         continue;
441                 }
442
443                 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
461                 if (drvr_priv->iflist[ifidx]) {
462                         ifp = drvr_priv->iflist[ifidx];
463                         ifp->ndev->last_rx = jiffies;
464                 }
465
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
482 void 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
499 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
500 {
501         struct brcmf_if *ifp = netdev_priv(ndev);
502         struct brcmf_info *drvr_priv = ifp->info;
503
504         brcmf_dbg(TRACE, "Enter\n");
505
506         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 */
526 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
527 {
528         struct brcmf_dcmd dcmd;
529         __le32 toe_le;
530         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
555         memcpy(&toe_le, buf, sizeof(u32));
556         *toe_ol = le32_to_cpu(toe_le);
557         return 0;
558 }
559
560 /* Set current toe component enables in toe_ol iovar,
561          and set toe global enable iovar */
562 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
563 {
564         struct brcmf_dcmd dcmd;
565         char buf[32];
566         int ret;
567         __le32 toe_le = cpu_to_le32(toe_ol);
568
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 */
577         strcpy(buf, "toe_ol");
578         memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
579
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. */
588         toe_le = cpu_to_le32(toe_ol != 0);
589
590         strcpy(buf, "toe");
591         memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
592
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
603 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
604                                     struct ethtool_drvinfo *info)
605 {
606         struct brcmf_if *ifp = netdev_priv(ndev);
607         struct brcmf_info *drvr_priv = ifp->info;
608
609         sprintf(info->driver, KBUILD_MODNAME);
610         sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
611         sprintf(info->bus_info, "%s",
612                 dev_name(brcmf_bus_get_device(drvr_priv->pub.bus)));
613 }
614
615 static struct ethtool_ops brcmf_ethtool_ops = {
616         .get_drvinfo = brcmf_ethtool_get_drvinfo
617 };
618
619 static int brcmf_ethtool(struct brcmf_info *drvr_priv, void __user *uaddr)
620 {
621         struct ethtool_drvinfo info;
622         char drvname[sizeof(info.driver)];
623         u32 cmd;
624         struct ethtool_value edata;
625         u32 toe_cmpnt, csum_dir;
626         int ret;
627
628         brcmf_dbg(TRACE, "Enter\n");
629
630         /* all ethtool calls start with a cmd word */
631         if (copy_from_user(&cmd, uaddr, sizeof(u32)))
632                 return -EFAULT;
633
634         switch (cmd) {
635         case ETHTOOL_GDRVINFO:
636                 /* Copy out any request driver name */
637                 if (copy_from_user(&info, uaddr, sizeof(info)))
638                         return -EFAULT;
639                 strncpy(drvname, info.driver, sizeof(info.driver));
640                 drvname[sizeof(info.driver) - 1] = '\0';
641
642                 /* clear struct for return */
643                 memset(&info, 0, sizeof(info));
644                 info.cmd = cmd;
645
646                 /* if requested, identify ourselves */
647                 if (strcmp(drvname, "?dhd") == 0) {
648                         sprintf(info.driver, "dhd");
649                         strcpy(info.version, BRCMF_VERSION_STR);
650                 }
651
652                 /* otherwise, require dongle to be up */
653                 else if (!drvr_priv->pub.up) {
654                         brcmf_dbg(ERROR, "dongle is not up\n");
655                         return -ENODEV;
656                 }
657
658                 /* finally, report dongle driver type */
659                 else if (drvr_priv->pub.iswl)
660                         sprintf(info.driver, "wl");
661                 else
662                         sprintf(info.driver, "xx");
663
664                 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
665                 if (copy_to_user(uaddr, &info, sizeof(info)))
666                         return -EFAULT;
667                 brcmf_dbg(CTL, "given %*s, returning %s\n",
668                           (int)sizeof(drvname), drvname, info.driver);
669                 break;
670
671                 /* Get toe offload components from dongle */
672         case ETHTOOL_GRXCSUM:
673         case ETHTOOL_GTXCSUM:
674                 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
675                 if (ret < 0)
676                         return ret;
677
678                 csum_dir =
679                     (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
680
681                 edata.cmd = cmd;
682                 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
683
684                 if (copy_to_user(uaddr, &edata, sizeof(edata)))
685                         return -EFAULT;
686                 break;
687
688                 /* Set toe offload components in dongle */
689         case ETHTOOL_SRXCSUM:
690         case ETHTOOL_STXCSUM:
691                 if (copy_from_user(&edata, uaddr, sizeof(edata)))
692                         return -EFAULT;
693
694                 /* Read the current settings, update and write back */
695                 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
696                 if (ret < 0)
697                         return ret;
698
699                 csum_dir =
700                     (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
701
702                 if (edata.data != 0)
703                         toe_cmpnt |= csum_dir;
704                 else
705                         toe_cmpnt &= ~csum_dir;
706
707                 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
708                 if (ret < 0)
709                         return ret;
710
711                 /* If setting TX checksum mode, tell Linux the new mode */
712                 if (cmd == ETHTOOL_STXCSUM) {
713                         if (edata.data)
714                                 drvr_priv->iflist[0]->ndev->features |=
715                                     NETIF_F_IP_CSUM;
716                         else
717                                 drvr_priv->iflist[0]->ndev->features &=
718                                     ~NETIF_F_IP_CSUM;
719                 }
720
721                 break;
722
723         default:
724                 return -EOPNOTSUPP;
725         }
726
727         return 0;
728 }
729
730 static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
731                                     int cmd)
732 {
733         struct brcmf_if *ifp = netdev_priv(ndev);
734         struct brcmf_info *drvr_priv = ifp->info;
735
736         brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
737
738         if (!drvr_priv->iflist[ifp->idx])
739                 return -1;
740
741         if (cmd == SIOCETHTOOL)
742                 return brcmf_ethtool(drvr_priv, ifr->ifr_data);
743
744         return -EOPNOTSUPP;
745 }
746
747 /* called only from within this driver. Sends a command to the dongle. */
748 s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
749 {
750         struct brcmf_dcmd dcmd;
751         s32 err = 0;
752         int buflen = 0;
753         bool is_set_key_cmd;
754         struct brcmf_if *ifp = netdev_priv(ndev);
755         struct brcmf_info *drvr_priv = ifp->info;
756
757         memset(&dcmd, 0, sizeof(dcmd));
758         dcmd.cmd = cmd;
759         dcmd.buf = arg;
760         dcmd.len = len;
761
762         if (dcmd.buf != NULL)
763                 buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
764
765         /* send to dongle (must be up, and wl) */
766         if ((drvr_priv->pub.bus_if->state != BRCMF_BUS_DATA)) {
767                 brcmf_dbg(ERROR, "DONGLE_DOWN\n");
768                 err = -EIO;
769                 goto done;
770         }
771
772         if (!drvr_priv->pub.iswl) {
773                 err = -EIO;
774                 goto done;
775         }
776
777         /*
778          * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
779          * set key CMD to prevent M4 encryption.
780          */
781         is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
782                           ((dcmd.cmd == BRCMF_C_SET_VAR) &&
783                            !(strncmp("wsec_key", dcmd.buf, 9))) ||
784                           ((dcmd.cmd == BRCMF_C_SET_VAR) &&
785                            !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
786         if (is_set_key_cmd)
787                 brcmf_netdev_wait_pend8021x(ndev);
788
789         err = brcmf_proto_dcmd(&drvr_priv->pub, ifp->idx, &dcmd, buflen);
790
791 done:
792         if (err > 0)
793                 err = 0;
794
795         return err;
796 }
797
798 static int brcmf_netdev_stop(struct net_device *ndev)
799 {
800         struct brcmf_if *ifp = netdev_priv(ndev);
801         struct brcmf_pub *drvr = &ifp->info->pub;
802
803         brcmf_dbg(TRACE, "Enter\n");
804         brcmf_cfg80211_down(drvr->config);
805         if (drvr->up == 0)
806                 return 0;
807
808         /* Set state and stop OS transmissions */
809         drvr->up = 0;
810         netif_stop_queue(ndev);
811
812         return 0;
813 }
814
815 static int brcmf_netdev_open(struct net_device *ndev)
816 {
817         struct brcmf_if *ifp = netdev_priv(ndev);
818         struct brcmf_info *drvr_priv = ifp->info;
819         u32 toe_ol;
820         s32 ret = 0;
821
822         brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
823
824         if (ifp->idx == 0) {    /* do it only for primary eth0 */
825                 /* try to bring up bus */
826                 ret = brcmf_bus_start(&drvr_priv->pub);
827                 if (ret != 0) {
828                         brcmf_dbg(ERROR, "failed with code %d\n", ret);
829                         return -1;
830                 }
831                 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
832
833                 memcpy(ndev->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
834
835                 /* Get current TOE mode from dongle */
836                 if (brcmf_toe_get(drvr_priv, ifp->idx, &toe_ol) >= 0
837                     && (toe_ol & TOE_TX_CSUM_OL) != 0)
838                         drvr_priv->iflist[ifp->idx]->ndev->features |=
839                                 NETIF_F_IP_CSUM;
840                 else
841                         drvr_priv->iflist[ifp->idx]->ndev->features &=
842                                 ~NETIF_F_IP_CSUM;
843         }
844         /* Allow transmit calls */
845         netif_start_queue(ndev);
846         drvr_priv->pub.up = 1;
847         if (brcmf_cfg80211_up(drvr_priv->pub.config)) {
848                 brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
849                 return -1;
850         }
851
852         return ret;
853 }
854
855 static const struct net_device_ops brcmf_netdev_ops_pri = {
856         .ndo_open = brcmf_netdev_open,
857         .ndo_stop = brcmf_netdev_stop,
858         .ndo_get_stats = brcmf_netdev_get_stats,
859         .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
860         .ndo_start_xmit = brcmf_netdev_start_xmit,
861         .ndo_set_mac_address = brcmf_netdev_set_mac_address,
862         .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
863 };
864
865 int
866 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, char *name, u8 *mac_addr)
867 {
868         struct brcmf_if *ifp;
869         struct net_device *ndev;
870
871         brcmf_dbg(TRACE, "idx %d\n", ifidx);
872
873         ifp = drvr_priv->iflist[ifidx];
874         /*
875          * Delete the existing interface before overwriting it
876          * in case we missed the BRCMF_E_IF_DEL event.
877          */
878         if (ifp) {
879                 brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
880                           ifp->ndev->name);
881                 netif_stop_queue(ifp->ndev);
882                 unregister_netdev(ifp->ndev);
883                 free_netdev(ifp->ndev);
884                 drvr_priv->iflist[ifidx] = NULL;
885         }
886
887         /* Allocate netdev, including space for private structure */
888         ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
889         if (!ndev) {
890                 brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
891                 return -ENOMEM;
892         }
893
894         ifp = netdev_priv(ndev);
895         ifp->ndev = ndev;
896         ifp->info = drvr_priv;
897         drvr_priv->iflist[ifidx] = ifp;
898         ifp->idx = ifidx;
899         if (mac_addr != NULL)
900                 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
901
902         if (brcmf_net_attach(&drvr_priv->pub, ifp->idx)) {
903                 brcmf_dbg(ERROR, "brcmf_net_attach failed");
904                 free_netdev(ifp->ndev);
905                 drvr_priv->iflist[ifidx] = NULL;
906                 return -EOPNOTSUPP;
907         }
908
909         brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
910                   current->pid, ifp->ndev->name);
911
912         return 0;
913 }
914
915 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
916 {
917         struct brcmf_if *ifp;
918
919         brcmf_dbg(TRACE, "idx %d\n", ifidx);
920
921         ifp = drvr_priv->iflist[ifidx];
922         if (!ifp) {
923                 brcmf_dbg(ERROR, "Null interface\n");
924                 return;
925         }
926         if (ifp->ndev) {
927                 if (ifidx == 0) {
928                         if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
929                                 rtnl_lock();
930                                 brcmf_netdev_stop(ifp->ndev);
931                                 rtnl_unlock();
932                         }
933                 } else {
934                         netif_stop_queue(ifp->ndev);
935                 }
936
937                 unregister_netdev(ifp->ndev);
938                 drvr_priv->iflist[ifidx] = NULL;
939                 if (ifidx == 0)
940                         brcmf_cfg80211_detach(drvr_priv->pub.config);
941                 free_netdev(ifp->ndev);
942         }
943 }
944
945 struct brcmf_pub *brcmf_attach(struct brcmf_sdio *bus, uint bus_hdrlen,
946                                struct device *dev)
947 {
948         struct brcmf_info *drvr_priv = NULL;
949
950         brcmf_dbg(TRACE, "Enter\n");
951
952         /* Allocate primary brcmf_info */
953         drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
954         if (!drvr_priv)
955                 goto fail;
956
957         mutex_init(&drvr_priv->proto_block);
958
959         /* Link to info module */
960         drvr_priv->pub.info = drvr_priv;
961
962         /* Link to bus module */
963         drvr_priv->pub.bus = bus;
964         drvr_priv->pub.hdrlen = bus_hdrlen;
965         drvr_priv->pub.bus_if = dev_get_drvdata(dev);
966         drvr_priv->pub.dev = dev;
967
968         /* Attach and link in the protocol */
969         if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
970                 brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
971                 goto fail;
972         }
973
974         INIT_WORK(&drvr_priv->setmacaddr_work, _brcmf_set_mac_address);
975         INIT_WORK(&drvr_priv->multicast_work, _brcmf_set_multicast_list);
976
977         return &drvr_priv->pub;
978
979 fail:
980         if (drvr_priv)
981                 brcmf_detach(&drvr_priv->pub);
982
983         return NULL;
984 }
985
986 int brcmf_bus_start(struct brcmf_pub *drvr)
987 {
988         int ret = -1;
989         struct brcmf_info *drvr_priv = drvr->info;
990         /* Room for "event_msgs" + '\0' + bitvec */
991         char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
992
993         brcmf_dbg(TRACE, "\n");
994
995         /* Bring up the bus */
996         ret = brcmf_sdbrcm_bus_init(drvr_priv->pub.dev);
997         if (ret != 0) {
998                 brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
999                 return ret;
1000         }
1001
1002         /* If bus is not ready, can't come up */
1003         if (drvr_priv->pub.bus_if->state != BRCMF_BUS_DATA) {
1004                 brcmf_dbg(ERROR, "failed bus is not ready\n");
1005                 return -ENODEV;
1006         }
1007
1008         brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1009                       iovbuf, sizeof(iovbuf));
1010         brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1011                                     sizeof(iovbuf));
1012         memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1013
1014         setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1015         setbit(drvr->eventmask, BRCMF_E_PRUNE);
1016         setbit(drvr->eventmask, BRCMF_E_AUTH);
1017         setbit(drvr->eventmask, BRCMF_E_REASSOC);
1018         setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1019         setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1020         setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1021         setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1022         setbit(drvr->eventmask, BRCMF_E_JOIN);
1023         setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1024         setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1025         setbit(drvr->eventmask, BRCMF_E_LINK);
1026         setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1027         setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1028         setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1029         setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1030         setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1031         setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1032
1033 /* enable dongle roaming event */
1034
1035         drvr->pktfilter_count = 1;
1036         /* Setup filter to allow only unicast */
1037         drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1038
1039         /* Bus is ready, do any protocol initialization */
1040         ret = brcmf_proto_init(&drvr_priv->pub);
1041         if (ret < 0)
1042                 return ret;
1043
1044         return 0;
1045 }
1046
1047 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1048 {
1049         struct brcmf_info *drvr_priv = drvr->info;
1050         struct net_device *ndev;
1051         u8 temp_addr[ETH_ALEN] = {
1052                 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1053
1054         brcmf_dbg(TRACE, "ifidx %d\n", ifidx);
1055
1056         ndev = drvr_priv->iflist[ifidx]->ndev;
1057         ndev->netdev_ops = &brcmf_netdev_ops_pri;
1058
1059         /*
1060          * We have to use the primary MAC for virtual interfaces
1061          */
1062         if (ifidx != 0) {
1063                 /* for virtual interfaces use the primary MAC  */
1064                 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1065
1066         }
1067
1068         if (ifidx == 1) {
1069                 brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
1070                 /*  ACCESSPOINT INTERFACE CASE */
1071                 temp_addr[0] |= 0X02;   /* set bit 2 ,
1072                          - Locally Administered address  */
1073
1074         }
1075         ndev->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1076         ndev->ethtool_ops = &brcmf_ethtool_ops;
1077
1078         drvr_priv->pub.rxsz = ndev->mtu + ndev->hard_header_len +
1079                               drvr_priv->pub.hdrlen;
1080
1081         memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
1082
1083         /* attach to cfg80211 for primary interface */
1084         if (!ifidx) {
1085                 drvr->config =
1086                         brcmf_cfg80211_attach(ndev,
1087                                               brcmf_bus_get_device(drvr->bus),
1088                                               drvr);
1089                 if (drvr->config == NULL) {
1090                         brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
1091                         goto fail;
1092                 }
1093         }
1094
1095         if (register_netdev(ndev) != 0) {
1096                 brcmf_dbg(ERROR, "couldn't register the net device\n");
1097                 goto fail;
1098         }
1099
1100         brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
1101
1102         return 0;
1103
1104 fail:
1105         ndev->netdev_ops = NULL;
1106         return -EBADE;
1107 }
1108
1109 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1110 {
1111         struct brcmf_info *drvr_priv;
1112
1113         brcmf_dbg(TRACE, "Enter\n");
1114
1115         if (drvr) {
1116                 drvr_priv = drvr->info;
1117                 if (drvr_priv) {
1118                         /* Stop the protocol module */
1119                         brcmf_proto_stop(&drvr_priv->pub);
1120
1121                         /* Stop the bus module */
1122                         brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus);
1123                 }
1124         }
1125 }
1126
1127 void brcmf_detach(struct brcmf_pub *drvr)
1128 {
1129         struct brcmf_info *drvr_priv;
1130
1131         brcmf_dbg(TRACE, "Enter\n");
1132
1133         if (drvr) {
1134                 drvr_priv = drvr->info;
1135                 if (drvr_priv) {
1136                         int i;
1137
1138                         /* make sure primary interface removed last */
1139                         for (i = BRCMF_MAX_IFS-1; i > -1; i--)
1140                                 if (drvr_priv->iflist[i])
1141                                         brcmf_del_if(drvr_priv, i);
1142
1143                         cancel_work_sync(&drvr_priv->setmacaddr_work);
1144                         cancel_work_sync(&drvr_priv->multicast_work);
1145
1146                         brcmf_bus_detach(drvr);
1147
1148                         if (drvr->prot)
1149                                 brcmf_proto_detach(drvr);
1150
1151                         kfree(drvr_priv);
1152                 }
1153         }
1154 }
1155
1156 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1157 {
1158         struct brcmf_info *drvr_priv = drvr->info;
1159
1160         if (drvr_priv) {
1161                 mutex_lock(&drvr_priv->proto_block);
1162                 return 1;
1163         }
1164         return 0;
1165 }
1166
1167 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1168 {
1169         struct brcmf_info *drvr_priv = drvr->info;
1170
1171         if (drvr_priv) {
1172                 mutex_unlock(&drvr_priv->proto_block);
1173                 return 1;
1174         }
1175
1176         return 0;
1177 }
1178
1179 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1180 {
1181         return atomic_read(&drvr_priv->pend_8021x_cnt);
1182 }
1183
1184 #define MAX_WAIT_FOR_8021X_TX   10
1185
1186 int brcmf_netdev_wait_pend8021x(struct net_device *ndev)
1187 {
1188         struct brcmf_if *ifp = netdev_priv(ndev);
1189         struct brcmf_info *drvr_priv = ifp->info;
1190         int timeout = 10 * HZ / 1000;
1191         int ntimes = MAX_WAIT_FOR_8021X_TX;
1192         int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1193
1194         while (ntimes && pend) {
1195                 if (pend) {
1196                         set_current_state(TASK_INTERRUPTIBLE);
1197                         schedule_timeout(timeout);
1198                         set_current_state(TASK_RUNNING);
1199                         ntimes--;
1200                 }
1201                 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1202         }
1203         return pend;
1204 }
1205
1206 #ifdef BCMDBG
1207 int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
1208 {
1209         int ret = 0;
1210         struct file *fp;
1211         mm_segment_t old_fs;
1212         loff_t pos = 0;
1213
1214         /* change to KERNEL_DS address limit */
1215         old_fs = get_fs();
1216         set_fs(KERNEL_DS);
1217
1218         /* open file to write */
1219         fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1220         if (!fp) {
1221                 brcmf_dbg(ERROR, "open file error\n");
1222                 ret = -1;
1223                 goto exit;
1224         }
1225
1226         /* Write buf to file */
1227         fp->f_op->write(fp, (char __user *)buf, size, &pos);
1228
1229 exit:
1230         /* free buf before return */
1231         kfree(buf);
1232         /* close file before return */
1233         if (fp)
1234                 filp_close(fp, current->files);
1235         /* restore previous address limit */
1236         set_fs(old_fs);
1237
1238         return ret;
1239 }
1240 #endif                          /* BCMDBG */