#!/bin/sh
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
#
# /etc/rc2.d/S91ifbinit - UNIX initialization of IFB

PATH=/usr/bin:/bin:/usr/sbin:/sbin

case $1 in 
'start')

#
# Find out how many IFB cards are installed on the system.
#
ifb_count=`prtconf -vp | egrep -c "\'(pci1091,108|pci1091,7a0|SUNW,Expert3D|pci1091,140|SUNW,Expert3D-Lite)\'"`
	if [ ${ifb_count} -eq 0 ]
	then
		exit 0
	fi		

# 
# Check the $devpath directory for any ifb devices
#
	devpath=/dev
	test -d /dev/fbs && devpath=/dev/fbs
	devs=`/bin/ls $devpath | grep ifb`

#
# If there aren't any around, there's no need to load
# the driver, so quit.
#
	if [ "$devs" = "" ]
	then
		exit 0
	fi

#
# We have a ifb, so modload the driver and make sure that it was successful.  
# Determine if we are running on a 64-bit boot and should load the 64-bit driver.
# First try to locate the driver in the uname -i directory.  Otherwise 
# look in uname -m.
#
	isainfo=`isalist`
	case "$isainfo" in
		*v9*)	drvdir="drv/sparcv9"
			;;
		*)	drvdir="drv"
			;;
	esac

	plat=`uname -i`
	if [ ! -f /platform/$plat/kernel/$drvdir/ifb ]
	then
		plat=`uname -m`
		if [ ! -f /platform/$plat/kernel/$drvdir/ifb ]
		then
			echo "ifbinit: cannot find the ifb driver module."
			exit 1
		fi
	fi

	err=`modload /platform/$plat/kernel/$drvdir/ifb 2>&1`
	if [ $? -ne 0 ]
	then
		echo "ifbinit: Problems loading IFB driver: $err"
		exit 1
	fi

#
#  ucode download error flag - a non-zero flag means there was a failure
#
	err_flag=0

#
#  If we can find the binary, then download the microcode to each
#  instance of the ifb device present in the system.
#
	if [ -f /usr/sbin/ifbdaemon ]
	then
		#
		#  Now download the microcode to each ifb. Note any failures but
		#  continue to attempt to download ucode to other ifbs.
		#
		for inst in $devs
		do
			/usr/sbin/ifbdaemon /dev/fbs/${inst} >/dev/console  2>&1 &
		done
	else
		err_flag=1
	fi

#
#  Check to see if ucode download succeeded for all devices
#
	if [ $err_flag -ne 0 ] 
	then
		exit 1
	fi
	exit 0
	;;
'stop')
	pid=`/usr/bin/ps -eo pid,comm | /usr/bin/awk '{ if ($2 == "/usr/sbin/ifbdaemon") print $1 }'`
        if test "$pid"
        then
                kill $pid
        fi
        exit 0
        ;;
*)
        echo "Usage: /etc/init.d/ifbdaemon { start | stop }"
        ;;
esac
exit 0
