1.\" $OpenBSD: ugen.4,v 1.19 2022/03/31 17:27:21 naddy Exp $ 2.\" $NetBSD: ugen.4,v 1.7 1999/07/30 01:32:05 augustss Exp $ 3.\" 4.\" Copyright (c) 1999 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Lennart Augustsson. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd $Mdocdate: March 31 2022 $ 32.Dt UGEN 4 33.Os 34.Sh NAME 35.Nm ugen 36.Nd USB generic device support 37.Sh SYNOPSIS 38.Cd "ugen* at uhub?" 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, but not in a way that is as 45convenient as a special purpose driver. 46.Pp 47There can be up to 127 USB devices connected to a USB bus. 48Each USB device can have up to 16 endpoints. 49Each of these endpoints will communicate in one of four different modes: 50control, isochronous, bulk, or interrupt. 51Each of the endpoints will have a different device node. 52The four least significant bits in the minor device number determine which 53endpoint the device accesses and the rest of the bits determine which 54USB device. 55.Pp 56If an endpoint address is used both for input and output, the device 57can be opened for both read or write. 58.Pp 59To find out what endpoints exist there are a series of 60.Xr ioctl 2 61operations available for the control endpoint that return the USB descriptors 62of the device, configurations, interfaces, and endpoints. 63.Pp 64The control transfer mode can only happen on the control endpoint, 65which is always endpoint 0. 66Control requests are issued by 67.Xr ioctl 2 68calls. 69.\" .Pp 70.\" The isochronous transfer mode can be in or out depending on the 71.\" endpoint. To perform I/O on an isochronous endpoint 72.\" .Xr read 2 73.\" and 74.\" .Xr write 2 75.\" should be used. 76.\" Before any I/O operations can take place, the transfer rate in 77.\" bytes/second has to be set. This is done with 78.\" .Xr ioctl 2 79.\" .Dv USB_SET_ISO_RATE . 80.\" Performing this call sets up a buffer corresponding to 81.\" about 1 second of data. 82.Pp 83The bulk transfer mode can be in or out depending on the 84endpoint. 85To perform I/O on a bulk endpoint 86.Xr read 2 87and 88.Xr write 2 89should be used. 90All I/O operations on a bulk endpoint are unbuffered. 91.Pp 92The interrupt transfer mode can only be in. 93To perform input from an interrupt endpoint 94.Xr read 2 95should be used. 96A moderate amount of buffering is done by the driver. 97.Pp 98All endpoints handle the following 99.Xr ioctl 2 100calls: 101.Pp 102.Bl -tag -width indent -compact 103.It Dv USB_SET_SHORT_XFER (int) 104Allow short read transfer. 105Normally a transfer from the device which is shorter than the request 106specified is reported as an error. 107.Pp 108.It Dv USB_SET_TIMEOUT (int) 109Set the timeout on the device operations, the time is specified 110in milliseconds. 111The value 0 is used to indicate that there is no timeout. 112.El 113.Pp 114The control endpoint (endpoint 0) handles the following 115.Xr ioctl 2 116calls: 117.Pp 118.Bl -tag -width indent -compact 119.It Dv USB_GET_CONFIG (int) 120Get the device configuration number. 121.Pp 122.It Dv USB_SET_CONFIG (int) 123Set the device into the given configuration number. 124This operation can only be performed when the control endpoint 125is the sole open endpoint. 126.Pp 127.It Dv USB_GET_ALTINTERFACE (struct usb_alt_interface) 128Get the alternative setting number for the interface with the given 129index. 130The 131.Fa uai_config_index 132is ignored in this call. 133.Bd -literal 134struct usb_alt_interface { 135 int uai_config_index; 136 int uai_interface_index; 137 int uai_alt_no; 138}; 139.Ed 140.Pp 141.It Dv USB_SET_ALTINTERFACE (struct usb_alt_interface) 142Set the alternative setting to the given number in the interface with the 143given index. 144The 145.Fa uai_config_index 146is ignored in this call. 147.Pp 148This operation can only be performed when no endpoints for the interface 149are open. 150.Pp 151.It Dv USB_GET_NO_ALT (struct usb_alt_interface) 152Return the number of different alternate settings in the 153.Fa uai_alt_no 154field. 155.Pp 156.It Dv USB_GET_DEVICE_DESC (usb_device_descriptor_t) 157Return the device descriptor. 158.Pp 159.It Dv USB_GET_CONFIG_DESC (struct usb_config_desc) 160Return the descriptor for the configuration with the given index. 161For convenience the current configuration can be specified by 162.Dv USB_CURRENT_CONFIG_INDEX . 163.Bd -literal 164struct usb_config_desc { 165 int ucd_config_index; 166 usb_config_descriptor_t ucd_desc; 167}; 168.Ed 169.Pp 170.It Dv USB_GET_INTERFACE_DESC (struct usb_interface_desc) 171Return the interface descriptor for an interface specified by its 172configuration index, interface index, and alternative index. 173For convenience the current alternative can be specified by 174.Dv USB_CURRENT_ALT_INDEX . 175.Bd -literal 176struct usb_interface_desc { 177 int uid_config_index; 178 int uid_interface_index; 179 int uid_alt_index; 180 usb_interface_descriptor_t uid_desc; 181}; 182.Ed 183.Pp 184.It Dv USB_GET_ENDPOINT_DESC (struct usb_endpoint_desc) 185Return the endpoint descriptor for the endpoint specified by its 186configuration index, interface index, alternative index, and 187endpoint index. 188.Bd -literal 189struct usb_endpoint_desc { 190 int ued_config_index; 191 int ued_interface_index; 192 int ued_alt_index; 193 int ued_endpoint_index; 194 usb_endpoint_descriptor_t ued_desc; 195}; 196.Ed 197.Pp 198.It Dv USB_GET_FULL_DESC (struct usb_full_desc) 199Return all the descriptors for the given configuration. 200.Bd -literal 201struct usb_full_desc { 202 int ufd_config_index; 203 u_int ufd_size; 204 u_char *ufd_data; 205}; 206.Ed 207.Pp 208The 209.Fa ufd_data 210field should point to a memory area of the size given in the 211.Fa ufd_size 212field. 213The proper size can be determined by first issuing a 214.Dv USB_GET_CONFIG_DESC 215and inspecting the 216.Fa wTotalLength 217field. 218.Pp 219.It Dv USB_DO_REQUEST 220Send a USB request to the device on the control endpoint. 221Any data sent to/from the device is located at 222.Fa ucr_data . 223The size of the transferred data is determined from the 224.Fa ucr_request . 225The 226.Fa ucr_addr 227field is ignored in this call. 228.Bd -literal 229struct usb_ctl_request { 230 int ucr_addr; 231 usb_device_request_t ucr_request; 232 void *ucr_data; 233 int ucr_flags; 234#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */ 235 int ucr_actlen; /* actual length transferred */ 236}; 237.Ed 238.Pp 239This is a dangerous operation in that it can perform arbitrary operations 240on the device. 241Some of the most dangerous (e.g., changing the device address) are not 242allowed. 243.Pp 244.It Dv USB_GET_DEVICEINFO (struct usb_device_info) 245Get an information summary for the device. 246This call will not issue any USB transactions. 247.El 248.Pp 249Note that there are two different ways of addressing configurations, interfaces, 250alternatives, and endpoints: by index or by number. 251The index is the ordinal number (starting from 0) of the descriptor 252as presented by the device. 253The number is the respective number of the entity as found in its descriptor. 254Enumeration of descriptors use the index, getting and setting typically uses 255numbers. 256.Pp 257Example: 258All endpoints (except the control endpoint) for the current configuration 259can be found by iterating the 260.Fa interface_index 261from 0 to 262.Fa config_desc->bNumInterfaces-1 263and for each of these iterating the 264.Fa endpoint_index 265from 0 to 266.Fa interface_desc->bNumEndpoints-1 . 267The 268.Fa config_index 269should be set to 270.Dv USB_CURRENT_CONFIG_INDEX 271and 272.Fa alt_index 273should be set to 274.Dv USB_CURRENT_ALT_INDEX . 275.Sh FILES 276.Bl -tag -width Pa 277.It Pa /dev/ugenN.EE 278Endpoint 279.Pa EE 280of device 281.Pa N . 282.El 283.Sh SEE ALSO 284.Xr intro 4 , 285.Xr uhub 4 , 286.Xr usb 4 287.Sh HISTORY 288The 289.Nm 290driver 291appeared in 292.Ox 2.6 . 293.Sh BUGS 294The driver is not yet finished; there is no access to isochronous endpoints. 295