rust: helpers: add SPI helpers
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v3 1/3 rust: helpers: add SPI helpers
Add C helpers for the SPI functions that are static inline in C and therefore cannot be accessed from Rust directly: spi_unregister_driver(), spi_write() and spi_read().
The helpers are guarded by CONFIG_SPI, since the SPI core functions they call are not available when SPI support is disabled.
Also include <linux/spi/spi.h> in the bindgen bindings helper, so that the Rust side can access the SPI data types.
--- rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/spi.c | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 rust/helpers/spi.c
rust/bindings/bindings_helper.h +1 -0
@@ -88,6 +88,7 @@#include <linux/security.h> #include <linux/serdev.h> #include <linux/slab.h> +#include <linux/spi/spi.h> #include <linux/sys_soc.h> #include <linux/task_work.h> #include <linux/tracepoint.h>
rust/helpers/helpers.c +1 -0
@@ -96,6 +96,7 @@#include "serdev.c" #include "signal.c" #include "slab.c" +#include "spi.c" #include "spinlock.c" #include "string.c" #include "srcu.c"
rust/helpers/spi.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/spi/spi.h> + +#ifdef CONFIG_SPI + +__rust_helper +void rust_helper_spi_unregister_driver(struct spi_driver *sdrv) +{ +spi_unregister_driver (sdrv); +} + +__rust_helper +int rust_helper_spi_write(struct spi_device *spi, const void *buf, size_t len) +{ +return spi_write (spi, buf, len); +} + +__rust_helper +int rust_helper_spi_read(struct spi_device *spi, void *buf, size_t len) +{ +return spi_read (spi, buf, len); +} + +#endif
Signature · 1 line
2.43.0
PATCH v3 2/3 rust: spi: add SPI driver abstractions
In reply to a message not in the archiveAdd the Rust abstractions for the SPI driver subsystem, allowing SPI drivers to be written in Rust.
This adds the following abstractions:
- spi::DeviceId: a driver-side device id.
- spi::Adapter: the registration of SPI drivers, implementing driver::DriverLayout and driver::RegistrationOps.
- spi::Driver: the trait that SPI drivers implement, with probe(), shutdown() and unbind() callbacks.
- spi::SpiDevice: a representation of struct spi_device, providing device configuration accessors and the write(), read() and write_then_read() operations.
- module_spi_driver! and spi_device_table! macros.
The module is only built when CONFIG_SPI is enabled, since the SPI core functions it uses are not available otherwise.
The write() and read() methods copy the data into DMA-safe bounce buffers before transferring, as the SPI core requires transfer buffers to be DMA-safe and the caller-provided slices may be located anywhere (e.g. on the stack). write_then_read() does not need this, since the C core's spi_write_then_read() already copies both buffers into internal DMA-safe bounce buffers.
Match data lookup follows the same order as the C core's spi_get_device_match_data(): first the OF/ACPI match data, then the SPI id table. Entries without driver data (e.g. @none id tables) are handled gracefully.
I/O is available from any device context and SpiDevice is Sync, following the precedent of the other bus abstractions (e.g. usb::Device).
--- rust/kernel/Kconfig.test | 11 + rust/kernel/lib.rs | 2 + rust/kernel/spi.rs | 548 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 561 insertions(+) create mode 100644 rust/kernel/spi.rs
rust/kernel/Kconfig.test +11 -0
@@ -94,6 +94,17 @@ config RUST_BUG_KUNIT_TEST If unsure, say N. +config RUST_SPI_KUNIT_TEST + bool "KUnit tests for the Rust SPI driver abstractions" if !KUNIT_ALL_TESTS + depends on SPI=y + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for the Rust SPI driver + abstractions. These are only for development and testing, not + for regular kernel use cases. + + If unsure, say N. + config RUST_TIME_KUNIT_TEST bool "KUnit tests for the Rust time API" if !KUNIT_ALL_TESTS default KUNIT_ALL_TESTS
rust/kernel/lib.rs +2 -0
@@ -133,6 +133,8 @@pub mod sizes;#[cfg( CONFIG_SOC_BUS )] pub mod soc; +#[cfg( CONFIG_SPI )] +pub mod spi;#[doc(hidden)] pub mod std_vendor;pub mod str;
rust/kernel/spi.rs new file +548 -0
548 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 3/3 samples: rust: add SPI driver sample
In reply to a message not in the archiveAdd a sample driver for the Rust SPI abstractions to demonstrate their usage.
--- samples/rust/Kconfig | 11 ++++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_spi.rs | 66 +++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 samples/rust/rust_driver_spi.rs
samples/rust/Kconfig +11 -0
@@ -84,6 +84,17 @@ config SAMPLE_RUST_DEBUGFS_SCOPED If unsure, say N. +config SAMPLE_RUST_DRIVER_SPI + tristate "SPI Driver" + depends on SPI=y + help + This option builds the Rust SPI driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_spi. + + If unsure, say N. + config SAMPLE_RUST_DRIVER_I2C tristate "I2C Driver" depends on I2C=y
samples/rust/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_SAMPLE_RUST_DEBUGFS) += rust_debugfs.o obj-$(CONFIG_SAMPLE_RUST_DEBUGFS_SCOPED) += rust_debugfs_scoped.o obj-$(CONFIG_SAMPLE_RUST_DMA) += rust_dma.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) += rust_driver_i2c.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_SPI) += rust_driver_spi.o obj-$(CONFIG_SAMPLE_RUST_I2C_CLIENT) += rust_i2c_client.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
samples/rust/rust_driver_spi.rs new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
2.43.0