From 1b05c9e1c19c1190930f0b4120ed229ecad776ab Mon Sep 17 00:00:00 2001 From: Frodo Looijaard Date: Wed, 13 Jan 1999 16:46:38 +0000 Subject: [PATCH] User-space header file added for i2c-dev access git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@159 7894878c-1315-0410-8ee3-d5d059ff63e0 --- TODO | 3 + kernel/Module.mk | 2 +- kernel/include/i2c-dev.h | 150 +++++++++++++++++++++++++++++++++++++++ kernel/include/smbus.h | 2 +- src/Module.mk | 2 +- src/i2c-dev.h | 150 +++++++++++++++++++++++++++++++++++++++ src/smbus.h | 2 +- 7 files changed, 307 insertions(+), 4 deletions(-) create mode 100644 kernel/include/i2c-dev.h create mode 100644 src/i2c-dev.h diff --git a/TODO b/TODO index fb0edd6c..1bfd9f05 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,8 @@ Many, many things. Most notably: +* Merge newest Simon Vogl archive +* Merge man-pages +* Rename some public kernel header files * Fans give overflow if small values are written to them. * At least the bit-lp and bit-velle modules do no detection on loading; ask Simon whether this is possible to add. diff --git a/kernel/Module.mk b/kernel/Module.mk index f8211b59..8a9e609c 100644 --- a/kernel/Module.mk +++ b/kernel/Module.mk @@ -32,7 +32,7 @@ SRCTARGETS := $(MODULE_DIR)/smbus.o $(MODULE_DIR)/piix4.o $(MODULE_DIR)/isa.o \ $(MODULE_DIR)/i2c-dev.o SRCHEADERFILES := $(MODULE_DIR)/sensors.h $(MODULE_DIR)/isa.h \ - $(MODULE_DIR)/smbus.h + $(MODULE_DIR)/smbus.h $(MODULE_DIR)/i2c-dev.h # Include all dependency files INCLUDEFILES += $(SRCTARGETS:.o=.d) diff --git a/kernel/include/i2c-dev.h b/kernel/include/i2c-dev.h new file mode 100644 index 00000000..8eaae447 --- /dev/null +++ b/kernel/include/i2c-dev.h @@ -0,0 +1,150 @@ +/* + i2c-dev.h - Part of lm_sensors, Linux kernel modules for hardware + monitoring + Copyright (c) 1998 Frodo Looijaard + + 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. +*/ + +#ifndef SENSORS_I2C_DEV_H +#define SENSORS_I2C_DEV_H + +#ifdef LM_SENSORS +#include "i2c.h" +#else /* ndef LM_SENSORS */ +#include +#endif /* def LM_SENSORS */ + +/* Some IOCTL commands are defined in */ +/* Note: 10-bit addresses are NOT supported! */ + +#define I2C_SMBUS 0x0720 + +/* This is the structure as used in the I2C_SMBUS ioctl call */ +struct i2c_smbus_data { + char read_write, + u8 command, + int size, + union smbus_data data +}; + +extern inline s32 i2c_smbus_access(int file, char read_write, u8 command, + int size, union smbus_data *data) +{ + struct i2c_smbus_data args; + int res; + + args.read_write = read_write; + args.command = command; + args.size = size; + if (data) + memcpy(&args.data,data,sizeof(union smbus_data)); + res = ioctl(f,I2C_SMBUS,&args); + if (data) + memcpy(data,&args,sizeof(union smbus_data)); + return res; +} + + +extern inline s32 i2c_smbus_write_quick(int file, u8 value) +{ + return i2c_smbus_access(file,value,0,SMBUS_QUICK,NULL); +} + +extern inline s32 i2c_smbus_read_byte(int file) +{ + union smbus_data data; + if (i2c_smbus_access(file,SMBUS_READ,0,SMBUS_BYTE,&data)) + return -1; + else + return 0x0FF & data.byte; +} + +extern inline s32 i2c_smbus_write_byte(int file, u8 value) +{ + return i2c_smbus_access(file,SMBUS_WRITE,value, SMBUS_BYTE,NULL); +} + +extern inline s32 i2c_smbus_read_byte_data(int file, u8 command) +{ + union smbus_data data; + if (i2c_smbus_access(file,SMBUS_READ,command,SMBUS_BYTE_DATA,&data)) + return -1; + else + return 0x0FF & data.byte; +} + +extern inline s32 i2c_smbus_write_byte_data(int file, u8 command, u8 value) +{ + union smbus_data data; + data.byte = value; + return i2c_smbus_access(file,SMBUS_WRITE,command,SMBUS_BYTE_DATA,&data); +} + +extern inline s32 i2c_smbus_read_word_data(int file, u8 command) +{ + union smbus_data data; + if (i2c_smbus_access(file,SMBUS_READ,command,SMBUS_WORD_DATA,&data)) + return -1; + else + return 0x0FFFF & data.word; +} + +extern inline s32 i2c_smbus_write_word_data(int file, u8 command, u16 value) +{ + union smbus_data data; + data.word = value; + return i2c_smbus_access(file,SMBUS_WRITE,command,SMBUS_WORD_DATA, &data); +} + +extern inline s32 i2c_smbus_process_call(int file, u8 command, u16 value) +{ + union smbus_data data; + data.word = value; + if (i2c_smbus_access(file,SMBUS_WRITE,command,SMBUS_PROC_CALL,&data)) + return -1; + else + return 0x0FFFF & data.word; +} + + +/* Returns the number of read bytes */ +extern inline s32 i2c_smbus_read_block_data(int file, u8 command, u8 *values) +{ + union smbus_data data; + int i; + if (i2c_smbus_access(file,SMBUS_READ,command,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]; + } +} + +extern inline s32 i2c_smbus_write_block_data(int file, u8 command, u8 length, + u8 *values) +{ + union 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,SMBUS_WRITE,command,SMBUS_BLOCK_DATA,&data); +} + +#endif diff --git a/kernel/include/smbus.h b/kernel/include/smbus.h index ce0d002b..86bbd96b 100644 --- a/kernel/include/smbus.h +++ b/kernel/include/smbus.h @@ -255,7 +255,7 @@ extern inline s32 smbus_read_block_data(struct i2c_adapter * adapter, } } -extern inline int smbus_write_block_data(struct i2c_adapter * adapter, +extern inline s32 smbus_write_block_data(struct i2c_adapter * adapter, u8 addr, u8 command, u8 length, u8 *values) { diff --git a/src/Module.mk b/src/Module.mk index f8211b59..8a9e609c 100644 --- a/src/Module.mk +++ b/src/Module.mk @@ -32,7 +32,7 @@ SRCTARGETS := $(MODULE_DIR)/smbus.o $(MODULE_DIR)/piix4.o $(MODULE_DIR)/isa.o \ $(MODULE_DIR)/i2c-dev.o SRCHEADERFILES := $(MODULE_DIR)/sensors.h $(MODULE_DIR)/isa.h \ - $(MODULE_DIR)/smbus.h + $(MODULE_DIR)/smbus.h $(MODULE_DIR)/i2c-dev.h # Include all dependency files INCLUDEFILES += $(SRCTARGETS:.o=.d) diff --git a/src/i2c-dev.h b/src/i2c-dev.h new file mode 100644 index 00000000..8eaae447 --- /dev/null +++ b/src/i2c-dev.h @@ -0,0 +1,150 @@ +/* + i2c-dev.h - Part of lm_sensors, Linux kernel modules for hardware + monitoring + Copyright (c) 1998 Frodo Looijaard + + 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. +*/ + +#ifndef SENSORS_I2C_DEV_H +#define SENSORS_I2C_DEV_H + +#ifdef LM_SENSORS +#include "i2c.h" +#else /* ndef LM_SENSORS */ +#include +#endif /* def LM_SENSORS */ + +/* Some IOCTL commands are defined in */ +/* Note: 10-bit addresses are NOT supported! */ + +#define I2C_SMBUS 0x0720 + +/* This is the structure as used in the I2C_SMBUS ioctl call */ +struct i2c_smbus_data { + char read_write, + u8 command, + int size, + union smbus_data data +}; + +extern inline s32 i2c_smbus_access(int file, char read_write, u8 command, + int size, union smbus_data *data) +{ + struct i2c_smbus_data args; + int res; + + args.read_write = read_write; + args.command = command; + args.size = size; + if (data) + memcpy(&args.data,data,sizeof(union smbus_data)); + res = ioctl(f,I2C_SMBUS,&args); + if (data) + memcpy(data,&args,sizeof(union smbus_data)); + return res; +} + + +extern inline s32 i2c_smbus_write_quick(int file, u8 value) +{ + return i2c_smbus_access(file,value,0,SMBUS_QUICK,NULL); +} + +extern inline s32 i2c_smbus_read_byte(int file) +{ + union smbus_data data; + if (i2c_smbus_access(file,SMBUS_READ,0,SMBUS_BYTE,&data)) + return -1; + else + return 0x0FF & data.byte; +} + +extern inline s32 i2c_smbus_write_byte(int file, u8 value) +{ + return i2c_smbus_access(file,SMBUS_WRITE,value, SMBUS_BYTE,NULL); +} + +extern inline s32 i2c_smbus_read_byte_data(int file, u8 command) +{ + union smbus_data data; + if (i2c_smbus_access(file,SMBUS_READ,command,SMBUS_BYTE_DATA,&data)) + return -1; + else + return 0x0FF & data.byte; +} + +extern inline s32 i2c_smbus_write_byte_data(int file, u8 command, u8 value) +{ + union smbus_data data; + data.byte = value; + return i2c_smbus_access(file,SMBUS_WRITE,command,SMBUS_BYTE_DATA,&data); +} + +extern inline s32 i2c_smbus_read_word_data(int file, u8 command) +{ + union smbus_data data; + if (i2c_smbus_access(file,SMBUS_READ,command,SMBUS_WORD_DATA,&data)) + return -1; + else + return 0x0FFFF & data.word; +} + +extern inline s32 i2c_smbus_write_word_data(int file, u8 command, u16 value) +{ + union smbus_data data; + data.word = value; + return i2c_smbus_access(file,SMBUS_WRITE,command,SMBUS_WORD_DATA, &data); +} + +extern inline s32 i2c_smbus_process_call(int file, u8 command, u16 value) +{ + union smbus_data data; + data.word = value; + if (i2c_smbus_access(file,SMBUS_WRITE,command,SMBUS_PROC_CALL,&data)) + return -1; + else + return 0x0FFFF & data.word; +} + + +/* Returns the number of read bytes */ +extern inline s32 i2c_smbus_read_block_data(int file, u8 command, u8 *values) +{ + union smbus_data data; + int i; + if (i2c_smbus_access(file,SMBUS_READ,command,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]; + } +} + +extern inline s32 i2c_smbus_write_block_data(int file, u8 command, u8 length, + u8 *values) +{ + union 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,SMBUS_WRITE,command,SMBUS_BLOCK_DATA,&data); +} + +#endif diff --git a/src/smbus.h b/src/smbus.h index ce0d002b..86bbd96b 100644 --- a/src/smbus.h +++ b/src/smbus.h @@ -255,7 +255,7 @@ extern inline s32 smbus_read_block_data(struct i2c_adapter * adapter, } } -extern inline int smbus_write_block_data(struct i2c_adapter * adapter, +extern inline s32 smbus_write_block_data(struct i2c_adapter * adapter, u8 addr, u8 command, u8 length, u8 *values) {