/*
    i2c-stub.c - Part of lm_sensors, Linux kernel modules for hardware
              monitoring

    Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>

    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.
*/

//#define DEBUG 1

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/i2c.h>

static u8  stub_pointer;
static u8 bank = 0;

static u8 stub_bytes[4][256] = {
{
  0x80,0x00,0x00,0x00,0x00,0x10,0xbf,0x10,0xbf,0x64,0x64,0x5e,0x73,0x5c,0x7b,0x12,
  0x94,0x93,0x9a,0xff,0x5e,0x70,0xd0,0x7c,0xce,0xcf,0xca,0x21,0x1e,0x21,0x00,0x00,
  0x2c,0x25,0x00,0x01,0xc5,0x02,0x84,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,
  0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,
  0x09,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,
  0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x01,0x00,0x00,0x7f,0x0f,0x0f,0x03,
  0xff,0x00,0xff,0x00,0x83,0x00,0xff,0xff,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0x00,
  0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0x64,0x5f,0x55,0x50,0x64,0x5f,0x55,0x50,
  0x64,0x5f,0x55,0x50,0x64,0x5f,0x55,0x50,0x64,0x5f,0x55,0x50,0x64,0x5f,0x55,0x50,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
  0x08,0x01,0x30,0x2c,0x2c,0x2c,0x2c,0x2c,0x2c,0x2c,0x2c,0x89,0x89,0x89,0x89,0x89,
  0x89,0x89,0x89,0x02,0x10,0x50,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0x30,0x4b,0x4b,0x4b,0x4b,0x10,0xc0,0xff,0xf4,0xc0,0xf5,0x80,0x80,0x01,0x80,0x01,
  0xbb,0xc0,0x82,0xff,0x80,0x0c,0xfb,0xfb,0xfe,0xff,0xe2,0x4e,0xfe,0xff,0xff,0xff,
  0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x80,0x1b,0x00,0xff,0x00,0x00,0x10,0x00,0x00,
},
{
  0x81,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x64,0x5e,0x73,0x5c,0x7b,0x12,
  0x93,0x94,0x9a,0xff,0x5e,0x70,0xcf,0x7c,0xce,0xcf,0xca,0x18,0x1e,0x21,0x00,0x00,
  0x2b,0x25,0x00,0x01,0xc3,0x02,0x7f,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,
  0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,
  0x09,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x01,0x00,0x00,0x7f,0x0f,0x0f,0x03,
  0xff,0x00,0xff,0x00,0xfa,0x00,0xff,0xff,0x00,0x00,0xff,0x00,0xff,0x00,0xff,0x00,
  0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0x64,0x5f,0x55,0x50,0x64,0x5f,0x55,0x50,
  0x64,0x5f,0x55,0x50,0x64,0x5f,0x55,0x50,0x64,0x5f,0x55,0x50,0x64,0x5f,0x55,0x50,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
  0x08,0x01,0x30,0x2c,0x2c,0x2c,0x2c,0x2c,0x2c,0x2c,0x2c,0x89,0x89,0x89,0x89,0x89,
  0x89,0x89,0x89,0x02,0x10,0x50,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0x30,0x4b,0x4b,0x4b,0x4b,0x10,0xc0,0xff,0xf4,0xc0,0xf5,0x80,0x80,0x01,0x80,0x01,
  0xbb,0xc0,0x82,0xff,0x80,0x0c,0xfb,0xfb,0xfe,0xff,0xe2,0x4e,0xfe,0xff,0xff,0xff,
  0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x80,0x1b,0x00,0xff,0x00,0x00,0x10,0x00,0x00,
},
{
  0x82,0x09,0x09,0xbd,0x09,0x09,0x09,0x00,0x11,0x11,0x11,0xff,0xff,0x5c,0x7b,0x12,
  0x28,0x28,0x28,0x28,0x20,0x20,0xff,0xff,0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x14,
  0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x39,0x3c,0x3f,0x42,0x45,0x48,0x4b,0xff,0x2c,0x2c,0x2c,0x2c,0x2c,0x32,0x38,0xff,
  0x39,0x3c,0x3f,0x42,0x45,0x48,0x4b,0xff,0x2c,0x2c,0x2c,0x2c,0x2c,0x32,0x38,0xff,
  0x39,0x3c,0x3f,0x42,0x45,0x48,0x4b,0xff,0x2c,0x2c,0x2c,0x2c,0x2c,0x32,0x38,0xff,
  0x39,0x3c,0x3f,0x42,0x45,0x48,0x4b,0xff,0x2c,0x2c,0x04,0x2c,0x2c,0x32,0x38,0xff,
  0x2a,0x2d,0x30,0x33,0x36,0x39,0x3c,0xff,0x2c,0x2c,0x2c,0x2c,0x2c,0x32,0x38,0xff,
  0x2a,0x2d,0x30,0x33,0x36,0x39,0x3c,0xff,0x2c,0x2c,0x2c,0x2c,0x2c,0x32,0x38,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x80,0x1b,0x00,0xff,0x00,0x00,0x10,0x00,0x00,
},
};



/* Return -1 on error. */
static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
	char read_write, u8 command, int size, union i2c_smbus_data * data)
{
	s32 ret;

	if (addr != 0x2f)
		return -1;
		
	switch (size) {

	case I2C_SMBUS_QUICK:
		dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
		ret = 0;
		break;

	case I2C_SMBUS_BYTE:
		if (read_write == I2C_SMBUS_WRITE) {
			stub_pointer = command;
			dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
					"wrote 0x%02x.\n",
					addr, command);
		} else {
			data->byte = stub_bytes[bank][stub_pointer++];
			dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
					"read  0x%02x.\n",
					addr, data->byte);
		}

		ret = 0;
		break;

	case I2C_SMBUS_BYTE_DATA:
		if (read_write == I2C_SMBUS_WRITE) {

			if (command == 0) 
				bank = data->byte & 3;
			stub_bytes[command == 0 ? 0 : bank][command] = data->byte;
			dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
					"wrote 0x%02x at 0x%02x.\n",
					addr, data->byte, command);
				
		} else {
			data->byte = stub_bytes[command == 0 ? 0 : bank][command];
			dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
					"read  0x%02x at 0x%02x.\n",
					addr, data->byte, command);
			if (command == 0xD)
				data->byte = (stub_bytes[0][0] & 0x80) ? 0x5c : 0xa3; 
			if ((command == 0xE) ||  (command == 0xF)) 
				data->byte = stub_bytes[0][command];
		}
		stub_pointer = command + 1;

		ret = 0;
		break;
	default:
		dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
		ret = -1;
		break;
	} /* switch (size) */

	return ret;
}


static u32 stub_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
		I2C_FUNC_SMBUS_BYTE_DATA;
}

static struct i2c_algorithm smbus_algorithm = {
	.functionality	= stub_func,
	.smbus_xfer	= stub_xfer,
};

static struct i2c_adapter stub_adapter = {
	.owner		= THIS_MODULE,
	.class		= I2C_CLASS_HWMON,
	.algo		= &smbus_algorithm,
	.name		= "SMBus stub driver",
};

static int __init i2c_stub_init(void)
{
	printk(KERN_INFO "i2c-stub loaded\n");
	return i2c_add_adapter(&stub_adapter);
}

static void __exit i2c_stub_exit(void)
{
	i2c_del_adapter(&stub_adapter);
}

MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
MODULE_DESCRIPTION("I2C stub driver");
MODULE_LICENSE("GPL");

module_init(i2c_stub_init);
module_exit(i2c_stub_exit);


