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