aboutsummaryrefslogtreecommitdiff
path: root/hw/i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw/i2c.c')
-rw-r--r--hw/i2c.c35
1 files changed, 19 insertions, 16 deletions
diff --git a/hw/i2c.c b/hw/i2c.c
index ce9de29b77..8a0c4d7a75 100644
--- a/hw/i2c.c
+++ b/hw/i2c.c
@@ -11,6 +11,7 @@
struct i2c_bus
{
+ BusState qbus;
i2c_slave *current_dev;
i2c_slave *dev;
int saved_address;
@@ -39,11 +40,12 @@ static int i2c_bus_load(QEMUFile *f, void *opaque, int version_id)
}
/* Create a new I2C bus. */
-i2c_bus *i2c_init_bus(void)
+i2c_bus *i2c_init_bus(DeviceState *parent, const char *name)
{
i2c_bus *bus;
- bus = (i2c_bus *)qemu_mallocz(sizeof(i2c_bus));
+ bus = FROM_QBUS(i2c_bus, qbus_create(BUS_TYPE_I2C, sizeof(i2c_bus),
+ parent, name));
register_savevm("i2c_bus", -1, 1, i2c_bus_save, i2c_bus_load, bus);
return bus;
}
@@ -63,20 +65,22 @@ int i2c_bus_busy(i2c_bus *bus)
/* TODO: Make this handle multiple masters. */
int i2c_start_transfer(i2c_bus *bus, int address, int recv)
{
- i2c_slave *dev;
+ DeviceState *qdev;
+ i2c_slave *slave = NULL;
- for (dev = bus->dev; dev; dev = dev->next) {
- if (dev->address == address)
+ LIST_FOREACH(qdev, &bus->qbus.children, sibling) {
+ slave = I2C_SLAVE_FROM_QDEV(qdev);
+ if (slave->address == address)
break;
}
- if (!dev)
+ if (!slave)
return 1;
/* If the bus is already busy, assume this is a repeated
start condition. */
- bus->current_dev = dev;
- dev->info->event(dev, recv ? I2C_START_RECV : I2C_START_SEND);
+ bus->current_dev = slave;
+ slave->info->event(slave, recv ? I2C_START_RECV : I2C_START_SEND);
return 0;
}
@@ -130,23 +134,20 @@ void i2c_slave_save(QEMUFile *f, i2c_slave *dev)
void i2c_slave_load(QEMUFile *f, i2c_slave *dev)
{
i2c_bus *bus;
- bus = qdev_get_bus(&dev->qdev);
+ bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev));
dev->address = qemu_get_byte(f);
if (bus->saved_address == dev->address) {
bus->current_dev = dev;
}
}
-static void i2c_slave_qdev_init(DeviceState *dev, void *opaque)
+static void i2c_slave_qdev_init(DeviceState *dev, DeviceInfo *base)
{
- I2CSlaveInfo *info = opaque;
+ I2CSlaveInfo *info = container_of(base, I2CSlaveInfo, qdev);
i2c_slave *s = I2C_SLAVE_FROM_QDEV(dev);
s->info = info;
- s->bus = qdev_get_bus(dev);
s->address = qdev_get_prop_int(dev, "address", 0);
- s->next = s->bus->dev;
- s->bus->dev = s;
info->init(s);
}
@@ -154,14 +155,16 @@ static void i2c_slave_qdev_init(DeviceState *dev, void *opaque)
void i2c_register_slave(const char *name, int size, I2CSlaveInfo *info)
{
assert(size >= sizeof(i2c_slave));
- qdev_register(name, size, i2c_slave_qdev_init, info);
+ info->qdev.init = i2c_slave_qdev_init;
+ info->qdev.bus_type = BUS_TYPE_I2C;
+ qdev_register(name, size, &info->qdev);
}
DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, int addr)
{
DeviceState *dev;
- dev = qdev_create(bus, name);
+ dev = qdev_create(&bus->qbus, name);
qdev_set_prop_int(dev, "address", addr);
qdev_init(dev);
return dev;