i2c-stub.c 5.4 KB

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