i2c-stub.c 3.6 KB

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