Skip to contentSkip to conversation

rust: input: add input/serio abstractions and port Gravis Stinger driver

PATCH Superseded clicedvly768 2 participants 5 messages Nov 6

Subsystems (3)

v2 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v2 cover rust: input: add input/serio abstractions and port Gravis Stinger driver

This series introduces safe Rust abstractions for the Linux input and serio (Serial I/O) subsystems and demonstrates their real-world usage by porting the legacy Gravis Stinger serial gamepad driver to safe Rust.

Following community review, the serio bus abstractions and the Stinger driver port have been consolidated into the second patch, while keeping the input device abstractions in the first patch.

Motivation and Design:
- In the Rust for Linux (RfL) philosophy, device drivers should not rely on raw FFI and unsafe blocks. Instead, the kernel crate provides safe, idiomatic Rust abstractions.
- The Gravis Stinger gamepad is an ideal candidate for demonstrating safe Rust driver development in the input subsystem: it uses a well-defined 4-byte serial protocol at 9600 baud, 10 buttons, and 2 analog axes.
- The ported driver (drivers/input/joystick/stinger_rust.rs) contains ZERO unsafe blocks, using RAII types (AllocatedDevice, RegisteredDevice, serio::Registration) for memory safety and device lifetime management.
- Non-regression: CONFIG_JOYSTICK_STINGER_RUST is offered alongside the original C driver (stinger.c), ensuring architectures without Rust (e.g. powerpc defconfigs) suffer no hardware support regressions.

Testing:
- Built with Clang/LLVM and rustc 1.85.
- Passes all strict kernel Rust checks: make LLVM=1 rustfmtcheck, make LLVM=1 rustdoc, and make LLVM=1 CLIPPY=1 with zero warnings.
- Tested both built-in (CONFIG_JOYSTICK_STINGER=y) and loadable module (CONFIG_JOYSTICK_STINGER=m) configurations.
- Tested end-to-end in a custom QEMU virtual test environment with serial port loopback (serport line discipline N_MOUSE and SERIO_STINGER).
- Verified event reporting (EV_KEY, EV_ABS, EV_SYN) via evtest and an automated test suite across all 10 buttons and
both axes (-64..64).

Changes in v2:
- Consolidated serio abstractions and Stinger driver port into patch 2/2.
- Gated built-in Rust modules and helpers with #[cfg(CONFIG_* = "y")] and #if IS_REACHABLE(CONFIG_*) to prevent vmlinux link failures when CONFIG_INPUT or CONFIG_SERIO is compiled as a module or disabled.
- Retained original stinger.c and introduced JOYSTICK_STINGER_RUST to avoid hardware support regressions on architectures without RUST.
- Replaced raw slice &'static [DeviceId] with kernel::device_id::RawDeviceId and kernel::device_id::IdTable via kernel::serio_device_table!, guaranteeing compile-time zero-sentinel termination and exporting modpost device aliases.
- Fixed module owner tracking: removed helpers.o registration wrappers; Registration::new now takes &'static ThisModule and calls bindings::__serio_register_driver with the driver's own module pointer.
- Stored Opaque<bindings::serio_driver> in Pin<KBox<...>> to avoid aliasing violations on C-owned memory.
- Fixed return error code sign in connect_callback (to_errno() is already negative).
- Reordered port opening: serio_open is called before publishing the input device to userspace.
- Reused kernel::irq::IrqReturn instead of duplicating a custom enum.
- Integrated serio::Device with generic device infrastructure:
implemented AsRef<device::Device> and Deref<Target = device::Device>, and added AllocatedDevice::set_parent() in input.rs to maintain the sysfs device hierarchy (&serio->dev).
- Switched AllocatedDevice::set_phys() to &'static CStr.
- Added bounds check axis < ABS_CNT in set_abs_params().
- Added #[inline] on small helper methods and used vertical imports.

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        |   4 +
 drivers/input/joystick/stinger_rust.rs | 178 +++++++++++++++++
 rust/bindings/bindings_helper.h        |   2 +
 rust/helpers/helpers.c                 |   2 +
 rust/helpers/input.c                   |  22 +++
 rust/helpers/serio.c                   |  17 ++
 rust/kernel/input.rs                   | 214 +++++++++++++++++++++
 rust/kernel/lib.rs                     |   4 +
 rust/kernel/serio.rs                   | 256 +++++++++++++++++++++++++
 10 files changed, 713 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
#1 lore raw

PATCH v2 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 axes 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.
- 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            | 214 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   2 +
 5 files changed, 240 insertions(+)
 create mode 100644 rust/helpers/input.c
 create mode 100644 rust/kernel/input.rs
rust/bindings/bindings_helper.h +1 -0
index 3791af0aadc7..4b09d3980e10 100644
@@ -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
index d62c7be1ca7b..77cec85ad153 100644
@@ -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
new file mode 100644
index 000000000000..f43a06acfdc3
@@ -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 +214 -0

214 lines not on this page · raw message

rust/kernel/lib.rs +2 -0
index 1ab41129d557..ff5c978758d0 100644
@@ -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
#3 lore raw

PATCH v2 2/2 rust: serio: add serio bus abstractions and port Gravis Stinger driver

In reply to clicedvly768

Add 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 borrowed serio port device instance implementing AsRef<device::Device> and Deref<Target = device::Device>.
- IrqReturn: re-exports kernel::irq::IrqReturn for interrupt handling.
- Driver trait: safe interface for serio drivers (connect, interrupt, disconnect).
- Registration<T>: safe RAII registration adapter that stores Opaque<bindings::serio_driver> inside Pin<KBox<...>> to avoid aliasing violations, and calls __serio_register_driver with the caller's &'static ThisModule to correctly track module ownership.
- Port management in connect_callback: opens the serio port prior to probe completion, sets driver data safely, and correctly propagates negative error numbers.
- stinger_rust: port of the Gravis Stinger serial gamepad driver in safe Rust (drivers/input/joystick/stinger_rust.rs), containing zero unsafe blocks, using kernel::input and kernel::serio abstractions.
- 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        |   4 +
 drivers/input/joystick/stinger_rust.rs | 178 +++++++++++++++++
 rust/bindings/bindings_helper.h        |   1 +
 rust/helpers/helpers.c                 |   1 +
 rust/helpers/serio.c                   |  17 ++
 rust/kernel/lib.rs                     |   2 +
 rust/kernel/serio.rs                   | 256 +++++++++++++++++++++++++
 8 files changed, 473 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
index 7755e5b454d2..89cc1dd9d865 100644
@@ -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 +4 -0
index 9976f596a920..786f950dcf52 100644
@@ -33,7 +33,11 @@ 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
+obj-$(CONFIG_JOYSTICK_STINGER)		+= stinger_rust.o
+else
 obj-$(CONFIG_JOYSTICK_STINGER)		+= stinger.o
+endif
 obj-$(CONFIG_JOYSTICK_TMDC)		+= tmdc.o
 obj-$(CONFIG_JOYSTICK_TURBOGRAFX)	+= turbografx.o
 obj-$(CONFIG_JOYSTICK_TWIDJOY)		+= twidjoy.o
drivers/input/joystick/stinger_rust.rs new file +178 -0

178 lines not on this page · raw message

rust/bindings/bindings_helper.h +1 -0
index 4b09d3980e10..0e44af5d770c 100644
@@ -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
index 77cec85ad153..61cf3f77e85a 100644
@@ -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 +17 -0
new file mode 100644
index 000000000000..849d0d48ac62
@@ -0,0 +1,17 @@
+// 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);
+}
+
+#endif /* IS_REACHABLE(CONFIG_SERIO) */
rust/kernel/lib.rs +2 -0
index ff5c978758d0..711b7ef06b57 100644
@@ -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 +256 -0

256 lines not on this page · raw message

Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH v2 1/2] rust: input: add input device abstractions

In reply to clicedvly768
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] rust: input: unreferenced parent pointer in AllocatedDevice::set_parent() - [Low] rust: input…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] rust: input: unreferenced parent pointer in AllocatedDevice::set_parent()
- [Low] rust: input: missing #[inline] on C wrapper methods in input.rs
- [Low] rust: input: non-vertical import style and redundant imports in input.rs
- [Low] rust: input: duplicate error cleanup in AllocatedDevice::register()
--

commit e772f543b269b5d6668847be715161384d0d5953
Author: clicedvly768 <[email protected]>

rust: input: add input device abstractions

This commit adds Rust abstractions for allocating, configuring, and registering Linux input devices (struct input_dev).

Quoted 12 lines
rust/kernel/input.rs new file +6 -0
new file mode 100644
index 0000000000000..bef0c3a07da06
@@ -0,0 +1,214 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Input subsystem abstractions for safe Rust drivers.
+//!
+//! Provides a safe Rust object-oriented interface around Linux `struct input_dev`,
+//! ensuring safe allocation, capability configuration, registration, and RAII cleanup.

rust/kernel/input.rs line 16

Quoted 10 lines
rust/kernel/input.rs new file +10 -0
+
+use crate::{
+    bindings, device,
+    error::{
+        code::{EINVAL, ENOMEM},
+        Result,
+    },
+    prelude::*,
+    str::CStr,
+};

[Severity: Low]
This isn't a bug, but the imports here group multiple items on a single line without trailing comments instead of using the kernel vertical import style. Also, EINVAL, ENOMEM, Error, Result, and CStr are already exported by crate::prelude::*, so importing them explicitly is redundant.

[ ... ]

rust/kernel/input.rs line 97

Quoted 8 lines
rust/kernel/input.rs new file +8 -0
+    /// Sets the parent device for this input device.
+    #[inline]
+    pub fn set_parent(&mut self, parent: &device::Device) {
+        // SAFETY: `self.ptr` is valid, and `parent.as_raw()` returns a valid `struct device`.
+        unsafe {
+            (*self.ptr.as_ptr()).dev.parent = parent.as_raw();
+        }
+    }

[Severity: Medium]
Can AllocatedDevice::set_parent() leave dev.parent as a dangling pointer?

set_parent() takes a short-lived &device::Device borrow and stores parent.as_raw() in dev.parent without holding a reference count or tying the lifetime of AllocatedDevice to parent. The C driver core does not call get_device(dev->parent) until input_register_device() -> device_add(), and input_alloc_absinfo() also dereferences dev->dev.parent before registration on allocation failure. If a caller drops the parent device after set_parent() returns, a subsequent call to set_capability(), set_abs_params(), or register() from safe Rust will trigger a use-after-free.

Quoted 43 lines
rust/kernel/input.rs new file +43 -0
+    /// Enables support for a capability (e.g. key or axis).
+    #[inline]
+    pub fn set_capability(&mut self, type_: u32, code: u32) {
+        // SAFETY: `self.ptr` is valid and type/code are valid capability values.
+        unsafe {
+            bindings::input_set_capability(self.ptr.as_ptr(), type_, code);
+        }
+    }
+
+    /// Sets parameter limits for an absolute axis.
+    ///
+    /// Bounds-checks `axis < ABS_CNT` (0x40) to prevent out-of-bounds writes
+    /// to the `absinfo` array in `struct input_dev`.
+    pub fn set_abs_params(
+        &mut self,
+        axis: u32,
+        min: i32,
+        max: i32,
+        fuzz: i32,
+        flat: i32,
+    ) -> Result {
+        if axis >= bindings::ABS_CNT {
+            return Err(EINVAL);
+        }
+
+        // SAFETY: `self.ptr` is valid and `axis` has been verified within bounds.
+        unsafe {
+            bindings::input_set_abs_params(self.ptr.as_ptr(), axis, min, max, fuzz, flat);
+        }
+        Ok(())
+    }
+
+    /// Registers the input device with the kernel input core.
+    ///
+    /// Consumes the `AllocatedDevice` and returns a [`RegisteredDevice`].
+    pub fn register(self) -> Result<RegisteredDevice> {
+        let ptr = self.ptr;
+        // Don't run the AllocatedDevice drop destructor, registration takes ownership.
+        core::mem::forget(self);
+
+        // SAFETY: `ptr` is valid and was allocated by `input_allocate_device`.
+        let ret = unsafe { bindings::input_register_device(ptr.as_ptr()) };
+        if ret != 0 {

rust/kernel/input.rs line 151

Quoted 10 lines
rust/kernel/input.rs new file +10 -0
+            // SAFETY: Free the allocated device if registration failed.
+            unsafe {
+                bindings::input_free_device(ptr.as_ptr());
+            }
+            return Err(crate::error::Error::from_errno(ret));
+        }
+
+        Ok(RegisteredDevice { ptr })
+    }
+}

[Severity: Low]
This isn't a bug, but calling core::mem::forget(self) before input_register_device() forces the error path in register() to manually call input_free_device() and Error::from_errno(), duplicating Drop::drop() for AllocatedDevice and error::to_result(). Checking to_result(unsafe { bindings::input_register_device(self.ptr.as_ptr()) })? before forgetting self would let Drop handle cleanup on failure.

Quoted 8 lines
rust/kernel/input.rs new file +8 -0
+impl Drop for AllocatedDevice {
+    fn drop(&mut self) {
+        // SAFETY: `self.ptr` was allocated by `input_allocate_device` and has not been registered.
+        unsafe {
+            bindings::input_free_device(self.ptr.as_ptr());
+        }
+    }
+}

[ ... ]

rust/kernel/input.rs line 208

Quoted 9 lines
rust/kernel/input.rs new file +9 -0
+impl Drop for RegisteredDevice {
+    fn drop(&mut self) {
+        // SAFETY: `self.ptr` was successfully registered with `input_register_device`.
+        // Unregistering automatically unbinds and frees the device.
+        unsafe {
+            bindings::input_unregister_device(self.ptr.as_ptr());
+        }
+    }
+}

[Severity: Low]
Small functions in kernel crate abstractions that forward to C bindings should be marked #[inline]. AllocatedDevice::new(), AllocatedDevice::set_abs_params(), AllocatedDevice::register(), <AllocatedDevice as Drop>::drop(), and <RegisteredDevice as Drop>::drop() are missing #[inline].

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH v2 2/2] rust: serio: add serio bus abstractions and port Gravis Stinger driver

In reply to clicedvly768
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] rust: serio: premature serio_open() and unlocked drvdata in connect_callback() - [Medium] input…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] rust: serio: premature serio_open() and unlocked drvdata in connect_callback()
- [Medium] input: stinger_rust changes device name, phys path, description, and module name
- [Medium] rust: serio: invalid irqreturn_t and IRQ_NONE bindings in interrupt_callback()
- [Low] rust: serio: Device and Registration bypass generic driver abstractions
- [Low] rust: serio: non-vertical import style in serio.rs and stinger_rust.rs
--

commit 986d37559af3527a1a01463ffbfccd5c47d94c58
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
drivers/input/joystick/stinger_rust.rs new file +0 -0
new file mode 100644
index 0000000000000..6250d628ee3da

[ ... ]

Quoted 3 lines
drivers/input/joystick/stinger_rust.rs new file +3 -0
+const DRIVER_NAME: &CStr = c"stinger";
+const DRIVER_DESC: &CStr = c"Gravis Stinger gamepad";
+const DRIVER_PHYS: &CStr = c"serio0/input0";

[ ... ]

drivers/input/joystick/stinger_rust.rs line 97

Quoted 5 lines
drivers/input/joystick/stinger_rust.rs new file +5 -0
+    fn connect(dev: &mut serio::Device) -> Result<Self::Data> {
+        let mut input_alloc = AllocatedDevice::new()?;
+
+        input_alloc.set_name(DRIVER_DESC);
+        input_alloc.set_phys(DRIVER_PHYS);

[Severity: Medium]
Does replacing stinger.c with stinger_rust.rs when CONFIG_JOYSTICK_STINGER_RUST=y change the userspace-visible device and module attributes?

In drivers/input/joystick/stinger.c, stinger_connect() sets input_dev->name to "Gravis Stinger" and formats input_dev->phys dynamically as "%s/input0" from serio->phys. Setting the name to "Gravis Stinger gamepad" breaks userspace controller mappings and udev rules that match EVIOCGNAME, and hardcoding "serio0/input0" (leaving serio::Device::phys() unused) reports the wrong physical path on any port other than serio0 or when multiple devices are attached.

In addition, drivers/input/joystick/Makefile builds stinger_rust.ko (with module! name "stinger_rust") instead of stinger.ko as documented in the CONFIG_JOYSTICK_STINGER Kconfig help text, and Registration::new() sets serio_driver.description to "stinger" instead of "Gravis Stinger gamepad driver".

Quoted 13 lines
rust/kernel/serio.rs new file +7 -0
new file mode 100644
index 0000000000000..b4a8ec7139a28
@@ -0,0 +1,256 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Serio (Serial I/O) bus abstractions for safe Rust drivers.
+//!
+//! Provides a safe Rust interface for serio devices and drivers in the Linux kernel,
+//! supporting input devices such as serial gamepads (e.g. Gravis Stinger), mice, and keyboards.
+

rust/kernel/serio.rs line 17

Quoted 10 lines
rust/kernel/serio.rs new file +10 -0
+use crate::{
+    bindings, device,
+    device_id::{IdTable as GenericIdTable, RawDeviceId},
+    error::{to_result, Result},
+    ffi::{c_int, c_uchar, c_uint},
+    prelude::*,
+    types::{ForeignOwnable, Opaque},
+    ThisModule,
+};
+use core::{marker::PhantomData, ops::Deref, pin::Pin, ptr::NonNull};

[Severity: Low]
This isn't a bug, but the imports here and in drivers/input/joystick/stinger_rust.rs group multiple items on single horizontal lines instead of following the kernel vertical import style in Documentation/rust/coding-guidelines.rst.

[ ... ]

Quoted 4 lines
rust/kernel/serio.rs new file +4 -0
+/// A serio port device instance passed to driver callbacks.
+pub struct Device {
+    ptr: NonNull<bindings::serio>,
+}

[ ... ]

rust/kernel/serio.rs line 127

Quoted 5 lines
rust/kernel/serio.rs new file +5 -0
+/// Driver adapter that bridges the safe Rust [`Driver`] trait to the C serio bus subsystem.
+pub struct Registration<T: Driver> {
+    drv: Pin<KBox<Opaque<bindings::serio_driver>>>,
+    _p: PhantomData<T>,
+}

[Severity: Low]
Should serio::Device and serio::Registration use the generic bus device and driver registration infrastructure in the kernel crate instead of custom wrappers?

Other bus abstractions (such as serdev, i2c, platform, and pci) define Device<Ctx: device::DeviceContext = device::Normal> as a repr(transparent) wrapper around Opaque<bindings::...> implementing device::AsBusDevice<Ctx> and AlwaysRefCounted, and implement driver::DriverLayout and driver::RegistrationOps on an Adapter<T> type. Because serio::Device lacks a DeviceContext parameter, driver callbacks do not receive &serio::Device<device::Bound> and cannot use device-managed resources such as devres::register() or Devres::new().

[ ... ]

Quoted 23 lines
rust/kernel/serio.rs new file +23 -0
+    unsafe extern "C" fn connect_callback(
+        serio: *mut bindings::serio,
+        drv: *mut bindings::serio_driver,
+    ) -> c_int {
+        let Some(serio_nn) = NonNull::new(serio) else {
+            return crate::error::code::EINVAL.to_errno();
+        };
+
+        // SAFETY: `serio` and `drv` are valid non-null pointers provided by the kernel.
+        let ret = unsafe { bindings::serio_open(serio, drv) };
+        if ret != 0 {
+            return ret;
+        }
+
+        let mut dev = Device { ptr: serio_nn };
+
+        let data = match T::connect(&mut dev) {
+            Ok(d) => d,
+            Err(e) => {
+                // SAFETY: Close the opened port on probe error.
+                unsafe { bindings::serio_close(serio) };
+                return e.to_errno();
+            }

rust/kernel/serio.rs line 200

Quoted 10 lines
rust/kernel/serio.rs new file +10 -0
+        };
+
+        let raw_data = data.into_foreign();
+        // SAFETY: `serio` is valid; store driver data pointer.
+        unsafe {
+            bindings::serio_set_drvdata(serio, raw_data.cast());
+        }
+
+        0
+    }

[Severity: High]
Does calling serio_open() before T::connect() and serio_set_drvdata() race with incoming serial interrupts?

serio_open() binds serio->drv under serio->lock and immediately enables port interrupts via serio->open(serio), while T::connect() sleeps (for example in AllocatedDevice::new(), input_alloc.register(), and KBox::new() in StingerDriver::connect()) before drvdata is populated:

CPU0 CPU1 (<hardirq>)
connect_callback()
serio_open(serio, drv)
T::connect(&mut dev)

                                      serio_interrupt()
                                        spin_lock_irqsave(&serio->lock)
                                        interrupt_callback()
                                          serio_get_drvdata() -> NULL
                                          return IRQ_NONE  <-- drops byte

serio_set_drvdata(serio, raw_data)
interrupt_callback()
serio_get_drvdata() <-- data race ForeignOwnable::borrow_mut(raw)

First, any bytes arriving while T::connect() runs are dropped because drvdata is still NULL. In StingerDriver::interrupt(), packets are framed solely by counting 4 bytes (stinger.idx == 4), so dropping 1 to 3 bytes during connect permanently misaligns all subsequent packet decoding.

Second, serio_set_drvdata() performs a plain unlocked store to serio->dev.driver_data without holding serio->lock or issuing a release barrier, racing with interrupt_callback() reading serio_get_drvdata() and dereferencing T::Data on weakly-ordered architectures.

Third, StingerDriver::connect() calls input_alloc.register() before KBox::new(Stinger { ... }, GFP_KERNEL) and before drvdata is set, exposing the input device to userspace before driver initialization can no longer fail.

In C serio drivers such as stinger_connect() in drivers/input/joystick/stinger.c, driver state is allocated and stored via serio_set_drvdata() before calling serio_open() (which synchronizes under serio->lock in serio_set_drv()), and input_register_device() is called after serio_open() succeeds.

Quoted 11 lines
rust/kernel/serio.rs new file +11 -0
+
+    /// # Safety
+    ///
+    /// Must only be invoked by the serio subsystem during an interrupt on a connected port.
+    /// `serio` must be a valid pointer.
+    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 221

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::IRQ_NONE;
+        }
+
+        // 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_t(ret as c_uint)
+    }

[Severity: Medium]
Does interrupt_callback() fail to compile when CONFIG_SERIO=y and CONFIG_RUST=y?

bindgen generates irqreturn and irqreturn_t as type aliases for ffi::c_uint along with top-level constants (as seen in rust/kernel/irq/request.rs), not a module or newtype struct. bindings::irqreturn::IRQ_NONE does not exist (it is bindings::irqreturn_IRQ_NONE), and bindings::irqreturn_t(ret as c_uint) attempts to call a type alias as a tuple struct constructor instead of casting with ret as bindings::irqreturn_t.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2