allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / i2c / busses / i2c-stub.c
bloba54adc50d162a5a580ae60a709b87bb802280c75
1 /*
2 i2c-stub.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
5 Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define DEBUG 1
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/i2c.h>
30 static unsigned short chip_addr;
31 module_param(chip_addr, ushort, S_IRUGO);
32 MODULE_PARM_DESC(chip_addr, "Chip address (between 0x03 and 0x77)\n");
34 static u8 stub_pointer;
35 static u8 stub_bytes[256];
36 static u16 stub_words[256];
38 /* Return -1 on error. */
39 static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
40 char read_write, u8 command, int size, union i2c_smbus_data * data)
42 s32 ret;
44 if (addr != chip_addr)
45 return -ENODEV;
47 switch (size) {
49 case I2C_SMBUS_QUICK:
50 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
51 ret = 0;
52 break;
54 case I2C_SMBUS_BYTE:
55 if (read_write == I2C_SMBUS_WRITE) {
56 stub_pointer = command;
57 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
58 "wrote 0x%02x.\n",
59 addr, command);
60 } else {
61 data->byte = stub_bytes[stub_pointer++];
62 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
63 "read 0x%02x.\n",
64 addr, data->byte);
67 ret = 0;
68 break;
70 case I2C_SMBUS_BYTE_DATA:
71 if (read_write == I2C_SMBUS_WRITE) {
72 stub_bytes[command] = data->byte;
73 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
74 "wrote 0x%02x at 0x%02x.\n",
75 addr, data->byte, command);
76 } else {
77 data->byte = stub_bytes[command];
78 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
79 "read 0x%02x at 0x%02x.\n",
80 addr, data->byte, command);
82 stub_pointer = command + 1;
84 ret = 0;
85 break;
87 case I2C_SMBUS_WORD_DATA:
88 if (read_write == I2C_SMBUS_WRITE) {
89 stub_words[command] = data->word;
90 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
91 "wrote 0x%04x at 0x%02x.\n",
92 addr, data->word, command);
93 } else {
94 data->word = stub_words[command];
95 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
96 "read 0x%04x at 0x%02x.\n",
97 addr, data->word, command);
100 ret = 0;
101 break;
103 default:
104 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
105 ret = -1;
106 break;
107 } /* switch (size) */
109 return ret;
112 static u32 stub_func(struct i2c_adapter *adapter)
114 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
115 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
118 static const struct i2c_algorithm smbus_algorithm = {
119 .functionality = stub_func,
120 .smbus_xfer = stub_xfer,
123 static struct i2c_adapter stub_adapter = {
124 .owner = THIS_MODULE,
125 .class = I2C_CLASS_HWMON,
126 .algo = &smbus_algorithm,
127 .name = "SMBus stub driver",
130 static int __init i2c_stub_init(void)
132 if (!chip_addr) {
133 printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
134 return -ENODEV;
136 if (chip_addr < 0x03 || chip_addr > 0x77) {
137 printk(KERN_ERR "i2c-stub: Invalid chip address 0x%02x\n",
138 chip_addr);
139 return -EINVAL;
142 printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n", chip_addr);
143 return i2c_add_adapter(&stub_adapter);
146 static void __exit i2c_stub_exit(void)
148 i2c_del_adapter(&stub_adapter);
151 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
152 MODULE_DESCRIPTION("I2C stub driver");
153 MODULE_LICENSE("GPL");
155 module_init(i2c_stub_init);
156 module_exit(i2c_stub_exit);