Pasthis

Raw | New paste
Never expires.
#! /bin/sh
# SPDX-License-Identifier: MIT

set -eu

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

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/"
}

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

	if [ ! -c "$TX" ]; then
		echo "$TX isn't a character device, failing..."
		false
	fi

	if [ ! -c "$RX" ]; then
		echo "$RX isn't a character device, failing..."
		false
	fi

	(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"
}

UDC="ff300000.usb"
(timeout -s 9 10 udevadm monitor --subsystem-match=usb | grep -q "add.*$UDC" && echo "Found USB stick" || (echo "ERROR: Could not find USB stick" && false)) &
PID=$!

echo "Please insert a USB stick in Q7 USB P1 port (likely through a male micro-USB-A to female USB-A adapter) in the next 10 seconds..."

wait $PID

# Give some time for the kernel messages to be printed following the insertion of the USB stick
sleep 5

echo "Checking high-speed for Q7 USB P1..."
run_cmd grep -E "^480$" /sys/devices/platform/$UDC/*/*/speed

read -p "Please insert the micro-USB-A end of a male micro-USB-A to male USB-A cable into Q7 USB P1 port and the male USB-A end into Q7 USB P2 port and press any key to continue..." UNUSED

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 Ringneck-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"

run_cmd cleanup_usb_otg