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