#!/bin/sh
#
# H3600 Sleeve hotplug policy agent for Linux 2.4 kernels
#
# Kernel NET hotplug params include:
#	
#	ACTION=%s [add or remove]
#	VENDOR_ID=%s
#	DEVICE_ID=%x
#	DEVICE_NAME=%s
#

. /etc/hotplug/hotplug.functions
#DEBUG=yes export DEBUG

if [ "$DEBUG" != "" ]; then
    mesg "Executing $0 action=$ACTION, device=$DEVICE_NAME ($VENDOR_ID,$DEVICE_ID)"
fi

COMPACTFLASH_TIMING_INCREMENT=0
PCMCIA_TIMING_INCREMENT=0
DUAL_PCMCIA_TIMING_INCREMENT=0
NAVMAN_TIMING_INCREMENT=0
BACKPAQ_TIMING_INCREMENT=-100

if [ -f /etc/sysconfig/hotplug ] ; then
    . /etc/sysconfig/hotplug
fi

case $ACTION in
add)
    case $DEVICE_NAME in 
    "Compaq Mercury Backpaq")
	mkdir -p /backpaq
	echo $BACKPAQ_TIMING_INCREMENT > /proc/sys/backpaq/pcmcia/timing_increment_ns
	## mount -t jffs2 /dev/mtdblock/4 /backpaq
	modprobe h3600_backpaq_fpga || mesg 'could not load backpaq fpga driver'
	modprobe h3600_backpaq_camera || mesg 'could not load backpaq camera driver'
	if [ -c /dev/v4l/video0 ] ; then
	    ln -s /dev/v4l/video0 /dev/video0
	    ln -s /dev/v4l/video0 /dev/video
	fi
	CAMERATYPE=`/bin/grep "CameraType" /proc/backpaq/camera | /bin/sed -e 's/.*: //'`
	BACKPAQ_NUMBER=`/bin/grep "Major revision" /proc/backpaq/eeprom | /bin/sed -e 's/.*0x//'`
	FPGA_NUMBER=`/bin/grep "FPGA version" /proc/backpaq/eeprom | /bin/sed -e 's/.*0x//'`
	CAMERA_NUMBER=`/bin/grep "Camera" /proc/backpaq/eeprom | /bin/sed -e 's/.*0x//'`
	
	echo "CAMERA_TYPE = $CAMERATYPE"
	echo "BACKPAQ_NUMBER = $BACKPAQ_NUMBER"
	echo "FPGA_NUMBER = $FPGA_NUMBER"
	echo "CAMERA_NUMBER = $CAMERA_NUMBER"
	echo "looking for file: /etc/fpga_B${BACKPAQ_NUMBER}_F${FPGA_NUMBER}_C${CAMERA_NUMBER}.bin"
	if [ -e /etc/fpga_B${BACKPAQ_NUMBER}_F${FPGA_NUMBER}_C${CAMERA_NUMBER}.bin ] ; then
		    cat /etc/fpga_B${BACKPAQ_NUMBER}_F${FPGA_NUMBER}_C${CAMERA_NUMBER}.bin > /dev/backpaq/fpga
	elif [ -e /etc/fpga_$CAMERATYPE.bin ] ; then
	    cat /etc/fpga_$CAMERATYPE.bin > /dev/backpaq/fpga
	elif [ -e /etc/fpga.bin ] ; then
	    cat /etc/fpga.bin > /dev/backpaq/fpga
	else
	    mesg 'unable to program the backpaq fpga'
	fi
	modprobe h3600_backpaq_accel || mesg 'could not load backpaq accelerometer driver'
	mesg 'BackPAQ drivers loaded'
	;;
    "Compaq CompactFlash Sleeve")
	mesg 'CompactFlash drivers loaded'
	echo $COMPACTFLASH_TIMING_INCREMENT > /proc/sys/bus/pcmcia/timing_increment_ns
	;;
    "Compaq PC Card Sleeve")
	mesg 'PC card sleeve drivers loaded'
	echo $PCMCIA_TIMING_INCREMENT > /proc/sys/bus/pcmcia/timing_increment_ns
	;;
    "Compaq Dual PC Card Sleeve")
	mesg 'Dual PC card sleeve drivers loaded'
	echo $DUAL_PCMCIA_TIMING_INCREMENT > /proc/sys/bus/pcmcia/timing_increment_ns
	;;
    "Navman GPS/CF Sleeve")
	mesg 'Navman GPS sleeve drivers loaded'
	echo $NAVMAN_TIMING_INCREMENT > /proc/sys/bus/pcmcia/timing_increment_ns
	start-stop-daemon --start --exec /usr/sbin/gpsd.bin -- -p /dev/tts/0 -s 57600
	;;
    *)
	mesg "$1 $ACTION event not handled for $DEVICE_NAME"
	;;
    esac
    ;;

remove)
    case $DEVICE_NAME in 
    "Compaq Mercury Backpaq")
	mesg 'Unloading BackPAQ drivers'
	## umount /backpaq
	rmmod h3600_backpaq_camera
	rmmod h3600_backpaq_accel
	rmmod h3600_backpaq_fpga
	;;
    "Compaq CompactFlash Sleeve")
	mesg 'Unloading CompactFlash sleeve'
	;;
    "Compaq PC Card Sleeve")
	mesg 'Unloading PC Card sleeve'
	;;
    "Compaq Dual PC Card Sleeve")
	mesg 'Unloading Dual PC Card sleeve'
	;;
    "Navman GPS/CF Sleeve")
	mesg 'Unloading Navman GPS sleeve'
	start-stop-daemon --stop --name gpsd.bin
	;;
    *)
	mesg $1 $ACTION event not handled for $DEVICE_NAME
	;;
    esac
    ;;

*)
    mesg "$1 $ACTION event not supported"
    exit 1 ;;

esac
