#!/bin/bash
#
# modules Insert/Remove the kernel loadable modules 
#

RETVAL=0
RC=0

AWK=`which awk` || AWK=/opt/bin/awk

# Call cpu_rev utility to distinguish between board variants
cpu_rev=rev`/nn/bin/cpu_rev`

# See how we were called.

# removeifunused()
# parameter : kernel loadable module name
# 1) determines if a specified kernel loadable module is loaded
# 2) determines if it is currently still being used
# 3) if not being used, unloads the module
# 4) if still being used, logs message to /var/log/shutdown.log

removeifunused() {
    lsmod | grep -w $1
    if [ "$?" -eq "0" ]; then
        lsmod | grep -w $1 | $AWK '{print $1" "$3}' | grep -w 0
        if [ "$?" -eq "0" ]; then
            /sbin/rmmod $1
        else
            echo Could not remove device $1, being used >> /var/log/shutdown.log
        fi
    fi
}

start() {
        echo -n $"Inserting kernel loadable modules."
        /sbin/insmod -f /nn/lib/modules/nn_gpiol.o
        /sbin/insmod -f /nn/lib/modules/nn_gpioh.o
        /sbin/insmod -f /nn/lib/modules/nn_dsp.o
        /sbin/insmod -f /nn/lib/modules/nn_fes.o
        /sbin/insmod -f /nn/lib/modules/nn_fpga.o
        /sbin/insmod -f /nn/lib/modules/nn_codec.o
        /sbin/insmod -f /nn/lib/modules/lm85.o
        /sbin/insmod -f /nn/lib/modules/gen_probe.o
        /sbin/insmod -f /nn/lib/modules/cfi_cmdset_0002.o
        /sbin/insmod -f /nn/lib/modules/cfi_probe.o
        /sbin/insmod -f /nn/lib/modules/nn_wanfailoverfwdr.o
        /sbin/insmod -f /nn/lib/modules/isdn.o

	# Determine the HW platform and load appropriate mtd_nn_csc module
	if [ -f /proc/nn/boardtype ] &&
	   [ "`cat /proc/nn/boardtype`" == "2" ]; then
            /sbin/insmod -f /nn/lib/modules/mtd_nn_csc2.o
            /sbin/insmod -f /nn/lib/modules/nn_spi.o
        if [ -e /nn/bin/fixsecsi ]; then
           /nn/bin/fixsecsi
           echo `date` " fixsecsi RC=" $? >> /var/log/fixsecsi.log
        fi
	else
            /sbin/insmod -f /nn/lib/modules/mtd_nn_csc.o
	fi
        /sbin/insmod -f /nn/lib/modules/nn_timeswitch.o
        /sbin/insmod -f /nn/lib/modules/nn_dchan_drv.o
        /sbin/insmod -f /nn/lib/modules/nn_vcom.o
        /sbin/insmod -f /nn/lib/modules/nn_mcc.o
        /sbin/insmod -f /nn/lib/modules/nn_sanity.o
        /sbin/insmod -f /nn/lib/modules/system-$cpu_rev.o
        /sbin/insmod -f /nn/lib/modules/usb-mhcd-$cpu_rev.o
        /sbin/insmod -f /nn/lib/modules/nn_secsi.o
        /sbin/insmod -f /nn/lib/modules/nn_rtp.o
        #the nn_isdndrv must be loaded after nn_mcc.o
        /sbin/insmod -f /nn/lib/modules/nn_isdndrv.o
        /sbin/insmod -f /nn/lib/modules/nn_nat.o

        # Run the midplane communications script for BCM if present
        if [ -f /etc/rc.d/init.d/mpc.sh ]; then
            /etc/rc.d/init.d/mpc.sh
        fi

        # nn_netfilter module is installed via following binary
        # nn_netfilter_install commented out on 11/26/04 at request of driver team 
        # /nn/bin/nn_netfilter_install
        return 0 
}

stop() {
    #reset router if it exists
    echo 0 >/nn/altroot/dev/resetCNIC
    sleep 1
    echo 1 >/nn/altroot/dev/resetCNIC

    echo -n $"Removing kernel loadable modules, if possible and if unused."

    removeifunused nn_isdndrv
    removeifunused nn_rtp
    removeifunused nn_netfilter
    removeifunused nn_secsi
    removeifunused nn_wanfailoverfwdr
    removeifunused isdn
    removeifunused nn_spi
    # CAN'T REMOVE EITHER system-$cpu_rev.o OR usb-mhcd-$cpu_rev.o MODULES
    # CAN'T DO THIS : removeifunused system-$cpu_rev
    # CAN'T DO THIS : removeifunused usb-mhcd-$cpu_rev
    removeifunused nn_mcc
    removeifunused nn_vcom
    removeifunused nn_dchan_drv
    removeifunused nn_timeswitch
    # Following will unload either of the "mtd_nn_csc" modules if they are loaded.
    removeifunused mtd_nn_csc
    removeifunused mtd_nn_csc2
    removeifunused cfi_probe
    removeifunused cfi_cmdset_0002
    removeifunused gen_probe
    removeifunused lm85
    removeifunused nn_codec
    # Wait for latest valid opportunity to signal that shutdown is complete.
    # This must occur before nn_gpioh kernel loadable module is unloaded.
    # LEDs are set to: Power LED = Off ( x88 ), Status LED = Solid Yellow ( x99 )
    echo `date` "Shutdown Sequence - Graceful Shutdown complete. Power may be removed." >> /var/log/StartupSequence.log
    echo -en "\x88" > /dev/led0
    echo -en "\x99" > /dev/led1
    removeifunused nn_fpga
    removeifunused nn_fes
    removeifunused nn_dsp
    removeifunused nn_gpioh
    removeifunused nn_gpiol
    removeifunused nn_nat
    removeifunused nn_sanity

    return 0
}

restart() {
        stop
        start
}        

case "$1" in
  start)
          start
        ;;
  stop)
          stop
        ;;
  restart)
          restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?
