i2c-stub.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/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)\n");
  29. struct stub_chip {
  30. u8 pointer;
  31. u8 bytes[256];
  32. u16 words[256];
  33. };
  34. static struct stub_chip *stub_chips;
  35. /* Return -1 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->bytes[chip->pointer++];
  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->bytes[command] = data->byte;
  73. dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
  74. "wrote 0x%02x at 0x%02x.\n",
  75. addr, data->byte, command);
  76. } else {
  77. data->byte = chip->bytes[command];
  78. dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
  79. "read 0x%02x at 0x%02x.\n",
  80. addr, data->byte, command);
  81. }
  82. chip->pointer = command + 1;
  83. ret = 0;
  84. break;
  85. case I2C_SMBUS_WORD_DATA:
  86. if (read_write == I2C_SMBUS_WRITE) {
  87. chip->words[command] = data->word;
  88. dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
  89. "wrote 0x%04x at 0x%02x.\n",
  90. addr, data->word, command);
  91. } else {
  92. data->word = chip->words[command];
  93. dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
  94. "read 0x%04x at 0x%02x.\n",
  95. addr, data->word, command);
  96. }
  97. ret = 0;
  98. break;
  99. default:
  100. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
  101. ret = -1;
  102. break;
  103. } /* switch (size) */
  104. return ret;
  105. }
  106. static u32 stub_func(struct i2c_adapter *adapter)
  107. {
  108. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  109. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
  110. }
  111. static const struct i2c_algorithm smbus_algorithm = {
  112. .functionality = stub_func,
  113. .smbus_xfer = stub_xfer,
  114. };
  115. static struct i2c_adapter stub_adapter = {
  116. .owner = THIS_MODULE,
  117. .class = I2C_CLASS_HWMON,
  118. .algo = &smbus_algorithm,
  119. .name = "SMBus stub driver",
  120. };
  121. static int __init i2c_stub_init(void)
  122. {
  123. int i, ret;
  124. if (!chip_addr[0]) {
  125. printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
  126. return -ENODEV;
  127. }
  128. for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
  129. if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
  130. printk(KERN_ERR "i2c-stub: Invalid chip address "
  131. "0x%02x\n", chip_addr[i]);
  132. return -EINVAL;
  133. }
  134. printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n",
  135. chip_addr[i]);
  136. }
  137. /* Allocate memory for all chips at once */
  138. stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
  139. if (!stub_chips) {
  140. printk(KERN_ERR "i2c-stub: Out of memory\n");
  141. return -ENOMEM;
  142. }
  143. ret = i2c_add_adapter(&stub_adapter);
  144. if (ret)
  145. kfree(stub_chips);
  146. return ret;
  147. }
  148. static void __exit i2c_stub_exit(void)
  149. {
  150. i2c_del_adapter(&stub_adapter);
  151. kfree(stub_chips);
  152. }
  153. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  154. MODULE_DESCRIPTION("I2C stub driver");
  155. MODULE_LICENSE("GPL");
  156. module_init(i2c_stub_init);
  157. module_exit(i2c_stub_exit);