rust: add basic serial device bus abstractions
Subsystems (7)
Markus Probst
maintainerv7 cover rust: add basic serial device bus abstractions
This patch series adds the serdev device bus rust abstraction into the kernel.
This abstraction will be used by a driver, which targets the MCU devices in Synology devices.
Kari Argillander also messaged me, stating that he wants to write a watchdog driver with this abstraction (needing initial device data).
@Rob: Are you willing to maintain these rust abstractions yourself, as you are the expert on this subsystem, otherwise I would take care of it with a "SERIAL DEVICE BUS [RUST]" section in the MAINTAINERS file. In the second case, I assume you are going to pick those patches as-is into your tree, after they have been reviewed?
This series depends on [1].
[1]
https://lore.kernel.org/driver-core/[email protected]/
---
Changes in v7:
- adapted to driver-lifetime patch series
- Link to v6: https://patch.msgid.link/[email protected]
Changes in v6:
- rebased onto v7.1-rc1
- Link to v5: https://patch.msgid.link/[email protected]
Changes in v5:
- fix typo in documentation
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- fixed not selecting rust serdev abstraction in sample
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- fix vertical import style
- add Kconfig entry for the rust abstraction
- fix documentation in include/linux/serdev.h
- rename private_data to rust_private_data
- fix complete_all <-> wait_for_completion typo
- move drvdata_borrow call after the completion
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- fix documentation in serdev::Driver::write and serdev::Driver::write_all
- remove use of dev_info in probe from the sample
- remove properties_parse from the sample
- add optional baudrate property to the sample
- remove 1. patch
- remove TryFrom<&device::Device<Ctx>> for &serdev::Device<Ctx> implementation
- fix import style
- add patch to return reference in devres::register to fix safety
issue
- add patch to add private data to serdev_device, to fix Device.drvdata() from failing
- simplify abstraction by removing ability to receive the initial transmission. It may be added later in a separate patch series if needed.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Markus Probst (4):
rust: devres: return reference in devres::register
serdev: add rust private data to serdev_device
rust: add basic serial device bus abstractions
samples: rust: add Rust serial device bus sample device driver
drivers/tty/serdev/Kconfig | 7 + include/linux/serdev.h | 15 +- rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/serdev.c | 22 ++ rust/kernel/cpufreq.rs | 3 +- rust/kernel/devres.rs | 15 +- rust/kernel/drm/driver.rs | 3 +- rust/kernel/lib.rs | 2 + rust/kernel/serdev.rs | 571 +++++++++++++++++++++++++++++++++++++ samples/rust/Kconfig | 11 + samples/rust/Makefile | 1 + samples/rust/rust_driver_serdev.rs | 86 ++++++ 13 files changed, 728 insertions(+), 10 deletions(-)
---
base-commit: 5e9b7d093f3f77cb0af4409559e3d139babfb443
change-id: 20251217-rust_serdev-ee5481e9085c
prerequisite-message-id: [email protected]
prerequisite-patch-id: 925690ac15ddd64777b2da34b343d2547bbf79e8
prerequisite-patch-id: 67318671a5eed5fb4ad23a450f1cf0e442bf8ca2
prerequisite-message-id: [email protected]
prerequisite-patch-id: 87127047dbf7d948ae98f1ff0f4a782214a516e2
prerequisite-patch-id: 7664165039fb510368a721edfe60f5b67449b7b6
prerequisite-patch-id: 1e8c97720bab512d8646409c129edd6047164c4c
prerequisite-patch-id: f0fdd1e9912f31a3945f1c0f227dcb64cf71885d
prerequisite-patch-id: be515f7e13f7800eaadb3ed85dec6a9ddb733828
prerequisite-patch-id: 3a3c7749e017d9335f58497404d1350e96caf471
prerequisite-patch-id: 3526c9154f581497a11465b936d83ef61a875454
prerequisite-patch-id: 65d8c757b52475c2acc7d22ddc92cd3f0152b55d
prerequisite-patch-id: 4bd31f1414d5248dc080884caadf5f21684a8427
prerequisite-patch-id: 7beadbb0da3e589ed86d12f512d1c83427dd82b4
prerequisite-patch-id: f04f5427c592cb078c08ee071b965ef6fd4a9a48
prerequisite-patch-id: 64c0098c3d2420f82c89f44fccd3eed459557bfc
prerequisite-patch-id: 3294340ecd964b8e3e87e5a787ff1ffa28e0b698
prerequisite-patch-id: b2c14d06f068dc6a52814fc55d1188737dc29861
prerequisite-patch-id: c7fd96983b606bc42cc5b003022b8fa2bb1c0c34
prerequisite-patch-id: 8e9f4c2d5521fb6cdc5bed2d6661f4bbed5eb63d
prerequisite-patch-id: d868f7925ee9dfe44c5ddf50f4deb50263ff203f
prerequisite-patch-id: 5677248690560ec45e512a8cdbbf8aecfad62d77
prerequisite-patch-id: 5d9674da330ee9d0f4c91edb8acc20727ba1cd9c
prerequisite-patch-id: d5f29453bfd2e3354fb96b57d7a55521bedb2b0b
prerequisite-patch-id: 9d6f96e26fe651304e950766d6bc2006dd33e86a
prerequisite-patch-id: c7a73c58c6d4ca8556d29ce651ad78cc647f6f31
prerequisite-patch-id: c5f35030637e78ab6f5fa489172f1506493fbae1
prerequisite-patch-id: aad19e48d4fa45734050bba927a5b72d6def9673
Markus Probst
maintainerPATCH v7 1/4 rust: devres: return reference in `devres::register`
Return the reference to the initialized data in the devres::register function.
This is needed in a following commit (rust: add basic serial device bus abstractions).
--- rust/kernel/cpufreq.rs | 3 ++- rust/kernel/devres.rs | 15 +++++++++++++-- rust/kernel/drm/driver.rs | 3 ++- 3 files changed, 17 insertions(+), 4 deletions(-)
rust/kernel/cpufreq.rs +2 -1
@@ -1052,7 +1052,8 @@ pub fn new_foreign_owned(dev: &Device<Bound>) -> Result where T: 'static, { - devres::register(dev, Self::new()?, GFP_KERNEL) + devres::register(dev, Self::new()?, GFP_KERNEL)?; +Ok (()) } }
rust/kernel/devres.rs +13 -2
@@ -438,15 +438,26 @@ fn register_foreign<P>(dev: &Device<Bound>, data: P) -> Result/// } /// /// fn from_bound_context(dev: &Device<Bound>) -> Result { -/// devres::register(dev, Registration::new(), GFP_KERNEL) +/// devres::register(dev, Registration::new(), GFP_KERNEL)?; +/// Ok(()) /// } /// ``` -pub fn register <T ,E >(dev: &Device <Bound >, data:impl PinInit <T ,E >, flags:Flags ) ->Result +pub fn register <'a,T ,E >( + dev: &'aDevice <Bound >, + data:impl PinInit <T ,E >, + flags:Flags , +) ->Result <&'aT >where T :Send + 'static,Error :From <E >, {let data =KBox ::pin_init (data, flags)?; +let data_ptr = &raw const *data; +register_foreign (dev, data) +// SAFETY: `dev` is valid for the lifetime of 'a. As long as there is a reference to +// `Device<Bound>`, it is guaranteed that the device is not unbound and data has not been +// dropped. Thus `data_ptr` is also valid for the lifetime of 'a. + .map (|()|unsafe { &*data_ptr }) }
rust/kernel/drm/driver.rs +2 -1
@@ -147,7 +147,8 @@ pub fn new_foreign_owned(let reg =Registration ::<T >::new (drm, flags)?; - devres::register (dev, reg,GFP_KERNEL ) + devres::register (dev, reg,GFP_KERNEL )?; +Ok (()) }/// Returns a reference to the `Device` instance for this registration.
Signature · 1 line
2.53.0
Markus Probst
maintainerPATCH v7 2/4 serdev: add rust private data to serdev_device
In reply to Markus ProbstAdd rust private data to struct serdev_device, as it is required by the rust abstraction added in the following commit
(rust: add basic serial device bus abstractions).
--- include/linux/serdev.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
include/linux/serdev.h +9 -6
@@ -33,12 +33,14 @@ struct serdev_device_ops {/** * struct serdev_device - Basic representation of an serdev device -* @dev: Driver model representation of the device. -* @nr: Device number on serdev bus. -* @ctrl: serdev controller managing this device. -* @ops: Device operations. -* @write_comp: Completion used by serdev_device_write() internally -* @write_lock: Lock to serialize access when writing data +* @dev: Driver model representation of the device. +* @nr: Device number on serdev bus. +* @ctrl: serdev controller managing this device. +* @ops: Device operations. +* @write_comp: Completion used by serdev_device_write() internally +* @write_lock: Lock to serialize access when writing data +* @rust_private_data: Private data for the rust abstraction. This should +* not be used by the C drivers. */ struct serdev_device {struct device dev;
@@ -47,6 +49,7 @@ struct serdev_device {const struct serdev_device_ops *ops;struct completion write_comp;struct mutex write_lock; +void *rust_private_data; };#define to_serdev_device (d) container_of_const(d, struct serdev_device, dev)
Signature · 1 line
2.53.0
Markus Probst
maintainerPATCH v7 3/4 rust: add basic serial device bus abstractions
In reply to Markus ProbstImplement the basic serial device bus abstractions required to write a serial device bus device driver with or without the need for initial device data. This includes the following data structures:
The serdev::Driver trait represents the interface to the driver.
The serdev::Device abstraction represents a struct serdev_device.
In order to provide the Serdev specific parts to a generic driver::Registration the driver::RegistrationOps trait is implemented by serdev::Adapter.
--- drivers/tty/serdev/Kconfig | 7 + rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/serdev.c | 22 ++ rust/kernel/lib.rs | 2 + rust/kernel/serdev.rs | 571 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 604 insertions(+)
drivers/tty/serdev/Kconfig +7 -0
@@ -9,6 +9,13 @@ menuconfig SERIAL_DEV_BUS Note that you typically also want to enable TTY port controller support. +config RUST_SERIAL_DEV_BUS_ABSTRACTIONS + bool "Rust Serial device bus abstractions" + depends on RUST + select SERIAL_DEV_BUS + help + This enables the Rust abstraction for the serial device bus API. + if SERIAL_DEV_BUS config SERIAL_DEV_CTRL_TTYPORT
rust/bindings/bindings_helper.h +1 -0
@@ -84,6 +84,7 @@#include <linux/regulator/consumer.h> #include <linux/sched.h> #include <linux/security.h> +#include <linux/serdev.h> #include <linux/slab.h> #include <linux/sys_soc.h> #include <linux/task_work.h>
rust/helpers/helpers.c +1 -0
@@ -86,6 +86,7 @@#include "regulator.c" #include "scatterlist.c" #include "security.c" +#include "serdev.c" #include "signal.c" #include "slab.c" #include "spinlock.c"
rust/helpers/serdev.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/serdev.h> + +__rust_helper +void rust_helper_serdev_device_driver_unregister(struct serdev_device_driver *sdrv) +{ +serdev_device_driver_unregister (sdrv); +} + +__rust_helper +void rust_helper_serdev_device_put(struct serdev_device *serdev) +{ +serdev_device_put (serdev); +} + +__rust_helper +void rust_helper_serdev_device_set_client_ops(struct serdev_device *serdev, +const struct serdev_device_ops *ops) +{ +serdev_device_set_client_ops (serdev, ops); +}
rust/kernel/lib.rs +2 -0
@@ -118,6 +118,8 @@pub mod scatterlist;pub mod security;pub mod seq_file; +#[cfg( CONFIG_RUST_SERIAL_DEV_BUS_ABSTRACTIONS )] +pub mod serdev;pub mod sizes;#[cfg( CONFIG_SOC_BUS )] pub mod soc;
rust/kernel/serdev.rs new file +571 -0
571 lines not on this page · raw message
Signature · 1 line
2.53.0
Markus Probst
maintainerPATCH v7 4/4 samples: rust: add Rust serial device bus sample device driver
In reply to Markus ProbstAdd a sample Rust serial device bus device driver illustrating the usage of the serial device bus abstractions.
This drivers probes through either a match of device / driver name or a match within the OF ID table.
--- samples/rust/Kconfig | 11 +++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_serdev.rs | 86 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+)
samples/rust/Kconfig +11 -0
@@ -161,6 +161,17 @@ config SAMPLE_RUST_DRIVER_AUXILIARY If unsure, say N. +config SAMPLE_RUST_DRIVER_SERDEV + tristate "Serial Device Bus Device Driver" + select RUST_SERIAL_DEV_BUS_ABSTRACTIONS + help + This option builds the Rust serial device bus driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_serdev. + + If unsure, say N. + config SAMPLE_RUST_SOC tristate "SoC Driver" select SOC_BUS
samples/rust/Makefile +1 -0
@@ -14,6 +14,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_SERDEV) += rust_driver_serdev.o obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o obj-$(CONFIG_SAMPLE_RUST_SOC) += rust_soc.o
samples/rust/rust_driver_serdev.rs new file +86 -0
86 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 6 lines · Markus Probst
This patch series adds the serdev device bus rust abstraction into the kernel.
This abstraction will be used by a driver, which targets the MCU devices in Synology devices.
Quoted 8 lines
Kari Argillander also messaged me, stating that he wants to write a watchdog driver with this abstraction (needing initial device data).
@Rob: Are you willing to maintain these rust abstractions yourself, as you are the expert on this subsystem, otherwise I would take care of it with a "SERIAL DEVICE BUS [RUST]" section in the
MAINTAINERSfile. In the second case, I assume you are going to pick those patches as-is into your tree, after they have been reviewed?
Well I can ignore the Rust part as much as I ignore the C serdev part... Honestly, I need to find someone else to maintain all of it as I don't really have the bandwidth. I don't think we should split it though.
And I don't have a tree for serdev. Greg picks up the serdev patches. If the Rust folks are fine with them, then I am.
Rob
Markus Probst
maintainerQuoted 16 lines · Rob Herring
Quoted 6 lines · Markus Probst
This patch series adds the serdev device bus rust abstraction into the kernel.
This abstraction will be used by a driver, which targets the MCU devices in Synology devices.
Quoted 8 lines
Kari Argillander also messaged me, stating that he wants to write a watchdog driver with this abstraction (needing initial device data).
@Rob: Are you willing to maintain these rust abstractions yourself, as you are the expert on this subsystem, otherwise I would take care of it with a "SERIAL DEVICE BUS [RUST]" section in the
MAINTAINERSfile. In the second case, I assume you are going to pick those patches as-is into your tree, after they have been reviewed?
Quoted 3 lines
Well I can ignore the Rust part as much as I ignore the C serdev part... Honestly, I need to find someone else to maintain all of it as I don't really have the bandwidth. I don't think we should split it though.
I could maintain both C and Rust.
However my knowledge regarding the tty subsystem is very limited, so I would need to heavily rely on kernel documentation.
Thanks
- Markus Probst
Quoted 7 lines
And I don't have a tree for serdev. Greg picks up the serdev patches. If the Rust folks are fine with them, then I am.
Rob