# -*- shell-script -*-

# 37device_dt - Hardware database scanning routines and variables for
#               systems with device-tree, with or without ibm,vpd properties.

# 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: 37device_dt,v 1.4 2005/05/19 07:32:18 martins Exp $

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

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

add_device_hook_scsi ()
{
    # Uses: bus_info, device_adapter_name, device_type
    # Sets: device_bus_dir, yl

    local adapter_bus_dir

    if ! add_device_hook_common ; then
	: "add_device_hook_scsi: no device-tree node for $device_adapter_name"
	return 1
    fi

    local host bus target lun
    scsi_parse_addr_hook "${bus_info#*/}"
    
    # Check for multi-channel card.
    local bus_hex=$(dec2hex "$bus")

    local i maybe
    for i in "scsi" "k2-sata" ; do
	maybe="${adapter_bus_dir}/${i}@${bus_hex}"
	if [ -d "$maybe" ] ; then
	    adapter_bus_dir="$maybe"
	    break
	fi
    done
    
    local dt_type
    case "$device_type" in
	enclosure) dt_type="Enclosure"    ;;
	*)         dt_type="$device_type" ;;
    esac

    local t=$(dec2hex "$target")
    device_bus_dir="${adapter_bus_dir}/${dt_type}@${t}"
    if [ $lun -ne 0 ] ; then
	t=$(dec2hex "$lun")
	device_bus_dir="${device_bus_dir},${t}"
    fi

    bus_alias_set "$bus_info" "${device_bus_dir#${db_bus_dir}/}"

    device_backlink_adapter_bus_dir "$device_bus_dir" "$adapter_bus_dir"

    device_set_yl "${bus_info%/*}"
}

add_device_hook_ide ()
{
    # Uses: bus_info, device_adapter_name, device_type
    # Sets: device_bus_dir, yl

    local adapter_bus_dir

    if ! add_device_hook_common ; then
	: "add_device_hook_ide: no device-tree node for $device_adapter_name"
	return 1
    fi

    local dt_type
    case "$device_type" in
	cdrom) dt_type="disk"         ;;
	*)     dt_type="$device_type" ;;
    esac

    # FIXME: This should work in most cases.
    local number
    dt_ide_set_number "$bus_info"
    local t=$(dec2hex "$number")  # Use "number" part.
    device_bus_dir="${adapter_bus_dir}/${dt_type}@${t}"

    bus_alias_set "$bus_info" "${device_bus_dir#${db_bus_dir}/}"

    device_backlink_adapter_bus_dir "$device_bus_dir" "$adapter_bus_dir"

    device_set_yl "${bus_info%/*}"
}

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

dt_ide_set_number ()
{
    # Sets: number

    local bus_info="$1"

    # FIXME: This should work in most cases.
    local cu="${bus_info#*/}"
    local channel="${cu%.*}"
    local unit="${cu#*.}"
    number=$(( $channel % 2 * 2 + $unit ))
}
