/* Zipit poweroff tool. * (c) 2005 Chris Studholme * GPL */ #include #include #include #include #include #include #include #include #include "i2c-dev.h" static int i2c_open(int i2cbus) { char filename1[32]; char filename2[32]; char filename3[32]; int e1,e2,e3; int file; /* * 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 ((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"); } return -1; } else { //filename = filename3; } } else { //filename = filename2; } } else { //filename = filename1; } return file; } int main(int argc, char **argv) { if (argc<2 || (strcmp(argv[1],"-p")!=0 && strcmp(argv[1],"-r")!=0)) { printf("Usage:\n"); printf(" poweroff-zipit [ -p | -r ]\n"); return 1; } int i2cbus = 0; int address = 0x55; int file = i2c_open(i2cbus); if (file==-1) { return 1; } /* use FORCE so that we can look at registers even when a driver is also running */ if (ioctl(file,I2C_SLAVE_FORCE,address) < 0) { fprintf(stderr,"Error: Could not set address to %d: %s\n",address, strerror(errno)); return 1; } if (strcmp(argv[1],"-p")==0) i2c_smbus_read_byte_data(file,0x81); else if (strcmp(argv[1],"-r")==0) i2c_smbus_read_byte_data(file,0x84); close(file); return 0; }