Changeset 5195

Show
Ignore:
Timestamp:
04/20/08 19:32:12 (6 years ago)
Author:
khali
Message:

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.

Location:
i2c-tools/trunk/tools
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • i2c-tools/trunk/tools/i2cbusses.c

    r5194 r5195  
    341341} 
    342342 
     343static int lookup_i2c_bus_by_name(const char *bus_name) 
     344{ 
     345        struct i2c_adap *adapters; 
     346        int i, i2cbus = -1; 
     347 
     348        adapters = gather_i2c_busses(); 
     349        if (adapters == NULL) { 
     350                fprintf(stderr, "Error: Out of memory!\n"); 
     351                return -3; 
     352        } 
     353 
     354        /* Walk the list of i2c busses, looking for the one with the 
     355           right name */ 
     356        for (i = 0; adapters[i].name; i++) { 
     357                if (strcmp(adapters[i].name, bus_name) == 0) { 
     358                        if (i2cbus >= 0) { 
     359                                fprintf(stderr, 
     360                                        "Error: I2C bus name is not unique!\n"); 
     361                                i2cbus = -4; 
     362                                goto done; 
     363                        } 
     364                        i2cbus = adapters[i].nr; 
     365                } 
     366        } 
     367 
     368        if (i2cbus == -1) 
     369                fprintf(stderr, "Error: I2CBUS argument doesn't match any " 
     370                        "I2C bus name\n"); 
     371 
     372done: 
     373        free_adapters(adapters); 
     374        return i2cbus; 
     375} 
     376 
    343377/* 
    344378 * Parse an I2CBUS command line argument and return the corresponding 
     
    352386        i2cbus = strtol(i2cbus_arg, &end, 0); 
    353387        if (*end || !*i2cbus_arg) { 
    354                 fprintf(stderr, "Error: I2CBUS argument not a number!\n"); 
    355                 return -1; 
     388                /* Not a number, maybe a name? */ 
     389                return lookup_i2c_bus_by_name(i2cbus_arg); 
    356390        } 
    357391        if (i2cbus < 0 || i2cbus > 0xff) { 
  • i2c-tools/trunk/tools/i2cdetect.8

    r4488 r5195  
    1 .TH I2CDETECT 8 "June 2007" 
     1.TH I2CDETECT 8 "April 2008" 
    22.SH NAME 
    33i2cdetect \- detect I2C chips 
     
    2424i2cdetect is a userspace program to scan an I2C bus for devices. It 
    2525outputs a table with the list of detected devices on the specified bus. 
    26 \fIi2cbus\fR indicates the number of the I2C bus to be scanned, and 
     26\fIi2cbus\fR indicates the number or name of the I2C bus to be scanned, and 
    2727should correspond to one of the busses listed by \fIi2cdetect -l\fR. 
    2828The optional parameters \fIfirst\fR and \fIlast\fR restrict the scanning 
  • i2c-tools/trunk/tools/i2cdump.8

    r5151 r5195  
    1 .TH I2CDUMP 8 "March 2008" 
     1.TH I2CDUMP 8 "April 2008" 
    22.SH NAME 
    33i2cdump \- examine I2C registers 
     
    4444.PP 
    4545At least two options must be provided to i2cdump. \fIi2cbus\fR indicates the 
    46 number of the I2C bus to be scanned. This number should correspond to one 
     46number or name of the I2C bus to be scanned. This number should correspond to one 
    4747of the busses listed by \fIi2cdetect -l\fR. \fIaddress\fR indicates the 
    4848address to be scanned on that bus, and is an integer between 0x00 and 0x7F. 
  • i2c-tools/trunk/tools/i2cget.8

    r4488 r5195  
    1 .TH I2CGET 8 "June 2007" 
     1.TH I2CGET 8 "April 2008" 
    22.SH "NAME" 
    33i2cget \- read from I2C/SMBus chip registers 
     
    3737.PP 
    3838There are two required options to i2cget. \fIi2cbus\fR indicates the number 
    39 of the I2C bus to be scanned.  This number should correspond to one of 
     39or name of the I2C bus to be scanned.  This number should correspond to one of 
    4040the busses listed by \fIi2cdetect -l\fR. \fIchip-address\fR specifies the 
    4141address of the chip on that bus, and is an integer between 0x03 and 0x77. 
  • i2c-tools/trunk/tools/i2cset.8

    r4488 r5195  
    1 .TH I2CSET 8 "June 2007" 
     1.TH I2CSET 8 "April 2008" 
    22.SH "NAME" 
    33i2cset \- set I2C registers 
     
    4141.PP 
    4242There are four required options to i2cset. \fIi2cbus\fR indicates the number 
    43 of the I2C bus to be scanned.  This number should correspond to one of 
     43or name of the I2C bus to be scanned.  This number should correspond to one of 
    4444the busses listed by \fIi2cdetect -l\fR. \fIchip-address\fR specifies the 
    4545address of the chip on that bus, and is an integer between 0x00 and 0x7F.