# Copyright (c) 2014, 2017-2018, 2020-2021, The Linux Foundation. All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#    * Neither the name of The Linux Foundation nor the names of its
#      contributors may be used to endorse or promote products derived from
#      this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE DISCLAIMED.  IN NO
# EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Changes from Qualcomm Innovation Center are provided under the following license:

# Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted (subject to the limitations in the
# disclaimer below) provided that the following conditions are met:

#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.

#    * Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials provided
#      with the distribution.

#    * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
#      contributors may be used to endorse or promote products derived
#      from this software without specific prior written permission.

# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
# GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
# HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# DESCRIPTION: ADB

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

if [ "$1" = "y" ]; then
	num="1"
else
	num="0"
fi

if [ "$#" -ge 2 ]; then
	delay=$2
else
	delay="0"
fi

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

if [ -f /sys/class/android_usb/android$num/enable ]; then
	configfs_supported="n"
else
	configfs_supported="y"
fi

run_non_configfs() {
	if [ $from_adb = "n" ]
	then
		pkill adbd
	fi
	echo 0 > /sys/class/android_usb/android$num/enable
	echo 18D1 > /sys/class/android_usb/android$num/idProduct
	echo 4EE7 > /sys/class/android_usb/android$num/idVendor
	echo adb > /sys/class/android_usb/android$num/functions
	echo 1 > /sys/class/android_usb/android$num/remote_wakeup
	sleep $delay
	echo 1 > /sys/class/android_usb/android$num/enable
	if [ $from_adb = "n" ]
	then
		/etc/launch_adbd start
	fi
}

run_configfs() {
	echo start > /data/usb_bind_in_progress
	if [ $from_adb = "n" ]
	then
		pkill adbd
		# Give enough time to restart ADBD and open EP0 file node
		sleep 1
	fi
	cd /sys/kernel/config/usb_gadget/g1
	rm os_desc/c* 2> /dev/null
	rm configs/c*/f* 2> /dev/null
	rm -rf configs/c.2 configs/c.3 2> /dev/null
	echo 0x18D1 > idVendor
	echo 0x4EE7 > idProduct
	echo "ADB" > configs/c.1/strings/0x409/configuration
	ln -s functions/ffs.adb configs/c.1/f1
	rm -f /data/usb_bind_in_progress
	echo $bmattributes > configs/c.1/bmAttributes
	echo "binding UDC with Gadget..." $1
	echo $1 > UDC
	cd /
}

target=`source /sbin/usb/target`
case $target in
	* )
		if [ $configfs_supported = "y" ]; then
			udcname=`ls -1 /sys/class/udc | head -n 1`

			# Cinder is self-powered, hence set the self-powered bit of bmAttributes
			if [ $target == "cinder" ]; then
				bmattributes="0xC0"
			else
				bmattributes="0x80"
			fi

			run_configfs $udcname &
		else
			run_non_configfs &
		fi
		;;
esac
