#!/bin/bash set -euo pipefail # Reset USB controller and re-bind gadget UDC="4e00000.usb" GADGET="/sys/kernel/config/usb_gadget/g1" # Find the driver binding DRIVER="" for d in /sys/bus/platform/drivers/*/; do dname=$(basename "$d") if [ -L "$d/$UDC" ] 2>/dev/null; then DRIVER="$dname" break fi done if [ -z "$DRIVER" ]; then # Try looking at the UDC device itself UDC_PATH=$(readlink -f /sys/class/udc/$UDC/device 2>/dev/null || echo "") if [ -n "$UDC_PATH" ]; then DRIVER=$(basename "$(dirname "$UDC_PATH")" 2>/dev/null || echo "") fi fi echo "Driver: ${DRIVER:-unknown}" # Unbind if [ -n "$DRIVER" ]; then echo "$UDC" > "/sys/bus/platform/drivers/$DRIVER/unbind" 2>/dev/null && echo "Unbound" || echo "Unbind failed" sleep 2 echo "$UDC" > "/sys/bus/platform/drivers/$DRIVER/bind" 2>/dev/null && echo "Rebound" || echo "Rebind failed" sleep 1 fi # Bind gadget echo "$UDC" > "$GADGET/UDC" 2>&1 && echo "Gadget bound" || echo "Gadget bind failed" cat /sys/class/udc/$UDC/state