#!/bin/bash
# $Id: ieee1394.agent 296 2004-10-01 12:39:26Z zoz $
#
# IEEE1394-specific hotplug policy agent.
#
# This should handle 2.4.10 (or later) IEEE1394 hotplugging, with a
# consistent framework for adding device and driver specific treatments.
#
# Kernel IEEE1394 params are:
#
#        ACTION=add or remove
#        VENDOR_ID=24 bit vendor id
#        GUID=64 bit globally unique id
#        SPECIFIER_ID=24 bit id of owner of specification
#        VERSION=version of specification
#
# See IEEE1212 for details on these parameters.
#

: ${ACTION?Bad invocation: \$ACTION is not set}
: ${DEVPATH?Bad invocation: \$DEVPATH is not set}
# : ${VENDOR_ID?Bad invocation: \$VENDOR_ID is not set}
# : ${GUID?Bad invocation: \$GUID is not set}
# : ${SPECIFIER_ID?Bad invocation: \$SPECIFIER_ID is not set}
# : ${VERSION?Bad invocation: \$VERSION is not set}

cd /etc/hotplug
. ./hotplug.functions

# generated by modutils 2.4.9 or later, for 2.4.10 and later kernels
MAP_CURRENT=$MODULE_DIR/modules.ieee1394map

# used for kernel drivers that don't show up in MAP_CURRENT
# or to overwrite default from MAP_CURRENT
MAP_HANDMAP=$HOTPLUG_DIR/ieee1394.handmap
    
device_vendor_id=$((0x$VENDOR_ID))
device_specifier_id=$((0x$SPECIFIER_ID))
device_version=$((0x$VERSION))

MATCH_VENDOR_ID=0x0001
MATCH_SPECIFIER_ID=0x0004
MATCH_VERSION=0x0008

if [ -e /sbin/hwscanqueue ]; then
    HWSCAN=/sbin/hwscanqueue
elif [ -e /usr/sbin/hwscanqueue ]; then
    HWSCAN=/usr/sbin/hwscanqueue
fi

# stdin is "modules.ieee1394map" syntax
# on return, all matching modules were added to $DRIVERS
#
ieee1394_map_modules ()
{
    local line module ignored

    while read line
    do
        case "$line" in
            \#*|"") continue ;;
        esac

        read module match_flags vendor_id model_id \
             specifier_id version < <(echo $line)
        : check match for $module
        match_flags=$(($match_flags))
        vendor_id=$(($vendor_id))
        model_id=$(($model_id))
        specifier_id=$(($specifier_id))
        version=$(($version))

        : vendor_id $vendor_id $device_vendor_id
        if [ $(($match_flags & $MATCH_VENDOR_ID)) -ne 0 ] &&
           [ "$vendor_id" -ne "$device_vendor_id" ]; then
            continue
        fi

        : specifier_id $specifier_id $device_specifier_id
        if [ $(($match_flags & $MATCH_SPECIFIER_ID)) -ne 0 ] &&
           [ "$specifier_id" -ne "$device_specifier_id" ]; then
            continue
        fi

        : version $version $device_version
        if [ $(($match_flags & $MATCH_VERSION)) -ne 0 ] &&
           [ "$version" != "$device_version" ]; then
            continue
        fi

        DRIVERS="$module $DRIVERS"
    done
}

#
# What to do with this IEEE1394 hotplug event?
#
case "$ACTION" in

    add)

        # If hwup does initialize the device there is nothing more to do
        if [ -x /sbin/hwup ] ; then
            /sbin/hwup  ${1:+$1-}devpath-$DEVPATH -o hotplug && exit
        fi

        LABEL="IEEE1394 product 0x$VENDOR_ID/0x$SPECIFIER_ID/0x$VERSION"
        FOUND=""

        # cope with special driver module configurations first. So we can
        # overwrite default settings in MAP_CURRENT provide missing information
        if [ -r $MAP_HANDMAP ]; then
            load_drivers ieee1394 $MAP_HANDMAP "$LABEL"
            FOUND="$FOUND${DRIVERS:+ $DRIVERS}"
        fi

        # on 2.4 systems, modutils maintains MAP_CURRENT
        if [ -r $MAP_CURRENT -a -z "$FOUND" ]; then
            load_drivers ieee1394 $MAP_CURRENT "$LABEL"
            FOUND="$FOUND${DRIVERS:+ $DRIVERS}"
        fi

	# update hardware database
	[ "$HWSCAN" ] && $HWSCAN --firewire

        if [ -z "$FOUND" ]; then
            mesg "... no drivers for $LABEL"
            exit 2
        fi
        ;;

    remove)

        # If hwdown shuts down the device there is nothing more to do
        if [ -x /sbin/hwdown ] ; then
            /sbin/hwdown  ${1:+$1-}devpath-$DEVPATH -o hotplug && exit
        fi

	# update hardware database
	[ "$HWSCAN" ] && $HWSCAN --firewire
        ;;

    *)

        debug_mesg "IEEE1394 '$ACTION' event not supported"
        exit 1
        ;;

esac
