Re: [PATCH v6 1/2] rust: core abstractions for HID drivers

From: Alice Ryhl

Date: Sun Mar 01 2026 - 07:30:48 EST


On Sun, Mar 01, 2026 at 06:48:20AM +0000, Rahul Rameshbabu wrote:
> On Fri, 27 Feb, 2026 15:27:59 +0000 "Gary Guo" <gary@xxxxxxxxxxx> wrote:
> > On Sun Feb 22, 2026 at 9:56 PM GMT, Rahul Rameshbabu wrote:
> >> +/// The HID driver trait.
> >> +///
> >> +/// # Examples
> >> +///
> >> +/// ```
> >> +/// use kernel::{bindings, device, hid};
> >> +///
> >> +/// struct MyDriver;
> >> +///
> >> +/// kernel::hid_device_table!(
> >> +/// HID_TABLE,
> >> +/// MODULE_HID_TABLE,
> >> +/// <MyDriver as hid::Driver>::IdInfo,
> >> +/// [(
> >> +/// hid::DeviceId::new_usb(
> >> +/// hid::Group::Steam,
> >> +/// bindings::USB_VENDOR_ID_VALVE,
> >> +/// bindings::USB_DEVICE_ID_STEAM_DECK,
> >> +/// ),
> >> +/// (),
> >> +/// )]
> >> +/// );
> >> +///
> >> +/// #[vtable]
> >> +/// impl hid::Driver for MyDriver {
> >> +/// type IdInfo = ();
> >> +/// const ID_TABLE: hid::IdTable<Self::IdInfo> = &HID_TABLE;
> >> +///
> >> +/// /// This function is optional to implement.
> >> +/// fn report_fixup<'a, 'b: 'a>(_hdev: &hid::Device<device::Core>, rdesc: &'b mut [u8]) -> &'a [u8] {
> >> +/// // Perform some report descriptor fixup.
> >> +/// rdesc
> >> +/// }
> >> +/// }
> >> +/// ```
> >> +/// Drivers must implement this trait in order to get a HID driver registered.
> >> +/// Please refer to the `Adapter` documentation for an example.
> >> +#[vtable]
> >> +pub trait Driver: Send {
> >> + /// The type holding information about each device id supported by the driver.
> >> + // TODO: Use `associated_type_defaults` once stabilized:
> >> + //
> >> + // ```
> >> + // type IdInfo: 'static = ();
> >> + // ```
> >> + type IdInfo: 'static;
> >> +
> >> + /// The table of device ids supported by the driver.
> >> + const ID_TABLE: IdTable<Self::IdInfo>;
> >> +
> >> + /// Called before report descriptor parsing. Can be used to mutate the
> >> + /// report descriptor before the core HID logic processes the descriptor.
> >> + /// Useful for problematic report descriptors that prevent HID devices from
> >> + /// functioning correctly.
> >> + ///
> >> + /// Optional to implement.
> >> + fn report_fixup<'a, 'b: 'a>(_hdev: &Device<device::Core>, _rdesc: &'b mut [u8]) -> &'a [u8] {
> >
> > I think this can just use a single lifetime?
> >
>
> I think the problem is when a driver decides to replace the _rdesc with
> a static lifetime report descriptor declared in the device driver. My
> example driver does not do this, but this is another common pattern in C
> HID drivers. This is why two separate lifetimes were required.

I'm not entirely sure what you mean, but adding a lifetime here does not
change what this function is allowed to do at all.

Alice