#!/bin/sh
# chkconfig: 2345 70 70
### BEGIN INIT INFO
# Provides:          pvr
# Required-Start:    modules $local_fs
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start pvr services
# Description:       Enable service provided by pvr driver
# X-Start-Before:    
# X-Stop-After:      
# X-Timesys-Start-Number:  70
# X-Timesys-Stop-Number:  30
### END INIT INFO

CPUTYPE=TI33XX

case "$1" in 
  start)
    # For X11, which uses DRI/DRM, the drm and pvrsrv modules will loaded by
    # the libraries when the X11 server starts.
    if [ -d /usr/local/XSGX ]; then
        exit 0
    fi

    echo Starting PVR

    if [ ! -f /lib/modules/`uname -r`/modules.dep ]; then
      depmod
    elif ! grep pvrsrvkm /lib/modules/`uname -r`/modules.dep >/dev/null; then
      depmod
    fi

    modprobe pvrsrvkm
    modprobe omaplfb
    modprobe bufferclass_ti

    pvr_maj=`grep "pvrsrvkm$" /proc/devices | cut -b1,2,3`
    rm -f /dev/pvrsrvkm
    mknod /dev/pvrsrvkm c $pvr_maj 0
    chmod 666 /dev/pvrsrvkm

    # XXX  without this, pvrsrvinit causes a kernel oops
    case $CPUTYPE in
    "TIOMAP")
      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:0x00010205:5: -e s:0x00010201:3: -e s:0x00010003:2: | tail -n1 | awk -F': ' '{print $2}')"
      ;;
    "TI33XX")
      devmem2 0x44e01104 w 0x0 > /dev/null
      devmem2 0x44e00904 w 0x2 > /dev/null
      ES_REVISION="$(devmem2 0x56000014 | sed -e s:0x00010205:8: | tail -n1 | awk -F': ' '{print $2}')"
      ;;
    "TI81XX")
      devmem2 0x48180F04 w 0x0 > /dev/null
      devmem2 0x48180900 w 0x2 > /dev/null
      devmem2 0x48180920 w 0x2 > /dev/null
      ES_REVISION="$(devmem2 0x56000014 | sed -e s:0x00010205:6: -e s:0x00010201:3: -e s:0x00010003:2: | tail -n1 | awk -F': ' '{print $2}')"
      ;;
    esac

	  /usr/bin/pvrsrvctl --start --no-module
  ;;
  stop)
	  echo Stopping PVR
	  if [ ! -d /usr/local/XSGX ]; then
	      rmmod bufferclass_ti
	      rmmod omaplfb 2>/dev/null
	  fi
	  rmmod pvrsrvkm 2>/dev/null
	  rmmod drm 2>/dev/null
  ;;

  *)
    echo "$0 [start|stop]"
  ;;
esac
