usb.h revision 1.9 1 /* $NetBSD: usb.h,v 1.9 1998/12/28 02:20:28 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40
41 #ifndef _USB_H_
42 #define _USB_H_
43
44 #include <sys/types.h>
45 #if defined(__NetBSD__)
46 #include <sys/ioctl.h>
47 #endif
48
49 #if defined(__NetBSD__)
50 #if defined(_KERNEL)
51 #include <dev/usb/usb_port.h>
52 #endif /* _KERNEL */
53
54 #elif defined(__FreeBSD__)
55 #include <sys/malloc.h>
56
57 #if defined(KERNEL)
58 MALLOC_DECLARE(M_USB);
59 MALLOC_DECLARE(M_USBDEV);
60
61 #include <dev/usb/usb_port.h>
62 #endif /* KERNEL */
63 #endif /* __FreeBSD__ */
64
65
66 #define USB_MAX_DEVICES 128
67 #define USB_START_ADDR 0
68
69 #define USB_CONTROL_ENDPOINT 0
70 #define USB_MAX_ENDPOINTS 16
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 16 bit
76 * values.
77 */
78 typedef u_int8_t uByte;
79 typedef u_int8_t uWord[2];
80 #define UGETW(w) ((w)[0] | ((w)[1] << 8))
81 #define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
82 #define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
83 /*
84 * On little-endian machines that can handle unanliged accesses
85 * (e.g. i386) these macros can be replaced by the following.
86 */
87 #if 0
88 #define UGETW(w) (*(u_int16_t *)(w))
89 #define USETW(w,v) (*(u_int16_t *)(w) = (v))
90 #endif
91
92 typedef struct {
93 uByte bmRequestType;
94 uByte bRequest;
95 uWord wValue;
96 uWord wIndex;
97 uWord wLength;
98 } usb_device_request_t;
99
100 #define UT_WRITE 0x00
101 #define UT_READ 0x80
102 #define UT_STANDARD 0x00
103 #define UT_CLASS 0x20
104 #define UT_VENDOR 0x40
105 #define UT_DEVICE 0x00
106 #define UT_INTERFACE 0x01
107 #define UT_ENDPOINT 0x02
108 #define UT_OTHER 0x03
109
110 #define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
111 #define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
112 #define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
113 #define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
114 #define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
115 #define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
116 #define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
117 #define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
118 #define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
119 #define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
120 #define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
121 #define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
122
123 /* Requests */
124 #define UR_GET_STATUS 0x00
125 #define UR_CLEAR_FEATURE 0x01
126 #define UR_SET_FEATURE 0x03
127 #define UR_SET_ADDRESS 0x05
128 #define UR_GET_DESCRIPTOR 0x06
129 #define UDESC_DEVICE 1
130 #define UDESC_CONFIG 2
131 #define UDESC_STRING 3
132 #define UDESC_INTERFACE 4
133 #define UDESC_ENDPOINT 5
134 #define UR_SET_DESCRIPTOR 0x07
135 #define UR_GET_CONFIG 0x08
136 #define UR_SET_CONFIG 0x09
137 #define UR_GET_INTERFACE 0x0a
138 #define UR_SET_INTERFACE 0x0b
139 #define UR_SYNCH_FRAME 0x0c
140
141 /* Feature numbers */
142 #define UF_ENDPOINT_HALT 0
143 #define UF_DEVICE_REMOTE_WAKEUP 1
144
145 #define USB_MAX_IPACKET 8 /* maximum size of the initial packet */
146
147 typedef struct {
148 uByte bLength;
149 uByte bDescriptorType;
150 uByte bDescriptorSubtype;
151 } usb_descriptor_t;
152
153 typedef struct {
154 uByte bLength;
155 uByte bDescriptorType;
156 uWord bcdUSB;
157 uByte bDeviceClass;
158 uByte bDeviceSubClass;
159 uByte bDeviceProtocol;
160 uByte bMaxPacketSize;
161 /* The fields below are not part of the initial descriptor. */
162 uWord idVendor;
163 uWord idProduct;
164 uWord bcdDevice;
165 uByte iManufacturer;
166 uByte iProduct;
167 uByte iSerialNumber;
168 uByte bNumConfigurations;
169 } usb_device_descriptor_t;
170 #define USB_DEVICE_DESCRIPTOR_SIZE 18
171
172 typedef struct {
173 uByte bLength;
174 uByte bDescriptorType;
175 uWord wTotalLength;
176 uByte bNumInterface;
177 uByte bConfigurationValue;
178 uByte iConfiguration;
179 uByte bmAttributes;
180 #define UC_BUS_POWERED 0x80
181 #define UC_SELF_POWERED 0x40
182 #define UC_REMOTE_WAKEUP 0x20
183 uByte bMaxPower; /* max current in 2 mA units */
184 #define UC_POWER_FACTOR 2
185 } usb_config_descriptor_t;
186 #define USB_CONFIG_DESCRIPTOR_SIZE 9
187
188 typedef struct {
189 uByte bLength;
190 uByte bDescriptorType;
191 uByte bInterfaceNumber;
192 uByte bAlternateSetting;
193 uByte bNumEndpoints;
194 uByte bInterfaceClass;
195 uByte bInterfaceSubClass;
196 uByte bInterfaceProtocol;
197 uByte iInterface;
198 } usb_interface_descriptor_t;
199 #define USB_INTERFACE_DESCRIPTOR_SIZE 9
200
201 typedef struct {
202 uByte bLength;
203 uByte bDescriptorType;
204 uByte bEndpointAddress;
205 #define UE_IN 0x80
206 #define UE_OUT 0x00
207 #define UE_ADDR 0x0f
208 #define UE_GET_ADDR(a) ((a) & UE_ADDR)
209 #define UE_GET_IN(a) (((a) >> 7) & 1)
210 uByte bmAttributes;
211 #define UE_CONTROL 0x00
212 #define UE_ISOCHRONOUS 0x01
213 #define UE_BULK 0x02
214 #define UE_INTERRUPT 0x03
215 #define UE_XFERTYPE 0x03
216 uWord wMaxPacketSize;
217 uByte bInterval;
218 } usb_endpoint_descriptor_t;
219 #define USB_ENDPOINT_DESCRIPTOR_SIZE 7
220
221 typedef struct {
222 uByte bLength;
223 uByte bDescriptorType;
224 uWord bString[127];
225 } usb_string_descriptor_t;
226 #define USB_MAX_STRING_LEN 128
227
228 /* Hub specific request */
229 #define UR_GET_BUS_STATE 0x02
230
231 /* Hub features */
232 #define UHF_C_HUB_LOCAL_POWER 0
233 #define UHF_C_HUB_OVER_CURRENT 1
234 #define UHF_PORT_CONNECTION 0
235 #define UHF_PORT_ENABLE 1
236 #define UHF_PORT_SUSPEND 2
237 #define UHF_PORT_OVER_CURRENT 3
238 #define UHF_PORT_RESET 4
239 #define UHF_PORT_POWER 8
240 #define UHF_PORT_LOW_SPEED 9
241 #define UHF_C_PORT_CONNECTION 16
242 #define UHF_C_PORT_ENABLE 17
243 #define UHF_C_PORT_SUSPEND 18
244 #define UHF_C_PORT_OVER_CURRENT 19
245 #define UHF_C_PORT_RESET 20
246
247 typedef struct {
248 uByte bDescLength;
249 uByte bDescriptorType;
250 uByte bNbrPorts;
251 uWord wHubCharacteristics;
252 #define UHD_PWR 0x03
253 #define UHD_PWR_GANGED 0x00
254 #define UHD_PWR_INDIVIDUAL 0x01
255 #define UHD_PWR_NO_SWITCH 0x02
256 #define UHD_COMPOUND 0x04
257 #define UHD_OC 0x18
258 #define UHD_OC_GLOBAL 0x00
259 #define UHD_OC_INDIVIDUAL 0x08
260 #define UHD_OC_NONE 0x10
261 uByte bPwrOn2PwrGood; /* delay in 2 ms units */
262 #define UHD_PWRON_FACTOR 2
263 uByte bHubContrCurrent;
264 uByte DeviceRemovable[32]; /* max 255 ports */
265 #define UHD_NOT_REMOV(desc, i) \
266 (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
267 /* deprecated uByte PortPowerCtrlMask[]; */
268 } usb_hub_descriptor_t;
269 #define USB_HUB_DESCRIPTOR_SIZE 8
270
271 typedef struct {
272 uWord wStatus;
273 /* Device status flags */
274 #define UDS_SELF_POWERED 0x0001
275 #define UDS_REMOTE_WAKEUP 0x0002
276 /* Endpoint status flags */
277 #define UES_HALT 0x0001
278 } usb_status_t;
279
280 typedef struct {
281 uWord wHubStatus;
282 #define UHS_LOCAL_POWER 0x0001
283 #define UHS_OVER_CURRENT 0x0002
284 uWord wHubChange;
285 } usb_hub_status_t;
286
287 typedef struct {
288 uWord wPortStatus;
289 #define UPS_CURRENT_CONNECT_STATUS 0x0001
290 #define UPS_PORT_ENABLED 0x0002
291 #define UPS_SUSPEND 0x0004
292 #define UPS_OVERCURRENT_INDICATOR 0x0008
293 #define UPS_RESET 0x0010
294 #define UPS_PORT_POWER 0x0100
295 #define UPS_LOW_SPEED 0x0200
296 uWord wPortChange;
297 #define UPS_C_CONNECT_STATUS 0x0001
298 #define UPS_C_PORT_ENABLED 0x0002
299 #define UPS_C_SUSPEND 0x0004
300 #define UPS_C_OVERCURRENT_INDICATOR 0x0008
301 #define UPS_C_PORT_RESET 0x0010
302 } usb_port_status_t;
303
304 #define UDESC_CS_DEVICE 0x21
305 #define UDESC_CS_CONFIG 0x22
306 #define UDESC_CS_STRING 0x23
307 #define UDESC_CS_INTERFACE 0x24
308 #define UDESC_CS_ENDPOINT 0x25
309
310 #define UDESC_HUB 0x29
311
312 #define UCLASS_UNSPEC 0
313 #define UCLASS_AUDIO 1
314 #define USUBCLASS_AUDIOCONTROL 1
315 #define USUBCLASS_AUDIOSTREAM 2
316 #define UCLASS_CDC 2
317 #define USUBCLASS_ABSTRACT_CONTROL_MODEL 2
318 #define UPROTO_CDC_AT 1
319 #define UCLASS_HID 3
320 #define USUBCLASS_BOOT 1
321 #define UCLASS_PRINTER 7
322 #define USUBCLASS_PRINTER 1
323 #define UPROTO_PRINTER_UNI 1
324 #define UPROTO_PRINTER_BI 2
325 #define UCLASS_HUB 9
326 #define USUBCLASS_HUB 0
327 #define UCLASS_DATA 10
328
329 #define USB_HUB_MAX_DEPTH 5
330
331 /*
332 * Minimum time a device needs to be powered down to go through
333 * a power cycle. XXX Is this time in the spec?
334 */
335 #define USB_POWER_DOWN_TIME 1000 /* ms */
336 #define USB_PORT_POWER_DOWN_TIME 100 /* ms */
337
338 #if 0
339 /* These are the values from the spec. */
340 #define USB_PORT_RESET_DELAY 10 /* ms */
341 #define USB_PORT_RESET_SETTLE 10 /* ms */
342 #define USB_PORT_POWERUP_DELAY 100 /* ms */
343 #define USB_SET_ADDRESS_SETTLE 2 /* ms */
344 #else
345 /* Allow for marginal (i.e. non-conforming) devices. */
346 #define USB_PORT_RESET_DELAY 10 /* ms */
347 #define USB_PORT_RESET_RECOVERY 20 /* ms */
348 #define USB_PORT_POWERUP_DELAY 200 /* ms */
349 #define USB_SET_ADDRESS_SETTLE 10 /* ms */
350 #endif
351
352 #define USB_MIN_POWER 100 /* mA */
353 #define USB_MAX_POWER 500 /* mA */
354
355
356 #define USB_RESET_DELAY 100 /* ms XXX?*/
357 #define USB_RESUME_DELAY 10 /* ms XXX?*/
358
359 /*** ioctl() related stuff ***/
360
361 struct usb_ctl_request {
362 int addr;
363 usb_device_request_t request;
364 void *data;
365 };
366
367 struct usb_alt_interface {
368 int config_index;
369 int interface_index;
370 int alt_no;
371 };
372
373 #define USB_CURRENT_CONFIG_INDEX (-1)
374 #define USB_CURRENT_ALT_INDEX (-1)
375
376 struct usb_config_desc {
377 int config_index;
378 usb_config_descriptor_t desc;
379 };
380
381 struct usb_interface_desc {
382 int config_index;
383 int interface_index;
384 int alt_index;
385 usb_interface_descriptor_t desc;
386 };
387
388 struct usb_endpoint_desc {
389 int config_index;
390 int interface_index;
391 int alt_index;
392 int endpoint_index;
393 usb_endpoint_descriptor_t desc;
394 };
395
396 struct usb_full_desc {
397 int config_index;
398 u_int size;
399 u_char *data;
400 };
401
402 struct usb_string_desc {
403 int string_index;
404 int language_id;
405 usb_string_descriptor_t desc;
406 };
407
408 struct usb_ctl_report_desc {
409 int size;
410 u_char data[1024]; /* filled data size will vary */
411 };
412
413 struct usb_device_info {
414 uByte addr; /* device address */
415 char product[USB_MAX_STRING_LEN];
416 char vendor[USB_MAX_STRING_LEN];
417 char revision[8];
418 uByte class;
419 uByte config;
420 uByte lowspeed;
421 int power; /* power consumption in mA, 0 if selfpowered */
422 int nports;
423 uByte ports[16]; /* hub only: addresses of devices on ports */
424 #define USB_PORT_ENABLED 0xff
425 #define USB_PORT_SUSPENDED 0xfe
426 #define USB_PORT_POWERED 0xfd
427 #define USB_PORT_DISABLED 0xfc
428 };
429
430 struct usb_ctl_report {
431 int report;
432 u_char data[1024]; /* filled data size will vary */
433 };
434
435 struct usb_device_stats {
436 u_long requests[4]; /* indexed by transfer type UE_* */
437 };
438
439 /* USB controller */
440 #define USB_REQUEST _IOWR('U', 1, struct usb_ctl_request)
441 #define USB_SETDEBUG _IOW ('U', 2, int)
442 #define USB_DISCOVER _IO ('U', 3)
443 #define USB_DEVICEINFO _IOWR('U', 4, struct usb_device_info)
444 #define USB_DEVICESTATS _IOR ('U', 5, struct usb_device_stats)
445
446 /* Generic HID device */
447 #define USB_GET_REPORT_DESC _IOR ('U', 21, struct usb_ctl_report_desc)
448 #define USB_SET_IMMED _IOW ('U', 22, int)
449 #define USB_GET_REPORT _IOWR('U', 23, struct usb_ctl_report)
450
451 /* Generic USB device */
452 #define USB_GET_CONFIG _IOR ('U', 100, int)
453 #define USB_SET_CONFIG _IOW ('U', 101, int)
454 #define USB_GET_ALTINTERFACE _IOWR('U', 102, struct usb_alt_interface)
455 #define USB_SET_ALTINTERFACE _IOWR('U', 103, struct usb_alt_interface)
456 #define USB_GET_NO_ALT _IOWR('U', 104, struct usb_alt_interface)
457 #define USB_GET_DEVICE_DESC _IOR ('U', 105, usb_device_descriptor_t)
458 #define USB_GET_CONFIG_DESC _IOWR('U', 106, struct usb_config_desc)
459 #define USB_GET_INTERFACE_DESC _IOWR('U', 107, struct usb_interface_desc)
460 #define USB_GET_ENDPOINT_DESC _IOWR('U', 108, struct usb_endpoint_desc)
461 #define USB_GET_FULL_DESC _IOWR('U', 109, struct usb_full_desc)
462 #define USB_GET_STRING_DESC _IOWR('U', 110, struct usb_string_desc)
463 #define USB_DO_REQUEST _IOWR('U', 111, struct usb_ctl_request)
464 #define USB_GET_DEVICEINFO _IOR ('U', 112, struct usb_device_info)
465
466 #endif /* _USB_H_ */
467