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