Skip to contentSkip to conversation

Add support for message modifier flags

PATCH Applied Benoît Monin 3 participants 11 messages Jan 27 – Jun 8 I2C subsystem host drivers

v3 of 5 Applied Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add support for message modifier flags

I2C messages can be modified with a set of flags covered by the protocol mangling and the skip repeated start functionalities. This series add support for such flags to i2cdetect and i2ctransfer.

The first patch shows the support of protocol mangling and repeated start skipping in the output of 'i2cdetect -F'.

The second patch adds the parsing of optional flags to i2ctransfer message description. Those command-line flags then set the i2c message flags alongside the read/write flag.

I wrote these changes to test the insertion of I2C_M_STOP flag in multi-message transactions with i2ctransfer, but the other flags may be useful for various test scenarios.

---
Changes in v3:
- Use ifdefs to check that the flags and functionalities are known
at compile-time.
- Check that the adapter supports the requested flags before using them
in i2ctransfer.
- Add a warning in i2ctransfer man page about the risk of using these flags.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Document the flags in i2ctransfer.8 man page.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Benoît Monin (2):
i2cdetect: Display mangling and nostart support
i2ctransfer: Add optional message modifier flags

 tools/i2cdetect.c   |   8 ++++
 tools/i2ctransfer.8 |  28 ++++++++++++-
 tools/i2ctransfer.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 141 insertions(+), 14 deletions(-)

---
base-commit: c1b0305972e89aae048068a3b228f35ef0206243
change-id: 20251127-msg-flags-3d2b2da9ae28

Best regards,

Signature · 3 lines
Benoît Monin, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#1 lore raw

PATCH v3 1/2 i2cdetect: Display mangling and nostart support

Show the support of protocol mangling and repeated start elision in the list of fonctionnalities displayed with '-F'. We only use these functionnalities if they are known at compile-time so i2cdetect can be built against older kernels.

---
 tools/i2cdetect.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/i2cdetect.c +8 -0
index bb2f146..3c22f17 100644
@@ -164,6 +164,14 @@ static const struct func all_func[] = {
 #if defined(I2C_FUNC_SLAVE)
 	{ .value = I2C_FUNC_SLAVE,
 	  .name = "Target mode" },
+#endif
+#if defined(I2C_FUNC_PROTOCOL_MANGLING)
+	{ .value = I2C_FUNC_PROTOCOL_MANGLING,
+	  .name = "Protocol mangling" },
+#endif
+#if defined(I2C_FUNC_NOSTART)
+	{ .value = I2C_FUNC_NOSTART,
+	  .name = "Repeated start skipping" },
 #endif
 	{ .value = 0, .name = "" }
 };
Signature · 1 line
2.52.0
#3 lore raw

PATCH v3 2/2 i2ctransfer: Add optional message modifier flags

In reply to Benoît Monin

Allow setting protocol mangling and repeated start elision flags of an i2c message with a set of optional command-line flags. These optional flags are parsed at the beginning of the DESC field up to a read or write flag.

For example, to read one byte from address 0x50 followed by a stop, then write two bytes at 0x54 on bus 0, one would call i2ctransfer as follow:

    i2ctransfer 0 pr1@0x50 w2@0x54 0x10 0x20

Since the new flags are optional, this patch preserves the compatibility of the i2ctransfer syntax.

Handling of the message flags is done in add_flag_if_supported(). This function checks if the flag is defined at compile time and if the adapter supports the required functionality to handle the flag.

---
 tools/i2ctransfer.8 |  28 ++++++++++++-
 tools/i2ctransfer.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 133 insertions(+), 14 deletions(-)
tools/i2ctransfer.8 +27 -1
index 4bdf436..ca41241 100644
@@ -96,8 +96,11 @@ The number of blocks is limited by the Linux Kernel and defined by I2C_RDWR_IOCT
 .I desc
 blocks are composed like this:
 
-.I {r|w}<length_of_message>[@address]
+.I [inpst]{r|w}<length_of_message>[@address]
 
+.TP
+.B [inpst]
+If supported, specifies optional MESSAGE MODIFIER FLAGS. See the section below for details.
 .TP
 .B {r|w}
 specifies if the message is read or write
@@ -141,6 +144,29 @@ decrease value by 1 until end of message (i.e. 0xff- means 0xff, 0xfe, 0xfd, ...
 p
 use value as seed for an 8 bit pseudo random sequence (i.e. 0p means 0x00, 0x50, 0xb0, ...)
 
+.SH "MESSAGE MODIFIER FLAGS"
+.PP
+These optional flags can be used to change the handling of a message when it is sent on the bus.
+They allow you to deviate from the I2C standard to work around device issues.
+They should be used with care, as they can confuse your I2C bus.
+Note that not all flags (or any) may be supported by your particular hardware and your kernel version.
+
+.TP
+.B i
+ignore NACK from client, treat them as ACK.
+.TP
+.B n
+in a read message, master ACK/NACK bit is skipped.
+.TP
+.B p
+emit a STOP after the message.
+.TP
+.B s
+skip repeated start.
+.TP
+.B t
+toggle read/write bit.
+
 .SH EXAMPLES
 .PP
 On bus 0, from an EEPROM at address 0x50, read 8 byte from offset 0x64
tools/i2ctransfer.c +106 -13

168 lines not on this page · raw message

Signature · 1 line
2.52.0
#8 lore raw
In reply to Benoît Monin

Hi Benoît,

Quoted 35 lines · Benoît Monin

I2C messages can be modified with a set of flags covered by the protocol mangling and the skip repeated start functionalities. This series add support for such flags to i2cdetect and i2ctransfer.

The first patch shows the support of protocol mangling and repeated start skipping in the output of 'i2cdetect -F'.

The second patch adds the parsing of optional flags to i2ctransfer message description. Those command-line flags then set the i2c message flags alongside the read/write flag.

I wrote these changes to test the insertion of I2C_M_STOP flag in multi-message transactions with i2ctransfer, but the other flags may be useful for various test scenarios.

---
Changes in v3:
- Use ifdefs to check that the flags and functionalities are known
at compile-time.
- Check that the adapter supports the requested flags before using them
in i2ctransfer.
- Add a warning in i2ctransfer man page about the risk of using these flags.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Document the flags in i2ctransfer.8 man page.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Benoît Monin (2):
i2cdetect: Display mangling and nostart support
i2ctransfer: Add optional message modifier flags

Quoted 4 lines
 tools/i2cdetect.c   |   8 ++++
 tools/i2ctransfer.8 |  28 ++++++++++++-
 tools/i2ctransfer.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 141 insertions(+), 14 deletions(-)

Both patches look very good to me. I was able to test the i2cdetect patch and did not find any issue. I can't easily test the i2ctransfer patch though.

Wolfram, can you please give some basic testing to the i2ctransfer patch? Not necessarily with the mangling flags, but at least test that the changes do not introduce any regression for your own use cases?

Thanks,

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#9 lore raw

Hello Wolfram,

Quoted 47 lines · Jean Delvare

Hi Benoît,

Quoted 35 lines · Benoît Monin

I2C messages can be modified with a set of flags covered by the protocol mangling and the skip repeated start functionalities. This series add support for such flags to i2cdetect and i2ctransfer.

The first patch shows the support of protocol mangling and repeated start skipping in the output of 'i2cdetect -F'.

The second patch adds the parsing of optional flags to i2ctransfer message description. Those command-line flags then set the i2c message flags alongside the read/write flag.

I wrote these changes to test the insertion of I2C_M_STOP flag in multi-message transactions with i2ctransfer, but the other flags may be useful for various test scenarios.

---
Changes in v3:
- Use ifdefs to check that the flags and functionalities are known
at compile-time.
- Check that the adapter supports the requested flags before using them
in i2ctransfer.
- Add a warning in i2ctransfer man page about the risk of using these flags.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Document the flags in i2ctransfer.8 man page.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Benoît Monin (2):
i2cdetect: Display mangling and nostart support
i2ctransfer: Add optional message modifier flags

Quoted 4 lines
 tools/i2cdetect.c   |   8 ++++
 tools/i2ctransfer.8 |  28 ++++++++++++-
 tools/i2ctransfer.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 141 insertions(+), 14 deletions(-)

Both patches look very good to me. I was able to test the i2cdetect patch and did not find any issue. I can't easily test the i2ctransfer patch though.

Quoted 6 lines

Wolfram, can you please give some basic testing to the i2ctransfer patch? Not necessarily with the mangling flags, but at least test that the changes do not introduce any regression for your own use cases?

Thanks,

Did you have a chance to test the patches?

Best regards,

Signature · 3 lines
Benoît Monin, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#10 lore raw
Quoted 2 lines

Did you have a chance to test the patches?

Code looks good to me, will test it in the next days.

#4 lore raw

Re: [PATCH i2c-tools v3 2/2] i2ctransfer: Add optional message modifier flags

In reply to Benoît Monin

Hi Benoît,

Quoted 9 lines · Benoît Monin

Allow setting protocol mangling and repeated start elision flags of an i2c message with a set of optional command-line flags. These optional flags are parsed at the beginning of the DESC field up to a read or write flag.

For example, to read one byte from address 0x50 followed by a stop, then write two bytes at 0x54 on bus 0, one would call i2ctransfer as follow:

    i2ctransfer 0 pr1@0x50 w2@0x54 0x10 0x20
Quoted 8 lines

Since the new flags are optional, this patch preserves the compatibility of the i2ctransfer syntax.

Handling of the message flags is done in add_flag_if_supported(). This function checks if the flag is defined at compile time and if the adapter supports the required functionality to handle the flag.

I don't have a controller supporting 'mangling' here at the moment, but the code looks good and my regression tests went fine. There is one thing about error handling, though:

tools/i2ctransfer.c int main(int argc, char *argv[]) · line 304

Quoted 9 lines
tools/i2ctransfer.c +7 -0
+			for (ret = 0; *arg_ptr && !ret; arg_ptr++) {
+				ret = add_flag_if_supported(&flags, funcs, *arg_ptr);
+				if (ret < 0)
+					goto err_out_with_arg;
+			}
+			if (ret != 1) {
+				fprintf(stderr, "Error: Missing direction flag 'r' or 'w'\n");
 				goto err_out_with_arg;
 			}

This error message now only shows up if the 'rw'-flag is missing at the end of the commandline:

# ./i2ctransfer -y 0 p
Error: Missing direction flag 'r' or 'w'
Error: faulty argument is 'p'

Otherwise it complains about '@' not being a flag:

# ./i2ctransfer -y 0 p@0x1a
Error: Unsupported flag '@'
Error: faulty argument is 'p@0x1a'

It would be nice if '@' would be recognized as a terminator for the flags and would then complain about the missing direction flag, or?

Happy hacking,

Wolfram

Applied
#2 lore raw

Re: [PATCH i2c-tools v3 1/2] i2cdetect: Display mangling and nostart support

In reply to Benoît Monin
Quoted 6 lines · Benoît Monin

Show the support of protocol mangling and repeated start elision in the list of fonctionnalities displayed with '-F'. We only use these functionnalities if they are known at compile-time so i2cdetect can be built against older kernels.

Applied, thanks!

#5 lore raw

Re: [PATCH i2c-tools v3 2/2] i2ctransfer: Add optional message modifier flags

In reply to Wolfram Sang

Hi Wolfram,

On Wednesday, 3 June 2026 at 09:33:37 CEST, Wolfram Sang wrote:
[...]

Quoted 7 lines

This error message now only shows up if the 'rw'-flag is missing at the end of the commandline:

# ./i2ctransfer -y 0 p
Error: Missing direction flag 'r' or 'w'
Error: faulty argument is 'p'
Quoted 8 lines

Otherwise it complains about '@' not being a flag:

# ./i2ctransfer -y 0 p@0x1a
Error: Unsupported flag '@'
Error: faulty argument is 'p@0x1a'

It would be nice if '@' would be recognized as a terminator for the flags and would then complain about the missing direction flag, or?

It's true that the error message is imprecise but I wanted to avoid duplicating the parsing that is done after parsing the flags. I can add something like the following in the switch case in add_flag_if_supported():

@@ -202,6 +202,20 @@ static int add_flag_if_supported(__u16 *flags, unsigned long funcs, char arg)

                        break;

#endif

+               case '0':
+               case '1':
+               case '2':
+               case '3':
+               case '4':
+               case '5':
+               case '6':
+               case '7':
+               case '8':
+               case '9':
+               case '?':
+               case '@':

+ return 0;
+
default:

With this we get out of the flags parsing and the "Missing direction flag" error message is shown. What's your opinion on this?

Best regards,

Signature · 3 lines
Benoît Monin, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw

Re: [PATCH i2c-tools v3 2/2] i2ctransfer: Add optional message modifier flags

Hi Benoît,

Quoted 21 lines
(quoted diff) +14 -0
@@ -202,6 +202,20 @@ static int add_flag_if_supported(__u16 *flags, unsigned long funcs, char arg)
                        break;
 #endif
 
+               case '0':
+               case '1':
+               case '2':
+               case '3':
+               case '4':
+               case '5':
+               case '6':
+               case '7':
+               case '8':
+               case '9':
+               case '?':
+               case '@':
+                       return 0;
+
                default:
 
 
Quoted 2 lines

With this we get out of the flags parsing and the "Missing direction flag" error message is shown. What's your opinion on this?

Basically fine with me, but we need another adaption then because this documentation is not precise anymore:

+ * 0 if the argument is a valid and supported optional flag

Doesn't fit anymore because for the above cases, the argument is not valid.

I think we should return a dedicated value for this 'unexpected' case.

Which brings something again to the table I was close to suggesting last time already but back then refrained from doing so. Now, it might be good after all:

It is about not using magic numbers for the return values of that function but an enum with descriptive entries. Example:

enum supported_flags_retval {
FLAGS_GOT_RW,
FLAGS_GOT_OPTIONAL,
FLAGS_UNEXPECTED,
FLAGS_UNKNOWN,
};

I think this will make life for future hackers quite easier. What do you think?

Happy hacking,

Wolfram

#7 lore raw

Re: [PATCH i2c-tools v3 2/2] i2ctransfer: Add optional message modifier flags

Hi Wolfram,

Quoted 48 lines · Wolfram Sang
Quoted 21 lines
(quoted diff) +14 -0
@@ -202,6 +202,20 @@ static int add_flag_if_supported(__u16 *flags, unsigned long funcs, char arg)
                        break;
 #endif
 
+               case '0':
+               case '1':
+               case '2':
+               case '3':
+               case '4':
+               case '5':
+               case '6':
+               case '7':
+               case '8':
+               case '9':
+               case '?':
+               case '@':
+                       return 0;
+
                default:
 
 
Quoted 2 lines

With this we get out of the flags parsing and the "Missing direction flag" error message is shown. What's your opinion on this?

Basically fine with me, but we need another adaption then because this documentation is not precise anymore:

+ * 0 if the argument is a valid and supported optional flag

Doesn't fit anymore because for the above cases, the argument is not valid.

I think we should return a dedicated value for this 'unexpected' case.

Which brings something again to the table I was close to suggesting last time already but back then refrained from doing so. Now, it might be good after all:

It is about not using magic numbers for the return values of that function but an enum with descriptive entries. Example:

enum supported_flags_retval {
FLAGS_GOT_RW,
FLAGS_GOT_OPTIONAL,
FLAGS_UNEXPECTED,
FLAGS_UNKNOWN,
};

Quoted 3 lines

I think this will make life for future hackers quite easier. What do you think?

Good idea! I'll update to patch and resend.

Best regards,

Signature · 3 lines
Benoît Monin, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com