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