Skip to content

Instantly share code, notes, and snippets.

@ykazakov
Created March 22, 2021 19:27
Show Gist options
  • Save ykazakov/bf11751eeae3737f74222717684c4599 to your computer and use it in GitHub Desktop.
Save ykazakov/bf11751eeae3737f74222717684c4599 to your computer and use it in GitHub Desktop.
Patch rpi3b+ v4l decoder to increase buffer timeouts
#!/bin/sh
# patch bcm2835-codec.ko to increase the buffer timeout
# based on: https://github.com/raspberrypi/linux/issues/3325#issuecomment-684040830
# https://www.raspberrypi.org/documentation/linux/kernel/building.md
# we create a patch in an lxc container to keep the installation clean
set -ex
LXC_IMAGE=images:debian/11/cloud/armhf # base image for LXC container
USER=debian # exsting user with sudo inside the container
CONTAINER=bcm2835-codec-patch
[ -n "$1" ] && CONTAINER=$1 # can be set from the command line
firmware=$(zgrep "firmware as of" \
"/usr/share/doc/raspberrypi-kernel/changelog.Debian.gz" | \
head -n1 | sed -n 's|.* \([^ ]*\)$|\1|p')
kernel="$(curl -s -L "https://github.com/raspberrypi/firmware/raw/$firmware/extra/git_hash")"
# We only care about the Pi3 here
uname="$(curl -s -L "https://github.com/raspberrypi/firmware/raw/$firmware/extra/uname_string7")"
KVER="$(echo ${uname} | grep -Po '\b(Linux version )\K(?<price>[^\ ]+)' | cat)"
builddir=/home/$USER/linux
# prepare the container
lxc launch $LXC_IMAGE $CONTAINER
lxc exec $CONTAINER -- /bin/bash -c "while ! (id -u $USER 2> /dev/null); do sleep 1; done"
lxc exec $CONTAINER -- sh -c 'echo "deb http://archive.raspberrypi.org/debian/ buster main" > /etc/apt/sources.list.d/raspi.list'
lxc exec $CONTAINER -- sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 82B129927FA3303E
lxc exec $CONTAINER -- sh -c "apt update && apt upgrade -y && apt install raspberrypi-kernel-headers git build-essential flex bison libssl-dev bc -y"
# Remaining commands are executed inside the container by the local user
lxc exec $CONTAINER -- sudo --login --user $USER /bin/bash <<EOF
set -ex
mkdir -p $builddir
cd $builddir
git init
git remote add origin https://github.com/raspberrypi/linux.git
git fetch --depth 1 origin ${kernel}
git reset --hard FETCH_HEAD
KERNEL=kernel7
cp /lib/modules/$KVER/build/Module.symvers $builddir/
make bcm2709_defconfig prepare modules_prepare
sed -i 's/COMPLETE_TIMEOUT (2 \* HZ)/COMPLETE_TIMEOUT (10 * HZ)/g' drivers/staging/vc04_services/bcm2835-codec/bcm2835-v4l2-codec.c
cd drivers/staging/vc04_services/bcm2835-codec/
make -C $builddir M=\$(pwd) modules
EOF
lxc file pull ${CONTAINER}$builddir/drivers/staging/vc04_services/bcm2835-codec/bcm2835-codec.ko .
#sudo cp bcm2835-codec.ko /lib/modules/${KVER}/kernel/drivers/staging/vc04_services/bcm2835-codec/bcm2835-codec.ko
#sudo mkdir -p /lib/modules/${KVER}/extra
#sudo cp bcm2835-codec.ko /lib/modules/${KVER}/extra/bcm2835-codec.ko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment