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 v2 1/3] spi: spidev: Allow userspace to request DTR transfers
Date: Tue,  1 Dec 2026 14:52:00 +0800	[thread overview]
Message-ID: <20261201065202.2667609-2-chengminglin@mxic.com.tw> (raw)
In-Reply-To: <20261201065202.2667609-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 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


  reply	other threads:[~2026-12-01  6:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

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=20261201065202.2667609-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