#!/bin/sh # Hardware test on the USB I2C rig: panel U1 (0x74) and keys U2 (0x76). # # tests/hw_test.sh [-b BUS] [MODE] # # MODE: # keys (default) init the panel, show the keys on it; Ctrl-C quits # keys-only the keys printed to the terminal, panel untouched # pattern geometry and gray patterns, a few seconds each, then keys # ui the full UI with the I2C keys, against the fake MPD (port 6601), # never the real player; commands go to build/mpd-commands.log # init set up the panel (i2c-dev) and U2, then exit leaving them on # drm (sudo) load s1d13700-tca and bind it to 0x74, show a pattern, # backlight on, set up U2; left active for radio-ui drm etc. # release (sudo) unbind the DRM driver from 0x74 again # # Without -b, the bus is the one whose adapter is named i2c-tiny-usb. Only the # names in /sys are read: no bus is scanned. set -eu cd "$(dirname "$0")/.." bus= while getopts b:h opt; do case $opt in b) bus=$OPTARG ;; *) sed -n '2,18s/^# \{0,1\}//p' "$0"; exit 2 ;; esac done shift $((OPTIND - 1)) mode=${1:-keys} if [ -z "$bus" ]; then for f in /sys/class/i2c-dev/i2c-*/name; do case $(cat "$f") in i2c-tiny-usb*) [ -z "$bus" ] || { echo "more than one i2c-tiny-usb bus; pass -b" >&2; exit 1; } bus=${f#/sys/class/i2c-dev/i2c-} bus=${bus%/name} ;; esac done [ -n "$bus" ] || { echo "no i2c-tiny-usb bus; plug the bridge in and run:" >&2 echo " sudo modprobe i2c_tiny_usb delay=1" >&2; exit 1; } fi echo "bus i2c-$bus: $(cat /sys/class/i2c-dev/i2c-"$bus"/name)" DRV=s1d13700-tca KO=driver/$DRV.ko dev74=/sys/bus/i2c/devices/$bus-0074 as_root() { if [ "$(id -u)" = 0 ]; then "$@"; else sudo "$@"; fi; } write_root() { echo "$2" | as_root tee "$1" >/dev/null; } bound_to() { # driver name bound to $bus-$1, or nothing [ -e "/sys/bus/i2c/devices/$bus-$1/driver" ] && basename "$(readlink "/sys/bus/i2c/devices/$bus-$1/driver")" return 0 } # A kernel driver bound to either chip would fight over i2c-dev access. # The DRM modes manage 0x74 themselves; U2 is always ours. for a in 0074 0076; do case $mode:$a in drm:0074|release:0074) continue ;; esac drv=$(bound_to $a) if [ -n "$drv" ]; then echo "0x${a#00} on i2c-$bus is bound to $drv; unbind it first:" >&2 [ "$drv" = $DRV ] && echo " $0 -b $bus release" >&2 || echo " echo $bus-$a | sudo tee /sys/bus/i2c/devices/$bus-$a/driver/unbind" >&2 exit 1 fi done [ -e build/libradiopanel.so ] || make lib ui() { ./radio-ui "$@"; } drm_setup() { if [ ! -d /sys/module/s1d13700_tca ]; then if [ ! -e $KO ] || [ "$(modinfo -F vermagic $KO | cut -d' ' -f1)" != "$(uname -r)" ]; then make module fi for dep in $(modinfo -F depends $KO | tr , ' '); do as_root modprobe "$dep" done as_root insmod $KO fi drv=$(bound_to 0074) if [ -z "$drv" ]; then if [ -e "$dev74" ]; then write_root /sys/bus/i2c/drivers/$DRV/bind "$bus-0074" else write_root "/sys/bus/i2c/devices/i2c-$bus/new_device" "s1d13700-tca9539 0x74" fi elif [ "$drv" != $DRV ]; then echo "0x74 on i2c-$bus is bound to $drv" >&2; exit 1 fi i=0 until ls -d "$dev74"/drm/card* >/dev/null 2>&1; do i=$((i + 1)) [ $i -lt 50 ] || { echo "no DRM card appeared for $bus-0074" >&2; exit 1; } sleep 0.1 done card=$(basename "$(ls -d "$dev74"/drm/card* | head -n 1)") echo "$DRV bound to $bus-0074: /dev/dri/$card" # udev sets the node's group and ACL a moment after it appears command -v udevadm >/dev/null && udevadm settle --timeout=5 2>/dev/null || true i=0 until [ -w /dev/dri/"$card" ]; do i=$((i + 1)) [ $i -lt 50 ] || { echo "no write access to /dev/dri/$card (group video?)" >&2; exit 1; } sleep 0.1 done ui drm --pattern geometry bl=/sys/class/backlight/radio-backlight/brightness if [ -w $bl ]; then echo 1 > $bl; else write_root $bl 1; fi ui keytest --bus "$bus" --no-panel --init-only echo "left active. Try: ./radio-ui drm --host radio.lo (keys from 0x76 on i2c-$bus)" echo "undo with: $0 -b $bus release" } drm_release() { drv=$(bound_to 0074) if [ "$drv" = $DRV ]; then # a device made by new_device goes away; one from DT only unbinds write_root "/sys/bus/i2c/devices/i2c-$bus/delete_device" 0x74 2>/dev/null || write_root /sys/bus/i2c/drivers/$DRV/unbind "$bus-0074" echo "$DRV released 0x74 on i2c-$bus (module left loaded)" elif [ -n "$drv" ]; then echo "0x74 on i2c-$bus is bound to $drv, not $DRV; left alone" >&2; exit 1 else echo "nothing bound to 0x74 on i2c-$bus" fi } case $mode in keys) ui keytest --bus "$bus" ;; keys-only) ui keytest --bus "$bus" --no-panel ;; pattern) ui i2c --bus "$bus" --pattern geometry sleep 3 ui i2c --bus "$bus" --pattern gray sleep 3 ui keytest --bus "$bus" ;; ui) mkdir -p build log=build/mpd-commands.log : > "$log" python3 tests/fake_mpd.py 6601 "$log" 5 & mpd_pid=$! trap 'kill $mpd_pid 2>/dev/null' EXIT INT TERM sleep 0.5 echo "MPD commands: $log" ui i2c --bus "$bus" --host localhost --port 6601 || true ;; init) ui keytest --bus "$bus" --init-only ;; drm) drm_setup ;; release) drm_release ;; *) echo "unknown mode $mode (keys, keys-only, pattern, ui, init, drm, release)" >&2; exit 2 ;; esac