#!/bin/sh
#
# Copyright (c) 2012, 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.

# Starts the USB Android Gadget.

export KERNEL=`uname -r`

configfs_comp=/sys/kernel/config/usb_gadget/g1/configs/c.1/strings/0x409/configuration
android_comp=/sys/class/android_usb/android0/functions

case "$1" in
  start)

        case $KERNEL in
          [34].*)
            # boot hsusb composition:
            /usr/bin/usb/boot_hsusb_composition n

            # FFS/ADB: Try configfs gadget first, then android gadget
            if [ -r $configfs_comp ]; then
                compstr=$configfs_comp
            else
                compstr=$android_comp
            fi
            compstr=`grep -w ffs $compstr || true`
            if [ ! -z "$compstr" ] ; then
                mkdir -p /dev/usb-ffs/adb
                mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb
                mount -o remount,gid=5,mode=620 /dev/pts
                mkdir -p /system/bin 2>/dev/null && ln -s /bin/sh /system/bin/sh
            fi
            ;;
          2.*)
                # Nothing to do for 2.x kernels
                ;;
          *)
                # Some other version of kernel?
                ;;
        esac
        ;;

  stop)
        echo "Stopping USB Android Gadget"
        adbd_running=`pgrep adbd || true`
        if [ ! -z $adbd_running ]
        then
            start-stop-daemon -K -n adbd
        fi

        # If using configfs gadget
        if [ -w /sys/kernel/config/usb_gadget/g1/UDC ]
        then
            echo "" > /sys/kernel/config/usb_gadget/g1/UDC
        fi
        # If using android gadget
        if [ -w /sys/class/android_usb/android0/enable ]
        then
            echo 0 >/sys/class/android_usb/android0/enable
        fi

        functionfs_mounted=`mount | grep "\/dev\/usb\-ffs\/adb" || true`
        if [ ! -z $functionfs_mounted ]
        then
            umount /dev/usb-ffs/adb
        fi
        ;;

  restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "Usage usb { start | stop | restart}" >&2
        exit 1
        ;;
esac

