]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/brcm80211/brcmfmac/dhd_linux.c
staging: brcm80211: move sdio related suspend/resume code to bus interface layer
[mv-sheeva.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 #if defined(CONFIG_HAS_EARLYSUSPEND)
33 #include <linux/earlysuspend.h>
34 #endif
35 #include <defs.h>
36 #include <brcmu_utils.h>
37 #include <brcmu_wifi.h>
38
39 #include "dngl_stats.h"
40 #include "dhd.h"
41 #include "dhd_bus.h"
42 #include "dhd_proto.h"
43 #include "dhd_dbg.h"
44 #include "wl_cfg80211.h"
45
46 /* Global ASSERT type flag */
47 u32 g_assert_type;
48
49 #if defined(CONFIG_PM_SLEEP)
50 #include <linux/suspend.h>
51 atomic_t brcmf_mmc_suspend;
52 DECLARE_WAIT_QUEUE_HEAD(dhd_dpc_wait);
53 #endif  /*  defined(CONFIG_PM_SLEEP) */
54
55 MODULE_AUTHOR("Broadcom Corporation");
56 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
57 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
58 MODULE_LICENSE("Dual BSD/GPL");
59
60
61 /* Interface control information */
62 typedef struct dhd_if {
63         struct dhd_info *info;  /* back pointer to dhd_info */
64         /* OS/stack specifics */
65         struct net_device *net;
66         struct net_device_stats stats;
67         int idx;                /* iface idx in dongle */
68         int state;              /* interface state */
69         uint subunit;           /* subunit */
70         u8 mac_addr[ETH_ALEN];  /* assigned MAC address */
71         bool attached;          /* Delayed attachment when unset */
72         bool txflowcontrol;     /* Per interface flow control indicator */
73         char name[IFNAMSIZ];    /* linux interface name */
74 } dhd_if_t;
75
76 /* Local private structure (extension of pub) */
77 typedef struct dhd_info {
78         dhd_pub_t pub;
79
80         /* OS/stack specifics */
81         dhd_if_t *iflist[DHD_MAX_IFS];
82
83         struct semaphore proto_sem;
84         wait_queue_head_t ioctl_resp_wait;
85         struct timer_list timer;
86         bool wd_timer_valid;
87         struct tasklet_struct tasklet;
88         spinlock_t sdlock;
89         /* Thread based operation */
90         bool threads_only;
91         struct semaphore sdsem;
92         struct task_struct *watchdog_tsk;
93         struct semaphore watchdog_sem;
94         struct task_struct *dpc_tsk;
95         struct semaphore dpc_sem;
96
97         /* Thread to issue ioctl for multicast */
98         struct task_struct *sysioc_tsk;
99         struct semaphore sysioc_sem;
100         bool set_multicast;
101         bool set_macaddress;
102         u8 macvalue[ETH_ALEN];
103         wait_queue_head_t ctrl_wait;
104         atomic_t pend_8021x_cnt;
105
106 #ifdef CONFIG_HAS_EARLYSUSPEND
107         struct early_suspend early_suspend;
108 #endif                          /* CONFIG_HAS_EARLYSUSPEND */
109 } dhd_info_t;
110
111 /* Definitions to provide path to the firmware and nvram
112  * example nvram_path[MOD_PARAM_PATHLEN]="/projects/wlan/nvram.txt"
113  */
114 char firmware_path[MOD_PARAM_PATHLEN];
115 char nvram_path[MOD_PARAM_PATHLEN];
116
117 /* load firmware and/or nvram values from the filesystem */
118 module_param_string(firmware_path, firmware_path, MOD_PARAM_PATHLEN, 0);
119 module_param_string(nvram_path, nvram_path, MOD_PARAM_PATHLEN, 0);
120
121 /* No firmware required */
122 bool brcmf_no_fw_req;
123 module_param(brcmf_no_fw_req, bool, 0);
124
125 /* Error bits */
126 module_param(brcmf_msg_level, int, 0);
127
128 /* Spawn a thread for system ioctls (set mac, set mcast) */
129 uint brcmf_sysioc = true;
130 module_param(brcmf_sysioc, uint, 0);
131
132 /* Watchdog interval */
133 uint brcmf_watchdog_ms = 10;
134 module_param(brcmf_watchdog_ms, uint, 0);
135
136 #ifdef BCMDBG
137 /* Console poll interval */
138 uint brcmf_console_ms;
139 module_param(brcmf_console_ms, uint, 0);
140 #endif                          /* BCMDBG */
141
142 /* ARP offload agent mode : Enable ARP Host Auto-Reply
143 and ARP Peer Auto-Reply */
144 uint brcmf_arp_mode = 0xb;
145 module_param(brcmf_arp_mode, uint, 0);
146
147 /* ARP offload enable */
148 uint brcmf_arp_enable = true;
149 module_param(brcmf_arp_enable, uint, 0);
150
151 /* Global Pkt filter enable control */
152 uint brcmf_pkt_filter_enable = true;
153 module_param(brcmf_pkt_filter_enable, uint, 0);
154
155 /*  Pkt filter init setup */
156 uint brcmf_pkt_filter_init;
157 module_param(brcmf_pkt_filter_init, uint, 0);
158
159 /* Pkt filter mode control */
160 uint brcmf_master_mode = true;
161 module_param(brcmf_master_mode, uint, 1);
162
163 /* Watchdog thread priority, -1 to use kernel timer */
164 int brcmf_watchdog_prio = 97;
165 module_param(brcmf_watchdog_prio, int, 0);
166
167 /* DPC thread priority, -1 to use tasklet */
168 int brcmf_dpc_prio = 98;
169 module_param(brcmf_dpc_prio, int, 0);
170
171 /* DPC thread priority, -1 to use tasklet */
172 extern int brcmf_dongle_memsize;
173 module_param(brcmf_dongle_memsize, int, 0);
174
175 /* Contorl fw roaming */
176 uint brcmf_roam = 1;
177
178 /* Control radio state */
179 uint brcmf_radio_up = 1;
180
181 /* Network inteface name */
182 char iface_name[IFNAMSIZ] = "wlan";
183 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
184
185 /* The following are specific to the SDIO dongle */
186
187 /* IOCTL response timeout */
188 int brcmf_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
189
190 /* Idle timeout for backplane clock */
191 int brcmf_idletime = BRCMF_IDLETIME_TICKS;
192 module_param(brcmf_idletime, int, 0);
193
194 /* Use polling */
195 uint brcmf_poll;
196 module_param(brcmf_poll, uint, 0);
197
198 /* Use interrupts */
199 uint brcmf_intr = true;
200 module_param(brcmf_intr, uint, 0);
201
202 /* SDIO Drive Strength (in milliamps) */
203 uint brcmf_sdiod_drive_strength = 6;
204 module_param(brcmf_sdiod_drive_strength, uint, 0);
205
206 /* Tx/Rx bounds */
207 extern uint brcmf_txbound;
208 extern uint brcmf_rxbound;
209 module_param(brcmf_txbound, uint, 0);
210 module_param(brcmf_rxbound, uint, 0);
211
212 /* Deferred transmits */
213 extern uint brcmf_deferred_tx;
214 module_param(brcmf_deferred_tx, uint, 0);
215
216 #ifdef SDTEST
217 /* Echo packet generator (pkts/s) */
218 uint brcmf_pktgen;
219 module_param(brcmf_pktgen, uint, 0);
220
221 /* Echo packet len (0 => sawtooth, max 2040) */
222 uint brcmf_pktgen_len;
223 module_param(brcmf_pktgen_len, uint, 0);
224 #endif
225
226 /* Version string to report */
227 #ifdef BCMDBG
228 #define DHD_COMPILED "\nCompiled in " SRCBASE
229 #else
230 #define DHD_COMPILED
231 #endif
232
233 static void brcmf_dpc(unsigned long data);
234 static int brcmf_toe_get(dhd_info_t *dhd, int idx, u32 *toe_ol);
235 static int brcmf_toe_set(dhd_info_t *dhd, int idx, u32 toe_ol);
236 static int brcmf_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
237                              brcmf_event_msg_t *event_ptr, void **data_ptr);
238
239 static void brcmf_set_packet_filter(int value, dhd_pub_t *dhd)
240 {
241         DHD_TRACE(("%s: %d\n", __func__, value));
242         /* 1 - Enable packet filter, only allow unicast packet to send up */
243         /* 0 - Disable packet filter */
244         if (brcmf_pkt_filter_enable) {
245                 int i;
246
247                 for (i = 0; i < dhd->pktfilter_count; i++) {
248                         brcmf_c_pktfilter_offload_set(dhd, dhd->pktfilter[i]);
249                         brcmf_c_pktfilter_offload_enable(dhd, dhd->pktfilter[i],
250                                                      value, brcmf_master_mode);
251                 }
252         }
253 }
254
255 #if defined(CONFIG_HAS_EARLYSUSPEND)
256 static int brcmf_set_suspend(int value, dhd_pub_t *dhd)
257 {
258         int power_mode = PM_MAX;
259         /* wl_pkt_filter_enable_t       enable_parm; */
260         char iovbuf[32];
261         int bcn_li_dtim = 3;
262
263         DHD_TRACE(("%s: enter, value = %d in_suspend=%d\n",
264                    __func__, value, dhd->in_suspend));
265
266         if (dhd && dhd->up) {
267                 if (value && dhd->in_suspend) {
268
269                         /* Kernel suspended */
270                         DHD_TRACE(("%s: force extra Suspend setting\n",
271                                    __func__));
272
273                         brcmf_proto_cdc_set_ioctl(dhd, 0, BRCMF_C_SET_PM,
274                                          (char *)&power_mode,
275                                          sizeof(power_mode));
276
277                         /* Enable packet filter, only allow unicast
278                                  packet to send up */
279                         brcmf_set_packet_filter(1, dhd);
280
281                         /* if dtim skip setup as default force it
282                          * to wake each third dtim
283                          * for better power saving.
284                          * Note that side effect is chance to miss BC/MC
285                          * packet
286                          */
287                         if ((dhd->dtim_skip == 0) || (dhd->dtim_skip == 1))
288                                 bcn_li_dtim = 3;
289                         else
290                                 bcn_li_dtim = dhd->dtim_skip;
291                         brcmu_mkiovar("bcn_li_dtim", (char *)&bcn_li_dtim,
292                                     4, iovbuf, sizeof(iovbuf));
293                         brcmf_proto_cdc_set_ioctl(dhd, 0, BRCMF_C_SET_VAR,
294                                                   iovbuf, sizeof(iovbuf));
295                 } else {
296
297                         /* Kernel resumed  */
298                         DHD_TRACE(("%s: Remove extra suspend setting\n",
299                                    __func__));
300
301                         power_mode = PM_FAST;
302                         brcmf_proto_cdc_set_ioctl(dhd, 0, BRCMF_C_SET_PM,
303                                          (char *)&power_mode,
304                                          sizeof(power_mode));
305
306                         /* disable pkt filter */
307                         brcmf_set_packet_filter(0, dhd);
308
309                         /* restore pre-suspend setting for dtim_skip */
310                         brcmu_mkiovar("bcn_li_dtim", (char *)&dhd->dtim_skip,
311                                     4, iovbuf, sizeof(iovbuf));
312
313                         brcmf_proto_cdc_set_ioctl(dhd, 0, BRCMF_C_SET_VAR,
314                                                   iovbuf, sizeof(iovbuf));
315                 }
316         }
317
318         return 0;
319 }
320
321 static void brcmf_suspend_resume_helper(struct dhd_info *dhd, int val)
322 {
323         dhd_pub_t *dhdp = &dhd->pub;
324
325         brcmf_os_proto_block(dhdp);
326         /* Set flag when early suspend was called */
327         dhdp->in_suspend = val;
328         if (!dhdp->suspend_disable_flag)
329                 brcmf_set_suspend(val, dhdp);
330         brcmf_os_proto_unblock(dhdp);
331 }
332
333 static void brcmf_early_suspend(struct early_suspend *h)
334 {
335         struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
336
337         DHD_TRACE(("%s: enter\n", __func__));
338
339         if (dhd)
340                 dhd_suspend_resume_helper(dhd, 1);
341
342 }
343
344 static void brcmf_late_resume(struct early_suspend *h)
345 {
346         struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
347
348         DHD_TRACE(("%s: enter\n", __func__));
349
350         if (dhd)
351                 dhd_suspend_resume_helper(dhd, 0);
352 }
353 #endif                          /* defined(CONFIG_HAS_EARLYSUSPEND) */
354
355 /*
356  * Generalized timeout mechanism.  Uses spin sleep with exponential
357  * back-off until
358  * the sleep time reaches one jiffy, then switches over to task delay.  Usage:
359  *
360  *      brcmf_timeout_start(&tmo, usec);
361  *      while (!brcmf_timeout_expired(&tmo))
362  *              if (poll_something())
363  *                      break;
364  *      if (brcmf_timeout_expired(&tmo))
365  *              fatal();
366  */
367
368 void brcmf_timeout_start(dhd_timeout_t *tmo, uint usec)
369 {
370         tmo->limit = usec;
371         tmo->increment = 0;
372         tmo->elapsed = 0;
373         tmo->tick = 1000000 / HZ;
374 }
375
376 int brcmf_timeout_expired(dhd_timeout_t *tmo)
377 {
378         /* Does nothing the first call */
379         if (tmo->increment == 0) {
380                 tmo->increment = 1;
381                 return 0;
382         }
383
384         if (tmo->elapsed >= tmo->limit)
385                 return 1;
386
387         /* Add the delay that's about to take place */
388         tmo->elapsed += tmo->increment;
389
390         if (tmo->increment < tmo->tick) {
391                 udelay(tmo->increment);
392                 tmo->increment *= 2;
393                 if (tmo->increment > tmo->tick)
394                         tmo->increment = tmo->tick;
395         } else {
396                 wait_queue_head_t delay_wait;
397                 DECLARE_WAITQUEUE(wait, current);
398                 int pending;
399                 init_waitqueue_head(&delay_wait);
400                 add_wait_queue(&delay_wait, &wait);
401                 set_current_state(TASK_INTERRUPTIBLE);
402                 schedule_timeout(1);
403                 pending = signal_pending(current);
404                 remove_wait_queue(&delay_wait, &wait);
405                 set_current_state(TASK_RUNNING);
406                 if (pending)
407                         return 1;       /* Interrupted */
408         }
409
410         return 0;
411 }
412
413 static int brcmf_net2idx(dhd_info_t *dhd, struct net_device *net)
414 {
415         int i = 0;
416
417         ASSERT(dhd);
418         while (i < DHD_MAX_IFS) {
419                 if (dhd->iflist[i] && (dhd->iflist[i]->net == net))
420                         return i;
421                 i++;
422         }
423
424         return DHD_BAD_IF;
425 }
426
427 int brcmf_ifname2idx(dhd_info_t *dhd, char *name)
428 {
429         int i = DHD_MAX_IFS;
430
431         ASSERT(dhd);
432
433         if (name == NULL || *name == '\0')
434                 return 0;
435
436         while (--i > 0)
437                 if (dhd->iflist[i]
438                     && !strncmp(dhd->iflist[i]->name, name, IFNAMSIZ))
439                         break;
440
441         DHD_TRACE(("%s: return idx %d for \"%s\"\n", __func__, i, name));
442
443         return i;               /* default - the primary interface */
444 }
445
446 char *brcmf_ifname(dhd_pub_t *dhdp, int ifidx)
447 {
448         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
449
450         ASSERT(dhd);
451
452         if (ifidx < 0 || ifidx >= DHD_MAX_IFS) {
453                 DHD_ERROR(("%s: ifidx %d out of range\n", __func__, ifidx));
454                 return "<if_bad>";
455         }
456
457         if (dhd->iflist[ifidx] == NULL) {
458                 DHD_ERROR(("%s: null i/f %d\n", __func__, ifidx));
459                 return "<if_null>";
460         }
461
462         if (dhd->iflist[ifidx]->net)
463                 return dhd->iflist[ifidx]->net->name;
464
465         return "<if_none>";
466 }
467
468 static void _brcmf_set_multicast_list(dhd_info_t *dhd, int ifidx)
469 {
470         struct net_device *dev;
471         struct netdev_hw_addr *ha;
472         u32 allmulti, cnt;
473
474         wl_ioctl_t ioc;
475         char *buf, *bufp;
476         uint buflen;
477         int ret;
478
479         ASSERT(dhd && dhd->iflist[ifidx]);
480         dev = dhd->iflist[ifidx]->net;
481         cnt = netdev_mc_count(dev);
482
483         /* Determine initial value of allmulti flag */
484         allmulti = (dev->flags & IFF_ALLMULTI) ? true : false;
485
486         /* Send down the multicast list first. */
487
488         buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
489         bufp = buf = kmalloc(buflen, GFP_ATOMIC);
490         if (!bufp) {
491                 DHD_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
492                            brcmf_ifname(&dhd->pub, ifidx), cnt));
493                 return;
494         }
495
496         strcpy(bufp, "mcast_list");
497         bufp += strlen("mcast_list") + 1;
498
499         cnt = cpu_to_le32(cnt);
500         memcpy(bufp, &cnt, sizeof(cnt));
501         bufp += sizeof(cnt);
502
503         netdev_for_each_mc_addr(ha, dev) {
504                 if (!cnt)
505                         break;
506                 memcpy(bufp, ha->addr, ETH_ALEN);
507                 bufp += ETH_ALEN;
508                 cnt--;
509         }
510
511         memset(&ioc, 0, sizeof(ioc));
512         ioc.cmd = BRCMF_C_SET_VAR;
513         ioc.buf = buf;
514         ioc.len = buflen;
515         ioc.set = true;
516
517         ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
518         if (ret < 0) {
519                 DHD_ERROR(("%s: set mcast_list failed, cnt %d\n",
520                            brcmf_ifname(&dhd->pub, ifidx), cnt));
521                 allmulti = cnt ? true : allmulti;
522         }
523
524         kfree(buf);
525
526         /* Now send the allmulti setting.  This is based on the setting in the
527          * net_device flags, but might be modified above to be turned on if we
528          * were trying to set some addresses and dongle rejected it...
529          */
530
531         buflen = sizeof("allmulti") + sizeof(allmulti);
532         buf = kmalloc(buflen, GFP_ATOMIC);
533         if (!buf) {
534                 DHD_ERROR(("%s: out of memory for allmulti\n",
535                            brcmf_ifname(&dhd->pub, ifidx)));
536                 return;
537         }
538         allmulti = cpu_to_le32(allmulti);
539
540         if (!brcmu_mkiovar
541             ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
542                 DHD_ERROR(("%s: mkiovar failed for allmulti, datalen %d "
543                         "buflen %u\n", brcmf_ifname(&dhd->pub, ifidx),
544                         (int)sizeof(allmulti), buflen));
545                 kfree(buf);
546                 return;
547         }
548
549         memset(&ioc, 0, sizeof(ioc));
550         ioc.cmd = BRCMF_C_SET_VAR;
551         ioc.buf = buf;
552         ioc.len = buflen;
553         ioc.set = true;
554
555         ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
556         if (ret < 0) {
557                 DHD_ERROR(("%s: set allmulti %d failed\n",
558                            brcmf_ifname(&dhd->pub, ifidx),
559                            le32_to_cpu(allmulti)));
560         }
561
562         kfree(buf);
563
564         /* Finally, pick up the PROMISC flag as well, like the NIC
565                  driver does */
566
567         allmulti = (dev->flags & IFF_PROMISC) ? true : false;
568         allmulti = cpu_to_le32(allmulti);
569
570         memset(&ioc, 0, sizeof(ioc));
571         ioc.cmd = BRCMF_C_SET_PROMISC;
572         ioc.buf = &allmulti;
573         ioc.len = sizeof(allmulti);
574         ioc.set = true;
575
576         ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
577         if (ret < 0) {
578                 DHD_ERROR(("%s: set promisc %d failed\n",
579                            brcmf_ifname(&dhd->pub, ifidx),
580                            le32_to_cpu(allmulti)));
581         }
582 }
583
584 static int _brcmf_set_mac_address(dhd_info_t *dhd, int ifidx, u8 *addr)
585 {
586         char buf[32];
587         wl_ioctl_t ioc;
588         int ret;
589
590         DHD_TRACE(("%s enter\n", __func__));
591         if (!brcmu_mkiovar
592             ("cur_etheraddr", (char *)addr, ETH_ALEN, buf, 32)) {
593                 DHD_ERROR(("%s: mkiovar failed for cur_etheraddr\n",
594                            brcmf_ifname(&dhd->pub, ifidx)));
595                 return -1;
596         }
597         memset(&ioc, 0, sizeof(ioc));
598         ioc.cmd = BRCMF_C_SET_VAR;
599         ioc.buf = buf;
600         ioc.len = 32;
601         ioc.set = true;
602
603         ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
604         if (ret < 0) {
605                 DHD_ERROR(("%s: set cur_etheraddr failed\n",
606                            brcmf_ifname(&dhd->pub, ifidx)));
607         } else {
608                 memcpy(dhd->iflist[ifidx]->net->dev_addr, addr, ETH_ALEN);
609         }
610
611         return ret;
612 }
613
614 #ifdef SOFTAP
615 extern struct net_device *ap_net_dev;
616 #endif
617
618 static void brcmf_op_if(dhd_if_t *ifp)
619 {
620         dhd_info_t *dhd;
621         int ret = 0, err = 0;
622
623         ASSERT(ifp && ifp->info && ifp->idx);   /* Virtual interfaces only */
624
625         dhd = ifp->info;
626
627         DHD_TRACE(("%s: idx %d, state %d\n", __func__, ifp->idx, ifp->state));
628
629         switch (ifp->state) {
630         case BRCMF_E_IF_ADD:
631                 /*
632                  * Delete the existing interface before overwriting it
633                  * in case we missed the BRCMF_E_IF_DEL event.
634                  */
635                 if (ifp->net != NULL) {
636                         DHD_ERROR(("%s: ERROR: netdev:%s already exists, "
637                         "try free & unregister\n",
638                         __func__, ifp->net->name));
639                         netif_stop_queue(ifp->net);
640                         unregister_netdev(ifp->net);
641                         free_netdev(ifp->net);
642                 }
643                 /* Allocate etherdev, including space for private structure */
644                 ifp->net = alloc_etherdev(sizeof(dhd));
645                 if (!ifp->net) {
646                         DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
647                         ret = -ENOMEM;
648                 }
649                 if (ret == 0) {
650                         strcpy(ifp->net->name, ifp->name);
651                         memcpy(netdev_priv(ifp->net), &dhd, sizeof(dhd));
652                         err = brcmf_net_attach(&dhd->pub, ifp->idx);
653                         if (err != 0) {
654                                 DHD_ERROR(("%s: dhd_net_attach failed, "
655                                         "err %d\n",
656                                         __func__, err));
657                                 ret = -EOPNOTSUPP;
658                         } else {
659 #ifdef SOFTAP
660                                 /* semaphore that the soft AP CODE
661                                          waits on */
662                                 extern struct semaphore ap_eth_sema;
663
664                                 /* save ptr to wl0.1 netdev for use
665                                          in wl_iw.c  */
666                                 ap_net_dev = ifp->net;
667                                 /* signal to the SOFTAP 'sleeper' thread,
668                                          wl0.1 is ready */
669                                 up(&ap_eth_sema);
670 #endif
671                                 DHD_TRACE(("\n ==== pid:%x, net_device for "
672                                         "if:%s created ===\n\n",
673                                         current->pid, ifp->net->name));
674                                 ifp->state = 0;
675                         }
676                 }
677                 break;
678         case BRCMF_E_IF_DEL:
679                 if (ifp->net != NULL) {
680                         DHD_TRACE(("\n%s: got 'WLC_E_IF_DEL' state\n",
681                                    __func__));
682                         netif_stop_queue(ifp->net);
683                         unregister_netdev(ifp->net);
684                         ret = DHD_DEL_IF;       /* Make sure the free_netdev()
685                                                          is called */
686                 }
687                 break;
688         default:
689                 DHD_ERROR(("%s: bad op %d\n", __func__, ifp->state));
690                 ASSERT(!ifp->state);
691                 break;
692         }
693
694         if (ret < 0) {
695                 if (ifp->net)
696                         free_netdev(ifp->net);
697
698                 dhd->iflist[ifp->idx] = NULL;
699                 kfree(ifp);
700 #ifdef SOFTAP
701                 if (ifp->net == ap_net_dev)
702                         ap_net_dev = NULL;      /*  NULL  SOFTAP global
703                                                          wl0.1 as well */
704 #endif                          /*  SOFTAP */
705         }
706 }
707
708 static int _brcmf_sysioc_thread(void *data)
709 {
710         dhd_info_t *dhd = (dhd_info_t *) data;
711         int i;
712 #ifdef SOFTAP
713         bool in_ap = false;
714 #endif
715
716         allow_signal(SIGTERM);
717
718         while (down_interruptible(&dhd->sysioc_sem) == 0) {
719                 if (kthread_should_stop())
720                         break;
721                 for (i = 0; i < DHD_MAX_IFS; i++) {
722                         if (dhd->iflist[i]) {
723 #ifdef SOFTAP
724                                 in_ap = (ap_net_dev != NULL);
725 #endif                          /* SOFTAP */
726                                 if (dhd->iflist[i]->state)
727                                         brcmf_op_if(dhd->iflist[i]);
728 #ifdef SOFTAP
729                                 if (dhd->iflist[i] == NULL) {
730                                         DHD_TRACE(("\n\n %s: interface %d "
731                                                 "removed!\n", __func__, i));
732                                         continue;
733                                 }
734
735                                 if (in_ap && dhd->set_macaddress) {
736                                         DHD_TRACE(("attempt to set MAC for %s "
737                                                 "in AP Mode," "blocked.\n",
738                                                 dhd->iflist[i]->net->name));
739                                         dhd->set_macaddress = false;
740                                         continue;
741                                 }
742
743                                 if (in_ap && dhd->set_multicast) {
744                                         DHD_TRACE(("attempt to set MULTICAST list for %s" "in AP Mode, blocked.\n",
745                                                 dhd->iflist[i]->net->name));
746                                         dhd->set_multicast = false;
747                                         continue;
748                                 }
749 #endif                          /* SOFTAP */
750                                 if (dhd->set_multicast) {
751                                         dhd->set_multicast = false;
752                                         _brcmf_set_multicast_list(dhd, i);
753                                 }
754                                 if (dhd->set_macaddress) {
755                                         dhd->set_macaddress = false;
756                                         _brcmf_set_mac_address(dhd, i,
757                                                              dhd->macvalue);
758                                 }
759                         }
760                 }
761         }
762         return 0;
763 }
764
765 static int brcmf_netdev_set_mac_address(struct net_device *dev, void *addr)
766 {
767         int ret = 0;
768
769         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
770         struct sockaddr *sa = (struct sockaddr *)addr;
771         int ifidx;
772
773         ifidx = brcmf_net2idx(dhd, dev);
774         if (ifidx == DHD_BAD_IF)
775                 return -1;
776
777         ASSERT(dhd->sysioc_tsk);
778         memcpy(&dhd->macvalue, sa->sa_data, ETH_ALEN);
779         dhd->set_macaddress = true;
780         up(&dhd->sysioc_sem);
781
782         return ret;
783 }
784
785 static void brcmf_netdev_set_multicast_list(struct net_device *dev)
786 {
787         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
788         int ifidx;
789
790         ifidx = brcmf_net2idx(dhd, dev);
791         if (ifidx == DHD_BAD_IF)
792                 return;
793
794         ASSERT(dhd->sysioc_tsk);
795         dhd->set_multicast = true;
796         up(&dhd->sysioc_sem);
797 }
798
799 int brcmf_sendpkt(dhd_pub_t *dhdp, int ifidx, struct sk_buff *pktbuf)
800 {
801         dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
802
803         /* Reject if down */
804         if (!dhdp->up || (dhdp->busstate == DHD_BUS_DOWN))
805                 return -ENODEV;
806
807         /* Update multicast statistic */
808         if (pktbuf->len >= ETH_ALEN) {
809                 u8 *pktdata = (u8 *) (pktbuf->data);
810                 struct ethhdr *eh = (struct ethhdr *)pktdata;
811
812                 if (is_multicast_ether_addr(eh->h_dest))
813                         dhdp->tx_multicast++;
814                 if (ntohs(eh->h_proto) == ETH_P_PAE)
815                         atomic_inc(&dhd->pend_8021x_cnt);
816         }
817
818         /* If the protocol uses a data header, apply it */
819         brcmf_proto_hdrpush(dhdp, ifidx, pktbuf);
820
821         /* Use bus module to send data frame */
822         return brcmf_sdbrcm_bus_txdata(dhdp->bus, pktbuf);
823 }
824
825 static int brcmf_netdev_start_xmit(struct sk_buff *skb, struct net_device *net)
826 {
827         int ret;
828         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
829         int ifidx;
830
831         DHD_TRACE(("%s: Enter\n", __func__));
832
833         /* Reject if down */
834         if (!dhd->pub.up || (dhd->pub.busstate == DHD_BUS_DOWN)) {
835                 DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d\n",
836                            __func__, dhd->pub.up, dhd->pub.busstate));
837                 netif_stop_queue(net);
838                 return -ENODEV;
839         }
840
841         ifidx = brcmf_net2idx(dhd, net);
842         if (ifidx == DHD_BAD_IF) {
843                 DHD_ERROR(("%s: bad ifidx %d\n", __func__, ifidx));
844                 netif_stop_queue(net);
845                 return -ENODEV;
846         }
847
848         /* Make sure there's enough room for any header */
849         if (skb_headroom(skb) < dhd->pub.hdrlen) {
850                 struct sk_buff *skb2;
851
852                 DHD_INFO(("%s: insufficient headroom\n",
853                           brcmf_ifname(&dhd->pub, ifidx)));
854                 dhd->pub.tx_realloc++;
855                 skb2 = skb_realloc_headroom(skb, dhd->pub.hdrlen);
856                 dev_kfree_skb(skb);
857                 skb = skb2;
858                 if (skb == NULL) {
859                         DHD_ERROR(("%s: skb_realloc_headroom failed\n",
860                                    brcmf_ifname(&dhd->pub, ifidx)));
861                         ret = -ENOMEM;
862                         goto done;
863                 }
864         }
865
866         ret = brcmf_sendpkt(&dhd->pub, ifidx, skb);
867
868 done:
869         if (ret)
870                 dhd->pub.dstats.tx_dropped++;
871         else
872                 dhd->pub.tx_packets++;
873
874         /* Return ok: we always eat the packet */
875         return 0;
876 }
877
878 void brcmf_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool state)
879 {
880         struct net_device *net;
881         dhd_info_t *dhd = dhdp->info;
882
883         DHD_TRACE(("%s: Enter\n", __func__));
884
885         dhdp->txoff = state;
886         ASSERT(dhd && dhd->iflist[ifidx]);
887         net = dhd->iflist[ifidx]->net;
888         if (state == ON)
889                 netif_stop_queue(net);
890         else
891                 netif_wake_queue(net);
892 }
893
894 void brcmf_rx_frame(dhd_pub_t *dhdp, int ifidx, struct sk_buff *skb,
895                   int numpkt)
896 {
897         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
898         unsigned char *eth;
899         uint len;
900         void *data;
901         struct sk_buff *pnext, *save_pktbuf;
902         int i;
903         dhd_if_t *ifp;
904         brcmf_event_msg_t event;
905
906         DHD_TRACE(("%s: Enter\n", __func__));
907
908         save_pktbuf = skb;
909
910         for (i = 0; skb && i < numpkt; i++, skb = pnext) {
911
912                 pnext = skb->next;
913                 skb->next = NULL;
914
915                 /* Get the protocol, maintain skb around eth_type_trans()
916                  * The main reason for this hack is for the limitation of
917                  * Linux 2.4 where 'eth_type_trans' uses the
918                  * 'net->hard_header_len'
919                  * to perform skb_pull inside vs ETH_HLEN. Since to avoid
920                  * coping of the packet coming from the network stack to add
921                  * BDC, Hardware header etc, during network interface
922                  * registration
923                  * we set the 'net->hard_header_len' to ETH_HLEN + extra space
924                  * required
925                  * for BDC, Hardware header etc. and not just the ETH_HLEN
926                  */
927                 eth = skb->data;
928                 len = skb->len;
929
930                 ifp = dhd->iflist[ifidx];
931                 if (ifp == NULL)
932                         ifp = dhd->iflist[0];
933
934                 ASSERT(ifp);
935                 skb->dev = ifp->net;
936                 skb->protocol = eth_type_trans(skb, skb->dev);
937
938                 if (skb->pkt_type == PACKET_MULTICAST)
939                         dhd->pub.rx_multicast++;
940
941                 skb->data = eth;
942                 skb->len = len;
943
944                 /* Strip header, count, deliver upward */
945                 skb_pull(skb, ETH_HLEN);
946
947                 /* Process special event packets and then discard them */
948                 if (ntohs(skb->protocol) == ETH_P_LINK_CTL)
949                         brcmf_host_event(dhd, &ifidx,
950                                           skb_mac_header(skb),
951                                           &event, &data);
952
953                 ASSERT(ifidx < DHD_MAX_IFS && dhd->iflist[ifidx]);
954                 if (dhd->iflist[ifidx] && !dhd->iflist[ifidx]->state)
955                         ifp = dhd->iflist[ifidx];
956
957                 if (ifp->net)
958                         ifp->net->last_rx = jiffies;
959
960                 dhdp->dstats.rx_bytes += skb->len;
961                 dhdp->rx_packets++;     /* Local count */
962
963                 if (in_interrupt()) {
964                         netif_rx(skb);
965                 } else {
966                         /* If the receive is not processed inside an ISR,
967                          * the softirqd must be woken explicitly to service
968                          * the NET_RX_SOFTIRQ.  In 2.6 kernels, this is handled
969                          * by netif_rx_ni(), but in earlier kernels, we need
970                          * to do it manually.
971                          */
972                         netif_rx_ni(skb);
973                 }
974         }
975 }
976
977 void brcmf_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx)
978 {
979         /* Linux version has nothing to do */
980         return;
981 }
982
983 void brcmf_txcomplete(dhd_pub_t *dhdp, struct sk_buff *txp, bool success)
984 {
985         uint ifidx;
986         dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
987         struct ethhdr *eh;
988         u16 type;
989
990         brcmf_proto_hdrpull(dhdp, &ifidx, txp);
991
992         eh = (struct ethhdr *)(txp->data);
993         type = ntohs(eh->h_proto);
994
995         if (type == ETH_P_PAE)
996                 atomic_dec(&dhd->pend_8021x_cnt);
997
998 }
999
1000 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *net)
1001 {
1002         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1003         dhd_if_t *ifp;
1004         int ifidx;
1005
1006         DHD_TRACE(("%s: Enter\n", __func__));
1007
1008         ifidx = brcmf_net2idx(dhd, net);
1009         if (ifidx == DHD_BAD_IF)
1010                 return NULL;
1011
1012         ifp = dhd->iflist[ifidx];
1013         ASSERT(dhd && ifp);
1014
1015         if (dhd->pub.up) {
1016                 /* Use the protocol to get dongle stats */
1017                 brcmf_proto_dstats(&dhd->pub);
1018         }
1019
1020         /* Copy dongle stats to net device stats */
1021         ifp->stats.rx_packets = dhd->pub.dstats.rx_packets;
1022         ifp->stats.tx_packets = dhd->pub.dstats.tx_packets;
1023         ifp->stats.rx_bytes = dhd->pub.dstats.rx_bytes;
1024         ifp->stats.tx_bytes = dhd->pub.dstats.tx_bytes;
1025         ifp->stats.rx_errors = dhd->pub.dstats.rx_errors;
1026         ifp->stats.tx_errors = dhd->pub.dstats.tx_errors;
1027         ifp->stats.rx_dropped = dhd->pub.dstats.rx_dropped;
1028         ifp->stats.tx_dropped = dhd->pub.dstats.tx_dropped;
1029         ifp->stats.multicast = dhd->pub.dstats.multicast;
1030
1031         return &ifp->stats;
1032 }
1033
1034 static int brcmf_watchdog_thread(void *data)
1035 {
1036         dhd_info_t *dhd = (dhd_info_t *) data;
1037
1038         /* This thread doesn't need any user-level access,
1039          * so get rid of all our resources
1040          */
1041         if (brcmf_watchdog_prio > 0) {
1042                 struct sched_param param;
1043                 param.sched_priority = (brcmf_watchdog_prio < MAX_RT_PRIO) ?
1044                     brcmf_watchdog_prio : (MAX_RT_PRIO - 1);
1045                 sched_setscheduler(current, SCHED_FIFO, &param);
1046         }
1047
1048         allow_signal(SIGTERM);
1049         /* Run until signal received */
1050         while (1) {
1051                 if (kthread_should_stop())
1052                         break;
1053                 if (down_interruptible(&dhd->watchdog_sem) == 0) {
1054                         if (dhd->pub.dongle_reset == false) {
1055                                 /* Call the bus module watchdog */
1056                                 brcmf_sdbrcm_bus_watchdog(&dhd->pub);
1057                         }
1058                         /* Count the tick for reference */
1059                         dhd->pub.tickcnt++;
1060                 } else
1061                         break;
1062         }
1063         return 0;
1064 }
1065
1066 static void brcmf_watchdog(unsigned long data)
1067 {
1068         dhd_info_t *dhd = (dhd_info_t *) data;
1069
1070         if (dhd->watchdog_tsk) {
1071                 up(&dhd->watchdog_sem);
1072
1073                 /* Reschedule the watchdog */
1074                 if (dhd->wd_timer_valid) {
1075                         mod_timer(&dhd->timer,
1076                                   jiffies + brcmf_watchdog_ms * HZ / 1000);
1077                 }
1078                 return;
1079         }
1080
1081         /* Call the bus module watchdog */
1082         brcmf_sdbrcm_bus_watchdog(&dhd->pub);
1083
1084         /* Count the tick for reference */
1085         dhd->pub.tickcnt++;
1086
1087         /* Reschedule the watchdog */
1088         if (dhd->wd_timer_valid)
1089                 mod_timer(&dhd->timer, jiffies + brcmf_watchdog_ms * HZ / 1000);
1090 }
1091
1092 static int brcmf_dpc_thread(void *data)
1093 {
1094         dhd_info_t *dhd = (dhd_info_t *) data;
1095
1096         /* This thread doesn't need any user-level access,
1097          * so get rid of all our resources
1098          */
1099         if (brcmf_dpc_prio > 0) {
1100                 struct sched_param param;
1101                 param.sched_priority =
1102                     (brcmf_dpc_prio <
1103                      MAX_RT_PRIO) ? brcmf_dpc_prio : (MAX_RT_PRIO - 1);
1104                 sched_setscheduler(current, SCHED_FIFO, &param);
1105         }
1106
1107         allow_signal(SIGTERM);
1108         /* Run until signal received */
1109         while (1) {
1110                 if (kthread_should_stop())
1111                         break;
1112                 if (down_interruptible(&dhd->dpc_sem) == 0) {
1113                         /* Call bus dpc unless it indicated down
1114                                  (then clean stop) */
1115                         if (dhd->pub.busstate != DHD_BUS_DOWN) {
1116                                 if (dhd_bus_dpc(dhd->pub.bus)) {
1117                                         up(&dhd->dpc_sem);
1118                                 }
1119                         } else {
1120                                 brcmf_sdbrcm_bus_stop(dhd->pub.bus, true);
1121                         }
1122                 } else
1123                         break;
1124         }
1125         return 0;
1126 }
1127
1128 static void brcmf_dpc(unsigned long data)
1129 {
1130         dhd_info_t *dhd;
1131
1132         dhd = (dhd_info_t *) data;
1133
1134         /* Call bus dpc unless it indicated down (then clean stop) */
1135         if (dhd->pub.busstate != DHD_BUS_DOWN) {
1136                 if (dhd_bus_dpc(dhd->pub.bus))
1137                         tasklet_schedule(&dhd->tasklet);
1138         } else {
1139                 brcmf_sdbrcm_bus_stop(dhd->pub.bus, true);
1140         }
1141 }
1142
1143 void brcmf_sched_dpc(dhd_pub_t *dhdp)
1144 {
1145         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1146
1147         if (dhd->dpc_tsk) {
1148                 up(&dhd->dpc_sem);
1149                 return;
1150         }
1151
1152         tasklet_schedule(&dhd->tasklet);
1153 }
1154
1155 /* Retrieve current toe component enables, which are kept
1156          as a bitmap in toe_ol iovar */
1157 static int brcmf_toe_get(dhd_info_t *dhd, int ifidx, u32 *toe_ol)
1158 {
1159         wl_ioctl_t ioc;
1160         char buf[32];
1161         int ret;
1162
1163         memset(&ioc, 0, sizeof(ioc));
1164
1165         ioc.cmd = BRCMF_C_GET_VAR;
1166         ioc.buf = buf;
1167         ioc.len = (uint) sizeof(buf);
1168         ioc.set = false;
1169
1170         strcpy(buf, "toe_ol");
1171         ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1172         if (ret < 0) {
1173                 /* Check for older dongle image that doesn't support toe_ol */
1174                 if (ret == -EIO) {
1175                         DHD_ERROR(("%s: toe not supported by device\n",
1176                                    brcmf_ifname(&dhd->pub, ifidx)));
1177                         return -EOPNOTSUPP;
1178                 }
1179
1180                 DHD_INFO(("%s: could not get toe_ol: ret=%d\n",
1181                           brcmf_ifname(&dhd->pub, ifidx), ret));
1182                 return ret;
1183         }
1184
1185         memcpy(toe_ol, buf, sizeof(u32));
1186         return 0;
1187 }
1188
1189 /* Set current toe component enables in toe_ol iovar,
1190          and set toe global enable iovar */
1191 static int brcmf_toe_set(dhd_info_t *dhd, int ifidx, u32 toe_ol)
1192 {
1193         wl_ioctl_t ioc;
1194         char buf[32];
1195         int toe, ret;
1196
1197         memset(&ioc, 0, sizeof(ioc));
1198
1199         ioc.cmd = BRCMF_C_SET_VAR;
1200         ioc.buf = buf;
1201         ioc.len = (uint) sizeof(buf);
1202         ioc.set = true;
1203
1204         /* Set toe_ol as requested */
1205
1206         strcpy(buf, "toe_ol");
1207         memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
1208
1209         ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1210         if (ret < 0) {
1211                 DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
1212                            brcmf_ifname(&dhd->pub, ifidx), ret));
1213                 return ret;
1214         }
1215
1216         /* Enable toe globally only if any components are enabled. */
1217
1218         toe = (toe_ol != 0);
1219
1220         strcpy(buf, "toe");
1221         memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
1222
1223         ret = brcmf_proto_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1224         if (ret < 0) {
1225                 DHD_ERROR(("%s: could not set toe: ret=%d\n",
1226                            brcmf_ifname(&dhd->pub, ifidx), ret));
1227                 return ret;
1228         }
1229
1230         return 0;
1231 }
1232
1233 static void brcmf_ethtool_get_drvinfo(struct net_device *net,
1234                                     struct ethtool_drvinfo *info)
1235 {
1236         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1237
1238         sprintf(info->driver, KBUILD_MODNAME);
1239         sprintf(info->version, "%lu", dhd->pub.drv_version);
1240         sprintf(info->fw_version, "%s", wl_cfg80211_get_fwname());
1241         sprintf(info->bus_info, "%s", dev_name(&wl_cfg80211_get_sdio_func()->dev));
1242 }
1243
1244 struct ethtool_ops brcmf_ethtool_ops = {
1245         .get_drvinfo = brcmf_ethtool_get_drvinfo
1246 };
1247
1248 static int brcmf_ethtool(dhd_info_t *dhd, void *uaddr)
1249 {
1250         struct ethtool_drvinfo info;
1251         char drvname[sizeof(info.driver)];
1252         u32 cmd;
1253         struct ethtool_value edata;
1254         u32 toe_cmpnt, csum_dir;
1255         int ret;
1256
1257         DHD_TRACE(("%s: Enter\n", __func__));
1258
1259         /* all ethtool calls start with a cmd word */
1260         if (copy_from_user(&cmd, uaddr, sizeof(u32)))
1261                 return -EFAULT;
1262
1263         switch (cmd) {
1264         case ETHTOOL_GDRVINFO:
1265                 /* Copy out any request driver name */
1266                 if (copy_from_user(&info, uaddr, sizeof(info)))
1267                         return -EFAULT;
1268                 strncpy(drvname, info.driver, sizeof(info.driver));
1269                 drvname[sizeof(info.driver) - 1] = '\0';
1270
1271                 /* clear struct for return */
1272                 memset(&info, 0, sizeof(info));
1273                 info.cmd = cmd;
1274
1275                 /* if dhd requested, identify ourselves */
1276                 if (strcmp(drvname, "?dhd") == 0) {
1277                         sprintf(info.driver, "dhd");
1278                         strcpy(info.version, BRCMF_VERSION_STR);
1279                 }
1280
1281                 /* otherwise, require dongle to be up */
1282                 else if (!dhd->pub.up) {
1283                         DHD_ERROR(("%s: dongle is not up\n", __func__));
1284                         return -ENODEV;
1285                 }
1286
1287                 /* finally, report dongle driver type */
1288                 else if (dhd->pub.iswl)
1289                         sprintf(info.driver, "wl");
1290                 else
1291                         sprintf(info.driver, "xx");
1292
1293                 sprintf(info.version, "%lu", dhd->pub.drv_version);
1294                 if (copy_to_user(uaddr, &info, sizeof(info)))
1295                         return -EFAULT;
1296                 DHD_CTL(("%s: given %*s, returning %s\n", __func__,
1297                          (int)sizeof(drvname), drvname, info.driver));
1298                 break;
1299
1300                 /* Get toe offload components from dongle */
1301         case ETHTOOL_GRXCSUM:
1302         case ETHTOOL_GTXCSUM:
1303                 ret = brcmf_toe_get(dhd, 0, &toe_cmpnt);
1304                 if (ret < 0)
1305                         return ret;
1306
1307                 csum_dir =
1308                     (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1309
1310                 edata.cmd = cmd;
1311                 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
1312
1313                 if (copy_to_user(uaddr, &edata, sizeof(edata)))
1314                         return -EFAULT;
1315                 break;
1316
1317                 /* Set toe offload components in dongle */
1318         case ETHTOOL_SRXCSUM:
1319         case ETHTOOL_STXCSUM:
1320                 if (copy_from_user(&edata, uaddr, sizeof(edata)))
1321                         return -EFAULT;
1322
1323                 /* Read the current settings, update and write back */
1324                 ret = brcmf_toe_get(dhd, 0, &toe_cmpnt);
1325                 if (ret < 0)
1326                         return ret;
1327
1328                 csum_dir =
1329                     (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1330
1331                 if (edata.data != 0)
1332                         toe_cmpnt |= csum_dir;
1333                 else
1334                         toe_cmpnt &= ~csum_dir;
1335
1336                 ret = brcmf_toe_set(dhd, 0, toe_cmpnt);
1337                 if (ret < 0)
1338                         return ret;
1339
1340                 /* If setting TX checksum mode, tell Linux the new mode */
1341                 if (cmd == ETHTOOL_STXCSUM) {
1342                         if (edata.data)
1343                                 dhd->iflist[0]->net->features |=
1344                                     NETIF_F_IP_CSUM;
1345                         else
1346                                 dhd->iflist[0]->net->features &=
1347                                     ~NETIF_F_IP_CSUM;
1348                 }
1349
1350                 break;
1351
1352         default:
1353                 return -EOPNOTSUPP;
1354         }
1355
1356         return 0;
1357 }
1358
1359 static int brcmf_netdev_ioctl_entry(struct net_device *net, struct ifreq *ifr,
1360                                     int cmd)
1361 {
1362         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1363         dhd_ioctl_t ioc;
1364         int bcmerror = 0;
1365         int buflen = 0;
1366         void *buf = NULL;
1367         uint driver = 0;
1368         int ifidx;
1369         bool is_set_key_cmd;
1370
1371         ifidx = brcmf_net2idx(dhd, net);
1372         DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __func__, ifidx, cmd));
1373
1374         if (ifidx == DHD_BAD_IF)
1375                 return -1;
1376
1377         if (cmd == SIOCETHTOOL)
1378                 return brcmf_ethtool(dhd, (void *)ifr->ifr_data);
1379
1380         if (cmd != SIOCDEVPRIVATE)
1381                 return -EOPNOTSUPP;
1382
1383         memset(&ioc, 0, sizeof(ioc));
1384
1385         /* Copy the ioc control structure part of ioctl request */
1386         if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
1387                 bcmerror = -EINVAL;
1388                 goto done;
1389         }
1390
1391         /* Copy out any buffer passed */
1392         if (ioc.buf) {
1393                 buflen = min_t(int, ioc.len, DHD_IOCTL_MAXLEN);
1394                 /* optimization for direct ioctl calls from kernel */
1395                 /*
1396                    if (segment_eq(get_fs(), KERNEL_DS)) {
1397                    buf = ioc.buf;
1398                    } else {
1399                  */
1400                 {
1401                         buf = kmalloc(buflen, GFP_ATOMIC);
1402                         if (!buf) {
1403                                 bcmerror = -ENOMEM;
1404                                 goto done;
1405                         }
1406                         if (copy_from_user(buf, ioc.buf, buflen)) {
1407                                 bcmerror = -EINVAL;
1408                                 goto done;
1409                         }
1410                 }
1411         }
1412
1413         /* To differentiate between wl and dhd read 4 more byes */
1414         if ((copy_from_user(&driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
1415                             sizeof(uint)) != 0)) {
1416                 bcmerror = -EINVAL;
1417                 goto done;
1418         }
1419
1420         if (!capable(CAP_NET_ADMIN)) {
1421                 bcmerror = -EPERM;
1422                 goto done;
1423         }
1424
1425         /* check for local dhd ioctl and handle it */
1426         if (driver == DHD_IOCTL_MAGIC) {
1427                 bcmerror = brcmf_c_ioctl((void *)&dhd->pub, &ioc, buf, buflen);
1428                 if (bcmerror)
1429                         dhd->pub.bcmerror = bcmerror;
1430                 goto done;
1431         }
1432
1433         /* send to dongle (must be up, and wl) */
1434         if ((dhd->pub.busstate != DHD_BUS_DATA)) {
1435                 DHD_ERROR(("%s DONGLE_DOWN,__func__\n", __func__));
1436                 bcmerror = -EIO;
1437                 goto done;
1438         }
1439
1440         if (!dhd->pub.iswl) {
1441                 bcmerror = -EIO;
1442                 goto done;
1443         }
1444
1445         /*
1446          * Intercept BRCMF_C_SET_KEY IOCTL - serialize M4 send and
1447          * set key IOCTL to prevent M4 encryption.
1448          */
1449         is_set_key_cmd = ((ioc.cmd == BRCMF_C_SET_KEY) ||
1450                           ((ioc.cmd == BRCMF_C_SET_VAR) &&
1451                            !(strncmp("wsec_key", ioc.buf, 9))) ||
1452                           ((ioc.cmd == BRCMF_C_SET_VAR) &&
1453                            !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1454         if (is_set_key_cmd)
1455                 brcmf_netdev_wait_pend8021x(net);
1456
1457         bcmerror =
1458             brcmf_proto_ioctl(&dhd->pub, ifidx, (wl_ioctl_t *)&ioc, buf,
1459                               buflen);
1460
1461 done:
1462         if (!bcmerror && buf && ioc.buf) {
1463                 if (copy_to_user(ioc.buf, buf, buflen))
1464                         bcmerror = -EFAULT;
1465         }
1466
1467         kfree(buf);
1468
1469         if (bcmerror > 0)
1470                 bcmerror = 0;
1471
1472         return bcmerror;
1473 }
1474
1475 static int brcmf_netdev_stop(struct net_device *net)
1476 {
1477 #if !defined(IGNORE_ETH0_DOWN)
1478         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1479
1480         DHD_TRACE(("%s: Enter\n", __func__));
1481         wl_cfg80211_down();
1482         if (dhd->pub.up == 0)
1483                 return 0;
1484
1485         /* Set state and stop OS transmissions */
1486         dhd->pub.up = 0;
1487         netif_stop_queue(net);
1488 #else
1489         DHD_ERROR(("BYPASS %s:due to BRCM compilation : under investigation\n",
1490                 __func__));
1491 #endif                          /* !defined(IGNORE_ETH0_DOWN) */
1492
1493         return 0;
1494 }
1495
1496 static int brcmf_netdev_open(struct net_device *net)
1497 {
1498         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1499         u32 toe_ol;
1500         int ifidx = brcmf_net2idx(dhd, net);
1501         s32 ret = 0;
1502
1503         DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1504
1505         if (ifidx == 0) {       /* do it only for primary eth0 */
1506
1507                 /* try to bring up bus */
1508                 ret = brcmf_bus_start(&dhd->pub);
1509                 if (ret != 0) {
1510                         DHD_ERROR(("%s: failed with code %d\n", __func__, ret));
1511                         return -1;
1512                 }
1513                 atomic_set(&dhd->pend_8021x_cnt, 0);
1514
1515                 memcpy(net->dev_addr, dhd->pub.mac, ETH_ALEN);
1516
1517                 /* Get current TOE mode from dongle */
1518                 if (brcmf_toe_get(dhd, ifidx, &toe_ol) >= 0
1519                     && (toe_ol & TOE_TX_CSUM_OL) != 0)
1520                         dhd->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
1521                 else
1522                         dhd->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
1523         }
1524         /* Allow transmit calls */
1525         netif_start_queue(net);
1526         dhd->pub.up = 1;
1527         if (unlikely(wl_cfg80211_up())) {
1528                 DHD_ERROR(("%s: failed to bring up cfg80211\n",
1529                            __func__));
1530                 return -1;
1531         }
1532
1533         return ret;
1534 }
1535
1536 int
1537 brcmf_add_if(dhd_info_t *dhd, int ifidx, void *handle, char *name,
1538            u8 *mac_addr, u32 flags, u8 bssidx)
1539 {
1540         dhd_if_t *ifp;
1541
1542         DHD_TRACE(("%s: idx %d, handle->%p\n", __func__, ifidx, handle));
1543
1544         ASSERT(dhd && (ifidx < DHD_MAX_IFS));
1545
1546         ifp = dhd->iflist[ifidx];
1547         if (!ifp) {
1548                 ifp = kmalloc(sizeof(dhd_if_t), GFP_ATOMIC);
1549                 if (!ifp) {
1550                         DHD_ERROR(("%s: OOM - dhd_if_t\n", __func__));
1551                         return -ENOMEM;
1552                 }
1553         }
1554
1555         memset(ifp, 0, sizeof(dhd_if_t));
1556         ifp->info = dhd;
1557         dhd->iflist[ifidx] = ifp;
1558         strlcpy(ifp->name, name, IFNAMSIZ);
1559         if (mac_addr != NULL)
1560                 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
1561
1562         if (handle == NULL) {
1563                 ifp->state = BRCMF_E_IF_ADD;
1564                 ifp->idx = ifidx;
1565                 ASSERT(dhd->sysioc_tsk);
1566                 up(&dhd->sysioc_sem);
1567         } else
1568                 ifp->net = (struct net_device *)handle;
1569
1570         return 0;
1571 }
1572
1573 void brcmf_del_if(dhd_info_t *dhd, int ifidx)
1574 {
1575         dhd_if_t *ifp;
1576
1577         DHD_TRACE(("%s: idx %d\n", __func__, ifidx));
1578
1579         ASSERT(dhd && ifidx && (ifidx < DHD_MAX_IFS));
1580         ifp = dhd->iflist[ifidx];
1581         if (!ifp) {
1582                 DHD_ERROR(("%s: Null interface\n", __func__));
1583                 return;
1584         }
1585
1586         ifp->state = BRCMF_E_IF_DEL;
1587         ifp->idx = ifidx;
1588         ASSERT(dhd->sysioc_tsk);
1589         up(&dhd->sysioc_sem);
1590 }
1591
1592 dhd_pub_t *brcmf_attach(struct dhd_bus *bus, uint bus_hdrlen)
1593 {
1594         dhd_info_t *dhd = NULL;
1595         struct net_device *net;
1596
1597         DHD_TRACE(("%s: Enter\n", __func__));
1598         /* updates firmware nvram path if it was provided as module
1599                  paramters */
1600         if ((firmware_path != NULL) && (firmware_path[0] != '\0'))
1601                 strcpy(brcmf_fw_path, firmware_path);
1602         if ((nvram_path != NULL) && (nvram_path[0] != '\0'))
1603                 strcpy(brcmf_nv_path, nvram_path);
1604
1605         /* Allocate etherdev, including space for private structure */
1606         net = alloc_etherdev(sizeof(dhd));
1607         if (!net) {
1608                 DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
1609                 goto fail;
1610         }
1611
1612         /* Allocate primary dhd_info */
1613         dhd = kzalloc(sizeof(dhd_info_t), GFP_ATOMIC);
1614         if (!dhd) {
1615                 DHD_ERROR(("%s: OOM - alloc dhd_info\n", __func__));
1616                 goto fail;
1617         }
1618
1619         /*
1620          * Save the dhd_info into the priv
1621          */
1622         memcpy(netdev_priv(net), &dhd, sizeof(dhd));
1623
1624         /* Set network interface name if it was provided as module parameter */
1625         if (iface_name[0]) {
1626                 int len;
1627                 char ch;
1628                 strncpy(net->name, iface_name, IFNAMSIZ);
1629                 net->name[IFNAMSIZ - 1] = 0;
1630                 len = strlen(net->name);
1631                 ch = net->name[len - 1];
1632                 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1633                         strcat(net->name, "%d");
1634         }
1635
1636         if (brcmf_add_if(dhd, 0, (void *)net, net->name, NULL, 0, 0) ==
1637             DHD_BAD_IF)
1638                 goto fail;
1639
1640         net->netdev_ops = NULL;
1641         sema_init(&dhd->proto_sem, 1);
1642         /* Initialize other structure content */
1643         init_waitqueue_head(&dhd->ioctl_resp_wait);
1644         init_waitqueue_head(&dhd->ctrl_wait);
1645
1646         /* Initialize the spinlocks */
1647         spin_lock_init(&dhd->sdlock);
1648
1649         /* Link to info module */
1650         dhd->pub.info = dhd;
1651
1652         /* Link to bus module */
1653         dhd->pub.bus = bus;
1654         dhd->pub.hdrlen = bus_hdrlen;
1655
1656         /* Attach and link in the protocol */
1657         if (brcmf_proto_attach(&dhd->pub) != 0) {
1658                 DHD_ERROR(("dhd_prot_attach failed\n"));
1659                 goto fail;
1660         }
1661
1662         /* Attach and link in the cfg80211 */
1663         if (unlikely(wl_cfg80211_attach(net, &dhd->pub))) {
1664                 DHD_ERROR(("wl_cfg80211_attach failed\n"));
1665                 goto fail;
1666         }
1667         if (!brcmf_no_fw_req) {
1668                 strcpy(brcmf_fw_path, wl_cfg80211_get_fwname());
1669                 strcpy(brcmf_nv_path, wl_cfg80211_get_nvramname());
1670         }
1671
1672         /* Set up the watchdog timer */
1673         init_timer(&dhd->timer);
1674         dhd->timer.data = (unsigned long) dhd;
1675         dhd->timer.function = brcmf_watchdog;
1676
1677         /* Initialize thread based operation and lock */
1678         sema_init(&dhd->sdsem, 1);
1679         if ((brcmf_watchdog_prio >= 0) && (brcmf_dpc_prio >= 0))
1680                 dhd->threads_only = true;
1681         else
1682                 dhd->threads_only = false;
1683
1684         if (brcmf_dpc_prio >= 0) {
1685                 /* Initialize watchdog thread */
1686                 sema_init(&dhd->watchdog_sem, 0);
1687                 dhd->watchdog_tsk = kthread_run(brcmf_watchdog_thread, dhd,
1688                                                 "dhd_watchdog");
1689                 if (IS_ERR(dhd->watchdog_tsk)) {
1690                         printk(KERN_WARNING
1691                                 "dhd_watchdog thread failed to start\n");
1692                         dhd->watchdog_tsk = NULL;
1693                 }
1694         } else {
1695                 dhd->watchdog_tsk = NULL;
1696         }
1697
1698         /* Set up the bottom half handler */
1699         if (brcmf_dpc_prio >= 0) {
1700                 /* Initialize DPC thread */
1701                 sema_init(&dhd->dpc_sem, 0);
1702                 dhd->dpc_tsk = kthread_run(brcmf_dpc_thread, dhd, "dhd_dpc");
1703                 if (IS_ERR(dhd->dpc_tsk)) {
1704                         printk(KERN_WARNING
1705                                 "dhd_dpc thread failed to start\n");
1706                         dhd->dpc_tsk = NULL;
1707                 }
1708         } else {
1709                 tasklet_init(&dhd->tasklet, brcmf_dpc, (unsigned long) dhd);
1710                 dhd->dpc_tsk = NULL;
1711         }
1712
1713         if (brcmf_sysioc) {
1714                 sema_init(&dhd->sysioc_sem, 0);
1715                 dhd->sysioc_tsk = kthread_run(_brcmf_sysioc_thread, dhd,
1716                                                 "_dhd_sysioc");
1717                 if (IS_ERR(dhd->sysioc_tsk)) {
1718                         printk(KERN_WARNING
1719                                 "_dhd_sysioc thread failed to start\n");
1720                         dhd->sysioc_tsk = NULL;
1721                 }
1722         } else
1723                 dhd->sysioc_tsk = NULL;
1724
1725         /*
1726          * Save the dhd_info into the priv
1727          */
1728         memcpy(netdev_priv(net), &dhd, sizeof(dhd));
1729
1730 #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
1731         g_bus = bus;
1732 #endif
1733 #if defined(CONFIG_PM_SLEEP)
1734         atomic_set(&brcmf_mmc_suspend, false);
1735 #endif  /* defined(CONFIG_PM_SLEEP) */
1736         /* && defined(DHD_GPL) */
1737         /* Init lock suspend to prevent kernel going to suspend */
1738 #ifdef CONFIG_HAS_EARLYSUSPEND
1739         dhd->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 20;
1740         dhd->early_suspend.suspend = brcmf_early_suspend;
1741         dhd->early_suspend.resume = brcmf_late_resume;
1742         register_early_suspend(&dhd->early_suspend);
1743 #endif
1744
1745         return &dhd->pub;
1746
1747 fail:
1748         if (net)
1749                 free_netdev(net);
1750         if (dhd)
1751                 brcmf_detach(&dhd->pub);
1752
1753         return NULL;
1754 }
1755
1756 int brcmf_bus_start(dhd_pub_t *dhdp)
1757 {
1758         int ret = -1;
1759         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1760         char iovbuf[WL_EVENTING_MASK_LEN + 12]; /*  Room for "event_msgs" +
1761                                                  '\0' + bitvec  */
1762
1763         ASSERT(dhd);
1764
1765         DHD_TRACE(("%s:\n", __func__));
1766
1767         /* try to download image and nvram to the dongle */
1768         if (dhd->pub.busstate == DHD_BUS_DOWN) {
1769                 if (!(dhd_bus_download_firmware(dhd->pub.bus, brcmf_fw_path,
1770                                                 brcmf_nv_path))) {
1771                         DHD_ERROR(("%s: dhd_bus_download_firmware failed. "
1772                                 "firmware = %s nvram = %s\n",
1773                                 __func__, brcmf_fw_path, brcmf_nv_path));
1774                         return -1;
1775                 }
1776         }
1777
1778         /* Start the watchdog timer */
1779         dhd->pub.tickcnt = 0;
1780         brcmf_os_wd_timer(&dhd->pub, brcmf_watchdog_ms);
1781
1782         /* Bring up the bus */
1783         ret = brcmf_sdbrcm_bus_init(&dhd->pub, true);
1784         if (ret != 0) {
1785                 DHD_ERROR(("%s, brcmf_sdbrcm_bus_init failed %d\n", __func__,
1786                            ret));
1787                 return ret;
1788         }
1789
1790         /* If bus is not ready, can't come up */
1791         if (dhd->pub.busstate != DHD_BUS_DATA) {
1792                 del_timer_sync(&dhd->timer);
1793                 dhd->wd_timer_valid = false;
1794                 DHD_ERROR(("%s failed bus is not ready\n", __func__));
1795                 return -ENODEV;
1796         }
1797
1798         brcmu_mkiovar("event_msgs", dhdp->eventmask, WL_EVENTING_MASK_LEN,
1799                       iovbuf, sizeof(iovbuf));
1800         brcmf_proto_cdc_query_ioctl(dhdp, 0, BRCMF_C_GET_VAR, iovbuf,
1801                                     sizeof(iovbuf));
1802         memcpy(dhdp->eventmask, iovbuf, WL_EVENTING_MASK_LEN);
1803
1804         setbit(dhdp->eventmask, BRCMF_E_SET_SSID);
1805         setbit(dhdp->eventmask, BRCMF_E_PRUNE);
1806         setbit(dhdp->eventmask, BRCMF_E_AUTH);
1807         setbit(dhdp->eventmask, BRCMF_E_REASSOC);
1808         setbit(dhdp->eventmask, BRCMF_E_REASSOC_IND);
1809         setbit(dhdp->eventmask, BRCMF_E_DEAUTH_IND);
1810         setbit(dhdp->eventmask, BRCMF_E_DISASSOC_IND);
1811         setbit(dhdp->eventmask, BRCMF_E_DISASSOC);
1812         setbit(dhdp->eventmask, BRCMF_E_JOIN);
1813         setbit(dhdp->eventmask, BRCMF_E_ASSOC_IND);
1814         setbit(dhdp->eventmask, BRCMF_E_PSK_SUP);
1815         setbit(dhdp->eventmask, BRCMF_E_LINK);
1816         setbit(dhdp->eventmask, BRCMF_E_NDIS_LINK);
1817         setbit(dhdp->eventmask, BRCMF_E_MIC_ERROR);
1818         setbit(dhdp->eventmask, BRCMF_E_PMKID_CACHE);
1819         setbit(dhdp->eventmask, BRCMF_E_TXFAIL);
1820         setbit(dhdp->eventmask, BRCMF_E_JOIN_START);
1821         setbit(dhdp->eventmask, BRCMF_E_SCAN_COMPLETE);
1822
1823 /* enable dongle roaming event */
1824
1825         dhdp->pktfilter_count = 1;
1826         /* Setup filter to allow only unicast */
1827         dhdp->pktfilter[0] = "100 0 0 0 0x01 0x00";
1828
1829         /* Bus is ready, do any protocol initialization */
1830         ret = brcmf_proto_init(&dhd->pub);
1831         if (ret < 0)
1832                 return ret;
1833
1834         return 0;
1835 }
1836
1837 int brcmf_iovar(dhd_pub_t *pub, int ifidx, char *name, char *cmd_buf,
1838           uint cmd_len, int set)
1839 {
1840         char buf[strlen(name) + 1 + cmd_len];
1841         int len = sizeof(buf);
1842         wl_ioctl_t ioc;
1843         int ret;
1844
1845         len = brcmu_mkiovar(name, cmd_buf, cmd_len, buf, len);
1846
1847         memset(&ioc, 0, sizeof(ioc));
1848
1849         ioc.cmd = set ? BRCMF_C_SET_VAR : BRCMF_C_GET_VAR;
1850         ioc.buf = buf;
1851         ioc.len = len;
1852         ioc.set = set;
1853
1854         ret = brcmf_proto_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
1855         if (!set && ret >= 0)
1856                 memcpy(cmd_buf, buf, cmd_len);
1857
1858         return ret;
1859 }
1860
1861 static struct net_device_ops brcmf_netdev_ops_pri = {
1862         .ndo_open = brcmf_netdev_open,
1863         .ndo_stop = brcmf_netdev_stop,
1864         .ndo_get_stats = brcmf_netdev_get_stats,
1865         .ndo_do_ioctl = brcmf_netdev_ioctl_entry,
1866         .ndo_start_xmit = brcmf_netdev_start_xmit,
1867         .ndo_set_mac_address = brcmf_netdev_set_mac_address,
1868         .ndo_set_multicast_list = brcmf_netdev_set_multicast_list
1869 };
1870
1871 int brcmf_net_attach(dhd_pub_t *dhdp, int ifidx)
1872 {
1873         dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1874         struct net_device *net;
1875         u8 temp_addr[ETH_ALEN] = {
1876                 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
1877
1878         DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1879
1880         ASSERT(dhd && dhd->iflist[ifidx]);
1881
1882         net = dhd->iflist[ifidx]->net;
1883         ASSERT(net);
1884
1885         ASSERT(!net->netdev_ops);
1886         net->netdev_ops = &brcmf_netdev_ops_pri;
1887
1888         /*
1889          * We have to use the primary MAC for virtual interfaces
1890          */
1891         if (ifidx != 0) {
1892                 /* for virtual interfaces use the primary MAC  */
1893                 memcpy(temp_addr, dhd->pub.mac, ETH_ALEN);
1894
1895         }
1896
1897         if (ifidx == 1) {
1898                 DHD_TRACE(("%s ACCESS POINT MAC:\n", __func__));
1899                 /*  ACCESSPOINT INTERFACE CASE */
1900                 temp_addr[0] |= 0X02;   /* set bit 2 ,
1901                          - Locally Administered address  */
1902
1903         }
1904         net->hard_header_len = ETH_HLEN + dhd->pub.hdrlen;
1905         net->ethtool_ops = &brcmf_ethtool_ops;
1906
1907         dhd->pub.rxsz = net->mtu + net->hard_header_len + dhd->pub.hdrlen;
1908
1909         memcpy(net->dev_addr, temp_addr, ETH_ALEN);
1910
1911         if (register_netdev(net) != 0) {
1912                 DHD_ERROR(("%s: couldn't register the net device\n",
1913                         __func__));
1914                 goto fail;
1915         }
1916
1917         DHD_INFO(("%s: Broadcom Dongle Host Driver\n", net->name));
1918
1919         return 0;
1920
1921 fail:
1922         net->netdev_ops = NULL;
1923         return -EBADE;
1924 }
1925
1926 static void brcmf_bus_detach(dhd_pub_t *dhdp)
1927 {
1928         dhd_info_t *dhd;
1929
1930         DHD_TRACE(("%s: Enter\n", __func__));
1931
1932         if (dhdp) {
1933                 dhd = (dhd_info_t *) dhdp->info;
1934                 if (dhd) {
1935                         /* Stop the protocol module */
1936                         brcmf_proto_stop(&dhd->pub);
1937
1938                         /* Stop the bus module */
1939                         brcmf_sdbrcm_bus_stop(dhd->pub.bus, true);
1940
1941                         /* Clear the watchdog timer */
1942                         del_timer_sync(&dhd->timer);
1943                         dhd->wd_timer_valid = false;
1944                 }
1945         }
1946 }
1947
1948 void brcmf_detach(dhd_pub_t *dhdp)
1949 {
1950         dhd_info_t *dhd;
1951
1952         DHD_TRACE(("%s: Enter\n", __func__));
1953
1954         if (dhdp) {
1955                 dhd = (dhd_info_t *) dhdp->info;
1956                 if (dhd) {
1957                         dhd_if_t *ifp;
1958                         int i;
1959
1960 #if defined(CONFIG_HAS_EARLYSUSPEND)
1961                         if (dhd->early_suspend.suspend)
1962                                 unregister_early_suspend(&dhd->early_suspend);
1963 #endif                          /* defined(CONFIG_HAS_EARLYSUSPEND) */
1964
1965                         for (i = 1; i < DHD_MAX_IFS; i++)
1966                                 if (dhd->iflist[i])
1967                                         brcmf_del_if(dhd, i);
1968
1969                         ifp = dhd->iflist[0];
1970                         ASSERT(ifp);
1971                         if (ifp->net->netdev_ops == &brcmf_netdev_ops_pri) {
1972                                 brcmf_netdev_stop(ifp->net);
1973                                 unregister_netdev(ifp->net);
1974                         }
1975
1976                         if (dhd->watchdog_tsk) {
1977                                 send_sig(SIGTERM, dhd->watchdog_tsk, 1);
1978                                 kthread_stop(dhd->watchdog_tsk);
1979                                 dhd->watchdog_tsk = NULL;
1980                         }
1981
1982                         if (dhd->dpc_tsk) {
1983                                 send_sig(SIGTERM, dhd->dpc_tsk, 1);
1984                                 kthread_stop(dhd->dpc_tsk);
1985                                 dhd->dpc_tsk = NULL;
1986                         } else
1987                                 tasklet_kill(&dhd->tasklet);
1988
1989                         if (dhd->sysioc_tsk) {
1990                                 send_sig(SIGTERM, dhd->sysioc_tsk, 1);
1991                                 kthread_stop(dhd->sysioc_tsk);
1992                                 dhd->sysioc_tsk = NULL;
1993                         }
1994
1995                         brcmf_bus_detach(dhdp);
1996
1997                         if (dhdp->prot)
1998                                 brcmf_proto_detach(dhdp);
1999
2000                         wl_cfg80211_detach();
2001
2002                         /* && defined(DHD_GPL) */
2003                         free_netdev(ifp->net);
2004                         kfree(ifp);
2005                         kfree(dhd);
2006                 }
2007         }
2008 }
2009
2010 static void __exit brcmf_module_cleanup(void)
2011 {
2012         DHD_TRACE(("%s: Enter\n", __func__));
2013
2014         dhd_bus_unregister();
2015 }
2016
2017 static int __init brcmf_module_init(void)
2018 {
2019         int error;
2020
2021         DHD_TRACE(("%s: Enter\n", __func__));
2022
2023         /* Sanity check on the module parameters */
2024         do {
2025                 /* Both watchdog and DPC as tasklets are ok */
2026                 if ((brcmf_watchdog_prio < 0) && (brcmf_dpc_prio < 0))
2027                         break;
2028
2029                 /* If both watchdog and DPC are threads, TX must be deferred */
2030                 if ((brcmf_watchdog_prio >= 0) && (brcmf_dpc_prio >= 0)
2031                     && brcmf_deferred_tx)
2032                         break;
2033
2034                 DHD_ERROR(("Invalid module parameters.\n"));
2035                 return -EINVAL;
2036         } while (0);
2037
2038         error = dhd_bus_register();
2039
2040         if (error) {
2041                 DHD_ERROR(("%s: dhd_bus_register failed\n", __func__));
2042                 goto failed;
2043         }
2044         return 0;
2045
2046 failed:
2047         return -EINVAL;
2048 }
2049
2050 module_init(brcmf_module_init);
2051 module_exit(brcmf_module_cleanup);
2052
2053 /*
2054  * OS specific functions required to implement DHD driver in OS independent way
2055  */
2056 int brcmf_os_proto_block(dhd_pub_t *pub)
2057 {
2058         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2059
2060         if (dhd) {
2061                 down(&dhd->proto_sem);
2062                 return 1;
2063         }
2064         return 0;
2065 }
2066
2067 int brcmf_os_proto_unblock(dhd_pub_t *pub)
2068 {
2069         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2070
2071         if (dhd) {
2072                 up(&dhd->proto_sem);
2073                 return 1;
2074         }
2075
2076         return 0;
2077 }
2078
2079 unsigned int brcmf_os_get_ioctl_resp_timeout(void)
2080 {
2081         return (unsigned int)brcmf_ioctl_timeout_msec;
2082 }
2083
2084 void brcmf_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
2085 {
2086         brcmf_ioctl_timeout_msec = (int)timeout_msec;
2087 }
2088
2089 int brcmf_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition, bool *pending)
2090 {
2091         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2092         DECLARE_WAITQUEUE(wait, current);
2093         int timeout = brcmf_ioctl_timeout_msec;
2094
2095         /* Convert timeout in millsecond to jiffies */
2096         timeout = timeout * HZ / 1000;
2097
2098         /* Wait until control frame is available */
2099         add_wait_queue(&dhd->ioctl_resp_wait, &wait);
2100         set_current_state(TASK_INTERRUPTIBLE);
2101
2102         while (!(*condition) && (!signal_pending(current) && timeout))
2103                 timeout = schedule_timeout(timeout);
2104
2105         if (signal_pending(current))
2106                 *pending = true;
2107
2108         set_current_state(TASK_RUNNING);
2109         remove_wait_queue(&dhd->ioctl_resp_wait, &wait);
2110
2111         return timeout;
2112 }
2113
2114 int brcmf_os_ioctl_resp_wake(dhd_pub_t *pub)
2115 {
2116         dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2117
2118         if (waitqueue_active(&dhd->ioctl_resp_wait))
2119                 wake_up_interruptible(&dhd->ioctl_resp_wait);
2120
2121         return 0;
2122 }
2123
2124 void brcmf_os_wd_timer(void *bus, uint wdtick)
2125 {
2126         dhd_pub_t *pub = bus;
2127         static uint save_dhd_watchdog_ms;
2128         dhd_info_t *dhd = (dhd_info_t *) pub->info;
2129
2130         /* don't start the wd until fw is loaded */
2131         if (pub->busstate == DHD_BUS_DOWN)
2132                 return;
2133
2134         /* Totally stop the timer */
2135         if (!wdtick && dhd->wd_timer_valid == true) {
2136                 del_timer_sync(&dhd->timer);
2137                 dhd->wd_timer_valid = false;
2138                 save_dhd_watchdog_ms = wdtick;
2139                 return;
2140         }
2141
2142         if (wdtick) {
2143                 brcmf_watchdog_ms = (uint) wdtick;
2144
2145                 if (save_dhd_watchdog_ms != brcmf_watchdog_ms) {
2146
2147                         if (dhd->wd_timer_valid == true)
2148                                 /* Stop timer and restart at new value */
2149                                 del_timer_sync(&dhd->timer);
2150
2151                         /* Create timer again when watchdog period is
2152                            dynamically changed or in the first instance
2153                          */
2154                         dhd->timer.expires =
2155                             jiffies + brcmf_watchdog_ms * HZ / 1000;
2156                         add_timer(&dhd->timer);
2157
2158                 } else {
2159                         /* Re arm the timer, at last watchdog period */
2160                         mod_timer(&dhd->timer,
2161                                   jiffies + brcmf_watchdog_ms * HZ / 1000);
2162                 }
2163
2164                 dhd->wd_timer_valid = true;
2165                 save_dhd_watchdog_ms = wdtick;
2166         }
2167 }
2168
2169 void *brcmf_os_open_image(char *filename)
2170 {
2171         struct file *fp;
2172
2173         if (!brcmf_no_fw_req)
2174                 return wl_cfg80211_request_fw(filename);
2175
2176         fp = filp_open(filename, O_RDONLY, 0);
2177         /*
2178          * 2.6.11 (FC4) supports filp_open() but later revs don't?
2179          * Alternative:
2180          * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
2181          * ???
2182          */
2183         if (IS_ERR(fp))
2184                 fp = NULL;
2185
2186         return fp;
2187 }
2188
2189 int brcmf_os_get_image_block(char *buf, int len, void *image)
2190 {
2191         struct file *fp = (struct file *)image;
2192         int rdlen;
2193
2194         if (!brcmf_no_fw_req)
2195                 return wl_cfg80211_read_fw(buf, len);
2196
2197         if (!image)
2198                 return 0;
2199
2200         rdlen = kernel_read(fp, fp->f_pos, buf, len);
2201         if (rdlen > 0)
2202                 fp->f_pos += rdlen;
2203
2204         return rdlen;
2205 }
2206
2207 void brcmf_os_close_image(void *image)
2208 {
2209         if (!brcmf_no_fw_req)
2210                 return wl_cfg80211_release_fw();
2211         if (image)
2212                 filp_close((struct file *)image, NULL);
2213 }
2214
2215 void brcmf_os_sdlock(dhd_pub_t *pub)
2216 {
2217         dhd_info_t *dhd;
2218
2219         dhd = (dhd_info_t *) (pub->info);
2220
2221         if (dhd->threads_only)
2222                 down(&dhd->sdsem);
2223         else
2224                 spin_lock_bh(&dhd->sdlock);
2225 }
2226
2227 void brcmf_os_sdunlock(dhd_pub_t *pub)
2228 {
2229         dhd_info_t *dhd;
2230
2231         dhd = (dhd_info_t *) (pub->info);
2232
2233         if (dhd->threads_only)
2234                 up(&dhd->sdsem);
2235         else
2236                 spin_unlock_bh(&dhd->sdlock);
2237 }
2238
2239 static int brcmf_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
2240                             brcmf_event_msg_t *event, void **data)
2241 {
2242         int bcmerror = 0;
2243
2244         ASSERT(dhd != NULL);
2245
2246         bcmerror = brcmf_c_host_event(dhd, ifidx, pktdata, event, data);
2247         if (bcmerror != 0)
2248                 return bcmerror;
2249
2250         ASSERT(dhd->iflist[*ifidx] != NULL);
2251         ASSERT(dhd->iflist[*ifidx]->net != NULL);
2252         if (dhd->iflist[*ifidx]->net)
2253                 wl_cfg80211_event(dhd->iflist[*ifidx]->net, event, *data);
2254
2255         return bcmerror;
2256 }
2257
2258 void brcmf_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
2259 {
2260         struct dhd_info *dhdinfo = dhd->info;
2261         brcmf_os_sdunlock(dhd);
2262         wait_event_interruptible_timeout(dhdinfo->ctrl_wait,
2263                                          (*lockvar == false), HZ * 2);
2264         brcmf_os_sdlock(dhd);
2265         return;
2266 }
2267
2268 void brcmf_wait_event_wakeup(dhd_pub_t *dhd)
2269 {
2270         struct dhd_info *dhdinfo = dhd->info;
2271         if (waitqueue_active(&dhdinfo->ctrl_wait))
2272                 wake_up_interruptible(&dhdinfo->ctrl_wait);
2273         return;
2274 }
2275
2276 int brcmf_netdev_reset(struct net_device *dev, u8 flag)
2277 {
2278         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2279
2280         /* Turning off watchdog */
2281         if (flag)
2282                 brcmf_os_wd_timer(&dhd->pub, 0);
2283
2284         brcmf_bus_devreset(&dhd->pub, flag);
2285
2286         /* Turning on watchdog back */
2287         if (!flag)
2288                 brcmf_os_wd_timer(&dhd->pub, brcmf_watchdog_ms);
2289         DHD_ERROR(("%s:  WLAN OFF DONE\n", __func__));
2290
2291         return 1;
2292 }
2293
2294 int brcmf_netdev_set_suspend_disable(struct net_device *dev, int val)
2295 {
2296         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2297         int ret = 0;
2298
2299         if (dhd) {
2300                 ret = dhd->pub.suspend_disable_flag;
2301                 dhd->pub.suspend_disable_flag = val;
2302         }
2303         return ret;
2304 }
2305
2306 int brcmf_netdev_set_suspend(struct net_device *dev, int val)
2307 {
2308         int ret = 0;
2309 #if defined(CONFIG_HAS_EARLYSUSPEND)
2310         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2311
2312         if (dhd) {
2313                 brcmf_os_proto_block(&dhd->pub);
2314                 ret = brcmf_set_suspend(val, &dhd->pub);
2315                 brcmf_os_proto_unblock(&dhd->pub);
2316         }
2317 #endif          /* defined(CONFIG_HAS_EARLYSUSPEND) */
2318         return ret;
2319 }
2320
2321 int brcmf_netdev_set_dtim_skip(struct net_device *dev, int val)
2322 {
2323         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2324
2325         if (dhd)
2326                 dhd->pub.dtim_skip = val;
2327
2328         return 0;
2329 }
2330
2331 int brcmf_netdev_set_packet_filter(struct net_device *dev, int val)
2332 {
2333         dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2334         int ret = 0;
2335
2336         /* Packet filtering is set only if we still in early-suspend and
2337          * we need either to turn it ON or turn it OFF
2338          * We can always turn it OFF in case of early-suspend, but we turn it
2339          * back ON only if suspend_disable_flag was not set
2340          */
2341         if (dhd && dhd->pub.up) {
2342                 brcmf_os_proto_block(&dhd->pub);
2343                 if (dhd->pub.in_suspend) {
2344                         if (!val || (val && !dhd->pub.suspend_disable_flag))
2345                                 brcmf_set_packet_filter(val, &dhd->pub);
2346                 }
2347                 brcmf_os_proto_unblock(&dhd->pub);
2348         }
2349         return ret;
2350 }
2351
2352 void brcmf_netdev_init_ioctl(struct net_device *dev)
2353 {
2354         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2355
2356         brcmf_c_preinit_ioctls(&dhd->pub);
2357 }
2358
2359 static int brcmf_get_pend_8021x_cnt(dhd_info_t *dhd)
2360 {
2361         return atomic_read(&dhd->pend_8021x_cnt);
2362 }
2363
2364 #define MAX_WAIT_FOR_8021X_TX   10
2365
2366 int brcmf_netdev_wait_pend8021x(struct net_device *dev)
2367 {
2368         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2369         int timeout = 10 * HZ / 1000;
2370         int ntimes = MAX_WAIT_FOR_8021X_TX;
2371         int pend = brcmf_get_pend_8021x_cnt(dhd);
2372
2373         while (ntimes && pend) {
2374                 if (pend) {
2375                         set_current_state(TASK_INTERRUPTIBLE);
2376                         schedule_timeout(timeout);
2377                         set_current_state(TASK_RUNNING);
2378                         ntimes--;
2379                 }
2380                 pend = brcmf_get_pend_8021x_cnt(dhd);
2381         }
2382         return pend;
2383 }
2384
2385 #ifdef BCMDBG
2386 int brcmf_write_to_file(dhd_pub_t *dhd, u8 *buf, int size)
2387 {
2388         int ret = 0;
2389         struct file *fp;
2390         mm_segment_t old_fs;
2391         loff_t pos = 0;
2392
2393         /* change to KERNEL_DS address limit */
2394         old_fs = get_fs();
2395         set_fs(KERNEL_DS);
2396
2397         /* open file to write */
2398         fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
2399         if (!fp) {
2400                 DHD_ERROR(("%s: open file error\n", __func__));
2401                 ret = -1;
2402                 goto exit;
2403         }
2404
2405         /* Write buf to file */
2406         fp->f_op->write(fp, buf, size, &pos);
2407
2408 exit:
2409         /* free buf before return */
2410         kfree(buf);
2411         /* close file before return */
2412         if (fp)
2413                 filp_close(fp, current->files);
2414         /* restore previous address limit */
2415         set_fs(old_fs);
2416
2417         return ret;
2418 }
2419 #endif                          /* BCMDBG */
2420
2421 #if defined(BCMDBG)
2422 void osl_assert(char *exp, char *file, int line)
2423 {
2424         char tempbuf[256];
2425         char *basename;
2426
2427         basename = strrchr(file, '/');
2428         /* skip the '/' */
2429         if (basename)
2430                 basename++;
2431
2432         if (!basename)
2433                 basename = file;
2434
2435         snprintf(tempbuf, 256,
2436                  "assertion \"%s\" failed: file \"%s\", line %d\n", exp,
2437                  basename, line);
2438
2439         /*
2440          * Print assert message and give it time to
2441          * be written to /var/log/messages
2442          */
2443         if (!in_interrupt()) {
2444                 const int delay = 3;
2445                 printk(KERN_ERR "%s", tempbuf);
2446                 printk(KERN_ERR "panic in %d seconds\n", delay);
2447                 set_current_state(TASK_INTERRUPTIBLE);
2448                 schedule_timeout(delay * HZ);
2449         }
2450
2451         switch (g_assert_type) {
2452         case 0:
2453                 panic(KERN_ERR "%s", tempbuf);
2454                 break;
2455         case 1:
2456                 printk(KERN_ERR "%s", tempbuf);
2457                 BUG();
2458                 break;
2459         case 2:
2460                 printk(KERN_ERR "%s", tempbuf);
2461                 break;
2462         default:
2463                 break;
2464         }
2465 }
2466 #endif                          /* defined(BCMDBG) */