staging: brcm80211: return error code to mac80211 for 40MHz channels
[deliverable/linux.git] / drivers / staging / brcm80211 / brcmfmac / dhd_linux.c
CommitLineData
cf2b4488
HP
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#ifdef CONFIG_WIFI_CONTROL_FUNC
18#include <linux/platform_device.h>
19#endif
cf2b4488
HP
20#include <linux/init.h>
21#include <linux/kernel.h>
860708d9 22#include <linux/kthread.h>
cf2b4488
HP
23#include <linux/slab.h>
24#include <linux/skbuff.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
93ad12cf 27#include <linux/mmc/sdio_func.h>
cf2b4488
HP
28#include <linux/random.h>
29#include <linux/spinlock.h>
30#include <linux/ethtool.h>
31#include <linux/fcntl.h>
32#include <linux/fs.h>
93ad12cf 33#include <linux/uaccess.h>
a1c16ed2 34#include <bcmdefs.h>
a1c16ed2 35#include <osl.h>
cf2b4488
HP
36#include <bcmutils.h>
37#include <bcmendian.h>
38
cf2b4488
HP
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
cf2b4488 45#include <wl_cfg80211.h>
cf2b4488 46
c09240ac
AS
47#define EPI_VERSION_STR "4.218.248.5"
48#define ETH_P_BRCM 0x886c
cf2b4488
HP
49
50#if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
51#include <linux/wifi_tiwlan.h>
52
53struct semaphore wifi_control_sem;
54
55struct dhd_bus *g_bus;
56
5f782dee
JC
57static struct wifi_platform_data *wifi_control_data;
58static struct resource *wifi_irqres;
cf2b4488
HP
59
60int wifi_get_irq_number(unsigned long *irq_flags_ptr)
61{
62 if (wifi_irqres) {
63 *irq_flags_ptr = wifi_irqres->flags & IRQF_TRIGGER_MASK;
64 return (int)wifi_irqres->start;
65 }
66#ifdef CUSTOM_OOB_GPIO_NUM
67 return CUSTOM_OOB_GPIO_NUM;
68#else
69 return -1;
70#endif
71}
72
73int wifi_set_carddetect(int on)
74{
75 printk(KERN_ERR "%s = %d\n", __func__, on);
76 if (wifi_control_data && wifi_control_data->set_carddetect)
77 wifi_control_data->set_carddetect(on);
78 return 0;
79}
80
81int wifi_set_power(int on, unsigned long msec)
82{
83 printk(KERN_ERR "%s = %d\n", __func__, on);
84 if (wifi_control_data && wifi_control_data->set_power)
85 wifi_control_data->set_power(on);
86 if (msec)
87 mdelay(msec);
88 return 0;
89}
90
91int wifi_set_reset(int on, unsigned long msec)
92{
93 printk(KERN_ERR "%s = %d\n", __func__, on);
94 if (wifi_control_data && wifi_control_data->set_reset)
95 wifi_control_data->set_reset(on);
96 if (msec)
97 mdelay(msec);
98 return 0;
99}
100
101static int wifi_probe(struct platform_device *pdev)
102{
103 struct wifi_platform_data *wifi_ctrl =
104 (struct wifi_platform_data *)(pdev->dev.platform_data);
105
106 printk(KERN_ERR "## %s\n", __func__);
107 wifi_irqres =
108 platform_get_resource_byname(pdev, IORESOURCE_IRQ,
109 "bcm4329_wlan_irq");
110 wifi_control_data = wifi_ctrl;
111
112 wifi_set_power(1, 0); /* Power On */
113 wifi_set_carddetect(1); /* CardDetect (0->1) */
114
115 up(&wifi_control_sem);
116 return 0;
117}
118
119static int wifi_remove(struct platform_device *pdev)
120{
121 struct wifi_platform_data *wifi_ctrl =
122 (struct wifi_platform_data *)(pdev->dev.platform_data);
123
124 printk(KERN_ERR "## %s\n", __func__);
125 wifi_control_data = wifi_ctrl;
126
127 wifi_set_carddetect(0); /* CardDetect (1->0) */
128 wifi_set_power(0, 0); /* Power Off */
129
130 up(&wifi_control_sem);
131 return 0;
132}
133
134static int wifi_suspend(struct platform_device *pdev, pm_message_t state)
135{
136 DHD_TRACE(("##> %s\n", __func__));
137 return 0;
138}
139
140static int wifi_resume(struct platform_device *pdev)
141{
142 DHD_TRACE(("##> %s\n", __func__));
143 return 0;
144}
145
146static struct platform_driver wifi_device = {
147 .probe = wifi_probe,
148 .remove = wifi_remove,
149 .suspend = wifi_suspend,
150 .resume = wifi_resume,
151 .driver = {
c836f77f 152 .name = KBUILD_MODNAME,
cf2b4488
HP
153 }
154};
155
156int wifi_add_dev(void)
157{
158 DHD_TRACE(("## Calling platform_driver_register\n"));
159 return platform_driver_register(&wifi_device);
160}
161
162void wifi_del_dev(void)
163{
164 DHD_TRACE(("## Unregister platform_driver_register\n"));
165 platform_driver_unregister(&wifi_device);
166}
167#endif /* defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC) */
168
169#if defined(CONFIG_PM_SLEEP)
170#include <linux/suspend.h>
0965ae88 171volatile bool dhd_mmc_suspend = false;
cf2b4488
HP
172DECLARE_WAIT_QUEUE_HEAD(dhd_dpc_wait);
173#endif /* defined(CONFIG_PM_SLEEP) */
174
175#if defined(OOB_INTR_ONLY)
176extern void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable);
177#endif /* defined(OOB_INTR_ONLY) */
178
179MODULE_AUTHOR("Broadcom Corporation");
180MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac driver.");
181MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac cards");
182MODULE_LICENSE("Dual BSD/GPL");
183
93ad12cf 184#define DRV_MODULE_NAME "brcmfmac"
185
cf2b4488
HP
186/* Linux wireless extension support */
187#if defined(CONFIG_WIRELESS_EXT)
188#include <wl_iw.h>
189extern wl_iw_extra_params_t g_wl_iw_params;
190#endif /* defined(CONFIG_WIRELESS_EXT) */
191
192#if defined(CONFIG_HAS_EARLYSUSPEND)
193#include <linux/earlysuspend.h>
194extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
195 uint len);
196#endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
197
198#ifdef PKT_FILTER_SUPPORT
199extern void dhd_pktfilter_offload_set(dhd_pub_t *dhd, char *arg);
200extern void dhd_pktfilter_offload_enable(dhd_pub_t *dhd, char *arg, int enable,
201 int master_mode);
202#endif
203
204/* Interface control information */
205typedef struct dhd_if {
206 struct dhd_info *info; /* back pointer to dhd_info */
207 /* OS/stack specifics */
208 struct net_device *net;
209 struct net_device_stats stats;
210 int idx; /* iface idx in dongle */
211 int state; /* interface state */
212 uint subunit; /* subunit */
b8d63078 213 u8 mac_addr[ETH_ALEN]; /* assigned MAC address */
cf2b4488
HP
214 bool attached; /* Delayed attachment when unset */
215 bool txflowcontrol; /* Per interface flow control indicator */
cbf6baac 216 char name[IFNAMSIZ]; /* linux interface name */
cf2b4488
HP
217} dhd_if_t;
218
219/* Local private structure (extension of pub) */
220typedef struct dhd_info {
221#if defined(CONFIG_WIRELESS_EXT)
222 wl_iw_t iw; /* wireless extensions state (must be first) */
223#endif /* defined(CONFIG_WIRELESS_EXT) */
224
225 dhd_pub_t pub;
226
227 /* OS/stack specifics */
228 dhd_if_t *iflist[DHD_MAX_IFS];
229
230 struct semaphore proto_sem;
231 wait_queue_head_t ioctl_resp_wait;
232 struct timer_list timer;
233 bool wd_timer_valid;
234 struct tasklet_struct tasklet;
235 spinlock_t sdlock;
236 spinlock_t txqlock;
237 /* Thread based operation */
238 bool threads_only;
239 struct semaphore sdsem;
860708d9 240 struct task_struct *watchdog_tsk;
cf2b4488 241 struct semaphore watchdog_sem;
ecd7559d 242 struct task_struct *dpc_tsk;
cf2b4488 243 struct semaphore dpc_sem;
cf2b4488
HP
244
245 /* Thread to issue ioctl for multicast */
d809dcb9 246 struct task_struct *sysioc_tsk;
cf2b4488 247 struct semaphore sysioc_sem;
cf2b4488
HP
248 bool set_multicast;
249 bool set_macaddress;
a44d4236 250 u8 macvalue[ETH_ALEN];
cf2b4488
HP
251 wait_queue_head_t ctrl_wait;
252 atomic_t pend_8021x_cnt;
253
254#ifdef CONFIG_HAS_EARLYSUSPEND
255 struct early_suspend early_suspend;
256#endif /* CONFIG_HAS_EARLYSUSPEND */
257} dhd_info_t;
258
259/* Definitions to provide path to the firmware and nvram
260 * example nvram_path[MOD_PARAM_PATHLEN]="/projects/wlan/nvram.txt"
261 */
262char firmware_path[MOD_PARAM_PATHLEN];
263char nvram_path[MOD_PARAM_PATHLEN];
264
265/* load firmware and/or nvram values from the filesystem */
266module_param_string(firmware_path, firmware_path, MOD_PARAM_PATHLEN, 0);
267module_param_string(nvram_path, nvram_path, MOD_PARAM_PATHLEN, 0);
268
269/* Error bits */
270module_param(dhd_msg_level, int, 0);
271
272/* Spawn a thread for system ioctls (set mac, set mcast) */
0f0881b0 273uint dhd_sysioc = true;
cf2b4488
HP
274module_param(dhd_sysioc, uint, 0);
275
276/* Watchdog interval */
277uint dhd_watchdog_ms = 10;
278module_param(dhd_watchdog_ms, uint, 0);
279
280#ifdef DHD_DEBUG
281/* Console poll interval */
6998d337 282uint dhd_console_ms;
cf2b4488
HP
283module_param(dhd_console_ms, uint, 0);
284#endif /* DHD_DEBUG */
285
286/* ARP offload agent mode : Enable ARP Host Auto-Reply
287and ARP Peer Auto-Reply */
288uint dhd_arp_mode = 0xb;
289module_param(dhd_arp_mode, uint, 0);
290
291/* ARP offload enable */
0f0881b0 292uint dhd_arp_enable = true;
cf2b4488
HP
293module_param(dhd_arp_enable, uint, 0);
294
295/* Global Pkt filter enable control */
0f0881b0 296uint dhd_pkt_filter_enable = true;
cf2b4488
HP
297module_param(dhd_pkt_filter_enable, uint, 0);
298
299/* Pkt filter init setup */
6998d337 300uint dhd_pkt_filter_init;
cf2b4488
HP
301module_param(dhd_pkt_filter_init, uint, 0);
302
303/* Pkt filter mode control */
0f0881b0 304uint dhd_master_mode = true;
cf2b4488
HP
305module_param(dhd_master_mode, uint, 1);
306
307/* Watchdog thread priority, -1 to use kernel timer */
308int dhd_watchdog_prio = 97;
309module_param(dhd_watchdog_prio, int, 0);
310
311/* DPC thread priority, -1 to use tasklet */
312int dhd_dpc_prio = 98;
313module_param(dhd_dpc_prio, int, 0);
314
315/* DPC thread priority, -1 to use tasklet */
316extern int dhd_dongle_memsize;
317module_param(dhd_dongle_memsize, int, 0);
318
319/* Contorl fw roaming */
320#ifdef CUSTOMER_HW2
6998d337 321uint dhd_roam;
cf2b4488
HP
322#else
323uint dhd_roam = 1;
324#endif
325
326/* Control radio state */
327uint dhd_radio_up = 1;
328
329/* Network inteface name */
330char iface_name[IFNAMSIZ];
331module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
332
cf2b4488
HP
333/* The following are specific to the SDIO dongle */
334
335/* IOCTL response timeout */
336int dhd_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
337
338/* Idle timeout for backplane clock */
339int dhd_idletime = DHD_IDLETIME_TICKS;
340module_param(dhd_idletime, int, 0);
341
342/* Use polling */
0965ae88 343uint dhd_poll = false;
cf2b4488
HP
344module_param(dhd_poll, uint, 0);
345
cf2b4488 346/* Use cfg80211 */
0f0881b0 347uint dhd_cfg80211 = true;
cf2b4488 348module_param(dhd_cfg80211, uint, 0);
cf2b4488
HP
349
350/* Use interrupts */
0f0881b0 351uint dhd_intr = true;
cf2b4488
HP
352module_param(dhd_intr, uint, 0);
353
354/* SDIO Drive Strength (in milliamps) */
355uint dhd_sdiod_drive_strength = 6;
356module_param(dhd_sdiod_drive_strength, uint, 0);
357
358/* Tx/Rx bounds */
359extern uint dhd_txbound;
360extern uint dhd_rxbound;
361module_param(dhd_txbound, uint, 0);
362module_param(dhd_rxbound, uint, 0);
363
364/* Deferred transmits */
365extern uint dhd_deferred_tx;
366module_param(dhd_deferred_tx, uint, 0);
367
368#ifdef SDTEST
369/* Echo packet generator (pkts/s) */
6998d337 370uint dhd_pktgen;
cf2b4488
HP
371module_param(dhd_pktgen, uint, 0);
372
373/* Echo packet len (0 => sawtooth, max 2040) */
6998d337 374uint dhd_pktgen_len;
cf2b4488
HP
375module_param(dhd_pktgen_len, uint, 0);
376#endif
377
cf2b4488
HP
378#define FAVORITE_WIFI_CP (!!dhd_cfg80211)
379#define IS_CFG80211_FAVORITE() FAVORITE_WIFI_CP
380#define DBG_CFG80211_GET() ((dhd_cfg80211 & WL_DBG_MASK) >> 1)
381#define NO_FW_REQ() (dhd_cfg80211 & 0x80)
cf2b4488
HP
382
383/* Version string to report */
384#ifdef DHD_DEBUG
385#define DHD_COMPILED "\nCompiled in " SRCBASE
386#else
387#define DHD_COMPILED
388#endif
389
cf2b4488
HP
390#if defined(CONFIG_WIRELESS_EXT)
391struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev);
392#endif /* defined(CONFIG_WIRELESS_EXT) */
393
3deea904 394static void dhd_dpc(unsigned long data);
cf2b4488
HP
395/* forward decl */
396extern int dhd_wait_pend8021x(struct net_device *dev);
397
398#ifdef TOE
399#ifndef BDC
400#error TOE requires BDC
401#endif /* !BDC */
66cbd3ab
GKH
402static int dhd_toe_get(dhd_info_t *dhd, int idx, u32 *toe_ol);
403static int dhd_toe_set(dhd_info_t *dhd, int idx, u32 toe_ol);
cf2b4488
HP
404#endif /* TOE */
405
406static int dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
407 wl_event_msg_t *event_ptr, void **data_ptr);
408
409#if defined(CONFIG_PM_SLEEP)
410static int dhd_sleep_pm_callback(struct notifier_block *nfb,
411 unsigned long action, void *ignored)
412{
413 switch (action) {
414 case PM_HIBERNATION_PREPARE:
415 case PM_SUSPEND_PREPARE:
0f0881b0 416 dhd_mmc_suspend = true;
cf2b4488
HP
417 return NOTIFY_OK;
418 case PM_POST_HIBERNATION:
419 case PM_POST_SUSPEND:
0965ae88 420 dhd_mmc_suspend = false;
cf2b4488
HP
421 return NOTIFY_OK;
422 }
423 return 0;
424}
425
426static struct notifier_block dhd_sleep_pm_notifier = {
427 .notifier_call = dhd_sleep_pm_callback,
428 .priority = 0
429};
430
431extern int register_pm_notifier(struct notifier_block *nb);
432extern int unregister_pm_notifier(struct notifier_block *nb);
433#endif /* defined(CONFIG_PM_SLEEP) */
434 /* && defined(DHD_GPL) */
435static void dhd_set_packet_filter(int value, dhd_pub_t *dhd)
436{
437#ifdef PKT_FILTER_SUPPORT
438 DHD_TRACE(("%s: %d\n", __func__, value));
439 /* 1 - Enable packet filter, only allow unicast packet to send up */
440 /* 0 - Disable packet filter */
441 if (dhd_pkt_filter_enable) {
442 int i;
443
444 for (i = 0; i < dhd->pktfilter_count; i++) {
445 dhd_pktfilter_offload_set(dhd, dhd->pktfilter[i]);
446 dhd_pktfilter_offload_enable(dhd, dhd->pktfilter[i],
447 value, dhd_master_mode);
448 }
449 }
450#endif
451}
452
453#if defined(CONFIG_HAS_EARLYSUSPEND)
454static int dhd_set_suspend(int value, dhd_pub_t *dhd)
455{
456 int power_mode = PM_MAX;
457 /* wl_pkt_filter_enable_t enable_parm; */
458 char iovbuf[32];
459 int bcn_li_dtim = 3;
460#ifdef CUSTOMER_HW2
461 uint roamvar = 1;
462#endif /* CUSTOMER_HW2 */
463
464 DHD_TRACE(("%s: enter, value = %d in_suspend=%d\n",
465 __func__, value, dhd->in_suspend));
466
467 if (dhd && dhd->up) {
468 if (value && dhd->in_suspend) {
469
470 /* Kernel suspended */
471 DHD_TRACE(("%s: force extra Suspend setting\n",
472 __func__));
473
474 dhdcdc_set_ioctl(dhd, 0, WLC_SET_PM,
475 (char *)&power_mode,
476 sizeof(power_mode));
477
478 /* Enable packet filter, only allow unicast
479 packet to send up */
480 dhd_set_packet_filter(1, dhd);
481
482 /* if dtim skip setup as default force it
483 * to wake each thrid dtim
484 * for better power saving.
485 * Note that side effect is chance to miss BC/MC
486 * packet
487 */
488 if ((dhd->dtim_skip == 0) || (dhd->dtim_skip == 1))
489 bcn_li_dtim = 3;
490 else
491 bcn_li_dtim = dhd->dtim_skip;
492 bcm_mkiovar("bcn_li_dtim", (char *)&bcn_li_dtim,
493 4, iovbuf, sizeof(iovbuf));
494 dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
495 sizeof(iovbuf));
496#ifdef CUSTOMER_HW2
497 /* Disable build-in roaming to allowed \
498 * supplicant to take of romaing
499 */
500 bcm_mkiovar("roam_off", (char *)&roamvar, 4,
501 iovbuf, sizeof(iovbuf));
502 dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
503 sizeof(iovbuf));
504#endif /* CUSTOMER_HW2 */
505 } else {
506
507 /* Kernel resumed */
508 DHD_TRACE(("%s: Remove extra suspend setting\n",
509 __func__));
510
511 power_mode = PM_FAST;
512 dhdcdc_set_ioctl(dhd, 0, WLC_SET_PM,
513 (char *)&power_mode,
514 sizeof(power_mode));
515
516 /* disable pkt filter */
517 dhd_set_packet_filter(0, dhd);
518
519 /* restore pre-suspend setting for dtim_skip */
520 bcm_mkiovar("bcn_li_dtim", (char *)&dhd->dtim_skip,
521 4, iovbuf, sizeof(iovbuf));
522
523 dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
524 sizeof(iovbuf));
525#ifdef CUSTOMER_HW2
526 roamvar = 0;
527 bcm_mkiovar("roam_off", (char *)&roamvar, 4, iovbuf,
528 sizeof(iovbuf));
529 dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
530 sizeof(iovbuf));
531#endif /* CUSTOMER_HW2 */
532 }
533 }
534
535 return 0;
536}
537
538static void dhd_suspend_resume_helper(struct dhd_info *dhd, int val)
539{
540 dhd_pub_t *dhdp = &dhd->pub;
541
542 dhd_os_proto_block(dhdp);
543 /* Set flag when early suspend was called */
544 dhdp->in_suspend = val;
545 if (!dhdp->suspend_disable_flag)
546 dhd_set_suspend(val, dhdp);
547 dhd_os_proto_unblock(dhdp);
548}
549
550static void dhd_early_suspend(struct early_suspend *h)
551{
552 struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
553
554 DHD_TRACE(("%s: enter\n", __func__));
555
556 if (dhd)
557 dhd_suspend_resume_helper(dhd, 1);
558
559}
560
561static void dhd_late_resume(struct early_suspend *h)
562{
563 struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
564
565 DHD_TRACE(("%s: enter\n", __func__));
566
567 if (dhd)
568 dhd_suspend_resume_helper(dhd, 0);
569}
570#endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
571
572/*
573 * Generalized timeout mechanism. Uses spin sleep with exponential
574 * back-off until
575 * the sleep time reaches one jiffy, then switches over to task delay. Usage:
576 *
577 * dhd_timeout_start(&tmo, usec);
578 * while (!dhd_timeout_expired(&tmo))
579 * if (poll_something())
580 * break;
581 * if (dhd_timeout_expired(&tmo))
582 * fatal();
583 */
584
585void dhd_timeout_start(dhd_timeout_t *tmo, uint usec)
586{
587 tmo->limit = usec;
588 tmo->increment = 0;
589 tmo->elapsed = 0;
590 tmo->tick = 1000000 / HZ;
591}
592
593int dhd_timeout_expired(dhd_timeout_t *tmo)
594{
595 /* Does nothing the first call */
596 if (tmo->increment == 0) {
597 tmo->increment = 1;
598 return 0;
599 }
600
601 if (tmo->elapsed >= tmo->limit)
602 return 1;
603
604 /* Add the delay that's about to take place */
605 tmo->elapsed += tmo->increment;
606
607 if (tmo->increment < tmo->tick) {
7383141b 608 udelay(tmo->increment);
cf2b4488
HP
609 tmo->increment *= 2;
610 if (tmo->increment > tmo->tick)
611 tmo->increment = tmo->tick;
612 } else {
613 wait_queue_head_t delay_wait;
614 DECLARE_WAITQUEUE(wait, current);
615 int pending;
616 init_waitqueue_head(&delay_wait);
617 add_wait_queue(&delay_wait, &wait);
618 set_current_state(TASK_INTERRUPTIBLE);
619 schedule_timeout(1);
620 pending = signal_pending(current);
621 remove_wait_queue(&delay_wait, &wait);
622 set_current_state(TASK_RUNNING);
623 if (pending)
624 return 1; /* Interrupted */
625 }
626
627 return 0;
628}
629
630static int dhd_net2idx(dhd_info_t *dhd, struct net_device *net)
631{
632 int i = 0;
633
634 ASSERT(dhd);
635 while (i < DHD_MAX_IFS) {
636 if (dhd->iflist[i] && (dhd->iflist[i]->net == net))
637 return i;
638 i++;
639 }
640
641 return DHD_BAD_IF;
642}
643
644int dhd_ifname2idx(dhd_info_t *dhd, char *name)
645{
646 int i = DHD_MAX_IFS;
647
648 ASSERT(dhd);
649
650 if (name == NULL || *name == '\0')
651 return 0;
652
653 while (--i > 0)
654 if (dhd->iflist[i]
655 && !strncmp(dhd->iflist[i]->name, name, IFNAMSIZ))
656 break;
657
658 DHD_TRACE(("%s: return idx %d for \"%s\"\n", __func__, i, name));
659
660 return i; /* default - the primary interface */
661}
662
663char *dhd_ifname(dhd_pub_t *dhdp, int ifidx)
664{
665 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
666
667 ASSERT(dhd);
668
669 if (ifidx < 0 || ifidx >= DHD_MAX_IFS) {
670 DHD_ERROR(("%s: ifidx %d out of range\n", __func__, ifidx));
671 return "<if_bad>";
672 }
673
674 if (dhd->iflist[ifidx] == NULL) {
675 DHD_ERROR(("%s: null i/f %d\n", __func__, ifidx));
676 return "<if_null>";
677 }
678
679 if (dhd->iflist[ifidx]->net)
680 return dhd->iflist[ifidx]->net->name;
681
682 return "<if_none>";
683}
684
685static void _dhd_set_multicast_list(dhd_info_t *dhd, int ifidx)
686{
687 struct net_device *dev;
688 struct netdev_hw_addr *ha;
66cbd3ab 689 u32 allmulti, cnt;
cf2b4488
HP
690
691 wl_ioctl_t ioc;
692 char *buf, *bufp;
693 uint buflen;
694 int ret;
695
696 ASSERT(dhd && dhd->iflist[ifidx]);
697 dev = dhd->iflist[ifidx]->net;
698 cnt = netdev_mc_count(dev);
699
700 /* Determine initial value of allmulti flag */
0965ae88 701 allmulti = (dev->flags & IFF_ALLMULTI) ? true : false;
cf2b4488
HP
702
703 /* Send down the multicast list first. */
704
b8d63078 705 buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETH_ALEN);
5fcc1fcb 706 bufp = buf = kmalloc(buflen, GFP_ATOMIC);
a618cc28 707 if (!bufp) {
cf2b4488
HP
708 DHD_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
709 dhd_ifname(&dhd->pub, ifidx), cnt));
710 return;
711 }
712
713 strcpy(bufp, "mcast_list");
714 bufp += strlen("mcast_list") + 1;
715
716 cnt = htol32(cnt);
717 memcpy(bufp, &cnt, sizeof(cnt));
718 bufp += sizeof(cnt);
719
720 netdev_for_each_mc_addr(ha, dev) {
721 if (!cnt)
722 break;
b8d63078
JP
723 memcpy(bufp, ha->addr, ETH_ALEN);
724 bufp += ETH_ALEN;
cf2b4488
HP
725 cnt--;
726 }
727
728 memset(&ioc, 0, sizeof(ioc));
729 ioc.cmd = WLC_SET_VAR;
730 ioc.buf = buf;
731 ioc.len = buflen;
0f0881b0 732 ioc.set = true;
cf2b4488
HP
733
734 ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
735 if (ret < 0) {
736 DHD_ERROR(("%s: set mcast_list failed, cnt %d\n",
737 dhd_ifname(&dhd->pub, ifidx), cnt));
0f0881b0 738 allmulti = cnt ? true : allmulti;
cf2b4488
HP
739 }
740
182acb3c 741 kfree(buf);
cf2b4488
HP
742
743 /* Now send the allmulti setting. This is based on the setting in the
744 * net_device flags, but might be modified above to be turned on if we
745 * were trying to set some addresses and dongle rejected it...
746 */
747
748 buflen = sizeof("allmulti") + sizeof(allmulti);
5fcc1fcb 749 buf = kmalloc(buflen, GFP_ATOMIC);
a618cc28 750 if (!buf) {
cf2b4488
HP
751 DHD_ERROR(("%s: out of memory for allmulti\n",
752 dhd_ifname(&dhd->pub, ifidx)));
753 return;
754 }
755 allmulti = htol32(allmulti);
756
757 if (!bcm_mkiovar
758 ("allmulti", (void *)&allmulti, sizeof(allmulti), buf, buflen)) {
759 DHD_ERROR(("%s: mkiovar failed for allmulti, datalen %d "
760 "buflen %u\n", dhd_ifname(&dhd->pub, ifidx),
761 (int)sizeof(allmulti), buflen));
182acb3c 762 kfree(buf);
cf2b4488
HP
763 return;
764 }
765
766 memset(&ioc, 0, sizeof(ioc));
767 ioc.cmd = WLC_SET_VAR;
768 ioc.buf = buf;
769 ioc.len = buflen;
0f0881b0 770 ioc.set = true;
cf2b4488
HP
771
772 ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
773 if (ret < 0) {
774 DHD_ERROR(("%s: set allmulti %d failed\n",
775 dhd_ifname(&dhd->pub, ifidx), ltoh32(allmulti)));
776 }
777
182acb3c 778 kfree(buf);
cf2b4488
HP
779
780 /* Finally, pick up the PROMISC flag as well, like the NIC
781 driver does */
782
0965ae88 783 allmulti = (dev->flags & IFF_PROMISC) ? true : false;
cf2b4488
HP
784 allmulti = htol32(allmulti);
785
786 memset(&ioc, 0, sizeof(ioc));
787 ioc.cmd = WLC_SET_PROMISC;
788 ioc.buf = &allmulti;
789 ioc.len = sizeof(allmulti);
0f0881b0 790 ioc.set = true;
cf2b4488
HP
791
792 ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
793 if (ret < 0) {
794 DHD_ERROR(("%s: set promisc %d failed\n",
795 dhd_ifname(&dhd->pub, ifidx), ltoh32(allmulti)));
796 }
797}
798
799static int
a44d4236 800_dhd_set_mac_address(dhd_info_t *dhd, int ifidx, u8 *addr)
cf2b4488
HP
801{
802 char buf[32];
803 wl_ioctl_t ioc;
804 int ret;
805
806 DHD_TRACE(("%s enter\n", __func__));
807 if (!bcm_mkiovar
b8d63078 808 ("cur_etheraddr", (char *)addr, ETH_ALEN, buf, 32)) {
cf2b4488
HP
809 DHD_ERROR(("%s: mkiovar failed for cur_etheraddr\n",
810 dhd_ifname(&dhd->pub, ifidx)));
811 return -1;
812 }
813 memset(&ioc, 0, sizeof(ioc));
814 ioc.cmd = WLC_SET_VAR;
815 ioc.buf = buf;
816 ioc.len = 32;
0f0881b0 817 ioc.set = true;
cf2b4488
HP
818
819 ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
820 if (ret < 0) {
821 DHD_ERROR(("%s: set cur_etheraddr failed\n",
822 dhd_ifname(&dhd->pub, ifidx)));
823 } else {
b8d63078 824 memcpy(dhd->iflist[ifidx]->net->dev_addr, addr, ETH_ALEN);
cf2b4488
HP
825 }
826
827 return ret;
828}
829
830#ifdef SOFTAP
831extern struct net_device *ap_net_dev;
832#endif
833
834static void dhd_op_if(dhd_if_t *ifp)
835{
836 dhd_info_t *dhd;
837 int ret = 0, err = 0;
838
839 ASSERT(ifp && ifp->info && ifp->idx); /* Virtual interfaces only */
840
841 dhd = ifp->info;
842
843 DHD_TRACE(("%s: idx %d, state %d\n", __func__, ifp->idx, ifp->state));
844
845 switch (ifp->state) {
846 case WLC_E_IF_ADD:
847 /*
848 * Delete the existing interface before overwriting it
849 * in case we missed the WLC_E_IF_DEL event.
850 */
851 if (ifp->net != NULL) {
852 DHD_ERROR(("%s: ERROR: netdev:%s already exists, "
853 "try free & unregister\n",
854 __func__, ifp->net->name));
855 netif_stop_queue(ifp->net);
856 unregister_netdev(ifp->net);
857 free_netdev(ifp->net);
858 }
859 /* Allocate etherdev, including space for private structure */
a618cc28
JC
860 ifp->net = alloc_etherdev(sizeof(dhd));
861 if (!ifp->net) {
cf2b4488
HP
862 DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
863 ret = -ENOMEM;
864 }
865 if (ret == 0) {
866 strcpy(ifp->net->name, ifp->name);
867 memcpy(netdev_priv(ifp->net), &dhd, sizeof(dhd));
a618cc28
JC
868 err = dhd_net_attach(&dhd->pub, ifp->idx);
869 if (err != 0) {
cf2b4488
HP
870 DHD_ERROR(("%s: dhd_net_attach failed, "
871 "err %d\n",
872 __func__, err));
873 ret = -EOPNOTSUPP;
874 } else {
875#ifdef SOFTAP
876 /* semaphore that the soft AP CODE
877 waits on */
878 extern struct semaphore ap_eth_sema;
879
880 /* save ptr to wl0.1 netdev for use
881 in wl_iw.c */
882 ap_net_dev = ifp->net;
883 /* signal to the SOFTAP 'sleeper' thread,
884 wl0.1 is ready */
885 up(&ap_eth_sema);
886#endif
887 DHD_TRACE(("\n ==== pid:%x, net_device for "
888 "if:%s created ===\n\n",
889 current->pid, ifp->net->name));
890 ifp->state = 0;
891 }
892 }
893 break;
894 case WLC_E_IF_DEL:
895 if (ifp->net != NULL) {
896 DHD_TRACE(("\n%s: got 'WLC_E_IF_DEL' state\n",
897 __func__));
898 netif_stop_queue(ifp->net);
899 unregister_netdev(ifp->net);
900 ret = DHD_DEL_IF; /* Make sure the free_netdev()
901 is called */
902 }
903 break;
904 default:
905 DHD_ERROR(("%s: bad op %d\n", __func__, ifp->state));
906 ASSERT(!ifp->state);
907 break;
908 }
909
910 if (ret < 0) {
911 if (ifp->net)
912 free_netdev(ifp->net);
913
914 dhd->iflist[ifp->idx] = NULL;
182acb3c 915 kfree(ifp);
cf2b4488
HP
916#ifdef SOFTAP
917 if (ifp->net == ap_net_dev)
918 ap_net_dev = NULL; /* NULL SOFTAP global
919 wl0.1 as well */
920#endif /* SOFTAP */
921 }
922}
923
924static int _dhd_sysioc_thread(void *data)
925{
926 dhd_info_t *dhd = (dhd_info_t *) data;
927 int i;
928#ifdef SOFTAP
0965ae88 929 bool in_ap = false;
cf2b4488
HP
930#endif
931
af737136 932 allow_signal(SIGTERM);
933
cf2b4488 934 while (down_interruptible(&dhd->sysioc_sem) == 0) {
eeb8e46b 935 if (kthread_should_stop())
d809dcb9 936 break;
cf2b4488
HP
937 for (i = 0; i < DHD_MAX_IFS; i++) {
938 if (dhd->iflist[i]) {
939#ifdef SOFTAP
940 in_ap = (ap_net_dev != NULL);
941#endif /* SOFTAP */
942 if (dhd->iflist[i]->state)
943 dhd_op_if(dhd->iflist[i]);
944#ifdef SOFTAP
945 if (dhd->iflist[i] == NULL) {
946 DHD_TRACE(("\n\n %s: interface %d "
947 "removed!\n", __func__, i));
948 continue;
949 }
950
951 if (in_ap && dhd->set_macaddress) {
952 DHD_TRACE(("attempt to set MAC for %s "
953 "in AP Mode," "blocked. \n",
954 dhd->iflist[i]->net->name));
0965ae88 955 dhd->set_macaddress = false;
cf2b4488
HP
956 continue;
957 }
958
959 if (in_ap && dhd->set_multicast) {
960 DHD_TRACE(("attempt to set MULTICAST list for %s" "in AP Mode, blocked. \n",
961 dhd->iflist[i]->net->name));
0965ae88 962 dhd->set_multicast = false;
cf2b4488
HP
963 continue;
964 }
965#endif /* SOFTAP */
966 if (dhd->set_multicast) {
0965ae88 967 dhd->set_multicast = false;
cf2b4488
HP
968 _dhd_set_multicast_list(dhd, i);
969 }
970 if (dhd->set_macaddress) {
0965ae88 971 dhd->set_macaddress = false;
cf2b4488 972 _dhd_set_mac_address(dhd, i,
a44d4236 973 dhd->macvalue);
cf2b4488
HP
974 }
975 }
976 }
977 }
d809dcb9 978 return 0;
cf2b4488
HP
979}
980
981static int dhd_set_mac_address(struct net_device *dev, void *addr)
982{
983 int ret = 0;
984
985 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
986 struct sockaddr *sa = (struct sockaddr *)addr;
987 int ifidx;
988
989 ifidx = dhd_net2idx(dhd, dev);
990 if (ifidx == DHD_BAD_IF)
991 return -1;
992
d809dcb9 993 ASSERT(dhd->sysioc_tsk);
b8d63078 994 memcpy(&dhd->macvalue, sa->sa_data, ETH_ALEN);
0f0881b0 995 dhd->set_macaddress = true;
cf2b4488
HP
996 up(&dhd->sysioc_sem);
997
998 return ret;
999}
1000
1001static void dhd_set_multicast_list(struct net_device *dev)
1002{
1003 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
1004 int ifidx;
1005
1006 ifidx = dhd_net2idx(dhd, dev);
1007 if (ifidx == DHD_BAD_IF)
1008 return;
1009
d809dcb9 1010 ASSERT(dhd->sysioc_tsk);
0f0881b0 1011 dhd->set_multicast = true;
cf2b4488
HP
1012 up(&dhd->sysioc_sem);
1013}
1014
c26b1378 1015int dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, struct sk_buff *pktbuf)
cf2b4488
HP
1016{
1017 int ret;
1018 dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
1019
1020 /* Reject if down */
1021 if (!dhdp->up || (dhdp->busstate == DHD_BUS_DOWN))
1022 return -ENODEV;
1023
1024 /* Update multicast statistic */
b8d63078 1025 if (pktbuf->len >= ETH_ALEN) {
54991ad6 1026 u8 *pktdata = (u8 *) (pktbuf->data);
e2582ad8 1027 struct ethhdr *eh = (struct ethhdr *)pktdata;
cf2b4488 1028
e2582ad8 1029 if (is_multicast_ether_addr(eh->h_dest))
cf2b4488 1030 dhdp->tx_multicast++;
e2582ad8 1031 if (ntoh16(eh->h_proto) == ETH_P_PAE)
cf2b4488
HP
1032 atomic_inc(&dhd->pend_8021x_cnt);
1033 }
1034
cf2b4488
HP
1035 /* If the protocol uses a data header, apply it */
1036 dhd_prot_hdrpush(dhdp, ifidx, pktbuf);
1037
1038 /* Use bus module to send data frame */
1039#ifdef BCMDBUS
1040 ret = dbus_send_pkt(dhdp->dbus, pktbuf, NULL /* pktinfo */);
1041#else
cf2b4488
HP
1042 ret = dhd_bus_txdata(dhdp->bus, pktbuf);
1043#endif /* BCMDBUS */
1044
1045 return ret;
1046}
1047
411ee44a
BR
1048static inline void *
1049osl_pkt_frmnative(struct osl_info *osh, struct sk_buff *skb)
1050{
1051 struct sk_buff *nskb;
1052
1053 for (nskb = skb; nskb; nskb = nskb->next)
1054 osh->pktalloced++;
1055
1056 return (void *)skb;
1057}
1058#define PKTFRMNATIVE(osh, skb) \
1059 osl_pkt_frmnative((osh), (struct sk_buff *)(skb))
1060
1061static inline struct sk_buff *
1062osl_pkt_tonative(struct osl_info *osh, void *pkt)
1063{
1064 struct sk_buff *nskb;
1065
1066 for (nskb = (struct sk_buff *)pkt; nskb; nskb = nskb->next)
1067 osh->pktalloced--;
1068
1069 return (struct sk_buff *)pkt;
1070}
1071#define PKTTONATIVE(osh, pkt) \
1072 osl_pkt_tonative((osh), (pkt))
1073
cf2b4488
HP
1074static int dhd_start_xmit(struct sk_buff *skb, struct net_device *net)
1075{
1076 int ret;
1077 void *pktbuf;
1078 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1079 int ifidx;
1080
1081 DHD_TRACE(("%s: Enter\n", __func__));
1082
1083 /* Reject if down */
1084 if (!dhd->pub.up || (dhd->pub.busstate == DHD_BUS_DOWN)) {
1085 DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d\n",
1086 __func__, dhd->pub.up, dhd->pub.busstate));
1087 netif_stop_queue(net);
1088 return -ENODEV;
1089 }
1090
1091 ifidx = dhd_net2idx(dhd, net);
1092 if (ifidx == DHD_BAD_IF) {
1093 DHD_ERROR(("%s: bad ifidx %d\n", __func__, ifidx));
1094 netif_stop_queue(net);
1095 return -ENODEV;
1096 }
1097
1098 /* Make sure there's enough room for any header */
1099 if (skb_headroom(skb) < dhd->pub.hdrlen) {
1100 struct sk_buff *skb2;
1101
1102 DHD_INFO(("%s: insufficient headroom\n",
1103 dhd_ifname(&dhd->pub, ifidx)));
1104 dhd->pub.tx_realloc++;
1105 skb2 = skb_realloc_headroom(skb, dhd->pub.hdrlen);
1106 dev_kfree_skb(skb);
a618cc28
JC
1107 skb = skb2;
1108 if (skb == NULL) {
cf2b4488
HP
1109 DHD_ERROR(("%s: skb_realloc_headroom failed\n",
1110 dhd_ifname(&dhd->pub, ifidx)));
1111 ret = -ENOMEM;
1112 goto done;
1113 }
1114 }
1115
1116 /* Convert to packet */
a618cc28
JC
1117 pktbuf = PKTFRMNATIVE(dhd->pub.osh, skb);
1118 if (!pktbuf) {
cf2b4488
HP
1119 DHD_ERROR(("%s: PKTFRMNATIVE failed\n",
1120 dhd_ifname(&dhd->pub, ifidx)));
1121 dev_kfree_skb_any(skb);
1122 ret = -ENOMEM;
1123 goto done;
1124 }
1125
1126 ret = dhd_sendpkt(&dhd->pub, ifidx, pktbuf);
1127
1128done:
1129 if (ret)
1130 dhd->pub.dstats.tx_dropped++;
1131 else
1132 dhd->pub.tx_packets++;
1133
1134 /* Return ok: we always eat the packet */
1135 return 0;
1136}
1137
1138void dhd_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool state)
1139{
1140 struct net_device *net;
1141 dhd_info_t *dhd = dhdp->info;
1142
1143 DHD_TRACE(("%s: Enter\n", __func__));
1144
1145 dhdp->txoff = state;
1146 ASSERT(dhd && dhd->iflist[ifidx]);
1147 net = dhd->iflist[ifidx]->net;
1148 if (state == ON)
1149 netif_stop_queue(net);
1150 else
1151 netif_wake_queue(net);
1152}
1153
c26b1378
AS
1154void dhd_rx_frame(dhd_pub_t *dhdp, int ifidx, struct sk_buff *pktbuf,
1155 int numpkt)
cf2b4488
HP
1156{
1157 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1158 struct sk_buff *skb;
580a0bd9 1159 unsigned char *eth;
cf2b4488 1160 uint len;
c26b1378
AS
1161 void *data;
1162 struct sk_buff *pnext, *save_pktbuf;
cf2b4488
HP
1163 int i;
1164 dhd_if_t *ifp;
1165 wl_event_msg_t event;
1166
1167 DHD_TRACE(("%s: Enter\n", __func__));
1168
1169 save_pktbuf = pktbuf;
1170
1171 for (i = 0; pktbuf && i < numpkt; i++, pktbuf = pnext) {
1172
54991ad6
AS
1173 pnext = pktbuf->next;
1174 pktbuf->next = NULL;
cf2b4488
HP
1175
1176 skb = PKTTONATIVE(dhdp->osh, pktbuf);
1177
1178 /* Get the protocol, maintain skb around eth_type_trans()
1179 * The main reason for this hack is for the limitation of
1180 * Linux 2.4 where 'eth_type_trans' uses the
1181 * 'net->hard_header_len'
1182 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
1183 * coping of the packet coming from the network stack to add
1184 * BDC, Hardware header etc, during network interface
1185 * registration
1186 * we set the 'net->hard_header_len' to ETH_HLEN + extra space
1187 * required
1188 * for BDC, Hardware header etc. and not just the ETH_HLEN
1189 */
1190 eth = skb->data;
1191 len = skb->len;
1192
1193 ifp = dhd->iflist[ifidx];
1194 if (ifp == NULL)
1195 ifp = dhd->iflist[0];
1196
1197 ASSERT(ifp);
1198 skb->dev = ifp->net;
1199 skb->protocol = eth_type_trans(skb, skb->dev);
1200
1201 if (skb->pkt_type == PACKET_MULTICAST)
1202 dhd->pub.rx_multicast++;
1203
1204 skb->data = eth;
1205 skb->len = len;
1206
1207 /* Strip header, count, deliver upward */
1208 skb_pull(skb, ETH_HLEN);
1209
1210 /* Process special event packets and then discard them */
c09240ac 1211 if (ntoh16(skb->protocol) == ETH_P_BRCM)
cf2b4488 1212 dhd_wl_host_event(dhd, &ifidx,
d4fcdc68 1213 skb_mac_header(skb),
cf2b4488
HP
1214 &event, &data);
1215
1216 ASSERT(ifidx < DHD_MAX_IFS && dhd->iflist[ifidx]);
1217 if (dhd->iflist[ifidx] && !dhd->iflist[ifidx]->state)
1218 ifp = dhd->iflist[ifidx];
1219
1220 if (ifp->net)
1221 ifp->net->last_rx = jiffies;
1222
1223 dhdp->dstats.rx_bytes += skb->len;
1224 dhdp->rx_packets++; /* Local count */
1225
1226 if (in_interrupt()) {
1227 netif_rx(skb);
1228 } else {
1229 /* If the receive is not processed inside an ISR,
1230 * the softirqd must be woken explicitly to service
1231 * the NET_RX_SOFTIRQ. In 2.6 kernels, this is handled
1232 * by netif_rx_ni(), but in earlier kernels, we need
1233 * to do it manually.
1234 */
1235 netif_rx_ni(skb);
1236 }
1237 }
1238}
1239
1240void dhd_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx)
1241{
1242 /* Linux version has nothing to do */
1243 return;
1244}
1245
c26b1378 1246void dhd_txcomplete(dhd_pub_t *dhdp, struct sk_buff *txp, bool success)
cf2b4488
HP
1247{
1248 uint ifidx;
1249 dhd_info_t *dhd = (dhd_info_t *) (dhdp->info);
e2582ad8 1250 struct ethhdr *eh;
7d4df48e 1251 u16 type;
cf2b4488
HP
1252
1253 dhd_prot_hdrpull(dhdp, &ifidx, txp);
1254
e2582ad8
AS
1255 eh = (struct ethhdr *)(txp->data);
1256 type = ntoh16(eh->h_proto);
cf2b4488 1257
fcbdbed0 1258 if (type == ETH_P_PAE)
cf2b4488
HP
1259 atomic_dec(&dhd->pend_8021x_cnt);
1260
1261}
1262
1263static struct net_device_stats *dhd_get_stats(struct net_device *net)
1264{
1265 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1266 dhd_if_t *ifp;
1267 int ifidx;
1268
1269 DHD_TRACE(("%s: Enter\n", __func__));
1270
1271 ifidx = dhd_net2idx(dhd, net);
1272 if (ifidx == DHD_BAD_IF)
1273 return NULL;
1274
1275 ifp = dhd->iflist[ifidx];
1276 ASSERT(dhd && ifp);
1277
1278 if (dhd->pub.up) {
1279 /* Use the protocol to get dongle stats */
1280 dhd_prot_dstats(&dhd->pub);
1281 }
1282
1283 /* Copy dongle stats to net device stats */
1284 ifp->stats.rx_packets = dhd->pub.dstats.rx_packets;
1285 ifp->stats.tx_packets = dhd->pub.dstats.tx_packets;
1286 ifp->stats.rx_bytes = dhd->pub.dstats.rx_bytes;
1287 ifp->stats.tx_bytes = dhd->pub.dstats.tx_bytes;
1288 ifp->stats.rx_errors = dhd->pub.dstats.rx_errors;
1289 ifp->stats.tx_errors = dhd->pub.dstats.tx_errors;
1290 ifp->stats.rx_dropped = dhd->pub.dstats.rx_dropped;
1291 ifp->stats.tx_dropped = dhd->pub.dstats.tx_dropped;
1292 ifp->stats.multicast = dhd->pub.dstats.multicast;
1293
1294 return &ifp->stats;
1295}
1296
1297static int dhd_watchdog_thread(void *data)
1298{
1299 dhd_info_t *dhd = (dhd_info_t *) data;
cf2b4488
HP
1300
1301 /* This thread doesn't need any user-level access,
1302 * so get rid of all our resources
1303 */
1304#ifdef DHD_SCHED
1305 if (dhd_watchdog_prio > 0) {
1306 struct sched_param param;
1307 param.sched_priority = (dhd_watchdog_prio < MAX_RT_PRIO) ?
1308 dhd_watchdog_prio : (MAX_RT_PRIO - 1);
1309 setScheduler(current, SCHED_FIFO, &param);
1310 }
1311#endif /* DHD_SCHED */
1312
af737136 1313 allow_signal(SIGTERM);
cf2b4488
HP
1314 /* Run until signal received */
1315 while (1) {
860708d9
JC
1316 if (kthread_should_stop())
1317 break;
cf2b4488 1318 if (down_interruptible(&dhd->watchdog_sem) == 0) {
0965ae88 1319 if (dhd->pub.dongle_reset == false) {
cf2b4488
HP
1320 /* Call the bus module watchdog */
1321 dhd_bus_watchdog(&dhd->pub);
cf2b4488
HP
1322 }
1323 /* Count the tick for reference */
1324 dhd->pub.tickcnt++;
1325 } else
1326 break;
1327 }
860708d9 1328 return 0;
cf2b4488
HP
1329}
1330
3deea904 1331static void dhd_watchdog(unsigned long data)
cf2b4488
HP
1332{
1333 dhd_info_t *dhd = (dhd_info_t *) data;
1334
860708d9 1335 if (dhd->watchdog_tsk) {
cf2b4488
HP
1336 up(&dhd->watchdog_sem);
1337
1338 /* Reschedule the watchdog */
1339 if (dhd->wd_timer_valid) {
1340 mod_timer(&dhd->timer,
1341 jiffies + dhd_watchdog_ms * HZ / 1000);
1342 }
1343 return;
1344 }
1345
1346 /* Call the bus module watchdog */
1347 dhd_bus_watchdog(&dhd->pub);
1348
1349 /* Count the tick for reference */
1350 dhd->pub.tickcnt++;
1351
1352 /* Reschedule the watchdog */
1353 if (dhd->wd_timer_valid)
1354 mod_timer(&dhd->timer, jiffies + dhd_watchdog_ms * HZ / 1000);
1355}
1356
1357static int dhd_dpc_thread(void *data)
1358{
1359 dhd_info_t *dhd = (dhd_info_t *) data;
1360
cf2b4488
HP
1361 /* This thread doesn't need any user-level access,
1362 * so get rid of all our resources
1363 */
1364#ifdef DHD_SCHED
1365 if (dhd_dpc_prio > 0) {
1366 struct sched_param param;
1367 param.sched_priority =
1368 (dhd_dpc_prio <
1369 MAX_RT_PRIO) ? dhd_dpc_prio : (MAX_RT_PRIO - 1);
1370 setScheduler(current, SCHED_FIFO, &param);
1371 }
1372#endif /* DHD_SCHED */
1373
af737136 1374 allow_signal(SIGTERM);
cf2b4488
HP
1375 /* Run until signal received */
1376 while (1) {
eeb8e46b 1377 if (kthread_should_stop())
ecd7559d 1378 break;
cf2b4488
HP
1379 if (down_interruptible(&dhd->dpc_sem) == 0) {
1380 /* Call bus dpc unless it indicated down
1381 (then clean stop) */
1382 if (dhd->pub.busstate != DHD_BUS_DOWN) {
cf2b4488
HP
1383 if (dhd_bus_dpc(dhd->pub.bus)) {
1384 up(&dhd->dpc_sem);
cf2b4488 1385 }
cf2b4488 1386 } else {
0f0881b0 1387 dhd_bus_stop(dhd->pub.bus, true);
cf2b4488
HP
1388 }
1389 } else
1390 break;
1391 }
ecd7559d 1392 return 0;
cf2b4488
HP
1393}
1394
3deea904 1395static void dhd_dpc(unsigned long data)
cf2b4488
HP
1396{
1397 dhd_info_t *dhd;
1398
1399 dhd = (dhd_info_t *) data;
1400
1401 /* Call bus dpc unless it indicated down (then clean stop) */
1402 if (dhd->pub.busstate != DHD_BUS_DOWN) {
1403 if (dhd_bus_dpc(dhd->pub.bus))
1404 tasklet_schedule(&dhd->tasklet);
1405 } else {
0f0881b0 1406 dhd_bus_stop(dhd->pub.bus, true);
cf2b4488
HP
1407 }
1408}
1409
1410void dhd_sched_dpc(dhd_pub_t *dhdp)
1411{
1412 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
1413
ecd7559d 1414 if (dhd->dpc_tsk) {
cf2b4488
HP
1415 up(&dhd->dpc_sem);
1416 return;
1417 }
1418
1419 tasklet_schedule(&dhd->tasklet);
1420}
1421
1422#ifdef TOE
1423/* Retrieve current toe component enables, which are kept
1424 as a bitmap in toe_ol iovar */
66cbd3ab 1425static int dhd_toe_get(dhd_info_t *dhd, int ifidx, u32 *toe_ol)
cf2b4488
HP
1426{
1427 wl_ioctl_t ioc;
1428 char buf[32];
1429 int ret;
1430
1431 memset(&ioc, 0, sizeof(ioc));
1432
1433 ioc.cmd = WLC_GET_VAR;
1434 ioc.buf = buf;
1435 ioc.len = (uint) sizeof(buf);
0965ae88 1436 ioc.set = false;
cf2b4488
HP
1437
1438 strcpy(buf, "toe_ol");
a618cc28
JC
1439 ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1440 if (ret < 0) {
cf2b4488
HP
1441 /* Check for older dongle image that doesn't support toe_ol */
1442 if (ret == -EIO) {
1443 DHD_ERROR(("%s: toe not supported by device\n",
1444 dhd_ifname(&dhd->pub, ifidx)));
1445 return -EOPNOTSUPP;
1446 }
1447
1448 DHD_INFO(("%s: could not get toe_ol: ret=%d\n",
1449 dhd_ifname(&dhd->pub, ifidx), ret));
1450 return ret;
1451 }
1452
66cbd3ab 1453 memcpy(toe_ol, buf, sizeof(u32));
cf2b4488
HP
1454 return 0;
1455}
1456
1457/* Set current toe component enables in toe_ol iovar,
1458 and set toe global enable iovar */
66cbd3ab 1459static int dhd_toe_set(dhd_info_t *dhd, int ifidx, u32 toe_ol)
cf2b4488
HP
1460{
1461 wl_ioctl_t ioc;
1462 char buf[32];
1463 int toe, ret;
1464
1465 memset(&ioc, 0, sizeof(ioc));
1466
1467 ioc.cmd = WLC_SET_VAR;
1468 ioc.buf = buf;
1469 ioc.len = (uint) sizeof(buf);
0f0881b0 1470 ioc.set = true;
cf2b4488
HP
1471
1472 /* Set toe_ol as requested */
1473
1474 strcpy(buf, "toe_ol");
66cbd3ab 1475 memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(u32));
cf2b4488 1476
a618cc28
JC
1477 ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1478 if (ret < 0) {
cf2b4488
HP
1479 DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
1480 dhd_ifname(&dhd->pub, ifidx), ret));
1481 return ret;
1482 }
1483
1484 /* Enable toe globally only if any components are enabled. */
1485
1486 toe = (toe_ol != 0);
1487
1488 strcpy(buf, "toe");
66cbd3ab 1489 memcpy(&buf[sizeof("toe")], &toe, sizeof(u32));
cf2b4488 1490
a618cc28
JC
1491 ret = dhd_prot_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
1492 if (ret < 0) {
cf2b4488
HP
1493 DHD_ERROR(("%s: could not set toe: ret=%d\n",
1494 dhd_ifname(&dhd->pub, ifidx), ret));
1495 return ret;
1496 }
1497
1498 return 0;
1499}
1500#endif /* TOE */
1501
1502static void dhd_ethtool_get_drvinfo(struct net_device *net,
1503 struct ethtool_drvinfo *info)
1504{
1505 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1506
93ad12cf 1507 sprintf(info->driver, DRV_MODULE_NAME);
cf2b4488 1508 sprintf(info->version, "%lu", dhd->pub.drv_version);
93ad12cf 1509 sprintf(info->fw_version, "%s", wl_cfg80211_get_fwname());
1510 sprintf(info->bus_info, "%s", dev_name(&wl_cfg80211_get_sdio_func()->dev));
cf2b4488
HP
1511}
1512
1513struct ethtool_ops dhd_ethtool_ops = {
1514 .get_drvinfo = dhd_ethtool_get_drvinfo
1515};
1516
1517static int dhd_ethtool(dhd_info_t *dhd, void *uaddr)
1518{
1519 struct ethtool_drvinfo info;
1520 char drvname[sizeof(info.driver)];
66cbd3ab 1521 u32 cmd;
cf2b4488
HP
1522#ifdef TOE
1523 struct ethtool_value edata;
66cbd3ab 1524 u32 toe_cmpnt, csum_dir;
cf2b4488
HP
1525 int ret;
1526#endif
1527
1528 DHD_TRACE(("%s: Enter\n", __func__));
1529
1530 /* all ethtool calls start with a cmd word */
66cbd3ab 1531 if (copy_from_user(&cmd, uaddr, sizeof(u32)))
cf2b4488
HP
1532 return -EFAULT;
1533
1534 switch (cmd) {
1535 case ETHTOOL_GDRVINFO:
1536 /* Copy out any request driver name */
1537 if (copy_from_user(&info, uaddr, sizeof(info)))
1538 return -EFAULT;
1539 strncpy(drvname, info.driver, sizeof(info.driver));
1540 drvname[sizeof(info.driver) - 1] = '\0';
1541
1542 /* clear struct for return */
1543 memset(&info, 0, sizeof(info));
1544 info.cmd = cmd;
1545
1546 /* if dhd requested, identify ourselves */
1547 if (strcmp(drvname, "?dhd") == 0) {
1548 sprintf(info.driver, "dhd");
1549 strcpy(info.version, EPI_VERSION_STR);
1550 }
1551
1552 /* otherwise, require dongle to be up */
1553 else if (!dhd->pub.up) {
1554 DHD_ERROR(("%s: dongle is not up\n", __func__));
1555 return -ENODEV;
1556 }
1557
1558 /* finally, report dongle driver type */
1559 else if (dhd->pub.iswl)
1560 sprintf(info.driver, "wl");
1561 else
1562 sprintf(info.driver, "xx");
1563
1564 sprintf(info.version, "%lu", dhd->pub.drv_version);
1565 if (copy_to_user(uaddr, &info, sizeof(info)))
1566 return -EFAULT;
1567 DHD_CTL(("%s: given %*s, returning %s\n", __func__,
1568 (int)sizeof(drvname), drvname, info.driver));
1569 break;
1570
1571#ifdef TOE
1572 /* Get toe offload components from dongle */
1573 case ETHTOOL_GRXCSUM:
1574 case ETHTOOL_GTXCSUM:
a618cc28
JC
1575 ret = dhd_toe_get(dhd, 0, &toe_cmpnt);
1576 if (ret < 0)
cf2b4488
HP
1577 return ret;
1578
1579 csum_dir =
1580 (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1581
1582 edata.cmd = cmd;
1583 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
1584
1585 if (copy_to_user(uaddr, &edata, sizeof(edata)))
1586 return -EFAULT;
1587 break;
1588
1589 /* Set toe offload components in dongle */
1590 case ETHTOOL_SRXCSUM:
1591 case ETHTOOL_STXCSUM:
1592 if (copy_from_user(&edata, uaddr, sizeof(edata)))
1593 return -EFAULT;
1594
1595 /* Read the current settings, update and write back */
a618cc28
JC
1596 ret = dhd_toe_get(dhd, 0, &toe_cmpnt);
1597 if (ret < 0)
cf2b4488
HP
1598 return ret;
1599
1600 csum_dir =
1601 (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1602
1603 if (edata.data != 0)
1604 toe_cmpnt |= csum_dir;
1605 else
1606 toe_cmpnt &= ~csum_dir;
1607
a618cc28
JC
1608 ret = dhd_toe_set(dhd, 0, toe_cmpnt);
1609 if (ret < 0)
cf2b4488
HP
1610 return ret;
1611
1612 /* If setting TX checksum mode, tell Linux the new mode */
1613 if (cmd == ETHTOOL_STXCSUM) {
1614 if (edata.data)
1615 dhd->iflist[0]->net->features |=
1616 NETIF_F_IP_CSUM;
1617 else
1618 dhd->iflist[0]->net->features &=
1619 ~NETIF_F_IP_CSUM;
1620 }
1621
1622 break;
1623#endif /* TOE */
1624
1625 default:
1626 return -EOPNOTSUPP;
1627 }
1628
1629 return 0;
1630}
1631
9014378b
BR
1632static s16 linuxbcmerrormap[] = { 0, /* 0 */
1633 -EINVAL, /* BCME_ERROR */
1634 -EINVAL, /* BCME_BADARG */
1635 -EINVAL, /* BCME_BADOPTION */
1636 -EINVAL, /* BCME_NOTUP */
1637 -EINVAL, /* BCME_NOTDOWN */
1638 -EINVAL, /* BCME_NOTAP */
1639 -EINVAL, /* BCME_NOTSTA */
1640 -EINVAL, /* BCME_BADKEYIDX */
1641 -EINVAL, /* BCME_RADIOOFF */
1642 -EINVAL, /* BCME_NOTBANDLOCKED */
1643 -EINVAL, /* BCME_NOCLK */
1644 -EINVAL, /* BCME_BADRATESET */
1645 -EINVAL, /* BCME_BADBAND */
1646 -E2BIG, /* BCME_BUFTOOSHORT */
1647 -E2BIG, /* BCME_BUFTOOLONG */
1648 -EBUSY, /* BCME_BUSY */
1649 -EINVAL, /* BCME_NOTASSOCIATED */
1650 -EINVAL, /* BCME_BADSSIDLEN */
1651 -EINVAL, /* BCME_OUTOFRANGECHAN */
1652 -EINVAL, /* BCME_BADCHAN */
1653 -EFAULT, /* BCME_BADADDR */
1654 -ENOMEM, /* BCME_NORESOURCE */
1655 -EOPNOTSUPP, /* BCME_UNSUPPORTED */
1656 -EMSGSIZE, /* BCME_BADLENGTH */
1657 -EINVAL, /* BCME_NOTREADY */
1658 -EPERM, /* BCME_NOTPERMITTED */
1659 -ENOMEM, /* BCME_NOMEM */
1660 -EINVAL, /* BCME_ASSOCIATED */
1661 -ERANGE, /* BCME_RANGE */
1662 -EINVAL, /* BCME_NOTFOUND */
1663 -EINVAL, /* BCME_WME_NOT_ENABLED */
1664 -EINVAL, /* BCME_TSPEC_NOTFOUND */
1665 -EINVAL, /* BCME_ACM_NOTSUPPORTED */
1666 -EINVAL, /* BCME_NOT_WME_ASSOCIATION */
1667 -EIO, /* BCME_SDIO_ERROR */
1668 -ENODEV, /* BCME_DONGLE_DOWN */
1669 -EINVAL, /* BCME_VERSION */
1670 -EIO, /* BCME_TXFAIL */
1671 -EIO, /* BCME_RXFAIL */
1672 -EINVAL, /* BCME_NODEVICE */
1673 -EINVAL, /* BCME_NMODE_DISABLED */
1674 -ENODATA, /* BCME_NONRESIDENT */
1675};
1676
cf2b4488
HP
1677static int dhd_ioctl_entry(struct net_device *net, struct ifreq *ifr, int cmd)
1678{
1679 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1680 dhd_ioctl_t ioc;
1681 int bcmerror = 0;
1682 int buflen = 0;
1683 void *buf = NULL;
1684 uint driver = 0;
1685 int ifidx;
1686 bool is_set_key_cmd;
1687
1688 ifidx = dhd_net2idx(dhd, net);
1689 DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __func__, ifidx, cmd));
1690
1691 if (ifidx == DHD_BAD_IF)
1692 return -1;
1693
1694#if defined(CONFIG_WIRELESS_EXT)
1695 /* linux wireless extensions */
1696 if ((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST)) {
1697 /* may recurse, do NOT lock */
1698 return wl_iw_ioctl(net, ifr, cmd);
1699 }
1700#endif /* defined(CONFIG_WIRELESS_EXT) */
1701
1702 if (cmd == SIOCETHTOOL)
1703 return dhd_ethtool(dhd, (void *)ifr->ifr_data);
1704
1705 if (cmd != SIOCDEVPRIVATE)
1706 return -EOPNOTSUPP;
1707
1708 memset(&ioc, 0, sizeof(ioc));
1709
1710 /* Copy the ioc control structure part of ioctl request */
1711 if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
1712 bcmerror = -BCME_BADADDR;
1713 goto done;
1714 }
1715
1716 /* Copy out any buffer passed */
1717 if (ioc.buf) {
53e974db 1718 buflen = min_t(int, ioc.len, DHD_IOCTL_MAXLEN);
cf2b4488
HP
1719 /* optimization for direct ioctl calls from kernel */
1720 /*
1721 if (segment_eq(get_fs(), KERNEL_DS)) {
1722 buf = ioc.buf;
1723 } else {
1724 */
1725 {
5fcc1fcb 1726 buf = kmalloc(buflen, GFP_ATOMIC);
a618cc28 1727 if (!buf) {
cf2b4488
HP
1728 bcmerror = -BCME_NOMEM;
1729 goto done;
1730 }
1731 if (copy_from_user(buf, ioc.buf, buflen)) {
1732 bcmerror = -BCME_BADADDR;
1733 goto done;
1734 }
1735 }
1736 }
1737
1738 /* To differentiate between wl and dhd read 4 more byes */
1739 if ((copy_from_user(&driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
1740 sizeof(uint)) != 0)) {
1741 bcmerror = -BCME_BADADDR;
1742 goto done;
1743 }
1744
1745 if (!capable(CAP_NET_ADMIN)) {
1746 bcmerror = -BCME_EPERM;
1747 goto done;
1748 }
1749
1750 /* check for local dhd ioctl and handle it */
1751 if (driver == DHD_IOCTL_MAGIC) {
1752 bcmerror = dhd_ioctl((void *)&dhd->pub, &ioc, buf, buflen);
1753 if (bcmerror)
1754 dhd->pub.bcmerror = bcmerror;
1755 goto done;
1756 }
1757
1758 /* send to dongle (must be up, and wl) */
1759 if ((dhd->pub.busstate != DHD_BUS_DATA)) {
1760 DHD_ERROR(("%s DONGLE_DOWN,__func__\n", __func__));
1761 bcmerror = BCME_DONGLE_DOWN;
1762 goto done;
1763 }
1764
1765 if (!dhd->pub.iswl) {
1766 bcmerror = BCME_DONGLE_DOWN;
1767 goto done;
1768 }
1769
1770 /* Intercept WLC_SET_KEY IOCTL - serialize M4 send and set key IOCTL to
1771 * prevent M4 encryption.
1772 */
1773 is_set_key_cmd = ((ioc.cmd == WLC_SET_KEY) ||
1774 ((ioc.cmd == WLC_SET_VAR) &&
1775 !(strncmp("wsec_key", ioc.buf, 9))) ||
1776 ((ioc.cmd == WLC_SET_VAR) &&
1777 !(strncmp("bsscfg:wsec_key", ioc.buf, 15))));
1778 if (is_set_key_cmd)
1779 dhd_wait_pend8021x(net);
1780
cf2b4488
HP
1781 bcmerror =
1782 dhd_prot_ioctl(&dhd->pub, ifidx, (wl_ioctl_t *)&ioc, buf, buflen);
1783
cf2b4488
HP
1784done:
1785 if (!bcmerror && buf && ioc.buf) {
1786 if (copy_to_user(ioc.buf, buf, buflen))
1787 bcmerror = -EFAULT;
1788 }
1789
1790 if (buf)
182acb3c 1791 kfree(buf);
cf2b4488 1792
9014378b
BR
1793 if (bcmerror > 0)
1794 bcmerror = 0;
1795 else if (bcmerror < BCME_LAST)
1796 bcmerror = BCME_ERROR;
1797
1798 return linuxbcmerrormap[-bcmerror];
cf2b4488
HP
1799}
1800
1801static int dhd_stop(struct net_device *net)
1802{
1803#if !defined(IGNORE_ETH0_DOWN)
1804 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1805
1806 DHD_TRACE(("%s: Enter\n", __func__));
cf2b4488
HP
1807 if (IS_CFG80211_FAVORITE()) {
1808 wl_cfg80211_down();
1809 }
cf2b4488
HP
1810 if (dhd->pub.up == 0)
1811 return 0;
1812
1813 /* Set state and stop OS transmissions */
1814 dhd->pub.up = 0;
1815 netif_stop_queue(net);
1816#else
1817 DHD_ERROR(("BYPASS %s:due to BRCM compilation : under investigation\n",
1818 __func__));
1819#endif /* !defined(IGNORE_ETH0_DOWN) */
1820
cf2b4488
HP
1821 return 0;
1822}
1823
1824static int dhd_open(struct net_device *net)
1825{
1826 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(net);
1827#ifdef TOE
66cbd3ab 1828 u32 toe_ol;
cf2b4488
HP
1829#endif
1830 int ifidx = dhd_net2idx(dhd, net);
3e26416e 1831 s32 ret = 0;
cf2b4488
HP
1832
1833 DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
1834
1835 if (ifidx == 0) { /* do it only for primary eth0 */
1836
1837 /* try to bring up bus */
a618cc28
JC
1838 ret = dhd_bus_start(&dhd->pub);
1839 if (ret != 0) {
cf2b4488
HP
1840 DHD_ERROR(("%s: failed with code %d\n", __func__, ret));
1841 return -1;
1842 }
1843 atomic_set(&dhd->pend_8021x_cnt, 0);
1844
a44d4236 1845 memcpy(net->dev_addr, dhd->pub.mac, ETH_ALEN);
cf2b4488
HP
1846
1847#ifdef TOE
1848 /* Get current TOE mode from dongle */
1849 if (dhd_toe_get(dhd, ifidx, &toe_ol) >= 0
1850 && (toe_ol & TOE_TX_CSUM_OL) != 0)
1851 dhd->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
1852 else
1853 dhd->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
1854#endif
1855 }
1856 /* Allow transmit calls */
1857 netif_start_queue(net);
1858 dhd->pub.up = 1;
cf2b4488
HP
1859 if (IS_CFG80211_FAVORITE()) {
1860 if (unlikely(wl_cfg80211_up())) {
1861 DHD_ERROR(("%s: failed to bring up cfg80211\n",
1862 __func__));
1863 return -1;
1864 }
1865 }
cf2b4488 1866
cf2b4488
HP
1867 return ret;
1868}
1869
e69284f2 1870struct osl_info *dhd_osl_attach(void *pdev, uint bustype)
cf2b4488 1871{
997dd24f 1872 return osl_attach(pdev, bustype);
cf2b4488
HP
1873}
1874
e69284f2 1875void dhd_osl_detach(struct osl_info *osh)
cf2b4488 1876{
cf2b4488
HP
1877 osl_detach(osh);
1878}
1879
1880int
1881dhd_add_if(dhd_info_t *dhd, int ifidx, void *handle, char *name,
66cbd3ab 1882 u8 *mac_addr, u32 flags, u8 bssidx)
cf2b4488
HP
1883{
1884 dhd_if_t *ifp;
1885
1886 DHD_TRACE(("%s: idx %d, handle->%p\n", __func__, ifidx, handle));
1887
1888 ASSERT(dhd && (ifidx < DHD_MAX_IFS));
1889
1890 ifp = dhd->iflist[ifidx];
5fcc1fcb 1891 if (!ifp && !(ifp = kmalloc(sizeof(dhd_if_t), GFP_ATOMIC))) {
cf2b4488
HP
1892 DHD_ERROR(("%s: OOM - dhd_if_t\n", __func__));
1893 return -ENOMEM;
1894 }
1895
1896 memset(ifp, 0, sizeof(dhd_if_t));
1897 ifp->info = dhd;
1898 dhd->iflist[ifidx] = ifp;
cbf6baac 1899 strlcpy(ifp->name, name, IFNAMSIZ);
cf2b4488 1900 if (mac_addr != NULL)
b8d63078 1901 memcpy(&ifp->mac_addr, mac_addr, ETH_ALEN);
cf2b4488
HP
1902
1903 if (handle == NULL) {
1904 ifp->state = WLC_E_IF_ADD;
1905 ifp->idx = ifidx;
d809dcb9 1906 ASSERT(dhd->sysioc_tsk);
cf2b4488
HP
1907 up(&dhd->sysioc_sem);
1908 } else
1909 ifp->net = (struct net_device *)handle;
1910
1911 return 0;
1912}
1913
1914void dhd_del_if(dhd_info_t *dhd, int ifidx)
1915{
1916 dhd_if_t *ifp;
1917
1918 DHD_TRACE(("%s: idx %d\n", __func__, ifidx));
1919
1920 ASSERT(dhd && ifidx && (ifidx < DHD_MAX_IFS));
1921 ifp = dhd->iflist[ifidx];
1922 if (!ifp) {
1923 DHD_ERROR(("%s: Null interface\n", __func__));
1924 return;
1925 }
1926
1927 ifp->state = WLC_E_IF_DEL;
1928 ifp->idx = ifidx;
d809dcb9 1929 ASSERT(dhd->sysioc_tsk);
cf2b4488
HP
1930 up(&dhd->sysioc_sem);
1931}
1932
e69284f2
BR
1933dhd_pub_t *dhd_attach(struct osl_info *osh, struct dhd_bus *bus,
1934 uint bus_hdrlen)
cf2b4488
HP
1935{
1936 dhd_info_t *dhd = NULL;
1937 struct net_device *net;
1938
1939 DHD_TRACE(("%s: Enter\n", __func__));
1940 /* updates firmware nvram path if it was provided as module
1941 paramters */
1942 if ((firmware_path != NULL) && (firmware_path[0] != '\0'))
1943 strcpy(fw_path, firmware_path);
1944 if ((nvram_path != NULL) && (nvram_path[0] != '\0'))
1945 strcpy(nv_path, nvram_path);
1946
1947 /* Allocate etherdev, including space for private structure */
a618cc28
JC
1948 net = alloc_etherdev(sizeof(dhd));
1949 if (!net) {
cf2b4488
HP
1950 DHD_ERROR(("%s: OOM - alloc_etherdev\n", __func__));
1951 goto fail;
1952 }
1953
1954 /* Allocate primary dhd_info */
5fcc1fcb 1955 dhd = kmalloc(sizeof(dhd_info_t), GFP_ATOMIC);
a618cc28 1956 if (!dhd) {
cf2b4488
HP
1957 DHD_ERROR(("%s: OOM - alloc dhd_info\n", __func__));
1958 goto fail;
1959 }
1960
1961 memset(dhd, 0, sizeof(dhd_info_t));
1962
1963 /*
1964 * Save the dhd_info into the priv
1965 */
1966 memcpy(netdev_priv(net), &dhd, sizeof(dhd));
1967 dhd->pub.osh = osh;
1968
1969 /* Set network interface name if it was provided as module parameter */
1970 if (iface_name[0]) {
1971 int len;
1972 char ch;
1973 strncpy(net->name, iface_name, IFNAMSIZ);
1974 net->name[IFNAMSIZ - 1] = 0;
1975 len = strlen(net->name);
1976 ch = net->name[len - 1];
1977 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
1978 strcat(net->name, "%d");
1979 }
1980
1981 if (dhd_add_if(dhd, 0, (void *)net, net->name, NULL, 0, 0) ==
1982 DHD_BAD_IF)
1983 goto fail;
1984
1985 net->netdev_ops = NULL;
45f4d024 1986 sema_init(&dhd->proto_sem, 1);
cf2b4488
HP
1987 /* Initialize other structure content */
1988 init_waitqueue_head(&dhd->ioctl_resp_wait);
1989 init_waitqueue_head(&dhd->ctrl_wait);
1990
1991 /* Initialize the spinlocks */
1992 spin_lock_init(&dhd->sdlock);
1993 spin_lock_init(&dhd->txqlock);
1994
1995 /* Link to info module */
1996 dhd->pub.info = dhd;
1997
1998 /* Link to bus module */
1999 dhd->pub.bus = bus;
2000 dhd->pub.hdrlen = bus_hdrlen;
2001
2002 /* Attach and link in the protocol */
2003 if (dhd_prot_attach(&dhd->pub) != 0) {
2004 DHD_ERROR(("dhd_prot_attach failed\n"));
2005 goto fail;
2006 }
2007#if defined(CONFIG_WIRELESS_EXT)
2008 /* Attach and link in the iw */
2009 if (wl_iw_attach(net, (void *)&dhd->pub) != 0) {
2010 DHD_ERROR(("wl_iw_attach failed\n"));
2011 goto fail;
2012 }
1e8dd5b9 2013#endif /* defined(CONFIG_WIRELESS_EXT) */
cf2b4488 2014
cf2b4488
HP
2015 /* Attach and link in the cfg80211 */
2016 if (IS_CFG80211_FAVORITE()) {
2017 if (unlikely(wl_cfg80211_attach(net, &dhd->pub))) {
2018 DHD_ERROR(("wl_cfg80211_attach failed\n"));
2019 goto fail;
2020 }
2021 if (!NO_FW_REQ()) {
2022 strcpy(fw_path, wl_cfg80211_get_fwname());
2023 strcpy(nv_path, wl_cfg80211_get_nvramname());
2024 }
2025 wl_cfg80211_dbg_level(DBG_CFG80211_GET());
2026 }
cf2b4488
HP
2027
2028 /* Set up the watchdog timer */
2029 init_timer(&dhd->timer);
3deea904 2030 dhd->timer.data = (unsigned long) dhd;
cf2b4488
HP
2031 dhd->timer.function = dhd_watchdog;
2032
2033 /* Initialize thread based operation and lock */
45f4d024 2034 sema_init(&dhd->sdsem, 1);
cf2b4488 2035 if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0))
0f0881b0 2036 dhd->threads_only = true;
cf2b4488 2037 else
0965ae88 2038 dhd->threads_only = false;
cf2b4488
HP
2039
2040 if (dhd_dpc_prio >= 0) {
2041 /* Initialize watchdog thread */
2042 sema_init(&dhd->watchdog_sem, 0);
860708d9
JC
2043 dhd->watchdog_tsk = kthread_run(dhd_watchdog_thread, dhd,
2044 "dhd_watchdog");
2045 if (IS_ERR(dhd->watchdog_tsk)) {
2046 printk(KERN_WARNING
2047 "dhd_watchdog thread failed to start\n");
2048 dhd->watchdog_tsk = NULL;
2049 }
cf2b4488 2050 } else {
860708d9 2051 dhd->watchdog_tsk = NULL;
cf2b4488
HP
2052 }
2053
2054 /* Set up the bottom half handler */
2055 if (dhd_dpc_prio >= 0) {
2056 /* Initialize DPC thread */
2057 sema_init(&dhd->dpc_sem, 0);
ecd7559d
JC
2058 dhd->dpc_tsk = kthread_run(dhd_dpc_thread, dhd, "dhd_dpc");
2059 if (IS_ERR(dhd->dpc_tsk)) {
2060 printk(KERN_WARNING
2061 "dhd_dpc thread failed to start\n");
2062 dhd->dpc_tsk = NULL;
2063 }
cf2b4488 2064 } else {
3deea904 2065 tasklet_init(&dhd->tasklet, dhd_dpc, (unsigned long) dhd);
ecd7559d 2066 dhd->dpc_tsk = NULL;
cf2b4488
HP
2067 }
2068
2069 if (dhd_sysioc) {
2070 sema_init(&dhd->sysioc_sem, 0);
d809dcb9
JC
2071 dhd->sysioc_tsk = kthread_run(_dhd_sysioc_thread, dhd,
2072 "_dhd_sysioc");
2073 if (IS_ERR(dhd->sysioc_tsk)) {
2074 printk(KERN_WARNING
2075 "_dhd_sysioc thread failed to start\n");
2076 dhd->sysioc_tsk = NULL;
2077 }
2078 } else
2079 dhd->sysioc_tsk = NULL;
cf2b4488
HP
2080
2081 /*
2082 * Save the dhd_info into the priv
2083 */
2084 memcpy(netdev_priv(net), &dhd, sizeof(dhd));
2085
2086#if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2087 g_bus = bus;
2088#endif
2089#if defined(CONFIG_PM_SLEEP)
2090 register_pm_notifier(&dhd_sleep_pm_notifier);
2091#endif /* defined(CONFIG_PM_SLEEP) */
2092 /* && defined(DHD_GPL) */
2093 /* Init lock suspend to prevent kernel going to suspend */
cf2b4488
HP
2094#ifdef CONFIG_HAS_EARLYSUSPEND
2095 dhd->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 20;
2096 dhd->early_suspend.suspend = dhd_early_suspend;
2097 dhd->early_suspend.resume = dhd_late_resume;
2098 register_early_suspend(&dhd->early_suspend);
2099#endif
2100
2101 return &dhd->pub;
2102
2103fail:
2104 if (net)
2105 free_netdev(net);
2106 if (dhd)
2107 dhd_detach(&dhd->pub);
2108
2109 return NULL;
2110}
2111
2112int dhd_bus_start(dhd_pub_t *dhdp)
2113{
2114 int ret = -1;
2115 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
2116#ifdef EMBEDDED_PLATFORM
2117 char iovbuf[WL_EVENTING_MASK_LEN + 12]; /* Room for "event_msgs" +
2118 '\0' + bitvec */
2119#endif /* EMBEDDED_PLATFORM */
2120
2121 ASSERT(dhd);
2122
2123 DHD_TRACE(("%s:\n", __func__));
2124
2125 /* try to download image and nvram to the dongle */
2126 if (dhd->pub.busstate == DHD_BUS_DOWN) {
cf2b4488
HP
2127 if (!(dhd_bus_download_firmware(dhd->pub.bus, dhd->pub.osh,
2128 fw_path, nv_path))) {
2129 DHD_ERROR(("%s: dhdsdio_probe_download failed. "
2130 "firmware = %s nvram = %s\n",
2131 __func__, fw_path, nv_path));
cf2b4488
HP
2132 return -1;
2133 }
cf2b4488
HP
2134 }
2135
2136 /* Start the watchdog timer */
2137 dhd->pub.tickcnt = 0;
2138 dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
2139
2140 /* Bring up the bus */
0f0881b0 2141 ret = dhd_bus_init(&dhd->pub, true);
a618cc28 2142 if (ret != 0) {
cf2b4488
HP
2143 DHD_ERROR(("%s, dhd_bus_init failed %d\n", __func__, ret));
2144 return ret;
2145 }
2146#if defined(OOB_INTR_ONLY)
2147 /* Host registration for OOB interrupt */
2148 if (bcmsdh_register_oob_intr(dhdp)) {
2149 del_timer_sync(&dhd->timer);
0965ae88 2150 dhd->wd_timer_valid = false;
cf2b4488
HP
2151 DHD_ERROR(("%s Host failed to resgister for OOB\n", __func__));
2152 return -ENODEV;
2153 }
2154
2155 /* Enable oob at firmware */
0f0881b0 2156 dhd_enable_oob_intr(dhd->pub.bus, true);
cf2b4488
HP
2157#endif /* defined(OOB_INTR_ONLY) */
2158
2159 /* If bus is not ready, can't come up */
2160 if (dhd->pub.busstate != DHD_BUS_DATA) {
2161 del_timer_sync(&dhd->timer);
0965ae88 2162 dhd->wd_timer_valid = false;
cf2b4488
HP
2163 DHD_ERROR(("%s failed bus is not ready\n", __func__));
2164 return -ENODEV;
2165 }
2166#ifdef EMBEDDED_PLATFORM
2167 bcm_mkiovar("event_msgs", dhdp->eventmask, WL_EVENTING_MASK_LEN, iovbuf,
2168 sizeof(iovbuf));
2169 dhdcdc_query_ioctl(dhdp, 0, WLC_GET_VAR, iovbuf, sizeof(iovbuf));
2170 bcopy(iovbuf, dhdp->eventmask, WL_EVENTING_MASK_LEN);
2171
2172 setbit(dhdp->eventmask, WLC_E_SET_SSID);
2173 setbit(dhdp->eventmask, WLC_E_PRUNE);
2174 setbit(dhdp->eventmask, WLC_E_AUTH);
2175 setbit(dhdp->eventmask, WLC_E_REASSOC);
2176 setbit(dhdp->eventmask, WLC_E_REASSOC_IND);
2177 setbit(dhdp->eventmask, WLC_E_DEAUTH_IND);
2178 setbit(dhdp->eventmask, WLC_E_DISASSOC_IND);
2179 setbit(dhdp->eventmask, WLC_E_DISASSOC);
2180 setbit(dhdp->eventmask, WLC_E_JOIN);
2181 setbit(dhdp->eventmask, WLC_E_ASSOC_IND);
2182 setbit(dhdp->eventmask, WLC_E_PSK_SUP);
2183 setbit(dhdp->eventmask, WLC_E_LINK);
2184 setbit(dhdp->eventmask, WLC_E_NDIS_LINK);
2185 setbit(dhdp->eventmask, WLC_E_MIC_ERROR);
2186 setbit(dhdp->eventmask, WLC_E_PMKID_CACHE);
2187 setbit(dhdp->eventmask, WLC_E_TXFAIL);
2188 setbit(dhdp->eventmask, WLC_E_JOIN_START);
2189 setbit(dhdp->eventmask, WLC_E_SCAN_COMPLETE);
2190#ifdef PNO_SUPPORT
2191 setbit(dhdp->eventmask, WLC_E_PFN_NET_FOUND);
2192#endif /* PNO_SUPPORT */
2193
2194/* enable dongle roaming event */
2195
2196 dhdp->pktfilter_count = 1;
2197 /* Setup filter to allow only unicast */
2198 dhdp->pktfilter[0] = "100 0 0 0 0x01 0x00";
2199#endif /* EMBEDDED_PLATFORM */
2200
2201 /* Bus is ready, do any protocol initialization */
a618cc28
JC
2202 ret = dhd_prot_init(&dhd->pub);
2203 if (ret < 0)
cf2b4488
HP
2204 return ret;
2205
2206 return 0;
2207}
2208
2209int
2210dhd_iovar(dhd_pub_t *pub, int ifidx, char *name, char *cmd_buf, uint cmd_len,
2211 int set)
2212{
2213 char buf[strlen(name) + 1 + cmd_len];
2214 int len = sizeof(buf);
2215 wl_ioctl_t ioc;
2216 int ret;
2217
2218 len = bcm_mkiovar(name, cmd_buf, cmd_len, buf, len);
2219
2220 memset(&ioc, 0, sizeof(ioc));
2221
2222 ioc.cmd = set ? WLC_SET_VAR : WLC_GET_VAR;
2223 ioc.buf = buf;
2224 ioc.len = len;
2225 ioc.set = set;
2226
2227 ret = dhd_prot_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
2228 if (!set && ret >= 0)
2229 memcpy(cmd_buf, buf, cmd_len);
2230
2231 return ret;
2232}
2233
2234static struct net_device_ops dhd_ops_pri = {
2235 .ndo_open = dhd_open,
2236 .ndo_stop = dhd_stop,
2237 .ndo_get_stats = dhd_get_stats,
2238 .ndo_do_ioctl = dhd_ioctl_entry,
2239 .ndo_start_xmit = dhd_start_xmit,
2240 .ndo_set_mac_address = dhd_set_mac_address,
2241 .ndo_set_multicast_list = dhd_set_multicast_list
2242};
2243
cf2b4488
HP
2244int dhd_net_attach(dhd_pub_t *dhdp, int ifidx)
2245{
2246 dhd_info_t *dhd = (dhd_info_t *) dhdp->info;
2247 struct net_device *net;
b8d63078 2248 u8 temp_addr[ETH_ALEN] = {
cf2b4488
HP
2249 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33};
2250
2251 DHD_TRACE(("%s: ifidx %d\n", __func__, ifidx));
2252
2253 ASSERT(dhd && dhd->iflist[ifidx]);
2254
2255 net = dhd->iflist[ifidx]->net;
2256 ASSERT(net);
2257
2258 ASSERT(!net->netdev_ops);
cf2b4488
HP
2259 net->netdev_ops = &dhd_ops_pri;
2260
2261 /*
2262 * We have to use the primary MAC for virtual interfaces
2263 */
2264 if (ifidx != 0) {
2265 /* for virtual interfaces use the primary MAC */
a44d4236 2266 memcpy(temp_addr, dhd->pub.mac, ETH_ALEN);
cf2b4488
HP
2267
2268 }
2269
2270 if (ifidx == 1) {
2271 DHD_TRACE(("%s ACCESS POINT MAC: \n", __func__));
2272 /* ACCESSPOINT INTERFACE CASE */
2273 temp_addr[0] |= 0X02; /* set bit 2 ,
2274 - Locally Administered address */
2275
2276 }
2277 net->hard_header_len = ETH_HLEN + dhd->pub.hdrlen;
2278 net->ethtool_ops = &dhd_ethtool_ops;
2279
2280#if defined(CONFIG_WIRELESS_EXT)
cf2b4488 2281 if (!IS_CFG80211_FAVORITE()) {
cf2b4488
HP
2282#if WIRELESS_EXT < 19
2283 net->get_wireless_stats = dhd_get_wireless_stats;
2284#endif /* WIRELESS_EXT < 19 */
2285#if WIRELESS_EXT > 12
2286 net->wireless_handlers =
2287 (struct iw_handler_def *)&wl_iw_handler_def;
2288#endif /* WIRELESS_EXT > 12 */
cf2b4488 2289 }
cf2b4488
HP
2290#endif /* defined(CONFIG_WIRELESS_EXT) */
2291
2292 dhd->pub.rxsz = net->mtu + net->hard_header_len + dhd->pub.hdrlen;
2293
b8d63078 2294 memcpy(net->dev_addr, temp_addr, ETH_ALEN);
cf2b4488
HP
2295
2296 if (register_netdev(net) != 0) {
2297 DHD_ERROR(("%s: couldn't register the net device\n",
2298 __func__));
2299 goto fail;
2300 }
2301
2302 printf("%s: Broadcom Dongle Host Driver\n", net->name);
2303
2304 return 0;
2305
2306fail:
2307 net->netdev_ops = NULL;
2308 return BCME_ERROR;
2309}
2310
2311void dhd_bus_detach(dhd_pub_t *dhdp)
2312{
2313 dhd_info_t *dhd;
2314
2315 DHD_TRACE(("%s: Enter\n", __func__));
2316
2317 if (dhdp) {
2318 dhd = (dhd_info_t *) dhdp->info;
2319 if (dhd) {
2320 /* Stop the protocol module */
2321 dhd_prot_stop(&dhd->pub);
2322
2323 /* Stop the bus module */
0f0881b0 2324 dhd_bus_stop(dhd->pub.bus, true);
cf2b4488
HP
2325#if defined(OOB_INTR_ONLY)
2326 bcmsdh_unregister_oob_intr();
2327#endif /* defined(OOB_INTR_ONLY) */
2328
2329 /* Clear the watchdog timer */
2330 del_timer_sync(&dhd->timer);
0965ae88 2331 dhd->wd_timer_valid = false;
cf2b4488
HP
2332 }
2333 }
2334}
2335
2336void dhd_detach(dhd_pub_t *dhdp)
2337{
2338 dhd_info_t *dhd;
2339
2340 DHD_TRACE(("%s: Enter\n", __func__));
2341
2342 if (dhdp) {
2343 dhd = (dhd_info_t *) dhdp->info;
2344 if (dhd) {
2345 dhd_if_t *ifp;
2346 int i;
2347
2348#if defined(CONFIG_HAS_EARLYSUSPEND)
2349 if (dhd->early_suspend.suspend)
2350 unregister_early_suspend(&dhd->early_suspend);
2351#endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
2352
2353 for (i = 1; i < DHD_MAX_IFS; i++)
2354 if (dhd->iflist[i])
2355 dhd_del_if(dhd, i);
2356
2357 ifp = dhd->iflist[0];
2358 ASSERT(ifp);
2359 if (ifp->net->netdev_ops == &dhd_ops_pri) {
2360 dhd_stop(ifp->net);
2361 unregister_netdev(ifp->net);
2362 }
2363
860708d9 2364 if (dhd->watchdog_tsk) {
7356f429 2365 send_sig(SIGTERM, dhd->watchdog_tsk, 1);
860708d9
JC
2366 kthread_stop(dhd->watchdog_tsk);
2367 dhd->watchdog_tsk = NULL;
cf2b4488
HP
2368 }
2369
ecd7559d 2370 if (dhd->dpc_tsk) {
7356f429 2371 send_sig(SIGTERM, dhd->dpc_tsk, 1);
ecd7559d
JC
2372 kthread_stop(dhd->dpc_tsk);
2373 dhd->dpc_tsk = NULL;
eeb8e46b 2374 } else
cf2b4488
HP
2375 tasklet_kill(&dhd->tasklet);
2376
d809dcb9 2377 if (dhd->sysioc_tsk) {
7356f429 2378 send_sig(SIGTERM, dhd->sysioc_tsk, 1);
d809dcb9
JC
2379 kthread_stop(dhd->sysioc_tsk);
2380 dhd->sysioc_tsk = NULL;
cf2b4488
HP
2381 }
2382
2383 dhd_bus_detach(dhdp);
2384
2385 if (dhdp->prot)
2386 dhd_prot_detach(dhdp);
2387
2388#if defined(CONFIG_WIRELESS_EXT)
2389 wl_iw_detach();
2390#endif /* (CONFIG_WIRELESS_EXT) */
2391
cf2b4488
HP
2392 if (IS_CFG80211_FAVORITE())
2393 wl_cfg80211_detach();
cf2b4488
HP
2394
2395#if defined(CONFIG_PM_SLEEP)
2396 unregister_pm_notifier(&dhd_sleep_pm_notifier);
2397#endif /* defined(CONFIG_PM_SLEEP) */
2398 /* && defined(DHD_GPL) */
cf2b4488 2399 free_netdev(ifp->net);
182acb3c 2400 kfree(ifp);
2401 kfree(dhd);
cf2b4488
HP
2402 }
2403 }
2404}
2405
2406static void __exit dhd_module_cleanup(void)
2407{
2408 DHD_TRACE(("%s: Enter\n", __func__));
2409
2410 dhd_bus_unregister();
2411#if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2412 wifi_del_dev();
2413#endif
2414 /* Call customer gpio to turn off power with WL_REG_ON signal */
2415 dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
2416}
2417
2418static int __init dhd_module_init(void)
2419{
2420 int error;
2421
2422 DHD_TRACE(("%s: Enter\n", __func__));
2423
2424 /* Sanity check on the module parameters */
2425 do {
2426 /* Both watchdog and DPC as tasklets are ok */
2427 if ((dhd_watchdog_prio < 0) && (dhd_dpc_prio < 0))
2428 break;
2429
2430 /* If both watchdog and DPC are threads, TX must be deferred */
2431 if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0)
2432 && dhd_deferred_tx)
2433 break;
2434
2435 DHD_ERROR(("Invalid module parameters.\n"));
2436 return -EINVAL;
2437 } while (0);
2438 /* Call customer gpio to turn on power with WL_REG_ON signal */
2439 dhd_customer_gpio_wlan_ctrl(WLAN_POWER_ON);
2440
2441#if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC)
2442 sema_init(&wifi_control_sem, 0);
2443
2444 error = wifi_add_dev();
2445 if (error) {
2446 DHD_ERROR(("%s: platform_driver_register failed\n", __func__));
2447 goto faild;
2448 }
2449
2450 /* Waiting callback after platform_driver_register is done or
2451 exit with error */
2452 if (down_timeout(&wifi_control_sem, msecs_to_jiffies(1000)) != 0) {
2453 printk(KERN_ERR "%s: platform_driver_register timeout\n",
2454 __func__);
2455 /* remove device */
2456 wifi_del_dev();
2457 goto faild;
2458 }
2459#endif /* #if defined(CUSTOMER_HW2) && defined(CONFIG_WIFI_CONTROL_FUNC) */
2460
2461 error = dhd_bus_register();
2462
49ba9d2d 2463 if (error) {
cf2b4488
HP
2464 DHD_ERROR(("%s: sdio_register_driver failed\n", __func__));
2465 goto faild;
2466 }
2467 return error;
2468
2469faild:
2470 /* turn off power and exit */
2471 dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
2472 return -EINVAL;
2473}
2474
2475module_init(dhd_module_init);
2476module_exit(dhd_module_cleanup);
2477
2478/*
2479 * OS specific functions required to implement DHD driver in OS independent way
2480 */
2481int dhd_os_proto_block(dhd_pub_t *pub)
2482{
2483 dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2484
2485 if (dhd) {
2486 down(&dhd->proto_sem);
2487 return 1;
2488 }
2489 return 0;
2490}
2491
2492int dhd_os_proto_unblock(dhd_pub_t *pub)
2493{
2494 dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2495
2496 if (dhd) {
2497 up(&dhd->proto_sem);
2498 return 1;
2499 }
2500
2501 return 0;
2502}
2503
2504unsigned int dhd_os_get_ioctl_resp_timeout(void)
2505{
2506 return (unsigned int)dhd_ioctl_timeout_msec;
2507}
2508
2509void dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
2510{
2511 dhd_ioctl_timeout_msec = (int)timeout_msec;
2512}
2513
2514int dhd_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition, bool *pending)
2515{
2516 dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2517 DECLARE_WAITQUEUE(wait, current);
2518 int timeout = dhd_ioctl_timeout_msec;
2519
2520 /* Convert timeout in millsecond to jiffies */
2521 timeout = timeout * HZ / 1000;
2522
2523 /* Wait until control frame is available */
2524 add_wait_queue(&dhd->ioctl_resp_wait, &wait);
2525 set_current_state(TASK_INTERRUPTIBLE);
2526
2527 while (!(*condition) && (!signal_pending(current) && timeout))
2528 timeout = schedule_timeout(timeout);
2529
2530 if (signal_pending(current))
0f0881b0 2531 *pending = true;
cf2b4488
HP
2532
2533 set_current_state(TASK_RUNNING);
2534 remove_wait_queue(&dhd->ioctl_resp_wait, &wait);
2535
2536 return timeout;
2537}
2538
2539int dhd_os_ioctl_resp_wake(dhd_pub_t *pub)
2540{
2541 dhd_info_t *dhd = (dhd_info_t *) (pub->info);
2542
2543 if (waitqueue_active(&dhd->ioctl_resp_wait))
2544 wake_up_interruptible(&dhd->ioctl_resp_wait);
2545
2546 return 0;
2547}
2548
2549void dhd_os_wd_timer(void *bus, uint wdtick)
2550{
2551 dhd_pub_t *pub = bus;
5f782dee 2552 static uint save_dhd_watchdog_ms;
cf2b4488
HP
2553 dhd_info_t *dhd = (dhd_info_t *) pub->info;
2554
2555 /* don't start the wd until fw is loaded */
2556 if (pub->busstate == DHD_BUS_DOWN)
2557 return;
2558
2559 /* Totally stop the timer */
0f0881b0 2560 if (!wdtick && dhd->wd_timer_valid == true) {
cf2b4488 2561 del_timer_sync(&dhd->timer);
0965ae88 2562 dhd->wd_timer_valid = false;
cf2b4488
HP
2563 save_dhd_watchdog_ms = wdtick;
2564 return;
2565 }
2566
2567 if (wdtick) {
2568 dhd_watchdog_ms = (uint) wdtick;
2569
2570 if (save_dhd_watchdog_ms != dhd_watchdog_ms) {
2571
0f0881b0 2572 if (dhd->wd_timer_valid == true)
cf2b4488
HP
2573 /* Stop timer and restart at new value */
2574 del_timer_sync(&dhd->timer);
2575
2576 /* Create timer again when watchdog period is
2577 dynamically changed or in the first instance
2578 */
2579 dhd->timer.expires =
2580 jiffies + dhd_watchdog_ms * HZ / 1000;
2581 add_timer(&dhd->timer);
2582
2583 } else {
2584 /* Re arm the timer, at last watchdog period */
2585 mod_timer(&dhd->timer,
2586 jiffies + dhd_watchdog_ms * HZ / 1000);
2587 }
2588
0f0881b0 2589 dhd->wd_timer_valid = true;
cf2b4488
HP
2590 save_dhd_watchdog_ms = wdtick;
2591 }
2592}
2593
2594void *dhd_os_open_image(char *filename)
2595{
2596 struct file *fp;
2597
cf2b4488
HP
2598 if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2599 return wl_cfg80211_request_fw(filename);
cf2b4488
HP
2600
2601 fp = filp_open(filename, O_RDONLY, 0);
2602 /*
2603 * 2.6.11 (FC4) supports filp_open() but later revs don't?
2604 * Alternative:
2605 * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
2606 * ???
2607 */
2608 if (IS_ERR(fp))
2609 fp = NULL;
2610
2611 return fp;
2612}
2613
2614int dhd_os_get_image_block(char *buf, int len, void *image)
2615{
2616 struct file *fp = (struct file *)image;
2617 int rdlen;
2618
cf2b4488
HP
2619 if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2620 return wl_cfg80211_read_fw(buf, len);
cf2b4488
HP
2621
2622 if (!image)
2623 return 0;
2624
2625 rdlen = kernel_read(fp, fp->f_pos, buf, len);
2626 if (rdlen > 0)
2627 fp->f_pos += rdlen;
2628
2629 return rdlen;
2630}
2631
2632void dhd_os_close_image(void *image)
2633{
cf2b4488
HP
2634 if (IS_CFG80211_FAVORITE() && !NO_FW_REQ())
2635 return wl_cfg80211_release_fw();
cf2b4488
HP
2636 if (image)
2637 filp_close((struct file *)image, NULL);
2638}
2639
2640void dhd_os_sdlock(dhd_pub_t *pub)
2641{
2642 dhd_info_t *dhd;
2643
2644 dhd = (dhd_info_t *) (pub->info);
2645
2646 if (dhd->threads_only)
2647 down(&dhd->sdsem);
2648 else
2649 spin_lock_bh(&dhd->sdlock);
2650}
2651
2652void dhd_os_sdunlock(dhd_pub_t *pub)
2653{
2654 dhd_info_t *dhd;
2655
2656 dhd = (dhd_info_t *) (pub->info);
2657
2658 if (dhd->threads_only)
2659 up(&dhd->sdsem);
2660 else
2661 spin_unlock_bh(&dhd->sdlock);
2662}
2663
2664void dhd_os_sdlock_txq(dhd_pub_t *pub)
2665{
2666 dhd_info_t *dhd;
2667
2668 dhd = (dhd_info_t *) (pub->info);
2669 spin_lock_bh(&dhd->txqlock);
2670}
2671
2672void dhd_os_sdunlock_txq(dhd_pub_t *pub)
2673{
2674 dhd_info_t *dhd;
2675
2676 dhd = (dhd_info_t *) (pub->info);
2677 spin_unlock_bh(&dhd->txqlock);
2678}
2679
2680void dhd_os_sdlock_rxq(dhd_pub_t *pub)
2681{
2682}
2683
2684void dhd_os_sdunlock_rxq(dhd_pub_t *pub)
2685{
2686}
2687
2688void dhd_os_sdtxlock(dhd_pub_t *pub)
2689{
2690 dhd_os_sdlock(pub);
2691}
2692
2693void dhd_os_sdtxunlock(dhd_pub_t *pub)
2694{
2695 dhd_os_sdunlock(pub);
2696}
2697
cf2b4488
HP
2698#if defined(CONFIG_WIRELESS_EXT)
2699struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev)
2700{
2701 int res = 0;
2702 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2703
2704 res = wl_iw_get_wireless_stats(dev, &dhd->iw.wstats);
2705
2706 if (res == 0)
2707 return &dhd->iw.wstats;
2708 else
2709 return NULL;
2710}
2711#endif /* defined(CONFIG_WIRELESS_EXT) */
2712
2713static int
2714dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
2715 wl_event_msg_t *event, void **data)
2716{
2717 int bcmerror = 0;
2718
2719 ASSERT(dhd != NULL);
2720
2721 bcmerror = wl_host_event(dhd, ifidx, pktdata, event, data);
2722 if (bcmerror != BCME_OK)
2723 return bcmerror;
2724
2725#if defined(CONFIG_WIRELESS_EXT)
cf2b4488 2726 if (!IS_CFG80211_FAVORITE()) {
cf2b4488
HP
2727 if ((dhd->iflist[*ifidx] == NULL)
2728 || (dhd->iflist[*ifidx]->net == NULL)) {
2729 DHD_ERROR(("%s Exit null pointer\n", __func__));
2730 return bcmerror;
2731 }
2732
2733 if (dhd->iflist[*ifidx]->net)
2734 wl_iw_event(dhd->iflist[*ifidx]->net, event, *data);
cf2b4488 2735 }
cf2b4488
HP
2736#endif /* defined(CONFIG_WIRELESS_EXT) */
2737
cf2b4488
HP
2738 if (IS_CFG80211_FAVORITE()) {
2739 ASSERT(dhd->iflist[*ifidx] != NULL);
2740 ASSERT(dhd->iflist[*ifidx]->net != NULL);
2741 if (dhd->iflist[*ifidx]->net)
2742 wl_cfg80211_event(dhd->iflist[*ifidx]->net, event,
2743 *data);
2744 }
cf2b4488
HP
2745
2746 return bcmerror;
2747}
2748
2749/* send up locally generated event */
2750void dhd_sendup_event(dhd_pub_t *dhdp, wl_event_msg_t *event, void *data)
2751{
2752 switch (ntoh32(event->event_type)) {
2753 default:
2754 break;
2755 }
2756}
2757
2758void dhd_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
2759{
2760 struct dhd_info *dhdinfo = dhd->info;
2761 dhd_os_sdunlock(dhd);
2762 wait_event_interruptible_timeout(dhdinfo->ctrl_wait,
0965ae88 2763 (*lockvar == false), HZ * 2);
cf2b4488
HP
2764 dhd_os_sdlock(dhd);
2765 return;
2766}
2767
2768void dhd_wait_event_wakeup(dhd_pub_t *dhd)
2769{
2770 struct dhd_info *dhdinfo = dhd->info;
2771 if (waitqueue_active(&dhdinfo->ctrl_wait))
2772 wake_up_interruptible(&dhdinfo->ctrl_wait);
2773 return;
2774}
2775
3fd79f7c 2776int dhd_dev_reset(struct net_device *dev, u8 flag)
cf2b4488
HP
2777{
2778 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2779
2780 /* Turning off watchdog */
2781 if (flag)
2782 dhd_os_wd_timer(&dhd->pub, 0);
2783
2784 dhd_bus_devreset(&dhd->pub, flag);
2785
2786 /* Turning on watchdog back */
2787 if (!flag)
2788 dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
2789 DHD_ERROR(("%s: WLAN OFF DONE\n", __func__));
2790
2791 return 1;
2792}
2793
2794int net_os_set_suspend_disable(struct net_device *dev, int val)
2795{
2796 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2797 int ret = 0;
2798
2799 if (dhd) {
2800 ret = dhd->pub.suspend_disable_flag;
2801 dhd->pub.suspend_disable_flag = val;
2802 }
2803 return ret;
2804}
2805
2806int net_os_set_suspend(struct net_device *dev, int val)
2807{
2808 int ret = 0;
2809#if defined(CONFIG_HAS_EARLYSUSPEND)
2810 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2811
2812 if (dhd) {
2813 dhd_os_proto_block(&dhd->pub);
2814 ret = dhd_set_suspend(val, &dhd->pub);
2815 dhd_os_proto_unblock(&dhd->pub);
2816 }
2817#endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
2818 return ret;
2819}
2820
2821int net_os_set_dtim_skip(struct net_device *dev, int val)
2822{
2823 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2824
2825 if (dhd)
2826 dhd->pub.dtim_skip = val;
2827
2828 return 0;
2829}
2830
2831int net_os_set_packet_filter(struct net_device *dev, int val)
2832{
2833 dhd_info_t *dhd = *(dhd_info_t **) netdev_priv(dev);
2834 int ret = 0;
2835
2836 /* Packet filtering is set only if we still in early-suspend and
2837 * we need either to turn it ON or turn it OFF
2838 * We can always turn it OFF in case of early-suspend, but we turn it
2839 * back ON only if suspend_disable_flag was not set
2840 */
2841 if (dhd && dhd->pub.up) {
2842 dhd_os_proto_block(&dhd->pub);
2843 if (dhd->pub.in_suspend) {
2844 if (!val || (val && !dhd->pub.suspend_disable_flag))
2845 dhd_set_packet_filter(val, &dhd->pub);
2846 }
2847 dhd_os_proto_unblock(&dhd->pub);
2848 }
2849 return ret;
2850}
2851
2852void dhd_dev_init_ioctl(struct net_device *dev)
2853{
2854 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2855
2856 dhd_preinit_ioctls(&dhd->pub);
2857}
2858
2859#ifdef PNO_SUPPORT
2860/* Linux wrapper to call common dhd_pno_clean */
2861int dhd_dev_pno_reset(struct net_device *dev)
2862{
2863 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2864
2865 return dhd_pno_clean(&dhd->pub);
2866}
2867
2868/* Linux wrapper to call common dhd_pno_enable */
2869int dhd_dev_pno_enable(struct net_device *dev, int pfn_enabled)
2870{
2871 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2872
2873 return dhd_pno_enable(&dhd->pub, pfn_enabled);
2874}
2875
2876/* Linux wrapper to call common dhd_pno_set */
2877int
2878dhd_dev_pno_set(struct net_device *dev, wlc_ssid_t *ssids_local, int nssid,
580a0bd9 2879 unsigned char scan_fr)
cf2b4488
HP
2880{
2881 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2882
2883 return dhd_pno_set(&dhd->pub, ssids_local, nssid, scan_fr);
2884}
2885
2886/* Linux wrapper to get pno status */
2887int dhd_dev_get_pno_status(struct net_device *dev)
2888{
2889 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2890
2891 return dhd_pno_get_status(&dhd->pub);
2892}
2893
2894#endif /* PNO_SUPPORT */
2895
2896static int dhd_get_pend_8021x_cnt(dhd_info_t *dhd)
2897{
2898 return atomic_read(&dhd->pend_8021x_cnt);
2899}
2900
2901#define MAX_WAIT_FOR_8021X_TX 10
2902
2903int dhd_wait_pend8021x(struct net_device *dev)
2904{
2905 dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
2906 int timeout = 10 * HZ / 1000;
2907 int ntimes = MAX_WAIT_FOR_8021X_TX;
2908 int pend = dhd_get_pend_8021x_cnt(dhd);
2909
2910 while (ntimes && pend) {
2911 if (pend) {
2912 set_current_state(TASK_INTERRUPTIBLE);
2913 schedule_timeout(timeout);
2914 set_current_state(TASK_RUNNING);
2915 ntimes--;
2916 }
2917 pend = dhd_get_pend_8021x_cnt(dhd);
2918 }
2919 return pend;
2920}
2921
2922#ifdef DHD_DEBUG
3fd79f7c 2923int write_to_file(dhd_pub_t *dhd, u8 *buf, int size)
cf2b4488
HP
2924{
2925 int ret = 0;
2926 struct file *fp;
2927 mm_segment_t old_fs;
2928 loff_t pos = 0;
2929
2930 /* change to KERNEL_DS address limit */
2931 old_fs = get_fs();
2932 set_fs(KERNEL_DS);
2933
2934 /* open file to write */
2935 fp = filp_open("/tmp/mem_dump", O_WRONLY | O_CREAT, 0640);
2936 if (!fp) {
2937 printf("%s: open file error\n", __func__);
2938 ret = -1;
2939 goto exit;
2940 }
2941
2942 /* Write buf to file */
2943 fp->f_op->write(fp, buf, size, &pos);
2944
2945exit:
2946 /* free buf before return */
182acb3c 2947 kfree(buf);
cf2b4488
HP
2948 /* close file before return */
2949 if (fp)
2950 filp_close(fp, current->files);
2951 /* restore previous address limit */
2952 set_fs(old_fs);
2953
2954 return ret;
2955}
2956#endif /* DHD_DEBUG */
This page took 0.194534 seconds and 5 git commands to generate.