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 | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 5 | Copyright (C) 2007-2014 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #define DEBUG 1 |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/kernel.h> |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 23 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/errno.h> |
| 25 | #include <linux/i2c.h> |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 26 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 28 | #define MAX_CHIPS 10 |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Support for I2C_FUNC_SMBUS_BLOCK_DATA is disabled by default and must |
| 32 | * be enabled explicitly by setting the I2C_FUNC_SMBUS_BLOCK_DATA bits |
| 33 | * in the 'functionality' module parameter. |
| 34 | */ |
| 35 | #define STUB_FUNC_DEFAULT \ |
| 36 | (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \ |
| 37 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \ |
| 38 | I2C_FUNC_SMBUS_I2C_BLOCK) |
| 39 | |
| 40 | #define STUB_FUNC_ALL \ |
| 41 | (STUB_FUNC_DEFAULT | I2C_FUNC_SMBUS_BLOCK_DATA) |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 42 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 43 | static unsigned short chip_addr[MAX_CHIPS]; |
| 44 | module_param_array(chip_addr, ushort, NULL, S_IRUGO); |
| 45 | MODULE_PARM_DESC(chip_addr, |
Jean Delvare | 85d6931 | 2008-04-29 23:11:37 +0200 | [diff] [blame] | 46 | "Chip addresses (up to 10, between 0x03 and 0x77)"); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 47 | |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 48 | static unsigned long functionality = STUB_FUNC_DEFAULT; |
Jean Delvare | 38f41f2 | 2009-12-06 17:06:29 +0100 | [diff] [blame] | 49 | module_param(functionality, ulong, S_IRUGO | S_IWUSR); |
| 50 | MODULE_PARM_DESC(functionality, "Override functionality bitfield"); |
| 51 | |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 52 | /* Some chips have banked register ranges */ |
| 53 | |
| 54 | static u8 bank_reg[MAX_CHIPS]; |
| 55 | module_param_array(bank_reg, byte, NULL, S_IRUGO); |
| 56 | MODULE_PARM_DESC(bank_reg, "Bank register"); |
| 57 | |
| 58 | static u8 bank_mask[MAX_CHIPS]; |
| 59 | module_param_array(bank_mask, byte, NULL, S_IRUGO); |
| 60 | MODULE_PARM_DESC(bank_mask, "Bank value mask"); |
| 61 | |
| 62 | static u8 bank_start[MAX_CHIPS]; |
| 63 | module_param_array(bank_start, byte, NULL, S_IRUGO); |
| 64 | MODULE_PARM_DESC(bank_start, "First banked register"); |
| 65 | |
| 66 | static u8 bank_end[MAX_CHIPS]; |
| 67 | module_param_array(bank_end, byte, NULL, S_IRUGO); |
| 68 | MODULE_PARM_DESC(bank_end, "Last banked register"); |
| 69 | |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 70 | struct smbus_block_data { |
| 71 | struct list_head node; |
| 72 | u8 command; |
| 73 | u8 len; |
| 74 | u8 block[I2C_SMBUS_BLOCK_MAX]; |
| 75 | }; |
| 76 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 77 | struct stub_chip { |
| 78 | u8 pointer; |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 79 | u16 words[256]; /* Byte operations use the LSB as per SMBus |
| 80 | specification */ |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 81 | struct list_head smbus_blocks; |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 82 | |
| 83 | /* For chips with banks, extra registers are allocated dynamically */ |
| 84 | u8 bank_reg; |
| 85 | u8 bank_shift; |
| 86 | u8 bank_mask; |
| 87 | u8 bank_sel; /* Currently selected bank */ |
| 88 | u8 bank_start; |
| 89 | u8 bank_end; |
| 90 | u16 bank_size; |
| 91 | u16 *bank_words; /* Room for bank_mask * bank_size registers */ |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static struct stub_chip *stub_chips; |
Jean Delvare | 1dff598 | 2014-07-10 12:45:11 +0200 | [diff] [blame] | 95 | static int stub_chips_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 97 | static struct smbus_block_data *stub_find_block(struct device *dev, |
| 98 | struct stub_chip *chip, |
| 99 | u8 command, bool create) |
| 100 | { |
| 101 | struct smbus_block_data *b, *rb = NULL; |
| 102 | |
| 103 | list_for_each_entry(b, &chip->smbus_blocks, node) { |
| 104 | if (b->command == command) { |
| 105 | rb = b; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | if (rb == NULL && create) { |
| 110 | rb = devm_kzalloc(dev, sizeof(*rb), GFP_KERNEL); |
| 111 | if (rb == NULL) |
| 112 | return rb; |
| 113 | rb->command = command; |
| 114 | list_add(&rb->node, &chip->smbus_blocks); |
| 115 | } |
| 116 | return rb; |
| 117 | } |
| 118 | |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 119 | static u16 *stub_get_wordp(struct stub_chip *chip, u8 offset) |
| 120 | { |
| 121 | if (chip->bank_sel && |
| 122 | offset >= chip->bank_start && offset <= chip->bank_end) |
| 123 | return chip->bank_words + |
| 124 | (chip->bank_sel - 1) * chip->bank_size + |
| 125 | offset - chip->bank_start; |
| 126 | else |
| 127 | return chip->words + offset; |
| 128 | } |
| 129 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 130 | /* Return negative errno on error. */ |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 131 | static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, |
| 132 | char read_write, u8 command, int size, union i2c_smbus_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | s32 ret; |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 135 | int i, len; |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 136 | struct stub_chip *chip = NULL; |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 137 | struct smbus_block_data *b; |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 138 | u16 *wordp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 140 | /* Search for the right chip */ |
Jean Delvare | 1dff598 | 2014-07-10 12:45:11 +0200 | [diff] [blame] | 141 | for (i = 0; i < stub_chips_nr; i++) { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 142 | if (addr == chip_addr[i]) { |
| 143 | chip = stub_chips + i; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | if (!chip) |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 148 | return -ENODEV; |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | switch (size) { |
| 151 | |
| 152 | case I2C_SMBUS_QUICK: |
| 153 | dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr); |
| 154 | ret = 0; |
| 155 | break; |
| 156 | |
| 157 | case I2C_SMBUS_BYTE: |
| 158 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 159 | chip->pointer = command; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 160 | dev_dbg(&adap->dev, |
| 161 | "smbus byte - addr 0x%02x, wrote 0x%02x.\n", |
| 162 | addr, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } else { |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 164 | wordp = stub_get_wordp(chip, chip->pointer++); |
| 165 | data->byte = *wordp & 0xff; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 166 | dev_dbg(&adap->dev, |
| 167 | "smbus byte - addr 0x%02x, read 0x%02x.\n", |
| 168 | addr, data->byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | ret = 0; |
| 172 | break; |
| 173 | |
| 174 | case I2C_SMBUS_BYTE_DATA: |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 175 | wordp = stub_get_wordp(chip, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 177 | *wordp &= 0xff00; |
| 178 | *wordp |= data->byte; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 179 | dev_dbg(&adap->dev, |
| 180 | "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n", |
| 181 | addr, data->byte, command); |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 182 | |
| 183 | /* Set the bank as needed */ |
| 184 | if (chip->bank_words && command == chip->bank_reg) { |
| 185 | chip->bank_sel = |
| 186 | (data->byte >> chip->bank_shift) |
| 187 | & chip->bank_mask; |
| 188 | dev_dbg(&adap->dev, |
| 189 | "switching to bank %u.\n", |
| 190 | chip->bank_sel); |
| 191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } else { |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 193 | data->byte = *wordp & 0xff; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 194 | dev_dbg(&adap->dev, |
| 195 | "smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n", |
| 196 | addr, data->byte, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 198 | chip->pointer = command + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | ret = 0; |
| 201 | break; |
| 202 | |
| 203 | case I2C_SMBUS_WORD_DATA: |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 204 | wordp = stub_get_wordp(chip, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 206 | *wordp = data->word; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 207 | dev_dbg(&adap->dev, |
| 208 | "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n", |
| 209 | addr, data->word, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } else { |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 211 | data->word = *wordp; |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 212 | dev_dbg(&adap->dev, |
| 213 | "smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n", |
| 214 | addr, data->word, command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | ret = 0; |
| 218 | break; |
| 219 | |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 220 | case I2C_SMBUS_I2C_BLOCK_DATA: |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 221 | /* |
| 222 | * We ignore banks here, because banked chips don't use I2C |
| 223 | * block transfers |
| 224 | */ |
Jean Delvare | 0f6ba0d | 2014-07-13 17:17:17 +0200 | [diff] [blame] | 225 | if (data->block[0] > 256 - command) /* Avoid overrun */ |
| 226 | data->block[0] = 256 - command; |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 227 | len = data->block[0]; |
| 228 | if (read_write == I2C_SMBUS_WRITE) { |
| 229 | for (i = 0; i < len; i++) { |
| 230 | chip->words[command + i] &= 0xff00; |
| 231 | chip->words[command + i] |= data->block[1 + i]; |
| 232 | } |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 233 | dev_dbg(&adap->dev, |
| 234 | "i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n", |
| 235 | addr, len, command); |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 236 | } else { |
| 237 | for (i = 0; i < len; i++) { |
| 238 | data->block[1 + i] = |
| 239 | chip->words[command + i] & 0xff; |
| 240 | } |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 241 | dev_dbg(&adap->dev, |
| 242 | "i2c block data - addr 0x%02x, read %d bytes at 0x%02x.\n", |
| 243 | addr, len, command); |
Jean Delvare | 4710317 | 2009-12-06 17:06:28 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | ret = 0; |
| 247 | break; |
| 248 | |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 249 | case I2C_SMBUS_BLOCK_DATA: |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 250 | /* |
| 251 | * We ignore banks here, because chips typically don't use both |
| 252 | * banks and SMBus block transfers |
| 253 | */ |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 254 | b = stub_find_block(&adap->dev, chip, command, false); |
| 255 | if (read_write == I2C_SMBUS_WRITE) { |
| 256 | len = data->block[0]; |
| 257 | if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) { |
| 258 | ret = -EINVAL; |
| 259 | break; |
| 260 | } |
| 261 | if (b == NULL) { |
| 262 | b = stub_find_block(&adap->dev, chip, command, |
| 263 | true); |
| 264 | if (b == NULL) { |
| 265 | ret = -ENOMEM; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | /* Largest write sets read block length */ |
| 270 | if (len > b->len) |
| 271 | b->len = len; |
| 272 | for (i = 0; i < len; i++) |
| 273 | b->block[i] = data->block[i + 1]; |
| 274 | /* update for byte and word commands */ |
| 275 | chip->words[command] = (b->block[0] << 8) | b->len; |
| 276 | dev_dbg(&adap->dev, |
| 277 | "smbus block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n", |
| 278 | addr, len, command); |
| 279 | } else { |
| 280 | if (b == NULL) { |
| 281 | dev_dbg(&adap->dev, |
| 282 | "SMBus block read command without prior block write not supported\n"); |
| 283 | ret = -EOPNOTSUPP; |
| 284 | break; |
| 285 | } |
| 286 | len = b->len; |
| 287 | data->block[0] = len; |
| 288 | for (i = 0; i < len; i++) |
| 289 | data->block[i + 1] = b->block[i]; |
| 290 | dev_dbg(&adap->dev, |
| 291 | "smbus block data - addr 0x%02x, read %d bytes at 0x%02x.\n", |
| 292 | addr, len, command); |
| 293 | } |
| 294 | |
| 295 | ret = 0; |
| 296 | break; |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | default: |
| 299 | dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n"); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 300 | ret = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | break; |
| 302 | } /* switch (size) */ |
| 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static u32 stub_func(struct i2c_adapter *adapter) |
| 308 | { |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 309 | return STUB_FUNC_ALL & functionality; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 312 | static const struct i2c_algorithm smbus_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | .functionality = stub_func, |
| 314 | .smbus_xfer = stub_xfer, |
| 315 | }; |
| 316 | |
| 317 | static struct i2c_adapter stub_adapter = { |
| 318 | .owner = THIS_MODULE, |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 319 | .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | .algo = &smbus_algorithm, |
| 321 | .name = "SMBus stub driver", |
| 322 | }; |
| 323 | |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 324 | static int __init i2c_stub_allocate_banks(int i) |
| 325 | { |
| 326 | struct stub_chip *chip = stub_chips + i; |
| 327 | |
| 328 | chip->bank_reg = bank_reg[i]; |
| 329 | chip->bank_start = bank_start[i]; |
| 330 | chip->bank_end = bank_end[i]; |
| 331 | chip->bank_size = bank_end[i] - bank_start[i] + 1; |
| 332 | |
| 333 | /* We assume that all bits in the mask are contiguous */ |
| 334 | chip->bank_mask = bank_mask[i]; |
| 335 | while (!(chip->bank_mask & 1)) { |
| 336 | chip->bank_shift++; |
| 337 | chip->bank_mask >>= 1; |
| 338 | } |
| 339 | |
| 340 | chip->bank_words = kzalloc(chip->bank_mask * chip->bank_size * |
| 341 | sizeof(u16), GFP_KERNEL); |
| 342 | if (!chip->bank_words) |
| 343 | return -ENOMEM; |
| 344 | |
| 345 | pr_debug("i2c-stub: Allocated %u banks of %u words each (registers 0x%02x to 0x%02x)\n", |
| 346 | chip->bank_mask, chip->bank_size, chip->bank_start, |
| 347 | chip->bank_end); |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static void i2c_stub_free(void) |
| 353 | { |
| 354 | int i; |
| 355 | |
| 356 | for (i = 0; i < stub_chips_nr; i++) |
| 357 | kfree(stub_chips[i].bank_words); |
| 358 | kfree(stub_chips); |
| 359 | } |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | static int __init i2c_stub_init(void) |
| 362 | { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 363 | int i, ret; |
| 364 | |
| 365 | if (!chip_addr[0]) { |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 366 | pr_err("i2c-stub: Please specify a chip address\n"); |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 367 | return -ENODEV; |
| 368 | } |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 369 | |
| 370 | for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) { |
| 371 | if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) { |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 372 | pr_err("i2c-stub: Invalid chip address 0x%02x\n", |
| 373 | chip_addr[i]); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 374 | return -EINVAL; |
| 375 | } |
| 376 | |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 377 | 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] | 378 | } |
| 379 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 380 | /* Allocate memory for all chips at once */ |
Jean Delvare | 1dff598 | 2014-07-10 12:45:11 +0200 | [diff] [blame] | 381 | stub_chips_nr = i; |
| 382 | stub_chips = kcalloc(stub_chips_nr, sizeof(struct stub_chip), |
| 383 | GFP_KERNEL); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 384 | if (!stub_chips) { |
Jean Delvare | 31d178b | 2012-10-28 21:37:00 +0100 | [diff] [blame] | 385 | pr_err("i2c-stub: Out of memory\n"); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 386 | return -ENOMEM; |
| 387 | } |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 388 | for (i = 0; i < stub_chips_nr; i++) { |
Guenter Roeck | 6f16b75 | 2014-07-17 09:56:03 -0700 | [diff] [blame] | 389 | INIT_LIST_HEAD(&stub_chips[i].smbus_blocks); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 390 | |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 391 | /* Allocate extra memory for banked register ranges */ |
| 392 | if (bank_mask[i]) { |
| 393 | ret = i2c_stub_allocate_banks(i); |
| 394 | if (ret) |
| 395 | goto fail_free; |
| 396 | } |
| 397 | } |
| 398 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 399 | ret = i2c_add_adapter(&stub_adapter); |
| 400 | if (ret) |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 401 | goto fail_free; |
| 402 | |
| 403 | return 0; |
| 404 | |
| 405 | fail_free: |
| 406 | i2c_stub_free(); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 407 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static void __exit i2c_stub_exit(void) |
| 411 | { |
| 412 | i2c_del_adapter(&stub_adapter); |
Jean Delvare | 2408c17 | 2014-07-10 12:56:59 +0200 | [diff] [blame] | 413 | i2c_stub_free(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 417 | MODULE_DESCRIPTION("I2C stub driver"); |
| 418 | MODULE_LICENSE("GPL"); |
| 419 | |
| 420 | module_init(i2c_stub_init); |
| 421 | module_exit(i2c_stub_exit); |