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