From abeefb85f28e6d8613cc5604540226c24c95e705 Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:48:51 +0300 Subject: [PATCH 1/2] libipchw: ask one sensor connector at a time (ipchw_sensor_ports, ipchw_sensor_probe) A Hi3519DV500 board has four sensor connectors, and the sensors on them come and go with their ribbon cables. detect_sensors() lists them all for the report, but a library consumer had only getsensorshort(): the first sensor anywhere, found by a sweep that names the sensor on SENSOR1 as SENSOR0's when SENSOR0 is empty. majestic needs to know which connector each sensor is on to pick its MIPI receiver, VI device and clock. - ipchw_sensor_ports(): how many connectors the HAL knows the wiring of (hal_sensor_buses; 4 on DV500, 0 everywhere else). - ipchw_sensor_probe(port, &out): that connector's bus only -- no SPI, no fallback sweep -- under the identity lock, with the adapter number put back. It fills the bus, the model as getsensorshort() spells it, and the part that shares its ID registers (os08a10 / os08a20), so a consumer can look for a profile under either name. sensor_id_shared_with() moves out of the report-only block so the library can use it. sensors_test drives the probe on a mock two-connector board with the sensor on the second one: connector 0 answers nothing and opens only bus 4, connector 1 answers os08a10 / os08a20, and out-of-range ports are refused. Making the probe use the sweep fails it. --- include/ipchw.h | 37 +++++++++++++++++++++++ src/sensors.c | 59 ++++++++++++++++++++++++++++++++++-- src/sensors_test.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 2 deletions(-) diff --git a/include/ipchw.h b/include/ipchw.h index c6e8406..c9afa78 100644 --- a/include/ipchw.h +++ b/include/ipchw.h @@ -10,6 +10,43 @@ const char *getsensoridentity(); const char *getsensorshort(); float gethwtemp(); +/* ------------------------------------------------------------------------ + * Sensor connectors + * + * A board family with more than one sensor connector, and a HAL that knows + * which i2c bus each one is wired to, can be asked one connector at a time. + * Today that is the Hi3519DV500 reference design: SENSOR0..3 on I2C4, I2C5, + * I2C3 and I2C6. Everywhere else ipchw_sensor_ports() is 0 and + * getsensorshort() remains the question to ask. + * + * Sensors on these boards come and go with their ribbon cables, so a consumer + * that wants to know what is fitted asks at start-up rather than trusting a + * name saved on an earlier boot. + * + * The probe ungates the connector's sensor clock where it has to, and on + * DV500 switches SENSOR1's I2C5 pads from GPIO for the duration; both go back + * as they were found. A consumer that goes on to run that sensor muxes the + * pads itself (ipchw_padmux_set()). + * + * Threading: serialised with getsensoridentity() and getsensorshort(). It + * reaches registers through the same unlocked window as ipchw_padmux_get(). + * ---------------------------------------------------------------------- */ +typedef struct { + int bus; /* the connector's i2c adapter */ + char model[16]; /* as getsensorshort() spells it: "os08a10" */ + char twin[16]; /* another part with the same ID registers, spelled the + * same way ("os08a20"), or "" */ +} ipchw_sensor_t; + +/* The number of connectors, 0 where the board family has one or the HAL does + * not know the wiring. */ +int ipchw_sensor_ports(void); + +/* What is on connector `port` (0-based): 1 and *out filled, 0 when nothing + * answers on its bus (out->bus still says which bus was asked), -1 for a port + * this board family does not have. */ +int ipchw_sensor_probe(int port, ipchw_sensor_t *out); + /* ------------------------------------------------------------------------ * Pad multiplexing * diff --git a/src/sensors.c b/src/sensors.c index bbb964f..e9c2a99 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -19,6 +19,7 @@ #include "cjson/cJSON.h" #include "hal/common.h" #include "hal/xm.h" +#include "ipchw.h" #include "sensors.h" #include "tools.h" @@ -1663,8 +1664,6 @@ bool getsensorid(sensor_ctx_t *ctx) { return found; } -#ifndef STANDALONE_LIBRARY - /* Parts that answer with the same ID registers, so a probe cannot tell them * apart. sensor_id keeps the name it always had -- libipchw consumers pick a * driver by it -- and the report says what else it could be. */ @@ -1677,6 +1676,8 @@ static const char *sensor_id_shared_with(const char *sensor_id) { return NULL; } +#ifndef STANDALONE_LIBRARY + static void add_sensor_entry(cJSON *j_sensors, sensor_ctx_t *ctx, int bus, bool vi_info) { cJSON *j_inner = ctx->j_sensor; @@ -1814,3 +1815,57 @@ const char *getsensorshort() { pthread_mutex_unlock(&sensor_indentity_mtx); return ret; } + +/* How many sensor connectors this board family has, where it has more than + * one and the HAL knows which bus each is on (hal_sensor_buses). */ +int ipchw_sensor_ports(void) { + if (!getchipname() || !hal_sensor_buses) + return 0; + int n = 0; + while (hal_sensor_buses[n] >= 0) + n++; + return n; +} + +/* One connector's bus and nothing else: no fallback to SPI or to the other + * buses, which getsensorid() does and which would name the sensor on the + * next connector as this one's. Under the identity lock, because the probe + * steers the process-global adapter number. */ +int ipchw_sensor_probe(int port, ipchw_sensor_t *out) { + if (!out || port < 0 || port >= ipchw_sensor_ports()) + return -1; + memset(out, 0, sizeof(*out)); + const int bus = hal_sensor_buses[port]; + out->bus = bus; + + pthread_mutex_lock(&sensor_indentity_mtx); + const int saved = i2c_adapter_nr; + /* Before the open: the HAL openers take no argument and open the adapter + * this global names. */ + i2c_adapter_nr = bus; + int found = 0; + dev_t dev; + if (i2c_bus_openable(bus, &dev)) { + sensor_ctx_t ctx; + memset(&ctx, 0, sizeof(ctx)); + ctx.data_width = 1; + ctx.reg_width = 2; + arm_sensor_clock(); + if (get_sensor_id_i2c(&ctx)) { + found = 1; + lsnprintf(out->model, sizeof(out->model), "%s", ctx.sensor_id); + const char *twin = sensor_id_shared_with(ctx.sensor_id); + if (twin) + lsnprintf(out->twin, sizeof(out->twin), "%s", twin); + } + } + /* A probe whose own open fails returns without a cleanup, and what the + * arm switched must go back; after one that ran this is a no-op, each + * undo being spent. */ + if (hal_cleanup) + hal_cleanup(); + i2c_adapter_nr = saved; + restore_printk(); + pthread_mutex_unlock(&sensor_indentity_mtx); + return found; +} diff --git a/src/sensors_test.c b/src/sensors_test.c index 077dc40..f0414ee 100644 --- a/src/sensors_test.c +++ b/src/sensors_test.c @@ -806,6 +806,80 @@ static void check_bus_sweep(void) { i2c_read_register = mock_read; } +/* ---- one connector at a time ------------------------------------------- */ + +/* A board with two connectors and the sensor fitted to the second one only: + * asking the first must not answer with the second's sensor, as getsensorid() + * would after its sweep, and a port the board does not have is refused. The + * bus each probe opened is recorded, because the openers take no argument and + * read the process-global adapter number. */ +static int ports_sensor_bus; +static int ports_opened[8]; +static int ports_nopened; + +static int ports_open(int adapter_nr) { + if (ports_nopened < (int)ARRCNT(ports_opened)) + ports_opened[ports_nopened++] = adapter_nr; + if (adapter_nr != 4 && adapter_nr != 5) + return -1; + return universal_open_sensor_fd("/dev/null"); +} + +static int ports_read(int fd, unsigned char addr, uint32_t reg, + unsigned int reg_width, unsigned int data_width) { + if (i2c_adapter_nr != ports_sensor_bus) + return -1; + return mock_read(fd, addr, reg, reg_width, data_width); +} + +static void check_sensor_ports(void) { + static unsigned char addrs[] = {0x60, 0}; + static sensor_addr_t table[] = {{SENSOR_OMNIVISION, addrs}, {0, NULL}}; + static const int buses[] = {4, 5, -1}; + ipchw_sensor_t s; + + setup_hal_fallback(); + CHECK(ipchw_sensor_ports() == 0); + CHECK(ipchw_sensor_probe(0, &s) == -1); + + hal_sensor_buses = buses; + open_i2c_sensor_fd = ports_open; + i2c_read_register = ports_read; + i2c_change_addr = mock_change_addr; + possible_i2c_addrs = table; + mock_clear(); + mock_set(0x300A, 2, 1, 0x53); + mock_set(0x300B, 2, 1, 0x08); + i2c_adapter_nr = 0; + + CHECK(ipchw_sensor_ports() == 2); + + ports_sensor_bus = 5; + ports_nopened = 0; + CHECK(ipchw_sensor_probe(0, &s) == 0); + CHECK(s.bus == 4 && !s.model[0] && !s.twin[0]); + for (int i = 0; i < ports_nopened; i++) + CHECK(ports_opened[i] == 4); + + CHECK(ipchw_sensor_probe(1, &s) == 1); + CHECK(s.bus == 5); + CHECK(!strcmp(s.model, "os08a10")); + CHECK(!strcmp(s.twin, "os08a20")); + if (strcmp(s.model, "os08a10") || strcmp(s.twin, "os08a20")) + fprintf(stderr, " (port 1: got \"%s\" / \"%s\")\n", s.model, s.twin); + + CHECK(ipchw_sensor_probe(2, &s) == -1); + CHECK(ipchw_sensor_probe(-1, &s) == -1); + CHECK(ipchw_sensor_probe(0, NULL) == -1); + /* The global is put back for `ipctool i2cget` and friends. */ + CHECK(i2c_adapter_nr == 0); + + hal_sensor_buses = NULL; + possible_i2c_addrs = NULL; + i2c_read_register = mock_read; + mock_clear(); +} + int main(int argc, char **argv) { i2c_read_register = mock_read; i2c_write_register = mock_write; @@ -835,6 +909,7 @@ int main(int argc, char **argv) { check_galaxycore_reg_width(); check_probe_restores_console(); check_superpix_spared_by_galaxycore(); + check_sensor_ports(); check_bus_sweep(); if (failures) From e59d4584541ec4e0f0f7e089b4698366a7485c10 Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin <6576495+widgetii@users.noreply.github.com> Date: Fri, 27 Nov 2026 21:12:31 +0300 Subject: [PATCH 2/2] libipchw: sensor probe undoes only its own arm, and sets the HAL up under the lock Review fixes (Qodo on #297): - A connector whose bus does not open reached hal_cleanup() without an arm before it. That spends a restore somebody else is owed: the HiSilicon HAL's setup ungates the sensor clock too, so a probe of a missing adapter could gate a clock it never touched. The cleanup now runs only after the probe's own arm_sensor_clock(). The adapter number and the console are still put back on every path. - ipchw_sensor_probe() counted the connectors -- which runs getchipname(), the HAL's unlocked first-use setup -- and read the bus table before taking the identity lock. Two first calls could race through that setup. The count, the bus choice and the probe now all run under the lock, through sensor_ports_locked(). ipchw_sensor_ports() takes the lock as well. sensors_test gains a third connector whose adapter does not open. Probing it answers 0 with the bus filled in and no cleanup. A probe that armed is undone. Putting the unconditional cleanup back fails the test. --- src/sensors.c | 39 ++++++++++++++++++++++++++++----------- src/sensors_test.c | 23 ++++++++++++++++++++--- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/sensors.c b/src/sensors.c index e9c2a99..30e765d 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -1817,8 +1817,10 @@ const char *getsensorshort() { } /* How many sensor connectors this board family has, where it has more than - * one and the HAL knows which bus each is on (hal_sensor_buses). */ -int ipchw_sensor_ports(void) { + * one and the HAL knows which bus each is on (hal_sensor_buses). Called with + * the identity lock held: getchipname() sets the HAL up on first use and is + * not safe to race. */ +static int sensor_ports_locked(void) { if (!getchipname() || !hal_sensor_buses) return 0; int n = 0; @@ -1827,18 +1829,31 @@ int ipchw_sensor_ports(void) { return n; } +int ipchw_sensor_ports(void) { + pthread_mutex_lock(&sensor_indentity_mtx); + const int n = sensor_ports_locked(); + pthread_mutex_unlock(&sensor_indentity_mtx); + return n; +} + /* One connector's bus and nothing else: no fallback to SPI or to the other * buses, which getsensorid() does and which would name the sensor on the - * next connector as this one's. Under the identity lock, because the probe - * steers the process-global adapter number. */ + * next connector as this one's. Everything from the HAL's first-use setup to + * the restore of the process-global adapter number runs under the identity + * lock, as getsensoridentity() does. */ int ipchw_sensor_probe(int port, ipchw_sensor_t *out) { - if (!out || port < 0 || port >= ipchw_sensor_ports()) + if (!out || port < 0) return -1; memset(out, 0, sizeof(*out)); + + pthread_mutex_lock(&sensor_indentity_mtx); + if (port >= sensor_ports_locked()) { + pthread_mutex_unlock(&sensor_indentity_mtx); + return -1; + } const int bus = hal_sensor_buses[port]; out->bus = bus; - pthread_mutex_lock(&sensor_indentity_mtx); const int saved = i2c_adapter_nr; /* Before the open: the HAL openers take no argument and open the adapter * this global names. */ @@ -1858,12 +1873,14 @@ int ipchw_sensor_probe(int port, ipchw_sensor_t *out) { if (twin) lsnprintf(out->twin, sizeof(out->twin), "%s", twin); } + /* Only after an arm: the undo it owes is this probe's. A probe whose + * own open fails returns without one, and after one that ran this is + * a no-op, each undo being spent. A bus that would not open was never + * armed, and a cleanup there would spend a restore somebody else is + * owed -- the HAL's setup ungates the sensor clock too. */ + if (hal_cleanup) + hal_cleanup(); } - /* A probe whose own open fails returns without a cleanup, and what the - * arm switched must go back; after one that ran this is a no-op, each - * undo being spent. */ - if (hal_cleanup) - hal_cleanup(); i2c_adapter_nr = saved; restore_printk(); pthread_mutex_unlock(&sensor_indentity_mtx); diff --git a/src/sensors_test.c b/src/sensors_test.c index f0414ee..6a7315f 100644 --- a/src/sensors_test.c +++ b/src/sensors_test.c @@ -825,6 +825,11 @@ static int ports_open(int adapter_nr) { return universal_open_sensor_fd("/dev/null"); } +/* A cleanup with no arm before it spends a restore somebody else is owed -- + * the HAL's own setup ungates the sensor clock -- so it is counted. */ +static int ports_cleanups; +static void ports_cleanup(void) { ports_cleanups++; } + static int ports_read(int fd, unsigned char addr, uint32_t reg, unsigned int reg_width, unsigned int data_width) { if (i2c_adapter_nr != ports_sensor_bus) @@ -835,7 +840,8 @@ static int ports_read(int fd, unsigned char addr, uint32_t reg, static void check_sensor_ports(void) { static unsigned char addrs[] = {0x60, 0}; static sensor_addr_t table[] = {{SENSOR_OMNIVISION, addrs}, {0, NULL}}; - static const int buses[] = {4, 5, -1}; + /* Bus 6 has no adapter: the third connector's open fails. */ + static const int buses[] = {4, 5, 6, -1}; ipchw_sensor_t s; setup_hal_fallback(); @@ -843,6 +849,7 @@ static void check_sensor_ports(void) { CHECK(ipchw_sensor_probe(0, &s) == -1); hal_sensor_buses = buses; + hal_cleanup = ports_cleanup; open_i2c_sensor_fd = ports_open; i2c_read_register = ports_read; i2c_change_addr = mock_change_addr; @@ -852,7 +859,7 @@ static void check_sensor_ports(void) { mock_set(0x300B, 2, 1, 0x08); i2c_adapter_nr = 0; - CHECK(ipchw_sensor_ports() == 2); + CHECK(ipchw_sensor_ports() == 3); ports_sensor_bus = 5; ports_nopened = 0; @@ -868,13 +875,23 @@ static void check_sensor_ports(void) { if (strcmp(s.model, "os08a10") || strcmp(s.twin, "os08a20")) fprintf(stderr, " (port 1: got \"%s\" / \"%s\")\n", s.model, s.twin); - CHECK(ipchw_sensor_probe(2, &s) == -1); + /* Connector 2's bus does not open: nothing armed, so nothing undone. */ + ports_cleanups = 0; + CHECK(ipchw_sensor_probe(2, &s) == 0); + CHECK(s.bus == 6 && !s.model[0]); + CHECK(ports_cleanups == 0); + /* An armed probe is undone, found or not. */ + CHECK(ipchw_sensor_probe(0, &s) == 0); + CHECK(ports_cleanups > 0); + + CHECK(ipchw_sensor_probe(3, &s) == -1); CHECK(ipchw_sensor_probe(-1, &s) == -1); CHECK(ipchw_sensor_probe(0, NULL) == -1); /* The global is put back for `ipctool i2cget` and friends. */ CHECK(i2c_adapter_nr == 0); hal_sensor_buses = NULL; + hal_cleanup = NULL; possible_i2c_addrs = NULL; i2c_read_register = mock_read; mock_clear();