rust: input: add input/serio abstractions and port Gravis Stinger driver
Subsystems (3)
v5 cover rust: input: add input/serio abstractions and port Gravis Stinger driver
This patch series introduces safe Rust abstractions for Linux input devices (struct input_dev) and the serio (Serial I/O) bus subsystem, along with a port of the legacy Gravis Stinger serial gamepad driver to safe Rust.
Changes in v5:
- serio: resolved invalid irqreturn_t constructor compilation error by constructing bindings::irqreturn(ret as u32).
- serio: decoupled child device allocation and publication in Driver lifecycle (init -> start -> attach). Child input devices are allocated and configured before opening the port, but only registered via Driver::start() after serio_open() succeeds. This prevents premature registration and spurious uevents on probe failures.
- serio: added C wrappers rust_helper_serio_pause_rx() and rust_helper_serio_continue_rx() to safely attach child devices in Driver::attach() under hardware RX pause, eliminating races with concurrent interrupts without sleeping.
- serio: removed redundant Device::as_device() method and made Device::as_raw() crate-private (pub(crate)), relying on the standard AsRef<device::Device> implementation.
Changes in v4:
- Makefile: restored analog.o and db9.o in joystick Makefile that were inadvertently removed during previous revisions.
- serio: eliminated concurrent borrow_mut() and aliasing data races between connect_callback() and interrupt_callback() by initializing and registering the input device prior to serio_open().
- serio: mapped interrupt return codes via bindings::irqreturn(..).
- serio & stinger: formatted all module imports in strict vertical style with trailing // comments.
- serio: annotated Registration::drop() and Driver::disconnect() with
#[inline].
Changes in v3:
- input: added AllocatedDevice::set_phys_from_parent() to dynamically format the physical device path ("%s/input0") in an owned kernel heap buffer.
- input: managed parent device reference counts in set_parent(), drop(), and register() using get_device()/put_device() to avoid dangling pointers and reference leaks.
- input: eliminated manual cleanup duplication in register() by checking to_result() before mem::forget(self).
- input: implemented Send and Sync for AllocatedDevice, added #[inline] on small methods, and formatted imports vertically.
- serio: integrated serio::Device with kernel device abstractions via AsBusDevice, impl_device_context_deref!, and AsRef<device::Device>.
- stinger: restored userspace-visible device name ("Gravis Stinger") and dynamic phys path, preserving udev and EVIOCGNAME compatibility.
- stinger: maintained module name "stinger.ko" using Kbuild object aliasing under CONFIG_JOYSTICK_STINGER_RUST.
Changes in v2:
- Combined serio subsystem abstractions and Gravis Stinger driver port into a single patch (2/2).
- Retained original C driver (stinger.c) under CONFIG_JOYSTICK_STINGER and introduced CONFIG_JOYSTICK_STINGER_RUST to avoid regressions on architectures and configurations without Rust support.
- Ensured IS_REACHABLE() guards for C helpers and #[cfg(CONFIG_* = "y")] for rust/kernel modules to prevent unresolved symbols.
clicedvly768 (2):
rust: input: add input device abstractions
rust: serio: add serio bus abstractions and port Gravis Stinger driver
drivers/input/joystick/Kconfig | 14 ++ drivers/input/joystick/Makefile | 3 + drivers/input/joystick/stinger_rust.rs | 191 +++++++++++++++ rust/bindings/bindings_helper.h | 2 + rust/helpers/helpers.c | 2 + rust/helpers/input.c | 22 ++ rust/helpers/serio.c | 27 ++ rust/kernel/input.rs | 253 +++++++++++++++++++ rust/kernel/lib.rs | 4 + rust/kernel/serio.rs | 327 +++++++++++++++++++++++++ 10 files changed, 845 insertions(+) create mode 100644 drivers/input/joystick/stinger_rust.rs create mode 100644 rust/helpers/input.c create mode 100644 rust/helpers/serio.c create mode 100644 rust/kernel/input.rs create mode 100644 rust/kernel/serio.rs
Signature · 1 line
2.56.0
PATCH v5 1/2 rust: input: add input device abstractions
Add safe Rust abstractions for Linux input devices (struct input_dev).
This provides:
- AllocatedDevice: a newly allocated, unregistered input device handle that allows configuring capabilities (keys, absolute axis limits, parent device) before registration.
- Safe string methods: set_name and set_phys accept &'static CStr to prevent use-after-free since the input core does not copy strings.
- set_phys_from_parent: dynamic physical location path formatting ("%s/input0") stored safely in an owned kernel heap buffer.
- Device lifecycle and reference count management: set_parent takes a reference count via bindings::get_device to avoid dangling pointers, releasing it appropriately on drop or post-registration.
- Bounds-checked set_abs_params validating axis < ABS_CNT (0x40) to prevent memory corruption in dev->absinfo.
- RegisteredDevice: a registered input device handle providing safe, inlined methods to report keys, absolute coordinates, and synchronization events. Automatically calls input_unregister_device on drop (RAII).
- Constants for bus types (bus::*) and event types (event_type::*).
- Alphabetically sorted imports and module entries gated with #[cfg(CONFIG_INPUT = "y")] and #if IS_REACHABLE(CONFIG_INPUT) to avoid link failures when CONFIG_INPUT is disabled or modular.
--- rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/input.c | 22 +++ rust/kernel/input.rs | 253 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 5 files changed, 279 insertions(+) create mode 100644 rust/helpers/input.c create mode 100644 rust/kernel/input.rs
rust/bindings/bindings_helper.h +1 -0
@@ -64,6 +64,7 @@#include <linux/fwctl.h> #include <linux/fs.h> #include <linux/i2c.h> +#include <linux/input.h> #include <linux/interrupt.h> #include <linux/io-pgtable.h> #include <linux/ioport.h>
rust/helpers/helpers.c +1 -0
@@ -67,6 +67,7 @@#include "fs.c" #include "fwctl.c" #include "gpu.c" +#include "input.c" #include "interrupt.c" #include "io.c" #include "irq.c"
rust/helpers/input.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/input.h> + +#if IS_REACHABLE (CONFIG_INPUT ) + +__rust_helper void rust_helper_input_report_key(struct input_dev *dev, unsigned int code, int value) +{ +input_report_key (dev, code, value); +} + +__rust_helper void rust_helper_input_report_abs(struct input_dev *dev, unsigned int code, int value) +{ +input_report_abs (dev, code, value); +} + +__rust_helper void rust_helper_input_sync(struct input_dev *dev) +{ +input_sync (dev); +} + +#endif /* IS_REACHABLE(CONFIG_INPUT) */
rust/kernel/input.rs new file +253 -0
253 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -88,6 +88,8 @@#[doc(hidden)] pub mod impl_flags;pub mod init; +#[cfg( CONFIG_INPUT = "y" )] +pub mod input;pub mod interop;pub mod interrupt;pub mod io;
Signature · 1 line
2.56.0
PATCH v5 2/2 rust: serio: add serio bus abstractions and port Gravis Stinger driver
In reply to clicedvly768Add safe Rust abstractions for the serio (Serial I/O) bus subsystem and port the legacy Gravis Stinger serial gamepad driver to safe Rust.
This introduces:
- DeviceId: a transparent wrapper around struct serio_device_id implementing RawDeviceId for type-safe matching tables.
- IdTable and serio_device_table! macro: guarantees a zero sentinel termination entry at compile time and creates modpost device aliases.
- Device: a serio port device instance implementing AsBusDevice, impl_device_context_deref!, and AsRef<device::Device>.
- Port lifecycle & interrupt race protection: T::init initializes driver state and allocates input devices, storing drvdata BEFORE serio_open() opens the hardware port. T::start publishes child devices only after serio_open() succeeds, and T::attach safely binds them under serio_pause_rx to eliminate races with interrupts.
- IrqReturn mapping: properly converts kernel::irq::IrqReturn variants to C irqreturn struct values without UB.
- Driver trait: safe interface for serio drivers (init, start, attach, interrupt, disconnect).
- Registration<T>: safe RAII registration adapter that stores Opaque<bindings::serio_driver> inside Pin<KBox<...>> and calls __serio_register_driver with the caller's &'static ThisModule to correctly track module ownership.
- stinger_rust: port of the Gravis Stinger serial gamepad driver in safe Rust (drivers/input/joystick/stinger_rust.rs), containing zero unsafe blocks, preserving the userspace device name ("Gravis Stinger") for udev/EVIOCGNAME, dynamically formatting phys paths ("%s/input0"), and compiling as the standard stinger.ko module name.
- Non-regression Kconfig: adds CONFIG_JOYSTICK_STINGER_RUST guarded on RUST && SERIO=y && INPUT=y while retaining the original C driver (stinger.c) to maintain hardware support on non-Rust configurations and architectures without HAVE_RUST.
- Gated module entry #[cfg(CONFIG_SERIO = "y")] and helper guards #if IS_REACHABLE(CONFIG_SERIO) to avoid undefined symbols when serio is disabled or modular.
--- drivers/input/joystick/Kconfig | 14 ++ drivers/input/joystick/Makefile | 3 + drivers/input/joystick/stinger_rust.rs | 191 +++++++++++++++ rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/serio.c | 27 ++ rust/kernel/lib.rs | 2 + rust/kernel/serio.rs | 327 +++++++++++++++++++++++++ 8 files changed, 566 insertions(+) create mode 100644 drivers/input/joystick/stinger_rust.rs create mode 100644 rust/helpers/serio.c create mode 100644 rust/kernel/serio.rs
drivers/input/joystick/Kconfig +14 -0
@@ -197,6 +197,20 @@ config JOYSTICK_STINGER To compile this driver as a module, choose M here: the module will be called stinger. +config JOYSTICK_STINGER_RUST + bool "Safe Rust implementation of Gravis Stinger gamepad driver" + depends on JOYSTICK_STINGER + depends on RUST && SERIO = y && INPUT = y + default y + help + Say Y here to use the safe Rust rewrite of the Gravis Stinger + gamepad driver instead of the legacy C implementation. + + The Rust driver provides memory safety guarantees while preserving + full hardware protocol compatibility. + + If unsure, say Y. + config JOYSTICK_TWIDJOY tristate "Twiddler as a joystick" select SERIO
drivers/input/joystick/Makefile +3 -0
@@ -33,6 +33,9 @@ obj-$(CONFIG_JOYSTICK_SENSEHAT) += sensehat-joystick.o obj-$(CONFIG_JOYSTICK_SIDEWINDER) += sidewinder.o obj-$(CONFIG_JOYSTICK_SPACEBALL) += spaceball.o obj-$(CONFIG_JOYSTICK_SPACEORB) += spaceorb.o +ifdef CONFIG_JOYSTICK_STINGER_RUST +stinger-objs := stinger_rust.o +endif obj-$(CONFIG_JOYSTICK_STINGER) += stinger.o obj-$(CONFIG_JOYSTICK_TMDC) += tmdc.o obj-$(CONFIG_JOYSTICK_TURBOGRAFX) += turbografx.o
drivers/input/joystick/stinger_rust.rs new file +191 -0
191 lines not on this page · raw message
rust/bindings/bindings_helper.h +1 -0
@@ -89,6 +89,7 @@#include <linux/sched.h> #include <linux/security.h> #include <linux/serdev.h> +#include <linux/serio.h> #include <linux/slab.h> #include <linux/sys_soc.h> #include <linux/task_work.h>
rust/helpers/helpers.c +1 -0
@@ -96,6 +96,7 @@#include "scatterlist.c" #include "security.c" #include "serdev.c" +#include "serio.c" #include "signal.c" #include "slab.c" #include "spinlock.c"
rust/helpers/serio.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/serio.h> + +#if IS_REACHABLE (CONFIG_SERIO ) + +__rust_helper void *rust_helper_serio_get_drvdata(struct serio *serio) +{ +return serio_get_drvdata (serio); +} + +__rust_helper void rust_helper_serio_set_drvdata(struct serio *serio, void *data) +{ +serio_set_drvdata (serio, data); +} + +__rust_helper void rust_helper_serio_pause_rx(struct serio *serio) +{ +serio_pause_rx (serio); +} + +__rust_helper void rust_helper_serio_continue_rx(struct serio *serio) +{ +serio_continue_rx (serio); +} + +#endif /* IS_REACHABLE(CONFIG_SERIO) */
rust/kernel/lib.rs +2 -0
@@ -134,6 +134,8 @@pub mod seq_file;#[cfg( CONFIG_RUST_SERIAL_DEV_BUS_ABSTRACTIONS )] pub mod serdev; +#[cfg( CONFIG_SERIO = "y" )] +pub mod serio;pub mod sizes;#[cfg( CONFIG_SOC_BUS )] pub mod soc;
rust/kernel/serio.rs new file +327 -0
327 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v5 2/2] rust: serio: add serio bus abstractions and port Gravis Stinger driver
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] rust: serio: invalid irqreturn constructor call in interrupt_callback() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] rust: serio: invalid irqreturn constructor call in interrupt_callback()
--
commit 80f5c0b2557c72b097feba2779e0c1b32a6bd3c7
Author: clicedvly768 <[email protected]>
rust: serio: add serio bus abstractions and port Gravis Stinger driver
This commit adds Rust abstractions for the serio bus subsystem and ports the Gravis Stinger serial gamepad driver to Rust.
Quoted 5 lines
rust/kernel/serio.rs new file +0
-0
[ ... ]
Quoted 6 lines
rust/kernel/serio.rs new file +6
-0+unsafe extern "C" fn interrupt_callback ( + serio: *mut bindings::serio , + data:c_uchar , + flags:c_uint , + ) -> bindings::irqreturn_t { +// SAFETY: `serio` is valid.
rust/kernel/serio.rs line 291
Quoted 10 lines
rust/kernel/serio.rs new file +10
-0+let raw =unsafe { bindings::serio_get_drvdata (serio) }; +if raw.is_null () { +return bindings::irqreturn (IrqReturn ::None as u32 ); + } + +// SAFETY: `raw` was created via `into_foreign()` in `connect_callback`. +let mut borrowed =unsafe { <T ::Data as ForeignOwnable >::borrow_mut (raw) }; +let ret =T ::interrupt (&mut borrowed, data, flags); + bindings::irqreturn (retas u32 ) + }
[Severity: High]
Does calling bindings::irqreturn(...) as a tuple-struct constructor break the build when CONFIG_SERIO=y and CONFIG_RUST=y are enabled?
bindgen emits bindings::irqreturn and bindings::irqreturn_t as type aliases for ffi::c_uint rather than a newtype struct, so invoking bindings::irqreturn with function call syntax fails to compile with E0423. Casting the values with "as bindings::irqreturn_t" avoids this.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Dmitry Torokhov
maintainerRe: [PATCH v5 1/2] rust: input: add input device abstractions
In reply to clicedvly768Quoted 23 lines · On Sat, Nov 07, 2026 at 03:30:32AM +0500, clicedvly768 wrote:
Add safe Rust abstractions for Linux input devices (struct
input_dev).This provides:
-AllocatedDevice: a newly allocated, unregistered input device handle that allows configuring capabilities (keys, absolute axis limits, parent device) before registration.
- Safe string methods:set_nameandset_physaccept&'static CStrto prevent use-after-free since the input core does not copy strings.
-set_phys_from_parent: dynamic physical location path formatting ("%s/input0") stored safely in an owned kernel heap buffer.
- Device lifecycle and reference count management:set_parenttakes a reference count viabindings::get_deviceto avoid dangling pointers, releasing it appropriately on drop or post-registration.
- Bounds-checkedset_abs_paramsvalidatingaxis < ABS_CNT(0x40) to prevent memory corruption indev->absinfo.
-RegisteredDevice: a registered input device handle providing safe, inlined methods to report keys, absolute coordinates, and synchronization events. Automatically callsinput_unregister_deviceon drop (RAII).
- Constants for bus types (bus::*) and event types (event_type::*).
- Alphabetically sorted imports and module entries gated with#[cfg(CONFIG_INPUT = "y")]and#if IS_REACHABLE(CONFIG_INPUT)to avoid link failures whenCONFIG_INPUTis disabled or modular.
Quoted 1 line
Signed-off-by clicedvly768 <[email protected]>
I am sorry, who??
If you are not willing to put your name when "certifying" a body of code I am not interested in looking at it.
Thanks.
Signature · 1 line
Dmitry