i2c-stub.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 u8 stub_pointer;
  24. static u8 stub_bytes[256];
  25. static u16 stub_words[256];
  26. /* Return -1 on error. */
  27. static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
  28. char read_write, u8 command, int size, union i2c_smbus_data * data)
  29. {
  30. s32 ret;
  31. switch (size) {
  32. case I2C_SMBUS_QUICK:
  33. dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
  34. ret = 0;
  35. break;
  36. case I2C_SMBUS_BYTE:
  37. if (read_write == I2C_SMBUS_WRITE) {
  38. stub_pointer = command;
  39. dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
  40. "wrote 0x%02x.\n",
  41. addr, command);
  42. } else {
  43. data->byte = stub_bytes[stub_pointer++];
  44. dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
  45. "read 0x%02x.\n",
  46. addr, data->byte);
  47. }
  48. ret = 0;
  49. break;
  50. case I2C_SMBUS_BYTE_DATA:
  51. if (read_write == I2C_SMBUS_WRITE) {
  52. stub_bytes[command] = data->byte;
  53. dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
  54. "wrote 0x%02x at 0x%02x.\n",
  55. addr, data->byte, command);
  56. } else {
  57. data->byte = stub_bytes[command];
  58. dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
  59. "read 0x%02x at 0x%02x.\n",
  60. addr, data->byte, command);
  61. }
  62. stub_pointer = command + 1;
  63. ret = 0;
  64. break;
  65. case I2C_SMBUS_WORD_DATA:
  66. if (read_write == I2C_SMBUS_WRITE) {
  67. stub_words[command] = data->word;
  68. dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
  69. "wrote 0x%04x at 0x%02x.\n",
  70. addr, data->word, command);
  71. } else {
  72. data->word = stub_words[command];
  73. dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
  74. "read 0x%04x at 0x%02x.\n",
  75. addr, data->word, command);
  76. }
  77. ret = 0;
  78. break;
  79. default:
  80. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
  81. ret = -1;
  82. break;
  83. } /* switch (size) */
  84. return ret;
  85. }
  86. static u32 stub_func(struct i2c_adapter *adapter)
  87. {
  88. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  89. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
  90. }
  91. static struct i2c_algorithm smbus_algorithm = {
  92. .functionality = stub_func,
  93. .smbus_xfer = stub_xfer,
  94. };
  95. static struct i2c_adapter stub_adapter = {
  96. .owner = THIS_MODULE,
  97. .class = I2C_CLASS_HWMON,
  98. .algo = &smbus_algorithm,
  99. .name = "SMBus stub driver",
  100. };
  101. static int __init i2c_stub_init(void)
  102. {
  103. printk(KERN_INFO "i2c-stub loaded\n");
  104. return i2c_add_adapter(&stub_adapter);
  105. }
  106. static void __exit i2c_stub_exit(void)
  107. {
  108. i2c_del_adapter(&stub_adapter);
  109. }
  110. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  111. MODULE_DESCRIPTION("I2C stub driver");
  112. MODULE_LICENSE("GPL");
  113. module_init(i2c_stub_init);
  114. module_exit(i2c_stub_exit);