#!/bin/bash

# FunctionFS uMTPrd startup example/test script
# Must be launched from a writable/temporary folder.
CFG_PATH=/sys/kernel/config

if [[ -z "$1" ]]; then  
  echo "$0 脚本的第一个参数不可为空"  
  echo "第一个参数可选择为：[start, stop]"
  echo "例如启动mtp功能，执行：$0 start"
  echo "    关闭mtp功能，执行：$0 stop"
  exit 1
fi

if [ "$1" = "stop" ]; then 
  UMTPRD_PRO=`ps -elf | grep /usr/bin/umtprd | grep -v grep`
  if [[ -n "${UMTPRD_PRO}" ]]; then
     killall umtprd
  fi 
  echo "关闭mtp功能"  
  exit 1
fi

if [ "$1" != "start" ]; then 
  echo "第一个参数可选择为：[start, stop]"
  echo "例如启动mtp功能，执行：$0 start"
  echo "    关闭mtp功能，执行：$0 stop"  
  exit 1
fi

MOUNT_FLAG=`mount | grep "/sys/kernel/config"`
if [[ -z "${MOUNT_FLAG}" ]]; then
  mount none /sys/kernel/config -t configfs
  sleep 1  
fi

if [ ! -e "${CFG_PATH}/usb_gadget/rockchip" ]; then 
  mkdir ${CFG_PATH}/usb_gadget/rockchip
fi

KYLIN_PATH=${CFG_PATH}/usb_gadget/rockchip

if [ ! -e "${KYLIN_PATH}/configs/b.1" ]; then
   mkdir ${KYLIN_PATH}/configs/b.1
fi

if [ ! -e "${KYLIN_PATH}/functions/ffs.mtp" ]; then
   mkdir ${KYLIN_PATH}/functions/ffs.mtp
fi
# Uncomment / Change the follow line to enable another usb gadget function
#mkdir functions/acm.usb0

if [ ! -e "${KYLIN_PATH}/strings/0x409" ]; then
   mkdir ${KYLIN_PATH}/strings/0x409
fi

if [ ! -e "${KYLIN_PATH}/configs/b.1/strings/0x409" ]; then
   mkdir ${KYLIN_PATH}/configs/b.1/strings/0x409
fi 

UDC_Content=$(cat ${KYLIN_PATH}/UDC)
if [[ ! -z "${UDC_Content}" ]]; then
	echo "" > ${KYLIN_PATH}/UDC
fi

if [ -e "/sys/kernel/config/usb_gadget/rockchip/functions/ffs.mtp" ]; then
        rm -f ${KYLIN_PATH}/configs/b.1/f1
fi

killall umtprd 2>/dev/null
umount /dev/usb-ffs/mtp 2>/dev/null

if [ ! -e "/sys/kernel/config/usb_gadget/rockchip/functions/ffs.mtp" ]; then
        mkdir ${KYLIN_PATH}/functions/ffs.mtp
fi

echo 0x0100 > ${KYLIN_PATH}/idProduct
echo 0x1D6B > ${KYLIN_PATH}/idVendor

SERIAL=`cat /proc/cpuinfo | grep Serial | awk '{print $3}'`
echo "${SERIAL:-01234567}" > ${KYLIN_PATH}/strings/0x409/serialnumber
echo "Kylin Technologies" > ${KYLIN_PATH}/strings/0x409/manufacturer
echo "Kylin Product" > ${KYLIN_PATH}/strings/0x409/product

echo "Conf 1" > ${KYLIN_PATH}/configs/b.1/strings/0x409/configuration
echo 500 > ${KYLIN_PATH}/configs/b.1/MaxPower

if [ ! -e "${KYLIN_PATH}/configs/b.1/ffs.mtp" ]; then
   ln -s ${KYLIN_PATH}/functions/ffs.mtp ${KYLIN_PATH}/configs/b.1/
fi
# Uncomment / Change the follow line to enable another usb gadget function
#ln -s functions/acm.usb0 configs/b.1

if [ ! -e "/dev/usb-ffs/mtp" ]; then
   mkdir -p /dev/usb-ffs/mtp
fi

MOUNT_FLAG=`mount | grep "/dev/usb-ffs/mtp"`
if [[ -z "${MOUNT_FLAG}" ]]; then
  mount -t functionfs mtp /dev/usb-ffs/mtp
fi

# Start the umtprd service
UMTPRD_PRO=`ps -elf | grep /usr/bin/umtprd | grep -v grep`
if [[ -z "${UMTPRD_PRO}" ]]; then
   /usr/bin/umtprd >/dev/null 2>&1 &
fi

sleep 1

# enable the usb functions
UDC_Content=$(cat ${KYLIN_PATH}/UDC)  
if [[ -z "${UDC_Content}" ]]; then
   ls /sys/class/udc/ > ${KYLIN_PATH}/UDC
fi 
