# -*- shell-script -*-

# 27device_sysfs - Hardware database scanning routines and variables for sysfs.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2002, 2003, 2004, 2005

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 27device_sysfs,v 1.2 2005/02/09 06:02:36 martins Exp $

[ -n "$sysfs_dir" ] || return 0

######################################################################

list_devices_functions="sysfs_list_devices"

sysfs_list_devices ()
{
    # Sets: device_list

    local types="scsi ide"

    local t d

    for t in $types ; do
	for d in "${sysfs_dir}/bus/${t}/devices/"* ; do
	    local device_info="${t}/${d##*/}" # basename
	    device_list="${device_list} ${device_info}"
	done
    done
}

######################################################################

sysfs_add_device_hook_common ()
{
    # Uses: bus_info, adapter
    # Sets: device_bus_dir

    device_bus_dir=""

    local adapter_bus_dir
    add_device_hook_common || return 1

    # $adapter_bus_dir points to IDE channel or SCSI host adapter, so
    # strip off the first part (of the bus address... including the
    # bus_type) and use subdirectories instead of punctuation.
    local t="${bus_info#*[:.]}"
    device_bus_dir="${adapter_bus_dir}/${t//[:.]//}"
    bus_alias_set "$bus_info" "${device_bus_dir#${db_bus_dir}/}"

    device_backlink_adapter_bus_dir "$device_bus_dir" "$adapter_bus_dir"
}

add_device_hook_DEFAULT ()
{
    # Uses: bus_info, adapter
    # Sets: device_bus_dir, yl

    local type="$1"

    sysfs_add_device_hook_common || return 1

    device_set_yl "$type"
}

device_set_yl_DEFAULT ()
{
    # Uses: bus_info, device_bus_dir
    # Sets: yl
    yl=""

    local type="$1"

    local adapter_yl
    device_set_adapter_yl "$type" "$device_bus_dir"

    [ -n "$adapter_yl" ] && yl="${adapter_yl%/*}/${bus_info#*/}"
}

get_kernel_device_name_DEFAULT ()
{
    # Sets: device_name
    device_name=""

    local type="$1"
    local bus_addr="$2"
    shift 2

    [ -n "$1" ] || set -- "block"  # Default instance link to check.

    local d l dl

    d="${sysfs_dir}/bus/${type}/devices/${bus_addr}"

    for l in "$@" ; do
	dl="${d}/${l}"
	if [ -L "$dl" ] ; then
	    cd -P "$dl"
	    device_name="/dev/${PWD##*/}"  # basename
	    cd "$OLDPWD"
	    break
	fi
    done
}

