i2c-stub.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. i2c-stub.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
  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/errno.h>
  22. #include <linux/i2c.h>
  23. static unsigned short chip_addr;
  24. module_param(chip_addr, ushort, S_IRUGO);
  25. MODULE_PARM_DESC(chip_addr, "Chip address (between 0x03 and 0x77)\n");
  26. static u8 stub_pointer;
  27. static u8 stub_bytes[256];
  28. static u16 stub_words[256];
  29. /* Return -1 on error. */
  30. static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
  31. char read_write, u8 command, int size, union i2c_smbus_data * data)
  32. {
  33. s32 ret;
  34. if (addr != chip_addr)
  35. return -ENODEV;
  36. switch (size) {
  37. case I2C_SMBUS_QUICK:
  38. dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
  39. ret = 0;
  40. break;
  41. case I2C_SMBUS_BYTE:
  42. if (read_write == I2C_SMBUS_WRITE) {
  43. stub_pointer = command;
  44. dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
  45. "wrote 0x%02x.\n",
  46. addr, command);
  47. } else {
  48. data->byte = stub_bytes[stub_pointer++];
  49. dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
  50. "read 0x%02x.\n",
  51. addr, data->byte);
  52. }
  53. ret = 0;
  54. break;
  55. case I2C_SMBUS_BYTE_DATA:
  56. if (read_write == I2C_SMBUS_WRITE) {
  57. stub_bytes[command] = data->byte;
  58. dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
  59. "wrote 0x%02x at 0x%02x.\n",
  60. addr, data->byte, command);
  61. } else {
  62. data->byte = stub_bytes[command];
  63. dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
  64. "read 0x%02x at 0x%02x.\n",
  65. addr, data->byte, command);
  66. }
  67. stub_pointer = command + 1;
  68. ret = 0;
  69. break;
  70. case I2C_SMBUS_WORD_DATA:
  71. if (read_write == I2C_SMBUS_WRITE) {
  72. stub_words[command] = data->word;
  73. dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
  74. "wrote 0x%04x at 0x%02x.\n",
  75. addr, data->word, command);
  76. } else {
  77. data->word = stub_words[command];
  78. dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
  79. "read 0x%04x at 0x%02x.\n",
  80. addr, data->word, command);
  81. }
  82. ret = 0;
  83. break;
  84. default:
  85. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
  86. ret = -1;
  87. break;
  88. } /* switch (size) */
  89. return ret;
  90. }
  91. static u32 stub_func(struct i2c_adapter *adapter)
  92. {
  93. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  94. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
  95. }
  96. static const struct i2c_algorithm smbus_algorithm = {
  97. .functionality = stub_func,
  98. .smbus_xfer = stub_xfer,
  99. };
  100. static struct i2c_adapter stub_adapter = {
  101. .owner = THIS_MODULE,
  102. .class = I2C_CLASS_HWMON,
  103. .algo = &smbus_algorithm,
  104. .name = "SMBus stub driver",
  105. };
  106. static int __init i2c_stub_init(void)
  107. {
  108. if (!chip_addr) {
  109. printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
  110. return -ENODEV;
  111. }
  112. if (chip_addr < 0x03 || chip_addr > 0x77) {
  113. printk(KERN_ERR "i2c-stub: Invalid chip address 0x%02x\n",
  114. chip_addr);
  115. return -EINVAL;
  116. }
  117. printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n", chip_addr);
  118. return i2c_add_adapter(&stub_adapter);
  119. }
  120. static void __exit i2c_stub_exit(void)
  121. {
  122. i2c_del_adapter(&stub_adapter);
  123. }
  124. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  125. MODULE_DESCRIPTION("I2C stub driver");
  126. MODULE_LICENSE("GPL");
  127. module_init(i2c_stub_init);
  128. module_exit(i2c_stub_exit);