staging: brcm80211: removed remains of assert mechanism in fullmac
[deliverable/linux.git] / drivers / staging / 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 <net/cfg80211.h>
32 #include <defs.h>
33 #include <brcmu_utils.h>
34 #include <brcmu_wifi.h>
35
36 #include "dngl_stats.h"
37 #include "dhd.h"
38 #include "dhd_bus.h"
39 #include "dhd_proto.h"
40 #include "dhd_dbg.h"
41 #include "wl_cfg80211.h"
42 #include "bcmchip.h"
43
44 #if defined(CONFIG_PM_SLEEP)
45 #include <linux/suspend.h>
46 atomic_t brcmf_mmc_suspend;
47 #endif /* defined(CONFIG_PM_SLEEP) */
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_info *info; /* back pointer to brcmf_info */
58 /* OS/stack specifics */
59 struct net_device *net;
60 struct net_device_stats stats;
61 int idx; /* iface idx in dongle */
62 int state; /* interface state */
63 uint subunit; /* subunit */
64 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
65 bool attached; /* Delayed attachment when unset */
66 bool txflowcontrol; /* Per interface flow control indicator */
67 char name[IFNAMSIZ]; /* linux interface name */
68 };
69
70 /* Local private structure (extension of pub) */
71 struct brcmf_info {
72 struct brcmf_pub pub;
73
74 /* OS/stack specifics */
75 struct brcmf_if *iflist[BRCMF_MAX_IFS];
76
77 struct semaphore proto_sem;
78 wait_queue_head_t ioctl_resp_wait;
79
80 /* Thread to issue ioctl for multicast */
81 struct task_struct *sysioc_tsk;
82 struct semaphore sysioc_sem;
83 bool set_multicast;
84 bool set_macaddress;
85 u8 macvalue[ETH_ALEN];
86 atomic_t pend_8021x_cnt;
87 };
88
89 /* Error bits */
90 module_param(brcmf_msg_level, int, 0);
91
92 /* Spawn a thread for system ioctls (set mac, set mcast) */
93 uint brcmf_sysioc = true;
94 module_param(brcmf_sysioc, uint, 0);
95
96 /* ARP offload agent mode : Enable ARP Host Auto-Reply
97 and ARP Peer Auto-Reply */
98 uint brcmf_arp_mode = 0xb;
99 module_param(brcmf_arp_mode, uint, 0);
100
101 /* ARP offload enable */
102 uint brcmf_arp_enable = true;
103 module_param(brcmf_arp_enable, uint, 0);
104
105 /* Global Pkt filter enable control */
106 uint brcmf_pkt_filter_enable = true;
107 module_param(brcmf_pkt_filter_enable, uint, 0);
108
109 /* Pkt filter init setup */
110 uint brcmf_pkt_filter_init;
111 module_param(brcmf_pkt_filter_init, uint, 0);
112
113 /* Pkt filter mode control */
114 uint brcmf_master_mode = true;
115 module_param(brcmf_master_mode, uint, 1);
116
117 extern int brcmf_dongle_memsize;
118 module_param(brcmf_dongle_memsize, int, 0);
119
120 /* Contorl fw roaming */
121 uint brcmf_roam = 1;
122
123 /* Control radio state */
124 uint brcmf_radio_up = 1;
125
126 /* Network inteface name */
127 char iface_name[IFNAMSIZ] = "wlan";
128 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
129
130 /* The following are specific to the SDIO dongle */
131
132 /* IOCTL response timeout */
133 int brcmf_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
134
135 /* Idle timeout for backplane clock */
136 int brcmf_idletime = BRCMF_IDLETIME_TICKS;
137 module_param(brcmf_idletime, int, 0);
138
139 /* Use polling */
140 uint brcmf_poll;
141 module_param(brcmf_poll, uint, 0);
142
143 /* Use interrupts */
144 uint brcmf_intr = true;
145 module_param(brcmf_intr, uint, 0);
146
147 /* SDIO Drive Strength (in milliamps) */
148 uint brcmf_sdiod_drive_strength = 6;
149 module_param(brcmf_sdiod_drive_strength, uint, 0);
150
151 /* Tx/Rx bounds */
152 extern uint brcmf_txbound;
153 extern uint brcmf_rxbound;
154 module_param(brcmf_txbound, uint, 0);
155 module_param(brcmf_rxbound, uint, 0);
156
157 #ifdef SDTEST
158 /* Echo packet generator (pkts/s) */
159 uint brcmf_pktgen;
160 module_param(brcmf_pktgen, uint, 0);
161
162 /* Echo packet len (0 => sawtooth, max 2040) */
163 uint brcmf_pktgen_len;
164 module_param(brcmf_pktgen_len, uint, 0);
165 #endif
166
167 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int idx, u32 *toe_ol);
168 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int idx, u32 toe_ol);
169 static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx, void *pktdata,
170 struct brcmf_event_msg *event_ptr,
171 void **data_ptr);
172
173 /*
174 * Generalized timeout mechanism. Uses spin sleep with exponential
175 * back-off until
176 * the sleep time reaches one jiffy, then switches over to task delay. Usage:
177 *
178 * brcmf_timeout_start(&tmo, usec);
179 * while (!brcmf_timeout_expired(&tmo))
180 * if (poll_something())
181 * break;
182 * if (brcmf_timeout_expired(&tmo))
183 * fatal();
184 */
185
186 void brcmf_timeout_start(struct brcmf_timeout *tmo, uint usec)
187 {
188 tmo->limit = usec;
189 tmo->increment = 0;
190 tmo->elapsed = 0;
191 tmo->tick = 1000000 / HZ;
192 }
193
194 int brcmf_timeout_expired(struct brcmf_timeout *tmo)
195 {
196 /* Does nothing the first call */
197 if (tmo->increment == 0) {
198 tmo->increment = 1;
199 return 0;
200 }
201
202 if (tmo->elapsed >= tmo->limit)
203 return 1;
204
205 /* Add the delay that's about to take place */
206 tmo->elapsed += tmo->increment;
207
208 if (tmo->increment < tmo->tick) {
209 udelay(tmo->increment);
210 tmo->increment *= 2;
211 if (tmo->increment > tmo->tick)
212 tmo->increment = tmo->tick;
213 } else {
214 wait_queue_head_t delay_wait;
215 DECLARE_WAITQUEUE(wait, current);
216 int pending;
217 init_waitqueue_head(&delay_wait);
218 add_wait_queue(&delay_wait, &wait);
219 set_current_state(TASK_INTERRUPTIBLE);
220 schedule_timeout(1);
221 pending = signal_pending(current);
222 remove_wait_queue(&delay_wait, &wait);
223 set_current_state(TASK_RUNNING);
224 if (pending)
225 return 1; /* Interrupted */
226 }
227
228 return 0;
229 }
230
231 static int brcmf_net2idx(struct brcmf_info *drvr_priv, struct net_device *net)
232 {
233 int i = 0;
234
235 while (i < BRCMF_MAX_IFS) {
236 if (drvr_priv->iflist[i] && (drvr_priv->iflist[i]->net == net))
237 return i;
238 i++;
239 }
240
241 return BRCMF_BAD_IF;
242 }
243
244 int brcmf_ifname2idx(struct brcmf_info *drvr_priv, char *name)
245 {
246 int i = BRCMF_MAX_IFS;
247
248 if (name == NULL || *name == '\0')
249 return 0;
250
251 while (--i > 0)
252 if (drvr_priv->iflist[i]
253 && !strncmp(drvr_priv->iflist[i]->name, name, IFNAMSIZ))
254 break;
255
256 BRCMF_TRACE(("%s: return idx %d for \"%s\"\n", __func__, i, name));
257
258 return i; /* default - the primary interface */
259 }
260
261 char *brcmf_ifname(struct brcmf_pub *drvr, int ifidx)
262 {
263 struct brcmf_info *drvr_priv = drvr->info;
264
265 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
266 BRCMF_ERROR(("%s: ifidx %d out of range\n", __func__, ifidx));
267 return "<if_bad>";
268 }
269
270 if (drvr_priv->iflist[ifidx] == NULL) {
271 BRCMF_ERROR(("%s: null i/f %d\n", __func__, ifidx));
272 return "<if_null>";
273 }
274
275 if (drvr_priv->iflist[ifidx]->net)
276 return drvr_priv->iflist[ifidx]->net->name;
277
278 return "<if_none>";
279 }
280
281 static void _brcmf_set_multicast_list(struct brcmf_info *drvr_priv, int ifidx)
282 {
283 struct net_device *dev;
284 struct netdev_hw_addr *ha;
285 u32 allmulti, cnt;
286
287 struct brcmf_ioctl ioc;
288 char *buf, *bufp;
289 uint buflen;
290 int ret;
291
292 dev = drvr_priv->iflist[ifidx]->net;
293 cnt = netdev_mc_count(dev);
294
295 /* Determine initial value of allmulti flag */
296 allmulti = (dev->flags & IFF_ALLMULTI) ? true : false;
297
298 /* Send down the multicast list first. */
299
300 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
301 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
302 if (!bufp) {
303 BRCMF_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
304 brcmf_ifname(&drvr_priv->pub, ifidx), cnt));
305 return;
306 }
307
308 strcpy(bufp, "mcast_list");
309 bufp += strlen("mcast_list") + 1;
310
311 cnt = cpu_to_le32(cnt);
312 memcpy(bufp, &cnt, sizeof(cnt));
313 bufp += sizeof(cnt);
314
315 netdev_for_each_mc_addr(ha, dev) {
316 if (!cnt)
317 break;
318 memcpy(bufp, ha->addr, ETH_ALEN);
319 bufp += ETH_ALEN;
320 cnt--;
321 }
322
323 memset(&ioc, 0, sizeof(ioc));
324 ioc.cmd = BRCMF_C_SET_VAR;
325 ioc.buf = buf;
326 ioc.len = buflen;
327 ioc.set = true;
328
329 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
330 if (ret < 0) {
331 BRCMF_ERROR(("%s: set mcast_list failed, cnt %d\n",
332 brcmf_ifname(&drvr_priv->pub, ifidx), cnt));
333 allmulti = cnt ? true : allmulti;
334 }
335
336 kfree(buf);
337
338 /* Now send the allmulti setting. This is based on the setting in the
339 * net_device flags, but might be modified above to be turned on if we
340 * were trying to set some addresses and dongle rejected it...
341 */
342
343 buflen = sizeof("allmulti") + sizeof(allmulti);
344 buf = kmalloc(buflen, GFP_ATOMIC);
345 if (!buf) {
346 BRCMF_ERROR(("%s: out of memory for allmulti\n",
347 brcmf_ifname(&drvr_priv->pub, ifidx)));
348 return;
349 }
350 allmulti = cpu_to_le32(allmulti);
351
352 if (!brcmu_mkiovar
353 ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
354 BRCMF_ERROR(("%s: mkiovar failed for allmulti, datalen %d "
355 "buflen %u\n",
356 brcmf_ifname(&drvr_priv->pub, ifidx),
357 (int)sizeof(allmulti), buflen));
358 kfree(buf);
359 return;
360 }
361
362 memset(&ioc, 0, sizeof(ioc));
363 ioc.cmd = BRCMF_C_SET_VAR;
364 ioc.buf = buf;
365 ioc.len = buflen;
366 ioc.set = true;
367
368 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
369 if (ret < 0) {
370 BRCMF_ERROR(("%s: set allmulti %d failed\n",
371 brcmf_ifname(&drvr_priv->pub, ifidx),
372 le32_to_cpu(allmulti)));
373 }
374
375 kfree(buf);
376
377 /* Finally, pick up the PROMISC flag as well, like the NIC
378 driver does */
379
380 allmulti = (dev->flags & IFF_PROMISC) ? true : false;
381 allmulti = cpu_to_le32(allmulti);
382
383 memset(&ioc, 0, sizeof(ioc));
384 ioc.cmd = BRCMF_C_SET_PROMISC;
385 ioc.buf = &allmulti;
386 ioc.len = sizeof(allmulti);
387 ioc.set = true;
388
389 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
390 if (ret < 0) {
391 BRCMF_ERROR(("%s: set promisc %d failed\n",
392 brcmf_ifname(&drvr_priv->pub, ifidx),
393 le32_to_cpu(allmulti)));
394 }
395 }
396
397 static int _brcmf_set_mac_address(struct brcmf_info *drvr_priv, int ifidx, u8 *addr)
398 {
399 char buf[32];
400 struct brcmf_ioctl ioc;
401 int ret;
402
403 BRCMF_TRACE(("%s enter\n", __func__));
404 if (!brcmu_mkiovar
405 ("cur_etheraddr", (char *)addr, ETH_ALEN, buf, 32)) {
406 BRCMF_ERROR(("%s: mkiovar failed for cur_etheraddr\n",
407 brcmf_ifname(&drvr_priv->pub, ifidx)));
408 return -1;
409 }
410 memset(&ioc, 0, sizeof(ioc));
411 ioc.cmd = BRCMF_C_SET_VAR;
412 ioc.buf = buf;
413 ioc.len = 32;
414 ioc.set = true;
415
416 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
417 if (ret < 0) {
418 BRCMF_ERROR(("%s: set cur_etheraddr failed\n",
419 brcmf_ifname(&drvr_priv->pub, ifidx)));
420 } else {
421 memcpy(drvr_priv->iflist[ifidx]->net->dev_addr, addr, ETH_ALEN);
422 }
423
424 return ret;
425 }
426
427 #ifdef SOFTAP
428 extern struct net_device *ap_net_dev;
429 #endif
430
431 /* Virtual interfaces only ((ifp && ifp->info && ifp->idx == true) */
432 static void brcmf_op_if(struct brcmf_if *ifp)
433 {
434 struct brcmf_info *drvr_priv;
435 int ret = 0, err = 0;
436
437 drvr_priv = ifp->info;
438
439 BRCMF_TRACE(("%s: idx %d, state %d\n", __func__, ifp->idx, ifp->state));
440
441 switch (ifp->state) {
442 case BRCMF_E_IF_ADD:
443 /*
444 * Delete the existing interface before overwriting it
445 * in case we missed the BRCMF_E_IF_DEL event.
446 */
447 if (ifp->net != NULL) {
448 BRCMF_ERROR(("%s: ERROR: netdev:%s already exists, "
449 "try free & unregister\n",
450 __func__, ifp->net->name));
451 netif_stop_queue(ifp->net);
452 unregister_netdev(ifp->net);
453 free_netdev(ifp->net);
454 }
455 /* Allocate etherdev, including space for private structure */
456 ifp->net = alloc_etherdev(sizeof(drvr_priv));
457 if (!ifp->net) {
458 BRCMF_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
459 ret = -ENOMEM;
460 }
461 if (ret == 0) {
462 strcpy(ifp->net->name, ifp->name);
463 memcpy(netdev_priv(ifp->net), &drvr_priv, sizeof(drvr_priv));
464 err = brcmf_net_attach(&drvr_priv->pub, ifp->idx);
465 if (err != 0) {
466 BRCMF_ERROR(("%s: brcmf_net_attach failed, "
467 "err %d\n",
468 __func__, err));
469 ret = -EOPNOTSUPP;
470 } else {
471 #ifdef SOFTAP
472 /* semaphore that the soft AP CODE
473 waits on */
474 extern struct semaphore ap_eth_sema;
475
476 /* save ptr to wl0.1 netdev for use
477 in wl_iw.c */
478 ap_net_dev = ifp->net;
479 /* signal to the SOFTAP 'sleeper' thread,
480 wl0.1 is ready */
481 up(&ap_eth_sema);
482 #endif
483 BRCMF_TRACE(("\n ==== pid:%x, net_device for "
484 "if:%s created ===\n\n",
485 current->pid, ifp->net->name));
486 ifp->state = 0;
487 }
488 }
489 break;
490 case BRCMF_E_IF_DEL:
491 if (ifp->net != NULL) {
492 BRCMF_TRACE(("\n%s: got 'WLC_E_IF_DEL' state\n",
493 __func__));
494 netif_stop_queue(ifp->net);
495 unregister_netdev(ifp->net);
496 ret = BRCMF_DEL_IF; /* Make sure the free_netdev()
497 is called */
498 }
499 break;
500 default:
501 BRCMF_ERROR(("%s: bad op %d\n", __func__, ifp->state));
502 break;
503 }
504
505 if (ret < 0) {
506 if (ifp->net)
507 free_netdev(ifp->net);
508
509 drvr_priv->iflist[ifp->idx] = NULL;
510 kfree(ifp);
511 #ifdef SOFTAP
512 if (ifp->net == ap_net_dev)
513 ap_net_dev = NULL; /* NULL SOFTAP global
514 wl0.1 as well */
515 #endif /* SOFTAP */
516 }
517 }
518
519 static int _brcmf_sysioc_thread(void *data)
520 {
521 struct brcmf_info *drvr_priv = (struct brcmf_info *) data;
522 int i;
523 #ifdef SOFTAP
524 bool in_ap = false;
525 #endif
526
527 allow_signal(SIGTERM);
528
529 while (down_interruptible(&drvr_priv->sysioc_sem) == 0) {
530 if (kthread_should_stop())
531 break;
532 for (i = 0; i < BRCMF_MAX_IFS; i++) {
533 struct brcmf_if *ifentry = drvr_priv->iflist[i];
534 if (ifentry) {
535 #ifdef SOFTAP
536 in_ap = (ap_net_dev != NULL);
537 #endif /* SOFTAP */
538 if (ifentry->state)
539 brcmf_op_if(ifentry);
540 #ifdef SOFTAP
541 if (drvr_priv->iflist[i] == NULL) {
542 BRCMF_TRACE(("\n\n %s: interface %d "
543 "removed!\n", __func__,
544 i));
545 continue;
546 }
547
548 if (in_ap && drvr_priv->set_macaddress) {
549 BRCMF_TRACE(("attempt to set MAC for"
550 " %s in AP Mode,"
551 " blocked.\n",
552 ifentry->net->name));
553 drvr_priv->set_macaddress = false;
554 continue;
555 }
556
557 if (in_ap && drvr_priv->set_multicast) {
558 BRCMF_TRACE(("attempt to set MULTICAST "
559 "list for %s in AP Mode, "
560 "blocked.\n",
561 ifentry->net->name));
562 drvr_priv->set_multicast = false;
563 continue;
564 }
565 #endif /* SOFTAP */
566 if (drvr_priv->set_multicast) {
567 drvr_priv->set_multicast = false;
568 _brcmf_set_multicast_list(drvr_priv, i);
569 }
570 if (drvr_priv->set_macaddress) {
571 drvr_priv->set_macaddress = false;
572 _brcmf_set_mac_address(drvr_priv, i,
573 drvr_priv->macvalue);
574 }
575 }
576 }
577 }
578 return 0;
579 }
580
581 static int brcmf_netdev_set_mac_address(struct net_device *dev, void *addr)
582 {
583 int ret = 0;
584
585 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(dev);
586 struct sockaddr *sa = (struct sockaddr *)addr;
587 int ifidx;
588
589 ifidx = brcmf_net2idx(drvr_priv, dev);
590 if (ifidx == BRCMF_BAD_IF)
591 return -1;
592
593 memcpy(&drvr_priv->macvalue, sa->sa_data, ETH_ALEN);
594 drvr_priv->set_macaddress = true;
595 up(&drvr_priv->sysioc_sem);
596
597 return ret;
598 }
599
600 static void brcmf_netdev_set_multicast_list(struct net_device *dev)
601 {
602 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(dev);
603 int ifidx;
604
605 ifidx = brcmf_net2idx(drvr_priv, dev);
606 if (ifidx == BRCMF_BAD_IF)
607 return;
608
609 drvr_priv->set_multicast = true;
610 up(&drvr_priv->sysioc_sem);
611 }
612
613 int brcmf_sendpkt(struct brcmf_pub *drvr, int ifidx, struct sk_buff *pktbuf)
614 {
615 struct brcmf_info *drvr_priv = drvr->info;
616
617 /* Reject if down */
618 if (!drvr->up || (drvr->busstate == BRCMF_BUS_DOWN))
619 return -ENODEV;
620
621 /* Update multicast statistic */
622 if (pktbuf->len >= ETH_ALEN) {
623 u8 *pktdata = (u8 *) (pktbuf->data);
624 struct ethhdr *eh = (struct ethhdr *)pktdata;
625
626 if (is_multicast_ether_addr(eh->h_dest))
627 drvr->tx_multicast++;
628 if (ntohs(eh->h_proto) == ETH_P_PAE)
629 atomic_inc(&drvr_priv->pend_8021x_cnt);
630 }
631
632 /* If the protocol uses a data header, apply it */
633 brcmf_proto_hdrpush(drvr, ifidx, pktbuf);
634
635 /* Use bus module to send data frame */
636 return brcmf_sdbrcm_bus_txdata(drvr->bus, pktbuf);
637 }
638
639 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *net)
640 {
641 int ret;
642 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
643 int ifidx;
644
645 BRCMF_TRACE(("%s: Enter\n", __func__));
646
647 /* Reject if down */
648 if (!drvr_priv->pub.up || (drvr_priv->pub.busstate == BRCMF_BUS_DOWN)) {
649 BRCMF_ERROR(("%s: xmit rejected pub.up=%d busstate=%d\n",
650 __func__, drvr_priv->pub.up,
651 drvr_priv->pub.busstate));
652 netif_stop_queue(net);
653 return -ENODEV;
654 }
655
656 ifidx = brcmf_net2idx(drvr_priv, net);
657 if (ifidx == BRCMF_BAD_IF) {
658 BRCMF_ERROR(("%s: bad ifidx %d\n", __func__, ifidx));
659 netif_stop_queue(net);
660 return -ENODEV;
661 }
662
663 /* Make sure there's enough room for any header */
664 if (skb_headroom(skb) < drvr_priv->pub.hdrlen) {
665 struct sk_buff *skb2;
666
667 BRCMF_INFO(("%s: insufficient headroom\n",
668 brcmf_ifname(&drvr_priv->pub, ifidx)));
669 drvr_priv->pub.tx_realloc++;
670 skb2 = skb_realloc_headroom(skb, drvr_priv->pub.hdrlen);
671 dev_kfree_skb(skb);
672 skb = skb2;
673 if (skb == NULL) {
674 BRCMF_ERROR(("%s: skb_realloc_headroom failed\n",
675 brcmf_ifname(&drvr_priv->pub, ifidx)));
676 ret = -ENOMEM;
677 goto done;
678 }
679 }
680
681 ret = brcmf_sendpkt(&drvr_priv->pub, ifidx, skb);
682
683 done:
684 if (ret)
685 drvr_priv->pub.dstats.tx_dropped++;
686 else
687 drvr_priv->pub.tx_packets++;
688
689 /* Return ok: we always eat the packet */
690 return 0;
691 }
692
693 void brcmf_txflowcontrol(struct brcmf_pub *drvr, int ifidx, bool state)
694 {
695 struct net_device *net;
696 struct brcmf_info *drvr_priv = drvr->info;
697
698 BRCMF_TRACE(("%s: Enter\n", __func__));
699
700 drvr->txoff = state;
701 net = drvr_priv->iflist[ifidx]->net;
702 if (state == ON)
703 netif_stop_queue(net);
704 else
705 netif_wake_queue(net);
706 }
707
708 void brcmf_rx_frame(struct brcmf_pub *drvr, int ifidx, struct sk_buff *skb,
709 int numpkt)
710 {
711 struct brcmf_info *drvr_priv = drvr->info;
712 unsigned char *eth;
713 uint len;
714 void *data;
715 struct sk_buff *pnext, *save_pktbuf;
716 int i;
717 struct brcmf_if *ifp;
718 struct brcmf_event_msg event;
719
720 BRCMF_TRACE(("%s: Enter\n", __func__));
721
722 save_pktbuf = skb;
723
724 for (i = 0; skb && i < numpkt; i++, skb = pnext) {
725
726 pnext = skb->next;
727 skb->next = NULL;
728
729 /* Get the protocol, maintain skb around eth_type_trans()
730 * The main reason for this hack is for the limitation of
731 * Linux 2.4 where 'eth_type_trans' uses the
732 * 'net->hard_header_len'
733 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
734 * coping of the packet coming from the network stack to add
735 * BDC, Hardware header etc, during network interface
736 * registration
737 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
738 * required
739 * for BDC, Hardware header etc. and not just the ETH_HLEN
740 */
741 eth = skb->data;
742 len = skb->len;
743
744 ifp = drvr_priv->iflist[ifidx];
745 if (ifp == NULL)
746 ifp = drvr_priv->iflist[0];
747
748 skb->dev = ifp->net;
749 skb->protocol = eth_type_trans(skb, skb->dev);
750
751 if (skb->pkt_type == PACKET_MULTICAST)
752 drvr_priv->pub.rx_multicast++;
753
754 skb->data = eth;
755 skb->len = len;
756
757 /* Strip header, count, deliver upward */
758 skb_pull(skb, ETH_HLEN);
759
760 /* Process special event packets and then discard them */
761 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
762 brcmf_host_event(drvr_priv, &ifidx,
763 skb_mac_header(skb),
764 &event, &data);
765
766 if (drvr_priv->iflist[ifidx] &&
767 !drvr_priv->iflist[ifidx]->state)
768 ifp = drvr_priv->iflist[ifidx];
769
770 if (ifp->net)
771 ifp->net->last_rx = jiffies;
772
773 drvr->dstats.rx_bytes += skb->len;
774 drvr->rx_packets++; /* Local count */
775
776 if (in_interrupt()) {
777 netif_rx(skb);
778 } else {
779 /* If the receive is not processed inside an ISR,
780 * the softirqd must be woken explicitly to service
781 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
782 * by netif_rx_ni(), but in earlier kernels, we need
783 * to do it manually.
784 */
785 netif_rx_ni(skb);
786 }
787 }
788 }
789
790 void brcmf_txcomplete(struct brcmf_pub *drvr, struct sk_buff *txp, bool success)
791 {
792 uint ifidx;
793 struct brcmf_info *drvr_priv = drvr->info;
794 struct ethhdr *eh;
795 u16 type;
796
797 brcmf_proto_hdrpull(drvr, &ifidx, txp);
798
799 eh = (struct ethhdr *)(txp->data);
800 type = ntohs(eh->h_proto);
801
802 if (type == ETH_P_PAE)
803 atomic_dec(&drvr_priv->pend_8021x_cnt);
804
805 }
806
807 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *net)
808 {
809 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
810 struct brcmf_if *ifp;
811 int ifidx;
812
813 BRCMF_TRACE(("%s: Enter\n", __func__));
814
815 ifidx = brcmf_net2idx(drvr_priv, net);
816 if (ifidx == BRCMF_BAD_IF)
817 return NULL;
818
819 ifp = drvr_priv->iflist[ifidx];
820
821 if (drvr_priv->pub.up) {
822 /* Use the protocol to get dongle stats */
823 brcmf_proto_dstats(&drvr_priv->pub);
824 }
825
826 /* Copy dongle stats to net device stats */
827 ifp->stats.rx_packets = drvr_priv->pub.dstats.rx_packets;
828 ifp->stats.tx_packets = drvr_priv->pub.dstats.tx_packets;
829 ifp->stats.rx_bytes = drvr_priv->pub.dstats.rx_bytes;
830 ifp->stats.tx_bytes = drvr_priv->pub.dstats.tx_bytes;
831 ifp->stats.rx_errors = drvr_priv->pub.dstats.rx_errors;
832 ifp->stats.tx_errors = drvr_priv->pub.dstats.tx_errors;
833 ifp->stats.rx_dropped = drvr_priv->pub.dstats.rx_dropped;
834 ifp->stats.tx_dropped = drvr_priv->pub.dstats.tx_dropped;
835 ifp->stats.multicast = drvr_priv->pub.dstats.multicast;
836
837 return &ifp->stats;
838 }
839
840 /* Retrieve current toe component enables, which are kept
841 as a bitmap in toe_ol iovar */
842 static int brcmf_toe_get(struct brcmf_info *drvr_priv, int ifidx, u32 *toe_ol)
843 {
844 struct brcmf_ioctl ioc;
845 char buf[32];
846 int ret;
847
848 memset(&ioc, 0, sizeof(ioc));
849
850 ioc.cmd = BRCMF_C_GET_VAR;
851 ioc.buf = buf;
852 ioc.len = (uint) sizeof(buf);
853 ioc.set = false;
854
855 strcpy(buf, "toe_ol");
856 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
857 if (ret < 0) {
858 /* Check for older dongle image that doesn't support toe_ol */
859 if (ret == -EIO) {
860 BRCMF_ERROR(("%s: toe not supported by device\n",
861 brcmf_ifname(&drvr_priv->pub, ifidx)));
862 return -EOPNOTSUPP;
863 }
864
865 BRCMF_INFO(("%s: could not get toe_ol: ret=%d\n",
866 brcmf_ifname(&drvr_priv->pub, ifidx), ret));
867 return ret;
868 }
869
870 memcpy(toe_ol, buf, sizeof(u32));
871 return 0;
872 }
873
874 /* Set current toe component enables in toe_ol iovar,
875 and set toe global enable iovar */
876 static int brcmf_toe_set(struct brcmf_info *drvr_priv, int ifidx, u32 toe_ol)
877 {
878 struct brcmf_ioctl ioc;
879 char buf[32];
880 int toe, ret;
881
882 memset(&ioc, 0, sizeof(ioc));
883
884 ioc.cmd = BRCMF_C_SET_VAR;
885 ioc.buf = buf;
886 ioc.len = (uint) sizeof(buf);
887 ioc.set = true;
888
889 /* Set toe_ol as requested */
890
891 strcpy(buf, "toe_ol");
892 memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
893
894 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
895 if (ret < 0) {
896 BRCMF_ERROR(("%s: could not set toe_ol: ret=%d\n",
897 brcmf_ifname(&drvr_priv->pub, ifidx), ret));
898 return ret;
899 }
900
901 /* Enable toe globally only if any components are enabled. */
902
903 toe = (toe_ol != 0);
904
905 strcpy(buf, "toe");
906 memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
907
908 ret = brcmf_proto_ioctl(&drvr_priv->pub, ifidx, &ioc, ioc.buf, ioc.len);
909 if (ret < 0) {
910 BRCMF_ERROR(("%s: could not set toe: ret=%d\n",
911 brcmf_ifname(&drvr_priv->pub, ifidx), ret));
912 return ret;
913 }
914
915 return 0;
916 }
917
918 static void brcmf_ethtool_get_drvinfo(struct net_device *net,
919 struct ethtool_drvinfo *info)
920 {
921 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
922
923 sprintf(info->driver, KBUILD_MODNAME);
924 sprintf(info->version, "%lu", drvr_priv->pub.drv_version);
925 sprintf(info->fw_version, "%s", BCM4329_FW_NAME);
926 sprintf(info->bus_info, "%s",
927 dev_name(&brcmf_cfg80211_get_sdio_func()->dev));
928 }
929
930 struct ethtool_ops brcmf_ethtool_ops = {
931 .get_drvinfo = brcmf_ethtool_get_drvinfo
932 };
933
934 static int brcmf_ethtool(struct brcmf_info *drvr_priv, void *uaddr)
935 {
936 struct ethtool_drvinfo info;
937 char drvname[sizeof(info.driver)];
938 u32 cmd;
939 struct ethtool_value edata;
940 u32 toe_cmpnt, csum_dir;
941 int ret;
942
943 BRCMF_TRACE(("%s: Enter\n", __func__));
944
945 /* all ethtool calls start with a cmd word */
946 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
947 return -EFAULT;
948
949 switch (cmd) {
950 case ETHTOOL_GDRVINFO:
951 /* Copy out any request driver name */
952 if (copy_from_user(&info, uaddr, sizeof(info)))
953 return -EFAULT;
954 strncpy(drvname, info.driver, sizeof(info.driver));
955 drvname[sizeof(info.driver) - 1] = '\0';
956
957 /* clear struct for return */
958 memset(&info, 0, sizeof(info));
959 info.cmd = cmd;
960
961 /* if requested, identify ourselves */
962 if (strcmp(drvname, "?dhd") == 0) {
963 sprintf(info.driver, "dhd");
964 strcpy(info.version, BRCMF_VERSION_STR);
965 }
966
967 /* otherwise, require dongle to be up */
968 else if (!drvr_priv->pub.up) {
969 BRCMF_ERROR(("%s: dongle is not up\n", __func__));
970 return -ENODEV;
971 }
972
973 /* finally, report dongle driver type */
974 else if (drvr_priv->pub.iswl)
975 sprintf(info.driver, "wl");
976 else
977 sprintf(info.driver, "xx");
978
979 sprintf(info.version, "%lu", drvr_priv->pub.drv_version);
980 if (copy_to_user(uaddr, &info, sizeof(info)))
981 return -EFAULT;
982 BRCMF_CTL(("%s: given %*s, returning %s\n", __func__,
983 (int)sizeof(drvname), drvname, info.driver));
984 break;
985
986 /* Get toe offload components from dongle */
987 case ETHTOOL_GRXCSUM:
988 case ETHTOOL_GTXCSUM:
989 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
990 if (ret < 0)
991 return ret;
992
993 csum_dir =
994 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
995
996 edata.cmd = cmd;
997 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
998
999 if (copy_to_user(uaddr, &edata, sizeof(edata)))
1000 return -EFAULT;
1001 break;
1002
1003 /* Set toe offload components in dongle */
1004 case ETHTOOL_SRXCSUM:
1005 case ETHTOOL_STXCSUM:
1006 if (copy_from_user(&edata, uaddr, sizeof(edata)))
1007 return -EFAULT;
1008
1009 /* Read the current settings, update and write back */
1010 ret = brcmf_toe_get(drvr_priv, 0, &toe_cmpnt);
1011 if (ret < 0)
1012 return ret;
1013
1014 csum_dir =
1015 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1016
1017 if (edata.data != 0)
1018 toe_cmpnt |= csum_dir;
1019 else
1020 toe_cmpnt &= ~csum_dir;
1021
1022 ret = brcmf_toe_set(drvr_priv, 0, toe_cmpnt);
1023 if (ret < 0)
1024 return ret;
1025
1026 /* If setting TX checksum mode, tell Linux the new mode */
1027 if (cmd == ETHTOOL_STXCSUM) {
1028 if (edata.data)
1029 drvr_priv->iflist[0]->net->features |=
1030 NETIF_F_IP_CSUM;
1031 else
1032 drvr_priv->iflist[0]->net->features &=
1033 ~NETIF_F_IP_CSUM;
1034 }
1035
1036 break;
1037
1038 default:
1039 return -EOPNOTSUPP;
1040 }
1041
1042 return 0;
1043 }
1044
1045 static int brcmf_netdev_ioctl_entry(struct net_device *net, struct ifreq *ifr,
1046 int cmd)
1047 {
1048 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
1049 struct brcmf_c_ioctl ioc;
1050 int bcmerror = 0;
1051 int buflen = 0;
1052 void *buf = NULL;
1053 uint driver = 0;
1054 int ifidx;
1055 bool is_set_key_cmd;
1056
1057 ifidx = brcmf_net2idx(drvr_priv, net);
1058 BRCMF_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __func__, ifidx, cmd));
1059
1060 if (ifidx == BRCMF_BAD_IF)
1061 return -1;
1062
1063 if (cmd == SIOCETHTOOL)
1064 return brcmf_ethtool(drvr_priv, (void *)ifr->ifr_data);
1065
1066 if (cmd != SIOCDEVPRIVATE)
1067 return -EOPNOTSUPP;
1068
1069 memset(&ioc, 0, sizeof(ioc));
1070
1071 /* Copy the ioc control structure part of ioctl request */
1072 if (copy_from_user(&ioc, ifr->ifr_data, sizeof(struct brcmf_ioctl))) {
1073 bcmerror = -EINVAL;
1074 goto done;
1075 }
1076
1077 /* Copy out any buffer passed */
1078 if (ioc.buf) {
1079 buflen = min_t(int, ioc.len, BRCMF_IOCTL_MAXLEN);
1080 /* optimization for direct ioctl calls from kernel */
1081 /*
1082 if (segment_eq(get_fs(), KERNEL_DS)) {
1083 buf = ioc.buf;
1084 } else {
1085 */
1086 {
1087 buf = kmalloc(buflen, GFP_ATOMIC);
1088 if (!buf) {
1089 bcmerror = -ENOMEM;
1090 goto done;
1091 }
1092 if (copy_from_user(buf, ioc.buf, buflen)) {
1093 bcmerror = -EINVAL;
1094 goto done;
1095 }
1096 }
1097 }
1098
1099 /* To differentiate read 4 more byes */
1100 if ((copy_from_user(&driver, (char *)ifr->ifr_data +
1101 sizeof(struct brcmf_ioctl), sizeof(uint)) != 0)) {
1102 bcmerror = -EINVAL;
1103 goto done;
1104 }
1105
1106 if (!capable(CAP_NET_ADMIN)) {
1107 bcmerror = -EPERM;
1108 goto done;
1109 }
1110
1111 /* check for local brcmf ioctl and handle it */
1112 if (driver == BRCMF_IOCTL_MAGIC) {
1113 bcmerror = brcmf_c_ioctl((void *)&drvr_priv->pub, &ioc, buf, buflen);
1114 if (bcmerror)
1115 drvr_priv->pub.bcmerror = bcmerror;
1116 goto done;
1117 }
1118
1119 /* send to dongle (must be up, and wl) */
1120 if ((drvr_priv->pub.busstate != BRCMF_BUS_DATA)) {
1121 BRCMF_ERROR(("%s DONGLE_DOWN,__func__\n", __func__));
1122 bcmerror = -EIO;
1123 goto done;
1124 }
1125
1126 if (!drvr_priv->pub.iswl) {
1127 bcmerror = -EIO;
1128 goto done;
1129 }
1130
1131 /*
1132 * Intercept BRCMF_C_SET_KEY IOCTL - serialize M4 send and
1133 * set key IOCTL to prevent M4 encryption.
1134 */
1135 is_set_key_cmd = ((ioc.cmd == BRCMF_C_SET_KEY) ||
1136 ((ioc.cmd == BRCMF_C_SET_VAR) &&
1137 !(strncmp("wsec_key", ioc.buf, 9))) ||
1138 ((ioc.cmd == BRCMF_C_SET_VAR) &&
1139 !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1140 if (is_set_key_cmd)
1141 brcmf_netdev_wait_pend8021x(net);
1142
1143 bcmerror =
1144 brcmf_proto_ioctl(&drvr_priv->pub, ifidx, (struct brcmf_ioctl *)&ioc,
1145 buf, buflen);
1146
1147 done:
1148 if (!bcmerror && buf && ioc.buf) {
1149 if (copy_to_user(ioc.buf, buf, buflen))
1150 bcmerror = -EFAULT;
1151 }
1152
1153 kfree(buf);
1154
1155 if (bcmerror > 0)
1156 bcmerror = 0;
1157
1158 return bcmerror;
1159 }
1160
1161 static int brcmf_netdev_stop(struct net_device *net)
1162 {
1163 #if !defined(IGNORE_ETH0_DOWN)
1164 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
1165
1166 BRCMF_TRACE(("%s: Enter\n", __func__));
1167 brcmf_cfg80211_down();
1168 if (drvr_priv->pub.up == 0)
1169 return 0;
1170
1171 /* Set state and stop OS transmissions */
1172 drvr_priv->pub.up = 0;
1173 netif_stop_queue(net);
1174 #else
1175 BRCMF_ERROR(("BYPASS %s:due to BRCM compilation: under investigation\n",
1176 __func__));
1177 #endif /* !defined(IGNORE_ETH0_DOWN) */
1178
1179 return 0;
1180 }
1181
1182 static int brcmf_netdev_open(struct net_device *net)
1183 {
1184 struct brcmf_info *drvr_priv = *(struct brcmf_info **) netdev_priv(net);
1185 u32 toe_ol;
1186 int ifidx = brcmf_net2idx(drvr_priv, net);
1187 s32 ret = 0;
1188
1189 BRCMF_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1190
1191 if (ifidx == 0) { /* do it only for primary eth0 */
1192
1193 /* try to bring up bus */
1194 ret = brcmf_bus_start(&drvr_priv->pub);
1195 if (ret != 0) {
1196 BRCMF_ERROR(("%s: failed with code %d\n",
1197 __func__, ret));
1198 return -1;
1199 }
1200 atomic_set(&drvr_priv->pend_8021x_cnt, 0);
1201
1202 memcpy(net->dev_addr, drvr_priv->pub.mac, ETH_ALEN);
1203
1204 /* Get current TOE mode from dongle */
1205 if (brcmf_toe_get(drvr_priv, ifidx, &toe_ol) >= 0
1206 && (toe_ol & TOE_TX_CSUM_OL) != 0)
1207 drvr_priv->iflist[ifidx]->net->features |=
1208 NETIF_F_IP_CSUM;
1209 else
1210 drvr_priv->iflist[ifidx]->net->features &=
1211 ~NETIF_F_IP_CSUM;
1212 }
1213 /* Allow transmit calls */
1214 netif_start_queue(net);
1215 drvr_priv->pub.up = 1;
1216 if (unlikely(brcmf_cfg80211_up())) {
1217 BRCMF_ERROR(("%s: failed to bring up cfg80211\n",
1218 __func__));
1219 return -1;
1220 }
1221
1222 return ret;
1223 }
1224
1225 int
1226 brcmf_add_if(struct brcmf_info *drvr_priv, int ifidx, void *handle, char *name,
1227 u8 *mac_addr, u32 flags, u8 bssidx)
1228 {
1229 struct brcmf_if *ifp;
1230
1231 BRCMF_TRACE(("%s: idx %d, handle->%p\n", __func__, ifidx, handle));
1232
1233 ifp = drvr_priv->iflist[ifidx];
1234 if (!ifp) {
1235 ifp = kmalloc(sizeof(struct brcmf_if), GFP_ATOMIC);
1236 if (!ifp) {
1237 BRCMF_ERROR(("%s: OOM - struct brcmf_if\n", __func__));
1238 return -ENOMEM;
1239 }
1240 }
1241
1242 memset(ifp, 0, sizeof(struct brcmf_if));
1243 ifp->info = drvr_priv;
1244 drvr_priv->iflist[ifidx] = ifp;
1245 strlcpy(ifp->name, name, IFNAMSIZ);
1246 if (mac_addr != NULL)
1247 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
1248
1249 if (handle == NULL) {
1250 ifp->state = BRCMF_E_IF_ADD;
1251 ifp->idx = ifidx;
1252 up(&drvr_priv->sysioc_sem);
1253 } else
1254 ifp->net = (struct net_device *)handle;
1255
1256 return 0;
1257 }
1258
1259 void brcmf_del_if(struct brcmf_info *drvr_priv, int ifidx)
1260 {
1261 struct brcmf_if *ifp;
1262
1263 BRCMF_TRACE(("%s: idx %d\n", __func__, ifidx));
1264
1265 ifp = drvr_priv->iflist[ifidx];
1266 if (!ifp) {
1267 BRCMF_ERROR(("%s: Null interface\n", __func__));
1268 return;
1269 }
1270
1271 ifp->state = BRCMF_E_IF_DEL;
1272 ifp->idx = ifidx;
1273 up(&drvr_priv->sysioc_sem);
1274 }
1275
1276 struct brcmf_pub *brcmf_attach(struct brcmf_bus *bus, uint bus_hdrlen)
1277 {
1278 struct brcmf_info *drvr_priv = NULL;
1279 struct net_device *net;
1280
1281 BRCMF_TRACE(("%s: Enter\n", __func__));
1282
1283 /* Allocate etherdev, including space for private structure */
1284 net = alloc_etherdev(sizeof(drvr_priv));
1285 if (!net) {
1286 BRCMF_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
1287 goto fail;
1288 }
1289
1290 /* Allocate primary brcmf_info */
1291 drvr_priv = kzalloc(sizeof(struct brcmf_info), GFP_ATOMIC);
1292 if (!drvr_priv) {
1293 BRCMF_ERROR(("%s: OOM - alloc brcmf_info\n", __func__));
1294 goto fail;
1295 }
1296
1297 /*
1298 * Save the brcmf_info into the priv
1299 */
1300 memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1301
1302 /* Set network interface name if it was provided as module parameter */
1303 if (iface_name[0]) {
1304 int len;
1305 char ch;
1306 strncpy(net->name, iface_name, IFNAMSIZ);
1307 net->name[IFNAMSIZ - 1] = 0;
1308 len = strlen(net->name);
1309 ch = net->name[len - 1];
1310 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1311 strcat(net->name, "%d");
1312 }
1313
1314 if (brcmf_add_if(drvr_priv, 0, (void *)net, net->name, NULL, 0, 0) ==
1315 BRCMF_BAD_IF)
1316 goto fail;
1317
1318 net->netdev_ops = NULL;
1319 sema_init(&drvr_priv->proto_sem, 1);
1320 /* Initialize other structure content */
1321 init_waitqueue_head(&drvr_priv->ioctl_resp_wait);
1322
1323 /* Link to info module */
1324 drvr_priv->pub.info = drvr_priv;
1325
1326 /* Link to bus module */
1327 drvr_priv->pub.bus = bus;
1328 drvr_priv->pub.hdrlen = bus_hdrlen;
1329
1330 /* Attach and link in the protocol */
1331 if (brcmf_proto_attach(&drvr_priv->pub) != 0) {
1332 BRCMF_ERROR(("brcmf_prot_attach failed\n"));
1333 goto fail;
1334 }
1335
1336 /* Attach and link in the cfg80211 */
1337 if (unlikely(brcmf_cfg80211_attach(net, &drvr_priv->pub))) {
1338 BRCMF_ERROR(("wl_cfg80211_attach failed\n"));
1339 goto fail;
1340 }
1341
1342 if (brcmf_sysioc) {
1343 sema_init(&drvr_priv->sysioc_sem, 0);
1344 drvr_priv->sysioc_tsk = kthread_run(_brcmf_sysioc_thread, drvr_priv,
1345 "_brcmf_sysioc");
1346 if (IS_ERR(drvr_priv->sysioc_tsk)) {
1347 printk(KERN_WARNING
1348 "_brcmf_sysioc thread failed to start\n");
1349 drvr_priv->sysioc_tsk = NULL;
1350 }
1351 } else
1352 drvr_priv->sysioc_tsk = NULL;
1353
1354 /*
1355 * Save the brcmf_info into the priv
1356 */
1357 memcpy(netdev_priv(net), &drvr_priv, sizeof(drvr_priv));
1358
1359 #if defined(CONFIG_PM_SLEEP)
1360 atomic_set(&brcmf_mmc_suspend, false);
1361 #endif /* defined(CONFIG_PM_SLEEP) */
1362 return &drvr_priv->pub;
1363
1364 fail:
1365 if (net)
1366 free_netdev(net);
1367 if (drvr_priv)
1368 brcmf_detach(&drvr_priv->pub);
1369
1370 return NULL;
1371 }
1372
1373 int brcmf_bus_start(struct brcmf_pub *drvr)
1374 {
1375 int ret = -1;
1376 struct brcmf_info *drvr_priv = drvr->info;
1377 /* Room for "event_msgs" + '\0' + bitvec */
1378 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];
1379
1380 BRCMF_TRACE(("%s:\n", __func__));
1381
1382 /* Bring up the bus */
1383 ret = brcmf_sdbrcm_bus_init(&drvr_priv->pub, true);
1384 if (ret != 0) {
1385 BRCMF_ERROR(("%s, brcmf_sdbrcm_bus_init failed %d\n", __func__,
1386 ret));
1387 return ret;
1388 }
1389
1390 /* If bus is not ready, can't come up */
1391 if (drvr_priv->pub.busstate != BRCMF_BUS_DATA) {
1392 BRCMF_ERROR(("%s failed bus is not ready\n", __func__));
1393 return -ENODEV;
1394 }
1395
1396 brcmu_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
1397 iovbuf, sizeof(iovbuf));
1398 brcmf_proto_cdc_query_ioctl(drvr, 0, BRCMF_C_GET_VAR, iovbuf,
1399 sizeof(iovbuf));
1400 memcpy(drvr->eventmask, iovbuf, BRCMF_EVENTING_MASK_LEN);
1401
1402 setbit(drvr->eventmask, BRCMF_E_SET_SSID);
1403 setbit(drvr->eventmask, BRCMF_E_PRUNE);
1404 setbit(drvr->eventmask, BRCMF_E_AUTH);
1405 setbit(drvr->eventmask, BRCMF_E_REASSOC);
1406 setbit(drvr->eventmask, BRCMF_E_REASSOC_IND);
1407 setbit(drvr->eventmask, BRCMF_E_DEAUTH_IND);
1408 setbit(drvr->eventmask, BRCMF_E_DISASSOC_IND);
1409 setbit(drvr->eventmask, BRCMF_E_DISASSOC);
1410 setbit(drvr->eventmask, BRCMF_E_JOIN);
1411 setbit(drvr->eventmask, BRCMF_E_ASSOC_IND);
1412 setbit(drvr->eventmask, BRCMF_E_PSK_SUP);
1413 setbit(drvr->eventmask, BRCMF_E_LINK);
1414 setbit(drvr->eventmask, BRCMF_E_NDIS_LINK);
1415 setbit(drvr->eventmask, BRCMF_E_MIC_ERROR);
1416 setbit(drvr->eventmask, BRCMF_E_PMKID_CACHE);
1417 setbit(drvr->eventmask, BRCMF_E_TXFAIL);
1418 setbit(drvr->eventmask, BRCMF_E_JOIN_START);
1419 setbit(drvr->eventmask, BRCMF_E_SCAN_COMPLETE);
1420
1421 /* enable dongle roaming event */
1422
1423 drvr->pktfilter_count = 1;
1424 /* Setup filter to allow only unicast */
1425 drvr->pktfilter[0] = "100 0 0 0 0x01 0x00";
1426
1427 /* Bus is ready, do any protocol initialization */
1428 ret = brcmf_proto_init(&drvr_priv->pub);
1429 if (ret < 0)
1430 return ret;
1431
1432 return 0;
1433 }
1434
1435 static struct net_device_ops brcmf_netdev_ops_pri = {
1436 .ndo_open = brcmf_netdev_open,
1437 .ndo_stop = brcmf_netdev_stop,
1438 .ndo_get_stats = brcmf_netdev_get_stats,
1439 .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1440 .ndo_start_xmit = brcmf_netdev_start_xmit,
1441 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1442 .ndo_set_multicast_list = brcmf_netdev_set_multicast_list
1443 };
1444
1445 int brcmf_net_attach(struct brcmf_pub *drvr, int ifidx)
1446 {
1447 struct brcmf_info *drvr_priv = drvr->info;
1448 struct net_device *net;
1449 u8 temp_addr[ETH_ALEN] = {
1450 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1451
1452 BRCMF_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1453
1454 net = drvr_priv->iflist[ifidx]->net;
1455 net->netdev_ops = &brcmf_netdev_ops_pri;
1456
1457 /*
1458 * We have to use the primary MAC for virtual interfaces
1459 */
1460 if (ifidx != 0) {
1461 /* for virtual interfaces use the primary MAC */
1462 memcpy(temp_addr, drvr_priv->pub.mac, ETH_ALEN);
1463
1464 }
1465
1466 if (ifidx == 1) {
1467 BRCMF_TRACE(("%s ACCESS POINT MAC:\n", __func__));
1468 /* ACCESSPOINT INTERFACE CASE */
1469 temp_addr[0] |= 0X02; /* set bit 2 ,
1470 - Locally Administered address */
1471
1472 }
1473 net->hard_header_len = ETH_HLEN + drvr_priv->pub.hdrlen;
1474 net->ethtool_ops = &brcmf_ethtool_ops;
1475
1476 drvr_priv->pub.rxsz = net->mtu + net->hard_header_len +
1477 drvr_priv->pub.hdrlen;
1478
1479 memcpy(net->dev_addr, temp_addr, ETH_ALEN);
1480
1481 if (register_netdev(net) != 0) {
1482 BRCMF_ERROR(("%s: couldn't register the net device\n",
1483 __func__));
1484 goto fail;
1485 }
1486
1487 BRCMF_INFO(("%s: Broadcom Dongle Host Driver\n", net->name));
1488
1489 return 0;
1490
1491 fail:
1492 net->netdev_ops = NULL;
1493 return -EBADE;
1494 }
1495
1496 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1497 {
1498 struct brcmf_info *drvr_priv;
1499
1500 BRCMF_TRACE(("%s: Enter\n", __func__));
1501
1502 if (drvr) {
1503 drvr_priv = drvr->info;
1504 if (drvr_priv) {
1505 /* Stop the protocol module */
1506 brcmf_proto_stop(&drvr_priv->pub);
1507
1508 /* Stop the bus module */
1509 brcmf_sdbrcm_bus_stop(drvr_priv->pub.bus, true);
1510 }
1511 }
1512 }
1513
1514 void brcmf_detach(struct brcmf_pub *drvr)
1515 {
1516 struct brcmf_info *drvr_priv;
1517
1518 BRCMF_TRACE(("%s: Enter\n", __func__));
1519
1520 if (drvr) {
1521 drvr_priv = drvr->info;
1522 if (drvr_priv) {
1523 struct brcmf_if *ifp;
1524 int i;
1525
1526 for (i = 1; i < BRCMF_MAX_IFS; i++)
1527 if (drvr_priv->iflist[i])
1528 brcmf_del_if(drvr_priv, i);
1529
1530 ifp = drvr_priv->iflist[0];
1531 if (ifp->net->netdev_ops == &brcmf_netdev_ops_pri) {
1532 brcmf_netdev_stop(ifp->net);
1533 unregister_netdev(ifp->net);
1534 }
1535
1536 if (drvr_priv->sysioc_tsk) {
1537 send_sig(SIGTERM, drvr_priv->sysioc_tsk, 1);
1538 kthread_stop(drvr_priv->sysioc_tsk);
1539 drvr_priv->sysioc_tsk = NULL;
1540 }
1541
1542 brcmf_bus_detach(drvr);
1543
1544 if (drvr->prot)
1545 brcmf_proto_detach(drvr);
1546
1547 brcmf_cfg80211_detach();
1548
1549 free_netdev(ifp->net);
1550 kfree(ifp);
1551 kfree(drvr_priv);
1552 }
1553 }
1554 }
1555
1556 static void __exit brcmf_module_cleanup(void)
1557 {
1558 BRCMF_TRACE(("%s: Enter\n", __func__));
1559
1560 brcmf_bus_unregister();
1561 }
1562
1563 static int __init brcmf_module_init(void)
1564 {
1565 int error;
1566
1567 BRCMF_TRACE(("%s: Enter\n", __func__));
1568
1569 error = brcmf_bus_register();
1570
1571 if (error) {
1572 BRCMF_ERROR(("%s: brcmf_bus_register failed\n", __func__));
1573 goto failed;
1574 }
1575 return 0;
1576
1577 failed:
1578 return -EINVAL;
1579 }
1580
1581 module_init(brcmf_module_init);
1582 module_exit(brcmf_module_cleanup);
1583
1584 int brcmf_os_proto_block(struct brcmf_pub *drvr)
1585 {
1586 struct brcmf_info *drvr_priv = drvr->info;
1587
1588 if (drvr_priv) {
1589 down(&drvr_priv->proto_sem);
1590 return 1;
1591 }
1592 return 0;
1593 }
1594
1595 int brcmf_os_proto_unblock(struct brcmf_pub *drvr)
1596 {
1597 struct brcmf_info *drvr_priv = drvr->info;
1598
1599 if (drvr_priv) {
1600 up(&drvr_priv->proto_sem);
1601 return 1;
1602 }
1603
1604 return 0;
1605 }
1606
1607 unsigned int brcmf_os_get_ioctl_resp_timeout(void)
1608 {
1609 return (unsigned int)brcmf_ioctl_timeout_msec;
1610 }
1611
1612 void brcmf_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
1613 {
1614 brcmf_ioctl_timeout_msec = (int)timeout_msec;
1615 }
1616
1617 int brcmf_os_ioctl_resp_wait(struct brcmf_pub *drvr, uint *condition,
1618 bool *pending)
1619 {
1620 struct brcmf_info *drvr_priv = drvr->info;
1621 DECLARE_WAITQUEUE(wait, current);
1622 int timeout = brcmf_ioctl_timeout_msec;
1623
1624 /* Convert timeout in millsecond to jiffies */
1625 timeout = timeout * HZ / 1000;
1626
1627 /* Wait until control frame is available */
1628 add_wait_queue(&drvr_priv->ioctl_resp_wait, &wait);
1629 set_current_state(TASK_INTERRUPTIBLE);
1630
1631 while (!(*condition) && (!signal_pending(current) && timeout))
1632 timeout = schedule_timeout(timeout);
1633
1634 if (signal_pending(current))
1635 *pending = true;
1636
1637 set_current_state(TASK_RUNNING);
1638 remove_wait_queue(&drvr_priv->ioctl_resp_wait, &wait);
1639
1640 return timeout;
1641 }
1642
1643 int brcmf_os_ioctl_resp_wake(struct brcmf_pub *drvr)
1644 {
1645 struct brcmf_info *drvr_priv = drvr->info;
1646
1647 if (waitqueue_active(&drvr_priv->ioctl_resp_wait))
1648 wake_up_interruptible(&drvr_priv->ioctl_resp_wait);
1649
1650 return 0;
1651 }
1652
1653 static int brcmf_host_event(struct brcmf_info *drvr_priv, int *ifidx, void *pktdata,
1654 struct brcmf_event_msg *event, void **data)
1655 {
1656 int bcmerror = 0;
1657
1658 bcmerror = brcmf_c_host_event(drvr_priv, ifidx, pktdata, event, data);
1659 if (bcmerror != 0)
1660 return bcmerror;
1661
1662 if (drvr_priv->iflist[*ifidx]->net)
1663 brcmf_cfg80211_event(drvr_priv->iflist[*ifidx]->net,
1664 event, *data);
1665
1666 return bcmerror;
1667 }
1668
1669 int brcmf_netdev_reset(struct net_device *dev, u8 flag)
1670 {
1671 struct brcmf_info *drvr_priv = *(struct brcmf_info **)netdev_priv(dev);
1672
1673 brcmf_bus_devreset(&drvr_priv->pub, flag);
1674
1675 return 1;
1676 }
1677
1678 static int brcmf_get_pend_8021x_cnt(struct brcmf_info *drvr_priv)
1679 {
1680 return atomic_read(&drvr_priv->pend_8021x_cnt);
1681 }
1682
1683 #define MAX_WAIT_FOR_8021X_TX 10
1684
1685 int brcmf_netdev_wait_pend8021x(struct net_device *dev)
1686 {
1687 struct brcmf_info *drvr_priv = *(struct brcmf_info **)netdev_priv(dev);
1688 int timeout = 10 * HZ / 1000;
1689 int ntimes = MAX_WAIT_FOR_8021X_TX;
1690 int pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1691
1692 while (ntimes && pend) {
1693 if (pend) {
1694 set_current_state(TASK_INTERRUPTIBLE);
1695 schedule_timeout(timeout);
1696 set_current_state(TASK_RUNNING);
1697 ntimes--;
1698 }
1699 pend = brcmf_get_pend_8021x_cnt(drvr_priv);
1700 }
1701 return pend;
1702 }
1703
1704 #ifdef BCMDBG
1705 int brcmf_write_to_file(struct brcmf_pub *drvr, u8 *buf, int size)
1706 {
1707 int ret = 0;
1708 struct file *fp;
1709 mm_segment_t old_fs;
1710 loff_t pos = 0;
1711
1712 /* change to KERNEL_DS address limit */
1713 old_fs = get_fs();
1714 set_fs(KERNEL_DS);
1715
1716 /* open file to write */
1717 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
1718 if (!fp) {
1719 BRCMF_ERROR(("%s: open file error\n", __func__));
1720 ret = -1;
1721 goto exit;
1722 }
1723
1724 /* Write buf to file */
1725 fp->f_op->write(fp, buf, size, &pos);
1726
1727 exit:
1728 /* free buf before return */
1729 kfree(buf);
1730 /* close file before return */
1731 if (fp)
1732 filp_close(fp, current->files);
1733 /* restore previous address limit */
1734 set_fs(old_fs);
1735
1736 return ret;
1737 }
1738 #endif /* BCMDBG */
This page took 0.067881 seconds and 6 git commands to generate.