* [PATCH v2 1/3] spi: spidev: Allow userspace to request DTR transfers
2026-12-01 6:51 [PATCH v2 0/3] spi: spidev: Allow userspace to request DTR transfers Cheng Ming Lin
@ 2026-12-01 6:52 ` Cheng Ming Lin
2026-12-01 6:52 ` [PATCH v2 2/3] spi: spidev_test: Add an option " Cheng Ming Lin
2026-12-01 6:52 ` [PATCH v2 3/3] spi: spidev: Document SPI_IOC_MESSAGE_FLAGS Cheng Ming Lin
2 siblings, 0 replies; 4+ messages in thread
From: Cheng Ming Lin @ 2026-12-01 6:52 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 reuse the last padding byte as a flags field and
add SPI_IOC_XFER_DTR to set dtr_mode on the corresponding transfer. The
field is an anonymous union with pad so that existing code which
initialises pad explicitly still builds.
SPI_IOC_MESSAGE never checked that the padding was zero, so it cannot
start interpreting it now: existing users that leave garbage there
would break, and a kernel without this change would silently perform a
regular transfer when DTR is requested. Add SPI_IOC_MESSAGE_FLAGS(N)
instead, which takes the same array of transfers but honours flags and
rejects unknown ones. Kernels that do not know about it fail with
-ENOTTY, so userspace can tell that flags are not supported.
SPI_IOC_MESSAGE keeps ignoring the byte.
DTR transfers on controllers without dtr_caps are rejected by
__spi_validate().
Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
---
drivers/spi/spidev.c | 43 +++++++++++++++++++++++++--------
include/uapi/linux/spi/spidev.h | 21 ++++++++++++++--
2 files changed, 52 insertions(+), 12 deletions(-)
diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c
index 15d2aaeb0..ee4678c7c 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;
@@ -202,7 +205,8 @@ spidev_write(struct file *filp, const char __user *buf,
}
static int spidev_message(struct spidev_data *spidev,
- struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
+ struct spi_ioc_transfer *u_xfers, unsigned int n_xfers,
+ bool use_flags)
{
struct spi_message msg;
struct spi_transfer *k_xfers;
@@ -234,6 +238,11 @@ static int spidev_message(struct spidev_data *spidev,
*/
unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_DMA_MINALIGN);
+ if (use_flags && (u_tmp->flags & ~SPIDEV_XFER_VALID_FLAGS)) {
+ status = -EINVAL;
+ goto done;
+ }
+
k_tmp->len = u_tmp->len;
total += k_tmp->len;
@@ -275,6 +284,8 @@ 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;
+ if (use_flags)
+ 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 +296,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,
@@ -322,6 +334,19 @@ static int spidev_message(struct spidev_data *spidev,
return status;
}
+static bool spidev_is_message_ioc(unsigned int cmd)
+{
+ return _IOC_TYPE(cmd) == SPI_IOC_MAGIC &&
+ _IOC_DIR(cmd) == _IOC_WRITE &&
+ (_IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0)) ||
+ _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE_FLAGS(0)));
+}
+
+static bool spidev_ioc_uses_flags(unsigned int cmd)
+{
+ return _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE_FLAGS(0));
+}
+
static struct spi_ioc_transfer *
spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
unsigned *n_ioc)
@@ -329,9 +354,7 @@ spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
u32 tmp;
/* Check type, command number and direction */
- if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC
- || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
- || _IOC_DIR(cmd) != _IOC_WRITE)
+ if (!spidev_is_message_ioc(cmd))
return ERR_PTR(-ENOTTY);
tmp = _IOC_SIZE(cmd);
@@ -491,7 +514,8 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
break; /* n_ioc is also 0 */
/* translate to spi_message, execute */
- retval = spidev_message(spidev, ioc, n_ioc);
+ retval = spidev_message(spidev, ioc, n_ioc,
+ spidev_ioc_uses_flags(cmd));
kfree(ioc);
break;
}
@@ -542,7 +566,8 @@ spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
}
/* translate to spi_message, execute */
- retval = spidev_message(spidev, ioc, n_ioc);
+ retval = spidev_message(spidev, ioc, n_ioc,
+ spidev_ioc_uses_flags(cmd));
kfree(ioc);
done:
@@ -554,9 +579,7 @@ spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
static long
spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
- if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC
- && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0))
- && _IOC_DIR(cmd) == _IOC_WRITE)
+ if (spidev_is_message_ioc(cmd))
return spidev_compat_ioc_message(filp, cmd, arg);
return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
diff --git a/include/uapi/linux/spi/spidev.h b/include/uapi/linux/spi/spidev.h
index 0c3da08f2..5888d1ca2 100644
--- a/include/uapi/linux/spi/spidev.h
+++ b/include/uapi/linux/spi/spidev.h
@@ -45,6 +45,11 @@
* @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.
+ * @pad: Alias of @flags, kept for source compatibility.
+ * @flags: Bitmask of SPI_IOC_XFER_* flags, only honoured by
+ * SPI_IOC_MESSAGE_FLAGS, which rejects unknown flags. SPI_IOC_XFER_DTR
+ * requests a Double Transfer Rate transfer and needs explicit support in
+ * the SPI controller, otherwise the message is rejected.
*
* This structure is mapped directly to the kernel spi_transfer structure;
* the fields have the same meanings, except of course that the pointers
@@ -61,6 +66,10 @@
* ...
* status = ioctl(fd, SPI_IOC_MESSAGE(4), mesg);
*
+ * SPI_IOC_MESSAGE ignores @flags. Use SPI_IOC_MESSAGE_FLAGS to have them
+ * honoured; kernels that do not support it fail with ENOTTY instead of
+ * silently ignoring the flags.
+ *
* So for example one transfer might send a nine bit command (right aligned
* in a 16-bit word), the next could read a block of 8-bit data before
* terminating that command by temporarily deselecting the chip; the next
@@ -80,10 +89,13 @@ struct spi_ioc_transfer {
__u8 tx_nbits;
__u8 rx_nbits;
__u8 word_delay_usecs;
- __u8 pad;
+ union {
+ __u8 pad;
+ __u8 flags;
+ };
/* If the contents of 'struct spi_ioc_transfer' ever change
- * incompatibly, then the ioctl number (currently 0) must change;
+ * incompatibly, then the ioctl numbers (currently 0 and 6) must change;
* ioctls with constant size fields get a bit more in the way of
* error checking than ones (like this) where that field varies.
*
@@ -91,11 +103,16 @@ 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)) \
? ((N)*(sizeof (struct spi_ioc_transfer))) : 0)
#define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)])
+/* Same as SPI_IOC_MESSAGE, but spi_ioc_transfer.flags is honoured */
+#define SPI_IOC_MESSAGE_FLAGS(N) _IOW(SPI_IOC_MAGIC, 6, char[SPI_MSGSIZE(N)])
/* Read / Write of SPI mode (SPI_MODE_0..SPI_MODE_3) (limited to 8 bits) */
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v2 2/3] spi: spidev_test: Add an option to request DTR transfers
2026-12-01 6:51 [PATCH v2 0/3] spi: spidev: Allow userspace to request DTR transfers Cheng Ming Lin
2026-12-01 6:52 ` [PATCH v2 1/3] " Cheng Ming Lin
@ 2026-12-01 6:52 ` Cheng Ming Lin
2026-12-01 6:52 ` [PATCH v2 3/3] spi: spidev: Document SPI_IOC_MESSAGE_FLAGS Cheng Ming Lin
2 siblings, 0 replies; 4+ messages in thread
From: Cheng Ming Lin @ 2026-12-01 6:52 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.
The message is then sent with SPI_IOC_MESSAGE_FLAGS, so that the
request cannot be silently ignored. Kernels without
SPI_IOC_MESSAGE_FLAGS and controllers without DTR support both reject
it, 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 | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c
index 5bd3159f6..5eee86131 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)
@@ -199,7 +201,10 @@ static void transfer(int fd, uint8_t const * const tx, uint8_t const * const rx,
}
}
- ret = ioctl(fd, SPI_IOC_MESSAGE(effective_transfers), tr);
+ if (dtr)
+ ret = ioctl(fd, SPI_IOC_MESSAGE_FLAGS(effective_transfers), tr);
+ else
+ ret = ioctl(fd, SPI_IOC_MESSAGE(effective_transfers), tr);
free(tr);
if (ret < 0) {
const size_t dump_len = min_t(size_t, len, 256);
@@ -211,6 +216,8 @@ static void transfer(int fd, uint8_t const * const tx, uint8_t const * const rx,
printf("... (%zu more bytes)\n", len - dump_len);
}
errno = saved_errno;
+ if (dtr && errno == ENOTTY)
+ pabort("this kernel does not support DTR requests");
pabort("can't send spi message");
}
@@ -244,7 +251,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 +269,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 +326,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 +438,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 +660,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* [PATCH v2 3/3] spi: spidev: Document SPI_IOC_MESSAGE_FLAGS
2026-12-01 6:51 [PATCH v2 0/3] spi: spidev: Allow userspace to request DTR transfers Cheng Ming Lin
2026-12-01 6:52 ` [PATCH v2 1/3] " Cheng Ming Lin
2026-12-01 6:52 ` [PATCH v2 2/3] spi: spidev_test: Add an option " Cheng Ming Lin
@ 2026-12-01 6:52 ` Cheng Ming Lin
2 siblings, 0 replies; 4+ messages in thread
From: Cheng Ming Lin @ 2026-12-01 6:52 UTC (permalink / raw)
To: Mark Brown; +Cc: linux-spi, linux-api, Miquel Raynal, alvinzhou
Describe the flags field of struct spi_ioc_transfer, SPI_IOC_XFER_DTR
and the SPI_IOC_MESSAGE_FLAGS request, including how it differs from
SPI_IOC_MESSAGE and how it fails on unsupported controllers and older
kernels.
Signed-off-by: Cheng Ming Lin <chengminglin@mxic.com.tw>
---
Documentation/spi/spidev.rst | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/Documentation/spi/spidev.rst b/Documentation/spi/spidev.rst
index e08b301ad..5595629c2 100644
--- a/Documentation/spi/spidev.rst
+++ b/Documentation/spi/spidev.rst
@@ -112,7 +112,7 @@ would expect.
Standard read() and write() operations are obviously only half-duplex, and
the chipselect is deactivated between those operations. Full-duplex access,
and composite operation without chipselect de-activation, is available using
-the SPI_IOC_MESSAGE(N) request.
+the SPI_IOC_MESSAGE(N) and SPI_IOC_MESSAGE_FLAGS(N) requests.
Several ioctl() requests let your driver read or override the device's current
settings for data transfer parameters:
@@ -189,3 +189,11 @@ and bitrate for each transfer segment.)
To make a full duplex request, provide both rx_buf and tx_buf for the
same transfer. It's even OK if those are the same buffer.
+
+Each transfer can also request a Double Transfer Rate (DTR) transfer by
+setting SPI_IOC_XFER_DTR in the flags field of struct spi_ioc_transfer.
+The flags field is only honoured by the SPI_IOC_MESSAGE_FLAGS(N) request,
+which otherwise behaves like SPI_IOC_MESSAGE(N); SPI_IOC_MESSAGE(N) ignores
+it. SPI_IOC_MESSAGE_FLAGS(N) fails with EINVAL if a transfer sets an
+unknown flag or requests DTR from a controller that does not support it,
+and with ENOTTY on kernels that do not support the request.
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread