STRUCT USB_GADGET_DRIVER(9)
πΈ NAME πΈ
struct usb_gadget_driver β driver for usb gadget devices. hiii bestie π this is the struct usb_gadget_driver(9) page from the Kernel API Manual, and it's basically the whole vibe check for a USB gadget driver. no cap.
β¨ SYNOPSIS β¨
okay so here's the struct, exactly as it appears:
struct usb_gadget_driver {
char *function;
enum usb_device_speed max_speed;
int (*bind)(struct usb_gadget *gadget, struct usb_gadget_driver
*driver);
void (*unbind)(struct usb_gadget *);
int (*setup)(struct usb_gadget *, const struct usb_ctrlrequest *);
void (*disconnect)(struct usb_gadget *);
void (*suspend)(struct usb_gadget *);
void (*resume)(struct usb_gadget *);
void (*reset)(struct usb_gadget *);
struct device_driver driver;
char *udc_name;
unsigned match_existing_only:1;
bool is_bound:1;
};
π Members π
functionβ String describing the gadget's function πmax_speedβ Highest speed the driver handles.bindβ the driver's bind callback.unbindβ Invoked when the driver is unbound from a gadget, usually fromrmmod(after a disconnect is reported). Called in a context that permits sleeping.setupβ Invoked for ep0 control requests that aren't handled by the hardware level driver. Most calls must be handled by the gadget driver, including descriptor and configuration management. The 16 bit members of the setup data are in USB byte order. Calledin_interrupt; this may not sleep. Driver queues a response to ep0, or returns negative to stall.disconnectβ Invoked after all transfers have been stopped, when the host is disconnected. May be calledin_interrupt; this may not sleep. Some devices can't detect disconnect, so this might not be called except as part of controller shutdown.suspendβ Invoked on USB suspend. May be calledin_interrupt.resumeβ Invoked on USB resume. May be calledin_interrupt.resetβ Invoked on USB bus reset. It is mandatory for all gadget drivers and should be calledin_interrupt.driverβ Driver model state for this driver.udc_nameβ A name of UDC this driver should be bound to. Ifudc_nameisNULL, this driver will be bound to any available UDC.match_existing_onlyβ If udc is not found, return an error and fail the driver registration.is_boundβ Allow a driver to be bound to only one gadget.
π¦ Description π¦
Devices are disabled till a gadget driver successfully binds, which means the driver will handle setup requests needed to enumerate (and meet "chapter 9" requirements) then do some useful work. periodt β¨
If gadget->is_otg is true, the gadget driver must provide an OTG descriptor during enumeration, or else fail the bind call. In such cases, no USB traffic may flow until both bind returns without having called usb_gadget_disconnect, and the USB host stack has initialized.
Drivers use hardware-specific knowledge to configure the usb hardware. endpoint addressing is only one of several hardware characteristics that are in descriptors the ep0 implementation returns from setup calls.
Except for ep0 implementation, most driver code shouldn't need change to run on top of different usb controllers. It'll use endpoints set up by that ep0 implementation.
The usb controller driver handles a few standard usb requests. Those include set_address, and feature flags for devices, interfaces, and endpoints (the get_status, set_feature, and clear_feature requests).
Accordingly, the driver's setup callback must always implement all get_descriptor requests, returning at least a device descriptor and a configuration descriptor. Drivers must make sure the endpoint descriptors match any hardware constraints. Some hardware also constrains other descriptors. (The pxa250 allows only configurations 1, 2, or 3).
The driver's setup callback must also implement set_configuration, and should also implement set_interface, get_configuration, and get_interface. Setting a configuration (or interface) is where endpoints should be activated or (config 0) shut down.
The gadget driver's setup callback does not have to queue a response to ep0 within the setup call, the driver can do it after setup returns. The UDC driver must wait until such a response is queued before proceeding with the data/status stages of the control transfer. bestie that last bit is important π
π NOTE π
β οΈ Currently, a number of UDC drivers rely onUSB_GADGET_DELAYED_STATUSbeing returned from the setup callback, which is a bug. See the comment next toUSB_GADGET_DELAYED_STATUSfor details. not me calling out a bug π
(Note that only the default control endpoint is supported. Neither hosts nor devices generally support control traffic except to ep0.)
Most devices will ignore USB suspend/resume operations, and so will not provide those callbacks. However, some may need to change modes when the host is no longer directing those activities. For example, local controls (buttons, dials, etc) may need to be re-enabled since the (remote) host can't do that any longer; or an error state might be cleared, to make the device behave identically whether or not power is maintained.
π SEE ALSO π
Kernel file ./include/linux/usb/gadget.h
struct usb_request(9)struct usb_ep_caps(9)struct usb_ep(9)free_usb_request(9)struct usb_gadget(9)usb_ep_align(9)usb_ep_align_maybe(9)gadget_is_altset_supported(9)gadget_is_stall_supported(9)gadget_is_zlp_supported(9)gadget_avoids_skb_reserve(9)gadget_is_dualspeed(9)gadget_is_superspeed(9)gadget_is_superspeed_plus(9)gadget_is_otg(9)usb_gadget_register_driver_owner(9)usb_gadget_unregister_driver(9)struct usb_string(9)struct usb_gadget_strings(9)usb_free_descriptors(9)
okay that's the whole page, bestie π
the source/version footer energy is ./include/linux/usb and struct usb_gadget_driver(9), dated October 2026. mwah π«Ά
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms