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