From 4f3ded5af904e0e63b426097fdc196069649eb5c Mon Sep 17 00:00:00 2001 From: "Mark D. Studebaker" Date: Mon, 17 Nov 2003 03:04:09 +0000 Subject: [PATCH] use common function to print out available i2c busses; add sysfs support to that function git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@2065 7894878c-1315-0410-8ee3-d5d059ff63e0 --- prog/detect/Module.mk | 3 ++ prog/detect/i2cdetect.c | 22 ++++----- prog/dump/Module.mk | 13 +++-- prog/dump/i2cbusses.c | 107 ++++++++++++++++++++++++++++++++++++++++ prog/dump/i2cdump.c | 20 ++++---- prog/dump/i2cset.c | 19 ++++--- 6 files changed, 150 insertions(+), 34 deletions(-) create mode 100644 prog/dump/i2cbusses.c diff --git a/prog/detect/Module.mk b/prog/detect/Module.mk index 22eca7ad..a6939d01 100644 --- a/prog/detect/Module.mk +++ b/prog/detect/Module.mk @@ -35,6 +35,9 @@ INCLUDEFILES += $(PROGDETECTSOURCES:.c=.rd) all-prog-detect: $(PROGDETECTTARGETS) user :: all-prog-detect +$(MODULE_DIR)/i2cdetect: $(MODULE_DIR)/i2cdetect.ro prog/dump/i2cbusses.ro + $(CC) -o $@ $^ + install-prog-detect: all-prog-detect mkdir -p $(DESTDIR)$(SBINDIR) $(INSTALL) -o root -g root -m 755 $(PROGDETECTSBININSTALL) $(DESTDIR)$(SBINDIR) diff --git a/prog/detect/i2cdetect.c b/prog/detect/i2cdetect.c index f05c715d..472a4815 100644 --- a/prog/detect/i2cdetect.c +++ b/prog/detect/i2cdetect.c @@ -26,20 +26,15 @@ #include #include #include "i2c-dev.h" +#include "version.h" + +void print_i2c_busses(); void help(void) { - FILE *fptr; - char s[100]; - - fprintf(stderr,"Syntax: i2cdetect I2CBUS\n"); - fprintf(stderr," I2CBUS is an integer\n"); - if((fptr = fopen("/proc/bus/i2c", "r"))) { - fprintf(stderr," Installed I2C busses:\n"); - while(fgets(s, 100, fptr)) - fprintf(stderr, " %s", s); - fclose(fptr); - } + fprintf(stderr,"Syntax: i2cdetect I2CBUS\n"); + fprintf(stderr," I2CBUS is an integer\n"); + print_i2c_busses(); } int main(int argc, char *argv[]) @@ -60,6 +55,11 @@ int main(int argc, char *argv[]) exit(1); } + if((!strcmp(argv[1], "-v")) || (!strcmp(argv[1], "-V"))) { + fprintf(stderr,"i2cdetect version %s\n", LM_VERSION); + exit(1); + } + i2cbus = strtol(argv[1],&end,0); if (*end) { fprintf(stderr,"Error: First argument not a number!\n"); diff --git a/prog/dump/Module.mk b/prog/dump/Module.mk index c09bf24b..5bc7012a 100644 --- a/prog/dump/Module.mk +++ b/prog/dump/Module.mk @@ -24,11 +24,12 @@ PROGDUMPDIR := $(MODULE_DIR) # Regrettably, even 'simply expanded variables' will not put their currently # defined value verbatim into the command-list of rules... PROGDUMPTARGETS := $(MODULE_DIR)/isadump $(MODULE_DIR)/i2cdump \ - $(MODULE_DIR)/i2cset + $(MODULE_DIR)/i2cset $(MODULE_DIR)/isaset PROGDUMPSOURCES := $(MODULE_DIR)/isadump.c $(MODULE_DIR)/i2cdump.c \ - $(MODULE_DIR)/i2cset.c + $(MODULE_DIR)/i2cset.c $(MODULE_DIR)/isaset.c \ + $(MODULE_DIR)/i2cbusses.c PROGDUMPBININSTALL := $(MODULE_DIR)/isadump $(MODULE_DIR)/i2cdump \ - $(MODULE_DIR)/i2cset + $(MODULE_DIR)/i2cset $(MODULE_DIR)/isaset # Include all dependency files. We use '.rd' to indicate this will create # executables. @@ -37,6 +38,12 @@ INCLUDEFILES += $(PROGDUMPSOURCES:.c=.rd) all-prog-dump: $(PROGDUMPTARGETS) user :: all-prog-dump +$(MODULE_DIR)/i2cdump: $(MODULE_DIR)/i2cdump.ro $(MODULE_DIR)/i2cbusses.ro + $(CC) -o $@ $^ + +$(MODULE_DIR)/i2cset: $(MODULE_DIR)/i2cset.ro $(MODULE_DIR)/i2cbusses.ro + $(CC) -o $@ $^ + install-prog-dump: all-prog-dump mkdir -p $(DESTDIR)$(SBINDIR) $(INSTALL) -o root -g root -m 755 $(PROGDUMPBININSTALL) $(DESTDIR)$(SBINDIR) diff --git a/prog/dump/i2cbusses.c b/prog/dump/i2cbusses.c new file mode 100644 index 00000000..c381fa7f --- /dev/null +++ b/prog/dump/i2cbusses.c @@ -0,0 +1,107 @@ +/* + i2cbusses: print the installed i2c busses. + Part of user-space programs to access for I2C + devices. + Copyright (c) 1999-2003 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include +#include +#include +#include + +/* + this just prints out the installed i2c busses in a consistent format, whether + on a 2.4 kernel using /proc or a 2.6 kernel using /sys. +*/ +void print_i2c_busses() +{ + FILE *fptr; + char s[100]; + struct dirent *de; + DIR *dir; + FILE *f; + char *border; + char dev[NAME_MAX], fstype[NAME_MAX], sysfs[NAME_MAX], n[NAME_MAX]; + int foundsysfs = 0; + int tmp; + int count=0; + + + /* look in /proc/bus/i2c */ + if((fptr = fopen("/proc/bus/i2c", "r"))) { + fprintf(stderr," Installed I2C busses:\n"); + while(fgets(s, 100, fptr)) { + fprintf(stderr, " %s", s); + count++; + } + fclose(fptr); + if(count == 0) + fprintf(stderr," Warning - no I2C busses found!\n"); + return; + } + + /* look in sysfs */ + /* First figure out where sysfs was mounted */ + if ((f = fopen("/proc/mounts", "r")) == NULL) + return; + while (fgets(n, NAME_MAX, f)) { + sscanf(n, "%[^ ] %[^ ] %[^ ] %*s\n", dev, sysfs, fstype); + if (strcasecmp(fstype, "sysfs") == 0) { + foundsysfs++; + break; + } + } + fclose(f); + if (! foundsysfs) + return; + strcat(sysfs, "/class/i2c-adapter"); + dir = opendir(sysfs); + if (! dir) + return; + fprintf(stderr," Installed I2C busses:\n"); + while ((de = readdir(dir)) != NULL) { + if (!strcmp(de->d_name, ".")) + continue; + if (!strcmp(de->d_name, "..")) + continue; + + sprintf(n, "%s/%s/device/name", sysfs, de->d_name); + + if ((f = fopen(n, "r")) != NULL) { + char x[120]; + fgets(x, 120, f); + fclose(f); + if((border = index(x, '\n')) != NULL) + *border = 0; + if(!strncmp(x, "ISA ", 4)) + fprintf(stderr, " %s\t%-10s\t%-32s\t%s\n", de->d_name, + "dummy", x, "ISA bus algorithm"); + else if(!sscanf(de->d_name, "i2c-%x", &tmp)) + fprintf(stderr, " %s\t%-10s\t%-32s\t%s\n", de->d_name, + "dummy", x, "Dummy bus algorithm"); + else + fprintf(stderr, " %s\t%-10s\t%-32s\t%s\n", de->d_name, + "unknown", x, "Algorithm unavailable"); + count++; + } + } + closedir(dir); + if(count == 0) + fprintf(stderr," Warning - no I2C busses found!\n"); +} diff --git a/prog/dump/i2cdump.c b/prog/dump/i2cdump.c index 6111bdf3..1485ea5e 100644 --- a/prog/dump/i2cdump.c +++ b/prog/dump/i2cdump.c @@ -1,6 +1,6 @@ /* i2cdump.c - Part of i2cdump, a user-space program to dump I2C registers - Copyright (c) 2002 Frodo Looijaard , and + Copyright (c) 2002-2003 Frodo Looijaard , and Mark D. Studebaker This program is free software; you can redistribute it and/or modify @@ -25,6 +25,7 @@ #include #include #include "i2c-dev.h" +#include "version.h" /* We don't use this #define but it was put into i2c.h at the same time as @@ -41,11 +42,10 @@ #define HAVE_PEC 1 #endif +void print_i2c_busses(); + void help(void) { - FILE *fptr; - char s[100]; - fprintf(stderr,"Syntax: i2cdump I2CBUS ADDRESS [MODE] [BANK [BANKREG]]\n"); fprintf(stderr," MODE is 'b[yte]', 'w[ord]', 's[mbusblock], or 'i[2cblock]' (default b)\n"); fprintf(stderr," Append MODE with 'p' for PEC checking\n"); @@ -53,12 +53,7 @@ void help(void) fprintf(stderr," ADDRESS is an integer 0x00 - 0x7f\n"); fprintf(stderr," BANK and BANKREG are for byte and word accesses (default bank 0, reg 0x4e)\n"); fprintf(stderr," BANK is the command for smbusblock accesses (default 0)\n"); - if((fptr = fopen("/proc/bus/i2c", "r"))) { - fprintf(stderr," Installed I2C busses:\n"); - while(fgets(s, 100, fptr)) - fprintf(stderr, " %s", s); - fclose(fptr); - } + print_i2c_busses(); } int main(int argc, char *argv[]) @@ -82,6 +77,11 @@ int main(int argc, char *argv[]) exit(1); } + if((!strcmp(argv[1], "-v")) || (!strcmp(argv[1], "-V"))) { + fprintf(stderr,"i2cdump version %s\n", LM_VERSION); + exit(1); + } + i2cbus = strtol(argv[1],&end,0); if (*end) { fprintf(stderr,"Error: First argument not a number!\n"); diff --git a/prog/dump/i2cset.c b/prog/dump/i2cset.c index 8dfa3412..fa28591b 100644 --- a/prog/dump/i2cset.c +++ b/prog/dump/i2cset.c @@ -1,6 +1,6 @@ /* i2cset.c - A user-space program to write an I2C register. - Copyright (c) 2001 Frodo Looijaard , and + Copyright (c) 2001-2003 Frodo Looijaard , and Mark D. Studebaker This program is free software; you can redistribute it and/or modify @@ -25,23 +25,17 @@ #include #include #include "i2c-dev.h" +#include "version.h" +void print_i2c_busses(); void help(void) __attribute__ ((noreturn)); void help(void) { - FILE *fptr; - char s[100]; - fprintf(stderr,"Syntax: i2cset I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE [MODE]\n"); fprintf(stderr," MODE is 'b[yte]' or 'w[ord]' (default b)\n"); fprintf(stderr," I2CBUS is an integer\n"); - if((fptr = fopen("/proc/bus/i2c", "r"))) { - fprintf(stderr," Installed I2C busses:\n"); - while(fgets(s, 100, fptr)) - fprintf(stderr, " %s", s); - fclose(fptr); - } + print_i2c_busses(); exit(1); } @@ -57,6 +51,11 @@ int main(int argc, char *argv[]) char *filename; long funcs; + if(argc >= 2 && ((!strcmp(argv[1], "-v")) || (!strcmp(argv[1], "-V")))) { + fprintf(stderr,"i2cset version %s\n", LM_VERSION); + exit(1); + } + if (argc < 5) help();