UGEN(4)

πŸ’– UGEN β€” USB generic device support πŸ’–

okay bestie, we're in section 4 of the kernel interfaces manual, this is the UGEN(4) page, and honestly? it's giving "the backup dancer of USB drivers but make it powerful." let's go πŸ¦‹


✨ SYNOPSIS ✨

ugen is integrated into the usb(4) kernel module.

like, it's not even its own standalone thing β€” it's built right into usb(4). no cap. πŸ’…


🌸 DESCRIPTION 🌸

okay so picture this: the ugen driver is the catch-all bestie for every USB device that doesn't have its own special driver. it's the "we'll figure it out" friend of the kernel. it gives you access to all parts of the device</>, but ngl it's not as convenient as having a purpose-built driver for it. trade-offs, we love them. πŸ™ˆ

so here's the tea β˜• β€” there can be up to 127 USB devices connected to a USB bus. each USB device can have up to 16 endpoints. and every one of those endpoints communicates in one of four different modes:

  1. control
  2. isochronous
  3. bulk
  4. interrupt

each endpoint gets its own device node, which is very demure, very mindful. the four least significant bits in the minor device number determine which endpoint the device accesses, and the rest of the bits determine which USB device. iykyk. πŸŽ€

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

to find out which endpoints exist, there's a whole series of ioctl(2) operations on the control endpoint that return the USB descriptors of the device, configurations, interfaces, and endpoints. it's like a little info dump but make it system calls. ✨

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

the bulk transfer mode can be in or out depending on the endpoint. to do I/O on a bulk endpoint, read(2) and write(2) should be used. and listen β€” all I/O operations on a bulk endpoint are unbuffered. no buffer, no filter, just vibes. 😭

the interrupt transfer mode can be in or out depending on the endpoint. to do I/O on an interrupt endpoint, read(2) and write(2) should be used. a moderate amount of buffering is done by the driver. so like, medium buffering. a healthy amount. 🫢


πŸ’œ the ioctls EVERY endpoint handles πŸ’œ

all endpoints handle the following ioctl(2) calls:

  • USB_SET_SHORT_XFER (int) β€” allow short read transfer. normally a transfer from the device that's shorter than the request specified is reported as an error. this says "nah, it's fine, we ball." πŸ”₯
  • 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. (live dangerously bestie πŸ’€)

πŸ’… the ioctls the CONTROL endpoint (endpoint 0) handles πŸ’…

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

  • USB_GET_CONFIG (int) β€” get the device configuration number. simple. clean. iconic.
  • USB_SET_CONFIG (int) β€” set the device into the given configuration number.
heads up! this operation can only be performed when the control endpoint is the sole open endpoint. like, it needs to be the main character and the only character. πŸ‘‘
  • USB_GET_ALTINTERFACE (struct usb_alt_interface) β€” get the alternative setting number for the interface with the given index. the uai_config_index is ignored in this call. (rude of it, honestly.)
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 too.
and! this operation can only be performed when no endpoints for the interface are open. closed for business, fr fr.
  • USB_GET_NO_ALT (struct usb_alt_interface) β€” return the number of different alternate settings in the uai_alt_no field. cute little count. πŸ“
  • USB_GET_DEVICE_DESC (usb_device_descriptor_t) β€” return the device descriptor. here's your device's little ID card. πŸͺͺ
  • 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. yes bestie, four whole indices. she's thorough. πŸ’œ
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. the whole collection. the full set. the entire wardrobe. πŸ‘—
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. so like, peek first, then commit. 🌈

  • 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 (struct usb_ctl_request) β€” send a USB request to the device on the control endpoint. any data sent to/from the device is located at ucr_data. the size of the transferred data is determined from the ucr_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 ucr_actlen 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 */
};
🚨 bestie, WARNING: 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. so like, the scariest ones are locked but still β€” be careful. this is not a vibe check you want to fail. πŸ’€
  • USB_GET_DEVICEINFO (struct usb_device_info) β€” get an information summary for the device. and the best part? this call will not issue any USB transactions. zero transactions. just info. immaculate. ✨

🧚 addressing: index vs number (the plot twist) 🧚

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 uses the index, getting and setting typically uses numbers. two names, two jobs, no overlap. periodt. πŸ’…

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 be set to USB_CURRENT_CONFIG_INDEX and alt_index should be set to USB_CURRENT_ALT_INDEX. πŸ’–


⭐ SYSCTL VARIABLES ⭐

the following variables are available as both sysctl(8) variables and loader(8) tunables:

  • hw.usb.ugen.debug β€” debug output level, where 0 is debugging disabled and larger values increase debug message verbosity. default is 0. so if you're like "why is this so quiet," that's why bestie. turn it up. πŸ”Š

πŸŽ€ FILES πŸŽ€

  • /dev/ugenN.E β€” Endpoint E of device N. that's it, that's the file. so elegant. 🌸

πŸ‘‘ SEE ALSO πŸ‘‘


πŸ¦‹ HISTORY πŸ¦‹

the ugen driver appeared in NetBSD 1.4. an elder. a legend. we stan a long-running icon. πŸ‘‘


and that's the whole page bestie!! this beauty is from the Debian crew, dated April 24, 2018 β€” go drink some water and go read your descriptors responsibly πŸ’–βœ¨πŸ«Ά

πŸ“¦ where this page comes from