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