i2c-stub.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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;
  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. default:
  101. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
  102. ret = -EOPNOTSUPP;
  103. break;
  104. } /* switch (size) */
  105. return ret;
  106. }
  107. static u32 stub_func(struct i2c_adapter *adapter)
  108. {
  109. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  110. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
  111. }
  112. static const struct i2c_algorithm smbus_algorithm = {
  113. .functionality = stub_func,
  114. .smbus_xfer = stub_xfer,
  115. };
  116. static struct i2c_adapter stub_adapter = {
  117. .owner = THIS_MODULE,
  118. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  119. .algo = &smbus_algorithm,
  120. .name = "SMBus stub driver",
  121. };
  122. static int __init i2c_stub_init(void)
  123. {
  124. int i, ret;
  125. if (!chip_addr[0]) {
  126. printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
  127. return -ENODEV;
  128. }
  129. for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
  130. if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
  131. printk(KERN_ERR "i2c-stub: Invalid chip address "
  132. "0x%02x\n", chip_addr[i]);
  133. return -EINVAL;
  134. }
  135. printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n",
  136. chip_addr[i]);
  137. }
  138. /* Allocate memory for all chips at once */
  139. stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
  140. if (!stub_chips) {
  141. printk(KERN_ERR "i2c-stub: Out of memory\n");
  142. return -ENOMEM;
  143. }
  144. ret = i2c_add_adapter(&stub_adapter);
  145. if (ret)
  146. kfree(stub_chips);
  147. return ret;
  148. }
  149. static void __exit i2c_stub_exit(void)
  150. {
  151. i2c_del_adapter(&stub_adapter);
  152. kfree(stub_chips);
  153. }
  154. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  155. MODULE_DESCRIPTION("I2C stub driver");
  156. MODULE_LICENSE("GPL");
  157. module_init(i2c_stub_init);
  158. module_exit(i2c_stub_exit);