UGEN(4) | Device Drivers Manual | UGEN(4)

# NAME

`ugen` —
USB generic device support

# SYNOPSIS

`ugen* at uhub? flags N`
`ugenif* at uhub? vendor V product P configuration C interface I`

# DESCRIPTION

The `ugen` driver provides support for all USB devices that do not have a special driver. It supports access to all parts of the device, but not in a way that is as convenient as a special purpose driver.

Normally the `ugen` driver is used when no other driver attaches to a device. If “flags 1” is specified, the `ugen` will instead attach with a very high priority and always be used. Together with the `vendor` and `product` locators this can be used to force the `ugen` driver to be used for a certain device.

The second form of attachment can be used to “steal” only one interface from some device for use by the `ugen` driver. Most likely you want to explicitily specify at least vendor, product and interface with this form, as otherwise the `ugen` driver would capture all of your `usb` devices. NOTE: You have to be extremely careful, when using this form, as the attached `ugen` driver has access to all of the device and can easily interfere with the driver(s) used for the other interface(s).

As an example of this second form of attachment there are various debugging boards available based on some FTDI chip, where one interface is used for JTAG debugging and the other is used as a serial interface. In this case you want to attach the `ugen` driver to interface 0 of this particular board identified by `vendor` and `product` while letting uftdi(4) together with ucom(4) to attach at interface 1.

There can be up to 127 USB devices connected to a USB bus. Each USB device can have up to 16 endpoints. Each of these endpoints will communicate in one of four different modes: control, isochronous, bulk, or interrupt. Each of the endpoints will have a different device node. The four least significant bits in the minor device number determines which endpoint the device accesses and the rest of the bits determines which USB device.

If an endpoint address is used both for input and output the device can be opened for both read or write.

To find out what endpoints exist there are a series of ioctl(2) operations on the control endpoint that return the USB descriptors of the device, configurations, interfaces, and endpoints.

The control transfer mode can only happen on the control endpoint which is always endpoint 0. The control endpoint accepts requests and may respond with an answer to such requests. Control requests are issued by ioctl(2) calls.

The bulk transfer mode can be in or out depending on the endpoint. To perform IO on a bulk endpoint read(2) and write(2) should be used. All IO operations on a bulk endpoint are normally unbuffered. The `USB_SET_BULK_RA` and `USB_SET_BULK_WB` ioctl(2) calls enable read-ahead and write-behind buffering, respectively. This buffering supports fixed-sized USB transfers and is intended for devices with regular and continuing data transfers. When read-ahead or write-behind are enabled, the file descriptor may be set to use non-blocking IO.

When in a read-ahead/writeback mode, select(2) for read and write operates normally, returning true if there is data in the read buffer and space in the write buffer, respectively. When not, select(2) always returns true, because there is no way to predict how the device will respond to a read or write request.

The interrupt transfer mode can be in or out depending on the endpoint. To perform IO on an interrupt endpoint read(2) and write(2) should be used. A moderate amount of buffering is done by the driver.

All endpoints handle the following ioctl(2) calls:

`USB_SET_SHORT_XFER` (`int`)
  Allow short read transfer. Normally a transfer from the device which is shorter than the request specified is reported as an error.
`USB_SET_TIMEOUT` (`int`)
  Set the timeout on the device operations, the time is specified in milliseconds. The value 0 is used to indicate that there is no timeout.

The control endpoint (endpoint 0) handles the following ioctl(2) calls:

`USB_GET_CONFIG` (`int`)
  Get the device configuration number.
`USB_SET_CONFIG` (`int`)
  Set the device into the given configuration number.

  This operation can only be performed when the control endpoint is the sole open endpoint.

`USB_GET_ALTINTERFACE` (`struct usb_alt_interface`)
  Get the alternative setting number for the interface with the given index. The `config_index` is ignored in this call.

      struct usb_alt_interface {
      	int	uai_config_index;
      	int	uai_interface_index;
      	int	uai_alt_no;
      };

`USB_SET_ALTINTERFACE` (`struct usb_alt_interface`)
  Set the alternative setting to the given number in the interface with the given index. The `uai_config_index` is ignored in this call.

  This operation can only be performed when no endpoints for the interface are open.

`USB_GET_NO_ALT` (`struct usb_alt_interface`)
  Return the number of different alternate settings in the `uai_alt_no` field.
`USB_GET_DEVICE_DESC` (`usb_device_descriptor_t`)
  Return the device descriptor.
`USB_GET_CONFIG_DESC` (`struct usb_config_desc`)
  Return the descriptor for the configuration with the given index. For convenience the current configuration can be specified by `USB_CURRENT_CONFIG_INDEX`.

      struct usb_config_desc {
      	int	ucd_config_index;
      	usb_config_descriptor_t ucd_desc;
      };

`USB_GET_INTERFACE_DESC` (`struct usb_interface_desc`)
  Return the interface descriptor for an interface specified by its configuration index, interface index, and alternative index. For convenience the current alternative can be specified by `USB_CURRENT_ALT_INDEX`.

      struct usb_interface_desc {
      	int	uid_config_index;
      	int	uid_interface_index;
      	int	uid_alt_index;
      	usb_interface_descriptor_t uid_desc;
      };

`USB_GET_ENDPOINT_DESC` (`struct usb_endpoint_desc`)
  Return the endpoint descriptor for the endpoint specified by its configuration index, interface index, alternative index, and endpoint index.

      struct usb_endpoint_desc {
      	int	ued_config_index;
      	int	ued_interface_index;
      	int	ued_alt_index;
      	int	ued_endpoint_index;
      	usb_endpoint_descriptor_t ued_desc;
      };

`USB_GET_FULL_DESC` (`struct usb_full_desc`)
  Return all the descriptors for the given configuration.

      struct usb_full_desc {
      	int	ufd_config_index;
      	u_int	ufd_size;
      	u_char	*ufd_data;
      };

  The `ufd_data` field should point to a memory area of the size given in the `ufd_size` field. The proper size can be determined by first issuing a `USB_GET_CONFIG_DESC` and inspecting the `wTotalLength` field.
`USB_GET_STRING_DESC` (`struct usb_string_desc`)
  Get a string descriptor for the given language id and string index.

      struct usb_string_desc {
      	int	usd_string_index;
      	int	usd_language_id;
      	usb_string_descriptor_t usd_desc;
      };

`USB_DO_REQUEST`
  Send a USB request to the device on the control endpoint. Any data sent to/from the device is located at `data`. The size of the transferred data is determined from the `request`. The `ucr_addr` field is ignored in this call. The `ucr_flags` field can be used to flag that the request is allowed to be shorter than the requested size, and the `ucr_actlen` field will contain the actual size on completion.

      struct usb_ctl_request {
      	int	ucr_addr;
      	usb_device_request_t ucr_request;
      	void	*ucr_data;
      	int	ucr_flags;
      #define USBD_SHORT_XFER_OK	0x04	/* allow short reads */
      	int	ucr_actlen;		/* actual length transferred */
      };

  This is a dangerous operation in that it can perform arbitrary operations on the device. Some of the most dangerous (e.g., changing the device address) are not allowed.
`USB_GET_DEVICEINFO` (`struct usb_device_info`)
  Get an information summary for the device. This call will not issue any USB transactions.

Bulk endpoints handle the following ioctl(2) calls:

`USB_SET_BULK_RA` (`int`)
  Enable or disable bulk read-ahead. When enabled, the driver will begin to read data from the device into a buffer, and will perform reads from the device whenever there is room in the buffer. The read(2) call will read data from this buffer, blocking if necessary until there is enough data to read the length of data requested. The buffer size and the read request length can be set by the `USB_SET_BULK_RA_OPT` ioctl(2) call.
`USB_SET_BULK_WB` (`int`)
  Enable or disable bulk write-behind. When enabled, the driver will buffer data from the write(2) call before writing it to the device, enabling the write(2) call to return immediately. write(2) will block if there is not enough room in the buffer for all the data. The buffer size and the write request length can be set by the `USB_SET_BULK_WB_OPT` ioctl(2) call.
`USB_SET_BULK_RA_OPT` (`struct usb_bulk_ra_wb_opt`)
  Set the size of the buffer and the length of the read requests used by the driver when bulk read-ahead is enabled. The changes do not take effect until the next time bulk read-ahead is enabled. Read requests are made for the length specified, and the host controller driver (i.e., ehci(4), ohci(4), and uhci(4)) will perform as many bus transfers as required. If transfers from the device should be smaller than the maximum length, `ra_wb_request_size` must be set to the required length.

      struct usb_bulk_ra_wb_opt {
      	u_int	ra_wb_buffer_size;
      	u_int	ra_wb_request_size;
      };

`USB_SET_BULK_WB_OPT` (`struct usb_bulk_ra_wb_opt`)
  Set the size of the buffer and the length of the write requests used by the driver when bulk write-behind is enabled. The changes do not take effect until the next time bulk write-behind is enabled.

Note that there are two different ways of addressing configurations, interfaces, alternatives, and endpoints: by index or by number. The index is the ordinal number (starting from 0) of the descriptor as presented by the device. The number is the respective number of the entity as found in its descriptor. Enumeration of descriptors use the index, getting and setting typically uses numbers.

Example: All endpoints (except the control endpoint) for the current configuration can be found by iterating the `interface_index` from 0 to `config_desc->bNumInterface-1` and for each of these iterating the `endpoint_index` from 0 to `interface_desc->bNumEndpoints`. The `config_index` should set to `USB_CURRENT_CONFIG_INDEX` and `alt_index` should be set to `USB_CURRENT_ALT_INDEX`.

# FILES

/dev/ugenN.EE
  Endpoint EE of device N.

# SEE ALSO

usb(4)

# HISTORY

The `ugen` driver appeared in NetBSD 1.4.

March 5, 2018 | NetBSD 9.0