#!/bin/sh
#
# ===========================================================================
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2016, Marvell International Ltd.
#
# Alternatively, this software may be distributed under the terms of the GNU
# General Public License Version 2, and any use shall comply with the terms and
# conditions of the GPL.  A copy of the GPL is available at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
# IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
# ARE EXPRESSLY DISCLAIMED.  The GPL license provides additional details about
# this warranty disclaimer.
# ================================================================================

#
# Simple script to load scan on MRVL embedded linux platform.

echo "powering imaging island"
tail -f /dev/imagepower 2>&1 > /dev/null &
IMAGEPOWER_PID=$!

echo "loading scan subsystem"

if [ "$#" -ne 1 ]; then
    kernelname=`uname -r`
    modulepath1=/lib/modules/$kernelname/kernel/drivers/scan
    modulepath2=/lib/modules/$kernelname/kernel/drivers/scan_pipe
else
    modulepath1=$1
    modulepath2=$1
fi

load_module() {
    loaded=`lsmod | grep ^$1\\\\s | wc -l`
    if [ $loaded -eq 0 ]; then
        echo "loading: " $1
        if [ -f $modulepath1/$1.ko ];
        then
            insmod $modulepath1/$1.ko
        else
            insmod $modulepath2/$1.ko
        fi
        if [ $? -ne 0 ]; then
            echo "load failed: " $1
            exit 2
        fi
    else
        echo "already loaded: " $1
    fi
}

# Continue without rt thresholding (as it was prior to 3.14 kernel)
echo -1 > /proc/sys/kernel/sched_rt_runtime_us

# Make sure our external dependencies are loaded
modprobe dros
modprobe sccplite
# Note: The platform needs to load the stepper driver ... which one?
#modprobe stepper-a0
#modprobe stepper-b0
modprobe dmaalloc

# Now load the actual scan modules
load_module descriptors

load_module afe_top

if [ -f $modulepath2/afe_*.ko ];
then
    for afe in $modulepath2/afe_*.ko
    do
        afe_nopath=${afe#$modulepath2/}
        afe_name=${afe_nopath%.ko}
        load_module $afe_name
    done
else
    load_module afe_sample
fi

load_module icetestdriver

load_module piedriver
load_module pieconvenience
load_module pieverificationmodule

load_module picdriver
load_module picconvenience
load_module picverificationmodule

load_module cisxdriver

load_module scanblkdriver

load_module qsort

load_module scansb
load_module kscantask
load_module kscanman

load_module pic_ctrl

load_module icefile

# create /dev/scanman
mkdevscan
if [ $? -ne 0 ] ; then
    echo "mkdevscan script failed"
    exit 3
fi

# create /dev/icefile
mkdev_icefile
if [ $? -ne 0 ] ; then
    echo "mkdev_icefile script failed"
    exit 3
fi

set_cpu_affinity() {
    # davep 07-Dec-2012 ; kscantask needs many, many hours of work to chase SMP
    # bugs from the code. For now, hardwire everything to a single CPU (the 2nd)

    # if this kernel has SMP
    if [ -f /proc/irq/default_smp_affinity ] ; then
        # hardwire all scan code and interrupts to 2nd CPU
        cpumask=2
        echo $cpumask > /proc/irq/222/smp_affinity
        echo $cpumask > /proc/irq/223/smp_affinity
        echo $cpumask > /proc/irq/224/smp_affinity
        echo $cpumask > /proc/irq/225/smp_affinity
        echo $cpumask > /proc/irq/226/smp_affinity
        echo $cpumask > /proc/irq/227/smp_affinity

        taskset -p $cpumask $(pidof kscantask)
        taskset -p $cpumask $(pidof kscanman)
        taskset -p $cpumask $(pidof qsort)
        taskset -p $cpumask $(pidof scansb)
    fi

    return 0
}

set_cpu_affinity

if [ $? -ne 0 ] ; then
    echo "failed to change kscantask cpu affinity"
    exit 3
fi

echo "scan successfully loaded"

echo "releasing hold on imaging power island"
kill $IMAGEPOWER_PID

exit 0

