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