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