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