Hi @bartsch !!
You were on point there.
The script you posted did not quite cut it for me but the fact that you can check the state of the connection on your script was everything I needed.
I’ve checked the link_state variable before, but in my case it only shows changes after you’ve plugged the cable once and then run the commands I was discussing before.
echo -n "host" | tee /sys/bus/platform/drivers/rockchip-usb2phy/ff770000.syscon\:usb2-phy\@e450/otg_mode
echo -n "peripheral" | tee /sys/bus/platform/drivers/rockchip-usb2phy/ff770000.syscon\:usb2-phy\@e450/otg_mode
So what my version of the script does is constantly switching on and off the variables until the cable happens to connect. Once the cable Is connected and the commands are executed, then I can see on the link_state variable that the cable is in fact connected.
This solution is very hacky in my opinion since it requires me to toggle the f770000 drivers from host to peripheral constantly until the cable is connected. Still better than nothing. This was driving me insane.
This is my version of the script.
#!/usr/bin/env bash
set -e
UDC_NAME=$(ls /sys/class/udc)
UDC_CLS_D="/sys/class/udc/$UDC_NAME"
UDC_DBG_D="/sys/kernel/debug/$UDC_NAME"
link_prev=""
while true; do
echo -n "host" | tee /sys/bus/platform/drivers/rockchip-usb2phy/ff770000.syscon\:usb2-phy\@e450/otg_mode
sleep 3
echo ""
echo -n "peripheral" | tee /sys/bus/platform/drivers/rockchip-usb2phy/ff770000.syscon\:usb2-phy\@e450/otg_mode
echo ""
sleep 3
link_cur=$(< $UDC_DBG_D/link_state) # XXX: link state from debugfs
echo $link_cur
#U0 is the state of "unconnected", so continue running if we are
#in the state
if [ "$link_cur" == "U0" ]; then
echo -n "."
else
#The connected state is usually "U2"
exit 0
fi
done
Again, thank you @bartsch . You have helped me a lot here.
Maybe @jack can tell us how to properly solve this issue.