Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dhd_linux.c
Go to the documentation of this file.
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/kthread.h>
22 #include <linux/slab.h>
23 #include <linux/skbuff.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/random.h>
28 #include <linux/spinlock.h>
29 #include <linux/ethtool.h>
30 #include <linux/fcntl.h>
31 #include <linux/fs.h>
32 #include <linux/uaccess.h>
33 #include <linux/hardirq.h>
34 #include <linux/mutex.h>
35 #include <linux/wait.h>
36 #include <linux/module.h>
37 #include <net/cfg80211.h>
38 #include <net/rtnetlink.h>
39 #include <defs.h>
40 #include <brcmu_utils.h>
41 #include <brcmu_wifi.h>
42 
43 #include "dhd.h"
44 #include "dhd_bus.h"
45 #include "dhd_proto.h"
46 #include "dhd_dbg.h"
47 #include "wl_cfg80211.h"
48 
49 MODULE_AUTHOR("Broadcom Corporation");
50 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
51 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
52 MODULE_LICENSE("Dual BSD/GPL");
53 
54 
55 /* Interface control information */
56 struct brcmf_if {
57  struct brcmf_pub *drvr; /* back pointer to brcmf_pub */
58  /* OS/stack specifics */
59  struct net_device *ndev;
61  int idx; /* iface idx in dongle */
62  u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
63 };
64 
65 /* Error bits */
68 
69 int brcmf_ifname2idx(struct brcmf_pub *drvr, char *name)
70 {
71  int i = BRCMF_MAX_IFS;
72  struct brcmf_if *ifp;
73 
74  if (name == NULL || *name == '\0')
75  return 0;
76 
77  while (--i > 0) {
78  ifp = drvr->iflist[i];
79  if (ifp && !strncmp(ifp->ndev->name, name, IFNAMSIZ))
80  break;
81  }
82 
83  brcmf_dbg(TRACE, "return idx %d for \"%s\"\n", i, name);
84 
85  return i; /* default - the primary interface */
86 }
87 
88 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
89 {
90  if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
91  brcmf_dbg(ERROR, "ifidx %d out of range\n", ifidx);
92  return "<if_bad>";
93  }
94 
95  if (drvr->iflist[ifidx] == NULL) {
96  brcmf_dbg(ERROR, "null i/f %d\n", ifidx);
97  return "<if_null>";
98  }
99 
100  if (drvr->iflist[ifidx]->ndev)
101  return drvr->iflist[ifidx]->ndev->name;
102 
103  return "<if_none>";
104 }
105 
106 static void _brcmf_set_multicast_list(struct work_struct *work)
107 {
108  struct net_device *ndev;
109  struct netdev_hw_addr *ha;
110  u32 dcmd_value, cnt;
111  __le32 cnt_le;
112  __le32 dcmd_le_value;
113 
114  struct brcmf_dcmd dcmd;
115  char *buf, *bufp;
116  uint buflen;
117  int ret;
118 
119  struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
121 
122  ndev = drvr->iflist[0]->ndev;
123  cnt = netdev_mc_count(ndev);
124 
125  /* Determine initial value of allmulti flag */
126  dcmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
127 
128  /* Send down the multicast list first. */
129 
130  buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
131  bufp = buf = kmalloc(buflen, GFP_ATOMIC);
132  if (!bufp)
133  return;
134 
135  strcpy(bufp, "mcast_list");
136  bufp += strlen("mcast_list") + 1;
137 
138  cnt_le = cpu_to_le32(cnt);
139  memcpy(bufp, &cnt_le, sizeof(cnt));
140  bufp += sizeof(cnt_le);
141 
142  netdev_for_each_mc_addr(ha, ndev) {
143  if (!cnt)
144  break;
145  memcpy(bufp, ha->addr, ETH_ALEN);
146  bufp += ETH_ALEN;
147  cnt--;
148  }
149 
150  memset(&dcmd, 0, sizeof(dcmd));
151  dcmd.cmd = BRCMF_C_SET_VAR;
152  dcmd.buf = buf;
153  dcmd.len = buflen;
154  dcmd.set = true;
155 
156  ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
157  if (ret < 0) {
158  brcmf_dbg(ERROR, "%s: set mcast_list failed, cnt %d\n",
159  brcmf_ifname(drvr, 0), cnt);
160  dcmd_value = cnt ? true : dcmd_value;
161  }
162 
163  kfree(buf);
164 
165  /* Now send the allmulti setting. This is based on the setting in the
166  * net_device flags, but might be modified above to be turned on if we
167  * were trying to set some addresses and dongle rejected it...
168  */
169 
170  buflen = sizeof("allmulti") + sizeof(dcmd_value);
171  buf = kmalloc(buflen, GFP_ATOMIC);
172  if (!buf)
173  return;
174 
175  dcmd_le_value = cpu_to_le32(dcmd_value);
176 
177  if (!brcmf_c_mkiovar
178  ("allmulti", (void *)&dcmd_le_value,
179  sizeof(dcmd_le_value), buf, buflen)) {
180  brcmf_dbg(ERROR, "%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
181  brcmf_ifname(drvr, 0),
182  (int)sizeof(dcmd_value), buflen);
183  kfree(buf);
184  return;
185  }
186 
187  memset(&dcmd, 0, sizeof(dcmd));
188  dcmd.cmd = BRCMF_C_SET_VAR;
189  dcmd.buf = buf;
190  dcmd.len = buflen;
191  dcmd.set = true;
192 
193  ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
194  if (ret < 0) {
195  brcmf_dbg(ERROR, "%s: set allmulti %d failed\n",
196  brcmf_ifname(drvr, 0),
197  le32_to_cpu(dcmd_le_value));
198  }
199 
200  kfree(buf);
201 
202  /* Finally, pick up the PROMISC flag as well, like the NIC
203  driver does */
204 
205  dcmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
206  dcmd_le_value = cpu_to_le32(dcmd_value);
207 
208  memset(&dcmd, 0, sizeof(dcmd));
210  dcmd.buf = &dcmd_le_value;
211  dcmd.len = sizeof(dcmd_le_value);
212  dcmd.set = true;
213 
214  ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
215  if (ret < 0) {
216  brcmf_dbg(ERROR, "%s: set promisc %d failed\n",
217  brcmf_ifname(drvr, 0),
218  le32_to_cpu(dcmd_le_value));
219  }
220 }
221 
222 static void
223 _brcmf_set_mac_address(struct work_struct *work)
224 {
225  char buf[32];
226  struct brcmf_dcmd dcmd;
227  int ret;
228 
229  struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
231 
232  brcmf_dbg(TRACE, "enter\n");
233  if (!brcmf_c_mkiovar("cur_etheraddr", (char *)drvr->macvalue,
234  ETH_ALEN, buf, 32)) {
235  brcmf_dbg(ERROR, "%s: mkiovar failed for cur_etheraddr\n",
236  brcmf_ifname(drvr, 0));
237  return;
238  }
239  memset(&dcmd, 0, sizeof(dcmd));
240  dcmd.cmd = BRCMF_C_SET_VAR;
241  dcmd.buf = buf;
242  dcmd.len = 32;
243  dcmd.set = true;
244 
245  ret = brcmf_proto_dcmd(drvr, 0, &dcmd, dcmd.len);
246  if (ret < 0)
247  brcmf_dbg(ERROR, "%s: set cur_etheraddr failed\n",
248  brcmf_ifname(drvr, 0));
249  else
250  memcpy(drvr->iflist[0]->ndev->dev_addr,
251  drvr->macvalue, ETH_ALEN);
252 
253  return;
254 }
255 
256 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
257 {
258  struct brcmf_if *ifp = netdev_priv(ndev);
259  struct brcmf_pub *drvr = ifp->drvr;
260  struct sockaddr *sa = (struct sockaddr *)addr;
261 
262  memcpy(&drvr->macvalue, sa->sa_data, ETH_ALEN);
264  return 0;
265 }
266 
267 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
268 {
269  struct brcmf_if *ifp = netdev_priv(ndev);
270  struct brcmf_pub *drvr = ifp->drvr;
271 
273 }
274 
275 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
276 {
277  int ret;
278  struct brcmf_if *ifp = netdev_priv(ndev);
279  struct brcmf_pub *drvr = ifp->drvr;
280 
281  brcmf_dbg(TRACE, "Enter\n");
282 
283  /* Reject if down */
284  if (!drvr->bus_if->drvr_up ||
285  (drvr->bus_if->state == BRCMF_BUS_DOWN)) {
286  brcmf_dbg(ERROR, "xmit rejected drvup=%d state=%d\n",
287  drvr->bus_if->drvr_up,
288  drvr->bus_if->state);
289  netif_stop_queue(ndev);
290  return -ENODEV;
291  }
292 
293  if (!drvr->iflist[ifp->idx]) {
294  brcmf_dbg(ERROR, "bad ifidx %d\n", ifp->idx);
295  netif_stop_queue(ndev);
296  return -ENODEV;
297  }
298 
299  /* Make sure there's enough room for any header */
300  if (skb_headroom(skb) < drvr->hdrlen) {
301  struct sk_buff *skb2;
302 
303  brcmf_dbg(INFO, "%s: insufficient headroom\n",
304  brcmf_ifname(drvr, ifp->idx));
305  drvr->bus_if->tx_realloc++;
306  skb2 = skb_realloc_headroom(skb, drvr->hdrlen);
307  dev_kfree_skb(skb);
308  skb = skb2;
309  if (skb == NULL) {
310  brcmf_dbg(ERROR, "%s: skb_realloc_headroom failed\n",
311  brcmf_ifname(drvr, ifp->idx));
312  ret = -ENOMEM;
313  goto done;
314  }
315  }
316 
317  /* Update multicast statistic */
318  if (skb->len >= ETH_ALEN) {
319  u8 *pktdata = (u8 *)(skb->data);
320  struct ethhdr *eh = (struct ethhdr *)pktdata;
321 
322  if (is_multicast_ether_addr(eh->h_dest))
323  drvr->tx_multicast++;
324  if (ntohs(eh->h_proto) == ETH_P_PAE)
325  atomic_inc(&drvr->pend_8021x_cnt);
326  }
327 
328  /* If the protocol uses a data header, apply it */
329  brcmf_proto_hdrpush(drvr, ifp->idx, skb);
330 
331  /* Use bus module to send data frame */
332  ret = drvr->bus_if->brcmf_bus_txdata(drvr->dev, skb);
333 
334 done:
335  if (ret)
336  drvr->bus_if->dstats.tx_dropped++;
337  else
338  drvr->bus_if->dstats.tx_packets++;
339 
340  /* Return ok: we always eat the packet */
341  return 0;
342 }
343 
344 void brcmf_txflowblock(struct device *dev, bool state)
345 {
346  struct net_device *ndev;
347  struct brcmf_bus *bus_if = dev_get_drvdata(dev);
348  struct brcmf_pub *drvr = bus_if->drvr;
349  int i;
350 
351  brcmf_dbg(TRACE, "Enter\n");
352 
353  for (i = 0; i < BRCMF_MAX_IFS; i++)
354  if (drvr->iflist[i]) {
355  ndev = drvr->iflist[i]->ndev;
356  if (state)
357  netif_stop_queue(ndev);
358  else
359  netif_wake_queue(ndev);
360  }
361 }
362 
363 static int brcmf_host_event(struct brcmf_pub *drvr, int *ifidx,
364  void *pktdata, struct brcmf_event_msg *event,
365  void **data)
366 {
367  int bcmerror = 0;
368 
369  bcmerror = brcmf_c_host_event(drvr, ifidx, pktdata, event, data);
370  if (bcmerror != 0)
371  return bcmerror;
372 
373  if (drvr->iflist[*ifidx]->ndev)
374  brcmf_cfg80211_event(drvr->iflist[*ifidx]->ndev,
375  event, *data);
376 
377  return bcmerror;
378 }
379 
380 void brcmf_rx_frame(struct device *dev, int ifidx,
381  struct sk_buff_head *skb_list)
382 {
383  unsigned char *eth;
384  uint len;
385  void *data;
386  struct sk_buff *skb, *pnext;
387  struct brcmf_if *ifp;
388  struct brcmf_event_msg event;
389  struct brcmf_bus *bus_if = dev_get_drvdata(dev);
390  struct brcmf_pub *drvr = bus_if->drvr;
391 
392  brcmf_dbg(TRACE, "Enter\n");
393 
394  skb_queue_walk_safe(skb_list, skb, pnext) {
395  skb_unlink(skb, skb_list);
396 
397  /* Get the protocol, maintain skb around eth_type_trans()
398  * The main reason for this hack is for the limitation of
399  * Linux 2.4 where 'eth_type_trans' uses the
400  * 'net->hard_header_len'
401  * to perform skb_pull inside vs ETH_HLEN. Since to avoid
402  * coping of the packet coming from the network stack to add
403  * BDC, Hardware header etc, during network interface
404  * registration
405  * we set the 'net->hard_header_len' to ETH_HLEN + extra space
406  * required
407  * for BDC, Hardware header etc. and not just the ETH_HLEN
408  */
409  eth = skb->data;
410  len = skb->len;
411 
412  ifp = drvr->iflist[ifidx];
413  if (ifp == NULL)
414  ifp = drvr->iflist[0];
415 
416  if (!ifp || !ifp->ndev ||
417  ifp->ndev->reg_state != NETREG_REGISTERED) {
419  continue;
420  }
421 
422  skb->dev = ifp->ndev;
423  skb->protocol = eth_type_trans(skb, skb->dev);
424 
425  if (skb->pkt_type == PACKET_MULTICAST)
426  bus_if->dstats.multicast++;
427 
428  skb->data = eth;
429  skb->len = len;
430 
431  /* Strip header, count, deliver upward */
432  skb_pull(skb, ETH_HLEN);
433 
434  /* Process special event packets and then discard them */
435  if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
436  brcmf_host_event(drvr, &ifidx,
437  skb_mac_header(skb),
438  &event, &data);
439 
440  if (drvr->iflist[ifidx]) {
441  ifp = drvr->iflist[ifidx];
442  ifp->ndev->last_rx = jiffies;
443  }
444 
445  bus_if->dstats.rx_bytes += skb->len;
446  bus_if->dstats.rx_packets++; /* Local count */
447 
448  if (in_interrupt())
449  netif_rx(skb);
450  else
451  /* If the receive is not processed inside an ISR,
452  * the softirqd must be woken explicitly to service
453  * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
454  * by netif_rx_ni(), but in earlier kernels, we need
455  * to do it manually.
456  */
457  netif_rx_ni(skb);
458  }
459 }
460 
461 void brcmf_txcomplete(struct device *dev, struct sk_buff *txp, bool success)
462 {
463  uint ifidx;
464  struct ethhdr *eh;
465  u16 type;
466  struct brcmf_bus *bus_if = dev_get_drvdata(dev);
467  struct brcmf_pub *drvr = bus_if->drvr;
468 
469  brcmf_proto_hdrpull(dev, &ifidx, txp);
470 
471  eh = (struct ethhdr *)(txp->data);
472  type = ntohs(eh->h_proto);
473 
474  if (type == ETH_P_PAE)
475  atomic_dec(&drvr->pend_8021x_cnt);
476 
477 }
478 
479 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
480 {
481  struct brcmf_if *ifp = netdev_priv(ndev);
482  struct brcmf_bus *bus_if = ifp->drvr->bus_if;
483 
484  brcmf_dbg(TRACE, "Enter\n");
485 
486  /* Copy dongle stats to net device stats */
487  ifp->stats.rx_packets = bus_if->dstats.rx_packets;
488  ifp->stats.tx_packets = bus_if->dstats.tx_packets;
489  ifp->stats.rx_bytes = bus_if->dstats.rx_bytes;
490  ifp->stats.tx_bytes = bus_if->dstats.tx_bytes;
491  ifp->stats.rx_errors = bus_if->dstats.rx_errors;
492  ifp->stats.tx_errors = bus_if->dstats.tx_errors;
493  ifp->stats.rx_dropped = bus_if->dstats.rx_dropped;
494  ifp->stats.tx_dropped = bus_if->dstats.tx_dropped;
495  ifp->stats.multicast = bus_if->dstats.multicast;
496 
497  return &ifp->stats;
498 }
499 
500 /* Retrieve current toe component enables, which are kept
501  as a bitmap in toe_ol iovar */
502 static int brcmf_toe_get(struct brcmf_pub *drvr, int ifidx, u32 *toe_ol)
503 {
504  struct brcmf_dcmd dcmd;
505  __le32 toe_le;
506  char buf[32];
507  int ret;
508 
509  memset(&dcmd, 0, sizeof(dcmd));
510 
511  dcmd.cmd = BRCMF_C_GET_VAR;
512  dcmd.buf = buf;
513  dcmd.len = (uint) sizeof(buf);
514  dcmd.set = false;
515 
516  strcpy(buf, "toe_ol");
517  ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
518  if (ret < 0) {
519  /* Check for older dongle image that doesn't support toe_ol */
520  if (ret == -EIO) {
521  brcmf_dbg(ERROR, "%s: toe not supported by device\n",
522  brcmf_ifname(drvr, ifidx));
523  return -EOPNOTSUPP;
524  }
525 
526  brcmf_dbg(INFO, "%s: could not get toe_ol: ret=%d\n",
527  brcmf_ifname(drvr, ifidx), ret);
528  return ret;
529  }
530 
531  memcpy(&toe_le, buf, sizeof(u32));
532  *toe_ol = le32_to_cpu(toe_le);
533  return 0;
534 }
535 
536 /* Set current toe component enables in toe_ol iovar,
537  and set toe global enable iovar */
538 static int brcmf_toe_set(struct brcmf_pub *drvr, int ifidx, u32 toe_ol)
539 {
540  struct brcmf_dcmd dcmd;
541  char buf[32];
542  int ret;
543  __le32 toe_le = cpu_to_le32(toe_ol);
544 
545  memset(&dcmd, 0, sizeof(dcmd));
546 
547  dcmd.cmd = BRCMF_C_SET_VAR;
548  dcmd.buf = buf;
549  dcmd.len = (uint) sizeof(buf);
550  dcmd.set = true;
551 
552  /* Set toe_ol as requested */
553  strcpy(buf, "toe_ol");
554  memcpy(&buf[sizeof("toe_ol")], &toe_le, sizeof(u32));
555 
556  ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
557  if (ret < 0) {
558  brcmf_dbg(ERROR, "%s: could not set toe_ol: ret=%d\n",
559  brcmf_ifname(drvr, ifidx), ret);
560  return ret;
561  }
562 
563  /* Enable toe globally only if any components are enabled. */
564  toe_le = cpu_to_le32(toe_ol != 0);
565 
566  strcpy(buf, "toe");
567  memcpy(&buf[sizeof("toe")], &toe_le, sizeof(u32));
568 
569  ret = brcmf_proto_dcmd(drvr, ifidx, &dcmd, dcmd.len);
570  if (ret < 0) {
571  brcmf_dbg(ERROR, "%s: could not set toe: ret=%d\n",
572  brcmf_ifname(drvr, ifidx), ret);
573  return ret;
574  }
575 
576  return 0;
577 }
578 
579 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
580  struct ethtool_drvinfo *info)
581 {
582  struct brcmf_if *ifp = netdev_priv(ndev);
583  struct brcmf_pub *drvr = ifp->drvr;
584 
585  sprintf(info->driver, KBUILD_MODNAME);
586  sprintf(info->version, "%lu", drvr->drv_version);
587  sprintf(info->bus_info, "%s", dev_name(drvr->dev));
588 }
589 
590 static const struct ethtool_ops brcmf_ethtool_ops = {
591  .get_drvinfo = brcmf_ethtool_get_drvinfo,
592 };
593 
594 static int brcmf_ethtool(struct brcmf_pub *drvr, void __user *uaddr)
595 {
596  struct ethtool_drvinfo info;
597  char drvname[sizeof(info.driver)];
598  u32 cmd;
599  struct ethtool_value edata;
600  u32 toe_cmpnt, csum_dir;
601  int ret;
602 
603  brcmf_dbg(TRACE, "Enter\n");
604 
605  /* all ethtool calls start with a cmd word */
606  if (copy_from_user(&cmd, uaddr, sizeof(u32)))
607  return -EFAULT;
608 
609  switch (cmd) {
610  case ETHTOOL_GDRVINFO:
611  /* Copy out any request driver name */
612  if (copy_from_user(&info, uaddr, sizeof(info)))
613  return -EFAULT;
614  strncpy(drvname, info.driver, sizeof(info.driver));
615  drvname[sizeof(info.driver) - 1] = '\0';
616 
617  /* clear struct for return */
618  memset(&info, 0, sizeof(info));
619  info.cmd = cmd;
620 
621  /* if requested, identify ourselves */
622  if (strcmp(drvname, "?dhd") == 0) {
623  sprintf(info.driver, "dhd");
625  }
626 
627  /* otherwise, require dongle to be up */
628  else if (!drvr->bus_if->drvr_up) {
629  brcmf_dbg(ERROR, "dongle is not up\n");
630  return -ENODEV;
631  }
632 
633  /* finally, report dongle driver type */
634  else if (drvr->iswl)
635  sprintf(info.driver, "wl");
636  else
637  sprintf(info.driver, "xx");
638 
639  sprintf(info.version, "%lu", drvr->drv_version);
640  if (copy_to_user(uaddr, &info, sizeof(info)))
641  return -EFAULT;
642  brcmf_dbg(CTL, "given %*s, returning %s\n",
643  (int)sizeof(drvname), drvname, info.driver);
644  break;
645 
646  /* Get toe offload components from dongle */
647  case ETHTOOL_GRXCSUM:
648  case ETHTOOL_GTXCSUM:
649  ret = brcmf_toe_get(drvr, 0, &toe_cmpnt);
650  if (ret < 0)
651  return ret;
652 
653  csum_dir =
655 
656  edata.cmd = cmd;
657  edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
658 
659  if (copy_to_user(uaddr, &edata, sizeof(edata)))
660  return -EFAULT;
661  break;
662 
663  /* Set toe offload components in dongle */
664  case ETHTOOL_SRXCSUM:
665  case ETHTOOL_STXCSUM:
666  if (copy_from_user(&edata, uaddr, sizeof(edata)))
667  return -EFAULT;
668 
669  /* Read the current settings, update and write back */
670  ret = brcmf_toe_get(drvr, 0, &toe_cmpnt);
671  if (ret < 0)
672  return ret;
673 
674  csum_dir =
676 
677  if (edata.data != 0)
678  toe_cmpnt |= csum_dir;
679  else
680  toe_cmpnt &= ~csum_dir;
681 
682  ret = brcmf_toe_set(drvr, 0, toe_cmpnt);
683  if (ret < 0)
684  return ret;
685 
686  /* If setting TX checksum mode, tell Linux the new mode */
687  if (cmd == ETHTOOL_STXCSUM) {
688  if (edata.data)
689  drvr->iflist[0]->ndev->features |=
691  else
692  drvr->iflist[0]->ndev->features &=
694  }
695 
696  break;
697 
698  default:
699  return -EOPNOTSUPP;
700  }
701 
702  return 0;
703 }
704 
705 static int brcmf_netdev_ioctl_entry(struct net_device *ndev, struct ifreq *ifr,
706  int cmd)
707 {
708  struct brcmf_if *ifp = netdev_priv(ndev);
709  struct brcmf_pub *drvr = ifp->drvr;
710 
711  brcmf_dbg(TRACE, "ifidx %d, cmd 0x%04x\n", ifp->idx, cmd);
712 
713  if (!drvr->iflist[ifp->idx])
714  return -1;
715 
716  if (cmd == SIOCETHTOOL)
717  return brcmf_ethtool(drvr, ifr->ifr_data);
718 
719  return -EOPNOTSUPP;
720 }
721 
722 /* called only from within this driver. Sends a command to the dongle. */
723 s32 brcmf_exec_dcmd(struct net_device *ndev, u32 cmd, void *arg, u32 len)
724 {
725  struct brcmf_dcmd dcmd;
726  s32 err = 0;
727  int buflen = 0;
728  bool is_set_key_cmd;
729  struct brcmf_if *ifp = netdev_priv(ndev);
730  struct brcmf_pub *drvr = ifp->drvr;
731 
732  memset(&dcmd, 0, sizeof(dcmd));
733  dcmd.cmd = cmd;
734  dcmd.buf = arg;
735  dcmd.len = len;
736 
737  if (dcmd.buf != NULL)
738  buflen = min_t(uint, dcmd.len, BRCMF_DCMD_MAXLEN);
739 
740  /* send to dongle (must be up, and wl) */
741  if ((drvr->bus_if->state != BRCMF_BUS_DATA)) {
742  brcmf_dbg(ERROR, "DONGLE_DOWN\n");
743  err = -EIO;
744  goto done;
745  }
746 
747  if (!drvr->iswl) {
748  err = -EIO;
749  goto done;
750  }
751 
752  /*
753  * Intercept BRCMF_C_SET_KEY CMD - serialize M4 send and
754  * set key CMD to prevent M4 encryption.
755  */
756  is_set_key_cmd = ((dcmd.cmd == BRCMF_C_SET_KEY) ||
757  ((dcmd.cmd == BRCMF_C_SET_VAR) &&
758  !(strncmp("wsec_key", dcmd.buf, 9))) ||
759  ((dcmd.cmd == BRCMF_C_SET_VAR) &&
760  !(strncmp("bsscfg:wsec_key", dcmd.buf, 15))));
761  if (is_set_key_cmd)
763 
764  err = brcmf_proto_dcmd(drvr, ifp->idx, &dcmd, buflen);
765 
766 done:
767  if (err > 0)
768  err = 0;
769 
770  return err;
771 }
772 
773 int brcmf_netlink_dcmd(struct net_device *ndev, struct brcmf_dcmd *dcmd)
774 {
775  brcmf_dbg(TRACE, "enter: cmd %x buf %p len %d\n",
776  dcmd->cmd, dcmd->buf, dcmd->len);
777 
778  return brcmf_exec_dcmd(ndev, dcmd->cmd, dcmd->buf, dcmd->len);
779 }
780 
781 static int brcmf_netdev_stop(struct net_device *ndev)
782 {
783  struct brcmf_if *ifp = netdev_priv(ndev);
784  struct brcmf_pub *drvr = ifp->drvr;
785 
786  brcmf_dbg(TRACE, "Enter\n");
788  if (drvr->bus_if->drvr_up == 0)
789  return 0;
790 
791  /* Set state and stop OS transmissions */
792  drvr->bus_if->drvr_up = false;
793  netif_stop_queue(ndev);
794 
795  return 0;
796 }
797 
798 static int brcmf_netdev_open(struct net_device *ndev)
799 {
800  struct brcmf_if *ifp = netdev_priv(ndev);
801  struct brcmf_pub *drvr = ifp->drvr;
802  struct brcmf_bus *bus_if = drvr->bus_if;
803  u32 toe_ol;
804  s32 ret = 0;
805  uint up = 0;
806 
807  brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
808 
809  if (ifp->idx == 0) { /* do it only for primary eth0 */
810  /* If bus is not ready, can't continue */
811  if (bus_if->state != BRCMF_BUS_DATA) {
812  brcmf_dbg(ERROR, "failed bus is not ready\n");
813  return -EAGAIN;
814  }
815 
816  atomic_set(&drvr->pend_8021x_cnt, 0);
817 
818  memcpy(ndev->dev_addr, drvr->mac, ETH_ALEN);
819 
820  /* Get current TOE mode from dongle */
821  if (brcmf_toe_get(drvr, ifp->idx, &toe_ol) >= 0
822  && (toe_ol & TOE_TX_CSUM_OL) != 0)
823  drvr->iflist[ifp->idx]->ndev->features |=
825  else
826  drvr->iflist[ifp->idx]->ndev->features &=
828  }
829 
830  /* make sure RF is ready for work */
831  brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_UP, (char *)&up, sizeof(up));
832 
833  /* Allow transmit calls */
834  netif_start_queue(ndev);
835  drvr->bus_if->drvr_up = true;
836  if (brcmf_cfg80211_up(drvr->config)) {
837  brcmf_dbg(ERROR, "failed to bring up cfg80211\n");
838  return -1;
839  }
840 
841  return ret;
842 }
843 
844 static const struct net_device_ops brcmf_netdev_ops_pri = {
845  .ndo_open = brcmf_netdev_open,
846  .ndo_stop = brcmf_netdev_stop,
847  .ndo_get_stats = brcmf_netdev_get_stats,
848  .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
849  .ndo_start_xmit = brcmf_netdev_start_xmit,
850  .ndo_set_mac_address = brcmf_netdev_set_mac_address,
851  .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
852 };
853 
854 static int brcmf_net_attach(struct brcmf_if *ifp)
855 {
856  struct brcmf_pub *drvr = ifp->drvr;
857  struct net_device *ndev;
858  u8 temp_addr[ETH_ALEN];
859 
860  brcmf_dbg(TRACE, "ifidx %d\n", ifp->idx);
861 
862  ndev = drvr->iflist[ifp->idx]->ndev;
863  ndev->netdev_ops = &brcmf_netdev_ops_pri;
864 
865  /*
866  * determine mac address to use
867  */
868  if (is_valid_ether_addr(ifp->mac_addr))
869  memcpy(temp_addr, ifp->mac_addr, ETH_ALEN);
870  else
871  memcpy(temp_addr, drvr->mac, ETH_ALEN);
872 
873  if (ifp->idx == 1) {
874  brcmf_dbg(TRACE, "ACCESS POINT MAC:\n");
875  /* ACCESSPOINT INTERFACE CASE */
876  temp_addr[0] |= 0X02; /* set bit 2 ,
877  - Locally Administered address */
878 
879  }
880  ndev->hard_header_len = ETH_HLEN + drvr->hdrlen;
881  ndev->ethtool_ops = &brcmf_ethtool_ops;
882 
883  drvr->rxsz = ndev->mtu + ndev->hard_header_len +
884  drvr->hdrlen;
885 
886  memcpy(ndev->dev_addr, temp_addr, ETH_ALEN);
887 
888  /* attach to cfg80211 for primary interface */
889  if (!ifp->idx) {
890  drvr->config = brcmf_cfg80211_attach(ndev, drvr->dev, drvr);
891  if (drvr->config == NULL) {
892  brcmf_dbg(ERROR, "wl_cfg80211_attach failed\n");
893  goto fail;
894  }
895  }
896 
897  if (register_netdev(ndev) != 0) {
898  brcmf_dbg(ERROR, "couldn't register the net device\n");
899  goto fail;
900  }
901 
902  brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
903 
904  return 0;
905 
906 fail:
907  ndev->netdev_ops = NULL;
908  return -EBADE;
909 }
910 
911 int
912 brcmf_add_if(struct device *dev, int ifidx, char *name, u8 *mac_addr)
913 {
914  struct brcmf_if *ifp;
915  struct net_device *ndev;
916  struct brcmf_bus *bus_if = dev_get_drvdata(dev);
917  struct brcmf_pub *drvr = bus_if->drvr;
918 
919  brcmf_dbg(TRACE, "idx %d\n", ifidx);
920 
921  ifp = drvr->iflist[ifidx];
922  /*
923  * Delete the existing interface before overwriting it
924  * in case we missed the BRCMF_E_IF_DEL event.
925  */
926  if (ifp) {
927  brcmf_dbg(ERROR, "ERROR: netdev:%s already exists, try free & unregister\n",
928  ifp->ndev->name);
929  netif_stop_queue(ifp->ndev);
930  unregister_netdev(ifp->ndev);
931  free_netdev(ifp->ndev);
932  drvr->iflist[ifidx] = NULL;
933  }
934 
935  /* Allocate netdev, including space for private structure */
936  ndev = alloc_netdev(sizeof(struct brcmf_if), name, ether_setup);
937  if (!ndev) {
938  brcmf_dbg(ERROR, "OOM - alloc_netdev\n");
939  return -ENOMEM;
940  }
941 
942  ifp = netdev_priv(ndev);
943  ifp->ndev = ndev;
944  ifp->drvr = drvr;
945  drvr->iflist[ifidx] = ifp;
946  ifp->idx = ifidx;
947  if (mac_addr != NULL)
948  memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
949 
950  if (brcmf_net_attach(ifp)) {
951  brcmf_dbg(ERROR, "brcmf_net_attach failed");
952  free_netdev(ifp->ndev);
953  drvr->iflist[ifidx] = NULL;
954  return -EOPNOTSUPP;
955  }
956 
957  brcmf_dbg(TRACE, " ==== pid:%x, net_device for if:%s created ===\n",
958  current->pid, ifp->ndev->name);
959 
960  return 0;
961 }
962 
963 void brcmf_del_if(struct brcmf_pub *drvr, int ifidx)
964 {
965  struct brcmf_if *ifp;
966 
967  brcmf_dbg(TRACE, "idx %d\n", ifidx);
968 
969  ifp = drvr->iflist[ifidx];
970  if (!ifp) {
971  brcmf_dbg(ERROR, "Null interface\n");
972  return;
973  }
974  if (ifp->ndev) {
975  if (ifidx == 0) {
976  if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
977  rtnl_lock();
978  brcmf_netdev_stop(ifp->ndev);
979  rtnl_unlock();
980  }
981  } else {
982  netif_stop_queue(ifp->ndev);
983  }
984 
985  unregister_netdev(ifp->ndev);
986  drvr->iflist[ifidx] = NULL;
987  if (ifidx == 0)
989  free_netdev(ifp->ndev);
990  }
991 }
992 
993 int brcmf_attach(uint bus_hdrlen, struct device *dev)
994 {
995  struct brcmf_pub *drvr = NULL;
996  int ret = 0;
997 
998  brcmf_dbg(TRACE, "Enter\n");
999 
1000  /* Allocate primary brcmf_info */
1001  drvr = kzalloc(sizeof(struct brcmf_pub), GFP_ATOMIC);
1002  if (!drvr)
1003  return -ENOMEM;
1004 
1005  mutex_init(&drvr->proto_block);
1006 
1007  /* Link to bus module */
1008  drvr->hdrlen = bus_hdrlen;
1009  drvr->bus_if = dev_get_drvdata(dev);
1010  drvr->bus_if->drvr = drvr;
1011  drvr->dev = dev;
1012 
1013  /* create device debugfs folder */
1014  brcmf_debugfs_attach(drvr);
1015 
1016  /* Attach and link in the protocol */
1017  ret = brcmf_proto_attach(drvr);
1018  if (ret != 0) {
1019  brcmf_dbg(ERROR, "brcmf_prot_attach failed\n");
1020  goto fail;
1021  }
1022 
1023  INIT_WORK(&drvr->setmacaddr_work, _brcmf_set_mac_address);
1024  INIT_WORK(&drvr->multicast_work, _brcmf_set_multicast_list);
1025 
1026  INIT_LIST_HEAD(&drvr->bus_if->dcmd_list);
1027 
1028  return ret;
1029 
1030 fail:
1031  brcmf_detach(dev);
1032 
1033  return ret;
1034 }
1035 
1037 {
1038  int ret = -1;
1039  /* Room for "event_msgs" + '\0' + bitvec */
1040  char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1041  struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1042  struct brcmf_pub *drvr = bus_if->drvr;
1043 
1044  brcmf_dbg(TRACE, "\n");
1045 
1046  /* Bring up the bus */
1047  ret = bus_if->brcmf_bus_init(dev);
1048  if (ret != 0) {
1049  brcmf_dbg(ERROR, "brcmf_sdbrcm_bus_init failed %d\n", ret);
1050  return ret;
1051  }
1052 
1053  brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1054  iovbuf, sizeof(iovbuf));
1056  sizeof(iovbuf));
1057  memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1058 
1060  setbit(drvr->eventmask, BRCMF_E_PRUNE);
1061  setbit(drvr->eventmask, BRCMF_E_AUTH);
1067  setbit(drvr->eventmask, BRCMF_E_JOIN);
1070  setbit(drvr->eventmask, BRCMF_E_LINK);
1077 
1078 /* enable dongle roaming event */
1079 
1080  drvr->pktfilter_count = 1;
1081  /* Setup filter to allow only unicast */
1082  drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1083 
1084  /* Bus is ready, do any protocol initialization */
1085  ret = brcmf_proto_init(drvr);
1086  if (ret < 0)
1087  return ret;
1088 
1089  /* add primary networking interface */
1090  ret = brcmf_add_if(dev, 0, "wlan%d", drvr->mac);
1091  if (ret < 0)
1092  return ret;
1093 
1094  /* signal bus ready */
1095  bus_if->state = BRCMF_BUS_DATA;
1096  return 0;
1097 }
1098 
1099 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1100 {
1101  brcmf_dbg(TRACE, "Enter\n");
1102 
1103  if (drvr) {
1104  /* Stop the protocol module */
1105  brcmf_proto_stop(drvr);
1106 
1107  /* Stop the bus module */
1108  drvr->bus_if->brcmf_bus_stop(drvr->dev);
1109  }
1110 }
1111 
1112 void brcmf_detach(struct device *dev)
1113 {
1114  int i;
1115  struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1116  struct brcmf_pub *drvr = bus_if->drvr;
1117 
1118  brcmf_dbg(TRACE, "Enter\n");
1119 
1120 
1121  /* make sure primary interface removed last */
1122  for (i = BRCMF_MAX_IFS-1; i > -1; i--)
1123  if (drvr->iflist[i])
1124  brcmf_del_if(drvr, i);
1125 
1126  brcmf_bus_detach(drvr);
1127 
1128  if (drvr->prot) {
1131  brcmf_proto_detach(drvr);
1132  }
1133 
1134  brcmf_debugfs_detach(drvr);
1135  bus_if->drvr = NULL;
1136  kfree(drvr);
1137 }
1138 
1139 static int brcmf_get_pend_8021x_cnt(struct brcmf_pub *drvr)
1140 {
1141  return atomic_read(&drvr->pend_8021x_cnt);
1142 }
1143 
1144 #define MAX_WAIT_FOR_8021X_TX 10
1145 
1147 {
1148  struct brcmf_if *ifp = netdev_priv(ndev);
1149  struct brcmf_pub *drvr = ifp->drvr;
1150  int timeout = 10 * HZ / 1000;
1151  int ntimes = MAX_WAIT_FOR_8021X_TX;
1152  int pend = brcmf_get_pend_8021x_cnt(drvr);
1153 
1154  while (ntimes && pend) {
1155  if (pend) {
1157  schedule_timeout(timeout);
1159  ntimes--;
1160  }
1161  pend = brcmf_get_pend_8021x_cnt(drvr);
1162  }
1163  return pend;
1164 }
1165 
1166 #ifdef DEBUG
1167 int brcmf_write_to_file(struct brcmf_pub *drvr, const u8 *buf, int size)
1168 {
1169  int ret = 0;
1170  struct file *fp;
1171  mm_segment_t old_fs;
1172  loff_t pos = 0;
1173 
1174  /* change to KERNEL_DS address limit */
1175  old_fs = get_fs();
1176  set_fs(KERNEL_DS);
1177 
1178  /* open file to write */
1179  fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1180  if (!fp) {
1181  brcmf_dbg(ERROR, "open file error\n");
1182  ret = -1;
1183  goto exit;
1184  }
1185 
1186  /* Write buf to file */
1187  fp->f_op->write(fp, (char __user *)buf, size, &pos);
1188 
1189 exit:
1190  /* free buf before return */
1191  kfree(buf);
1192  /* close file before return */
1193  if (fp)
1194  filp_close(fp, NULL);
1195  /* restore previous address limit */
1196  set_fs(old_fs);
1197 
1198  return ret;
1199 }
1200 #endif /* DEBUG */
1201 
1202 static void brcmf_driver_init(struct work_struct *work)
1203 {
1205 
1206 #ifdef CONFIG_BRCMFMAC_SDIO
1207  brcmf_sdio_init();
1208 #endif
1209 #ifdef CONFIG_BRCMFMAC_USB
1210  brcmf_usb_init();
1211 #endif
1212 }
1213 static DECLARE_WORK(brcmf_driver_work, brcmf_driver_init);
1214 
1215 static int __init brcmfmac_module_init(void)
1216 {
1217  if (!schedule_work(&brcmf_driver_work))
1218  return -EBUSY;
1219 
1220  return 0;
1221 }
1222 
1223 static void __exit brcmfmac_module_exit(void)
1224 {
1225  cancel_work_sync(&brcmf_driver_work);
1226 
1227 #ifdef CONFIG_BRCMFMAC_SDIO
1228  brcmf_sdio_exit();
1229 #endif
1230 #ifdef CONFIG_BRCMFMAC_USB
1231  brcmf_usb_exit();
1232 #endif
1234 }
1235 
1236 module_init(brcmfmac_module_init);
1237 module_exit(brcmfmac_module_exit);