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