Linux-2.6.12-rc2
[linux-2.6.git] / drivers / i2c / busses / i2c-stub.c
1 /*
2     i2c-stub.c - Part of lm_sensors, Linux kernel modules for hardware
3               monitoring
4
5     Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
6
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.
11
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.
16
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.
20 */
21
22 #define DEBUG 1
23
24 #include <linux/config.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/i2c.h>
30
31 static u8  stub_pointer;
32 static u8  stub_bytes[256];
33 static u16 stub_words[256];
34
35 /* Return -1 on error. */
36 static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
37         char read_write, u8 command, int size, union i2c_smbus_data * data)
38 {
39         s32 ret;
40
41         switch (size) {
42
43         case I2C_SMBUS_QUICK:
44                 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
45                 ret = 0;
46                 break;
47
48         case I2C_SMBUS_BYTE:
49                 if (read_write == I2C_SMBUS_WRITE) {
50                         stub_pointer = command;
51                         dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
52                                         "wrote 0x%02x.\n",
53                                         addr, command);
54                 } else {
55                         data->byte = stub_bytes[stub_pointer++];
56                         dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
57                                         "read  0x%02x.\n",
58                                         addr, data->byte);
59                 }
60
61                 ret = 0;
62                 break;
63
64         case I2C_SMBUS_BYTE_DATA:
65                 if (read_write == I2C_SMBUS_WRITE) {
66                         stub_bytes[command] = data->byte;
67                         dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
68                                         "wrote 0x%02x at 0x%02x.\n",
69                                         addr, data->byte, command);
70                 } else {
71                         data->byte = stub_bytes[command];
72                         dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
73                                         "read  0x%02x at 0x%02x.\n",
74                                         addr, data->byte, command);
75                 }
76                 stub_pointer = command + 1;
77
78                 ret = 0;
79                 break;
80
81         case I2C_SMBUS_WORD_DATA:
82                 if (read_write == I2C_SMBUS_WRITE) {
83                         stub_words[command] = data->word;
84                         dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
85                                         "wrote 0x%04x at 0x%02x.\n",
86                                         addr, data->word, command);
87                 } else {
88                         data->word = stub_words[command];
89                         dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
90                                         "read  0x%04x at 0x%02x.\n",
91                                         addr, data->word, command);
92                 }
93
94                 ret = 0;
95                 break;
96
97         default:
98                 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
99                 ret = -1;
100                 break;
101         } /* switch (size) */
102
103         return ret;
104 }
105
106 static u32 stub_func(struct i2c_adapter *adapter)
107 {
108         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
109                 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
110 }
111
112 static struct i2c_algorithm smbus_algorithm = {
113         .name           = "Non-I2C SMBus adapter",
114         .id             = I2C_ALGO_SMBUS,
115         .functionality  = stub_func,
116         .smbus_xfer     = stub_xfer,
117 };
118
119 static struct i2c_adapter stub_adapter = {
120         .owner          = THIS_MODULE,
121         .class          = I2C_CLASS_HWMON,
122         .algo           = &smbus_algorithm,
123         .name           = "SMBus stub driver",
124 };
125
126 static int __init i2c_stub_init(void)
127 {
128         printk(KERN_INFO "i2c-stub loaded\n");
129         return i2c_add_adapter(&stub_adapter);
130 }
131
132 static void __exit i2c_stub_exit(void)
133 {
134         i2c_del_adapter(&stub_adapter);
135 }
136
137 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
138 MODULE_DESCRIPTION("I2C stub driver");
139 MODULE_LICENSE("GPL");
140
141 module_init(i2c_stub_init);
142 module_exit(i2c_stub_exit);
143