usb.h revision 1.5 1 /* $NetBSD: usb.h,v 1.5 1998/12/02 22:47:20 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 #include <sys/ioctl.h>
46
47 #define USB_MAX_DEVICES 128
48 #define USB_START_ADDR 0
49
50 #define USB_CONTROL_ENDPOINT 0
51 #define USB_MAX_ENDPOINTS 16
52
53 /*
54 * The USB records contain some unaligned little-endian word
55 * components. The U[SG]ETW macros take care of both the alignment
56 * and endian problem and should always be used to access 16 bit
57 * values.
58 */
59 typedef u_int8_t uByte;
60 typedef u_int8_t uWord[2];
61 #define UGETW(w) ((w)[0] | ((w)[1] << 8))
62 #define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
63 #define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
64 /*
65 * On little-endian machines that can handle unanliged accesses
66 * (e.g. i386) these macros can be replaced by the following.
67 */
68 #if 0
69 #define UGETW(w) (*(u_int16_t *)(w))
70 #define USETW(w,v) (*(u_int16_t *)(w) = (v))
71 #endif
72
73 typedef struct {
74 uByte bmRequestType;
75 uByte bRequest;
76 uWord wValue;
77 uWord wIndex;
78 uWord wLength;
79 } usb_device_request_t;
80
81 #define UT_WRITE 0x00
82 #define UT_READ 0x80
83 #define UT_STANDARD 0x00
84 #define UT_CLASS 0x20
85 #define UT_VENDOR 0x40
86 #define UT_DEVICE 0x00
87 #define UT_INTERFACE 0x01
88 #define UT_ENDPOINT 0x02
89 #define UT_OTHER 0x03
90
91 #define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
92 #define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
93 #define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
94 #define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
95 #define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
96 #define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
97 #define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
98 #define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
99 #define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
100 #define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
101 #define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
102 #define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
103
104 /* Requests */
105 #define UR_GET_STATUS 0x00
106 #define UR_CLEAR_FEATURE 0x01
107 #define UR_SET_FEATURE 0x03
108 #define UR_SET_ADDRESS 0x05
109 #define UR_GET_DESCRIPTOR 0x06
110 #define UDESC_DEVICE 1
111 #define UDESC_CONFIG 2
112 #define UDESC_STRING 3
113 #define UDESC_INTERFACE 4
114 #define UDESC_ENDPOINT 5
115 #define UR_SET_DESCRIPTOR 0x07
116 #define UR_GET_CONFIG 0x08
117 #define UR_SET_CONFIG 0x09
118 #define UR_GET_INTERFACE 0x0a
119 #define UR_SET_INTERFACE 0x0b
120 #define UR_SYNCH_FRAME 0x0c
121
122 /* Feature numbers */
123 #define UF_ENDPOINT_STALL 0
124 #define UF_DEVICE_REMOTE_WAKEUP 1
125
126 #define USB_MAX_IPACKET 8 /* maximum size of the initial packet */
127
128 typedef struct {
129 uByte bLength;
130 uByte bDescriptorType;
131 uByte bDescriptorSubtype;
132 } usb_descriptor_t;
133
134 typedef struct {
135 uByte bLength;
136 uByte bDescriptorType;
137 uWord bcdUSB;
138 uByte bDeviceClass;
139 uByte bDeviceSubClass;
140 uByte bDeviceProtocol;
141 uByte bMaxPacketSize;
142 /* The fields below are not part of the initial descriptor. */
143 uWord idVendor;
144 uWord idProduct;
145 uWord bcdDevice;
146 uByte iManufacturer;
147 uByte iProduct;
148 uByte iSerialNumber;
149 uByte bNumConfigurations;
150 } usb_device_descriptor_t;
151 #define USB_DEVICE_DESCRIPTOR_SIZE 18
152
153 typedef struct {
154 uByte bLength;
155 uByte bDescriptorType;
156 uWord wTotalLength;
157 uByte bNumInterface;
158 uByte bConfigurationValue;
159 uByte iConfiguration;
160 uByte bmAttributes;
161 #define UC_BUS_POWERED 0x80
162 #define UC_SELF_POWERED 0x40
163 #define UC_REMOTE_WAKEUP 0x20
164 uByte bMaxPower; /* max current in 2 mA units */
165 #define UC_POWER_FACTOR 2
166 } usb_config_descriptor_t;
167 #define USB_CONFIG_DESCRIPTOR_SIZE 9
168
169 typedef struct {
170 uByte bLength;
171 uByte bDescriptorType;
172 uByte bInterfaceNumber;
173 uByte bAlternateSetting;
174 uByte bNumEndpoints;
175 uByte bInterfaceClass;
176 uByte bInterfaceSubClass;
177 uByte bInterfaceProtocol;
178 uByte iInterface;
179 } usb_interface_descriptor_t;
180 #define USB_INTERFACE_DESCRIPTOR_SIZE 9
181
182 typedef struct {
183 uByte bLength;
184 uByte bDescriptorType;
185 uByte bEndpointAddress;
186 #define UE_IN 0x80
187 #define UE_OUT 0x00
188 #define UE_ADDR 0x0f
189 #define UE_GET_IN(a) (((a) >> 7) & 1)
190 uByte bmAttributes;
191 #define UE_CONTROL 0x00
192 #define UE_ISOCHRONOUS 0x01
193 #define UE_BULK 0x02
194 #define UE_INTERRUPT 0x03
195 #define UE_XFERTYPE 0x03
196 uWord wMaxPacketSize;
197 uByte bInterval;
198 } usb_endpoint_descriptor_t;
199 #define USB_ENDPOINT_DESCRIPTOR_SIZE 7
200
201 typedef struct {
202 uByte bLength;
203 uByte bDescriptorType;
204 uWord bString[127];
205 } usb_string_descriptor_t;
206 #define USB_MAX_STRING_LEN 128
207
208 /* Hub specific request */
209 #define UR_GET_BUS_STATE 0x02
210
211 /* Hub features */
212 #define UHF_C_HUB_LOCAL_POWER 0
213 #define UHF_C_HUB_OVER_CURRENT 1
214 #define UHF_PORT_CONNECTION 0
215 #define UHF_PORT_ENABLE 1
216 #define UHF_PORT_SUSPEND 2
217 #define UHF_PORT_OVER_CURRENT 3
218 #define UHF_PORT_RESET 4
219 #define UHF_PORT_POWER 8
220 #define UHF_PORT_LOW_SPEED 9
221 #define UHF_C_PORT_CONNECTION 16
222 #define UHF_C_PORT_ENABLE 17
223 #define UHF_C_PORT_SUSPEND 18
224 #define UHF_C_PORT_OVER_CURRENT 19
225 #define UHF_C_PORT_RESET 20
226
227 typedef struct {
228 uByte bDescLength;
229 uByte bDescriptorType;
230 uByte bNbrPorts;
231 uWord bHubCharacteristics;
232 #define UHD_PWR 0x03
233 #define UHD_PWR_GANGED 0x00
234 #define UHD_PWR_INDIVIDUAL 0x01
235 #define UHD_PWR_NO_SWITCH 0x02
236 #define UHD_COMPOUND 0x04
237 #define UHD_OC 0x18
238 #define UHD_OC_GLOBAL 0x00
239 #define UHD_OC_INDIVIDUAL 0x08
240 #define UHD_OC_NONE 0x10
241 uByte bPwrOn2PwrGood; /* delay in 2 ms units */
242 #define UHD_PWRON_FACTOR 2
243 uByte bHubContrCurrent;
244 uByte DeviceRemovable[1];
245 /* this is only correct with 1-7 ports on the hub */
246 uByte PortPowerCtrlMask[3];
247 } usb_hub_descriptor_t;
248 #define USB_HUB_DESCRIPTOR_SIZE 9
249
250 typedef struct {
251 uWord wStatus;
252 /* Device status flags */
253 #define UDS_SELF_POWERED 0x0001
254 #define UDS_REMOTE_WAKEUP 0x0002
255 } usb_status_t;
256
257 typedef struct {
258 uWord wHubStatus;
259 #define UHS_LOCAL_POWER 0x0001
260 #define UHS_OVER_CURRENT 0x0002
261 uWord wHubChange;
262 } usb_hub_status_t;
263
264 typedef struct {
265 uWord wPortStatus;
266 #define UPS_CURRENT_CONNECT_STATUS 0x0001
267 #define UPS_PORT_ENABLED 0x0002
268 #define UPS_SUSPEND 0x0004
269 #define UPS_OVERCURRENT_INDICATOR 0x0008
270 #define UPS_RESET 0x0010
271 #define UPS_PORT_POWER 0x0100
272 #define UPS_LOW_SPEED 0x0200
273 uWord wPortChange;
274 #define UPS_C_CONNECT_STATUS 0x0001
275 #define UPS_C_PORT_ENABLED 0x0002
276 #define UPS_C_SUSPEND 0x0004
277 #define UPS_C_OVERCURRENT_INDICATOR 0x0008
278 #define UPS_C_PORT_RESET 0x0010
279 } usb_port_status_t;
280
281 #define UDESC_CS_DEVICE 0x21
282 #define UDESC_CS_CONFIG 0x22
283 #define UDESC_CS_STRING 0x23
284 #define UDESC_CS_INTERFACE 0x24
285 #define UDESC_CS_ENDPOINT 0x25
286
287 #define UDESC_HUB 0x29
288
289 #define UCLASS_UNSPEC 0
290 #define UCLASS_AUDIO 1
291 #define USUBCLASS_AUDIOCONTROL 1
292 #define USUBCLASS_AUDIOSTREAM 2
293 #define UCLASS_CDC 2
294 #define USUBCLASS_MODEM 2
295 #define UCLASS_HID 3
296 #define USUBCLASS_BOOT 1
297 #define UCLASS_PRINTER 7
298 #define USUBCLASS_PRINTER 1
299 #define UPROTO_PRINTER_UNI 1
300 #define UPROTO_PRINTER_BI 2
301 #define UCLASS_HUB 9
302 #define USUBCLASS_HUB 1
303
304 #define USB_HUB_MAX_DEPTH 5
305
306 #define USB_PORT_RESET_DELAY 10 /* ms */
307 #define USB_PORT_POWERUP_DELAY 100 /* ms */
308 #define USB_POWER_SETTLE 100 /* ms */
309
310 #define USB_MIN_POWER 100 /* mA */
311 #define USB_MAX_POWER 500 /* mA */
312
313
314 #define USB_RESET_DELAY 100 /* ms XXX?*/
315 #define USB_RESUME_DELAY 10 /* ms XXX?*/
316
317 /*** ioctl() related stuff ***/
318
319 struct usb_ctl_request {
320 int addr;
321 usb_device_request_t request;
322 void *data;
323 };
324
325 struct usb_all_desc {
326 u_char data[1024]; /* filled data size will vary */
327 };
328
329 struct usb_ctl_report_desc {
330 int size;
331 u_char data[1024]; /* filled data size will vary */
332 };
333
334 struct usb_device_info {
335 uByte addr; /* device address */
336 char product[USB_MAX_STRING_LEN];
337 char vendor[USB_MAX_STRING_LEN];
338 char revision[8];
339 uByte class;
340 uByte config;
341 uByte lowspeed;
342 int power; /* power consumption in mA, 0 if selfpowered */
343 int nports;
344 uByte ports[16]; /* hub only: addresses of devices on ports */
345 #define USB_PORT_ENABLED 0xff
346 #define USB_PORT_SUSPENDED 0xfe
347 #define USB_PORT_POWERED 0xfd
348 #define USB_PORT_DISABLED 0xfc
349 };
350
351 struct usb_ctl_report {
352 int report;
353 u_char data[1024]; /* filled data size will vary */
354 };
355
356 struct usb_device_stats {
357 u_long requests[4]; /* indexed by transfer type UE_* */
358 };
359
360 /* USB controller */
361 #define USB_REQUEST _IOWR('U', 1, struct usb_ctl_request)
362 #define USB_SETDEBUG _IOW ('U', 2, int)
363 #define USB_DISCOVER _IO ('U', 3)
364 #define USB_DEVICEINFO _IOWR('U', 4, struct usb_device_info)
365 #define USB_DEVICESTATS _IOR ('U', 5, struct usb_device_stats)
366
367 /* Generic HID device */
368 #define USB_GET_REPORT_DESC _IOR ('U', 21, struct usb_ctl_report_desc)
369 #define USB_SET_IMMED _IOW ('U', 22, int)
370 #define USB_GET_REPORT _IOWR('U', 23, struct usb_ctl_report)
371
372 /* Generic USB device */
373 #define USB_SET_CONFIG _IOW ('U', 100, int)
374 #define USB_SET_INTERFACE _IOW ('U', 101, int)
375 #define USB_GET_DEVICE_DESC _IOR ('U', 102, usb_device_descriptor_t)
376 #define USB_GET_CONFIG_DESC _IOR ('U', 103, usb_config_descriptor_t)
377 #define USB_GET_INTERFACE_DESC _IOR ('U', 104, usb_interface_descriptor_t)
378 #define USB_GET_ALL_DESC _IOR ('U', 105, struct usb_all_desc)
379
380
381 #endif /* _USB_H_ */
382