Files
nixos-configs/hosts/pinwheel/modules/webcam/default.nix
T
Alexander Heldt c40dec90bd 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.
2026-11-17 10:36:46 +01:00

139 lines
3.8 KiB
Nix

{
config,
lib,
pkgs,
...
}:
let
inherit (config.boot.kernelPackages) kernel kernelModuleMakeFlags;
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";
srcPaths = [ "drivers/media/pci/intel/ipu6/*" ];
dir = "drivers/media/pci/intel/ipu6";
patches = [ ./ipu6-isys-runtime-pm-null-check.patch ];
postPatch = ''
sed -i '/+= intel-ipu6\.o/d' drivers/media/pci/intel/ipu6/Makefile
'';
};
usbio = mkPatchedModules {
pname = "usbio";
srcPaths = [
"drivers/usb/misc/usbio.c"
"drivers/gpio/gpio-usbio.c"
"include/linux/usb/usbio.h"
];
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
{
nixpkgs.overlays = [
(final: prev: {
ipu6epmtl-camera-hal = prev.ipu6epmtl-camera-hal.overrideAttrs (old: {
postPatch = (old.postPatch or "") + ''
substituteInPlace src/v4l2/MediaControl.cpp \
--replace-fail 'static const string ivscName = "Intel IVSC CSI";' \
'static const string ivscName = "Intel CVS";'
'';
});
gst_all_1 = prev.gst_all_1 // {
icamerasrc-ipu6epmtl =
(prev.gst_all_1.icamerasrc-ipu6epmtl.override {
ipu6-camera-hal = final.ipu6epmtl-camera-hal;
}).overrideAttrs
(old: {
version = "20250923_ov02e";
src = final.fetchFromGitHub {
owner = "intel";
repo = "icamerasrc";
tag = "20250923_ov02e";
hash = "sha256-mKRK0Z3dnyvzvoL6BotpVFRtkb53GQw9wUOrHIUoEsA=";
};
});
};
})
];
boot.extraModulePackages = [
ipu6-isys
usbio
];
hardware.ipu6 = {
enable = true;
platform = "ipu6epmtl";
};
services.v4l2-relayd.instances.ipu6.input = {
pipeline = lib.mkForce "icamerasrc buffer-count=10";
width = 1920;
height = 1080;
};
systemd.services.v4l2-relayd-ipu6 = {
preStart = lib.mkOverride 40 ''
mkdir -p "$(dirname "$V4L2_DEVICE_FILE")"
${config.boot.kernelPackages.v4l2loopback.bin}/bin/v4l2loopback-ctl \
add --name "Intel MIPI Camera" --exclusive-caps=1 --buffers 8 \
${toString config.hardware.ipu6.videoDeviceNumber} || [ $? -eq 17 ]
echo /dev/video${toString config.hardware.ipu6.videoDeviceNumber} > "$V4L2_DEVICE_FILE"
'';
};
}