Skip to contentSkip to conversation

Initial work for Rust abstraction for HID device driver development

PATCH Superseded Rahul Rameshbabu 1 participant 4 messages Jul 21, 2025

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover Initial work for Rust abstraction for HID device driver development

Hello again,

I incorporated Danilo's and Miguel's feedback from my v2. Additionally, I noticed I had basic formatting issues when running scripts/checkpatch.pl. I made sure to check the generated rustdocs and that the Rust examples compile as part of the kunit infrastructure. I dropped the kref bindings as they are no longer needed for this series.

Rahul Rameshbabu (3):
HID: core: Change hid_driver to use a const char* for name
rust: core abstractions for HID drivers
rust: hid: Glorious PC Gaming Race Model O and O- mice reference
driver

 MAINTAINERS                      |  16 +
 drivers/hid/Kconfig              |  16 +
 drivers/hid/Makefile             |   1 +
 drivers/hid/hid-glorious.c       |   2 +
 drivers/hid/hid_glorious_rust.rs |  60 ++++
 include/linux/hid.h              |   2 +-
 rust/bindings/bindings_helper.h  |   3 +
 rust/kernel/hid.rs               | 503 +++++++++++++++++++++++++++++++
 rust/kernel/lib.rs               |   2 +
 9 files changed, 604 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hid/hid_glorious_rust.rs
 create mode 100644 rust/kernel/hid.rs

base-commit: 1523590203786bf4e1d29b7d08a7100c783f20ba

Signature · 1 line
2.47.2
#1 lore raw

PATCH v3 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
index 568a9d8c749b..d65c202783da 100644
@@ -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.47.2
#2 lore raw

PATCH v3 2/3 rust: core abstractions for HID drivers

In reply to Rahul Rameshbabu

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:
Some points I did not address from the last review cycle:

* 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:

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                     |   9 +
 drivers/hid/Kconfig             |   8 +
 rust/bindings/bindings_helper.h |   3 +
 rust/kernel/hid.rs              | 503 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   2 +
 5 files changed, 525 insertions(+)
 create mode 100644 rust/kernel/hid.rs
MAINTAINERS +9 -0
index dd810da5261b..6c60765f2aaa 100644
@@ -10686,6 +10686,15 @@ 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
+T:	git git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git rust
+F:	drivers/hid/*.rs
+F:	rust/kernel/hid.rs
+
 HID LOGITECH DRIVERS
 R:	Filipe Laíns <[email protected]>
 L:	[email protected]
drivers/hid/Kconfig +8 -0
index 43859fc75747..922e76e18af2 100644
@@ -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 +3 -0
index 8cbb660e2ec2..7145fb1cdff1 100644
@@ -45,6 +45,7 @@
 #include <linux/cpufreq.h>
 #include <linux/cpumask.h>
 #include <linux/cred.h>
+#include <linux/device.h>
 #include <linux/device/faux.h>
 #include <linux/dma-mapping.h>
 #include <linux/errname.h>
@@ -52,6 +53,8 @@
 #include <linux/file.h>
 #include <linux/firmware.h>
 #include <linux/fs.h>
+#include <linux/hid.h>
+#include "../../drivers/hid/hid-ids.h"
 #include <linux/jiffies.h>
 #include <linux/jump_label.h>
 #include <linux/mdio.h>
rust/kernel/hid.rs new file +503 -0

503 lines not on this page · raw message

rust/kernel/lib.rs +2 -0
index e88bc4b27d6e..44c107f20174 100644
@@ -80,6 +80,8 @@
 pub mod firmware;
 pub mod fmt;
 pub mod fs;
+#[cfg(CONFIG_RUST_HID_ABSTRACTIONS)]
+pub mod hid;
 pub mod init;
 pub mod io;
 pub mod ioctl;
Signature · 1 line
2.47.2
#3 lore raw

PATCH v3 3/3 rust: hid: Glorious PC Gaming Race Model O and O- mice reference driver

In reply to 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) 140

---

Notes:
Changelog:

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                      |  7 ++++
 drivers/hid/Kconfig              |  8 +++++
 drivers/hid/Makefile             |  1 +
 drivers/hid/hid-glorious.c       |  2 ++
 drivers/hid/hid_glorious_rust.rs | 60 ++++++++++++++++++++++++++++++++
 5 files changed, 78 insertions(+)
 create mode 100644 drivers/hid/hid_glorious_rust.rs
MAINTAINERS +7 -0
index 6c60765f2aaa..eee9a33914ef 100644
@@ -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 rust
+F:	drivers/hid/hid_glorious_rust.rs
+
 GNSS SUBSYSTEM
 M:	Johan Hovold <[email protected]>
 S:	Maintained
drivers/hid/Kconfig +8 -0
index 922e76e18af2..b8ef750fb8b6 100644
@@ -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
index 10ae5dedbd84..bd86b3db5d88 100644
@@ -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
index 5bbd81248053..d7362852c20f 100644
@@ -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 +60 -0

60 lines not on this page · raw message

Signature · 1 line
2.47.2