usb.h revision 1.10 1 /* $NetBSD: usb.h,v 1.10 1998/12/28 03:27:00 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 #define USB_FRAMES_PER_SECOND 1000
73
74 /*
75 * The USB records contain some unaligned little-endian word
76 * components. The U[SG]ETW macros take care of both the alignment
77 * and endian problem and should always be used to access 16 bit
78 * values.
79 */
80 typedef u_int8_t uByte;
81 typedef u_int8_t uWord[2];
82 #define UGETW(w) ((w)[0] | ((w)[1] << 8))
83 #define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
84 #define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
85 /*
86 * On little-endian machines that can handle unanliged accesses
87 * (e.g. i386) these macros can be replaced by the following.
88 */
89 #if 0
90 #define UGETW(w) (*(u_int16_t *)(w))
91 #define USETW(w,v) (*(u_int16_t *)(w) = (v))
92 #endif
93
94 typedef struct {
95 uByte bmRequestType;
96 uByte bRequest;
97 uWord wValue;
98 uWord wIndex;
99 uWord wLength;
100 } usb_device_request_t;
101
102 #define UT_WRITE 0x00
103 #define UT_READ 0x80
104 #define UT_STANDARD 0x00
105 #define UT_CLASS 0x20
106 #define UT_VENDOR 0x40
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_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
122 #define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
123 #define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
124
125 /* Requests */
126 #define UR_GET_STATUS 0x00
127 #define UR_CLEAR_FEATURE 0x01
128 #define UR_SET_FEATURE 0x03
129 #define UR_SET_ADDRESS 0x05
130 #define UR_GET_DESCRIPTOR 0x06
131 #define UDESC_DEVICE 1
132 #define UDESC_CONFIG 2
133 #define UDESC_STRING 3
134 #define UDESC_INTERFACE 4
135 #define UDESC_ENDPOINT 5
136 #define UR_SET_DESCRIPTOR 0x07
137 #define UR_GET_CONFIG 0x08
138 #define UR_SET_CONFIG 0x09
139 #define UR_GET_INTERFACE 0x0a
140 #define UR_SET_INTERFACE 0x0b
141 #define UR_SYNCH_FRAME 0x0c
142
143 /* Feature numbers */
144 #define UF_ENDPOINT_HALT 0
145 #define UF_DEVICE_REMOTE_WAKEUP 1
146
147 #define USB_MAX_IPACKET 8 /* maximum size of the initial packet */
148
149 typedef struct {
150 uByte bLength;
151 uByte bDescriptorType;
152 uByte bDescriptorSubtype;
153 } usb_descriptor_t;
154
155 typedef struct {
156 uByte bLength;
157 uByte bDescriptorType;
158 uWord bcdUSB;
159 uByte bDeviceClass;
160 uByte bDeviceSubClass;
161 uByte bDeviceProtocol;
162 uByte bMaxPacketSize;
163 /* The fields below are not part of the initial descriptor. */
164 uWord idVendor;
165 uWord idProduct;
166 uWord bcdDevice;
167 uByte iManufacturer;
168 uByte iProduct;
169 uByte iSerialNumber;
170 uByte bNumConfigurations;
171 } usb_device_descriptor_t;
172 #define USB_DEVICE_DESCRIPTOR_SIZE 18
173
174 typedef struct {
175 uByte bLength;
176 uByte bDescriptorType;
177 uWord wTotalLength;
178 uByte bNumInterface;
179 uByte bConfigurationValue;
180 uByte iConfiguration;
181 uByte bmAttributes;
182 #define UC_BUS_POWERED 0x80
183 #define UC_SELF_POWERED 0x40
184 #define UC_REMOTE_WAKEUP 0x20
185 uByte bMaxPower; /* max current in 2 mA units */
186 #define UC_POWER_FACTOR 2
187 } usb_config_descriptor_t;
188 #define USB_CONFIG_DESCRIPTOR_SIZE 9
189
190 typedef struct {
191 uByte bLength;
192 uByte bDescriptorType;
193 uByte bInterfaceNumber;
194 uByte bAlternateSetting;
195 uByte bNumEndpoints;
196 uByte bInterfaceClass;
197 uByte bInterfaceSubClass;
198 uByte bInterfaceProtocol;
199 uByte iInterface;
200 } usb_interface_descriptor_t;
201 #define USB_INTERFACE_DESCRIPTOR_SIZE 9
202
203 typedef struct {
204 uByte bLength;
205 uByte bDescriptorType;
206 uByte bEndpointAddress;
207 #define UE_IN 0x80
208 #define UE_OUT 0x00
209 #define UE_ADDR 0x0f
210 #define UE_GET_ADDR(a) ((a) & UE_ADDR)
211 #define UE_GET_IN(a) (((a) >> 7) & 1)
212 uByte bmAttributes;
213 #define UE_XFERTYPE 0x03
214 #define UE_CONTROL 0x00
215 #define UE_ISOCHRONOUS 0x01
216 #define UE_BULK 0x02
217 #define UE_INTERRUPT 0x03
218 #define UE_ISO_TYPE 0x0c
219 #define UE_ISO_ASYNC 0x04
220 #define UE_ISO_ADAPT 0x08
221 #define UE_ISO_SYNC 0x0c
222 uWord wMaxPacketSize;
223 uByte bInterval;
224 } usb_endpoint_descriptor_t;
225 #define USB_ENDPOINT_DESCRIPTOR_SIZE 7
226
227 typedef struct {
228 uByte bLength;
229 uByte bDescriptorType;
230 uWord bString[127];
231 } usb_string_descriptor_t;
232 #define USB_MAX_STRING_LEN 128
233
234 /* Hub specific request */
235 #define UR_GET_BUS_STATE 0x02
236
237 /* Hub features */
238 #define UHF_C_HUB_LOCAL_POWER 0
239 #define UHF_C_HUB_OVER_CURRENT 1
240 #define UHF_PORT_CONNECTION 0
241 #define UHF_PORT_ENABLE 1
242 #define UHF_PORT_SUSPEND 2
243 #define UHF_PORT_OVER_CURRENT 3
244 #define UHF_PORT_RESET 4
245 #define UHF_PORT_POWER 8
246 #define UHF_PORT_LOW_SPEED 9
247 #define UHF_C_PORT_CONNECTION 16
248 #define UHF_C_PORT_ENABLE 17
249 #define UHF_C_PORT_SUSPEND 18
250 #define UHF_C_PORT_OVER_CURRENT 19
251 #define UHF_C_PORT_RESET 20
252
253 typedef struct {
254 uByte bDescLength;
255 uByte bDescriptorType;
256 uByte bNbrPorts;
257 uWord wHubCharacteristics;
258 #define UHD_PWR 0x03
259 #define UHD_PWR_GANGED 0x00
260 #define UHD_PWR_INDIVIDUAL 0x01
261 #define UHD_PWR_NO_SWITCH 0x02
262 #define UHD_COMPOUND 0x04
263 #define UHD_OC 0x18
264 #define UHD_OC_GLOBAL 0x00
265 #define UHD_OC_INDIVIDUAL 0x08
266 #define UHD_OC_NONE 0x10
267 uByte bPwrOn2PwrGood; /* delay in 2 ms units */
268 #define UHD_PWRON_FACTOR 2
269 uByte bHubContrCurrent;
270 uByte DeviceRemovable[32]; /* max 255 ports */
271 #define UHD_NOT_REMOV(desc, i) \
272 (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
273 /* deprecated uByte PortPowerCtrlMask[]; */
274 } usb_hub_descriptor_t;
275 #define USB_HUB_DESCRIPTOR_SIZE 8
276
277 typedef struct {
278 uWord wStatus;
279 /* Device status flags */
280 #define UDS_SELF_POWERED 0x0001
281 #define UDS_REMOTE_WAKEUP 0x0002
282 /* Endpoint status flags */
283 #define UES_HALT 0x0001
284 } usb_status_t;
285
286 typedef struct {
287 uWord wHubStatus;
288 #define UHS_LOCAL_POWER 0x0001
289 #define UHS_OVER_CURRENT 0x0002
290 uWord wHubChange;
291 } usb_hub_status_t;
292
293 typedef struct {
294 uWord wPortStatus;
295 #define UPS_CURRENT_CONNECT_STATUS 0x0001
296 #define UPS_PORT_ENABLED 0x0002
297 #define UPS_SUSPEND 0x0004
298 #define UPS_OVERCURRENT_INDICATOR 0x0008
299 #define UPS_RESET 0x0010
300 #define UPS_PORT_POWER 0x0100
301 #define UPS_LOW_SPEED 0x0200
302 uWord wPortChange;
303 #define UPS_C_CONNECT_STATUS 0x0001
304 #define UPS_C_PORT_ENABLED 0x0002
305 #define UPS_C_SUSPEND 0x0004
306 #define UPS_C_OVERCURRENT_INDICATOR 0x0008
307 #define UPS_C_PORT_RESET 0x0010
308 } usb_port_status_t;
309
310 #define UDESC_CS_DEVICE 0x21
311 #define UDESC_CS_CONFIG 0x22
312 #define UDESC_CS_STRING 0x23
313 #define UDESC_CS_INTERFACE 0x24
314 #define UDESC_CS_ENDPOINT 0x25
315
316 #define UDESC_HUB 0x29
317
318 #define UCLASS_UNSPEC 0
319 #define UCLASS_AUDIO 1
320 #define USUBCLASS_AUDIOCONTROL 1
321 #define USUBCLASS_AUDIOSTREAM 2
322 #define UCLASS_CDC 2
323 #define USUBCLASS_ABSTRACT_CONTROL_MODEL 2
324 #define UPROTO_CDC_AT 1
325 #define UCLASS_HID 3
326 #define USUBCLASS_BOOT 1
327 #define UCLASS_PRINTER 7
328 #define USUBCLASS_PRINTER 1
329 #define UPROTO_PRINTER_UNI 1
330 #define UPROTO_PRINTER_BI 2
331 #define UCLASS_HUB 9
332 #define USUBCLASS_HUB 0
333 #define UCLASS_DATA 10
334
335 #define USB_HUB_MAX_DEPTH 5
336
337 /*
338 * Minimum time a device needs to be powered down to go through
339 * a power cycle. XXX Is this time in the spec?
340 */
341 #define USB_POWER_DOWN_TIME 1000 /* ms */
342 #define USB_PORT_POWER_DOWN_TIME 100 /* ms */
343
344 #if 0
345 /* These are the values from the spec. */
346 #define USB_PORT_RESET_DELAY 10 /* ms */
347 #define USB_PORT_RESET_SETTLE 10 /* ms */
348 #define USB_PORT_POWERUP_DELAY 100 /* ms */
349 #define USB_SET_ADDRESS_SETTLE 2 /* ms */
350 #else
351 /* Allow for marginal (i.e. non-conforming) devices. */
352 #define USB_PORT_RESET_DELAY 10 /* ms */
353 #define USB_PORT_RESET_RECOVERY 20 /* ms */
354 #define USB_PORT_POWERUP_DELAY 200 /* ms */
355 #define USB_SET_ADDRESS_SETTLE 10 /* ms */
356 #endif
357
358 #define USB_MIN_POWER 100 /* mA */
359 #define USB_MAX_POWER 500 /* mA */
360
361
362 #define USB_RESET_DELAY 100 /* ms XXX?*/
363 #define USB_RESUME_DELAY 10 /* ms XXX?*/
364
365 /*** ioctl() related stuff ***/
366
367 struct usb_ctl_request {
368 int addr;
369 usb_device_request_t request;
370 void *data;
371 };
372
373 struct usb_alt_interface {
374 int config_index;
375 int interface_index;
376 int alt_no;
377 };
378
379 #define USB_CURRENT_CONFIG_INDEX (-1)
380 #define USB_CURRENT_ALT_INDEX (-1)
381
382 struct usb_config_desc {
383 int config_index;
384 usb_config_descriptor_t desc;
385 };
386
387 struct usb_interface_desc {
388 int config_index;
389 int interface_index;
390 int alt_index;
391 usb_interface_descriptor_t desc;
392 };
393
394 struct usb_endpoint_desc {
395 int config_index;
396 int interface_index;
397 int alt_index;
398 int endpoint_index;
399 usb_endpoint_descriptor_t desc;
400 };
401
402 struct usb_full_desc {
403 int config_index;
404 u_int size;
405 u_char *data;
406 };
407
408 struct usb_string_desc {
409 int string_index;
410 int language_id;
411 usb_string_descriptor_t desc;
412 };
413
414 struct usb_ctl_report_desc {
415 int size;
416 u_char data[1024]; /* filled data size will vary */
417 };
418
419 struct usb_device_info {
420 uByte addr; /* device address */
421 char product[USB_MAX_STRING_LEN];
422 char vendor[USB_MAX_STRING_LEN];
423 char revision[8];
424 uByte class;
425 uByte config;
426 uByte lowspeed;
427 int power; /* power consumption in mA, 0 if selfpowered */
428 int nports;
429 uByte ports[16]; /* hub only: addresses of devices on ports */
430 #define USB_PORT_ENABLED 0xff
431 #define USB_PORT_SUSPENDED 0xfe
432 #define USB_PORT_POWERED 0xfd
433 #define USB_PORT_DISABLED 0xfc
434 };
435
436 struct usb_ctl_report {
437 int report;
438 u_char data[1024]; /* filled data size will vary */
439 };
440
441 struct usb_device_stats {
442 u_long requests[4]; /* indexed by transfer type UE_* */
443 };
444
445 /* USB controller */
446 #define USB_REQUEST _IOWR('U', 1, struct usb_ctl_request)
447 #define USB_SETDEBUG _IOW ('U', 2, int)
448 #define USB_DISCOVER _IO ('U', 3)
449 #define USB_DEVICEINFO _IOWR('U', 4, struct usb_device_info)
450 #define USB_DEVICESTATS _IOR ('U', 5, struct usb_device_stats)
451
452 /* Generic HID device */
453 #define USB_GET_REPORT_DESC _IOR ('U', 21, struct usb_ctl_report_desc)
454 #define USB_SET_IMMED _IOW ('U', 22, int)
455 #define USB_GET_REPORT _IOWR('U', 23, struct usb_ctl_report)
456
457 /* Generic USB device */
458 #define USB_GET_CONFIG _IOR ('U', 100, int)
459 #define USB_SET_CONFIG _IOW ('U', 101, int)
460 #define USB_GET_ALTINTERFACE _IOWR('U', 102, struct usb_alt_interface)
461 #define USB_SET_ALTINTERFACE _IOWR('U', 103, struct usb_alt_interface)
462 #define USB_GET_NO_ALT _IOWR('U', 104, struct usb_alt_interface)
463 #define USB_GET_DEVICE_DESC _IOR ('U', 105, usb_device_descriptor_t)
464 #define USB_GET_CONFIG_DESC _IOWR('U', 106, struct usb_config_desc)
465 #define USB_GET_INTERFACE_DESC _IOWR('U', 107, struct usb_interface_desc)
466 #define USB_GET_ENDPOINT_DESC _IOWR('U', 108, struct usb_endpoint_desc)
467 #define USB_GET_FULL_DESC _IOWR('U', 109, struct usb_full_desc)
468 #define USB_GET_STRING_DESC _IOWR('U', 110, struct usb_string_desc)
469 #define USB_DO_REQUEST _IOWR('U', 111, struct usb_ctl_request)
470 #define USB_GET_DEVICEINFO _IOR ('U', 112, struct usb_device_info)
471
472 #endif /* _USB_H_ */
473