2 i2c-stub.c - Part of lm_sensors, Linux kernel modules for hardware
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.
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>
31 static u8 stub_bytes[256];
32 static u16 stub_words[256];
34 /* Return -1 on error. */
35 static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
36 char read_write, u8 command, int size, union i2c_smbus_data * data)
43 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
47 case I2C_SMBUS_BYTE_DATA:
48 if (read_write == I2C_SMBUS_WRITE) {
49 stub_bytes[command] = data->byte;
50 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
51 "wrote 0x%02x at 0x%02x.\n",
52 addr, data->byte, command);
54 data->byte = stub_bytes[command];
55 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
56 "read 0x%02x at 0x%02x.\n",
57 addr, data->byte, command);
63 case I2C_SMBUS_WORD_DATA:
64 if (read_write == I2C_SMBUS_WRITE) {
65 stub_words[command] = data->word;
66 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
67 "wrote 0x%04x at 0x%02x.\n",
68 addr, data->word, command);
70 data->word = stub_words[command];
71 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
72 "read 0x%04x at 0x%02x.\n",
73 addr, data->word, command);
80 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
88 static u32 stub_func(struct i2c_adapter *adapter)
90 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE_DATA |
91 I2C_FUNC_SMBUS_WORD_DATA;
94 static struct i2c_algorithm smbus_algorithm = {
95 .name = "Non-I2C SMBus adapter",
97 .functionality = stub_func,
98 .smbus_xfer = stub_xfer,
101 static struct i2c_adapter stub_adapter = {
102 .owner = THIS_MODULE,
103 .class = I2C_CLASS_HWMON,
104 .algo = &smbus_algorithm,
105 .name = "SMBus stub driver",
108 static int __init i2c_stub_init(void)
110 printk(KERN_INFO "i2c-stub loaded\n");
111 return i2c_add_adapter(&stub_adapter);
114 static void __exit i2c_stub_exit(void)
116 i2c_del_adapter(&stub_adapter);
119 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
120 MODULE_DESCRIPTION("I2C stub driver");
121 MODULE_LICENSE("GPL");
123 module_init(i2c_stub_init);
124 module_exit(i2c_stub_exit);