#!/bin/sh
PATH=$PATH:/usr/sbin

BITSPERPIXEL="$(fbset | grep geom | awk '{print $6}')"
YRES="$(fbset | grep geom | awk '{print $3}')"

unload_driver()
{
	rmmod bufferclass_ti 2>/dev/null
	rmmod omaplfb 2>/dev/null
	rmmod pvrsrvkm 2>/dev/null
}

load_driver()
{
	if lsmod | grep -q omaplfb; then
		echo "PVR driver is already loaded"
		return 0
	fi

	if test -f /etc/powervr-kmodver; then
		version=`cat /etc/powervr-kmodver`
	fi
	insmod /lib/modules/$(uname -r)/kernel/drivers/gpu/pvr/$version/pvrsrvkm.ko
	insmod /lib/modules/$(uname -r)/kernel/drivers/gpu/pvr/$version/omaplfb.ko
	bc_ko="/lib/modules/$(uname -r)/kernel/drivers/gpu/pvr/$version/bufferclass_ti.ko"
	if test -f "$bc_ko"; then
		insmod $bc_ko
	fi

	if ! lsmod | grep -q pvrsrvkm; then
		echo "pvrsrvkm failed to load"
		exit 1
	fi

	test -e /dev/pvrsrvkm || sleep 1
	if ! test -e /dev/pvrsrvkm; then
		echo "pvrsrvkm dev file did not appear"
		exit 1
	fi

	chmod 666 /dev/pvrsrvkm
}

# $1 - full pathname, like /usr/lib/ES5.0/libGLESv2.so.1.4.14.2616
install_lib()
{
	base=`basename $1`
	baseso=`echo $base | sed -e 's:\(lib.*.so\).*:\1:'`
	rm /usr/lib/${baseso}*
	cp -a $1 /usr/lib/

	cd /usr/lib
	ln -fs $base ${baseso}
	ln -fs $base ${baseso}.1
	cd $OLDPWD
}

if [ "$1" = "" ]; then
	echo PVR-INIT: Please use start, stop, or restart.
	exit 1
fi

if [ "$1" = "stop" -o  "$1" = "restart" ]; then
	echo Stopping PVR
	unload_driver
fi

if [ "$1" = "stop" ]; then
	exit 0
fi

# Set RGBA ordering to something the drivers like
if [ "$BITSPERPIXEL" = "32" ] ; then
	fbset -rgba 8/16,8/8,8/0,8/24
fi

# Try to enable triple buffering when there's enough VRAM
fbset -vyres $(expr $YRES \* 3) 

	echo Starting PVR

	touch /etc/powervr-esrev
	SAVED_ESREVISION="$(cat /etc/powervr-esrev)"

	devmem2 0x48004B48 w 0x2 > /dev/null
	devmem2 0x48004B10 w 0x1 > /dev/null
	devmem2 0x48004B00 w 0x2 > /dev/null

	ES_REVISION="$(devmem2 0x50000014 | sed -e s:0x10205:5: -e s:0x10201:3: -e s:0x10003:2: | tail -n1 | awk -F': ' '{print $2}')"

	# enable hardware supervised domain power state transition - makes suspend work
	devmem2 0x48004B48 w 0x3 > /dev/null

	if [ "${ES_REVISION}" != "${SAVED_ESREVISION}" -o ! -e /etc/powervr-kmodver ] ; then
		sdkver=`echo /usr/lib/ES${ES_REVISION}.0/libGLESv2.so.1.* | sed -e 's:.*lib.*.so.\(.*\).*:\1:'`
		echo -n "Performing SGX fixup for"
		echo " ES${ES_REVISION}.x (${sdkver})"
		for lib in /usr/lib/ES${ES_REVISION}.0/*.so*; do
			install_lib $lib
		done
		cp -a /usr/bin/ES${ES_REVISION}.0/* /usr/bin/
		echo "${ES_REVISION}" > /etc/powervr-esrev

		# if /etc/powervr-kmodver is available, trust it, else figure out something
		if ! test -e /etc/powervr-kmodver; then
			touch /etc/powervr-kmodver
			# for 1.4.14.2616 there are 2 incompatible modules,
			# assume the default one, which is selected by empty powervr-kmodver
			if [ "${sdkver}" != "1.4.14.2616" ]; then
				echo "Switching driver to ${sdkver}"
				echo "${sdkver}" > /etc/powervr-kmodver
			fi
		fi
		sync
	fi

	# set up optional armhf libs if needed
	if test -d /usr/lib/arm-linux-gnueabihf/ES${ES_REVISION}.0; then
		touch /etc/powervr-esrev-armhf
		SAVED_ESREVISION_HF="$(cat /etc/powervr-esrev-armhf)"

		if [ "${ES_REVISION}" != "${SAVED_ESREVISION_HF}" ] ; then
			echo -n "Performing SGX armhf fixup for"
			echo " ES${ES_REVISION}.x"
			for lib in /usr/lib/arm-linux-gnueabihf/ES${ES_REVISION}.0/*.so*; do
				cp -a $lib /usr/lib/arm-linux-gnueabihf/
			done
			echo "${ES_REVISION}" > /etc/powervr-esrev-armhf
		fi
	fi

	load_driver

	if test -x /usr/bin/pvrsrvinit; then
		/usr/bin/pvrsrvinit
	elif test -x /usr/bin/pvrsrvctl; then
		/usr/bin/pvrsrvctl --start --no-module
	else
		echo "Error: missing /usr/bin/pvrsrv* or bad permissions"
	fi
