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