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