#!/bin/sh
#this is called from 'init', runs as parallel process.
#110711 return found usb drives.

modprobe usb-storage & #run as separate process
mount -t usbfs none /proc/bus/usb

#110425 note, if 'usbhid.ko' builtin to kernel, might not need this...
#v423 problem recent kernels: kernel configured to load hid-* extra drivers when
#usbhid loads, but it doesn't work. Most unfortunate. Note, older kernels have the
#hid-* (drivers for specific hardware, like Logitech wireless keyboard) builtin to
#the usbhid driver. Now that they are separated out, it is a headache. Try this...
/bin/hotplug2stdout_notimeout >/tmp/uevents.log &
PIDHOT=$!
modprobe usbhid #for a usb keyboard.

#101127 very troublesome... bump up to 3...
#v3.94 Classmate laptop, needs more delay here... no, further down...
sleep 3 #2 v403 bumped it up to 3. v412 try 1sec again.
USBSTORAGES=0 ; CNTUSB=0
while [ $USBSTORAGES -eq 0 ];do
 echo -n "." > /dev/console
 sleep 1
 CNTUSB=$(($CNTUSB+1))
 [ $CNTUSB -gt 25 ] && break
 #v412 bug, ubuntu kernel, got duplicate 'device found at 2', need 'sort -u'...
 USBSTORAGES=`/bin/dmesg | grep "usb-storage: device found at" | sort -u | wc -l | sed -e 's/ //g'`
 #if booting from usb, USBSTORAGES must be non-zero...
 [ "`echo "$PMEDIA" | grep 'usb'`" != "" ] && [ $USBSTORAGES -eq 0 ] && continue
 [ $USBSTORAGES -eq 0 ] && break
 AVAILABLEUSBSTORAGES=`/bin/dmesg | grep "usb-storage: device scan complete" | wc -l | sed -e 's/ //g'`
 [ $USBSTORAGES -ne $AVAILABLEUSBSTORAGES ] && USBSTORAGES=0
done
[ $USBSTORAGES -ne 0 ] && sleep 1 #v412 was needed for classmate. was 2, try 1sec.
 
#v423 load hid-* driver...
for ONEHID in `grep -o 'MODALIAS=hid:[^ ]*' /tmp/uevents.log | cut -f 2 -d '=' | tr '\n' ' '`
do
 modprobe $ONEHID
done
kill $PIDHOT

#101127 note about extra sleeps:
#testing quirky 1.4, booting from cd, save-file on usb (hd). i found even with initial 'sleep 3'
#still got CNTUSB=3. it shows up in /sys/block, but the partitions /sys/block/*/ and /proc/partitions
#take a very long time to show up... kernel: 2.6.31.14. i think more recent kernels have improved timing.

#101127 wait for usb partitions to become available...
CNTUSB2=$CNTUSB
SDDRVS="`ls -1 /sys/block | grep '^sd' | tr '\n' ' '`"
for ONEDRV in $SDDRVS
do
 #[ "`echo -n "$ATADRIVES" | grep "$ONEDRV"`" != "" ] && continue
 [ "`readlink /sys/block/${ONEDRV} | grep '/usb[0-9]/'`" = "" ] && continue #not usb.
 while [ ! -e /sys/block/${ONEDRV}/${ONEDRV}1 ];do
  echo -en "\\033[1;31m.\\033[0;39m" >/dev/console #red dot
  sleep 1
  CNTUSB2=$(($CNTUSB2+1))
  [ $CNTUSB2 -gt 15 ] && break
 done
 #force update of /proc/partitions...
 dd if=/dev/${ONEDRV} of=/dev/null bs=512 count=1 >/dev/null 2>&1
done
echo "USBSTORAGES=$USBSTORAGES AVAILABLEUSBSTORAGES=$AVAILABLEUSBSTORAGES CNTUSB=$CNTUSB CNTUSB2=$CNTUSB2" > /tmp/usb-drives-probe #101127 for debugging.

#110711 return found usb drives...
#touch /tmp/flag-usb-ready
ALLUSBDRVS="`find /sys/block -maxdepth 1 -name 'sd*' -o -name 'sr*' | xargs -l readlink 2>/dev/null | grep '/usb[0-9]' | rev | cut -f 1 -d '/' | rev | tr '\n' ' '`"
[ "$ALLUSBDRVS" = " " ] && ALLUSBDRVS=""
echo -n "$ALLUSBDRVS" > /tmp/flag-usb-ready

###end###
