Linux userland API discussions
 help / color / mirror / Atom feed
* [PATCH 0/2] spi: spidev: Allow userspace to request DTR transfers
@ 2026-11-30  5:49 Cheng Ming Lin
  2026-11-30  5:49 ` [PATCH 1/2] " Cheng Ming Lin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Cheng Ming Lin @ 2026-11-30  5:49 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-api, Miquel Raynal, alvinzhou

The SPI core can perform Double Transfer Rate transfers through
spi_transfer.dtr_mode on controllers advertising dtr_caps, but there
is currently no way for userspace to request them through spidev.

The first patch turns the last padding byte of struct spi_ioc_transfer
into a flags field and adds SPI_IOC_XFER_DTR to set dtr_mode on the
corresponding transfer. The struct size and layout are unchanged.

The second patch adds a matching -x/--dtr option to spidev_test.

Cheng Ming Lin (2):
  spi: spidev: Allow userspace to request DTR transfers
  spi: spidev_test: Add an option to request DTR transfers

 drivers/spi/spidev.c            | 12 +++++++++++-
 include/uapi/linux/spi/spidev.h |  9 ++++++++-
 tools/spi/spidev_test.c         | 13 +++++++++++--
 3 files changed, 30 insertions(+), 4 deletions(-)


base-commit: 543bc7b4337302ae75119059bdeff6df8eb34b56
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] spi: spidev: Allow userspace to request DTR transfers
  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
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Cheng Ming Lin @ 2026-11-30  5:49 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-api, Miquel Raynal, alvinzhou

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] spi: spidev_test: Add an option to request DTR transfers
  2026-11-30  5:49 [PATCH 0/2] spi: spidev: Allow userspace to request DTR transfers Cheng Ming Lin
  2026-11-30  5:49 ` [PATCH 1/2] " Cheng Ming Lin
@ 2026-11-30  5:49 ` Cheng Ming Lin
  2026-11-30  8:36 ` [PATCH 0/2] spi: spidev: Allow userspace " Miquel Raynal
  2 siblings, 0 replies; 4+ messages in thread
From: Cheng Ming Lin @ 2026-11-30  5:49 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-api, Miquel Raynal, alvinzhou

Add a -x/--dtr option setting SPI_IOC_XFER_DTR on every transfer of the
message, so that Double Transfer Rate transfers can be exercised with
spidev_test. Like the bus width options, it applies to all the
transfers. Report it along with the other device settings.

Controllers without DTR support reject such transfers, in which case
spidev_test reports that the message could not be sent.

Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
---
 tools/spi/spidev_test.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c
index 5bd3159f6..dd429a94c 100644
--- a/tools/spi/spidev_test.c
+++ b/tools/spi/spidev_test.c
@@ -41,6 +41,7 @@ static char *output_file;
 static uint32_t speed = 500000;
 static uint16_t delay;
 static uint16_t word_delay;
+static int dtr;
 static int verbose;
 static int transfer_size = -1;
 static int transfers = 1;
@@ -169,6 +170,7 @@ static void transfer(int fd, uint8_t const * const tx, uint8_t const * const rx,
 			.word_delay_usecs = word_delay,
 			.speed_hz = speed,
 			.bits_per_word = bits,
+			.flags = dtr ? SPI_IOC_XFER_DTR : 0,
 		};
 
 		if (tx_buf)
@@ -244,7 +246,7 @@ static void transfer(int fd, uint8_t const * const tx, uint8_t const * const rx,
 
 static void print_usage(const char *prog)
 {
-	printf("Usage: %s [-2348CDFHILMNOPRSTZbcdiloprstvwz]\n", prog);
+	printf("Usage: %s [-2348CDFHILMNOPRSTZbcdiloprstvwxz]\n", prog);
 	puts("general device settings:\n"
 		 "  -D --device         device to use (default /dev/spidev1.1)\n"
 		 "  -s --speed          max speed (Hz)\n"
@@ -262,6 +264,7 @@ static void print_usage(const char *prog)
 		 "  -2 --dual           dual transfer\n"
 		 "  -4 --quad           quad transfer\n"
 		 "  -8 --octal          octal transfer\n"
+		 "  -x --dtr            double transfer rate\n"
 		 "  -3 --3wire          SI/SO signals shared\n"
 		 "  -Z --3wire-hiz      high impedance turnaround\n"
 		 "data:\n"
@@ -318,12 +321,13 @@ static void parse_opts(int argc, char *argv[])
 			{ "mosi-idle-low", 0, 0, 'M' },
 			{ "predictable",   1, 0, 'P' },
 			{ "transfers",     1, 0, 'T' },
+			{ "dtr",           0, 0, 'x' },
 			{ "verbose",       0, 0, 'v' },
 			{ NULL, 0, 0, 0 },
 		};
 		int c;
 
-		c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:P:T:vS:zI:",
+		c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:P:T:vS:zI:x",
 				lopts, NULL);
 
 		if (c == -1)
@@ -429,6 +433,9 @@ static void parse_opts(int argc, char *argv[])
 		case 'I':
 			iterations = atoi(optarg);
 			break;
+		case 'x':
+			dtr = 1;
+			break;
 		default:
 			print_usage(argv[0]);
 		}
@@ -648,6 +655,8 @@ int main(int argc, char *argv[])
 	printf("spi mode: 0x%x\n", mode);
 	printf("bits per word: %u\n", bits);
 	printf("max speed: %u Hz (%u kHz)\n", speed, speed/1000);
+	if (dtr)
+		printf("dtr: on\n");
 
 	if (input_tx)
 		transfer_escaped_string(fd, input_tx);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] spi: spidev: Allow userspace to request DTR transfers
  2026-11-30  5:49 [PATCH 0/2] spi: spidev: Allow userspace to request DTR transfers Cheng Ming Lin
  2026-11-30  5:49 ` [PATCH 1/2] " Cheng Ming Lin
  2026-11-30  5:49 ` [PATCH 2/2] spi: spidev_test: Add an option " Cheng Ming Lin
@ 2026-11-30  8:36 ` Miquel Raynal
  2 siblings, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2026-11-30  8:36 UTC (permalink / raw)
  To: Cheng Ming Lin; +Cc: Mark Brown, linux-spi, linux-api, alvinzhou

On 30/11/2026 at 13:49:29 +08, Cheng Ming Lin <linchengming884@gmail.com> wrote:

> The SPI core can perform Double Transfer Rate transfers through
> spi_transfer.dtr_mode on controllers advertising dtr_caps, but there
> is currently no way for userspace to request them through spidev.
>
> The first patch turns the last padding byte of struct spi_ioc_transfer
> into a flags field and adds SPI_IOC_XFER_DTR to set dtr_mode on the
> corresponding transfer. The struct size and layout are unchanged.
>
> The second patch adds a matching -x/--dtr option to spidev_test.

I don't know much this backend, but looks fine, besides the fact that we
are naturally running out of extra bytes in the structure.

Maybe you mentioned it, but it is worth repeating that these padding
bytes are expected to be 0 already.

Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30  8:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  5:49 [PATCH 0/2] spi: spidev: Allow userspace to request DTR transfers Cheng Ming Lin
2026-11-30  5:49 ` [PATCH 1/2] " Cheng Ming Lin
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox