Skip to content

Commit

Permalink
Add multi-UDC support
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeChenL committed Jan 23, 2024
1 parent a7407f7 commit ed9edbb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ After=network.target

[Service]
RemainAfterExit=yes
ExecStart=/usr/sbin/radxa-otgutils adbd start
ExecStop=/usr/sbin/radxa-otgutils adbd stop
ExecReload=/usr/sbin/radxa-otgutils adbd reload
ExecStart=/usr/sbin/radxa-otgutils adbd start %i
ExecStop=/usr/sbin/radxa-otgutils adbd stop %i
ExecReload=/usr/sbin/radxa-otgutils adbd reload %i

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ After=network.target

[Service]
RemainAfterExit=yes
ExecStart=/usr/sbin/radxa-otgutils usbnet start
ExecStop=/usr/sbin/radxa-otgutils usbnet stop
ExecReload=/usr/sbin/radxa-otgutils usbnet reload
ExecStart=/usr/sbin/radxa-otgutils usbnet start %i
ExecStop=/usr/sbin/radxa-otgutils usbnet stop %i
ExecReload=/usr/sbin/radxa-otgutils usbnet reload %i

[Install]
WantedBy=multi-user.target
4 changes: 2 additions & 2 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ override_dh_builddeb:
dh_builddeb -- -Zxz

override_dh_installsystemd:
dh_installsystemd --no-enable --no-start --name=radxa-adbd
dh_installsystemd --no-enable --no-start --name=radxa-usbnet
dh_installsystemd --no-enable --no-start --name=radxa-adbd@
dh_installsystemd --no-enable --no-start --name=radxa-usbnet@
80 changes: 43 additions & 37 deletions src/radxa-otgutils
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ configfs_init()
{
modprobe libcomposite

mkdir -p "/sys/kernel/config/usb_gadget/radxa-otgutils"
pushd "/sys/kernel/config/usb_gadget/radxa-otgutils"
mkdir -p "/sys/kernel/config/usb_gadget/$UDC"
pushd "/sys/kernel/config/usb_gadget/$UDC"

echo "0x1d6b" > "idVendor" # Linux Foundation
echo "0x0104" > "idProduct" # Multifunction Composite Gadget
Expand Down Expand Up @@ -38,14 +38,14 @@ function_init()
;;
esac

if [ ! -e "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET" ];
if [ ! -e "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET" ];
then
mkdir -p "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET"
mkdir -p "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET"
fi

if [ ! -e "/sys/kernel/config/usb_gadget/radxa-otgutils/configs/r.1/$GADGET" ];
if [ ! -e "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/$GADGET" ];
then
ln -s "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET" "/sys/kernel/config/usb_gadget/radxa-otgutils/configs/r.1/$GADGET"
ln -s "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET" "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/$GADGET"
fi
}

Expand All @@ -60,14 +60,24 @@ function_stop()
;;
esac

if [ -e "/sys/kernel/config/usb_gadget/radxa-otgutils/configs/r.1/$GADGET" ];
if [ -e "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/$GADGET" ];
then
rm "/sys/kernel/config/usb_gadget/radxa-otgutils/configs/r.1/$GADGET"
rm "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/$GADGET"
fi

if [ -e "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET" ];
if [ -e "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET" ];
then
rmdir "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET"
rmdir "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET"
fi
if [ -n "$(find "/sys/kernel/config/usb_gadget/$UDC/functions" -maxdepth 0 -type d -empty)" ];
then
echo "Unregister: /sys/kernel/config/usb_gadget/$UDC"
rmdir "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/strings/0x409"
rmdir "/sys/kernel/config/usb_gadget/$UDC/configs/r.1"
rmdir "/sys/kernel/config/usb_gadget/$UDC/strings/0x409"
rmdir "/sys/kernel/config/usb_gadget/$UDC"
else
echo "$UDC" > "/sys/kernel/config/usb_gadget/$UDC/UDC" || true
fi
}

Expand All @@ -89,54 +99,50 @@ start()
;;
esac

local UDC=
UDC="$(basename "$(find /sys/class/udc -maxdepth 1 -mindepth 1 | sort | head -1)")"
if [ -z "$UDC" ];
then
echo "No USB Device Controller was detected. Did you set your OTG port to host mode?"
exit 1
fi
echo "$UDC" > "/sys/kernel/config/usb_gadget/radxa-otgutils/UDC" || true

if [[ ! $(< "/sys/kernel/config/usb_gadget/$UDC/UDC") == "" ]] ;
then
echo "Unregister: /sys/kernel/config/usb_gadget/$UDC/UDC"
echo "" > "/sys/kernel/config/usb_gadget/$UDC/UDC"
fi
sleep 1
echo "$UDC" > "/sys/kernel/config/usb_gadget/$UDC/UDC" || true
}

stop()
{
function_stop

case "$MODE" in
adbd)
start-stop-daemon --stop --oknodo --pidfile /var/run/adbd.pid --retry 5
killall adbd
# start-stop-daemon --stop --oknodo --pidfile /var/run/adbd.pid --retry 5
umount /dev/usb-ffs/adb
rmdir /dev/usb-ffs/adb
rmdir /dev/usb-ffs
;;
esac
}

MODE=
case "$1" in
adbd|usbnet)
MODE="$1"
;;
*)
echo "Unknown mode. Valid values are adbd or usbnet."
exit 1
;;
esac

MODE="$1"
UDC="$3"
case "$2" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
*)
echo "Usage: $0 <adbd|usbnet> <start|stop|restart>"
echo "Usage: $0 {start|stop|restart}"
exit 1
esac

Expand Down

0 comments on commit ed9edbb

Please sign in to comment.