#!/bin/bash
#**********@@@SOFT@@@WARE@@@COPY@@@RIGHT@@@**********************************
# Copyright (C) 2001-2006 Dialogic Corporation. All Rights Reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1.      Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2.      Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3.      Neither the name Dialogic Corporation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#***********************************@@@SOFT@@@WARE@@@COPY@@@RIGHT@@@**********/
######################################################################
#
# Script to start the Dialogic(R) drivers
#
#######################################################################

# Check for file containing our environment variables.
# If it is not found, abort the configuration.
# GCR: OURVARS=/etc/profile.d/ct_intel.sh
#if [ ! -x ${OURVARS} ]
#then
#	echo "$0: FATAL ERROR: Unable to find ${OURVARS}."
#	echo "$0: FATAL ERROR: Execution aborted due to invalid installation."
#	echo "$0: FATAL ERROR: Please reinstall the software by running install.sh."
#	exit 1
#fi

# get our environment variables
#. /etc/profile.d/ct_intel.sh

#####################################################################
# Driver building
#####################################################################
build_drvs(){
    SAVDIR=`pwd`
    cd ${INTEL_DIALOGIC_DIR}/drivers/

    ./ins_rmv_drvs.sh install
    RC=$?
    cd $SAVDIR
    if [ ${RC} -ne 0 ]
    then
       echo "ERROR: Driver Build failed."
       echo "Please check /usr/dialogic/drivers/drv.log for error."
       exit 1
    fi
}

#####################################################################
# Driver loading
#####################################################################
sw_copy(){
    # Copy driver if not present
    if [ ! -f /lib/modules/${VERSION}/misc/streams-dlgnDriver.$OBJ ]
    then
	build_drvs   
    fi
}	

sw_load(){
    # See if drivers are installed
    if [ ! -f ${MOD_DIR}/streams-dlgnDriver.$OBJ ]
    then
	return
    fi

    if [ -d /usr/src/LiS ]
    then
    # See if streams is already loaded
    cat /proc/devices | grep streams  > /dev/null 2>&1
    RC=$?
    if [ $RC -ne 0 ]
    then
	# Check for SELinux
        if [ -f /usr/sbin/getenforce ]
        then
            VAL=`/usr/sbin/getenforce`
            case "${VAL}" in
                Enforcing)
                   # Temporary Disable Enforcing mode
                   /usr/sbin/setenforce Permissive
                   /sbin/insmod -f ${MOD_DIR}/streams.$OBJ > /dev/null 2>&1
                   /usr/sbin/setenforce Enforcing
                   ;;
                *)
                   /sbin/insmod -f ${MOD_DIR}/streams.$OBJ >/dev/null 2>&1
                   ;;
            esac
        else
            /sbin/insmod -f ${MOD_DIR}/streams.$OBJ > /dev/null 2>&1
        fi
    fi

    # Get major number for clone device
    clone_major=`sed -n "s/[\n\t ]clone.*$//p" /proc/devices`

    for driver in dlgn gncfd gpio sctmr dvbm
    do
       # See if driver is already loaded
       grep ${driver} /proc/modules > /dev/null 2>&1
       RC=$?
       if [ $RC -ne 0 ]
       then
    	  # If not, then load it if the file exists
          if [ -f ${MOD_DIR}/streams-${driver}Driver.$OBJ ]
          then
             /sbin/insmod -f ${MOD_DIR}/streams-${driver}Driver.$OBJ > /dev/null 2>&1


             case "${driver}" in
                 dlgn)
          	    echo "Loading Springware Drivers"

                    # Get major device number
		    device_major=`grep ${driver} /proc/devices | cut -f1 -d" "`
                    rm -f /dev/${driver}
                    mknod /dev/${driver} c $clone_major $device_major
                    chmod 0666 /dev/${driver}
                    ;;
                 gncfd)
                    # Get major device number
		    device_major=`grep ${driver} /proc/devices | cut -f1 -d" "`
                    rm -f /dev/${driver}
                    rm -f /dev/d_gn_cnfg
                    mknod /dev/${driver} c $clone_major $device_major
                    mknod /dev/d_gn_cnfg c $device_major 255
                    chmod 0666 /dev/${driver} /dev/d_gn_cnfg
                    ;;
                 gpio)
                    # Get major device number
		    device_major=`grep ${driver} /proc/devices | cut -f1 -d" "`
                    rm -f /dev/${driver}DRV
                    mknod /dev/gpioDRV c $device_major 255
                    chmod 0666 /dev/${driver}DRV 
                    ;;
             esac
	  else
	     echo "Drivers not found"
             return
          fi  # end driver file was found
       fi     # end driver not loaded
    done
    fi
}

dm3_copy(){
    # Copy driver if not present
    if [ ! -f /lib/modules/${VERSION}/misc/mercd.$OBJ ]
    then
    # GCR: build_drvs   
            echo
            echo "!!! Error: mercd.ko is not installed."
            echo "!!!    Build mercd.ko and copy it to /lib/modules/${VERSION}/misc/"
            echo "!!!    If the /lib/modules/${VERSION}/misc directory does not exist, create it."
            echo
    fi
}	

dm3_load(){

    # See if loaded already
    cat /proc/devices | grep mercd > /dev/null 2>&1
    RC=$?
    if [ $RC -ne 0 ]
    then
        # Check if driver is installed before attempt
        if [ -f ${MOD_DIR}/mercd.$OBJ ]
        then
            echo "Loading DM3 Driver"
            OUTPUT=`/sbin/modprobe -f mercd dm3_feature=$DM3_FEATURE  > /dev/null 2>&1`
            RC=$?
            if [ $RC -ne 0 ]
            then
               echo $OUTPUT | grep "already exists" >/dev/null;RC1=$?
               echo $OUTPUT | grep "No such device" >/dev/null;RC2=$?
               if [ $RC1 -eq 0 ] ; then
                  echo "**>DM3 driver already loaded"  
               elif [ $RC2 -eq 0 ] ; then
                    echo "**>DM3 no devices found"
               else  
                   echo "**>DM3 driver would not load:"
                   echo $OUTPUT
               fi 
               return
            fi
        else
            echo
            echo "!!! Error: mercd.ko is not installed."
            echo "!!!    Build mercd.ko and copy it to /lib/modules/${VERSION}/misc/"
            echo "!!!    If the /lib/modules/${VERSION}/misc directory does not exist, create it."
            echo
            return
        fi
    else   
        echo "DM3 driver already loaded"
    fi

    # If we get here, we have a loaded driver, now make the devices if 
    # the do not exist yet

    mercd_device=`cat /proc/devices | grep mercd`
    mercd_major=`echo $mercd_device | cut -f1 -d" "`
    devName="/dev/mercd"

    # Remove this device and build the correct one
    #echo "Creating device $devName"
    rm -f $devName > /dev/null
    mknod $devName c $mercd_major 0
    chmod 0666 $devName 

     
    if [ -f /proc/mercd ]
    then
        grep 'DOWNLOADED' /proc/mercd > /dev/null 2>&1
        RC=$?
    else
        ${INTEL_DIALOGIC_BIN}/listboards -l2 --extrainfo | grep 'DOWNLOADED' > /dev/null 2>&1
        RC=$?
    fi

    if [ $RC -eq 1 ]
    then
        # Run the Gemini Kernel loader
        if [ -f ${INTEL_DIALOGIC_DIR}/bin/dm3kload ] 
        then
	   dm3kload -f ${INTEL_DIALOGIC_DIR}/data/hmp2_pdk.bin -g 1 > /dev/null 2>&1
        fi
   fi

}

pmac_copy(){
    # Copy driver if not present
    if [ ! -f /lib/modules/${VERSION}/misc/pmac.o ]
    then
       build_drvs   
    fi
}

pmac_load(){

    # See if loaded already
    cat /proc/devices | grep pmac  >/dev/null 2>&1
    RC=$?
    if [ $RC -ne 0 ]
    then
        # Check if driver is installed before attempt
        if [ -f ${MOD_DIR}/pmac.$OBJ ]
        then
            echo "Loading NetStructure(PMAC) Driver"
            OUTPUT=`/sbin/insmod -f ${MOD_DIR}/pmac.$OBJ >/dev/null 2>&1`
            RC=$?
            if [ $RC -ne 0 ]
            then
		echo $OUTPUT | grep "already exists" >/dev/null;RC1=$?
		echo $OUTPUT | grep "No such device" >/dev/null;RC2=$?
		if [ $RC1 -eq 0 ] ; then
		    echo "**>Netstructure IPT (PMAC) driver already loaded"  
		elif [ $RC2 -eq 0 ] ; then
                    echo "**>Netstructure IPT (PMAC) no devices found"
		else  
                    echo "**>Netstructure IPT (PMAC) driver would not load:"
		    echo $OUTPUT
		fi 
                return
            fi
        fi
    fi

    # If we get here, we have a loaded driver, now make the devices 
    pmacd_device=`cat /proc/devices | grep pmac`
    pmacd_major=`echo $pmacd_device | cut -f1 -d" "`
    minor=0

    for devName in /dev/pmac_control /dev/pmac_queue /dev/pmac_stream
    do
        rm -f $devName > /dev/null
        mknod $devName c $pmacd_major $minor
        chmod 0666 $devName 

        # increment minor number for next loop
        minor=$(( minor+1 ))
    done
}


######################################################################
# Load ctimod - driver kernel interface module (replaces LiS for DM3)
######################################################################
ctimod_load() {
    if [ ! -f /lib/modules/${VERSION}/misc/ctimod.ko ]
    then
         echo
         echo "!!! Error: ctimod.ko is not installed."
         echo "!!!    Build ctimod.ko and copy it to /lib/modules/${VERSION}/misc/"
         echo "!!!    If the /lib/modules/${VERSION}/misc directory does not exist, create it."
         echo
    fi
    
    # Start up driver kernel interface module
    /sbin/lsmod | grep ctimod > /dev/null 2>&1
    RC=$?
    if [ $RC -ne 0 ]
    then
       OUTPUT=`/sbin/insmod -f ${MOD_DIR}/ctimod.$OBJ 2>&1`
    fi

}

ctimod_copy() {
    # Copy driver if not present
    if [ ! -f /lib/modules/${VERSION}/misc/ctimod.ko ]
    then
        # GCR: build_drvs
         echo
         echo "!!! Error: ctimod.ko is not installed."
         echo "!!!    Build ctimod.ko and copy it to /lib/modules/${VERSION}/misc/"
         echo "!!!    If the /lib/modules/${VERSION}/misc directory does not exist, create it."
         echo
    fi
}


######################################################################
# Pigeon Point hotswap software start
######################################################################
hotswap_start() {

    # get kernel version and isolate major and minor numbers
    VERSION=`uname -r`
    MOD_DIR=/lib/modules/${VERSION}/misc
    K_MAJOR=${VERSION%%.*}
    K_MINTMP=${VERSION##$K_MAJOR.}
    K_MINOR=${K_MINTMP%%.[0-9]*}

    # assume K_MAJOR is 2 for now so just check K_MINOR
    case ${K_MINOR} in
        4)
            # 2.4 kernels use /proc/ksyms
	     SYMSDEV=/proc/ksyms
	     OBJ=o
            # Is the Pigeon Point software installed in kernel
             grep free_module ${SYMSDEV} >/dev/null
             RC=$?

            if [ $RC -eq 0 ]
            then
	    # If Pigeon point software is available & not running??
	    ps -ae | grep hssd_notify &> /dev/null
	    RC=$?
	       if [ -x /sbin/hsk ] && [ $RC -ne 0 ]
	       then
	          /sbin/hsk start
	       fi
               else
    	       # Clean up stale nodes
    	       rm -f /dev/hssd_dev
            fi
	    ;;
        6)
            # 2.6 kernels use /proc/kallsyms
	    SYMSDEV=/proc/kallsyms
	    OBJ=ko
	    ;;
        *)
            # catch unknown minor version
	    echo "ERROR: Unknown Linux kernel minor version ${K_MINOR}"
	    return 1
	    ;;
    esac

}
######################################################################
# Beginning of the script
######################################################################
PATH=$PATH:/sbin:/usr/sbin

# Start hotswap 
hotswap_start

#case $1 in

case "dm3" in
   'hotswap')
        # Hotswap started by default
        exit 0
    ;;
   'dm3')
        ctimod_load
        # DM3_FEATURE=0   Stub is turned on , Rate Interrupt Callback is at DPC level
        # DM3_FEATURE=256 Stub is turned off, Rate Interrupt Callback is at DPC level
        # DM3_FEATURE=512 Stub is turned on , Rate Interrupt Callback is at ISR level
        # DM3_FEATURE=768 Stub is turned off, Rate Interrupt Callback is at ISR level
        #   Note: The above values will be rearranged when the default becomes "Off, ISR".
        # Enabling X-msging flag 0x400 (1024)

   # PACKAGE COMMANDS
   if [ -f /etc/debian_version ] ; then
      DIST="Debian"
      BID_TEST_PKG="dpkg -l"
   else
      BID_TEST_PKG="rpm -q --quiet"
   fi

   if [ "${DIST}" = "Debian" ]
   then
       ${BID_TEST_PKG} lsb-dialogic-hmp-t1e1 | grep t1e1 &>/dev/null
       if [ $? = 1 ]
       then
          BID_HMP_INSTALLED=1
          DM3_FEATURE=768
       fi
   else
       ${BID_TEST_PKG} dialogic-hmp-t1e1
       if [ $? = 1 ]
       then
          BID_HMP_INSTALLED=1
          DM3_FEATURE=768
       fi
    fi
 
        dm3_load
    ;;

   'pmac')
        ctimod_load
        pmac_load
    ;;

   'springware')
        sw_load
    ;;

   *)
      ctimod_load
      dm3_load
      pmac_load
      sw_load
      ;;

esac

