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