Skip to content

Commit a749888

Browse files
committed
rust: spi: add spi_sync
Signed-off-by: Ryo Munakata <ryomnktml@gmail.com>
1 parent e72e27b commit a749888

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

rust/helpers.c

+12
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,18 @@ int rust_helper_spi_read(struct spi_device *spi, void *buf, size_t len)
698698
return spi_read(spi, buf, len);
699699
}
700700
EXPORT_SYMBOL_GPL(rust_helper_spi_read);
701+
702+
void rust_helper_spi_message_init(struct spi_message *m)
703+
{
704+
spi_message_init(m);
705+
}
706+
EXPORT_SYMBOL_GPL(rust_helper_spi_message_init);
707+
708+
void rust_helper_spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)
709+
{
710+
spi_message_add_tail(t, m);
711+
}
712+
EXPORT_SYMBOL_GPL(rust_helper_spi_message_add_tail);
701713
#endif
702714

703715
/*

rust/kernel/spi.rs

+31
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,37 @@ impl Device {
326326
})
327327
}
328328

329+
/// Blocking/synchronous SPI data transfers
330+
///
331+
/// This call may only be used from a context that may sleep. The sleep
332+
/// is non-interruptible, and has no timeout. Low-overhead controller
333+
/// drivers may DMA directly into and out of the message buffers.
334+
///
335+
/// # Safety
336+
/// `tx_buf` and `rx_buf` must be DMA-safe otherwise UB.
337+
pub unsafe fn sync(&self, tx_buf: &[u8], rx_buf: &mut [u8]) -> Result {
338+
let mut tx_transfer = bindings::spi_transfer {
339+
tx_buf: tx_buf.as_ptr() as _,
340+
len: tx_buf.len() as _,
341+
..Default::default()
342+
};
343+
let mut rx_transfer = bindings::spi_transfer {
344+
rx_buf: rx_buf.as_mut_ptr() as _,
345+
len: rx_buf.len() as _,
346+
..Default::default()
347+
};
348+
349+
let mut message: bindings::spi_message = Default::default();
350+
unsafe {
351+
bindings::spi_message_init(&mut message as _);
352+
bindings::spi_message_add_tail(&mut tx_transfer as _, &mut message as _);
353+
bindings::spi_message_add_tail(&mut rx_transfer as _, &mut message as _);
354+
355+
// SAFETY: `Device` is alive until it gets dropped
356+
to_result(bindings::spi_sync(self.0, &mut message as _))
357+
}
358+
}
359+
329360
/// SPI synchronous write
330361
pub fn write(&self, tx_buf: &[u8]) -> Result {
331362
to_result(unsafe {

0 commit comments

Comments
 (0)