#!/bin/sh
#
# drivers_tdaq:       Starts TDAQ related drivers
#
# Version:      @(#) /etc/rc.d/init.d/drivers_tdaq 1.1
#
# chkconfig: 5 95 5
# description: Starts and stops tdaq drivers at boot time and shutdown.
#
# hide: true

# Source function library.
. /etc/rc.d/init.d/functions

export DRIVER_PATH=/usr/atlas/driver
export LD_LIBRARY_PATH=/usr/atlas/lib
export BINARY_PATH=/usr/atlas/bin
export VMETAB_PATH=/usr/atlas/driver

# See how we were called.
case "$1" in
  start)
        echo "Starting CMEM_RCC driver "
        # load the module
        /sbin/insmod -f $DRIVER_PATH/cmem_rcc-`uname -r`.ko
        # remove old device node
        rm -f /dev/cmem_rcc
        # get major number
        major=`awk "\\$2==\"cmem_rcc\" {print \\$1}" /proc/devices`
        echo major number is $major
        # make device node
        mknod /dev/cmem_rcc c $major 0
        #give permissions
        chmod 666 /dev/cmem_rcc
        echo
	
        echo "Starting IO_RCC driver "
        # load the module
        /sbin/insmod -f $DRIVER_PATH/io_rcc-`uname -r`.ko
        # remove old device node
        rm -f /dev/io_rcc
        # get major number
        major=`awk "\\$2==\"io_rcc\" {print \\$1}" /proc/devices`
        echo major number is $major
        # make device node
        mknod /dev/io_rcc c $major 0
        # give permissions
        chmod 666 /dev/io_rcc
 	echo

	export NROBINS=`/sbin/lspci -n | grep -c "10dc:0144"`
	echo $NROBINS Robin cards found
	if [ $NROBINS -gt 0 ]; then
          echo "Starting ROBIN driver "
          # load the module
          /sbin/insmod -f $DRIVER_PATH/robin-`uname -r`.ko
          # remove old device node
          rm -f /dev/robin
          # get major number
          major=`awk "\\$2==\"robin\" {print \\$1}" /proc/devices`
          echo major number is $major
          # make device node
          mknod /dev/robin c $major 0
          # give permissions
          chmod 666 /dev/robin
   	  if [ $NROBINS -gt 0 ]; then
            $BINARY_PATH/robinconfig 0 5
	  fi  
   	  if [ $NROBINS -gt 1 ]; then
            $BINARY_PATH/robinconfig 1 5
	  fi
	  if [ $NROBINS -gt 2 ]; then
            $BINARY_PATH/robinconfig 2 5
	  fi
	  if [ $NROBINS -gt 3 ]; then
            $BINARY_PATH/robinconfig 3 5
	  fi 
	  if [ $NROBINS -gt 4 ]; then
	    echo configuring robin 4 
            $BINARY_PATH/robinconfig 4 5
	  fi 
	  echo
        fi 

        /sbin/lspci -n | grep "10e3:0000"
	if [ $? = 0 ]; then
          echo "Starting VME_RCC driver "
          # load the module
          /sbin/insmod -f $DRIVER_PATH/vme_rcc-`uname -r`.ko
          # remove old device node
          rm -f /dev/vme_rcc
          # get major number
          major=`awk "\\$2==\"vme_rcc\" {print \\$1}" /proc/devices`
          echo major number is $major
          # make device node
          mknod /dev/vme_rcc c $major 0
          # give permissions
          chmod 666 /dev/vme_rcc
          # Initialize the Universe chip
          $BINARY_PATH/vmeconfig -a $VMETAB_PATH/vmetab
          echo
	fi
 
        /sbin/lspci -n | grep "10dc:001b"
	if [ $? = 0 ]; then
          echo "Starting QUEST driver "
          # load the module
          /sbin/insmod -f $DRIVER_PATH/quest-`uname -r`.ko
          # remove old device node
          rm -f /dev/quest
          # get major number
          major=`awk "\\$2==\"quest\" {print \\$1}" /proc/devices`
          echo major number is $major
          # make device node
          mknod /dev/quest c $major 0
          # give permissions
          chmod 666 /dev/quest
          echo
	fi
	
        /sbin/lspci -n | grep "10dc:0017"
	if [ $? = 0 ]; then
	  echo "Starting SOLAR driver "
          # load the module
          /sbin/insmod -f $DRIVER_PATH/solar-`uname -r`.ko
          # remove old device node
          rm -f /dev/solar
          # get major number
          major=`awk "\\$2==\"solar\" {print \\$1}" /proc/devices`
          echo major number is $major
          # make device node
          mknod /dev/solar c $major 0
          # give permissions
          chmod 666 /dev/solar
          echo
	fi

        /sbin/lspci -n | grep "10dc:0014"
	if [ $? = 0 ]; then
          echo "Starting FILAR driver "
          # load the module
          /sbin/insmod -f $DRIVER_PATH/filar-`uname -r`.ko
          # remove old device node
          rm -f /dev/filar
          # get major number
          major=`awk "\\$2==\"filar\" {print \\$1}" /proc/devices`
          echo major number is $major
          # make device node
          mknod /dev/filar c $major 0
          # give permissions
          chmod 666 /dev/filar
          echo
	fi
        ;;

  stop)       
        echo "Shutting down CMEM_RCC driver "
        /sbin/rmmod cmem_rcc

        echo "Shutting down IO_RCC driver "
        /sbin/rmmod io_rcc

        /sbin/lsmod | grep robin
	if [ $? = 0 ]; then
          echo "Shutting down ROBIN driver "
          /sbin/rmmod robin
	fi

        /sbin/lsmod | grep vme_rcc
	if [ $? = 0 ]; then
          echo "Shutting down VME_RCC driver "
          /sbin/rmmod vme_rcc
	fi

        /sbin/lsmod | grep filar
	if [ $? = 0 ]; then
          echo "Shutting down FILAR driver "
          /sbin/rmmod filar
	fi
	
	/sbin/lsmod | grep quest
	if [ $? = 0 ]; then
          echo "Shutting down QUEST driver "
          /sbin/rmmod quest
	fi
	
	/sbin/lsmod | grep solar
	if [ $? = 0 ]; then
          echo "Shutting down SOLAR driver "
          /sbin/rmmod solar
	fi
	
	/sbin/lsmod | grep robintty
	if [ $? = 0 ]; then
          echo "Shutting down robin TTY driver "
          /sbin/rmmod robintty
	fi	
        ;;

  start_robin_tty)
        /sbin/lsmod | grep robin-`uname -r`
	if [ $? = 0 ]; then
          echo "Starting robin TTY driver "
          # load the module
          /sbin/insmod -f $DRIVER_PATH/robintty-`uname -r`.ko 
          # remove old device node
          rm -f /dev/robintty_*
          # get major number
          major=`awk "\\$2==\"tty_robin\" {print \\$1}" /proc/devices`
          echo major number is $major
          # make device node
          mknod /dev/robin_tty0 c $major 0
          mknod /dev/robin_tty1 c $major 1
          mknod /dev/robin_tty2 c $major 2
          mknod /dev/robin_tty3 c $major 3
          mknod /dev/robin_tty4 c $major 4
          mknod /dev/robin_tty5 c $major 5
          # give permissions
          chmod 666 /dev/robin_tty0
          chmod 666 /dev/robin_tty1
          chmod 666 /dev/robin_tty2
          chmod 666 /dev/robin_tty3
          chmod 666 /dev/robin_tty4
          chmod 666 /dev/robin_tty5
          echo
	fi  
	;;

  stop_robin_tty)
        echo "Shutting down robin TTY driver "
        /sbin/rmmod robintty-`uname -r`
        echo
	;;

  status)       
	echo "Status of the cmem_rcc driver"
	more /proc/cmem_rcc

	echo "Status of the io_rcc driver"
	more /proc/io_rcc

        /sbin/lsmod | grep robin-`uname -r`
	if [ $? = 0 ]; then
          echo "Status of the ROBIN driver "
          more /proc/robin
	fi

        /sbin/lsmod | grep vme_rcc-`uname -r`
	if [ $? = 0 ]; then
          echo "Status of the VME_RCC driver "
          more /proc/vme_rcc
	fi

        /sbin/lsmod | grep filar-`uname -r`
	if [ $? = 0 ]; then
          echo "Status of the FILAR driver "
          more /proc/filar
	fi
	
	/sbin/lsmod | grep quest-`uname -r`
	if [ $? = 0 ]; then
          echo "Status of the QUEST driver "
          more /proc/quest
	fi
	
	/sbin/lsmod | grep solar-`uname -r`
	if [ $? = 0 ]; then
          echo "Status of the SOLAR driver "
          /more /proc/solar
	fi	
        ;; 
  *)
        echo "*** Usage: drivers_tdaq {start|stop|status|start_robin_tty|stop_robin_tty}"
        exit 1

esac

exit 0
