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