i2c-stub.c 5.6 KB

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