#!/bin/sh
#
# serial 1.35 2000/06/06 21:02:09 (David Hinds)
#
# Initialize or shutdown a PCMCIA serial device
#
# The first argument should be either 'start' or 'stop'.  The second
# argument is the base name for the device.
#
# The script passes an extended device address to 'serial.opts' in the 
# ADDRESS variable, to retrieve device-specific configuration options.
# The address format is "scheme,socket,instance" where "scheme" is the
# PCMCIA configuration scheme, "socket" is the socket number, and
# "instance" is used to number multiple ports on a single card.  
#

if [ -r ./shared ] ; then . ./shared ; else . /etc/pcmcia/shared ; fi

UART_CONF="/etc/bluetooth/bt-uart.conf"
ID=""
UART_SET=""
SERIAL_SET=""
if [ -f $UART_CONF ]; then
    ID=`/sbin/cardctl ident | grep info: | sed 's/^ *product info: *//' | sed 's/"/\\"/g'`
    UART_SET=`grep "$ID" $UART_CONF | cut -f2 -d":"`
    SERIAL_SET=`grep "$ID" $UART_CONF | cut -f3 -d":"`
fi

NO_CHECK="true"

# Get device attributes
get_info $DEVICE

# Load site-specific settings
ADDRESS="$SCHEME,$SOCKET,$INSTANCE"
. $0.opts

# Newer kernels deprecate use of "cua" devices, but we need to
# keep track of them anyway, if the device files are present
NR=`expr $MINOR - 64`
if [ -c /dev/cua1 ] ; then
    CALLOUT=cua$NR
else
    CALLOUT=$DEVICE
fi

case "$ACTION" in

'start')
    if [ ! -c /dev/$DEVICE ] ; then
	if [ -x /sbin/MAKEDEV ]; then
	    cd /dev ; /sbin/MAKEDEV $DEVICE
	fi
    fi
    # Workaround for serial driver bug
    IRQ=`setserial /dev/$DEVICE | sed -e 's/.*IRQ: //'`
    setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ
    if [ "$SERIAL_SET" != "" ] ; then
	/sbin/setserial $SERIAL_SET
    else
	setserial /dev/$DEVICE $SERIAL_OPTS
    fi
    if [ "$SERIAL_SET" = "" -a "$UART_SET" = "" ]; then
	if [ "$LINK" ] ; then
	    mv -f $LINK $LINK.O 2>/dev/null
	    if uname -r | grep -q '^2\.[2345]' ; then
    		ln -s /dev/$DEVICE $LINK
	    else
    		ln -s /dev/$CALLOUT $LINK
	    fi
	fi
	if [ "$INITTAB" ] ; then
	    echo "S$NR:12345:respawn:$INITTAB $DEVICE" >> /etc/inittab
	    telinit q
	fi
    fi
    #Bind Bluetooth CSR UART based CF cards if they are defined in $UART_CONF
    if [ "$UART_SET" != "" ]; then
	modprobe hci_uart > /dev/null 2>&1
	/sbin/hciattach $UART_SET
	hciconfig hci0 up
	if [ -f /etc/bluetooth/rfcomm.conf ]; then
	    rfcomm release all
	    /bin/rfcomm -f /etc/bluetooth/rfcomm.conf bind all
	fi
    fi
    ;;

'check')
    if [ "$UART_SET" != "" ] ; then 
	exit 0 
    fi
    is_true $NO_CHECK && exit 0
    if [ "$INITTAB" ] ; then
    do_fuser -v /dev/$DEVICE /dev/$CALLOUT $LINK | grep -v getty \
        | grep -q /dev/$DEVICE && exit 1
    else
    do_fuser -s /dev/$DEVICE /dev/$CALLOUT $LINK && exit 1
    fi
    ;;

'cksum')
    chk_simple "$NEW_SCHEME,$SOCKET,$INSTANCE" || exit 1
    ;;
    
'stop')
    #Shutdown Bluetooth CSR UART based CF cards if they are defined in $UART_CONF
    if [ "$UART_SET" != "" ]; then
	rfcomm release all
	hciconfig hci0 down
	killall hciattach
	sleep 1
        modprobe -r hci_uart > /dev/null 2>&1
    fi
    if [ "$SERIAL_SET" = "" -a "$UART_SET" = "" ]; then
	if [ "$INITTAB" ] ; then
	    fgrep -v $DEVICE /etc/inittab > /etc/inittab.new
	    mv /etc/inittab.new /etc/inittab
	    telinit q
	fi
	do_fuser -k /dev/$DEVICE /dev/$CALLOUT $LINK > /dev/null
	rm -f $LINK ; mv -f $LINK.O $LINK 2>/dev/null
    fi
    ;;

'suspend')
    do_fuser -k -STOP /dev/$DEVICE /dev/$CALLOUT > /dev/null
    ;;

'resume')
    if [ "$SERIAL_OPTS" ] ; then
    setserial /dev/$DEVICE $SERIAL_OPTS
    fi
    do_fuser -k -CONT /dev/$DEVICE /dev/$CALLOUT $LINK > /dev/null
    #cardctl suspend
    ;;

*)
    echo "Unknown action: $ACTION."
    usage
    ;;

esac

exit 0
