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