16
16
//#define DEBUG_SMBUS 1
19
#define DPRINTF(fmt, args...) \
20
do { printf("smbus(%02x): " fmt , dev->i2c.address, ##args); } while (0)
21
#define BADF(fmt, args...) \
22
do { fprintf(stderr, "smbus: error: " fmt , ##args); exit(1);} while (0)
19
#define DPRINTF(fmt, ...) \
20
do { printf("smbus(%02x): " fmt , dev->i2c.address, ## __VA_ARGS__); } while (0)
21
#define BADF(fmt, ...) \
22
do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
24
#define DPRINTF(fmt, args...) do {} while(0)
25
#define BADF(fmt, args...) \
26
do { fprintf(stderr, "smbus: error: " fmt , ##args);} while (0)
24
#define DPRINTF(fmt, ...) do {} while(0)
25
#define BADF(fmt, ...) \
26
do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__);} while (0)
38
38
static void smbus_do_quick_cmd(SMBusDevice *dev, int recv)
40
SMBusDeviceInfo *t = container_of(dev->i2c.info, SMBusDeviceInfo, i2c);
40
42
DPRINTF("Quick Command %d\n", recv);
42
dev->quick_cmd(dev, recv);
44
t->quick_cmd(dev, recv);
45
47
static void smbus_do_write(SMBusDevice *dev)
49
SMBusDeviceInfo *t = container_of(dev->i2c.info, SMBusDeviceInfo, i2c);
47
51
if (dev->data_len == 0) {
48
52
smbus_do_quick_cmd(dev, 0);
49
53
} else if (dev->data_len == 1) {
50
54
DPRINTF("Send Byte\n");
52
dev->send_byte(dev, dev->data_buf[0]);
56
t->send_byte(dev, dev->data_buf[0]);
55
59
dev->command = dev->data_buf[0];
56
60
DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1);
57
if (dev->write_data) {
58
dev->write_data(dev, dev->command, dev->data_buf + 1,
62
t->write_data(dev, dev->command, dev->data_buf + 1,
64
68
static void smbus_i2c_event(i2c_slave *s, enum i2c_event event)
66
SMBusDevice *dev = (SMBusDevice *)s;
70
SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
68
73
case I2C_START_SEND:
69
74
switch (dev->mode) {
146
151
static int smbus_i2c_recv(i2c_slave *s)
148
SMBusDevice *dev = (SMBusDevice *)s;
153
SMBusDeviceInfo *t = container_of(s->info, SMBusDeviceInfo, i2c);
154
SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
151
157
switch (dev->mode) {
152
158
case SMBUS_RECV_BYTE:
153
if (dev->receive_byte) {
154
ret = dev->receive_byte(dev);
159
if (t->receive_byte) {
160
ret = t->receive_byte(dev);
179
185
static int smbus_i2c_send(i2c_slave *s, uint8_t data)
181
SMBusDevice *dev = (SMBusDevice *)s;
187
SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
182
189
switch (dev->mode) {
183
190
case SMBUS_WRITE_DATA:
184
191
DPRINTF("Write data %02x\n", data);
194
SMBusDevice *smbus_device_init(i2c_bus *bus, int address, int size)
198
if (size < sizeof(SMBusDevice))
199
hw_error("SMBus struct too small");
201
dev = (SMBusDevice *)i2c_slave_init(bus, address, size);
202
dev->i2c.event = smbus_i2c_event;
203
dev->i2c.recv = smbus_i2c_recv;
204
dev->i2c.send = smbus_i2c_send;
201
static void smbus_device_init(i2c_slave *i2c)
203
SMBusDeviceInfo *t = container_of(i2c->info, SMBusDeviceInfo, i2c);
204
SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, i2c);
209
void smbus_register_device(SMBusDeviceInfo *info)
211
assert(info->i2c.qdev.size >= sizeof(SMBusDevice));
212
info->i2c.init = smbus_device_init;
213
info->i2c.event = smbus_i2c_event;
214
info->i2c.recv = smbus_i2c_recv;
215
info->i2c.send = smbus_i2c_send;
216
i2c_register_slave(&info->i2c);
209
219
/* Master device commands. */