i2c-tools: Make tools accept bus path
v4 cover i2c-tools: Make tools accept bus path
The purpose of this series is to make i2c-tools userspace utilities accept an i2c bus character device file path as an argument. This is useful, for example, if a sysadmin creates udev rules to create a persistent symlink to an i2c bus and wishes to use that symlink with i2c-tools.
I've tested these changes by running i2cdetect, i2cget, and i2cset. Please be aware that my current test setup has an i801 bus controller with what seems to be no slave devices connected, so my testing has been superficial, involving mostly failed read/writes. This patch series only touches code that actually opens the character device file, so I'm satisfied with this testing. If maintainers prefer, I'm happy to test more rigorously.
Since v1:
* Scrapped brittle checking against device major number 89 in favor of an i2c_bus_hint variant struct.
Since v2:
* Scrapped i2c_bus_hint variant in favor of combining the lookup and open steps into one function. No more awkward variant.
Since v3:
* Rebased onto master and fixed minor conflicts.
---
Brigham Campbell (2):
i2c-tools: Allow passing device file paths
i2c-tools: Document device paths as I2CBUS arg
tools/i2cbusses.c | 40 ++++++++++++++++++++++++++++++++++------ tools/i2cbusses.h | 4 +++- tools/i2cdetect.8 | 8 ++++---- tools/i2cdetect.c | 14 +++++--------- tools/i2cdump.8 | 7 ++++--- tools/i2cdump.c | 14 +++++--------- tools/i2cget.8 | 9 +++++---- tools/i2cget.c | 13 +++++-------- tools/i2cset.8 | 12 ++++++------ tools/i2cset.c | 13 +++++-------- tools/i2ctransfer.8 | 2 +- tools/i2ctransfer.c | 13 +++++-------- 12 files changed, 82 insertions(+), 67 deletions(-)
---
base-commit: 5852f5375eea6ca9dabf31f4772cb8b8ecc905d8
change-id: 20260530-accept-device-path-539164c9c1df
Best regards,
Signature · 1 line
Brigham Campbell <[email protected]>
PATCH v4 1/2 i2c-tools: Allow passing device file paths
Make i2c-tools binaries accept a device file path or symlink directly. This is useful, for example, for use with stable symlinks generated by udev.
In the case of conflict, the bus parameter is parsed with the following precedence in order to maintain backwards compatibility:
number -> name -> device path
--- tools/i2cbusses.c | 40 ++++++++++++++++++++++++++++++++++------ tools/i2cbusses.h | 4 +++- tools/i2cdetect.c | 12 ++++-------- tools/i2cdump.c | 12 ++++-------- tools/i2cget.c | 11 ++++------- tools/i2cset.c | 11 ++++------- tools/i2ctransfer.c | 11 ++++------- 7 files changed, 57 insertions(+), 44 deletions(-)
tools/i2cbusses.c +34 -6
@@ -65,7 +65,7 @@ static enum adt i2c_get_funcs(int i2cbus)char filename[20 ];enum adt ret; - file =open_i2c_dev (i2cbus, filename,sizeof (filename),1 ); + file =open_i2c_dev_num (i2cbus, filename,sizeof (filename),1 );if (file <0 )return adt_unknown;
@@ -348,10 +348,6 @@ static int lookup_i2c_bus_by_name(const char *bus_name) } } -if (i2cbus ==-1 ) -fprintf (stderr,"Error: I2C bus name doesn't match any " -"bus present!\n" ); - done:free_adapters (adapters);return i2cbus;
@@ -410,7 +406,7 @@ int parse_i2c_address(const char *address_arg, int all_addrs)return address; } -int open_i2c_dev (int i2cbus,char *filename,size_t size,int quiet) +int open_i2c_dev_num (int i2cbus,char *filename,size_t size,int quiet) {int file, len;
@@ -446,6 +442,38 @@ int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet)return file; } +int open_i2c_dev_path (const char *i2cbus_arg,int quiet) +{ +int file =open (i2cbus_arg,O_RDWR ); + +if (file <0 && !quiet) { +fprintf (stderr,"Error: Could not open file " +"`%s': %s\n" , i2cbus_arg,strerror (errno)); +if (errno ==EACCES ) +fprintf (stderr,"Run as root?\n" ); + } + +return file; +} + +int open_i2c_dev (char *i2cbus_arg,char **filename,size_t size,int quiet) +{ +int file, i2cbus; + + i2cbus =lookup_i2c_bus (i2cbus_arg); + +if (i2cbus <0 ) { + *filename = i2cbus_arg; + file =open_i2c_dev_path (i2cbus_arg, quiet); +if (file <0 ) +fprintf (stderr,"Failed to open `%s' as a bus name " +"and as a path.\n" , i2cbus_arg); +return file; + } + +return open_i2c_dev_num (i2cbus, *filename, size, quiet); +} +int set_slave_addr (int file,int address,int force) {/* With force, let the user read from/write to the registers
tools/i2cbusses.h +3 -1
@@ -31,7 +31,9 @@ void free_adapters(struct i2c_adap *adapters);int lookup_i2c_bus (const char *i2cbus_arg);int parse_i2c_address (const char *address_arg,int all_addrs); -int open_i2c_dev (int i2cbus,char *filename,size_t size,int quiet); +int open_i2c_dev (char *i2cbus_arg,char **filename,size_t size,int quiet); +int open_i2c_dev_num (int i2cbus,char *filename,size_t size,int quiet); +int open_i2c_dev_path (const char *i2cbus_arg,int quiet);int set_slave_addr (int file,int address,int force);#define MISSING_FUNC_FMT "Error: Adapter does not have %s capability\n"
tools/i2cdetect.c +4 -8
@@ -213,8 +213,9 @@ static void print_i2c_busses(void)int main (int argc,char *argv[]) {char *end; -int i2cbus, file, res; -char filename[20 ]; +int file, res; +char filename_buf[20 ]; +char *filename = filename_buf;unsigned long funcs;int mode =MODE_AUTO ;int first =0x08 , last =0x77 ;
@@ -277,11 +278,6 @@ int main(int argc, char *argv[])help ();exit (1 ); } - i2cbus =lookup_i2c_bus (argv[optind]); -if (i2cbus <0 ) { -help (); -exit (1 ); - }/* read address range if present */ if (argc == optind +3 && mode !=MODE_FUNC ) {
@@ -321,7 +317,7 @@ int main(int argc, char *argv[])exit (1 ); } - file =open_i2c_dev (i2cbus, filename,sizeof (filename),0 ); + file =open_i2c_dev (argv[optind], &filename,sizeof (filename_buf),0 );if (file <0 ) {exit (1 ); }
tools/i2cdump.c +4 -8
@@ -100,9 +100,10 @@ static int check_funcs(int file, int size, int pec)int main (int argc,char *argv[]) {char *end; -int i, j, res, i2cbus, address, size, file; +int i, j, res, address, size, file;int bank =0 , bankreg =0x4E , old_bank =0 ; -char filename[20 ]; +char filename_buf[20 ]; +char *filename = filename_buf;int block[256 ];int pec =0 , even =0 ;int opt;
@@ -135,11 +136,6 @@ int main(int argc, char *argv[])help ();exit (1 ); } - i2cbus =lookup_i2c_bus (argv[optind]); -if (i2cbus <0 ) { -help (); -exit (1 ); - }if (argc < optind +2 ) {fprintf (stderr,"Error: No address specified!\n" );
@@ -235,7 +231,7 @@ int main(int argc, char *argv[]) } } - file =open_i2c_dev (i2cbus, filename,sizeof (filename),0 ); + file =open_i2c_dev (argv[optind], &filename,sizeof (filename_buf),0 );if (file <0 ||check_funcs (file, size, pec) ||set_slave_addr (file, address, force))
tools/i2cget.c +4 -7
@@ -169,9 +169,10 @@ static int confirm(const char *filename, int address, int size, int daddress,int main (int argc,char *argv[]) {char *end; -int res, i2cbus, address, size, file; +int res, address, size, file;int daddress; -char filename[20 ]; +char filename_buf[20 ]; +char *filename = filename_buf;int pec =0 ;int opt;int force =0 , yes =0 , version =0 , all_addrs =0 ;
@@ -199,10 +200,6 @@ int main(int argc, char *argv[])if (argc < optind +2 )help (1 ); - i2cbus =lookup_i2c_bus (argv[optind]); -if (i2cbus <0 ) -help (1 ); - address =parse_i2c_address (argv[optind+1 ], all_addrs);if (address <0 )help (1 );
@@ -251,7 +248,7 @@ int main(int argc, char *argv[]) length =I2C_SMBUS_BLOCK_MAX ; } - file =open_i2c_dev (i2cbus, filename,sizeof (filename),0 ); + file =open_i2c_dev (argv[optind], &filename,sizeof (filename_buf),0 );if (file <0 ||check_funcs (file, size, daddress, pec) ||set_slave_addr (file, address, force))
tools/i2cset.c +4 -7
@@ -151,9 +151,10 @@ int main(int argc, char *argv[]) {char *end;const char *maskp =NULL ; -int res, i2cbus, address, size, file; +int res, address, size, file;int value, daddress, vmask =0 ; -char filename[20 ]; +char filename_buf[20 ]; +char *filename = filename_buf;int pec =0 ;int opt;int force =0 , yes =0 , version =0 , readback =0 , all_addrs =0 ;
@@ -183,10 +184,6 @@ int main(int argc, char *argv[])if (argc < optind +3 )help (1 ); - i2cbus =lookup_i2c_bus (argv[optind]); -if (i2cbus <0 ) -help (1 ); - address =parse_i2c_address (argv[optind+1 ], all_addrs);if (address <0 )help (1 );
@@ -301,7 +298,7 @@ int main(int argc, char *argv[]) } } - file =open_i2c_dev (i2cbus, filename,sizeof (filename),0 ); + file =open_i2c_dev (argv[optind], &filename,sizeof (filename_buf),0 );if (file <0 ||check_funcs (file, size, pec) ||set_slave_addr (file, address, force))
tools/i2ctransfer.c +4 -7
@@ -257,8 +257,9 @@ static enum supported_flags_retvalint main (int argc,char *argv[]) { -char filename[20 ]; -int i2cbus, address =-1 , file, opt, nmsgs =0 , nmsgs_sent, i; +char filename_buf[20 ]; +char *filename = filename_buf; +int address =-1 , file, opt, nmsgs =0 , nmsgs_sent, i;int force =0 , yes =0 , version =0 , verbose =0 , all_addrs =0 , binary =0 ;struct i2c_msg msgs[I2C_RDRW_IOCTL_MAX_MSGS ];enum parse_state state =PARSE_GET_DESC ;
@@ -293,11 +294,7 @@ int main(int argc, char *argv[])exit (1 ); } - i2cbus =lookup_i2c_bus (argv[optind++]); -if (i2cbus <0 ) -exit (1 ); - - file =open_i2c_dev (i2cbus, filename,sizeof (filename),0 ); + file =open_i2c_dev (argv[optind++], &filename,sizeof (filename_buf),0 );if (file <0 ||get_funcs (file, &funcs) ||check_funcs (funcs))exit (1 );
Signature · 1 line
2.55.0
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Quoted 11 lines · Brigham Campbell
Make i2c-tools binaries accept a device file path or symlink directly. This is useful, for example, for use with stable symlinks generated by udev.
In the case of conflict, the bus parameter is parsed with the following precedence in order to maintain backwards compatibility:
number -> name -> device path
Suggested-by Gero Schwäricke <[email protected]>Signed-off-by Brigham Campbell <[email protected]>
I general, I like the approach. There is only some inconsistency in the error reporting which is done twice now:
$ sudo tools/i2cdetect -F "i915 gmbus dpe" Error: Could not open file `i915 gmbus dpe': No such file or directory Failed to open `i915 gmbus dpe' as a bus name and as a path.
Here is my proposed solution as it is easier to discuss code. What do you think? In a second step, we could move open_i2c_dev_num() up in the code, make it static, and remove it from the header. Then, only open_i2c_dev() would stay in the public header. I'd think the tools themselves shouldn't know about the differences how to reach the adapter. Or?
From: Wolfram Sang <[email protected]>
Date: Thu, 9 Jul 2026 14:09:41 +0200
Subject: [PATCH i2c-tools] improve error reporting
--- tools/i2cbusses.c | 21 +++++---------------- tools/i2cbusses.h | 1 - 2 files changed, 5 insertions(+), 17 deletions(-)
tools/i2cbusses.c +5 -16
@@ -442,20 +442,6 @@ int open_i2c_dev_num(int i2cbus, char *filename, size_t size, int quiet)return file; } -int open_i2c_dev_path (const char *i2cbus_arg,int quiet) -{ -int file =open (i2cbus_arg,O_RDWR ); - -if (file <0 && !quiet) { -fprintf (stderr,"Error: Could not open file " -"`%s': %s\n" , i2cbus_arg,strerror (errno)); -if (errno ==EACCES ) -fprintf (stderr,"Run as root?\n" ); - } - -return file; -} -int open_i2c_dev (char *i2cbus_arg,char **filename,size_t size,int quiet) {int file, i2cbus;
@@ -464,10 +450,13 @@ int open_i2c_dev(char *i2cbus_arg, char **filename, size_t size, int quiet)if (i2cbus <0 ) { *filename = i2cbus_arg; - file =open_i2c_dev_path (i2cbus_arg, quiet); -if (file <0 ) + file =open (i2cbus_arg,O_RDWR ); +if (file <0 && !quiet) {fprintf (stderr,"Failed to open `%s' as a bus name " "and as a path.\n" , i2cbus_arg); +if (errno ==EACCES ) +fprintf (stderr,"Run as root?\n" ); + }return file; }
tools/i2cbusses.h +0 -1
@@ -33,7 +33,6 @@ int lookup_i2c_bus(const char *i2cbus_arg);int parse_i2c_address (const char *address_arg,int all_addrs);int open_i2c_dev (char *i2cbus_arg,char **filename,size_t size,int quiet);int open_i2c_dev_num (int i2cbus,char *filename,size_t size,int quiet); -int open_i2c_dev_path (const char *i2cbus_arg,int quiet);int set_slave_addr (int file,int address,int force);#define MISSING_FUNC_FMT "Error: Adapter does not have %s capability\n"
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
In reply to Brigham CampbellHi Brigham,
I just finished reviewing your patch. Aaand I saw Wolfram beat me to it by 20 minutes. I hope you forgive me for sending it as is even though it may now contain duplicates to his findings.
On Mon Jul 6, 2026 at 6:27 AM CEST, Brigham Campbell wrote:
[...]
tools/i2cbusses.c int parse_i2c_address(const char *address_arg, int all_addrs) · line 412
Quoted 9 lines
tools/i2cbusses.c +1
-1@@ -410,7 +406,7 @@ int parse_i2c_address(const char *address_arg, int all_addrs)return address; } -int open_i2c_dev (int i2cbus,char *filename,size_t size,int quiet) +int open_i2c_dev_num (int i2cbus,char *filename,size_t size,int quiet) {int file, len;
The lookup function uses suffix _by_name, so for consistency maybe use _by_nr instead? That is what it's called in struct i2c_adap.
Quoted 26 lines
tools/i2cbusses.c +22
-0@@ -446,6 +442,38 @@ int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet)return file; } +int open_i2c_dev_path (const char *i2cbus_arg,int quiet) +{ +int file =open (i2cbus_arg,O_RDWR ); + +if (file <0 && !quiet) { +fprintf (stderr,"Error: Could not open file " +"`%s': %s\n" , i2cbus_arg,strerror (errno)); +if (errno ==EACCES ) +fprintf (stderr,"Run as root?\n" ); + } + +return file; +} + +int open_i2c_dev (char *i2cbus_arg,char **filename,size_t size,int quiet) +{ +int file, i2cbus; + + i2cbus =lookup_i2c_bus (i2cbus_arg); + +if (i2cbus <0 ) { + *filename = i2cbus_arg;
tools/i2cbusses.c int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet) · line 476
Quoted 10 lines
tools/i2cbusses.c +10
-0+ file =open_i2c_dev_path (i2cbus_arg, quiet); +if (file <0 ) +fprintf (stderr,"Failed to open `%s' as a bus name " +"and as a path.\n" , i2cbus_arg); +return file; + } + +return open_i2c_dev_num (i2cbus, *filename, size, quiet); +} +
This is a minimally invasive change that makes this work, but it also spaghettifies the code base a little. lookup_i2c_bus() tries to parse the input to a bus number or, if that doesn't work, tries to resolve it as a bus name. And if that fails we try to open as path. The issue is that lookup_i2c_bus() is not very descriptive. Without reading the code it's not understandable what's happening. Instead I would propose to inline lookup_i2c_bus() and then have the logic inside open_i2c_dev() be imparative like:
1. try to convert input to i2cbus bus number, if it worked return open_i2c_dev_by_nr() with the bus number, otherwise,
2. try lookup_i2c_bus_by_name(), if it worked we now have the bus number, so we return open_i2c_dev_by_nr() with it, otherwise,
3. try to open as path.
I think we can also inline open_i2c_dev_path() and remove it's fprintf() in the failure case. We're just speculating here that it's a path, so printing at the end that the given input could not be found as bus number, bus name, or path is sufficient.
Quoted 8 lines
tools/i2cbusses.c +0
-0int set_slave_addr (int file,int address,int force) {/* With force, let the user read from/write to the registers tools/i2cbusses.h +0
-0
tools/i2cbusses.h void free_adapters(struct i2c_adap *adapters); · line 39
Quoted 10 lines
tools/i2cbusses.h +3
-1@@ -31,7 +31,9 @@ void free_adapters(struct i2c_adap *adapters);int lookup_i2c_bus (const char *i2cbus_arg);int parse_i2c_address (const char *address_arg,int all_addrs); -int open_i2c_dev (int i2cbus,char *filename,size_t size,int quiet); +int open_i2c_dev (char *i2cbus_arg,char **filename,size_t size,int quiet); +int open_i2c_dev_num (int i2cbus,char *filename,size_t size,int quiet); +int open_i2c_dev_path (const char *i2cbus_arg,int quiet);int set_slave_addr (int file,int address,int force);#define MISSING_FUNC_FMT "Error: Adapter does not have %s capability\n"
I don't think there's a need to expose _num() and _path(). In fact, you can even remove lookup_i2c_bus() as that is not used anymore by any tool.
Quoted 29 lines
tools/i2cdetect.c +3
-7@@ -213,8 +213,9 @@ static void print_i2c_busses(void)int main (int argc,char *argv[]) {char *end; -int i2cbus, file, res; -char filename[20 ]; +int file, res; +char filename_buf[20 ]; +char *filename = filename_buf;unsigned long funcs;int mode =MODE_AUTO ;int first =0x08 , last =0x77 ;@@ -277,11 +278,6 @@ int main(int argc, char *argv[])help ();exit (1 ); } - i2cbus =lookup_i2c_bus (argv[optind]); -if (i2cbus <0 ) { -help (); -exit (1 ); - }/* read address range if present */ if (argc == optind +3 && mode !=MODE_FUNC ) {
tools/i2cdetect.c int main(int argc, char *argv[]) · line 323
Quoted 8 lines
tools/i2cdetect.c +1
-1@@ -321,7 +317,7 @@ int main(int argc, char *argv[])exit (1 ); } - file =open_i2c_dev (i2cbus, filename,sizeof (filename),0 ); + file =open_i2c_dev (argv[optind], &filename,sizeof (filename_buf),0 );if (file <0 ) {exit (1 ); }
Removing this will change the error path, so a call that would previously fail in lookup_i2c_bus() may now fail somewhere else, changing error code and message. Same for all other tools.
@Jean Is that acceptable or against the targeted stability requirements of i2c-tools?
Best,
Gero
[...] the rest is tool changes similar to i2cdetect
Signature · 6 lines
sevenlab engineering GmbH <https://sevenlab.de> serious engineering. Geschäftsführer: Christian J. Pereira Amtsgericht Köln, HRB 121730 Anschrift: Hansaring 20, 50670 Köln
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Quoted 10 lines
code it's not understandable what's happening. Instead I would propose to inline
lookup_i2c_bus()and then have the logic insideopen_i2c_dev()be imparative like:1. try to convert input to i2cbus bus number, if it worked
return open_i2c_dev_by_nr()with the bus number, otherwise,
2. trylookup_i2c_bus_by_name(), if it worked we now have the bus number, so wereturn open_i2c_dev_by_nr()with it, otherwise,
3. try to open as path.
I like this approach.
Quoted 12 lines
Quoted 12 lines
tools/i2cdetect.c +0
-5- i2cbus =lookup_i2c_bus (argv[optind]); -if (i2cbus <0 ) { -help (); -exit (1 ); - }/* read address range if present */ if (argc == optind +3 && mode !=MODE_FUNC ) {@@ -321,7 +317,7 @@ int main(int argc, char *argv[])exit (1 ); }
Quoted 9 lines
tools/i2cdetect.c int main(int argc, char *argv[]) · line 323
Quoted 5 lines
tools/i2cdetect.c +1
-1- file =open_i2c_dev (i2cbus, filename,sizeof (filename),0 ); + file =open_i2c_dev (argv[optind], &filename,sizeof (filename_buf),0 );if (file <0 ) {exit (1 ); }Removing this will change the error path, so a call that would previously fail in
lookup_i2c_bus()may now fail somewhere else, changing error code and message. Same for all other tools.
You mean that help() is not printed in some cases then? The error code is 1 in all cases, or am I missing something?
Quoted 2 lines
@Jean Is that acceptable or against the targeted stability requirements of i2c-tools?
I am also curious about Jean's opinion here. In my book it is okay. If not, we could also release the new version as 5.0, I'd say. Would even match adding the DDR5 support nicely ;)
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Hi Wolfram,
Quoted 22 lines · Wolfram Sang
Quoted 12 lines
Quoted 12 lines
tools/i2cdetect.c +0
-5- i2cbus =lookup_i2c_bus (argv[optind]); -if (i2cbus <0 ) { -help (); -exit (1 ); - }/* read address range if present */ if (argc == optind +3 && mode !=MODE_FUNC ) {@@ -321,7 +317,7 @@ int main(int argc, char *argv[])exit (1 ); }Quoted 9 lines
tools/i2cdetect.c int main(int argc, char *argv[]) · line 323
Quoted 5 lines
tools/i2cdetect.c +1
-1- file =open_i2c_dev (i2cbus, filename,sizeof (filename),0 ); + file =open_i2c_dev (argv[optind], &filename,sizeof (filename_buf),0 );if (file <0 ) {exit (1 ); }Removing this will change the error path, so a call that would previously fail in
lookup_i2c_bus()may now fail somewhere else, changing error code and message. Same for all other tools.
Quoted 2 lines
You mean that
help()is not printed in some cases then? The error code is 1 in all cases, or am I missing something?
You're right, all error codes are converted to 1, so the error codes are fine. Yes, I meant that a call with the current version may have failed in the lookup_i2c_bus() call and printed for example
Error: I2C bus name is not unique!
but with the new patch the lookup is done later, so it may fail in a diffent location and print for example
Error: No address specified!
instead. (Example for i2cdump)
This would be a problem, e.g., if someone wrote a script to check if they have non-unique I2C bus names which uses i2cdump and evaluates stderr for the string and if instead it returned the no address string it assumed the check passed and all names are unique. Something like this:
i2cdetect -l | cut -f3 | sort -u | while read -r n; do
i2cdump -y "$n" 2>&1 >/dev/null | grep -qF "Error: I2C bus name is not unique!" && echo "DUP: $n"
doneThat is the most reasonable example I found which may run into trouble with the change.
Best,
Gero
Signature · 6 lines
sevenlab engineering GmbH <https://sevenlab.de> serious engineering. Geschäftsführer: Christian J. Pereira Amtsgericht Köln, HRB 121730 Anschrift: Hansaring 20, 50670 Köln
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Hi Gero,
Quoted 2 lines
they have non-unique I2C bus names which uses i2cdump and evaluates stderr for the string
I get your point. I'd argue that relying on error strings is very thin ice, as they are not an API. I think error messages are going to change with this patch set anyhow. Probably for the better, if done right. I'd suggest to continue with the series based on our feedback and see how we end up with the error reporting in a user friendly way. Maybe we can even use better return values to help scripts? We will see...
D'accord?
Happy hacking,
Wolfram
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Hi Wolfram,
Quoted 5 lines · Wolfram Sang
Hi Gero,
Quoted 2 lines
they have non-unique I2C bus names which uses i2cdump and evaluates stderr for the string
Quoted 8 lines
I get your point. I'd argue that relying on error strings is very thin ice, as they are not an API. I think error messages are going to change with this patch set anyhow. Probably for the better, if done right. I'd suggest to continue with the series based on our feedback and see how we end up with the error reporting in a user friendly way. Maybe we can even use better return values to help scripts? We will see...
D'accord?
Agreed, though I would keep the return values as is for this patch set.
Best,
Gero
Quoted 4 lines
Happy hacking,
Wolfram
Signature · 6 lines
sevenlab engineering GmbH <https://sevenlab.de> serious engineering. Geschäftsführer: Christian J. Pereira Amtsgericht Köln, HRB 121730 Anschrift: Hansaring 20, 50670 Köln
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Quoted 2 lines
Agreed, though I would keep the return values as is for this patch set.
Yes, I agree.
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
In reply to Gero SchwärickeHi Gero,
Quoted 3 lines · Gero Schwäricke
I just finished reviewing your patch. Aaand I saw Wolfram beat me to it by 20 minutes. I hope you forgive me for sending it as is even though it may now contain duplicates to his findings.
Not at all! I understand that it takes a lot of effort to carefully review changes. I appreciate your work.
Quoted 6 lines · Brigham Campbell
[...]
Quoted 4 lines
tools/i2cbusses.c +0
-0@@ -410,7 +406,7 @@ int parse_i2c_address(const char *address_arg, int all_addrs)return address; }
Quoted 8 lines
tools/i2cbusses.c int parse_i2c_address(const char *address_arg, int all_addrs) · line 412
Quoted 5 lines
tools/i2cbusses.c +1
-1-int open_i2c_dev (int i2cbus,char *filename,size_t size,int quiet) +int open_i2c_dev_num (int i2cbus,char *filename,size_t size,int quiet) {int file, len;The lookup function uses suffix
_by_name, so for consistency maybe use_by_nrinstead? That is what it's called instruct i2c_adap.
I don't have a strong opinion. Happy to change to _by_nr.
Quoted 9 lines
This is a minimally invasive change that makes this work, but it also spaghettifies the code base a little.
lookup_i2c_bus()tries to parse the input to a bus number or, if that doesn't work, tries to resolve it as a bus name. And if that fails we try to open as path. The issue is thatlookup_i2c_bus()is not very descriptive. Without reading the code it's not understandable what's happening. Instead I would propose to inlinelookup_i2c_bus()and then have the logic insideopen_i2c_dev()be imparative like:
Quoted 5 lines
1. try to convert input to i2cbus bus number, if it worked
return open_i2c_dev_by_nr()with the bus number, otherwise,
2. trylookup_i2c_bus_by_name(), if it worked we now have the bus number, so wereturn open_i2c_dev_by_nr()with it, otherwise,
3. try to open as path.
You make some really good points. I'll make these changes in v5.
Quoted 4 lines
I think we can also inline
open_i2c_dev_path()and remove it'sfprintf()in the failure case. We're just speculating here that it's a path, so printing at the end that the given input could not be found as bus number, bus name, or path is sufficient.
I agree that open_i2c_dev_path() should be inlined, but I don't think it would be a good idea to remove its fprintf() altogether. How about something like the following, which would make the error messages more orthogonal?
if (errno != ENOENT) {
fprintf(stderr, "Error: Could not open file "
"`%s': %s\n", i2cbus_arg, strerror(errno));
if (errno == EACCES)
fprintf(stderr, "Run as root?\n");
return file;
}
fprintf(stderr, "Error: `%s' is not a bus number, name, or device file "
"path!\n", i2cbus_arg);If the i2cbus_arg parameter didn't appear to be a file (ENOENT), it will print an error indicating that all three methods failed. If it did appear to be a file but couldn't open the file for whatever reason, it will print the error along with a suggestion to run as root if it's a permissions issue. This behavior reflects the behavior of open_i2c_dev_by_nr().
Quoted 3 lines
I don't think there's a need to expose
_num()and_path(). In fact, you can even removelookup_i2c_bus()as that is not used anymore by any tool.
I'll clean up the header and remove unnecessary header exports in v5.
Signature · 2 lines
Brigham Campbell https://brighamcampbell.com
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Hi Brigham,
Quoted 16 lines · Brigham Campbell
I agree that
open_i2c_dev_path()should be inlined, but I don't think it would be a good idea to remove itsfprintf()altogether. How about something like the following, which would make the error messages more orthogonal?if (errno != ENOENT) { fprintf(stderr, "Error: Could not open file " "`%s': %s\n", i2cbus_arg, strerror(errno)); if (errno == EACCES) fprintf(stderr, "Run as root?\n"); return file; } fprintf(stderr, "Error: `%s' is not a bus number, name, or device file " "path!\n", i2cbus_arg);
Quoted 6 lines
If the
i2cbus_argparameter didn't appear to be a file (ENOENT), it will print an error indicating that all three methods failed. If it did appear to be a file but couldn't open the file for whatever reason, it will print the error along with a suggestion to run as root if it's a permissions issue. This behavior reflects the behavior ofopen_i2c_dev_by_nr().
I'm unsure about this: Yes, this reflects the behavior of open_i2c_dev_by_nr(), but we only call that after we have validated that the input is indeed an adapter number. With the path we don't do that, we just try to open the input as a path. We know it's not a valid integer, and not a valid adapter name, but it may still not be a path, maybe it's a mistyped adapter name.
To that I'm not sure we can conclude that errno != ENOENT means it is indeed a path.
Looking at libgpiod (gpiod_chip_open_lookup()), they solved that by assuming that all paths must start with /dev/. Unsure if we want to go that route as well. It would definitely simplify things and I think for the sake of progress that whould be fine. Thoughts?
Best,
Gero
Signature · 6 lines
sevenlab engineering GmbH <https://sevenlab.de> serious engineering. Geschäftsführer: Christian J. Pereira Amtsgericht Köln, HRB 121730 Anschrift: Hansaring 20, 50670 Köln
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Hi Gero,
Quoted 9 lines · Gero Schwäricke
Quoted 8 lines · Brigham Campbell
if (errno != ENOENT) { fprintf(stderr, "Error: Could not open file " "`%s': %s\n", i2cbus_arg, strerror(errno)); if (errno == EACCES) fprintf(stderr, "Run as root?\n"); return file; }
Quoted 6 lines
Quoted 2 lines
fprintf(stderr, "Error: `%s' is not a bus number, name, or device file " "path!\n", i2cbus_arg);I'm unsure about this: Yes, this reflects the behavior of
open_i2c_dev_by_nr(), but we only call that after we have validated that the input is indeed an adapter number.
The input is not necessarily an adapter number. We call open_i2c_dev_by_nr after parsing the argument as an unsigned long, but that's no guarantee that the input is an adapter number. For example, if a system has /dev/i2c-0 and /dev/i2c-1, i2cdetect -F 100 will dutifully try to open /dev/i2c-100 and eventually fail without trying to interpret the number as a bus name. That's ok because it's just a heuristic and we don't expect the collision of file paths, bus names, and unsigned integers.
Quoted 7 lines
With the path we don't do
that, we just try to open the input as a path. We know it's not a valid integer, and not a valid adapter name, but it may still not be a path, maybe it's a mistyped adapter name.To that I'm not sure we can conclude that
errno != ENOENTmeans it is indeed a path.
This is a good point. errno != ENOENT doesn't necessarily mean that the parameter is a file and that i2c-tools should be able to open it.
Quoted 4 lines
Looking at libgpiod (
gpiod_chip_open_lookup()), they solved that by assuming that all paths must start with/dev/. Unsure if we want to go that route as well. It would definitely simplify things and I think for the sake of progress that whould be fine. Thoughts?
The approach you suggest assumes that when the user passes in a device file path, the path is _not_ a relative path, devtmpfs is mounted at /dev/, and if the path is a symlink, it's also in /dev/. Maybe these assumptions are ok (after all, libgpiod makes them) but they seem unnecessary.
What if we only indicate that the argument couldn't be opened as a file specifically when open() returns EACCES? Otherwise, we'll print a generic message, indicating that the argument couldn't be parsed as a bus number, name, or device file path. Even when i2c-tools should have been able to open the argument as a path, but couldn't because of some unrelated error, the binary will print a less-specific error message which is still applicable and correct:
file = open(i2cbus_arg, O_RDWR);
if (file >= 0 || quiet) return file;
if (errno == EACCES) {
fprintf(stderr, "Error: Could not open file `%s': %s\n"
"Run as root?\n", i2cbus_arg, strerror(errno));
return file;
}
fprintf(stderr, "Error: Couldn't interpret `%s' as a bus number, "
"name, or device file path!\n", i2cbus_arg);
return file;Sorry to double down on the bikeshedding (again, I do really appreciate the feedback)... I'm interested to hear what Wolfram has to say. I may send out v5 (which includes the above code) along with an invitation to either make editorial changes to the patch in case the maintainer has something different in mind or request that I send out a v6. I have v5 queued up which includes the above snippet.
Signature · 2 lines
Brigham Campbell https://brighamcampbell.com
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
In reply to Brigham CampbellHi Brigham,
Quoted 25 lines · Brigham Campbell
tools/i2cbusses.c +21
-0@@ -446,6 +442,38 @@ int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet)return file; } +int open_i2c_dev_path (const char *i2cbus_arg,int quiet) +{ +int file =open (i2cbus_arg,O_RDWR ); + +if (file <0 && !quiet) { +fprintf (stderr,"Error: Could not open file " +"`%s': %s\n" , i2cbus_arg,strerror (errno)); +if (errno ==EACCES ) +fprintf (stderr,"Run as root?\n" ); + } + +return file; +} + +int open_i2c_dev (char *i2cbus_arg,char **filename,size_t size,int quiet) +{ +int file, i2cbus; + + i2cbus =lookup_i2c_bus (i2cbus_arg); + +if (i2cbus <0 ) {
tools/i2cbusses.c int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet) · line 475
Quoted 10 lines
tools/i2cbusses.c +10
-0+ *filename = i2cbus_arg; + file =open_i2c_dev_path (i2cbus_arg, quiet); +if (file <0 ) +fprintf (stderr,"Failed to open `%s' as a bus name " +"and as a path.\n" , i2cbus_arg); +return file; + } + +return open_i2c_dev_num (i2cbus, *filename, size, quiet); +}
It just occurred to me that the zero-copy pointer shuffling may be
overkill: I think using char *filename, instead of char **filename, and copying i2cbus_arg in case it is a path reduces complexity at a reasonably small cost of copying 20 bytes in the worst case. But it's a matter of oppinion I assume.
Best,
Gero
Signature · 6 lines
sevenlab engineering GmbH <https://sevenlab.de> serious engineering. Geschäftsführer: Christian J. Pereira Amtsgericht Köln, HRB 121730 Anschrift: Hansaring 20, 50670 Köln
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Quoted 34 lines · Gero Schwäricke
Quoted 33 lines · Brigham Campbell
tools/i2cbusses.c +29
-0@@ -446,6 +442,38 @@ int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet)return file; } +int open_i2c_dev_path (const char *i2cbus_arg,int quiet) +{ +int file =open (i2cbus_arg,O_RDWR ); + +if (file <0 && !quiet) { +fprintf (stderr,"Error: Could not open file " +"`%s': %s\n" , i2cbus_arg,strerror (errno)); +if (errno ==EACCES ) +fprintf (stderr,"Run as root?\n" ); + } + +return file; +} + +int open_i2c_dev (char *i2cbus_arg,char **filename,size_t size,int quiet) +{ +int file, i2cbus; + + i2cbus =lookup_i2c_bus (i2cbus_arg); + +if (i2cbus <0 ) { + *filename = i2cbus_arg; + file =open_i2c_dev_path (i2cbus_arg, quiet); +if (file <0 ) +fprintf (stderr,"Failed to open `%s' as a bus name " +"and as a path.\n" , i2cbus_arg); +return file; + } +
Quoted 8 lines
tools/i2cbusses.c int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet) · line 475
Quoted 2 lines
tools/i2cbusses.c +2
-0+return open_i2c_dev_num (i2cbus, *filename, size, quiet); +}It just occurred to me that the zero-copy pointer shuffling may be
overkill: I think usingchar *filename, instead ofchar **filename, and copyingi2cbus_argin case it is a path reduces complexity at a reasonably small cost of copying 20 bytes in the worst case. But it's a matter of oppinion I assume.
I was not interested in avoiding the cost of copying 20 bytes. I expect the biggest performance overhead would be the actual i2c transaction itself, followed by enumeration of sysfs in gather_i2c_busses. I'm not at all worried about a single strcpy.
I was actually interested in accounting for the fact that because this patch allows users to pass in symlinks, users may very well pass in paths which are longer than 20 characters. In this case, the filename buffer isn't big enough. Rather than allocate a new buffer and copy the string over, I simply copied the pointer to the path in argv.
Maybe it would be easier to understand an approach which simply increases the size of the filename buffer to PATH_MAX from limits.h and just copies the path directly into the buffer via strcpy? I had considered this approach. In this case, I might consider moving the (much bigger) buffer from the stack to the heap so as not to overrun the stack frame...
Thanks for your thorough reviewal of the code! I appreciate the feedback I've received from you and Gero. I'll try to respond to some more of the feedback tonight.
Signature · 2 lines
Brigham Campbell https://brighamcampbell.com
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Hi Brigham,
Quoted 10 lines · Brigham Campbell
I was not interested in avoiding the cost of copying 20 bytes. I expect the biggest performance overhead would be the actual i2c transaction itself, followed by enumeration of sysfs in
gather_i2c_busses. I'm not at all worried about a single strcpy.I was actually interested in accounting for the fact that because this patch allows users to pass in symlinks, users may very well pass in paths which are longer than 20 characters. In this case, the filename buffer isn't big enough. Rather than allocate a new buffer and copy the string over, I simply copied the pointer to the path in argv.
Very good point, I missed that intent.
Quoted 6 lines
Maybe it would be easier to understand an approach which simply increases the size of the filename buffer to
PATH_MAXfromlimits.hand just copies the path directly into the buffer via strcpy? I had considered this approach. In this case, I might consider moving the (much bigger) buffer from the stack to the heap so as not to overrun the stack frame...
hm, simpler to understand indeed, but 4k is a significant allocation for smaller targets. I'm a bit torn. In the end both are valid options, but I think your implementation is fine as is. Maybe just add a comment why we assign the pointer instead of copying into the buffer, so the intent is made clear.
Best,
Gero
Quoted 3 lines
Thanks for your thorough reviewal of the code! I appreciate the feedback I've received from you and Gero. I'll try to respond to some more of the feedback tonight.
Signature · 6 lines
sevenlab engineering GmbH <https://sevenlab.de> serious engineering. Geschäftsführer: Christian J. Pereira Amtsgericht Köln, HRB 121730 Anschrift: Hansaring 20, 50670 Köln
Re: [PATCH v4 1/2] i2c-tools: Allow passing device file paths
Hi Gero,
Quoted 8 lines · Gero Schwäricke
Quoted 6 lines · Brigham Campbell
Maybe it would be easier to understand an approach which simply increases the size of the filename buffer to
PATH_MAXfromlimits.hand just copies the path directly into the buffer via strcpy? I had considered this approach. In this case, I might consider moving the (much bigger) buffer from the stack to the heap so as not to overrun the stack frame...
Quoted 5 lines
hm, simpler to understand indeed, but 4k is a significant allocation for smaller targets. I'm a bit torn. In the end both are valid options, but I think your implementation is fine as is. Maybe just add a comment why we assign the pointer instead of copying into the buffer, so the intent is made clear.
I'll keep it as-is and add a comment for the next revision, then.
Quoted 2 lines
Quoted 2 lines
I appreciate the feedback
I've received from you and Gero.
Woops, I meant to say you and Wolfram. I went backpacking last weekend. Clearly, the Utah heat wave fried my brain!
Signature · 2 lines
Brigham Campbell https://brighamcampbell.com
PATCH v4 2/2 i2c-tools: Document device paths as I2CBUS arg
In reply to Brigham Campbelli2c-tools now accept device file paths in addition to the i2c adapter number and i2c adapter name. Add this capability to documentation.
--- tools/i2cdetect.8 | 8 ++++---- tools/i2cdetect.c | 2 +- tools/i2cdump.8 | 7 ++++--- tools/i2cdump.c | 2 +- tools/i2cget.8 | 9 +++++---- tools/i2cget.c | 2 +- tools/i2cset.8 | 12 ++++++------ tools/i2cset.c | 2 +- tools/i2ctransfer.8 | 2 +- tools/i2ctransfer.c | 2 +- 10 files changed, 25 insertions(+), 23 deletions(-)
tools/i2cdetect.8 +4 -4
@@ -26,10 +26,10 @@ i2cdetect \- detect I2C chips .SH DESCRIPTION i2cdetect is a userspace program to scan an I2C bus for devices. It outputs a table with the list of detected devices on the specified bus. -\fIi2cbus\fR indicates the number or name of the I2C bus to be scanned, and -should correspond to one of the busses listed by \fIi2cdetect -l\fR. -The optional parameters \fIfirst\fR and \fIlast\fR restrict the scanning -range (default: from 0x08 to 0x77). +\fIi2cbus\fR indicates the number, name, or device file path of the I2C +bus to be scanned, and should correspond to one of the busses listed by +\fIi2cdetect -l\fR. The optional parameters \fIfirst\fR and \fIlast\fR +restrict the scanning range (default: from 0x08 to 0x77). .PP As there is no standard I2C detection command, i2cdetect uses arbitrary SMBus commands (namely SMBus quick write and SMBus receive byte) to probe
tools/i2cdetect.c +1 -1
@@ -38,7 +38,7 @@ static void help(void)"Usage: i2cdetect [-y] [-a] [-q|-r] I2CBUS [FIRST LAST]\n" " i2cdetect -F I2CBUS\n" " i2cdetect -l\n" -" I2CBUS is an integer or an I2C bus name\n" +" I2CBUS is an integer, I2C bus name, or I2C bus device path\n" " If provided, FIRST and LAST limit the probing range.\n"); }
tools/i2cdump.8 +4 -3
@@ -52,9 +52,10 @@ scripts. Allow using addresses between 0x00 - 0x07 and 0x78 - 0x7f. Not recommended. .PP At least two options must be provided to i2cdump. \fIi2cbus\fR indicates the -number or name of the I2C bus to be scanned. This number should correspond to one -of the busses listed by \fIi2cdetect -l\fR. \fIaddress\fR indicates the -address to be scanned on that bus, and is an integer between 0x08 and 0x77. +number, name, or device file path of the I2C bus to be scanned. This number +should correspond to one of the busses listed by \fIi2cdetect -l\fR. +\fIaddress\fR indicates the address to be scanned on that bus, and is an integer +between 0x08 and 0x77. .PP The \fImode\fR parameter, if specified, is one of the letters \fBb\fP, \fBw\fP, or \fBi\fP, corresponding to a read size of a single byte, a 16-bit
tools/i2cdump.c +1 -1
@@ -32,7 +32,7 @@ static void help(void) {fprintf (stderr,"Usage: i2cdump [-f] [-y] [-r first-last] [-a] I2CBUS ADDRESS [MODE [BANK [BANKREG]]]\n" -" I2CBUS is an integer or an I2C bus name\n" +" I2CBUS is an integer, I2C bus name, or I2C bus device path\n" " ADDRESS is an integer (0x08 - 0x77, or 0x00 - 0x7f if -a is given)\n" " MODE is one of:\n" " b (byte, default)\n"
tools/i2cget.8 +5 -4
@@ -45,10 +45,11 @@ scripts. Use with caution. .B -a Allow using addresses between 0x00 - 0x07 and 0x78 - 0x7f. Not recommended. .PP -There are two required options to i2cget. \fIi2cbus\fR indicates the number -or name of the I2C bus to be scanned. This number should correspond to one of -the busses listed by \fIi2cdetect -l\fR. \fIchip-address\fR specifies the -address of the chip on that bus, and is an integer between 0x08 and 0x77. +There are two required options to i2cget. \fIi2cbus\fR indicates the number, +name, or device file path of the I2C bus to be scanned. This number should +correspond to one of the busses listed by \fIi2cdetect -l\fR. +\fIchip-address\fR specifies the address of the chip on that bus, and is an +integer between 0x08 and 0x77. .PP \fIdata-address\fR specifies the address on that chip to read from, and is an integer between 0x00 and 0xFF. If omitted, the currently active register
tools/i2cget.c +1 -1
@@ -35,7 +35,7 @@ static void __attribute__ ((noreturn)) help(int status) {fprintf (stderr,"Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE [LENGTH]]]\n" -" I2CBUS is an integer or an I2C bus name\n" +" I2CBUS is an integer, I2C bus name, or I2C bus device path\n" " ADDRESS is an integer (0x08 - 0x77, or 0x00 - 0x7f if -a is given)\n" " MODE is one of:\n" " b (read byte data, default)\n"
tools/i2cset.8 +6 -6
@@ -65,12 +65,12 @@ apply as those of option \fB-m\fR. .B -a Allow using addresses between 0x00 - 0x07 and 0x78 - 0x7f. Not recommended. .PP -There are three required options to i2cset. \fIi2cbus\fR indicates the number -or name of the I2C bus to be scanned. This number should correspond to one of -the busses listed by \fIi2cdetect -l\fR. \fIchip-address\fR specifies the -address of the chip on that bus, and is an integer between 0x08 and 0x77. -\fIdata-address\fR specifies the address on that chip to write to, and is an -integer between 0x00 and 0xFF. +There are three required options to i2cset. \fIi2cbus\fR indicates the number, +name, or device file path of the I2C bus to be scanned. This number should +correspond to one of the busses listed by \fIi2cdetect -l\fR. \fIchip-address\fR +specifies the address of the chip on that bus, and is an integer between 0x08 +and 0x77. \fIdata-address\fR specifies the address on that chip to write to, and +is an integer between 0x00 and 0xFF. .PP The \fIvalue\fR parameter, if specified, is the value to write to that location on the chip. If this parameter is omitted, then a short write is
tools/i2cset.c +1 -1
@@ -32,7 +32,7 @@ static void __attribute__ ((noreturn)) help(int status) {fprintf (stderr,"Usage: i2cset [-f] [-y] [-m MASK] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]\n" -" I2CBUS is an integer or an I2C bus name\n" +" I2CBUS is an integer, I2C bus name, or I2C bus device path\n" " ADDRESS is an integer (0x08 - 0x77, or 0x00 - 0x7f if -a is given)\n" " MODE is one of:\n" " c (byte, no value)\n"
tools/i2ctransfer.8 +1 -1
@@ -84,7 +84,7 @@ This is mainly meant to be used in scripts. .PP The first parameter .I i2cbus -indicates the number or name of the I2C bus to be used. +indicates the number, name, or device file path of the I2C bus to be used. This number should correspond to one of the busses listed by .B i2cdetect -l.
tools/i2ctransfer.c +1 -1
@@ -59,7 +59,7 @@ static void help(void)" -v verbose mode\n" " -V version info\n" " -y yes to all confirmations\n" -" I2CBUS is an integer or an I2C bus name\n" +" I2CBUS is an integer, I2C bus name, or I2C bus device path\n" " DESC describes the transfer in the form: [inpst]{r|w}LENGTH[@address]\n" " 1) optional message modifier flags, if supported\n" " i: ignore NACK from client\n"
Signature · 1 line
2.55.0
Re: [PATCH v4 2/2] i2c-tools: Document device paths as I2CBUS arg
Quoted 4 lines · Brigham Campbell
i2c-tools now accept device file paths in addition to the i2c adapter number and i2c adapter name. Add this capability to documentation.
Signed-off-by Brigham Campbell <[email protected]>
Looks okay to me. Thank you!