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

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

case $1 in 
'start')

#
# Find out how many JFB cards are installed on the system.
#
jfb_count=`prtconf -vp | egrep -c "\'(pci3d3d,1044|SUNW,XVR-1200|pci3d3d,1047|SUNW,XVR-600)\'"`
	if [ ${jfb_count} -eq 0 ]
	then
		exit 0
	fi		

# 
# Check the $devpath directory for any jfb devices
#
	devpath=/dev
	test -d /dev/fbs && devpath=/dev/fbs
	devs=`/bin/ls -1 $devpath | /bin/grep 'jfb[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 jfb, 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/jfb ]
	then
		plat=`uname -m`
		if [ ! -f /platform/$plat/kernel/$drvdir/jfb ]
		then
			echo "jfbinit: cannot find the jfb driver module."
			exit 1
		fi
	fi

	err=`modload /platform/$plat/kernel/$drvdir/jfb 2>&1`
	if [ $? -ne 0 ]
	then
		echo "jfbinit: Problems loading JFB 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 jfb device present in the system.
#
	if [ -f /usr/sbin/jfbdaemon ]
	then
		#
		#  Now download the microcode to each jfb. Note any failures but
		#  continue to attempt to download ucode to other jfbs.
		#
		for inst in $devs
		do
			/usr/sbin/jfbdaemon /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/jfbdaemon") print $1 }'`
        if test "$pid"
        then
                kill $pid
        fi
        exit 0
        ;;
*)
        echo "Usage: /etc/init.d/jfbdaemon { start | stop }"
        ;;
esac
exit 0
