2
0
mirror of https://github.com/lm-sensors/lm-sensors synced 2025-08-31 14:25:39 +00:00

Refactor i2c device opening.

git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@2675 7894878c-1315-0410-8ee3-d5d059ff63e0
This commit is contained in:
Jean Delvare
2004-08-28 20:42:49 +00:00
parent 680fc92ede
commit 1cc6544e2b
4 changed files with 45 additions and 114 deletions

View File

@@ -25,7 +25,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "../dump/i2cbusses.h"
#include "i2c-dev.h"
#include "version.h"
@@ -33,8 +33,6 @@
#define MODE_QUICK 1
#define MODE_READ 2
void print_i2c_busses(int);
void help(void)
{
fprintf(stderr,
@@ -49,23 +47,6 @@ void help(void)
print_i2c_busses(0);
}
int open_i2c_dev(const int i2cbus, char *filename)
{
int file;
sprintf(filename, "/dev/i2c-%d", i2cbus);
file = open(filename, O_RDWR);
if (file >= 0 || errno != ENOENT) {
return file;
}
sprintf(filename, "/dev/i2c/%d", i2cbus);
file = open(filename, O_RDWR);
return file;
}
int scan_i2c_bus(int file, const int mode, const int first, const int last)
{
int i, j;
@@ -231,15 +212,6 @@ int main(int argc, char *argv[])
file = open_i2c_dev(i2cbus, filename);
if (file < 0) {
if (errno == ENOENT) {
fprintf(stderr, "Error: Could not open file `/dev/i2c-%d' or `/dev/i2c/%d': %s\n",
i2cbus, i2cbus, strerror(ENOENT));
} else {
fprintf(stderr, "Error: Could not open file `%s': %s\n",
filename, strerror(errno));
if (errno == EACCES)
fprintf(stderr, "Run as root?\n");
}
exit(1);
}

View File

@@ -20,10 +20,15 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include "i2cbusses.h"
/*
this just prints out the installed i2c busses in a consistent format, whether
@@ -163,3 +168,31 @@ done:
"Be sure you have done 'modprobe i2c-dev'\n"
"and also modprobed your i2c bus drivers\n");
}
int open_i2c_dev(const int i2cbus, char *filename)
{
int file;
sprintf(filename, "/dev/i2c/%d", i2cbus);
file = open(filename, O_RDWR);
if (file < 0 && errno == ENOENT) {
sprintf(filename, "/dev/i2c-%d", i2cbus);
file = open(filename, O_RDWR);
}
if (file < 0) {
if (errno == ENOENT) {
fprintf(stderr, "Error: Could not open file "
"`/dev/i2c-%d' or `/dev/i2c/%d': %s\n",
i2cbus, i2cbus, strerror(ENOENT));
} else {
fprintf(stderr, "Error: Could not open file "
"`%s': %s\n", filename, strerror(errno));
if (errno == EACCES)
fprintf(stderr, "Run as root?\n");
}
}
return file;
}

View File

@@ -24,7 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "i2cbusses.h"
#include "i2c-dev.h"
#include "version.h"
@@ -43,8 +43,6 @@
#define HAVE_PEC 1
#endif
void print_i2c_busses(int);
void help(void)
{
fprintf(stderr, "Syntax: i2cdump I2CBUS ADDRESS [MODE] [BANK "
@@ -64,11 +62,8 @@ int main(int argc, char *argv[])
{
char *end;
int i, j, res, res2, i2cbus, address, size, file;
int e1, e2;
int bank = 0, bankreg = 0x4E;
char filename1[20];
char filename2[20];
char *filename;
char filename[20];
long funcs;
unsigned char cblock[256];
int block[256];
@@ -174,42 +169,9 @@ int main(int argc, char *argv[])
}
}
/*
* Try both variants and give the correct error message
* upon failure
*/
sprintf(filename1, "/dev/i2c-%d", i2cbus);
sprintf(filename2, "/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 (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;
}
} else {
filename = filename1;
file = open_i2c_dev(i2cbus, filename);
if (file < 0) {
exit(1);
}
/* check adapter functionality */

View File

@@ -24,11 +24,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "i2cbusses.h"
#include "i2c-dev.h"
#include "version.h"
void print_i2c_busses(int);
void help(void) __attribute__ ((noreturn));
void help(void)
@@ -46,10 +45,8 @@ int main(int argc, char *argv[])
char *end;
int res, i2cbus, address, size, file;
int value, daddress;
int e1, e2;
char filename1[20];
char filename2[20];
char *filename;
int e1;
char filename[20];
long funcs;
if (argc >= 2 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "-V"))) {
@@ -103,42 +100,9 @@ int main(int argc, char *argv[])
help();
}
/*
* Try both variants and give the correct error message
* upon failure
*/
sprintf(filename1, "/dev/i2c-%d", i2cbus);
sprintf(filename2, "/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 (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;
}
} else {
filename = filename1;
file = open_i2c_dev(i2cbus, filename);
if (file < 0) {
exit(1);
}
/* check adapter functionality */