| 1 | /* |
|---|
| 2 | i2cdetect.c - Part of i2cdetect, a user-space program to scan for I2C |
|---|
| 3 | devices. |
|---|
| 4 | Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | (at your option) any later version. |
|---|
| 10 | |
|---|
| 11 | This program is distributed in the hope that it will be useful, |
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | GNU General Public License for more details. |
|---|
| 15 | |
|---|
| 16 | You should have received a copy of the GNU General Public License |
|---|
| 17 | along with this program; if not, write to the Free Software |
|---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include <errno.h> |
|---|
| 22 | #include <string.h> |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <unistd.h> |
|---|
| 26 | #include <fcntl.h> |
|---|
| 27 | #include <linux/i2c-dev.h> |
|---|
| 28 | |
|---|
| 29 | void help(void) |
|---|
| 30 | { |
|---|
| 31 | fprintf(stderr,"Syntax: i2cdetect I2CBUS\n"); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | int main(int argc, char *argv[]) |
|---|
| 35 | { |
|---|
| 36 | char *end; |
|---|
| 37 | int i,j,res,i2cbus,file; |
|---|
| 38 | char filename[20]; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | if (argc < 2) { |
|---|
| 42 | fprintf(stderr,"Error: No i2c-bus specified!\n"); |
|---|
| 43 | help(); |
|---|
| 44 | exit(1); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | i2cbus = strtol(argv[1],&end,0); |
|---|
| 48 | if (*end) { |
|---|
| 49 | fprintf(stderr,"Error: First argument not a number!\n"); |
|---|
| 50 | help(); |
|---|
| 51 | exit(1); |
|---|
| 52 | } |
|---|
| 53 | if ((i2cbus < 0) || (i2cbus > 0xff)) { |
|---|
| 54 | fprintf(stderr,"Error: I2CBUS argument out of range!\n"); |
|---|
| 55 | help(); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | sprintf(filename,"/dev/i2c-%d",i2cbus); |
|---|
| 59 | if ((file = open(filename,O_RDWR)) < 0) { |
|---|
| 60 | fprintf(stderr,"Error: Could not open file `%s': %s\n",filename, |
|---|
| 61 | strerror(errno)); |
|---|
| 62 | exit(1); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | fprintf(stderr," WARNING! This program can confuse your I2C bus, " |
|---|
| 66 | "cause data loss and worse!\n"); |
|---|
| 67 | fprintf(stderr," I will probe file %s\n", filename); |
|---|
| 68 | fprintf(stderr," You have five seconds to reconsider and press CTRL-C!\n\n"); |
|---|
| 69 | sleep(5); |
|---|
| 70 | |
|---|
| 71 | printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
|---|
| 72 | for (i = 0; i < 128; i+=16) { |
|---|
| 73 | printf("%02x: ",i); |
|---|
| 74 | for(j = 0; j < 16; j++) { |
|---|
| 75 | if (ioctl(file,I2C_SLAVE,i+j) < 0) { |
|---|
| 76 | fprintf(stderr,"Error: Could not set address to %d: %s\n",i+j, |
|---|
| 77 | strerror(errno)); |
|---|
| 78 | exit(1); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | res = i2c_smbus_read_byte(file); |
|---|
| 82 | if (res < 0) |
|---|
| 83 | printf("XX "); |
|---|
| 84 | else |
|---|
| 85 | printf("%02x ",i+j); |
|---|
| 86 | } |
|---|
| 87 | printf("\n"); |
|---|
| 88 | } |
|---|
| 89 | exit(0); |
|---|
| 90 | } |
|---|