Merge pull request #3 from hgxl64/wait-write-timeout

wait for eeprom write to complete
This commit is contained in:
Stefano Barbato 2021-01-13 10:29:49 +01:00 committed by GitHub
commit fd58aaae76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 395 deletions

22
24cXX.c
View file

@ -72,7 +72,7 @@ int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom* e)
int funcs, fd, r; int funcs, fd, r;
e->fd = e->addr = 0; e->fd = e->addr = 0;
e->dev = 0; e->dev = 0;
fd = open(dev_fqn, O_RDWR); fd = open(dev_fqn, O_RDWR);
if(fd <= 0) if(fd <= 0)
{ {
@ -87,8 +87,9 @@ int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom* e)
return -1; return -1;
} }
// check for req funcs // check for req funcs
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_QUICK );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
@ -179,11 +180,24 @@ int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
__u8 buf[2] = { mem_addr & 0x00ff, data }; __u8 buf[2] = { mem_addr & 0x00ff, data };
return i2c_write_2b(e, buf); return i2c_write_2b(e, buf);
} else if(e->type == EEPROM_TYPE_16BIT_ADDR) { } else if(e->type == EEPROM_TYPE_16BIT_ADDR) {
__u8 buf[3] = __u8 buf[3] =
{ (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data }; { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
return i2c_write_3b(e, buf); return i2c_write_3b(e, buf);
} }
fprintf(stderr, "ERR: unknown eeprom type\n"); fprintf(stderr, "ERR: unknown eeprom type\n");
return -1; return -1;
} }
int eeprom_wait_ready(struct eeprom *e, int max_ms_to_wait)
{
while (max_ms_to_wait > 0) {
int r = i2c_smbus_write_quick(e->fd, 0);
if (r == 0) {
return r;
}
usleep(1000);
--max_ms_to_wait;
}
return -1;
}

18
24cXX.h
View file

@ -15,7 +15,7 @@
***************************************************************************/ ***************************************************************************/
#ifndef _24CXX_H_ #ifndef _24CXX_H_
#define _24CXX_H_ #define _24CXX_H_
#include "i2c-dev.h" #include <linux/i2c-dev.h>
#define EEPROM_TYPE_UNKNOWN 0 #define EEPROM_TYPE_UNKNOWN 0
#define EEPROM_TYPE_8BIT_ADDR 1 #define EEPROM_TYPE_8BIT_ADDR 1
@ -35,24 +35,28 @@ struct eeprom
*/ */
int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom*); int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom*);
/* /*
* closees the eeprom device [e] * closees the eeprom device [e]
*/ */
int eeprom_close(struct eeprom *e); int eeprom_close(struct eeprom *e);
/* /*
* read and returns the eeprom byte at memory address [mem_addr] * read and returns the eeprom byte at memory address [mem_addr]
* Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)
*/ */
int eeprom_read_byte(struct eeprom* e, __u16 mem_addr); int eeprom_read_byte(struct eeprom* e, __u16 mem_addr);
/* /*
* read the current byte * read the current byte
* Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)
*/ */
int eeprom_read_current_byte(struct eeprom *e); int eeprom_read_current_byte(struct eeprom *e);
/* /*
* writes [data] at memory address [mem_addr] * writes [data] at memory address [mem_addr]
* Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)
*/ */
int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data); int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data);
/*
* wait for the eeprom to accept new commands, to be called after a write
*/
int eeprom_wait_ready(struct eeprom *e, int max_ms_to_wait);
#endif #endif

View file

@ -3,7 +3,7 @@ CFLAGS=-g -I. -Wall -O2
all: eeprog all: eeprog
clean: clean:
rm -f eeprog *.o rm -f eeprog eeprog.o 24cXX.o
eeprog: eeprog.o 24cXX.o eeprog: eeprog.o 24cXX.o

View file

@ -34,22 +34,23 @@ int g_quiet;
#define usage_if(a) do { do_usage_if( a , __LINE__); } while(0); #define usage_if(a) do { do_usage_if( a , __LINE__); } while(0);
void do_usage_if(int b, int line) void do_usage_if(int b, int line)
{ {
const static char *eeprog_usage = const static char *eeprog_usage =
"eeprog " VERSION ", a 24Cxx EEPROM reader/writer\n" "eeprog " VERSION ", a 24Cxx EEPROM reader/writer\n"
"Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved.\n" "Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved.\n"
"Usage: eeprog [-fqxdh] [-16|-8] [ -r addr[:count] | -w addr ] /dev/i2c-N i2c-address\n" "Usage: eeprog [-fqxdh] [-16|-8] [ -r addr[:count] | -w addr ] /dev/i2c-N i2c-address\n"
"\n" "\n"
" Address modes: \n" " Address modes: \n"
" -8 Use 8bit address mode for 24c0x...24C16 [default]\n" " -8 Use 8bit address mode for 24c0x...24C16 [default]\n"
" -16 Use 16bit address mode for 24c32...24C256\n" " -16 Use 16bit address mode for 24c32...24C256\n"
" Actions: \n" " Actions: \n"
" -r addr[:count] Read [count] (1 if omitted) bytes from [addr]\n" " -r addr[:count] Read [count] (1 if omitted) bytes from [addr]\n"
" and print them to the standard output\n" " and print them to the standard output\n"
" -w addr Write input (stdin) at address [addr] of the EEPROM\n" " -w addr Write input (stdin) at address [addr] of the EEPROM\n"
" -t n write timeout in ms (default 10)\n"
" -h Print this help\n" " -h Print this help\n"
" Options: \n" " Options: \n"
" -x Set hex output mode\n" " -x Set hex output mode\n"
" -d Dummy mode, display what *would* have been done\n" " -d Dummy mode, display what *would* have been done\n"
" -f Disable warnings and don't ask confirmation\n" " -f Disable warnings and don't ask confirmation\n"
" -q Quiet mode\n" " -q Quiet mode\n"
"\n" "\n"
@ -98,7 +99,7 @@ void parse_arg(char *arg, int* paddr, int *psize)
int confirm_action() int confirm_action()
{ {
fprintf(stderr, fprintf(stderr,
"\n" "\n"
"____________________________WARNING____________________________\n" "____________________________WARNING____________________________\n"
"Erroneously writing to a system EEPROM (like DIMM SPD modules)\n" "Erroneously writing to a system EEPROM (like DIMM SPD modules)\n"
@ -115,7 +116,7 @@ int confirm_action()
"\n" "\n"
); );
getchar(); getchar();
return 1; return 1;
} }
int read_from_eeprom(struct eeprom *e, int addr, int size, int hex) int read_from_eeprom(struct eeprom *e, int addr, int size, int hex)
@ -126,12 +127,12 @@ int read_from_eeprom(struct eeprom *e, int addr, int size, int hex)
die_if((ch = eeprom_read_byte(e, addr)) < 0, "read error"); die_if((ch = eeprom_read_byte(e, addr)) < 0, "read error");
if(hex) if(hex)
{ {
if( (i % 16) == 0 ) if( (i % 16) == 0 )
printf("\n %.4x| ", addr); printf("\n %.4x| ", addr);
else if( (i % 8) == 0 ) else if( (i % 8) == 0 )
printf(" "); printf(" ");
printf("%.2x ", ch); printf("%.2x ", ch);
} else } else
putchar(ch); putchar(ch);
} }
if(hex) if(hex)
@ -140,7 +141,7 @@ int read_from_eeprom(struct eeprom *e, int addr, int size, int hex)
return 0; return 0;
} }
int write_to_eeprom(struct eeprom *e, int addr) int write_to_eeprom(struct eeprom *e, int addr, int timeout)
{ {
int c; int c;
while((c = getchar()) != EOF) while((c = getchar()) != EOF)
@ -148,6 +149,7 @@ int write_to_eeprom(struct eeprom *e, int addr)
print_info("."); print_info(".");
fflush(stdout); fflush(stdout);
die_if(eeprom_write_byte(e, addr++, c), "write error"); die_if(eeprom_write_byte(e, addr++, c), "write error");
die_if(eeprom_wait_ready(e, timeout), "write timeout");
} }
print_info("\n\n"); print_info("\n\n");
return 0; return 0;
@ -157,6 +159,7 @@ int main(int argc, char** argv)
{ {
struct eeprom e; struct eeprom e;
int ret, op, i2c_addr, memaddr, size, want_hex, dummy, force, sixteen; int ret, op, i2c_addr, memaddr, size, want_hex, dummy, force, sixteen;
int timeout = 10;
char *device, *arg = 0, *i2c_addr_s; char *device, *arg = 0, *i2c_addr_s;
struct stat st; struct stat st;
int eeprom_type = 0; int eeprom_type = 0;
@ -164,14 +167,14 @@ int main(int argc, char** argv)
op = want_hex = dummy = force = sixteen = 0; op = want_hex = dummy = force = sixteen = 0;
g_quiet = 0; g_quiet = 0;
while((ret = getopt(argc, argv, "1:8fr:qhw:xd")) != -1) while((ret = getopt(argc, argv, "1:8fr:qhw:xdt:")) != -1)
{ {
switch(ret) switch(ret)
{ {
case '1': case '1':
usage_if(*optarg != '6' || strlen(optarg) != 1); usage_if(*optarg != '6' || strlen(optarg) != 1);
die_if(eeprom_type, "EEPROM type switch (-8 or -16) used twice"); die_if(eeprom_type, "EEPROM type switch (-8 or -16) used twice");
eeprom_type = EEPROM_TYPE_16BIT_ADDR; eeprom_type = EEPROM_TYPE_16BIT_ADDR;
break; break;
case 'x': case 'x':
want_hex++; want_hex++;
@ -192,8 +195,11 @@ int main(int argc, char** argv)
case 'h': case 'h':
usage_if(1); usage_if(1);
break; break;
case 't':
timeout = atoi(optarg);
break;
default: default:
die_if(op != 0, "Both read and write requested"); die_if(op != 0, "Both read and write requested");
arg = optarg; arg = optarg;
op = ret; op = ret;
} }
@ -201,7 +207,7 @@ int main(int argc, char** argv)
if(!eeprom_type) if(!eeprom_type)
eeprom_type = EEPROM_TYPE_8BIT_ADDR; // default eeprom_type = EEPROM_TYPE_8BIT_ADDR; // default
usage_if(op == 0); // no switches usage_if(op == 0); // no switches
// set device and i2c_addr reading from cmdline or env // set device and i2c_addr reading from cmdline or env
device = i2c_addr_s = 0; device = i2c_addr_s = 0;
switch(argc - optind) switch(argc - optind)
@ -232,15 +238,15 @@ int main(int argc, char** argv)
print_info("eeprog %s, a 24Cxx EEPROM reader/writer\n", VERSION); print_info("eeprog %s, a 24Cxx EEPROM reader/writer\n", VERSION);
print_info("Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved.\n"); print_info("Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved.\n");
print_info(" Bus: %s, Address: 0x%x, Mode: %dbit\n", print_info(" Bus: %s, Address: 0x%x, Mode: %dbit\n",
device, i2c_addr, device, i2c_addr,
(eeprom_type == EEPROM_TYPE_8BIT_ADDR ? 8 : 16) ); (eeprom_type == EEPROM_TYPE_8BIT_ADDR ? 8 : 16) );
if(dummy) if(dummy)
{ {
fprintf(stderr, "Dummy mode selected, nothing done.\n"); fprintf(stderr, "Dummy mode selected, nothing done.\n");
return 0; return 0;
} }
die_if(eeprom_open(device, i2c_addr, eeprom_type, &e) < 0, die_if(eeprom_open(device, i2c_addr, eeprom_type, &e) < 0,
"unable to open eeprom device file " "unable to open eeprom device file "
"(check that the file exists and that it's readable)"); "(check that the file exists and that it's readable)");
switch(op) switch(op)
@ -257,9 +263,9 @@ int main(int argc, char** argv)
if(force == 0) if(force == 0)
confirm_action(); confirm_action();
parse_arg(arg, &memaddr, &size); parse_arg(arg, &memaddr, &size);
print_info(" Writing stdin starting at address 0x%x\n", print_info(" Writing stdin starting at address 0x%x with timeout %dms\n",
memaddr); memaddr, timeout);
write_to_eeprom(&e, memaddr); write_to_eeprom(&e, memaddr, timeout);
break; break;
default: default:
usage_if(1); usage_if(1);

361
i2c-dev.h
View file

@ -1,361 +0,0 @@
/*
i2c-dev.h - i2c-bus driver, char device interface
Copyright (C) 1995-97 Simon G. Vogl
Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
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.
*/
/* $Id: i2c-dev.h,v 1.4 2003/11/27 23:08:06 tat Exp $ */
#ifndef LIB_I2CDEV_H
#define LIB_I2CDEV_H
#include <linux/types.h>
#include <sys/ioctl.h>
/* -- i2c.h -- */
/*
* I2C Message - used for pure i2c transaction, also from /dev interface
*/
struct i2c_msg {
__u16 addr; /* slave address */
unsigned short flags;
#define I2C_M_TEN 0x10 /* we have a ten bit chip address */
#define I2C_M_RD 0x01
#define I2C_M_NOSTART 0x4000
#define I2C_M_REV_DIR_ADDR 0x2000
#define I2C_M_IGNORE_NAK 0x1000
#define I2C_M_NO_RD_ACK 0x0800
short len; /* msg length */
char *buf; /* pointer to msg data */
int err;
short done;
};
/* To determine what functionality is present */
#define I2C_FUNC_I2C 0x00000001
#define I2C_FUNC_10BIT_ADDR 0x00000002
#define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */
#define I2C_FUNC_SMBUS_HWPEC_CALC 0x00000008 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_READ_WORD_DATA_PEC 0x00000800 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_WRITE_WORD_DATA_PEC 0x00001000 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_PROC_CALL_PEC 0x00002000 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL_PEC 0x00004000 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_QUICK 0x00010000
#define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
#define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
#define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
#define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
#define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
#define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
#define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
#define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
#define I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 0x10000000 /* I2C-like block xfer */
#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2 0x20000000 /* w/ 2-byte reg. addr. */
#define I2C_FUNC_SMBUS_READ_BLOCK_DATA_PEC 0x40000000 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC 0x80000000 /* SMBus 2.0 */
#define I2C_FUNC_SMBUS_BYTE I2C_FUNC_SMBUS_READ_BYTE | \
I2C_FUNC_SMBUS_WRITE_BYTE
#define I2C_FUNC_SMBUS_BYTE_DATA I2C_FUNC_SMBUS_READ_BYTE_DATA | \
I2C_FUNC_SMBUS_WRITE_BYTE_DATA
#define I2C_FUNC_SMBUS_WORD_DATA I2C_FUNC_SMBUS_READ_WORD_DATA | \
I2C_FUNC_SMBUS_WRITE_WORD_DATA
#define I2C_FUNC_SMBUS_BLOCK_DATA I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
#define I2C_FUNC_SMBUS_I2C_BLOCK I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
#define I2C_FUNC_SMBUS_I2C_BLOCK_2 I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 | \
I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2
#define I2C_FUNC_SMBUS_BLOCK_DATA_PEC I2C_FUNC_SMBUS_READ_BLOCK_DATA_PEC | \
I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC
#define I2C_FUNC_SMBUS_WORD_DATA_PEC I2C_FUNC_SMBUS_READ_WORD_DATA_PEC | \
I2C_FUNC_SMBUS_WRITE_WORD_DATA_PEC
#define I2C_FUNC_SMBUS_READ_BYTE_PEC I2C_FUNC_SMBUS_READ_BYTE_DATA
#define I2C_FUNC_SMBUS_WRITE_BYTE_PEC I2C_FUNC_SMBUS_WRITE_BYTE_DATA
#define I2C_FUNC_SMBUS_READ_BYTE_DATA_PEC I2C_FUNC_SMBUS_READ_WORD_DATA
#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA_PEC I2C_FUNC_SMBUS_WRITE_WORD_DATA
#define I2C_FUNC_SMBUS_BYTE_PEC I2C_FUNC_SMBUS_BYTE_DATA
#define I2C_FUNC_SMBUS_BYTE_DATA_PEC I2C_FUNC_SMBUS_WORD_DATA
#define I2C_FUNC_SMBUS_EMUL I2C_FUNC_SMBUS_QUICK | \
I2C_FUNC_SMBUS_BYTE | \
I2C_FUNC_SMBUS_BYTE_DATA | \
I2C_FUNC_SMBUS_WORD_DATA | \
I2C_FUNC_SMBUS_PROC_CALL | \
I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \
I2C_FUNC_SMBUS_WRITE_BLOCK_DATA_PEC | \
I2C_FUNC_SMBUS_I2C_BLOCK
/*
* Data for SMBus Messages
*/
#define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
#define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
union i2c_smbus_data {
__u8 byte;
__u16 word;
__u8 block[I2C_SMBUS_BLOCK_MAX + 3]; /* block[0] is used for length */
/* one more for read length in block process call */
/* and one more for PEC */
};
/* smbus_access read or write markers */
#define I2C_SMBUS_READ 1
#define I2C_SMBUS_WRITE 0
/* SMBus transaction types (size parameter in the above functions)
Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
#define I2C_SMBUS_QUICK 0
#define I2C_SMBUS_BYTE 1
#define I2C_SMBUS_BYTE_DATA 2
#define I2C_SMBUS_WORD_DATA 3
#define I2C_SMBUS_PROC_CALL 4
#define I2C_SMBUS_BLOCK_DATA 5
#define I2C_SMBUS_I2C_BLOCK_DATA 6
#define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
#define I2C_SMBUS_BLOCK_DATA_PEC 8 /* SMBus 2.0 */
#define I2C_SMBUS_PROC_CALL_PEC 9 /* SMBus 2.0 */
#define I2C_SMBUS_BLOCK_PROC_CALL_PEC 10 /* SMBus 2.0 */
#define I2C_SMBUS_WORD_DATA_PEC 11 /* SMBus 2.0 */
/* ----- commands for the ioctl like i2c_command call:
* note that additional calls are defined in the algorithm and hw
* dependent layers - these can be listed here, or see the
* corresponding header files.
*/
/* -> bit-adapter specific ioctls */
#define I2C_RETRIES 0x0701 /* number of times a device address */
/* should be polled when not */
/* acknowledging */
#define I2C_TIMEOUT 0x0702 /* set timeout - call with int */
/* this is for i2c-dev.c */
#define I2C_SLAVE 0x0703 /* Change slave address */
/* Attn.: Slave address is 7 or 10 bits */
#define I2C_SLAVE_FORCE 0x0706 /* Change slave address */
/* Attn.: Slave address is 7 or 10 bits */
/* This changes the address, even if it */
/* is already taken! */
#define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */
#define I2C_FUNCS 0x0705 /* Get the adapter functionality */
#define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/
#define I2C_PEC 0x0708 /* != 0 for SMBus PEC */
#if 0
#define I2C_ACK_TEST 0x0710 /* See if a slave is at a specific address */
#endif
#define I2C_SMBUS 0x0720 /* SMBus-level access */
/* -- i2c.h -- */
/* Note: 10-bit addresses are NOT supported! */
/* This is the structure as used in the I2C_SMBUS ioctl call */
struct i2c_smbus_ioctl_data {
char read_write;
__u8 command;
int size;
union i2c_smbus_data *data;
};
/* This is the structure as used in the I2C_RDWR ioctl call */
struct i2c_rdwr_ioctl_data {
struct i2c_msg *msgs; /* pointers to i2c_msgs */
int nmsgs; /* number of i2c_msgs */
};
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
int size, union i2c_smbus_data *data)
{
struct i2c_smbus_ioctl_data args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(file,I2C_SMBUS,&args);
}
static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
{
return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
}
static inline __s32 i2c_smbus_read_byte(int file)
{
union i2c_smbus_data data;
if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
return -1;
else
return 0x0FF & data.byte;
}
static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
{
return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
I2C_SMBUS_BYTE,NULL);
}
static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
{
union i2c_smbus_data data;
if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
I2C_SMBUS_BYTE_DATA,&data))
return -1;
else
return 0x0FF & data.byte;
}
static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
__u8 value)
{
union i2c_smbus_data data;
data.byte = value;
return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_BYTE_DATA, &data);
}
static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
{
union i2c_smbus_data data;
if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
I2C_SMBUS_WORD_DATA,&data))
return -1;
else
return 0x0FFFF & data.word;
}
static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
__u16 value)
{
union i2c_smbus_data data;
data.word = value;
return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_WORD_DATA, &data);
}
static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
{
union i2c_smbus_data data;
data.word = value;
if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_PROC_CALL,&data))
return -1;
else
return 0x0FFFF & data.word;
}
/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
__u8 *values)
{
union i2c_smbus_data data;
int i;
if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
I2C_SMBUS_BLOCK_DATA,&data))
return -1;
else {
for (i = 1; i <= data.block[0]; i++)
values[i-1] = data.block[i];
return data.block[0];
}
}
static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
__u8 length, __u8 *values)
{
union i2c_smbus_data data;
int i;
if (length > 32)
length = 32;
for (i = 1; i <= length; i++)
data.block[i] = values[i-1];
data.block[0] = length;
return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_BLOCK_DATA, &data);
}
/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
__u8 *values)
{
union i2c_smbus_data data;
int i;
if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
I2C_SMBUS_I2C_BLOCK_DATA,&data))
return -1;
else {
for (i = 1; i <= data.block[0]; i++)
values[i-1] = data.block[i];
return data.block[0];
}
}
static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
__u8 length, __u8 *values)
{
union i2c_smbus_data data;
int i;
if (length > 32)
length = 32;
for (i = 1; i <= length; i++)
data.block[i] = values[i-1];
data.block[0] = length;
return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_I2C_BLOCK_DATA, &data);
}
/* Returns the number of read bytes */
static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
__u8 length, __u8 *values)
{
union i2c_smbus_data data;
int i;
if (length > 32)
length = 32;
for (i = 1; i <= length; i++)
data.block[i] = values[i-1];
data.block[0] = length;
if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_BLOCK_PROC_CALL,&data))
return -1;
else {
for (i = 1; i <= data.block[0]; i++)
values[i-1] = data.block[i];
return data.block[0];
}
}
#endif /* LIB_I2CDEV_H */