diff --git a/scripts/daemon/lesavka-core.sh b/scripts/daemon/lesavka-core.sh index 4f9918a..bed24f5 100644 --- a/scripts/daemon/lesavka-core.sh +++ b/scripts/daemon/lesavka-core.sh @@ -1,16 +1,16 @@ #!/usr/bin/env bash -# scripts/daemon/lesavka-core.sh - background stealth daemon to present gadget as usb hub of genuine devices -# Proven Pi-5 configfs gadget: HID keyboard+mouse -# Still need Web Cam Support + stereo UAC2 - -# lesavka-core - one-shot gadget bring-up for Pi-5 / Arch-ARM +# lesavka‑core.sh – one‑shot USB‑gadget bring‑up (Pi‑5 / Arch‑ARM) +# Presents: • Boot‑protocol keyboard (hidg0) +# • Boot‑protocol mouse (hidg1) +# • Stereo UAC2 speaker + microphone set -euo pipefail -# 1) Ensure the dwc2 peripheral overlay is active exactly once +#────────────────────────────────────────────────── +# 1. Ensure overlay + kernel modules +#────────────────────────────────────────────────── CFG=/boot/config.txt grep -q 'dtoverlay=dwc2,dr_mode=peripheral' "$CFG" || echo 'dtoverlay=dwc2,dr_mode=peripheral' >> "$CFG" -# 2) Load kernel modules (idempotent) modprobe dwc2 || { echo "dwc2 not in kernel; abort" >&2; exit 1; } modprobe libcomposite || { echo "libcomposite not in kernel; abort" >&2; exit 1; } @@ -21,6 +21,9 @@ udevadm control --reload udevadm trigger --subsystem-match=video4linux udevadm settle +#────────────────────────────────────────────────── +# 2. Wait for UDC device to appear (max 10 s) +#────────────────────────────────────────────────── echo "[lesavka-core] ⏳ waiting for UDC to register ..." UDC="" for _ in {1..100}; do # 100 × 100ms = 10s @@ -48,11 +51,12 @@ fi [[ -n $UDC ]] || { echo "❌ UDC not present after manual bind"; exit 1; } echo "[lesavka-core] ✅ UDC detected: $UDC" -# 3) Mount configfs once +#────────────────────────────────────────────────── +# 3. (Re‑)create gadget +#────────────────────────────────────────────────── mountpoint -q /sys/kernel/config || mount -t configfs none /sys/kernel/config G=/sys/kernel/config/usb_gadget/lesavka -# 4) Tear down any previous half-built gadget if [[ -d $G ]]; then echo '' >"$G/UDC" 2>/dev/null || true sleep 0.2 @@ -60,7 +64,6 @@ if [[ -d $G ]]; then rm -rf "$G" 2>/dev/null || true fi -# 5) Create gadget (boot-keyboard + UAC2 mic/spkr, 500 mA max) mkdir -p "$G" echo 0x1d6b >"$G/idVendor" # Linux Foundation echo 0x0104 >"$G/idProduct" # Multifunction Composite Gadget @@ -95,28 +98,32 @@ printf '\x05\x01\x09\x02\xa1\x01\x09\x01\xa1\x00'\ # ---------- UAC2 function – speaker + mic, 2×48 kHz stereo --------- mkdir -p "$G/functions/uac2.usb0" -cfg="$G/functions/uac2.usb0" -echo 48000 >"$cfg/s_spk/freq" -echo 2 >"$cfg/s_spk/chs" # L+R -echo 16 >"$cfg/s_spk/strm_maxpacket" -echo 48000 >"$cfg/c_mic/freq" -echo 2 >"$cfg/c_mic/chs" -echo 16 >"$cfg/c_mic/strm_maxpacket" +U="$G/functions/uac2.usb0" +# Playback (speaker) +echo 0x3 >"$U/p_chmask" # L+R +echo 48000 >"$U/p_srate" +echo 2 >"$U/p_ssize" # 16 bit +# Capture (microphone) +echo 0x3 >"$U/c_chmask" +echo 48000 >"$U/c_srate" +echo 2 >"$U/c_ssize" +# Optional: allocate a few extra request buffers +echo 32 >"$U/req_number" 2>/dev/null || true # ----------------------- configuration ----------------------------- mkdir -p "$G/configs/c.1/strings/0x409" echo 500 > "$G/configs/c.1/MaxPower" -echo "Config 1" > "$G/configs/c.1/strings/0x409/configuration" +# echo "Config 1" > "$G/configs/c.1/strings/0x409/configuration" +echo "Config 1: HID + UAC2" >"$G/configs/c.1/strings/0x409/configuration" -# 6) Finally bind to first available UDC ln -s $G/functions/hid.usb0 $G/configs/c.1/ ln -s $G/functions/hid.usb1 $G/configs/c.1/ ln -sf "$cfg" "$G/configs/c.1/" -# 7) Finally bind to first available UDC -echo "$UDC" > "$G/UDC" -echo "[lesavka-core] 🎉 gadget ready" -echo "⌨️ hidg0 gadget ready on controller $UDC" -echo "🖱️ hidg1 gadget ready on controller $UDC" -echo "🎧 UAC2 gadget ready on controller $UDC" -echo "🎤 UAC2 gadget ready on controller $UDC" +#────────────────────────────────────────────────── +# 4. Bind gadget +#────────────────────────────────────────────────── +echo "$UDC" >"$G/UDC" +log "🎉 gadget bound on $UDC (hidg0, hidg1, UAC2 L+R)" + +exit 0