From 47a3cc59c0cc19167fd77814d8062251b8c61843 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Thu, 29 Sep 2005 20:59:22 +0000 Subject: [PATCH] New helper program to read a single register from an I2C/SMBus chip. It has byte data and word data read support as well as byte write/byte read, and supports PEC too. Beware that byte write with PEC enabled can write random values to registers on non-PEC-aware chips. git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@3108 7894878c-1315-0410-8ee3-d5d059ff63e0 --- prog/dump/Module.mk | 14 ++- prog/dump/i2cget.8 | 63 ++++++++++ prog/dump/i2cget.c | 276 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 349 insertions(+), 4 deletions(-) create mode 100644 prog/dump/i2cget.8 create mode 100644 prog/dump/i2cget.c diff --git a/prog/dump/Module.mk b/prog/dump/Module.mk index b82cb1b1..adb0733f 100644 --- a/prog/dump/Module.mk +++ b/prog/dump/Module.mk @@ -22,14 +22,17 @@ MODULE_DIR := prog/dump PROGDUMPDIR := $(MODULE_DIR) PROGDUMPMAN8DIR := $(MANDIR)/man8 -PROGDUMPMAN8FILES := $(MODULE_DIR)/i2cdump.8 $(MODULE_DIR)/i2cset.8 +PROGDUMPMAN8FILES := $(MODULE_DIR)/i2cdump.8 $(MODULE_DIR)/i2cset.8 \ + $(MODULE_DIR)/i2cget.8 # Regrettably, even 'simply expanded variables' will not put their currently # defined value verbatim into the command-list of rules... -PROGDUMPTARGETS := $(MODULE_DIR)/i2cdump $(MODULE_DIR)/i2cset +PROGDUMPTARGETS := $(MODULE_DIR)/i2cdump $(MODULE_DIR)/i2cset \ + $(MODULE_DIR)/i2cget PROGDUMPSOURCES := $(MODULE_DIR)/i2cdump.c $(MODULE_DIR)/i2cset.c \ - $(MODULE_DIR)/i2cbusses.c -PROGDUMPBININSTALL := $(MODULE_DIR)/i2cdump $(MODULE_DIR)/i2cset + $(MODULE_DIR)/i2cget.c $(MODULE_DIR)/i2cbusses.c +PROGDUMPBININSTALL := $(MODULE_DIR)/i2cdump $(MODULE_DIR)/i2cset \ + $(MODULE_DIR)/i2cget # Only build isadump and isaset on x86 machines. ifneq (,$(findstring $(MACHINE), i386 i486 i586 i686 x86_64)) @@ -56,6 +59,9 @@ $(MODULE_DIR)/i2cdump: $(MODULE_DIR)/i2cdump.ro $(MODULE_DIR)/i2cbusses.ro $(MODULE_DIR)/i2cset: $(MODULE_DIR)/i2cset.ro $(MODULE_DIR)/i2cbusses.ro $(CC) $(EXLDFLAGS) -o $@ $^ +$(MODULE_DIR)/i2cget: $(MODULE_DIR)/i2cget.ro $(MODULE_DIR)/i2cbusses.ro + $(CC) $(EXLDFLAGS) -o $@ $^ + $(MODULE_DIR)/isadump: $(MODULE_DIR)/isadump.ro $(MODULE_DIR)/superio.ro $(CC) $(EXLDFLAGS) -o $@ $^ diff --git a/prog/dump/i2cget.8 b/prog/dump/i2cget.8 new file mode 100644 index 00000000..fbd7ef1c --- /dev/null +++ b/prog/dump/i2cget.8 @@ -0,0 +1,63 @@ +.TH I2CGET 8 "September 2005" +.SH "NAME" +i2cget \- read from I2C/SMBus chip registers + +.SH SYNOPSIS +.B i2cget +.RB [ -y ] +.I i2cbus +.I chip-address +.RI [ data-address " [" mode ]] +.br +.B i2cget +.B -V + +.SH DESCRIPTION +i2cget is a small helper program to read registers visible through the I2C +bus (or SMBus). + +.SH OPTIONS +.TP +.B -V +Display the version and exit. +.TP +.B -y +Disable interactive mode. By default, i2cget will wait for a confirmation +from the user before messing with the I2C bus. When this flag is used, it +will perform the operation directly. This is mainly meant to be used in +scripts. Use with caution. +.PP +There are two required options to i2cget. \fIi2cbus\fR indicates the number +of the I2C bus to be scanned. This number should correspond to one of +the busses listed by \fIi2cdetect -l\fR. \fIchip-address\fR specifies the +address of the chip on that bus, and is an integer between 0x03 and 0x77. +.PP +\fIdata-address\fR specifies the address on that chip to read from, and is +an integer between 0x00 and 0xFF. If omitted, the currently active register +will be read (if that makes sense for the considered chip). +.PP +The \fImode\fR parameter, if specified, is one of the letters \fBb\fP, +\fBw\fP or \fBc\fP, corresponding to a read byte data, a read word data or a +write byte/read byte transaction, respectively. A \fBp\fP can also be appended +to the \fImode\fR parameter to enable PEC. If the \fImode\fR parameter is omitted, +i2cget defaults to a read byte data transaction, unless \fIdata-address\fR is +also omitted, in which case the default (and only valid) transaction is a +single read byte. + +.SH WARNING +i2cget can be extremely dangerous if used improperly. I2C and SMBus are designed +in such a way that an SMBus read transaction can be seen as a write transaction by +certain chips. This is particularly true if setting \fImode\fR to \fBcp\fP (write byte/read +byte with PEC). Be extremely careful using this program. + +.SH CONFORMING TO +lm_sensors-2.x + +.SH SEE ALSO +i2cdump(8), i2cset(8) + +.SH AUTHOR +Jean Delvare +http://www.lm-sensors.nu/ +This manual page was strongly inspired from those written by David Z Maze +for i2cset. diff --git a/prog/dump/i2cget.c b/prog/dump/i2cget.c new file mode 100644 index 00000000..667fc7ea --- /dev/null +++ b/prog/dump/i2cget.c @@ -0,0 +1,276 @@ +/* + i2cget.c - A user-space program to read an I2C register. + Copyright (C) 2005 Jean Delvare + + Based on i2cset.c: + Copyright (C) 2001-2003 Frodo Looijaard , and + Mark D. Studebaker + Copyright (C) 2004-2005 Jean Delvare + + 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 +#include +#include "i2cbusses.h" +#include "i2c-dev.h" +#include "version.h" + +void help(void) __attribute__ ((noreturn)); + +void help(void) +{ + fprintf(stderr, "Syntax: i2cget [-y] I2CBUS CHIP-ADDRESS [DATA-ADDRESS " + "[MODE]]\n" + " i2cget -V\n" + " MODE can be: 'b' (read byte data, default)\n" + " 'w' (read word data)\n" + " 'c' (write byte/read byte)\n" + " If DATA-ADDRESS is omitted, a single read byte command is " + "issued\n" + " Append 'p' to MODE for PEC checking\n" + " I2CBUS is an integer\n"); + print_i2c_busses(0); + exit(1); +} + +int check_funcs(int file, int i2cbus, int size, int daddress, int pec) +{ + long funcs; + + /* check adapter functionality */ + if (ioctl(file, I2C_FUNCS, &funcs) < 0) { + fprintf(stderr, "Error: Could not get the adapter " + "functionality matrix: %s\n", strerror(errno)); + return -1; + } + + switch (size) { + case I2C_SMBUS_BYTE: + if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) { + fprintf(stderr, "Error: Adapter for i2c bus %d does " + "not have read byte capability\n", i2cbus); + return -1; + } + if (daddress >= 0 + && !(funcs & I2C_FUNC_SMBUS_WRITE_BYTE)) { + fprintf(stderr, "Error: Adapter for i2c bus %d does " + "not have write byte capability\n", i2cbus); + return -1; + } + break; + + case I2C_SMBUS_BYTE_DATA: + if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) { + fprintf(stderr, "Error: Adapter for i2c bus %d does " + "not have read byte data capability\n", i2cbus); + return -1; + } + break; + + case I2C_SMBUS_WORD_DATA: + if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) { + fprintf(stderr, "Error: Adapter for i2c bus %d does " + "not have read word data capability\n", i2cbus); + return -1; + } + break; + } + + if (pec + && !(funcs & (I2C_FUNC_SMBUS_HWPEC_CALC | I2C_FUNC_I2C))) { + fprintf(stderr, "Warning: Adapter for i2c bus %d does " + "not seem to support PEC\n", i2cbus); + } + + return 0; +} + +int set_slave(int file, int address) +{ + /* use FORCE so that we can read 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; + } + + return 0; +} + +int confirm(const char *filename, int address, int size, int daddress, int pec) +{ + char s[2]; + int dont = 0; + + fprintf(stderr, "WARNING! This program can confuse your I2C " + "bus, cause data loss and worse!\n"); + + /* Don't let the user break his/her EEPROMs */ + if (address >= 0x50 && address <= 0x57 && pec) { + fprintf(stderr, "STOP! EEPROMs are I2C devices, not " + "SMBus devices. Using PEC\non I2C devices may " + "result in unexpected results, such as\n" + "trashing the contents of EEPROMs. We can't " + "let you do that, sorry.\n"); + return 0; + } + + if (size == I2C_SMBUS_BYTE && daddress >= 0 && pec) { + fprintf(stderr, "WARNING! All I2C chips and some SMBus chips " + "will interpret a write\nbyte command with PEC as a" + "write byte data command, effectively writing a\n" + "value into a register!\n"); + dont++; + } + + fprintf(stderr, "I will read from device file %s, chip " + "address 0x%02x, ", filename, address); + if (daddress < 0) + fprintf(stderr, "current data\naddress"); + else + fprintf(stderr, "data address\n0x%02x", daddress); + fprintf(stderr, ", using %s.\n", + size == I2C_SMBUS_BYTE ? (daddress < 0 ? + "read byte" : "write byte/read byte") : + size == I2C_SMBUS_BYTE_DATA ? "read byte data" : + "read word data"); + if (pec) + fprintf(stderr, "PEC checking enabled.\n"); + + fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n"); + fflush(stderr); + fgets(s, 2, stdin); + if ((s[0] != '\n' || dont) && s[0] != 'y' && s[0] != 'Y') { + fprintf(stderr, "Aborting on user request.\n"); + return 0; + } + + return 1; +} + +int main(int argc, char *argv[]) +{ + char *end; + int res, i2cbus, address, file; + int size = I2C_SMBUS_BYTE_DATA; + int daddress; + char filename[20]; + int pec = 0; + int flags = 0; + int yes = 0, version = 0; + + /* handle (optional) flags first */ + while (1+flags < argc && argv[1+flags][0] == '-') { + switch (argv[1+flags][1]) { + case 'V': version = 1; break; + case 'y': yes = 1; break; + default: + fprintf(stderr, "Warning: Unsupported flag " + "\"-%c\"!\n", argv[1+flags][1]); + help(); + exit(1); + } + flags++; + } + + if (version) { + fprintf(stderr, "i2cget version %s\n", LM_VERSION); + exit(0); + } + + if (argc - flags < 3) + help(); + + i2cbus = strtol(argv[flags+1], &end, 0); + if (*end || i2cbus < 0 || i2cbus > 0x3f) { + fprintf(stderr, "Error: I2CBUS argument invalid!\n"); + help(); + } + + address = strtol(argv[flags+2], &end, 0); + if (*end || address < 3 || address > 0x77) { + fprintf(stderr, "Error: Chip address invalid!\n"); + help(); + } + + if (!(flags+3 < argc)) { + size = I2C_SMBUS_BYTE; + daddress = -1; + } else { + daddress = strtol(argv[flags+3], &end, 0); + if (*end || daddress < 0 || daddress > 0xff) { + fprintf(stderr, "Error: Data address invalid!\n"); + help(); + } + } + + if (flags+4 < argc) { + switch (argv[flags+4][0]) { + case 'b': size = I2C_SMBUS_BYTE_DATA; break; + case 'w': size = I2C_SMBUS_WORD_DATA; break; + case 'c': size = I2C_SMBUS_BYTE; break; + default: + fprintf(stderr, "Error: Invalid mode!\n"); + help(); + } + pec = argv[flags+4][1] == 'p'; + } + + file = open_i2c_dev(i2cbus, filename, 0); + if (file < 0 + || check_funcs(file, i2cbus, size, daddress, pec) + || set_slave(file, address)) + exit(1); + + if (!yes && !confirm(filename, address, size, daddress, pec)) + exit(0); + + if (pec && ioctl(file, I2C_PEC, 1) < 0) { + fprintf(stderr, "Error: Could not set PEC: %s\n", + strerror(errno)); + exit(1); + } + + switch (size) { + case I2C_SMBUS_BYTE: + if (daddress >= 0) { + res = i2c_smbus_write_byte(file, daddress); + if (res < 0) + fprintf(stderr, "Warning - write failed\n"); + } + res = i2c_smbus_read_byte(file); + break; + case I2C_SMBUS_WORD_DATA: + res = i2c_smbus_read_word_data(file, daddress); + break; + default: /* I2C_SMBUS_BYTE_DATA */ + res = i2c_smbus_read_byte_data(file, daddress); + } + close(file); + + if (res < 0) { + fprintf(stderr, "Error: Read failed\n"); + exit(2); + } + + printf("0x%0*x\n", size == I2C_SMBUS_WORD_DATA ? 4 : 2, res); + + exit(0); +}