smbus_slave.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * QEMU SMBus device (slave) API
  3. *
  4. * Copyright (c) 2007 Arastra, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #ifndef HW_SMBUS_SLAVE_H
  25. #define HW_SMBUS_SLAVE_H
  26. #include "hw/i2c/i2c.h"
  27. #include "qom/object.h"
  28. #define TYPE_SMBUS_DEVICE "smbus-device"
  29. OBJECT_DECLARE_TYPE(SMBusDevice, SMBusDeviceClass,
  30. SMBUS_DEVICE)
  31. struct SMBusDeviceClass {
  32. I2CSlaveClass parent_class;
  33. /*
  34. * An operation with no data, special in SMBus.
  35. * This may be NULL, quick commands are ignore in that case.
  36. */
  37. void (*quick_cmd)(SMBusDevice *dev, uint8_t read);
  38. /*
  39. * We can't distinguish between a word write and a block write with
  40. * length 1, so pass the whole data block including the length byte
  41. * (if present). The device is responsible figuring out what type of
  42. * command this is.
  43. * This may be NULL if no data is written to the device. Writes
  44. * will be ignore in that case.
  45. */
  46. int (*write_data)(SMBusDevice *dev, uint8_t *buf, uint8_t len);
  47. /*
  48. * Likewise we can't distinguish between different reads, or even know
  49. * the length of the read until the read is complete, so read data a
  50. * byte at a time. The device is responsible for adding the length
  51. * byte on block reads. This call cannot fail, it should return
  52. * something, preferably 0xff if nothing is available.
  53. * This may be NULL if no data is read from the device. Reads will
  54. * return 0xff in that case.
  55. */
  56. uint8_t (*receive_byte)(SMBusDevice *dev);
  57. };
  58. #define SMBUS_DATA_MAX_LEN 34 /* command + len + 32 bytes of data. */
  59. struct SMBusDevice {
  60. /* The SMBus protocol is implemented on top of I2C. */
  61. I2CSlave i2c;
  62. /* Remaining fields for internal use only. */
  63. int32_t mode;
  64. int32_t data_len;
  65. uint8_t data_buf[SMBUS_DATA_MAX_LEN];
  66. };
  67. extern const VMStateDescription vmstate_smbus_device;
  68. #define VMSTATE_SMBUS_DEVICE(_field, _state) { \
  69. .name = (stringify(_field)), \
  70. .size = sizeof(SMBusDevice), \
  71. .vmsd = &vmstate_smbus_device, \
  72. .flags = VMS_STRUCT, \
  73. .offset = vmstate_offset_value(_state, _field, SMBusDevice), \
  74. }
  75. /*
  76. * Users should call this in their .needed functions to know if the
  77. * SMBus slave data needs to be transferred.
  78. */
  79. bool smbus_vmstate_needed(SMBusDevice *dev);
  80. #endif