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

set_debug_transport()
{
    if [ "$1" == "pcie" ]; then
        # set adb to pcie mode
        printf "pcie=true\0" > /data/adb.conf
        #set usb composition to none
        echo none > /etc/usb/boot_hsusb_comp
        echo -n "Debug transport mode is set to pcie. Update /data/debug_transport.conf to usb and reboot to switch" > /dev/kmsg
    else
        # set adb tansport to usb mode
        printf "usb\0" > /data/adb.conf
        #set usb composition to 901F
        echo 901F > /etc/usb/boot_hsusb_comp
        echo -n "Debug transport mode is set to usb. Update /data/debug_transport.conf to pcie and reboot to switch" > /dev/kmsg
    fi
}

# Check for PCIe support
if [ -e /dev/mhi_ctrl ]; then
    if [ -e /data/debug_transport.conf ]; then
        # Set the value from debug_transport.conf if the file is present
        # Don't set debug port. xingduo.du 2024-06-17
        # set_debug_transport `cat /data/debug_transport.conf`
    else
        # Create debug_transport.conf file in the first boot with default debug option
        default_debug_transport="pcie"

        # SDXLEMUR IPQ fusion devices with platform subtype 11 need usb as debug transport
        if [ -e /sys/devices/soc0/platform_subtype_id ] && [ -e /sys/devices/soc0/machine ]; then
            soc_subtypeid=`cat /sys/devices/soc0/platform_subtype_id`
            soc_machine=`cat /sys/devices/soc0/machine`
            if [ $soc_machine == "SDXLEMUR-SD" -a $soc_subtypeid == "11" ]; then
                default_debug_transport="usb"
            fi
        fi

        printf "$default_debug_transport\0" > /data/debug_transport.conf
        # Don't set debug port. xingduo.du 2024-06-17
        # set_debug_transport $default_debug_transport
    fi
else
    if [ -e /data/debug_transport.conf ]; then
        transport=`cat /data/debug_transport.conf`
        if [ $transport != "usb" ]; then
            printf "usb\0" > /data/debug_transport.conf
            # Don't set debug port. xingduo.du 2024-06-17
            # set_debug_transport "usb"
        fi
    fi
fi
