#!/bin/sh

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH
ARCH=`uname -p`
export ARCH

# since ohci and uhci are self probing nexus drivers, add_drv -n hid, hubd and
# usb_mid before add_drv ohci/uhci.
# ohci/uhci will create the hub and usb,device nodes and attach
# hubd and usb_mid drivers

not_installed() {
	driver=$1
	grep "^${driver} " $ROOTDIR/etc/name_to_major > /dev/null 2>&1
	return $?
}

EXIT=0
# The following is are the usba1.0 framework drivers and modules.  Drivers are
# force-bound by the framework, to their classes as defined above.  (Example:
# usba10_usbprn will forcebind to usbif,class7.1.1 and usbif,classs7.1.2 if
# found on a USB port to which usba10_ohci or usba10_ehci has control.)  Since
# drivers are forcebound, no alias (-i) is needed for them.
install_usb10_drivers() {
	not_installed usba10_hid ||  add_drv -b ${ROOTDIR} \
		-n -i '"usbif,class3"' -m '* 0666 root sys' usba10_hid || \
		EXIT=1 

	not_installed usba10_hubd || \
		add_drv -b ${ROOTDIR} -n -m '* 0666 root sys' usba10_hubd || \
		EXIT=1 

	not_installed usba10_scsa2usb || \
		add_drv -b ${ROOTDIR} -n usba10_scsa2usb || EXIT=1 

	not_installed usba10_usb_mid || \
		add_drv -b ${ROOTDIR} -n usba10_usb_mid || EXIT=1 

	not_installed usba10_usbprn || \
		add_drv -b ${ROOTDIR} -n -m '* 0666 root sys' usba10_usbprn || \
		EXIT=1 

	not_installed usba10_ugen || \
		add_drv -b ${ROOTDIR} -n -m '* 0666 root sys' usba10_ugen || \
		EXIT=1 

	not_installed usba10_usbser_edge || \
		add_drv -b ${ROOTDIR} -n -m '* 0666 root sys' \
		usba10_usbser_edge ||  EXIT=1

	# Host controller drivers

	not_installed usba10_ohci || \
		add_drv -b ${ROOTDIR} -i '"pci1033,35"' -n usba10_ohci || \
		EXIT=1

	not_installed usba10_ehci || \
		add_drv -b ${ROOTDIR} -i '"pciclass,0c0320"' -n usba10_ehci || \
		EXIT=1

}

if [ -f $ROOTDIR/kernel/drv/ohci ]; then
	$ROOTDIR/usr/sbin/rem_drv -b $ROOTDIR ohci
	$ROOTDIR/usr/sbin/add_drv -b ${ROOTDIR} -i '"pciclass,0c0310"' ohci || \
		exit 1
fi

if [ -f $ROOTDIR/kernel/drv/hid ]; then
	$ROOTDIR/usr/sbin/rem_drv -b $ROOTDIR hid
	$ROOTDIR/usr/sbin/add_drv -b ${ROOTDIR} -m '* 0666 root sys' \
		-i '"usbif,class3.1"' -n hid || exit 1
fi

# Install old framework client drivers

not_installed hid || add_drv -b ${ROOTDIR} -m '* 0666 root sys' \
	-i '"usbif,class3.1"' -n hid || EXIT=1

not_installed hubd || add_drv -b ${ROOTDIR} -m '* 0666 root sys' \
	-i '"usbif,class9"' -n hubd || EXIT=1

not_installed scsa2usb || add_drv -b ${ROOTDIR} \
	-i '"usbif,class8"' -n scsa2usb || EXIT=1

not_installed usb_mid || add_drv -b ${ROOTDIR} \
	-i '"usb,device"' -n usb_mid || EXIT=1

not_installed usbprn || add_drv -b ${ROOTDIR}  -m '* 0666 root sys' \
	-i '"usbif,class7.1.1" "usbif,class7.1.2"' -n usbprn || EXIT=1 


case ${ARCH} in
	i386)
		not_installed uhci || \
		add_drv -b ${ROOTDIR} -i '"pciclass,0c0300"' -n uhci || EXIT=1
	;;
	sparc)
		not_installed ohci || \
		add_drv -b ${ROOTDIR} -i '"pciclass,0c0310"' -n ohci || \ EXIT=1

		not_installed usb_sd || \
		add_drv -b ${ROOTDIR} -n usb_sd || EXIT=1 

		# The following is the  Serial Port driver.
		USBSER_EDGE_ALIASES="\
			\"usbif1608,1.config1.0\" \
			\"usbif1608,3.config1.0\" \
			\"usbif1608,4.config1.0\" \
			\"usbif1608,5.config1.0\" \
			\"usbif1608,6.config1.0\" \
			\"usbif1608,7.config1.0\" \
			\"usbif1608,c.config1.0\" \
			\"usbif1608,d.config1.0\" \
			\"usbif1608,e.config1.0\" \
			\"usbif1608,f.config1.0\" \
			\"usbif1608,10.config1.0\" \
			\"usbif1608,11.config1.0\" \
			\"usbif1608,12.config1.0\" \
			\"usbif1608,13.config1.0\" \
			\"usbif1608,14.config1.0\" \
			"

		not_installed usbser_edge || \
		add_drv -b ${ROOTDIR} -m '* 0666 root sys' \
			-i "${USBSER_EDGE_ALIASES}" -n usbser_edge || EXIT=1

		# Install the new framework drivers
		install_usb10_drivers
	;;
esac

exit $EXIT
