* [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; 2+ 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] 2+ messages in thread* Re: [PATCH] i2c-tools: i2ctransfer: Set I2C adapter timeout using I2C_TIMEOUT ioctl
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
0 siblings, 0 replies; 2+ messages in thread
From: Wolfram Sang @ 2026-07-20 22:10 UTC (permalink / raw)
To: Vasantha Likitha T; +Cc: linux-i2c, jdelvare, git
[-- Attachment #1: Type: text/plain, Size: 1451 bytes --]
Hi,
thank you for this patch!
On Mon, Jul 20, 2026 at 10:04:01PM +0530, Vasantha Likitha T wrote:
> 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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-20 22:10 UTC | newest]
Thread overview: 2+ 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox