#!/bin/sh
# by GO!

ISUSBHID=0
ISUSBDRV=0

if [ -f /proc/modules ]; then
  grep -q usbhid /proc/modules
  [ "$?" = "0" ] && ISUSBDRV=1
fi

if [ "$ISUSBDRV" = "0" ]; then
  for FILE in /sys/class/usb_device/usbdev*/device/*/bInterfaceClass ; do
    DIR=`dirname $FILE`
    CLASS="00"
    SUBCLASS="00"
    PROTCOL="00"
    pushd $DIR > /dev/null
    [ -f bInterfaceClass ] && CLASS=`cat bInterfaceClass`
    [ -f bInterfaceSubClass ] && SUBCLASS=`cat bInterfaceSubClass`
    [ -f bInterfaceProtocol ] && PROTCOL=`cat bInterfaceProtocol`
    if [ "$CLASS" = "03" ] && [ "$SUBCLASS" = "01" ]; then
      if [ "$PROTCOL" = "01" ] || [ "$PROTCOL" = "02" ]; then
        ISUSBHID=1
        popd > /dev/null
        break
      fi
    fi
    popd > /dev/null
  done
fi
if [ "$ISUSBDRV" = "0" ] && [ "$ISUSBHID" = "0" ]; then
  exit 0
fi
if [ "$ISUSBDRV" = "0" ] && [ "$ISUSBHID" = "1" ]; then
    modprobe usbhid
    usleep 200000
fi
if [ -f /proc/usbhidscan ]; then
  for i in 1 2 3 4 5 6 7 8 9 0; do
    COUNT=`cat /proc/usbhidscan`
    if [ "$COUNT" = "0" ]; then
      break
    fi
    usleep 500000
  done
fi
exit 1
