diff options
author | Paul Brook | 2009-05-23 01:05:19 +0200 |
---|---|---|
committer | Paul Brook | 2009-05-23 01:13:41 +0200 |
commit | 02e2da45c4601909d5105838323d5c529ec7010b (patch) | |
tree | e9b1099baa55c3f8084110feb28f57ede8d2de3a /hw/i2c.c | |
parent | Fix lance segfaults (diff) | |
download | qemu-02e2da45c4601909d5105838323d5c529ec7010b.tar.gz qemu-02e2da45c4601909d5105838323d5c529ec7010b.tar.xz qemu-02e2da45c4601909d5105838323d5c529ec7010b.zip |
Add common BusState
Implement and use a common device bus state. The main side-effect is
that creating a bus and attaching it to a parent device are no longer
separate operations. For legacy code we allow a NULL parent, but that
should go away eventually.
Also tweak creation code to veriry theat a device in on the right bus.
Signed-off-by: Paul Brook <paul@codesourcery.com>
Diffstat (limited to 'hw/i2c.c')
-rw-r--r-- | hw/i2c.c | 35 |
1 files changed, 19 insertions, 16 deletions
@@ -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; |