i2ctransfer: Add optional message modifier flags
PATCH v4 1/1 i2ctransfer: Add optional message modifier flags
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 at runtime.
---
I2C messages can be modified with a set of flags covered by the protocol mangling and the skip repeated start functionalities.
The 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 v4:
- Rebase on current upstream and drop first patch that has been merged.
- Return an enum from add_flag_if_supported(), to distinguish between the various possible cases.
- Link to v3: https://lore.kernel.org/r/[email protected]
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]
--- tools/i2ctransfer.8 | 28 +++++++++- tools/i2ctransfer.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 159 insertions(+), 14 deletions(-)
tools/i2ctransfer.8 +27 -1
@@ -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 +132 -13
200 lines not on this page · raw message
---
base-commit: ec1267371e378fc6aaf1cd7686b5b9b31293255a
change-id: 20251127-msg-flags-3d2b2da9ae28
Best regards,
Signature · 3 lines
Benoît Monin, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Hi Benoît,
Looks mostly good, I think we are nearly there.
Quoted 5 lines
tools/i2ctransfer.c +5
-0+/* unexpected next part of the message: length or address */ +case '0' : +case '1' : +case '2' : +case '3' :
tools/i2ctransfer.c static int confirm(const char *filename, struct i2c_msg *msgs, __u32 nmsgs) · line 230
Quoted 10 lines
tools/i2ctransfer.c +10
-0+case '4' : +case '5' : +case '6' : +case '7' : +case '8' : +case '9' : +case '?' : +case '@' : +fprintf (stderr,"Error: Unexpected flag '%c'\n" , arg); +return FLAGS_UNEXPECTED ;
I suggest to drop this error message. The above characters are not exactly flags. By removing the printout here...
tools/i2ctransfer.c int main(int argc, char *argv[]) · line 325
Quoted 3 lines
tools/i2ctransfer.c +3
-0+for (ret =FLAGS_GOT_OPTIONAL ; *arg_ptr && ret ==FLAGS_GOT_OPTIONAL ; arg_ptr++) + ret =add_flag_if_supported (&flags, funcs, *arg_ptr); +if (ret ==FLAGS_UNKNOWN || ret ==FLAGS_UNEXPECTED || ret ==FLAGS_UNSUPPORTED )
... and simplifying this to ...
+ if (ret == FLAGS_UNKNOWN || ret == FLAGS_UNSUPPORTED)
... we get the error message of a missing r/w-flag instead. I think this is more helpful to the user? What do you think?
Happy hacking,
Wolfram