1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef _wl_cfg80211_h_
18 #define _wl_cfg80211_h_
19 
20 #include <linux/wireless.h>
21 #include <linux/wireless.h>
22 #include <net/cfg80211.h>
23 #include <wlioctl.h>
24 
25 struct wl_conf;
26 struct wl_iface;
27 struct wl_priv;
28 struct wl_security;
29 struct wl_ibss;
30 
31 #define WL_DBG_NONE	0
32 #define WL_DBG_DBG 	(1 << 2)
33 #define WL_DBG_INFO	(1 << 1)
34 #define WL_DBG_ERR	(1 << 0)
35 #define WL_DBG_MASK ((WL_DBG_DBG | WL_DBG_INFO | WL_DBG_ERR) << 1)
36 
37 #define WL_DBG_LEVEL 1		/* 0 invalidates all debug messages.
38 				 default is 1 */
39 #define	WL_ERR(fmt, args...)					\
40 do {								\
41 	if (wl_dbg_level & WL_DBG_ERR) {			\
42 		if (net_ratelimit()) {				\
43 			printk(KERN_ERR "ERROR @%s : " fmt,	\
44 			       __func__, ##args);		\
45 		}						\
46 	}							\
47 } while (0)
48 
49 #define	WL_INFO(fmt, args...)					\
50 do {								\
51 	if (wl_dbg_level & WL_DBG_INFO) {			\
52 		if (net_ratelimit()) {				\
53 			printk(KERN_ERR "INFO @%s : " fmt,	\
54 			       __func__, ##args);		\
55 		}						\
56 	}							\
57 } while (0)
58 
59 #if (WL_DBG_LEVEL > 0)
60 #define	WL_DBG(fmt, args...)					\
61 do {								\
62 	if (wl_dbg_level & WL_DBG_DBG) {			\
63 		printk(KERN_ERR "DEBUG @%s :" fmt,		\
64 		       __func__, ##args);			\
65 	}							\
66 } while (0)
67 #else				/* !(WL_DBG_LEVEL > 0) */
68 #define	WL_DBG(fmt, args...) noprintk(fmt, ##args)
69 #endif				/* (WL_DBG_LEVEL > 0) */
70 
71 #define WL_SCAN_RETRY_MAX	3	/* used for ibss scan */
72 #define WL_NUM_SCAN_MAX		1
73 #define WL_NUM_PMKIDS_MAX	MAXPMKID	/* will be used
74 						 * for 2.6.33 kernel
75 						 * or later
76 						 */
77 #define WL_SCAN_BUF_MAX 		(1024 * 8)
78 #define WL_TLV_INFO_MAX 		1024
79 #define WL_BSS_INFO_MAX			2048
80 #define WL_ASSOC_INFO_MAX	512	/*
81 				 * needs to grab assoc info from dongle to
82 				 * report it to cfg80211 through "connect"
83 				 * event
84 				 */
85 #define WL_IOCTL_LEN_MAX	1024
86 #define WL_EXTRA_BUF_MAX	2048
87 #define WL_ISCAN_BUF_MAX	2048	/*
88 				 * the buf lengh can be WLC_IOCTL_MAXLEN (8K)
89 				 * to reduce iteration
90 				 */
91 #define WL_ISCAN_TIMER_INTERVAL_MS	3000
92 #define WL_SCAN_ERSULTS_LAST 	(WL_SCAN_RESULTS_NO_MEM+1)
93 #define WL_AP_MAX	256	/* virtually unlimitted as long
94 				 * as kernel memory allows
95 				 */
96 #define WL_FILE_NAME_MAX		256
97 
98 /* dongle status */
99 enum wl_status {
100 	WL_STATUS_READY,
101 	WL_STATUS_SCANNING,
102 	WL_STATUS_SCAN_ABORTING,
103 	WL_STATUS_CONNECTING,
104 	WL_STATUS_CONNECTED
105 };
106 
107 /* wi-fi mode */
108 enum wl_mode {
109 	WL_MODE_BSS,
110 	WL_MODE_IBSS,
111 	WL_MODE_AP
112 };
113 
114 /* dongle profile list */
115 enum wl_prof_list {
116 	WL_PROF_MODE,
117 	WL_PROF_SSID,
118 	WL_PROF_SEC,
119 	WL_PROF_IBSS,
120 	WL_PROF_BAND,
121 	WL_PROF_BSSID,
122 	WL_PROF_ACT,
123 	WL_PROF_BEACONINT,
124 	WL_PROF_DTIMPERIOD
125 };
126 
127 /* dongle iscan state */
128 enum wl_iscan_state {
129 	WL_ISCAN_STATE_IDLE,
130 	WL_ISCAN_STATE_SCANING
131 };
132 
133 /* fw downloading status */
134 enum wl_fw_status {
135 	WL_FW_LOADING_DONE,
136 	WL_NVRAM_LOADING_DONE
137 };
138 
139 /* beacon / probe_response */
140 struct beacon_proberesp {
141 	__le64 timestamp;
142 	__le16 beacon_int;
143 	__le16 capab_info;
144 	u8 variable[0];
145 } __attribute__ ((packed));
146 
147 /* dongle configuration */
148 struct wl_conf {
149 	u32 mode;		/* adhoc , infrastructure or ap */
150 	u32 frag_threshold;
151 	u32 rts_threshold;
152 	u32 retry_short;
153 	u32 retry_long;
154 	s32 tx_power;
155 	struct ieee80211_channel channel;
156 };
157 
158 /* cfg80211 main event loop */
159 struct wl_event_loop {
160 	s32(*handler[WLC_E_LAST]) (struct wl_priv *wl,
161 				     struct net_device *ndev,
162 				     const wl_event_msg_t *e, void *data);
163 };
164 
165 /* representing interface of cfg80211 plane */
166 struct wl_iface {
167 	struct wl_priv *wl;
168 };
169 
170 struct wl_dev {
171 	void *driver_data;	/* to store cfg80211 object information */
172 };
173 
174 /* bss inform structure for cfg80211 interface */
175 struct wl_cfg80211_bss_info {
176 	u16 band;
177 	u16 channel;
178 	s16 rssi;
179 	u16 frame_len;
180 	u8 frame_buf[1];
181 };
182 
183 /* basic structure of scan request */
184 struct wl_scan_req {
185 	struct wlc_ssid ssid;
186 };
187 
188 /* basic structure of information element */
189 struct wl_ie {
190 	u16 offset;
191 	u8 buf[WL_TLV_INFO_MAX];
192 };
193 
194 /* event queue for cfg80211 main event */
195 struct wl_event_q {
196 	struct list_head eq_list;
197 	u32 etype;
198 	wl_event_msg_t emsg;
199 	s8 edata[1];
200 };
201 
202 /* security information with currently associated ap */
203 struct wl_security {
204 	u32 wpa_versions;
205 	u32 auth_type;
206 	u32 cipher_pairwise;
207 	u32 cipher_group;
208 	u32 wpa_auth;
209 };
210 
211 /* ibss information for currently joined ibss network */
212 struct wl_ibss {
213 	u8 beacon_interval;	/* in millisecond */
214 	u8 atim;		/* in millisecond */
215 	s8 join_only;
216 	u8 band;
217 	u8 channel;
218 };
219 
220 /* dongle profile */
221 struct wl_profile {
222 	u32 mode;
223 	struct wlc_ssid ssid;
224 	u8 bssid[ETH_ALEN];
225 	u16 beacon_interval;
226 	u8 dtim_period;
227 	struct wl_security sec;
228 	struct wl_ibss ibss;
229 	s32 band;
230 	bool active;
231 };
232 
233 /* dongle iscan event loop */
234 struct wl_iscan_eloop {
235 	s32(*handler[WL_SCAN_ERSULTS_LAST]) (struct wl_priv *wl);
236 };
237 
238 /* dongle iscan controller */
239 struct wl_iscan_ctrl {
240 	struct net_device *dev;
241 	struct timer_list timer;
242 	u32 timer_ms;
243 	u32 timer_on;
244 	s32 state;
245 	struct task_struct *tsk;
246 	struct semaphore sync;
247 	struct wl_iscan_eloop el;
248 	void *data;
249 	s8 ioctl_buf[WLC_IOCTL_SMLEN];
250 	s8 scan_buf[WL_ISCAN_BUF_MAX];
251 };
252 
253 /* association inform */
254 struct wl_connect_info {
255 	u8 *req_ie;
256 	s32 req_ie_len;
257 	u8 *resp_ie;
258 	s32 resp_ie_len;
259 };
260 
261 /* firmware /nvram downloading controller */
262 struct wl_fw_ctrl {
263 	const struct firmware *fw_entry;
264 	unsigned long status;
265 	u32 ptr;
266 	s8 fw_name[WL_FILE_NAME_MAX];
267 	s8 nvram_name[WL_FILE_NAME_MAX];
268 };
269 
270 /* assoc ie length */
271 struct wl_assoc_ielen {
272 	u32 req_len;
273 	u32 resp_len;
274 };
275 
276 /* wpa2 pmk list */
277 struct wl_pmk_list {
278 	pmkid_list_t pmkids;
279 	pmkid_t foo[MAXPMKID - 1];
280 };
281 
282 /* dongle private data of cfg80211 interface */
283 struct wl_priv {
284 	struct wireless_dev *wdev;	/* representing wl cfg80211 device */
285 	struct wl_conf *conf;	/* dongle configuration */
286 	struct cfg80211_scan_request *scan_request;	/* scan request
287 							 object */
288 	struct wl_event_loop el;	/* main event loop */
289 	struct list_head eq_list;	/* used for event queue */
290 	spinlock_t eq_lock;	/* for event queue synchronization */
291 	struct mutex usr_sync;	/* maily for dongle up/down synchronization */
292 	struct wl_scan_results *bss_list;	/* bss_list holding scanned
293 						 ap information */
294 	struct wl_scan_results *scan_results;
295 	struct wl_scan_req *scan_req_int;	/* scan request object for
296 						 internal purpose */
297 	struct wl_cfg80211_bss_info *bss_info;	/* bss information for
298 						 cfg80211 layer */
299 	struct wl_ie ie;	/* information element object for
300 					 internal purpose */
301 	u8 bssid[ETH_ALEN];	/* bssid of currently engaged network */
302 	struct semaphore event_sync;	/* for synchronization of main event
303 					 thread */
304 	struct wl_profile *profile;	/* holding dongle profile */
305 	struct wl_iscan_ctrl *iscan;	/* iscan controller */
306 	struct wl_connect_info conn_info;	/* association information
307 						 container */
308 	struct wl_fw_ctrl *fw;	/* control firwmare / nvram paramter
309 				 downloading */
310 	struct wl_pmk_list *pmk_list;	/* wpa2 pmk list */
311 	struct task_struct *event_tsk;	/* task of main event handler thread */
312 	unsigned long status;		/* current dongle status */
313 	void *pub;
314 	u32 channel;		/* current channel */
315 	bool iscan_on;		/* iscan on/off switch */
316 	bool iscan_kickstart;	/* indicate iscan already started */
317 	bool active_scan;	/* current scan mode */
318 	bool ibss_starter;	/* indicates this sta is ibss starter */
319 	bool link_up;		/* link/connection up flag */
320 	bool pwr_save;		/* indicate whether dongle to support
321 					 power save mode */
322 	bool dongle_up;		/* indicate whether dongle up or not */
323 	bool roam_on;		/* on/off switch for dongle self-roaming */
324 	bool scan_tried;	/* indicates if first scan attempted */
325 	u8 *ioctl_buf;	/* ioctl buffer */
326 	u8 *extra_buf;	/* maily to grab assoc information */
327 	struct dentry *debugfsdir;
328 	u8 ci[0] __attribute__ ((__aligned__(NETDEV_ALIGN)));
329 };
330 
331 #define wl_to_dev(w) (wiphy_dev(wl->wdev->wiphy))
332 #define wl_to_wiphy(w) (w->wdev->wiphy)
333 #define wiphy_to_wl(w) ((struct wl_priv *)(wiphy_priv(w)))
334 #define wl_to_wdev(w) (w->wdev)
335 #define wdev_to_wl(w) ((struct wl_priv *)(wdev_priv(w)))
336 #define wl_to_ndev(w) (w->wdev->netdev)
337 #define ndev_to_wl(n) (wdev_to_wl(n->ieee80211_ptr))
338 #define ci_to_wl(c) (ci->wl)
339 #define wl_to_ci(w) (&w->ci)
340 #define wl_to_sr(w) (w->scan_req_int)
341 #define wl_to_ie(w) (&w->ie)
342 #define iscan_to_wl(i) ((struct wl_priv *)(i->data))
343 #define wl_to_iscan(w) (w->iscan)
344 #define wl_to_conn(w) (&w->conn_info)
345 
next_bss(struct wl_scan_results * list,struct wl_bss_info * bss)346 static inline struct wl_bss_info *next_bss(struct wl_scan_results *list,
347 					   struct wl_bss_info *bss)
348 {
349 	return bss = bss ?
350 		(struct wl_bss_info *)((unsigned long)bss +
351 				       le32_to_cpu(bss->length)) :
352 		list->bss_info;
353 }
354 
355 #define for_each_bss(list, bss, __i)	\
356 	for (__i = 0; __i < list->count && __i < WL_AP_MAX; __i++, bss = next_bss(list, bss))
357 
358 extern s32 wl_cfg80211_attach(struct net_device *ndev, void *data);
359 extern void wl_cfg80211_detach(void);
360 /* event handler from dongle */
361 extern void wl_cfg80211_event(struct net_device *ndev, const wl_event_msg_t *e,
362 			      void *data);
363 extern void wl_cfg80211_sdio_func(void *func);	/* set sdio function info */
364 extern struct sdio_func *wl_cfg80211_get_sdio_func(void);	/* set sdio function info */
365 extern s32 wl_cfg80211_up(void);	/* dongle up */
366 extern s32 wl_cfg80211_down(void);	/* dongle down */
367 extern void wl_cfg80211_dbg_level(u32 level);	/* set dongle
368 							 debugging level */
369 extern void *wl_cfg80211_request_fw(s8 *file_name);	/* request fw /nvram
370 							 downloading */
371 extern s32 wl_cfg80211_read_fw(s8 *buf, u32 size);	/* read fw
372 								 image */
373 extern void wl_cfg80211_release_fw(void);	/* release fw */
374 extern s8 *wl_cfg80211_get_fwname(void);	/* get firmware name for
375 						 the dongle */
376 extern s8 *wl_cfg80211_get_nvramname(void);	/* get nvram name for
377 						 the dongle */
378 
379 #endif				/* _wl_cfg80211_h_ */
380