Initial work for Rust abstraction for HID device driver development
v7 cover Initial work for Rust abstraction for HID device driver development
Thanks Gary and others for the great review on v6. In parallel to v7, I am working on a more complex driver for RazerBlade laptops that I am hoping will be interesting for review.
Rahul Rameshbabu (2):
rust: core abstractions for HID drivers
rust: hid: Glorious PC Gaming Race Model O and O- mice reference
driver
MAINTAINERS | 14 + drivers/hid/Kconfig | 2 + drivers/hid/Makefile | 2 + drivers/hid/hid-glorious.c | 2 + drivers/hid/rust/Kconfig | 28 ++ drivers/hid/rust/Makefile | 6 + drivers/hid/rust/hid_glorious_rust.rs | 67 ++++ rust/bindings/bindings_helper.h | 3 + rust/kernel/hid.rs | 509 ++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 10 files changed, 635 insertions(+) create mode 100644 drivers/hid/rust/Kconfig create mode 100644 drivers/hid/rust/Makefile create mode 100644 drivers/hid/rust/hid_glorious_rust.rs create mode 100644 rust/kernel/hid.rs
base-commit: b3d161f22ba9b2dc16bb82aa2b8515d98c99624f
Signature · 1 line
2.52.0
PATCH v7 1/2 rust: core abstractions for HID drivers
These 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:
Changelog:
v6->v7:
* Implemented HID items as bitflags
* Used #[inline] for into_u16 and Device::as_raw
* Defined a custom GroupUnknownError type
* Fixed the description of DeviceId::new_usb
* Made report_fixup use a single lifetime value due to references being covariant
* Simplified error handling in report_fixup_callback
v5->v6:
* Converted From<u16> for Group to TryFrom<u16> to properly handle error case
* Renamed into method for Group to into_u16 to not conflate with the From trait
* Refactored new upstream changes to RegistrationOps
* Implemented DriverLayout trait for hid::Adapter<T>
v4->v5:
* Add rust/ to drivers/hid/Makefile
* Implement RawDeviceIdIndex trait
v3->v4:
* Removed specifying tree in MAINTAINERS file since that is up for debate
* Minor rebase cleanup
* Moved driver logic under drivers/hid/rust
v2->v3:
* Implemented AlwaysRefCounted trait using embedded struct device's reference counts instead of the separate reference counter in struct hid_device
* Used &raw mut as appropriate
* Binded include/linux/device.h for get_device and put_device
* Cleaned up various comment related formatting
* Minified dev_err! format string
* Updated Group enum to be repr(u16)
* Implemented From<u16> trait for Group
* Added TODO comment when const_trait_impl stabilizes
* Made group getter functions return a Group variant instead of a raw number
* Made sure example code builds
v1->v2:
* Binded drivers/hid/hid-ids.h for use in Rust drivers
* Remove pre-emptive referencing of a C HID driver instance before it is fully initialized in the driver registration path
* Moved static getters to generic Device trait implementation, so they can be used by all device::DeviceContext
* Use core macros for supporting DeviceContext transitions
* Implemented the AlwaysRefCounted and AsRef traits
* Make use for dev_err! as appropriate
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 | 8 + drivers/hid/Kconfig | 2 + drivers/hid/Makefile | 2 + drivers/hid/rust/Kconfig | 12 + rust/bindings/bindings_helper.h | 3 + rust/kernel/hid.rs | 509 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 7 files changed, 538 insertions(+) create mode 100644 drivers/hid/rust/Kconfig create mode 100644 rust/kernel/hid.rs
MAINTAINERS +8 -0
@@ -11316,6 +11316,14 @@ F: include/uapi/linux/hid* F: samples/hid/ F: tools/testing/selftests/hid/ +HID CORE LAYER [RUST] +M: Rahul Rameshbabu <[email protected]> +R: Benjamin Tissoires <[email protected]> +L: [email protected] +S: Maintained +F: drivers/hid/rust/*.rs +F: rust/kernel/hid.rs + HID LOGITECH DRIVERS R: Filipe Laíns <[email protected]> L: [email protected]
drivers/hid/Kconfig +2 -0
@@ -1439,6 +1439,8 @@ endmenu source "drivers/hid/bpf/Kconfig" +source "drivers/hid/rust/Kconfig" + source "drivers/hid/i2c-hid/Kconfig" source "drivers/hid/intel-ish-hid/Kconfig"
drivers/hid/Makefile +2 -0
@@ -8,6 +8,8 @@ hid-$(CONFIG_HID_HAPTIC) += hid-haptic.o obj-$(CONFIG_HID_BPF) += bpf/ +obj-$(CONFIG_RUST_HID_ABSTRACTIONS) += rust/ + obj-$(CONFIG_HID) += hid.o obj-$(CONFIG_UHID) += uhid.o
drivers/hid/rust/Kconfig new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "Rust HID support" + +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. + +endmenu # Rust HID support
rust/bindings/bindings_helper.h +3 -0
@@ -48,6 +48,7 @@#include <linux/cpumask.h> #include <linux/cred.h> #include <linux/debugfs.h> +#include <linux/device.h> #include <linux/device/faux.h> #include <linux/dma-direction.h> #include <linux/dma-mapping.h>
@@ -60,6 +61,8 @@#include <linux/i2c.h> #include <linux/interrupt.h> #include <linux/io-pgtable.h> +#include <linux/hid.h> +#include "../../drivers/hid/hid-ids.h" #include <linux/ioport.h> #include <linux/jiffies.h> #include <linux/jump_label.h>
rust/kernel/hid.rs new file +509 -0
509 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -102,6 +102,8 @@pub mod id_pool;#[doc(hidden)] pub mod impl_flags; +#[cfg( CONFIG_RUST_HID_ABSTRACTIONS )] +pub mod hid;pub mod init;pub mod io;pub mod ioctl;
Signature · 1 line
2.52.0
PATCH v7 2/2 rust: hid: Glorious PC Gaming 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
---
Notes:
Changelog:
v6->v7:
* Removed duplicate driver/hid/hid_glorious_rust.rs source
* Made use of HID item bitflags
* Updated report_fixup to use a single lifetime
v5->v6:
* NONE
v4->v5:
* NONE
v3->v4:
* Removed specifying tree in MAINTAINERS file since that is up for debate
* Minor rebase cleanup
* Moved driver logic under drivers/hid/rust
* Use hex instead of decimal for the report descriptor comparisons
v2->v3:
* Fixed docstring formatting
* Updated MAINTAINERS file based on v1 and v2 discussion
v1->v2:
* Use vendor id and device id from drivers/hid/hid-ids.h bindings
* Make use for dev_err! as appropriate
MAINTAINERS | 6 +++ drivers/hid/hid-glorious.c | 2 + drivers/hid/rust/Kconfig | 16 +++++++ drivers/hid/rust/Makefile | 6 +++ drivers/hid/rust/hid_glorious_rust.rs | 67 +++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 drivers/hid/rust/Makefile create mode 100644 drivers/hid/rust/hid_glorious_rust.rs
MAINTAINERS +6 -0
@@ -10805,6 +10805,12 @@ 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 +F: drivers/hid/rust/hid_glorious_rust.rs + GNSS SUBSYSTEM M: Johan Hovold <[email protected]> S: Maintained
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/rust/Kconfig +16 -0
@@ -9,4 +9,20 @@ config RUST_HID_ABSTRACTIONS Adds support needed for HID drivers written in Rust. It provides a wrapper around the C hid core. +if RUST_HID_ABSTRACTIONS + +menu "Special HID drivers" + +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. + +endmenu # Special HID drivers + +endif # RUST_HID_ABSTRACTIONS + endmenu # Rust HID support
drivers/hid/rust/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for Rust HID support +# + +obj-$(CONFIG_HID_GLORIOUS_RUST) += hid_glorious_rust.o
drivers/hid/rust/hid_glorious_rust.rs new file +67 -0
67 lines not on this page · raw message
Signature · 1 line
2.52.0
kernel test robot
build botRe: [PATCH v7 1/2] rust: core abstractions for HID drivers
In reply to Rahul Rameshbabukernel test robot noticed the following build errors:
Hi Rahul,
kernel test robot noticed the following build errors:
[auto build test ERROR on b3d161f22ba9b2dc16bb82aa2b8515d98c99624f]
url: https://github.com/intel-lab-lkp/linux/commits/Rahul-Rameshbabu/rust-core-abstractions-for-HID-drivers/20260315-071004 base: b3d161f22ba9b2dc16bb82aa2b8515d98c99624f patch link: https://lore.kernel.org/r/20260314215527.57575-2-sergeantsagara%40protonmail.com patch subject: [PATCH v7 1/2] rust: core abstractions for HID drivers config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260315/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260315/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
PATH=/opt/cross/clang-20/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
INFO PATH=/opt/cross/rustc-1.88.0-bindgen-0.72.1/cargo/bin:/opt/cross/clang-20/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/usr/bin/timeout -k 100 12h /usr/bin/make KCFLAGS=\ -fno-crash-diagnostics\ -Wno-error=return-type\ -Wreturn-type\ -funsigned-char\ -Wundef\ -falign-functions=64 W=1 --keep-going LLVM=1 -j32 -C source O=/kbuild/obj/consumer/x86_64-rhel-9.4-rust ARCH=x86_64 SHELL=/bin/bash rustfmtcheck
make: Entering directory '/kbuild/src/consumer'
make[1]: Entering directory '/kbuild/obj/consumer/x86_64-rhel-9.4-rust'
>> Diff in rust/kernel/hid.rs:6:
//!
//! C header: [`include/linux/hid.h`](srctree/include/linux/hid.h) -use core::{
- marker::PhantomData,
- ptr::{
- addr_of_mut,
- NonNull, //
- } //
-};
use crate::{
device,
device_id::{
Diff in rust/kernel/hid.rs:24:
impl_flags,
prelude::*,
types::Opaque, //
+};
+use core::{
+ marker::PhantomData,
+ ptr::{
+ addr_of_mut,
+ NonNull, //
+ }, //
};
use kernel::fmt;>> Diff in rust/kernel/hid.rs:6:
//!
//! C header: [`include/linux/hid.h`](srctree/include/linux/hid.h) -use core::{
- marker::PhantomData,
- ptr::{
- addr_of_mut,
- NonNull, //
- } //
-};
use crate::{
device,
device_id::{
Diff in rust/kernel/hid.rs:24:
impl_flags,
prelude::*,
types::Opaque, //
+};
+use core::{
+ marker::PhantomData,
+ ptr::{
+ addr_of_mut,
+ NonNull, //
+ }, //
};
use kernel::fmt;>> Diff in rust/kernel/lib.rs:97:
pub mod firmware;
pub mod fmt;
pub mod fs;
+#[cfg(CONFIG_RUST_HID_ABSTRACTIONS)]
+pub mod hid;
#[cfg(CONFIG_I2C = "y")]
pub mod i2c;
pub mod id_pool;
Diff in rust/kernel/lib.rs:103:
#[doc(hidden)]
pub mod impl_flags;
-#[cfg(CONFIG_RUST_HID_ABSTRACTIONS)]
-pub mod hid;
pub mod init;
pub mod io;
pub mod ioctl; make[2]: *** [Makefile:1912: rustfmt] Error 123
make[2]: Target 'rustfmtcheck' not remade because of errors. make[1]: Leaving directory '/kbuild/obj/consumer/x86_64-rhel-9.4-rust' make[1]: *** [Makefile:248: __sub-make] Error 2
make[1]: Target 'rustfmtcheck' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2
make: Target 'rustfmtcheck' not remade because of errors.
make: Leaving directory '/kbuild/src/consumer'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki