xref: /openbsd/sys/dev/usb/if_atureg.h (revision 4b1a56af)
1 /*	$OpenBSD: if_atureg.h,v 1.34 2022/01/09 05:43:00 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 simultaneously requested RX transfers */
39 #define ATU_RX_LIST_CNT	1
40 
41 /*
42  * the number of simultaneously 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 CAREFUL! 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 	struct usbd_xfer	*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_RSSI)			|	\
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_rssi;
139 	u_int8_t				rr_max_rssi;
140 } __packed;
141 
142 #define ATU_TX_RADIOTAP_PRESENT				\
143 	((1 << IEEE80211_RADIOTAP_FLAGS)	|	\
144 	 (1 << IEEE80211_RADIOTAP_RATE)		|	\
145 	 (1 << IEEE80211_RADIOTAP_CHANNEL)	|	\
146 	 0)
147 
148 struct atu_tx_radiotap_header {
149 	struct ieee80211_radiotap_header	rt_ihdr;
150 	u_int8_t				rt_flags;
151 	u_int8_t				rt_rate;
152 	u_int16_t				rt_chan_freq;
153 	u_int16_t				rt_chan_flags;
154 } __packed;
155 
156 struct atu_cdata {
157 	struct atu_chain	atu_tx_chain[ATU_TX_LIST_CNT];
158 	struct atu_chain	atu_rx_chain[ATU_RX_LIST_CNT];
159 
160 	SLIST_HEAD(atu_list_head, atu_chain)	atu_rx_free;
161 	struct atu_list_head	atu_tx_free;
162 
163 	u_int8_t		atu_tx_inuse;
164 	u_int8_t		atu_tx_last_idx;
165 };
166 
167 #define MAX_SSID_LEN		32
168 #define ATU_AVG_TIME		20
169 
170 struct atu_softc {
171 	struct device           atu_dev;
172 	struct ieee80211com	sc_ic;
173 	int			(*sc_newstate)(struct ieee80211com *,
174 				    enum ieee80211_state, int);
175 
176 	char			sc_cmd;
177 #define ATU_C_NONE		0
178 #define ATU_C_SCAN		1
179 #define ATU_C_JOIN		2
180 	struct usb_task		sc_task;
181 
182 	struct usbd_device	*atu_udev;
183 	struct usbd_interface	*atu_iface;
184 	struct ifmedia		atu_media;
185 	int			atu_ed[ATU_ENDPT_MAX];
186 	struct usbd_pipe	*atu_ep[ATU_ENDPT_MAX];
187 	int			atu_unit;
188 	int			atu_if_flags;
189 
190 	struct atu_cdata	atu_cdata;
191 
192 	struct timeval		atu_rx_notice;
193 
194 	u_int8_t		atu_bssid[ETHER_ADDR_LEN];
195 	enum atu_radio_type	atu_radio;
196 	u_int16_t		atu_quirk;
197 
198 	u_int8_t		atu_channel;
199 	u_int16_t		atu_desired_channel;
200 	u_int8_t		atu_mode;
201 #define NO_MODE_YET		0
202 #define AD_HOC_MODE		1
203 #define INFRASTRUCTURE_MODE	2
204 
205 	u_int8_t		atu_radio_on;
206 	caddr_t			sc_radiobpf;
207 
208 	union {
209 		struct atu_rx_radiotap_header	tap;
210 		u_int8_t			pad[64];
211 	} sc_rxtapu;
212 	union {
213 		struct atu_tx_radiotap_header	tap;
214 		u_int8_t			pad[64];
215 	} sc_txtapu;
216 
217 };
218 
219 #define sc_rxtap	sc_rxtapu.tap
220 #define sc_txtap	sc_txtapu.tap
221 
222 /* Commands for uploading the firmware (standard DFU interface) */
223 #define DFU_DNLOAD		UT_WRITE_CLASS_INTERFACE, 0x01
224 #define DFU_GETSTATUS		UT_READ_CLASS_INTERFACE, 0x03
225 #define DFU_GETSTATE		UT_READ_CLASS_INTERFACE, 0x05
226 #define DFU_REMAP		UT_WRITE_VENDOR_INTERFACE, 0x0a
227 
228 /* DFU states */
229 #define DFUState_AppIdle	0
230 #define DFUState_AppDetach	1
231 #define DFUState_DFUIdle	2
232 #define DFUState_DnLoadSync	3
233 #define DFUState_DnLoadBusy	4
234 #define DFUState_DnLoadIdle	5
235 #define DFUState_ManifestSync	6
236 #define DFUState_Manifest	7
237 #define DFUState_ManifestWait	8
238 #define DFUState_UploadIdle	9
239 #define DFUState_DFUError	10
240 
241 #define DFU_MaxBlockSize	1024
242 
243 /* AT76c503 operating modes */
244 #define MODE_NONE			0x00
245 #define MODE_NETCARD			0x01
246 #define MODE_CONFIG			0x02
247 #define MODE_DFU			0x03
248 #define MODE_NOFLASHNETCARD		0x04
249 
250 /* AT76c503 commands */
251 #define CMD_SET_MIB			0x01
252 #define CMD_START_SCAN			0x03
253 #define CMD_JOIN			0x04
254 #define CMD_START_IBSS			0x05
255 #define CMD_RADIO			0x06
256 #define CMD_RADIO_ON			0x06
257 #define CMD_RADIO_OFF			0x07
258 #define CMD_STARTUP			0x0b
259 
260 /* AT76c503 status messages -  used in atu_wait_completion */
261 #define STATUS_IDLE			0x00
262 #define STATUS_COMPLETE			0x01
263 #define STATUS_UNKNOWN			0x02
264 #define STATUS_INVALID_PARAMETER	0x03
265 #define STATUS_FUNCTION_NOT_SUPPORTED	0x04
266 #define STATUS_TIME_OUT			0x07
267 #define STATUS_IN_PROGRESS		0x08
268 #define STATUS_HOST_FAILURE		0xff
269 #define STATUS_SCAN_FAILED		0xf0
270 
271 /* AT76c503 command header */
272 struct atu_cmd {
273 	uByte			Cmd;
274 	uByte			Reserved;
275 	uWord			Size;
276 } __packed;
277 
278 /* CMD_SET_MIB command (0x01) */
279 struct atu_cmd_set_mib {
280 	/* AT76c503 command header */
281 	uByte		AtCmd;
282 	uByte		AtReserved;
283 	uWord		AtSize;
284 
285 	/* MIB header */
286 	uByte		MIBType;
287 	uByte		MIBSize;
288 	uByte		MIBIndex;
289 	uByte		MIBReserved;
290 
291 	/* MIB data */
292 	uByte		data[72];
293 } __packed;
294 
295 /* CMD_STARTUP command (0x0b) */
296 struct atu_cmd_card_config {
297 	uByte			Cmd;
298 	uByte			Reserved;
299 	uWord			Size;
300 
301 	uByte			ExcludeUnencrypted;
302 	uByte			PromiscuousMode;
303 	uByte			ShortRetryLimit;
304 	uByte			EncryptionType;
305 	uWord			RTS_Threshold;
306 	uWord			FragThreshold;		/* 256 .. 2346 */
307 	uByte			BasicRateSet[4];
308 	uByte			AutoRateFallback;
309 	uByte			Channel;
310 	uByte			PrivacyInvoked;		/* wep */
311 	uByte			WEP_DefaultKeyID;	/* 0 .. 3 */
312 	uByte			SSID[MAX_SSID_LEN];
313 	uByte			WEP_DefaultKey[4][13];
314 	uByte			SSID_Len;
315 	uByte			ShortPreamble;
316 	uWord			BeaconPeriod;
317 } __packed;
318 
319 /* CMD_SCAN command (0x03) */
320 struct atu_cmd_do_scan {
321 	uByte			Cmd;
322 	uByte			Reserved;
323 	uWord			Size;
324 
325 	uByte			BSSID[ETHER_ADDR_LEN];
326 	uByte			SSID[MAX_SSID_LEN];
327 	uByte			ScanType;
328 	uByte			Channel;
329 	uWord			ProbeDelay;
330 	uWord			MinChannelTime;
331 	uWord			MaxChannelTime;
332 	uByte			SSID_Len;
333 	uByte			InternationalScan;
334 } __packed;
335 
336 #define ATU_SCAN_ACTIVE		0x00
337 #define ATU_SCAN_PASSIVE	0x01
338 
339 /* CMD_JOIN command (0x04) */
340 struct atu_cmd_join {
341 	uByte			Cmd;
342 	uByte			Reserved;
343 	uWord			Size;
344 
345 	uByte			bssid[ETHER_ADDR_LEN];
346 	uByte			essid[32];
347 	uByte			bss_type;
348 	uByte			channel;
349 	uWord			timeout;
350 	uByte			essid_size;
351 	uByte			reserved;
352 } __packed;
353 
354 /* CMD_START_IBSS (0x05) */
355 struct atu_cmd_start_ibss {
356 	uByte		Cmd;
357 	uByte		Reserved;
358 	uWord		Size;
359 
360 	uByte		BSSID[ETHER_ADDR_LEN];
361 	uByte		SSID[32];
362 	uByte		BSSType;
363 	uByte		Channel;
364 	uByte		SSIDSize;
365 	uByte		Res[3];
366 } __packed;
367 
368 /*
369  * The At76c503 adapters come with different types of radios on them.
370  * At this moment the driver supports adapters with RFMD and Intersil radios.
371  */
372 
373 /* The config structure of an RFMD radio */
374 struct atu_rfmd_conf {
375 	u_int8_t		CR20[14];
376 	u_int8_t		CR21[14];
377 	u_int8_t		BB_CR[14];
378 	u_int8_t		PidVid[4];
379 	u_int8_t		MACAddr[ETHER_ADDR_LEN];
380 	u_int8_t		RegulatoryDomain;
381 	u_int8_t		LowPowerValues[14];
382 	u_int8_t		NormalPowerValues[14];
383 	u_int8_t		Reserved[3];
384 	/* then we have 84 bytes, somehow Windows reads 95?? */
385 	u_int8_t		Rest[11];
386 } __packed;
387 
388 /* The config structure of an Intersil radio */
389 struct atu_intersil_conf {
390 	u_int8_t		MACAddr[ETHER_ADDR_LEN];
391 	/* From the HFA3861B manual : */
392 	/* Manual TX power control (7bit : -64 to 63) */
393 	u_int8_t		CR31[14];
394 	/* TX power measurement */
395 	u_int8_t		CR58[14];
396 	u_int8_t		PidVid[4];
397 	u_int8_t		RegulatoryDomain;
398 	u_int8_t		Reserved[1];
399 } __packed;
400 
401 
402 /* Firmware information request */
403 struct atu_fw {
404 	u_int8_t		major;
405 	u_int8_t		minor;
406 	u_int8_t		patch;
407 	u_int8_t		build;
408 } __packed;
409 
410 /*
411  * The header the AT76c503 puts in front of RX packets (for both management &
412  * data)
413  */
414 struct atu_rx_hdr {
415 	uWord			length;
416 	uByte			rx_rate;
417 	uByte			newbss;
418 	uByte			fragmentation;
419 	uByte			rssi;
420 	uByte			link_quality;
421 	uByte			noise_level;
422 	uDWord			rx_time;
423 } __packed;
424 #define ATU_RX_HDRLEN sizeof(struct atu_rx_hdr)
425 
426 /*
427  * The header we have to put in front of a TX packet before sending it to the
428  * AT76c503
429  */
430 struct atu_tx_hdr {
431 	uWord				length;
432 	uByte				tx_rate;
433 	uByte				padding;
434 	uByte				reserved[4];
435 } __packed;
436 #define ATU_TX_HDRLEN sizeof(struct atu_tx_hdr)
437 
438 #define NR(x)		(void *)((long)x)
439 
440 /*
441  * The linux driver uses separate routines for every mib request they do
442  * (eg. set_radio / set_preamble / set_frag / etc etc )
443  * We just define a list of types, sizes and offsets and use those
444  */
445 
446 /*	Name				Type		Size	Index	*/
447 #define MIB_LOCAL			0x01
448 #define  MIB_LOCAL__BEACON_ENABLE	MIB_LOCAL,	1,	2
449 #define  MIB_LOCAL__AUTO_RATE_FALLBACK	MIB_LOCAL,	1,	3
450 #define  MIB_LOCAL__SSID_SIZE		MIB_LOCAL,	1,	5
451 #define  MIB_LOCAL__PREAMBLE		MIB_LOCAL,	1,	9
452 #define MIB_MAC_ADDR			0x02
453 #define  MIB_MAC_ADDR__ADDR		MIB_MAC_ADDR,	6,	0
454 #define MIB_MAC				0x03
455 #define  MIB_MAC__FRAG			MIB_MAC,	2,	8
456 #define  MIB_MAC__RTS			MIB_MAC,	2,	10
457 #define  MIB_MAC__DESIRED_SSID		MIB_MAC,	32,	28
458 #define MIB_MAC_MGMT			0x05
459 #define  MIB_MAC_MGMT__BEACON_PERIOD	MIB_MAC_MGMT,	2,	0
460 #define  MIB_MAC_MGMT__CURRENT_BSSID	MIB_MAC_MGMT,	6,	14
461 #define  MIB_MAC_MGMT__CURRENT_ESSID	MIB_MAC_MGMT,	32,	20
462 #define  MIB_MAC_MGMT__POWER_MODE	MIB_MAC_MGMT,	1,	53
463 #define  MIB_MAC_MGMT__IBSS_CHANGE	MIB_MAC_MGMT,	1,	54
464 #define MIB_MAC_WEP			0x06
465 #define  MIB_MAC_WEP__PRIVACY_INVOKED	MIB_MAC_WEP,	1,	0
466 #define  MIB_MAC_WEP__KEY_ID		MIB_MAC_WEP,	1,	1
467 #define  MIB_MAC_WEP__ICV_ERROR_COUNT	MIB_MAC_WEP,	4,	4
468 #define  MIB_MAC_WEP__EXCLUDED_COUNT	MIB_MAC_WEP,	4,	8
469 #define  MIB_MAC_WEP__KEYS(nr)		MIB_MAC_WEP,	13,	12+(nr)*13
470 #define  MIB_MAC_WEP__ENCR_LEVEL	MIB_MAC_WEP,	1,	64
471 #define MIB_PHY				0x07
472 #define  MIB_PHY__CHANNEL		MIB_PHY,	1,	20
473 #define  MIB_PHY__REG_DOMAIN		MIB_PHY,	1,	23
474 #define MIB_FW_VERSION			0x08
475 #define MIB_DOMAIN			0x09
476 #define  MIB_DOMAIN__POWER_LEVELS	MIB_DOMAIN,	14,	0
477 #define  MIB_DOMAIN__CHANNELS		MIB_DOMAIN,	14,	14
478 
479 #define ATU_WEP_OFF			0
480 #define ATU_WEP_40BITS			1
481 #define ATU_WEP_104BITS			2
482 
483 #define POWER_MODE_ACTIVE		1
484 #define POWER_MODE_SAVE			2
485 #define POWER_MODE_SMART		3
486 
487 #define PREAMBLE_SHORT			1
488 #define PREAMBLE_LONG			0
489