i2c-stub.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. i2c-stub.c - I2C/SMBus chip emulator
  3. Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
  4. Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
  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. #define MAX_CHIPS 10
  25. static unsigned short chip_addr[MAX_CHIPS];
  26. module_param_array(chip_addr, ushort, NULL, S_IRUGO);
  27. MODULE_PARM_DESC(chip_addr,
  28. "Chip addresses (up to 10, between 0x03 and 0x77)");
  29. static unsigned long functionality = ~0UL;
  30. module_param(functionality, ulong, S_IRUGO | S_IWUSR);
  31. MODULE_PARM_DESC(functionality, "Override functionality bitfield");
  32. struct stub_chip {
  33. u8 pointer;
  34. u16 words[256]; /* Byte operations use the LSB as per SMBus
  35. specification */
  36. };
  37. static struct stub_chip *stub_chips;
  38. /* Return negative errno on error. */
  39. static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
  40. char read_write, u8 command, int size, union i2c_smbus_data * data)
  41. {
  42. s32 ret;
  43. int i, len;
  44. struct stub_chip *chip = NULL;
  45. /* Search for the right chip */
  46. for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
  47. if (addr == chip_addr[i]) {
  48. chip = stub_chips + i;
  49. break;
  50. }
  51. }
  52. if (!chip)
  53. return -ENODEV;
  54. switch (size) {
  55. case I2C_SMBUS_QUICK:
  56. dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
  57. ret = 0;
  58. break;
  59. case I2C_SMBUS_BYTE:
  60. if (read_write == I2C_SMBUS_WRITE) {
  61. chip->pointer = command;
  62. dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
  63. "wrote 0x%02x.\n",
  64. addr, command);
  65. } else {
  66. data->byte = chip->words[chip->pointer++] & 0xff;
  67. dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
  68. "read 0x%02x.\n",
  69. addr, data->byte);
  70. }
  71. ret = 0;
  72. break;
  73. case I2C_SMBUS_BYTE_DATA:
  74. if (read_write == I2C_SMBUS_WRITE) {
  75. chip->words[command] &= 0xff00;
  76. chip->words[command] |= data->byte;
  77. dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
  78. "wrote 0x%02x at 0x%02x.\n",
  79. addr, data->byte, command);
  80. } else {
  81. data->byte = chip->words[command] & 0xff;
  82. dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
  83. "read 0x%02x at 0x%02x.\n",
  84. addr, data->byte, command);
  85. }
  86. chip->pointer = command + 1;
  87. ret = 0;
  88. break;
  89. case I2C_SMBUS_WORD_DATA:
  90. if (read_write == I2C_SMBUS_WRITE) {
  91. chip->words[command] = data->word;
  92. dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
  93. "wrote 0x%04x at 0x%02x.\n",
  94. addr, data->word, command);
  95. } else {
  96. data->word = chip->words[command];
  97. dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
  98. "read 0x%04x at 0x%02x.\n",
  99. addr, data->word, command);
  100. }
  101. ret = 0;
  102. break;
  103. case I2C_SMBUS_I2C_BLOCK_DATA:
  104. len = data->block[0];
  105. if (read_write == I2C_SMBUS_WRITE) {
  106. for (i = 0; i < len; i++) {
  107. chip->words[command + i] &= 0xff00;
  108. chip->words[command + i] |= data->block[1 + i];
  109. }
  110. dev_dbg(&adap->dev, "i2c block data - addr 0x%02x, "
  111. "wrote %d bytes at 0x%02x.\n",
  112. addr, len, command);
  113. } else {
  114. for (i = 0; i < len; i++) {
  115. data->block[1 + i] =
  116. chip->words[command + i] & 0xff;
  117. }
  118. dev_dbg(&adap->dev, "i2c block data - addr 0x%02x, "
  119. "read %d bytes at 0x%02x.\n",
  120. addr, len, command);
  121. }
  122. ret = 0;
  123. break;
  124. default:
  125. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
  126. ret = -EOPNOTSUPP;
  127. break;
  128. } /* switch (size) */
  129. return ret;
  130. }
  131. static u32 stub_func(struct i2c_adapter *adapter)
  132. {
  133. return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  134. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  135. I2C_FUNC_SMBUS_I2C_BLOCK) & functionality;
  136. }
  137. static const struct i2c_algorithm smbus_algorithm = {
  138. .functionality = stub_func,
  139. .smbus_xfer = stub_xfer,
  140. };
  141. static struct i2c_adapter stub_adapter = {
  142. .owner = THIS_MODULE,
  143. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  144. .algo = &smbus_algorithm,
  145. .name = "SMBus stub driver",
  146. };
  147. static int __init i2c_stub_init(void)
  148. {
  149. int i, ret;
  150. if (!chip_addr[0]) {
  151. printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
  152. return -ENODEV;
  153. }
  154. for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
  155. if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
  156. printk(KERN_ERR "i2c-stub: Invalid chip address "
  157. "0x%02x\n", chip_addr[i]);
  158. return -EINVAL;
  159. }
  160. printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n",
  161. chip_addr[i]);
  162. }
  163. /* Allocate memory for all chips at once */
  164. stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
  165. if (!stub_chips) {
  166. printk(KERN_ERR "i2c-stub: Out of memory\n");
  167. return -ENOMEM;
  168. }
  169. ret = i2c_add_adapter(&stub_adapter);
  170. if (ret)
  171. kfree(stub_chips);
  172. return ret;
  173. }
  174. static void __exit i2c_stub_exit(void)
  175. {
  176. i2c_del_adapter(&stub_adapter);
  177. kfree(stub_chips);
  178. }
  179. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  180. MODULE_DESCRIPTION("I2C stub driver");
  181. MODULE_LICENSE("GPL");
  182. module_init(i2c_stub_init);
  183. module_exit(i2c_stub_exit);