2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <linux/sched.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/skbuff.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/random.h>
32 #include <linux/spinlock.h>
33 #include <linux/ethtool.h>
34 #include <asm/uaccess.h>
35 #include <asm/unaligned.h>
36 #if defined(CONFIG_HAS_WAKELOCK)
37 #include <linux/wakelock.h>
38 #endif /* defined (CONFIG_HAS_WAKELOCK) */
39 /* The kernel threading is sdio-specific */
48 /* The level of bus communication with the dongle */
50 DHD_BUS_DOWN, /* Not ready for frame transfers */
51 DHD_BUS_LOAD, /* Download access only (CPU reset) */
52 DHD_BUS_DATA /* Ready for frame transfers */
55 enum dhd_bus_wake_state {
63 WAKE_LOCK_LINK_DOWN_TMOUT,
64 WAKE_LOCK_PNO_FIND_TMOUT,
66 WAKE_LOCK_SOFTAP_STOP,
67 WAKE_LOCK_SOFTAP_START,
70 enum dhd_prealloc_index {
71 DHD_PREALLOC_PROT = 0,
77 /* Common structure for module and instance linkage */
78 typedef struct dhd_pub {
80 osl_t *osh; /* OSL handle */
81 struct dhd_bus *bus; /* Bus module handle */
82 struct dhd_prot *prot; /* Protocol module handle */
83 struct dhd_info *info; /* Info module handle */
85 /* Internal dhd items */
86 bool up; /* Driver up/down (to OS) */
87 bool txoff; /* Transmit flow-controlled */
88 bool dongle_reset; /* true = DEVRESET put dongle into reset */
89 enum dhd_bus_state busstate;
90 uint hdrlen; /* Total DHD header length (proto + bus) */
91 uint maxctl; /* Max size rxctl request from proto to bus */
92 uint rxsz; /* Rx buffer size bus module should use */
93 u8 wme_dp; /* wme discard priority */
95 /* Dongle media info */
96 bool iswl; /* Dongle-resident driver is wl */
97 unsigned long drv_version; /* Version of dongle-resident driver */
98 struct ether_addr mac; /* MAC address obtained from dongle */
99 dngl_stats_t dstats; /* Stats for dongle-based data */
101 /* Additional stats for the bus level */
102 unsigned long tx_packets; /* Data packets sent to dongle */
103 unsigned long tx_multicast; /* Multicast data packets sent to dongle */
104 unsigned long tx_errors; /* Errors in sending data to dongle */
105 unsigned long tx_ctlpkts; /* Control packets sent to dongle */
106 unsigned long tx_ctlerrs; /* Errors sending control frames to dongle */
107 unsigned long rx_packets; /* Packets sent up the network interface */
108 unsigned long rx_multicast; /* Multicast packets sent up the network
110 unsigned long rx_errors; /* Errors processing rx data packets */
111 unsigned long rx_ctlpkts; /* Control frames processed from dongle */
112 unsigned long rx_ctlerrs; /* Errors in processing rx control frames */
113 unsigned long rx_dropped; /* Packets dropped locally (no memory) */
114 unsigned long rx_flushed; /* Packets flushed due to
115 unscheduled sendup thread */
116 unsigned long wd_dpc_sched; /* Number of times dhd dpc scheduled by
119 unsigned long rx_readahead_cnt; /* Number of packets where header read-ahead
121 unsigned long tx_realloc; /* Number of tx packets we had to realloc for
123 unsigned long fc_packets; /* Number of flow control pkts recvd */
125 /* Last error return */
129 /* Last error from dongle */
132 /* Suspend disable flag flag */
133 int suspend_disable_flag; /* "1" to disable all extra powersaving
135 int in_suspend; /* flag set to 1 when early suspend called */
137 int pno_enable; /* pno status : "1" is pno enable */
138 #endif /* PNO_SUPPORT */
139 int dtim_skip; /* dtim skip , default 0 means wake each dtim */
141 /* Pkt filter defination */
142 char *pktfilter[100];
145 u8 country_code[WLC_CNTRY_BUF_SZ];
146 char eventmask[WL_EVENTING_MASK_LEN];
148 #if defined(CONFIG_HAS_WAKELOCK)
149 struct wake_lock wakelock[WAKE_LOCK_MAX];
150 #endif /* defined (CONFIG_HAS_WAKELOCK) */
153 #if defined(CONFIG_PM_SLEEP)
155 #define DHD_PM_RESUME_WAIT_INIT(a) DECLARE_WAIT_QUEUE_HEAD(a);
156 #define _DHD_PM_RESUME_WAIT(a, b) do {\
158 while (dhd_mmc_suspend && retry++ != b) { \
159 wait_event_timeout(a, false, HZ/100); \
162 #define DHD_PM_RESUME_WAIT(a) _DHD_PM_RESUME_WAIT(a, 30)
163 #define DHD_PM_RESUME_WAIT_FOREVER(a) _DHD_PM_RESUME_WAIT(a, ~0)
164 #define DHD_PM_RESUME_RETURN_ERROR(a) \
165 do { if (dhd_mmc_suspend) return a; } while (0)
166 #define DHD_PM_RESUME_RETURN do { if (dhd_mmc_suspend) return; } while (0)
168 #define DHD_SPINWAIT_SLEEP_INIT(a) DECLARE_WAIT_QUEUE_HEAD(a);
169 #define SPINWAIT_SLEEP(a, exp, us) do { \
170 uint countdown = (us) + 9999; \
171 while ((exp) && (countdown >= 10000)) { \
172 wait_event_timeout(a, false, HZ/100); \
173 countdown -= 10000; \
179 #define DHD_PM_RESUME_WAIT_INIT(a)
180 #define DHD_PM_RESUME_WAIT(a)
181 #define DHD_PM_RESUME_WAIT_FOREVER(a)
182 #define DHD_PM_RESUME_RETURN_ERROR(a)
183 #define DHD_PM_RESUME_RETURN
185 #define DHD_SPINWAIT_SLEEP_INIT(a)
186 #define SPINWAIT_SLEEP(a, exp, us) do { \
187 uint countdown = (us) + 9; \
188 while ((exp) && (countdown >= 10)) { \
194 #endif /* defined(CONFIG_PM_SLEEP) */
195 #define DHD_IF_VIF 0x01 /* Virtual IF (Hidden from user) */
197 static inline void MUTEX_LOCK_INIT(dhd_pub_t *dhdp)
201 static inline void MUTEX_LOCK(dhd_pub_t *dhdp)
205 static inline void MUTEX_UNLOCK(dhd_pub_t *dhdp)
209 static inline void MUTEX_LOCK_SOFTAP_SET_INIT(dhd_pub_t *dhdp)
213 static inline void MUTEX_LOCK_SOFTAP_SET(dhd_pub_t *dhdp)
217 static inline void MUTEX_UNLOCK_SOFTAP_SET(dhd_pub_t *dhdp)
221 static inline void MUTEX_LOCK_WL_SCAN_SET_INIT(void)
225 static inline void MUTEX_LOCK_WL_SCAN_SET(void)
229 static inline void MUTEX_UNLOCK_WL_SCAN_SET(void)
233 static inline void WAKE_LOCK_INIT(dhd_pub_t *dhdp, int index, char *y)
235 #if defined(CONFIG_HAS_WAKELOCK)
236 wake_lock_init(&dhdp->wakelock[index], WAKE_LOCK_SUSPEND, y);
237 #endif /* defined (CONFIG_HAS_WAKELOCK) */
240 static inline void WAKE_LOCK(dhd_pub_t *dhdp, int index)
242 #if defined(CONFIG_HAS_WAKELOCK)
243 wake_lock(&dhdp->wakelock[index]);
244 #endif /* defined (CONFIG_HAS_WAKELOCK) */
247 static inline void WAKE_UNLOCK(dhd_pub_t *dhdp, int index)
249 #if defined(CONFIG_HAS_WAKELOCK)
250 wake_unlock(&dhdp->wakelock[index]);
251 #endif /* defined (CONFIG_HAS_WAKELOCK) */
254 static inline void WAKE_LOCK_TIMEOUT(dhd_pub_t *dhdp, int index, long time)
256 #if defined(CONFIG_HAS_WAKELOCK)
257 wake_lock_timeout(&dhdp->wakelock[index], time);
258 #endif /* defined (CONFIG_HAS_WAKELOCK) */
261 static inline void WAKE_LOCK_DESTROY(dhd_pub_t *dhdp, int index)
263 #if defined(CONFIG_HAS_WAKELOCK)
264 wake_lock_destroy(&dhdp->wakelock[index]);
265 #endif /* defined (CONFIG_HAS_WAKELOCK) */
268 typedef struct dhd_if_event {
276 * Exported from dhd OS modules (dhd_linux/dhd_ndis)
279 /* To allow osl_attach/detach calls from os-independent modules */
280 osl_t *dhd_osl_attach(void *pdev, uint bustype);
281 void dhd_osl_detach(osl_t *osh);
283 /* Indication from bus module regarding presence/insertion of dongle.
284 * Return dhd_pub_t pointer, used as handle to OS module in later calls.
285 * Returned structure should have bus and prot pointers filled in.
286 * bus_hdrlen specifies required headroom for bus module header.
288 extern dhd_pub_t *dhd_attach(osl_t *osh, struct dhd_bus *bus, uint bus_hdrlen);
289 extern int dhd_net_attach(dhd_pub_t *dhdp, int idx);
291 /* Indication from bus module regarding removal/absence of dongle */
292 extern void dhd_detach(dhd_pub_t *dhdp);
294 /* Indication from bus module to change flow-control state */
295 extern void dhd_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool on);
297 extern bool dhd_prec_enq(dhd_pub_t *dhdp, struct pktq *q, void *pkt, int prec);
299 /* Receive frame for delivery to OS. Callee disposes of rxp. */
300 extern void dhd_rx_frame(dhd_pub_t *dhdp, int ifidx, void *rxp, int numpkt);
302 /* Return pointer to interface name */
303 extern char *dhd_ifname(dhd_pub_t *dhdp, int idx);
305 /* Request scheduling of the bus dpc */
306 extern void dhd_sched_dpc(dhd_pub_t *dhdp);
308 /* Notify tx completion */
309 extern void dhd_txcomplete(dhd_pub_t *dhdp, void *txp, bool success);
312 extern int dhdcdc_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
315 /* OS independent layer functions */
316 extern int dhd_os_proto_block(dhd_pub_t *pub);
317 extern int dhd_os_proto_unblock(dhd_pub_t *pub);
318 extern int dhd_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition,
320 extern int dhd_os_ioctl_resp_wake(dhd_pub_t *pub);
321 extern unsigned int dhd_os_get_ioctl_resp_timeout(void);
322 extern void dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec);
323 extern void *dhd_os_open_image(char *filename);
324 extern int dhd_os_get_image_block(char *buf, int len, void *image);
325 extern void dhd_os_close_image(void *image);
326 extern void dhd_os_wd_timer(void *bus, uint wdtick);
327 extern void dhd_os_sdlock(dhd_pub_t *pub);
328 extern void dhd_os_sdunlock(dhd_pub_t *pub);
329 extern void dhd_os_sdlock_txq(dhd_pub_t *pub);
330 extern void dhd_os_sdunlock_txq(dhd_pub_t *pub);
331 extern void dhd_os_sdlock_rxq(dhd_pub_t *pub);
332 extern void dhd_os_sdunlock_rxq(dhd_pub_t *pub);
333 extern void dhd_os_sdlock_sndup_rxq(dhd_pub_t *pub);
334 extern void dhd_customer_gpio_wlan_ctrl(int onoff);
335 extern int dhd_custom_get_mac_address(unsigned char *buf);
336 extern void dhd_os_sdunlock_sndup_rxq(dhd_pub_t *pub);
337 extern void dhd_os_sdlock_eventq(dhd_pub_t *pub);
338 extern void dhd_os_sdunlock_eventq(dhd_pub_t *pub);
340 extern int write_to_file(dhd_pub_t *dhd, u8 *buf, int size);
341 #endif /* DHD_DEBUG */
342 #if defined(OOB_INTR_ONLY)
343 extern int dhd_customer_oob_irq_map(unsigned long *irq_flags_ptr);
344 #endif /* defined(OOB_INTR_ONLY) */
345 extern void dhd_os_sdtxlock(dhd_pub_t *pub);
346 extern void dhd_os_sdtxunlock(dhd_pub_t *pub);
348 int setScheduler(struct task_struct *p, int policy, struct sched_param *param);
351 u32 limit; /* Expiration time (usec) */
352 u32 increment; /* Current expiration increment (usec) */
353 u32 elapsed; /* Current elapsed time (usec) */
354 u32 tick; /* O/S tick time (usec) */
357 extern void dhd_timeout_start(dhd_timeout_t *tmo, uint usec);
358 extern int dhd_timeout_expired(dhd_timeout_t *tmo);
360 extern int dhd_ifname2idx(struct dhd_info *dhd, char *name);
361 extern u8 *dhd_bssidx2bssid(dhd_pub_t *dhd, int idx);
362 extern int wl_host_event(struct dhd_info *dhd, int *idx, void *pktdata,
363 wl_event_msg_t *, void **data_ptr);
364 extern void wl_event_to_host_order(wl_event_msg_t *evt);
366 extern void dhd_common_init(void);
368 extern int dhd_add_if(struct dhd_info *dhd, int ifidx, void *handle,
369 char *name, u8 *mac_addr, u32 flags, u8 bssidx);
370 extern void dhd_del_if(struct dhd_info *dhd, int ifidx);
372 extern void dhd_vif_add(struct dhd_info *dhd, int ifidx, char *name);
373 extern void dhd_vif_del(struct dhd_info *dhd, int ifidx);
375 extern void dhd_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx);
376 extern void dhd_vif_sendup(struct dhd_info *dhd, int ifidx, unsigned char * cp,
379 /* Send packet to dongle via data channel */
380 extern int dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, void *pkt);
382 /* Send event to host */
383 extern void dhd_sendup_event(dhd_pub_t *dhdp, wl_event_msg_t *event,
385 extern int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag);
386 extern uint dhd_bus_status(dhd_pub_t *dhdp);
387 extern int dhd_bus_start(dhd_pub_t *dhdp);
389 extern void print_buf(void *pbuf, int len, int bytes_per_line);
391 typedef enum cust_gpio_modes {
398 * Insmod parameters for debug/test
401 /* Watchdog timer interval */
402 extern uint dhd_watchdog_ms;
404 #if defined(DHD_DEBUG)
405 /* Console output poll interval */
406 extern uint dhd_console_ms;
407 #endif /* defined(DHD_DEBUG) */
410 extern uint dhd_intr;
413 extern uint dhd_poll;
415 /* ARP offload agent mode */
416 extern uint dhd_arp_mode;
418 /* ARP offload enable */
419 extern uint dhd_arp_enable;
421 /* Pkt filte enable control */
422 extern uint dhd_pkt_filter_enable;
424 /* Pkt filter init setup */
425 extern uint dhd_pkt_filter_init;
427 /* Pkt filter mode control */
428 extern uint dhd_master_mode;
430 /* Roaming mode control */
431 extern uint dhd_roam;
433 /* Roaming mode control */
434 extern uint dhd_radio_up;
436 /* Initial idletime ticks (may be -1 for immediate idle, 0 for no idle) */
437 extern int dhd_idletime;
438 #define DHD_IDLETIME_TICKS 1
440 /* SDIO Drive Strength */
441 extern uint dhd_sdiod_drive_strength;
443 /* Override to force tx queueing all the time */
444 extern uint dhd_force_tx_queueing;
447 /* Echo packet generator (SDIO), pkts/s */
448 extern uint dhd_pktgen;
450 /* Echo packet len (0 => sawtooth, max 1800) */
451 extern uint dhd_pktgen_len;
452 #define MAX_PKTGEN_LEN 1800
455 /* optionally set by a module_param_string() */
456 #define MOD_PARAM_PATHLEN 2048
457 extern char fw_path[MOD_PARAM_PATHLEN];
458 extern char nv_path[MOD_PARAM_PATHLEN];
460 /* For supporting multiple interfaces */
461 #define DHD_MAX_IFS 16
462 #define DHD_DEL_IF -0xe
463 #define DHD_BAD_IF -0xf
465 extern void dhd_wait_for_event(dhd_pub_t *dhd, bool * lockvar);
466 extern void dhd_wait_event_wakeup(dhd_pub_t *dhd);