#!/bin/sh
#
# OpenIPC.org | 2021.06.16
#
# Useage: ./load [ -r|-i|-a ]
#         -r : rmmod all modules
#         -i : insmod all modules
#    default : rmmod all moules and then insmod them

PATH_MODULE=/lib/modules/4.9.84/sigmastar

# Sensor
sensor=$(fw_printenv -n sensor)

# Watchdog
watchdog -t 10 /dev/watchdog


insert_ko()
{
    insmod ${PATH_MODULE}/mhal.ko
    insmod ${PATH_MODULE}/mi_common.ko
    insmod ${PATH_MODULE}/mi_sys.ko cmdQBufSize=256 logBufSize=256
    insmod ${PATH_MODULE}/mi_ai.ko
    insmod ${PATH_MODULE}/mi_ao.ko
    insmod ${PATH_MODULE}/mi_rgn.ko
    insmod ${PATH_MODULE}/mi_divp.ko thread_priority=98
    #insmod ${PATH_MODULE}/mi_ipu.ko
    insmod ${PATH_MODULE}/mi_vpe.ko thread_priority=98
    insmod ${PATH_MODULE}/mi_sensor.ko
    insmod ${PATH_MODULE}/mi_vif.ko thread_priority=98
    #
    case $sensor in
        gc2053)
            # Untested | The information is obtained from the Gospell firmware
            insmod ${PATH_MODULE}/mi_venc.ko max_width=1920 max_height=1080 max_jpe_task=1;
            ;;
        gc4653)
            # Untested | The information is obtained from the Anjoy firmware
            insmod ${PATH_MODULE}/mi_venc.ko max_width=2560 max_height=1440 thread_priority=99;
            ;;
        imx307)
            # Untested | The information is obtained from the Anjoy and Brovotech (no any params) firmware
            insmod ${PATH_MODULE}/mi_venc.ko max_width=2304 max_height=1296 thread_priority=99;
            ;;
        imx335)
            # Untested | The information is obtained from the Anjoy firmware
            insmod ${PATH_MODULE}/mi_venc.ko max_width=2592 max_height=1944 thread_priority=99;
            ;;
        sc2239)
            # Untested | The information is obtained from the IMOU firmware
            insmod ${PATH_MODULE}/mi_venc.ko thread_priority=99;
            ;;
        sc2335)
            # Untested | The information is obtained from the Tiandy firmware
            insmod ${PATH_MODULE}/mi_venc.ko;
            ;;
        sc3335)
            # Untested | The information is obtained from the Uniview firmware (no any params on other version)
            insmod ${PATH_MODULE}/mi_venc.ko max_width=2304 max_height=1296 mi_timeout_reset=1;
            ;;
        auto)
            insmod ${PATH_MODULE}/mi_venc.ko;
            ;;
        *)
            echo -e "\n\e[1;31mAn UNSUPPORTED sensor type is specified - ${sensor}\e[0m\n" | logger -s -t OpenIPC;
            ;;
    esac
    #
    insmod ${PATH_MODULE}/mi_shadow.ko
    #
    major=$(awk '$2=="mi_poll" {print $1}' /proc/devices)
    mknod /dev/mi_poll c $major 0
    #
    # Disable it now, untested
    #echo hvsp2 down /config/iqfile/filter.txt /config/iqfile/filter.txt > /sys/class/mstar/mscl/hvsp
    #
    # Change firmware folder
    # insmod ${PATH_MODULE}/mi_venc.ko fw_path="/config/venc_fw/chagall.bin"
    #
    # Change configs folder
    echo isproot /etc/sensors/iqfile > /dev/ispmid
    #
    case $sensor in
        gc2053)
            # Untested | The information is obtained from the Gospell firmware
            insmod ${PATH_MODULE}/sensor_gc2053_mipi.ko chmap=1 lane_num=2 hdr_lane_num=2;
            ;;
        gc4653)
            # Untested | The information is obtained from the Anjoy firmware
            insmod ${PATH_MODULE}/sensor_gc4653_mipi.ko chmap=1;
            ;;
        imx307)
            # Untested | The information is obtained from the Anjoy and Brovotech (mclk=37.125M) firmware
            insmod ${PATH_MODULE}/sensor_imx307_mipi.ko chmap=1 lane_num=2 hdr_lane_num=2 mclk=37.125M;
            ;;
        imx335)
            # Untested | The information is obtained from the Anjoy firmware
            insmod ${PATH_MODULE}/sensor_imx335_mipi.ko chmap=1;
            ;;
        sc2239)
            # Untested | The information is obtained from the IMOU firmware
            insmod ${PATH_MODULE}/sensor_sc2239_mipi.ko chmap=1;
            ;;
        sc2335)
            # Untested | The information is obtained from the Tiandy firmware
            insmod ${PATH_MODULE}/sensor_sc2335_mipi.ko chmap=1 lane_num=2 hdr_lane_num=2;
            ;;
        sc3335)
            # Untested | The information is obtained from the Uniview firmware
            insmod ${PATH_MODULE}/sensor_sc3335_mipi.ko chmap=1;
            ;;
        auto)
            insmod ${PATH_MODULE}/sensor_imx307_mipi.ko chmap=1 lane_num=2 hdr_lane_num=2 mclk=37.125M;
            insmod ${PATH_MODULE}/sensor_gc2053_mipi.ko chmap=1 lane_num=2 hdr_lane_num=2;
            insmod ${PATH_MODULE}/sensor_sc2335_mipi.ko chmap=1 lane_num=2 hdr_lane_num=2;
            ;;
        *)
            echo -e "\n\e[1;31mAn UNSUPPORTED sensor type is specified - ${sensor}\e[0m\n" | logger -s -t OpenIPC;
            ;;
    esac
    #
    mdev -s
}


remove_ko()
{
    rmmod -w drv_ms_cus_gc2053_MIPI
    rmmod -w drv_ms_cus_gc4653_MIPI
    rmmod -w drv_ms_cus_imx307_MIPI
    rmmod -w drv_ms_cus_imx335_MIPI
    rmmod -w drv_ms_cus_sc2239_MIPI
    rmmod -w drv_ms_cus_sc2335_MIPI
    rmmod -w drv_ms_cus_sc3335_MIPI
    rmmod -w mi_shadow
    rmmod -w mi_venc
    rmmod -w mi_vif
    rmmod -w mi_sensor
    rmmod -w mi_vpe
    rmmod -w mi_ipu
    rmmod -w mi_divp
    rmmod -w mi_rgn
    rmmod -w mi_ao
    rmmod -w mi_ai
    rmmod -w mi_sys
    rmmod -w mi_common
    rmmod -w mhal
}


if [ -z ${sensor} ]; then
    echo -e "\n\e[1;31mThe sensor parameter is MISSING\e[0m\n" | logger -s -t OpenIPC
    exit 1
elif [ ${sensor} = "none" ]; then
    echo -e "\n\e[1;33mThe sensor is assigned to NONE\e[0m\n" | logger -s -t OpenIPC
    exit 0
elif [ ${sensor} = "auto" ]; then
    echo -e "\n\e[1;33mThe sensor is assigned to AUTO\e[0m\n" | logger -s -t OpenIPC
else
    echo -e "\n\e[1;32mThe sensor is assigned - ${sensor}\e[0m\n" | logger -s -t OpenIPC
    # Temporary crutch, sorry
    [ ! -f /data/ut/chagall.bin ] && (mkdir -p /data/ut; ln -sf /etc/sensors/venc_fw/chagall.bin /data/ut/chagall.bin)
fi

if [ "$1" = "-i" ]; then
    insert_ko
fi

if [ "$1" = "-r" ]; then
    remove_ko
fi

if [ $# -eq 0 ] || [ "$1" = "-a" ]; then
    remove_ko
    insert_ko
fi

exit 0