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