hw/i2c: move search to i2c_scan_bus method

Moves the search for matching devices on an i2c bus into a separate
method.  This allows for an object that owns an I2CBus can avoid
duplicating this method.

Tested: A BMC firmware was booted to userspace and i2c devices were
detected.

Signed-off-by: Patrick Venture <venture@google.com>
Reviewed-by: Hao Wu <wuhaotsh@google.com>
Message-Id: <20210412194522.664594-4-venture@google.com>
Signed-off-by: Corey Minyard <cminyard@mvista.com>
This commit is contained in:
Patrick Venture 2021-04-12 12:45:21 -07:00 committed by Corey Minyard
parent 513ca82d89
commit 3f9b32595e
2 changed files with 28 additions and 12 deletions

View File

@ -77,6 +77,30 @@ int i2c_bus_busy(I2CBus *bus)
return !QLIST_EMPTY(&bus->current_devs); return !QLIST_EMPTY(&bus->current_devs);
} }
bool i2c_scan_bus(I2CBus *bus, uint8_t address, bool broadcast,
I2CNodeList *current_devs)
{
BusChild *kid;
QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
DeviceState *qdev = kid->child;
I2CSlave *candidate = I2C_SLAVE(qdev);
I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(candidate);
if (sc->match_and_add(candidate, address, broadcast, current_devs)) {
if (!broadcast) {
return true;
}
}
}
/*
* If broadcast was true, and the list was full or empty, return true. If
* broadcast was false, return false.
*/
return broadcast;
}
/* TODO: Make this handle multiple masters. */ /* TODO: Make this handle multiple masters. */
/* /*
* Start or continue an i2c transaction. When this is called for the * Start or continue an i2c transaction. When this is called for the
@ -93,7 +117,6 @@ int i2c_bus_busy(I2CBus *bus)
*/ */
int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv) int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
{ {
BusChild *kid;
I2CSlaveClass *sc; I2CSlaveClass *sc;
I2CNode *node; I2CNode *node;
bool bus_scanned = false; bool bus_scanned = false;
@ -115,17 +138,8 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
* terminating the previous transaction. * terminating the previous transaction.
*/ */
if (QLIST_EMPTY(&bus->current_devs)) { if (QLIST_EMPTY(&bus->current_devs)) {
QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { /* Disregard whether devices were found. */
DeviceState *qdev = kid->child; (void)i2c_scan_bus(bus, address, bus->broadcast, &bus->current_devs);
I2CSlave *candidate = I2C_SLAVE(qdev);
sc = I2C_SLAVE_GET_CLASS(candidate);
if (sc->match_and_add(candidate, address, bus->broadcast,
&bus->current_devs)) {
if (!bus->broadcast) {
break;
}
}
}
bus_scanned = true; bus_scanned = true;
} }

View File

@ -87,6 +87,8 @@ void i2c_nack(I2CBus *bus);
int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send); int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send);
int i2c_send(I2CBus *bus, uint8_t data); int i2c_send(I2CBus *bus, uint8_t data);
uint8_t i2c_recv(I2CBus *bus); uint8_t i2c_recv(I2CBus *bus);
bool i2c_scan_bus(I2CBus *bus, uint8_t address, bool broadcast,
I2CNodeList *current_devs);
/** /**
* Create an I2C slave device on the heap. * Create an I2C slave device on the heap.