Changeset 5239

Show
Ignore:
Timestamp:
05/05/08 15:02:34 (4 months ago)
Author:
khali
Message:

Split the functionality checking code into a separate function for
clarity.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • i2c-tools/trunk/CHANGES

    r5237 r5239  
    99           Shorten the usage message 
    1010           Restrict the chip address to 0x03-0x77 
     11           Split the functionality checking code into a separate function 
    1112  i2cget: Support i2c bus passed by name 
    1213          Shorten the usage message 
  • i2c-tools/trunk/tools/i2cdump.c

    r5236 r5239  
    4747} 
    4848 
     49static int check_funcs(int file, int i2cbus, int size, int pec) 
     50{ 
     51        unsigned long funcs; 
     52 
     53        /* check adapter functionality */ 
     54        if (ioctl(file, I2C_FUNCS, &funcs) < 0) { 
     55                fprintf(stderr, "Error: Could not get the adapter " 
     56                        "functionality matrix: %s\n", strerror(errno)); 
     57                return -1; 
     58        } 
     59 
     60        switch(size) { 
     61        case I2C_SMBUS_BYTE: 
     62                if (!((funcs & I2C_FUNC_SMBUS_BYTE) == I2C_FUNC_SMBUS_BYTE)) { 
     63                        fprintf(stderr, "Error: Adapter for i2c bus %d does " 
     64                                "not have byte capability\n", i2cbus); 
     65                        return -1; 
     66                } 
     67                break; 
     68 
     69        case I2C_SMBUS_BYTE_DATA: 
     70                if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) { 
     71                        fprintf(stderr, "Error: Adapter for i2c bus %d does " 
     72                                "not have byte read capability\n", i2cbus); 
     73                        return -1; 
     74                } 
     75                break; 
     76 
     77        case I2C_SMBUS_WORD_DATA: 
     78                if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) { 
     79                        fprintf(stderr, "Error: Adapter for i2c bus %d does " 
     80                                "not have word read capability\n", i2cbus); 
     81                        return -1; 
     82                } 
     83                break; 
     84 
     85        case I2C_SMBUS_BLOCK_DATA: 
     86                if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA)) { 
     87                        fprintf(stderr, "Error: Adapter for i2c bus %d does " 
     88                                "not have smbus block read capability\n", 
     89                                i2cbus); 
     90                        return -1; 
     91                } 
     92                break; 
     93 
     94        case I2C_SMBUS_I2C_BLOCK_DATA: 
     95                if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 
     96                        fprintf(stderr, "Error: Adapter for i2c bus %d does " 
     97                                "not have i2c block read capability\n", 
     98                                i2cbus); 
     99                        return -1; 
     100                } 
     101                break; 
     102        } 
     103 
     104        if (pec 
     105         && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) { 
     106                fprintf(stderr, "Warning: Adapter for i2c bus %d does " 
     107                        "not seem to support PEC\n", i2cbus); 
     108        } 
     109 
     110        return 0; 
     111} 
     112 
    49113int main(int argc, char *argv[]) 
    50114{ 
     
    53117        int bank = 0, bankreg = 0x4E, old_bank = 0; 
    54118        char filename[20]; 
    55         unsigned long funcs; 
    56119        int block[256], s_length = 0; 
    57120        int pec = 0, even = 0; 
     
    201264 
    202265        file = open_i2c_dev(i2cbus, filename, 0); 
    203         if (file < 0) { 
    204                 exit(1); 
    205         } 
    206  
    207         /* check adapter functionality */ 
    208         if (ioctl(file, I2C_FUNCS, &funcs) < 0) { 
    209                 fprintf(stderr, "Error: Could not get the adapter " 
    210                         "functionality matrix: %s\n", strerror(errno)); 
    211                 exit(1); 
    212         } 
    213  
    214         switch(size) { 
    215         case I2C_SMBUS_BYTE: 
    216                 if (!((funcs & I2C_FUNC_SMBUS_BYTE) == I2C_FUNC_SMBUS_BYTE)) { 
    217                         fprintf(stderr, "Error: Adapter for i2c bus %d does " 
    218                                 "not have byte capability\n", i2cbus); 
    219                         exit(1); 
    220                 } 
    221                 break; 
    222  
    223         case I2C_SMBUS_BYTE_DATA: 
    224                 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) { 
    225                         fprintf(stderr, "Error: Adapter for i2c bus %d does " 
    226                                 "not have byte read capability\n", i2cbus); 
    227                         exit(1); 
    228                 } 
    229                 break; 
    230  
    231         case I2C_SMBUS_WORD_DATA: 
    232                 if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) { 
    233                         fprintf(stderr, "Error: Adapter for i2c bus %d does " 
    234                                 "not have word read capability\n", i2cbus); 
    235                         exit(1); 
    236                 } 
    237                 break; 
    238  
    239         case I2C_SMBUS_BLOCK_DATA: 
    240                 if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA)) { 
    241                         fprintf(stderr, "Error: Adapter for i2c bus %d does " 
    242                                 "not have smbus block read capability\n", 
    243                                 i2cbus); 
    244                         exit(1); 
    245                 } 
    246                 break; 
    247  
    248         case I2C_SMBUS_I2C_BLOCK_DATA: 
    249                 if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 
    250                         fprintf(stderr, "Error: Adapter for i2c bus %d does " 
    251                                 "not have i2c block read capability\n", 
    252                                 i2cbus); 
    253                         exit(1); 
    254                 } 
    255                 break; 
    256         } 
    257  
    258         if (set_slave_addr(file, address, force) < 0) 
     266        if (file < 0 
     267         || check_funcs(file, i2cbus, size, pec) 
     268         || set_slave_addr(file, address, force)) 
    259269                exit(1); 
    260270 
     
    264274                                strerror(errno)); 
    265275                        exit(1); 
    266                 } 
    267                 if (!(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) { 
    268                         fprintf(stderr, "Warning: Adapter for i2c bus %d does " 
    269                                 "not seem to actually support PEC\n", i2cbus); 
    270276                } 
    271277        }