#!/bin/sh

##########@@@SOFT@@@WARE@@@COPY@@@RIGHT@@@##################################
# DIALOGIC CONFIDENTIAL
#
# Copyright (C) 1990-2008 Dialogic Corporation. All Rights Reserved.
# The source code contained or described herein and all documents related
# to the source code ("Material") are owned by Dialogic Corporation or its
# suppliers or licensors. Title to the Material remains with Dialogic Corporation
# or its suppliers and licensors. The Material contains trade secrets and
# proprietary and confidential information of Dialogic or its suppliers and
# licensors. The Material is protected by worldwide copyright and trade secret
# laws and treaty provisions. No part of the Material may be used, copied,
# reproduced, modified, published, uploaded, posted, transmitted, distributed,
# or disclosed in any way without Dialogic's prior express written permission.
#
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be
# express and approved by Dialogic in writing.
#
###################################@@@SOFT@@@WARE@@@COPY@@@RIGHT@@@##########
############################################################################
#
#   Filename:        dinstlib
#   %date_modified:  Mon Nov 19 17:10:36 2007 %
#   Description:     Installation script functions for Boston device Driver
#   
#############################################################################


#"Exported functions" are:
#_mill_driver_install
#_mill_driver_install_check
#_mill_driver_defaults
#_mill_driver_remove

#This could be written to use function parameters instead of shell
#variables, but since there is no concept of local variables, we would
#likely have naming conflicts. So we'll just use shell variables for
#most things.

#_mill_driver_install
#Required input variables:
#num_isa_addrs - Number of ISA boards.
#isa_addrs_hex - a string consisting of the hex representation of all
#       $num_isa_addrs ISA board addresses seperated by commas, each with the
#       0x prefix, e.g. "0x280,0x288". If none, should be set to "0".
#       This is a platform specific variable.
#intrline - ISA interrupt line.
#max_pci_hw_modules - Maximum number of PCI/cPCI hw modules
#physbufsize - Physical buffer size.
#appbufsize - Application buffer size.
#machine_id - Machine ID, in hex (no leading 0x).
#do_reset - Reset on driver startup.
#history_enable - History enable.
#dhsize - History size.
#num_hists_phy - Number of physical histories.
#num_hists_apl - Number of application histories.
#hist_phy_admin_only - Whether to restrict physical histories to
#       admin channels only.
#num_I2O_frames_per_module - Number of I2O frames per module
#fc_interval - Driver flow control interval
#mem_drv_alloc_min - Min memory allocated per allocation
#mem_drv_alloc_quanta - Unit of allocation increment above min
#num_reserved_modules - Number of reserved modules.
#init_mod_vals - A string consisting of the hex representation of pairs
#       of module IDs and module numbers, each with the 0x prefix.
#       Each pair is inside a {} with a comma in between, and commas
#       between each pair.
#       This is a platform specific variable.
#mill_test1 - Driver test value
#mill_test2 - Driver test value
#mill_test3 - Driver test value
#Below are all platform specific variables
#driv_kvers_dir - Location of the kvers directory containing subdirs with
#       all the binaries for different kernel versions, both up/smp.
#
#On output - exit code is 0 for success, 1 for error.
#Variable err_msg - contains displayable error message
#Below are all platform specific variables
#These variables will not be modified except to set to 1 or error message.
_mill_driver_install()
{
    _mill_os_check
    if [ $? != 0 ]
    then
        return 1
    fi

    _mill_root_check
    if [ $? != 0 ]
    then
        return 1
    fi

    if [ ! -d "$driv_kvers_dir" ]
    then
        err_msg="Kernel version directory $driv_kvers_dir cannot be found"
        return 1
    fi

    #insmod doesn't handle negative numbers correctly
    if [ "$intrline" -lt 0 ]
    then
        intrline=0xffffffff
    fi
    if [ "$dmachan" -lt 0 ]
    then
        dmachan=0xffffffff
    fi

    #Create shell script to be run to start driver
    /bin/cat > startbost <<!
#!/bin/sh

if [ \$# -gt 0 ]
then
    if [ \$# != 1 -o "\$1" != -r ]
    then
        echo Usage: \$0 [-r]
        exit 1
    fi
    /bin/grep "^boston" /proc/modules > /dev/null
    if [ \$? != 0 ]
    then
        echo Boston driver not loaded.
        exit 1
    fi
    /sbin/rmmod boston
    exit 0
fi

/bin/grep "^boston" /proc/modules > /dev/null
if [ \$? = 0 ]
then
    echo Boston driver already loaded.
    exit 1
fi

kv=\`uname -r\`
case \$kv in
    2.0* | 2.2* | 2.4*)
        bin_name=boston.o
	;;
    *)
        bin_name=boston.ko
	;;
esac

/bin/sync; /bin/sync; /bin/sync
/sbin/insmod /lib/modules/\$kv/kernel/drivers/boston/\$bin_name \
nmill=$num_isa_addrs mill_isa_addrs=$isa_addrs_hex \
mill_isa_hw_int=$intrline mill_isa_dma_chan=$dmachan \
mill_mphm=$max_pci_hw_modules \
mill_phy_buf_size=$phybufsize mill_app_buf_size=$appbufsize \
mill_machine_id=0x$machine_id mill_do_reset=$do_reset \
mill_history_enable=$history_enable mill_history_size=$dhsize \
mill_num_hists_phy=$num_hists_phy mill_num_hists_apl=$num_hists_apl \
mill_hist_phy_admin_only=$hist_phy_admin_only \
mill_num_I2O_frames_per_module=$num_I2O_frames_per_module \
mill_fc_interval=$fc_interval \
mill_mem_drv_alloc_min=$mem_drv_alloc_min \
mill_mem_drv_alloc_quanta=$mem_drv_alloc_quanta \
mill_num_reserved_modules=$num_reserved_modules \
mill_mod_vals=$init_mod_vals \
mill_test1=$mill_test1 \
mill_test2=$mill_test2 \
mill_test3=$mill_test3 \
mill_config_vars_set=1 > /tmp/ii\$\$ 2>&1
ss=\$?
/bin/grep -v taint /tmp/ii\$\$ | /bin/grep -v "with warnings"
/bin/rm -f /tmp/ii\$\$
if [ \$ss != 0 ]
then
    echo Boston driver load failed.
    exit 1
fi

major=\`/bin/cat /proc/devices | /bin/awk '{if (\$2=="boston") print \$1}'\`
if [ x\$major = x ]
then
    echo Unable to locate boston device number.
    exit 1
fi
/bin/rm -f /dev/boston
/bin/mknod /dev/boston c \$major 0
/bin/sync; /bin/sync; /bin/sync
!

    /bin/chmod 755 startbost

    # Get list of kernel versions installed on this system
    # Install drivers for those kernel versions which we support
    kvs=`(cd /lib/modules; ls)`
    for kv in $kvs
    do
        # Make sure that the kernel version is really installed
        if [ ! -e /boot/vmlinuz-$kv ]
	then
	    continue
	fi

        if [ -e $driv_kvers_dir/$kv ]
        then
            #Compute name of driver binary
            case $kv in
                2.0* | 2.2* | 2.4*)
                    bin_name=boston.o
                    ;;
                *)
                    bin_name=boston.ko
                    ;;
            esac
            #If lib dir exists, then we will install that one.
            #Otherwise we use rpm to figure out the ARCH of the installed
            #kernel and use that one.
            if [ -d $driv_kvers_dir/$kv/lib ]
            then
                ldir=lib
            else
                #Compute proper package name if kernel is smp or enterprise
                case $kv in
                    *smp)
                        ltmp=`echo $kv | /bin/sed 's/smp//'`
                        ltmp=smp-$ltmp
                        ;;
                    *enterprise)
                        ltmp=`echo $kv | /bin/sed 's/enterprise//'`
                        ltmp=enterprise-$ltmp
                        ;;
                    *)
                        ltmp=$kv
                        ;;
                esac
                ltmp=`/bin/rpm --queryformat '%{ARCH}' -q kernel-$ltmp 2>&1`
                if [ "$ltmp" != i386 -a "$ltmp" != i586 -a \
                     "$ltmp" != i686 -a "$ltmp" != athlon ]
                then
                    if [ $kv = `/bin/uname -r` ]
                    then
                        ltmp=`/bin/uname -m`
                        if [ "$ltmp" != i386 -a "$ltmp" != i586 -a \
                             "$ltmp" != i686 -a "$ltmp" != athlon ]
                        then
                            
                            err_msg="Error determining arch for kernel ver in use ($kv)."
                            return 1
                        else
                            echo Determining arch for kernel ver in use using alternate mechanism.
                        fi
                    else
                        echo Error determining arch for kernel version $kv, skipping.
                        continue
                    fi
                fi
                ldir=lib-$ltmp
            fi
            if [ ! -e $driv_kvers_dir/$kv/$ldir/$bin_name ]
            then
                err_msg="Driver file $driv_kvers_dir/$kv/$ldir/$bin_name not found"
                return 1
            fi
            case $kv in
                2.2*)
                    /usr/bin/install $driv_kvers_dir/$kv/$ldir/$bin_name /lib/modules/$kv/misc
                    if [ $? != 0 ]
                    then
                        err_msg="A problem occurred executing install for /lib/modules/$kv/misc/$bin_name."
                        return 1
                    fi
                    ;;
                *)
                    /bin/mkdir /lib/modules/$kv/kernel/drivers/boston
                    /usr/bin/install $driv_kvers_dir/$kv/$ldir/$bin_name /lib/modules/$kv/kernel/drivers/boston/$bin_name
                    if [ $? != 0 ]
                    then
                        err_msg="A problem occurred executing install for /lib/modules/$kv/kernel/drivers/boston/$bin_name."
                        return 1
                    fi
                    ;;
            esac
        else
#           echo Kernel $kv not supported. Skipping.
            if [ $kv = `/bin/uname -r` ]
            then
                err_msg="Kernel version in use ($kv) not supported."
                return 1
            fi
        fi
    done
    /usr/bin/install startbost /etc
    if [ $? != 0 ]
    then
        err_msg="A problem occurred executing install for /etc/startbost."
        return 1
    fi
    /bin/rm -f startbost

    /bin/sync; /bin/sync; /bin/sync
    return 0
}

#_mill_driver_install_check
#No input/output, just exit code, 0 for not installed, non-zero for installed
#Will return 0 if a partial removal was already done
#Returns 2 on error
_mill_driver_install_check()
{
    _mill_os_check
    if [ $? != 0 ]
    then
        return 2
    fi

    if [ "$partial_removal_done" = 1 ]
    then
        return 0
    fi

    /bin/sync; /bin/sync; /bin/sync
    btmp=`/usr/bin/find /lib/modules -name boston -print`
    if [ "$btmp" = "" ]
    then
        return 0
    else
        return 1
    fi
}

#Defines defaults for use with getting parameter values.
_mill_driver_defaults()
{
    DEFAULTINTRLINE=5
    DEFAULTDMACHAN=-1
    DEFAULTNMILL=0
    MAXNMILL=8
    DEFAULTPHYBUFSIZE=32768
    MINPHYBUFSIZE=32768
    MAXPHYBUFSIZE=524288
    DEFAULTAPPBUFSIZE=10240
    MINAPPBUFSIZE=10240
    MAXAPPBUFSIZE=524288
    DEFAULTMACHINEID=1
    MINMACHINEID=1
    MAXMACHINEID=254
    DHSIZEDEF=1024000
    DHSIZEMIN=1024
    NUM_HISTS_PHY_DEF=0
    NUM_HISTS_APL_DEF=0
    HIST_PHY_ADMIN_ONLY_DEF=1
    MAX_RESERVED_MODULE_NUMS=32
    DEFAULT_MOD_NUM=F0
    DEFAULTMAXPCIHWMOD=16
    DFLT_I2O_FRAMES_PER_MODULE=16
    DFLT_FC_INTERVAL=250
    MIN_FC_INTERVAL=50
    MEM_DRV_ALLOC_MIN=152
    MEM_DRV_ALLOC_QUANTA=128

    #Below are platform specific values

    return 0
}

#_mill_driver_remove
#Optionally takes one input argument. If the argument is the word
#partial, then only a partial removal is performed. Can be used
#for removal prior to reinstallation.
#On output - exit code is 0 for success, 1 for error
#Variable err_msg - contains displayable error message
#Below are all platform specific variables
#These variables will not be modified except to set to 1 or error message.
_mill_driver_remove()
{
    _mill_os_check
    if [ $? != 0 ]
    then
        return 1
    fi

    _mill_root_check
    if [ $? != 0 ]
    then
        return 1
    fi

    #Check whether driver is loaded
    /bin/sync; /bin/sync; /bin/sync
    /bin/grep boston /proc/modules > /dev/null 2>&1
    if [ $? = 0 ]
    then
        /sbin/rmmod boston
        if [ $? != 0 ]
        then
            err_msg="A problem occurred executing rmmod."
            return 1
        fi
    fi

    #Remove the driver files
    /bin/rm -f /lib/modules/misc/$bin_name /lib/modules/*/misc/$bin_name
    /bin/rm -rf /lib/modules/*/kernel/drivers/boston
    /bin/rm -f /etc/startbost
    /bin/rm -f /dev/boston

    return 0
}

_mill_root_check()
{
    us=`/usr/bin/whoami`
    if [ "$us" != root ]
    then
        err_msg="User must be root (super user)."
        return 1
    fi
    return 0
}

_mill_os_check()
{
    osx=`/bin/uname -s`
    if [ "$osx" != Linux ]
    then
        err_msg="Wrong O/S: "$osx" instead of Linux."
        return 1
    fi
    return 0
}
