#!/bin/sh
# vim: syntax=sh
#
# pci.rc	mostly to recover lost boot-time pci hotplug events
#
# $Id: pci.rc,v 1.7 2003/06/06 18:27:23 dbrownell Exp $
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin

cd /etc/hotplug

# source function library
if [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
fi

. hotplug.functions


# # override any of the defaults?
# if [ -f /etc/sysconfig/pci ]; then
#     . /etc/sysconfig/pci
# fi

pci_boot_events ()
{
    #
    # FIXME on 2.5, /sys/bus/pci/devices gives some of this
    # information, and more can be gotten with 'lspci'.
    # don't expect pcimodules to exist!!
    #
    LISTER=`type -p pcimodules`
    if [ "$LISTER" = "" -o ! -f /proc/bus/pci/devices -o ! -x pci.agent ]; then
	    echo $"** can't synthesize pci hotplug events"
	    return
    fi

    # make sure the pci agent will run
    ACTION=add
    PCI_CLASS=0
    PCI_ID=0:0
    PCI_SLOT=0:0.0
    PCI_SUBSYS_ID=0:0
    export ACTION PCI_CLASS PCI_ID PCI_SLOT PCI_SUBSYS_ID

    # these notifications will be handled by pcimodules
    for BUS in `cd /proc/bus/pci;find * -type d -print`; do
	for SLOT_FUNC in `cd /proc/bus/pci/$BUS; echo *`; do
	    PCI_SLOT=$BUS:$SLOT_FUNC
	    : hotplug pci for $PCI_SLOT
	    /sbin/hotplug pci
	done
    done
}

# See how we were called.
case "$1" in
  start)
	pci_boot_events
        ;;
  stop)
	# echo $"pci stop -- ignored"
        ;;
  status)
	echo $"PCI Status for kernel: "  `uname -srm`
	echo ''

	if [ -f /proc/bus/pci/devices ]; then
	    COUNT=`ls /proc/bus/pci | wc -l`
	    if [ $COUNT -gt 1 ]; then
		COUNT=`expr $COUNT - 1`
		echo $"PCI up; bus count is $COUNT"
		if [ -x /sbin/lspci ]; then
		    /sbin/lspci
		fi
	    else
		echo $"no PCI busses?"
	    fi
	    echo ''
	else
	    echo $"no PCI /proc support?"
	fi
	echo ''

	;;
  restart)
	# always invoke by absolute path, else PATH=$PATH:
	$0 stop && $0 start
	;;
  *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
esac
