All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] i2c-tools: i2ctransfer: Set I2C adapter timeout using I2C_TIMEOUT ioctl
@ 2026-07-20 16:34 Vasantha Likitha T
  2026-07-20 22:10 ` Wolfram Sang
  0 siblings, 1 reply; 6+ messages in thread
From: Vasantha Likitha T @ 2026-07-20 16:34 UTC (permalink / raw)
  To: linux-i2c; +Cc: jdelvare, git, Vasantha Likitha T

Add a -t TIMEOUT option that calls ioctl(I2C_TIMEOUT) before the
I2C_RDWR transfer. This allows the adapter timeout to be set from
user space at runtime, controlling how long the I2C core waits for
a transfer to complete before returning an error. Drivers that rely
on the adapter timeout will use the value set by this ioctl.

The timeout value is specified in units of 10 ms, as this is the unit
the Linux kernel expects for the I2C_TIMEOUT ioctl: i2c-dev converts it
with adapter->timeout = msecs_to_jiffies(arg * 10). Omitting -t leaves
the driver default unchanged.

Signed-off-by: Vasantha Likitha T <vasanthalikitha.tandra@amd.com>
---
 tools/i2ctransfer.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/tools/i2ctransfer.c b/tools/i2ctransfer.c
index 5fe44fa..f2c9929 100644
--- a/tools/i2ctransfer.c
+++ b/tools/i2ctransfer.c
@@ -59,6 +59,7 @@ static void help(void)
 		"           -v verbose mode\n"
 		"           -V version info\n"
 		"           -y yes to all confirmations\n"
+		"           -t TIMEOUT set adapter timeout via I2C_TIMEOUT ioctl (unit: 10 ms)\n"
 		"  I2CBUS is an integer or an I2C bus name\n"
 		"  DESC describes the transfer in the form: [inpst]{r|w}LENGTH[@address]\n"
 		"    1) optional message modifier flags, if supported\n"
@@ -281,6 +282,8 @@ int main(int argc, char *argv[])
 	char filename[20];
 	int i2cbus, address = -1, file, opt, nmsgs = 0, nmsgs_sent, i;
 	int force = 0, yes = 0, version = 0, verbose = 0, all_addrs = 0, binary = 0;
+	/* Adapter timeout requested via -t, in I2C_TIMEOUT units (10 ms). 0 = keep the driver default. */
+	unsigned long timeout_10ms = 0;
 	struct i2c_msg msgs[I2C_RDRW_IOCTL_MAX_MSGS];
 	enum parse_state state = PARSE_GET_DESC;
 	unsigned int buf_idx = 0;
@@ -289,7 +292,7 @@ int main(int argc, char *argv[])
 	memset(msgs, 0, sizeof(msgs));
 
 	/* handle (optional) flags first */
-	while ((opt = getopt(argc, argv, "abfhvVy")) != -1) {
+	while ((opt = getopt(argc, argv, "abfhvVyt:")) != -1) {
 		switch (opt) {
 		case 'a': all_addrs = 1; break;
 		case 'b': binary = 1; break;
@@ -297,6 +300,8 @@ int main(int argc, char *argv[])
 		case 'v': verbose = 1; break;
 		case 'V': version = 1; break;
 		case 'y': yes = 1; break;
+		/* -t TIMEOUT: adapter timeout in units of 10 ms */
+		case 't': timeout_10ms = strtoul(optarg, NULL, 0); break;
 		case '?':
 		case 'h':
 			help();
@@ -475,6 +480,20 @@ int main(int argc, char *argv[])
 		memset(&rdwr, 0, sizeof(rdwr));
 		rdwr.msgs = msgs;
 		rdwr.nmsgs = nmsgs;
+		/*
+		 * If the user passed -t, set the adapter timeout before the
+		 * transfer. This lets the timeout be controlled from user space
+		 * so its effect on mux arbitration (e.g. pca9541) can be observed.
+		 * Without -t the timeout is left at the driver default.
+		 */
+		if (timeout_10ms) {
+			if (ioctl(file, I2C_TIMEOUT, timeout_10ms) < 0)
+				fprintf(stderr, "Warning: I2C_TIMEOUT ioctl failed: %s\n",
+					strerror(errno));
+			else
+				fprintf(stderr, "I2C_TIMEOUT set to %lu (x10 ms)\n",
+					timeout_10ms);
+		}
 		nmsgs_sent = ioctl(file, I2C_RDWR, &rdwr);
 		if (nmsgs_sent < 0) {
 			fprintf(stderr, "Error: Sending messages failed: %s\n", strerror(errno));
-- 
2.34.1


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

end of thread, other threads:[~2026-07-22  7:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-20 16:34 [PATCH] i2c-tools: i2ctransfer: Set I2C adapter timeout using I2C_TIMEOUT ioctl Vasantha Likitha T
2026-07-20 22:10 ` Wolfram Sang
2026-07-21  5:17   ` Tandra, Vasantha Likitha
2026-07-21  6:53     ` Wolfram Sang
2026-07-22  6:06   ` Mukesh Savaliya
2026-07-22  7:56     ` Wolfram Sang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.