From 9f4acd58b1ad4c64dc5593b804b19f47a4d27204 Mon Sep 17 00:00:00 2001 From: Frodo Looijaard Date: Thu, 19 Nov 1998 15:14:01 +0000 Subject: [PATCH] piix4.c basic skeleton The real PIIX4 access code must still be written; perhaps somebody else could do this? Most of it could be copied from the old piix4.c. But check carefully what you are doing, as some things *have* changed (like the SMBUS_{BYTE,...} variables!). git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@9 7894878c-1315-0410-8ee3-d5d059ff63e0 --- kernel/Module.mk | 2 +- kernel/busses/i2c-piix4.c | 118 ++++++++++++++++++++++++++++++++++++++ kernel/include/smbus.h | 15 +++-- kernel/smbus.c | 27 ++++++++- src/Module.mk | 2 +- src/piix4.c | 118 ++++++++++++++++++++++++++++++++++++++ src/smbus.c | 27 ++++++++- src/smbus.h | 15 +++-- 8 files changed, 304 insertions(+), 20 deletions(-) create mode 100644 kernel/busses/i2c-piix4.c create mode 100644 src/piix4.c diff --git a/kernel/Module.mk b/kernel/Module.mk index 2495e5aa..cc7ced8d 100644 --- a/kernel/Module.mk +++ b/kernel/Module.mk @@ -22,7 +22,7 @@ MODULE_DIR := src # Regrettably, even 'simply expanded variables' will not put their currently # defined value verbatim into the command-list of rules... -SRCTARGETS := $(MODULE_DIR)/smbus.o +SRCTARGETS := $(MODULE_DIR)/smbus.o $(MODULE_DIR)/piix4.o # Include all dependency files include $(SRCTARGETS:.o=.d) diff --git a/kernel/busses/i2c-piix4.c b/kernel/busses/i2c-piix4.c new file mode 100644 index 00000000..2796312d --- /dev/null +++ b/kernel/busses/i2c-piix4.c @@ -0,0 +1,118 @@ +/* + smbus.c - A Linux module for reading sensor data. + 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. +*/ + +#include +#include +#include "smbus.h" + +static int piix4_init(void); +static int piix4_cleanup(void); +static int piix4_setup(void); +static s32 piix4_access(u8 addr, char read_write, + u8 command, int size, union smbus_data * data); + +#ifdef MODULE +extern int init_module(void); +extern int cleanup_module(void); +#endif /* MODULE */ + +static struct smbus_adapter piix4_adapter; +static int piix4_initialized; + +/* Detect whether a PIIX4 can be found, and initialize it, where necessary. + Return -ENODEV if not found. */ +int piix4_setup(void) +{ + /* TO BE WRITTEN! */ +} + +/* Return -1 on error. See smbus.h for more information */ +s32 piix4_access(u8 addr, char read_write, + u8 command, int size, union smbus_data * data) +{ + /* TO BE WRITTEN! */ +} + + +int piix4_init(void) +{ + int res; +#ifdef DEBUG + if (piix4_initialized) { + printk("piix4.o: Oops, piix4_init called a second time!"); + return -EBUSY; + } +#endif + piix4_initialized = 0; + if ((res = piix4_setup())) { + printk("piix4.o: PIIX4 not detected, module not inserted"); + piix4_cleanup(); + return res; + } + piix4_initialized ++; + strcpy(piix4_adapter.name,"SMBus PIIX4 adapter"); + piix4_adapter.id = ALGO_SMBUS | SMBUS_PIIX4; + piix4_adapter.algo = &smbus_algorithm; + piix4_adapter.smbus_access = &piix4_access; + if ((res = smbus_add_adapter(&piix4_adapter))) { + printk("piix4.o: smbus_add_adapter failed, module not inserted"); + piix4_cleanup(); + return res; + } + piix4_initialized++; + printk("piix4.o: PIIX4 bus detected and initialized"); + return 0; +} + +int piix4_cleanup(void) +{ + int res; + if (piix4_initialized >= 2) + { + if ((res = smbus_del_adapter(&piix4_adapter))) { + printk("piix4.o: smbus_del_adapter failed, module not removed"); + return res; + } + else + piix4_initialized--; + } + if (piix4_initialized >= 1) { + /* Undo anything piix4_setup did */ + piix4_initialized--; + } + return 0; +} + +#ifdef MODULE + +MODULE_AUTHOR("Frodo Looijaard "); +MODULE_DESCRIPTION("PIIX4 SMBus driver"); + +int init_module(void) +{ + return piix4_init(); +} + +int cleanup_module(void) +{ + return piix4_cleanup(); +} + +#endif /* MODULE */ + diff --git a/kernel/include/smbus.h b/kernel/include/smbus.h index bbe2334b..9aa49b13 100644 --- a/kernel/include/smbus.h +++ b/kernel/include/smbus.h @@ -109,9 +109,9 @@ struct smbus_adapter { int timeout; int retries; - /* Here ended i2c_algorithm */ - s32 (* smbus_access) (__u8 addr, char read_write, - __u8 command, int size, union smbus_data * data); + /* Here ended i2c_adapter */ + s32 (* smbus_access) (u8 addr, char read_write, + u8 command, int size, union smbus_data * data); }; /* We need to mark SMBus algorithms in the algorithm structure. @@ -121,11 +121,14 @@ struct smbus_adapter { this flag is set! */ #define ALGO_SMBUS 0x40000 +/* SMBus Adapter ids */ +#define SMBUS_PIIX4 1 + /* This union is used within smbus_access routines */ union smbus_data { - __u8 byte; - __u16 word; - __u8 block[32]; + u8 byte; + u16 word; + u8 block[32]; }; /* smbus_access read or write markers */ diff --git a/kernel/smbus.c b/kernel/smbus.c index ab592806..3d847cb2 100644 --- a/kernel/smbus.c +++ b/kernel/smbus.c @@ -19,6 +19,13 @@ #include #include + +#ifdef SPINLOCK +#include +#else +#include +#endif + #include "smbus.h" static s32 smbus_access_i2c (struct smbus_adapter * adapter, u8 addr, @@ -57,14 +64,28 @@ struct smbus_algorithm smbus_algorithm = { /* OK, so you want to access a bus using the SMBus protocols. Well, it either is registered as a SMBus-only adapter (like the PIIX4), or we need to - simulate the SMBus commands using the i2c access routines. */ + simulate the SMBus commands using the i2c access routines. + We do all locking here, so you can ignore that in the adapter-specific + smbus_accesss routine. */ s32 smbus_access (struct smbus_adapter * adapter, u8 addr, char read_write, u8 command, int size, union smbus_data * data) { + int res; +#ifdef SPINLOCK + spin_lock_irqsave(&adapter->lock,adapter->lockflags); +#else + down(&adapter->lock); +#endif if (adapter->id & ALGO_SMBUS) - return adapter->smbus_access(addr,read_write,command,size,data); + res = adapter->smbus_access(addr,read_write,command,size,data); else - return smbus_access_i2c(adapter,addr,read_write,command,size,data); + res = smbus_access_i2c(adapter,addr,read_write,command,size,data); +#ifdef SPINLOCK + spin_unlock_irqrestore(&adapter->lock,adapter->lockflags); +#else + up(&adapter->lock); +#endif + return res; } /* Simulate a SMBus command using the i2c protocol diff --git a/src/Module.mk b/src/Module.mk index 2495e5aa..cc7ced8d 100644 --- a/src/Module.mk +++ b/src/Module.mk @@ -22,7 +22,7 @@ MODULE_DIR := src # Regrettably, even 'simply expanded variables' will not put their currently # defined value verbatim into the command-list of rules... -SRCTARGETS := $(MODULE_DIR)/smbus.o +SRCTARGETS := $(MODULE_DIR)/smbus.o $(MODULE_DIR)/piix4.o # Include all dependency files include $(SRCTARGETS:.o=.d) diff --git a/src/piix4.c b/src/piix4.c new file mode 100644 index 00000000..2796312d --- /dev/null +++ b/src/piix4.c @@ -0,0 +1,118 @@ +/* + smbus.c - A Linux module for reading sensor data. + 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. +*/ + +#include +#include +#include "smbus.h" + +static int piix4_init(void); +static int piix4_cleanup(void); +static int piix4_setup(void); +static s32 piix4_access(u8 addr, char read_write, + u8 command, int size, union smbus_data * data); + +#ifdef MODULE +extern int init_module(void); +extern int cleanup_module(void); +#endif /* MODULE */ + +static struct smbus_adapter piix4_adapter; +static int piix4_initialized; + +/* Detect whether a PIIX4 can be found, and initialize it, where necessary. + Return -ENODEV if not found. */ +int piix4_setup(void) +{ + /* TO BE WRITTEN! */ +} + +/* Return -1 on error. See smbus.h for more information */ +s32 piix4_access(u8 addr, char read_write, + u8 command, int size, union smbus_data * data) +{ + /* TO BE WRITTEN! */ +} + + +int piix4_init(void) +{ + int res; +#ifdef DEBUG + if (piix4_initialized) { + printk("piix4.o: Oops, piix4_init called a second time!"); + return -EBUSY; + } +#endif + piix4_initialized = 0; + if ((res = piix4_setup())) { + printk("piix4.o: PIIX4 not detected, module not inserted"); + piix4_cleanup(); + return res; + } + piix4_initialized ++; + strcpy(piix4_adapter.name,"SMBus PIIX4 adapter"); + piix4_adapter.id = ALGO_SMBUS | SMBUS_PIIX4; + piix4_adapter.algo = &smbus_algorithm; + piix4_adapter.smbus_access = &piix4_access; + if ((res = smbus_add_adapter(&piix4_adapter))) { + printk("piix4.o: smbus_add_adapter failed, module not inserted"); + piix4_cleanup(); + return res; + } + piix4_initialized++; + printk("piix4.o: PIIX4 bus detected and initialized"); + return 0; +} + +int piix4_cleanup(void) +{ + int res; + if (piix4_initialized >= 2) + { + if ((res = smbus_del_adapter(&piix4_adapter))) { + printk("piix4.o: smbus_del_adapter failed, module not removed"); + return res; + } + else + piix4_initialized--; + } + if (piix4_initialized >= 1) { + /* Undo anything piix4_setup did */ + piix4_initialized--; + } + return 0; +} + +#ifdef MODULE + +MODULE_AUTHOR("Frodo Looijaard "); +MODULE_DESCRIPTION("PIIX4 SMBus driver"); + +int init_module(void) +{ + return piix4_init(); +} + +int cleanup_module(void) +{ + return piix4_cleanup(); +} + +#endif /* MODULE */ + diff --git a/src/smbus.c b/src/smbus.c index ab592806..3d847cb2 100644 --- a/src/smbus.c +++ b/src/smbus.c @@ -19,6 +19,13 @@ #include #include + +#ifdef SPINLOCK +#include +#else +#include +#endif + #include "smbus.h" static s32 smbus_access_i2c (struct smbus_adapter * adapter, u8 addr, @@ -57,14 +64,28 @@ struct smbus_algorithm smbus_algorithm = { /* OK, so you want to access a bus using the SMBus protocols. Well, it either is registered as a SMBus-only adapter (like the PIIX4), or we need to - simulate the SMBus commands using the i2c access routines. */ + simulate the SMBus commands using the i2c access routines. + We do all locking here, so you can ignore that in the adapter-specific + smbus_accesss routine. */ s32 smbus_access (struct smbus_adapter * adapter, u8 addr, char read_write, u8 command, int size, union smbus_data * data) { + int res; +#ifdef SPINLOCK + spin_lock_irqsave(&adapter->lock,adapter->lockflags); +#else + down(&adapter->lock); +#endif if (adapter->id & ALGO_SMBUS) - return adapter->smbus_access(addr,read_write,command,size,data); + res = adapter->smbus_access(addr,read_write,command,size,data); else - return smbus_access_i2c(adapter,addr,read_write,command,size,data); + res = smbus_access_i2c(adapter,addr,read_write,command,size,data); +#ifdef SPINLOCK + spin_unlock_irqrestore(&adapter->lock,adapter->lockflags); +#else + up(&adapter->lock); +#endif + return res; } /* Simulate a SMBus command using the i2c protocol diff --git a/src/smbus.h b/src/smbus.h index bbe2334b..9aa49b13 100644 --- a/src/smbus.h +++ b/src/smbus.h @@ -109,9 +109,9 @@ struct smbus_adapter { int timeout; int retries; - /* Here ended i2c_algorithm */ - s32 (* smbus_access) (__u8 addr, char read_write, - __u8 command, int size, union smbus_data * data); + /* Here ended i2c_adapter */ + s32 (* smbus_access) (u8 addr, char read_write, + u8 command, int size, union smbus_data * data); }; /* We need to mark SMBus algorithms in the algorithm structure. @@ -121,11 +121,14 @@ struct smbus_adapter { this flag is set! */ #define ALGO_SMBUS 0x40000 +/* SMBus Adapter ids */ +#define SMBUS_PIIX4 1 + /* This union is used within smbus_access routines */ union smbus_data { - __u8 byte; - __u16 word; - __u8 block[32]; + u8 byte; + u16 word; + u8 block[32]; }; /* smbus_access read or write markers */