Skip to contentSkip to conversation

rust: helpers: add SPI helpers

PATCH Superseded Shuai Liu 2 participants 14 messages Oct 27 – Nov 13 3 messages not in the archive

Subsystems (2)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Shuai Liu

author
#1 lore raw

PATCH v6 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
index c706dda5b5ca..c9062aae959a 100644
@@ -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
index 9bb850294a2f..6b0f50c12f9c 100644
@@ -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
new file mode 100644
index 000000000000..8609d54962c0
@@ -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
author
#2 lore raw

PATCH v6 2/3 rust: spi: add SPI driver abstractions

In reply to a message not in the archive

Add 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() I/O operations on bound devices.
- 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 device configuration accessors and the I/O operations are only available on SpiDevice<Bound>: an ARef<SpiDevice> may outlive the driver's unbind and the device's or controller's unregistration, and the SPI core's transfer path does not check the registration state, so performing I/O on an unbound device is not sound. Moreover, once the device is unbound, another driver may bind to the same struct spi_device and mutate the device configuration with plain, non-atomic writes (e.g. via spi_setup()), so reading the configuration would be a data race. Driver callbacks receive a SpiDevice<Core>, which dereferences to SpiDevice<Bound>.

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. write_then_read() additionally validates that the sum of the two buffer lengths fits in u32, as the C core sums the two lengths as unsigned int when sizing its bounce buffer.

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.

---
 rust/kernel/Kconfig.test |  11 +
 rust/kernel/lib.rs       |   2 +
 rust/kernel/spi.rs       | 570 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 583 insertions(+)
 create mode 100644 rust/kernel/spi.rs
rust/kernel/Kconfig.test +11 -0
index 3bf4194f28c7..bda70dcc7793 100644
@@ -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
index a4747f75b163..cbe5a4ed825d 100644
@@ -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 +570 -0

570 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#3 lore raw

Re: [PATCH v6 2/3] rust: spi: add SPI driver abstractions

Quoted 9 lines

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. write_then_read() additionally validates that the sum of the two buffer lengths fits in u32, as the C core sums the two lengths as unsigned int when sizing its bounce buffer.

I do wonder if it's more sensible to go straight to the spi_sync()/spi_async() APIs - these APIs you're implementing are all convenience wrappers at the C level, I don't know how idiomatic they are in Rust but if there's a more Rust native way of doing this stuff it might be better to go with that and then arrange to map it onto the lower level C APIs rather than deal with the C wrappers. Of course if this is actually fairly sensible for Rust then leveraging the existing wrappers makes sense.

Signature · 1 line
Mark Brown <[email protected]>
author
#4 lore raw

Re: [PATCH v6 2/3] rust: spi: add SPI driver abstractions

Hi Mark,

Thanks for the review.

Agreed -- mapping onto the lower-level APIs is the better design for the Rust layer, so v7 (coming shortly) reworks the device I/O around a single SpiDevice::transfer() primitive that maps onto spi_sync(): a sequence of Transfers (write, read, or full duplex) is submitted as one spi_message, executed in order with the chip select asserted for the whole message.

Since spi_sync() requires DMA-safe transfer buffers and Rust slices may be located anywhere (e.g. on the stack), the Rust layer owns the
bounce-buffering: every transfer buffer is copied into a kmalloc'd buffer before submission, and received data is copied back afterwards. This removes the dependency on the C convenience wrappers (and their helpers) entirely, and adds full-duplex support, which the wrappers do not offer.

write(), read() and write_then_read() remain as thin convenience methods built on top of transfer(): register-style access is what most SPI drivers need, and keeping the familiar names makes porting C drivers straightforward.

Best regards,
Shuai Liu

author
#5 lore raw

v7 cover rust: spi: add SPI driver abstractions

In reply to Mark Brown

Rework the device I/O to map onto the lower-level C APIs, as suggested by Mark Brown [1]: instead of leveraging the C convenience wrappers (spi_write(), spi_read() and spi_write_then_read()), all device I/O is now built on a single SpiDevice::transfer() primitive that maps onto spi_sync(). The convenience methods write(), read() and write_then_read() remain as thin wrappers on top of transfer().

spi_sync() requires DMA-safe transfer buffers and Rust slices may be located anywhere (e.g. on the stack), so the Rust layer owns the
bounce-buffering: every transfer buffer is copied into a kmalloc'd buffer before submission, and received data is copied back afterwards. This removes the dependency on the C convenience wrappers (and their helpers) entirely, and adds full-duplex support via Transfer::full_duplex(), which the wrappers do not offer.

Changes in v7:
- Rework device I/O around a transfer() primitive that maps onto spi_sync(), per Mark Brown's review (patch 2).
- Drop the spi_write()/spi_read() helpers; add spi_message_init()/spi_message_add_tail() helpers instead (patch 1).

[1] https://lore.kernel.org/rust-for-linux/179451185979.339049.12323339081307899766.b4-review@b4/

Shuai Liu (3):
rust: helpers: add SPI helpers
rust: spi: add SPI driver abstractions
samples: rust: add SPI driver sample

 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/helpers.c          |   1 +
 rust/helpers/spi.c              |  26 ++
 rust/kernel/Kconfig.test        |  11 +
 rust/kernel/lib.rs              |   2 +
 rust/kernel/spi.rs              | 673 ++++++++++++++++++++++++++++++++
 samples/rust/Kconfig            |  11 +
 samples/rust/Makefile           |   1 +
 samples/rust/rust_driver_spi.rs |  66 ++++
 9 files changed, 792 insertions(+)
 create mode 100644 rust/helpers/spi.c
 create mode 100644 rust/kernel/spi.rs
 create mode 100644 samples/rust/rust_driver_spi.rs
Signature · 1 line
2.43.0

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#7 lore raw

PATCH v7 2/3 rust: spi: add SPI driver abstractions

Add 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 device I/O on bound devices.
- spi::Transfer: a single SPI transfer that transmits, receives, or does both simultaneously (full duplex).
- 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 device configuration accessors and the I/O operations are only available on SpiDevice<Bound>: an ARef<SpiDevice> may outlive the driver's unbind and the device's or controller's unregistration, and the SPI core's transfer path does not check the registration state, so performing I/O on an unbound device is not sound. Moreover, once the device is unbound, another driver may bind to the same struct spi_device and mutate the device configuration with plain, non-atomic writes (e.g. via spi_setup()), so reading the configuration would be a data race. Driver callbacks receive a SpiDevice<Core>, which dereferences to SpiDevice<Bound>.

Device I/O is built on a single primitive, SpiDevice::transfer(), which maps to the C spi_sync() API: a sequence of Transfers is submitted as one spi_message, executed in order with the chip select asserted for the whole message. The convenience methods write(), read() and write_then_read() are thin wrappers on top of it.

spi_sync() requires transfer buffers to be DMA-safe, and Rust slices may be located anywhere (e.g. on the stack), so transfer() copies all data into DMA-safe bounce buffers before submitting the message and copies received data back afterwards. Unlike the previous approach of leveraging the C convenience wrappers, this maps directly onto the lower-level C APIs and also supports full-duplex transfers, which the C convenience wrappers do not offer.

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.

---
 rust/kernel/Kconfig.test |  11 +
 rust/kernel/lib.rs       |   2 +
 rust/kernel/spi.rs       | 673 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 686 insertions(+)
 create mode 100644 rust/kernel/spi.rs
rust/kernel/Kconfig.test +11 -0
index 3bf4194f28c7c..bda70dcc77934 100644
@@ -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
index a4747f75b1632..cbe5a4ed825dc 100644
@@ -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 +673 -0

673 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#8 lore raw

PATCH v7 3/3 samples: rust: add SPI driver sample

In reply to a message not in the archive

Add 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
index 31d62533ef25f..f99bf52654a15 100644
@@ -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
index b986b681cde5a..efe04638a990b 100644
@@ -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
author
#9 lore raw

PATCH v7 1/3 rust: helpers: add SPI helpers

In reply to Shuai Liu

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              | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 rust/helpers/spi.c
rust/bindings/bindings_helper.h +1 -0
index c706dda5b5ca7..c9062aae959a1 100644
@@ -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
index 9bb850294a2f3..6b0f50c12f9ca 100644
@@ -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 +26 -0
new file mode 100644
index 0000000000000..179a90e014e04
@@ -0,0 +1,26 @@
+// 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
+void rust_helper_spi_message_init(struct spi_message *msg)
+{
+	spi_message_init(msg);
+}
+
+__rust_helper
+void rust_helper_spi_message_add_tail(struct spi_transfer *t,
+				      struct spi_message *msg)
+{
+	spi_message_add_tail(t, msg);
+}
+
+#endif
Signature · 1 line
2.43.0

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#11 lore raw

PATCH v7 2/3 rust: spi: add SPI driver abstractions

Add 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 device I/O on bound devices.
- spi::Transfer: a single SPI transfer that transmits, receives, or does both simultaneously (full duplex).
- 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 device configuration accessors and the I/O operations are only available on SpiDevice<Bound>: an ARef<SpiDevice> may outlive the driver's unbind and the device's or controller's unregistration, and the SPI core's transfer path does not check the registration state, so performing I/O on an unbound device is not sound. Moreover, once the device is unbound, another driver may bind to the same struct spi_device and mutate the device configuration with plain, non-atomic writes (e.g. via spi_setup()), so reading the configuration would be a data race. Driver callbacks receive a SpiDevice<Core>, which dereferences to SpiDevice<Bound>.

Device I/O is built on a single primitive, SpiDevice::transfer(), which maps to the C spi_sync() API: a sequence of Transfers is submitted as one spi_message, executed in order with the chip select asserted for the whole message. The convenience methods write(), read() and write_then_read() are thin wrappers on top of it.

spi_sync() requires transfer buffers to be DMA-safe, and Rust slices may be located anywhere (e.g. on the stack), so transfer() copies all data into DMA-safe bounce buffers before submitting the message and copies received data back afterwards. Unlike the previous approach of leveraging the C convenience wrappers, this maps directly onto the lower-level C APIs and also supports full-duplex transfers, which the C convenience wrappers do not offer.

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.

---
 rust/kernel/Kconfig.test |  11 +
 rust/kernel/lib.rs       |   2 +
 rust/kernel/spi.rs       | 673 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 686 insertions(+)
 create mode 100644 rust/kernel/spi.rs
rust/kernel/Kconfig.test +11 -0
index 3bf4194f28c7c..bda70dcc77934 100644
@@ -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
index a4747f75b1632..cbe5a4ed825dc 100644
@@ -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 +673 -0

673 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#12 lore raw

PATCH v7 3/3 samples: rust: add SPI driver sample

In reply to a message not in the archive

Add 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
index 31d62533ef25f..f99bf52654a15 100644
@@ -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
index b986b681cde5a..efe04638a990b 100644
@@ -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
author
#13 lore raw

PATCH v7 1/3 rust: helpers: add SPI helpers

In reply to Shuai Liu

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              | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 rust/helpers/spi.c
rust/bindings/bindings_helper.h +1 -0
index c706dda5b5ca7..c9062aae959a1 100644
@@ -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
index 9bb850294a2f3..6b0f50c12f9ca 100644
@@ -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 +26 -0
new file mode 100644
index 0000000000000..179a90e014e04
@@ -0,0 +1,26 @@
+// 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
+void rust_helper_spi_message_init(struct spi_message *msg)
+{
+	spi_message_init(msg);
+}
+
+__rust_helper
+void rust_helper_spi_message_add_tail(struct spi_transfer *t,
+				      struct spi_message *msg)
+{
+	spi_message_add_tail(t, msg);
+}
+
+#endif
Signature · 1 line
2.43.0
maintainer
#14 lore raw

Re: [PATCH v7 0/3] rust: spi: add SPI driver abstractions

In reply to Shuai Liu
Quoted 6 lines · Shuai Liu

Rework the device I/O to map onto the lower-level C APIs, as suggested by Mark Brown [1]: instead of leveraging the C convenience wrappers (spi_write(), spi_read() and spi_write_then_read()), all device I/O is now built on a single SpiDevice::transfer() primitive that maps onto spi_sync(). The convenience methods write(), read() and write_then_read() remain as thin wrappers on top of transfer().

Please don't send new patches in reply to old patches or serieses, this makes it harder for both people and tools to understand what is going on - it can bury things in mailboxes and make it difficult to keep track of what current patches are, both for the new patches and the old ones.

maintainer
#15 lore raw

Re: [PATCH v7 0/3] rust: spi: add SPI driver abstractions

In reply to Shuai Liu
Quoted 6 lines · Shuai Liu

Rework the device I/O to map onto the lower-level C APIs, as suggested by Mark Brown [1]: instead of leveraging the C convenience wrappers (spi_write(), spi_read() and spi_write_then_read()), all device I/O is now built on a single SpiDevice::transfer() primitive that maps onto spi_sync(). The convenience methods write(), read() and write_then_read() remain as thin wrappers on top of transfer().

This doesn't apply against current code, please check and resend.

author
#16 lore raw

PATCH v6 3/3 samples: rust: add SPI driver sample

In reply to a message not in the archive

Add 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
index 31d62533ef25..f99bf52654a1 100644
@@ -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
index b986b681cde5..efe04638a990 100644
@@ -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