#!/bin/sh


if [ "$1" = "start" ]
then
	. /etc/profile
	if [ -e /boot/usb.host ]
	then
		echo "usb mode: host"
		echo host > /proc/cviusb/otg_role
	elif [ -e /boot/usb.dev ]
	then
		echo "usb mode: device"
		cd /sys/kernel/config/usb_gadget
		mkdir -p g0
		cd g0
		if [ -e /boot/usb.idVendor ]
		then
			cat /boot/usb.idVendor > idVendor
		else
			echo 0x359F > idVendor
		fi
		if [ -e /boot/usb.idProduct ]
		then
			cat /boot/usb.idProduct > idProduct
		else
			echo 0x2120 > idProduct
		fi
		if [ -e /boot/usb.bcdDevice ]
		then
			cat /boot/usb.bcdDevice > bcdDevice
		else
			echo 0x0300 > bcdDevice
		fi
		if [ -e /boot/usb.bcdUSB ]
		then
			cat /boot/usb.bcdUSB > bcdUSB
		else
			echo 0x0200 > bcdUSB
		fi
		if [ -e /boot/usb.bDeviceClass ]
		then
			cat /boot/usb.bDeviceClass > bDeviceClass
		else
			echo 0xef > bDeviceClass
		fi
		if [ -e /boot/usb.bDeviceSubClass ]
		then
			cat /boot/usb.bDeviceSubClass > bDeviceSubClass
		else
			echo 0x02 > bDeviceSubClass
		fi
		if [ -e /boot/usb.bDeviceProtocol ]
		then
			cat /boot/usb.bDeviceProtocol > bDeviceProtocol
		else
			echo 0x01 > bDeviceProtocol
		fi
		mkdir -p strings/0x409
		if [ -e /boot/usb.serialnumber ]
		then
			cat /boot/usb.serialnumber > strings/0x409/serialnumber
		else
			cat /device_key > strings/0x409/serialnumber
		fi
		if [ -e /boot/usb.manufacturer ]
		then
			cat /boot/usb.manufacturer > strings/0x409/manufacturer
		else
			echo 'sipeed' > strings/0x409/manufacturer
		fi
		if [ -e /boot/usb.product ]
		then
			cat /boot/usb.product > strings/0x409/product
		else
			echo 'licheervnano' > strings/0x409/product
		fi
		mkdir -p configs/c.1
		# uvc
		if [ -e /boot/usb.uvc ]
		then
			/etc/init.d/uvc_tool.sh mount /boot/usb.uvc
			/etc/init.d/uvc_tool.sh server
		fi

		if [ -e /boot/usb.GS0 ]
		then
			mkdir -p functions/acm.GS0
			rm -rf configs/c.1/acm.GS0
			ln -s functions/acm.GS0 configs/c.1/
		fi

		if [ -e /boot/usb.disk0 ]
		then
			mkdir -p functions/mass_storage.disk0
			rm -rf configs/c.1/mass_storage.disk0
			ln -s functions/mass_storage.disk0 configs/c.1/
			echo 1 > functions/mass_storage.disk0/lun.0/removable
			if [ -e /boot/usb.disk0.ro ]
			then
				echo 1 > functions/mass_storage.disk0/lun.0/ro
				echo 1 > functions/mass_storage.disk0/lun.0/cdrom
			fi
			disk=$(cat /boot/usb.disk0)
			if [ -z "${disk}" ]
			then
				if [ ! -e /mnt/usbdisk.img ]
				then
					dd if=/dev/zero of=/mnt/usbdisk.img bs=1M count=16
					mkfs.vfat /mnt/usbdisk.img
				fi
				echo /mnt/usbdisk.img > functions/mass_storage.disk0/lun.0/file
			else
				cat /boot/usb.disk0 > functions/mass_storage.disk0/lun.0/file
			fi
		fi

		# rndis
		if [ -e /boot/usb.rndis ]
		then
			mkdir -p functions/rndis.usb0
			ln -s functions/rndis.usb0 configs/c.1/
			echo e0 > functions/rndis.usb0/class
			echo 01 > functions/rndis.usb0/subclass
			echo 03 > functions/rndis.usb0/protocol
		fi

		# ncm
		if [ -e /boot/usb.ncm ]
		then
			mkdir -p functions/ncm.usb0
			ln -s functions/ncm.usb0 configs/c.1/
		fi

		# keyboard
		if [ -e /boot/usb.keyboard ]
		then
			mkdir functions/hid.GS0
			# echo 1 > functions/hid.GS0/subclass
			echo 1 > functions/hid.GS0/wakeup_on_write
			echo 1 > functions/hid.GS0/protocol
			echo 6 > functions/hid.GS0/report_length
			echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.GS0/report_desc
			ln -s functions/hid.GS0 configs/c.1
		fi

		# mouse
		if [ -e /boot/usb.mouse ]
		then
			mkdir functions/hid.GS1
			# echo 1 > functions/hid.GS1/subclass
			echo 1 > functions/hid.GS1/wakeup_on_write
			echo 2 > functions/hid.GS1/protocol
			echo 4 > functions/hid.GS1/report_length
			echo -ne \\x5\\x1\\x9\\x2\\xa1\\x1\\x9\\x1\\xa1\\x0\\x5\\x9\\x19\\x1\\x29\\x3\\x15\\x0\\x25\\x1\\x95\\x3\\x75\\x1\\x81\\x2\\x95\\x1\\x75\\x5\\x81\\x3\\x5\\x1\\x9\\x30\\x9\\x31\\x9\\x38\\x15\\x81\\x25\\x7f\\x75\\x8\\x95\\x3\\x81\\x6\\xc0\\xc0 > functions/hid.GS1/report_desc        
			ln -s functions/hid.GS1 configs/c.1
		fi

		# touchpad
		if [ -e /boot/usb.touchpad ]
		then
			mkdir functions/hid.GS2
			# echo 1 > functions/hid.GS2/subclass
			echo 1 > functions/hid.GS2/wakeup_on_write
			echo 2 > functions/hid.GS2/protocol
			echo 6 > functions/hid.GS2/report_length
			echo -ne \\x05\\x01\\x09\\x02\\xa1\\x01\\x09\\x01\\xa1\\x00\\x05\\x09\\x19\\x01\\x29\\x03\\x15\\x00\\x25\\x01\\x95\\x03\\x75\\x01\\x81\\x02\\x95\\x01\\x75\\x05\\x81\\x01\\x05\\x01\\x09\\x30\\x09\\x31\\x15\\x00\\x26\\xff\\x7f\\x35\\x00\\x46\\xff\\x7f\\x75\\x10\\x95\\x02\\x81\\x02\\x05\\x01\\x09\\x38\\x15\\x81\\x25\\x7f\\x35\\x00\\x45\\x00\\x75\\x08\\x95\\x01\\x81\\x06\\xc0\\xc0 > functions/hid.GS2/report_desc
			ln -s functions/hid.GS2 configs/c.1
		fi

		ls /sys/class/udc/ | cat > UDC
		echo device > /proc/cviusb/otg_role
	fi
fi

if [ "$1" = "stop" ]
then
	echo '' > /sys/kernel/config/usb_gadget/g0/UDC
	echo host > /proc/cviusb/otg_role
	# uvc
	if [ -e /sys/kernel/config/usb_gadget/g0/configs/c.1/uvc.usb0 ]
	then
		/etc/init.d/uvc_tool.sh unmount
	fi

	# rndis
	if [ -e /sys/kernel/config/usb_gadget/g0/configs/c.1/rndis.usb0 ]
	then
		unlink /sys/kernel/config/usb_gadget/g0/configs/c.1/rndis.usb0
		rmdir /sys/kernel/config/usb_gadget/g0/functions/rndis.usb0
	fi

	# ncm
	if [ -e /sys/kernel/config/usb_gadget/g0/configs/c.1/ncm.usb0 ]
	then
		unlink /sys/kernel/config/usb_gadget/g0/configs/c.1/ncm.usb0
		rmdir /sys/kernel/config/usb_gadget/g0/functions/ncm.usb0
	fi
fi

