Linux userland API discussions
 help / color / mirror / Atom feed
From: Cheng Ming Lin <linchengming884@gmail.com>
To: Mark Brown <broonie@kernel.org>
Cc: linux-spi@vger.kernel.org, linux-api@vger.kernel.org,
	Miquel Raynal <miquel.raynal@bootlin.com>,
	alvinzhou@mxic.com.tw
Subject: [PATCH 1/2] spi: spidev: Allow userspace to request DTR transfers
Date: Mon, 30 Nov 2026 13:49:30 +0800	[thread overview]
Message-ID: <20261130054931.2441438-2-chengminglin@mxic.com.tw> (raw)
In-Reply-To: <20261130054931.2441438-1-chengminglin@mxic.com.tw>

The SPI core supports Double Transfer Rate transfers through
spi_transfer.dtr_mode, which __spi_validate() checks against
spi_controller.dtr_caps, and the spi-mem fallback to the regular SPI
API already makes use of it. However, there is no way for userspace to
request such a transfer through spidev: struct spi_ioc_transfer only
carries the bus width in tx_nbits and rx_nbits.

The size of struct spi_ioc_transfer is encoded in SPI_IOC_MESSAGE(N)
and cannot change, so turn the last padding byte into a flags field and
add SPI_IOC_XFER_DTR to set dtr_mode on the corresponding transfer. The
uapi header already asks userspace to zero-initialize the unused fields
for future updates, and commit 2ed6692e8ce9 ("spi: spidev: Enable
control of inter-word delays") used part of the same padding for
word_delay_usecs.

Reject unknown flags so that more of them can be added later. DTR
transfers on controllers without dtr_caps are still rejected by
__spi_validate().

Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
---
 drivers/spi/spidev.c            | 12 +++++++++++-
 include/uapi/linux/spi/spidev.h |  9 ++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c
index 15d2aaeb0..276ad9f7f 100644
--- a/drivers/spi/spidev.c
+++ b/drivers/spi/spidev.c
@@ -66,6 +66,9 @@ static_assert(N_SPI_MINORS > 0 && N_SPI_MINORS <= 256);
 				| SPI_RX_CPHA_FLIP | SPI_3WIRE_HIZ \
 				| SPI_MOSI_IDLE_LOW)
 
+/* Flags userspace may set in spi_ioc_transfer.flags */
+#define SPIDEV_XFER_VALID_FLAGS	(SPI_IOC_XFER_DTR)
+
 struct spidev_data {
 	dev_t			devt;
 	struct mutex		spi_lock;
@@ -234,6 +237,11 @@ static int spidev_message(struct spidev_data *spidev,
 		 */
 		unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_DMA_MINALIGN);
 
+		if (u_tmp->flags & ~SPIDEV_XFER_VALID_FLAGS) {
+			status = -EINVAL;
+			goto done;
+		}
+
 		k_tmp->len = u_tmp->len;
 
 		total += k_tmp->len;
@@ -275,6 +283,7 @@ static int spidev_message(struct spidev_data *spidev,
 		k_tmp->cs_change = !!u_tmp->cs_change;
 		k_tmp->tx_nbits = u_tmp->tx_nbits;
 		k_tmp->rx_nbits = u_tmp->rx_nbits;
+		k_tmp->dtr_mode = u_tmp->flags & SPI_IOC_XFER_DTR;
 		k_tmp->bits_per_word = u_tmp->bits_per_word;
 		k_tmp->delay.value = u_tmp->delay_usecs;
 		k_tmp->delay.unit = SPI_DELAY_UNIT_USECS;
@@ -285,11 +294,12 @@ static int spidev_message(struct spidev_data *spidev,
 			k_tmp->speed_hz = spidev->speed_hz;
 #ifdef VERBOSE
 		dev_dbg(&spidev->spi->dev,
-			"  xfer len %u %s%s%s%dbits %u usec %u usec %uHz\n",
+			"  xfer len %u %s%s%s%s%dbits %u usec %u usec %uHz\n",
 			k_tmp->len,
 			k_tmp->rx_buf ? "rx " : "",
 			k_tmp->tx_buf ? "tx " : "",
 			k_tmp->cs_change ? "cs " : "",
+			k_tmp->dtr_mode ? "dtr " : "",
 			k_tmp->bits_per_word ? : spidev->spi->bits_per_word,
 			k_tmp->delay.value,
 			k_tmp->word_delay.value,
diff --git a/include/uapi/linux/spi/spidev.h b/include/uapi/linux/spi/spidev.h
index 0c3da08f2..f43acf6db 100644
--- a/include/uapi/linux/spi/spidev.h
+++ b/include/uapi/linux/spi/spidev.h
@@ -45,6 +45,10 @@
  * @word_delay_usecs: If nonzero, how long to wait between words within one
  *	transfer. This property needs explicit support in the SPI controller,
  *	otherwise it is silently ignored.
+ * @flags: Bitmask of SPI_IOC_XFER_* flags. SPI_IOC_XFER_DTR requests a
+ *	Double Transfer Rate transfer and needs explicit support in the SPI
+ *	controller, otherwise the message is rejected. Unknown flags are
+ *	rejected as well.
  *
  * This structure is mapped directly to the kernel spi_transfer structure;
  * the fields have the same meanings, except of course that the pointers
@@ -80,7 +84,7 @@ struct spi_ioc_transfer {
 	__u8		tx_nbits;
 	__u8		rx_nbits;
 	__u8		word_delay_usecs;
-	__u8		pad;
+	__u8		flags;
 
 	/* If the contents of 'struct spi_ioc_transfer' ever change
 	 * incompatibly, then the ioctl number (currently 0) must change;
@@ -91,6 +95,9 @@ struct spi_ioc_transfer {
 	 */
 };
 
+/* Flags for spi_ioc_transfer.flags */
+#define SPI_IOC_XFER_DTR		_BITUL(0)	/* Double Transfer Rate */
+
 /* not all platforms use <asm-generic/ioctl.h> or _IOC_TYPECHECK() ... */
 #define SPI_MSGSIZE(N) \
 	((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \
-- 
2.25.1


  reply	other threads:[~2026-11-30  5:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  5:49 [PATCH 0/2] spi: spidev: Allow userspace to request DTR transfers Cheng Ming Lin
2026-11-30  5:49 ` Cheng Ming Lin [this message]
2026-11-30  5:49 ` [PATCH 2/2] spi: spidev_test: Add an option " Cheng Ming Lin
2026-11-30  8:36 ` [PATCH 0/2] spi: spidev: Allow userspace " Miquel Raynal

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=20261130054931.2441438-2-chengminglin@mxic.com.tw \
    --to=linchengming884@gmail.com \
    --cc=alvinzhou@mxic.com.tw \
    --cc=broonie@kernel.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=miquel.raynal@bootlin.com \
    /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