# -*- shell-script -*-

# 07device_ide_proc - IDE device 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: 07device_ide_proc,v 1.6 2004/01/19 10:33:31 martins Exp $

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

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

function list_devices_ide ()
{
    local i
    for i in /proc/ide/ide*/* ; do
	if [ -d "$i" ] ; then
	    local bn=$(basename "$i")
	    local dev="/dev/${bn}"
	    [ -b "$dev" ] && echo "$dev"
	fi
    done
}

function do_device_ide ()
{
    local dev="$1"

    local name=$(basename "$dev")

    local d="/proc/ide/${name}"

    if [ -d "$d" ] ; then
	local if="${d}/identify"
	local tf="${d}/media"
	local sf="${d}/settings"
	local os_prefix=$(get_adapter_os_prefix "ide")

	if [ -f "$if" -a -f "$tf" -a -f "$sf" ] ; then

	    declare -i number channel unit
	    local adapter device_type
	    number=$(sed -n -e 's/^number *\([0-9]*\) *.*$/\1/p' <"$sf")
	    channel=number/2
	    unit=number%2
	    adapter=$(dirname $(readlink "$d"))
	    read device_type <"$tf"

	    # Hooks sets these variables.
	    local vpdprop yl
	    do_device_hook "ide"

	    if [ -n "$vpdprop" ] ; then
	        # This "inefficiency" is intentional - it loses the newlines.
		local bytes=$(cat "$if")
		bytes=$(echo $bytes | sed -e 's/ //g')

		# Extract interesting fields.
		# Fields documented in /usr/include/linux/hdreg.h.
		local sn=$(hex2substring "$bytes" 20 20)
		local rm=$(hex2substring "$bytes" 46  8)
		local tm=$(hex2substring "$bytes" 54 40)
	    
		local mf="UNKNOWN"
		case "$tm" in
		    IBM*)      mf="IBM"            ;;
		    QUANTUM*)  mf="Quantum"        ;;
		    ST*)       mf="Seagate"        ;;
		    MATSHITA*) mf="Matshita"       ;;
		    HL-DT-ST*) mf="LG Electronics" ;;
		    TOSHIBA*)  mf="Toshiba"        ;;
		    LTN*)      mf="Lite-On"        ;;
		    AOpen*)    mf="AOpen"          ;;
		    RICOH*)    mf="Ricoh"          ;;
		esac
		
		local ds
		case "$device_type" in
		    disk)  ds="IDE Disk Drive"     ;;
		    cdrom) ds="IDE CD-ROM Drive"   ;;
		    *)     ds="IDE Unknown Device" ;;
		esac
		
		{
		    do_fc_field $(dirname "$vpdprop")
		    echo "*DS ${ds}"
		    echo "*AX ${dev}"
		    echo "*MF ${mf}"
		    [ -n "$tm" ] && echo "*TM ${tm}"
		    [ -n "$sn" ] && echo "*SN ${sn}"
		    [ -n "$rm" ] && echo "*RM ${rm}"
		    [ -n "$yl" ] && echo "*YL ${yl}"
		} >"$vpdprop"
	    fi
	fi
    fi
}
