1.\" $NetBSD: ugen.4,v 1.27 2009/04/10 15:07:08 joerg Exp $ 2.\" 3.\" Copyright (c) 1999 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Lennart Augustsson. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd January 19, 2008 31.Dt UGEN 4 32.Os 33.Sh NAME 34.Nm ugen 35.Nd USB generic device support 36.Sh SYNOPSIS 37.Cd "ugen* at uhub? flags N" 38.Cd options UGEN_BULK_RA_WB 39.Sh DESCRIPTION 40The 41.Nm 42driver provides support for all USB devices that do not have 43a special driver. 44It supports access to all parts of the device, 45but not in a way that is as convenient as a special purpose driver. 46.Pp 47Normally the 48.Nm 49driver is used when no other driver attaches to a device. 50If 51.Dq flags 1 52is specified, the 53.Nm 54will instead attach with a very high priority and always be used. 55Together with the 56.Cd vendor 57and 58.Cd product 59locators this can be used to force the 60.Nm 61driver to be used for a certain 62device. 63.Pp 64There can be up to 127 USB devices connected to a USB bus. 65Each USB device can have up to 16 endpoints. 66Each of these endpoints will communicate in one of four different 67modes: control, isochronous, bulk, or interrupt. 68Each of the endpoints will have a different device node. 69The four least significant bits in the minor device 70number determines which endpoint the device accesses and the rest 71of the bits determines which USB device. 72.Pp 73If an endpoint address is used both for input and output the device 74can be opened for both read or write. 75.Pp 76To find out what endpoints exist there are a series of 77.Xr ioctl 2 78operations on the control endpoint that return the USB descriptors 79of the device, configurations, interfaces, and endpoints. 80.Pp 81The control transfer mode can only happen on the control endpoint 82which is always endpoint 0. 83The control endpoint accepts requests 84and may respond with an answer to such requests. 85Control requests are issued by 86.Xr ioctl 2 87calls. 88.\" .Pp 89.\" The isochronous transfer mode can be in or out depending on the 90.\" endpoint. 91.\" To perform IO on an isochronous endpoint 92.\" .Xr read 2 93.\" and 94.\" .Xr write 2 95.\" should be used. 96.\" Before any IO operations can take place the transfer rate in 97.\" bytes/second has to be set. 98.\" This is done with 99.\" .Xr ioctl 2 100.\" .Dv USB_SET_ISO_RATE . 101.\" Performing this call sets up a buffer corresponding to 102.\" about 1 second of data. 103.Pp 104The bulk transfer mode can be in or out depending on the 105endpoint. 106To perform IO on a bulk endpoint 107.Xr read 2 108and 109.Xr write 2 110should be used. 111All IO operations on a bulk endpoint are normally unbuffered. 112On kernels built with the 113.Dv UGEN_BULK_RA_WB 114option, the 115.Dv USB_SET_BULK_RA 116and 117.Dv USB_SET_BULK_WB 118.Xr ioctl 2 119calls are available, and enable read-ahead and write-behind buffering 120respectively. 121When read-ahead or write-behind are enabled, the file descriptor 122may be set to use non-blocking IO. 123.Pp 124When in a 125.Dv UGEN_BULK_RA_WB 126mode, 127.Xr select 2 128for read and write operates normally, returning true if there is data 129in the read buffer and space in the write buffer, respectively. 130When not in a 131.Dv UGEN_BULK_RA_WB 132mode, 133.Xr select 2 134always returns true, because there is no way to predict how the device 135will respond to a read or write request. 136.Pp 137The interrupt transfer mode can be in or out depending on the 138endpoint. 139To perform IO on an interrupt endpoint 140.Xr read 2 141and 142.Xr write 2 143should be used. 144A moderate amount of buffering is done 145by the driver. 146.Pp 147All endpoints handle the following 148.Xr ioctl 2 149calls: 150.Pp 151.Bl -tag -width indent -compact 152.It Dv USB_SET_SHORT_XFER (int) 153Allow short read transfer. 154Normally a transfer from the device which is shorter than the 155request specified is reported as an error. 156.It Dv USB_SET_TIMEOUT (int) 157Set the timeout on the device operations, the time is specified 158in milliseconds. 159The value 0 is used to indicate that there is no timeout. 160.El 161.Pp 162The control endpoint (endpoint 0) handles the following 163.Xr ioctl 2 164calls: 165.Pp 166.Bl -tag -width indent -compact 167.It Dv USB_GET_CONFIG (int) 168Get the device configuration number. 169.It Dv USB_SET_CONFIG (int) 170Set the device into the given configuration number. 171.Pp 172This operation can only be performed when the control endpoint 173is the sole open endpoint. 174.It Dv USB_GET_ALTINTERFACE (struct usb_alt_interface) 175Get the alternative setting number for the interface with the given 176index. 177The 178.Dv config_index 179is ignored in this call. 180.Bd -literal 181struct usb_alt_interface { 182 int uai_config_index; 183 int uai_interface_index; 184 int uai_alt_no; 185}; 186.Ed 187.It Dv USB_SET_ALTINTERFACE (struct usb_alt_interface) 188Set the alternative setting to the given number in the interface with the 189given index. 190The 191.Dv uai_config_index 192is ignored in this call. 193.Pp 194This operation can only be performed when no endpoints for the interface 195are open. 196.It Dv USB_GET_NO_ALT (struct usb_alt_interface) 197Return the number of different alternate settings in the 198.Dv aui_alt_no 199field. 200.It Dv USB_GET_DEVICE_DESC (usb_device_descriptor_t) 201Return the device descriptor. 202.It Dv USB_GET_CONFIG_DESC (struct usb_config_desc) 203Return the descriptor for the configuration with the given index. 204For convenience the current configuration can be specified by 205.Dv USB_CURRENT_CONFIG_INDEX . 206.Bd -literal 207struct usb_config_desc { 208 int ucd_config_index; 209 usb_config_descriptor_t ucd_desc; 210}; 211.Ed 212.It Dv USB_GET_INTERFACE_DESC (struct usb_interface_desc) 213Return the interface descriptor for an interface specified by its 214configuration index, interface index, and alternative index. 215For convenience the current alternative can be specified by 216.Dv USB_CURRENT_ALT_INDEX . 217.Bd -literal 218struct usb_interface_desc { 219 int uid_config_index; 220 int uid_interface_index; 221 int uid_alt_index; 222 usb_interface_descriptor_t uid_desc; 223}; 224.Ed 225.It Dv USB_GET_ENDPOINT_DESC (struct usb_endpoint_desc) 226Return the endpoint descriptor for the endpoint specified by its 227configuration index, interface index, alternative index, and 228endpoint index. 229.Bd -literal 230struct usb_endpoint_desc { 231 int ued_config_index; 232 int ued_interface_index; 233 int ued_alt_index; 234 int ued_endpoint_index; 235 usb_endpoint_descriptor_t ued_desc; 236}; 237.Ed 238.It Dv USB_GET_FULL_DESC (struct usb_full_desc) 239Return all the descriptors for the given configuration. 240.Bd -literal 241struct usb_full_desc { 242 int ufd_config_index; 243 u_int ufd_size; 244 u_char *ufd_data; 245}; 246.Ed 247The 248.Dv ufd_data 249field should point to a memory area of the size given in the 250.Dv ufd_size 251field. 252The proper size can be determined by first issuing a 253.Dv USB_GET_CONFIG_DESC 254and inspecting the 255.Dv wTotalLength 256field. 257.It Dv USB_GET_STRING_DESC (struct usb_string_desc) 258Get a string descriptor for the given language id and 259string index. 260.Bd -literal 261struct usb_string_desc { 262 int usd_string_index; 263 int usd_language_id; 264 usb_string_descriptor_t usd_desc; 265}; 266.Ed 267.It Dv USB_DO_REQUEST 268Send a USB request to the device on the control endpoint. 269Any data sent to/from the device is located at 270.Dv data . 271The size of the transferred data is determined from the 272.Dv request . 273The 274.Dv ucr_addr 275field is ignored in this call. 276The 277.Dv ucr_flags 278field can be used to flag that the request is allowed to 279be shorter than the requested size, and the 280.Dv ucr_actlen 281field will contain the actual size on completion. 282.Bd -literal 283struct usb_ctl_request { 284 int ucr_addr; 285 usb_device_request_t ucr_request; 286 void *ucr_data; 287 int ucr_flags; 288#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */ 289 int ucr_actlen; /* actual length transferred */ 290}; 291.Ed 292This is a dangerous operation in that it can perform arbitrary operations 293on the device. 294Some of the most dangerous (e.g., changing the device 295address) are not allowed. 296.It Dv USB_GET_DEVICEINFO (struct usb_device_info) 297Get an information summary for the device. 298This call will not issue any USB transactions. 299.El 300.Pp 301Bulk endpoints handle the following 302.Xr ioctl 2 303calls: 304.Pp 305.Bl -tag -width indent -compact 306.It Dv USB_SET_BULK_RA (int) 307Enable or disable bulk read-ahead. 308When enabled, the driver will begin to read data from the device into 309a buffer, and will perform reads from the device whenever there is 310room in the buffer. 311The 312.Xr read 2 313call will read data from this buffer, blocking if necessary until 314there is enough data to read the length of data requested. 315The buffer size and the read request length can be set by the 316.Dv USB_SET_BULK_RA_OPT 317.Xr ioctl 2 318call. 319.It Dv USB_SET_BULK_WB (int) 320Enable or disable bulk write-behind. 321When enabled, the driver will buffer data from the 322.Xr write 2 323call before writing it to the device, enabling the 324.Xr write 2 325call to return immediately. 326.Xr write 2 327will block if there is not enough room in the buffer for all 328the data. 329The buffer size and the write request length can be set by the 330.Dv USB_SET_BULK_WB_OPT 331.Xr ioctl 2 332call. 333.It Dv USB_SET_BULK_RA_OPT (struct usb_bulk_ra_wb_opt) 334Set the size of the buffer and the length of the read requests used by 335the driver when bulk read-ahead is enabled. 336The changes do not take 337effect until the next time bulk read-ahead is enabled. 338Read requests 339are made for the length specified, and the host controller driver 340(i.e., 341.Xr ehci 4 , 342.Xr ohci 4 , 343and 344.Xr uhci 4 ) 345will perform as many bus transfers as required. 346If transfers from the device should be smaller than the maximum length, 347.Dv ra_wb_request_size 348must be set to the required length. 349.Bd -literal 350struct usb_bulk_ra_wb_opt { 351 u_int ra_wb_buffer_size; 352 u_int ra_wb_request_size; 353}; 354.Ed 355.It Dv USB_SET_BULK_WB_OPT (struct usb_bulk_ra_wb_opt) 356Set the size of the buffer and the length of the write requests used 357by the driver when bulk write-behind is enabled. 358The changes do not 359take effect until the next time bulk write-behind is enabled. 360.El 361.Pp 362Note that there are two different ways of addressing configurations, interfaces, 363alternatives, and endpoints: by index or by number. 364The index is the ordinal number (starting from 0) of the descriptor 365as presented by the device. 366The number is the respective number of 367the entity as found in its descriptor. 368Enumeration of descriptors 369use the index, getting and setting typically uses numbers. 370.Pp 371Example: 372All endpoints (except the control endpoint) for the current configuration 373can be found by iterating the 374.Dv interface_index 375from 0 to 376.Dv config_desc-\*[Gt]bNumInterface-1 377and for each of these iterating the 378.Dv endpoint_index 379from 0 to 380.Dv interface_desc-\*[Gt]bNumEndpoints . 381The 382.Dv config_index 383should set to 384.Dv USB_CURRENT_CONFIG_INDEX 385and 386.Dv alt_index 387should be set to 388.Dv USB_CURRENT_ALT_INDEX . 389.Sh FILES 390.Bl -tag -width Pa 391.It Pa /dev/ugenN.EE 392Endpoint 393.Pa EE 394of device 395.Pa N . 396.El 397.Sh SEE ALSO 398.Xr usb 4 399.Sh HISTORY 400The 401.Nm 402driver 403appeared in 404.Nx 1.4 . 405.\" .Sh BUGS 406.\" The driver is not yet finished; there is no access to isochronous endpoints. 407