Adds gk7205v200_otg_generic, a firmware profile that puts the single DWC3 controller into dual-role so the board can either consume a USB webcam (existing usbcam path, host role) or present itself to a PC as one (UVC gadget, device role). The roles are mutually exclusive -- xHCI HCSPARAMS1 reports MaxPorts = 1 -- but usb-mode switches between them at runtime, so one image covers both and no reboot is needed. Kernel side, three patches: 0015 sets dr_mode = "otg" in the DTS so dwc3 registers both an xHCI host and a UDC. 0016 makes device mode actually work on a part that has only ever run as a host. Three fixes: re-run dwc3_event_buffers_setup() on gadget start (an xHCI reset wipes GEVNTADR), soft-reset the core before taking the lock so the PHY is re-initialised (without it high-speed chirp fails), and swallow the run_stop timeout on pullup(0), which otherwise permanently poisons cdev->deactivations. 0017 disables the vendor's UVC_SG_REQ path. It hands a whole frame to the UDC as one scatter-gather request; dwc3 chains those sg entries into a single isochronous service interval, which on a full-speed link is one 1023-byte packet, so every frame larger than that reached the host truncated and H.264 decoders reported "bytestream -5" on roughly half the frames. Goke never shipped this path enabled -- CONFIG_USB_CONFIGFS_F_UVC is unset in every vendor defconfig, including gk7605v100's, and the SDK carries no userspace UVC sample -- so it was never exercised. The mainline path (32 plain one-packet requests) is used instead. Userspace side: usb-mode switches roles with the unbind/flip/bind sequencing dwc3_set_mode() does not do on its own, uvc-gadget-setup composes the configfs gadget, and S72usbmode forces a concrete role at boot -- not optional, since dr_mode = "otg" leaves GCTL.PRTCAPDIR in a state this 4.9 dwc3 never moves off by itself. The descriptors advertise exactly the one format, frame size and interval majestic is configured to serve, read from /etc/majestic.yaml. majestic answers every PROBE/COMMIT with its own bFormatIndex/bFrameIndex because it has one stream and cannot follow the host, so anything extra is an invitation to select something that will not arrive: asking for MJPEG got raw H.264 (an SPS where a JPEG SOI belongs, garbage to every consumer), and asking for 1080p got 720p. Profile drops the air-unit packages (wifibroadcast, rtl8812au, mavfwd, mavlink-router, vtund, linux-firmware, wireless-tools) and the whole wireless kernel stack, which pays for usb_f_uvc.ko and for keeping the full sensor list -- the fpv trim to IMX307/IMX335 is keyed on OPENIPC_VARIANT="fpv", and a general-purpose USB webcam should not be sensor-locked. Measured on a gk7205v200 lab board over a full-speed link: H.264 1280x720 at 30 fps with zero decoder errors and no dropped buffers, and MJPEG delivering valid JPEGs at the jpeg channel's own rate. Requires a majestic built with ENABLE_UVC_GADGET to feed the gadget; that is a separate change.
160 lines
5.0 KiB
Bash
Executable File
160 lines
5.0 KiB
Bash
Executable File
#!/bin/sh
|
|
#
|
|
# Switch the single DWC3 controller between USB host and USB device roles.
|
|
#
|
|
# host - xHCI drives the port. This is what the usbcam second-camera path
|
|
# needs (majestic consuming a USB webcam).
|
|
# device - the UVC gadget drives the port, so a PC sees this camera as a
|
|
# webcam.
|
|
#
|
|
# The two are mutually exclusive: xHCI HCSPARAMS1 reports MaxPorts = 1, so
|
|
# there is one port and one role at a time. Switching does not need a reboot.
|
|
#
|
|
# Why this is more than one echo: dwc3_set_mode() (drivers/usb/dwc3/core.c)
|
|
# only flips GCTL.PRTCAPDIR. It does not stop the host stack or start the
|
|
# gadget stack. Writing the debugfs `mode` file on its own leaves xHCI still
|
|
# bound and its DMA engine still live, pointed at a controller that has been
|
|
# re-aimed underneath it. Unbind first, then flip, then bring the other side
|
|
# up.
|
|
|
|
set -e
|
|
|
|
STATE=/etc/usbmode
|
|
GADGET=/sys/kernel/config/usb_gadget/uvc0
|
|
|
|
die() { echo "usb-mode: $*" >&2; exit 1; }
|
|
|
|
dwc3_mode_file() {
|
|
for f in /sys/kernel/debug/*.dwc3/mode; do
|
|
[ -e "$f" ] && { echo "$f"; return 0; }
|
|
done
|
|
return 1
|
|
}
|
|
|
|
xhci_device() {
|
|
for d in /sys/bus/platform/drivers/xhci-hcd/*.auto; do
|
|
[ -e "$d" ] && { basename "$d"; return 0; }
|
|
done
|
|
# Not currently bound; fall back to the device that exists on the bus.
|
|
for d in /sys/bus/platform/devices/xhci-hcd.*.auto; do
|
|
[ -e "$d" ] && { basename "$d"; return 0; }
|
|
done
|
|
return 1
|
|
}
|
|
|
|
udc_name() {
|
|
for u in /sys/class/udc/*; do
|
|
[ -e "$u" ] && { basename "$u"; return 0; }
|
|
done
|
|
return 1
|
|
}
|
|
|
|
need_debugfs() {
|
|
mountpoint -q /sys/kernel/debug 2>/dev/null ||
|
|
mount -t debugfs none /sys/kernel/debug
|
|
MODE_FILE=$(dwc3_mode_file) ||
|
|
die "no dwc3 mode file; is CONFIG_USB_DWC3_DUAL_ROLE enabled and debugfs mounted?"
|
|
}
|
|
|
|
teardown_gadget() {
|
|
[ -d "$GADGET" ] || return 0
|
|
|
|
# Unbind from the UDC first: this drops the D+ pullup so the host sees a
|
|
# disconnect rather than a device that stops answering.
|
|
if [ -e "$GADGET/UDC" ] && [ -s "$GADGET/UDC" ]; then
|
|
echo "" > "$GADGET/UDC" 2>/dev/null || true
|
|
fi
|
|
|
|
# configfs rmdir is strict about ordering: links before the things they
|
|
# point at, leaves before their parents. ENOTEMPTY here means something
|
|
# below was missed.
|
|
rm -f "$GADGET/configs/c.1/uvc.0"
|
|
|
|
F="$GADGET/functions/uvc.0"
|
|
rm -f "$F/control/class/fs/h" "$F/control/class/ss/h"
|
|
rm -f "$F/streaming/class/fs/h" "$F/streaming/class/hs/h" "$F/streaming/class/ss/h"
|
|
# uvc-gadget-setup composes whichever single format majestic is set to, so
|
|
# tear down both possibilities: only one of them exists on any given boot.
|
|
rm -f "$F/streaming/header/h/f"
|
|
rmdir "$F/streaming/header/h" 2>/dev/null || true
|
|
rmdir "$F/streaming/mjpeg/m/f" 2>/dev/null || true
|
|
rmdir "$F/streaming/mjpeg/m" 2>/dev/null || true
|
|
rmdir "$F/streaming/framebased/h/f" 2>/dev/null || true
|
|
rmdir "$F/streaming/framebased/h" 2>/dev/null || true
|
|
rmdir "$F/control/header/h" 2>/dev/null || true
|
|
|
|
# EBUSY here means a userspace process still holds the gadget's
|
|
# /dev/videoN open -- almost always majestic. Stop it first.
|
|
if ! rmdir "$F" 2>/dev/null; then
|
|
die "cannot remove uvc.0 (EBUSY?) -- is majestic still holding /dev/video*? stop it first"
|
|
fi
|
|
|
|
rmdir "$GADGET/configs/c.1/strings/0x409" 2>/dev/null || true
|
|
rmdir "$GADGET/configs/c.1" 2>/dev/null || true
|
|
rmdir "$GADGET/strings/0x409" 2>/dev/null || true
|
|
rmdir "$GADGET" 2>/dev/null || true
|
|
}
|
|
|
|
to_device() {
|
|
need_debugfs
|
|
|
|
if X=$(xhci_device) && [ -e "/sys/bus/platform/drivers/xhci-hcd/$X" ]; then
|
|
echo "usb-mode: unbinding host controller $X"
|
|
echo "$X" > /sys/bus/platform/drivers/xhci-hcd/unbind
|
|
fi
|
|
|
|
echo device > "$MODE_FILE"
|
|
|
|
modprobe configfs 2>/dev/null || true
|
|
modprobe libcomposite 2>/dev/null || true
|
|
modprobe usb_f_uvc 2>/dev/null || true
|
|
|
|
mountpoint -q /sys/kernel/config 2>/dev/null ||
|
|
mount -t configfs none /sys/kernel/config
|
|
|
|
/usr/bin/uvc-gadget-setup
|
|
|
|
UDC=$(udc_name) ||
|
|
die "no UDC registered -- is dr_mode=\"otg\" in the device tree?"
|
|
echo "$UDC" > "$GADGET/UDC"
|
|
|
|
echo device > "$STATE"
|
|
echo "usb-mode: device (UVC gadget bound to $UDC)"
|
|
}
|
|
|
|
to_host() {
|
|
need_debugfs
|
|
|
|
teardown_gadget
|
|
|
|
echo host > "$MODE_FILE"
|
|
|
|
if X=$(xhci_device) && [ ! -e "/sys/bus/platform/drivers/xhci-hcd/$X" ]; then
|
|
echo "usb-mode: rebinding host controller $X"
|
|
echo "$X" > /sys/bus/platform/drivers/xhci-hcd/bind
|
|
fi
|
|
|
|
echo host > "$STATE"
|
|
echo "usb-mode: host"
|
|
}
|
|
|
|
status() {
|
|
need_debugfs
|
|
echo "dwc3 role : $(cat "$MODE_FILE")"
|
|
echo "persisted : $([ -r "$STATE" ] && cat "$STATE" || echo '(unset, defaults to host)')"
|
|
echo "udc : $(udc_name || echo '(none)')"
|
|
echo "xhci bound: $(xhci_device >/dev/null 2>&1 && \
|
|
([ -e "/sys/bus/platform/drivers/xhci-hcd/$(xhci_device)" ] && echo yes || echo no) || echo '(no device)')"
|
|
echo "gadget : $([ -d "$GADGET" ] && echo "composed, UDC='$(cat "$GADGET/UDC" 2>/dev/null)'" || echo 'not composed')"
|
|
}
|
|
|
|
case "$1" in
|
|
device) to_device ;;
|
|
host) to_host ;;
|
|
status) status ;;
|
|
*)
|
|
echo "Usage: $0 {host|device|status}"
|
|
exit 1
|
|
;;
|
|
esac
|