Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 2 | i2c-stub.c - I2C/SMBus chip emulator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
| 4 | Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com> |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 5 | Copyright (C) 2007, 2012 Jean Delvare <jdelvare@suse.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/kernel.h> |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 27 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/errno.h> |
| 29 | #include <linux/i2c.h> |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 30 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 32 | #define MAX_CHIPS 10 |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 33 | |
| 34 | /* |
| 35 | * Support for I2C_FUNC_SMBUS_BLOCK_DATA is disabled by default and must |
| 36 | * be enabled explicitly by setting the I2C_FUNC_SMBUS_BLOCK_DATA bits |
| 37 | * in the 'functionality' module parameter. |
| 38 | */ |
| 39 | #define STUB_FUNC_DEFAULT \ |
| 40 | (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \ |
| 41 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \ |
| 42 | I2C_FUNC_SMBUS_I2C_BLOCK) |
| 43 | |
| 44 | #define STUB_FUNC_ALL \ |
| 45 | (STUB_FUNC_DEFAULT | I2C_FUNC_SMBUS_BLOCK_DATA) |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 46 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 47 | static unsigned short chip_addr[MAX_CHIPS]; |
| 48 | module_param_array(chip_addr, ushort, NULL, S_IRUGO); |
| 49 | MODULE_PARM_DESC(chip_addr, |
Jean Delvare | 85d6931 | 2008-04-29 23:11:37 +0200 | [diff] [blame] | 50 | "Chip addresses (up to 10, between 0x03 and 0x77)"); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 51 | |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 52 | static unsigned long functionality = STUB_FUNC_DEFAULT; |
Jean Delvare | 38f41f2 | 2009-12-06 17:06:29 +0100 | [diff] [blame] | 53 | module_param(functionality, ulong, S_IRUGO | S_IWUSR); |
| 54 | MODULE_PARM_DESC(functionality, "Override functionality bitfield"); |
| 55 | |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 56 | struct smbus_block_data { |
| 57 | struct list_head node; |
| 58 | u8 command; |
| 59 | u8 len; |
| 60 | u8 block[I2C_SMBUS_BLOCK_MAX]; |
| 61 | }; |
| 62 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 63 | struct stub_chip { |
| 64 | u8 pointer; |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 65 | u16 words[256]; /* Byte operations use the LSB as per SMBus |
| 66 | specification */ |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 67 | struct list_head smbus_blocks; |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static struct stub_chip *stub_chips; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 72 | static struct smbus_block_data *stub_find_block(struct device *dev, |
| 73 | struct stub_chip *chip, |
| 74 | u8 command, bool create) |
| 75 | { |
| 76 | struct smbus_block_data *b, *rb = NULL; |
| 77 | |
| 78 | list_for_each_entry(b, &chip->smbus_blocks, node) { |
| 79 | if (b->command == command) { |
| 80 | rb = b; |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | if (rb == NULL && create) { |
| 85 | rb = devm_kzalloc(dev, sizeof(*rb), GFP_KERNEL); |
| 86 | if (rb == NULL) |
| 87 | return rb; |
| 88 | rb->command = command; |
| 89 | list_add(&rb->node, &chip->smbus_blocks); |
| 90 | } |
| 91 | return rb; |
| 92 | } |
| 93 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 94 | /* Return negative errno on error. */ |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 95 | static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, |
| 96 | char read_write, u8 command, int size, union i2c_smbus_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
| 98 | s32 ret; |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 99 | int i, len; |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 100 | struct stub_chip *chip = NULL; |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 101 | struct smbus_block_data *b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 103 | /* Search for the right chip */ |
| 104 | for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) { |
| 105 | if (addr == chip_addr[i]) { |
| 106 | chip = stub_chips + i; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | if (!chip) |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 111 | return -ENODEV; |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | switch (size) { |
| 114 | |
| 115 | case I2C_SMBUS_QUICK: |
| 116 | dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr); |
| 117 | ret = 0; |
| 118 | break; |
| 119 | |
| 120 | case I2C_SMBUS_BYTE: |
| 121 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 122 | chip->pointer = command; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 123 | dev_dbg(&adap->dev, |
| 124 | "smbus byte - addr 0x%02x, wrote 0x%02x.\n", |
| 125 | addr, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } else { |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 127 | data->byte = chip->words[chip->pointer++] & 0xff; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 128 | dev_dbg(&adap->dev, |
| 129 | "smbus byte - addr 0x%02x, read 0x%02x.\n", |
| 130 | addr, data->byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ret = 0; |
| 134 | break; |
| 135 | |
| 136 | case I2C_SMBUS_BYTE_DATA: |
| 137 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 138 | chip->words[command] &= 0xff00; |
| 139 | chip->words[command] |= data->byte; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 140 | dev_dbg(&adap->dev, |
| 141 | "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n", |
| 142 | addr, data->byte, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } else { |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 144 | data->byte = chip->words[command] & 0xff; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 145 | dev_dbg(&adap->dev, |
| 146 | "smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n", |
| 147 | addr, data->byte, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 149 | chip->pointer = command + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | ret = 0; |
| 152 | break; |
| 153 | |
| 154 | case I2C_SMBUS_WORD_DATA: |
| 155 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 156 | chip->words[command] = data->word; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 157 | dev_dbg(&adap->dev, |
| 158 | "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n", |
| 159 | addr, data->word, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } else { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 161 | data->word = chip->words[command]; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 162 | dev_dbg(&adap->dev, |
| 163 | "smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n", |
| 164 | addr, data->word, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | ret = 0; |
| 168 | break; |
| 169 | |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 170 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 171 | len = data->block[0]; |
| 172 | if (read_write == I2C_SMBUS_WRITE) { |
| 173 | for (i = 0; i < len; i++) { |
| 174 | chip->words[command + i] &= 0xff00; |
| 175 | chip->words[command + i] |= data->block[1 + i]; |
| 176 | } |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 177 | dev_dbg(&adap->dev, |
| 178 | "i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n", |
| 179 | addr, len, command); |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 180 | } else { |
| 181 | for (i = 0; i < len; i++) { |
| 182 | data->block[1 + i] = |
| 183 | chip->words[command + i] & 0xff; |
| 184 | } |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 185 | dev_dbg(&adap->dev, |
| 186 | "i2c block data - addr 0x%02x, read %d bytes at 0x%02x.\n", |
| 187 | addr, len, command); |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | ret = 0; |
| 191 | break; |
| 192 | |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 193 | case I2C_SMBUS_BLOCK_DATA: |
| 194 | b = stub_find_block(&adap->dev, chip, command, false); |
| 195 | if (read_write == I2C_SMBUS_WRITE) { |
| 196 | len = data->block[0]; |
| 197 | if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) { |
| 198 | ret = -EINVAL; |
| 199 | break; |
| 200 | } |
| 201 | if (b == NULL) { |
| 202 | b = stub_find_block(&adap->dev, chip, command, |
| 203 | true); |
| 204 | if (b == NULL) { |
| 205 | ret = -ENOMEM; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | /* Largest write sets read block length */ |
| 210 | if (len > b->len) |
| 211 | b->len = len; |
| 212 | for (i = 0; i < len; i++) |
| 213 | b->block[i] = data->block[i + 1]; |
| 214 | /* update for byte and word commands */ |
| 215 | chip->words[command] = (b->block[0] << 8) | b->len; |
| 216 | dev_dbg(&adap->dev, |
| 217 | "smbus block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n", |
| 218 | addr, len, command); |
| 219 | } else { |
| 220 | if (b == NULL) { |
| 221 | dev_dbg(&adap->dev, |
| 222 | "SMBus block read command without prior block write not supported\n"); |
| 223 | ret = -EOPNOTSUPP; |
| 224 | break; |
| 225 | } |
| 226 | len = b->len; |
| 227 | data->block[0] = len; |
| 228 | for (i = 0; i < len; i++) |
| 229 | data->block[i + 1] = b->block[i]; |
| 230 | dev_dbg(&adap->dev, |
| 231 | "smbus block data - addr 0x%02x, read %d bytes at 0x%02x.\n", |
| 232 | addr, len, command); |
| 233 | } |
| 234 | |
| 235 | ret = 0; |
| 236 | break; |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | default: |
| 239 | dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n"); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 240 | ret = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | break; |
| 242 | } /* switch (size) */ |
| 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static u32 stub_func(struct i2c_adapter *adapter) |
| 248 | { |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 249 | return STUB_FUNC_ALL & functionality; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 252 | static const struct i2c_algorithm smbus_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | .functionality = stub_func, |
| 254 | .smbus_xfer = stub_xfer, |
| 255 | }; |
| 256 | |
| 257 | static struct i2c_adapter stub_adapter = { |
| 258 | .owner = THIS_MODULE, |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 259 | .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | .algo = &smbus_algorithm, |
| 261 | .name = "SMBus stub driver", |
| 262 | }; |
| 263 | |
| 264 | static int __init i2c_stub_init(void) |
| 265 | { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 266 | int i, ret; |
| 267 | |
| 268 | if (!chip_addr[0]) { |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 269 | pr_err("i2c-stub: Please specify a chip address\n"); |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 270 | return -ENODEV; |
| 271 | } |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 272 | |
| 273 | for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) { |
| 274 | if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) { |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 275 | pr_err("i2c-stub: Invalid chip address 0x%02x\n", |
| 276 | chip_addr[i]); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 277 | return -EINVAL; |
| 278 | } |
| 279 | |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 280 | pr_info("i2c-stub: Virtual chip at 0x%02x\n", chip_addr[i]); |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 283 | /* Allocate memory for all chips at once */ |
| 284 | stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL); |
| 285 | if (!stub_chips) { |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 286 | pr_err("i2c-stub: Out of memory\n"); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 287 | return -ENOMEM; |
| 288 | } |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame^] | 289 | for (i--; i >= 0; i--) |
| 290 | INIT_LIST_HEAD(&stub_chips[i].smbus_blocks); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 291 | |
| 292 | ret = i2c_add_adapter(&stub_adapter); |
| 293 | if (ret) |
| 294 | kfree(stub_chips); |
| 295 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void __exit i2c_stub_exit(void) |
| 299 | { |
| 300 | i2c_del_adapter(&stub_adapter); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 301 | kfree(stub_chips); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 305 | MODULE_DESCRIPTION("I2C stub driver"); |
| 306 | MODULE_LICENSE("GPL"); |
| 307 | |
| 308 | module_init(i2c_stub_init); |
| 309 | module_exit(i2c_stub_exit); |