1 /*	$OpenBSD: if_atureg.h,v 1.24 2005/05/23 20:09:31 jsg Exp $ */
2 /*
3  * Copyright (c) 2003
4  *	Daan Vreeken <Danovitsch@Vitsch.net>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Daan Vreeken.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY DAAN VREEKEN AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Daan Vreeken OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #define ATU_CONFIG_NO		1
36 #define ATU_IFACE_IDX		0
37 
38 /* the number of simultaniuously requested RX transfers */
39 #define ATU_RX_LIST_CNT	1
40 
41 /*
42  * the number of simultaniously started TX transfers
43  * my measurements :
44  * 1		430.82 KB/sec
45  * 2		534.66 KB/sec
46  * 3		536.23 KB/sec
47  * 4		537.80 KB/sec
48  * 6		537.30 KB/sec
49  * 8		535.31 KB/sec
50  * 16		535.68 KB/sec
51  * 128		535.67 KB/sec (before you ask : yes, 128 is silly :)
52  * (+/- 24% increase)
53  */
54 #define ATU_TX_LIST_CNT	8
55 
56 /*
57  * According to the 802.11 spec (7.1.2) the frame body can be up to 2312 bytes
58  */
59 #define ATU_RX_BUFSZ		(ATU_RX_HDRLEN + \
60 				 sizeof(struct ieee80211_frame_addr4) + 2312 + 4)
61 /* BE CAREFULL! should add ATU_TX_PADDING */
62 #define ATU_TX_BUFSZ		(ATU_TX_HDRLEN + \
63 				 sizeof(struct ieee80211_frame_addr4) + 2312)
64 
65 #define ATU_MIN_FRAMELEN	60
66 
67 /*
68  * Sending packets of more than 1500 bytes confuses some access points, so the
69  * default MTU is set to 1500 but can be increased up to 2310 bytes using
70  * ifconfig
71  */
72 #define ATU_DEFAULT_MTU	1500
73 #define ATU_MAX_MTU		(2312 - 2)
74 
75 #define ATU_ENDPT_RX		0x0
76 #define ATU_ENDPT_TX		0x1
77 #define ATU_ENDPT_MAX		0x2
78 
79 #define ATU_TX_TIMEOUT		10000
80 #define ATU_JOIN_TIMEOUT	2000
81 
82 #define ATU_NO_QUIRK		0x0000
83 #define ATU_QUIRK_NO_REMAP	0x0001
84 #define ATU_QUIRK_FW_DELAY	0x0002
85 
86 #define ATU_DEFAULT_SSID	""
87 #define ATU_DEFAULT_CHANNEL	10
88 
89 enum atu_radio_type {
90 	RadioRFMD = 0,
91 	RadioRFMD2958,
92 	RadioRFMD2958_SMC,
93 	RadioIntersil,
94 	AT76C503_i3863,
95 	AT76C503_rfmd_acc,
96 	AT76C505_rfmd
97 };
98 
99 struct atu_type {
100 	u_int16_t		atu_vid;
101 	u_int16_t		atu_pid;
102 	enum atu_radio_type	atu_radio;
103 	u_int16_t		atu_quirk;
104 };
105 
106 struct atu_softc;
107 
108 struct atu_chain {
109 	struct atu_softc	*atu_sc;
110 	usbd_xfer_handle	atu_xfer;
111 	char			*atu_buf;
112 	struct mbuf		*atu_mbuf;
113 	u_int8_t		atu_idx;
114 	u_int16_t		atu_length;
115 	int			atu_in_xfer;
116 	SLIST_ENTRY(atu_chain)	atu_list;
117 };
118 
119 /* Radio capture format */
120 
121 #define ATU_RX_RADIOTAP_PRESENT					\
122 	((1 << IEEE80211_RADIOTAP_TSFT)			|	\
123 	 (1 << IEEE80211_RADIOTAP_FLAGS)		|	\
124 	 (1 << IEEE80211_RADIOTAP_RATE)			|	\
125 	 (1 << IEEE80211_RADIOTAP_CHANNEL)		|	\
126 	 (1 << IEEE80211_RADIOTAP_LOCK_QUALITY)		|	\
127 	 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL)		|	\
128 	 0)
129 
130 struct atu_rx_radiotap_header {
131 	struct ieee80211_radiotap_header	rr_ihdr;
132 	u_int64_t				rr_tsft;
133 	u_int8_t				rr_flags;
134 	u_int8_t				rr_rate;
135 	u_int16_t				rr_chan_freq;
136 	u_int16_t				rr_chan_flags;
137 	u_int16_t				rr_barker_lock;
138 	u_int8_t				rr_antsignal;
139 } __attribute__((__packed__));
140 
141 #define ATU_TX_RADIOTAP_PRESENT				\
142 	((1 << IEEE80211_RADIOTAP_FLAGS)	|	\
143 	 (1 << IEEE80211_RADIOTAP_RATE)		|	\
144 	 (1 << IEEE80211_RADIOTAP_CHANNEL)	|	\
145 	 0)
146 
147 struct atu_tx_radiotap_header {
148 	struct ieee80211_radiotap_header	rt_ihdr;
149 	u_int8_t				rt_flags;
150 	u_int8_t				rt_rate;
151 	u_int16_t				rt_chan_freq;
152 	u_int16_t				rt_chan_flags;
153 } __attribute__((__packed__));
154 
155 struct atu_cdata {
156 	struct atu_chain	atu_tx_chain[ATU_TX_LIST_CNT];
157 	struct atu_chain	atu_rx_chain[ATU_RX_LIST_CNT];
158 
159 	SLIST_HEAD(atu_list_head, atu_chain)	atu_rx_free;
160 	struct atu_list_head	atu_tx_free;
161 
162 	u_int8_t		atu_tx_inuse;
163 	u_int8_t		atu_tx_last_idx;
164 };
165 
166 #define MAX_SSID_LEN		32
167 #define ATU_AVG_TIME		20
168 
169 struct atu_softc {
170 	USBBASEDEVICE           atu_dev;
171 	struct ieee80211com	sc_ic;
172 	int			(*sc_newstate)(struct ieee80211com *,
173 				    enum ieee80211_state, int);
174 
175 	char			sc_state;
176 #define ATU_S_DEAD		0
177 #define ATU_S_OK		1
178 #define ATU_S_UNCONFIG		2
179 	char			sc_cmd;
180 #define ATU_C_NONE		0
181 #define ATU_C_SCAN		1
182 #define ATU_C_JOIN		2
183 	struct usb_task		sc_task;
184 
185 	usbd_device_handle	atu_udev;
186 	usbd_interface_handle	atu_iface;
187 	struct ifmedia		atu_media;
188 	int			atu_ed[ATU_ENDPT_MAX];
189 	usbd_pipe_handle	atu_ep[ATU_ENDPT_MAX];
190 	int			atu_unit;
191 	int			atu_if_flags;
192 
193 	struct atu_cdata	atu_cdata;
194 
195 	struct timeval		atu_rx_notice;
196 
197 	u_int8_t		atu_bssid[ETHER_ADDR_LEN];
198 	enum atu_radio_type	atu_radio;
199 	u_int16_t		atu_quirk;
200 
201 	u_int8_t		atu_ssid[MAX_SSID_LEN];
202 	u_int8_t		atu_ssidlen;
203 	u_int8_t		atu_channel;
204 	u_int16_t		atu_desired_channel;
205 	u_int8_t		atu_mode;
206 #define NO_MODE_YET		0
207 #define AD_HOC_MODE		1
208 #define INFRASTRUCTURE_MODE	2
209 
210 	u_int8_t		atu_radio_on;
211 	caddr_t			sc_radiobpf;
212 
213 	union {
214 		struct atu_rx_radiotap_header	tap;
215 		u_int8_t			pad[64];
216 	} sc_rxtapu;
217 	union {
218 		struct atu_tx_radiotap_header	tap;
219 		u_int8_t			pad[64];
220 	} sc_txtapu;
221 
222 };
223 
224 #define sc_rxtap	sc_rxtapu.tap
225 #define sc_txtap	sc_txtapu.tap
226 
227 /* Commands for uploading the firmware (standard DFU interface) */
228 #define DFU_DNLOAD		UT_WRITE_CLASS_INTERFACE, 0x01
229 #define DFU_GETSTATUS		UT_READ_CLASS_INTERFACE, 0x03
230 #define DFU_GETSTATE		UT_READ_CLASS_INTERFACE, 0x05
231 #define DFU_REMAP		UT_WRITE_VENDOR_INTERFACE, 0x0a
232 
233 /* DFU states */
234 #define DFUState_AppIdle	0
235 #define DFUState_AppDetach	1
236 #define DFUState_DFUIdle	2
237 #define DFUState_DnLoadSync	3
238 #define DFUState_DnLoadBusy	4
239 #define DFUState_DnLoadIdle	5
240 #define DFUState_ManifestSync	6
241 #define DFUState_Manifest	7
242 #define DFUState_ManifestWait	8
243 #define DFUState_UploadIdle	9
244 #define DFUState_DFUError	10
245 
246 #define DFU_MaxBlockSize	1024
247 
248 /* AT76c503 operating modes */
249 #define MODE_NONE			0x00
250 #define MODE_NETCARD			0x01
251 #define MODE_CONFIG			0x02
252 #define MODE_DFU			0x03
253 #define MODE_NOFLASHNETCARD		0x04
254 
255 /* AT76c503 commands */
256 #define CMD_SET_MIB			0x01
257 #define CMD_START_SCAN			0x03
258 #define CMD_JOIN			0x04
259 #define CMD_START_IBSS			0x05
260 #define CMD_RADIO			0x06
261 #define CMD_RADIO_ON			0x06
262 #define CMD_RADIO_OFF			0x07
263 #define CMD_STARTUP			0x0b
264 
265 /* AT76c503 status messages -  used in atu_wait_completion */
266 #define STATUS_IDLE			0x00
267 #define STATUS_COMPLETE			0x01
268 #define STATUS_UNKNOWN			0x02
269 #define STATUS_INVALID_PARAMETER	0x03
270 #define STATUS_FUNCTION_NOT_SUPPORTED	0x04
271 #define STATUS_TIME_OUT			0x07
272 #define STATUS_IN_PROGRESS		0x08
273 #define STATUS_HOST_FAILURE		0xff
274 #define STATUS_SCAN_FAILED		0xf0
275 
276 /* AT76c503 command header */
277 struct atu_cmd {
278 	uByte			Cmd;
279 	uByte			Reserved;
280 	uWord			Size;
281 } UPACKED;
282 
283 /* CMD_SET_MIB command (0x01) */
284 struct atu_cmd_set_mib {
285 	/* AT76c503 command header */
286 	uByte		AtCmd;
287 	uByte		AtReserved;
288 	uWord		AtSize;
289 
290 	/* MIB header */
291 	uByte		MIBType;
292 	uByte		MIBSize;
293 	uByte		MIBIndex;
294 	uByte		MIBReserved;
295 
296 	/* MIB data */
297 	uByte		data[72];
298 } UPACKED;
299 
300 /* CMD_STARTUP command (0x0b) */
301 struct atu_cmd_card_config {
302 	uByte			Cmd;
303 	uByte			Reserved;
304 	uWord			Size;
305 
306 	uByte			ExcludeUnencrypted;
307 	uByte			PromiscuousMode;
308 	uByte			ShortRetryLimit;
309 	uByte			EncryptionType;
310 	uWord			RTS_Threshold;
311 	uWord			FragThreshold;		/* 256 .. 2346 */
312 	uByte			BasicRateSet[4];
313 	uByte			AutoRateFallback;
314 	uByte			Channel;
315 	uByte			PrivacyInvoked;		/* wep */
316 	uByte			WEP_DefaultKeyID;	/* 0 .. 3 */
317 	uByte			SSID[MAX_SSID_LEN];
318 	uByte			WEP_DefaultKey[4][13];
319 	uByte			SSID_Len;
320 	uByte			ShortPreamble;
321 	uWord			BeaconPeriod;
322 } UPACKED;
323 
324 /* CMD_SCAN command (0x03) */
325 struct atu_cmd_do_scan {
326 	uByte			Cmd;
327 	uByte			Reserved;
328 	uWord			Size;
329 
330 	uByte			BSSID[ETHER_ADDR_LEN];
331 	uByte			SSID[MAX_SSID_LEN];
332 	uByte			ScanType;
333 	uByte			Channel;
334 	uWord			ProbeDelay;
335 	uWord			MinChannelTime;
336 	uWord			MaxChannelTime;
337 	uByte			SSID_Len;
338 	uByte			InternationalScan;
339 } UPACKED;
340 
341 #define ATU_SCAN_ACTIVE		0x00
342 #define ATU_SCAN_PASSIVE	0x01
343 
344 /* CMD_JOIN command (0x04) */
345 struct atu_cmd_join {
346 	uByte			Cmd;
347 	uByte			Reserved;
348 	uWord			Size;
349 
350 	uByte			bssid[ETHER_ADDR_LEN];
351 	uByte			essid[32];
352 	uByte			bss_type;
353 	uByte			channel;
354 	uWord			timeout;
355 	uByte			essid_size;
356 	uByte			reserved;
357 } UPACKED;
358 
359 /* CMD_START_IBSS (0x05) */
360 struct atu_cmd_start_ibss {
361 	uByte		Cmd;
362 	uByte		Reserved;
363 	uWord		Size;
364 
365 	uByte		BSSID[ETHER_ADDR_LEN];
366 	uByte		SSID[32];
367 	uByte		BSSType;
368 	uByte		Channel;
369 	uByte		SSIDSize;
370 	uByte		Res[3];
371 } UPACKED;
372 
373 /*
374  * The At76c503 adapters come with different types of radios on them.
375  * At this moment the driver supports adapters with RFMD and Intersil radios.
376  */
377 
378 /* The config structure of an RFMD radio */
379 struct atu_rfmd_conf {
380 	u_int8_t		CR20[14];
381 	u_int8_t		CR21[14];
382 	u_int8_t		BB_CR[14];
383 	u_int8_t		PidVid[4];
384 	u_int8_t		MACAddr[ETHER_ADDR_LEN];
385 	u_int8_t		RegulatoryDomain;
386 	u_int8_t		LowPowerValues[14];
387 	u_int8_t		NormalPowerValues[14];
388 	u_int8_t		Reserved[3];
389 	/* then we have 84 bytes, somehow Windows reads 95?? */
390 	u_int8_t		Rest[11];
391 } UPACKED;
392 
393 /* The config structure of an Intersil radio */
394 struct atu_intersil_conf {
395 	u_int8_t		MACAddr[ETHER_ADDR_LEN];
396 	/* From the HFA3861B manual : */
397 	/* Manual TX power control (7bit : -64 to 63) */
398 	u_int8_t		CR31[14];
399 	/* TX power measurement */
400 	u_int8_t		CR58[14];
401 	u_int8_t		PidVid[4];
402 	u_int8_t		RegulatoryDomain;
403 	u_int8_t		Reserved[1];
404 } UPACKED;
405 
406 
407 /* Firmware information request */
408 struct atu_fw {
409 	u_int8_t		major;
410 	u_int8_t		minor;
411 	u_int8_t		patch;
412 	u_int8_t		build;
413 } UPACKED;
414 
415 /*
416  * The header the AT76c503 puts in front of RX packets (for both managment &
417  * data)
418  */
419 struct atu_rx_hdr {
420 	uWord			length;
421 	uByte			rx_rate;
422 	uByte			newbss;
423 	uByte			fragmentation;
424 	uByte			rssi;
425 	uByte			link_quality;
426 	uByte			noise_level;
427 	uDWord			rx_time;
428 } UPACKED;
429 #define ATU_RX_HDRLEN sizeof(struct atu_rx_hdr)
430 
431 /*
432  * The header we have to put in front of a TX packet before sending it to the
433  * AT76c503
434  */
435 struct atu_tx_hdr {
436 	uWord				length;
437 	uByte				tx_rate;
438 	uByte				padding;
439 	uByte				reserved[4];
440 } UPACKED;
441 #define ATU_TX_HDRLEN sizeof(struct atu_tx_hdr)
442 
443 #define NR(x)		(void *)((long)x)
444 
445 /*
446  * The linux driver uses seperate routines for every mib request they do
447  * (eg. set_radio / set_preamble / set_frag / etc etc )
448  * We just define a list of types, sizes and offsets and use those
449  */
450 
451 /*	Name				Type		Size	Index	*/
452 #define MIB_LOCAL			0x01
453 #define  MIB_LOCAL__BEACON_ENABLE	MIB_LOCAL,	1,	2
454 #define  MIB_LOCAL__AUTO_RATE_FALLBACK	MIB_LOCAL,	1,	3
455 #define  MIB_LOCAL__SSID_SIZE		MIB_LOCAL,	1,	5
456 #define  MIB_LOCAL__PREAMBLE		MIB_LOCAL,	1,	9
457 #define MIB_MAC_ADDR			0x02
458 #define  MIB_MAC_ADDR__ADDR		MIB_MAC_ADDR,	6,	0
459 #define MIB_MAC				0x03
460 #define  MIB_MAC__FRAG			MIB_MAC,	2,	8
461 #define  MIB_MAC__RTS			MIB_MAC,	2,	10
462 #define  MIB_MAC__DESIRED_SSID		MIB_MAC,	32,	28
463 #define MIB_MAC_MGMT			0x05
464 #define  MIB_MAC_MGMT__BEACON_PERIOD	MIB_MAC_MGMT,	2,	0
465 #define  MIB_MAC_MGMT__CURRENT_BSSID	MIB_MAC_MGMT,	6,	14
466 #define  MIB_MAC_MGMT__CURRENT_ESSID	MIB_MAC_MGMT,	32,	20
467 #define  MIB_MAC_MGMT__POWER_MODE	MIB_MAC_MGMT,	1,	53
468 #define  MIB_MAC_MGMT__IBSS_CHANGE	MIB_MAC_MGMT,	1,	54
469 #define MIB_MAC_WEP			0x06
470 #define  MIB_MAC_WEP__PRIVACY_INVOKED	MIB_MAC_WEP,	1,	0
471 #define  MIB_MAC_WEP__KEY_ID		MIB_MAC_WEP,	1,	1
472 #define  MIB_MAC_WEP__ICV_ERROR_COUNT	MIB_MAC_WEP,	4,	4
473 #define  MIB_MAC_WEP__EXCLUDED_COUNT	MIB_MAC_WEP,	4,	8
474 #define  MIB_MAC_WEP__KEYS(nr)		MIB_MAC_WEP,	13,	12+(nr)*13
475 #define  MIB_MAC_WEP__ENCR_LEVEL	MIB_MAC_WEP,	1,	64
476 #define MIB_PHY				0x07
477 #define  MIB_PHY__CHANNEL		MIB_PHY,	1,	20
478 #define  MIB_PHY__REG_DOMAIN		MIB_PHY,	1,	23
479 #define MIB_FW_VERSION			0x08
480 #define MIB_DOMAIN			0x09
481 #define  MIB_DOMAIN__POWER_LEVELS	MIB_DOMAIN,	14,	0
482 #define  MIB_DOMAIN__CHANNELS		MIB_DOMAIN,	14,	14
483 
484 #define ATU_WEP_OFF			0
485 #define ATU_WEP_40BITS			1
486 #define ATU_WEP_104BITS			2
487 
488 #define POWER_MODE_ACTIVE		1
489 #define POWER_MODE_SAVE			2
490 #define POWER_MODE_SMART		3
491 
492 #define PREAMBLE_SHORT			1
493 #define PREAMBLE_LONG			0
494