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