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 struct osl_info *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 struct osl_info *dhd_osl_attach(void *pdev, uint bustype);
281 void dhd_osl_detach(struct osl_info *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(struct osl_info *osh, struct dhd_bus *bus,
290 extern int dhd_net_attach(dhd_pub_t *dhdp, int idx);
292 /* Indication from bus module regarding removal/absence of dongle */
293 extern void dhd_detach(dhd_pub_t *dhdp);
295 /* Indication from bus module to change flow-control state */
296 extern void dhd_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool on);
298 extern bool dhd_prec_enq(dhd_pub_t *dhdp, struct pktq *q,
299 struct sk_buff *pkt, int prec);
301 /* Receive frame for delivery to OS. Callee disposes of rxp. */
302 extern void dhd_rx_frame(dhd_pub_t *dhdp, int ifidx,
303 struct sk_buff *rxp, int numpkt);
305 /* Return pointer to interface name */
306 extern char *dhd_ifname(dhd_pub_t *dhdp, int idx);
308 /* Request scheduling of the bus dpc */
309 extern void dhd_sched_dpc(dhd_pub_t *dhdp);
311 /* Notify tx completion */
312 extern void dhd_txcomplete(dhd_pub_t *dhdp, struct sk_buff *txp, bool success);
315 extern int dhdcdc_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
318 /* OS independent layer functions */
319 extern int dhd_os_proto_block(dhd_pub_t *pub);
320 extern int dhd_os_proto_unblock(dhd_pub_t *pub);
321 extern int dhd_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition,
323 extern int dhd_os_ioctl_resp_wake(dhd_pub_t *pub);
324 extern unsigned int dhd_os_get_ioctl_resp_timeout(void);
325 extern void dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec);
326 extern void *dhd_os_open_image(char *filename);
327 extern int dhd_os_get_image_block(char *buf, int len, void *image);
328 extern void dhd_os_close_image(void *image);
329 extern void dhd_os_wd_timer(void *bus, uint wdtick);
330 extern void dhd_os_sdlock(dhd_pub_t *pub);
331 extern void dhd_os_sdunlock(dhd_pub_t *pub);
332 extern void dhd_os_sdlock_txq(dhd_pub_t *pub);
333 extern void dhd_os_sdunlock_txq(dhd_pub_t *pub);
334 extern void dhd_os_sdlock_rxq(dhd_pub_t *pub);
335 extern void dhd_os_sdunlock_rxq(dhd_pub_t *pub);
336 extern void dhd_os_sdlock_sndup_rxq(dhd_pub_t *pub);
337 extern void dhd_customer_gpio_wlan_ctrl(int onoff);
338 extern int dhd_custom_get_mac_address(unsigned char *buf);
339 extern void dhd_os_sdunlock_sndup_rxq(dhd_pub_t *pub);
340 extern void dhd_os_sdlock_eventq(dhd_pub_t *pub);
341 extern void dhd_os_sdunlock_eventq(dhd_pub_t *pub);
343 extern int write_to_file(dhd_pub_t *dhd, u8 *buf, int size);
344 #endif /* DHD_DEBUG */
345 #if defined(OOB_INTR_ONLY)
346 extern int dhd_customer_oob_irq_map(unsigned long *irq_flags_ptr);
347 #endif /* defined(OOB_INTR_ONLY) */
348 extern void dhd_os_sdtxlock(dhd_pub_t *pub);
349 extern void dhd_os_sdtxunlock(dhd_pub_t *pub);
351 int setScheduler(struct task_struct *p, int policy, struct sched_param *param);
354 u32 limit; /* Expiration time (usec) */
355 u32 increment; /* Current expiration increment (usec) */
356 u32 elapsed; /* Current elapsed time (usec) */
357 u32 tick; /* O/S tick time (usec) */
360 extern void dhd_timeout_start(dhd_timeout_t *tmo, uint usec);
361 extern int dhd_timeout_expired(dhd_timeout_t *tmo);
363 extern int dhd_ifname2idx(struct dhd_info *dhd, char *name);
364 extern u8 *dhd_bssidx2bssid(dhd_pub_t *dhd, int idx);
365 extern int wl_host_event(struct dhd_info *dhd, int *idx, void *pktdata,
366 wl_event_msg_t *, void **data_ptr);
367 extern void wl_event_to_host_order(wl_event_msg_t *evt);
369 extern void dhd_common_init(void);
371 extern int dhd_add_if(struct dhd_info *dhd, int ifidx, void *handle,
372 char *name, u8 *mac_addr, u32 flags, u8 bssidx);
373 extern void dhd_del_if(struct dhd_info *dhd, int ifidx);
375 extern void dhd_vif_add(struct dhd_info *dhd, int ifidx, char *name);
376 extern void dhd_vif_del(struct dhd_info *dhd, int ifidx);
378 extern void dhd_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx);
379 extern void dhd_vif_sendup(struct dhd_info *dhd, int ifidx, unsigned char * cp,
382 /* Send packet to dongle via data channel */
383 extern int dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, struct sk_buff *pkt);
385 /* Send event to host */
386 extern void dhd_sendup_event(dhd_pub_t *dhdp, wl_event_msg_t *event,
388 extern int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag);
389 extern uint dhd_bus_status(dhd_pub_t *dhdp);
390 extern int dhd_bus_start(dhd_pub_t *dhdp);
392 extern void print_buf(void *pbuf, int len, int bytes_per_line);
394 typedef enum cust_gpio_modes {
401 * Insmod parameters for debug/test
404 /* Watchdog timer interval */
405 extern uint dhd_watchdog_ms;
407 #if defined(DHD_DEBUG)
408 /* Console output poll interval */
409 extern uint dhd_console_ms;
410 #endif /* defined(DHD_DEBUG) */
413 extern uint dhd_intr;
416 extern uint dhd_poll;
418 /* ARP offload agent mode */
419 extern uint dhd_arp_mode;
421 /* ARP offload enable */
422 extern uint dhd_arp_enable;
424 /* Pkt filte enable control */
425 extern uint dhd_pkt_filter_enable;
427 /* Pkt filter init setup */
428 extern uint dhd_pkt_filter_init;
430 /* Pkt filter mode control */
431 extern uint dhd_master_mode;
433 /* Roaming mode control */
434 extern uint dhd_roam;
436 /* Roaming mode control */
437 extern uint dhd_radio_up;
439 /* Initial idletime ticks (may be -1 for immediate idle, 0 for no idle) */
440 extern int dhd_idletime;
441 #define DHD_IDLETIME_TICKS 1
443 /* SDIO Drive Strength */
444 extern uint dhd_sdiod_drive_strength;
446 /* Override to force tx queueing all the time */
447 extern uint dhd_force_tx_queueing;
450 /* Echo packet generator (SDIO), pkts/s */
451 extern uint dhd_pktgen;
453 /* Echo packet len (0 => sawtooth, max 1800) */
454 extern uint dhd_pktgen_len;
455 #define MAX_PKTGEN_LEN 1800
458 /* optionally set by a module_param_string() */
459 #define MOD_PARAM_PATHLEN 2048
460 extern char fw_path[MOD_PARAM_PATHLEN];
461 extern char nv_path[MOD_PARAM_PATHLEN];
463 /* For supporting multiple interfaces */
464 #define DHD_MAX_IFS 16
465 #define DHD_DEL_IF -0xe
466 #define DHD_BAD_IF -0xf
468 extern void dhd_wait_for_event(dhd_pub_t *dhd, bool * lockvar);
469 extern void dhd_wait_event_wakeup(dhd_pub_t *dhd);