Initial work for Rust abstraction for HID device driver development
v3 cover Initial work for Rust abstraction for HID device driver development
Hello again,
Resending my patches for review.
https://lore.kernel.org/rust-for-linux/[email protected]/
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
PATCH RESEND 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
@@ -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
PATCH RESEND v3 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:
* 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH RESEND v3 3/3 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:
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
@@ -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
@@ -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 +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.47.2