#!/bin/sh

. /usr/bin/parse_cmdline.sh
. /usr/bin/modulefunc.sh
. /usr/cisco/bin/gpio.sh

MERAKI_BOARD=`cat /MERAKI_BOARD`
modules="rhl.ko rhlcmddisp.ko dualdfs.ko"

# RHL for Vanc/Axel/Baffin only
board_id=`cat /var/platform/board_id`
rhl_service=0
antenna_type=`cat /var/platform/antenna`
if [ "$MERAKI_BOARD" == "axel-bcm" ]; then
    # skip SS
    if [ -n "$board_id" ] && [ $board_id -eq 1 ]; then
        rhl_service=1
    fi
elif [ "$MERAKI_BOARD" == "axel-qca" ]; then
    # skip HG
    if [ -n "$board_id" ] && [ $board_id -eq 5 -o $board_id -eq 7 ]; then
        rhl_service=1
    fi
fi

if [ $rhl_service -eq 0 ]; then
    for f in $modules ; do
        rm -f /lib/modules/$f
    done
    exit 0
fi

if [ x"$1" != x"stop" ]; then
    for f in $modules ; do
        if [ ! -f /lib/modules/$f ]; then
            echo "ERROR: RHL: $f does not exist" 1>&2
            exit 1
        fi
    done
fi

usage()
{
    local me=$(basename $0)
    echo
    echo "$me: RHL service utility"
    echo "Usage:$me [command]"
    echo "command:"
    echo "  status      Check RHL driver status"
    echo "  start       Start RHL service"
    echo "  stop        Stop RHL service"
    echo
}

set_irq_affinity_list()
{
    local cpulist=$1
    local irqs

    if [ "$MERAKI_BOARD" == "axel-bcm" ]; then
        # BCM platform use INT-A for RHL interrupts
        irqs=$(cat /proc/interrupts | grep -E -m1 'GIC  92' | cut -d ':' -f1)
    elif [ "$MERAKI_BOARD" == "axel-qca" ]; then
        # QCA platform use MSI for RHL interrupts
        irqs="$irqs $(cat /proc/interrupts | grep -E -m1 'GIC 449' | cut -d ':' -f1)"
        irqs="$irqs $(cat /proc/interrupts | grep -E -m1 'GIC 450' | cut -d ':' -f1)"
        irqs="$irqs $(cat /proc/interrupts | grep -E -m1 'GIC 451' | cut -d ':' -f1)"
        irqs="$irqs $(cat /proc/interrupts | grep -E -m1 'GIC 452' | cut -d ':' -f1)"
        irqs="$irqs $(cat /proc/interrupts | grep -E -m1 'GIC 453' | cut -d ':' -f1)"
    fi

    for irq in $irqs ; do
       echo $cpulist > /proc/irq/${irq}/smp_affinity_list
    done
}

setup_rhl_gpio() {
    local gpio_file=/var/platform/rhb_reset_gpio

    # GPIO for SEL_WARM_COLD_RESET
    if [ ! -e $GPIO_DIR/rhb_reset_sel ] && [ -f $gpio_file ]; then
        setup_gpio $(cat $gpio_file) "out" "rhb_reset_sel"
    fi
}

#
# main
#
case "$1" in
    status)
        drv=$(lspci -k|grep RHL)
        if [ x"$drv" = x ]; then
            echo "RHL driver not loaded"
        else
            echo "RHL driver loaded"
            echo [lspci] $drv
            cat /proc/iomem | grep rhb-shiva | cut -d":" -f1 | \
                awk 'BEGIN{bar=0}/-/{printf "[bar %d] %s\n", bar, $1; bar+=1}'
        fi
        exit 0
        ;;

    start)
        drv=$(lspci -k|grep RHL)
        if [ x"$drv" != x ]; then
            exit 0
        fi
        if [ "$antenna_type" == "internal" ]; then
            ap_type=0
        fi
        if [ "$antenna_type" == "external" ] && [ "$MERAKI_BOARD" == "axel-bcm" ]; then
            ap_type=1
        fi
        if [ "$antenna_type" == "external" ] && [ "$MERAKI_BOARD" == "axel-qca" ]; then
            ap_type=2
        fi              
        modload /lib/modules/rhl.ko ap_antenna=$ap_type && modrm /lib/modules/rhl.ko
        modload /lib/modules/rhlcmddisp.ko && modrm /lib/modules/rhlcmddisp.ko
        modload /lib/modules/dualdfs.ko && modrm /lib/modules/dualdfs.ko
        set_irq_affinity_list 2,3
        exit 0
        ;;

    stop)
        rmmod dualdfs
        rmmod rhlcmddisp
        rmmod rhl
        exit 0
        ;;

    h)
        usage
        exit 0
        ;;

    *)
        echo "ERROR: unrecognized command $1"
        usage
        exit 1
        ;;

esac

