patch-2.4.0-test10 linux/drivers/sbus/char/envctrl.c

Next file: linux/drivers/sbus/char/sab82532.c
Previous file: linux/drivers/sbus/char/bpp.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.4.0-test9/linux/drivers/sbus/char/envctrl.c linux/drivers/sbus/char/envctrl.c
@@ -1,23 +1,29 @@
-/* $Id: envctrl.c,v 1.17 2000/06/19 06:24:47 davem Exp $
+/* $Id: envctrl.c,v 1.18 2000/10/17 16:20:35 davem Exp $
  * envctrl.c: Temperature and Fan monitoring on Machines providing it.
  *
  * Copyright (C) 1998  Eddie C. Dost  (ecd@skynet.be)
+ * Copyright (C) 2000  Vinh Truong    (vinh.truong@eng.sun.com)
+ * VT - The implementation is to support Sun Microelectronics (SME) platform
+ *      environment monitoring.  SME platforms use pcf8584 as the i2c bus 
+ *      controller to access pcf8591 (8-bit A/D and D/A converter) and 
+ *      pcf8571 (256 x 8-bit static low-voltage RAM with I2C-bus interface).
+ *      At board level, it follows SME Firmware I2C Specification. Reference:
+ * 	http://www-eu2.semiconductors.com/pip/PCF8584P
+ * 	http://www-eu2.semiconductors.com/pip/PCF8574AP
+ * 	http://www-eu2.semiconductors.com/pip/PCF8591P
+ * 
  */
 
-#include <linux/version.h>
 #include <linux/config.h>
 #include <linux/module.h>
-
-#define __KERNEL_SYSCALLS__
 #include <linux/sched.h>
-#include <linux/unistd.h>
 #include <linux/errno.h>
 #include <linux/delay.h>
 #include <linux/ioport.h>
 #include <linux/init.h>
-#include <linux/malloc.h>
 #include <linux/miscdevice.h>
-#include <linux/smp_lock.h>
+#include <linux/mm.h>
+#include <linux/malloc.h>
 
 #include <asm/ebus.h>
 #include <asm/uaccess.h>
@@ -25,18 +31,6 @@
 
 #define ENVCTRL_MINOR	162
 
-
-#undef U450_SUPPORT		/* might fry you machine, careful here !!! */
-
-
-#undef DEBUG
-#undef DEBUG_BUS_SCAN
-
-
-#if LINUX_VERSION_CODE < KERNEL_VERSION(2,2,0)
-#define schedule_timeout(a) { current->timeout = jiffies + (a); schedule(); }
-#endif
-
 #define PCF8584_ADDRESS	0x55
 
 #define CONTROL_PIN	0x80
@@ -71,1532 +65,879 @@
 #define CLK_8		0x18
 #define CLK_12		0x1c
 
+#define OBD_SEND_START	0xc5    /* value to generate I2c_bus START condition */
+#define OBD_SEND_STOP 	0xc3    /* value to generate I2c_bus STOP condition */
 
-#define I2C_WRITE	0x00
-#define I2C_READ	0x01
-
-/* PCF8584 register offsets */
-#define I2C_DATA	0x00UL
-#define I2C_CSR		0x01UL
-#define I2C_REG_SIZE	0x02UL
-
-struct i2c_device {
-	unsigned char		addr;
-	struct i2c_device	*next;
-};
-
-static unsigned long i2c_regs;
-static struct i2c_device  *i2c_devices;
-
-static int errno;
-
-#define MAX_TEMPERATURE		111
-#define MAX_FAN_SPEED		63
-
-
-/*
- * UltraAXi constants.
+/* Monitor type of i2c child device.
+ * Firmware definitions.
  */
-#define AXI_THERM_ADDR		0x9e
-#define AXI_THERM_PORT_CPU	0
-#define AXI_THERM_PORT_MOD	1
-#define AXI_THERM_PORT_PCI	2
-#define AXI_THERM_PORT_DISK	3
-
-#define AXI_FAN_ADDR		0x4e
-#define AXI_FAN_PORT_FRONT	0
-#define AXI_FAN_PORT_BACK	1
+#define PCF8584_MAX_CHANNELS            8
+#define PCF8584_FANSTAT_TYPE            3  /* fan status monitor */
+#define PCF8584_VOLTAGE_TYPE            2  /* voltage monitor    */
+#define PCF8584_TEMP_TYPE	        1  /* temperature monitor*/
 
-#define AXI_PIO_ADDR		0x70
-
-/*
- * Ultra 450 constants.
+/* Monitor type of i2c child device.
+ * Driver definitions.
  */
-#define U450_FAN_ADDR		0x4e
-#define U450_FAN_PORT_CPU	0
-#define U450_FAN_PORT_PS	1
-
-#define U450_PIO_ADDR		0x70
-#define U450_TIMER_ADDR		0xa0
-
-static unsigned char
-axi_cpu_temp_table[256] =
-{
-	0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69, 0x67,
-	0x66, 0x65, 0x64, 0x63, 0x61, 0x60, 0x5f, 0x5e,
-	0x5d, 0x5b, 0x5a, 0x59, 0x58, 0x57, 0x55, 0x54,
-	0x53, 0x52, 0x50, 0x4f, 0x4e, 0x4d, 0x4c, 0x4a,
-	0x49, 0x48, 0x47, 0x46, 0x44, 0x43, 0x42, 0x41,
-	0x40, 0x3e, 0x3d, 0x3c, 0x3c, 0x3b, 0x3b, 0x3a,
-	0x3a, 0x39, 0x39, 0x38, 0x38, 0x37, 0x37, 0x36,
-	0x36, 0x35, 0x35, 0x34, 0x34, 0x33, 0x33, 0x32,
-	0x32, 0x31, 0x31, 0x30, 0x30, 0x2f, 0x2f, 0x2e,
-	0x2d, 0x2d, 0x2c, 0x2c, 0x2b, 0x2b, 0x2a, 0x2a,
-	0x29, 0x29, 0x28, 0x28, 0x27, 0x27, 0x26, 0x26,
-	0x25, 0x25, 0x24, 0x24, 0x23, 0x23, 0x22, 0x22,
-	0x21, 0x21, 0x20, 0x20, 0x1f, 0x1f, 0x1e, 0x1e,
-	0x1e, 0x1e, 0x1d, 0x1d, 0x1d, 0x1d, 0x1c, 0x1c,
-	0x1c, 0x1c, 0x1b, 0x1b, 0x1b, 0x1b, 0x1a, 0x1a,
-	0x1a, 0x1a, 0x1a, 0x19, 0x19, 0x19, 0x19, 0x18,
-	0x18, 0x18, 0x18, 0x17, 0x17, 0x17, 0x17, 0x16,
-	0x16, 0x16, 0x16, 0x16, 0x16, 0x15, 0x15, 0x15,
-	0x15, 0x14, 0x14, 0x14, 0x14, 0x13, 0x13, 0x13,
-	0x13, 0x12, 0x12, 0x12, 0x12, 0x12, 0x11, 0x11,
-	0x11, 0x11, 0x10, 0x10, 0x10, 0x10, 0x0f, 0x0f,
-	0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
-	0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b,
-	0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
-	0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08,
-	0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06, 0x06,
-	0x06, 0x05, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04,
-	0x04, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02,
-	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
-};
-
-static unsigned char
-axi_mod_temp_table[256] =
-{
-	0x65, 0x64, 0x63, 0x62, 0x61, 0x60, 0x5f, 0x5e,
-	0x5d, 0x5c, 0x5b, 0x5a, 0x59, 0x58, 0x57, 0x56,
-	0x55, 0x54, 0x53, 0x52, 0x51, 0x50, 0x4f, 0x4e,
-	0x4d, 0x4c, 0x4b, 0x4a, 0x49, 0x48, 0x47, 0x46,
-	0x45, 0x44, 0x43, 0x42, 0x41, 0x40, 0x3f, 0x3e,
-	0x3d, 0x3c, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x39,
-	0x39, 0x38, 0x38, 0x37, 0x37, 0x36, 0x36, 0x35,
-	0x35, 0x35, 0x34, 0x34, 0x33, 0x33, 0x32, 0x32,
-	0x31, 0x31, 0x30, 0x30, 0x2f, 0x2f, 0x2e, 0x2e,
-	0x2e, 0x2d, 0x2d, 0x2c, 0x2c, 0x2b, 0x2b, 0x2a,
-	0x2a, 0x29, 0x29, 0x29, 0x28, 0x28, 0x27, 0x27,
-	0x26, 0x26, 0x25, 0x25, 0x24, 0x24, 0x23, 0x23,
-	0x23, 0x22, 0x22, 0x21, 0x21, 0x20, 0x20, 0x1f,
-	0x1f, 0x1e, 0x1e, 0x1e, 0x1d, 0x1d, 0x1d, 0x1d,
-	0x1d, 0x1c, 0x1c, 0x1c, 0x1c, 0x1b, 0x1b, 0x1b,
-	0x1b, 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x19, 0x19,
-	0x19, 0x19, 0x18, 0x18, 0x18, 0x18, 0x17, 0x17,
-	0x17, 0x17, 0x17, 0x16, 0x16, 0x16, 0x16, 0x15,
-	0x15, 0x15, 0x15, 0x14, 0x14, 0x14, 0x14, 0x13,
-	0x13, 0x13, 0x13, 0x13, 0x12, 0x12, 0x12, 0x12,
-	0x11, 0x11, 0x11, 0x11, 0x10, 0x10, 0x10, 0x10,
-	0x10, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,
-	0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c,
-	0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
-	0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08,
-	0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
-	0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05,
-	0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
-	0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
-	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
-};
-
-static unsigned char
-axi_fan_speeds[112] =
-{
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x20,
-	0x22, 0x23, 0x25, 0x27, 0x28, 0x2a, 0x2b, 0x2d,
-	0x2f, 0x30, 0x32, 0x33, 0x35, 0x37, 0x38, 0x3a,
-	0x3b, 0x3d, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f
-};
-
-
-struct therm_regs {
-	u32	addr;
-	u32	port;
-	u32	min_temp;
-	u32	warning;
-	u32	shutdown;
-	u32	num;
-	u32	den;
-};
-
-struct thermistor {
-	char			name[8];
-	struct therm_regs	regs;
-	unsigned char		(*temperature) (struct thermistor *);
-	unsigned char		(*fan_speed) (struct thermistor *);
-	struct thermistor	*next;		/* all thermistors */
-	struct thermistor	*chain;		/* thermistors for one fan */
-};
-
-struct fan_regs {
-	u32	addr;
-	u32	port;
-};
-
-struct fan {
-	char			name[8];
-	struct fan_regs		regs;
-	int			(*set_speed)(struct fan *, unsigned char value);
-	int			(*check_failure)(struct fan *);
-	unsigned char		value;
-	struct thermistor	*monitor;
-	struct fan		*next;
-};
+#define ENVCTRL_NOMON			0
+#define ENVCTRL_CPUTEMP_MON		1    /* cpu temperature monitor */
+#define ENVCTRL_CPUVOLTAGE_MON	  	2    /* voltage monitor         */
+#define ENVCTRL_FANSTAT_MON  		3    /* fan status monitor      */
+#define ENVCTRL_ETHERTEMP_MON		4    /* ethernet temperarture */
+					     /* monitor                     */
+#define ENVCTRL_VOLTAGESTAT_MON	  	5    /* voltage status monitor  */
+#define ENVCTRL_MTHRBDTEMP_MON		6    /* motherboard temperature */
+#define ENVCTRL_SCSITEMP_MON		7    /* scsi temperarture */
 
+/* Child device type.
+ * Driver definitions.
+ */
+#define I2C_ADC				0    /* pcf8591 */
+#define I2C_GPIO			1    /* pcf8571 */
 
-struct environment {
-	struct thermistor	*thermistors;
-	struct fan		*fans;
-	unsigned char		*cpu_temp_table;
-	unsigned char		*cpu_fan_speeds;
-	unsigned char		*ps_temp_table;
-	unsigned char		*ps_fan_speeds;
-	void			(*enable) (struct environment *);
-	void			(*disable) (struct environment *);
-	void			(*keep_alive) (struct environment *);
-	int			interval;
-	pid_t			kenvd_pid;
-	wait_queue_head_t	kenvd_wait;
-	int			terminate;
+/* Data read from child device may need to decode
+ * through a data table and a scale.
+ * Translation type as defined by firmware.
+ */
+#define ENVCTRL_TRANSLATE_NO		0
+#define ENVCTRL_TRANSLATE_PARTIAL	1
+#define ENVCTRL_TRANSLATE_COMBINED	2
+#define ENVCTRL_TRANSLATE_FULL		3     /* table[data] */
+#define ENVCTRL_TRANSLATE_SCALE		4     /* table[data]/scale */
+
+/* Driver miscellaneous definitions. */
+#define ENVCTRL_MAX_CPU			4
+#define CHANNEL_DESC_SZ			256
+
+struct pcf8584_reg {
+	 unsigned char data;
+	 unsigned char csr;
 };
 
+/* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
+ * Property of a port or channel as defined by the firmware.
+ */
+struct pcf8584_channel {
+        unsigned char chnl_no;
+        unsigned char io_direction;
+        unsigned char type;
+        unsigned char last;
+};
+
+/* Each child device may have one or more tables of bytes to help decode
+ * data. Table property as defined by the firmware.
+ */ 
+struct pcf8584_tblprop {
+        unsigned int type;
+        unsigned int scale;  
+        unsigned int offset; /* offset from the beginning of the table */
+        unsigned int size;
+};
+
+/* i2c child */
+struct i2c_child_t {
+	/* Either ADC or GPIO. */
+	unsigned char i2ctype;
+        unsigned long addr;    
+        struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
+
+	/* Channel info. */ 
+	unsigned int total_chnls;	/* Number of monitor channels. */
+	unsigned char fan_mask;		/* Byte mask for fan status channels. */
+	unsigned char voltage_mask;	/* Byte mask for voltage status channels. */
+        struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
+
+	/* Properties of all monitor channels. */
+	unsigned int total_tbls;	/* Number of monitor tables. */
+        char *tables;			/* Pointer to table(s). */
+	char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
+	char mon_type[PCF8584_MAX_CHANNELS];
+};
+
+volatile static struct pcf8584_reg *i2c = NULL;
+static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
+static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
+static unsigned int warning_temperature = 0;
+static unsigned int shutdown_temperature = 0;
+static char read_cpu;
 
-static struct environment	envctrl;
-
-
-#ifdef DEBUG_BUS_SCAN
-struct i2c_addr_map {
-	unsigned char addr;
-	unsigned char mask;
-	char *name;
-};
+/* Forward declarations. */
+static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
 
-static struct i2c_addr_map devmap[] = {
-	{ 0x70, 0xf0, "PCF8574A" },
-	{ 0x40, 0xf0, "TDA8444" },
-	{ 0x90, 0xf0, "PCF8591" },
-	{ 0xa0, 0xf0, "PCF8583" },
-};
-#define NR_DEVMAP (sizeof(devmap) / sizeof(devmap[0]))
-#endif
-
-static __inline__ int
-PUT_DATA(unsigned long data, char *buffer, int user)
+/* Function description: Read a byte from an i2c controller register.
+ * Return: A byte from the passed in address.
+ */
+static inline unsigned char envctrl_readb(volatile unsigned char *p)
 {
-	if (user) {
-		u8 tmp = readb(data);
-		if (put_user(tmp, buffer))
-			return -EFAULT;
-	} else {
-		*buffer = readb(data);
-	}
-	return 0;
+	return readb(p);
 }
 
-static __inline__ int
-GET_DATA(unsigned long data, const char *buffer, int user)
+/* Function description: Write a byte to an i2c controller register.
+ * Return: Nothing.
+ */
+static inline void envctrl_writeb(unsigned char val, volatile unsigned char *p)
 {
-	if (user) {
-		u8 tmp;
-		if (get_user(tmp, buffer))
-			return -EFAULT;
-		writeb(tmp, data);
-	} else {
-		writeb(*buffer, data);
-	}
-	return 0;
+	writeb(val, p);
 }
 
-
-static int
-i2c_read(unsigned char dev, char *buffer, int len, int user)
+/* Function Description: Test the PIN bit (Pending Interrupt Not) 
+ * 			 to test when serial transmission is completed .
+ * Return : None.
+ */
+static void envtrl_i2c_test_pin(void)
 {
-	unsigned char dummy;
-	unsigned char stat;
-	int error = -ENODEV;
-	int count = 0;
-
-	writeb((dev & 0xfe) | I2C_READ, i2c_regs + I2C_DATA);
-
-	while (!(readb(i2c_regs + I2C_CSR) & STATUS_BB))
-		udelay(1);
-
-	writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_STA | CONTROL_ACK,
-	       i2c_regs + I2C_CSR);
+	int limit = 1000000;
 
-	do {
-		udelay(1);
-		while ((stat = readb(i2c_regs + I2C_CSR)) & STATUS_PIN)
-			udelay(1);
-
-		if (stat & STATUS_LRB)
-			goto stop;
-
-		error = 0;
-		if (len == 0) {
-			count--;
+	while (--limit > 0) {
+		if (!(envctrl_readb(&i2c->csr) & STATUS_PIN)) 
 			break;
-		}
-
-		if (count == (len - 1))
-			break;
-
-		if (count++ > 0) {
-			error = PUT_DATA(i2c_regs + I2C_DATA, buffer++, user);
-			if (error)
-				break;
-		} else
-			dummy = readb(i2c_regs + I2C_DATA);
-	} while (1);
-
-	writeb(CONTROL_ES0, i2c_regs + I2C_CSR);
-	if (!error && (count++ > 0))
-		error = PUT_DATA(i2c_regs + I2C_DATA, buffer++, user);
-	else
-		dummy = readb(i2c_regs + I2C_DATA);
-
-	udelay(1);
-	while ((stat = readb(i2c_regs + I2C_CSR)) & STATUS_PIN)
 		udelay(1);
+	} 
 
-stop:
-	writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_STO | CONTROL_ACK,
-	       i2c_regs + I2C_CSR);
-	if (!error && (count++ > 0))
-		error = PUT_DATA(i2c_regs + I2C_DATA, buffer++, user);
-	else
-		dummy = readb(i2c_regs + I2C_DATA);
-
-	if (error)
-		return error;
-	return count - 1;
+	if (limit <= 0)
+		printk(KERN_INFO "envctrl: Pin status will not clear.\n");
 }
 
-static int
-i2c_write(unsigned char dev, const char *buffer, int len, int user)
-{
-	int error = -ENODEV;
-	int count = 0;
-	int timeout;
-
-	timeout = 1000000;
-	while (!(readb(i2c_regs + I2C_CSR) & STATUS_BB) && --timeout)
-		udelay(1);
-	if (!timeout) {
-		printk("%s [%d]: TIMEOUT\n", __FUNCTION__, __LINE__);
-		return -ENODEV;
-	}
-
-	writeb((dev & 0xfe) | I2C_WRITE, i2c_regs + I2C_DATA);
-	writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_STA | CONTROL_ACK,
-	       i2c_regs + I2C_CSR);
-
-	do {
-		unsigned char stat;
-
-		udelay(1);
-		timeout = 1000000;
-		while (((stat = readb(i2c_regs + I2C_CSR)) & STATUS_PIN) && --timeout)
-			udelay(1);
-
-		if (!timeout) {
-			printk("%s [%d]: TIMEOUT\n", __FUNCTION__, __LINE__);
-			break;
-		}
-
-		if (stat & STATUS_LRB)
-			break;
-
-		error = count;
-		if (count == len)
-			break;
-
-		error = GET_DATA(i2c_regs + I2C_DATA, buffer++, user);
-		if (error)
-			break;
-
-		count++;
-	} while (1);
-
-	writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_STO | CONTROL_ACK, 
-	       i2c_regs + I2C_CSR);
-	return error;
-}
-
-#ifdef U450_SUPPORT
-static int
-i2c_write_read(unsigned char dev, char *outbuf, int outlen,
-				  char *inbuf, int inlen, int user)
+/* Function Description: Test busy bit.
+ * Return : None.
+ */
+static void envctrl_i2c_test_bb(void)
 {
-	unsigned char dummy;
-	unsigned char stat;
-	int error = -ENODEV;
-	int count = 0;
-
-	while (!(readb(i2c_regs + I2C_CSR) & STATUS_BB))
-		udelay(1);
-
-	writeb((dev & 0xfe) | I2C_WRITE, i2c_regs + I2C_DATA);
-	writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_STA | CONTROL_ACK, 
-	       i2c_regs + I2C_CSR);
-
-	do {
-		unsigned char stat;
-
-		udelay(1);
-		while ((stat = readb(i2c_regs + I2C_CSR)) & STATUS_PIN)
-			udelay(1);
-
-		if (stat & STATUS_LRB)
-			break;
-
-		error = count;
-		if (count == outlen)
-			break;
-
-		error = GET_DATA(i2c_regs + I2C_DATA, outbuf++, user);
-		if (error)
-			break;
+	int limit = 1000000;
 
-		count++;
-	} while (1);
-
-	if (error < 0) {
-		writeb(CONTROL_PIN | CONTROL_ES0 |
-		       CONTROL_STO | CONTROL_ACK, i2c_regs + I2C_CSR);
-		return error;
-	}
-
-	writeb(CONTROL_ES0 | CONTROL_STA | CONTROL_ACK, i2c_regs + I2C_CSR);
-	udelay(1);
-	writeb((dev & 0xfe) | I2C_READ, i2c_regs + I2C_DATA);
-
-	count = 0;
-	do {
-		udelay(1);
-		while ((stat = readb(i2c_regs + I2C_CSR)) & STATUS_PIN)
-			udelay(1);
-
-		if (stat & STATUS_LRB)
-			goto stop;
-
-		error = 0;
-		if (inlen == 0) {
-			count--;
+	while (--limit > 0) {
+		/* Busy bit 0 means busy. */
+		if (envctrl_readb(&i2c->csr) & STATUS_BB)
 			break;
-		}
-
-		if (count == (inlen - 1))
-			break;
-
-		if (count++ > 0) {
-			error = PUT_DATA(i2c_regs + I2C_DATA, inbuf++, user);
-			if (error)
-				break;
-		} else
-			dummy = readb(i2c_regs + I2C_DATA);
-	} while (1);
-
-	writeb(CONTROL_ES0, i2c_regs + I2C_CSR);
-	if (!error && (count++ > 0))
-		error = PUT_DATA(i2c_regs + I2C_DATA, inbuf++, user);
-	else
-		dummy = readb(i2c_regs + I2C_DATA);
-
-	udelay(1);
-	while ((stat = readb(i2c_regs + I2C_CSR)) & STATUS_PIN)
 		udelay(1);
+	} 
 
-stop:
-	writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_STO | CONTROL_ACK,
-	       i2c_regs + I2C_CSR);
-	if (!error && (count++ > 0))
-		error = PUT_DATA(i2c_regs + I2C_DATA, inbuf++, user);
-	else
-		dummy = readb(i2c_regs + I2C_DATA);
-
-	if (error)
-		return error;
-	return count - 1;
-}
-#endif /* U450_SUPPORT */
-
-static struct i2c_device *
-i2c_find_device(unsigned char addr)
-{
-	struct i2c_device *dev;
-
-	for (dev = i2c_devices; dev; dev = dev->next) {
-		if (dev->addr == addr)
-			return dev;
-	}
-	return 0;
+	if (limit <= 0)
+		printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
 }
 
-static void
-i2c_free_devices(void)
+/* Function Description: Send the adress for a read access.
+ * Return : 0 if not acknowledged, otherwise acknowledged.
+ */
+static int envctrl_i2c_read_addr(unsigned char addr)
 {
-	struct i2c_device *dev;
+	envctrl_i2c_test_bb();
 
-	dev = i2c_devices;
-	while (dev) {
-		i2c_devices = dev->next;
-		kfree(dev);
-		dev = i2c_devices;
-	}
-}
+	/* Load address. */
+	envctrl_writeb(addr + 1, &i2c->data);
 
-static __init int i2c_scan_bus(void)
-{
-	struct i2c_device *dev, **last;
-	unsigned int addr;
-	int count = 0;
-
-	last = &i2c_devices;
-	for (addr = 0; addr < 256; addr += 2) {
-		if (i2c_write(addr, 0, 0, 0) == 0) {
-#ifdef DEBUG_BUS_SCAN
-			int i;
-			for (i = 0; i < NR_DEVMAP; i++)
-				if ((addr & devmap[i].mask) == devmap[i].addr)
-					break;
-			printk("envctrl: i2c device at %02x: %s\n", addr,
-			       i < NR_DEVMAP ? devmap[i].name : "unknown");
-#endif
+	envctrl_i2c_test_bb();
 
-			dev = kmalloc(sizeof(struct i2c_device), GFP_KERNEL);
-			if (!dev) {
-				printk("i2c: can't alloc i2c_device\n");
-				i2c_free_devices();
-				return -ENOMEM;
-			}
-			memset(dev, 0, sizeof(struct i2c_device));
+	envctrl_writeb(OBD_SEND_START, &i2c->csr);
 
-			dev->addr = addr;
+	/* Wait for PIN. */
+	envtrl_i2c_test_pin();
 
-			*last = dev;
-			last = &dev->next;
-
-			count++;
-		}
-	}
-	if (!count) {
-		printk("%s: no devices found\n", __FUNCTION__);
-		return -ENODEV;
+	/* CSR 0 means acknowledged. */
+	if (!(envctrl_readb(&i2c->csr) & STATUS_LRB)) {
+		return envctrl_readb(&i2c->data);
+	} else {
+		envctrl_writeb(OBD_SEND_STOP, &i2c->csr);
+		return 0;
 	}
-	return 0;
 }
 
-
-static int
-read_8591(unsigned char dev, unsigned char offset, unsigned char *value)
+/* Function Description: Send the adress for write mode.  
+ * Return : None.
+ */
+static void envctrl_i2c_write_addr(unsigned char addr)
 {
-	unsigned char data[2];
+	envctrl_i2c_test_bb();
+	envctrl_writeb(addr, &i2c->data);
 
-	data[0] = 0x40 | offset;
-	if (i2c_write(dev, data, 1, 0) != 1)
-		return -1;
-	if (i2c_read(dev, data, 2, 0) != 2)
-		return -1;
-	*value = data[1];
-	return 0;
+	/* Generate Start condition. */
+	envctrl_writeb(OBD_SEND_START, &i2c->csr);
 }
 
-static int
-write_8444(unsigned char dev, unsigned char offset, unsigned char value)
+/* Function Description: Read 1 byte of data from addr 
+ *			 set by envctrl_i2c_read_addr() 
+ * Return : Data from address set by envctrl_i2c_read_addr().
+ */
+static unsigned char envctrl_i2c_read_data(void)
 {
-	unsigned char data[2];
-
-	data[0] = offset;
-	data[1] = value;
-	if (i2c_write(dev, data, 2, 0) != 2)
-		return -1;
-	return 0;
+	envtrl_i2c_test_pin();
+	envctrl_writeb(CONTROL_ES0, &i2c->csr);  /* Send neg ack. */
+	return envctrl_readb(&i2c->data);
 }
 
-#ifdef U450_SUPPORT
-static int
-read_8583(unsigned char dev, unsigned char offset, unsigned char *value)
+/* Function Description: Instruct the device which port to read data from.  
+ * Return : None.
+ */
+static void envctrl_i2c_write_data(unsigned char port)
 {
-	unsigned char data;
-
-	data = offset;
-	if (i2c_write_read(dev, &data, 1, &data, 1, 0) != 1)
-		return -1;
-	*value = data;
-	return 0;
+	envtrl_i2c_test_pin();
+	envctrl_writeb(port, &i2c->data);
 }
 
-static int
-write_8583(unsigned char dev, unsigned char offset, unsigned char value)
+/* Function Description: Generate Stop condition after last byte is sent.
+ * Return : None.
+ */
+static void envctrl_i2c_stop(void)
 {
-	unsigned char data[2];
-
-	data[0] = offset;
-	data[1] = value;
-	if (i2c_write(dev, data, 2, 0) != 2)
-		return -1;
-	return 0;
+	envtrl_i2c_test_pin();
+	envctrl_writeb(OBD_SEND_STOP, &i2c->csr);
 }
-#endif /* U450_SUPPORT */
 
-struct thermistor *
-find_thermistor(const char *name, struct thermistor *from)
+/* Function Description: Read adc device.
+ * Return : Data at address and port.
+ */
+static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
 {
-	int n;
+	/* Send address. */
+	envctrl_i2c_write_addr(addr);
 
-	if (!from)
-		from = envctrl.thermistors;
-	else
-		from = from->next;
+	/* Setup port to read. */
+	envctrl_i2c_write_data(port);
+	envctrl_i2c_stop();
 
-	n = strlen(name);
-	while (from && strncmp(from->name, name, n))
-		from = from->next;
+	/* Read port. */
+	envctrl_i2c_read_addr(addr);
 
-	return from;
-}
+	/* Do a single byte read and send stop. */
+	envctrl_i2c_read_data();
+	envctrl_i2c_stop();
 
-void
-check_temperatures(struct environment *env)
-{
-	struct thermistor *t;
-
-	for (t = env->thermistors; t; t = t->next) {
-#ifdef DEBUG
-		printk("Thermistor `%s' [%02x:%d]: "
-		       "%d C (%d C, %d C)\n",
-		       t->name, t->regs.addr, t->regs.port,
-		       t->temperature(t), t->regs.warning, t->regs.shutdown);
-#endif
-
-		/*
-		 * Implement slow-down or shutdown here...
-		 */
-	}
+	return envctrl_readb(&i2c->data);
 }
 
-void
-check_fan_speeds(struct environment *env)
+/* Function Description: Read gpio device.
+ * Return : Data at address.
+ */
+static unsigned char envctrl_i2c_read_8574(unsigned char addr)
 {
-	unsigned char speed, max;
-	struct thermistor *t;
-	struct fan *f;
-
-	for (f = env->fans; f; f = f->next) {
-#ifdef DEBUG
-		printk("Fan `%s' [%02x:%d]:", f->name,
-		       f->regs.addr, f->regs.port);
-#endif
-		max = 0;
-		for (t = f->monitor; t; t = t->chain) {
-			speed = t->fan_speed(t);
-			if (speed > max)
-				max = speed;
-#ifdef DEBUG
-			printk(" %s:%02x", t->name, speed);
-#endif
-		}
+	unsigned char rd;
 
-		f->set_speed(f, max);
-#ifdef DEBUG
-		printk(" -> %02x\n", f->value);
-#endif
-	}
-}
+	envctrl_i2c_read_addr(addr);
 
-void
-envctrl_fans_blast(struct environment *env)
-{
-	struct fan *f;
+	/* Do a single byte read and send stop. */
+	rd = envctrl_i2c_read_data();
+	envctrl_i2c_stop();
 
-	for (f = env->fans; f; f = f->next)
-		f->set_speed(f, MAX_FAN_SPEED);
+	return rd;
 }
 
-int
-kenvd(void *data)
+/* Function Description: Decode data read from an adc device using firmware
+ *                       table.
+ * Return: Number of read bytes. Data is stored in bufdata in ascii format.
+ */
+static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
+				      int scale, char *tbl, char *bufdata)
 {
-	struct environment *env = data;
-
-	MOD_INC_USE_COUNT;
-	lock_kernel();
-
-	env->kenvd_pid = current->pid;
-
-	exit_files(current);
-	exit_mm(current);
-
-	spin_lock_irq(&current->sigmask_lock);
-	siginitsetinv(&current->blocked, sigmask(SIGKILL));
-	recalc_sigpending(current);
-	spin_unlock_irq(&current->sigmask_lock);
-
-	current->session = 1;
-	current->pgrp = 1;
-	strcpy(current->comm, "kenvd");
+	int len = 0;
 
-	if (env->enable)
-		env->enable(env);
+	switch (translate_type) {
+	case ENVCTRL_TRANSLATE_NO:
+		/* No decode necessary. */
+		len = 1;
+		bufdata[0] = data;
+		break;
+
+	case ENVCTRL_TRANSLATE_FULL:
+		/* Decode this way: data = table[data]. */
+		len = 1;
+		bufdata[0] = tbl[data];
+		break;
+
+	case ENVCTRL_TRANSLATE_SCALE:
+		/* Decode this way: data = table[data]/scale */
+		sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
+		len = strlen(bufdata);
+		bufdata[len - 1] = bufdata[len - 2];
+		bufdata[len - 2] = '.';
+		break;
+
+	default:
+		break;
+	};
 
-	while (!env->terminate) {
-
-		check_temperatures(env);
-		check_fan_speeds(env);
-		if (env->keep_alive)
-			env->keep_alive(env);
+	return len;
+}
 
-		current->state = TASK_INTERRUPTIBLE;
-		schedule_timeout(env->interval * HZ);
+/* Function Description: Read cpu-related data such as cpu temperature, voltage.
+ * Return: Number of read bytes. Data is stored in bufdata in ascii format.
+ */
+static int envctrl_read_cpu_info(struct i2c_child_t *pchild,
+				 char mon_type, unsigned char *bufdata)
+{
+	unsigned char data;
+	int i;
+	char *tbl, j = -1;
 
-		if (signal_pending(current)) {
-			spin_lock_irq(&current->sigmask_lock);
-			flush_signals(current);
-			spin_unlock_irq(&current->sigmask_lock);
-			break;
+	/* Find the right monitor type and channel. */
+	for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
+		if (pchild->mon_type[i] == mon_type) {
+			if (++j == read_cpu) {
+				break;
+			}
 		}
 	}
 
-	if (env->disable)
-		env->disable(env);
+	if (j != read_cpu)
+		return 0;
 
-	env->kenvd_pid = 0;
-	wake_up(&envctrl.kenvd_wait);
+        /* Read data from address and port. */
+	data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
+				     (unsigned char)pchild->chnl_array[i].chnl_no);
 
-	MOD_DEC_USE_COUNT;
-	return 0;
+	/* Find decoding table. */
+	tbl = pchild->tables + pchild->tblprop_array[i].offset;
+
+	return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
+					  pchild->tblprop_array[i].scale,
+					  tbl, bufdata);
 }
 
-void
-envctrl_stop(void)
+/* Function Description: Read noncpu-related data such as motherboard 
+ *                       temperature.
+ * Return: Number of read bytes. Data is stored in bufdata in ascii format.
+ */
+static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
+				    char mon_type, unsigned char *bufdata)
 {
-	DECLARE_WAITQUEUE(wait, current);
-	struct thermistor *t;
-	struct fan *f;
-	pid_t pid;
-
-	if (envctrl.kenvd_pid) {
-		pid = envctrl.kenvd_pid;
-
-		current->state = TASK_INTERRUPTIBLE;
-		add_wait_queue(&envctrl.kenvd_wait, &wait);
-
-		envctrl.terminate = 1;
-		kill_proc(pid, SIGKILL, 1);
-
-		schedule();
-
-		remove_wait_queue(&envctrl.kenvd_wait, &wait);
-		current->state = TASK_RUNNING;
-	}
-
-	t = envctrl.thermistors;
-	while (t) {
-		envctrl.thermistors = t->next;
-		kfree(t);
-		t = envctrl.thermistors;
-	}
+	unsigned char data;
+	int i;
+	char *tbl = NULL;
 
-	f = envctrl.fans;
-	while (f) {
-		envctrl.fans = f->next;
-		kfree(f);
-		f = envctrl.fans;
+	for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
+		if (pchild->mon_type[i] == mon_type)
+			break;
 	}
 
-	if (envctrl.cpu_temp_table)
-		kfree(envctrl.cpu_temp_table);
+	if (i >= PCF8584_MAX_CHANNELS)
+		return 0;
 
-	if (envctrl.cpu_fan_speeds)
-		kfree(envctrl.cpu_fan_speeds);
+        /* Read data from address and port. */
+	data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
+				     (unsigned char)pchild->chnl_array[i].chnl_no);
 
-	if (envctrl.ps_temp_table)
-		kfree(envctrl.ps_temp_table);
+	/* Find decoding table. */
+	tbl = pchild->tables + pchild->tblprop_array[i].offset;
 
-	if (envctrl.ps_fan_speeds)
-		kfree(envctrl.ps_fan_speeds);
+	return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
+					  pchild->tblprop_array[i].scale,
+					  tbl, bufdata);
 }
 
+/* Function Description: Read fan status.
+ * Return : Always 1 byte. Status stored in bufdata.
+ */
+static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
+				  unsigned char data,
+				  char *bufdata)
+{
+	unsigned char tmp, ret = 0;
+	int i, j = 0;
+
+	tmp = data & pchild->fan_mask;
+
+	if (tmp == pchild->fan_mask) {
+		/* All bits are on. All fans are functioning. */
+		ret = ENVCTRL_ALL_FANS_GOOD;
+	} else if (tmp == 0) {
+		/* No bits are on. No fans are functioning. */
+		ret = ENVCTRL_ALL_FANS_BAD;
+	} else {
+		/* Go through all channels, mark 'on' the matched bits.
+		 * Notice that fan_mask may have discontiguous bits but
+		 * return mask are always contiguous. For example if we
+		 * monitor 4 fans at channels 0,1,2,4, the return mask
+		 * should be 00010000 if only fan at channel 4 is working.
+		 */
+		for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
+			if (pchild->fan_mask & chnls_mask[i]) {
+				if (!(chnls_mask[i] & tmp))
+					ret |= chnls_mask[j];
 
-static unsigned char
-axi_get_temperature(struct thermistor *t)
-{
-	unsigned char value;
+				j++;
+			}
+		}
+	}
 
-	if (read_8591(t->regs.addr, t->regs.port, &value) < 0)
-		return MAX_TEMPERATURE;
-	if (t->regs.port == AXI_THERM_PORT_CPU)
-		return axi_cpu_temp_table[value];
-	else
-		return axi_mod_temp_table[value];
+	bufdata[0] = ret;
+	return 1;
 }
 
-static unsigned char
-axi_get_fan_speed(struct thermistor *t)
-{
-	unsigned char temp;
-
-	temp = t->temperature(t);
-	if (temp >= MAX_TEMPERATURE)
-		return MAX_FAN_SPEED;
-
-	return axi_fan_speeds[temp];
-}
+/* Function Description: Read voltage and power supply status.
+ * Return : Always 1 byte. Status stored in bufdata.
+ */
+static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
+						unsigned char data,
+						char *bufdata)
+{
+	unsigned char tmp, ret = 0;
+	int i, j = 0;
+
+	tmp = data & pchild->voltage_mask;
+
+	/* Two channels are used to monitor voltage and power supply. */
+	if (tmp == pchild->voltage_mask) {
+		/* All bits are on. Voltage and power supply are okay. */
+		ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
+	} else if (tmp == 0) {
+		/* All bits are off. Voltage and power supply are bad */
+		ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
+	} else {
+		/* Either voltage or power supply has problem. */
+		for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
+			if (pchild->voltage_mask & chnls_mask[i]) {
+				j++;
+
+				/* Break out when there is a mismatch. */
+				if (!(chnls_mask[i] & tmp))
+					break; 
+			}
+		}
 
-static int
-axi_set_fan_speed(struct fan *f, unsigned char value)
-{
-	if (value != f->value) {
-		if (write_8444(f->regs.addr, f->regs.port, value))
-			return -1;
-		f->value = value;
+		/* Make a wish that hardware will always use the
+		 * first channel for voltage and the second for
+		 * power supply.
+		 */
+		if (j == 1)
+			ret = ENVCTRL_VOLTAGE_BAD;
+		else
+			ret = ENVCTRL_POWERSUPPLY_BAD;
 	}
-	return 0;
+
+	bufdata[0] = ret;
+	return 1;
 }
 
-static void
-axi_toggle_i2c_int(struct environment *env)
+/* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
+ * Return: Number of read bytes. 0 for error.
+ */
+static ssize_t
+envctrl_read(struct file *file, char *buf, size_t count, loff_t *ppos)
 {
-	unsigned char data;
+	struct i2c_child_t *pchild;
+	unsigned char data[10];
+	int ret = 0;
+
+	/* Get the type of read as decided in ioctl() call.
+	 * Find the appropriate i2c child.
+	 * Get the data and put back to the user buffer.
+	 */
+
+	switch ((int)(long)file->private_data) {
+	case ENVCTRL_RD_WARNING_TEMPERATURE:
+		if (warning_temperature == 0)
+			return 0;
+
+		data[0] = (unsigned char)(warning_temperature);
+		ret = 1;
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
+		if (shutdown_temperature == 0)
+			return 0;
+
+		data[0] = (unsigned char)(shutdown_temperature);
+		ret = 1;
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	case ENVCTRL_RD_MTHRBD_TEMPERATURE:
+		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
+			return 0;
+		ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	case ENVCTRL_RD_CPU_TEMPERATURE:
+		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
+			return 0;
+		ret = envctrl_read_cpu_info(pchild, ENVCTRL_CPUTEMP_MON, data);
+
+		/* Reset cpu to the default cpu0. */
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	case ENVCTRL_RD_CPU_VOLTAGE:
+		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
+			return 0;
+		ret = envctrl_read_cpu_info(pchild, ENVCTRL_CPUVOLTAGE_MON, data);
+
+		/* Reset cpu to the default cpu0. */
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	case ENVCTRL_RD_SCSI_TEMPERATURE:
+		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
+			return 0;
+		ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	case ENVCTRL_RD_ETHERNET_TEMPERATURE:
+		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
+			return 0;
+		ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	case ENVCTRL_RD_FAN_STATUS:
+		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
+			return 0;
+		data[0] = envctrl_i2c_read_8574(pchild->addr);
+		ret = envctrl_i2c_fan_status(pchild,data[0], data);
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	case ENVCTRL_RD_VOLTAGE_STATUS:
+		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
+			return 0;
+		data[0] = envctrl_i2c_read_8574(pchild->addr);
+		ret = envctrl_i2c_voltage_status(pchild, data[0], data);
+		copy_to_user((unsigned char *)buf, data, ret);
+		break;
+
+	default:
+		break;
 
-	if (i2c_read(AXI_PIO_ADDR, &data, 1, 0) != 1)
-		return;
+	};
 
-	data &= ~(0x08);
-	if (i2c_write(AXI_PIO_ADDR, &data, 1, 0) != 1)
-		return;
-	mdelay(1);
-
-	data |= 0x08;
-	if (i2c_write(AXI_PIO_ADDR, &data, 1, 0) != 1)
-		return;
-	mdelay(1);
+	return ret;
 }
 
-
+/* Function Description: Command what to read.  Mapped to user ioctl().
+ * Return: Gives 0 for implemented commands, -EINVAL otherwise.
+ */
 static int
-rasctrl_setup(int node)
+envctrl_ioctl(struct inode *inode, struct file *file,
+	      unsigned int cmd, unsigned long arg)
 {
-	struct thermistor *t, **tlast;
-	struct fan *f, **flast;
-	char tmp[32];
-	int monitor;
-	int shutdown;
-	int warning;
-	int i;
+	char *infobuf;
 
-	prom_getstring(prom_root_node, "name", tmp, sizeof(tmp));
-	if (strcmp(tmp, "SUNW,UltraSPARC-IIi-Engine")) {
-		printk("SUNW,rasctrl will work only on Ultra AXi\n");
-		return -ENODEV;
-	}
-
-	monitor = prom_getintdefault(node, "env-monitor", 0);
-	if (monitor == 0)
-		return -ENODEV;
-
-	envctrl.interval = prom_getintdefault(node, "env-mon-interval", 60);
-	warning = prom_getintdefault(node, "warning-temp", 55);
-	shutdown = prom_getintdefault(node, "shutdown-temp", 58);
-
-	tlast = &envctrl.thermistors;
-	for (i = 0; i < 4; i++) {
-		t = kmalloc(sizeof(struct thermistor), GFP_KERNEL);
-		if (!t)
-			goto out;
-		memset(t, 0, sizeof(struct thermistor));
-
-		t->regs.addr = AXI_THERM_ADDR;
-		t->regs.port = i;
-		t->regs.warning = warning;
-		t->regs.shutdown = shutdown;
-
-		switch (i) {
-		case AXI_THERM_PORT_CPU:
-			sprintf(t->name, "%.7s", "CPU");
-			break;
-		case AXI_THERM_PORT_MOD:
-			sprintf(t->name, "%.7s", "MOD");
-			break;
-		case AXI_THERM_PORT_PCI:
-			sprintf(t->name, "%.7s", "PCI");
-			break;
-		case AXI_THERM_PORT_DISK:
-			sprintf(t->name, "%.7s", "DISK");
-			break;
-		}
-
-		t->temperature = axi_get_temperature;
-		t->fan_speed = axi_get_fan_speed;
-
-		if (!i2c_find_device(t->regs.addr)) {
-			printk("envctrl: `%s': i2c device %02x not found\n",
-			       t->name, t->regs.addr);
-			kfree(t);
-			continue;
-		}
-
-		*tlast = t;
-		tlast = &t->next;
-	}
-
-	flast = &envctrl.fans;
-	for (i = 0; i < 2; i++) {
-		f = kmalloc(sizeof(struct fan), GFP_KERNEL);
-		if (!f)
-			goto out;
-		memset(f, 0, sizeof(struct fan));
-
-		f->regs.addr = AXI_FAN_ADDR;
-		f->regs.port = i;
-
-		switch (i) {
-		case AXI_FAN_PORT_FRONT:
-			sprintf(f->name, "%.7s", "FRONT");
-			t = NULL;
-			while ((t = find_thermistor("CPU", t))) {
-				t->chain = f->monitor;
-				f->monitor = t;
-			}
-			break;
-		case AXI_FAN_PORT_BACK:
-			sprintf(f->name, "%.7s", "BACK");
-			t = NULL;
-			while ((t = find_thermistor("PCI", t))) {
-				t->chain = f->monitor;
-				f->monitor = t;
-			}
-			break;
-		}
-
-		if (!f->monitor) {
-			kfree(f);
-			continue;
-		}
-
-		if (!i2c_find_device(f->regs.addr)) {
-			printk("envctrl: `%s': i2c device %02x not found\n",
-			       f->name, f->regs.addr);
-			kfree(f);
-			continue;
+	switch (cmd) {
+	case ENVCTRL_RD_WARNING_TEMPERATURE:
+	case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
+	case ENVCTRL_RD_MTHRBD_TEMPERATURE:
+	case ENVCTRL_RD_FAN_STATUS:
+	case ENVCTRL_RD_VOLTAGE_STATUS:
+	case ENVCTRL_RD_ETHERNET_TEMPERATURE:
+	case ENVCTRL_RD_SCSI_TEMPERATURE:
+		file->private_data = (void *)(long)cmd;
+		break;
+
+	case ENVCTRL_RD_CPU_TEMPERATURE:
+	case ENVCTRL_RD_CPU_VOLTAGE:
+		/* Check to see if application passes in any cpu number,
+		 * the default is cpu0.
+		 */
+		infobuf = (char *) arg;
+		if (infobuf == NULL) {
+			read_cpu = 0;
+		}else {
+			get_user(read_cpu, infobuf);
 		}
 
-		*flast = f;
-		flast = &f->next;
+		/* Save the command for use when reading. */
+		file->private_data = (void *)(long)cmd;
+		break;
 
-		f->check_failure = NULL;
-		f->set_speed = axi_set_fan_speed;
-	}
-
-	envctrl.enable = axi_toggle_i2c_int;
-	envctrl.disable = envctrl_fans_blast;
+	default:
+		return -EINVAL;
+	};
 
-#ifdef DEBUG
-	printk("Warn: %d C, Shutdown %d C, Interval %d s, Monitor %d\n",
-		warning, shutdown, envctrl.interval, monitor);
-#endif
 	return 0;
-
-out:
-	return -ENODEV;
 }
 
-
-#ifdef U450_SUPPORT
-
-static unsigned char
-envctrl_get_temperature(struct thermistor *t)
-{
-	unsigned char value;
-
-	if (read_8591(t->regs.addr, t->regs.port, &value) < 0)
-		return MAX_TEMPERATURE;
-	if (!strncmp(t->name, "CPU", 3))
-		return envctrl.cpu_temp_table[value];
-	else
-		return envctrl.ps_temp_table[value];
-}
-
-static unsigned char
-envctrl_get_fan_speed(struct thermistor *t)
+/* Function Description: open device. Mapped to user open().
+ * Return: Always 0.
+ */
+static int
+envctrl_open(struct inode *inode, struct file *file)
 {
-	unsigned char temp;
-
-	temp = t->temperature(t);
-	if (temp >= MAX_TEMPERATURE)
-		return MAX_FAN_SPEED;
-
-	if (!strncmp(t->name, "CPU", 3))
-		return envctrl.cpu_fan_speeds[temp];
-	else
-		return envctrl.ps_fan_speeds[temp];
+	file->private_data = 0;
+	MOD_INC_USE_COUNT;
+	return 0;
 }
 
+/* Function Description: Open device. Mapped to user close().
+ * Return: Always 0.
+ */
 static int
-envctrl_set_fan_speed(struct fan *f, unsigned char value)
+envctrl_release(struct inode *inode, struct file *file)
 {
-	if (value != f->value) {
-		if (write_8444(f->regs.addr, f->regs.port, value))
-			return -1;
-		f->value = value;
-	}
-
+	MOD_DEC_USE_COUNT;
 	return 0;
 }
 
-static unsigned char u450_default_thermisters[] =
-{
-	/* CPU0 */
-	0x00, 0x00, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x46,
-	0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x01,
-	0x00, 0x00, 0x00, 0x01, 0x43, 0x50, 0x55, 0x30, 0x00,
-	/* CPU1 */
-	0x00, 0x00, 0x00, 0x9c, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x46,
-	0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x01,
-	0x00, 0x00, 0x00, 0x01, 0x43, 0x50, 0x55, 0x31, 0x00,
-	/* CPU2 */
-	0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x46,
-	0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x01,
-	0x00, 0x00, 0x00, 0x01, 0x43, 0x50, 0x55, 0x32, 0x00,
-	/* CPU3 */
-	0x00, 0x00, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x46,
-	0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x01,
-	0x00, 0x00, 0x00, 0x01, 0x43, 0x50, 0x55, 0x33, 0x00,
-	/* PS0 */
-	0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x5a,
-	0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x01,
-	0x00, 0x00, 0x00, 0x01, 0x50, 0x53, 0x30, 0x00,
-	/* PS1 */
-	0x00, 0x00, 0x00, 0x92, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x5a,
-	0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x01,
-	0x00, 0x00, 0x00, 0x01, 0x50, 0x53, 0x31, 0x00,
-	/* PS2 */
-	0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x5a,
-	0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x01,
-	0x00, 0x00, 0x00, 0x01, 0x50, 0x53, 0x32, 0x00,
-	/* AMB */
-	0x00, 0x00, 0x00, 0x9a, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x28,
-	0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x01,
-	0x00, 0x00, 0x00, 0x01, 0x41, 0x4d, 0x42, 0x00
-};
-
-static unsigned char u450_default_cpu_temp_factors[] =
-{
-	0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
-	0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
-	0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
-	0x96, 0x94, 0x92, 0x90, 0x8f, 0x8e, 0x8d, 0x8c,
-	0x8a, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82,
-	0x81, 0x80, 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a,
-	0x79, 0x79, 0x78, 0x78, 0x77, 0x76, 0x75, 0x74,
-	0x73, 0x72, 0x71, 0x70, 0x70, 0x6f, 0x6f, 0x6e,
-	0x6e, 0x6e, 0x6d, 0x6d, 0x6c, 0x6b, 0x6a, 0x6a,
-	0x69, 0x69, 0x68, 0x67, 0x66, 0x65, 0x65, 0x64,
-	0x64, 0x64, 0x63, 0x63, 0x62, 0x62, 0x61, 0x61,
-	0x60, 0x60, 0x5f, 0x5f, 0x5e, 0x5e, 0x5d, 0x5d,
-	0x5c, 0x5c, 0x5b, 0x5b, 0x5b, 0x5a, 0x5a, 0x5a,
-	0x59, 0x59, 0x58, 0x58, 0x57, 0x57, 0x56, 0x56,
-	0x55, 0x55, 0x54, 0x54, 0x53, 0x53, 0x52, 0x52,
-	0x52, 0x51, 0x51, 0x50, 0x50, 0x50, 0x50, 0x4f,
-	0x4f, 0x4f, 0x4e, 0x4e, 0x4e, 0x4d, 0x4d, 0x4d,
-	0x4c, 0x4c, 0x4c, 0x4b, 0x4b, 0x4b, 0x4a, 0x4a,
-	0x4a, 0x49, 0x49, 0x49, 0x48, 0x48, 0x48, 0x47,
-	0x47, 0x47, 0x46, 0x46, 0x46, 0x46, 0x45, 0x45,
-	0x45, 0x44, 0x44, 0x44, 0x44, 0x43, 0x43, 0x43,
-	0x43, 0x42, 0x42, 0x42, 0x42, 0x41, 0x41, 0x41,
-	0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e, 0x3e,
-	0x3e, 0x3d, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
-	0x3c, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
-	0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37, 0x37,
-	0x37, 0x37, 0x36, 0x36, 0x36, 0x35, 0x35, 0x35,
-	0x34, 0x34, 0x34, 0x33, 0x33, 0x33, 0x33, 0x32,
-	0x32, 0x32, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30,
-	0x2f, 0x2f, 0x2f, 0x2e, 0x2e, 0x2e, 0x2d, 0x2d,
-	0x2d, 0x2c, 0x2c, 0x2c, 0x2b, 0x2b, 0x2b, 0x2a,
-	0x2a, 0x2a, 0x29, 0x29, 0x29, 0x28, 0x28, 0x28
-};
-
-static unsigned char u450_default_cpu_fan_speeds[] =
-{
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x20, 0x21, 0x22, 0x23,
-	0x24, 0x25, 0x26, 0x27, 0x28, 0x2a, 0x2b, 0x2d,
-	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
-	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f
-};
+static struct file_operations envctrl_fops = {
+	owner:		THIS_MODULE,
+	read:		envctrl_read,
+	ioctl:		envctrl_ioctl,
+	open:		envctrl_open,
+	release:	envctrl_release,
+};	
 
-static unsigned char u450_default_ps_temp_factors[] =
-{
-	0x9a, 0x96, 0x82, 0x7d, 0x78, 0x73, 0x6e, 0x6b,
-	0x69, 0x67, 0x64, 0x5f, 0x5a, 0x57, 0x55, 0x53,
-	0x51, 0x50, 0x4e, 0x4d, 0x4c, 0x4b, 0x49, 0x47,
-	0x46, 0x45, 0x44, 0x43, 0x42, 0x41, 0x40, 0x3f,
-	0x3e, 0x3d, 0x3c, 0x3c, 0x3b, 0x3a, 0x39, 0x39,
-	0x38, 0x37, 0x37, 0x36, 0x35, 0x35, 0x34, 0x33,
-	0x32, 0x32, 0x32, 0x31, 0x31, 0x30, 0x30, 0x2f,
-	0x2f, 0x2e, 0x2e, 0x2d, 0x2d, 0x2c, 0x2c, 0x2b,
-	0x2a, 0x2a, 0x29, 0x29, 0x28, 0x28, 0x27, 0x27,
-	0x26, 0x26, 0x25, 0x25, 0x25, 0x25, 0x24, 0x24,
-	0x23, 0x23, 0x23, 0x22, 0x22, 0x22, 0x21, 0x21,
-	0x21, 0x20, 0x20, 0x20, 0x1f, 0x1f, 0x1e, 0x1e,
-	0x1e, 0x1d, 0x1d, 0x1d, 0x1d, 0x1c, 0x1c, 0x1c,
-	0x1b, 0x1b, 0x1b, 0x1a, 0x1a, 0x1a, 0x19, 0x19,
-	0x19, 0x18, 0x18, 0x18, 0x18, 0x17, 0x17, 0x17,
-	0x17, 0x16, 0x16, 0x16, 0x16, 0x15, 0x15, 0x15,
-	0x14, 0x14, 0x14, 0x13, 0x13, 0x13, 0x13, 0x13,
-	0x12, 0x12, 0x12, 0x12, 0x11, 0x11, 0x11, 0x11,
-	0x10, 0x10, 0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0f,
-	0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d,
-	0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b,
-	0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
-	0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08,
-	0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
-	0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
-	0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
-	0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
-	0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+static struct miscdevice envctrl_dev = {
+	ENVCTRL_MINOR,
+	"envctrl",
+	&envctrl_fops
 };
 
-static unsigned char u450_default_ps_fan_speeds[] =
+/* Function Description: Set monitor type based on firmware description.
+ * Return: None.
+ */
+static void envctrl_set_mon(struct i2c_child_t *pchild,
+			    char *chnl_desc,
+			    int chnl_no)
+{
+	/* Firmware only has temperature type.  It does not distinguish
+	 * different kinds of temperatures.  We use channel description
+	 * to disinguish them.
+	 */
+	if (!(strcmp(chnl_desc,"temp,cpu")) ||
+	    !(strcmp(chnl_desc,"temp,cpu0")) ||
+	    !(strcmp(chnl_desc,"temp,cpu1")) ||
+	    !(strcmp(chnl_desc,"temp,cpu2")) ||
+	    !(strcmp(chnl_desc,"temp,cpu3")))
+		pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
+
+	if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
+	    !(strcmp(chnl_desc,"vddcore,cpu1")) ||
+	    !(strcmp(chnl_desc,"vddcore,cpu2")) ||
+	    !(strcmp(chnl_desc,"vddcore,cpu3")))
+		pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
+
+	if (!(strcmp(chnl_desc,"temp,motherboard")))
+		pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
+
+	if (!(strcmp(chnl_desc,"temp,scsi")))
+		pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
+
+	if (!(strcmp(chnl_desc,"temp,ethernet")))
+		pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
+
+	if (!(strcmp(chnl_desc,"temp,ethernet")))
+		pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
+}
+
+/* Function Description: Initialize monitor channel with channel desc,
+ *                       decoding tables, monitor type, optional properties.
+ * Return: None.
+ */
+static void envctrl_init_adc(struct i2c_child_t *pchild, int node)
 {
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
-	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x21, 0x22, 0x23,
-	0x24, 0x25, 0x26, 0x26, 0x27, 0x28, 0x29, 0x2a,
-	0x2b, 0x2d, 0x2e, 0x2f, 0x30, 0x30, 0x30, 0x30,
-	0x30, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
-	0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
-	0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f
-};
+	char chnls_desc[CHANNEL_DESC_SZ];
+	int i, len, j = 0;
+	char *ptr;
+
+	/* Firmware describe channels into a stream separated by a '\0'.
+	 * Replace all '\0' with a space.
+	 */
+        len = prom_getproperty(node, "channels-description", chnls_desc,
+			       CHANNEL_DESC_SZ);
+        for (i = 0; i < len; i++) {
+                if (chnls_desc[i] == '\0')
+                        chnls_desc[i] = ' ';
+        }
+
+	ptr = strtok(chnls_desc, " ");
+	while (ptr != NULL) {
+		envctrl_set_mon(pchild, ptr, j);
+		ptr = strtok(NULL, " ");
+		j++;
+	}
+
+	/* Get optional properties. */
+        len = prom_getproperty(node, "warning-temp", (char *)&warning_temperature,
+			       sizeof(warning_temperature));
+        len = prom_getproperty(node, "shutdown-temp", (char *)&shutdown_temperature,
+			       sizeof(shutdown_temperature));
+}
 
-static void
-u450_toggle_i2c_int(struct environment *env)
+/* Function Description: Initialize child device monitoring fan status.
+ * Return: None.
+ */
+static void envctrl_init_fanstat(struct i2c_child_t *pchild)
 {
-	unsigned char tmp[80];
-	unsigned char data;
-	int i, n;
-
-	write_8583(U450_TIMER_ADDR, 0, 0x84);
-	write_8583(U450_TIMER_ADDR, 8, 0x0a);
-	write_8583(U450_TIMER_ADDR, 7, 0x00);
-	write_8583(U450_TIMER_ADDR, 0, 0x04);
-
-	n = sprintf(tmp, "envctrl: PCF8583:");
-	for (i = 0; i < 16; i++) {
-		if (read_8583(U450_TIMER_ADDR, i, &data) < 0) {
-			printk("envctrl: error reading PCF8583\n");
-			break;
-		}
-		n += sprintf(tmp+n, " %02x", data);
-	}
-	printk("%s\n", tmp);
+	int i;
 
-#if 1
-	data = 0x70;
-	if (i2c_write(U450_PIO_ADDR, &data, 1, 0) != 1)
-		return;
-	mdelay(1);
-
-	data = 0x78;
-	if (i2c_write(U450_PIO_ADDR, &data, 1, 0) != 1)
-		return;
-	mdelay(1);
-#endif
+	/* Go through all channels and set up the mask. */
+	for (i = 0; i < pchild->total_chnls; i++)
+		pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
+
+	/* We only need to know if this child has fan status monitored.
+	 * We dont care which channels since we have the mask already.
+	 */
+	pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
 }
 
-static void
-u450_set_egg_timer(struct environment *env)
+/* Initialize child device monitoring voltage status. */
+static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
 {
-	unsigned char value;
+	int i;
 
-#if 0
-	write_8583(U450_TIMER_ADDR, 0x00, 0x84);
-	read_8583(U450_TIMER_ADDR, 0x07, &value);
-	write_8583(U450_TIMER_ADDR, 0x07, 0x00);
-	write_8583(U450_TIMER_ADDR, 0x00, 0x04);
-#else
-	read_8583(U450_TIMER_ADDR, 0x07, &value);
-	printk("envctrl: TIMER [%02x:07]: %02x\n", U450_TIMER_ADDR, value);
-	read_8583(U450_TIMER_ADDR, 0x00, &value);
-	printk("envctrl: TIMER [%02x:00]: %02x\n", U450_TIMER_ADDR, value);
-#endif
+	/* Go through all channels and set up the mask. */
+	for (i = 0; i < pchild->total_chnls; i++)
+		pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
+
+	/* We only need to know if this child has voltage status monitored.
+	 * We dont care which channels since we have the mask already.
+	 */
+	pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
 }
 
-static int
-envctrl_setup(int node)
+/* Function Description: Initialize i2c child device.
+ * Return: None.
+ */
+static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
+				   struct i2c_child_t *pchild)
 {
-	struct thermistor *t, **tlast;
-	struct fan *f, **flast;
-	unsigned char *tmp = NULL, *p;
-	int len, n, err;
-	int defaults = 0;
-
-	len = prom_getproplen(node, "thermisters");
-	if (len <= 0) {
-		printk("envctrl: no property `thermisters', using defaults\n");
-		defaults++;
-		len = sizeof(u450_default_thermisters);
-	}
-
-	tmp = (unsigned char *)kmalloc(len, GFP_KERNEL);
-	if (!tmp) {
-		printk("envctrl: can't allocate property buffer\n");
-		return -ENODEV;
-	}
-
-	if (defaults) {
-		memcpy(tmp, u450_default_thermisters, len);
-	} else {
-		err = prom_getproperty(node, "thermisters", tmp, len);
-		if (err < 0) {
-			printk("envctrl: error reading property `thermisters'\n");
-			kfree(tmp);
-			return -ENODEV;
-		}
-	}
+	int node, len, i, tbls_size = 0;
 
-	p = tmp;
-	err = -ENOMEM;
+	node = edev_child->prom_node;
 
-	tlast = &envctrl.thermistors;
-	while (len > sizeof(struct therm_regs)) {
-		t = kmalloc(sizeof(struct thermistor), GFP_KERNEL);
-		if (!t) {
-			printk("envctrl: can't allocate thermistor struct\n");
-			goto out;
+	/* Get device address. */
+	len = prom_getproperty(node, "reg",
+			       (char *) &(pchild->addr),
+			       sizeof(pchild->addr));
+
+	/* Get tables property.  Read firmware temperature tables. */
+	len = prom_getproperty(node, "translation",
+			       (char *) pchild->tblprop_array,
+			       (PCF8584_MAX_CHANNELS *
+				sizeof(struct pcf8584_tblprop)));
+	if (len > 0) {
+                pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
+		for (i = 0; i < pchild->total_tbls; i++) {
+			if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
+				tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
+			}
 		}
-		memset(t, 0, sizeof(struct thermistor));
 
-		memcpy(&t->regs, p, sizeof(struct therm_regs));
-		p += sizeof(struct therm_regs);
-		len -= sizeof(struct therm_regs);
-
-		n = strlen(p) + 1;
-		strncpy(t->name, p, 7);
-		p += n;
-		len -= n;
-
-		if (!i2c_find_device(t->regs.addr)) {
-			printk("envctrl: `%s': i2c device %02x not found\n",
-			       t->name, t->regs.addr);
-			kfree(t);
-			continue;
+                pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
+                len = prom_getproperty(node, "tables",
+				       (char *) pchild->tables, tbls_size);
+                if (len <= 0) {
+			printk("envctrl: Failed to get table.\n");
+			return;
 		}
-
-		t->temperature = envctrl_get_temperature;
-		t->fan_speed = envctrl_get_fan_speed;
-
-		*tlast = t;
-		tlast = &t->next;
 	}
 
-	flast = &envctrl.fans;
-	for (n = 0; n < 2; n++) {
-		f = kmalloc(sizeof(struct fan), GFP_KERNEL);
-		if (!f)
-			goto out;
-		memset(f, 0, sizeof(struct fan));
-
-		f->regs.addr = U450_FAN_ADDR;
-		f->regs.port = n;
-
-		switch (n) {
-		case U450_FAN_PORT_CPU:
-			sprintf(f->name, "%.7s", "CPU");
-			t = NULL;
-			while ((t = find_thermistor("CPU", t))) {
-				t->chain = f->monitor;
-				f->monitor = t;
+	/* Get the monitor channels. */
+	len = prom_getproperty(node, "channels-in-use",
+			       (char *) pchild->chnl_array,
+			       (PCF8584_MAX_CHANNELS *
+				sizeof(struct pcf8584_channel)));
+	pchild->total_chnls = len / sizeof(struct pcf8584_channel);
+
+	for (i = 0; i < pchild->total_chnls; i++) {
+		switch (pchild->chnl_array[i].type) {
+		case PCF8584_TEMP_TYPE:
+			envctrl_init_adc(pchild, node);
+			break;
+
+		case PCF8584_FANSTAT_TYPE:
+			envctrl_init_fanstat(pchild);
+			i = pchild->total_chnls;
+			break;
+
+		case PCF8584_VOLTAGE_TYPE:
+			if (pchild->i2ctype == I2C_ADC) {
+				envctrl_init_adc(pchild,node);
+			} else {
+				envctrl_init_voltage_status(pchild);
 			}
+			i = pchild->total_chnls;
 			break;
-		case U450_FAN_PORT_PS:
-			sprintf(f->name, "%.7s", "PS");
-			t = NULL;
-			while ((t = find_thermistor("PS", t))) {
-				t->chain = f->monitor;
-				f->monitor = t;
-			}
-			break;
-		}
-
-		if (!f->monitor) {
-			kfree(f);
-			continue;
-		}
-
-		if (!i2c_find_device(f->regs.addr)) {
-			printk("envctrl: `%s': i2c device %02x not found\n",
-			       f->name, f->regs.addr);
-			kfree(f);
-			continue;
-		}
-
-		*flast = f;
-		flast = &f->next;
 
-		f->check_failure = NULL;
-		f->set_speed = envctrl_set_fan_speed;
-	}
-
-	envctrl.cpu_temp_table = kmalloc(256, GFP_KERNEL);
-	if (!envctrl.cpu_temp_table) {
-		printk("envctrl: can't allocate temperature table\n");
-		goto out;
-	}
-	if (defaults) {
-		memcpy(envctrl.cpu_temp_table,
-		       u450_default_cpu_temp_factors, 256);
-	} else {
-		err = prom_getproperty(node, "cpu-temp-factors",
-				       envctrl.cpu_temp_table, 256);
-		if (err < 0) {
-			printk("envctrl: can't read `cpu-temp-factors'\n");
-			goto out;
-		}
-	}
-
-	envctrl.cpu_fan_speeds = kmalloc(112, GFP_KERNEL);
-	if (!envctrl.cpu_fan_speeds) {
-		printk("envctrl: can't allocate fan speed table\n");
-		goto out;
-	}
-	if (defaults) {
-		memcpy(envctrl.cpu_fan_speeds,
-		       u450_default_cpu_fan_speeds, 112);
-	} else {
-		err = prom_getproperty(node, "cpu-fan-speeds",
-				       envctrl.cpu_fan_speeds, 112);
-		if (err < 0) {
-			printk("envctrl: can't read `cpu-fan-speeds'\n");
-			goto out;
-		}
-	}
-
-	envctrl.ps_temp_table = kmalloc(256, GFP_KERNEL);
-	if (!envctrl.ps_temp_table) {
-		printk("envctrl: can't allocate temperature table\n");
-		goto out;
-	}
-	if (defaults) {
-		memcpy(envctrl.ps_temp_table,
-		       u450_default_ps_temp_factors, 256);
-	} else {
-		err = prom_getproperty(node, "ps-temp-factors",
-				       envctrl.ps_temp_table, 256);
-		if (err < 0) {
-			printk("envctrl: can't read `ps-temp-factors'\n");
-			goto out;
-		}
-	}
-
-	envctrl.ps_fan_speeds = kmalloc(112, GFP_KERNEL);
-	if (!envctrl.ps_fan_speeds) {
-		printk("envctrl: can't allocate fan speed table\n");
-		goto out;
-	}
-	if (defaults) {
-		memcpy(envctrl.ps_fan_speeds,
-		       u450_default_ps_fan_speeds, 112);
-	} else {
-		err = prom_getproperty(node, "ps-fan-speeds",
-				       envctrl.ps_fan_speeds, 112);
-		if (err < 0) {
-			printk("envctrl: can't read `ps-fan-speeds'\n");
-			goto out;
-		}
+		default:
+			break;
+		};
 	}
-
-	envctrl.enable = u450_toggle_i2c_int;
-	envctrl.keep_alive = u450_set_egg_timer;
-	envctrl.disable = envctrl_fans_blast;
-	envctrl.interval = 60;
-
-	kfree(tmp);
-	return 0;
-
-out:
-	if (tmp)
-		kfree(tmp);
-	return err;
-}
-#endif /* U450_SUPPORT */
-
-
-
-static loff_t
-envctrl_llseek(struct file *file, loff_t offset, int type)
-{
-	return -ESPIPE;
-}
-
-static ssize_t
-envctrl_read(struct file *file, char *buf, size_t count, loff_t *ppos)
-{
-	unsigned long addr = (unsigned long)file->private_data;
-
-	return i2c_read(addr, buf, count, 1);
-}
-
-static ssize_t
-envctrl_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
-{
-	unsigned long addr = (unsigned long)file->private_data;
-
-	return i2c_write(addr, buf, count, 1);
 }
 
-static int
-envctrl_ioctl(struct inode *inode, struct file *file,
-	      unsigned int cmd, unsigned long arg)
+/* Function Description: Search the child device list for a device.
+ * Return : The i2c child if found. NULL otherwise.
+ */
+static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
 {
-	unsigned long data;
-	int addr;
+	int i, j;
 
-	switch (cmd) {
-		case I2CIOCSADR:
-			if (get_user(addr, (int *)arg))
-				return -EFAULT;
-			data = addr & 0xfe;
-			if (!i2c_find_device(addr & 0xfe))
-				return -ENODEV;
-			file->private_data = (void *)data;
-			break;
-		case I2CIOCGADR:
-			addr = (unsigned long)file->private_data;
-			if (put_user(addr, (int *)arg))
-				return -EFAULT;
-			break;
-		default:
-			return -EINVAL;
+	for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
+		for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
+			if (i2c_childlist[i].mon_type[j] == mon_type) {
+				return (struct i2c_child_t*)(&(i2c_childlist[i]));
+			}
+		}
 	}
-	return 0;
+	return NULL;
 }
 
-static int
-envctrl_open(struct inode *inode, struct file *file)
-{
-	file->private_data = 0;
-	return 0;
-}
-
-static struct file_operations envctrl_fops = {
-	owner:		THIS_MODULE,
-	llseek:		envctrl_llseek,
-	read:		envctrl_read,
-	write:		envctrl_write,
-	ioctl:		envctrl_ioctl,
-	open:		envctrl_open,
-};
-
-static struct miscdevice envctrl_dev = {
-	ENVCTRL_MINOR,
-	"envctrl",
-	&envctrl_fops
-};
-
-#ifdef MODULE
-int init_module(void)
-#else
-int __init envctrl_init(void)
-#endif
+static int __init envctrl_init(void)
 {
 #ifdef CONFIG_PCI
-	struct linux_ebus *ebus;
-	struct linux_ebus_device *edev = 0;
-	pid_t pid;
-	int err;
-
+	struct linux_ebus *ebus = NULL;
+	struct linux_ebus_device *edev = NULL;
+	struct linux_ebus_child *edev_child = NULL;
+	int i = 0;
+
+	/* Traverse through ebus and ebus device list for i2c device and
+	 * adc and gpio nodes.
+	 */
 	for_each_ebus(ebus) {
 		for_each_ebusdev(edev, ebus) {
-			if (!strcmp(edev->prom_name, "SUNW,envctrl"))
-				goto ebus_done;
-			if (!strcmp(edev->prom_name, "SUNW,rasctrl"))
-				goto ebus_done;
+			if (!strcmp(edev->prom_name, "i2c")) {
+				i2c = ioremap(	edev->resource[0].start, 
+								sizeof(struct pcf8584_reg));
+				for_each_edevchild(edev, edev_child) {
+					if (!strcmp("gpio", edev_child->prom_name)) {
+						i2c_childlist[i].i2ctype = I2C_GPIO;
+						envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
+					}
+					if (!strcmp("adc", edev_child->prom_name)) {
+						i2c_childlist[i].i2ctype = I2C_ADC;
+						envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
+					}
+				}
+				goto done;
+			}
 		}
 	}
-ebus_done:
+
+done:
 	if (!edev) {
-		printk("%s: ebus device not found\n", __FUNCTION__);
+		printk("envctrl: I2C device not found.\n");
 		return -ENODEV;
 	}
 
-	i2c_regs = (unsigned long) ioremap(edev->resource[0].start, I2C_REG_SIZE);
-	writeb(CONTROL_PIN, i2c_regs + I2C_CSR);
-	writeb(PCF8584_ADDRESS >> 1, i2c_regs + I2C_DATA);
-	writeb(CONTROL_PIN | CONTROL_ES1, i2c_regs + I2C_CSR);
-	writeb(CLK_4_43 | BUS_CLK_90, i2c_regs + I2C_DATA);
-	writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c_regs + I2C_CSR);
-	mdelay(10);
+	/* Set device address. */
+	envctrl_writeb(CONTROL_PIN, &i2c->csr);
+	envctrl_writeb(PCF8584_ADDRESS, &i2c->data);
+
+	/* Set system clock and SCL frequencies. */ 
+	envctrl_writeb(CONTROL_PIN | CONTROL_ES1, &i2c->csr);
+	envctrl_writeb(CLK_4_43 | BUS_CLK_90, &i2c->data);
+
+	/* Enable serial interface. */
+	envctrl_writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, &i2c->csr);
+	udelay(200);
 
+	/* Register the device as a minor miscellaneous device. */
 	if (misc_register(&envctrl_dev)) {
-		printk("%s: unable to get misc minor %d\n",
-		       __FUNCTION__, envctrl_dev.minor);
-		return -ENODEV;
-	}
-
-	err = i2c_scan_bus();
-	if (err) {
-		i2c_free_devices();
-		misc_deregister(&envctrl_dev);
-		return err;
-	}
-
-	memset(&envctrl, 0, sizeof(struct environment));
-
-	err = -ENODEV;
-	if (!strcmp(edev->prom_name, "SUNW,rasctrl"))
-		err = rasctrl_setup(edev->prom_node);
-#ifdef U450_SUPPORT
-	else if (!strcmp(edev->prom_name, "SUNW,envctrl"))
-		err = envctrl_setup(edev->prom_node);
-#endif
-
-	if (err) {
-		envctrl_stop();
-		i2c_free_devices();
-		misc_deregister(&envctrl_dev);
-		return err;
+		printk("envctrl: Unable to get misc minor %d\n",
+		       envctrl_dev.minor);
 	}
 
-	init_waitqueue_head(&envctrl.kenvd_wait);
-
-	pid = kernel_thread(kenvd, (void *)&envctrl, CLONE_FS);
-	if (pid < 0) {
-		envctrl_stop();
-		i2c_free_devices();
-		misc_deregister(&envctrl_dev);
-		return -ENODEV;
+	/* Note above traversal routine post-incremented 'i' to accomodate 
+	 * a next child device, so we decrement before reverse-traversal of
+	 * child devices.
+	 */
+	printk("envctrl: initialized ");
+	for(--i; i >= 0; --i)
+	{
+		printk("[%s 0x%lx]%s", 
+			(I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") : 
+			((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")), 
+			i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
 	}
 
 	return 0;
@@ -1605,13 +946,18 @@
 #endif
 }
 
-
-#ifdef MODULE
-void cleanup_module(void)
+static void __exit envctrl_cleanup(void)
 {
-	envctrl_stop();
-	i2c_free_devices();
-	iounmap(i2c_regs);
+	int i;
+
+	iounmap(i2c);
 	misc_deregister(&envctrl_dev);
+
+	for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
+		if (i2c_childlist[i].tables)
+			kfree(i2c_childlist[i].tables);
+	}
 }
-#endif
+
+module_init(envctrl_init);
+module_exit(envctrl_cleanup);

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)