hw_test.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/sh
  2. # Hardware test on the USB I2C rig: panel U1 (0x74) and keys U2 (0x76).
  3. #
  4. # tests/hw_test.sh [-b BUS] [MODE]
  5. #
  6. # MODE:
  7. # keys (default) init the panel, show the keys on it; Ctrl-C quits
  8. # keys-only the keys printed to the terminal, panel untouched
  9. # pattern geometry and gray patterns, a few seconds each, then keys
  10. # ui the full UI with the I2C keys, against the fake MPD (port 6601),
  11. # never the real player; commands go to build/mpd-commands.log
  12. # init set up the panel (i2c-dev) and U2, then exit leaving them on
  13. # drm (sudo) load s1d13700-tca and bind it to 0x74, show a pattern,
  14. # backlight on, set up U2; left active for radio-ui drm etc.
  15. # release (sudo) unbind the DRM driver from 0x74 again
  16. #
  17. # Without -b, the bus is the one whose adapter is named i2c-tiny-usb. Only the
  18. # names in /sys are read: no bus is scanned.
  19. set -eu
  20. cd "$(dirname "$0")/.."
  21. bus=
  22. while getopts b:h opt; do
  23. case $opt in
  24. b) bus=$OPTARG ;;
  25. *) sed -n '2,18s/^# \{0,1\}//p' "$0"; exit 2 ;;
  26. esac
  27. done
  28. shift $((OPTIND - 1))
  29. mode=${1:-keys}
  30. if [ -z "$bus" ]; then
  31. for f in /sys/class/i2c-dev/i2c-*/name; do
  32. case $(cat "$f") in
  33. i2c-tiny-usb*)
  34. [ -z "$bus" ] || { echo "more than one i2c-tiny-usb bus; pass -b" >&2; exit 1; }
  35. bus=${f#/sys/class/i2c-dev/i2c-}
  36. bus=${bus%/name} ;;
  37. esac
  38. done
  39. [ -n "$bus" ] || { echo "no i2c-tiny-usb bus; plug the bridge in and run:" >&2
  40. echo " sudo modprobe i2c_tiny_usb delay=1" >&2; exit 1; }
  41. fi
  42. echo "bus i2c-$bus: $(cat /sys/class/i2c-dev/i2c-"$bus"/name)"
  43. DRV=s1d13700-tca
  44. KO=driver/$DRV.ko
  45. dev74=/sys/bus/i2c/devices/$bus-0074
  46. as_root() { if [ "$(id -u)" = 0 ]; then "$@"; else sudo "$@"; fi; }
  47. write_root() { echo "$2" | as_root tee "$1" >/dev/null; }
  48. bound_to() { # driver name bound to $bus-$1, or nothing
  49. [ -e "/sys/bus/i2c/devices/$bus-$1/driver" ] &&
  50. basename "$(readlink "/sys/bus/i2c/devices/$bus-$1/driver")"
  51. return 0
  52. }
  53. # A kernel driver bound to either chip would fight over i2c-dev access.
  54. # The DRM modes manage 0x74 themselves; U2 is always ours.
  55. for a in 0074 0076; do
  56. case $mode:$a in drm:0074|release:0074) continue ;; esac
  57. drv=$(bound_to $a)
  58. if [ -n "$drv" ]; then
  59. echo "0x${a#00} on i2c-$bus is bound to $drv; unbind it first:" >&2
  60. [ "$drv" = $DRV ] && echo " $0 -b $bus release" >&2 ||
  61. echo " echo $bus-$a | sudo tee /sys/bus/i2c/devices/$bus-$a/driver/unbind" >&2
  62. exit 1
  63. fi
  64. done
  65. [ -e build/libradiopanel.so ] || make lib
  66. ui() { ./radio-ui "$@"; }
  67. drm_setup() {
  68. if [ ! -d /sys/module/s1d13700_tca ]; then
  69. if [ ! -e $KO ] || [ "$(modinfo -F vermagic $KO | cut -d' ' -f1)" != "$(uname -r)" ]; then
  70. make module
  71. fi
  72. for dep in $(modinfo -F depends $KO | tr , ' '); do
  73. as_root modprobe "$dep"
  74. done
  75. as_root insmod $KO
  76. fi
  77. drv=$(bound_to 0074)
  78. if [ -z "$drv" ]; then
  79. if [ -e "$dev74" ]; then
  80. write_root /sys/bus/i2c/drivers/$DRV/bind "$bus-0074"
  81. else
  82. write_root "/sys/bus/i2c/devices/i2c-$bus/new_device" "s1d13700-tca9539 0x74"
  83. fi
  84. elif [ "$drv" != $DRV ]; then
  85. echo "0x74 on i2c-$bus is bound to $drv" >&2; exit 1
  86. fi
  87. i=0
  88. until ls -d "$dev74"/drm/card* >/dev/null 2>&1; do
  89. i=$((i + 1))
  90. [ $i -lt 50 ] || { echo "no DRM card appeared for $bus-0074" >&2; exit 1; }
  91. sleep 0.1
  92. done
  93. card=$(basename "$(ls -d "$dev74"/drm/card* | head -n 1)")
  94. echo "$DRV bound to $bus-0074: /dev/dri/$card"
  95. # udev sets the node's group and ACL a moment after it appears
  96. command -v udevadm >/dev/null && udevadm settle --timeout=5 2>/dev/null || true
  97. i=0
  98. until [ -w /dev/dri/"$card" ]; do
  99. i=$((i + 1))
  100. [ $i -lt 50 ] || { echo "no write access to /dev/dri/$card (group video?)" >&2; exit 1; }
  101. sleep 0.1
  102. done
  103. ui drm --pattern geometry
  104. bl=/sys/class/backlight/radio-backlight/brightness
  105. if [ -w $bl ]; then echo 1 > $bl; else write_root $bl 1; fi
  106. ui keytest --bus "$bus" --no-panel --init-only
  107. echo "left active. Try: ./radio-ui drm --host radio.lo (keys from 0x76 on i2c-$bus)"
  108. echo "undo with: $0 -b $bus release"
  109. }
  110. drm_release() {
  111. drv=$(bound_to 0074)
  112. if [ "$drv" = $DRV ]; then
  113. # a device made by new_device goes away; one from DT only unbinds
  114. write_root "/sys/bus/i2c/devices/i2c-$bus/delete_device" 0x74 2>/dev/null ||
  115. write_root /sys/bus/i2c/drivers/$DRV/unbind "$bus-0074"
  116. echo "$DRV released 0x74 on i2c-$bus (module left loaded)"
  117. elif [ -n "$drv" ]; then
  118. echo "0x74 on i2c-$bus is bound to $drv, not $DRV; left alone" >&2; exit 1
  119. else
  120. echo "nothing bound to 0x74 on i2c-$bus"
  121. fi
  122. }
  123. case $mode in
  124. keys)
  125. ui keytest --bus "$bus" ;;
  126. keys-only)
  127. ui keytest --bus "$bus" --no-panel ;;
  128. pattern)
  129. ui i2c --bus "$bus" --pattern geometry
  130. sleep 3
  131. ui i2c --bus "$bus" --pattern gray
  132. sleep 3
  133. ui keytest --bus "$bus" ;;
  134. ui)
  135. mkdir -p build
  136. log=build/mpd-commands.log
  137. : > "$log"
  138. python3 tests/fake_mpd.py 6601 "$log" 5 &
  139. mpd_pid=$!
  140. trap 'kill $mpd_pid 2>/dev/null' EXIT INT TERM
  141. sleep 0.5
  142. echo "MPD commands: $log"
  143. ui i2c --bus "$bus" --host localhost --port 6601 || true ;;
  144. init)
  145. ui keytest --bus "$bus" --init-only ;;
  146. drm)
  147. drm_setup ;;
  148. release)
  149. drm_release ;;
  150. *)
  151. echo "unknown mode $mode (keys, keys-only, pattern, ui, init, drm, release)" >&2; exit 2 ;;
  152. esac