13fffc223Sths /* 293198b6cSCorey Minyard * QEMU SMBus device (slave) API 33fffc223Sths * 43fffc223Sths * Copyright (c) 2007 Arastra, Inc. 53fffc223Sths * 63fffc223Sths * Permission is hereby granted, free of charge, to any person obtaining a copy 73fffc223Sths * of this software and associated documentation files (the "Software"), to deal 83fffc223Sths * in the Software without restriction, including without limitation the rights 93fffc223Sths * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 103fffc223Sths * copies of the Software, and to permit persons to whom the Software is 113fffc223Sths * furnished to do so, subject to the following conditions: 123fffc223Sths * 133fffc223Sths * The above copyright notice and this permission notice shall be included in 143fffc223Sths * all copies or substantial portions of the Software. 153fffc223Sths * 163fffc223Sths * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 173fffc223Sths * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 183fffc223Sths * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 193fffc223Sths * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 203fffc223Sths * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 213fffc223Sths * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 223fffc223Sths * THE SOFTWARE. 233fffc223Sths */ 243fffc223Sths 2593198b6cSCorey Minyard #ifndef HW_SMBUS_SLAVE_H 2693198b6cSCorey Minyard #define HW_SMBUS_SLAVE_H 2793198b6cSCorey Minyard 280d09e41aSPaolo Bonzini #include "hw/i2c/i2c.h" 29db1015e9SEduardo Habkost #include "qom/object.h" 303fffc223Sths 31b5ea9327SAnthony Liguori #define TYPE_SMBUS_DEVICE "smbus-device" 32c821774aSEduardo Habkost OBJECT_DECLARE_TYPE(SMBusDevice, SMBusDeviceClass, 33*30b5707cSEduardo Habkost SMBUS_DEVICE) 340ff596d0Spbrook 35d183b00eSPhilippe Mathieu-Daudé 36db1015e9SEduardo Habkost struct SMBusDeviceClass { 37b5ea9327SAnthony Liguori 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 */ 433fffc223Sths 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 }; 660ff596d0Spbrook 67547db24aSCorey Minyard #define SMBUS_DATA_MAX_LEN 34 /* command + len + 32 bytes of data. */ 68547db24aSCorey Minyard 69b5ea9327SAnthony Liguori struct SMBusDevice { 70b5ea9327SAnthony Liguori /* The SMBus protocol is implemented on top of I2C. */ 71b5ea9327SAnthony Liguori I2CSlave i2c; 72b5ea9327SAnthony Liguori 73b5ea9327SAnthony Liguori /* 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]; 77b5ea9327SAnthony Liguori }; 78b5ea9327SAnthony Liguori 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 95b5ea9327SAnthony Liguori #endif 96