#!/bin/bash

LOG=/dev/hotplug_log

date=`date`
env=`env`

echo $SEQNUM $ACTION $SUBSYSTEM DEVPATH=$DEVPATH [$PHYSDEVBUS,$PHYSDEVDRIVER] [$MAJOR,$MINOR] PHYSDEVPATH=$PHYSDEVPATH > $LOG

if [ "$ACTION" = "add" ]; then

case $SUBSYSTEM in
	usb )
		[ -n "$PRODUCT" ] || exit
		
		logger -t hotplug Product: $PRODUCT Type: $TYPE Interface: $INTERFACE
		
		exit # disable wireless support for now

		case $PRODUCT in 
			846/6a00/* )
				echo Detected Netgear WG111v2 > $LOG
				echo "DRIVER=RTL8187" > /tmp/wireless.conf
				echo "ADAPTER=WG111V2" >> /tmp/wireless.conf ;;
			846/6100/* )
				echo Detected Netgear ???? > $LOG
				echo "DRIVER=RTL8187" > /tmp/wireless.conf
				echo "ADAPTER=????" >> /tmp/wireless.conf ;;
			bda/8187/* )
				echo Detected Realtec 8187 > $LOG
				echo "DRIVER=RTL8187" > /tmp/wireless.conf
				echo "ADAPTER=RTL8187" >> /tmp/wireless.conf ;;
			50d/705a/* )
				echo Detected Belkin F5D7050 > $LOG
				echo "DRIVER=RT2570" > /tmp/wireless.conf
				echo "ADAPTER=7050B" >> /tmp/wireless.conf ;;
			2001/3c00/* )
				echo Detected D-Link G122 B1 > $LOG
				echo "DRIVER=RT2570" > /tmp/wireless.conf
				echo "ADAPTER=G122B1" >> /tmp/wireless.conf ;;
		esac ;;
#				insmod crc-ccitt && insmod wlan && insmod islsm debug=65535 && insmod islsm_device && insmod islsm_usb ;;


	islsm )
		exit # disable wireless support for now
		[ -n "$MAJOR" -a -n "$MINOR" ] || exit

		DEV=/dev/islsm$MINOR
		rm -f $DEV
		mknod $DEV c $MAJOR $MINOR
		# Trigger driver firmaware load request ...
		islsmctrl --dev=$DEV --uploadfw ;;


	firmware )
		exit # disable wireless support for now
		[ -n "$PHYSDEVDRIVER" -a -n "$FIRMWARE" -a -n "$DEVPATH" ] || exit

		echo Firmware request from $PHYSDEVDRIVER for $FIRMWARE > $LOG

		case $PHYSDEVDRIVER in
			islusb )	
				#ls -lR /sys/class/firmware > $LOG
				CTRL=/sys/$DEVPATH/loading
				echo "1" > $CTRL
				FW=/lib/modules/2.13.17.0.arm
				if [ -f $FW ]; then
					cp $FW /sys/$DEVPATH/data
					echo "0" > $CTRL
					echo Firmware downloaded > $LOG
				else
					echo "-1" > $CTRL
					echo Firmware not found > $LOG
				fi ;;
		esac ;;
		
	usb_device )
		[ "$PHYSDEVBUS" = "usb" ] || exit
		pdp="/sys${PHYSDEVPATH}"
		[ -d $pdp ] || exit
		numif=`cat $pdp/bNumInterfaces`
		numif=`expr $numif`
		if [ $numif -ne 1 ]; then
			logger -t hotplug new USB device has unsupported number of interfaces: $numif
			exit
		fi
		desc=`cat $pdp/manufacturer $pdp/product $pdp/version`
		if0=$pdp/*:1.0
		if [ ! -d $if0 ]; then
			logger -t hotplug could not find interface directory $if0 for new USB device
			exit
		fi
		logger -t hotplug if0=$if0
		ifc=`cat $if0/bInterfaceClass`
		if [ "$ifc" != "08" ]; then
			logger -t hotplug new USB device has unsupported interface class: $ifc
			logger -t hotplug pdp=$pdp
			exit
		fi
		sc=`cat $if0/bInterfaceSubClass`
		if [ "$sc" != "06" ]; then
			logger -t hotplug new USB device has unsupported subclass: $sc
			exit
		fi
		touch /tmp/usb_mount_pending
		logger -t hotplug found USB stick $desc
		insmod usb-storage;;
		
	block )
		[ "$MINOR" -a "$MAJOR" ] || exit
		[ "$PHYSDEVBUS" = "scsi" -a "$PHYSDEVDRIVER" = "sd" ] || exit
		case $DEVPATH in
			/block/sdb/sdb[1-9] )
				dev=/dev${DEVPATH#/block/sdb} ;;
			/block/sdb )
				dev="/dev/sdb" ;;
			* )
				logger -t hotplug bad DEVPATH: $DEVPATH
				exit ;;
		esac
		logger -t hotplug USB stick on $dev
		rm -f $dev
		[ -d /usbstick ] || mkdir /usbstick
		mknod $dev b $MAJOR $MINOR
		x=0
		while [ $x -lt 4 ]; do
			mount -t vfat -r $dev /usbstick
			if [ $? -eq 0 ]; then
				logger -t hotplug $dev mounted on /usbstick
				break
			else
				logger -t hotplug $dev failed to mount on /usbstick
			fi
			x=`expr $x + 1`
			sleep 1
		done;;
														
esac

elif [ "$ACTION" = "remove" ]; then

case $SUBSYSTEM in
	block )
		[ "$PHYSDEVBUS" = "scsi" -a "$PHYSDEVDRIVER" = "sd" ] || exit
		umount /usbstick
		logger -t hotplug USB stick removed from $DEVPATH
		rmmod usb_storage;;
esac

fi
