#!/bin/sh
#
#pragma ident	"@(#)postpatch		1.1	04/01/13 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH

# Location of usb links, and the names of the types of links to remove
LINKDIR=${ROOTDIR}/dev/usb
LINKFILES="hub* mass-storage* device*"

# 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.  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_usba10_drivers() {
	not_installed usba10_hid || \
		add_drv -b ${ROOTDIR} -n -m '* 0666 root sys' usba10_hid || \
		EXIT=1 

	not_installed usba10_hubd ||
		add_drv -b ${ROOTDIR} -n -m '* 0644 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_usb_ac || \
		add_drv -b ${ROOTDIR} -n -m '* 0600 root sys' usba10_usb_ac || \
		EXIT=1 

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

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

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


# Host controller drivers
install_host_controller_drivers() {

	not_installed usba10_ohci || add_drv -b ${ROOTDIR} \
	 	-m '* 0644 root sys' \
		-i '"pciclass,0c0310"' -n usba10_ohci || EXIT=1

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

# The following is for the Serial Port driver.
# Different serial port drivers are installed for sparc and X86:
#   sparc: USBA 1.0 and USBA 0.0 both support usb serial driver on ohci
#   x86: USBA 1.0 supports usb serial driver on ohci.  No support on uhci
# All use the following aliases to bind to USB serial port devices.
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\" \
	"

# Install the new framework drivers
install_usba10_drivers

# Install host controller drivers
install_host_controller_drivers

# Cleanup to wipe out any defunct links

if [ -d ${LINKDIR} ]
then
	# ls -l shows linknames to curr dir and phys nodes relative to $LINKDIR
	OLDDIR=`pwd`
	cd ${LINKDIR}

	# List all entries with devctl (will be entries pointing to phys nodes
	# ending with "devctl"). Delete these entries & their physical nodes.
	ls -l ${LINKFILES} 2>/dev/null | grep ":devctl$" | \
	    nawk '{print $9, $11}' | xargs rm -f

	cd ${OLDDIR}
fi

exit $EXIT
