93050b882d12a5cc47ed0dd14557440758c90deb
[linux-2.6.git] / drivers / net / wireless / bcmdhd / dhd_linux.c
1 /*
2  * Broadcom Dongle Host Driver (DHD), Linux-specific network interface
3  * Basically selected code segments from usb-cdc.c and usb-rndis.c
4  *
5  * Copyright (C) 1999-2011, Broadcom Corporation
6  * 
7  *         Unless you and Broadcom execute a separate written software license
8  * agreement governing use of this software, this software is licensed to you
9  * under the terms of the GNU General Public License version 2 (the "GPL"),
10  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
11  * following added to such license:
12  * 
13  *      As a special exception, the copyright holders of this software give you
14  * permission to link this software with independent modules, and to copy and
15  * distribute the resulting executable under terms of your choice, provided that
16  * you also meet, for each linked independent module, the terms and conditions of
17  * the license of that module.  An independent module is a module which is not
18  * derived from this software.  The special exception does not apply to any
19  * modifications of the software.
20  * 
21  *      Notwithstanding the above, under no circumstances may you combine this
22  * software in any way with any other Broadcom software provided under a license
23  * other than the GPL, without Broadcom's express prior written consent.
24  *
25  * $Id: dhd_linux.c 329682 2012-04-26 09:20:38Z $
26  */
27
28 #include <typedefs.h>
29 #include <linuxver.h>
30 #include <osl.h>
31
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/inetdevice.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/etherdevice.h>
40 #include <linux/random.h>
41 #include <linux/spinlock.h>
42 #include <linux/ethtool.h>
43 #include <linux/fcntl.h>
44 #include <linux/fs.h>
45 #include <linux/device.h>
46
47 #include <asm/uaccess.h>
48 #include <asm/unaligned.h>
49
50 #include <epivers.h>
51 #include <bcmutils.h>
52 #include <bcmendian.h>
53 #include <bcmdevs.h>
54
55 #include <proto/ethernet.h>
56 #include <dngl_stats.h>
57 #include <dhd.h>
58 #include <dhd_bus.h>
59 #include <dhd_proto.h>
60 #include <dhd_dbg.h>
61 #ifdef CONFIG_HAS_WAKELOCK
62 #include <linux/wakelock.h>
63 #endif
64 #ifdef WL_CFG80211
65 #include <wl_cfg80211.h>
66 #endif
67
68 #include <proto/802.11_bta.h>
69 #include <proto/bt_amp_hci.h>
70 #include <dhd_bta.h>
71
72 #ifdef WLMEDIA_HTSF
73 #include <linux/time.h>
74 #include <htsf.h>
75
76 #define HTSF_MINLEN 200    /* min. packet length to timestamp */
77 #define HTSF_BUS_DELAY 150 /* assume a fix propagation in us  */
78 #define TSMAX  1000        /* max no. of timing record kept   */
79 #define NUMBIN 34
80
81 static uint32 tsidx = 0;
82 static uint32 htsf_seqnum = 0;
83 uint32 tsfsync;
84 struct timeval tsync;
85 static uint32 tsport = 5010;
86
87 typedef struct histo_ {
88         uint32 bin[NUMBIN];
89 } histo_t;
90
91 #if !ISPOWEROF2(DHD_SDALIGN)
92 #error DHD_SDALIGN is not a power of 2!
93 #endif
94
95 static histo_t vi_d1, vi_d2, vi_d3, vi_d4;
96 #endif /* WLMEDIA_HTSF */
97
98 #if defined(SOFTAP)
99 extern bool ap_cfg_running;
100 extern bool ap_fw_loaded;
101 #endif
102
103 /* enable HOSTIP cache update from the host side when an eth0:N is up */
104 #define AOE_IP_ALIAS_SUPPORT 1
105
106 #ifdef PROP_TXSTATUS
107 #include <wlfc_proto.h>
108 #include <dhd_wlfc.h>
109 #endif
110
111 #include <wl_android.h>
112
113 #ifdef ARP_OFFLOAD_SUPPORT
114 void aoe_update_host_ipv4_table(dhd_pub_t *dhd_pub, u32 ipa, bool add);
115 static int dhd_device_event(struct notifier_block *this,
116         unsigned long event,
117         void *ptr);
118
119 static struct notifier_block dhd_notifier = {
120         .notifier_call = dhd_device_event
121 };
122 #endif /* ARP_OFFLOAD_SUPPORT */
123
124 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP)
125 #include <linux/suspend.h>
126 volatile bool dhd_mmc_suspend = FALSE;
127 DECLARE_WAIT_QUEUE_HEAD(dhd_dpc_wait);
128 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP) */
129
130 #if defined(OOB_INTR_ONLY)
131 extern void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable);
132 #endif /* defined(OOB_INTR_ONLY) */
133 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
134 static void dhd_hang_process(struct work_struct *work);
135 #endif 
136 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
137 MODULE_LICENSE("GPL v2");
138 #endif /* LinuxVer */
139
140 #include <dhd_bus.h>
141
142 #ifndef PROP_TXSTATUS
143 #define DBUS_RX_BUFFER_SIZE_DHD(net)    (net->mtu + net->hard_header_len + dhd->pub.hdrlen)
144 #else
145 #define DBUS_RX_BUFFER_SIZE_DHD(net)    (net->mtu + net->hard_header_len + dhd->pub.hdrlen + 128)
146 #endif
147
148 #if LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 15)
149 const char *
150 print_tainted()
151 {
152         return "";
153 }
154 #endif  /* LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 15) */
155
156 /* Linux wireless extension support */
157 #if defined(CONFIG_BCMDHD_WEXT)
158 #include <wl_iw.h>
159 extern wl_iw_extra_params_t  g_wl_iw_params;
160 #endif /* defined(CONFIG_BCMDHD_WEXT) */
161
162 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
163 #include <linux/earlysuspend.h>
164 #endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
165 extern int dhd_get_dtim_skip(dhd_pub_t *dhd);
166
167 #ifdef PKT_FILTER_SUPPORT
168 extern void dhd_pktfilter_offload_set(dhd_pub_t * dhd, char *arg);
169 extern void dhd_pktfilter_offload_enable(dhd_pub_t * dhd, char *arg, int enable, int master_mode);
170 #endif
171
172 /* Interface control information */
173 typedef struct dhd_if {
174         struct dhd_info *info;                  /* back pointer to dhd_info */
175         /* OS/stack specifics */
176         struct net_device *net;
177         struct net_device_stats stats;
178         int                     idx;                    /* iface idx in dongle */
179         dhd_if_state_t  state;                  /* interface state */
180         uint                    subunit;                /* subunit */
181         uint8                   mac_addr[ETHER_ADDR_LEN];       /* assigned MAC address */
182         bool                    attached;               /* Delayed attachment when unset */
183         bool                    txflowcontrol;  /* Per interface flow control indicator */
184         char                    name[IFNAMSIZ+1]; /* linux interface name */
185         uint8                   bssidx;                 /* bsscfg index for the interface */
186         bool                    set_multicast;
187 } dhd_if_t;
188
189 #ifdef WLMEDIA_HTSF
190 typedef struct {
191         uint32 low;
192         uint32 high;
193 } tsf_t;
194
195 typedef struct {
196         uint32 last_cycle;
197         uint32 last_sec;
198         uint32 last_tsf;
199         uint32 coef;     /* scaling factor */
200         uint32 coefdec1; /* first decimal  */
201         uint32 coefdec2; /* second decimal */
202 } htsf_t;
203
204 typedef struct {
205         uint32 t1;
206         uint32 t2;
207         uint32 t3;
208         uint32 t4;
209 } tstamp_t;
210
211 static tstamp_t ts[TSMAX];
212 static tstamp_t maxdelayts;
213 static uint32 maxdelay = 0, tspktcnt = 0, maxdelaypktno = 0;
214
215 #endif  /* WLMEDIA_HTSF */
216
217 /* Local private structure (extension of pub) */
218 typedef struct dhd_info {
219 #if defined(CONFIG_BCMDHD_WEXT)
220         wl_iw_t         iw;             /* wireless extensions state (must be first) */
221 #endif /* defined(CONFIG_BCMDHD_WEXT) */
222
223         dhd_pub_t pub;
224
225         /* For supporting multiple interfaces */
226         dhd_if_t *iflist[DHD_MAX_IFS];
227
228         struct semaphore proto_sem;
229 #ifdef PROP_TXSTATUS
230         spinlock_t      wlfc_spinlock;
231 #endif /* PROP_TXSTATUS */
232 #ifdef WLMEDIA_HTSF
233         htsf_t  htsf;
234 #endif
235         wait_queue_head_t ioctl_resp_wait;
236         struct timer_list timer;
237         bool wd_timer_valid;
238         struct tasklet_struct tasklet;
239         spinlock_t      sdlock;
240         spinlock_t      txqlock;
241         spinlock_t      dhd_lock;
242 #ifdef DHDTHREAD
243         /* Thread based operation */
244         bool threads_only;
245         struct semaphore sdsem;
246
247         tsk_ctl_t       thr_dpc_ctl;
248         tsk_ctl_t       thr_wdt_ctl;
249
250 #else
251         bool dhd_tasklet_create;
252 #endif /* DHDTHREAD */
253         tsk_ctl_t       thr_sysioc_ctl;
254 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
255         struct work_struct work_hang;
256 #endif 
257
258         /* Wakelocks */
259 #if defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
260         struct wake_lock wl_wifi;   /* Wifi wakelock */
261         struct wake_lock wl_rxwake; /* Wifi rx wakelock */
262         struct wake_lock wl_ctrlwake; /* Wifi ctrl wakelock */
263 #endif
264
265 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
266         /* net_device interface lock, prevent race conditions among net_dev interface
267          * calls and wifi_on or wifi_off
268          */
269         struct mutex dhd_net_if_mutex;
270         struct mutex dhd_suspend_mutex;
271 #endif
272         spinlock_t wakelock_spinlock;
273         int wakelock_counter;
274         int wakelock_rx_timeout_enable;
275         int wakelock_ctrl_timeout_enable;
276
277         /* Thread to issue ioctl for multicast */
278         bool set_macaddress;
279         struct ether_addr macvalue;
280         wait_queue_head_t ctrl_wait;
281         atomic_t pend_8021x_cnt;
282         dhd_attach_states_t dhd_state;
283
284 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
285         struct early_suspend early_suspend;
286 #endif /* CONFIG_HAS_EARLYSUSPEND */
287
288 #ifdef ARP_OFFLOAD_SUPPORT
289         u32 pend_ipaddr;
290 #endif /* ARP_OFFLOAD_SUPPORT */
291 } dhd_info_t;
292
293 /* Definitions to provide path to the firmware and nvram
294  * example nvram_path[MOD_PARAM_PATHLEN]="/projects/wlan/nvram.txt"
295  */
296 char firmware_path[MOD_PARAM_PATHLEN];
297 char nvram_path[MOD_PARAM_PATHLEN];
298
299 int op_mode = 0;
300 module_param(op_mode, int, 0644);
301 extern int wl_control_wl_start(struct net_device *dev);
302 extern int net_os_send_hang_message(struct net_device *dev);
303 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
304 struct semaphore dhd_registration_sem;
305 #define DHD_REGISTRATION_TIMEOUT  12000  /* msec : allowed time to finished dhd registration */
306 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) */
307
308 /* Spawn a thread for system ioctls (set mac, set mcast) */
309 uint dhd_sysioc = TRUE;
310 module_param(dhd_sysioc, uint, 0);
311
312 /* Error bits */
313 module_param(dhd_msg_level, int, 0);
314
315 /* load firmware and/or nvram values from the filesystem */
316 module_param_string(firmware_path, firmware_path, MOD_PARAM_PATHLEN, 0660);
317 module_param_string(nvram_path, nvram_path, MOD_PARAM_PATHLEN, 0);
318
319 /* Watchdog interval */
320 uint dhd_watchdog_ms = 10;
321 module_param(dhd_watchdog_ms, uint, 0);
322
323 #if defined(DHD_DEBUG)
324 /* Console poll interval */
325 uint dhd_console_ms = 0;
326 module_param(dhd_console_ms, uint, 0644);
327 #endif /* defined(DHD_DEBUG) */
328
329 /* ARP offload agent mode : Enable ARP Host Auto-Reply and ARP Peer Auto-Reply */
330 uint dhd_arp_mode = 0xb;
331 module_param(dhd_arp_mode, uint, 0);
332
333 /* ARP offload enable */
334 uint dhd_arp_enable = TRUE;
335 module_param(dhd_arp_enable, uint, 0);
336
337 /* Global Pkt filter enable control */
338 uint dhd_pkt_filter_enable = TRUE;
339 module_param(dhd_pkt_filter_enable, uint, 0);
340
341 /*  Pkt filter init setup */
342 uint dhd_pkt_filter_init = 0;
343 module_param(dhd_pkt_filter_init, uint, 0);
344
345 /* Pkt filter mode control */
346 uint dhd_master_mode = TRUE;
347 module_param(dhd_master_mode, uint, 0);
348
349 #ifdef DHDTHREAD
350 /* Watchdog thread priority, -1 to use kernel timer */
351 int dhd_watchdog_prio = 97;
352 module_param(dhd_watchdog_prio, int, 0);
353
354 /* DPC thread priority, -1 to use tasklet */
355 int dhd_dpc_prio = 98;
356 module_param(dhd_dpc_prio, int, 0);
357
358 /* DPC thread priority, -1 to use tasklet */
359 extern int dhd_dongle_memsize;
360 module_param(dhd_dongle_memsize, int, 0);
361 #endif /* DHDTHREAD */
362 /* Control fw roaming */
363 uint dhd_roam_disable = 0;
364
365 /* Control radio state */
366 uint dhd_radio_up = 1;
367
368 /* Network inteface name */
369 char iface_name[IFNAMSIZ] = {'\0'};
370 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
371
372 /* The following are specific to the SDIO dongle */
373
374 /* IOCTL response timeout */
375 int dhd_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
376
377 /* Idle timeout for backplane clock */
378 int dhd_idletime = DHD_IDLETIME_TICKS;
379 module_param(dhd_idletime, int, 0);
380
381 /* Use polling */
382 uint dhd_poll = FALSE;
383 module_param(dhd_poll, uint, 0);
384
385 /* Use interrupts */
386 uint dhd_intr = TRUE;
387 module_param(dhd_intr, uint, 0);
388
389 /* SDIO Drive Strength (in milliamps) */
390 uint dhd_sdiod_drive_strength = 6;
391 module_param(dhd_sdiod_drive_strength, uint, 0);
392
393 /* Tx/Rx bounds */
394 extern uint dhd_txbound;
395 extern uint dhd_rxbound;
396 module_param(dhd_txbound, uint, 0);
397 module_param(dhd_rxbound, uint, 0);
398
399 /* Deferred transmits */
400 extern uint dhd_deferred_tx;
401 module_param(dhd_deferred_tx, uint, 0);
402
403 #ifdef BCMDBGFS
404 extern void dhd_dbg_init(dhd_pub_t *dhdp);
405 extern void dhd_dbg_remove(void);
406 #endif /* BCMDBGFS */
407
408
409
410 #ifdef SDTEST
411 /* Echo packet generator (pkts/s) */
412 uint dhd_pktgen = 0;
413 module_param(dhd_pktgen, uint, 0);
414
415 /* Echo packet len (0 => sawtooth, max 2040) */
416 uint dhd_pktgen_len = 0;
417 module_param(dhd_pktgen_len, uint, 0);
418 #endif /* SDTEST */
419
420 /* Version string to report */
421 #ifdef DHD_DEBUG
422 #ifndef SRCBASE
423 #define SRCBASE        "drivers/net/wireless/bcmdhd"
424 #endif
425 #define DHD_COMPILED "\nCompiled in " SRCBASE
426 #else
427 #define DHD_COMPILED
428 #endif /* DHD_DEBUG */
429
430 static char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR
431 #ifdef DHD_DEBUG
432 "\nCompiled in " SRCBASE " on " __DATE__ " at " __TIME__
433 #endif
434 ;
435 static void dhd_net_if_lock_local(dhd_info_t *dhd);
436 static void dhd_net_if_unlock_local(dhd_info_t *dhd);
437 static void dhd_suspend_lock(dhd_pub_t *dhdp);
438 static void dhd_suspend_unlock(dhd_pub_t *dhdp);
439 #if !defined(AP) && defined(WLP2P) && defined(WL_ENABLE_P2P_IF)
440 static u32 dhd_concurrent_fw(dhd_pub_t *dhd);
441 #endif 
442
443 #ifdef WLMEDIA_HTSF
444 void htsf_update(dhd_info_t *dhd, void *data);
445 tsf_t prev_tsf, cur_tsf;
446
447 uint32 dhd_get_htsf(dhd_info_t *dhd, int ifidx);
448 static int dhd_ioctl_htsf_get(dhd_info_t *dhd, int ifidx);
449 static void dhd_dump_latency(void);
450 static void dhd_htsf_addtxts(dhd_pub_t *dhdp, void *pktbuf);
451 static void dhd_htsf_addrxts(dhd_pub_t *dhdp, void *pktbuf);
452 static void dhd_dump_htsfhisto(histo_t *his, char *s);
453 #endif /* WLMEDIA_HTSF */
454
455 /* Monitor interface */
456 int dhd_monitor_init(void *dhd_pub);
457 int dhd_monitor_uninit(void);
458
459
460 #if defined(CONFIG_BCMDHD_WEXT)
461 struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev);
462 #endif /* defined(CONFIG_BCMDHD_WEXT) */
463
464 static void dhd_dpc(ulong data);
465 /* forward decl */
466 extern int dhd_wait_pend8021x(struct net_device *dev);
467
468 #ifdef TOE
469 #ifndef BDC
470 #error TOE requires BDC
471 #endif /* !BDC */
472 static int dhd_toe_get(dhd_info_t *dhd, int idx, uint32 *toe_ol);
473 static int dhd_toe_set(dhd_info_t *dhd, int idx, uint32 toe_ol);
474 #endif /* TOE */
475
476 static int dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
477                              wl_event_msg_t *event_ptr, void **data_ptr);
478
479 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP)
480 static int dhd_sleep_pm_callback(struct notifier_block *nfb, unsigned long action, void *ignored)
481 {
482         int ret = NOTIFY_DONE;
483
484 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
485         switch (action) {
486                 case PM_HIBERNATION_PREPARE:
487                 case PM_SUSPEND_PREPARE:
488                         dhd_mmc_suspend = TRUE;
489                         ret = NOTIFY_OK;
490                 break;
491                 case PM_POST_HIBERNATION:
492                 case PM_POST_SUSPEND:
493                         dhd_mmc_suspend = FALSE;
494                         ret = NOTIFY_OK;
495                 break;
496         }
497         smp_mb();
498 #endif
499         return ret;
500 }
501
502 static struct notifier_block dhd_sleep_pm_notifier = {
503         .notifier_call = dhd_sleep_pm_callback,
504         .priority = 10
505 };
506 extern int register_pm_notifier(struct notifier_block *nb);
507 extern int unregister_pm_notifier(struct notifier_block *nb);
508 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP) */
509
510 static void dhd_set_packet_filter(int value, dhd_pub_t *dhd)
511 {
512 #ifdef PKT_FILTER_SUPPORT
513         DHD_TRACE(("%s: %d\n", __FUNCTION__, value));
514         /* 1 - Enable packet filter, only allow unicast packet to send up */
515         /* 0 - Disable packet filter */
516         if (dhd_pkt_filter_enable && (!value ||
517             (dhd_check_ap_wfd_mode_set(dhd) == FALSE))) {
518                 int i;
519
520                 for (i = 0; i < dhd->pktfilter_count; i++) {
521                         dhd_pktfilter_offload_set(dhd, dhd->pktfilter[i]);
522                         dhd_pktfilter_offload_enable(dhd, dhd->pktfilter[i],
523                                 value, dhd_master_mode);
524                 }
525         }
526 #endif
527 }
528
529 static int dhd_set_suspend(int value, dhd_pub_t *dhd)
530 {
531         int power_mode = PM_MAX;
532         /* wl_pkt_filter_enable_t       enable_parm; */
533         char iovbuf[32];
534         int bcn_li_dtim = 3;
535         uint roamvar = 1;
536
537         DHD_TRACE(("%s: enter, value = %d in_suspend=%d\n",
538                 __FUNCTION__, value, dhd->in_suspend));
539
540         dhd_suspend_lock(dhd);
541         if (dhd && dhd->up) {
542                 if (value && dhd->in_suspend) {
543
544                         /* Kernel suspended */
545                         DHD_ERROR(("%s: force extra Suspend setting \n", __FUNCTION__));
546
547                         dhd_wl_ioctl_cmd(dhd, WLC_SET_PM, (char *)&power_mode,
548                                          sizeof(power_mode), TRUE, 0);
549
550                         /* Enable packet filter, only allow unicast packet to send up */
551                         dhd_set_packet_filter(1, dhd);
552
553                         /* If DTIM skip is set up as default, force it to wake
554                          * each third DTIM for better power savings.  Note that
555                          * one side effect is a chance to miss BC/MC packet.
556                          */
557                         bcn_li_dtim = dhd_get_dtim_skip(dhd);
558                         bcm_mkiovar("bcn_li_dtim", (char *)&bcn_li_dtim,
559                                 4, iovbuf, sizeof(iovbuf));
560                         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
561
562                         /* Disable firmware roaming during suspend */
563                         bcm_mkiovar("roam_off", (char *)&roamvar, 4,
564                                 iovbuf, sizeof(iovbuf));
565                         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
566                 } else {
567
568                         /* Kernel resumed  */
569                         DHD_TRACE(("%s: Remove extra suspend setting \n", __FUNCTION__));
570
571                         power_mode = PM_FAST;
572                         dhd_wl_ioctl_cmd(dhd, WLC_SET_PM, (char *)&power_mode,
573                                          sizeof(power_mode), TRUE, 0);
574
575                         /* disable pkt filter */
576                         dhd_set_packet_filter(0, dhd);
577
578                         /* restore pre-suspend setting for dtim_skip */
579                         bcm_mkiovar("bcn_li_dtim", (char *)&dhd->dtim_skip,
580                                 4, iovbuf, sizeof(iovbuf));
581
582                         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
583                         roamvar = dhd_roam_disable;
584                         bcm_mkiovar("roam_off", (char *)&roamvar, 4, iovbuf,
585                                 sizeof(iovbuf));
586                         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
587                 }
588         }
589         dhd_suspend_unlock(dhd);
590         return 0;
591 }
592
593 static int dhd_suspend_resume_helper(struct dhd_info *dhd, int val, int force)
594 {
595         dhd_pub_t *dhdp = &dhd->pub;
596         int ret = 0;
597
598         DHD_OS_WAKE_LOCK(dhdp);
599         /* Set flag when early suspend was called */
600         dhdp->in_suspend = val;
601         if ((force || !dhdp->suspend_disable_flag) &&
602             (dhd_check_ap_wfd_mode_set(dhdp) == FALSE)) {
603                 ret = dhd_set_suspend(val, dhdp);
604         }
605         DHD_OS_WAKE_UNLOCK(dhdp);
606         return ret;
607 }
608
609 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
610 static void dhd_early_suspend(struct early_suspend *h)
611 {
612         struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
613
614         DHD_TRACE(("%s: enter\n", __FUNCTION__));
615
616         if (dhd)
617                 dhd_suspend_resume_helper(dhd, 1, 0);
618 }
619
620 static void dhd_late_resume(struct early_suspend *h)
621 {
622         struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
623
624         DHD_TRACE(("%s: enter\n", __FUNCTION__));
625
626         if (dhd)
627                 dhd_suspend_resume_helper(dhd, 0, 0);
628 }
629 #endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
630
631 /*
632  * Generalized timeout mechanism.  Uses spin sleep with exponential back-off until
633  * the sleep time reaches one jiffy, then switches over to task delay.  Usage:
634  *
635  *      dhd_timeout_start(&tmo, usec);
636  *      while (!dhd_timeout_expired(&tmo))
637  *              if (poll_something())
638  *                      break;
639  *      if (dhd_timeout_expired(&tmo))
640  *              fatal();
641  */
642
643 void
644 dhd_timeout_start(dhd_timeout_t *tmo, uint usec)
645 {
646         tmo->limit = usec;
647         tmo->increment = 0;
648         tmo->elapsed = 0;
649         tmo->tick = 1000000 / HZ;
650 }
651
652 int
653 dhd_timeout_expired(dhd_timeout_t *tmo)
654 {
655         /* Does nothing the first call */
656         if (tmo->increment == 0) {
657                 tmo->increment = 1;
658                 return 0;
659         }
660
661         if (tmo->elapsed >= tmo->limit)
662                 return 1;
663
664         /* Add the delay that's about to take place */
665         tmo->elapsed += tmo->increment;
666
667         if (tmo->increment < tmo->tick) {
668                 OSL_DELAY(tmo->increment);
669                 tmo->increment *= 2;
670                 if (tmo->increment > tmo->tick)
671                         tmo->increment = tmo->tick;
672         } else {
673                 wait_queue_head_t delay_wait;
674                 DECLARE_WAITQUEUE(wait, current);
675                 int pending;
676                 init_waitqueue_head(&delay_wait);
677                 add_wait_queue(&delay_wait, &wait);
678                 set_current_state(TASK_INTERRUPTIBLE);
679                 schedule_timeout(1);
680                 pending = signal_pending(current);
681                 remove_wait_queue(&delay_wait, &wait);
682                 set_current_state(TASK_RUNNING);
683                 if (pending)
684                         return 1;       /* Interrupted */
685         }
686
687         return 0;
688 }
689
690 int
691 dhd_net2idx(dhd_info_t *dhd, struct net_device *net)
692 {
693         int i = 0;
694
695         ASSERT(dhd);
696         while (i < DHD_MAX_IFS) {
697                 if (dhd->iflist[i] && (dhd->iflist[i]->net == net))
698                         return i;
699                 i++;
700         }
701
702         return DHD_BAD_IF;
703 }
704
705 struct net_device * dhd_idx2net(void *pub, int ifidx)
706 {
707         struct dhd_pub *dhd_pub = (struct dhd_pub *)pub;
708         struct dhd_info *dhd_info;
709
710         if (!dhd_pub || ifidx < 0 || ifidx >= DHD_MAX_IFS)
711                 return NULL;
712         dhd_info = dhd_pub->info;
713         if (dhd_info && dhd_info->iflist[ifidx])
714                 return dhd_info->iflist[ifidx]->net;
715         return NULL;
716 }
717
718 int
719 dhd_ifname2idx(dhd_info_t *dhd, char *name)
720 {
721         int i = DHD_MAX_IFS;
722
723         ASSERT(dhd);
724
725         if (name == NULL || *name == '\0')
726                 return 0;
727
728         while (--i > 0)
729                 if (dhd->iflist[i] && !strncmp(dhd->iflist[i]->name, name, IFNAMSIZ))
730                                 break;
731
732         DHD_TRACE(("%s: return idx %d for \"%s\"\n", __FUNCTION__, i, name));
733
734         return i;       /* default - the primary interface */
735 }
736
737 char *
738 dhd_ifname(dhd_pub_t *dhdp, int ifidx)
739 {
740         dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
741
742         ASSERT(dhd);
743
744         if (ifidx < 0 || ifidx >= DHD_MAX_IFS) {
745                 DHD_ERROR(("%s: ifidx %d out of range\n", __FUNCTION__, ifidx));
746                 return "<if_bad>";
747         }
748
749         if (dhd->iflist[ifidx] == NULL) {
750                 DHD_ERROR(("%s: null i/f %d\n", __FUNCTION__, ifidx));
751                 return "<if_null>";
752         }
753
754         if (dhd->iflist[ifidx]->net)
755                 return dhd->iflist[ifidx]->net->name;
756
757         return "<if_none>";
758 }
759
760 uint8 *
761 dhd_bssidx2bssid(dhd_pub_t *dhdp, int idx)
762 {
763         int i;
764         dhd_info_t *dhd = (dhd_info_t *)dhdp;
765
766         ASSERT(dhd);
767         for (i = 0; i < DHD_MAX_IFS; i++)
768         if (dhd->iflist[i] && dhd->iflist[i]->bssidx == idx)
769                 return dhd->iflist[i]->mac_addr;
770
771         return NULL;
772 }
773
774
775 static void
776 _dhd_set_multicast_list(dhd_info_t *dhd, int ifidx)
777 {
778         struct net_device *dev;
779 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
780         struct netdev_hw_addr *ha;
781 #else
782         struct dev_mc_list *mclist;
783 #endif
784         uint32 allmulti, cnt;
785
786         wl_ioctl_t ioc;
787         char *buf, *bufp;
788         uint buflen;
789         int ret;
790
791         ASSERT(dhd && dhd->iflist[ifidx]);
792         dev = dhd->iflist[ifidx]->net;
793 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
794         netif_addr_lock_bh(dev);
795 #endif
796 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
797         cnt = netdev_mc_count(dev);
798 #else
799         cnt = dev->mc_count;
800 #endif
801 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
802         netif_addr_unlock_bh(dev);
803 #endif
804
805         /* Determine initial value of allmulti flag */
806         allmulti = (dev->flags & IFF_ALLMULTI) ? TRUE : FALSE;
807
808         /* Send down the multicast list first. */
809
810
811         buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETHER_ADDR_LEN);
812         if (!(bufp = buf = MALLOC(dhd->pub.osh, buflen))) {
813                 DHD_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
814                            dhd_ifname(&dhd->pub, ifidx), cnt));
815                 return;
816         }
817
818         strcpy(bufp, "mcast_list");
819         bufp += strlen("mcast_list") + 1;
820
821         cnt = htol32(cnt);
822         memcpy(bufp, &cnt, sizeof(cnt));
823         bufp += sizeof(cnt);
824
825 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
826         netif_addr_lock_bh(dev);
827 #endif
828 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
829         netdev_for_each_mc_addr(ha, dev) {
830                 if (!cnt)
831                         break;
832                 memcpy(bufp, ha->addr, ETHER_ADDR_LEN);
833                 bufp += ETHER_ADDR_LEN;
834                 cnt--;
835         }
836 #else
837         for (mclist = dev->mc_list; (mclist && (cnt > 0)); cnt--, mclist = mclist->next) {
838                 memcpy(bufp, (void *)mclist->dmi_addr, ETHER_ADDR_LEN);
839                 bufp += ETHER_ADDR_LEN;
840         }
841 #endif
842 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
843         netif_addr_unlock_bh(dev);
844 #endif
845
846         memset(&ioc, 0, sizeof(ioc));
847         ioc.cmd = WLC_SET_VAR;
848         ioc.buf = buf;
849         ioc.len = buflen;
850         ioc.set = TRUE;
851
852         ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
853         if (ret < 0) {
854                 DHD_ERROR(("%s: set mcast_list failed, cnt %d\n",
855                         dhd_ifname(&dhd->pub, ifidx), cnt));
856                 allmulti = cnt ? TRUE : allmulti;
857         }
858
859         MFREE(dhd->pub.osh, buf, buflen);
860
861         /* Now send the allmulti setting.  This is based on the setting in the
862          * net_device flags, but might be modified above to be turned on if we
863          * were trying to set some addresses and dongle rejected it...
864          */
865
866         buflen = sizeof("allmulti") + sizeof(allmulti);
867         if (!(buf = MALLOC(dhd->pub.osh, buflen))) {
868                 DHD_ERROR(("%s: out of memory for allmulti\n", dhd_ifname(&dhd->pub, ifidx)));
869                 return;
870         }
871         allmulti = htol32(allmulti);
872
873         if (!bcm_mkiovar("allmulti", (void*)&allmulti, sizeof(allmulti), buf, buflen)) {
874                 DHD_ERROR(("%s: mkiovar failed for allmulti, datalen %d buflen %u\n",
875                            dhd_ifname(&dhd->pub, ifidx), (int)sizeof(allmulti), buflen));
876                 MFREE(dhd->pub.osh, buf, buflen);
877                 return;
878         }
879
880
881         memset(&ioc, 0, sizeof(ioc));
882         ioc.cmd = WLC_SET_VAR;
883         ioc.buf = buf;
884         ioc.len = buflen;
885         ioc.set = TRUE;
886
887         ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
888         if (ret < 0) {
889                 DHD_ERROR(("%s: set allmulti %d failed\n",
890                            dhd_ifname(&dhd->pub, ifidx), ltoh32(allmulti)));
891         }
892
893         MFREE(dhd->pub.osh, buf, buflen);
894
895         /* Finally, pick up the PROMISC flag as well, like the NIC driver does */
896
897         allmulti = (dev->flags & IFF_PROMISC) ? TRUE : FALSE;
898         allmulti = htol32(allmulti);
899
900         memset(&ioc, 0, sizeof(ioc));
901         ioc.cmd = WLC_SET_PROMISC;
902         ioc.buf = &allmulti;
903         ioc.len = sizeof(allmulti);
904         ioc.set = TRUE;
905
906         ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
907         if (ret < 0) {
908                 DHD_ERROR(("%s: set promisc %d failed\n",
909                            dhd_ifname(&dhd->pub, ifidx), ltoh32(allmulti)));
910         }
911 }
912
913 static int
914 _dhd_set_mac_address(dhd_info_t *dhd, int ifidx, struct ether_addr *addr)
915 {
916         char buf[32];
917         wl_ioctl_t ioc;
918         int ret;
919
920         if (!bcm_mkiovar("cur_etheraddr", (char*)addr, ETHER_ADDR_LEN, buf, 32)) {
921                 DHD_ERROR(("%s: mkiovar failed for cur_etheraddr\n", dhd_ifname(&dhd->pub, ifidx)));
922                 return -1;
923         }
924         memset(&ioc, 0, sizeof(ioc));
925         ioc.cmd = WLC_SET_VAR;
926         ioc.buf = buf;
927         ioc.len = 32;
928         ioc.set = TRUE;
929
930         ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
931         if (ret < 0) {
932                 DHD_ERROR(("%s: set cur_etheraddr failed\n", dhd_ifname(&dhd->pub, ifidx)));
933         } else {
934                 memcpy(dhd->iflist[ifidx]->net->dev_addr, addr, ETHER_ADDR_LEN);
935                 memcpy(dhd->pub.mac.octet, addr, ETHER_ADDR_LEN);
936         }
937
938         return ret;
939 }
940
941 #ifdef SOFTAP
942 extern struct net_device *ap_net_dev;
943 extern tsk_ctl_t ap_eth_ctl; /* ap netdev heper thread ctl */
944 #endif
945
946 static void
947 dhd_op_if(dhd_if_t *ifp)
948 {
949         dhd_info_t *dhd;
950         int ret = 0, err = 0;
951 #ifdef SOFTAP
952         unsigned long flags;
953 #endif
954
955         if (!ifp || !ifp->info || !ifp->idx)
956                 return;
957         ASSERT(ifp && ifp->info && ifp->idx);   /* Virtual interfaces only */
958         dhd = ifp->info;
959
960         DHD_TRACE(("%s: idx %d, state %d\n", __FUNCTION__, ifp->idx, ifp->state));
961
962 #ifdef WL_CFG80211
963         if (wl_cfg80211_is_progress_ifchange())
964                         return;
965
966 #endif
967         switch (ifp->state) {
968         case DHD_IF_ADD:
969                 /*
970                  * Delete the existing interface before overwriting it
971                  * in case we missed the WLC_E_IF_DEL event.
972                  */
973                 if (ifp->net != NULL) {
974                         DHD_ERROR(("%s: ERROR: netdev:%s already exists, try free & unregister \n",
975                          __FUNCTION__, ifp->net->name));
976                         netif_stop_queue(ifp->net);
977                         unregister_netdev(ifp->net);
978                         free_netdev(ifp->net);
979                 }
980                 /* Allocate etherdev, including space for private structure */
981                 if (!(ifp->net = alloc_etherdev(sizeof(dhd)))) {
982                         DHD_ERROR(("%s: OOM - alloc_etherdev\n", __FUNCTION__));
983                         ret = -ENOMEM;
984                 }
985                 if (ret == 0) {
986                         strncpy(ifp->net->name, ifp->name, IFNAMSIZ);
987                         ifp->net->name[IFNAMSIZ - 1] = '\0';
988                         memcpy(netdev_priv(ifp->net), &dhd, sizeof(dhd));
989 #ifdef WL_CFG80211
990                         if (dhd->dhd_state & DHD_ATTACH_STATE_CFG80211)
991                                 if (!wl_cfg80211_notify_ifadd(ifp->net, ifp->idx, ifp->bssidx,
992                                         (void*)dhd_net_attach)) {
993                                         ifp->state = DHD_IF_NONE;
994                                         return;
995                                 }
996 #endif
997                         if ((err = dhd_net_attach(&dhd->pub, ifp->idx)) != 0) {
998                                 DHD_ERROR(("%s: dhd_net_attach failed, err %d\n",
999                                         __FUNCTION__, err));
1000                                 ret = -EOPNOTSUPP;
1001                         } else {
1002 #if defined(SOFTAP)
1003                 if (ap_fw_loaded && !(dhd->dhd_state & DHD_ATTACH_STATE_CFG80211)) {
1004                                  /* semaphore that the soft AP CODE waits on */
1005                                 flags = dhd_os_spin_lock(&dhd->pub);
1006
1007                                 /* save ptr to wl0.1 netdev for use in wl_iw.c  */
1008                                 ap_net_dev = ifp->net;
1009                                  /* signal to the SOFTAP 'sleeper' thread, wl0.1 is ready */
1010                                 up(&ap_eth_ctl.sema);
1011                                 dhd_os_spin_unlock(&dhd->pub, flags);
1012                 }
1013 #endif
1014                                 DHD_TRACE(("\n ==== pid:%x, net_device for if:%s created ===\n\n",
1015                                         current->pid, ifp->net->name));
1016                                 ifp->state = DHD_IF_NONE;
1017                         }
1018                 }
1019                 break;
1020         case DHD_IF_DEL:
1021                 /* Make sure that we don't enter again here if .. */
1022                 /* dhd_op_if is called again from some other context */
1023                 ifp->state = DHD_IF_DELETING;
1024                 if (ifp->net != NULL) {
1025                         DHD_TRACE(("\n%s: got 'DHD_IF_DEL' state\n", __FUNCTION__));
1026 #ifdef WL_CFG80211
1027                         if (dhd->dhd_state & DHD_ATTACH_STATE_CFG80211) {
1028                                 wl_cfg80211_ifdel_ops(ifp->net);
1029                         }
1030 #endif
1031                         netif_stop_queue(ifp->net);
1032                         unregister_netdev(ifp->net);
1033                         ret = DHD_DEL_IF;
1034
1035 #ifdef WL_CFG80211
1036                         if (dhd->dhd_state & DHD_ATTACH_STATE_CFG80211) {
1037                                 wl_cfg80211_notify_ifdel();
1038                         }
1039 #endif
1040                 }
1041                 break;
1042         case DHD_IF_DELETING:
1043                 break;
1044         default:
1045                 DHD_ERROR(("%s: bad op %d\n", __FUNCTION__, ifp->state));
1046                 ASSERT(!ifp->state);
1047                 break;
1048         }
1049
1050         if (ret < 0) {
1051                 ifp->set_multicast = FALSE;
1052                 if (ifp->net) {
1053                         free_netdev(ifp->net);
1054                         ifp->net = NULL;
1055                 }
1056                 dhd->iflist[ifp->idx] = NULL;
1057 #ifdef SOFTAP
1058                 flags = dhd_os_spin_lock(&dhd->pub);
1059                 if (ifp->net == ap_net_dev)
1060                         ap_net_dev = NULL;   /*  NULL  SOFTAP global wl0.1 as well */
1061                 dhd_os_spin_unlock(&dhd->pub, flags);
1062 #endif /*  SOFTAP */
1063                 MFREE(dhd->pub.osh, ifp, sizeof(*ifp));
1064         }
1065 }
1066
1067 static int
1068 _dhd_sysioc_thread(void *data)
1069 {
1070         tsk_ctl_t *tsk = (tsk_ctl_t *)data;
1071         dhd_info_t *dhd = (dhd_info_t *)tsk->parent;
1072
1073
1074         int i;
1075 #ifdef SOFTAP
1076         bool in_ap = FALSE;
1077         unsigned long flags;
1078 #endif
1079
1080         DAEMONIZE("dhd_sysioc");
1081
1082         complete(&tsk->completed);
1083
1084         while (down_interruptible(&tsk->sema) == 0) {
1085
1086                 SMP_RD_BARRIER_DEPENDS();
1087                 if (tsk->terminated) {
1088                         break;
1089                 }
1090
1091                 dhd_net_if_lock_local(dhd);
1092                 DHD_OS_WAKE_LOCK(&dhd->pub);
1093
1094                 for (i = 0; i < DHD_MAX_IFS; i++) {
1095                         if (dhd->iflist[i]) {
1096                                 DHD_TRACE(("%s: interface %d\n", __FUNCTION__, i));
1097 #ifdef SOFTAP
1098                                 flags = dhd_os_spin_lock(&dhd->pub);
1099                                 in_ap = (ap_net_dev != NULL);
1100                                 dhd_os_spin_unlock(&dhd->pub, flags);
1101 #endif /* SOFTAP */
1102                                 if (dhd->iflist[i] && dhd->iflist[i]->state)
1103                                         dhd_op_if(dhd->iflist[i]);
1104
1105                                 if (dhd->iflist[i] == NULL) {
1106                                         DHD_TRACE(("\n\n %s: interface %d just been removed,"
1107                                                 "!\n\n", __FUNCTION__, i));
1108                                         continue;
1109                                 }
1110 #ifdef SOFTAP
1111                                 if (in_ap && dhd->set_macaddress)  {
1112                                         DHD_TRACE(("attempt to set MAC for %s in AP Mode,"
1113                                                 "blocked. \n", dhd->iflist[i]->net->name));
1114                                         dhd->set_macaddress = FALSE;
1115                                         continue;
1116                                 }
1117
1118                                 if (in_ap && dhd->iflist[i]->set_multicast)  {
1119                                         DHD_TRACE(("attempt to set MULTICAST list for %s"
1120                                          "in AP Mode, blocked. \n", dhd->iflist[i]->net->name));
1121                                         dhd->iflist[i]->set_multicast = FALSE;
1122                                         continue;
1123                                 }
1124 #endif /* SOFTAP */
1125                                 if (dhd->iflist[i]->set_multicast) {
1126                                         dhd->iflist[i]->set_multicast = FALSE;
1127                                         _dhd_set_multicast_list(dhd, i);
1128                                 }
1129                                 if (dhd->set_macaddress) {
1130                                         dhd->set_macaddress = FALSE;
1131                                         _dhd_set_mac_address(dhd, i, &dhd->macvalue);
1132                                 }
1133                         }
1134                 }
1135
1136                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
1137                 dhd_net_if_unlock_local(dhd);
1138         }
1139         DHD_TRACE(("%s: stopped\n", __FUNCTION__));
1140         complete_and_exit(&tsk->completed, 0);
1141 }
1142
1143 static int
1144 dhd_set_mac_address(struct net_device *dev, void *addr)
1145 {
1146         int ret = 0;
1147
1148         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
1149         struct sockaddr *sa = (struct sockaddr *)addr;
1150         int ifidx;
1151
1152         ifidx = dhd_net2idx(dhd, dev);
1153         if (ifidx == DHD_BAD_IF)
1154                 return -1;
1155
1156         ASSERT(dhd->thr_sysioc_ctl.thr_pid >= 0);
1157         memcpy(&dhd->macvalue, sa->sa_data, ETHER_ADDR_LEN);
1158         dhd->set_macaddress = TRUE;
1159         up(&dhd->thr_sysioc_ctl.sema);
1160
1161         return ret;
1162 }
1163
1164 static void
1165 dhd_set_multicast_list(struct net_device *dev)
1166 {
1167         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
1168         int ifidx;
1169
1170         ifidx = dhd_net2idx(dhd, dev);
1171         if (ifidx == DHD_BAD_IF)
1172                 return;
1173
1174         ASSERT(dhd->thr_sysioc_ctl.thr_pid >= 0);
1175         dhd->iflist[ifidx]->set_multicast = TRUE;
1176         up(&dhd->thr_sysioc_ctl.sema);
1177 }
1178
1179 #ifdef PROP_TXSTATUS
1180 int
1181 dhd_os_wlfc_block(dhd_pub_t *pub)
1182 {
1183         dhd_info_t *di = (dhd_info_t *)(pub->info);
1184         ASSERT(di != NULL);
1185
1186         spin_lock_bh(&di->wlfc_spinlock);
1187         return 1;
1188 }
1189
1190 int
1191 dhd_os_wlfc_unblock(dhd_pub_t *pub)
1192 {
1193         dhd_info_t *di = (dhd_info_t *)(pub->info);
1194         ASSERT(di != NULL);
1195         spin_unlock_bh(&di->wlfc_spinlock);
1196         return 1;
1197 }
1198
1199 const uint8 wme_fifo2ac[] = { 0, 1, 2, 3, 1, 1 };
1200 uint8 prio2fifo[8] = { 1, 0, 0, 1, 2, 2, 3, 3 };
1201 #define WME_PRIO2AC(prio)       wme_fifo2ac[prio2fifo[(prio)]]
1202
1203 #endif /* PROP_TXSTATUS */
1204 int
1205 dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, void *pktbuf)
1206 {
1207         int ret;
1208         dhd_info_t *dhd = (dhd_info_t *)(dhdp->info);
1209         struct ether_header *eh = NULL;
1210
1211         /* Reject if down */
1212         if (!dhdp->up || (dhdp->busstate == DHD_BUS_DOWN)) {
1213                 /* free the packet here since the caller won't */
1214                 PKTFREE(dhdp->osh, pktbuf, TRUE);
1215                 return -ENODEV;
1216         }
1217
1218         /* Update multicast statistic */
1219         if (PKTLEN(dhdp->osh, pktbuf) >= ETHER_HDR_LEN) {
1220                 uint8 *pktdata = (uint8 *)PKTDATA(dhdp->osh, pktbuf);
1221                 eh = (struct ether_header *)pktdata;
1222
1223                 if (ETHER_ISMULTI(eh->ether_dhost))
1224                         dhdp->tx_multicast++;
1225                 if (ntoh16(eh->ether_type) == ETHER_TYPE_802_1X)
1226                         atomic_inc(&dhd->pend_8021x_cnt);
1227         } else {
1228                 PKTFREE(dhd->pub.osh, pktbuf, TRUE);
1229                 return BCME_ERROR;
1230         }
1231
1232         /* Look into the packet and update the packet priority */
1233         if (PKTPRIO(pktbuf) == 0)
1234                 pktsetprio(pktbuf, FALSE);
1235
1236 #ifdef PROP_TXSTATUS
1237         if (dhdp->wlfc_state) {
1238                 /* store the interface ID */
1239                 DHD_PKTTAG_SETIF(PKTTAG(pktbuf), ifidx);
1240
1241                 /* store destination MAC in the tag as well */
1242                 DHD_PKTTAG_SETDSTN(PKTTAG(pktbuf), eh->ether_dhost);
1243
1244                 /* decide which FIFO this packet belongs to */
1245                 if (ETHER_ISMULTI(eh->ether_dhost))
1246                         /* one additional queue index (highest AC + 1) is used for bc/mc queue */
1247                         DHD_PKTTAG_SETFIFO(PKTTAG(pktbuf), AC_COUNT);
1248                 else
1249                         DHD_PKTTAG_SETFIFO(PKTTAG(pktbuf), WME_PRIO2AC(PKTPRIO(pktbuf)));
1250         } else
1251 #endif /* PROP_TXSTATUS */
1252         /* If the protocol uses a data header, apply it */
1253         dhd_prot_hdrpush(dhdp, ifidx, pktbuf);
1254
1255         /* Use bus module to send data frame */
1256 #ifdef WLMEDIA_HTSF
1257         dhd_htsf_addtxts(dhdp, pktbuf);
1258 #endif
1259 #ifdef PROP_TXSTATUS
1260         if (dhdp->wlfc_state && ((athost_wl_status_info_t*)dhdp->wlfc_state)->proptxstatus_mode
1261                         != WLFC_FCMODE_NONE) {
1262                 dhd_os_wlfc_block(dhdp);
1263                 ret = dhd_wlfc_enque_sendq(dhdp->wlfc_state, DHD_PKTTAG_FIFO(PKTTAG(pktbuf)),
1264                         pktbuf);
1265                 dhd_wlfc_commit_packets(dhdp->wlfc_state,  (f_commitpkt_t)dhd_bus_txdata,
1266                         dhdp->bus);
1267                 if (((athost_wl_status_info_t*)dhdp->wlfc_state)->toggle_host_if) {
1268                         ((athost_wl_status_info_t*)dhdp->wlfc_state)->toggle_host_if = 0;
1269                 }
1270                 dhd_os_wlfc_unblock(dhdp);
1271         }
1272         else
1273                 /* non-proptxstatus way */
1274         ret = dhd_bus_txdata(dhdp->bus, pktbuf);
1275 #else
1276         ret = dhd_bus_txdata(dhdp->bus, pktbuf);
1277 #endif /* PROP_TXSTATUS */
1278
1279
1280         return ret;
1281 }
1282
1283 int
1284 dhd_start_xmit(struct sk_buff *skb, struct net_device *net)
1285 {
1286         int ret;
1287         void *pktbuf;
1288         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(net);
1289         int ifidx;
1290 #ifdef WLMEDIA_HTSF
1291         uint8 htsfdlystat_sz = dhd->pub.htsfdlystat_sz;
1292 #else
1293         uint8 htsfdlystat_sz = 0;
1294 #endif
1295
1296         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
1297
1298         DHD_OS_WAKE_LOCK(&dhd->pub);
1299
1300         /* Reject if down */
1301         if (!dhd->pub.up || (dhd->pub.busstate == DHD_BUS_DOWN)) {
1302                 DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d \n",
1303                         __FUNCTION__, dhd->pub.up, dhd->pub.busstate));
1304                 netif_stop_queue(net);
1305 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
1306                 /* Send Event when bus down detected during data session */
1307                 if (dhd->pub.busstate == DHD_BUS_DOWN)  {
1308                         DHD_ERROR(("%s: Event HANG sent up\n", __FUNCTION__));
1309                         net_os_send_hang_message(net);
1310                 }
1311 #endif
1312                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
1313                 return -ENODEV;
1314         }
1315
1316         ifidx = dhd_net2idx(dhd, net);
1317         if (ifidx == DHD_BAD_IF) {
1318                 DHD_ERROR(("%s: bad ifidx %d\n", __FUNCTION__, ifidx));
1319                 netif_stop_queue(net);
1320                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
1321                 return -ENODEV;
1322         }
1323
1324         /* Make sure there's enough room for any header */
1325
1326         if (skb_headroom(skb) < dhd->pub.hdrlen + htsfdlystat_sz) {
1327                 struct sk_buff *skb2;
1328
1329                 DHD_INFO(("%s: insufficient headroom\n",
1330                           dhd_ifname(&dhd->pub, ifidx)));
1331                 dhd->pub.tx_realloc++;
1332
1333                 skb2 = skb_realloc_headroom(skb, dhd->pub.hdrlen + htsfdlystat_sz);
1334
1335                 dev_kfree_skb(skb);
1336                 if ((skb = skb2) == NULL) {
1337                         DHD_ERROR(("%s: skb_realloc_headroom failed\n",
1338                                    dhd_ifname(&dhd->pub, ifidx)));
1339                         ret = -ENOMEM;
1340                         goto done;
1341                 }
1342         }
1343
1344         /* Convert to packet */
1345         if (!(pktbuf = PKTFRMNATIVE(dhd->pub.osh, skb))) {
1346                 DHD_ERROR(("%s: PKTFRMNATIVE failed\n",
1347                            dhd_ifname(&dhd->pub, ifidx)));
1348                 dev_kfree_skb_any(skb);
1349                 ret = -ENOMEM;
1350                 goto done;
1351         }
1352 #ifdef WLMEDIA_HTSF
1353         if (htsfdlystat_sz && PKTLEN(dhd->pub.osh, pktbuf) >= ETHER_ADDR_LEN) {
1354                 uint8 *pktdata = (uint8 *)PKTDATA(dhd->pub.osh, pktbuf);
1355                 struct ether_header *eh = (struct ether_header *)pktdata;
1356
1357                 if (!ETHER_ISMULTI(eh->ether_dhost) &&
1358                         (ntoh16(eh->ether_type) == ETHER_TYPE_IP)) {
1359                         eh->ether_type = hton16(ETHER_TYPE_BRCM_PKTDLYSTATS);
1360                 }
1361         }
1362 #endif
1363
1364         ret = dhd_sendpkt(&dhd->pub, ifidx, pktbuf);
1365
1366
1367 done:
1368         if (ret)
1369                 dhd->pub.dstats.tx_dropped++;
1370         else
1371                 dhd->pub.tx_packets++;
1372
1373         DHD_OS_WAKE_UNLOCK(&dhd->pub);
1374
1375         /* Return ok: we always eat the packet */
1376         return 0;
1377 }
1378
1379 void
1380 dhd_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool state)
1381 {
1382         struct net_device *net;
1383         dhd_info_t *dhd = dhdp->info;
1384         int i;
1385
1386         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
1387
1388         dhdp->txoff = state;
1389         ASSERT(dhd);
1390
1391         if (ifidx == ALL_INTERFACES) {
1392                 /* Flow control on all active interfaces */
1393                 for (i = 0; i < DHD_MAX_IFS; i++) {
1394                         if (dhd->iflist[i]) {
1395                                 net = dhd->iflist[i]->net;
1396                                 if (state == ON)
1397                                         netif_stop_queue(net);
1398                                 else
1399                                         netif_wake_queue(net);
1400                         }
1401                 }
1402         }
1403         else {
1404                 if (dhd->iflist[ifidx]) {
1405                         net = dhd->iflist[ifidx]->net;
1406                         if (state == ON)
1407                                 netif_stop_queue(net);
1408                         else
1409                                 netif_wake_queue(net);
1410                 }
1411         }
1412 }
1413
1414 void
1415 dhd_rx_frame(dhd_pub_t *dhdp, int ifidx, void *pktbuf, int numpkt, uint8 chan)
1416 {
1417         dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
1418         struct sk_buff *skb;
1419         uchar *eth;
1420         uint len;
1421         void *data, *pnext = NULL, *save_pktbuf;
1422         int i;
1423         dhd_if_t *ifp;
1424         wl_event_msg_t event;
1425         int tout_rx = 0;
1426         int tout_ctrl = 0;
1427
1428         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
1429
1430         save_pktbuf = pktbuf;
1431
1432         for (i = 0; pktbuf && i < numpkt; i++, pktbuf = pnext) {
1433                 struct ether_header *eh;
1434                 struct dot11_llc_snap_header *lsh;
1435
1436                 ifp = dhd->iflist[ifidx];
1437                 if (ifp == NULL) {
1438                         DHD_ERROR(("%s: ifp is NULL. drop packet\n",
1439                                 __FUNCTION__));
1440                         PKTFREE(dhdp->osh, pktbuf, TRUE);
1441                         continue;
1442                 }
1443 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1444                 /* Dropping packets before registering net device to avoid kernel panic */
1445                 if (!ifp->net || ifp->net->reg_state != NETREG_REGISTERED ||
1446                         !dhd->pub.up) {
1447                         DHD_ERROR(("%s: net device is NOT registered yet. drop packet\n",
1448                         __FUNCTION__));
1449                         PKTFREE(dhdp->osh, pktbuf, TRUE);
1450                         continue;
1451                 }
1452 #endif
1453
1454                 pnext = PKTNEXT(dhdp->osh, pktbuf);
1455                 PKTSETNEXT(wl->sh.osh, pktbuf, NULL);
1456
1457                 eh = (struct ether_header *)PKTDATA(wl->sh.osh, pktbuf);
1458                 lsh = (struct dot11_llc_snap_header *)&eh[1];
1459
1460                 if ((ntoh16(eh->ether_type) < ETHER_TYPE_MIN) &&
1461                     (PKTLEN(wl->sh.osh, pktbuf) >= RFC1042_HDR_LEN) &&
1462                     bcmp(lsh, BT_SIG_SNAP_MPROT, DOT11_LLC_SNAP_HDR_LEN - 2) == 0 &&
1463                     lsh->type == HTON16(BTA_PROT_L2CAP)) {
1464                         amp_hci_ACL_data_t *ACL_data = (amp_hci_ACL_data_t *)
1465                                 ((uint8 *)eh + RFC1042_HDR_LEN);
1466                         ACL_data = NULL;
1467                 }
1468
1469 #ifdef PROP_TXSTATUS
1470                 if (dhdp->wlfc_state && PKTLEN(wl->sh.osh, pktbuf) == 0) {
1471                         /* WLFC may send header only packet when
1472                         there is an urgent message but no packet to
1473                         piggy-back on
1474                         */
1475                         ((athost_wl_status_info_t*)dhdp->wlfc_state)->stats.wlfc_header_only_pkt++;
1476                         PKTFREE(dhdp->osh, pktbuf, TRUE);
1477                         DHD_TRACE(("RX: wlfc header \n"));
1478                         continue;
1479                 }
1480 #endif
1481
1482                 skb = PKTTONATIVE(dhdp->osh, pktbuf);
1483
1484                 /* Get the protocol, maintain skb around eth_type_trans()
1485                  * The main reason for this hack is for the limitation of
1486                  * Linux 2.4 where 'eth_type_trans' uses the 'net->hard_header_len'
1487                  * to perform skb_pull inside vs ETH_HLEN. Since to avoid
1488                  * coping of the packet coming from the network stack to add
1489                  * BDC, Hardware header etc, during network interface registration
1490                  * we set the 'net->hard_header_len' to ETH_HLEN + extra space required
1491                  * for BDC, Hardware header etc. and not just the ETH_HLEN
1492                  */
1493                 eth = skb->data;
1494                 len = skb->len;
1495
1496                 ifp = dhd->iflist[ifidx];
1497                 if (ifp == NULL)
1498                         ifp = dhd->iflist[0];
1499
1500                 ASSERT(ifp);
1501                 skb->dev = ifp->net;
1502                 skb->protocol = eth_type_trans(skb, skb->dev);
1503
1504                 if (skb->pkt_type == PACKET_MULTICAST) {
1505                         dhd->pub.rx_multicast++;
1506                 }
1507
1508                 skb->data = eth;
1509                 skb->len = len;
1510
1511 #ifdef WLMEDIA_HTSF
1512         dhd_htsf_addrxts(dhdp, pktbuf);
1513 #endif
1514                 /* Strip header, count, deliver upward */
1515                 skb_pull(skb, ETH_HLEN);
1516
1517                 /* Process special event packets and then discard them */
1518                 if (ntoh16(skb->protocol) == ETHER_TYPE_BRCM) {
1519                         dhd_wl_host_event(dhd, &ifidx,
1520 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
1521                         skb->mac_header,
1522 #else
1523                         skb->mac.raw,
1524 #endif
1525                         &event,
1526                         &data);
1527
1528                         wl_event_to_host_order(&event);
1529                         if (!tout_ctrl)
1530                                 tout_ctrl = DHD_PACKET_TIMEOUT_MS;
1531                         if (event.event_type == WLC_E_BTA_HCI_EVENT) {
1532                                 dhd_bta_doevt(dhdp, data, event.datalen);
1533                         } else if (event.event_type == WLC_E_PFN_NET_FOUND) {
1534                                 tout_ctrl *= 2;
1535                         }
1536                 } else {
1537                         tout_rx = DHD_PACKET_TIMEOUT_MS;
1538                 }
1539
1540                 ASSERT(ifidx < DHD_MAX_IFS && dhd->iflist[ifidx]);
1541                 if (dhd->iflist[ifidx] && !dhd->iflist[ifidx]->state)
1542                         ifp = dhd->iflist[ifidx];
1543
1544                 if (ifp->net)
1545                         ifp->net->last_rx = jiffies;
1546
1547                 dhdp->dstats.rx_bytes += skb->len;
1548                 dhdp->rx_packets++; /* Local count */
1549
1550                 if (in_interrupt()) {
1551                         netif_rx(skb);
1552                 } else {
1553                         /* If the receive is not processed inside an ISR,
1554                          * the softirqd must be woken explicitly to service
1555                          * the NET_RX_SOFTIRQ.  In 2.6 kernels, this is handled
1556                          * by netif_rx_ni(), but in earlier kernels, we need
1557                          * to do it manually.
1558                          */
1559 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1560                         netif_rx_ni(skb);
1561 #else
1562                         ulong flags;
1563                         netif_rx(skb);
1564                         local_irq_save(flags);
1565                         RAISE_RX_SOFTIRQ();
1566                         local_irq_restore(flags);
1567 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) */
1568                 }
1569         }
1570         DHD_OS_WAKE_LOCK_RX_TIMEOUT_ENABLE(dhdp, tout_rx);
1571         DHD_OS_WAKE_LOCK_CTRL_TIMEOUT_ENABLE(dhdp, tout_ctrl);
1572 }
1573
1574 void
1575 dhd_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx)
1576 {
1577         /* Linux version has nothing to do */
1578         return;
1579 }
1580
1581 void
1582 dhd_txcomplete(dhd_pub_t *dhdp, void *txp, bool success)
1583 {
1584         uint ifidx;
1585         dhd_info_t *dhd = (dhd_info_t *)(dhdp->info);
1586         struct ether_header *eh;
1587         uint16 type;
1588         uint len;
1589
1590         dhd_prot_hdrpull(dhdp, &ifidx, txp);
1591
1592         eh = (struct ether_header *)PKTDATA(dhdp->osh, txp);
1593         type  = ntoh16(eh->ether_type);
1594
1595         if (type == ETHER_TYPE_802_1X)
1596                 atomic_dec(&dhd->pend_8021x_cnt);
1597
1598         /* Crack open the packet and check to see if it is BT HCI ACL data packet.
1599          * If yes generate packet completion event.
1600          */
1601         len = PKTLEN(dhdp->osh, txp);
1602
1603         /* Generate ACL data tx completion event locally to avoid SDIO bus transaction */
1604         if ((type < ETHER_TYPE_MIN) && (len >= RFC1042_HDR_LEN)) {
1605                 struct dot11_llc_snap_header *lsh = (struct dot11_llc_snap_header *)&eh[1];
1606
1607                 if (bcmp(lsh, BT_SIG_SNAP_MPROT, DOT11_LLC_SNAP_HDR_LEN - 2) == 0 &&
1608                     ntoh16(lsh->type) == BTA_PROT_L2CAP) {
1609
1610                         dhd_bta_tx_hcidata_complete(dhdp, txp, success);
1611                 }
1612         }
1613 }
1614
1615 static struct net_device_stats *
1616 dhd_get_stats(struct net_device *net)
1617 {
1618         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(net);
1619         dhd_if_t *ifp;
1620         int ifidx;
1621
1622         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
1623
1624         ifidx = dhd_net2idx(dhd, net);
1625         if (ifidx == DHD_BAD_IF) {
1626                 DHD_ERROR(("%s: BAD_IF\n", __FUNCTION__));
1627                 return NULL;
1628         }
1629
1630         ifp = dhd->iflist[ifidx];
1631         ASSERT(dhd && ifp);
1632
1633         if (dhd->pub.up) {
1634                 /* Use the protocol to get dongle stats */
1635                 dhd_prot_dstats(&dhd->pub);
1636         }
1637
1638         /* Copy dongle stats to net device stats */
1639         ifp->stats.rx_packets = dhd->pub.dstats.rx_packets;
1640         ifp->stats.tx_packets = dhd->pub.dstats.tx_packets;
1641         ifp->stats.rx_bytes = dhd->pub.dstats.rx_bytes;
1642         ifp->stats.tx_bytes = dhd->pub.dstats.tx_bytes;
1643         ifp->stats.rx_errors = dhd->pub.dstats.rx_errors;
1644         ifp->stats.tx_errors = dhd->pub.dstats.tx_errors;
1645         ifp->stats.rx_dropped = dhd->pub.dstats.rx_dropped;
1646         ifp->stats.tx_dropped = dhd->pub.dstats.tx_dropped;
1647         ifp->stats.multicast = dhd->pub.dstats.multicast;
1648
1649         return &ifp->stats;
1650 }
1651
1652 #ifdef DHDTHREAD
1653 static int
1654 dhd_watchdog_thread(void *data)
1655 {
1656         tsk_ctl_t *tsk = (tsk_ctl_t *)data;
1657         dhd_info_t *dhd = (dhd_info_t *)tsk->parent;
1658         /* This thread doesn't need any user-level access,
1659          * so get rid of all our resources
1660          */
1661         if (dhd_watchdog_prio > 0) {
1662                 struct sched_param param;
1663                 param.sched_priority = (dhd_watchdog_prio < MAX_RT_PRIO)?
1664                         dhd_watchdog_prio:(MAX_RT_PRIO-1);
1665                 setScheduler(current, SCHED_FIFO, &param);
1666         }
1667
1668         DAEMONIZE("dhd_watchdog");
1669
1670         /* Run until signal received */
1671         complete(&tsk->completed);
1672
1673         while (1)
1674                 if (down_interruptible (&tsk->sema) == 0) {
1675                         unsigned long flags;
1676
1677                         SMP_RD_BARRIER_DEPENDS();
1678                         if (tsk->terminated) {
1679                                 break;
1680                         }
1681
1682                         dhd_os_sdlock(&dhd->pub);
1683                         if (dhd->pub.dongle_reset == FALSE) {
1684                                 DHD_TIMER(("%s:\n", __FUNCTION__));
1685
1686                                 /* Call the bus module watchdog */
1687                                 dhd_bus_watchdog(&dhd->pub);
1688
1689                                 flags = dhd_os_spin_lock(&dhd->pub);
1690                                 /* Count the tick for reference */
1691                                 dhd->pub.tickcnt++;
1692                                 /* Reschedule the watchdog */
1693                                 if (dhd->wd_timer_valid)
1694                                         mod_timer(&dhd->timer,
1695                                         jiffies + dhd_watchdog_ms * HZ / 1000);
1696                                 dhd_os_spin_unlock(&dhd->pub, flags);
1697                         }
1698                         dhd_os_sdunlock(&dhd->pub);
1699                         DHD_OS_WAKE_UNLOCK(&dhd->pub);
1700                 } else {
1701                         break;
1702                 }
1703
1704         complete_and_exit(&tsk->completed, 0);
1705 }
1706 #endif /* DHDTHREAD */
1707
1708 static void dhd_watchdog(ulong data)
1709 {
1710         dhd_info_t *dhd = (dhd_info_t *)data;
1711         unsigned long flags;
1712
1713         DHD_OS_WAKE_LOCK(&dhd->pub);
1714         if (dhd->pub.dongle_reset) {
1715                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
1716                 return;
1717         }
1718
1719 #ifdef DHDTHREAD
1720         if (dhd->thr_wdt_ctl.thr_pid >= 0) {
1721                 up(&dhd->thr_wdt_ctl.sema);
1722                 return;
1723         }
1724 #endif /* DHDTHREAD */
1725
1726         dhd_os_sdlock(&dhd->pub);
1727         /* Call the bus module watchdog */
1728         dhd_bus_watchdog(&dhd->pub);
1729
1730         flags = dhd_os_spin_lock(&dhd->pub);
1731         /* Count the tick for reference */
1732         dhd->pub.tickcnt++;
1733
1734         /* Reschedule the watchdog */
1735         if (dhd->wd_timer_valid)
1736                 mod_timer(&dhd->timer, jiffies + dhd_watchdog_ms * HZ / 1000);
1737         dhd_os_spin_unlock(&dhd->pub, flags);
1738         dhd_os_sdunlock(&dhd->pub);
1739         DHD_OS_WAKE_UNLOCK(&dhd->pub);
1740 }
1741
1742 #ifdef DHDTHREAD
1743 static int
1744 dhd_dpc_thread(void *data)
1745 {
1746         tsk_ctl_t *tsk = (tsk_ctl_t *)data;
1747         dhd_info_t *dhd = (dhd_info_t *)tsk->parent;
1748
1749         /* This thread doesn't need any user-level access,
1750          * so get rid of all our resources
1751          */
1752         if (dhd_dpc_prio > 0)
1753         {
1754                 struct sched_param param;
1755                 param.sched_priority = (dhd_dpc_prio < MAX_RT_PRIO)?dhd_dpc_prio:(MAX_RT_PRIO-1);
1756                 setScheduler(current, SCHED_FIFO, &param);
1757         }
1758
1759         DAEMONIZE("dhd_dpc");
1760         /* DHD_OS_WAKE_LOCK is called in dhd_sched_dpc[dhd_linux.c] down below  */
1761
1762         /*  signal: thread has started */
1763         complete(&tsk->completed);
1764
1765         /* Run until signal received */
1766         while (1) {
1767                 if (down_interruptible(&tsk->sema) == 0) {
1768
1769                         SMP_RD_BARRIER_DEPENDS();
1770                         if (tsk->terminated) {
1771                                 break;
1772                         }
1773
1774                         /* Call bus dpc unless it indicated down (then clean stop) */
1775                         if (dhd->pub.busstate != DHD_BUS_DOWN) {
1776                                 if (dhd_bus_dpc(dhd->pub.bus)) {
1777                                         up(&tsk->sema);
1778                                 }
1779                                 else {
1780                                         DHD_OS_WAKE_UNLOCK(&dhd->pub);
1781                                 }
1782                         } else {
1783                                 if (dhd->pub.up)
1784                                         dhd_bus_stop(dhd->pub.bus, TRUE);
1785                                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
1786                         }
1787                 }
1788                 else
1789                         break;
1790         }
1791
1792         complete_and_exit(&tsk->completed, 0);
1793 }
1794 #endif /* DHDTHREAD */
1795
1796 static void
1797 dhd_dpc(ulong data)
1798 {
1799         dhd_info_t *dhd;
1800
1801         dhd = (dhd_info_t *)data;
1802
1803         /* this (tasklet) can be scheduled in dhd_sched_dpc[dhd_linux.c]
1804          * down below , wake lock is set,
1805          * the tasklet is initialized in dhd_attach()
1806          */
1807         /* Call bus dpc unless it indicated down (then clean stop) */
1808         if (dhd->pub.busstate != DHD_BUS_DOWN) {
1809                 if (dhd_bus_dpc(dhd->pub.bus))
1810                         tasklet_schedule(&dhd->tasklet);
1811                 else
1812                         DHD_OS_WAKE_UNLOCK(&dhd->pub);
1813         } else {
1814                 dhd_bus_stop(dhd->pub.bus, TRUE);
1815                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
1816         }
1817 }
1818
1819 void
1820 dhd_sched_dpc(dhd_pub_t *dhdp)
1821 {
1822         dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
1823
1824         DHD_OS_WAKE_LOCK(dhdp);
1825 #ifdef DHDTHREAD
1826         if (dhd->thr_dpc_ctl.thr_pid >= 0) {
1827                 up(&dhd->thr_dpc_ctl.sema);
1828                 return;
1829         }
1830 #endif /* DHDTHREAD */
1831
1832         tasklet_schedule(&dhd->tasklet);
1833 }
1834
1835 #ifdef TOE
1836 /* Retrieve current toe component enables, which are kept as a bitmap in toe_ol iovar */
1837 static int
1838 dhd_toe_get(dhd_info_t *dhd, int ifidx, uint32 *toe_ol)
1839 {
1840         wl_ioctl_t ioc;
1841         char buf[32];
1842         int ret;
1843
1844         memset(&ioc, 0, sizeof(ioc));
1845
1846         ioc.cmd = WLC_GET_VAR;
1847         ioc.buf = buf;
1848         ioc.len = (uint)sizeof(buf);
1849         ioc.set = FALSE;
1850
1851         strcpy(buf, "toe_ol");
1852         if ((ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len)) < 0) {
1853                 /* Check for older dongle image that doesn't support toe_ol */
1854                 if (ret == -EIO) {
1855                         DHD_ERROR(("%s: toe not supported by device\n",
1856                                 dhd_ifname(&dhd->pub, ifidx)));
1857                         return -EOPNOTSUPP;
1858                 }
1859
1860                 DHD_INFO(("%s: could not get toe_ol: ret=%d\n", dhd_ifname(&dhd->pub, ifidx), ret));
1861                 return ret;
1862         }
1863
1864         memcpy(toe_ol, buf, sizeof(uint32));
1865         return 0;
1866 }
1867
1868 /* Set current toe component enables in toe_ol iovar, and set toe global enable iovar */
1869 static int
1870 dhd_toe_set(dhd_info_t *dhd, int ifidx, uint32 toe_ol)
1871 {
1872         wl_ioctl_t ioc;
1873         char buf[32];
1874         int toe, ret;
1875
1876         memset(&ioc, 0, sizeof(ioc));
1877
1878         ioc.cmd = WLC_SET_VAR;
1879         ioc.buf = buf;
1880         ioc.len = (uint)sizeof(buf);
1881         ioc.set = TRUE;
1882
1883         /* Set toe_ol as requested */
1884
1885         strcpy(buf, "toe_ol");
1886         memcpy(&buf[sizeof("toe_ol")], &toe_ol, sizeof(uint32));
1887
1888         if ((ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len)) < 0) {
1889                 DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
1890                         dhd_ifname(&dhd->pub, ifidx), ret));
1891                 return ret;
1892         }
1893
1894         /* Enable toe globally only if any components are enabled. */
1895
1896         toe = (toe_ol != 0);
1897
1898         strcpy(buf, "toe");
1899         memcpy(&buf[sizeof("toe")], &toe, sizeof(uint32));
1900
1901         if ((ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len)) < 0) {
1902                 DHD_ERROR(("%s: could not set toe: ret=%d\n", dhd_ifname(&dhd->pub, ifidx), ret));
1903                 return ret;
1904         }
1905
1906         return 0;
1907 }
1908 #endif /* TOE */
1909
1910 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
1911 static void
1912 dhd_ethtool_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
1913 {
1914         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(net);
1915
1916         sprintf(info->driver, "wl");
1917         sprintf(info->version, "%lu", dhd->pub.drv_version);
1918 }
1919
1920 struct ethtool_ops dhd_ethtool_ops = {
1921         .get_drvinfo = dhd_ethtool_get_drvinfo
1922 };
1923 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) */
1924
1925
1926 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 2)
1927 static int
1928 dhd_ethtool(dhd_info_t *dhd, void *uaddr)
1929 {
1930         struct ethtool_drvinfo info;
1931         char drvname[sizeof(info.driver)];
1932         uint32 cmd;
1933 #ifdef TOE
1934         struct ethtool_value edata;
1935         uint32 toe_cmpnt, csum_dir;
1936         int ret;
1937 #endif
1938
1939         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
1940
1941         /* all ethtool calls start with a cmd word */
1942         if (copy_from_user(&cmd, uaddr, sizeof (uint32)))
1943                 return -EFAULT;
1944
1945         switch (cmd) {
1946         case ETHTOOL_GDRVINFO:
1947                 /* Copy out any request driver name */
1948                 if (copy_from_user(&info, uaddr, sizeof(info)))
1949                         return -EFAULT;
1950                 strncpy(drvname, info.driver, sizeof(info.driver));
1951                 drvname[sizeof(info.driver)-1] = '\0';
1952
1953                 /* clear struct for return */
1954                 memset(&info, 0, sizeof(info));
1955                 info.cmd = cmd;
1956
1957                 /* if dhd requested, identify ourselves */
1958                 if (strcmp(drvname, "?dhd") == 0) {
1959                         sprintf(info.driver, "dhd");
1960                         strcpy(info.version, EPI_VERSION_STR);
1961                 }
1962
1963                 /* otherwise, require dongle to be up */
1964                 else if (!dhd->pub.up) {
1965                         DHD_ERROR(("%s: dongle is not up\n", __FUNCTION__));
1966                         return -ENODEV;
1967                 }
1968
1969                 /* finally, report dongle driver type */
1970                 else if (dhd->pub.iswl)
1971                         sprintf(info.driver, "wl");
1972                 else
1973                         sprintf(info.driver, "xx");
1974
1975                 sprintf(info.version, "%lu", dhd->pub.drv_version);
1976                 if (copy_to_user(uaddr, &info, sizeof(info)))
1977                         return -EFAULT;
1978                 DHD_CTL(("%s: given %*s, returning %s\n", __FUNCTION__,
1979                          (int)sizeof(drvname), drvname, info.driver));
1980                 break;
1981
1982 #ifdef TOE
1983         /* Get toe offload components from dongle */
1984         case ETHTOOL_GRXCSUM:
1985         case ETHTOOL_GTXCSUM:
1986                 if ((ret = dhd_toe_get(dhd, 0, &toe_cmpnt)) < 0)
1987                         return ret;
1988
1989                 csum_dir = (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
1990
1991                 edata.cmd = cmd;
1992                 edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
1993
1994                 if (copy_to_user(uaddr, &edata, sizeof(edata)))
1995                         return -EFAULT;
1996                 break;
1997
1998         /* Set toe offload components in dongle */
1999         case ETHTOOL_SRXCSUM:
2000         case ETHTOOL_STXCSUM:
2001                 if (copy_from_user(&edata, uaddr, sizeof(edata)))
2002                         return -EFAULT;
2003
2004                 /* Read the current settings, update and write back */
2005                 if ((ret = dhd_toe_get(dhd, 0, &toe_cmpnt)) < 0)
2006                         return ret;
2007
2008                 csum_dir = (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
2009
2010                 if (edata.data != 0)
2011                         toe_cmpnt |= csum_dir;
2012                 else
2013                         toe_cmpnt &= ~csum_dir;
2014
2015                 if ((ret = dhd_toe_set(dhd, 0, toe_cmpnt)) < 0)
2016                         return ret;
2017
2018                 /* If setting TX checksum mode, tell Linux the new mode */
2019                 if (cmd == ETHTOOL_STXCSUM) {
2020                         if (edata.data)
2021                                 dhd->iflist[0]->net->features |= NETIF_F_IP_CSUM;
2022                         else
2023                                 dhd->iflist[0]->net->features &= ~NETIF_F_IP_CSUM;
2024                 }
2025
2026                 break;
2027 #endif /* TOE */
2028
2029         default:
2030                 return -EOPNOTSUPP;
2031         }
2032
2033         return 0;
2034 }
2035 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 2) */
2036
2037 static bool dhd_check_hang(struct net_device *net, dhd_pub_t *dhdp, int error)
2038 {
2039 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
2040         if (!dhdp)
2041                 return FALSE;
2042         if ((error == -ETIMEDOUT) || ((dhdp->busstate == DHD_BUS_DOWN) &&
2043                 (!dhdp->dongle_reset))) {
2044                 DHD_ERROR(("%s: Event HANG send up due to  re=%d te=%d e=%d s=%d\n", __FUNCTION__,
2045                         dhdp->rxcnt_timeout, dhdp->txcnt_timeout, error, dhdp->busstate));
2046                 net_os_send_hang_message(net);
2047                 return TRUE;
2048         }
2049 #endif
2050         return FALSE;
2051 }
2052
2053 static int
2054 dhd_ioctl_entry(struct net_device *net, struct ifreq *ifr, int cmd)
2055 {
2056         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(net);
2057         dhd_ioctl_t ioc;
2058         int bcmerror = 0;
2059         int buflen = 0;
2060         void *buf = NULL;
2061         uint driver = 0;
2062         int ifidx;
2063         int ret;
2064
2065         DHD_OS_WAKE_LOCK(&dhd->pub);
2066
2067         /* send to dongle only if we are not waiting for reload already */
2068         if (dhd->pub.hang_was_sent) {
2069                 DHD_ERROR(("%s: HANG was sent up earlier\n", __FUNCTION__));
2070                 DHD_OS_WAKE_LOCK_CTRL_TIMEOUT_ENABLE(&dhd->pub, DHD_EVENT_TIMEOUT_MS);
2071                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
2072                 return OSL_ERROR(BCME_DONGLE_DOWN);
2073         }
2074
2075         ifidx = dhd_net2idx(dhd, net);
2076         DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __FUNCTION__, ifidx, cmd));
2077
2078         if (ifidx == DHD_BAD_IF) {
2079                 DHD_ERROR(("%s: BAD IF\n", __FUNCTION__));
2080                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
2081                 return -1;
2082         }
2083
2084 #if defined(CONFIG_BCMDHD_WEXT)
2085         /* linux wireless extensions */
2086         if ((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST)) {
2087                 /* may recurse, do NOT lock */
2088                 ret = wl_iw_ioctl(net, ifr, cmd);
2089                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
2090                 return ret;
2091         }
2092 #endif /* defined(CONFIG_BCMDHD_WEXT) */
2093
2094 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 2)
2095         if (cmd == SIOCETHTOOL) {
2096                 ret = dhd_ethtool(dhd, (void*)ifr->ifr_data);
2097                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
2098                 return ret;
2099         }
2100 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 2) */
2101
2102         if (cmd == SIOCDEVPRIVATE+1) {
2103                 ret = wl_android_priv_cmd(net, ifr, cmd);
2104                 dhd_check_hang(net, &dhd->pub, ret);
2105                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
2106                 return ret;
2107         }
2108
2109         if (cmd != SIOCDEVPRIVATE) {
2110                 DHD_OS_WAKE_UNLOCK(&dhd->pub);
2111                 return -EOPNOTSUPP;
2112         }
2113
2114         memset(&ioc, 0, sizeof(ioc));
2115
2116         /* Copy the ioc control structure part of ioctl request */
2117         if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
2118                 bcmerror = -BCME_BADADDR;
2119                 goto done;
2120         }
2121
2122         /* Copy out any buffer passed */
2123         if (ioc.buf) {
2124                 buflen = MIN(ioc.len, DHD_IOCTL_MAXLEN);
2125                 /* optimization for direct ioctl calls from kernel */
2126                 /*
2127                 if (segment_eq(get_fs(), KERNEL_DS)) {
2128                         buf = ioc.buf;
2129                 } else {
2130                 */
2131                 {
2132                         if (!(buf = (char*)MALLOC(dhd->pub.osh, buflen))) {
2133                                 bcmerror = -BCME_NOMEM;
2134                                 goto done;
2135                         }
2136                         if (copy_from_user(buf, ioc.buf, buflen)) {
2137                                 bcmerror = -BCME_BADADDR;
2138                                 goto done;
2139                         }
2140                 }
2141         }
2142
2143         /* To differentiate between wl and dhd read 4 more byes */
2144         if ((copy_from_user(&driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
2145                 sizeof(uint)) != 0)) {
2146                 bcmerror = -BCME_BADADDR;
2147                 goto done;
2148         }
2149
2150         if (!capable(CAP_NET_ADMIN)) {
2151                 bcmerror = -BCME_EPERM;
2152                 goto done;
2153         }
2154
2155         /* check for local dhd ioctl and handle it */
2156         if (driver == DHD_IOCTL_MAGIC) {
2157                 bcmerror = dhd_ioctl((void *)&dhd->pub, &ioc, buf, buflen);
2158                 if (bcmerror)
2159                         dhd->pub.bcmerror = bcmerror;
2160                 goto done;
2161         }
2162
2163         /* send to dongle (must be up, and wl). */
2164         if (dhd->pub.busstate != DHD_BUS_DATA) {
2165                 bcmerror = BCME_DONGLE_DOWN;
2166                 goto done;
2167         }
2168
2169         if (!dhd->pub.iswl) {
2170                 bcmerror = BCME_DONGLE_DOWN;
2171                 goto done;
2172         }
2173
2174         /*
2175          * Flush the TX queue if required for proper message serialization:
2176          * Intercept WLC_SET_KEY IOCTL - serialize M4 send and set key IOCTL to
2177          * prevent M4 encryption and
2178          * intercept WLC_DISASSOC IOCTL - serialize WPS-DONE and WLC_DISASSOC IOCTL to
2179          * prevent disassoc frame being sent before WPS-DONE frame.
2180          */
2181         if (ioc.cmd == WLC_SET_KEY ||
2182             (ioc.cmd == WLC_SET_VAR && ioc.buf != NULL &&
2183              strncmp("wsec_key", ioc.buf, 9) == 0) ||
2184             (ioc.cmd == WLC_SET_VAR && ioc.buf != NULL &&
2185              strncmp("bsscfg:wsec_key", ioc.buf, 15) == 0) ||
2186             ioc.cmd == WLC_DISASSOC)
2187                 dhd_wait_pend8021x(net);
2188
2189 #ifdef WLMEDIA_HTSF
2190         if (ioc.buf) {
2191                 /*  short cut wl ioctl calls here  */
2192                 if (strcmp("htsf", ioc.buf) == 0) {
2193                         dhd_ioctl_htsf_get(dhd, 0);
2194                         return BCME_OK;
2195                 }
2196
2197                 if (strcmp("htsflate", ioc.buf) == 0) {
2198                         if (ioc.set) {
2199                                 memset(ts, 0, sizeof(tstamp_t)*TSMAX);
2200                                 memset(&maxdelayts, 0, sizeof(tstamp_t));
2201                                 maxdelay = 0;
2202                                 tspktcnt = 0;
2203                                 maxdelaypktno = 0;
2204                                 memset(&vi_d1.bin, 0, sizeof(uint32)*NUMBIN);
2205                                 memset(&vi_d2.bin, 0, sizeof(uint32)*NUMBIN);
2206                                 memset(&vi_d3.bin, 0, sizeof(uint32)*NUMBIN);
2207                                 memset(&vi_d4.bin, 0, sizeof(uint32)*NUMBIN);
2208                         } else {
2209                                 dhd_dump_latency();
2210                         }
2211                         return BCME_OK;
2212                 }
2213                 if (strcmp("htsfclear", ioc.buf) == 0) {
2214                         memset(&vi_d1.bin, 0, sizeof(uint32)*NUMBIN);
2215                         memset(&vi_d2.bin, 0, sizeof(uint32)*NUMBIN);
2216                         memset(&vi_d3.bin, 0, sizeof(uint32)*NUMBIN);
2217                         memset(&vi_d4.bin, 0, sizeof(uint32)*NUMBIN);
2218                         htsf_seqnum = 0;
2219                         return BCME_OK;
2220                 }
2221                 if (strcmp("htsfhis", ioc.buf) == 0) {
2222                         dhd_dump_htsfhisto(&vi_d1, "H to D");
2223                         dhd_dump_htsfhisto(&vi_d2, "D to D");
2224                         dhd_dump_htsfhisto(&vi_d3, "D to H");
2225                         dhd_dump_htsfhisto(&vi_d4, "H to H");
2226                         return BCME_OK;
2227                 }
2228                 if (strcmp("tsport", ioc.buf) == 0) {
2229                         if (ioc.set) {
2230                                 memcpy(&tsport, ioc.buf + 7, 4);
2231                         } else {
2232                                 DHD_ERROR(("current timestamp port: %d \n", tsport));
2233                         }
2234                         return BCME_OK;
2235                 }
2236         }
2237 #endif /* WLMEDIA_HTSF */
2238
2239         bcmerror = dhd_wl_ioctl(&dhd->pub, ifidx, (wl_ioctl_t *)&ioc, buf, buflen);
2240
2241 done:
2242         dhd_check_hang(net, &dhd->pub, bcmerror);
2243
2244         if (!bcmerror && buf && ioc.buf) {
2245                 if (copy_to_user(ioc.buf, buf, buflen))
2246                         bcmerror = -EFAULT;
2247         }
2248
2249         if (buf)
2250                 MFREE(dhd->pub.osh, buf, buflen);
2251
2252         DHD_OS_WAKE_UNLOCK(&dhd->pub);
2253
2254         return OSL_ERROR(bcmerror);
2255 }
2256
2257 #ifdef WL_CFG80211
2258 static int
2259 dhd_cleanup_virt_ifaces(dhd_info_t *dhd)
2260 {
2261         int i = 1; /* Leave ifidx 0 [Primary Interface] */
2262 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
2263         int rollback_lock = FALSE;
2264 #endif
2265
2266         DHD_TRACE(("%s: Enter \n", __func__));
2267
2268 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
2269         /* release lock for unregister_netdev */
2270         if (rtnl_is_locked()) {
2271                 rtnl_unlock();
2272                 rollback_lock = TRUE;
2273         }
2274 #endif
2275
2276         for (i = 1; i < DHD_MAX_IFS; i++) {
2277                 dhd_net_if_lock_local(dhd);
2278                 if (dhd->iflist[i]) {
2279                         DHD_TRACE(("Deleting IF: %d \n", i));
2280                         if ((dhd->iflist[i]->state != DHD_IF_DEL) &&
2281                                 (dhd->iflist[i]->state != DHD_IF_DELETING)) {
2282                                 dhd->iflist[i]->state = DHD_IF_DEL;
2283                                 dhd->iflist[i]->idx = i;
2284                                 dhd_op_if(dhd->iflist[i]);
2285                         }
2286                 }
2287                 dhd_net_if_unlock_local(dhd);
2288         }
2289
2290 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
2291         if (rollback_lock)
2292                 rtnl_lock();
2293 #endif
2294
2295         return 0;
2296 }
2297 #endif /* WL_CFG80211 */
2298
2299 static int
2300 dhd_stop(struct net_device *net)
2301 {
2302         int ifidx = 0;
2303         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(net);
2304         DHD_OS_WAKE_LOCK(&dhd->pub);
2305         DHD_TRACE(("%s: Enter %p\n", __FUNCTION__, net));
2306         if (dhd->pub.up == 0) {
2307                 goto exit;
2308         }
2309         ifidx = dhd_net2idx(dhd, net);
2310
2311 #ifdef WL_CFG80211
2312         if (ifidx == 0) {
2313                 wl_cfg80211_down(NULL);
2314
2315                 /*
2316                  * For CFG80211: Clean up all the left over virtual interfaces
2317                  * when the primary Interface is brought down. [ifconfig wlan0 down]
2318                  */
2319                 if ((dhd->dhd_state & DHD_ATTACH_STATE_ADD_IF) &&
2320                         (dhd->dhd_state & DHD_ATTACH_STATE_CFG80211)) {
2321                         dhd_cleanup_virt_ifaces(dhd);
2322                 }
2323         }
2324 #endif
2325
2326 #ifdef PROP_TXSTATUS
2327         dhd_wlfc_cleanup(&dhd->pub);
2328 #endif
2329         /* Set state and stop OS transmissions */
2330         dhd->pub.up = 0;
2331         netif_stop_queue(net);
2332
2333         /* Stop the protocol module */
2334         dhd_prot_stop(&dhd->pub);
2335
2336         OLD_MOD_DEC_USE_COUNT;
2337 exit:
2338 #if defined(WL_CFG80211)
2339         if (ifidx == 0 && !dhd_download_fw_on_driverload)
2340                 wl_android_wifi_off(net);
2341 #endif
2342         dhd->pub.rxcnt_timeout = 0;
2343         dhd->pub.txcnt_timeout = 0;
2344
2345         DHD_OS_WAKE_UNLOCK(&dhd->pub);
2346         return 0;
2347 }
2348
2349 static int
2350 dhd_open(struct net_device *net)
2351 {
2352         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(net);
2353
2354 #ifdef TOE
2355         uint32 toe_ol;
2356 #endif
2357         int ifidx;
2358         int32 ret = 0;
2359
2360         DHD_OS_WAKE_LOCK(&dhd->pub);
2361         /* Update FW path if it was changed */
2362         if ((firmware_path != NULL) && (firmware_path[0] != '\0')) {
2363                 if (firmware_path[strlen(firmware_path)-1] == '\n')
2364                         firmware_path[strlen(firmware_path)-1] = '\0';
2365                 strcpy(fw_path, firmware_path);
2366                 firmware_path[0] = '\0';
2367         }
2368
2369         dhd->pub.hang_was_sent = 0;
2370 #if !defined(WL_CFG80211)
2371         /*
2372          * Force start if ifconfig_up gets called before START command
2373          * We keep WEXT's wl_control_wl_start to provide backward compatibility
2374          * This should be removed in the future
2375          */
2376         ret = wl_control_wl_start(net);
2377         if (ret != 0) {
2378                 DHD_ERROR(("%s: failed with code %d\n", __FUNCTION__, ret));
2379                 ret = -1;
2380                 goto exit;
2381         }
2382 #endif
2383
2384         ifidx = dhd_net2idx(dhd, net);
2385         DHD_TRACE(("%s: ifidx %d\n", __FUNCTION__, ifidx));
2386
2387         if (ifidx < 0) {
2388                 DHD_ERROR(("%s: Error: called with invalid IF\n", __FUNCTION__));
2389                 ret = -1;
2390                 goto exit;
2391         }
2392
2393         if (!dhd->iflist[ifidx] || dhd->iflist[ifidx]->state == DHD_IF_DEL) {
2394                 DHD_ERROR(("%s: Error: called when IF already deleted\n", __FUNCTION__));
2395                 ret = -1;
2396                 goto exit;
2397         }
2398
2399         if (ifidx == 0) {
2400                 atomic_set(&dhd->pend_8021x_cnt, 0);
2401 #if defined(WL_CFG80211)
2402                 DHD_ERROR(("\n%s\n", dhd_version));
2403                 if (!dhd_download_fw_on_driverload) {
2404                         ret = wl_android_wifi_on(net);
2405                         if (ret != 0) {
2406                                 DHD_ERROR(("%s: failed with code %d\n", __FUNCTION__, ret));
2407                                 ret = -1;
2408                                 goto exit;
2409                         }
2410                 }
2411 #endif /* defined(WL_CFG80211) */
2412
2413                 if (dhd->pub.busstate != DHD_BUS_DATA) {
2414
2415                         /* try to bring up bus */
2416                         if ((ret = dhd_bus_start(&dhd->pub)) != 0) {
2417                                 DHD_ERROR(("%s: failed with code %d\n", __FUNCTION__, ret));
2418                                 ret = -1;
2419                                 goto exit;
2420                         }
2421
2422                 }
2423
2424                 /* dhd_prot_init has been called in dhd_bus_start or wl_android_wifi_on */
2425                 memcpy(net->dev_addr, dhd->pub.mac.octet, ETHER_ADDR_LEN);
2426
2427 #ifdef TOE
2428                 /* Get current TOE mode from dongle */
2429                 if (dhd_toe_get(dhd, ifidx, &toe_ol) >= 0 && (toe_ol & TOE_TX_CSUM_OL) != 0)
2430                         dhd->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
2431                 else
2432                         dhd->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
2433 #endif /* TOE */
2434
2435 #if defined(WL_CFG80211)
2436                 if (unlikely(wl_cfg80211_up(NULL))) {
2437                         DHD_ERROR(("%s: failed to bring up cfg80211\n", __FUNCTION__));
2438                         ret = -1;
2439                         goto exit;
2440                 }
2441 #endif /* WL_CFG80211 */
2442         }
2443
2444         /* Allow transmit calls */
2445         netif_start_queue(net);
2446         dhd->pub.up = 1;
2447
2448 #ifdef BCMDBGFS
2449         dhd_dbg_init(&dhd->pub);
2450 #endif
2451
2452         OLD_MOD_INC_USE_COUNT;
2453 exit:
2454         if (ret)
2455                 dhd_stop(net);
2456
2457         DHD_OS_WAKE_UNLOCK(&dhd->pub);
2458         return ret;
2459 }
2460
2461 int dhd_do_driver_init(struct net_device *net)
2462 {
2463         dhd_info_t *dhd = NULL;
2464
2465         if (!net) {
2466                 DHD_ERROR(("Primary Interface not initialized \n"));
2467                 return -EINVAL;
2468         }
2469
2470         dhd = *(dhd_info_t **)netdev_priv(net);
2471
2472         /* If driver is already initialized, do nothing
2473          */
2474         if (dhd->pub.busstate == DHD_BUS_DATA) {
2475                 DHD_TRACE(("Driver already Inititalized. Nothing to do"));
2476                 return 0;
2477         }
2478
2479         if (dhd_open(net) < 0) {
2480                 DHD_ERROR(("Driver Init Failed \n"));
2481                 return -1;
2482         }
2483
2484         return 0;
2485 }
2486
2487 osl_t *
2488 dhd_osl_attach(void *pdev, uint bustype)
2489 {
2490         return osl_attach(pdev, bustype, TRUE);
2491 }
2492
2493 void
2494 dhd_osl_detach(osl_t *osh)
2495 {
2496         if (MALLOCED(osh)) {
2497                 DHD_ERROR(("%s: MEMORY LEAK %d bytes\n", __FUNCTION__, MALLOCED(osh)));
2498         }
2499         osl_detach(osh);
2500 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
2501         up(&dhd_registration_sem);
2502 #endif
2503 }
2504
2505 int
2506 dhd_add_if(dhd_info_t *dhd, int ifidx, void *handle, char *name,
2507         uint8 *mac_addr, uint32 flags, uint8 bssidx)
2508 {
2509         dhd_if_t *ifp;
2510
2511         DHD_TRACE(("%s: idx %d, handle->%p\n", __FUNCTION__, ifidx, handle));
2512
2513         ASSERT(dhd && (ifidx < DHD_MAX_IFS));
2514
2515         ifp = dhd->iflist[ifidx];
2516         if (ifp != NULL) {
2517                 if (ifp->net != NULL) {
2518                         netif_stop_queue(ifp->net);
2519                         unregister_netdev(ifp->net);
2520                         free_netdev(ifp->net);
2521                 }
2522         } else
2523                 if ((ifp = MALLOC(dhd->pub.osh, sizeof(dhd_if_t))) == NULL) {
2524                         DHD_ERROR(("%s: OOM - dhd_if_t\n", __FUNCTION__));
2525                         return -ENOMEM;
2526                 }
2527
2528         memset(ifp, 0, sizeof(dhd_if_t));
2529         ifp->info = dhd;
2530         dhd->iflist[ifidx] = ifp;
2531         strncpy(ifp->name, name, IFNAMSIZ);
2532         ifp->name[IFNAMSIZ] = '\0';
2533         if (mac_addr != NULL)
2534                 memcpy(&ifp->mac_addr, mac_addr, ETHER_ADDR_LEN);
2535
2536         if (handle == NULL) {
2537                 ifp->state = DHD_IF_ADD;
2538                 ifp->idx = ifidx;
2539                 ifp->bssidx = bssidx;
2540                 ASSERT(dhd->thr_sysioc_ctl.thr_pid >= 0);
2541                 up(&dhd->thr_sysioc_ctl.sema);
2542         } else
2543                 ifp->net = (struct net_device *)handle;
2544
2545         return 0;
2546 }
2547
2548 void
2549 dhd_del_if(dhd_info_t *dhd, int ifidx)
2550 {
2551         dhd_if_t *ifp;
2552
2553         DHD_TRACE(("%s: idx %d\n", __FUNCTION__, ifidx));
2554
2555         ASSERT(dhd && ifidx && (ifidx < DHD_MAX_IFS));
2556         ifp = dhd->iflist[ifidx];
2557         if (!ifp) {
2558                 DHD_ERROR(("%s: Null interface\n", __FUNCTION__));
2559                 return;
2560         }
2561
2562         ifp->state = DHD_IF_DEL;
2563         ifp->idx = ifidx;
2564         ASSERT(dhd->thr_sysioc_ctl.thr_pid >= 0);
2565         up(&dhd->thr_sysioc_ctl.sema);
2566 }
2567
2568 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
2569 static struct net_device_ops dhd_ops_pri = {
2570        .ndo_open = dhd_open,
2571        .ndo_stop = dhd_stop,
2572        .ndo_get_stats = dhd_get_stats,
2573        .ndo_do_ioctl = dhd_ioctl_entry,
2574        .ndo_start_xmit = dhd_start_xmit,
2575        .ndo_set_mac_address = dhd_set_mac_address,
2576        .ndo_set_multicast_list = dhd_set_multicast_list,
2577 };
2578
2579 static struct net_device_ops dhd_ops_virt = {
2580        .ndo_get_stats = dhd_get_stats,
2581        .ndo_do_ioctl = dhd_ioctl_entry,
2582        .ndo_start_xmit = dhd_start_xmit,
2583        .ndo_set_mac_address = dhd_set_mac_address,
2584        .ndo_set_multicast_list = dhd_set_multicast_list,
2585 };
2586 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) */
2587
2588 dhd_pub_t *
2589 dhd_attach(osl_t *osh, struct dhd_bus *bus, uint bus_hdrlen, void *dev)
2590 {
2591         dhd_info_t *dhd = NULL;
2592         struct net_device *net = NULL;
2593
2594         dhd_attach_states_t dhd_state = DHD_ATTACH_STATE_INIT;
2595         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
2596
2597         /* updates firmware nvram path if it was provided as module parameters */
2598         if ((firmware_path != NULL) && (firmware_path[0] != '\0'))
2599                 strcpy(fw_path, firmware_path);
2600         if ((nvram_path != NULL) && (nvram_path[0] != '\0'))
2601                 strcpy(nv_path, nvram_path);
2602
2603         /* Allocate etherdev, including space for private structure */
2604         if (!(net = alloc_etherdev(sizeof(dhd)))) {
2605                 DHD_ERROR(("%s: OOM - alloc_etherdev\n", __FUNCTION__));
2606                 goto fail;
2607         }
2608         dhd_state |= DHD_ATTACH_STATE_NET_ALLOC;
2609
2610         SET_NETDEV_DEV(net, (struct device *)dev);
2611         /* Allocate primary dhd_info */
2612         if (!(dhd = MALLOC(osh, sizeof(dhd_info_t)))) {
2613                 DHD_ERROR(("%s: OOM - alloc dhd_info\n", __FUNCTION__));
2614                 goto fail;
2615         }
2616         memset(dhd, 0, sizeof(dhd_info_t));
2617
2618 #ifdef DHDTHREAD
2619         dhd->thr_dpc_ctl.thr_pid = DHD_PID_KT_TL_INVALID;
2620         dhd->thr_wdt_ctl.thr_pid = DHD_PID_KT_INVALID;
2621 #else
2622         dhd->dhd_tasklet_create = FALSE;
2623 #endif /* DHDTHREAD */
2624         dhd->thr_sysioc_ctl.thr_pid = DHD_PID_KT_INVALID;
2625         dhd_state |= DHD_ATTACH_STATE_DHD_ALLOC;
2626
2627         /*
2628          * Save the dhd_info into the priv
2629          */
2630         memcpy((void *)netdev_priv(net), &dhd, sizeof(dhd));
2631         dhd->pub.osh = osh;
2632
2633         /* Link to info module */
2634         dhd->pub.info = dhd;
2635         /* Link to bus module */
2636         dhd->pub.bus = bus;
2637         dhd->pub.hdrlen = bus_hdrlen;
2638
2639         /* Set network interface name if it was provided as module parameter */
2640         if (iface_name[0]) {
2641                 int len;
2642                 char ch;
2643                 strncpy(net->name, iface_name, IFNAMSIZ);
2644                 net->name[IFNAMSIZ - 1] = 0;
2645                 len = strlen(net->name);
2646                 ch = net->name[len - 1];
2647                 if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
2648                         strcat(net->name, "%d");
2649         }
2650
2651         if (dhd_add_if(dhd, 0, (void *)net, net->name, NULL, 0, 0) == DHD_BAD_IF)
2652                 goto fail;
2653         dhd_state |= DHD_ATTACH_STATE_ADD_IF;
2654
2655 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
2656         net->open = NULL;
2657 #else
2658         net->netdev_ops = NULL;
2659 #endif
2660
2661         sema_init(&dhd->proto_sem, 1);
2662
2663 #ifdef PROP_TXSTATUS
2664         spin_lock_init(&dhd->wlfc_spinlock);
2665         dhd->pub.wlfc_enabled = TRUE;
2666 #endif /* PROP_TXSTATUS */
2667
2668         /* Initialize other structure content */
2669         init_waitqueue_head(&dhd->ioctl_resp_wait);
2670         init_waitqueue_head(&dhd->ctrl_wait);
2671
2672         /* Initialize the spinlocks */
2673         spin_lock_init(&dhd->sdlock);
2674         spin_lock_init(&dhd->txqlock);
2675         spin_lock_init(&dhd->dhd_lock);
2676
2677         /* Initialize Wakelock stuff */
2678         spin_lock_init(&dhd->wakelock_spinlock);
2679         dhd->wakelock_counter = 0;
2680         dhd->wakelock_rx_timeout_enable = 0;
2681         dhd->wakelock_ctrl_timeout_enable = 0;
2682 #ifdef CONFIG_HAS_WAKELOCK
2683         wake_lock_init(&dhd->wl_wifi, WAKE_LOCK_SUSPEND, "wlan_wake");
2684         wake_lock_init(&dhd->wl_rxwake, WAKE_LOCK_SUSPEND, "wlan_rx_wake");
2685         wake_lock_init(&dhd->wl_ctrlwake, WAKE_LOCK_SUSPEND, "wlan_ctrl_wake");
2686 #endif
2687 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
2688         mutex_init(&dhd->dhd_net_if_mutex);
2689         mutex_init(&dhd->dhd_suspend_mutex);
2690 #endif
2691         dhd_state |= DHD_ATTACH_STATE_WAKELOCKS_INIT;
2692
2693         /* Attach and link in the protocol */
2694         if (dhd_prot_attach(&dhd->pub) != 0) {
2695                 DHD_ERROR(("dhd_prot_attach failed\n"));
2696                 goto fail;
2697         }
2698         dhd_state |= DHD_ATTACH_STATE_PROT_ATTACH;
2699
2700 #ifdef WL_CFG80211
2701         /* Attach and link in the cfg80211 */
2702         if (unlikely(wl_cfg80211_attach(net, &dhd->pub))) {
2703                 DHD_ERROR(("wl_cfg80211_attach failed\n"));
2704                 goto fail;
2705         }
2706
2707         dhd_monitor_init(&dhd->pub);
2708         dhd_state |= DHD_ATTACH_STATE_CFG80211;
2709 #endif
2710 #if defined(CONFIG_BCMDHD_WEXT)
2711         /* Attach and link in the iw */
2712         if (!(dhd_state &  DHD_ATTACH_STATE_CFG80211)) {
2713                 if (wl_iw_attach(net, (void *)&dhd->pub) != 0) {
2714                         DHD_ERROR(("wl_iw_attach failed\n"));
2715                         goto fail;
2716                 }
2717         dhd_state |= DHD_ATTACH_STATE_WL_ATTACH;
2718         }
2719 #endif /* defined(CONFIG_BCMDHD_WEXT) */
2720
2721
2722         /* Set up the watchdog timer */
2723         init_timer(&dhd->timer);
2724         dhd->timer.data = (ulong)dhd;
2725         dhd->timer.function = dhd_watchdog;
2726
2727 #ifdef DHDTHREAD
2728         /* Initialize thread based operation and lock */
2729         sema_init(&dhd->sdsem, 1);
2730         if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0)) {
2731                 dhd->threads_only = TRUE;
2732         }
2733         else {
2734                 dhd->threads_only = FALSE;
2735         }
2736
2737         if (dhd_dpc_prio >= 0) {
2738                 /* Initialize watchdog thread */
2739                 PROC_START(dhd_watchdog_thread, dhd, &dhd->thr_wdt_ctl, 0);
2740         } else {
2741                 dhd->thr_wdt_ctl.thr_pid = -1;
2742         }
2743
2744         /* Set up the bottom half handler */
2745         if (dhd_dpc_prio >= 0) {
2746                 /* Initialize DPC thread */
2747                 PROC_START(dhd_dpc_thread, dhd, &dhd->thr_dpc_ctl, 0);
2748         } else {
2749                 /*  use tasklet for dpc */
2750                 tasklet_init(&dhd->tasklet, dhd_dpc, (ulong)dhd);
2751                 dhd->thr_dpc_ctl.thr_pid = -1;
2752         }
2753 #else
2754         /* Set up the bottom half handler */
2755         tasklet_init(&dhd->tasklet, dhd_dpc, (ulong)dhd);
2756         dhd->dhd_tasklet_create = TRUE;
2757 #endif /* DHDTHREAD */
2758
2759         if (dhd_sysioc) {
2760                 PROC_START(_dhd_sysioc_thread, dhd, &dhd->thr_sysioc_ctl, 0);
2761         } else {
2762                 dhd->thr_sysioc_ctl.thr_pid = -1;
2763         }
2764         dhd_state |= DHD_ATTACH_STATE_THREADS_CREATED;
2765 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
2766         INIT_WORK(&dhd->work_hang, dhd_hang_process);
2767 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))  */
2768         /*
2769          * Save the dhd_info into the priv
2770          */
2771         memcpy(netdev_priv(net), &dhd, sizeof(dhd));
2772
2773 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP)
2774         register_pm_notifier(&dhd_sleep_pm_notifier);
2775 #endif /*  (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP) */
2776
2777 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
2778         dhd->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 20;
2779         dhd->early_suspend.suspend = dhd_early_suspend;
2780         dhd->early_suspend.resume = dhd_late_resume;
2781         register_early_suspend(&dhd->early_suspend);
2782         dhd_state |= DHD_ATTACH_STATE_EARLYSUSPEND_DONE;
2783 #endif
2784
2785 #ifdef ARP_OFFLOAD_SUPPORT
2786         dhd->pend_ipaddr = 0;
2787         register_inetaddr_notifier(&dhd_notifier);
2788 #endif /* ARP_OFFLOAD_SUPPORT */
2789
2790         dhd_state |= DHD_ATTACH_STATE_DONE;
2791         dhd->dhd_state = dhd_state;
2792         return &dhd->pub;
2793
2794 fail:
2795         if (dhd_state < DHD_ATTACH_STATE_DHD_ALLOC) {
2796                 if (net) free_netdev(net);
2797         } else {
2798                 DHD_TRACE(("%s: Calling dhd_detach dhd_state 0x%x &dhd->pub %p\n",
2799                         __FUNCTION__, dhd_state, &dhd->pub));
2800                 dhd->dhd_state = dhd_state;
2801                 dhd_detach(&dhd->pub);
2802                 dhd_free(&dhd->pub);
2803         }
2804
2805         return NULL;
2806 }
2807
2808 int
2809 dhd_bus_start(dhd_pub_t *dhdp)
2810 {
2811         int ret = -1;
2812         dhd_info_t *dhd = (dhd_info_t*)dhdp->info;
2813         unsigned long flags;
2814
2815         ASSERT(dhd);
2816
2817         DHD_TRACE(("Enter %s:\n", __FUNCTION__));
2818
2819 #ifdef DHDTHREAD
2820         if (dhd->threads_only)
2821                 dhd_os_sdlock(dhdp);
2822 #endif /* DHDTHREAD */
2823
2824         /* try to download image and nvram to the dongle */
2825         if  ((dhd->pub.busstate == DHD_BUS_DOWN) &&
2826                 (fw_path != NULL) && (fw_path[0] != '\0') &&
2827                 (nv_path != NULL) && (nv_path[0] != '\0')) {
2828                 /* wake lock moved to dhdsdio_download_firmware */
2829                 if (!(dhd_bus_download_firmware(dhd->pub.bus, dhd->pub.osh,
2830                                                 fw_path, nv_path))) {
2831                         DHD_ERROR(("%s: dhdsdio_probe_download failed. firmware = %s nvram = %s\n",
2832                                    __FUNCTION__, fw_path, nv_path));
2833 #ifdef DHDTHREAD
2834                         if (dhd->threads_only)
2835                                 dhd_os_sdunlock(dhdp);
2836 #endif /* DHDTHREAD */
2837                         return -1;
2838                 }
2839         }
2840         if (dhd->pub.busstate != DHD_BUS_LOAD) {
2841 #ifdef DHDTHREAD
2842                 if (dhd->threads_only)
2843                         dhd_os_sdunlock(dhdp);
2844 #endif /* DHDTHREAD */
2845                 return -ENETDOWN;
2846         }
2847
2848         /* Start the watchdog timer */
2849         dhd->pub.tickcnt = 0;
2850         dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
2851
2852         /* Bring up the bus */
2853         if ((ret = dhd_bus_init(&dhd->pub, FALSE)) != 0) {
2854
2855                 DHD_ERROR(("%s, dhd_bus_init failed %d\n", __FUNCTION__, ret));
2856 #ifdef DHDTHREAD
2857                 if (dhd->threads_only)
2858                         dhd_os_sdunlock(dhdp);
2859 #endif /* DHDTHREAD */
2860                 return ret;
2861         }
2862 #if defined(OOB_INTR_ONLY)
2863         /* Host registration for OOB interrupt */
2864         if (bcmsdh_register_oob_intr(dhdp)) {
2865                 /* deactivate timer and wait for the handler to finish */
2866
2867                 flags = dhd_os_spin_lock(&dhd->pub);
2868                 dhd->wd_timer_valid = FALSE;
2869                 dhd_os_spin_unlock(&dhd->pub, flags);
2870                 del_timer_sync(&dhd->timer);
2871
2872                 DHD_ERROR(("%s Host failed to register for OOB\n", __FUNCTION__));
2873 #ifdef DHDTHREAD
2874                 if (dhd->threads_only)
2875                         dhd_os_sdunlock(dhdp);
2876 #endif /* DHDTHREAD */
2877                 return -ENODEV;
2878         }
2879
2880         /* Enable oob at firmware */
2881         dhd_enable_oob_intr(dhd->pub.bus, TRUE);
2882 #endif /* defined(OOB_INTR_ONLY) */
2883
2884         /* If bus is not ready, can't come up */
2885         if (dhd->pub.busstate != DHD_BUS_DATA) {
2886                 flags = dhd_os_spin_lock(&dhd->pub);
2887                 dhd->wd_timer_valid = FALSE;
2888                 dhd_os_spin_unlock(&dhd->pub, flags);
2889                 del_timer_sync(&dhd->timer);
2890                 DHD_ERROR(("%s failed bus is not ready\n", __FUNCTION__));
2891 #ifdef DHDTHREAD
2892                 if (dhd->threads_only)
2893                         dhd_os_sdunlock(dhdp);
2894 #endif /* DHDTHREAD */
2895                 return -ENODEV;
2896         }
2897
2898 #ifdef DHDTHREAD
2899         if (dhd->threads_only)
2900                 dhd_os_sdunlock(dhdp);
2901 #endif /* DHDTHREAD */
2902
2903 #ifdef READ_MACADDR
2904         dhd_read_macaddr(dhd);
2905 #endif
2906
2907         /* Bus is ready, do any protocol initialization */
2908         if ((ret = dhd_prot_init(&dhd->pub)) < 0)
2909                 return ret;
2910
2911 #ifdef WRITE_MACADDR
2912         dhd_write_macaddr(dhd->pub.mac.octet);
2913 #endif
2914
2915 #ifdef ARP_OFFLOAD_SUPPORT
2916         if (dhd->pend_ipaddr) {
2917 #ifdef AOE_IP_ALIAS_SUPPORT
2918                 aoe_update_host_ipv4_table(&dhd->pub, dhd->pend_ipaddr, TRUE);
2919 #endif /* AOE_IP_ALIAS_SUPPORT */
2920                 dhd->pend_ipaddr = 0;
2921         }
2922 #endif /* ARP_OFFLOAD_SUPPORT */
2923
2924         return 0;
2925 }
2926
2927 #if !defined(AP) && defined(WLP2P) && defined(WL_ENABLE_P2P_IF)
2928 /* For Android ICS MR2 release, the concurrent mode is enabled by default and the firmware
2929  * name would be fw_bcmdhd.bin. So we need to determine whether P2P is enabled in the STA
2930  * firmware and accordingly enable concurrent mode (Apply P2P settings). SoftAP firmware
2931  * would still be named as fw_bcmdhd_apsta.
2932  */
2933 static u32
2934 dhd_concurrent_fw(dhd_pub_t *dhd)
2935 {
2936         int ret = 0;
2937         char buf[WLC_IOCTL_SMLEN];
2938
2939         if ((!op_mode) && (strstr(fw_path, "_p2p") == NULL) &&
2940                 (strstr(fw_path, "_apsta") == NULL)) {
2941                 /* Given path is for the STA firmware. Check whether P2P support is present in
2942                  * the firmware. If so, set mode as P2P (concurrent support).
2943                  */
2944                 memset(buf, 0, sizeof(buf));
2945                 bcm_mkiovar("p2p", 0, 0, buf, sizeof(buf));
2946                 if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_VAR, buf, sizeof(buf),
2947                         FALSE, 0)) < 0) {
2948                         DHD_TRACE(("%s: Get P2P failed (error=%d)\n", __FUNCTION__, ret));
2949                 } else if (buf[0] == 1) {
2950                         DHD_TRACE(("%s: P2P is supported\n", __FUNCTION__));
2951                         return 1;
2952                 }
2953         }
2954         return 0;
2955 }
2956 #endif 
2957
2958 int
2959 dhd_preinit_ioctls(dhd_pub_t *dhd)
2960 {
2961         int ret = 0;
2962         char eventmask[WL_EVENTING_MASK_LEN];
2963         char iovbuf[WL_EVENTING_MASK_LEN + 12]; /*  Room for "event_msgs" + '\0' + bitvec  */
2964 #if !defined(WL_CFG80211)
2965         uint up = 0;
2966 #endif
2967         uint power_mode = PM_FAST;
2968         uint32 dongle_align = DHD_SDALIGN;
2969         uint32 glom = 0;
2970         uint bcn_timeout = DHD_BEACON_TIMEOUT_NORMAL;
2971
2972         uint retry_max = 3;
2973 #if defined(ARP_OFFLOAD_SUPPORT)
2974         int arpoe = 1;
2975 #endif
2976 #if defined(KEEP_ALIVE)
2977         int res;
2978 #endif /* defined(KEEP_ALIVE) */
2979         int scan_assoc_time = DHD_SCAN_ACTIVE_TIME;
2980         int scan_unassoc_time = 40;
2981         int scan_passive_time = DHD_SCAN_PASSIVE_TIME;
2982         char buf[WLC_IOCTL_SMLEN];
2983         char *ptr;
2984         uint32 listen_interval = LISTEN_INTERVAL; /* Default Listen Interval in Beacons */
2985         uint16 chipID;
2986 #if defined(SOFTAP)
2987         uint dtim = 1;
2988 #endif
2989 #if (defined(AP) && !defined(WLP2P)) || (!defined(AP) && defined(WL_CFG80211))
2990         uint32 mpc = 0; /* Turn MPC off for AP/APSTA mode */
2991 #endif
2992 #if defined(AP) || defined(WLP2P)
2993         uint32 apsta = 1; /* Enable APSTA mode */
2994 #endif /* defined(AP) || defined(WLP2P) */
2995 #ifdef GET_CUSTOM_MAC_ENABLE
2996         struct ether_addr ea_addr;
2997 #endif /* GET_CUSTOM_MAC_ENABLE */
2998         DHD_TRACE(("Enter %s\n", __FUNCTION__));
2999         dhd->op_mode = 0;
3000 #ifdef GET_CUSTOM_MAC_ENABLE
3001         ret = dhd_custom_get_mac_address(ea_addr.octet);
3002         if (!ret) {
3003                 memset(buf, 0, sizeof(buf));
3004                 bcm_mkiovar("cur_etheraddr", (void *)&ea_addr, ETHER_ADDR_LEN, buf, sizeof(buf));
3005                 ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, sizeof(buf), TRUE, 0);
3006                 if (ret < 0) {
3007                         DHD_ERROR(("%s: can't set custom MAC address , error=%d\n", __FUNCTION__, ret));
3008                         return BCME_NOTUP;
3009                 }
3010                 memcpy(dhd->mac.octet, ea_addr.octet, ETHER_ADDR_LEN);
3011         } else {
3012 #endif /* GET_CUSTOM_MAC_ENABLE */
3013                 /* Get the default device MAC address directly from firmware */
3014                 memset(buf, 0, sizeof(buf));
3015                 bcm_mkiovar("cur_etheraddr", 0, 0, buf, sizeof(buf));
3016                 if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_VAR, buf, sizeof(buf),
3017                         FALSE, 0)) < 0) {
3018                         DHD_ERROR(("%s: can't get MAC address , error=%d\n", __FUNCTION__, ret));
3019                         return BCME_NOTUP;
3020                 }
3021                 /* Update public MAC address after reading from Firmware */
3022                 memcpy(dhd->mac.octet, buf, ETHER_ADDR_LEN);
3023 #ifdef GET_CUSTOM_MAC_ENABLE
3024         }
3025 #endif /* GET_CUSTOM_MAC_ENABLE */
3026
3027 #ifdef SET_RANDOM_MAC_SOFTAP
3028         if ((!op_mode && strstr(fw_path, "_apsta") != NULL) || (op_mode == HOSTAPD_MASK)) {
3029                 uint rand_mac;
3030
3031                 srandom32((uint)jiffies);
3032                 rand_mac = random32();
3033                 iovbuf[0] = 0x02;              /* locally administered bit */
3034                 iovbuf[1] = 0x1A;
3035                 iovbuf[2] = 0x11;
3036                 iovbuf[3] = (unsigned char)(rand_mac & 0x0F) | 0xF0;
3037                 iovbuf[4] = (unsigned char)(rand_mac >> 8);
3038                 iovbuf[5] = (unsigned char)(rand_mac >> 16);
3039
3040                 bcm_mkiovar("cur_etheraddr", (void *)iovbuf, ETHER_ADDR_LEN, buf, sizeof(buf));
3041                 ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, sizeof(buf), TRUE, 0);
3042                 if (ret < 0) {
3043                         DHD_ERROR(("%s: can't set MAC address , error=%d\n", __FUNCTION__, ret));
3044                 } else
3045                         memcpy(dhd->mac.octet, iovbuf, ETHER_ADDR_LEN);
3046         }
3047 #endif /* SET_RANDOM_MAC_SOFTAP */
3048
3049         DHD_TRACE(("Firmware = %s\n", fw_path));
3050 #if !defined(AP)  && defined(WLP2P)
3051         /* Check if firmware with WFD support used */
3052         if ((!op_mode && strstr(fw_path, "_p2p") != NULL)
3053 #if defined(WL_ENABLE_P2P_IF)
3054                         || (op_mode == 0x04) ||(dhd_concurrent_fw(dhd))
3055 #endif
3056                 ) {
3057                 bcm_mkiovar("apsta", (char *)&apsta, 4, iovbuf, sizeof(iovbuf));
3058                 if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR,
3059                         iovbuf, sizeof(iovbuf), TRUE, 0)) < 0) {
3060                         DHD_ERROR(("%s APSTA for WFD failed ret= %d\n", __FUNCTION__, ret));
3061                 } else {
3062                         dhd->op_mode |= WFD_MASK;
3063                 }
3064         }
3065 #endif
3066
3067 #if !defined(AP) && defined(WL_CFG80211)
3068         /* Check if firmware with HostAPD support used */
3069         if ((!op_mode && strstr(fw_path, "_apsta") != NULL) || (op_mode == HOSTAPD_MASK)) {
3070                         /* Disable A-band for HostAPD  */
3071                         uint band = WLC_BAND_2G;
3072                         if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_BAND, (char *)&band, sizeof(band),
3073                                 TRUE, 0)) < 0) {
3074                                 DHD_ERROR(("%s:set band failed error (%d)\n", __FUNCTION__, ret));
3075                         }
3076
3077                         /* Turn off wme if we are having only g ONLY firmware */
3078                         bcm_mkiovar("nmode", 0, 0, buf, sizeof(buf));
3079                         if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_VAR, buf, sizeof(buf),
3080                                 FALSE, 0)) < 0) {
3081                                 DHD_ERROR(("%s:get nmode failed error (%d)\n", __FUNCTION__, ret));
3082                         }
3083                         else {
3084                                 DHD_TRACE(("%s:get nmode returned %d\n", __FUNCTION__,buf[0]));
3085                         }
3086                         if (buf[0] == 0) {
3087                                 int wme = 0;
3088                                 bcm_mkiovar("wme", (char *)&wme, 4, iovbuf, sizeof(iovbuf));
3089                                 if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf,
3090                                         sizeof(iovbuf), TRUE, 0)) < 0) {
3091                                         DHD_ERROR(("%s set wme for HostAPD failed  %d\n", __FUNCTION__, ret));
3092                                 }
3093                                 else {
3094                                         DHD_TRACE(("%s set wme succeeded for g ONLY firmware\n", __FUNCTION__));
3095                                 }
3096                         }
3097                         /* Turn off MPC in AP mode */
3098                         bcm_mkiovar("mpc", (char *)&mpc, 4, iovbuf, sizeof(iovbuf));
3099                         if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf,
3100                                 sizeof(iovbuf), TRUE, 0)) < 0) {
3101                                 DHD_ERROR(("%s mpc for HostAPD failed  %d\n", __FUNCTION__, ret));
3102                         } else {
3103                                 dhd->op_mode |= HOSTAPD_MASK;
3104 #if defined(ARP_OFFLOAD_SUPPORT)
3105                                 arpoe = 0;
3106 #endif /* (ARP_OFFLOAD_SUPPORT) */
3107                                 dhd_pkt_filter_enable = FALSE;
3108                         }
3109         }
3110 #endif
3111
3112         if ((dhd->op_mode != WFD_MASK) && (dhd->op_mode != HOSTAPD_MASK)) {
3113                 /* STA only operation mode */
3114                 dhd->op_mode |= STA_MASK;
3115                 dhd_pkt_filter_enable = TRUE;
3116         }
3117
3118         DHD_ERROR(("Firmware up: fw_path=%s op_mode=%d, "
3119                         "Broadcom Dongle Host Driver mac=%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
3120                         fw_path,
3121                         dhd->op_mode,
3122                         dhd->mac.octet[0], dhd->mac.octet[1], dhd->mac.octet[2],
3123                         dhd->mac.octet[3], dhd->mac.octet[4], dhd->mac.octet[5]));
3124
3125         /* Set Country code  */
3126         if (dhd->dhd_cspec.ccode[0] != 0) {
3127                 bcm_mkiovar("country", (char *)&dhd->dhd_cspec,
3128                         sizeof(wl_country_t), iovbuf, sizeof(iovbuf));
3129                 if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0)) < 0)
3130                         DHD_ERROR(("%s: country code setting failed\n", __FUNCTION__));
3131         }
3132
3133         /* Set Listen Interval */
3134         bcm_mkiovar("assoc_listen", (char *)&listen_interval, 4, iovbuf, sizeof(iovbuf));
3135         if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0)) < 0)
3136                 DHD_ERROR(("%s assoc_listen failed %d\n", __FUNCTION__, ret));
3137
3138         /* Set PowerSave mode */
3139         dhd_wl_ioctl_cmd(dhd, WLC_SET_PM, (char *)&power_mode, sizeof(power_mode), TRUE, 0);
3140
3141         /* Match Host and Dongle rx alignment */
3142         bcm_mkiovar("bus:txglomalign", (char *)&dongle_align, 4, iovbuf, sizeof(iovbuf));
3143         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
3144
3145         /* disable glom option for some chips */
3146         chipID = (uint16)dhd_bus_chip_id(dhd);
3147         if  ((chipID == BCM4330_CHIP_ID) || (chipID == BCM4329_CHIP_ID)) {
3148                 DHD_INFO(("%s disable glom for chipID=0x%X\n", __FUNCTION__, chipID));
3149                 bcm_mkiovar("bus:txglom", (char *)&glom, 4, iovbuf, sizeof(iovbuf));
3150                 dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
3151         }
3152
3153         /* Setup timeout if Beacons are lost and roam is off to report link down */
3154         bcm_mkiovar("bcn_timeout", (char *)&bcn_timeout, 4, iovbuf, sizeof(iovbuf));
3155         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
3156         /* Setup assoc_retry_max count to reconnect target AP in dongle */
3157         bcm_mkiovar("assoc_retry_max", (char *)&retry_max, 4, iovbuf, sizeof(iovbuf));
3158         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
3159
3160 #if defined(AP) && !defined(WLP2P)
3161         /* Turn off MPC in AP mode */
3162         bcm_mkiovar("mpc", (char *)&mpc, 4, iovbuf, sizeof(iovbuf));
3163         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
3164         bcm_mkiovar("apsta", (char *)&apsta, 4, iovbuf, sizeof(iovbuf));
3165         dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
3166 #endif /* defined(AP) && !defined(WLP2P) */
3167
3168 #if defined(SOFTAP)
3169         if (ap_fw_loaded == TRUE) {
3170                 dhd_wl_ioctl_cmd(dhd, WLC_SET_DTIMPRD, (char *)&dtim, sizeof(dtim), TRUE, 0);
3171         }
3172 #endif
3173
3174 #if defined(KEEP_ALIVE)
3175         /* Set Keep Alive : be sure to use FW with -keepalive */
3176 #if defined(SOFTAP)
3177         if (ap_fw_loaded == FALSE)
3178 #endif
3179                 if ((res = dhd_keep_alive_onoff(dhd)) < 0)
3180                         DHD_ERROR(("%s set keeplive failed %d\n",
3181                         __FUNCTION__, res));
3182 #endif /* defined(KEEP_ALIVE) */
3183
3184         /* Read event_msgs mask */
3185         bcm_mkiovar("event_msgs", eventmask, WL_EVENTING_MASK_LEN, iovbuf, sizeof(iovbuf));
3186         if ((ret  = dhd_wl_ioctl_cmd(dhd, WLC_GET_VAR, iovbuf, sizeof(iovbuf), FALSE, 0)) < 0) {
3187                 DHD_ERROR(("%s read Event mask failed %d\n", __FUNCTION__, ret));
3188                 goto done;
3189         }
3190         bcopy(iovbuf, eventmask, WL_EVENTING_MASK_LEN);
3191
3192         /* Setup event_msgs */
3193         setbit(eventmask, WLC_E_SET_SSID);
3194         setbit(eventmask, WLC_E_PRUNE);
3195         setbit(eventmask, WLC_E_AUTH);
3196         setbit(eventmask, WLC_E_REASSOC);
3197         setbit(eventmask, WLC_E_REASSOC_IND);
3198         setbit(eventmask, WLC_E_DEAUTH);
3199         setbit(eventmask, WLC_E_DEAUTH_IND);
3200         setbit(eventmask, WLC_E_DISASSOC_IND);
3201         setbit(eventmask, WLC_E_DISASSOC);
3202         setbit(eventmask, WLC_E_JOIN);
3203         setbit(eventmask, WLC_E_ASSOC_IND);
3204         setbit(eventmask, WLC_E_PSK_SUP);
3205         setbit(eventmask, WLC_E_LINK);
3206         setbit(eventmask, WLC_E_NDIS_LINK);
3207         setbit(eventmask, WLC_E_MIC_ERROR);
3208         setbit(eventmask, WLC_E_ASSOC_REQ_IE);
3209         setbit(eventmask, WLC_E_ASSOC_RESP_IE);
3210         setbit(eventmask, WLC_E_PMKID_CACHE);
3211         setbit(eventmask, WLC_E_JOIN_START);
3212         setbit(eventmask, WLC_E_SCAN_COMPLETE);
3213 #ifdef WLMEDIA_HTSF
3214         setbit(eventmask, WLC_E_HTSFSYNC);
3215 #endif /* WLMEDIA_HTSF */
3216 #ifdef PNO_SUPPORT
3217         setbit(eventmask, WLC_E_PFN_NET_FOUND);
3218 #endif /* PNO_SUPPORT */
3219         /* enable dongle roaming event */
3220         setbit(eventmask, WLC_E_ROAM);
3221 #ifdef WL_CFG80211
3222         setbit(eventmask, WLC_E_ESCAN_RESULT);
3223         if ((dhd->op_mode & WFD_MASK) == WFD_MASK) {
3224                 setbit(eventmask, WLC_E_ACTION_FRAME_RX);
3225                 setbit(eventmask, WLC_E_ACTION_FRAME_COMPLETE);
3226                 setbit(eventmask, WLC_E_ACTION_FRAME_OFF_CHAN_COMPLETE);
3227                 setbit(eventmask, WLC_E_P2P_PROBREQ_MSG);
3228                 setbit(eventmask, WLC_E_P2P_DISC_LISTEN_COMPLETE);
3229         }
3230 #endif /* WL_CFG80211 */
3231
3232         /* Write updated Event mask */
3233         bcm_mkiovar("event_msgs", eventmask, WL_EVENTING_MASK_LEN, iovbuf, sizeof(iovbuf));
3234         if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0)) < 0) {
3235                 DHD_ERROR(("%s Set Event mask failed %d\n", __FUNCTION__, ret));
3236                 goto done;
3237         }
3238
3239         dhd_wl_ioctl_cmd(dhd, WLC_SET_SCAN_CHANNEL_TIME, (char *)&scan_assoc_time,
3240                 sizeof(scan_assoc_time), TRUE, 0);
3241         dhd_wl_ioctl_cmd(dhd, WLC_SET_SCAN_UNASSOC_TIME, (char *)&scan_unassoc_time,
3242                 sizeof(scan_unassoc_time), TRUE, 0);
3243         dhd_wl_ioctl_cmd(dhd, WLC_SET_SCAN_PASSIVE_TIME, (char *)&scan_passive_time,
3244                 sizeof(scan_passive_time), TRUE, 0);
3245
3246 #ifdef ARP_OFFLOAD_SUPPORT
3247         /* Set and enable ARP offload feature for STA only  */
3248 #if defined(SOFTAP)
3249         if (arpoe && !ap_fw_loaded) {
3250 #else
3251         if (arpoe) {
3252 #endif 
3253                 dhd_arp_offload_set(dhd, dhd_arp_mode);
3254                 dhd_arp_offload_enable(dhd, arpoe);
3255         } else {
3256                 dhd_arp_offload_set(dhd, 0);
3257                 dhd_arp_offload_enable(dhd, FALSE);
3258         }
3259 #endif /* ARP_OFFLOAD_SUPPORT */
3260
3261 #ifdef PKT_FILTER_SUPPORT
3262         /* Setup defintions for pktfilter , enable in suspend */
3263         dhd->pktfilter_count = 5;
3264         /* Setup filter to allow only unicast */
3265         dhd->pktfilter[0] = "100 0 0 0 0x01 0x00";
3266         dhd->pktfilter[1] = NULL;
3267         dhd->pktfilter[2] = NULL;
3268         dhd->pktfilter[3] = NULL;
3269         dhd->pktfilter[4] = "104 0 0 0 0xFFFFFFFFFFFF 0x01005E0000FB";
3270 #if defined(SOFTAP)
3271         if (ap_fw_loaded) {
3272                 int i;
3273                 for (i = 0; i < dhd->pktfilter_count; i++) {
3274                         dhd_pktfilter_offload_enable(dhd, dhd->pktfilter[i],
3275                                 0, dhd_master_mode);
3276                 }
3277         }
3278 #endif /* defined(SOFTAP) */
3279 #endif /* PKT_FILTER_SUPPORT */
3280
3281 #if !defined(WL_CFG80211)
3282         /* Force STA UP */
3283         if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_UP, (char *)&up, sizeof(up), TRUE, 0)) < 0) {
3284                 DHD_ERROR(("%s Setting WL UP failed %d\n", __FUNCTION__, ret));
3285                 goto done;
3286         }
3287 #endif
3288         /* query for 'ver' to get version info from firmware */
3289         memset(buf, 0, sizeof(buf));
3290         ptr = buf;
3291         bcm_mkiovar("ver", (char *)&buf, 4, buf, sizeof(buf));
3292         if ((ret  = dhd_wl_ioctl_cmd(dhd, WLC_GET_VAR, buf, sizeof(buf), FALSE, 0)) < 0)
3293                 DHD_ERROR(("%s failed %d\n", __FUNCTION__, ret));
3294         else {
3295                 bcmstrtok(&ptr, "\n", 0);
3296                 /* Print fw version info */
3297                 DHD_ERROR(("Firmware version = %s\n", buf));
3298
3299                 DHD_BLOG(buf, strlen(buf) + 1);
3300                 DHD_BLOG(dhd_version, strlen(dhd_version) + 1);
3301
3302                 /* Check and adjust IOCTL response timeout for Manufactring firmware */
3303                 if (strstr(buf, MANUFACTRING_FW) != NULL) {
3304                         dhd_os_set_ioctl_resp_timeout(IOCTL_RESP_TIMEOUT * 10);
3305                         DHD_ERROR(("%s : adjust IOCTL response time for Manufactring Firmware\n", __FUNCTION__));
3306                 }
3307         }
3308
3309 done:
3310         return ret;
3311 }
3312
3313
3314 int
3315 dhd_iovar(dhd_pub_t *pub, int ifidx, char *name, char *cmd_buf, uint cmd_len, int set)
3316 {
3317         char buf[strlen(name) + 1 + cmd_len];
3318         int len = sizeof(buf);
3319         wl_ioctl_t ioc;
3320         int ret;
3321
3322         len = bcm_mkiovar(name, cmd_buf, cmd_len, buf, len);
3323
3324         memset(&ioc, 0, sizeof(ioc));
3325
3326         ioc.cmd = set? WLC_SET_VAR : WLC_GET_VAR;
3327         ioc.buf = buf;
3328         ioc.len = len;
3329         ioc.set = TRUE;
3330
3331         ret = dhd_wl_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
3332         if (!set && ret >= 0)
3333                 memcpy(cmd_buf, buf, cmd_len);
3334
3335         return ret;
3336 }
3337
3338 int dhd_change_mtu(dhd_pub_t *dhdp, int new_mtu, int ifidx)
3339 {
3340         struct dhd_info *dhd = dhdp->info;
3341         struct net_device *dev = NULL;
3342
3343         ASSERT(dhd && dhd->iflist[ifidx]);
3344         dev = dhd->iflist[ifidx]->net;
3345         ASSERT(dev);
3346
3347         if (netif_running(dev)) {
3348                 DHD_ERROR(("%s: Must be down to change its MTU", dev->name));
3349                 return BCME_NOTDOWN;
3350         }
3351
3352 #define DHD_MIN_MTU 1500
3353 #define DHD_MAX_MTU 1752
3354
3355         if ((new_mtu < DHD_MIN_MTU) || (new_mtu > DHD_MAX_MTU)) {
3356                 DHD_ERROR(("%s: MTU size %d is invalid.\n", __FUNCTION__, new_mtu));
3357                 return BCME_BADARG;
3358         }
3359
3360         dev->mtu = new_mtu;
3361         return 0;
3362 }
3363
3364 #ifdef ARP_OFFLOAD_SUPPORT
3365 /* add or remove AOE host ip(s) (up to 8 IPs on the interface)  */
3366 void
3367 aoe_update_host_ipv4_table(dhd_pub_t *dhd_pub, u32 ipa, bool add)
3368 {
3369         u32 ipv4_buf[MAX_IPV4_ENTRIES]; /* temp save for AOE host_ip table */
3370         int i;
3371         int ret;
3372
3373         bzero(ipv4_buf, sizeof(ipv4_buf));
3374
3375         /* display what we've got */
3376         ret = dhd_arp_get_arp_hostip_table(dhd_pub, ipv4_buf, sizeof(ipv4_buf));
3377         DHD_ARPOE(("%s: hostip table read from Dongle:\n", __FUNCTION__));
3378 #ifdef AOE_DBG
3379         dhd_print_buf(ipv4_buf, 32, 4); /* max 8 IPs 4b each */
3380 #endif
3381         /* now we saved hoste_ip table, clr it in the dongle AOE */
3382         dhd_aoe_hostip_clr(dhd_pub);
3383
3384         if (ret) {
3385                 DHD_ERROR(("%s failed\n", __FUNCTION__));
3386                 return;
3387         }
3388
3389         for (i = 0; i < MAX_IPV4_ENTRIES; i++) {
3390                 if (add && (ipv4_buf[i] == 0)) {
3391                                 ipv4_buf[i] = ipa;
3392                                 add = FALSE; /* added ipa to local table  */
3393                                 DHD_ARPOE(("%s: Saved new IP in temp arp_hostip[%d]\n",
3394                                 __FUNCTION__, i));
3395                 } else if (ipv4_buf[i] == ipa) {
3396                         ipv4_buf[i]     = 0;
3397                         DHD_ARPOE(("%s: removed IP:%x from temp table %d\n",
3398                                 __FUNCTION__, ipa, i));
3399                 }
3400
3401                 if (ipv4_buf[i] != 0) {
3402                         /* add back host_ip entries from our local cache */
3403                         dhd_arp_offload_add_ip(dhd_pub, ipv4_buf[i]);
3404                         DHD_ARPOE(("%s: added IP:%x to dongle arp_hostip[%d]\n\n",
3405                                 __FUNCTION__, ipv4_buf[i], i));
3406                 }
3407         }
3408 #ifdef AOE_DBG
3409         /* see the resulting hostip table */
3410         dhd_arp_get_arp_hostip_table(dhd_pub, ipv4_buf, sizeof(ipv4_buf));
3411         DHD_ARPOE(("%s: read back arp_hostip table:\n", __FUNCTION__));
3412         dhd_print_buf(ipv4_buf, 32, 4); /* max 8 IPs 4b each */
3413 #endif
3414 }
3415
3416 static int dhd_device_event(struct notifier_block *this,
3417         unsigned long event,
3418         void *ptr)
3419 {
3420         struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3421
3422         dhd_info_t *dhd;
3423         dhd_pub_t *dhd_pub;
3424
3425         if (!ifa)
3426                 return NOTIFY_DONE;
3427
3428         dhd = *(dhd_info_t **)netdev_priv(ifa->ifa_dev->dev);
3429         dhd_pub = &dhd->pub;
3430
3431 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
3432         if (ifa->ifa_dev->dev->netdev_ops == &dhd_ops_pri) {
3433 #else
3434         if (ifa->ifa_dev->dev) {
3435 #endif
3436                 switch (event) {
3437                 case NETDEV_UP:
3438                         DHD_ARPOE(("%s: [%s] Up IP: 0x%x\n",
3439                                 __FUNCTION__, ifa->ifa_label, ifa->ifa_address));
3440
3441                         if (dhd->pub.busstate != DHD_BUS_DATA) {
3442                                 DHD_ERROR(("%s: bus not ready, exit\n", __FUNCTION__));
3443                                 if (dhd->pend_ipaddr) {
3444                                         DHD_ERROR(("%s: overwrite pending ipaddr: 0x%x\n",
3445                                                 __FUNCTION__, dhd->pend_ipaddr));
3446                                 }
3447                                 dhd->pend_ipaddr = ifa->ifa_address;
3448                                 break;
3449                         }
3450
3451 #ifdef AOE_IP_ALIAS_SUPPORT
3452                         if (ifa->ifa_label[strlen(ifa->ifa_label)-2] == 0x3a) {
3453                                 DHD_ARPOE(("%s:add aliased IP to AOE hostip cache\n",
3454                                         __FUNCTION__));
3455                                 aoe_update_host_ipv4_table(dhd_pub, ifa->ifa_address, TRUE);
3456                         }
3457                         else
3458                                 aoe_update_host_ipv4_table(dhd_pub, ifa->ifa_address, TRUE);
3459 #endif
3460                         break;
3461
3462                 case NETDEV_DOWN:
3463                         DHD_ARPOE(("%s: [%s] Down IP: 0x%x\n",
3464                                 __FUNCTION__, ifa->ifa_label, ifa->ifa_address));
3465                         dhd->pend_ipaddr = 0;
3466 #ifdef AOE_IP_ALIAS_SUPPORT
3467                 if (!(ifa->ifa_label[strlen(ifa->ifa_label)-2] == 0x3a)) {
3468                                 DHD_ARPOE(("%s: primary interface is down, AOE clr all\n",
3469                                            __FUNCTION__));
3470                                 dhd_aoe_hostip_clr(&dhd->pub);
3471                                 dhd_aoe_arp_clr(&dhd->pub);
3472                 } else
3473                         aoe_update_host_ipv4_table(dhd_pub, ifa->ifa_address, FALSE);
3474 #else
3475                         dhd_aoe_hostip_clr(&dhd->pub);
3476                         dhd_aoe_arp_clr(&dhd->pub);
3477 #endif
3478                         break;
3479
3480                 default:
3481                         DHD_ARPOE(("%s: do noting for [%s] Event: %lu\n",
3482                                 __func__, ifa->ifa_label, event));
3483                         break;
3484                 }
3485         }
3486         return NOTIFY_DONE;
3487 }
3488 #endif /* ARP_OFFLOAD_SUPPORT */
3489
3490 int
3491 dhd_net_attach(dhd_pub_t *dhdp, int ifidx)
3492 {
3493         dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
3494         struct net_device *net = NULL;
3495         int err = 0;
3496         uint8 temp_addr[ETHER_ADDR_LEN] = { 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33 };
3497
3498         DHD_TRACE(("%s: ifidx %d\n", __FUNCTION__, ifidx));
3499
3500         ASSERT(dhd && dhd->iflist[ifidx]);
3501
3502         net = dhd->iflist[ifidx]->net;
3503         ASSERT(net);
3504
3505 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
3506         ASSERT(!net->open);
3507         net->get_stats = dhd_get_stats;
3508         net->do_ioctl = dhd_ioctl_entry;
3509         net->hard_start_xmit = dhd_start_xmit;
3510         net->set_mac_address = dhd_set_mac_address;
3511         net->set_multicast_list = dhd_set_multicast_list;
3512         net->open = net->stop = NULL;
3513 #else
3514         ASSERT(!net->netdev_ops);
3515         net->netdev_ops = &dhd_ops_virt;
3516 #endif
3517
3518         /* Ok, link into the network layer... */
3519         if (ifidx == 0) {
3520                 /*
3521                  * device functions for the primary interface only
3522                  */
3523 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
3524                 net->open = dhd_open;
3525                 net->stop = dhd_stop;
3526 #else
3527                 net->netdev_ops = &dhd_ops_pri;
3528 #endif
3529         } else {
3530                 /*
3531                  * We have to use the primary MAC for virtual interfaces
3532                  */
3533                 memcpy(temp_addr, dhd->iflist[ifidx]->mac_addr, ETHER_ADDR_LEN);
3534                 /*
3535                  * Android sets the locally administered bit to indicate that this is a
3536                  * portable hotspot.  This will not work in simultaneous AP/STA mode,
3537                  * nor with P2P.  Need to set the Donlge's MAC address, and then use that.
3538                  */
3539                 if (!memcmp(temp_addr, dhd->iflist[0]->mac_addr,
3540                         ETHER_ADDR_LEN)) {
3541                         DHD_ERROR(("%s interface [%s]: set locally administered bit in MAC\n",
3542                         __func__, net->name));
3543                         temp_addr[0] |= 0x02;
3544                 }
3545         }
3546
3547         net->hard_header_len = ETH_HLEN + dhd->pub.hdrlen;
3548 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
3549         net->ethtool_ops = &dhd_ethtool_ops;
3550 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) */
3551
3552 #if defined(CONFIG_BCMDHD_WEXT)
3553 #if WIRELESS_EXT < 19
3554         net->get_wireless_stats = dhd_get_wireless_stats;
3555 #endif /* WIRELESS_EXT < 19 */
3556 #if WIRELESS_EXT > 12
3557         net->wireless_handlers = (struct iw_handler_def *)&wl_iw_handler_def;
3558 #endif /* WIRELESS_EXT > 12 */
3559 #endif /* defined(CONFIG_BCMDHD_WEXT) */
3560
3561         dhd->pub.rxsz = DBUS_RX_BUFFER_SIZE_DHD(net);
3562
3563         memcpy(net->dev_addr, temp_addr, ETHER_ADDR_LEN);
3564
3565         if ((err = register_netdev(net)) != 0) {
3566                 DHD_ERROR(("couldn't register the net device, err %d\n", err));
3567                 goto fail;
3568         }
3569         printf("Broadcom Dongle Host Driver: register interface [%s]"
3570                 " MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
3571                 net->name,
3572                 net->dev_addr[0], net->dev_addr[1], net->dev_addr[2],
3573                 net->dev_addr[3], net->dev_addr[4], net->dev_addr[5]);
3574
3575 #if defined(SOFTAP) && defined(CONFIG_BCMDHD_WEXT) && !defined(WL_CFG80211)
3576                 wl_iw_iscan_set_scan_broadcast_prep(net, 1);
3577 #endif
3578
3579
3580 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
3581         if (ifidx == 0) {
3582                 up(&dhd_registration_sem);
3583         }
3584 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) */
3585         return 0;
3586
3587 fail:
3588 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)
3589         net->open = NULL;
3590 #else
3591         net->netdev_ops = NULL;
3592 #endif
3593         return err;
3594 }
3595
3596 void
3597 dhd_bus_detach(dhd_pub_t *dhdp)
3598 {
3599         dhd_info_t *dhd;
3600
3601         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
3602
3603         if (dhdp) {
3604                 dhd = (dhd_info_t *)dhdp->info;
3605                 if (dhd) {
3606
3607                         /*
3608                          * In case of Android cfg80211 driver, the bus is down in dhd_stop,
3609                          * calling stop again will cuase SD read/write errors.
3610                          */
3611                         if (dhd->pub.busstate != DHD_BUS_DOWN) {
3612                                 /* Stop the protocol module */
3613                                 dhd_prot_stop(&dhd->pub);
3614
3615                                 /* Stop the bus module */
3616                                 dhd_bus_stop(dhd->pub.bus, TRUE);
3617                         }
3618
3619 #if defined(OOB_INTR_ONLY)
3620                         bcmsdh_unregister_oob_intr();
3621 #endif /* defined(OOB_INTR_ONLY) */
3622                 }
3623         }
3624 }
3625
3626
3627 void dhd_detach(dhd_pub_t *dhdp)
3628 {
3629         dhd_info_t *dhd;
3630         unsigned long flags;
3631         int timer_valid = FALSE;
3632
3633         if (!dhdp)
3634                 return;
3635
3636         dhd = (dhd_info_t *)dhdp->info;
3637         if (!dhd)
3638                 return;
3639
3640         DHD_TRACE(("%s: Enter state 0x%x\n", __FUNCTION__, dhd->dhd_state));
3641
3642         if (!(dhd->dhd_state & DHD_ATTACH_STATE_DONE)) {
3643                 /* Give sufficient time for threads to start running in case
3644                  * dhd_attach() has failed
3645                  */
3646                 osl_delay(1000*100);
3647         }
3648
3649 #ifdef ARP_OFFLOAD_SUPPORT
3650         unregister_inetaddr_notifier(&dhd_notifier);
3651 #endif /* ARP_OFFLOAD_SUPPORT */
3652
3653 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
3654         if (dhd->dhd_state & DHD_ATTACH_STATE_EARLYSUSPEND_DONE) {
3655                 if (dhd->early_suspend.suspend)
3656                         unregister_early_suspend(&dhd->early_suspend);
3657         }
3658 #endif /* defined(CONFIG_HAS_EARLYSUSPEND) */
3659
3660 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
3661         cancel_work_sync(&dhd->work_hang);
3662 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))  */
3663
3664 #if defined(CONFIG_BCMDHD_WEXT)
3665         if (dhd->dhd_state & DHD_ATTACH_STATE_WL_ATTACH) {
3666                 /* Detatch and unlink in the iw */
3667                 wl_iw_detach();
3668         }
3669 #endif /* defined(CONFIG_BCMDHD_WEXT) */
3670
3671         if (dhd->thr_sysioc_ctl.thr_pid >= 0) {
3672                 PROC_STOP(&dhd->thr_sysioc_ctl);
3673         }
3674
3675         /* delete all interfaces, start with virtual  */
3676         if (dhd->dhd_state & DHD_ATTACH_STATE_ADD_IF) {
3677                 int i = 1;
3678                 dhd_if_t *ifp;
3679
3680                 /* Cleanup virtual interfaces */
3681                 for (i = 1; i < DHD_MAX_IFS; i++) {
3682                         dhd_net_if_lock_local(dhd);
3683                         if (dhd->iflist[i]) {
3684                                 dhd->iflist[i]->state = DHD_IF_DEL;
3685                                 dhd->iflist[i]->idx = i;
3686                                 dhd_op_if(dhd->iflist[i]);
3687                         }
3688                         dhd_net_if_unlock_local(dhd);
3689                 }
3690                 /*  delete primary interface 0 */
3691                 ifp = dhd->iflist[0];
3692                 ASSERT(ifp);
3693
3694 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
3695                 if (ifp->net->open)
3696 #else
3697                 if (ifp->net->netdev_ops == &dhd_ops_pri)
3698 #endif
3699                 {
3700                         if (ifp->net) {
3701                                 unregister_netdev(ifp->net);
3702                                 free_netdev(ifp->net);
3703                                 ifp->net = NULL;
3704                         }
3705                         MFREE(dhd->pub.osh, ifp, sizeof(*ifp));
3706                         dhd->iflist[0] = NULL;
3707                 }
3708         }
3709
3710         /* Clear the watchdog timer */
3711         flags = dhd_os_spin_lock(&dhd->pub);
3712         timer_valid = dhd->wd_timer_valid;
3713         dhd->wd_timer_valid = FALSE;
3714         dhd_os_spin_unlock(&dhd->pub, flags);
3715         if (timer_valid)
3716                 del_timer_sync(&dhd->timer);
3717
3718         if (dhd->dhd_state & DHD_ATTACH_STATE_THREADS_CREATED) {
3719 #ifdef DHDTHREAD
3720                 if (dhd->thr_wdt_ctl.thr_pid >= 0) {
3721                         PROC_STOP(&dhd->thr_wdt_ctl);
3722                 }
3723
3724                 if (dhd->thr_dpc_ctl.thr_pid >= 0) {
3725                         PROC_STOP(&dhd->thr_dpc_ctl);
3726                 }
3727                 else
3728 #endif /* DHDTHREAD */
3729                 tasklet_kill(&dhd->tasklet);
3730         }
3731         if (dhd->dhd_state & DHD_ATTACH_STATE_PROT_ATTACH) {
3732                 dhd_bus_detach(dhdp);
3733
3734                 if (dhdp->prot)
3735                         dhd_prot_detach(dhdp);
3736         }
3737
3738 #ifdef WL_CFG80211
3739         if (dhd->dhd_state & DHD_ATTACH_STATE_CFG80211) {
3740                 wl_cfg80211_detach(NULL);
3741                 dhd_monitor_uninit();
3742         }
3743 #endif
3744
3745 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP)
3746                 unregister_pm_notifier(&dhd_sleep_pm_notifier);
3747 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP) */
3748
3749         if (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT) {
3750 #ifdef CONFIG_HAS_WAKELOCK
3751                 wake_lock_destroy(&dhd->wl_wifi);
3752                 wake_lock_destroy(&dhd->wl_rxwake);
3753                 wake_lock_destroy(&dhd->wl_ctrlwake);
3754 #endif
3755         }
3756 }
3757
3758
3759 void
3760 dhd_free(dhd_pub_t *dhdp)
3761 {
3762         dhd_info_t *dhd;
3763         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
3764
3765         if (dhdp) {
3766                 dhd = (dhd_info_t *)dhdp->info;
3767                 if (dhd)
3768                         MFREE(dhd->pub.osh, dhd, sizeof(*dhd));
3769         }
3770 }
3771
3772 static void __exit
3773 dhd_module_cleanup(void)
3774 {
3775         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
3776
3777         dhd_bus_unregister();
3778
3779 #if defined(CONFIG_WIFI_CONTROL_FUNC)
3780         wl_android_wifictrl_func_del();
3781 #endif /* CONFIG_WIFI_CONTROL_FUNC */
3782         wl_android_exit();
3783
3784         /* Call customer gpio to turn off power with WL_REG_ON signal */
3785         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
3786 }
3787
3788 static int __init
3789 dhd_module_init(void)
3790 {
3791         int error = 0;
3792
3793         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
3794
3795         wl_android_init();
3796
3797 #ifdef DHDTHREAD
3798         /* Sanity check on the module parameters */
3799         do {
3800                 /* Both watchdog and DPC as tasklets are ok */
3801                 if ((dhd_watchdog_prio < 0) && (dhd_dpc_prio < 0))
3802                         break;
3803
3804                 /* If both watchdog and DPC are threads, TX must be deferred */
3805                 if ((dhd_watchdog_prio >= 0) && (dhd_dpc_prio >= 0) && dhd_deferred_tx)
3806                         break;
3807
3808                 DHD_ERROR(("Invalid module parameters.\n"));
3809                 return -EINVAL;
3810         } while (0);
3811 #endif /* DHDTHREAD */
3812
3813         /* Call customer gpio to turn on power with WL_REG_ON signal */
3814         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_ON);
3815
3816 #if defined(CONFIG_WIFI_CONTROL_FUNC)
3817         if (wl_android_wifictrl_func_add() < 0)
3818                 goto fail_1;
3819 #endif
3820
3821 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
3822                 sema_init(&dhd_registration_sem, 0);
3823 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) */
3824         error = dhd_bus_register();
3825
3826         if (!error)
3827                 printf("\n%s\n", dhd_version);
3828         else {
3829                 DHD_ERROR(("%s: sdio_register_driver failed\n", __FUNCTION__));
3830                 goto fail_1;
3831         }
3832
3833 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
3834         /*
3835          * Wait till MMC sdio_register_driver callback called and made driver attach.
3836          * It's needed to make sync up exit from dhd insmod  and
3837          * Kernel MMC sdio device callback registration
3838          */
3839         if (down_timeout(&dhd_registration_sem,  msecs_to_jiffies(DHD_REGISTRATION_TIMEOUT)) != 0) {
3840                 error = -ENODEV;
3841                 DHD_ERROR(("%s: sdio_register_driver timeout\n", __FUNCTION__));
3842                 goto fail_2;
3843         }
3844 #endif
3845 #if defined(WL_CFG80211)
3846         wl_android_post_init();
3847 #endif /* defined(WL_CFG80211) */
3848
3849         return error;
3850 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
3851 fail_2:
3852         dhd_bus_unregister();
3853 #endif
3854 fail_1:
3855 #if defined(CONFIG_WIFI_CONTROL_FUNC)
3856         wl_android_wifictrl_func_del();
3857 #endif 
3858
3859         /* Call customer gpio to turn off power with WL_REG_ON signal */
3860         dhd_customer_gpio_wlan_ctrl(WLAN_POWER_OFF);
3861
3862         return error;
3863 }
3864
3865 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
3866 late_initcall(dhd_module_init);
3867 #else
3868 module_init(dhd_module_init);
3869 #endif
3870 module_exit(dhd_module_cleanup);
3871
3872 /*
3873  * OS specific functions required to implement DHD driver in OS independent way
3874  */
3875 int
3876 dhd_os_proto_block(dhd_pub_t *pub)
3877 {
3878         dhd_info_t * dhd = (dhd_info_t *)(pub->info);
3879
3880         if (dhd) {
3881                 down(&dhd->proto_sem);
3882                 return 1;
3883         }
3884
3885         return 0;
3886 }
3887
3888 int
3889 dhd_os_proto_unblock(dhd_pub_t *pub)
3890 {
3891         dhd_info_t * dhd = (dhd_info_t *)(pub->info);
3892
3893         if (dhd) {
3894                 up(&dhd->proto_sem);
3895                 return 1;
3896         }
3897
3898         return 0;
3899 }
3900
3901 unsigned int
3902 dhd_os_get_ioctl_resp_timeout(void)
3903 {
3904         return ((unsigned int)dhd_ioctl_timeout_msec);
3905 }
3906
3907 void
3908 dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
3909 {
3910         dhd_ioctl_timeout_msec = (int)timeout_msec;
3911 }
3912
3913 int
3914 dhd_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition, bool *pending)
3915 {
3916         dhd_info_t * dhd = (dhd_info_t *)(pub->info);
3917         DECLARE_WAITQUEUE(wait, current);
3918         int timeout = dhd_ioctl_timeout_msec;
3919
3920         /* Convert timeout in millsecond to jiffies */
3921 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
3922         timeout = msecs_to_jiffies(timeout);
3923 #else
3924         timeout = timeout * HZ / 1000;
3925 #endif
3926
3927         /* Wait until control frame is available */
3928         add_wait_queue(&dhd->ioctl_resp_wait, &wait);
3929         set_current_state(TASK_INTERRUPTIBLE);
3930
3931         /* Memory barrier to support multi-processing
3932          * As the variable "condition", which points to dhd->rxlen (dhd_bus_rxctl[dhd_sdio.c])
3933          * Can be changed by another processor.
3934          */
3935         smp_mb();
3936         while (!(*condition) && timeout) {
3937                 timeout = schedule_timeout(timeout);
3938                 smp_mb();
3939         }
3940
3941         set_current_state(TASK_RUNNING);
3942         remove_wait_queue(&dhd->ioctl_resp_wait, &wait);
3943
3944         return timeout;
3945 }
3946
3947 int
3948 dhd_os_ioctl_resp_wake(dhd_pub_t *pub)
3949 {
3950         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
3951
3952         if (waitqueue_active(&dhd->ioctl_resp_wait)) {
3953                 wake_up_interruptible(&dhd->ioctl_resp_wait);
3954         }
3955
3956         return 0;
3957 }
3958
3959 void
3960 dhd_os_wd_timer(void *bus, uint wdtick)
3961 {
3962         dhd_pub_t *pub = bus;
3963         dhd_info_t *dhd = (dhd_info_t *)pub->info;
3964         unsigned long flags;
3965
3966         DHD_TRACE(("%s: Enter\n", __FUNCTION__));
3967
3968         flags = dhd_os_spin_lock(pub);
3969
3970         /* don't start the wd until fw is loaded */
3971         if (pub->busstate == DHD_BUS_DOWN) {
3972                 dhd_os_spin_unlock(pub, flags);
3973                 return;
3974         }
3975
3976         /* Totally stop the timer */
3977         if (!wdtick && dhd->wd_timer_valid == TRUE) {
3978                 dhd->wd_timer_valid = FALSE;
3979                 dhd_os_spin_unlock(pub, flags);
3980 #ifdef DHDTHREAD
3981                 del_timer_sync(&dhd->timer);
3982 #else
3983                 del_timer(&dhd->timer);
3984 #endif /* DHDTHREAD */
3985                 return;
3986         }
3987
3988         if (wdtick) {
3989                 dhd_watchdog_ms = (uint)wdtick;
3990                 /* Re arm the timer, at last watchdog period */
3991                 mod_timer(&dhd->timer, jiffies + dhd_watchdog_ms * HZ / 1000);
3992                 dhd->wd_timer_valid = TRUE;
3993         }
3994         dhd_os_spin_unlock(pub, flags);
3995 }
3996
3997 void *
3998 dhd_os_open_image(char *filename)
3999 {
4000         struct file *fp;
4001
4002         fp = filp_open(filename, O_RDONLY, 0);
4003         /*
4004          * 2.6.11 (FC4) supports filp_open() but later revs don't?
4005          * Alternative:
4006          * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
4007          * ???
4008          */
4009          if (IS_ERR(fp))
4010                  fp = NULL;
4011
4012          return fp;
4013 }
4014
4015 int
4016 dhd_os_get_image_block(char *buf, int len, void *image)
4017 {
4018         struct file *fp = (struct file *)image;
4019         int rdlen;
4020
4021         if (!image)
4022                 return 0;
4023
4024         rdlen = kernel_read(fp, fp->f_pos, buf, len);
4025         if (rdlen > 0)
4026                 fp->f_pos += rdlen;
4027
4028         return rdlen;
4029 }
4030
4031 void
4032 dhd_os_close_image(void *image)
4033 {
4034         if (image)
4035                 filp_close((struct file *)image, NULL);
4036 }
4037
4038
4039 void
4040 dhd_os_sdlock(dhd_pub_t *pub)
4041 {
4042         dhd_info_t *dhd;
4043
4044         dhd = (dhd_info_t *)(pub->info);
4045
4046 #ifdef DHDTHREAD
4047         if (dhd->threads_only)
4048                 down(&dhd->sdsem);
4049         else
4050 #endif /* DHDTHREAD */
4051         spin_lock_bh(&dhd->sdlock);
4052 }
4053
4054 void
4055 dhd_os_sdunlock(dhd_pub_t *pub)
4056 {
4057         dhd_info_t *dhd;
4058
4059         dhd = (dhd_info_t *)(pub->info);
4060
4061 #ifdef DHDTHREAD
4062         if (dhd->threads_only)
4063                 up(&dhd->sdsem);
4064         else
4065 #endif /* DHDTHREAD */
4066         spin_unlock_bh(&dhd->sdlock);
4067 }
4068
4069 void
4070 dhd_os_sdlock_txq(dhd_pub_t *pub)
4071 {
4072         dhd_info_t *dhd;
4073
4074         dhd = (dhd_info_t *)(pub->info);
4075         spin_lock_bh(&dhd->txqlock);
4076 }
4077
4078 void
4079 dhd_os_sdunlock_txq(dhd_pub_t *pub)
4080 {
4081         dhd_info_t *dhd;
4082
4083         dhd = (dhd_info_t *)(pub->info);
4084         spin_unlock_bh(&dhd->txqlock);
4085 }
4086
4087 void
4088 dhd_os_sdlock_rxq(dhd_pub_t *pub)
4089 {
4090 }
4091
4092 void
4093 dhd_os_sdunlock_rxq(dhd_pub_t *pub)
4094 {
4095 }
4096
4097 void
4098 dhd_os_sdtxlock(dhd_pub_t *pub)
4099 {
4100         dhd_os_sdlock(pub);
4101 }
4102
4103 void
4104 dhd_os_sdtxunlock(dhd_pub_t *pub)
4105 {
4106         dhd_os_sdunlock(pub);
4107 }
4108
4109 #if defined(CONFIG_DHD_USE_STATIC_BUF)
4110 uint8* dhd_os_prealloc(void *osh, int section, uint size)
4111 {
4112         return (uint8*)wl_android_prealloc(section, size);
4113 }
4114
4115 void dhd_os_prefree(void *osh, void *addr, uint size)
4116 {
4117 }
4118 #endif /* defined(CONFIG_DHD_USE_STATIC_BUF) */
4119
4120 #if defined(CONFIG_BCMDHD_WEXT)
4121 struct iw_statistics *
4122 dhd_get_wireless_stats(struct net_device *dev)
4123 {
4124         int res = 0;
4125         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4126
4127         if (!dhd->pub.up) {
4128                 return NULL;
4129         }
4130
4131         res = wl_iw_get_wireless_stats(dev, &dhd->iw.wstats);
4132
4133         if (res == 0)
4134                 return &dhd->iw.wstats;
4135         else
4136                 return NULL;
4137 }
4138 #endif /* defined(CONFIG_BCMDHD_WEXT) */
4139
4140 static int
4141 dhd_wl_host_event(dhd_info_t *dhd, int *ifidx, void *pktdata,
4142         wl_event_msg_t *event, void **data)
4143 {
4144         int bcmerror = 0;
4145         ASSERT(dhd != NULL);
4146
4147         bcmerror = wl_host_event(&dhd->pub, ifidx, pktdata, event, data);
4148         if (bcmerror != BCME_OK)
4149                 return (bcmerror);
4150
4151 #if defined(CONFIG_BCMDHD_WEXT)
4152         if (event->bsscfgidx == 0) {
4153                 /*
4154                  * Wireless ext is on primary interface only
4155                  */
4156
4157                 ASSERT(dhd->iflist[*ifidx] != NULL);
4158                 ASSERT(dhd->iflist[*ifidx]->net != NULL);
4159
4160                 if (dhd->iflist[*ifidx]->net) {
4161                         wl_iw_event(dhd->iflist[*ifidx]->net, event, *data);
4162                 }
4163         }
4164 #endif /* defined(CONFIG_BCMDHD_WEXT)  */
4165
4166 #ifdef WL_CFG80211
4167         if ((ntoh32(event->event_type) == WLC_E_IF) &&
4168                 (((dhd_if_event_t *)*data)->action == WLC_E_IF_ADD))
4169                 /* If ADD_IF has been called directly by wl utility then we
4170                 * should not report this. In case if ADD_IF was called from
4171                 * CFG stack, then too this event need not be reported back
4172                 */
4173                return (BCME_OK);
4174         if ((wl_cfg80211_is_progress_ifchange() ||
4175                 wl_cfg80211_is_progress_ifadd()) && (*ifidx != 0)) {
4176                 /*
4177                  * If IF_ADD/CHANGE operation is going on,
4178                  *  discard any event received on the virtual I/F
4179                  */
4180                 return (BCME_OK);
4181         }
4182
4183         ASSERT(dhd->iflist[*ifidx] != NULL);
4184         ASSERT(dhd->iflist[*ifidx]->net != NULL);
4185         if (dhd->iflist[*ifidx]->net) {
4186                 wl_cfg80211_event(dhd->iflist[*ifidx]->net, event, *data);
4187         }
4188 #endif /* defined(WL_CFG80211) */
4189
4190         return (bcmerror);
4191 }
4192
4193 /* send up locally generated event */
4194 void
4195 dhd_sendup_event(dhd_pub_t *dhdp, wl_event_msg_t *event, void *data)
4196 {
4197         switch (ntoh32(event->event_type)) {
4198         /* Send up locally generated AMP HCI Events */
4199         case WLC_E_BTA_HCI_EVENT: {
4200                 struct sk_buff *p, *skb;
4201                 bcm_event_t *msg;
4202                 wl_event_msg_t *p_bcm_event;
4203                 char *ptr;
4204                 uint32 len;
4205                 uint32 pktlen;
4206                 dhd_if_t *ifp;
4207                 dhd_info_t *dhd;
4208                 uchar *eth;
4209                 int ifidx;
4210
4211                 len = ntoh32(event->datalen);
4212                 pktlen = sizeof(bcm_event_t) + len + 2;
4213                 dhd = dhdp->info;
4214                 ifidx = dhd_ifname2idx(dhd, event->ifname);
4215
4216                 if ((p = PKTGET(dhdp->osh, pktlen, FALSE))) {
4217                         ASSERT(ISALIGNED((uintptr)PKTDATA(dhdp->osh, p), sizeof(uint32)));
4218
4219                         msg = (bcm_event_t *) PKTDATA(dhdp->osh, p);
4220
4221                         bcopy(&dhdp->mac, &msg->eth.ether_dhost, ETHER_ADDR_LEN);
4222                         bcopy(&dhdp->mac, &msg->eth.ether_shost, ETHER_ADDR_LEN);
4223                         ETHER_TOGGLE_LOCALADDR(&msg->eth.ether_shost);
4224
4225                         msg->eth.ether_type = hton16(ETHER_TYPE_BRCM);
4226
4227                         /* BCM Vendor specific header... */
4228                         msg->bcm_hdr.subtype = hton16(BCMILCP_SUBTYPE_VENDOR_LONG);
4229                         msg->bcm_hdr.version = BCMILCP_BCM_SUBTYPEHDR_VERSION;
4230                         bcopy(BRCM_OUI, &msg->bcm_hdr.oui[0], DOT11_OUI_LEN);
4231
4232                         /* vendor spec header length + pvt data length (private indication
4233                          *  hdr + actual message itself)
4234                          */
4235                         msg->bcm_hdr.length = hton16(BCMILCP_BCM_SUBTYPEHDR_MINLENGTH +
4236                                 BCM_MSG_LEN + sizeof(wl_event_msg_t) + (uint16)len);
4237                         msg->bcm_hdr.usr_subtype = hton16(BCMILCP_BCM_SUBTYPE_EVENT);
4238
4239                         PKTSETLEN(dhdp->osh, p, (sizeof(bcm_event_t) + len + 2));
4240
4241                         /* copy  wl_event_msg_t into sk_buf */
4242
4243                         /* pointer to wl_event_msg_t in sk_buf */
4244                         p_bcm_event = &msg->event;
4245                         bcopy(event, p_bcm_event, sizeof(wl_event_msg_t));
4246
4247                         /* copy hci event into sk_buf */
4248                         bcopy(data, (p_bcm_event + 1), len);
4249
4250                         msg->bcm_hdr.length  = hton16(sizeof(wl_event_msg_t) +
4251                                 ntoh16(msg->bcm_hdr.length));
4252                         PKTSETLEN(dhdp->osh, p, (sizeof(bcm_event_t) + len + 2));
4253
4254                         ptr = (char *)(msg + 1);
4255                         /* Last 2 bytes of the message are 0x00 0x00 to signal that there
4256                          * are no ethertypes which are following this
4257                          */
4258                         ptr[len+0] = 0x00;
4259                         ptr[len+1] = 0x00;
4260
4261                         skb = PKTTONATIVE(dhdp->osh, p);
4262                         eth = skb->data;
4263                         len = skb->len;
4264
4265                         ifp = dhd->iflist[ifidx];
4266                         if (ifp == NULL)
4267                              ifp = dhd->iflist[0];
4268
4269                         ASSERT(ifp);
4270                         skb->dev = ifp->net;
4271                         skb->protocol = eth_type_trans(skb, skb->dev);
4272
4273                         skb->data = eth;
4274                         skb->len = len;
4275
4276                         /* Strip header, count, deliver upward */
4277                         skb_pull(skb, ETH_HLEN);
4278
4279                         /* Send the packet */
4280                         if (in_interrupt()) {
4281                                 netif_rx(skb);
4282                         } else {
4283                                 netif_rx_ni(skb);
4284                         }
4285                 }
4286                 else {
4287                         /* Could not allocate a sk_buf */
4288                         DHD_ERROR(("%s: unable to alloc sk_buf", __FUNCTION__));
4289                 }
4290                 break;
4291         } /* case WLC_E_BTA_HCI_EVENT */
4292
4293         default:
4294                 break;
4295         }
4296 }
4297
4298 void dhd_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
4299 {
4300 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
4301         struct dhd_info *dhdinfo =  dhd->info;
4302         dhd_os_sdunlock(dhd);
4303         wait_event_interruptible_timeout(dhdinfo->ctrl_wait, (*lockvar == FALSE), HZ * 2);
4304         dhd_os_sdlock(dhd);
4305 #endif
4306         return;
4307 }
4308
4309 void dhd_wait_event_wakeup(dhd_pub_t *dhd)
4310 {
4311 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
4312         struct dhd_info *dhdinfo =  dhd->info;
4313         if (waitqueue_active(&dhdinfo->ctrl_wait))
4314                 wake_up_interruptible(&dhdinfo->ctrl_wait);
4315 #endif
4316         return;
4317 }
4318
4319 int
4320 dhd_dev_reset(struct net_device *dev, uint8 flag)
4321 {
4322         int ret;
4323
4324         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4325
4326         ret = dhd_bus_devreset(&dhd->pub, flag);
4327         if (ret) {
4328                 DHD_ERROR(("%s: dhd_bus_devreset: %d\n", __FUNCTION__, ret));
4329                 return ret;
4330         }
4331
4332         return ret;
4333 }
4334
4335 int net_os_set_suspend_disable(struct net_device *dev, int val)
4336 {
4337         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4338         int ret = 0;
4339
4340         if (dhd) {
4341                 ret = dhd->pub.suspend_disable_flag;
4342                 dhd->pub.suspend_disable_flag = val;
4343         }
4344         return ret;
4345 }
4346
4347 int net_os_set_suspend(struct net_device *dev, int val, int force)
4348 {
4349         int ret = 0;
4350         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4351
4352         if (dhd) {
4353 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
4354                 ret = dhd_set_suspend(val, &dhd->pub);
4355 #else
4356                 ret = dhd_suspend_resume_helper(dhd, val, force);
4357 #endif
4358         }
4359         return ret;
4360 }
4361
4362 int net_os_set_dtim_skip(struct net_device *dev, int val)
4363 {
4364         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4365
4366         if (dhd)
4367                 dhd->pub.dtim_skip = val;
4368
4369         return 0;
4370 }
4371
4372 int net_os_rxfilter_add_remove(struct net_device *dev, int add_remove, int num)
4373 {
4374         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4375         char *filterp = NULL;
4376         int ret = 0;
4377
4378         if (!dhd || (num == DHD_UNICAST_FILTER_NUM) ||
4379             (num == DHD_MDNS_FILTER_NUM))
4380                 return ret;
4381         if (num >= dhd->pub.pktfilter_count)
4382                 return -EINVAL;
4383         if (add_remove) {
4384                 switch (num) {
4385                 case DHD_BROADCAST_FILTER_NUM:
4386                         filterp = "101 0 0 0 0xFFFFFFFFFFFF 0xFFFFFFFFFFFF";
4387                         break;
4388                 case DHD_MULTICAST4_FILTER_NUM:
4389                         filterp = "102 0 0 0 0xFFFFFF 0x01005E";
4390                         break;
4391                 case DHD_MULTICAST6_FILTER_NUM:
4392                         filterp = "103 0 0 0 0xFFFF 0x3333";
4393                         break;
4394                 default:
4395                         return -EINVAL;
4396                 }
4397         }
4398         dhd->pub.pktfilter[num] = filterp;
4399         return ret;
4400 }
4401
4402 int dhd_os_set_packet_filter(dhd_pub_t *dhdp, int val)
4403 {
4404         int ret = 0;
4405
4406         /* Packet filtering is set only if we still in early-suspend and
4407          * we need either to turn it ON or turn it OFF
4408          * We can always turn it OFF in case of early-suspend, but we turn it
4409          * back ON only if suspend_disable_flag was not set
4410         */
4411         if (dhdp && dhdp->up) {
4412                 if (dhdp->in_suspend) {
4413                         if (!val || (val && !dhdp->suspend_disable_flag))
4414                                 dhd_set_packet_filter(val, dhdp);
4415                 }
4416         }
4417         return ret;
4418
4419 }
4420
4421 int net_os_set_packet_filter(struct net_device *dev, int val)
4422 {
4423         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4424
4425         return dhd_os_set_packet_filter(&dhd->pub, val);
4426 }
4427
4428 int
4429 dhd_dev_init_ioctl(struct net_device *dev)
4430 {
4431         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4432
4433         return dhd_preinit_ioctls(&dhd->pub);
4434 }
4435
4436 #ifdef PNO_SUPPORT
4437 /* Linux wrapper to call common dhd_pno_clean */
4438 int
4439 dhd_dev_pno_reset(struct net_device *dev)
4440 {
4441         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4442
4443         return (dhd_pno_clean(&dhd->pub));
4444 }
4445
4446
4447 /* Linux wrapper to call common dhd_pno_enable */
4448 int
4449 dhd_dev_pno_enable(struct net_device *dev,  int pfn_enabled)
4450 {
4451         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4452
4453         return (dhd_pno_enable(&dhd->pub, pfn_enabled));
4454 }
4455
4456
4457 /* Linux wrapper to call common dhd_pno_set */
4458 int
4459 dhd_dev_pno_set(struct net_device *dev, wlc_ssid_t* ssids_local, int nssid,
4460         ushort  scan_fr, int pno_repeat, int pno_freq_expo_max)
4461 {
4462         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4463
4464         return (dhd_pno_set(&dhd->pub, ssids_local, nssid, scan_fr, pno_repeat, pno_freq_expo_max));
4465 }
4466
4467 /* Linux wrapper to get  pno status */
4468 int
4469 dhd_dev_get_pno_status(struct net_device *dev)
4470 {
4471         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4472
4473         return (dhd_pno_get_status(&dhd->pub));
4474 }
4475
4476 #endif /* PNO_SUPPORT */
4477
4478 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4479 static void dhd_hang_process(struct work_struct *work)
4480 {
4481         dhd_info_t *dhd;
4482         struct net_device *dev;
4483
4484         dhd = (dhd_info_t *)container_of(work, dhd_info_t, work_hang);
4485                 dev = dhd->iflist[0]->net;
4486
4487                 if (dev) {
4488                 rtnl_lock();
4489                 dev_close(dev);
4490                 rtnl_unlock();
4491 #if defined(WL_WIRELESS_EXT)
4492                 wl_iw_send_priv_event(dev, "HANG");
4493 #endif
4494 #if defined(WL_CFG80211)
4495                 wl_cfg80211_hang(dev, WLAN_REASON_UNSPECIFIED);
4496 #endif
4497        }
4498 }
4499
4500 int net_os_send_hang_message(struct net_device *dev)
4501 {
4502         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4503         int ret = 0;
4504
4505         if (dhd) {
4506                 if (!dhd->pub.hang_was_sent) {
4507                         dhd->pub.hang_was_sent = 1;
4508                         schedule_work(&dhd->work_hang);
4509                 }
4510         }
4511         return ret;
4512 }
4513 #endif
4514
4515 void dhd_bus_country_set(struct net_device *dev, wl_country_t *cspec)
4516 {
4517         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4518
4519         if (dhd && dhd->pub.up)
4520                 memcpy(&dhd->pub.dhd_cspec, cspec, sizeof(wl_country_t));
4521 }
4522
4523 void dhd_net_if_lock(struct net_device *dev)
4524 {
4525         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4526         dhd_net_if_lock_local(dhd);
4527 }
4528
4529 void dhd_net_if_unlock(struct net_device *dev)
4530 {
4531         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4532         dhd_net_if_unlock_local(dhd);
4533 }
4534
4535 static void dhd_net_if_lock_local(dhd_info_t *dhd)
4536 {
4537 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
4538         if (dhd)
4539                 mutex_lock(&dhd->dhd_net_if_mutex);
4540 #endif
4541 }
4542
4543 static void dhd_net_if_unlock_local(dhd_info_t *dhd)
4544 {
4545 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
4546         if (dhd)
4547                 mutex_unlock(&dhd->dhd_net_if_mutex);
4548 #endif
4549 }
4550
4551 static void dhd_suspend_lock(dhd_pub_t *pub)
4552 {
4553 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
4554         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4555         if (dhd)
4556                 mutex_lock(&dhd->dhd_suspend_mutex);
4557 #endif
4558 }
4559
4560 static void dhd_suspend_unlock(dhd_pub_t *pub)
4561 {
4562 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
4563         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4564         if (dhd)
4565                 mutex_unlock(&dhd->dhd_suspend_mutex);
4566 #endif
4567 }
4568
4569 unsigned long dhd_os_spin_lock(dhd_pub_t *pub)
4570 {
4571         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4572         unsigned long flags = 0;
4573
4574         if (dhd)
4575                 spin_lock_irqsave(&dhd->dhd_lock, flags);
4576
4577         return flags;
4578 }
4579
4580 void dhd_os_spin_unlock(dhd_pub_t *pub, unsigned long flags)
4581 {
4582         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4583
4584         if (dhd)
4585                 spin_unlock_irqrestore(&dhd->dhd_lock, flags);
4586 }
4587
4588 static int
4589 dhd_get_pend_8021x_cnt(dhd_info_t *dhd)
4590 {
4591         return (atomic_read(&dhd->pend_8021x_cnt));
4592 }
4593
4594 #define MAX_WAIT_FOR_8021X_TX   10
4595
4596 int
4597 dhd_wait_pend8021x(struct net_device *dev)
4598 {
4599         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4600         int timeout = 10 * HZ / 1000;
4601         int ntimes = MAX_WAIT_FOR_8021X_TX;
4602         int pend = dhd_get_pend_8021x_cnt(dhd);
4603
4604         while (ntimes && pend) {
4605                 if (pend) {
4606                         set_current_state(TASK_INTERRUPTIBLE);
4607                         schedule_timeout(timeout);
4608                         set_current_state(TASK_RUNNING);
4609                         ntimes--;
4610                 }
4611                 pend = dhd_get_pend_8021x_cnt(dhd);
4612         }
4613         return pend;
4614 }
4615
4616 #ifdef DHD_DEBUG
4617 int
4618 write_to_file(dhd_pub_t *dhd, uint8 *buf, int size)
4619 {
4620         int ret = 0;
4621         struct file *fp;
4622         mm_segment_t old_fs;
4623         loff_t pos = 0;
4624
4625         /* change to KERNEL_DS address limit */
4626         old_fs = get_fs();
4627         set_fs(KERNEL_DS);
4628
4629         /* open file to write */
4630         fp = filp_open("/tmp/mem_dump", O_WRONLY|O_CREAT, 0640);
4631         if (!fp) {
4632                 printf("%s: open file error\n", __FUNCTION__);
4633                 ret = -1;
4634                 goto exit;
4635         }
4636
4637         /* Write buf to file */
4638         fp->f_op->write(fp, buf, size, &pos);
4639
4640 exit:
4641         /* free buf before return */
4642         MFREE(dhd->osh, buf, size);
4643         /* close file before return */
4644         if (fp)
4645                 filp_close(fp, current->files);
4646         /* restore previous address limit */
4647         set_fs(old_fs);
4648
4649         return ret;
4650 }
4651 #endif /* DHD_DEBUG */
4652
4653 int dhd_os_wake_lock_timeout(dhd_pub_t *pub)
4654 {
4655         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4656         unsigned long flags;
4657         int ret = 0;
4658
4659         if (dhd) {
4660                 spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
4661                 ret = dhd->wakelock_rx_timeout_enable > dhd->wakelock_ctrl_timeout_enable ?
4662                         dhd->wakelock_rx_timeout_enable : dhd->wakelock_ctrl_timeout_enable;
4663 #ifdef CONFIG_HAS_WAKELOCK
4664                 if (dhd->wakelock_rx_timeout_enable)
4665                         wake_lock_timeout(&dhd->wl_rxwake,
4666                                 msecs_to_jiffies(dhd->wakelock_rx_timeout_enable));
4667                 if (dhd->wakelock_ctrl_timeout_enable)
4668                         wake_lock_timeout(&dhd->wl_ctrlwake,
4669                                 msecs_to_jiffies(dhd->wakelock_ctrl_timeout_enable));
4670 #endif
4671                 dhd->wakelock_rx_timeout_enable = 0;
4672                 dhd->wakelock_ctrl_timeout_enable = 0;
4673                 spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
4674         }
4675         return ret;
4676 }
4677
4678 int net_os_wake_lock_timeout(struct net_device *dev)
4679 {
4680         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4681         int ret = 0;
4682
4683         if (dhd)
4684                 ret = dhd_os_wake_lock_timeout(&dhd->pub);
4685         return ret;
4686 }
4687
4688 int dhd_os_wake_lock_rx_timeout_enable(dhd_pub_t *pub, int val)
4689 {
4690         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4691         unsigned long flags;
4692
4693         if (dhd) {
4694                 spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
4695                 if (val > dhd->wakelock_rx_timeout_enable)
4696                         dhd->wakelock_rx_timeout_enable = val;
4697                 spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
4698         }
4699         return 0;
4700 }
4701
4702 int dhd_os_wake_lock_ctrl_timeout_enable(dhd_pub_t *pub, int val)
4703 {
4704         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4705         unsigned long flags;
4706
4707         if (dhd) {
4708                 spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
4709                 if (val > dhd->wakelock_ctrl_timeout_enable)
4710                         dhd->wakelock_ctrl_timeout_enable = val;
4711                 spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
4712         }
4713         return 0;
4714 }
4715
4716 int net_os_wake_lock_rx_timeout_enable(struct net_device *dev, int val)
4717 {
4718         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4719         int ret = 0;
4720
4721         if (dhd)
4722                 ret = dhd_os_wake_lock_rx_timeout_enable(&dhd->pub, val);
4723         return ret;
4724 }
4725
4726 int net_os_wake_lock_ctrl_timeout_enable(struct net_device *dev, int val)
4727 {
4728         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4729         int ret = 0;
4730
4731         if (dhd)
4732                 ret = dhd_os_wake_lock_ctrl_timeout_enable(&dhd->pub, val);
4733         return ret;
4734 }
4735
4736 int dhd_os_wake_lock(dhd_pub_t *pub)
4737 {
4738         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4739         unsigned long flags;
4740         int ret = 0;
4741
4742         if (dhd) {
4743                 spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
4744 #ifdef CONFIG_HAS_WAKELOCK
4745                 if (!dhd->wakelock_counter)
4746                         wake_lock(&dhd->wl_wifi);
4747 #endif
4748                 dhd->wakelock_counter++;
4749                 ret = dhd->wakelock_counter;
4750                 spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
4751         }
4752         return ret;
4753 }
4754
4755 int net_os_wake_lock(struct net_device *dev)
4756 {
4757         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4758         int ret = 0;
4759
4760         if (dhd)
4761                 ret = dhd_os_wake_lock(&dhd->pub);
4762         return ret;
4763 }
4764
4765 int dhd_os_wake_unlock(dhd_pub_t *pub)
4766 {
4767         dhd_info_t *dhd = (dhd_info_t *)(pub->info);
4768         unsigned long flags;
4769         int ret = 0;
4770
4771         dhd_os_wake_lock_timeout(pub);
4772         if (dhd) {
4773                 spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
4774                 if (dhd->wakelock_counter) {
4775                         dhd->wakelock_counter--;
4776 #ifdef CONFIG_HAS_WAKELOCK
4777                         if (!dhd->wakelock_counter)
4778                                 wake_unlock(&dhd->wl_wifi);
4779 #endif
4780                         ret = dhd->wakelock_counter;
4781                 }
4782                 spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
4783         }
4784         return ret;
4785 }
4786
4787 int dhd_os_check_wakelock(void *dhdp)
4788 {
4789 #ifdef CONFIG_HAS_WAKELOCK
4790         dhd_pub_t *pub = (dhd_pub_t *)dhdp;
4791         dhd_info_t *dhd;
4792
4793         if (!pub)
4794                 return 0;
4795         dhd = (dhd_info_t *)(pub->info);
4796
4797         if (dhd && wake_lock_active(&dhd->wl_wifi))
4798                 return 1;
4799 #endif
4800         return 0;
4801 }
4802
4803 int net_os_wake_unlock(struct net_device *dev)
4804 {
4805         dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
4806         int ret = 0;
4807
4808         if (dhd)
4809                 ret = dhd_os_wake_unlock(&dhd->pub);
4810         return ret;
4811 }
4812
4813 int dhd_os_check_if_up(void *dhdp)
4814 {
4815         dhd_pub_t *pub = (dhd_pub_t *)dhdp;
4816
4817         if (!pub)
4818                 return 0;
4819         return pub->up;
4820 }
4821
4822 int dhd_ioctl_entry_local(struct net_device *net, wl_ioctl_t *ioc, int cmd)
4823 {
4824         int ifidx;
4825         int ret = 0;
4826         dhd_info_t *dhd = NULL;
4827
4828         if (!net || !netdev_priv(net)) {
4829                 DHD_ERROR(("%s invalid parameter\n", __FUNCTION__));
4830                 return -EINVAL;
4831         }
4832
4833         dhd = *(dhd_info_t **)netdev_priv(net);
4834         ifidx = dhd_net2idx(dhd, net);
4835         if (ifidx == DHD_BAD_IF) {
4836                 DHD_ERROR(("%s bad ifidx\n", __FUNCTION__));
4837                 return -ENODEV;
4838         }
4839
4840         DHD_OS_WAKE_LOCK(&dhd->pub);
4841         ret = dhd_wl_ioctl(&dhd->pub, ifidx, ioc, ioc->buf, ioc->len);
4842         dhd_check_hang(net, &dhd->pub, ret);
4843         DHD_OS_WAKE_UNLOCK(&dhd->pub);
4844
4845         return ret;
4846 }
4847
4848 bool dhd_os_check_hang(dhd_pub_t *dhdp, int ifidx, int ret)
4849 {
4850         struct net_device *net;
4851
4852         net = dhd_idx2net(dhdp, ifidx);
4853         return dhd_check_hang(net, dhdp, ret);
4854 }
4855
4856 #ifdef PROP_TXSTATUS
4857 extern int dhd_wlfc_interface_entry_update(void* state, ewlfc_mac_entry_action_t action, uint8 ifid,
4858         uint8 iftype, uint8* ea);
4859 extern int dhd_wlfc_FIFOcreditmap_update(void* state, uint8* credits);
4860
4861 int dhd_wlfc_interface_event(struct dhd_info *dhd,
4862         ewlfc_mac_entry_action_t action, uint8 ifid, uint8 iftype, uint8* ea)
4863 {
4864         if (dhd->pub.wlfc_state == NULL)
4865                 return BCME_OK;
4866
4867         return dhd_wlfc_interface_entry_update(dhd->pub.wlfc_state, action, ifid, iftype, ea);
4868 }
4869
4870 int dhd_wlfc_FIFOcreditmap_event(struct dhd_info *dhd, uint8* event_data)
4871 {
4872         if (dhd->pub.wlfc_state == NULL)
4873                 return BCME_OK;
4874
4875         return dhd_wlfc_FIFOcreditmap_update(dhd->pub.wlfc_state, event_data);
4876 }
4877
4878 int dhd_wlfc_event(struct dhd_info *dhd)
4879 {
4880         return dhd_wlfc_enable(&dhd->pub);
4881 }
4882 #endif /* PROP_TXSTATUS */
4883
4884 #ifdef BCMDBGFS
4885
4886 #include <linux/debugfs.h>
4887
4888 extern uint32 dhd_readregl(void *bp, uint32 addr);
4889 extern uint32 dhd_writeregl(void *bp, uint32 addr, uint32 data);
4890
4891 typedef struct dhd_dbgfs {
4892         struct dentry   *debugfs_dir;
4893         struct dentry   *debugfs_mem;
4894         dhd_pub_t       *dhdp;
4895         uint32          size;
4896 } dhd_dbgfs_t;
4897
4898 dhd_dbgfs_t g_dbgfs;
4899
4900 static int
4901 dhd_dbg_state_open(struct inode *inode, struct file *file)
4902 {
4903         file->private_data = inode->i_private;
4904         return 0;
4905 }
4906
4907 static ssize_t
4908 dhd_dbg_state_read(struct file *file, char __user *ubuf,
4909                        size_t count, loff_t *ppos)
4910 {
4911         ssize_t rval;
4912         uint32 tmp;
4913         loff_t pos = *ppos;
4914         size_t ret;
4915
4916         if (pos < 0)
4917                 return -EINVAL;
4918         if (pos >= g_dbgfs.size || !count)
4919                 return 0;
4920         if (count > g_dbgfs.size - pos)
4921                 count = g_dbgfs.size - pos;
4922
4923         /* Basically enforce aligned 4 byte reads. It's up to the user to work out the details */
4924         tmp = dhd_readregl(g_dbgfs.dhdp->bus, file->f_pos & (~3));
4925
4926         ret = copy_to_user(ubuf, &tmp, 4);
4927         if (ret == count)
4928                 return -EFAULT;
4929
4930         count -= ret;
4931         *ppos = pos + count;
4932         rval = count;
4933
4934         return rval;
4935 }
4936
4937
4938 static ssize_t
4939 dhd_debugfs_write(struct file *file, const char __user *ubuf, size_t count, loff_t *ppos)
4940 {
4941         loff_t pos = *ppos;
4942         size_t ret;
4943         uint32 buf;
4944
4945         if (pos < 0)
4946                 return -EINVAL;
4947         if (pos >= g_dbgfs.size || !count)
4948                 return 0;
4949         if (count > g_dbgfs.size - pos)
4950                 count = g_dbgfs.size - pos;
4951
4952         ret = copy_from_user(&buf, ubuf, sizeof(uint32));
4953         if (ret == count)
4954                 return -EFAULT;
4955
4956         /* Basically enforce aligned 4 byte writes. It's up to the user to work out the details */
4957         dhd_writeregl(g_dbgfs.dhdp->bus, file->f_pos & (~3), buf);
4958
4959         return count;
4960 }
4961
4962
4963 loff_t
4964 dhd_debugfs_lseek(struct file *file, loff_t off, int whence)
4965 {
4966         loff_t pos = -1;
4967
4968         switch (whence) {
4969                 case 0:
4970                         pos = off;
4971                         break;
4972                 case 1:
4973                         pos = file->f_pos + off;
4974                         break;
4975                 case 2:
4976                         pos = g_dbgfs.size - off;
4977         }
4978         return (pos < 0 || pos > g_dbgfs.size) ? -EINVAL : (file->f_pos = pos);
4979 }
4980
4981 static const struct file_operations dhd_dbg_state_ops = {
4982         .read   = dhd_dbg_state_read,
4983         .write  = dhd_debugfs_write,
4984         .open   = dhd_dbg_state_open,
4985         .llseek = dhd_debugfs_lseek
4986 };
4987
4988 static void dhd_dbg_create(void)
4989 {
4990         if (g_dbgfs.debugfs_dir) {
4991                 g_dbgfs.debugfs_mem = debugfs_create_file("mem", 0644, g_dbgfs.debugfs_dir,
4992                         NULL, &dhd_dbg_state_ops);
4993         }
4994 }
4995
4996 void dhd_dbg_init(dhd_pub_t *dhdp)
4997 {
4998         int err;
4999
5000         g_dbgfs.dhdp = dhdp;
5001         g_dbgfs.size = 0x20000000; /* Allow access to various cores regs */
5002
5003         g_dbgfs.debugfs_dir = debugfs_create_dir("dhd", 0);
5004         if (IS_ERR(g_dbgfs.debugfs_dir)) {
5005                 err = PTR_ERR(g_dbgfs.debugfs_dir);
5006                 g_dbgfs.debugfs_dir = NULL;
5007                 return;
5008         }
5009
5010         dhd_dbg_create();
5011
5012         return;
5013 }
5014
5015 void dhd_dbg_remove(void)
5016 {
5017         debugfs_remove(g_dbgfs.debugfs_mem);
5018         debugfs_remove(g_dbgfs.debugfs_dir);
5019
5020         bzero((unsigned char *) &g_dbgfs, sizeof(g_dbgfs));
5021
5022 }
5023 #endif /* ifdef BCMDBGFS */
5024
5025 #ifdef WLMEDIA_HTSF
5026
5027 static
5028 void dhd_htsf_addtxts(dhd_pub_t *dhdp, void *pktbuf)
5029 {
5030         dhd_info_t *dhd = (dhd_info_t *)(dhdp->info);
5031         struct sk_buff *skb;
5032         uint32 htsf = 0;
5033         uint16 dport = 0, oldmagic = 0xACAC;
5034         char *p1;
5035         htsfts_t ts;
5036
5037         /*  timestamp packet  */
5038
5039         p1 = (char*) PKTDATA(dhdp->osh, pktbuf);
5040
5041         if (PKTLEN(dhdp->osh, pktbuf) > HTSF_MINLEN) {
5042 /*              memcpy(&proto, p1+26, 4);       */
5043                 memcpy(&dport, p1+40, 2);
5044 /*      proto = ((ntoh32(proto))>> 16) & 0xFF;  */
5045                 dport = ntoh16(dport);
5046         }
5047
5048         /* timestamp only if  icmp or udb iperf with port 5555 */
5049 /*      if (proto == 17 && dport == tsport) { */
5050         if (dport >= tsport && dport <= tsport + 20) {
5051
5052                 skb = (struct sk_buff *) pktbuf;
5053
5054                 htsf = dhd_get_htsf(dhd, 0);
5055                 memset(skb->data + 44, 0, 2); /* clear checksum */
5056                 memcpy(skb->data+82, &oldmagic, 2);
5057                 memcpy(skb->data+84, &htsf, 4);
5058
5059                 memset(&ts, 0, sizeof(htsfts_t));
5060                 ts.magic  = HTSFMAGIC;
5061                 ts.prio   = PKTPRIO(pktbuf);
5062                 ts.seqnum = htsf_seqnum++;
5063                 ts.c10    = get_cycles();
5064                 ts.t10    = htsf;
5065                 ts.endmagic = HTSFENDMAGIC;
5066
5067                 memcpy(skb->data + HTSF_HOSTOFFSET, &ts, sizeof(ts));
5068         }
5069 }
5070
5071 static void dhd_dump_htsfhisto(histo_t *his, char *s)
5072 {
5073         int pktcnt = 0, curval = 0, i;
5074         for (i = 0; i < (NUMBIN-2); i++) {
5075                 curval += 500;
5076                 printf("%d ",  his->bin[i]);
5077                 pktcnt += his->bin[i];
5078         }
5079         printf(" max: %d TotPkt: %d neg: %d [%s]\n", his->bin[NUMBIN-2], pktcnt,
5080                 his->bin[NUMBIN-1], s);
5081 }
5082
5083 static
5084 void sorttobin(int value, histo_t *histo)
5085 {
5086         int i, binval = 0;
5087
5088         if (value < 0) {
5089                 histo->bin[NUMBIN-1]++;
5090                 return;
5091         }
5092         if (value > histo->bin[NUMBIN-2])  /* store the max value  */
5093                 histo->bin[NUMBIN-2] = value;
5094
5095         for (i = 0; i < (NUMBIN-2); i++) {
5096                 binval += 500; /* 500m s bins */
5097                 if (value <= binval) {
5098                         histo->bin[i]++;
5099                         return;
5100                 }
5101         }
5102         histo->bin[NUMBIN-3]++;
5103 }
5104
5105 static
5106 void dhd_htsf_addrxts(dhd_pub_t *dhdp, void *pktbuf)
5107 {
5108         dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
5109         struct sk_buff *skb;
5110         char *p1;
5111         uint16 old_magic;
5112         int d1, d2, d3, end2end;
5113         htsfts_t *htsf_ts;
5114         uint32 htsf;
5115
5116         skb = PKTTONATIVE(dhdp->osh, pktbuf);
5117         p1 = (char*)PKTDATA(dhdp->osh, pktbuf);
5118
5119         if (PKTLEN(osh, pktbuf) > HTSF_MINLEN) {
5120                 memcpy(&old_magic, p1+78, 2);
5121                 htsf_ts = (htsfts_t*) (p1 + HTSF_HOSTOFFSET - 4);
5122         }
5123         else
5124                 return;
5125
5126         if (htsf_ts->magic == HTSFMAGIC) {
5127                 htsf_ts->tE0 = dhd_get_htsf(dhd, 0);
5128                 htsf_ts->cE0 = get_cycles();
5129         }
5130
5131         if (old_magic == 0xACAC) {
5132
5133                 tspktcnt++;
5134                 htsf = dhd_get_htsf(dhd, 0);
5135                 memcpy(skb->data+92, &htsf, sizeof(uint32));
5136
5137                 memcpy(&ts[tsidx].t1, skb->data+80, 16);
5138
5139                 d1 = ts[tsidx].t2 - ts[tsidx].t1;
5140                 d2 = ts[tsidx].t3 - ts[tsidx].t2;
5141                 d3 = ts[tsidx].t4 - ts[tsidx].t3;
5142                 end2end = ts[tsidx].t4 - ts[tsidx].t1;
5143
5144                 sorttobin(d1, &vi_d1);
5145                 sorttobin(d2, &vi_d2);
5146                 sorttobin(d3, &vi_d3);
5147                 sorttobin(end2end, &vi_d4);
5148
5149                 if (end2end > 0 && end2end >  maxdelay) {
5150                         maxdelay = end2end;
5151                         maxdelaypktno = tspktcnt;
5152                         memcpy(&maxdelayts, &ts[tsidx], 16);
5153                 }
5154                 if (++tsidx >= TSMAX)
5155                         tsidx = 0;
5156         }
5157 }
5158
5159 uint32 dhd_get_htsf(dhd_info_t *dhd, int ifidx)
5160 {
5161         uint32 htsf = 0, cur_cycle, delta, delta_us;
5162         uint32    factor, baseval, baseval2;
5163         cycles_t t;
5164
5165         t = get_cycles();
5166         cur_cycle = t;
5167
5168         if (cur_cycle >  dhd->htsf.last_cycle)
5169                 delta = cur_cycle -  dhd->htsf.last_cycle;
5170         else {
5171                 delta = cur_cycle + (0xFFFFFFFF -  dhd->htsf.last_cycle);
5172         }
5173
5174         delta = delta >> 4;
5175
5176         if (dhd->htsf.coef) {
5177                 /* times ten to get the first digit */
5178                 factor = (dhd->htsf.coef*10 + dhd->htsf.coefdec1);
5179                 baseval  = (delta*10)/factor;
5180                 baseval2 = (delta*10)/(factor+1);
5181                 delta_us  = (baseval -  (((baseval - baseval2) * dhd->htsf.coefdec2)) / 10);
5182                 htsf = (delta_us << 4) +  dhd->htsf.last_tsf + HTSF_BUS_DELAY;
5183         }
5184         else {
5185                 DHD_ERROR(("-------dhd->htsf.coef = 0 -------\n"));
5186         }
5187
5188         return htsf;
5189 }
5190
5191 static void dhd_dump_latency(void)
5192 {
5193         int i, max = 0;
5194         int d1, d2, d3, d4, d5;
5195
5196         printf("T1       T2       T3       T4           d1  d2   t4-t1     i    \n");
5197         for (i = 0; i < TSMAX; i++) {
5198                 d1 = ts[i].t2 - ts[i].t1;
5199                 d2 = ts[i].t3 - ts[i].t2;
5200                 d3 = ts[i].t4 - ts[i].t3;
5201                 d4 = ts[i].t4 - ts[i].t1;
5202                 d5 = ts[max].t4-ts[max].t1;
5203                 if (d4 > d5 && d4 > 0)  {
5204                         max = i;
5205                 }
5206                 printf("%08X %08X %08X %08X \t%d %d %d   %d i=%d\n",
5207                         ts[i].t1, ts[i].t2, ts[i].t3, ts[i].t4,
5208                         d1, d2, d3, d4, i);
5209         }
5210
5211         printf("current idx = %d \n", tsidx);
5212
5213         printf("Highest latency %d pkt no.%d total=%d\n", maxdelay, maxdelaypktno, tspktcnt);
5214         printf("%08X %08X %08X %08X \t%d %d %d   %d\n",
5215         maxdelayts.t1, maxdelayts.t2, maxdelayts.t3, maxdelayts.t4,
5216         maxdelayts.t2 - maxdelayts.t1,
5217         maxdelayts.t3 - maxdelayts.t2,
5218         maxdelayts.t4 - maxdelayts.t3,
5219         maxdelayts.t4 - maxdelayts.t1);
5220 }
5221
5222
5223 static int
5224 dhd_ioctl_htsf_get(dhd_info_t *dhd, int ifidx)
5225 {
5226         wl_ioctl_t ioc;
5227         char buf[32];
5228         int ret;
5229         uint32 s1, s2;
5230
5231         struct tsf {
5232                 uint32 low;
5233                 uint32 high;
5234         } tsf_buf;
5235
5236         memset(&ioc, 0, sizeof(ioc));
5237         memset(&tsf_buf, 0, sizeof(tsf_buf));
5238
5239         ioc.cmd = WLC_GET_VAR;
5240         ioc.buf = buf;
5241         ioc.len = (uint)sizeof(buf);
5242         ioc.set = FALSE;
5243
5244         strcpy(buf, "tsf");
5245         s1 = dhd_get_htsf(dhd, 0);
5246         if ((ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len)) < 0) {
5247                 if (ret == -EIO) {
5248                         DHD_ERROR(("%s: tsf is not supported by device\n",
5249                                 dhd_ifname(&dhd->pub, ifidx)));
5250                         return -EOPNOTSUPP;
5251                 }
5252                 return ret;
5253         }
5254         s2 = dhd_get_htsf(dhd, 0);
5255
5256         memcpy(&tsf_buf, buf, sizeof(tsf_buf));
5257         printf(" TSF_h=%04X lo=%08X Calc:htsf=%08X, coef=%d.%d%d delta=%d ",
5258                 tsf_buf.high, tsf_buf.low, s2, dhd->htsf.coef, dhd->htsf.coefdec1,
5259                 dhd->htsf.coefdec2, s2-tsf_buf.low);
5260         printf("lasttsf=%08X lastcycle=%08X\n", dhd->htsf.last_tsf, dhd->htsf.last_cycle);
5261         return 0;
5262 }
5263
5264 void htsf_update(dhd_info_t *dhd, void *data)
5265 {
5266         static ulong  cur_cycle = 0, prev_cycle = 0;
5267         uint32 htsf, tsf_delta = 0;
5268         uint32 hfactor = 0, cyc_delta, dec1 = 0, dec2, dec3, tmp;
5269         ulong b, a;
5270         cycles_t t;
5271
5272         /* cycles_t in inlcude/mips/timex.h */
5273
5274         t = get_cycles();
5275
5276         prev_cycle = cur_cycle;
5277         cur_cycle = t;
5278
5279         if (cur_cycle > prev_cycle)
5280                 cyc_delta = cur_cycle - prev_cycle;
5281         else {
5282                 b = cur_cycle;
5283                 a = prev_cycle;
5284                 cyc_delta = cur_cycle + (0xFFFFFFFF - prev_cycle);
5285         }
5286
5287         if (data == NULL)
5288                 printf(" tsf update ata point er is null \n");
5289
5290         memcpy(&prev_tsf, &cur_tsf, sizeof(tsf_t));
5291         memcpy(&cur_tsf, data, sizeof(tsf_t));
5292
5293         if (cur_tsf.low == 0) {
5294                 DHD_INFO((" ---- 0 TSF, do not update, return\n"));
5295                 return;
5296         }
5297
5298         if (cur_tsf.low > prev_tsf.low)
5299                 tsf_delta = (cur_tsf.low - prev_tsf.low);
5300         else {
5301                 DHD_INFO((" ---- tsf low is smaller cur_tsf= %08X, prev_tsf=%08X, \n",
5302                  cur_tsf.low, prev_tsf.low));
5303                 if (cur_tsf.high > prev_tsf.high) {
5304                         tsf_delta = cur_tsf.low + (0xFFFFFFFF - prev_tsf.low);
5305                         DHD_INFO((" ---- Wrap around tsf coutner  adjusted TSF=%08X\n", tsf_delta));
5306                 }
5307                 else
5308                         return; /* do not update */
5309         }
5310
5311         if (tsf_delta)  {
5312                 hfactor = cyc_delta / tsf_delta;
5313                 tmp  =  (cyc_delta - (hfactor * tsf_delta))*10;
5314                 dec1 =  tmp/tsf_delta;
5315                 dec2 =  ((tmp - dec1*tsf_delta)*10) / tsf_delta;
5316                 tmp  =  (tmp   - (dec1*tsf_delta))*10;
5317                 dec3 =  ((tmp - dec2*tsf_delta)*10) / tsf_delta;
5318
5319                 if (dec3 > 4) {
5320                         if (dec2 == 9) {
5321                                 dec2 = 0;
5322                                 if (dec1 == 9) {
5323                                         dec1 = 0;
5324                                         hfactor++;
5325                                 }
5326                                 else {
5327                                         dec1++;
5328                                 }
5329                         }
5330                         else
5331                                 dec2++;
5332                 }
5333         }
5334
5335         if (hfactor) {
5336                 htsf = ((cyc_delta * 10)  / (hfactor*10+dec1)) + prev_tsf.low;
5337                 dhd->htsf.coef = hfactor;
5338                 dhd->htsf.last_cycle = cur_cycle;
5339                 dhd->htsf.last_tsf = cur_tsf.low;
5340                 dhd->htsf.coefdec1 = dec1;
5341                 dhd->htsf.coefdec2 = dec2;
5342         }
5343         else {
5344                 htsf = prev_tsf.low;
5345         }
5346 }
5347
5348 #endif /* WLMEDIA_HTSF */