#!/bin/sh
# HF USB reset port functionality. Only one instance of check or reset can run at a time,
# as this script may be triggered multiple times at once by udev.
# 2 is top port.
# 1 is bottom port.
#
# Reset a USB port on hatchetfish.
# Usage: hf-usb-monitor reset port
#
# Check kernel messages for a USB hub port failure, and calls reset if a failure is found.
# Usage: hf-usb-monitor check port
set -u

source /lib/functions/navico_env.sh
source /etc/rc.d/rc.common

navico_env_init

kernel_time_fromline() {
    echo "$1" | sed -n 's/\[[ ]*\([0-9]*\)\..*\].*/\1/p'
}


kernel_time_current() {
    cat /proc/uptime | sed 's/\..*//'
}


hf_usb_cycle_port() {
    local cmd
    local device
    local usb_driver_path="/sys/bus/platform/drivers/imx_usb"

    case "$1" in
    1) # Bottom
        cmd="t 36 1 0"
        device="2184000.usb"
        ;;
    2) # Top
        cmd="t 36 0 1"
        device="2184200.usb"
        ;;
    esac

    echo "Resetting USB port $port"
    echo "$device" > "${usb_driver_path}/unbind"
    echo "$device" > "${usb_driver_path}/bind"
    echo "$cmd" | mcu-util
    sleep 2
}


hf_usb_reset_monitor() {
    local port=$1
    local dmesg_log=/tmp/hf-reset-${port}-dmesg.log
    local current_time=$(kernel_time_current)
    local period=3 # seconds in the past

    sleep $period

    dmesg > "$dmesg_log"
    local last_error="$(grep "hub ${port}-1.*err.*" "$dmesg_log" | tail -n1)"

    if [ -n "$last_error" ]; then
        local line_time="$(kernel_time_fromline "$last_error")"
        local min_time=${LAST_ERROR_TIME:-$((current_time - period))} # Minimum time within which an error can occur

        if [ $line_time -ge $min_time ]; then
            printf "error on usb re-init, reset port ${port}\n"
            hf_usb_cycle_port $port
        fi
    fi
    rm -rf "$dmesg_log"
}


reset_main() {
    local port=$1
    stdio_syslog_start "$2"

    # Get the current USB mux
    local mux_cfg="$(echo "t 10" | mcu-util | cut -d' ' -f1-2 | sed -e 's/ /\t/g')"

    # Determine CPU mux of the disconnected port
    local port_mapping=$(echo "$mux_cfg" | cut -f "$port")

    # mux=0 -- CPU B
    # mux=1 -- CPU A
    local cpu_mux_state
    case "$SUBFAMILYID" in
    1)
        cpu_mux_state="1"
        ;;
    2)
        cpu_mux_state="0"
        ;;
    esac

    if [ "$port_mapping" = "$cpu_mux_state" ]; then
        hf_usb_reset_monitor $port &
        local monitor_pid=$!
        hf_usb_cycle_port $port
        wait $monitor_pid
    fi

}


check_main() {
    local port=$1
    local period=10 # seconds in the past
    local current_time=$(kernel_time_current)
    local dmesg_log=/tmp/hf-check-${port}-dmesg.log

    stdio_syslog_start "$2"

    sleep 5 # udev event may occur before kernel message

    dmesg > "$dmesg_log"
    local last_error="$(grep "hub ${port}-1.*hub_port_status failed.*" "$dmesg_log" | tail -n1)"

    if [ -n "$last_error" ]; then
        local line_time="$(kernel_time_fromline "$last_error")"

        if [ $line_time -ge $((current_time - period)) ]; then
            printf "usb hub error on port ${port}, resetting\n"
            LAST_ERROR_TIME=$((line_time+1)) $0 reset $port
        fi
    fi

    rm "$dmesg_log"
}


print_help() {
    printf "Usage: %s [ reset port | check port ]\n" "${0##*/}" >&2
}


case ${1:-} in
    reset|check)
        if [ -z "${2:-}" ]; then
            print_help
            exit 1
        fi

        port="$2"
        process_name="$(basename $0)-${1}-${port}"
        PIDFILE="/run/${process_name}.pid"
        SERVICE_HANDLER=${1}_main start_service $port "$process_name"
        ;;

    *)
        print_help
        exit 1
        ;;
esac
