#!/bin/sh
# (c) MandrakeSoft, Chmouel Boudjnah <chmouel@mandrakesoft.com>
# 	$Id: usb,v 1.21 2001/02/17 11:30:31 chmouel Exp $	
#
# usb	        This shell script takes care of starting and stopping
#               your usb devices.
#
# chkconfig: 2345 9 30
# description: This startup script try to load your modules for your usb devices.

if [ -f /etc/modules.conf ];then
    conf_file=/etc/modules.conf
elif [ -f /etc/conf.modules ];then
    conf_file=/etc/conf.modules
else
    exit 0
fi

if [ -f /etc/sysconfig/usb ];then
    source /etc/sysconfig/usb
fi

grep -iq nousb /proc/cmdline && exit 0

. /etc/rc.d/init.d/functions

if [ "$USB" = "no" ];then
    exit 0
fi

ARCH=$(uname -m)

function sed_usb_interface () {
    local module
    module=$1
    echo "alias usb-interface $module" >> $conf_file
    # shut your mounth, modprobe :\
    depmod -A
}

function probe_usb_interface () {
    local t pci_f uhci_t ohci_t

    pci_f=/proc/bus/pci/devices
    uhci_t="11063038 80862422 80867020 80867112 80867602 80862412"
    ohci_t="0e117020 0e11a0f8 10227404 1022740c 10330035 10397001 1045a0f8 1045c861 10950670 10950673 10b95237"

    # let's try with lspcidrake, he has everything we need and should
    # do thing better
    if [ -x /usr/bin/lspcidrake ];then
	t=$(lspcidrake|sed -n 's/.*USB.* \(.*\))$/\1/p')
	if [ -n "$t" ];then
	    sed_usb_interface $t
	    return 0;
	fi
    fi
    # Ok nothing let's try to detect it by ourself and pci id
    # aka: i have some time to lost in the airplane
    for i in $uhci_t;do
	if grep -q ".*$i.*" $pci_f;then
	    sed_usb_interface usb-uhci
	    return 0
	fi
    done
    for i in $ohci_t;do
	if grep -q ".*$i.*" $pci_f;then
	    sed_usb_interface usb-ohci
	    return 0
	fi
    done
    return 1;
}

function get_usb_interface () {
    if  ! grep -q "^alias usb-interface" $conf_file;then
	echo -n "Detecting USB interface    "
	if ! probe_usb_interface;then
	    echo_failure;
	    echo
	    exit 1
	else
	    echo_success
	    echo
	fi
    fi
}

function mount_proc_usb () {
    if grep -q usbdevfs  /proc/filesystems;then
	if ! grep -q /proc/bus/usb /proc/mounts;then
	    action "Mount USB filesystem" mount -t usbdevfs -o devmode=0664,devgid=43 /proc/bus/usb /proc/bus/usb
	fi
    fi
}


if grep -q "^alias usbmouse" $conf_file;then
    # Backward compatibility: No good sed it
    sed 's|^alias usbmouse|alias usb-interface|' < $conf_file > /tmp/.conf.modules.tmp
    mv -f /tmp/.conf.modules.tmp $conf_file
    # shut your mounth, modprobe :\
    depmod -A
fi

PKLVL=`sed 's/^\(.\).*/\1/' < /proc/sys/kernel/printk`

case $1 in 
    start)
	
	get_usb_interface;
	
	sysctl -w kernel.printk=0
	
	action "Loading USB interface"  /sbin/modprobe usb-interface
	mount_proc_usb
	
	# Static modules that we want to insert and not detected by usbd.
	if [ "$MOUSE" = "yes" ];then
	    action "Loading USB mouse" /sbin/modprobe usbmouse && /sbin/modprobe mousedev
	fi
	if [ "$KEYBOARD" = "yes" ];then
	    action "Loading USB keyboard" /sbin/modprobe usbkbd && /sbin/modprobe keybdev
	fi
	if [ "$STORAGE" = "yes" ];then
	    action "Loading USB storage"  /sbin/modprobe usb-storage
	fi
	if [ "$PRINTER" = "yes" ];then
	    action "Loading USB printer"  /sbin/modprobe printer
	fi
	if [ "$VISOR" = "yes" ];then
	    action "Loading USB visor"  /sbin/modprobe visor
	fi
	
	sysctl -w kernel.printk=$PKLVL
	
	touch /var/lock/subsys/usb
	
	;;
    stop)
	rm -f /var/lock/subsys/usb
	;;

    status)

	if fgrep -q usbcore /proc/modules;then
	    echo "USB Loaded."
	else
	    echo "USB not loaded."
	fi
	exit 0
	;;

    reload)
	;;
    restart)
	$0 stop
	$0 start
	;;
    *)
	echo "Usage: $(basename $0) start|stop|restart|status"
	exit 0
	;;
esac

exit 0
