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

# 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.

# Starts the USB Android Gadget.
export KERNEL=`uname -r`
export SOFTAP_W_DUN="N"
export ANDROID_LOG_TAGS="*:d"
export LS_COLORS="none"
LOCAL_ECHO=$(which echo)

case "$1" in
  init)
	# enable concurrent SoftAP and DUN
	if [ -f /etc/data/usb/softap_w_dun ]
	then
		SOFTAP_W_DUN=`cat /etc/data/usb/softap_w_dun`
	fi

	# bail out from script if qcom_gadget is enabled and
	# SoftAP with DUN is not enabled
	enabled=`cat /sys/bus/platform/devices/qcom_gadget/enabled`
	if [ "$enabled" == "Y" ]; then
		if [ "$SOFTAP_W_DUN" == "N" ]; then
			exit 0
		else
			${LOCAL_ECHO} "SOFTAP + DUN is enabled, disabling qcom_gadget" > /dev/kmsg
			${LOCAL_ECHO} 0 > /sys/bus/platform/devices/qcom_gadget/enabled
			if [ $(cat /proc/cmdline | grep -c "root=.*ubi\|mtd") -gt 0 ]; then
				usb_qti_partition=`cat /proc/mtd | grep "usb_qti" | cut -d ":" -f1`
				if [ "$usb_qti_partition" != "" ]; then
					flash_erase -q /dev/$usb_qti_partition 0 4
				fi
			elif [ $(cat /proc/cmdline | grep -c "root=.*mmc") -gt 0 ]; then
				if [ -e /dev/block/bootdevice/by-name/usb_qti ]; then
					dd if=/dev/zero of=/dev/block/bootdevice/by-name/usb_qti
				fi
			fi
		fi
	fi

	if [ -d /sys/class/android_usb/android0/f_ffs ]
	then
		${LOCAL_ECHO} adb > /sys/class/android_usb/android0/f_ffs/aliases
	fi

	# get soc platform to update product related information
	if [ -f /sys/devices/soc0/hw_platform ]; then
		socplatform=`cat /sys/devices/soc0/machine` 2> /dev/null
		soc_hwplatform=`cat /sys/devices/soc0/hw_platform`
		soc_subtypeid=`cat /sys/devices/soc0/platform_subtype_id`
	else
		socplatform=`cat /sys/devices/system/soc/soc0/machine` 2> /dev/null
	fi
	${LOCAL_ECHO} "soc:" $socplatform

	# find serialno from cmdline
	serialno=`cat /proc/cmdline | grep -o "androidboot.serialno=[A-Za-z0-9]*" | cut -d "=" -f2`
	if [ "$serialno" == "" ]; then
		${LOCAL_ECHO} "serialno not found with cmdline, use default"
		serialno="12345678"
	fi
	${LOCAL_ECHO} "serialno:" $serialno

	msm_serial=`cat /sys/devices/soc0/serial_number`
	msm_serial_hex=`printf %08X $msm_serial`
	product_string="$socplatform-$soc_hwplatform _SN:$msm_serial_hex"

	# try to mount configfs, and create instance of USB functions if it goes through
	mount -t configfs none /sys/kernel/config
	if [ -d /sys/kernel/config/usb_gadget ]; then
		${LOCAL_ECHO} "Configuring the USB gadget using ConfigFS..."
		cd /sys/kernel/config/usb_gadget
		mkdir g1
		cd g1
		mkdir strings/0x409
		mkdir configs/c.1
		mkdir configs/c.1/strings/0x409
		mkdir functions/mass_storage.0
		mkdir functions/mass_storage.1
		mkdir functions/ffs.adb
		mkdir functions/diag.diag
		mkdir functions/diag.diag_mdm
		mkdir functions/cser.dun.0
		mkdir functions/cser.nmea.1
		mkdir functions/gser.0
		mkdir functions/rmnet_bam.rmnet
		mkdir functions/rmnet_bam.rmnet_bam_demux
		mkdir functions/gsi.rmnet
		mkdir functions/gsi.rndis
		mkdir functions/gsi.ecm
		mkdir functions/gsi.mbim
		mkdir functions/gsi.rmnet.v2x
		mkdir functions/gsi.dpl
		mkdir functions/gsi.gps
		mkdir functions/ncm.0
		mkdir functions/qdss.qdss
		mkdir functions/qdss.qdss_mdm
		mkdir functions/uac1.uac1
		mkdir functions/uac2.0
		mkdir functions/uvc.0
		mkdir functions/rndis.rndis
		mkdir functions/ipc.ipc
		mkdir functions/ecm.ecm
		mkdir functions/hid.usb0
		mkdir functions/ccid.ccid
		mkdir functions/acm.0

		kernel_version=${KERNEL%%.*}
		if [ "$kernel_version" -ge "5" ]; then
			mkdir functions/ffs.diag
		fi

		if [ ! -s "/etc/adb_devid" ]
		then
			${LOCAL_ECHO} $serialno > /etc/adb_devid
			sync
		fi
		${LOCAL_ECHO} $serialno > strings/0x409/serialnumber
		${LOCAL_ECHO} "QCOM" > strings/0x409/manufacturer
		${LOCAL_ECHO} "$product_string" > strings/0x409/product
		if [ -e functions/diag.diag/serial ]; then
			${LOCAL_ECHO} "$serialno" > functions/diag.diag/serial
		fi

		# correct SELinux labels on new /dev nodes
		if [ -x /sbin/restorecon ]
		then
			/sbin/restorecon /dev/at_usb0 /dev/at_usb1
		fi

		cd /
	fi

	if [ -d /sys/class/android_usb/android0 ]
	then
		${LOCAL_ECHO} "$product_string" > /sys/class/android_usb/android0/iProduct
	fi

	# mount functionfs
	mkdir -p /dev/usb-ffs/adb
	mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb

	if [ -e /sys/kernel/config/usb_gadget/g1/functions/ffs.diag ]; then
		mkdir -p /dev/ffs-diag
		mount -o uid=system,gid=system,no_disconnect=1,rmode=0550,fmode=0660 -t functionfs diag /dev/ffs-diag
	fi

	mount -o remount,gid=5,mode=620 /dev/pts

	case `source /sbin/usb/target` in
		*9x15* )
			# Nothing to do here for 9x15
			;;
		*9x25* )
			${LOCAL_ECHO} "Unbind EHCI HSIC host device driver"
			${LOCAL_ECHO} msm_hsic_host > /sys/bus/platform/drivers/msm_hsic_host/unbind
			;;
		*9x35* )
			${LOCAL_ECHO} "Unbind EHCI HSIC host device driver"
			${LOCAL_ECHO} f9a15000.hsic_host > /sys/bus/platform/drivers/msm_hsic_host/unbind
			${LOCAL_ECHO} 1 > /sys/devices/virtual/android_usb/android0/f_rndis_qc/max_pkt_per_xfer
			${LOCAL_ECHO} 3 > /sys/module/dwc3/parameters/bulk_ep_xfer_timeout_ms
			;;
		*8916* )
			${LOCAL_ECHO} 9091 > /etc/usb/boot_hsusb_comp
                        ;;
		*9640* )
			${LOCAL_ECHO} 16384 > /sys/module/g_android/parameters/rndis_dl_max_xfer_size
			;;
		*9650* )
			${LOCAL_ECHO} 16384 > /sys/module/g_android/parameters/gsi_in_rndis_aggr_size
			if [ -e /dev/mhi_ctrl ]; then
				${LOCAL_ECHO} 901F > /etc/usb/boot_hsusb_comp
			fi
			;;
		*sdx20* )
			${LOCAL_ECHO} Y > /sys/module/libcomposite/parameters/enable_l1_for_hs
			${LOCAL_ECHO} Y > /sys/module/dwc3/parameters/enable_dwc3_u1u2
			if [ -e /dev/mhi_ctrl ]; then
				${LOCAL_ECHO} 901F > /etc/usb/boot_hsusb_comp
			fi
			;;
		*sxr2130* )
			${LOCAL_ECHO} 9060 > /etc/usb/boot_hsusb_comp
			;;
		*sdxpoorwills* )
			# Check if device is standalone ADP
			if [ "$soc_hwplatform" == "ADP" ] && [ "$soc_subtypeid" == "0" ]; then
				${LOCAL_ECHO} 90DB > /etc/usb/boot_hsusb_comp
			# Check if device is ADP with PCIe fusion
			elif [ "$soc_hwplatform" == "ADP" ] && [ "$soc_subtypeid" == "1" ]; then
				${LOCAL_ECHO} 9104 > /etc/usb/boot_hsusb_comp
			# Check if device is ADP with USB fusion
			elif [ "$soc_hwplatform" == "ADP" ] && [ "$soc_subtypeid" == "2" ]; then
				case `cat /etc/usb/boot_hsusb_comp` in
				    *9102* )
					# Dual RmNet selected by user, no change required
				    ;;
				    * )
					${LOCAL_ECHO} 9106 > /etc/usb/boot_hsusb_comp
				    ;;
				esac
			# Use this for MTP/TTP in PCIe mode
			elif [ -e /dev/mhi_ctrl ]; then
				${LOCAL_ECHO} 901F > /etc/usb/boot_hsusb_comp
			fi
			;;
		*sdxprairie* )
			# Check if device is standalone ADP
			if [ "$soc_hwplatform" == "ADP" ] && [ "$soc_subtypeid" == "0" ]; then
				${LOCAL_ECHO} 90DB > /etc/usb/boot_hsusb_comp
			# Check if device is ADP with PCIe fusion
			elif [ "$soc_hwplatform" == "ADP" ] && [ "$soc_subtypeid" == "1" ]; then
				${LOCAL_ECHO} 9104 > /etc/usb/boot_hsusb_comp
			# Check if device is ADP with USB fusion
			elif [ "$soc_hwplatform" == "ADP" ] && [ "$soc_subtypeid" == "2" ]; then
				${LOCAL_ECHO} 9106 > /etc/usb/boot_hsusb_comp
			# Check if device is ADP with ETH fusion
			elif [ "$soc_hwplatform" == "ADP" ] && [ "$soc_subtypeid" == "5" ]; then
				${LOCAL_ECHO} 9104 > /etc/usb/boot_hsusb_comp
			fi
			;;
		*8909* | *8053* | *8017* | *qcs605* | *qcs40x* | *sa2150p* | *qcs610* | *qcs6490* )
			${LOCAL_ECHO} 901D > /etc/usb/boot_hsusb_comp
		        ;;
		*sm8150* )
			${LOCAL_ECHO} 901D > /etc/usb/boot_hsusb_comp
			if [ -f /sys/bus/platform/devices/a600000.ssusb/mode ]; then
				default_mode=`cat /sys/bus/platform/devices/a600000.ssusb/mode`
				case "$default_mode" in
				    "none")
					${LOCAL_ECHO} peripheral > /sys/bus/platform/devices/a600000.ssusb/mode
				    ;;
				esac
			fi
		        ;;
		*scuba* )
			${LOCAL_ECHO} 90DB > /etc/usb/boot_hsusb_comp
		        ;;
		*cinder* )
			${LOCAL_ECHO} 1 > /sys/kernel/config/usb_gadget/g1/functions/acm.0/console
			;;
	esac

	#
	# Initialize UVC conifguration.
	#
	if [ -d /sys/kernel/config/usb_gadget/g1/functions/uvc.0 ]; then
		cd /sys/kernel/config/usb_gadget/g1/functions/uvc.0

		${LOCAL_ECHO} 3072 > streaming_maxpacket
		${LOCAL_ECHO} 10 > streaming_maxburst
		mkdir control/header/h
		ln -s control/header/h control/class/fs/
		ln -s control/header/h control/class/ss

		mkdir -p streaming/uncompressed/u/360p
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/uncompressed/u/360p/dwFrameInterval
		${LOCAL_ECHO} 333333 > streaming/uncompressed/u/360p/dwDefaultFrameInterval

		mkdir -p streaming/uncompressed/u/720p
		${LOCAL_ECHO} 1280 > streaming/uncompressed/u/720p/wWidth
		${LOCAL_ECHO} 720 > streaming/uncompressed/u/720p/wHeight
		${LOCAL_ECHO} 29491200 > streaming/uncompressed/u/720p/dwMinBitRate
		${LOCAL_ECHO} 29491200 > streaming/uncompressed/u/720p/dwMaxBitRate
		${LOCAL_ECHO} 1843200 > streaming/uncompressed/u/720p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/uncompressed/u/720p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/uncompressed/u/720p/dwFrameInterval

		mkdir -p streaming/uncompressed/u/1080p
		${LOCAL_ECHO} 1920 > streaming/uncompressed/u/1080p/wWidth
		${LOCAL_ECHO} 1080 > streaming/uncompressed/u/1080p/wHeight
		${LOCAL_ECHO} 66355200 > streaming/uncompressed/u/1080p/dwMinBitRate
		${LOCAL_ECHO} 995328000 > streaming/uncompressed/u/1080p/dwMaxBitRate
		${LOCAL_ECHO} 4147200 > streaming/uncompressed/u/1080p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/uncompressed/u/1080p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/uncompressed/u/1080p/dwFrameInterval

		mkdir -p streaming/uncompressed/u1/360p
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/uncompressed/u1/360p/dwFrameInterval
		${LOCAL_ECHO} 333333 > streaming/uncompressed/u1/360p/dwDefaultFrameInterval

		mkdir -p streaming/uncompressed/u1/720p
		${LOCAL_ECHO} 1280 > streaming/uncompressed/u1/720p/wWidth
		${LOCAL_ECHO} 720 > streaming/uncompressed/u1/720p/wHeight
		${LOCAL_ECHO} 29491200 > streaming/uncompressed/u1/720p/dwMinBitRate
		${LOCAL_ECHO} 29491200 > streaming/uncompressed/u1/720p/dwMaxBitRate
		${LOCAL_ECHO} 1843200 > streaming/uncompressed/u1/720p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/uncompressed/u1/720p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/uncompressed/u1/720p/dwFrameInterval

		mkdir -p streaming/uncompressed/u1/1080p
		${LOCAL_ECHO} 1920 > streaming/uncompressed/u1/1080p/wWidth
		${LOCAL_ECHO} 1080 > streaming/uncompressed/u1/1080p/wHeight
		${LOCAL_ECHO} 66355200 > streaming/uncompressed/u1/1080p/dwMinBitRate
		${LOCAL_ECHO} 995328000 > streaming/uncompressed/u1/1080p/dwMaxBitRate
		${LOCAL_ECHO} 4147200 > streaming/uncompressed/u1/1080p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/uncompressed/u1/1080p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/uncompressed/u1/1080p/dwFrameInterval

		mkdir -p streaming/mjpeg/m1/360p
		${LOCAL_ECHO} 640 > streaming/mjpeg/m1/360p/wWidth
		${LOCAL_ECHO} 360 > streaming/mjpeg/m1/360p/wHeight
		${LOCAL_ECHO} 460800   > streaming/mjpeg/m1/360p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 18432000  > streaming/mjpeg/m1/360p/dwMinBitRate
		${LOCAL_ECHO} 55296000 > streaming/mjpeg/m1/360p/dwMaxBitRate
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/mjpeg/m1/360p/dwFrameInterval
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m1/360p/dwDefaultFrameInterval

		mkdir -p streaming/mjpeg/m1/720p
		${LOCAL_ECHO} 1280 > streaming/mjpeg/m1/720p/wWidth
		${LOCAL_ECHO} 720 > streaming/mjpeg/m1/720p/wHeight
		${LOCAL_ECHO} 29491200 > streaming/mjpeg/m1/720p/dwMinBitRate
		${LOCAL_ECHO} 29491200 > streaming/mjpeg/m1/720p/dwMaxBitRate
		${LOCAL_ECHO} 1843200 > streaming/mjpeg/m1/720p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m1/720p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/mjpeg/m1/720p/dwFrameInterval

		mkdir -p streaming/mjpeg/m1/1080p
		${LOCAL_ECHO} 1920 > streaming/mjpeg/m1/1080p/wWidth
		${LOCAL_ECHO} 1080 > streaming/mjpeg/m1/1080p/wHeight
		${LOCAL_ECHO} 66355200 > streaming/mjpeg/m1/1080p/dwMinBitRate
		${LOCAL_ECHO} 995328000 > streaming/mjpeg/m1/1080p/dwMaxBitRate
		${LOCAL_ECHO} 4147200 > streaming/mjpeg/m1/1080p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m1/1080p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/mjpeg/m1/1080p/dwFrameInterval

		mkdir -p streaming/mjpeg/m1/1440p
		${LOCAL_ECHO} 2560 > streaming/mjpeg/m1/1440p/wWidth
		${LOCAL_ECHO} 1440 > streaming/mjpeg/m1/1440p/wHeight
		${LOCAL_ECHO} 117964800 > streaming/mjpeg/m1/1440p/dwMinBitRate
		${LOCAL_ECHO} 1769472000 > streaming/mjpeg/m1/1440p/dwMaxBitRate
		${LOCAL_ECHO} 7372800 > streaming/mjpeg/m1/1440p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m1/1440p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n" > streaming/mjpeg/m1/1440p/dwFrameInterval

		mkdir -p streaming/mjpeg/m1/2160p
		${LOCAL_ECHO} 3840 > streaming/mjpeg/m1/2160p/wWidth
		${LOCAL_ECHO} 2160 > streaming/mjpeg/m1/2160p/wHeight
		${LOCAL_ECHO} 265420800 > streaming/mjpeg/m1/2160p/dwMinBitRate
		${LOCAL_ECHO} 3981312000 > streaming/mjpeg/m1/2160p/dwMaxBitRate
		${LOCAL_ECHO} 16588800 > streaming/mjpeg/m1/2160p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m1/2160p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/mjpeg/m1/2160p/dwFrameInterval

		mkdir -p streaming/mjpeg/m/360p
		${LOCAL_ECHO} 640 > streaming/mjpeg/m/360p/wWidth
		${LOCAL_ECHO} 360 > streaming/mjpeg/m/360p/wHeight
		${LOCAL_ECHO} 460800   > streaming/mjpeg/m/360p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 18432000  > streaming/mjpeg/m/360p/dwMinBitRate
		${LOCAL_ECHO} 55296000 > streaming/mjpeg/m/360p/dwMaxBitRate
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/mjpeg/m/360p/dwFrameInterval
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m/360p/dwDefaultFrameInterval

		mkdir -p streaming/mjpeg/m/720p
		${LOCAL_ECHO} 1280 > streaming/mjpeg/m/720p/wWidth
		${LOCAL_ECHO} 720 > streaming/mjpeg/m/720p/wHeight
		${LOCAL_ECHO} 29491200 > streaming/mjpeg/m/720p/dwMinBitRate
		${LOCAL_ECHO} 29491200 > streaming/mjpeg/m/720p/dwMaxBitRate
		${LOCAL_ECHO} 1843200 > streaming/mjpeg/m/720p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m/720p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/mjpeg/m/720p/dwFrameInterval

		mkdir -p streaming/mjpeg/m/1080p
		${LOCAL_ECHO} 1920 > streaming/mjpeg/m/1080p/wWidth
		${LOCAL_ECHO} 1080 > streaming/mjpeg/m/1080p/wHeight
		${LOCAL_ECHO} 66355200 > streaming/mjpeg/m/1080p/dwMinBitRate
		${LOCAL_ECHO} 995328000 > streaming/mjpeg/m/1080p/dwMaxBitRate
		${LOCAL_ECHO} 4147200 > streaming/mjpeg/m/1080p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m/1080p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/mjpeg/m/1080p/dwFrameInterval

		mkdir -p streaming/mjpeg/m/1440p
		${LOCAL_ECHO} 2560 > streaming/mjpeg/m/1440p/wWidth
		${LOCAL_ECHO} 1440 > streaming/mjpeg/m/1440p/wHeight
		${LOCAL_ECHO} 117964800 > streaming/mjpeg/m/1440p/dwMinBitRate
		${LOCAL_ECHO} 1769472000 > streaming/mjpeg/m/1440p/dwMaxBitRate
		${LOCAL_ECHO} 7372800 > streaming/mjpeg/m/1440p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m/1440p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n" > streaming/mjpeg/m/1440p/dwFrameInterval

		mkdir -p streaming/mjpeg/m/2160p
		${LOCAL_ECHO} 3840 > streaming/mjpeg/m/2160p/wWidth
		${LOCAL_ECHO} 2160 > streaming/mjpeg/m/2160p/wHeight
		${LOCAL_ECHO} 265420800 > streaming/mjpeg/m/2160p/dwMinBitRate
		${LOCAL_ECHO} 3981312000 > streaming/mjpeg/m/2160p/dwMaxBitRate
		${LOCAL_ECHO} 16588800 > streaming/mjpeg/m/2160p/dwMaxVideoFrameBufferSize
		${LOCAL_ECHO} 333333 > streaming/mjpeg/m/2160p/dwDefaultFrameInterval
		${LOCAL_ECHO} -e "333333\n666666\n1000000\n5000000\n" > streaming/mjpeg/m/2160p/dwFrameInterval

		${LOCAL_ECHO} 0x04 > /sys/kernel/config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/bmaControls
		${LOCAL_ECHO} 0x04 > /sys/kernel/config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m1/bmaControls

		mkdir -p streaming/h264/h/960p
		${LOCAL_ECHO} 1920 > streaming/h264/h/960p/wWidth
		${LOCAL_ECHO} 960 > streaming/h264/h/960p/wHeight
		${LOCAL_ECHO} 40 > streaming/h264/h/960p/bLevelIDC
		${LOCAL_ECHO} -e "333667\n" > streaming/h264/h/960p/dwFrameInterval

		mkdir -p streaming/h264/h/1920p
		${LOCAL_ECHO} -e "333667\n" > streaming/h264/h/1920p/dwFrameInterval

		mkdir streaming/header/h
		mkdir streaming/header/h1
		ln -s streaming/uncompressed/u1 streaming/header/h1
		ln -s streaming/mjpeg/m1 streaming/header/h1

		ln -s streaming/uncompressed/u streaming/header/h
		ln -s streaming/mjpeg/m streaming/header/h
		ln -s streaming/h264/h streaming/header/h
		ln -s streaming/header/h1 streaming/class/fs/
		ln -s streaming/header/h1 streaming/class/hs/
		ln -s streaming/header/h streaming/class/ss/
		cd /
	fi

	#Initialize NCM configuration
	if [ -d /sys/kernel/config/usb_gadget/g1/functions/ncm.0 ]; then
		cd /sys/kernel/config/usb_gadget/g1/functions/ncm.0
		${LOCAL_ECHO} WINNCM > os_desc/interface.ncm/compatible_id
	fi

	pid=`cat /etc/usb/boot_hsusb_comp`
	if [ -e /data/debug_transport.conf ]; then
		debug_transport=`cat /data/debug_transport.conf`
	else
		debug_transport="none"
	fi

	# If device is not in MHI/PCIE mode & USB composition is none,
	# then set the default composition from here
	if [ "$pid" == "none" ] && [ "$debug_transport" != "pcie" ]; then
		case `source /sbin/usb/target` in
			*cinder* )
				${LOCAL_ECHO} 9060 > /etc/usb/boot_hsusb_comp
				;;
			* )
				${LOCAL_ECHO} 90DB > /etc/usb/boot_hsusb_comp
				;;
		esac
	fi

	# put USB in LPM and exit with success if device is in mhi mode
	if [ "$pid" == "none" ] && [ "$debug_transport" == "pcie" ]; then
		udcname=`ls -1 /sys/bus/platform/devices/ | grep -E 'ssusb|hsusb' | head -n 1`
		${LOCAL_ECHO} none > /sys/bus/platform/devices/${udcname}/mode
		exit 0
	fi

	# enable debug message
	if [ -f /sbin/usb/debuger/default_debug ];
	then
		rm -rf /sbin/usb/debuger/statusFile
		usb_debug -f on n /sbin/usb/debuger/default_debug
	fi

	# boot hsic composition:
	if [ -d /sys/class/android_usb/android1 ]
	then
		pid=`cat /etc/usb/boot_hsic_comp`
		# make 901D as default composition
                if [ -z "$pid" ]
		then
                    pid="901D"
		fi

		/sbin/usb/compositions/$pid y
		# let hsic compostion script run before starting hsusb
		sleep 1
	fi

	# boot hsusb composition:
	if [ -d /sys/class/android_usb/android0 ]
	then
		pid=`cat /etc/usb/boot_hsusb_comp`
		# exit with success if device is in mhi mode
		if [ "$pid" == "none" ]; then
			exit 0
		fi
		# make 901D as default composition
                if [ -z "$pid" ]
		then
                    pid="901D"
		fi

		# check if more than 1 argument is passed
		if [ $# -gt 1 ]; then
			# if second argument is n, do nothing
			if [ $2 != 'n' ]; then
				/sbin/usb/compositions/$pid n $adbd_params
			fi
		else
			/sbin/usb/compositions/$pid n $adbd_params
		fi
	fi
	;;

  start)
	udcname=`ls -1 /sys/class/udc | head -n 1`
	if [ -d /sys/kernel/config/usb_gadget ]; then
		${LOCAL_ECHO} "Starting USB Android Gadget"
		rm -f /tmp/usb_bind_in_progress
		${LOCAL_ECHO} $udcname > /sys/kernel/config/usb_gadget/g1/UDC
	fi
	;;

  stop)
	if [ -d /sys/kernel/config/usb_gadget ]; then
		${LOCAL_ECHO} "Stopping USB Android Gadget"
		${LOCAL_ECHO} start > /tmp/usb_bind_in_progress
		${LOCAL_ECHO} none > /sys/kernel/config/usb_gadget/g1/UDC
	fi
        ;;

  restart)
	${LOCAL_ECHO} "Restarting USB Android Gadget"
	$0 stop
	$0 start
        ;;
  *)
        ${LOCAL_ECHO} "Usage usb { start | stop | restart}" >&2
        exit 1
        ;;
esac

