From 0709b19497d3a45b8284934701c1c542d372c84b Mon Sep 17 00:00:00 2001 From: "Mark D. Studebaker" Date: Sat, 31 Mar 2001 19:00:45 +0000 Subject: [PATCH] add devfs /dev/i2c/x support git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@1053 7894878c-1315-0410-8ee3-d5d059ff63e0 --- CHANGES | 1 + prog/detect/i2cdetect.c | 55 +++++++++++++++++++++++++++-------------- prog/dump/i2cdump.c | 54 ++++++++++++++++++++++++++-------------- 3 files changed, 74 insertions(+), 36 deletions(-) diff --git a/CHANGES b/CHANGES index 46df16c7..4c3b6099 100644 --- a/CHANGES +++ b/CHANGES @@ -44,6 +44,7 @@ ask CVS about it: Module via686a: Allow force=9191,address (for A7V/K7V boards) Module w83781d: Don't reinitialize as99127f chip; this may cause fan/temp reading changes + Programs i2cdetect, i2cdump: add devfs /dev/i2c/x support Program mkpatch.pl: Fix adm9240 typos, add more chips to Configure.help, remove ltc1710 Program sens_update_rrd: new diff --git a/prog/detect/i2cdetect.c b/prog/detect/i2cdetect.c index 0a8af6f4..242c4143 100644 --- a/prog/detect/i2cdetect.c +++ b/prog/detect/i2cdetect.c @@ -1,7 +1,8 @@ /* i2cdetect.c - Part of i2cdetect, a user-space program to scan for I2C devices. - Copyright (c) 1999 Frodo Looijaard + Copyright (c) 1999-2001 Frodo Looijaard and + Mark D. Studebaker This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -45,9 +46,10 @@ int main(int argc, char *argv[]) { char *end; int i,j,res,i2cbus,file; - int e1, e2; + int e1, e2, e3; char filename1[20]; char filename2[20]; + char filename3[20]; char *filename; long funcs; @@ -69,29 +71,46 @@ int main(int argc, char *argv[]) help(); } +/* + * Try all three variants and give the correct error message + * upon failure + */ + sprintf(filename1,"/dev/i2c-%d",i2cbus); sprintf(filename2,"/dev/i2c%d",i2cbus); + sprintf(filename3,"/dev/i2c/%d",i2cbus); if ((file = open(filename1,O_RDWR)) < 0) { e1 = errno; if ((file = open(filename2,O_RDWR)) < 0) { e2 = errno; - if(e1 == ENOENT && e2 == ENOENT) { - fprintf(stderr,"Error: Could not open file `%s' or `%s': %s\n", - filename1,filename2,strerror(ENOENT)); + if ((file = open(filename3,O_RDWR)) < 0) { + e3 = errno; + if(e1 == ENOENT && e2 == ENOENT && e3 == ENOENT) { + fprintf(stderr,"Error: Could not open file `%s', `%s', or `%s': %s\n", + filename1,filename2,filename3,strerror(ENOENT)); + } + if (e1 != ENOENT) { + fprintf(stderr,"Error: Could not open file `%s' : %s\n", + filename1,strerror(e1)); + if(e1 == EACCES) + fprintf(stderr,"Run as root?\n"); + } + if (e2 != ENOENT) { + fprintf(stderr,"Error: Could not open file `%s' : %s\n", + filename2,strerror(e2)); + if(e2 == EACCES) + fprintf(stderr,"Run as root?\n"); + } + if (e3 != ENOENT) { + fprintf(stderr,"Error: Could not open file `%s' : %s\n", + filename3,strerror(e3)); + if(e3 == EACCES) + fprintf(stderr,"Run as root?\n"); + } + exit(1); + } else { + filename = filename3; } - if (e1 != ENOENT) { - fprintf(stderr,"Error: Could not open file `%s' : %s\n", - filename1,strerror(e1)); - if(e1 == EACCES) - fprintf(stderr,"Run as root?\n"); - } - if (e2 != ENOENT) { - fprintf(stderr,"Error: Could not open file `%s' : %s\n", - filename2,strerror(e2)); - if(e2 == EACCES) - fprintf(stderr,"Run as root?\n"); - } - exit(1); } else { filename = filename2; } diff --git a/prog/dump/i2cdump.c b/prog/dump/i2cdump.c index dc50d27a..838fce99 100644 --- a/prog/dump/i2cdump.c +++ b/prog/dump/i2cdump.c @@ -46,10 +46,11 @@ int main(int argc, char *argv[]) { char *end; int i,j,res,i2cbus,address,size,file; - int e1, e2; + int e1, e2, e3; int bank = 0, bankreg = 0x4E; char filename1[20]; char filename2[20]; + char filename3[20]; char *filename; long funcs; unsigned char cblock[256]; @@ -133,31 +134,48 @@ int main(int argc, char *argv[]) } } +/* + * Try all three variants and give the correct error message + * upon failure + */ + sprintf(filename1,"/dev/i2c-%d",i2cbus); sprintf(filename2,"/dev/i2c%d",i2cbus); + sprintf(filename3,"/dev/i2c/%d",i2cbus); if ((file = open(filename1,O_RDWR)) < 0) { e1 = errno; if ((file = open(filename2,O_RDWR)) < 0) { e2 = errno; - if(e1 == ENOENT && e2 == ENOENT) { - fprintf(stderr,"Error: Could not open file `%s' or `%s': %s\n", - filename1,filename2,strerror(ENOENT)); + if ((file = open(filename3,O_RDWR)) < 0) { + e3 = errno; + if(e1 == ENOENT && e2 == ENOENT && e3 == ENOENT) { + fprintf(stderr,"Error: Could not open file `%s', `%s', or `%s': %s\n", + filename1,filename2,filename3,strerror(ENOENT)); + } + if (e1 != ENOENT) { + fprintf(stderr,"Error: Could not open file `%s' : %s\n", + filename1,strerror(e1)); + if(e1 == EACCES) + fprintf(stderr,"Run as root?\n"); + } + if (e2 != ENOENT) { + fprintf(stderr,"Error: Could not open file `%s' : %s\n", + filename2,strerror(e2)); + if(e2 == EACCES) + fprintf(stderr,"Run as root?\n"); + } + if (e3 != ENOENT) { + fprintf(stderr,"Error: Could not open file `%s' : %s\n", + filename3,strerror(e3)); + if(e3 == EACCES) + fprintf(stderr,"Run as root?\n"); + } + exit(1); + } else { + filename = filename3; } - if (e1 != ENOENT) { - fprintf(stderr,"Error: Could not open file `%s' : %s\n", - filename1,strerror(e1)); - if(e1 == EACCES) - fprintf(stderr,"Run as root?\n"); - } - if (e2 != ENOENT) { - fprintf(stderr,"Error: Could not open file `%s' : %s\n", - filename2,strerror(e2)); - if(e2 == EACCES) - fprintf(stderr,"Run as root?\n"); - } - exit(1); } else { - filename = filename2; + filename = filename2; } } else { filename = filename1;