From 98982a36538249838c5f8d1aa4a08356b67d1c40 Mon Sep 17 00:00:00 2001 From: cavers-travers <363089336+cavers-travers@users.noreply.github.com> Date: Fri, 4 Dec 2026 19:07:24 +0100 Subject: [PATCH] Find the GPIO chip by label so kernel renumbering cannot break activation On a current Raspberry Pi 5 Bookworm kernel the 40-pin header is /dev/gpiochip15 [pinctrl-rp1]; neither /dev/gpiochip4 from rpi5.json nor /dev/gpiochip0 exists, so mandeye-activate stops with "missing character device" (reproduced on real hardware: RPi 5, 2x Mid-360, fake_pps). device.json "gpiochip" now accepts a chip label as well as a device path. The controller, demos and FakePPS open it with gpiod_chip_open_lookup, and mandeye-activate resolves the label through GPIO_GET_CHIPINFO_IOCTL before its permission checks. The examples use pinctrl-rp1 (Pi 5, CM5) and pinctrl-bcm2711 (Pi 4, CM4); existing path-based configurations still work. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- code/gpios.cpp | 6 +++-- code/hardware_config/device_config.cpp | 12 ++++++++- extras/FakePPS/main.cpp | 8 ++++-- packing/examples/cm5-direct.json | 2 +- packing/examples/pro-cm4-bookworm.json | 2 +- packing/examples/rpi4.json | 2 +- packing/examples/rpi5.json | 2 +- packing/mandeye-activate | 34 ++++++++++++++++++++++++-- tests/package/test-device-config.py | 12 +++++++++ 10 files changed, 70 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0d6fa3b..e56b309 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ The package identifies itself as `mandeye-multisensor`, architecture `arm64`. Re sudoedit /etc/mandeye/device.json ``` - Check the GPIO chip and line offsets on this Pi (`gpiodetect` and `gpioinfo` from the `gpiod` package). Match each LED, buzzer and button role to its wire and button pull. For an unwired role, keep its key and set its value to `null` (for example `"buzzer": null` or `"stationary": null` inside `buttons`). Enable the UARTs used by the chosen carrier in `/boot/firmware/config.txt`. Use `dtoverlay -h ` for that board's pin choices. Disable the serial login console on any UART used by the scanner. Reboot after changing overlays, then check the device paths in `/dev`. In particular the historical Pi 4 example specifies `/dev/S0` for GNSS. Enter the actual UART or set `gnss` to `null` if absent. [Raspberry Pi UART configuration](https://www.raspberrypi.com/documentation/computers/configuration.html#configure-uarts) + The examples name the GPIO chip by its label (`pinctrl-rp1` on Pi 5 and CM5, `pinctrl-bcm2711` on Pi 4 and CM4) because kernel updates renumber `/dev/gpiochipN`. A device path still works. Check the chip label and line offsets on this Pi (`gpiodetect` and `gpioinfo` from the `gpiod` package). Match each LED, buzzer and button role to its wire and button pull. For an unwired role, keep its key and set its value to `null` (for example `"buzzer": null` or `"stationary": null` inside `buttons`). Enable the UARTs used by the chosen carrier in `/boot/firmware/config.txt`. Use `dtoverlay -h ` for that board's pin choices. Disable the serial login console on any UART used by the scanner. Reboot after changing overlays, then check the device paths in `/dev`. In particular the historical Pi 4 example specifies `/dev/S0` for GNSS. Enter the actual UART or set `gnss` to `null` if absent. [Raspberry Pi UART configuration](https://www.raspberrypi.com/documentation/computers/configuration.html#configure-uarts) **Two-head wiring example.** CaveTronic's scanner uses the Pro CM4 example and the Linux account `mandeye`. Set `gnss` to `null` and `operation.benchmark_usb` to `true`. Verify stationary/write/continuous LEDs on GPIO **20/16/21**, buzzer **24**, and stationary/continuous buttons **25/8**, both pull-down. Keep both FakePPS pairs, **3 → /dev/ttyAMA2** and **2 → /dev/ttyAMA5**. Check `gpiochip` against this unit. diff --git a/code/gpios.cpp b/code/gpios.cpp index 92a743d..6fd00dc 100644 --- a/code/gpios.cpp +++ b/code/gpios.cpp @@ -31,9 +31,11 @@ GpioClient::GpioClient(const DeviceConfig& device, bool sim) if(!sim && (!device.leds.empty() || !device.buttons.empty())) { std::cout << "Opening GPIO chip " << device.gpiochip << std::endl; - chip.reset(gpiod_chip_open(device.gpiochip.c_str())); + // Accepts a path, a gpiochipN name or a stable label such as pinctrl-rp1. + chip.reset(gpiod_chip_open_lookup(device.gpiochip.c_str())); if(!chip) - throw std::runtime_error(device.gpiochip + ": " + std::strerror(errno)); + throw std::runtime_error(device.gpiochip + ": GPIO chip not found (" + std::strerror(errno) + + "); compare with the labels listed by gpiodetect"); } auto lineError = [&](const std::string& role, int gpio) { return std::runtime_error(device.gpiochip + " GPIO " + std::to_string(gpio) + " (" + role + "): " + std::strerror(errno)); diff --git a/code/hardware_config/device_config.cpp b/code/hardware_config/device_config.cpp index bc5b53a..bcccbc5 100644 --- a/code/hardware_config/device_config.cpp +++ b/code/hardware_config/device_config.cpp @@ -57,6 +57,16 @@ struct Field return result; } + // Kernels renumber /dev/gpiochipN between releases (the Pi 5 header has been 4, 0 and 15), + // so a stable chip label such as "pinctrl-rp1" is accepted as well as a device path. + std::string gpioChip() const + { + auto result = string(); + if(result.front() != '/' && result.find_first_of(" \t\r\n/") != std::string::npos) + error("expected a /dev/gpiochipN path or a GPIO chip label such as pinctrl-rp1 (see gpiodetect)"); + return result; + } + int integer() const { if(!value.is_number_integer() || value < 0 || value > std::numeric_limits::max()) @@ -96,7 +106,7 @@ DeviceConfig parseDevice(nlohmann::json document, const std::string& path) const Field root{result.document, ""}; root.object({"name", "gpiochip", "leds", "buttons", "gnss", "fake_pps", "slave_pps", "operation"}); result.name = root.member("name").string(); - result.gpiochip = root.member("gpiochip").devicePath(); + result.gpiochip = root.member("gpiochip").gpioChip(); std::map usedPins; auto gpio = [&](const Field& field) { diff --git a/extras/FakePPS/main.cpp b/extras/FakePPS/main.cpp index 1cc51ca..7bf65a7 100644 --- a/extras/FakePPS/main.cpp +++ b/extras/FakePPS/main.cpp @@ -1,8 +1,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -122,10 +124,12 @@ void sendPulses(const hardware::DeviceConfig& device) const auto& chipPath = device.gpiochip; std::cout << "Opening GPIO chip " << chipPath << std::endl; - gpiod_chip* chip = gpiod_chip_open(chipPath.c_str()); + // Accepts a path, a gpiochipN name or a stable label such as pinctrl-rp1. + gpiod_chip* chip = gpiod_chip_open_lookup(chipPath.c_str()); if(chip == nullptr) { - std::cerr << "Error: Unable to open GPIO chip." << std::endl; + std::cerr << "Error: Unable to open GPIO chip " << chipPath << " (" << std::strerror(errno) + << "); compare with the labels listed by gpiodetect." << std::endl; std::abort(); } diff --git a/packing/examples/cm5-direct.json b/packing/examples/cm5-direct.json index 67e9302..a42e210 100644 --- a/packing/examples/cm5-direct.json +++ b/packing/examples/cm5-direct.json @@ -1,6 +1,6 @@ { "name": "cm5-direct", - "gpiochip": "/dev/gpiochip4", + "gpiochip": "pinctrl-rp1", "leds": { "stationary": 26, "write": 19, diff --git a/packing/examples/pro-cm4-bookworm.json b/packing/examples/pro-cm4-bookworm.json index 5642315..fe60cb2 100644 --- a/packing/examples/pro-cm4-bookworm.json +++ b/packing/examples/pro-cm4-bookworm.json @@ -1,6 +1,6 @@ { "name": "pro-cm4-bookworm", - "gpiochip": "/dev/gpiochip0", + "gpiochip": "pinctrl-bcm2711", "leds": { "stationary": 20, "write": 16, diff --git a/packing/examples/rpi4.json b/packing/examples/rpi4.json index 039148c..d4d3f5d 100644 --- a/packing/examples/rpi4.json +++ b/packing/examples/rpi4.json @@ -1,6 +1,6 @@ { "name": "rpi4", - "gpiochip": "/dev/gpiochip0", + "gpiochip": "pinctrl-bcm2711", "leds": { "stationary": 26, "write": 19, diff --git a/packing/examples/rpi5.json b/packing/examples/rpi5.json index 759179e..dd0fd33 100644 --- a/packing/examples/rpi5.json +++ b/packing/examples/rpi5.json @@ -1,6 +1,6 @@ { "name": "rpi5", - "gpiochip": "/dev/gpiochip4", + "gpiochip": "pinctrl-rp1", "leds": { "stationary": 26, "write": 19, diff --git a/packing/mandeye-activate b/packing/mandeye-activate index 5f2001f..1cc7e13 100755 --- a/packing/mandeye-activate +++ b/packing/mandeye-activate @@ -55,6 +55,36 @@ def check_device(user, path): raise ValueError(f"{path}: {user} needs read and write access") from None +GPIO_GET_CHIPINFO_IOCTL = 0x8044B401 # _IOR(0xB4, 0x01, struct gpiochip_info), 68 bytes + + +def gpio_chip_label(path): + with open(path, "rb", buffering=0) as chip: + info = fcntl.ioctl(chip, GPIO_GET_CHIPINFO_IOCTL, bytes(68)) + return info[32:64].split(b"\0", 1)[0].decode(errors="replace") + + +def gpio_chip_path(chip): + """Resolve the device.json gpiochip value the way libgpiod's gpiod_chip_open_lookup does.""" + if chip.startswith("/"): + return chip + if chip.isdigit(): + chip = "gpiochip" + chip + if Path("/dev", chip).is_char_device(): + return str(Path("/dev", chip)) + found = [] + for path in sorted(Path("/dev").glob("gpiochip*")): + try: + label = gpio_chip_label(path) + except OSError: + continue + if label == chip: + print(f"GPIO chip {chip}: {path}", flush=True) + return str(path) + found.append(f"{path.name} [{label}]") + raise ValueError(f"GPIO chip {chip!r} not found. Available: {', '.join(found) or 'none'}") + + def uses_controller_gpio(config): return any(value is not None for group in ("leds", "buttons") for value in config[group].values()) @@ -178,12 +208,12 @@ def activate(unit, repo_override, user): repo = repo_override if repo_override is not None else recording_directory(unit, user) as_account(user, "/usr/lib/mandeye/mandeye-storage", repo) if uses_controller_gpio(config): - check_device(user, config["gpiochip"]) + check_device(user, gpio_chip_path(config["gpiochip"])) if config["gnss"]: check_device(user, config["gnss"]["uart"]) if config["fake_pps"]: fake_user = property_of(FAKE, "User") or "root" - check_device(fake_user, config["gpiochip"]) + check_device(fake_user, gpio_chip_path(config["gpiochip"])) for output in config["fake_pps"]: check_device(fake_user, output["uart"]) if config["slave_pps"]: diff --git a/tests/package/test-device-config.py b/tests/package/test-device-config.py index c9eda81..9ee62c8 100644 --- a/tests/package/test-device-config.py +++ b/tests/package/test-device-config.py @@ -57,6 +57,17 @@ def check_optional_gpio(device, path): config[field]["stationary"] = None +def check_gpiochip(device, path): + # A chip label survives kernel renumbering of /dev/gpiochipN; a device path still works. + cmd = [PROGRAMS[0], "--validate-config", path] + for chip in ("pinctrl-rp1", "pinctrl-bcm2711", "/dev/gpiochip0"): + config = device | {"gpiochip": chip} + path.write_text(json.dumps(config)) + assert run(cmd) == config + path.write_text(json.dumps(device | {"gpiochip": "pinctrl rp1"})) + run(cmd, error="/gpiochip: expected a /dev/gpiochipN path or a GPIO chip label") + + def check_slave_pps(device, path): cmd = [SLAVE, "--validate-config", path] path.write_text(json.dumps(device)) @@ -91,5 +102,6 @@ def check_slave_pps(device, path): with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "device.json" check_optional_gpio(device, path) + check_gpiochip(device, path) check_slave_pps(device, path) print("All five consumers rejected an invalid device before I/O; SlavePPS overrides passed.")