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