#!/bin/sh
#
# pcmem 1.12 1996/07/24 16:32:40 (David Hinds)
#
# Initialize or shutdown a PCMCIA memory device using the "old" driver
#
# The first argument should be the action to be performed.  The second
# is the base name for the device.
#
# Three devices will be created:
#
#	/dev/{name}a	- character device, attribute memory
#	/dev/{name}b	- block device, common memory
#	/dev/{name}c	- character device, common memory
#
# The script passes an extended device address to 'pcmem.opts' in the 
# ADDRESS variable, to retrieve device-specific configuration options.
# The address format is "scheme,socket" where "scheme" is the PCMCIA
# configuration scheme and "socket" is the socket number.
#

usage()
{
    echo "usage: pcmem [action] [device name]"
    echo "  actions: start check stop suspend resume"
    exit 1
}

if [ $# -lt 2 ] ; then usage ; fi
ACTION=$1 ; DEVICE=$2

# Get device attributes
INFO=`fgrep "	$DEVICE	" /var/run/stab` || usage
set -- $INFO
SOCKET=$1 ; INSTANCE=$4 ; MAJOR=$6 ; MINOR=$7
if [ -s /var/run/pcmcia-scheme ] ; then
    SCHEME=`cat /var/run/pcmcia-scheme`
else
    SCHEME="default"
fi

# Load site-specific settings
ADDRESS="$SCHEME,$SOCKET"
. /etc/pcmcia/pcmem.opts

case "$ACTION" in

'start')
    rm -f /dev/${DEVICE}a /dev/${DEVICE}b /dev/${DEVICE}c
    mknod /dev/${DEVICE}c c $MAJOR $MINOR
    mknod /dev/${DEVICE}a c $MAJOR `expr $MINOR + 1`
    mknod /dev/${DEVICE}b b $MAJOR $MINOR
    ;;

'check')
    fuser -s /dev/${DEVICE}[ac] && exit 1
    fuser -s -m /dev/${DEVICE}b && exit 1
    ;;

'stop')
    fuser -s -k /dev/${DEVICE}[ac]
    test -b /dev/${DEVICE}b || exit 1
    fuser -s -k -m /dev/${DEVICE}b
    if mount | fgrep "/dev/${DEVICE}b on" ; then
	umount /dev/${DEVICE}b || exit 1
    fi
    rm -f /dev/${DEVICE}[abc]
    ;;

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0
