# -*- shell-script -*-

# 27device_ide_sysfs - IDE device support using sysfs.

# 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: 27device_ide_sysfs,v 1.4 2004/10/21 05:36:29 martins Exp $

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

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

list_devices_ide ()
{
    local d

    for d in "${sysfs_dir}/bus/ide/devices/"* ; do
	echo "${d##*/}" # basename = channel.unit
    done
}

unset -f device_setup_ide

# This gets the "kernel name", ignoring any userspace naming.
get_kernel_device_name_ide ()
{
    local cu="$1"  # channel.unit

    local d l t name

    d="${sysfs_dir}/bus/ide/devices/${cu}"

    l="${d}/block"
    if [ -L "$l" ] ; then
	t=$(readlink "$l")
	name="${t##*/}" # basename
	[ -n "$name" ] && echo "/dev/${name}"
    fi
}

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

    local adapter_bus_dir yl_prefix

    sysfs_add_device_hook_common || return 1

    yl="${adapter_bus_dir%/*}" # dirname
    yl="${yl##*/}"             # basename
    yl="${yl_prefix}${yl}/${bus_info#*/}"
}

sysfs_add_device_bus_alias_ide ()
{
    # Uses: bus_info

    local cu="${bus_info##*/}" # basename
    local d="${sysfs_dir}/bus/ide/devices/${cu}"

    local bl="${d}/block"
    if [ -L "$bl" ] ; then
        # pseudo_realpath won't work here due to symlink madness.
	local sysfs_node=$(cd "$bl" ; pwd -P) # realpath
	bus_alias_set "${sysfs_node#/}" "${device_bus_dir#${db_bus_dir}/}"
    fi
}

remove_device_hook_ide ()
{
    # Uses: device_bus_dir
    # Sets: bus_info

    local dn="${device_bus_dir%/*}" # dirname
    bus_info="ide/${dn##*/}.${device_bus_dir##*/}" # basename x 2
}

