xref: /openbmc/qemu/include/hw/i2c/smbus_slave.h (revision 0a553c58)
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 
25 #ifndef HW_SMBUS_SLAVE_H
26 #define HW_SMBUS_SLAVE_H
27 
28 #include "hw/i2c/i2c.h"
29 
30 #define TYPE_SMBUS_DEVICE "smbus-device"
31 #define SMBUS_DEVICE(obj) \
32      OBJECT_CHECK(SMBusDevice, (obj), TYPE_SMBUS_DEVICE)
33 #define SMBUS_DEVICE_CLASS(klass) \
34      OBJECT_CLASS_CHECK(SMBusDeviceClass, (klass), TYPE_SMBUS_DEVICE)
35 #define SMBUS_DEVICE_GET_CLASS(obj) \
36      OBJECT_GET_CLASS(SMBusDeviceClass, (obj), TYPE_SMBUS_DEVICE)
37 
38 typedef struct SMBusDevice SMBusDevice;
39 
40 typedef struct SMBusDeviceClass
41 {
42     I2CSlaveClass parent_class;
43 
44     /*
45      * An operation with no data, special in SMBus.
46      * This may be NULL, quick commands are ignore in that case.
47      */
48     void (*quick_cmd)(SMBusDevice *dev, uint8_t read);
49 
50     /*
51      * We can't distinguish between a word write and a block write with
52      * length 1, so pass the whole data block including the length byte
53      * (if present).  The device is responsible figuring out what type of
54      * command this is.
55      * This may be NULL if no data is written to the device.  Writes
56      * will be ignore in that case.
57      */
58     int (*write_data)(SMBusDevice *dev, uint8_t *buf, uint8_t len);
59 
60     /*
61      * Likewise we can't distinguish between different reads, or even know
62      * the length of the read until the read is complete, so read data a
63      * byte at a time.  The device is responsible for adding the length
64      * byte on block reads.  This call cannot fail, it should return
65      * something, preferably 0xff if nothing is available.
66      * This may be NULL if no data is read from the device.  Reads will
67      * return 0xff in that case.
68      */
69     uint8_t (*receive_byte)(SMBusDevice *dev);
70 } SMBusDeviceClass;
71 
72 #define SMBUS_DATA_MAX_LEN 34  /* command + len + 32 bytes of data.  */
73 
74 struct SMBusDevice {
75     /* The SMBus protocol is implemented on top of I2C.  */
76     I2CSlave i2c;
77 
78     /* Remaining fields for internal use only.  */
79     int32_t mode;
80     int32_t data_len;
81     uint8_t data_buf[SMBUS_DATA_MAX_LEN];
82 };
83 
84 extern const VMStateDescription vmstate_smbus_device;
85 
86 #define VMSTATE_SMBUS_DEVICE(_field, _state) {                       \
87     .name       = (stringify(_field)),                               \
88     .size       = sizeof(SMBusDevice),                               \
89     .vmsd       = &vmstate_smbus_device,                             \
90     .flags      = VMS_STRUCT,                                        \
91     .offset     = vmstate_offset_value(_state, _field, SMBusDevice), \
92 }
93 
94 /*
95  * Users should call this in their .needed functions to know if the
96  * SMBus slave data needs to be transferred.
97  */
98 bool smbus_vmstate_needed(SMBusDevice *dev);
99 
100 #endif
101