Skip to contentSkip to conversation

i2c-tools: i2ctransfer: Set I2C adapter timeout using I2C_TIMEOUT ioctl

PATCH In review Vasantha Likitha T 3 participants 8 messages Jul 20 – Nov 8 I2C subsystem host drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c-tools: i2ctransfer: Set I2C adapter timeout using I2C_TIMEOUT ioctl

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.

---
 tools/i2ctransfer.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
tools/i2ctransfer.c +20 -1
index 5fe44fa..f2c9929 100644
@@ -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));
Signature · 1 line
2.34.1
#1 lore raw

Hi,

thank you for this patch!

Quoted 10 lines · 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.

I agree that reading/setting TIMEOUT is missing in i2c-tools (same with RETRIES) and it would be nice to have.

I don't think 'i2ctransfer' is the right place for it, though. One might also want a different timeout with 'i2cget' or so.

My suggestion is to add it to 'i2cdetect'. It already has '-F' to list the features of an adapter. We could add '-T' to read the timeout value and '-R' to read the retries value. If '-T' or '-R' parameters also contain a value, then this value will be written to the adapter.

Was this undestandable? Do you agree?

Another option would be to write a dedicated tool ('i2cconfig'?) for handling extra parameters, but it feels a bit bloated IMHO.

Opinions?

All the best,

Wolfram

#2 lore raw

AMD General

Hi Wolfram,

Thanks for the review. Your suggestion makes sense to me - I'll move it out of i2ctransfer and add -T (timeout) and -R (retries) to i2cdetect instead. I agree a separate i2cconfig tool is not needed.

While checking the list of ioctls, I observed the I2C_TIMEOUT ioctl to set the timeout, but I did not observe any "get" ioctl to read the timeout value back from user space (same for retries). So setting a value with -T/-R works, but reading the current value (when no value is given) does not seem possible right now.

Could you let me know how you would like me to handle this - should I keep -T/-R as set-only for now?

Thanks,
Vasantha Likitha

-----Original Message-----
From: Wolfram Sang <[email protected]>
Sent: Tuesday, July 21, 2026 3:41 AM
To: Tandra, Vasantha Likitha <[email protected]>
Cc: [email protected]; [email protected]; git (AMD-Xilinx) <[email protected]>
Subject: Re: [PATCH] i2c-tools: i2ctransfer: Set I2C adapter timeout using I2C_TIMEOUT ioctl

Hi,

thank you for this patch!

Quoted 10 lines · 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.

I agree that reading/setting TIMEOUT is missing in i2c-tools (same with RETRIES) and it would be nice to have.

I don't think 'i2ctransfer' is the right place for it, though. One might also want a different timeout with 'i2cget' or so.

My suggestion is to add it to 'i2cdetect'. It already has '-F' to list the features of an adapter. We could add '-T' to read the timeout value and '-R' to read the retries value. If '-T' or '-R' parameters also contain a value, then this value will be written to the adapter.

Was this undestandable? Do you agree?

Another option would be to write a dedicated tool ('i2cconfig'?) for handling extra parameters, but it feels a bit bloated IMHO.

Opinions?

All the best,

Wolfram

#3 lore raw

Hi,

Quoted 3 lines

Thanks for the review. Your suggestion makes sense to me - I'll move it out of i2ctransfer and add -T (timeout) and -R (retries) to i2cdetect instead. I agree a separate i2cconfig tool is not needed.

Great, thanks.

Quoted 5 lines

While checking the list of ioctls, I observed the I2C_TIMEOUT ioctl to set the timeout, but I did not observe any "get" ioctl to read the timeout value back from user space (same for retries). So setting a value with -T/-R works, but reading the current value (when no value is given) does not seem possible right now.

You are right. That shows how seldom those IOCTLs are used. I forgot that one cannot read them, sorry about that.

Quoted 2 lines

Could you let me know how you would like me to handle this - should I keep -T/-R as set-only for now?

Yes. This is the way to go.

Happy hacking,

Wolfram

#4 lore raw

Hi Vasantha,

Quoted 4 lines
Quoted 2 lines

Could you let me know how you would like me to handle this - should I keep -T/-R as set-only for now?

Yes. This is the way to go.

Did you have time to work on this? I'd like to include it in the new i2c-tools version which shall come out somewhen this year.

Happy hacking,

Wolfram

#5 lore raw
In reply to Wolfram Sang
Quoted 8 lines · Wolfram Sang

Hi,

thank you for this patch!

Quoted 3 lines · 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

Why userspace need to decide and set the timeout values for adapter ?
Adapter knows the transfer frequency and transfer data length, it can be calculated by core layer itself and can set.

Retrials can be fixed and hard coded for everyone.

Is there any other purpose which is missing with the core layer deciding timeout ?

Review here @ https://lore.kernel.org/all/[email protected]/ discussion ongoing to add timeout by i2c-core-base.c

Quoted 30 lines
Quoted 7 lines

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.

I agree that reading/setting TIMEOUT is missing in i2c-tools (same with RETRIES) and it would be nice to have.

I don't think 'i2ctransfer' is the right place for it, though. One might also want a different timeout with 'i2cget' or so.

My suggestion is to add it to 'i2cdetect'. It already has '-F' to list the features of an adapter. We could add '-T' to read the timeout value and '-R' to read the retries value. If '-T' or '-R' parameters also contain a value, then this value will be written to the adapter.

Was this undestandable? Do you agree?

Another option would be to write a dedicated tool ('i2cconfig'?) for handling extra parameters, but it feels a bit bloated IMHO.

Opinions?

All the best,

Wolfram

#6 lore raw
Quoted 4 lines

Why userspace need to decide and set the timeout values for adapter ? Adapter knows the transfer frequency and transfer data length, it can be calculated by core layer itself and can set.

Freq and data length are not enough for timeout calculation. It is also board specific (e.g. capacitance) and environment specific (surrounding noise). It can be argued to modify it from userspace. Unclear how much that is used in practice, though.

Quoted 1 line

Retrials can be fixed and hard coded for everyone.

I don't think we can remove the interface because of the "don't break userspace" rule.

#7 lore raw

Thanks Wolfram !

Quoted 9 lines · Wolfram Sang
Quoted 4 lines

Why userspace need to decide and set the timeout values for adapter ? Adapter knows the transfer frequency and transfer data length, it can be calculated by core layer itself and can set.

Freq and data length are not enough for timeout calculation. It is also board specific (e.g. capacitance) and environment specific (surrounding noise). It can be argued to modify it from userspace. Unclear how much that is used in practice, though.

For board specific, i suggest to keep offset or co-efficient which can be read from DTSI. Passing from userspace would like just trial and errors.

if the framework (i2c core) manage this then kernel space and user space both can be taken cared, right ? And as i mentioned, it also takes care of misc board parameters with cushion parameters.

Quoted 6 lines
Quoted 2 lines

Retrials can be fixed and hard coded for everyone.

I don't think we can remove the interface because of the "don't break userspace" rule.

I am thinking timeout handling should be fixed within the kernel framework regardless of any userspace configuration. If a userspace knob is still needed, it should only serve as an optional override for cases where the kernel-side logic proves insufficient. Once the timeout calculation and retry behavior are handled correctly in-kernel, the userspace interface becomes largely redundant and may eventually be considered obsolete. No need to remove existing one.