Initial work for Rust abstraction for HID device driver development
cover Initial work for Rust abstraction for HID device driver development
Hello again,
I have come back with a revision of my HID binding work based on feedback from the maintainers and the community. Going with Benjamin Tissoires's feedback, I have opted to implement support for a much simpler HID device as a starting point for this work. I have gone ahead with making a Rust reference driver for the Glorious PC Gaming Race Model O and O- mice. I chose these devices because they were easily accessible for my development work. I have gone ahead with naming this driver the "Glorious Rust" driver. My binding work now adopts the core infrastructure Danilo Krummrich has provided for the Rust for Linux community.
I have decided to move this series out of RFC status, since I feel the implemented code is in a stable state. It could be used for other drivers as-is with the caveat that those drivers are only performing report fixups. Hopefully, a HID-BPF solution can be taken for production report fixup purposes. In reality, I will need to bring more of the HID subsystem functionality to Rust to promote implementations of more complex drivers, such as the USB Monitor Control driver I am planning to implement (as expressed in my RFC series). I am hoping to do a better job of documenting my progress involving this work on my blog while I am working on each iteration/addition for anyone interested in following along outside my mailing list posts.
Thanks,
Rahul Rameshbabu
Rahul Rameshbabu (3):
HID: core: Change hid_driver to use a const char* for name
rust: core abstractions for HID drivers
rust: hid: Glorious Gaming PC Race Model O and O- mice reference
driver
MAINTAINERS | 14 + drivers/hid/Kconfig | 16 ++ drivers/hid/Makefile | 1 + drivers/hid/hid-glorious.c | 2 + drivers/hid/hid_glorious_rust.rs | 62 +++++ include/linux/hid.h | 2 +- rust/bindings/bindings_helper.h | 1 + rust/kernel/hid.rs | 421 +++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 9 files changed, 520 insertions(+), 1 deletion(-) create mode 100644 drivers/hid/hid_glorious_rust.rs create mode 100644 rust/kernel/hid.rs
base-commit: 0303584766b7bdb6564c7e8f13e0b59b6ef44984
Signature · 1 line
2.49.0
PATCH 1/3 HID: core: Change hid_driver to use a const char* for name
name is never mutated by the core HID stack. Making name a const char* simplifies passing the string from Rust to C. Otherwise, it becomes difficult to pass a 'static lifetime CStr from Rust to a char*, rather than a const char*, due to lack of guarantee that the underlying data of the CStr will not be mutated by the C code.
--- include/linux/hid.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/hid.h +1 -1
@@ -816,7 +816,7 @@ struct hid_usage_id {* zero from them. */ struct hid_driver { -char *name; +const char *name;const struct hid_device_id *id_table;struct list_head dyn_list;
Signature · 1 line
2.49.0
PATCH 2/3 rust: core abstractions for HID drivers
In reply to Rahul RameshbabuThese abstractions enable the development of HID drivers in Rust by binding with the HID core C API. They provide Rust types that map to the equivalents in C. In this initial draft, only hid_device and hid_device_id are provided direct Rust type equivalents. hid_driver is specially wrapped with a custom Driver type. The module_hid_driver! macro provides analogous functionality to its C equivalent. Only the .report_fixup callback is binded to Rust so far.
Future work for these abstractions would include more bindings for common HID-related types, such as hid_field, hid_report_enum, and hid_report as well as more bus callbacks. Providing Rust equivalents to useful core HID functions will also be necessary for HID driver development in Rust.
---
Notes:
Some points I did not address from the last review cycle:
* Enabling CONFIG_HID=m with the Rust bindings
- This is a limitation due to the Makefile rules that currently exist, compiling all the Rust bindings into kernel.o, which gets linked into the core kernel image.
From rust/Makefile
$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
--extern build_error --extern macros --extern bindings --extern uapi
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
$(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o FORCE
+$(call if_changed_rule,rustc_library)
ifdef CONFIG_JUMP_LABEL
$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs endif
- I would be happy to work on support for module-level bindings, but this is not a problem unique only to the Rust HID binding work.
* I did not look into autogenerating all the getter functions for various fields exported from the binded C structures.
- I would be interested in hearing opinions from folks actively involved with Rust for Linux on this topic.
Changelog:
RFC->v1:
* Use Danilo's core infrastructure
* Account for HID device groups
* Remove probe and remove callbacks
* Implement report_fixup support
* Properly comment code including SAFETY comments
MAINTAINERS | 7 + drivers/hid/Kconfig | 8 + rust/bindings/bindings_helper.h | 1 + rust/kernel/hid.rs | 421 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 5 files changed, 439 insertions(+) create mode 100644 rust/kernel/hid.rs
MAINTAINERS +7 -0
@@ -10686,6 +10686,13 @@ F: include/uapi/linux/hid* F: samples/hid/ F: tools/testing/selftests/hid/ +HID CORE LAYER [RUST] +M: Rahul Rameshbabu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git +F: rust/kernel/hid.rs + HID LOGITECH DRIVERS R: Filipe Laíns <[email protected]> L: [email protected]
drivers/hid/Kconfig +8 -0
@@ -744,6 +744,14 @@ config HID_MEGAWORLD_FF Say Y here if you have a Mega World based game controller and want to have force feedback support for it. +config RUST_HID_ABSTRACTIONS + bool "Rust HID abstractions support" + depends on RUST + depends on HID=y + help + Adds support needed for HID drivers written in Rust. It provides a + wrapper around the C hid core. + config HID_REDRAGON tristate "Redragon keyboards" default !EXPERT
rust/bindings/bindings_helper.h +1 -0
@@ -52,6 +52,7 @@#include <linux/file.h> #include <linux/firmware.h> #include <linux/fs.h> +#include <linux/hid.h> #include <linux/jiffies.h> #include <linux/jump_label.h> #include <linux/mdio.h>
rust/kernel/hid.rs new file +421 -0
421 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -78,6 +78,8 @@#[cfg( CONFIG_RUST_FW_LOADER_ABSTRACTIONS )] pub mod firmware;pub mod fs; +#[cfg( CONFIG_RUST_HID_ABSTRACTIONS )] +pub mod hid;pub mod init;pub mod io;pub mod ioctl;
Signature · 1 line
2.49.0
Danilo Krummrich
reviewerRe: [PATCH v1 2/3] rust: core abstractions for HID drivers
(Cc: +Jiri)
Quoted 5 lines · Rahul Rameshbabu
MAINTAINERS +0
-0
MAINTAINERS F: include/uapi/linux/hid* · line 10694
Quoted 9 lines
MAINTAINERS +6
-0@@ -10686,6 +10686,13 @@ F: include/uapi/linux/hid* F: samples/hid/ F: tools/testing/selftests/hid/ +HID CORE LAYER [RUST] +M: Rahul Rameshbabu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git +F: rust/kernel/hid.rs
I assume this is agreed with the HID CORE LAYER maintainers?
There are multiple possible options, for instance:
1) Maintain the Rust abstractions as part of the existing MAINTAINERS entry. Optionally, the author can be added as another maintainer or reviewer.
2) Add a separate MAINTAINERS entry; patches still go through the same subsystem tree.
3) Add a separate MAINTAINERS entry; patches don't go through the subsystem tree (e.g. because the subsystem maintainers don't want to deal with it).
I usually recommend (1), but that's of course up to you and the HID maintainers.
Here it seems you want to go with option (2). Given that I recommend to add the HID maintainers as reviewers (if they like) to the new MAINTAINERS entry, so they can easily follow on what's going on.
MAINTAINERS F: include/uapi/linux/hid*
Quoted 9 lines
MAINTAINERS +9
-0+/// The HID driver trait. +/// +/// # Example +/// +///``` +/// use kernel::hid; +/// +/// const USB_VENDOR_ID_VALVE: u32 = 0x28de; +/// const USB_DEVICE_ID_STEAM_DECK: u32 = 0x1205;
I think we should make the existing constants from drivers/hid/hid-ids.h available, rather than defining them again.
<snip>
Quoted 9 lines
MAINTAINERS +9
-0+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; +
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+ /// 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, _rdesc: &'b mut [u8]) -> &'a [u8] {
report_fixup() is a bus device callback and seems to be synchronized against other bus device callbacks, hence the device argument should be &Device<device::Core>.
Quoted 9 lines
MAINTAINERS +9
-0+ build_error!(VTABLE_DEFAULT_ERROR) + } +} + +/// An adapter for the registration of HID drivers. +pub struct Adapter<T: Driver>(T); + +// SAFETY: A call to `unregister` for a given instance of `RegType` is guaranteed to be valid if +// a preceding call to `register` has been successful.
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> { + type RegType = bindings::hid_driver; + + unsafe fn register( + hdrv: &Opaque<Self::RegType>, + name: &'static CStr, + module: &'static ThisModule, + ) -> Result { + // SAFETY: It's safe to set the fields of `struct hid_driver` on initialization. + let raw_hdrv = unsafe { &mut *hdrv.get() };
I don't think you can guarantee that all fields of hdrv are properly initialized to create this mutable reference. I think you should do what PCI and platform does instead.
MAINTAINERS
Quoted 12 lines
MAINTAINERS +12
-0+impl<T: Driver + 'static> Adapter<T> { + extern "C" fn report_fixup_callback( + hdev: *mut bindings::hid_device, + buf: *mut u8, + size: *mut kernel::ffi::c_uint, + ) -> *const u8 { + // SAFETY: The HID subsystem only ever calls the report_fixup callback + // with a valid pointer to a `struct hid_device`. + // + // INVARIANT: `hdev` is valid for the duration of + // `report_fixup_callback()`. + let hdev = unsafe { &*hdev.cast::<Device>() };
::<Device<device::Core>
MAINTAINERS
Quoted 13 lines
MAINTAINERS +13
-0+ + // SAFETY: The HID subsystem only ever calls the report_fixup callback + // with a valid pointer to a `kernel::ffi::c_uint`. + // + // INVARIANT: `size` is valid for the duration of + // `report_fixup_callback()`. + let buf_len: usize = match unsafe { *size }.try_into() { + Ok(len) => len, + Err(e) => { + pr_err!( + "Cannot fix report description due to length conversion failure: {}!\n", + e + );
You have a valid device at this point, please use it to print with dev_err!().
Quoted 14 lines
MAINTAINERS +14
-0+ + return buf; + } + }; + + // Build a mutable Rust slice from buf and size + // + // SAFETY: The HID subsystem only ever calls the report_fixup callback + // with a valid pointer to a `u8` buffer. + // + // INVARIANT: `buf` is valid for the duration of + // `report_fixup_callback()`. + let rdesc_slice = unsafe { core::slice::from_raw_parts_mut(buf, buf_len) }; + let rdesc_slice = T::report_fixup(hdev, rdesc_slice);
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+ + match rdesc_slice.len().try_into() { + // SAFETY: The HID subsystem only ever calls the report_fixup + // callback with a valid pointer to a `kernel::ffi::c_uint`. + // + // INVARIANT: `size` is valid for the duration of + // `report_fixup_callback()`. + Ok(len) => unsafe { *size = len }, + Err(e) => { + pr_err!("Fixed report description will not be used due to {}!\n", e);
Same here.
MAINTAINERS
Quoted 8 lines
MAINTAINERS +8
-0+ + return buf; + } + } + + rdesc_slice.as_ptr() + } +}
You also need to call the macros that generate the device context Deref hierarchy and ARef<Device> conversion for you, i.e.
// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
// argument.
kernel::impl_device_context_deref!(unsafe { Device });
kernel::impl_device_context_into_aref!(Device);You probably don't need that latter, given that the only bus callback you implement is report_fixup() and you don't implement AlwaysRefCounted for Device yet.
Jiri Kosina
maintainerRe: [PATCH v1 2/3] rust: core abstractions for HID drivers
Quoted 1 line · Danilo Krummrich
(Cc: +Jiri)
Thanks.
Quoted 19 lines
Quoted 5 lines
MAINTAINERS +0
-0MAINTAINERS F: include/uapi/linux/hid* · line 10694
Quoted 9 lines
MAINTAINERS +6
-0@@ -10686,6 +10686,13 @@ F: include/uapi/linux/hid* F: samples/hid/ F: tools/testing/selftests/hid/ +HID CORE LAYER [RUST] +M: Rahul Rameshbabu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git +F: rust/kernel/hid.rsI assume this is agreed with the HID CORE LAYER maintainers?
There are multiple possible options, for instance:
Quoted 8 lines
1) Maintain the Rust abstractions as part of the existing
MAINTAINERSentry. Optionally, the author can be added as another maintainer or reviewer.2) Add a separate
MAINTAINERSentry; patches still go through the same subsystem tree.3) Add a separate
MAINTAINERSentry; patches don't go through the subsystem tree (e.g. because the subsystem maintainers don't want to deal with it).
I can't speak for Benjamin, but as far as I am concerned, I'd personally prefer option (3).
Signature · 2 lines
Jiri Kosina SUSE Labs
Benjamin Tissoires
maintainerRe: [PATCH v1 2/3] rust: core abstractions for HID drivers
Quoted 28 lines · Jiri Kosina
Quoted 1 line · Danilo Krummrich
(Cc: +Jiri)
Thanks.
Quoted 22 lines
Quoted 5 lines
MAINTAINERS +0
-0MAINTAINERS F: include/uapi/linux/hid* · line 10694
Quoted 9 lines
MAINTAINERS +6
-0@@ -10686,6 +10686,13 @@ F: include/uapi/linux/hid* F: samples/hid/ F: tools/testing/selftests/hid/ +HID CORE LAYER [RUST] +M: Rahul Rameshbabu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git +F: rust/kernel/hid.rsI assume this is agreed with the HID CORE LAYER maintainers?
There are multiple possible options, for instance:
1) Maintain the Rust abstractions as part of the existing
MAINTAINERSentry. Optionally, the author can be added as another maintainer or reviewer.
Quoted 8 lines
Quoted 5 lines
2) Add a separate
MAINTAINERSentry; patches still go through the same subsystem tree.3) Add a separate
MAINTAINERSentry; patches don't go through the subsystem tree (e.g. because the subsystem maintainers don't want to deal with it).I can't speak for Benjamin, but as far as I am concerned, I'd personally prefer option (3).
I understand Jiri's concerns, but I'm slighlty worried (3) will end up also having the leaf drivers in a separate tree, which means that 2 trees will fight for the same resource.
First patch of this series is a first example where some changes are needed in the HID bus for making rust life's easier.
Personally, I'd like to have a say and an eye on the rust abstractions and most of it, on the leaf drivers. I can't say I'll proactively review/merge things (these past few months have shown that I merely manage to follow things happening ATM), but at least I can keep an eye and shout if something is wrong.
OTOH, the HID tree (the core part) is a low change tree now, so maybe a separate tree can be handled correctly without too much troubles.
Danilo, if a separate tree is chosen, is the common practice to directly send the PR to Linus or does it need to got through the subsystem tree first (like bpf with net for example).
Cheers,
Benjamin
Danilo Krummrich
reviewerRe: [PATCH v1 2/3] rust: core abstractions for HID drivers
Quoted 51 lines · Benjamin Tissoires
Quoted 28 lines · Jiri Kosina
Quoted 1 line · Danilo Krummrich
(Cc: +Jiri)
Thanks.
Quoted 22 lines
Quoted 5 lines
MAINTAINERS +0
-0MAINTAINERS F: include/uapi/linux/hid* · line 10694
Quoted 9 lines
MAINTAINERS +6
-0@@ -10686,6 +10686,13 @@ F: include/uapi/linux/hid* F: samples/hid/ F: tools/testing/selftests/hid/ +HID CORE LAYER [RUST] +M: Rahul Rameshbabu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git +F: rust/kernel/hid.rsI assume this is agreed with the HID CORE LAYER maintainers?
There are multiple possible options, for instance:
1) Maintain the Rust abstractions as part of the existing
MAINTAINERSentry. Optionally, the author can be added as another maintainer or reviewer.Quoted 8 lines
Quoted 5 lines
2) Add a separate
MAINTAINERSentry; patches still go through the same subsystem tree.3) Add a separate
MAINTAINERSentry; patches don't go through the subsystem tree (e.g. because the subsystem maintainers don't want to deal with it).I can't speak for Benjamin, but as far as I am concerned, I'd personally prefer option (3).
I understand Jiri's concerns, but I'm slighlty worried (3) will end up also having the leaf drivers in a separate tree, which means that 2 trees will fight for the same resource.
First patch of this series is a first example where some changes are needed in the HID bus for making rust life's easier.
Personally, I'd like to have a say and an eye on the rust abstractions and most of it, on the leaf drivers. I can't say I'll proactively review/merge things (these past few months have shown that I merely manage to follow things happening ATM), but at least I can keep an eye and shout if something is wrong.
Quoted 6 lines
OTOH, the HID tree (the core part) is a low change tree now, so maybe a separate tree can be handled correctly without too much troubles.
Danilo, if a separate tree is chosen, is the common practice to directly send the PR to Linus or does it need to got through the subsystem tree first (like bpf with net for example).
We usually fall back to the global Rust tree, which is ultimately maintained by Miguel, so it's his call in the end.
However, IMHO this is more like a "last resort" in case any other approaches within the same subsystem are not desired.
From what I can read above, it feels to me as if having a separate tree (maintained by Rahul) with him sending pull requests to you guys seems like a good option. What do you think about that?
Re: [PATCH v1 2/3] rust: core abstractions for HID drivers
Quoted 62 lines · On Thu, 03 Jul, 2025 10:20:19 +0200 "Danilo Krummrich" <[email protected]> wrote:
Quoted 51 lines · Benjamin Tissoires
Quoted 28 lines · Jiri Kosina
Quoted 1 line · Danilo Krummrich
(Cc: +Jiri)
Thanks.
Quoted 22 lines
Quoted 5 lines
MAINTAINERS +0
-0MAINTAINERS F: include/uapi/linux/hid* · line 10694
Quoted 9 lines
MAINTAINERS +6
-0@@ -10686,6 +10686,13 @@ F: include/uapi/linux/hid* F: samples/hid/ F: tools/testing/selftests/hid/ +HID CORE LAYER [RUST] +M: Rahul Rameshbabu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git +F: rust/kernel/hid.rsI assume this is agreed with the HID CORE LAYER maintainers?
There are multiple possible options, for instance:
1) Maintain the Rust abstractions as part of the existing
MAINTAINERSentry. Optionally, the author can be added as another maintainer or reviewer.Quoted 8 lines
Quoted 5 lines
2) Add a separate
MAINTAINERSentry; patches still go through the same subsystem tree.3) Add a separate
MAINTAINERSentry; patches don't go through the subsystem tree (e.g. because the subsystem maintainers don't want to deal with it).I can't speak for Benjamin, but as far as I am concerned, I'd personally prefer option (3).
I understand Jiri's concerns, but I'm slighlty worried (3) will end up also having the leaf drivers in a separate tree, which means that 2 trees will fight for the same resource.
First patch of this series is a first example where some changes are needed in the HID bus for making rust life's easier.
Personally, I'd like to have a say and an eye on the rust abstractions and most of it, on the leaf drivers. I can't say I'll proactively review/merge things (these past few months have shown that I merely manage to follow things happening ATM), but at least I can keep an eye and shout if something is wrong.
Quoted 6 lines
OTOH, the HID tree (the core part) is a low change tree now, so maybe a separate tree can be handled correctly without too much troubles.
Danilo, if a separate tree is chosen, is the common practice to directly send the PR to Linus or does it need to got through the subsystem tree first (like bpf with net for example).
We usually fall back to the global Rust tree, which is ultimately maintained by Miguel, so it's his call in the end.
Quoted 6 lines
However, IMHO this is more like a "last resort" in case any other approaches within the same subsystem are not desired.
From what I can read above, it feels to me as if having a separate tree
(maintained by Rahul) with him sending pull requests to you guys seems like a good option. What do you think about that?
First wanted to say thanks to everyone who responded to this series. I am personally open to any of the above.
From what I can tell, Jiri and Benjamin would prefer pull requests/discussion of the Rust for Linux bindings not contaminate the hid git tree or linux-input mailing list. Patches will instead be submitted through the rust-for-linux mailing list and pull requests would be sent through the Rust for Linux tree. I can make Benjamin a reviewer for this effort, so he will be explicitly CCed to take a look at how the bindings are taking shape. I can make Jiri a reviewer if he would like to participate (I was assuming maybe not based on the desire for option 3 listed above). Maybe with this model, I have a rust-for-linux-hid tree where I send pull requests to the rust-for-linux tree?
On the other hand, Danilo would prefer something similar to the above with the caveat of pull requests going through the hid tree and patches through linux-input mailing list.
I am open to either personally.
I will be sending a v2 hopefully soon based on Danilo's and Peter's feedback.
In parallel, I am thinking about how to design a Drop trait for handling HID device unplug instead of needing a C style remove callback. This will be crucial for implementing sophisticated leaf drivers. The driver implemented here purely does report fixup. In my ideal world, all pure report fixup work would be done in HID-BPF space and only sophisticated drivers would be implemented as a Rust leaf driver. I have some separate scratch work playing around HID-BPF that I have not had time to clean up. Right now, it's just samples for doing things like key remapping on a keyboard. I am envisioning an Alsa UCM like model for HID-BPF for dealing with report fixups.
Signature · 3 lines
Thanks, Rahul Rameshbabu
Re: [PATCH v1 2/3] rust: core abstractions for HID drivers
Quoted 2 lines · Rahul Rameshbabu
From what I can tell,
I think Benjamin wants to keep an eye on things but may not have much time to take care of patches and so on.
So it sounds like having a separate MAINTAINERS subentry under them is best, with you applying patches and then sending PRs to them.
That way, they remain in control, they can coordinate any changes that involve both the C and the Rust side if any, and so on; while they don't need to handle directly Rust patches themselves, fixing issues with the Rust side, etc., thus removing work from their side; i.e. you would be doing that since you want to drive and use those Rust abstractions.
That arrangement is great and what other subsystems have done (most of the entries with "... [RUST]"). It also helps more people involved/trained in the overall subsystem too (should they want more reviewers/maintainers), and also allows you to become a maintainer yourself, learning to send PRs, etc. (which you may welcome -- if you already took patches, sent PRs etc., then please excuse me! I only did a quick search so I may have missed it.)
We try to reserve the global Rust tree fallback for cases where there is really no other way, e.g. when a key subsystem doesn't want anything to do with Rust at all and thus would block many others otherwise.
Thanks!
Cheers,
Miguel
Re: [PATCH v1 2/3] rust: core abstractions for HID drivers
Quoted 18 lines · On Sat, 05 Jul, 2025 12:41:02 +0200 "Miguel Ojeda"…
Quoted 2 lines · Rahul Rameshbabu
From what I can tell,
I think Benjamin wants to keep an eye on things but may not have much time to take care of patches and so on.
So it sounds like having a separate
MAINTAINERSsubentry under them is best, with you applying patches and then sending PRs to them.That way, they remain in control, they can coordinate any changes that involve both the C and the Rust side if any, and so on; while they don't need to handle directly Rust patches themselves, fixing issues with the Rust side, etc., thus removing work from their side; i.e. you would be doing that since you want to drive and use those Rust abstractions.
Quoted 7 lines
That arrangement is great and what other subsystems have done (most of the entries with "... [RUST]"). It also helps more people involved/trained in the overall subsystem too (should they want more reviewers/maintainers), and also allows you to become a maintainer yourself, learning to send PRs, etc. (which you may welcome -- if you already took patches, sent PRs etc., then please excuse me! I only did a quick search so I may have missed it.)
Thanks Miguel. I would really enjoy the opportunity. Like you noticed, I do not have this experience on the mailing list. I also want to give Benjamin and Jiri time to consider these options. Let me get my v2 patches sent out in the meantime.
Thanks,
Rahul Rameshbabu
Quoted 10 lines
We try to reserve the global Rust tree fallback for cases where there is really no other way, e.g. when a key subsystem doesn't want anything to do with Rust at all and thus would block many others otherwise.
Thanks!
Cheers,
Miguel
Danilo Krummrich
reviewerRe: [PATCH v1 2/3] rust: core abstractions for HID drivers
In reply to Rahul RameshbabuQuoted 3 lines · Rahul Rameshbabu
In parallel, I am thinking about how to design a Drop trait for handling HID device unplug instead of needing a C style remove callback. This will be crucial for implementing sophisticated leaf drivers.
Please have a look at how other bus abstractions are implemented, e.g. platform, pci, auxiliary.
They all have a probe() function in the Driver trait, which returns a Pin<KBox<Self>>, i.e. the drivers private data.
This ForeignOwnable is then stored in the private data pointer of the C struct device, which I'm adding generic accessors for in [1].
On driver remove() this ForeignOwnable is extracted back into a Pin<KBox<Self>> and dropped.
This means there's already Driver::drop() which is called on device unplug.
However, this is not sufficient, because Driver::drop() only gives you a &mut self, i.e. a reference to Pin<KBox<Self>>, but not a Core context reference to the Device, i.e. &Device<Core>. Hence you need a separate Driver::unbind() callback. Please see [2] for that.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
PATCH 3/3 rust: hid: Glorious Gaming PC Race Model O and O- mice reference driver
In reply to Rahul RameshbabuDemonstrate how to perform a report fixup from a Rust HID driver. The mice specify the const flag incorrectly in the consumer input report descriptor, which leads to inputs being ignored. Correctly patch the report descriptor for the Model O and O- mice.
Portions of the HID report post-fixup:
device 0:0
...0x81, 0x06, // Input (Data,Var,Rel) 84
...0x81, 0x06, // Input (Data,Var,Rel) 112
...0x81, 0x06, // Input (Data,Var,Rel) 140
--- MAINTAINERS | 7 ++++ drivers/hid/Kconfig | 8 +++++ drivers/hid/Makefile | 1 + drivers/hid/hid-glorious.c | 2 ++ drivers/hid/hid_glorious_rust.rs | 62 ++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 drivers/hid/hid_glorious_rust.rs
MAINTAINERS +7 -0
@@ -10200,6 +10200,13 @@ L: [email protected] S: Maintained F: drivers/platform/x86/gigabyte-wmi.c +GLORIOUS RUST DRIVER [RUST] +M: Rahul Rameshbabu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git +F: drivers/hid/hid_glorious_rust.rs + GNSS SUBSYSTEM M: Johan Hovold <[email protected]> S: Maintained
drivers/hid/Kconfig +8 -0
@@ -406,6 +406,14 @@ config HID_GLORIOUS Support for Glorious PC Gaming Race mice such as the Glorious Model O, O- and D. +config HID_GLORIOUS_RUST + tristate "Glorious O and O- mice Rust reference driver" + depends on USB_HID + depends on RUST_HID_ABSTRACTIONS + help + Support for Glorious PC Gaming Race O and O- mice + in Rust + config HID_HOLTEK tristate "Holtek HID devices" depends on USB_HID
drivers/hid/Makefile +1 -0
@@ -55,6 +55,7 @@ obj-$(CONFIG_HID_FT260) += hid-ft260.o obj-$(CONFIG_HID_GEMBIRD) += hid-gembird.o obj-$(CONFIG_HID_GFRM) += hid-gfrm.o obj-$(CONFIG_HID_GLORIOUS) += hid-glorious.o +obj-$(CONFIG_HID_GLORIOUS_RUST) += hid_glorious_rust.o obj-$(CONFIG_HID_VIVALDI_COMMON) += hid-vivaldi-common.o obj-$(CONFIG_HID_GOODIX_SPI) += hid-goodix-spi.o obj-$(CONFIG_HID_GOOGLE_HAMMER) += hid-google-hammer.o
drivers/hid/hid-glorious.c +2 -0
@@ -76,8 +76,10 @@ static int glorious_probe(struct hid_device *hdev, }static const struct hid_device_id glorious_devices[] = { +#if !IS_ENABLED(CONFIG_HID_GLORIOUS_RUST) {HID_USB_DEVICE (USB_VENDOR_ID_SINOWEALTH ,USB_DEVICE_ID_GLORIOUS_MODEL_O ) }, +#endif {HID_USB_DEVICE (USB_VENDOR_ID_SINOWEALTH ,USB_DEVICE_ID_GLORIOUS_MODEL_D ) }, { HID_USB_DEVICE(USB_VENDOR_ID_LAVIEW,
drivers/hid/hid_glorious_rust.rs new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.49.0
Danilo Krummrich
reviewerRe: [PATCH v1 3/3] rust: hid: Glorious Gaming PC Race Model O and O- mice reference driver
drivers/hid/hid_glorious_rust.rs line 10
Quoted 2 lines · Rahul Rameshbabu
drivers/hid/hid_glorious_rust.rs new file +2
-0+const USB_VENDOR_ID_SINOWEALTH :u32 =0x258a ; +const USB_DEVICE_ID_GLORIOUS_MODEL_O :u32 =0x0036 ;
As mentioned in the other patch, I think we shouldn't duplicate them, but use the ones from drivers/hid/hid-ids.h.
Quoted 24 lines
drivers/hid/hid_glorious_rust.rs new file +24
-0+struct GloriousRust ; + +kernel::hid_device_table!( +HID_TABLE , +MODULE_HID_TABLE , + <GloriousRust as hid::Driver >::IdInfo , + [( + hid::DeviceId ::new_usb ( + hid::Group ::Generic , +USB_VENDOR_ID_SINOWEALTH , +USB_DEVICE_ID_GLORIOUS_MODEL_O , + ), + (), + )] +); + +#[vtable] +impl hid::Driver for GloriousRust { +type IdInfo = (); +const ID_TABLE : hid::IdTable <Self ::IdInfo > = &HID_TABLE ; + +/// Glorious Model O and O- specify the const flag in the consumer input +/// report descriptor, which leads to inputs being ignored. Fix this by +/// patching the descriptor.
drivers/hid/hid_glorious_rust.rs line 45
Quoted 10 lines
drivers/hid/hid_glorious_rust.rs new file +10
-0+fn report_fixup <'a, 'b: 'a>(_hdev: &hid::Device , rdesc: &'bmut [u8 ]) -> &'a [u8 ] { +if rdesc.len () ==213 + && rdesc[84 ] ==129 + && rdesc[112 ] ==129 + && rdesc[140 ] ==129 + && rdesc[85 ] ==3 + && rdesc[113 ] ==3 + && rdesc[141 ] ==3 + { +pr_info! ("patching Glorious Model O consumer control report descriptor\n" );
Please use dev_dbg!() instead.
Quoted 21 lines
drivers/hid/hid_glorious_rust.rs new file +17
-0+ + rdesc[85 ] = hid::MAIN_ITEM_VARIABLE | hid::MAIN_ITEM_RELATIVE ; + rdesc[113 ] = hid::MAIN_ITEM_VARIABLE | hid::MAIN_ITEM_RELATIVE ; + rdesc[141 ] = hid::MAIN_ITEM_VARIABLE | hid::MAIN_ITEM_RELATIVE ; + } + + rdesc + } +} + +kernel::module_hid_driver! { +type :GloriousRust , + name:"GloriousRust" , + authors: ["Rahul Rameshbabu <[email protected]>" ], + description:"Rust reference HID driver for Glorious Model O and O- mice" , + license:"GPL" , +}Signature · 1 line
2.49.0
Re: [PATCH v1 3/3] rust: hid: Glorious Gaming PC Race Model O and O- mice reference driver
In reply to Rahul RameshbabuQuoted 127 lines · Rahul Rameshbabu
Demonstrate how to perform a report fixup from a Rust HID driver. The mice specify the const flag incorrectly in the consumer input report descriptor, which leads to inputs being ignored. Correctly patch the report descriptor for the Model O and O- mice.
Portions of the HID report post-fixup:
device 0:0
...0x81,0x06, // Input (Data,Var,Rel) 84
...0x81,0x06, // Input (Data,Var,Rel) 112
...0x81,0x06, // Input (Data,Var,Rel) 140Signed-off-by Rahul Rameshbabu <[email protected]>--- MAINTAINERS | 7 ++++ drivers/hid/Kconfig | 8 +++++ drivers/hid/Makefile | 1 + drivers/hid/hid-glorious.c | 2 ++ drivers/hid/hid_glorious_rust.rs | 62 ++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 drivers/hid/hid_glorious_rust.rsMAINTAINERS +7
-0@@ -10200,6 +10200,13 @@ L: [email protected] S: Maintained F: drivers/platform/x86/gigabyte-wmi.c +GLORIOUS RUST DRIVER [RUST] +M: Rahul Rameshbabu <[email protected]> +L: [email protected] +S: Maintained +T: git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git +F: drivers/hid/hid_glorious_rust.rs + GNSS SUBSYSTEM M: Johan Hovold <[email protected]> S: Maintaineddrivers/hid/Kconfig +8
-0@@ -406,6 +406,14 @@ config HID_GLORIOUS Support for Glorious PC Gaming Race mice such as the Glorious Model O, O- and D. +config HID_GLORIOUS_RUST + tristate "Glorious O and O- mice Rust reference driver" + depends on USB_HID + depends on RUST_HID_ABSTRACTIONS + help + Support for Glorious PC Gaming Race O and O- mice + in Rust + config HID_HOLTEK tristate "Holtek HID devices" depends on USB_HIDdrivers/hid/Makefile +1
-0@@ -55,6 +55,7 @@ obj-$(CONFIG_HID_FT260) += hid-ft260.o obj-$(CONFIG_HID_GEMBIRD) += hid-gembird.o obj-$(CONFIG_HID_GFRM) += hid-gfrm.o obj-$(CONFIG_HID_GLORIOUS) += hid-glorious.o +obj-$(CONFIG_HID_GLORIOUS_RUST) += hid_glorious_rust.o obj-$(CONFIG_HID_VIVALDI_COMMON) += hid-vivaldi-common.o obj-$(CONFIG_HID_GOODIX_SPI) += hid-goodix-spi.o obj-$(CONFIG_HID_GOOGLE_HAMMER) += hid-google-hammer.odrivers/hid/hid-glorious.c +2
-0@@ -76,8 +76,10 @@ static int glorious_probe(struct hid_device *hdev, }static const struct hid_device_id glorious_devices[] = { +#if !IS_ENABLED(CONFIG_HID_GLORIOUS_RUST) {HID_USB_DEVICE (USB_VENDOR_ID_SINOWEALTH ,USB_DEVICE_ID_GLORIOUS_MODEL_O ) }, +#endif {HID_USB_DEVICE (USB_VENDOR_ID_SINOWEALTH ,USB_DEVICE_ID_GLORIOUS_MODEL_D ) }, { HID_USB_DEVICE(USB_VENDOR_ID_LAVIEW,drivers/hid/hid_glorious_rust.rs new file +33
-0@@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2025 Rahul Rameshbabu <[email protected]> + +//! Rust reference HID driver for Glorious Model O and O- mice + +use kernel::{self, hid, prelude::*}; + +const USB_VENDOR_ID_SINOWEALTH :u32 =0x258a ; +const USB_DEVICE_ID_GLORIOUS_MODEL_O :u32 =0x0036 ; + +struct GloriousRust ; + +kernel::hid_device_table!( +HID_TABLE , +MODULE_HID_TABLE , + <GloriousRust as hid::Driver >::IdInfo , + [( + hid::DeviceId ::new_usb ( + hid::Group ::Generic , +USB_VENDOR_ID_SINOWEALTH , +USB_DEVICE_ID_GLORIOUS_MODEL_O , + ), + (), + )] +); + +#[vtable] +impl hid::Driver for GloriousRust { +type IdInfo = (); +const ID_TABLE : hid::IdTable <Self ::IdInfo > = &HID_TABLE ; + +/// Glorious Model O and O- specify the const flag in the consumer input
drivers/hid/hid_glorious_rust.rs line 43
Quoted 10 lines
drivers/hid/hid_glorious_rust.rs new file +10
-0+/// report descriptor, which leads to inputs being ignored. Fix this by +/// patching the descriptor. +fn report_fixup <'a, 'b: 'a>(_hdev: &hid::Device , rdesc: &'bmut [u8 ]) -> &'a [u8 ] { + if rdesc.len() == 213 + && rdesc[84] == 129 + && rdesc[112] == 129 + && rdesc[140] == 129 + && rdesc[85] == 3 + && rdesc[113] == 3 + && rdesc[141] == 3
fwiw, all tools that I've used in the past print hex for the hid report descriptor items (including the excerpt in your commit message) so IMO using hex here would be more useful.
Also at least in my head the items are logical groups, so this comparison should arguably be:
drivers/hid/hid_glorious_rust.rs line 44
Quoted 1 line
drivers/hid/hid_glorious_rust.rs new file +1
-0+ && (rdesc[84 ] ==129 && rdesc[85 ] ==3 )
with extra () to hopefully stop rustfmt from breaking it up.
(pls ignore me if your approach is already a common pattern in the kernel)
Cheers,
Peter
Quoted 24 lines
drivers/hid/hid_glorious_rust.rs new file +19
-0+ { +pr_info! ("patching Glorious Model O consumer control report descriptor\n" ); + + rdesc[85 ] = hid::MAIN_ITEM_VARIABLE | hid::MAIN_ITEM_RELATIVE ; + rdesc[113 ] = hid::MAIN_ITEM_VARIABLE | hid::MAIN_ITEM_RELATIVE ; + rdesc[141 ] = hid::MAIN_ITEM_VARIABLE | hid::MAIN_ITEM_RELATIVE ; + } + + rdesc + } +} + +kernel::module_hid_driver! { +type :GloriousRust , + name:"GloriousRust" , + authors: ["Rahul Rameshbabu <[email protected]>" ], + description:"Rust reference HID driver for Glorious Model O and O- mice" , + license:"GPL" , +}Signature · 1 line
2.49.0
Quoted 12 lines · Rahul Rameshbabu
Hello again,
I have come back with a revision of my HID binding work based on feedback from the maintainers and the community. Going with Benjamin Tissoires's feedback, I have opted to implement support for a much simpler HID device as a starting point for this work. I have gone ahead with making a Rust reference driver for the Glorious PC Gaming Race Model O and O- mice. I chose these devices because they were easily accessible for my development work. I have gone ahead with naming this driver the "Glorious Rust" driver. My binding work now adopts the core infrastructure Danilo Krummrich has provided for the Rust for Linux community.
Quoted 10 lines
I have decided to move this series out of RFC status, since I feel the implemented code is in a stable state. It could be used for other drivers as-is with the caveat that those drivers are only performing report fixups. Hopefully, a HID-BPF solution can be taken for production report fixup purposes. In reality, I will need to bring more of the HID subsystem functionality to Rust to promote implementations of more complex drivers, such as the USB Monitor Control driver I am planning to implement (as expressed in my RFC series). I am hoping to do a better job of documenting my progress involving this work on my blog while I am working on each iteration/addition for anyone interested in following along outside my mailing list posts.
Interesting. Reminds me of the days trying to rewrite hid-apple in rust, but found no rust bindings.