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