#! /usr/bin/ksh
#
# @(#)findPCIDevices.sh	1.7	98/08/20 SMI
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#
# findPCIDevices $name $number $loc
#
# Purpose:      To read the device classes out of the FRU EEPROM and assign
#               Device instances to represent the Devices realised by the FRU.
# Result:       The PCI constituent and device _properties _device_name and _function_no
#		attributes will be set and a space-separated string of 
#		device instances is returned.
#
#		Any problems encountered when probing the FRU EEPROM cause
#		the script to exit and produce no output. The caller (i.e.
#		the cmsdef) should then perform no further configuration
#		operations.
#
# Mandatory Parameters
# $name                 - class name of FRU
# $number               - instance number of FRU
# $location             - location of FRU

if [[ $# -ne 3 ]]
then
	cmsdebug "findPCIDevices: malformed command line: $*"
	exit 1
fi

name=$1
number=$2
loc=$3

((inst_start=$number*4))
dev_string=""

for i in 0 1 2 3
do
    device="$(cmsfruinfo -s -l $loc EE_PCI_DEVDATA_${i}_DEVNAME)" || exit 2

    if [[ -n "$device" ]]
    then
	((instance=$inst_start+$i))

	case "$device" in
	"glm")	device="$device $instance"
		device_name="glm"
		;;
	"hme")	device="$device $instance"
		device_name="hme"
		;;
	"hsip") device="pcidef $instance"
		device_name="pci1214,334"
		;;
	"saip")	device="pcidef $instance"
		device_name="pci114f,1c"
		;;
	*)	device_name=$device
		device="pcidef $instance"
		if [[ -z "$device_name" ]] then device_name="string"; fi
		;;
	esac
	cmsreport $name $number slot$i "$device"
	cmsreport $device _device_name $device_name

	properties=$(cmsfruinfo -s -l $loc EE_PCI_DEVDATA_${i}_PROPS)
	if [[ $? -ne 0 ]] then exit 2; fi
	echo $properties > $CMSHOME/.config/.eeprom.$name.$number.$i
	cmsreport $device _properties $CMSHOME/.config/.eeprom.$name.$number.$i

	function_no=$(cmsfruinfo -s -l $loc EE_PCI_DEVDATA_${i}_FUNCTION_NO)
	if [[ $? -ne 0 ]] then exit 2; fi
	if [[ -z "$function_no" ]] then function_no="string"; fi
	cmsreport $device _function_no $function_no

	if [[ -z "$dev_string" ]]
	then	dev_string="$device"
	else	dev_string="$dev_string $device"
	fi
    fi
done
echo $dev_string
