#! /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
#
# Changes from Qualcomm Innovation Center are provided under the following license:
# Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause-Clear


if [ -e /sys/devices/soc0/machine ]; then
    soc_machine=`cat /sys/devices/soc0/machine`
else
    soc_machine="none"
fi

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 "start_pcie: setting usb composition to none for $soc_machine on PCIe mode" > /dev/kmsg
        echo -n "start_pcie: 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
        echo -n "start_pcie: 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
    echo -n "start_pcie: PCIe support enabled" > /dev/kmsg
    if [ -e /data/debug_transport.conf ]; then
        # Set the value from debug_transport.conf if the file is present
        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"
        if [ $soc_machine != "none" ]; then
            echo -n "start_pcie: $soc_machine" > /dev/kmsg
            if [ $soc_machine == "SDXLEMUR-SD" ]; then
                # SDXLEMUR IPQ fusion devices with platform subtype 11 and 17 need usb as debug transport
                if [ -e /sys/devices/soc0/platform_subtype_id ]; then
                    soc_subtypeid=`cat /sys/devices/soc0/platform_subtype_id`
                    if [ $soc_subtypeid == "11" -o $soc_subtypeid == "17" ]; then
                        echo -n "start_pcie: set USB as transport" > /dev/kmsg
                        default_debug_transport="usb"
                    fi
                fi
            fi
        fi

        printf "$default_debug_transport\0" > /data/debug_transport.conf
        set_debug_transport $default_debug_transport
    fi
fi
