# -*- shell-script -*-

# 33adapter_ide_dt_nosysfs - Device-tree only IDE adapter code.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 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: 33adapter_ide_dt_nosysfs,v 1.4 2004/11/25 05:10:34 martins Exp $

#pci_ide_map=$(type -p pci_ide_map)

#[ -d "/proc/ide" -a -n "$pci_ide_map" -a \
[ -f "/proc/ide/drivers" -a \
    -n "$source_device_tree" -a -z "$sysfs_dir" ] || \
    return 0

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

dt_list_adapters_setup_ide ()
{
    ensure_directory "$db_bus_dt_dir"

    local i
    for i in /proc/ide/ide[0-9]* ; do
	[ -d "$i" ] || continue

	local x bus devfun
	read x x bus x devfun x <"${i}/config"
    
	if [ -n "$bus" -a -n "$devfun" ] ; then
            # FIXME (domain)!
	    local pci_addr=$(printf "%04x:%02x:%02x.%x" \
		0 0x$bus $((0x$devfun >> 3)) $((0x$devfun & 0x7)))

	    echo "${pci_addr}" "${i##*/}"  # basename
	fi
    done > "${db_bus_dt_dir}/lsvpd,pci-ide-map"
}

dt_adapter_crosslink_ide ()
{
    local bus_info="$1"
    local node="$2"

    local bus_addr="${bus_info#*/}"

    case "$bus_info" in
	pci/*)
	    local names pci_addr name
	    while read pci_addr name ; do
		[ "$pci_addr" = "$bus_addr" ] && \
		    names="${names} ${name}"
	    done <"${db_bus_dt_dir}/lsvpd,pci-ide-map"
	    names="${names# }"

	    # If no spaces in $names, just 1, so no ambiguity.
	    [ -n "${names}" -a "${names}" = "${names% *}" ] && \
		add_adapter_crosslink "ide" "$bus_info" "${names}${ch}"
	    ;;
    esac
}
