#!/bin/sh
#
# @(#)kfbinit  1.13 05/07/22 SMI
#
# Copyright (c) 2004 by Sun Microsystems, Inc.
# All rights reserved.
#
# /etc/rc2.d/S91kfbinit 
#
# kfbdaemon is needed for the XVR-2500 and XVR-2600
# Graphics Accelerators (kfb).
#

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

case $1 in 
'start')

#
# Find out how many KFB cards are installed on the system.
#
kfb_count=`prtconf -vp | egrep -c "\'(pci3d3d,176|SUNW,XVR-2400|SUNW,XVR-2200|pci3d3d,171|pci3d3d,170|pci3d3d,178|pci3d3d,177|pci3d3d,1052|pci3d3d,1055|SUNW,P25-LITE|SUNW,P25-HEAVY|SUNW,XVR-2500|SUNW,XVR-2600|pciex3d3d,177|pciex3d3d,178|pci3d3d,1050|pciex3d3d,1050)\'"`
	if [ ${kfb_count} -eq 0 ]
	then
		exit 0
	fi		

# 
# Check the $devpath directory for any kfb devices
#
	devpath=/dev
	test -d /dev/fbs && devpath=/dev/fbs
	devs=`/bin/ls $devpath | grep 'kfb[0-9]*[0-9]$'`

#
# 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 kfb, 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"
			;;
	esac

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

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

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

if [ -f /usr/sbin/kfbdaemon ]
then
        #
        #  Start kfbdaemon on each kfb device
        #
        for inst in $devs
        do
                /usr/sbin/kfbdaemon -dev /dev/fbs/${inst} >/dev/console  2>&1 &
        done
else
        err_flag=1
fi

#
#  Check to see if initialization succeeded for all devices
#
	if [ $err_flag -ne 0 ] 
	then
		echo "There was an error in starting kfbdaemon"
		exit 1
	fi
	exit 0
	;;
'stop')
        #
        #  Stop kfbdaemon on each kfb device
        #
	if [ -x /usr/sbin/kfbdaemon ]
	then
	    devpath=/dev
	    test -d /dev/fbs && devpath=/dev/fbs
	    devs=`/bin/ls $devpath | grep 'kfb[0-9]*[0-9]$'`
	    for inst in $devs
	    do
		    /usr/sbin/kfbdaemon -shutdown -dev /dev/fbs/${inst} >/dev/console  2>&1 &
	    done
	fi
	/usr/sbin/modinfo | awk '/kfb/ {print "modunload -i",$1}' | /bin/sh
	exit 0
	;;
*)
        echo "Usage: /etc/init.d/kfbinit { start | stop }"
        ;;
esac
exit 0

