linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: clicedvly768 <harepro29@gmail.com>
To: ojeda@kernel.org, dmitry.torokhov@gmail.com
Cc: rust-for-linux@vger.kernel.org, linux-input@vger.kernel.org,
	clicedvly768 <harepro29@gmail.com>
Subject: [PATCH v2 0/2] rust: input: add input/serio abstractions and port Gravis Stinger driver
Date: Sat,  7 Nov 2026 01:14:27 +0500	[thread overview]
Message-ID: <20261106201429.376997-1-harepro29@gmail.com> (raw)

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

-- 
2.56.0


             reply	other threads:[~2026-11-06 20:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06 20:14 clicedvly768 [this message]
2026-11-06 20:14 ` [PATCH v2 1/2] rust: input: add input device abstractions clicedvly768
2026-11-06 20:27   ` sashiko-bot
2026-11-06 20:14 ` [PATCH v2 2/2] rust: serio: add serio bus abstractions and port Gravis Stinger driver clicedvly768
2026-11-06 20:33   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261106201429.376997-1-harepro29@gmail.com \
    --to=harepro29@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).