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

# DESCRIPTION: ACM

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

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

run_configfs() {
	echo start > /tmp/usb_bind_in_progress

	if [ $from_adb = "n" ]
	then
		pkill adbd
	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 0xF005 > idProduct

	echo "ACM" > configs/c.1/strings/0x409/configuration
	ln -s functions/acm.0 configs/c.1/f1

	# Cinder is self-powered, hence set the self-powered bit of bmAttributes
	echo 0xC0 > configs/c.1/bmAttributes
	rm -f /tmp/usb_bind_in_progress
	echo "binding UDC with Gadget..." $1
	echo $1 > UDC
	cd /
}


case `source /sbin/usb/target` in
	*cinder* )
		udcname=`ls -1 /sys/class/udc | head -n 1`
		run_configfs $udcname &
		;;
	* )
		echo "Failed: Target doesn't support 0xF005 composition"
		;;
esac
