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