Skip to content

libipchw: ask one sensor connector at a time (ipchw_sensor_ports, ipchw_sensor_probe) - #297

Merged
widgetii merged 2 commits into
masterfrom
sensor-ports
Nov 27, 2026
Merged

widgetii merged 2 commits into
masterfrom
sensor-ports

Conversation

@widgetii

Copy link
Copy Markdown
Member

A Hi3519DV500 board has four sensor connectors, and the sensors on them are detachable. detect_sensors() (#294) lists every sensor for the report. A library consumer, though, has only getsensorshort(): the first sensor anywhere, found by a sweep. That sweep 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 port, VI device and clock.

Changes

include/ipchw.h adds two calls:

  • ipchw_sensor_ports() returns how many connectors the HAL knows the wiring of (hal_sensor_buses). That is 4 on DV500 and 0 everywhere else.
  • ipchw_sensor_probe(port, &out) probes that connector's bus only: no SPI and no fallback sweep.

sensor_id_shared_with() moves out of the report-only block so the library can use it.

Tests

sensors_test gains a case that drives the probe on a mock two-connector board with the sensor on the second connector:

  • connector 0 answers nothing and opens only bus 4;
  • connector 1 answers os08a10 / os08a20;
  • out-of-range ports and a NULL output are refused;
  • the adapter number is restored afterwards.

If the probe is made to use the sweep (probe_sensor()), the case fails with 7 failures. All 17 test binaries from the PR build check pass (Release build).

On hardware

Tested on a Hi3519DV500 DMEBPRO kit with OS04A10 on SENSOR0 and OS08A20 on SENSOR1, through majestic built against this branch:

  • Both fitted: SENSOR0: os04a10 on I2C4, SENSOR1: os08a10 (or os08a20) on I2C5.
  • SENSOR0's I2C4 pads switched to GPIO, so SENSOR0 looks empty: SENSOR0: no sensor answers on I2C4, and SENSOR1 is still found.

…hw_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.
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Add connector-specific sensor probing to libipchw

✨ Enhancement 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Expose connector counts and probe results so consumers can associate sensors with the correct
 ports.
• Probe only the selected I2C bus, preserving adapter, clock, and pad state.
• Test empty and populated connectors, invalid inputs, and adapter restoration.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed API has one bounded end-to-end outcome: determining which sensor, if any, is fitted to each HAL-described connector.
  • The header contract, bus-specific probe, ID decoding, and adapter restoration are hops of that same capability, not separate capabilities.
  • No independent in-repository consumer behavior is changed.
Behavioral complexity 🟡 Medium 🧩 Coordinated invariants, Interacting branches

  • Before, the public library exposed only whole-device sensor identity; after, callers can select a connector and probe only its mapped bus.
  • The new correctness conditions are that port validation, the process-global adapter, sensor-clock/pad arming, cleanup ownership, and the identity mutex remain coordinated across open failure, no-sensor, found-sensor, and invalid-port branches.
  • The ID/twin mapping itself reuses existing detection logic, but the new per-port state protocol is materially interacting.
Potential damage 🟡 Medium 🩹 Recoverable

  • If this capability regresses, an external caller may be unable to distinguish empty connectors from fitted sensors or may receive incomplete connector identity.
  • The available code performs detection only; it does not persist results or apply sensor configuration.
  • Reverting the library restores the implementation, while any caller that already acted on an incorrect result would need to rerun detection; no automatic repair path is present in this repository.
  • Revert restores the probing code.
  • Recovery from a previously consumed incorrect result requires the external caller to probe again and correct its own selection; no persistent state or compensation mechanism is shown.
Assessed commit e59d458
Diagram

graph TD
  Consumer["Library consumer"] --> Probe["Connector probe"] --> Mapping["HAL bus mapping"] --> Detect["I2C ID detection"] --> Restore["State restoration"] --> Result["Bus and names"]
  Consumer --> Ports["Port count"] --> Mapping
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Derive results from the existing sensor report
  • ➕ Reuses multi-sensor reporting logic.
  • ➖ The report starts with a fallback sweep, so it can misattribute a sensor when an earlier connector is empty.
  • ➖ Report generation is excluded from standalone-library builds.

Recommendation: Keep the dedicated connector probe. It addresses port attribution directly without changing the report's sweep behavior or making library consumers depend on report generation.

Files changed (3) +169 / -2

Enhancement (2) +94 / -2
ipchw.hDeclare connector discovery and probing APIs +37/-0

Declare connector discovery and probing APIs

• Adds a result type containing the connector's I2C bus, detected model, and an indistinguishable twin. Documents supported boards, probe return values, temporary hardware changes, and threading constraints.

include/ipchw.h

sensors.cImplement bus-scoped sensor probing +57/-2

Implement bus-scoped sensor probing

• Counts HAL-mapped connectors and identifies a sensor on only the selected connector's I2C bus. Serializes probing with the identity APIs, restores temporary state, and makes the shared-ID lookup available to library builds.

src/sensors.c

Tests (1) +75 / -0
sensors_test.cTest connector isolation and probe results +75/-0

Test connector isolation and probe results

• Adds a two-connector mock with a sensor only on the second bus. Verifies empty and populated results, bus selection, shared-ID names, invalid inputs, and restoration of the global adapter number.

src/sensors_test.c

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. A failed probe can gate sensor clocks ✓ Resolved
Description
ipchw_sensor_probe() calls hal_cleanup() even when i2c_bus_openable() fails before
arm_sensor_clock() runs. If chip initialization ungated a HiSilicon sensor clock, probing an
unavailable adapter consumes that outstanding restore and gates the clock despite never probing a
sensor.


Remediation recommended

2. Concurrent first probes can use wrong setup ✓ Resolved
Description
ipchw_sensor_probe() validates through ipchw_sensor_ports() and reads hal_sensor_buses before
acquiring sensor_indentity_mtx, although that count call invokes the unsynchronized
getchipname() initialization. Concurrent first-use calls can run HAL fallback and chip setup
against each other, so a valid port can be rejected or a probe can use partially initialized HAL
state.

Grey Divider

Tip of the day
💡 Did you know, you can commit Qodo's fix in one click with committable suggestions (GitHub & GitLab)

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

…nder 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.
@widgetii

Copy link
Copy Markdown
Member Author

Both Qodo findings are fixed in e59d458:

  1. Cleanup without an arm. hal_cleanup() now runs only after the probe's own arm_sensor_clock(), so a bus that doesn't open no longer spends a restore owed to the HAL setup. sensors_test now has a third connector whose adapter doesn't open: probing it returns 0 with no cleanup. Putting the unconditional cleanup back makes the test fail.
  2. Setup outside the lock. The connector count, which runs getchipname(), the bus choice and the probe now all run under the identity lock, through sensor_ports_locked(). ipchw_sensor_ports() takes the lock as well.

@widgetii
widgetii merged commit e061dcf into master Nov 27, 2026
5 checks passed
@widgetii
widgetii deleted the sensor-ports branch November 27, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant