# -*- shell-script -*-

# 070device_minimal - Device scanning routines and variables.

# This file is part of the Linux lsvpd package.

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

# 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: 070device,v 1.3 2005/02/09 05:22:18 martins Exp $

# Default and common functions.

# This module is always loaded.
true || return 0

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

make_multiplexed device_set_yl

make_multiplexed add_device
make_multiplexed add_device_hook
make_multiplexed remove_device_hook
make_multiplexed get_kernel_device_name

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

list_devices_functions=""

list_devices ()
{
    # Sets: device_list
    device_list=""

    local f
    for f in $list_devices_functions ; do
	$f
    done
}

add_device_hooks="add_device_hook"

add_device_hooks ()
{
    type="$1"

    for i in $add_device_hooks ; do
	$i "$type" || return 1
    done
}

add_device_hook_common ()
{
    # Uses: bus_info, adapter
    # Sets: adapter_bus_dir

    local adapter_subdir
    adapter_set_adapter_subdir "${bus_info%%/*}"
    local f="${db_os_dir}/${adapter_subdir}/${adapter}/bus-node"
    adapter_bus_dir=""
    if [ -L "$f" ] ; then
	if cd -P "$f" ; then
	    adapter_bus_dir="$PWD"
	    cd "$OLDPWD"
	    return 0
	fi
    fi

    return 1
}

device_add_name_and_crosslink ()
{
    local type="$1"
    local bus_addr="$2"
    local name="$3"

    local bus_alias
    bus_alias_get "${type}/${bus_addr}"
    device_add_name_and_crosslink_basic "$bus_alias" "$name"
}

device_add_name_and_crosslink_basic ()
{
    local device_bus_dir="$1"
    local name="$2"

    [ -n "$device_bus_dir" ] || return

    local l1="${device_bus_dir}/lsvpd,adapter-bus-node"
    local l2="${l1}/lsvpd,os-node"

    if [ -L "$l1" -a -L "$l2" ] ; then
	cd -P "$l2"
	local adapter_os_dir="$PWD"
	cd "$OLDPWD"

	local device_os_dir="${adapter_os_dir}/${name#/dev/}"

    	cross_link "$device_bus_dir" "$device_os_dir"
    fi

    local vpd_dir
    vpd_dir_set_hook "$device_bus_dir"
    vpd_field_ensure "$vpd_dir" "AX" "$name"
}

get_device_name ()
{
    # Sets: device_name

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

    get_kernel_device_name "$type" "$bus_addr"
}

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

device_set_yl_DEFAULT ()
{
    # Sets: yl

    yl=""
}

device_set_adapter_yl ()
{
    # Sets: adapter_yl
    adapter_yl=""

    local type="$1"
    local device_bus_dir="$2"

    local yl
    local l="${device_bus_dir}/lsvpd,adapter-bus-node"

    if [ -L "$l" ] ; then
	cd -P "$l"
	local adapter_bus_dir="$PWD"
	cd "$OLDPWD"

	adapter_set_yl "$type"
	adapter_yl="$yl"
    fi
}

device_backlink_adapter_bus_dir ()
{
    local device_bus_dir="$1"
    local adapter_bus_dir="$2"

    local l="${device_bus_dir}/lsvpd,adapter-bus-node"
    if set_dot_dots_hook "$device_bus_dir" "$adapter_bus_dir" ; then
	rm -f "$l"
	ensure_directory "$device_bus_dir"
	ln -s "$dot_dots" "$l"
    fi
}

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

scsi_parse_addr_hook ()
{
    local scsi_addr="$1" # host:bus:target:lun

    # Sets: host, bus, target, lun

    local bustarget="${scsi_addr#*:}"
    bustarget="${bustarget%:*}"

    host="${scsi_addr%%:*}"
    bus="${bustarget%:*}"
    target="${bustarget#*:}"
    lun="${scsi_addr##*:}"
}

