#!/bin/sh

HDPARM=/sbin/hdparm
MAX_CHANNELNR=1

# ioports for the first two IDE controllers
IOPORT_DATA_0=0x1f0
IOPORT_DATA_1=0x170

# The channel to be reset cannot be open when it is reset.
# Therefore we open the *other* channel in order to send the
# RESET or SCAN command.
DEV_TO_OPEN_0=/dev/hdc
DEV_TO_OPEN_1=/dev/hda

usage () {
	if [ $# -gt 0 ]; then
		echo $* >&2
		echo
	fi

	echo "usage: $0 ide-channel-nr [off|on|rescan]" 2>&1
	exit 1
}

CHANNELNR=$1
MODE=$2

do_register=0
do_unregister=0


if [ ! "$CHANNELNR" ] || [ $CHANNELNR -lt 0 ] || [ $CHANNELNR -gt $MAX_CHANNELNR ]; then
	usage "Unrecognized IDE channel number"
fi

case "$MODE" in
on )		do_register=1 ;;
off )		do_unregister=1 ;;
rescan )	do_unregister=1; do_register=1 ;;
* )			usage "Unrecognized command" ;;
esac

eval "DEV=\$DEV_TO_OPEN_$CHANNELNR"
eval "IOPORT_DATA=\$IOPORT_DATA_$CHANNELNR"
IOPORT_CTRL=0           # Will be filled in by the driver
IRQ=0                   # Will be filled in by the driver

[ $do_unregister -eq 1 ] && eval "$HDPARM -U $CHANNELNR $DEV > /dev/null"
[ $do_register   -eq 1 ] && eval "$HDPARM -R $IOPORT_DATA $IOPORT_CTRL $IRQ $DEV > /dev/null"

