diff --git a/src/sensors.c b/src/sensors.c index 750da4d..bbb964f 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -9,6 +10,8 @@ #include #include #include +#include +#include #include #include @@ -1322,6 +1325,33 @@ static int detect_possible_sensors(sensor_ctx_t *ctx, int fd, return false; } +/* A bus the HAL did not nominate is left alone once transfers on it keep + * timing out. A controller with nothing on its pads can wait out its whole + * timeout on every transfer where a working bus would say NACK at once: on an + * NT98566 two such buses, one second a transfer, held `ipcinfo` for 27 s at + * boot (OpenIPC/firmware#1889). Only a timeout counts, and only several in a + * row, so an empty bus is still probed in full and one hiccup costs nothing. + * After that every read fails without touching the bus; the write that puts a + * register page back does not come through here and still goes out. */ +#define SWEEP_BUS_TIMEOUTS 2 +static read_register_t sweep_bus_read; +static int sweep_timeouts; + +static int sweep_read_register(int fd, unsigned char i2c_addr, + unsigned int reg_addr, unsigned int reg_width, + unsigned int data_width) { + if (sweep_timeouts >= SWEEP_BUS_TIMEOUTS) + return -1; + + errno = 0; + int val = sweep_bus_read(fd, i2c_addr, reg_addr, reg_width, data_width); + if (val == -1 && errno == ETIMEDOUT) + sweep_timeouts++; + else + sweep_timeouts = 0; + return val; +} + static bool get_sensor_id_i2c(sensor_ctx_t *ctx) { bool detected = false; int fd = open_i2c_sensor_fd(i2c_adapter_nr); @@ -1455,6 +1485,42 @@ static bool get_sensor_id_spi(sensor_ctx_t *ctx) { return res; } +/* get_sensor_id_i2c() with the reader above in place. The probes call + * i2c_read_register by that name, so it is the global that is borrowed, and + * given back as the HAL left it. */ +static bool get_sensor_id_on_swept_bus(sensor_ctx_t *ctx) { + sweep_timeouts = 0; + /* getsensorid() takes no lock. A second thread that finds the reader already + * borrowed must not save it as the HAL's: it would then call itself. */ + const read_register_t reader = i2c_read_register; + if (reader != sweep_read_register) + sweep_bus_read = reader; + i2c_read_register = sweep_read_register; + + const bool found = get_sensor_id_i2c(ctx); + + i2c_read_register = sweep_bus_read; + sensor_read_register = i2c_read_register; + return found; +} + +/* Which i2c adapter a descriptor is on, 0 if that is not known. Some HAL + * openers answer several numbers with one bus -- Novatek's gives /dev/i2c-0 for + * 5, and /dev/i2c-1 for every number on some dies -- and the sweep probed it + * again each time. Only an i2c-dev node (major 89) is proof of the adapter: a + * vendor's own sensor-bus device takes the number some other way. */ +#define I2C_DEV_MAJOR 89 +static dev_t i2c_adapter_of_fd(int fd) { + struct stat st; + + if (fstat(fd, &st) || !S_ISCHR(st.st_mode) || + major(st.st_rdev) != I2C_DEV_MAJOR) + return 0; + return st.st_rdev; +} +/* A seam: the test has no i2c adapters. */ +static dev_t (*i2c_fd_adapter)(int fd) = i2c_adapter_of_fd; + /* Is the bus there at all? A liveness check and nothing more: the probes below * (get_sensor_id_i2c) open the adapter again and close what they opened, so the * descriptor taken here has no further use. @@ -1470,11 +1536,12 @@ static bool get_sensor_id_spi(sensor_ctx_t *ctx) { * The test is `fd < 0`, not the `!fd` it replaces: open() reports failure as * -1, so the old form read a failed open as success and only the impossible * descriptor 0 as failure. */ -static bool i2c_bus_openable(int adapter_nr) { +static bool i2c_bus_openable(int adapter_nr, dev_t *dev) { int fd = open_i2c_sensor_fd(adapter_nr); if (fd < 0) return false; + *dev = i2c_fd_adapter(fd); close_sensor_fd(fd); return true; } @@ -1513,6 +1580,9 @@ static bool probe_sensor(sensor_ctx_t *ctx) { return false; const int preferred_adapter = i2c_adapter_nr; + dev_t probed[7] = {0}; /* the preferred bus and the sweep's six */ + int nprobed = 0; + dev_t dev = 0; probed_preferred_adapter = preferred_adapter; found_by_sweep = false; @@ -1523,9 +1593,12 @@ static bool probe_sensor(sensor_ctx_t *ctx) { /* The bus the HAL nominated, first. A missing adapter is no longer the end * of the probe: the sweep below can still find the sensor elsewhere. */ arm_sensor_clock(); - if (i2c_bus_openable(i2c_adapter_nr) && get_sensor_id_i2c(ctx)) { - strcpy(ctx->control, "i2c"); - return true; + if (i2c_bus_openable(i2c_adapter_nr, &dev)) { + probed[nprobed++] = dev; + if (get_sensor_id_i2c(ctx)) { + strcpy(ctx->control, "i2c"); + return true; + } } /* SPI once, not once per i2c bus: open_spi_sensor_fd() takes no adapter @@ -1546,15 +1619,23 @@ static bool probe_sensor(sensor_ctx_t *ctx) { continue; i2c_adapter_nr = i; - if (!i2c_bus_openable(i2c_adapter_nr)) + if (!i2c_bus_openable(i2c_adapter_nr, &dev)) + continue; + + bool again = false; + for (int j = 0; dev && j < nprobed; j++) + again |= probed[j] == dev; + if (again) continue; + probed[nprobed++] = dev; // Use common settings as default ctx->data_width = 1; ctx->reg_width = 2; arm_sensor_clock(); - if (get_sensor_id_i2c(ctx)) { + const bool found = get_sensor_id_on_swept_bus(ctx); + if (found) { strcpy(ctx->control, "i2c"); found_by_sweep = true; return true; @@ -1665,7 +1746,8 @@ cJSON *detect_sensors() { /* Before the open: the HAL openers take no argument and open the * adapter this global names. */ i2c_adapter_nr = bus; - if (!i2c_bus_openable(bus)) + dev_t dev; + if (!i2c_bus_openable(bus, &dev)) continue; sensor_ctx_t more; diff --git a/src/sensors_test.c b/src/sensors_test.c index ec40914..077dc40 100644 --- a/src/sensors_test.c +++ b/src/sensors_test.c @@ -681,6 +681,131 @@ static void check_superpix_spared_by_galaxycore(void) { i2c_write_register = mock_write; } +/* ---- the bus sweep ----------------------------------------------------- */ + +/* Buses 0..4 exist and 5 opens bus 0 again, as Novatek's opener has it. A + * descriptor says which bus it is on, through the same seam the probe uses. */ +static int sweep_fd_bus[1024]; +static int sweep_reads[5]; +static int sweep_err[5]; /* errno of every read there, 0: an IMX415 answers */ +static int sweep_flaky; /* bus 2: a NACK between two timeouts, repeating */ +static int sweep_anonymous; + +static int sweep_open(int nr) { + int fd = universal_open_sensor_fd("/dev/null"); + sweep_fd_bus[fd] = nr > 4 ? 0 : nr; + return fd; +} + +static dev_t sweep_adapter(int fd) { + return sweep_anonymous ? 0 : makedev(I2C_DEV_MAJOR, sweep_fd_bus[fd]); +} + +static int sweep_bus_read_mock(int fd, unsigned char addr, uint32_t reg, + unsigned int reg_width, + unsigned int data_width) { + (void)addr; + (void)reg_width; + (void)data_width; + int bus = sweep_fd_bus[fd]; + int n = sweep_reads[bus]++; + + errno = sweep_err[bus]; + if (bus == 2 && sweep_flaky) + errno = n % 2 ? EREMOTEIO : ETIMEDOUT; + if (errno) + return -1; + return reg == 0x3b00 ? 0x28 : reg == 0x300b ? 0xa0 : 0; +} + +static bool sweep_run(sensor_ctx_t *ctx) { + memset(ctx, 0, sizeof(*ctx)); + memset(sweep_reads, 0, sizeof(sweep_reads)); + return getsensorid(ctx); +} + +static void check_bus_sweep(void) { + static unsigned char addrs[] = {0x60, 0}; + /* Several families, so a probe in full is far more than two reads. */ + static sensor_addr_t table[] = { + {SENSOR_SONY, addrs}, {SENSOR_SMARTSENS, addrs}, + {SENSOR_ONSEMI, addrs}, {SENSOR_OMNIVISION, addrs}, + {SENSOR_GALAXYCORE, addrs}, {SENSOR_TECHPOINT, addrs}, + {0, NULL}}; + sensor_ctx_t ctx; + + setup_hal_fallback(); + open_i2c_sensor_fd = sweep_open; + i2c_read_register = sweep_bus_read_mock; + i2c_change_addr = mock_change_addr; + i2c_fd_adapter = sweep_adapter; + possible_i2c_addrs = table; + + /* Nothing anywhere, every bus says so at once: all probed alike, and bus 0 + * once although number 5 opens it too. */ + for (int i = 0; i < 5; i++) + sweep_err[i] = EREMOTEIO; + CHECK(!sweep_run(&ctx)); + const int full = sweep_reads[0]; + CHECK(full > SWEEP_BUS_TIMEOUTS); + for (int i = 1; i < 5; i++) + CHECK(sweep_reads[i] == full); + CHECK(i2c_adapter_nr == 0); + + /* Descriptors whose adapter is not known are never taken for one bus. */ + sweep_anonymous = 1; + CHECK(!sweep_run(&ctx)); + CHECK(sweep_reads[0] == 2 * full); + sweep_anonymous = 0; + + /* The dead buses are left after two timeouts, the others are not. */ + sweep_err[2] = sweep_err[3] = ETIMEDOUT; + CHECK(!sweep_run(&ctx)); + CHECK(sweep_reads[2] == SWEEP_BUS_TIMEOUTS); + CHECK(sweep_reads[3] == SWEEP_BUS_TIMEOUTS); + CHECK(sweep_reads[0] == full && sweep_reads[1] == full); + CHECK(sweep_reads[4] == full); + + /* Timeouts that do not follow one another do not add up. */ + sweep_err[2] = EREMOTEIO; + sweep_flaky = 1; + CHECK(!sweep_run(&ctx)); + CHECK(sweep_reads[2] == full); + sweep_flaky = 0; + + /* The bus the HAL nominated is probed in full whatever it does. */ + sweep_err[0] = ETIMEDOUT; + CHECK(!sweep_run(&ctx)); + CHECK(sweep_reads[0] == full); + + /* The HAL's reader is back in place once the sweep is over. */ + CHECK(i2c_read_register == sweep_bus_read_mock); + CHECK(sensor_read_register == sweep_bus_read_mock); + + /* A sensor behind a dead bus is still found. */ + sweep_err[2] = ETIMEDOUT; + sweep_err[4] = 0; + CHECK(sweep_run(&ctx)); + CHECK(!strcmp(ctx.sensor_id, "IMX415")); + CHECK(sweep_reads[2] == SWEEP_BUS_TIMEOUTS); + CHECK(i2c_adapter_nr == 4); + CHECK(i2c_read_register == sweep_bus_read_mock); + + /* Another thread is mid-sweep, with the reader borrowed: it is not taken + * for the HAL's own, which would recurse until the stack ran out. */ + i2c_read_register = sweep_read_register; + i2c_adapter_nr = 1; + sweep_err[1] = EREMOTEIO; + memset(&ctx, 0, sizeof(ctx)); + CHECK(!get_sensor_id_on_swept_bus(&ctx)); + CHECK(i2c_read_register == sweep_bus_read_mock); + + i2c_adapter_nr = 0; + i2c_fd_adapter = i2c_adapter_of_fd; + possible_i2c_addrs = NULL; + i2c_read_register = mock_read; +} + int main(int argc, char **argv) { i2c_read_register = mock_read; i2c_write_register = mock_write; @@ -710,6 +835,7 @@ int main(int argc, char **argv) { check_galaxycore_reg_width(); check_probe_restores_console(); check_superpix_spared_by_galaxycore(); + check_bus_sweep(); if (failures) fprintf(stderr, "sensors_test: %d failure(s)\n", failures);