Home | History | Annotate | Line # | Download | only in usb
usb.h revision 1.6
      1 /*	$NetBSD: usb.h,v 1.6 1998/12/08 15:18:45 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_ADDR(a)	((a) & UE_ADDR)
    190 #define UE_GET_IN(a)	(((a) >> 7) & 1)
    191 	uByte		bmAttributes;
    192 #define UE_CONTROL	0x00
    193 #define UE_ISOCHRONOUS	0x01
    194 #define UE_BULK		0x02
    195 #define UE_INTERRUPT	0x03
    196 #define UE_XFERTYPE	0x03
    197 	uWord		wMaxPacketSize;
    198 	uByte		bInterval;
    199 } usb_endpoint_descriptor_t;
    200 #define USB_ENDPOINT_DESCRIPTOR_SIZE 7
    201 
    202 typedef struct {
    203 	uByte		bLength;
    204 	uByte		bDescriptorType;
    205 	uWord		bString[127];
    206 } usb_string_descriptor_t;
    207 #define USB_MAX_STRING_LEN 128
    208 
    209 /* Hub specific request */
    210 #define UR_GET_BUS_STATE	0x02
    211 
    212 /* Hub features */
    213 #define UHF_C_HUB_LOCAL_POWER	0
    214 #define UHF_C_HUB_OVER_CURRENT	1
    215 #define UHF_PORT_CONNECTION	0
    216 #define UHF_PORT_ENABLE		1
    217 #define UHF_PORT_SUSPEND	2
    218 #define UHF_PORT_OVER_CURRENT	3
    219 #define UHF_PORT_RESET		4
    220 #define UHF_PORT_POWER		8
    221 #define UHF_PORT_LOW_SPEED	9
    222 #define UHF_C_PORT_CONNECTION	16
    223 #define UHF_C_PORT_ENABLE	17
    224 #define UHF_C_PORT_SUSPEND	18
    225 #define UHF_C_PORT_OVER_CURRENT	19
    226 #define UHF_C_PORT_RESET	20
    227 
    228 typedef struct {
    229 	uByte		bDescLength;
    230 	uByte		bDescriptorType;
    231 	uByte		bNbrPorts;
    232 	uWord		bHubCharacteristics;
    233 #define UHD_PWR			0x03
    234 #define UHD_PWR_GANGED		0x00
    235 #define UHD_PWR_INDIVIDUAL	0x01
    236 #define UHD_PWR_NO_SWITCH	0x02
    237 #define UHD_COMPOUND		0x04
    238 #define UHD_OC			0x18
    239 #define UHD_OC_GLOBAL		0x00
    240 #define UHD_OC_INDIVIDUAL	0x08
    241 #define UHD_OC_NONE		0x10
    242 	uByte		bPwrOn2PwrGood;	/* delay in 2 ms units */
    243 #define UHD_PWRON_FACTOR 2
    244 	uByte		bHubContrCurrent;
    245 	uByte		DeviceRemovable[1];
    246 	/* this is only correct with 1-7 ports on the hub */
    247 	uByte		PortPowerCtrlMask[3];
    248 } usb_hub_descriptor_t;
    249 #define USB_HUB_DESCRIPTOR_SIZE 9
    250 
    251 typedef struct {
    252 	uWord		wStatus;
    253 /* Device status flags */
    254 #define UDS_SELF_POWERED		0x0001
    255 #define UDS_REMOTE_WAKEUP		0x0002
    256 } usb_status_t;
    257 
    258 typedef struct {
    259 	uWord		wHubStatus;
    260 #define UHS_LOCAL_POWER			0x0001
    261 #define UHS_OVER_CURRENT		0x0002
    262 	uWord		wHubChange;
    263 } usb_hub_status_t;
    264 
    265 typedef struct {
    266 	uWord		wPortStatus;
    267 #define UPS_CURRENT_CONNECT_STATUS	0x0001
    268 #define UPS_PORT_ENABLED		0x0002
    269 #define UPS_SUSPEND			0x0004
    270 #define UPS_OVERCURRENT_INDICATOR	0x0008
    271 #define UPS_RESET			0x0010
    272 #define UPS_PORT_POWER			0x0100
    273 #define UPS_LOW_SPEED			0x0200
    274 	uWord		wPortChange;
    275 #define UPS_C_CONNECT_STATUS		0x0001
    276 #define UPS_C_PORT_ENABLED		0x0002
    277 #define UPS_C_SUSPEND			0x0004
    278 #define UPS_C_OVERCURRENT_INDICATOR	0x0008
    279 #define UPS_C_PORT_RESET		0x0010
    280 } usb_port_status_t;
    281 
    282 #define UDESC_CS_DEVICE		0x21
    283 #define UDESC_CS_CONFIG		0x22
    284 #define UDESC_CS_STRING		0x23
    285 #define UDESC_CS_INTERFACE	0x24
    286 #define UDESC_CS_ENDPOINT	0x25
    287 
    288 #define UDESC_HUB		0x29
    289 
    290 #define UCLASS_UNSPEC		0
    291 #define UCLASS_AUDIO		1
    292 #define  USUBCLASS_AUDIOCONTROL	1
    293 #define  USUBCLASS_AUDIOSTREAM	2
    294 #define UCLASS_CDC		2
    295 #define  USUBCLASS_MODEM	2
    296 #define UCLASS_HID		3
    297 #define  USUBCLASS_BOOT	 	1
    298 #define UCLASS_PRINTER		7
    299 #define  USUBCLASS_PRINTER	1
    300 #define  UPROTO_PRINTER_UNI	1
    301 #define  UPROTO_PRINTER_BI	2
    302 #define UCLASS_HUB		9
    303 #define  USUBCLASS_HUB		1
    304 #define UCLASS_DATA		10
    305 
    306 #define USB_HUB_MAX_DEPTH 5
    307 
    308 /*
    309  * Minimum time a device needs to be powered down to go through
    310  * a power cycle.  XXX Is this time in the spec?
    311  */
    312 #define USB_POWER_DOWN_TIME	1000 /* ms */
    313 
    314 #define USB_PORT_RESET_DELAY	10  /* ms */
    315 #define USB_PORT_POWERUP_DELAY	100 /* ms */
    316 #define USB_POWER_SETTLE	100 /* ms */
    317 
    318 #define USB_MIN_POWER		100 /* mA */
    319 #define USB_MAX_POWER		500 /* mA */
    320 
    321 
    322 #define USB_RESET_DELAY		100 /* ms XXX?*/
    323 #define USB_RESUME_DELAY	10 /* ms XXX?*/
    324 
    325 /*** ioctl() related stuff ***/
    326 
    327 struct usb_ctl_request {
    328 	int	addr;
    329 	usb_device_request_t request;
    330 	void	*data;
    331 };
    332 
    333 struct usb_alt_interface {
    334 	int	interface_index;
    335 	int	alt_no;
    336 };
    337 
    338 #define USB_CURRENT_CONFIG_INDEX (-1)
    339 #define USB_CURRENT_ALT_INDEX (-1)
    340 
    341 struct usb_config_desc {
    342 	int	config_index;
    343 	usb_config_descriptor_t desc;
    344 };
    345 
    346 struct usb_interface_desc {
    347 	int	config_index;
    348 	int	interface_index;
    349 	int	alt_index;
    350 	usb_interface_descriptor_t desc;
    351 };
    352 
    353 struct usb_endpoint_desc {
    354 	int	config_index;
    355 	int	interface_index;
    356 	int	alt_index;
    357 	int	endpoint_index;
    358 	usb_endpoint_descriptor_t desc;
    359 };
    360 
    361 struct usb_full_desc {
    362 	int	config_index;
    363 	u_int	size;
    364 	u_char	*data;
    365 };
    366 
    367 struct usb_ctl_report_desc {
    368 	int	size;
    369 	u_char	data[1024];	/* filled data size will vary */
    370 };
    371 
    372 struct usb_device_info {
    373 	uByte	addr;		/* device address */
    374 	char	product[USB_MAX_STRING_LEN];
    375 	char	vendor[USB_MAX_STRING_LEN];
    376 	char	revision[8];
    377 	uByte	class;
    378 	uByte	config;
    379 	uByte	lowspeed;
    380 	int	power;		/* power consumption in mA, 0 if selfpowered */
    381 	int	nports;
    382 	uByte	ports[16];	/* hub only: addresses of devices on ports */
    383 #define USB_PORT_ENABLED 0xff
    384 #define USB_PORT_SUSPENDED 0xfe
    385 #define USB_PORT_POWERED 0xfd
    386 #define USB_PORT_DISABLED 0xfc
    387 };
    388 
    389 struct usb_ctl_report {
    390 	int report;
    391 	u_char	data[1024];	/* filled data size will vary */
    392 };
    393 
    394 struct usb_device_stats {
    395 	u_long	requests[4];	/* indexed by transfer type UE_* */
    396 };
    397 
    398 /* USB controller */
    399 #define USB_REQUEST		_IOWR('U', 1, struct usb_ctl_request)
    400 #define USB_SETDEBUG		_IOW ('U', 2, int)
    401 #define USB_DISCOVER		_IO  ('U', 3)
    402 #define USB_DEVICEINFO		_IOWR('U', 4, struct usb_device_info)
    403 #define USB_DEVICESTATS		_IOR ('U', 5, struct usb_device_stats)
    404 
    405 /* Generic HID device */
    406 #define USB_GET_REPORT_DESC	_IOR ('U', 21, struct usb_ctl_report_desc)
    407 #define USB_SET_IMMED		_IOW ('U', 22, int)
    408 #define USB_GET_REPORT		_IOWR('U', 23, struct usb_ctl_report)
    409 
    410 /* Generic USB device */
    411 #define USB_GET_CONFIG		_IOR ('U', 100, int)
    412 #define USB_SET_CONFIG		_IOW ('U', 101, int)
    413 #define USB_GET_ALTINTERFACE	_IOWR('U', 102, struct usb_alt_interface)
    414 #define USB_SET_ALTINTERFACE	_IOW ('U', 103, struct usb_alt_interface)
    415 #define USB_GET_NO_ALT		_IOWR('U', 104, struct usb_alt_interface)
    416 #define USB_GET_DEVICE_DESC	_IOR ('U', 105, usb_device_descriptor_t)
    417 #define USB_GET_CONFIG_DESC	_IOR ('U', 106, struct usb_config_desc)
    418 #define USB_GET_INTERFACE_DESC	_IOWR('U', 107, struct usb_interface_desc)
    419 #define USB_GET_ENDPOINT_DESC	_IOWR('U', 108, struct usb_endpoint_desc)
    420 #define USB_GET_FULL_DESC	_IOR ('U', 109, struct usb_full_desc)
    421 #define USB_DO_REQUEST		_IOWR('U', 110, struct usb_ctl_request)
    422 
    423 
    424 #endif /* _USB_H_ */
    425