fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / i2c / busses / i2c-stub.c
index fec0bff..a54adc5 100644 (file)
 
 #define DEBUG 1
 
-#include <linux/config.h>
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/i2c.h>
 
+static unsigned short chip_addr;
+module_param(chip_addr, ushort, S_IRUGO);
+MODULE_PARM_DESC(chip_addr, "Chip address (between 0x03 and 0x77)\n");
+
+static u8  stub_pointer;
 static u8  stub_bytes[256];
 static u16 stub_words[256];
 
@@ -37,6 +41,9 @@ static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
 {
        s32 ret;
 
+       if (addr != chip_addr)
+               return -ENODEV;
+
        switch (size) {
 
        case I2C_SMBUS_QUICK:
@@ -44,6 +51,22 @@ static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
                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[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) {
                        stub_bytes[command] = data->byte;
@@ -56,6 +79,7 @@ static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
                                        "read  0x%02x at 0x%02x.\n",
                                        addr, data->byte, command);
                }
+               stub_pointer = command + 1;
 
                ret = 0;
                break;
@@ -87,13 +111,11 @@ static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
 
 static u32 stub_func(struct i2c_adapter *adapter)
 {
-       return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE_DATA |
-               I2C_FUNC_SMBUS_WORD_DATA;
+       return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
+               I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
 }
 
-static struct i2c_algorithm smbus_algorithm = {
-       .name           = "Non-I2C SMBus adapter",
-       .id             = I2C_ALGO_SMBUS,
+static const struct i2c_algorithm smbus_algorithm = {
        .functionality  = stub_func,
        .smbus_xfer     = stub_xfer,
 };
@@ -107,7 +129,17 @@ static struct i2c_adapter stub_adapter = {
 
 static int __init i2c_stub_init(void)
 {
-       printk(KERN_INFO "i2c-stub loaded\n");
+       if (!chip_addr) {
+               printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
+               return -ENODEV;
+       }
+       if (chip_addr < 0x03 || chip_addr > 0x77) {
+               printk(KERN_ERR "i2c-stub: Invalid chip address 0x%02x\n",
+                      chip_addr);
+               return -EINVAL;
+       }
+
+       printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n", chip_addr);
        return i2c_add_adapter(&stub_adapter);
 }