# -*- shell-script -*-

# 03adapter_ide_proc - IDE adapter support using /proc/ide.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 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: 03adapter_ide_proc,v 1.14 2004/05/24 04:49:35 martins Exp $

[ -d "/proc/ide/ide0" ] || return 0

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

# unset -f this function if name lookups can be done dynamically.
adapter_setup_ide ()
{
    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)))

	    do_adapter_crosslink "ide" "pci/${pci_addr}" "${i##*/}" # basename
	fi
    done
}

do_adapter_ide ()
{
    local bus_info="$1"

    # FIXME: Probably shouldn't do this here!
    local ax osname
    adapter_set_ax_osname_hook "ide" "$bus_info"

    local node=$(get_adapter_bus_dir "ide" "$bus_info")

    local d="/proc/ide/${ax}"
    local f="${d}/config"

    if [ -f "$f" ] ; then
	local x vendor device
	read x x x x x x vendor x device x <"$f"
    
	if [ -n "$vendor" -a -n "$device" ] ; then
	    local ds=$(get_adapter_ds "ide")

	    local subvendor subdevice
	    pci_set_mf_tm_hook

	    local channel
	    local cf="${d}/channel"
	    [ -f "$cf" ] && read channel <"$cf"

	    # Hook sets these variables.
	    local yl
	    do_adapter_hook "ide"

	    # Explicitly unset some variables.
	    local cd rl rm sn

	    vpd_create_hook "$node"

	    adapter_extra_vpd_hook "$type" "$bus_info" "$node"
	fi
    else
	do_adapter_DEFAULT "ide" "$node"
    fi
}
