# -*- shell-script -*-

# 27device_scsi_sg - SCSI 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_scsi_sysfs,v 1.1 2004/02/18 04:31:25 martins Exp $

# This should work with Linux >= 2.6.2.
[ -n "$sysfs_dir" -a -d "${sysfs_dir}/class/scsi_generic" ] || return 0

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

function sysfs_fake_sg_map_x ()
{
    local d bn dl hbtl tf type devlink dev
    for d in /sys/class/scsi_generic/* ; do
	bn=$(basename "$d")
	dl="${d}/device"
	if [ -h "$dl" ] ; then
	    hbtl=$(basename $(readlink "$dl"))

	    tf="${dl}/type"
	    if [ -f "$tf" ] ; then
		read type <"$tf"

		echo -n "/dev/${bn}  ${hbtl//:/ }  ${type}"

		case "$type" in
		    # FIXME!  Other types needed.
		    (0) devlink="${dl}/block" ;;
		    (*) devlink=""            ;;
		esac
		if [ -h "$devlink" ] ; then
		    dev=$(basename $(readlink "$devlink"))
		    echo "  /dev/${dev}"
		else
		    echo
		fi
	    fi
	fi
    done
}


function device_setup_scsi ()
{
    sysfs_fake_sg_map_x >"${db_misc_dir}/sg_map-x"
}

function list_devices_scsi ()
{
    local d bn
    for d in /sys/class/scsi_generic/* ; do
	bn=$(basename "$d")
	echo "/dev/${bn}"
    done
}
