xref: /qemu/include/hw/i2c/smbus_slave.h (revision 30b5707c)
193198b6cSCorey Minyard /*
293198b6cSCorey Minyard  * QEMU SMBus device (slave) API
393198b6cSCorey Minyard  *
493198b6cSCorey Minyard  * Copyright (c) 2007 Arastra, Inc.
593198b6cSCorey Minyard  *
693198b6cSCorey Minyard  * Permission is hereby granted, free of charge, to any person obtaining a copy
793198b6cSCorey Minyard  * of this software and associated documentation files (the "Software"), to deal
893198b6cSCorey Minyard  * in the Software without restriction, including without limitation the rights
993198b6cSCorey Minyard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1093198b6cSCorey Minyard  * copies of the Software, and to permit persons to whom the Software is
1193198b6cSCorey Minyard  * furnished to do so, subject to the following conditions:
1293198b6cSCorey Minyard  *
1393198b6cSCorey Minyard  * The above copyright notice and this permission notice shall be included in
1493198b6cSCorey Minyard  * all copies or substantial portions of the Software.
1593198b6cSCorey Minyard  *
1693198b6cSCorey Minyard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1793198b6cSCorey Minyard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1893198b6cSCorey Minyard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1993198b6cSCorey Minyard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2093198b6cSCorey Minyard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2193198b6cSCorey Minyard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2293198b6cSCorey Minyard  * THE SOFTWARE.
2393198b6cSCorey Minyard  */
2493198b6cSCorey Minyard 
2593198b6cSCorey Minyard #ifndef HW_SMBUS_SLAVE_H
2693198b6cSCorey Minyard #define HW_SMBUS_SLAVE_H
2793198b6cSCorey Minyard 
2893198b6cSCorey Minyard #include "hw/i2c/i2c.h"
29db1015e9SEduardo Habkost #include "qom/object.h"
3093198b6cSCorey Minyard 
3193198b6cSCorey Minyard #define TYPE_SMBUS_DEVICE "smbus-device"
32c821774aSEduardo Habkost OBJECT_DECLARE_TYPE(SMBusDevice, SMBusDeviceClass,
33*30b5707cSEduardo Habkost                     SMBUS_DEVICE)
3493198b6cSCorey Minyard 
3593198b6cSCorey Minyard 
36db1015e9SEduardo Habkost struct SMBusDeviceClass {
3793198b6cSCorey Minyard     I2CSlaveClass parent_class;
38905cec6dSCorey Minyard 
39905cec6dSCorey Minyard     /*
40905cec6dSCorey Minyard      * An operation with no data, special in SMBus.
41905cec6dSCorey Minyard      * This may be NULL, quick commands are ignore in that case.
42905cec6dSCorey Minyard      */
4393198b6cSCorey Minyard     void (*quick_cmd)(SMBusDevice *dev, uint8_t read);
449cf27d74SCorey Minyard 
459cf27d74SCorey Minyard     /*
469cf27d74SCorey Minyard      * We can't distinguish between a word write and a block write with
479cf27d74SCorey Minyard      * length 1, so pass the whole data block including the length byte
489cf27d74SCorey Minyard      * (if present).  The device is responsible figuring out what type of
499cf27d74SCorey Minyard      * command this is.
509cf27d74SCorey Minyard      * This may be NULL if no data is written to the device.  Writes
519cf27d74SCorey Minyard      * will be ignore in that case.
529cf27d74SCorey Minyard      */
539cf27d74SCorey Minyard     int (*write_data)(SMBusDevice *dev, uint8_t *buf, uint8_t len);
549cf27d74SCorey Minyard 
55031ac498SCorey Minyard     /*
56031ac498SCorey Minyard      * Likewise we can't distinguish between different reads, or even know
57031ac498SCorey Minyard      * the length of the read until the read is complete, so read data a
58031ac498SCorey Minyard      * byte at a time.  The device is responsible for adding the length
59031ac498SCorey Minyard      * byte on block reads.  This call cannot fail, it should return
60031ac498SCorey Minyard      * something, preferably 0xff if nothing is available.
61031ac498SCorey Minyard      * This may be NULL if no data is read from the device.  Reads will
62031ac498SCorey Minyard      * return 0xff in that case.
63031ac498SCorey Minyard      */
64031ac498SCorey Minyard     uint8_t (*receive_byte)(SMBusDevice *dev);
65db1015e9SEduardo Habkost };
6693198b6cSCorey Minyard 
67547db24aSCorey Minyard #define SMBUS_DATA_MAX_LEN 34  /* command + len + 32 bytes of data.  */
68547db24aSCorey Minyard 
6993198b6cSCorey Minyard struct SMBusDevice {
7093198b6cSCorey Minyard     /* The SMBus protocol is implemented on top of I2C.  */
7193198b6cSCorey Minyard     I2CSlave i2c;
7293198b6cSCorey Minyard 
7393198b6cSCorey Minyard     /* Remaining fields for internal use only.  */
74547db24aSCorey Minyard     int32_t mode;
75547db24aSCorey Minyard     int32_t data_len;
76547db24aSCorey Minyard     uint8_t data_buf[SMBUS_DATA_MAX_LEN];
7793198b6cSCorey Minyard };
7893198b6cSCorey Minyard 
79547db24aSCorey Minyard extern const VMStateDescription vmstate_smbus_device;
80547db24aSCorey Minyard 
81547db24aSCorey Minyard #define VMSTATE_SMBUS_DEVICE(_field, _state) {                       \
82547db24aSCorey Minyard     .name       = (stringify(_field)),                               \
83547db24aSCorey Minyard     .size       = sizeof(SMBusDevice),                               \
84547db24aSCorey Minyard     .vmsd       = &vmstate_smbus_device,                             \
85547db24aSCorey Minyard     .flags      = VMS_STRUCT,                                        \
86547db24aSCorey Minyard     .offset     = vmstate_offset_value(_state, _field, SMBusDevice), \
87547db24aSCorey Minyard }
88547db24aSCorey Minyard 
89547db24aSCorey Minyard /*
90547db24aSCorey Minyard  * Users should call this in their .needed functions to know if the
91547db24aSCorey Minyard  * SMBus slave data needs to be transferred.
92547db24aSCorey Minyard  */
93547db24aSCorey Minyard bool smbus_vmstate_needed(SMBusDevice *dev);
94547db24aSCorey Minyard 
9593198b6cSCorey Minyard #endif
96