#!/bin/sh
#
# Force a concrete USB role at boot.
#
# The device tree sets dr_mode = "otg" so that dwc3 registers both an xHCI host
# and a UDC, which is what makes runtime role switching possible. The role
# itself is settled in the kernel: all-patches 0018 programs
# GCTL.PRTCAPDIR = HOST before the xHCI host is registered, because a host that
# probes while the core is still in OTG(3) comes up unable to drive the port --
# it runs, powers the port, and never reports a connect. So the board boots as
# a host, and this script exists to apply a persisted "device" choice on top.
#
# Runs after S70vendor so the vendor modules are up first.

case "$1" in
	start)
		mode=host
		[ -r /etc/usbmode ] && read -r mode < /etc/usbmode
		case "$mode" in
			host|device) ;;
			*) mode=host ;;
		esac
		echo "Setting USB role to $mode..."
		/usr/bin/usb-mode "$mode" || echo "usb-mode $mode failed"
		;;

	stop)
		;;

	*)
		echo "Usage: $0 {start|stop}"
		exit 1
		;;
esac
