From c40dec90bda08d364beb8e7a17bd970c9dc18c2c Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Tue, 17 Nov 2026 10:32:45 +0100 Subject: [PATCH] pinwheel: Fix webcam breaking after resume from hibernation Hibernation powers off the Lattice USBIO bridge that the ov08x40 sensor and Intel CVS sit behind. usbio has no reset_resume callback, so on restore the USB core unbinds and rebinds it, which re-creates the sensor and CVS I2C clients. The new clients lack the fwnode graph that ipu-bridge built at boot, and ipu-bridge never rebuilds it, so the camera stays dead until a cold boot. Patch usbio to redo the bridge handshake in reset_resume instead of being rebound, and patch gpio-usbio to restore its pin configuration and output values after such a reset. Both are built from the kernel source and installed under updates/, sharing the build recipe with the patched ipu6-isys module. --- hosts/pinwheel/modules/webcam/default.nix | 102 ++++-- .../modules/webcam/usbio-reset-resume.patch | 320 ++++++++++++++++++ 2 files changed, 388 insertions(+), 34 deletions(-) create mode 100644 hosts/pinwheel/modules/webcam/usbio-reset-resume.patch diff --git a/hosts/pinwheel/modules/webcam/default.nix b/hosts/pinwheel/modules/webcam/default.nix index 96ec3ec..231303c 100644 --- a/hosts/pinwheel/modules/webcam/default.nix +++ b/hosts/pinwheel/modules/webcam/default.nix @@ -7,46 +7,77 @@ let inherit (config.boot.kernelPackages) kernel kernelModuleMakeFlags; - ipu6-isys = pkgs.stdenv.mkDerivation { + mkPatchedModules = + { + pname, + srcPaths, + dir, + patches, + postPatch, + }: + pkgs.stdenv.mkDerivation { + inherit pname patches postPatch; + inherit (kernel) version; + + __structuredAttrs = true; + + nativeBuildInputs = kernel.moduleBuildDependencies; + + unpackPhase = '' + tar -xf ${kernel.src} --strip-components=1 --wildcards \ + ${lib.escapeShellArgs (map (path: "*/${path}") srcPaths)} + ''; + + makeFlags = kernelModuleMakeFlags ++ [ + "KERNELRELEASE=${kernel.modDirVersion}" + ]; + + installFlags = [ + "DEPMOD=true" + "INSTALL_MOD_PATH=${placeholder "out"}" + ]; + + buildPhase = '' + runHook preBuild + make -C ${kernel.dev}/lib/modules/${kernel.modDirVersion}/build \ + M=$(pwd)/${dir} "''${makeFlags[@]}" + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + make -C ${kernel.dev}/lib/modules/${kernel.modDirVersion}/build \ + M=$(pwd)/${dir} modules_install \ + "''${makeFlags[@]}" "''${installFlags[@]}" + runHook postInstall + ''; + }; + + ipu6-isys = mkPatchedModules { pname = "ipu6-isys"; - inherit (kernel) version; - - __structuredAttrs = true; - + srcPaths = [ "drivers/media/pci/intel/ipu6/*" ]; + dir = "drivers/media/pci/intel/ipu6"; patches = [ ./ipu6-isys-runtime-pm-null-check.patch ]; - - nativeBuildInputs = kernel.moduleBuildDependencies; - - unpackPhase = '' - tar -xf ${kernel.src} --strip-components=1 --wildcards '*/drivers/media/pci/intel/ipu6/*' - ''; - postPatch = '' sed -i '/+= intel-ipu6\.o/d' drivers/media/pci/intel/ipu6/Makefile ''; + }; - makeFlags = kernelModuleMakeFlags ++ [ - "KERNELRELEASE=${kernel.modDirVersion}" + usbio = mkPatchedModules { + pname = "usbio"; + srcPaths = [ + "drivers/usb/misc/usbio.c" + "drivers/gpio/gpio-usbio.c" + "include/linux/usb/usbio.h" ]; - - installFlags = [ - "DEPMOD=true" - "INSTALL_MOD_PATH=${placeholder "out"}" - ]; - - buildPhase = '' - runHook preBuild - make -C ${kernel.dev}/lib/modules/${kernel.modDirVersion}/build \ - M=$(pwd)/drivers/media/pci/intel/ipu6 "''${makeFlags[@]}" - runHook postBuild - ''; - - installPhase = '' - runHook preInstall - make -C ${kernel.dev}/lib/modules/${kernel.modDirVersion}/build \ - M=$(pwd)/drivers/media/pci/intel/ipu6 modules_install \ - "''${makeFlags[@]}" "''${installFlags[@]}" - runHook postInstall + dir = "usbio"; + patches = [ ./usbio-reset-resume.patch ]; + postPatch = '' + mkdir -p usbio/include/linux/usb + cp drivers/usb/misc/usbio.c drivers/gpio/gpio-usbio.c usbio/ + cp include/linux/usb/usbio.h usbio/include/linux/usb/ + echo 'LINUXINCLUDE := -I$(src)/include $(LINUXINCLUDE)' > usbio/Kbuild + echo 'obj-m += usbio.o gpio-usbio.o' >> usbio/Kbuild ''; }; in @@ -79,7 +110,10 @@ in }) ]; - boot.extraModulePackages = [ ipu6-isys ]; + boot.extraModulePackages = [ + ipu6-isys + usbio + ]; hardware.ipu6 = { enable = true; diff --git a/hosts/pinwheel/modules/webcam/usbio-reset-resume.patch b/hosts/pinwheel/modules/webcam/usbio-reset-resume.patch new file mode 100644 index 0000000..a02a490 --- /dev/null +++ b/hosts/pinwheel/modules/webcam/usbio-reset-resume.patch @@ -0,0 +1,320 @@ +--- a/drivers/usb/misc/usbio.c ++++ b/drivers/usb/misc/usbio.c +@@ -83,6 +83,7 @@ + * @gpios: GPIO bank descriptors + * @nr_gpio_banks: Number of I2C busses + * @gpios: I2C bank descriptors ++ * @reset_count: Number of times the bridge was reset while bound + */ + struct usbio_device { + struct device *dev; +@@ -115,6 +116,8 @@ + + unsigned int nr_i2c_buses; + struct usbio_i2c_bus_desc i2cs[USBIO_MAX_I2CBUSES]; ++ ++ unsigned int reset_count; + }; + + /** +@@ -428,6 +431,21 @@ + } + EXPORT_SYMBOL_NS_GPL(usbio_get_quirks, "USBIO"); + ++unsigned int usbio_get_reset_count(struct auxiliary_device *adev) ++{ ++ struct usbio_client *client = adev_to_client(adev); ++ struct usbio_device *usbio; ++ ++ guard(mutex)(&client->mutex); ++ ++ usbio = client->bridge; ++ if (!usbio) ++ return 0; /* Disconnected */ ++ ++ return usbio->reset_count; ++} ++EXPORT_SYMBOL_NS_GPL(usbio_get_reset_count, "USBIO"); ++ + static void usbio_auxdev_release(struct device *dev) + { + struct auxiliary_device *adev = to_auxiliary_dev(dev); +@@ -503,6 +521,47 @@ + return 0; + } + ++static int usbio_init_bridge(struct usbio_device *usbio) ++{ ++ struct usbio_protver protver; ++ struct usbio_fwver fwver; ++ int ret; ++ ++ guard(mutex)(&usbio->ctrl_mutex); ++ ++ ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_HS, NULL, 0, NULL, 0); ++ if (ret < 0) ++ return ret; ++ ++ ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_PROTVER, NULL, 0, ++ &protver, sizeof(protver)); ++ if (ret < 0) ++ return ret; ++ ++ ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_FWVER, NULL, 0, ++ &fwver, sizeof(fwver)); ++ if (ret < 0) ++ return ret; ++ ++ ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMGPIO, NULL, 0, ++ usbio->gpios, sizeof(usbio->gpios)); ++ if (ret < 0 || ret % sizeof(struct usbio_gpio_bank_desc)) ++ return (ret < 0) ? ret : -EPROTO; ++ usbio->nr_gpio_banks = ret / sizeof(struct usbio_gpio_bank_desc); ++ ++ ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMI2C, NULL, 0, ++ usbio->i2cs, sizeof(usbio->i2cs)); ++ if (ret < 0 || ret % sizeof(struct usbio_i2c_bus_desc)) ++ return (ret < 0) ? ret : -EPROTO; ++ usbio->nr_i2c_buses = ret / sizeof(struct usbio_i2c_bus_desc); ++ ++ dev_dbg(usbio->dev, "ProtVer(BCD): %02x FwVer: %d.%d.%d.%d\n", ++ protver.ver, fwver.major, fwver.minor, ++ le16_to_cpu(fwver.patch), le16_to_cpu(fwver.build)); ++ ++ return 0; ++} ++ + static int usbio_suspend(struct usb_interface *intf, pm_message_t msg) + { + struct usbio_device *usbio = usb_get_intfdata(intf); +@@ -519,6 +578,26 @@ + return usb_submit_urb(usbio->urb, GFP_KERNEL); + } + ++static int usbio_reset_resume(struct usb_interface *intf) ++{ ++ struct usbio_device *usbio = usb_get_intfdata(intf); ++ int ret; ++ ++ ret = usb_submit_urb(usbio->urb, GFP_KERNEL); ++ if (ret) ++ return ret; ++ ++ ret = usbio_init_bridge(usbio); ++ if (ret) { ++ usb_kill_urb(usbio->urb); ++ return ret; ++ } ++ ++ usbio->reset_count++; ++ ++ return 0; ++} ++ + static void usbio_disconnect(struct usb_interface *intf) + { + struct usbio_device *usbio = usb_get_intfdata(intf); +@@ -550,9 +629,7 @@ + struct usb_device *udev = interface_to_usbdev(intf); + struct usb_endpoint_descriptor *ep_in, *ep_out; + struct device *dev = &intf->dev; +- struct usbio_protver protver; + struct usbio_device *usbio; +- struct usbio_fwver fwver; + int ret; + + usbio = devm_kzalloc(dev, sizeof(*usbio), GFP_KERNEL); +@@ -623,51 +700,16 @@ + goto err_free_urb; + } + +- mutex_lock(&usbio->ctrl_mutex); +- +- ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_HS, NULL, 0, NULL, 0); +- if (ret < 0) +- goto err_unlock; +- +- ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_PROTVER, NULL, 0, +- &protver, sizeof(protver)); +- if (ret < 0) +- goto err_unlock; +- +- ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_FWVER, NULL, 0, +- &fwver, sizeof(fwver)); +- if (ret < 0) +- goto err_unlock; +- +- ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMGPIO, NULL, 0, +- usbio->gpios, sizeof(usbio->gpios)); +- if (ret < 0 || ret % sizeof(struct usbio_gpio_bank_desc)) { +- ret = (ret < 0) ? ret : -EPROTO; +- goto err_unlock; +- } +- usbio->nr_gpio_banks = ret / sizeof(struct usbio_gpio_bank_desc); +- +- ret = usbio_ctrl_msg(usbio, USBIO_PKTTYPE_CTRL, USBIO_CTRLCMD_ENUMI2C, NULL, 0, +- usbio->i2cs, sizeof(usbio->i2cs)); +- if (ret < 0 || ret % sizeof(struct usbio_i2c_bus_desc)) { +- ret = (ret < 0) ? ret : -EPROTO; +- goto err_unlock; +- } +- usbio->nr_i2c_buses = ret / sizeof(struct usbio_i2c_bus_desc); +- +- mutex_unlock(&usbio->ctrl_mutex); +- +- dev_dbg(dev, "ProtVer(BCD): %02x FwVer: %d.%d.%d.%d\n", +- protver.ver, fwver.major, fwver.minor, +- le16_to_cpu(fwver.patch), le16_to_cpu(fwver.build)); ++ ret = usbio_init_bridge(usbio); ++ if (ret) ++ goto err_kill_urb; + + usbio_enum_gpios(usbio); + usbio_enum_i2cs(usbio); + + return 0; + +-err_unlock: +- mutex_unlock(&usbio->ctrl_mutex); ++err_kill_urb: + usb_kill_urb(usbio->urb); + err_free_urb: + usb_free_urb(usbio->urb); +@@ -694,6 +736,7 @@ + .disconnect = usbio_disconnect, + .suspend = usbio_suspend, + .resume = usbio_resume, ++ .reset_resume = usbio_reset_resume, + .id_table = usbio_table, + .supports_autosuspend = 1, + }; +--- a/drivers/gpio/gpio-usbio.c ++++ b/drivers/gpio/gpio-usbio.c +@@ -15,14 +15,16 @@ + + struct usbio_gpio_bank { + u8 config[USBIO_GPIOSPERBANK]; ++ u32 value; + u32 bitmap; + }; + + struct usbio_gpio { +- struct mutex config_mutex; /* Protects banks[x].config */ ++ struct mutex config_mutex; /* Protects banks[x].config and banks[x].value */ + struct usbio_gpio_bank banks[USBIO_MAX_GPIOBANKS]; + struct gpio_chip gc; + struct auxiliary_device *adev; ++ unsigned int reset_count; + }; + + static const struct acpi_device_id usbio_gpio_acpi_hids[] = { +@@ -101,6 +103,13 @@ + + usbio_gpio_get_bank_and_pin(gc, offset, &bank, &pin); + ++ scoped_guard(mutex, &gpio->config_mutex) { ++ if (value) ++ bank->value |= BIT(pin); ++ else ++ bank->value &= ~BIT(pin); ++ } ++ + gbuf.bankid = offset / USBIO_GPIOSPERBANK; + gbuf.pincount = 1; + gbuf.pin = pin; +@@ -178,6 +187,66 @@ + return usbio_gpio_update_config(gc, offset, USBIO_GPIO_PINCFG_MASK, value); + } + ++static int usbio_gpio_restore(struct usbio_gpio *gpio) ++{ ++ struct usbio_gpio_init ginit; ++ struct usbio_gpio_rw gbuf; ++ int ret; ++ ++ guard(mutex)(&gpio->config_mutex); ++ ++ for (unsigned int b = 0; b < USBIO_MAX_GPIOBANKS; b++) { ++ struct usbio_gpio_bank *bank = &gpio->banks[b]; ++ ++ for (unsigned int pin = 0; pin < USBIO_GPIOSPERBANK; pin++) { ++ u8 mode = bank->config[pin] & USBIO_GPIO_PINMOD_MASK; ++ ++ if (mode == USBIO_GPIO_PINMOD_INVAL) ++ continue; ++ ++ ginit.bankid = b; ++ ginit.config = bank->config[pin]; ++ ginit.pincount = 1; ++ ginit.pin = pin; ++ ++ ret = usbio_control_msg(gpio->adev, USBIO_PKTTYPE_GPIO, USBIO_GPIOCMD_INIT, ++ &ginit, sizeof(ginit), NULL, 0); ++ if (ret) ++ return ret; ++ ++ if (mode != USBIO_GPIO_PINMOD_OUTPUT) ++ continue; ++ ++ gbuf.bankid = b; ++ gbuf.pincount = 1; ++ gbuf.pin = pin; ++ gbuf.value = cpu_to_le32(bank->value & BIT(pin)); ++ ++ ret = usbio_control_msg(gpio->adev, USBIO_PKTTYPE_GPIO, USBIO_GPIOCMD_WRITE, ++ &gbuf, sizeof(gbuf), NULL, 0); ++ if (ret) ++ return ret; ++ } ++ } ++ ++ return 0; ++} ++ ++static int usbio_gpio_resume(struct device *dev) ++{ ++ struct usbio_gpio *gpio = dev_get_drvdata(dev); ++ unsigned int reset_count = usbio_get_reset_count(gpio->adev); ++ ++ if (reset_count == gpio->reset_count) ++ return 0; ++ ++ gpio->reset_count = reset_count; ++ ++ return usbio_gpio_restore(gpio); ++} ++ ++static DEFINE_SIMPLE_DEV_PM_OPS(usbio_gpio_pm_ops, NULL, usbio_gpio_resume); ++ + static int usbio_gpio_probe(struct auxiliary_device *adev, + const struct auxiliary_device_id *adev_id) + { +@@ -199,6 +268,8 @@ + return ret; + + gpio->adev = adev; ++ gpio->reset_count = usbio_get_reset_count(adev); ++ dev_set_drvdata(dev, gpio); + + usbio_acpi_bind(gpio->adev, usbio_gpio_acpi_hids); + +@@ -238,7 +309,8 @@ + static struct auxiliary_driver usbio_gpio_driver = { + .name = USBIO_GPIO_CLIENT, + .probe = usbio_gpio_probe, +- .id_table = usbio_gpio_id_table ++ .id_table = usbio_gpio_id_table, ++ .driver.pm = pm_sleep_ptr(&usbio_gpio_pm_ops), + }; + module_auxiliary_driver(usbio_gpio_driver); + +--- a/include/linux/usb/usbio.h ++++ b/include/linux/usb/usbio.h +@@ -172,6 +172,7 @@ + void usbio_release(struct auxiliary_device *adev); + void usbio_get_txrxbuf_len(struct auxiliary_device *adev, u16 *txbuf_len, u16 *rxbuf_len); + unsigned long usbio_get_quirks(struct auxiliary_device *adev); ++unsigned int usbio_get_reset_count(struct auxiliary_device *adev); + void usbio_acpi_bind(struct auxiliary_device *adev, const struct acpi_device_id *hids); + + #endif