#!/bin/sh
#
# Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause-Clear

# DESCRIPTION: DIAG + IPC_ROUTER + RmNet + RmNet + ECM + QDSS (Trace Only) + ADPL + ADB

echo "Switching to composition number 0x9102"  > /dev/kmsg

if [ "$#" -ge 3 ]; then
	from_adb=$3
else
	from_adb="n"
fi

run_configfs() {
	echo start > /data/usb_bind_in_progress
	if [ $from_adb = "n" ]
	then
		pkill adbd
		/etc/launch_adbd start
		sleep 1
	fi
	cd /sys/kernel/config/usb_gadget/g1
	rm os_desc/c* 2> /dev/null
	rm configs/c*/f* 2> /dev/null
	rmdir configs/c.2/strings/* configs/c.3/strings/* 2> /dev/null
	rmdir configs/c.2 configs/c.3 2> /dev/null
	echo 0x05c6 > idVendor
	echo 0x9102 > idProduct
	echo "DIAG_IPC_RMNET_RMNET_ECM_QDSS_DPL_ADB" > configs/c.1/strings/0x409/configuration
	if [ -e functions/ffs.diag ]; then
		ln -s functions/ffs.diag configs/c.1/f1
	else
		ln -s functions/diag.diag configs/c.1/f1
	fi
	ln -s functions/ipc.ipc configs/c.1/f2
	ln -s functions/gsi.rmnet configs/c.1/f3
	ln -s functions/gsi.rmnet.v2x configs/c.1/f4
	ln -s functions/ecm.ecm configs/c.1/f5
	ln -s functions/qdss.qdss configs/c.1/f6
	ln -s functions/gsi.dpl configs/c.1/f7
	ln -s functions/ffs.adb configs/c.1/f8
	echo 0xA0 > configs/c.1/bmAttributes
	rm -f /data/usb_bind_in_progress
	echo "binding UDC with Gadget..." $1
	echo $1 > UDC
	cd /
}

case `source /sbin/usb/target` in
	*sdxprairie* )
		udcname=`ls -1 /sys/class/udc | head -n 1`
		run_configfs $udcname &
		;;
        * )
		echo "This composition is currently not supported for this target "
		;;
esac
