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:
- control
- isochronous
- bulk
- 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 value0is 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. theuai_config_indexis 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. theuai_config_indexis 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 theuai_alt_nofield. 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 byUSB_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 byUSB_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 atucr_data. the size of the transferred data is determined from theucr_request. theucr_addrfield is ignored in this call. theucr_flagsfield can be used to flag that the request is allowed to be shorter than the requested size, anducr_actlenwill 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, where0is debugging disabled and larger values increase debug message verbosity. default is0. 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
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1999 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms