usb.h revision 1.111.2.6 1 /* $NetBSD: usb.h,v 1.111.2.6 2015/04/06 15:18:13 skrll Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb.h,v 1.14 1999/11/17 22:33:46 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
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 THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35 #ifndef _USB_H_
36 #define _USB_H_
37
38 #include <sys/types.h>
39 #include <sys/time.h>
40
41 #include <sys/ioctl.h>
42
43 #if defined(_KERNEL)
44
45 #include <sys/device.h>
46
47 #endif
48
49 #ifdef USB_DEBUG
50 #define Static
51 #else
52 #define Static static
53 #endif
54
55 #define USB_STACK_VERSION 2
56
57 #define USB_MAX_DEVICES 128
58 #define USB_MIN_DEVICES 2 /* unused + root HUB */
59 #define USB_START_ADDR 0
60
61 #define USB_CONTROL_ENDPOINT 0
62 #define USB_MAX_ENDPOINTS 16
63
64 #define USB_FRAMES_PER_SECOND 1000
65 #define USB_UFRAMES_PER_FRAME 8
66
67 /*
68 * The USB records contain some unaligned little-endian word
69 * components. The U[SG]ETW macros take care of both the alignment
70 * and endian problem and should always be used to access non-byte
71 * values.
72 */
73 typedef uint8_t uByte;
74 typedef uint8_t uWord[2];
75 typedef uint8_t uDWord[4];
76
77 #define USETW2(w,h,l) ((w)[0] = (uint8_t)(l), (w)[1] = (uint8_t)(h))
78
79 #define UGETW(w) ((w)[0] | ((w)[1] << 8))
80 #define USETW(w,v) ((w)[0] = (uint8_t)(v), (w)[1] = (uint8_t)((v) >> 8))
81 #define USETWD(val) { (uint8_t)(val), (uint8_t)((val) >> 8) }
82 #define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
83 #define USETDW(w,v) ((w)[0] = (uint8_t)(v), \
84 (w)[1] = (uint8_t)((v) >> 8), \
85 (w)[2] = (uint8_t)((v) >> 16), \
86 (w)[3] = (uint8_t)((v) >> 24))
87 #define UPACKED __packed
88
89 typedef struct {
90 uByte bmRequestType;
91 uByte bRequest;
92 uWord wValue;
93 uWord wIndex;
94 uWord wLength;
95 } UPACKED usb_device_request_t;
96
97 #define UT_GET_DIR(a) ((a) & 0x80)
98 #define UT_WRITE 0x00
99 #define UT_READ 0x80
100
101 #define UT_GET_TYPE(a) ((a) & 0x60)
102 #define UT_STANDARD 0x00
103 #define UT_CLASS 0x20
104 #define UT_VENDOR 0x40
105
106 #define UT_GET_RECIPIENT(a) ((a) & 0x1f)
107 #define UT_DEVICE 0x00
108 #define UT_INTERFACE 0x01
109 #define UT_ENDPOINT 0x02
110 #define UT_OTHER 0x03
111
112 #define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
113 #define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
114 #define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
115 #define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
116 #define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
117 #define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
118 #define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
119 #define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
120 #define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
121 #define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
122 #define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
123 #define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
124 #define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
125 #define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
126 #define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
127 #define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
128 #define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
129 #define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
130 #define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
131 #define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
132 #define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
133 #define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
134
135 /* Standard Requests Codes from the USB 2.0 spec, table 9-4 */
136 #define UR_GET_STATUS 0x00
137 #define UR_CLEAR_FEATURE 0x01
138 #define UR_SET_FEATURE 0x03
139 #define UR_SET_ADDRESS 0x05
140 #define UR_GET_DESCRIPTOR 0x06
141 #define UDESC_DEVICE 0x01
142 #define UDESC_CONFIG 0x02
143 #define UDESC_STRING 0x03
144 #define UDESC_INTERFACE 0x04
145 #define UDESC_ENDPOINT 0x05
146 #define UDESC_DEVICE_QUALIFIER 0x06
147 #define UDESC_OTHER_SPEED_CONFIGURATION 0x07
148 #define UDESC_INTERFACE_POWER 0x08
149 #define UDESC_OTG 0x09
150 #define UDESC_DEBUG 0x0a
151 #define UDESC_INTERFACE_ASSOC 0x0b
152 #define UDESC_BOS 0x0f
153 #define UDESC_DEVICE_CAPABILITY 0x10
154 #define UDESC_CS_DEVICE 0x21 /* class specific */
155 #define UDESC_CS_CONFIG 0x22
156 #define UDESC_CS_STRING 0x23
157 #define UDESC_CS_INTERFACE 0x24
158 #define UDESC_CS_ENDPOINT 0x25
159 #define UDESC_HUB 0x29
160 #define UDESC_SS_HUB 0x2a /* super speed */
161 #define UDESC_ENDPOINT_SS_COMP 0x30 /* super speed */
162 #define UDESC_ENDPOINT_ISOCH_SSP_COMP 0x31
163 #define UR_SET_DESCRIPTOR 0x07
164 #define UR_GET_CONFIG 0x08
165 #define UR_SET_CONFIG 0x09
166 #define UR_GET_INTERFACE 0x0a
167 #define UR_SET_INTERFACE 0x0b
168 #define UR_SYNCH_FRAME 0x0c
169 #define UR_SET_ENCRYPTION 0x0d
170 #define UR_GET_ENCRYPTION 0x0e
171 #define UR_SET_HANDSHAKE 0x0f
172 #define UR_GET_HANDSHAKE 0x10
173 #define UR_SET_CONNECTION 0x11
174 #define UR_SET_SECURITY_DATA 0x12
175 #define UR_GET_SECURITY_DATA 0x13
176 #define UR_SET_WUSB_DATA 0x14
177 #define UR_LOOPBACK_DATA_WRITE 0x15
178 #define UR_LOOPBACK_DATA_READ 0x16
179 #define UR_SET_INTERFACE_DS 0x17
180 #define UR_SET_SEL 0x30
181 #define UR_SET_ISOCH_DELAY 0x31
182
183 /*
184 * Feature selectors. USB 2.0 spec, table 9-6 and OTG and EH suppliment,
185 * table 6-2
186 */
187 #define UF_ENDPOINT_HALT 0
188 #define UF_INTERFACE_FUNCTION_SUSPEND 0
189 #define UF_DEVICE_REMOTE_WAKEUP 1
190 #define UF_TEST_MODE 2
191 #define UF_DEVICE_B_HNP_ENABLE 3
192 #define UF_DEVICE_A_HNP_SUPPORT 4
193 #define UF_DEVICE_A_ALT_HNP_SUPPORT 5
194 #define UF_DEVICE_WUSB_DEVICE 6
195 #define UF_U1_ENABLE 0x30
196 #define UF_U2_ENABLE 0x31
197 #define UF_LTM_ENABLE 0x32
198
199 #define USB_MAX_IPACKET 8 /* maximum size of the initial packet */
200
201 #define USB_2_MAX_CTRL_PACKET 64
202 #define USB_2_MAX_BULK_PACKET 512
203
204 #define USB_3_MAX_CTRL_PACKET 512
205
206 typedef struct {
207 uByte bLength;
208 uByte bDescriptorType;
209 } UPACKED usb_descriptor_t;
210
211 typedef struct {
212 uByte bLength;
213 uByte bDescriptorType;
214 uWord bcdUSB;
215 #define UD_USB_2_0 0x0200
216 #define UD_USB_3_0 0x0300
217 #define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
218 #define UD_IS_USB3(d) (UGETW((d)->bcdUSB) >= UD_USB_3_0)
219 uByte bDeviceClass;
220 uByte bDeviceSubClass;
221 uByte bDeviceProtocol;
222 uByte bMaxPacketSize;
223 /* The fields below are not part of the initial descriptor. */
224 uWord idVendor;
225 uWord idProduct;
226 uWord bcdDevice;
227 uByte iManufacturer;
228 uByte iProduct;
229 uByte iSerialNumber;
230 uByte bNumConfigurations;
231 } UPACKED usb_device_descriptor_t;
232 #define USB_DEVICE_DESCRIPTOR_SIZE 18
233
234 typedef struct {
235 uByte bLength;
236 uByte bDescriptorType;
237 uWord wTotalLength;
238 uByte bNumInterface;
239 uByte bConfigurationValue;
240 uByte iConfiguration;
241 uByte bmAttributes;
242 #define UC_ATTR_MBO 0x80
243 #define UC_SELF_POWERED 0x40
244 #define UC_REMOTE_WAKEUP 0x20
245 uByte bMaxPower; /* max current in 2 mA units */
246 #define UC_POWER_FACTOR 2
247 #define UC_POWER_FACTOR_SS 8
248 } UPACKED usb_config_descriptor_t;
249 #define USB_CONFIG_DESCRIPTOR_SIZE 9
250
251 typedef struct {
252 uByte bLength;
253 uByte bDescriptorType;
254 uByte bInterfaceNumber;
255 uByte bAlternateSetting;
256 uByte bNumEndpoints;
257 uByte bInterfaceClass;
258 uByte bInterfaceSubClass;
259 uByte bInterfaceProtocol;
260 uByte iInterface;
261 } UPACKED usb_interface_descriptor_t;
262 #define USB_INTERFACE_DESCRIPTOR_SIZE 9
263
264 typedef struct {
265 uByte bLength;
266 uByte bDescriptorType;
267 uByte bFirstInterface;
268 uByte bInterfaceCount;
269 uByte bFunctionClass;
270 uByte bFunctionSubClass;
271 uByte bFunctionProtocol;
272 uByte iFunction;
273 } UPACKED usb_interface_assoc_descriptor_t;
274 #define USB_INTERFACE_ASSOC_DESCRIPTOR_SIZE 8
275
276 typedef struct {
277 uByte bLength;
278 uByte bDescriptorType;
279 uByte bEndpointAddress;
280 #define UE_GET_DIR(a) ((a) & 0x80)
281 #define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
282 #define UE_DIR_IN 0x80
283 #define UE_DIR_OUT 0x00
284 #define UE_ADDR 0x0f
285 #define UE_GET_ADDR(a) ((a) & UE_ADDR)
286 uByte bmAttributes;
287 #define UE_XFERTYPE 0x03
288 #define UE_CONTROL 0x00
289 #define UE_ISOCHRONOUS 0x01
290 #define UE_BULK 0x02
291 #define UE_INTERRUPT 0x03
292 #define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
293 #define UE_ISO_TYPE 0x0c
294 #define UE_ISO_ASYNC 0x04
295 #define UE_ISO_ADAPT 0x08
296 #define UE_ISO_SYNC 0x0c
297 #define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
298 uWord wMaxPacketSize;
299 #define UE_GET_TRANS(a) (((a) >> 11) & 0x3)
300 #define UE_GET_SIZE(a) ((a) & 0x7ff)
301 uByte bInterval;
302 } UPACKED usb_endpoint_descriptor_t;
303 #define USB_ENDPOINT_DESCRIPTOR_SIZE 7
304
305 typedef struct {
306 uByte bLength;
307 uByte bDescriptorType;
308 uByte bMaxBurst;
309 uByte bmAttributes;
310 #define UE_SSC_MAXSTREAMS(x) __SHIFTOUT(x, __BITS(4,0)) /* bulk */
311 #define UE_SSC_MULT(x) __SHIFTOUT(x, __BITS(1,0)) /* isoch */
312 #define UE_SSC_SSP_ISO(x) __SHIFTOUT(x, __BIT(7)) /* isoch */
313 /* The fields below are only valid for periodic endpoints */
314 uWord wBytesPerInterval;
315 } UPACKED usb_endpoint_ss_comp_descriptor_t;
316 #define USB_ENDPOINT_SS_COMP_DESCRIPTOR_SIZE 6
317
318 /* USB 3.0 9.6.2, Table 9-12 */
319 typedef struct {
320 uByte bLength;
321 uByte bDescriptorType;
322 uWord wTotalLength;
323 uByte bNumDeviceCaps;
324 } UPACKED usb_bos_descriptor_t;
325 #define USB_BOS_DESCRIPTOR_SIZE 5
326
327 /* common members of device capability descriptors */
328 typedef struct {
329 uByte bLength;
330 uByte bDescriptorType;
331 uByte bDevCapabilityType;
332 /* Table 9-14 */
333 #define USB_DEVCAP_RESERVED 0x00
334 #define USB_DEVCAP_WUSB 0x01
335 #define USB_DEVCAP_USB2EXT 0x02
336 #define USB_DEVCAP_SUPER_SPEED 0x03
337 #define USB_DEVCAP_CONTAINER_ID 0x04
338 #define USB_DEVCAP_PLATFORM 0x05
339 #define USB_DEVCAP_POWER_DELIVERY_CAPABILITY 0x06
340 #define USB_DEVCAP_BATTERY_INFO_CAPABILITY 0x07
341 #define USB_DEVCAP_PD_CONSUMER_PORT_CAPABILITY 0x08
342 #define USB_DEVCAP_PD_PROVIDER_PORT_CAPABILITY 0x09
343 #define USB_DEVCAP_SUPERSPEED_PLUS 0x0a
344 #define USB_DEVCAP_PRECISION_TIME_MEASUREMENT 0x0b
345 #define USB_DEVCAP_WUSB_EXT 0x0c
346 /* data ... */
347 } UPACKED usb_device_capability_descriptor_t;
348 #define USB_DEVICE_CAPABILITY_DESCRIPTOR_SIZE 3 /* at least */
349
350 /* 9.6.2.1 */
351 typedef struct {
352 uByte bLength;
353 uByte bDescriptorType;
354 uByte bDevCapabilityType;
355 uDWord bmAttributes;
356 #define USB_DEVCAP_USB2EXT_LPM __BIT(1)
357 } UPACKED usb_devcap_usb2ext_descriptor_t;
358 #define USB_DEVCAP_USB2EXT_DESCRIPTOR_SIZE 7
359
360 /* 9.6.2.2 */
361 typedef struct {
362 uByte bLength;
363 uByte bDescriptorType;
364 uByte bDevCapabilityType;
365 uByte bmAttributes;
366 #define USB_DEVCAP_SS_LTM __BIT(1)
367 uWord wSpeedsSupported;
368 #define USB_DEVCAP_SS_SPEED_LS __BIT(0)
369 #define USB_DEVCAP_SS_SPEED_FS __BIT(1)
370 #define USB_DEVCAP_SS_SPEED_HS __BIT(2)
371 #define USB_DEVCAP_SS_SPEED_SS __BIT(3)
372 uByte bFunctionalitySupport;
373 uByte bU1DevExitLat;
374 uWord wU2DevExitLat;
375 } UPACKED usb_devcap_ss_descriptor_t;
376 #define USB_DEVCAP_SS_DESCRIPTOR_SIZE 10
377
378 /* 9.6.2.4 */
379 typedef struct {
380 uByte bLength;
381 uByte bDescriptorType;
382 uByte bDevCapabilityType;
383 uByte bReserved;
384 uByte ContainerID[16];
385 } UPACKED usb_devcap_container_id_descriptor_t;
386 #define USB_DEVCAP_CONTAINER_ID_DESCRIPTOR_SIZE 20
387
388 typedef struct {
389 uByte bLength;
390 uByte bDescriptorType;
391 uByte bDevCapabilityType;
392 uByte bReserved;
393 uByte PlatformCapabilityUUID[16];
394 uByte CapabilityData[0];
395 } UPACKED usb_devcap_platform_descriptor_t;
396 #define USB_DEVCAP_PLATFORM_DESCRIPTOR_SIZE 20
397
398 typedef struct {
399 uByte bLength;
400 uByte bDescriptorType;
401 uByte bDevCapabilityType;
402 uByte bReserved;
403 uDWord bmAttributes;
404 uWord wFunctionalitySupport;
405 uWord wReserved;
406 uDWord bmSublinkSpeedAttr[0];
407 } UPACKED usb_devcap_ssp_descriptor_t;
408 #define USB_DEVCAP_SSP_DESCRIPTOR_SIZE 12 /* variable length */
409
410 typedef struct {
411 uByte bLength;
412 uByte bDescriptorType;
413 uWord bString[126];
414 } UPACKED usb_string_descriptor_t;
415 #define USB_MAX_STRING_LEN 128
416 #define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
417 #define USB_MAX_ENCODED_STRING_LEN (USB_MAX_STRING_LEN * 3) /* UTF8 */
418
419 /* Hub specific request */
420 #define UR_GET_BUS_STATE 0x02
421 #define UR_CLEAR_TT_BUFFER 0x08
422 #define UR_RESET_TT 0x09
423 #define UR_GET_TT_STATE 0x0a
424 #define UR_STOP_TT 0x0b
425 #define UR_SET_HUB_DEPTH 0x0c
426 #define UR_GET_PORT_ERR_COUNT 0x0d
427
428 /*
429 * Hub features from USB 2.0 spec, table 11-17 and updated by the
430 * LPM ECN table 4-7.
431 */
432 #define UHF_C_HUB_LOCAL_POWER 0
433 #define UHF_C_HUB_OVER_CURRENT 1
434 #define UHF_PORT_CONNECTION 0
435 #define UHF_PORT_ENABLE 1
436 #define UHF_PORT_SUSPEND 2
437 #define UHF_PORT_OVER_CURRENT 3
438 #define UHF_PORT_RESET 4
439 #define UHF_PORT_LINK_STATE 5
440 #define UHF_PORT_POWER 8
441 #define UHF_PORT_LOW_SPEED 9
442 #define UHF_PORT_L1 10
443 #define UHF_C_PORT_CONNECTION 16
444 #define UHF_C_PORT_ENABLE 17
445 #define UHF_C_PORT_SUSPEND 18
446 #define UHF_C_PORT_OVER_CURRENT 19
447 #define UHF_C_PORT_RESET 20
448 #define UHF_PORT_TEST 21
449 #define UHF_PORT_INDICATOR 22
450 #define UHF_C_PORT_L1 23
451
452 /* SS HUB specific features */
453 #define UHF_PORT_U1_TIMEOUT 23
454 #define UHF_PORT_U2_TIMEOUT 24
455 #define UHF_C_PORT_LINK_STATE 25
456 #define UHF_C_PORT_CONFIG_ERROR 26
457 #define UHF_PORT_REMOTE_WAKE_MASK 27
458 #define UHF_BH_PORT_RESET 28
459 #define UHF_C_BH_PORT_RESET 29
460 #define UHF_FORCE_LINKPM_ACCEPT 30
461
462 typedef struct {
463 uByte bDescLength;
464 uByte bDescriptorType;
465 uByte bNbrPorts;
466 #define UHD_NPORTS_MAX 255
467 uWord wHubCharacteristics;
468 #define UHD_PWR 0x0003
469 #define UHD_PWR_GANGED 0x0000
470 #define UHD_PWR_INDIVIDUAL 0x0001
471 #define UHD_PWR_NO_SWITCH 0x0002
472 #define UHD_COMPOUND 0x0004
473 #define UHD_OC 0x0018
474 #define UHD_OC_GLOBAL 0x0000
475 #define UHD_OC_INDIVIDUAL 0x0008
476 #define UHD_OC_NONE 0x0010
477 #define UHD_TT_THINK 0x0060
478 #define UHD_TT_THINK_8 0x0000
479 #define UHD_TT_THINK_16 0x0020
480 #define UHD_TT_THINK_24 0x0040
481 #define UHD_TT_THINK_32 0x0060
482 #define UHD_PORT_IND 0x0080
483 uByte bPwrOn2PwrGood; /* delay in 2 ms units */
484 #define UHD_PWRON_FACTOR 2
485 uByte bHubContrCurrent;
486 uByte DeviceRemovable[32]; /* max 255 ports */
487 #define UHD_NOT_REMOV(desc, i) \
488 (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
489 /* deprecated */ uByte PortPowerCtrlMask[1];
490 } UPACKED usb_hub_descriptor_t;
491 #define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
492
493 typedef struct {
494 uByte bLength;
495 uByte bDescriptorType;
496 uByte bNbrPorts;
497 #define UHD_SS_NPORTS_MAX 15
498 uWord wHubCharacteristics;
499 uByte bPwrOn2PwrGood; /* delay in 2 ms units */
500 uByte bHubContrCurrent;
501 uByte bHubHdrDecLat;
502 uWord wHubDelay; /* forward delay in nanosec */
503 uByte DeviceRemovable[2]; /* max 15 ports */
504 } UPACKED usb_hub_ss_descriptor_t;
505 #define USB_HUB_SS_DESCRIPTOR_SIZE 12
506
507 typedef struct {
508 uByte bLength;
509 uByte bDescriptorType;
510 uWord bcdUSB;
511 uByte bDeviceClass;
512 uByte bDeviceSubClass;
513 uByte bDeviceProtocol;
514 uByte bMaxPacketSize0;
515 uByte bNumConfigurations;
516 uByte bReserved;
517 } UPACKED usb_device_qualifier_t;
518 #define USB_DEVICE_QUALIFIER_SIZE 10
519
520 typedef struct {
521 uByte bLength;
522 uByte bDescriptorType;
523 uByte bmAttributes;
524 #define UOTG_SRP 0x01
525 #define UOTG_HNP 0x02
526 } UPACKED usb_otg_descriptor_t;
527
528 /* OTG feature selectors */
529 #define UOTG_B_HNP_ENABLE 3
530 #define UOTG_A_HNP_SUPPORT 4
531 #define UOTG_A_ALT_HNP_SUPPORT 5
532
533 typedef struct {
534 uByte bLength;
535 uByte bDescriptorType;
536 uByte bDebugInEndpoint;
537 uByte bDebugOutEndpoint;
538 } UPACKED usb_debug_descriptor_t;
539
540 typedef struct {
541 uWord wStatus;
542 /* Device status flags */
543 #define UDS_SELF_POWERED 0x0001
544 #define UDS_REMOTE_WAKEUP 0x0002
545 /* Endpoint status flags */
546 #define UES_HALT 0x0001
547 } UPACKED usb_status_t;
548
549 typedef struct {
550 uWord wHubStatus;
551 #define UHS_LOCAL_POWER 0x0001
552 #define UHS_OVER_CURRENT 0x0002
553 uWord wHubChange;
554 } UPACKED usb_hub_status_t;
555
556 typedef struct {
557 uWord wPortStatus;
558 #define UPS_CURRENT_CONNECT_STATUS 0x0001
559 #define UPS_PORT_ENABLED 0x0002
560 #define UPS_SUSPEND 0x0004
561 #define UPS_OVERCURRENT_INDICATOR 0x0008
562 #define UPS_RESET 0x0010
563 #define UPS_PORT_L1 0x0020
564 #define UPS_PORT_LS_GET(x) __SHIFTOUT(x, __BITS(8,5))
565 #define UPS_PORT_LS_U0 0x00
566 #define UPS_PORT_LS_U1 0x01
567 #define UPS_PORT_LS_U2 0x02
568 #define UPS_PORT_LS_U3 0x03
569 #define UPS_PORT_LS_SS_DIS 0x04
570 #define UPS_PORT_LS_RX_DET 0x05
571 #define UPS_PORT_LS_SS_INA 0x06
572 #define UPS_PORT_LS_POLL 0x07
573 #define UPS_PORT_LS_RECOVER 0x08
574 #define UPS_PORT_LS_HOT_RST 0x09
575 #define UPS_PORT_LS_COMP_MODE 0x0a
576 #define UPS_PORT_LS_LOOPBACK 0x0b
577 #define UPS_PORT_LS_RESUME 0x0f
578 #define UPS_PORT_POWER 0x0100
579 #define UPS_PORT_POWER_SS 0x0200
580 #define UPS_FULL_SPEED 0x0000 /* for completeness */
581 #define UPS_LOW_SPEED 0x0200
582 #define UPS_HIGH_SPEED 0x0400
583 #define UPS_SUPER_SPEED 0x0800
584 #define UPS_PORT_TEST 0x0800
585 #define UPS_PORT_INDICATOR 0x1000
586 uWord wPortChange;
587 #define UPS_C_CONNECT_STATUS 0x0001
588 #define UPS_C_PORT_ENABLED 0x0002
589 #define UPS_C_SUSPEND 0x0004
590 #define UPS_C_OVERCURRENT_INDICATOR 0x0008
591 #define UPS_C_PORT_RESET 0x0010
592 #define UPS_C_PORT_L1 0x0020
593 #define UPS_C_BH_PORT_RESET 0x0020
594 #define UPS_C_PORT_LINK_STATE 0x0040
595 #define UPS_C_PORT_CONFIG_ERROR 0x0080
596 } UPACKED usb_port_status_t;
597
598 /* Device class codes */
599 #define UDCLASS_IN_INTERFACE 0x00
600 #define UDCLASS_COMM 0x02
601 #define UDCLASS_HUB 0x09
602 #define UDSUBCLASS_HUB 0x00
603 #define UDPROTO_FSHUB 0x00
604 #define UDPROTO_HSHUBSTT 0x01
605 #define UDPROTO_HSHUBMTT 0x02
606 #define UDPROTO_SSHUB 0x03
607 #define UDCLASS_DIAGNOSTIC 0xdc
608 #define UDCLASS_WIRELESS 0xe0
609 #define UDSUBCLASS_RF 0x01
610 #define UDPROTO_BLUETOOTH 0x01
611 #define UDCLASS_VENDOR 0xff
612
613 /* Interface class codes */
614 #define UICLASS_UNSPEC 0x00
615
616 #define UICLASS_AUDIO 0x01
617 #define UISUBCLASS_AUDIOCONTROL 1
618 #define UISUBCLASS_AUDIOSTREAM 2
619 #define UISUBCLASS_MIDISTREAM 3
620
621 #define UICLASS_VIDEO 0x0e
622 #define UISUBCLASS_VIDEOCONTROL 1
623 #define UISUBCLASS_VIDEOSTREAMING 2
624 #define UISUBCLASS_VIDEOCOLLECTION 3
625
626 #define UICLASS_CDC 0x02 /* communication */
627 #define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
628 #define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
629 #define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
630 #define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
631 #define UISUBCLASS_CAPI_CONTROLMODEL 5
632 #define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
633 #define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
634 #define UIPROTO_CDC_NOCLASS 0 /* no class specific
635 protocol required */
636 #define UIPROTO_CDC_AT 1
637
638 #define UICLASS_HID 0x03
639 #define UISUBCLASS_BOOT 1
640 #define UIPROTO_BOOT_KEYBOARD 1
641 #define UIPROTO_MOUSE 2
642
643 #define UICLASS_PHYSICAL 0x05
644
645 #define UICLASS_IMAGE 0x06
646
647 #define UICLASS_PRINTER 0x07
648 #define UISUBCLASS_PRINTER 1
649 #define UIPROTO_PRINTER_UNI 1
650 #define UIPROTO_PRINTER_BI 2
651 #define UIPROTO_PRINTER_1284 3
652
653 #define UICLASS_MASS 0x08
654 #define UISUBCLASS_RBC 1
655 #define UISUBCLASS_SFF8020I 2
656 #define UISUBCLASS_QIC157 3
657 #define UISUBCLASS_UFI 4
658 #define UISUBCLASS_SFF8070I 5
659 #define UISUBCLASS_SCSI 6
660 #define UIPROTO_MASS_CBI_I 0
661 #define UIPROTO_MASS_CBI 1
662 #define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
663 #define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
664 #define UIPROTO_MASS_UAS 98 /* USB Attached SCSI */
665
666 #define UICLASS_HUB 0x09
667 #define UISUBCLASS_HUB 0
668 #define UIPROTO_FSHUB 0
669 #define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
670 #define UIPROTO_HSHUBMTT 1
671
672 #define UICLASS_CDC_DATA 0x0a
673 #define UISUBCLASS_DATA 0
674 #define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
675 #define UIPROTO_DATA_HDLC 0x31 /* HDLC */
676 #define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
677 #define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
678 #define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
679 #define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
680 #define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
681 #define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
682 #define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
683 #define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
684 #define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
685 #define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
686 #define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
687
688 #define UICLASS_SMARTCARD 0x0b
689
690 /*#define UICLASS_FIRM_UPD 0x0c*/
691
692 #define UICLASS_SECURITY 0x0d
693
694 #define UICLASS_DIAGNOSTIC 0xdc
695
696 #define UICLASS_WIRELESS 0xe0
697 #define UISUBCLASS_RF 0x01
698 #define UIPROTO_BLUETOOTH 0x01
699 #define UIPROTO_RNDIS 0x03
700
701 #define UICLASS_APPL_SPEC 0xfe
702 #define UISUBCLASS_FIRMWARE_DOWNLOAD 1
703 #define UISUBCLASS_IRDA 2
704 #define UIPROTO_IRDA 0
705
706 #define UICLASS_VENDOR 0xff
707
708
709 #define USB_HUB_MAX_DEPTH 5
710
711 /*
712 * Minimum time a device needs to be powered down to go through
713 * a power cycle. XXX Are these time in the spec?
714 */
715 #define USB_POWER_DOWN_TIME 200 /* ms */
716 #define USB_PORT_POWER_DOWN_TIME 100 /* ms */
717
718 #if 0
719 /* These are the values from the spec. */
720 #define USB_PORT_RESET_DELAY 10 /* ms */
721 #define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
722 #define USB_PORT_RESET_RECOVERY 10 /* ms */
723 #define USB_PORT_POWERUP_DELAY 100 /* ms */
724 #define USB_SET_ADDRESS_SETTLE 2 /* ms */
725 #define USB_RESUME_DELAY (20*5) /* ms */
726 #define USB_RESUME_WAIT 10 /* ms */
727 #define USB_RESUME_RECOVERY 10 /* ms */
728 #define USB_EXTRA_POWER_UP_TIME 0 /* ms */
729 #else
730 /* Allow for marginal (i.e. non-conforming) devices. */
731 #define USB_PORT_RESET_DELAY 50 /* ms */
732 #define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
733 #define USB_PORT_RESET_RECOVERY 250 /* ms */
734 #define USB_PORT_POWERUP_DELAY 300 /* ms */
735 #define USB_SET_ADDRESS_SETTLE 10 /* ms */
736 #define USB_RESUME_DELAY (50*5) /* ms */
737 #define USB_RESUME_WAIT 50 /* ms */
738 #define USB_RESUME_RECOVERY 50 /* ms */
739 #define USB_EXTRA_POWER_UP_TIME 20 /* ms */
740 #endif
741
742 #define USB_MIN_POWER 100 /* mA */
743 #define USB_MIN_POWER_SS 150 /* mA */
744 #define USB_MAX_POWER 500 /* mA */
745 #define USB_MAX_POWER_SS 900 /* mA */
746
747 #define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
748
749
750 #define USB_UNCONFIG_NO 0
751 #define USB_UNCONFIG_INDEX (-1)
752
753
754 /* Packet IDs */
755 #define UPID_RESERVED 0xf0
756 #define UPID_OUT 0xe1
757 #define UPID_ACK 0xd2
758 #define UPID_DATA0 0xc3
759 #define UPID_PING 0xb4
760 #define UPID_SOF 0xa5
761 #define UPID_NYET 0x96
762 #define UPID_DATA2 0x87
763 #define UPID_SPLIT 0x78
764 #define UPID_IN 0x69
765 #define UPID_NAK 0x5a
766 #define UPID_DATA1 0x4b
767 #define UPID_ERR 0x3c
768 #define UPID_PREAMBLE 0x3c
769 #define UPID_SETUP 0x2d
770 #define UPID_STALL 0x1e
771 #define UPID_MDATA 0x0f
772
773
774 /*** ioctl() related stuff ***/
775
776 struct usb_ctl_request {
777 int ucr_addr;
778 usb_device_request_t ucr_request;
779 void *ucr_data;
780 int ucr_flags;
781 #define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
782 int ucr_actlen; /* actual length transferred */
783 };
784
785 struct usb_alt_interface {
786 int uai_config_index;
787 int uai_interface_index;
788 int uai_alt_no;
789 };
790
791 #define USB_CURRENT_CONFIG_INDEX (-1)
792 #define USB_CURRENT_ALT_INDEX (-1)
793
794 struct usb_config_desc {
795 int ucd_config_index;
796 usb_config_descriptor_t ucd_desc;
797 };
798
799 struct usb_interface_desc {
800 int uid_config_index;
801 int uid_interface_index;
802 int uid_alt_index;
803 usb_interface_descriptor_t uid_desc;
804 };
805
806 struct usb_endpoint_desc {
807 int ued_config_index;
808 int ued_interface_index;
809 int ued_alt_index;
810 int ued_endpoint_index;
811 usb_endpoint_descriptor_t ued_desc;
812 };
813
814 struct usb_full_desc {
815 int ufd_config_index;
816 u_int ufd_size;
817 u_char *ufd_data;
818 };
819
820 struct usb_string_desc {
821 int usd_string_index;
822 int usd_language_id;
823 usb_string_descriptor_t usd_desc;
824 };
825
826 struct usb_ctl_report_desc {
827 int ucrd_size;
828 u_char ucrd_data[1024]; /* filled data size will vary */
829 };
830
831 typedef struct { uint32_t cookie; } usb_event_cookie_t;
832
833 #define USB_MAX_DEVNAMES 4
834 #define USB_MAX_DEVNAMELEN 16
835 struct usb_device_info {
836 uint8_t udi_bus;
837 uint8_t udi_addr; /* device address */
838 usb_event_cookie_t udi_cookie;
839 char udi_product[USB_MAX_ENCODED_STRING_LEN];
840 char udi_vendor[USB_MAX_ENCODED_STRING_LEN];
841 char udi_release[8];
842 char udi_serial[USB_MAX_ENCODED_STRING_LEN];
843 uint16_t udi_productNo;
844 uint16_t udi_vendorNo;
845 uint16_t udi_releaseNo;
846 uint8_t udi_class;
847 uint8_t udi_subclass;
848 uint8_t udi_protocol;
849 uint8_t udi_config;
850 uint8_t udi_speed;
851 #define USB_SPEED_LOW 1
852 #define USB_SPEED_FULL 2
853 #define USB_SPEED_HIGH 3
854 #define USB_SPEED_SUPER 4
855 int udi_power; /* power consumption in mA, 0 if selfpowered */
856 int udi_nports;
857 char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
858 uint8_t udi_ports[16];/* hub only: addresses of devices on ports */
859 #define USB_PORT_ENABLED 0xff
860 #define USB_PORT_SUSPENDED 0xfe
861 #define USB_PORT_POWERED 0xfd
862 #define USB_PORT_DISABLED 0xfc
863 };
864
865 /* <=3.0 had this layout of the structure */
866 struct usb_device_info_old {
867 uint8_t udi_bus;
868 uint8_t udi_addr; /* device address */
869 usb_event_cookie_t udi_cookie;
870 char udi_product[USB_MAX_STRING_LEN];
871 char udi_vendor[USB_MAX_STRING_LEN];
872 char udi_release[8];
873 uint16_t udi_productNo;
874 uint16_t udi_vendorNo;
875 uint16_t udi_releaseNo;
876 uint8_t udi_class;
877 uint8_t udi_subclass;
878 uint8_t udi_protocol;
879 uint8_t udi_config;
880 uint8_t udi_speed;
881 int udi_power; /* power consumption in mA, 0 if selfpowered */
882 int udi_nports;
883 char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
884 uint8_t udi_ports[16];/* hub only: addresses of devices on ports */
885 };
886
887 struct usb_ctl_report {
888 int ucr_report;
889 u_char ucr_data[1024]; /* filled data size will vary */
890 };
891
892 struct usb_device_stats {
893 u_long uds_requests[4]; /* indexed by transfer type UE_* */
894 };
895
896 struct usb_bulk_ra_wb_opt {
897 u_int ra_wb_buffer_size;
898 u_int ra_wb_request_size;
899 };
900
901 /* Events that can be read from /dev/usb */
902 struct usb_event {
903 int ue_type;
904 #define USB_EVENT_CTRLR_ATTACH 1
905 #define USB_EVENT_CTRLR_DETACH 2
906 #define USB_EVENT_DEVICE_ATTACH 3
907 #define USB_EVENT_DEVICE_DETACH 4
908 #define USB_EVENT_DRIVER_ATTACH 5
909 #define USB_EVENT_DRIVER_DETACH 6
910 #define USB_EVENT_IS_ATTACH(n) ((n) == USB_EVENT_CTRLR_ATTACH || (n) == USB_EVENT_DEVICE_ATTACH || (n) == USB_EVENT_DRIVER_ATTACH)
911 #define USB_EVENT_IS_DETACH(n) ((n) == USB_EVENT_CTRLR_DETACH || (n) == USB_EVENT_DEVICE_DETACH || (n) == USB_EVENT_DRIVER_DETACH)
912 struct timespec ue_time;
913 union {
914 struct {
915 int ue_bus;
916 } ue_ctrlr;
917 struct usb_device_info ue_device;
918 struct {
919 usb_event_cookie_t ue_cookie;
920 char ue_devname[16];
921 } ue_driver;
922 } u;
923 };
924
925 /* old <=3.0 compat event */
926 struct usb_event_old {
927 int ue_type;
928 struct timespec ue_time;
929 union {
930 struct {
931 int ue_bus;
932 } ue_ctrlr;
933 struct usb_device_info_old ue_device;
934 struct {
935 usb_event_cookie_t ue_cookie;
936 char ue_devname[16];
937 } ue_driver;
938 } u;
939 };
940
941
942 /* USB controller */
943 #define USB_REQUEST _IOWR('U', 1, struct usb_ctl_request)
944 #define USB_SETDEBUG _IOW ('U', 2, int)
945 #define USB_DISCOVER _IO ('U', 3)
946 #define USB_DEVICEINFO _IOWR('U', 4, struct usb_device_info)
947 #define USB_DEVICEINFO_OLD _IOWR('U', 4, struct usb_device_info_old)
948 #define USB_DEVICESTATS _IOR ('U', 5, struct usb_device_stats)
949
950 /* Generic HID device */
951 #define USB_GET_REPORT_DESC _IOR ('U', 21, struct usb_ctl_report_desc)
952 #define USB_SET_IMMED _IOW ('U', 22, int)
953 #define USB_GET_REPORT _IOWR('U', 23, struct usb_ctl_report)
954 #define USB_SET_REPORT _IOW ('U', 24, struct usb_ctl_report)
955 #define USB_GET_REPORT_ID _IOR ('U', 25, int)
956
957 /* Generic USB device */
958 #define USB_GET_CONFIG _IOR ('U', 100, int)
959 #define USB_SET_CONFIG _IOW ('U', 101, int)
960 #define USB_GET_ALTINTERFACE _IOWR('U', 102, struct usb_alt_interface)
961 #define USB_SET_ALTINTERFACE _IOWR('U', 103, struct usb_alt_interface)
962 #define USB_GET_NO_ALT _IOWR('U', 104, struct usb_alt_interface)
963 #define USB_GET_DEVICE_DESC _IOR ('U', 105, usb_device_descriptor_t)
964 #define USB_GET_CONFIG_DESC _IOWR('U', 106, struct usb_config_desc)
965 #define USB_GET_INTERFACE_DESC _IOWR('U', 107, struct usb_interface_desc)
966 #define USB_GET_ENDPOINT_DESC _IOWR('U', 108, struct usb_endpoint_desc)
967 #define USB_GET_FULL_DESC _IOWR('U', 109, struct usb_full_desc)
968 #define USB_GET_STRING_DESC _IOWR('U', 110, struct usb_string_desc)
969 #define USB_DO_REQUEST _IOWR('U', 111, struct usb_ctl_request)
970 #define USB_GET_DEVICEINFO _IOR ('U', 112, struct usb_device_info)
971 #define USB_GET_DEVICEINFO_OLD _IOR ('U', 112, struct usb_device_info_old)
972 #define USB_SET_SHORT_XFER _IOW ('U', 113, int)
973 #define USB_SET_TIMEOUT _IOW ('U', 114, int)
974 #define USB_SET_BULK_RA _IOW ('U', 115, int)
975 #define USB_SET_BULK_WB _IOW ('U', 116, int)
976 #define USB_SET_BULK_RA_OPT _IOW ('U', 117, struct usb_bulk_ra_wb_opt)
977 #define USB_SET_BULK_WB_OPT _IOW ('U', 118, struct usb_bulk_ra_wb_opt)
978
979 /* Modem device */
980 #define USB_GET_CM_OVER_DATA _IOR ('U', 130, int)
981 #define USB_SET_CM_OVER_DATA _IOW ('U', 131, int)
982
983 #endif /* _USB_H_ */
984