From 31e9db10d072022e5662c3df6479ec4a011ecd71 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 20 Apr 2008 17:32:12 +0000 Subject: [PATCH] Make it possible to pass the i2c bus by name instead of by number. As there is no guarantee that i2c bus numbering will stay the same over time (for example if hardware is added of removed, or simply due to loading the drivers in a different order), passing the i2c bus by name is more robust. The i2c bus names are supposed to be unique. If you request a bus by name and this name happens to not be unique, then the tools will play it safe and quit. This is better than writing to the wrong device. git-svn-id: http://lm-sensors.org/svn/i2c-tools/trunk@5195 7894878c-1315-0410-8ee3-d5d059ff63e0 --- tools/i2cbusses.c | 38 ++++++++++++++++++++++++++++++++++++-- tools/i2cdetect.8 | 4 ++-- tools/i2cdump.8 | 4 ++-- tools/i2cget.8 | 4 ++-- tools/i2cset.8 | 4 ++-- 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/tools/i2cbusses.c b/tools/i2cbusses.c index f182df5..19000ab 100644 --- a/tools/i2cbusses.c +++ b/tools/i2cbusses.c @@ -340,6 +340,40 @@ void print_i2c_busses(int procfmt) free_adapters(adapters); } +static int lookup_i2c_bus_by_name(const char *bus_name) +{ + struct i2c_adap *adapters; + int i, i2cbus = -1; + + adapters = gather_i2c_busses(); + if (adapters == NULL) { + fprintf(stderr, "Error: Out of memory!\n"); + return -3; + } + + /* Walk the list of i2c busses, looking for the one with the + right name */ + for (i = 0; adapters[i].name; i++) { + if (strcmp(adapters[i].name, bus_name) == 0) { + if (i2cbus >= 0) { + fprintf(stderr, + "Error: I2C bus name is not unique!\n"); + i2cbus = -4; + goto done; + } + i2cbus = adapters[i].nr; + } + } + + if (i2cbus == -1) + fprintf(stderr, "Error: I2CBUS argument doesn't match any " + "I2C bus name\n"); + +done: + free_adapters(adapters); + return i2cbus; +} + /* * Parse an I2CBUS command line argument and return the corresponding * bus number, or a negative value if the bus is invalid. @@ -351,8 +385,8 @@ int lookup_i2c_bus(const char *i2cbus_arg) i2cbus = strtol(i2cbus_arg, &end, 0); if (*end || !*i2cbus_arg) { - fprintf(stderr, "Error: I2CBUS argument not a number!\n"); - return -1; + /* Not a number, maybe a name? */ + return lookup_i2c_bus_by_name(i2cbus_arg); } if (i2cbus < 0 || i2cbus > 0xff) { fprintf(stderr, "Error: I2CBUS argument out of range " diff --git a/tools/i2cdetect.8 b/tools/i2cdetect.8 index a0f9366..47cad04 100644 --- a/tools/i2cdetect.8 +++ b/tools/i2cdetect.8 @@ -1,4 +1,4 @@ -.TH I2CDETECT 8 "June 2007" +.TH I2CDETECT 8 "April 2008" .SH NAME i2cdetect \- detect I2C chips @@ -23,7 +23,7 @@ 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 of the I2C bus to be scanned, and +\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 0x03 to 0x77). diff --git a/tools/i2cdump.8 b/tools/i2cdump.8 index bc22925..7802b29 100644 --- a/tools/i2cdump.8 +++ b/tools/i2cdump.8 @@ -1,4 +1,4 @@ -.TH I2CDUMP 8 "March 2008" +.TH I2CDUMP 8 "April 2008" .SH NAME i2cdump \- examine I2C registers @@ -43,7 +43,7 @@ will perform the operation directly. This is mainly meant to be used in scripts. .PP At least two options must be provided to i2cdump. \fIi2cbus\fR indicates the -number of the I2C bus to be scanned. This number should correspond to one +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 0x00 and 0x7F. .PP diff --git a/tools/i2cget.8 b/tools/i2cget.8 index c2b65dc..5e59dd7 100644 --- a/tools/i2cget.8 +++ b/tools/i2cget.8 @@ -1,4 +1,4 @@ -.TH I2CGET 8 "June 2007" +.TH I2CGET 8 "April 2008" .SH "NAME" i2cget \- read from I2C/SMBus chip registers @@ -36,7 +36,7 @@ will perform the operation directly. This is mainly meant to be used in scripts. Use with caution. .PP There are two required options to i2cget. \fIi2cbus\fR indicates the number -of the I2C bus to be scanned. This number should correspond to one of +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 0x03 and 0x77. .PP diff --git a/tools/i2cset.8 b/tools/i2cset.8 index e6bdf88..eb34435 100644 --- a/tools/i2cset.8 +++ b/tools/i2cset.8 @@ -1,4 +1,4 @@ -.TH I2CSET 8 "June 2007" +.TH I2CSET 8 "April 2008" .SH "NAME" i2cset \- set I2C registers @@ -40,7 +40,7 @@ will perform the operation directly. This is mainly meant to be used in scripts. .PP There are four required options to i2cset. \fIi2cbus\fR indicates the number -of the I2C bus to be scanned. This number should correspond to one of +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 0x00 and 0x7F. \fIdata-address\fR specifies the address on that chip to write to, and is an