i2c-stub.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. i2c-stub.c - I2C/SMBus chip emulator
  3. Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
  4. Copyright (C) 2007, 2012 Jean Delvare <jdelvare@suse.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #define DEBUG 1
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/i2c.h>
  24. #include <linux/list.h>
  25. #define MAX_CHIPS 10
  26. /*
  27. * Support for I2C_FUNC_SMBUS_BLOCK_DATA is disabled by default and must
  28. * be enabled explicitly by setting the I2C_FUNC_SMBUS_BLOCK_DATA bits
  29. * in the 'functionality' module parameter.
  30. */
  31. #define STUB_FUNC_DEFAULT \
  32. (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \
  33. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \
  34. I2C_FUNC_SMBUS_I2C_BLOCK)
  35. #define STUB_FUNC_ALL \
  36. (STUB_FUNC_DEFAULT | I2C_FUNC_SMBUS_BLOCK_DATA)
  37. static unsigned short chip_addr[MAX_CHIPS];
  38. module_param_array(chip_addr, ushort, NULL, S_IRUGO);
  39. MODULE_PARM_DESC(chip_addr,
  40. "Chip addresses (up to 10, between 0x03 and 0x77)");
  41. static unsigned long functionality = STUB_FUNC_DEFAULT;
  42. module_param(functionality, ulong, S_IRUGO | S_IWUSR);
  43. MODULE_PARM_DESC(functionality, "Override functionality bitfield");
  44. struct smbus_block_data {
  45. struct list_head node;
  46. u8 command;
  47. u8 len;
  48. u8 block[I2C_SMBUS_BLOCK_MAX];
  49. };
  50. struct stub_chip {
  51. u8 pointer;
  52. u16 words[256]; /* Byte operations use the LSB as per SMBus
  53. specification */
  54. struct list_head smbus_blocks;
  55. };
  56. static struct stub_chip *stub_chips;
  57. static struct smbus_block_data *stub_find_block(struct device *dev,
  58. struct stub_chip *chip,
  59. u8 command, bool create)
  60. {
  61. struct smbus_block_data *b, *rb = NULL;
  62. list_for_each_entry(b, &chip->smbus_blocks, node) {
  63. if (b->command == command) {
  64. rb = b;
  65. break;
  66. }
  67. }
  68. if (rb == NULL && create) {
  69. rb = devm_kzalloc(dev, sizeof(*rb), GFP_KERNEL);
  70. if (rb == NULL)
  71. return rb;
  72. rb->command = command;
  73. list_add(&rb->node, &chip->smbus_blocks);
  74. }
  75. return rb;
  76. }
  77. /* Return negative errno on error. */
  78. static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
  79. char read_write, u8 command, int size, union i2c_smbus_data *data)
  80. {
  81. s32 ret;
  82. int i, len;
  83. struct stub_chip *chip = NULL;
  84. struct smbus_block_data *b;
  85. /* Search for the right chip */
  86. for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
  87. if (addr == chip_addr[i]) {
  88. chip = stub_chips + i;
  89. break;
  90. }
  91. }
  92. if (!chip)
  93. return -ENODEV;
  94. switch (size) {
  95. case I2C_SMBUS_QUICK:
  96. dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
  97. ret = 0;
  98. break;
  99. case I2C_SMBUS_BYTE:
  100. if (read_write == I2C_SMBUS_WRITE) {
  101. chip->pointer = command;
  102. dev_dbg(&adap->dev,
  103. "smbus byte - addr 0x%02x, wrote 0x%02x.\n",
  104. addr, command);
  105. } else {
  106. data->byte = chip->words[chip->pointer++] & 0xff;
  107. dev_dbg(&adap->dev,
  108. "smbus byte - addr 0x%02x, read 0x%02x.\n",
  109. addr, data->byte);
  110. }
  111. ret = 0;
  112. break;
  113. case I2C_SMBUS_BYTE_DATA:
  114. if (read_write == I2C_SMBUS_WRITE) {
  115. chip->words[command] &= 0xff00;
  116. chip->words[command] |= data->byte;
  117. dev_dbg(&adap->dev,
  118. "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n",
  119. addr, data->byte, command);
  120. } else {
  121. data->byte = chip->words[command] & 0xff;
  122. dev_dbg(&adap->dev,
  123. "smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n",
  124. addr, data->byte, command);
  125. }
  126. chip->pointer = command + 1;
  127. ret = 0;
  128. break;
  129. case I2C_SMBUS_WORD_DATA:
  130. if (read_write == I2C_SMBUS_WRITE) {
  131. chip->words[command] = data->word;
  132. dev_dbg(&adap->dev,
  133. "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n",
  134. addr, data->word, command);
  135. } else {
  136. data->word = chip->words[command];
  137. dev_dbg(&adap->dev,
  138. "smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n",
  139. addr, data->word, command);
  140. }
  141. ret = 0;
  142. break;
  143. case I2C_SMBUS_I2C_BLOCK_DATA:
  144. len = data->block[0];
  145. if (read_write == I2C_SMBUS_WRITE) {
  146. for (i = 0; i < len; i++) {
  147. chip->words[command + i] &= 0xff00;
  148. chip->words[command + i] |= data->block[1 + i];
  149. }
  150. dev_dbg(&adap->dev,
  151. "i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
  152. addr, len, command);
  153. } else {
  154. for (i = 0; i < len; i++) {
  155. data->block[1 + i] =
  156. chip->words[command + i] & 0xff;
  157. }
  158. dev_dbg(&adap->dev,
  159. "i2c block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
  160. addr, len, command);
  161. }
  162. ret = 0;
  163. break;
  164. case I2C_SMBUS_BLOCK_DATA:
  165. b = stub_find_block(&adap->dev, chip, command, false);
  166. if (read_write == I2C_SMBUS_WRITE) {
  167. len = data->block[0];
  168. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) {
  169. ret = -EINVAL;
  170. break;
  171. }
  172. if (b == NULL) {
  173. b = stub_find_block(&adap->dev, chip, command,
  174. true);
  175. if (b == NULL) {
  176. ret = -ENOMEM;
  177. break;
  178. }
  179. }
  180. /* Largest write sets read block length */
  181. if (len > b->len)
  182. b->len = len;
  183. for (i = 0; i < len; i++)
  184. b->block[i] = data->block[i + 1];
  185. /* update for byte and word commands */
  186. chip->words[command] = (b->block[0] << 8) | b->len;
  187. dev_dbg(&adap->dev,
  188. "smbus block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
  189. addr, len, command);
  190. } else {
  191. if (b == NULL) {
  192. dev_dbg(&adap->dev,
  193. "SMBus block read command without prior block write not supported\n");
  194. ret = -EOPNOTSUPP;
  195. break;
  196. }
  197. len = b->len;
  198. data->block[0] = len;
  199. for (i = 0; i < len; i++)
  200. data->block[i + 1] = b->block[i];
  201. dev_dbg(&adap->dev,
  202. "smbus block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
  203. addr, len, command);
  204. }
  205. ret = 0;
  206. break;
  207. default:
  208. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
  209. ret = -EOPNOTSUPP;
  210. break;
  211. } /* switch (size) */
  212. return ret;
  213. }
  214. static u32 stub_func(struct i2c_adapter *adapter)
  215. {
  216. return STUB_FUNC_ALL & functionality;
  217. }
  218. static const struct i2c_algorithm smbus_algorithm = {
  219. .functionality = stub_func,
  220. .smbus_xfer = stub_xfer,
  221. };
  222. static struct i2c_adapter stub_adapter = {
  223. .owner = THIS_MODULE,
  224. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  225. .algo = &smbus_algorithm,
  226. .name = "SMBus stub driver",
  227. };
  228. static int __init i2c_stub_init(void)
  229. {
  230. int i, ret;
  231. if (!chip_addr[0]) {
  232. pr_err("i2c-stub: Please specify a chip address\n");
  233. return -ENODEV;
  234. }
  235. for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
  236. if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
  237. pr_err("i2c-stub: Invalid chip address 0x%02x\n",
  238. chip_addr[i]);
  239. return -EINVAL;
  240. }
  241. pr_info("i2c-stub: Virtual chip at 0x%02x\n", chip_addr[i]);
  242. }
  243. /* Allocate memory for all chips at once */
  244. stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
  245. if (!stub_chips) {
  246. pr_err("i2c-stub: Out of memory\n");
  247. return -ENOMEM;
  248. }
  249. for (i--; i >= 0; i--)
  250. INIT_LIST_HEAD(&stub_chips[i].smbus_blocks);
  251. ret = i2c_add_adapter(&stub_adapter);
  252. if (ret)
  253. kfree(stub_chips);
  254. return ret;
  255. }
  256. static void __exit i2c_stub_exit(void)
  257. {
  258. i2c_del_adapter(&stub_adapter);
  259. kfree(stub_chips);
  260. }
  261. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  262. MODULE_DESCRIPTION("I2C stub driver");
  263. MODULE_LICENSE("GPL");
  264. module_init(i2c_stub_init);
  265. module_exit(i2c_stub_exit);