device that supports both usb host & usb device modes?

Quentin Schulz quentin.schulz at cherry.de
Mon Oct 14 18:09:49 CEST 2024


Hi Marek,

On 10/10/24 9:31 AM, Marek Behún wrote:
> Hi all,
> 
> I am searching for HW that supports both USB host & USB device/gadget
> mode, on the same USB port, capable of switching between these modes
> at runtime.
> 
> Preferably supported in upstream kernel.
> 

I'm doing a very simple test on our (formerly known as Theobroma 
Systems) RK3399 Puma with Haikou devkit and it seems to work fine 
(insert USB stick into microUSB, detect the stick was connected, remove 
USB stick, loop microUSB to USB, configure as ACM, send 
test123test123test one way and then the other) as I have not commented 
out the test for our internal releases :)

I believe that our RK3588 Tiger with Haikou devkit also supports it 
(same test) but I am not the one who upstreamed nor tested that part 
(and it doesn't work on our devices in peripheral mode on Rockchip BSP 
kernel 5.10/6.1).

I don't think we do anything crazy here on the HW level, so I would say 
anything based on RK3399 and RK3588(S) which advertises an USB OTG port 
should be able to work. For RK3588(S) maybe check if it's on USB-C, as I 
am not entirely sure whether we (upstream kernel) support FUSB302 (used 
in the reference design from Rockchip I believe, so most manufacturers 
do use them to limit risks) USB-C connector for OTG just yet (about to 
though if I remember correctly). Maybe have a look on the side of RK356x 
(RK3566/RK3568) as well, I've seen a few with ROCKUSB enabled in U-Boot, 
which means OTG (at least in peripheral mode) is supported in U-Boot.

For reference, this is the test I run:
"""
#!/bin/sh
# Change UDC environment variable according to USB controller connected 
to USB OTG port

set -eu

UDC=${UDC:-"fc000000.usb"}

cleanup_usb_otg() {
	echo "" > "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/UDC"
	rm "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/configs/conf.1/acm.1"
	rmdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/configs/conf.1/strings/0x409/"
	rmdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/configs/conf.1/"
	rmdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/functions/acm.1/"
	rmdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/strings/0x409/"
	rmdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/"
}

run_cmd() {
	set -x
	"$@"
	set +x
}

test_serial() {
	TX=$1
	RX=$2
	RXPROOF=$(mktemp)
	ORIG=$(mktemp)

	(cat "$RX" > "$RXPROOF")&
	PID=$!

	printf "test123test123test\n" > "$ORIG"
	# NOTE: \r instead of \n
	# The message is sent only if \r or \n is passed, however the receiving
	# end adds an additional newline. Therefore, to check data integrity the
	# last character is different in the reference file and the message
	# being sent.
	printf "test123test123test\r" > "$TX"

	# Give some time for the message to make it to the other side of the
	# communication before killing the process.
	sleep 1

	kill $PID
	wait $PID || true

	diff "$ORIG" "$RXPROOF"

	rm -rf "$ORIG" "$RXPROOF"
}

CONFIGFS_HOME=$(findmnt --noheadings --types configfs -o target)

if [ -z "$CONFIGFS_HOME" ]; then
	CONFIGFS_HOME=$(mktemp --directory)
	run_cmd mount none -t configfs "$CONFIGFS_HOME"
fi

if [ -e "$CONFIGFS_HOME/usb_gadget/q7-usb-p2" ]; then
	set +e
	run_cmd cleanup_usb_otg
	set -e
fi

run_cmd mkdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2"

run_cmd echo 0xabcd > "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/idVendor"
run_cmd echo 0xef01 > "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/idProduct"
run_cmd mkdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/strings/0x409"
run_cmd echo SN1234567890 > 
"$CONFIGFS_HOME/usb_gadget/q7-usb-p2/strings/0x409/serialnumber"
run_cmd echo Theobroma > 
"$CONFIGFS_HOME/usb_gadget/q7-usb-p2/strings/0x409/manufacturer"
run_cmd echo Tiger-Haikou > 
"$CONFIGFS_HOME/usb_gadget/q7-usb-p2/strings/0x409/product"

run_cmd mkdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/configs/conf.1/"
run_cmd mkdir 
"$CONFIGFS_HOME/usb_gadget/q7-usb-p2/configs/conf.1/strings/0x409"
run_cmd echo "USB ACM test" > 
"$CONFIGFS_HOME/usb_gadget/q7-usb-p2/configs/conf.1/strings/0x409/configuration"

run_cmd mkdir "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/functions/acm.1"

run_cmd ln -s "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/functions/acm.1" 
"$CONFIGFS_HOME/usb_gadget/q7-usb-p2/configs/conf.1"

run_cmd echo "$UDC" > "$CONFIGFS_HOME/usb_gadget/q7-usb-p2/UDC"

# Give some time for the ACM device to be probed by the kernel
sleep 2

ACM="/dev/ttyACM0"
GS="/dev/ttyGS$(cat 
"$CONFIGFS_HOME/usb_gadget/q7-usb-p2/functions/acm.1/port_num")"

run_cmd test_serial "$ACM" "$GS"
run_cmd test_serial "$GS" "$ACM"
echo "All good!"
"""

Once run, connect a USB stick to the port, it should be detected.

Cheers,
Quentin


More information about the U-Boot mailing list