#!/bin/bash
#
# Script for the driver par0.
#
# Caution: In most cases SuSE 9.2 hangsup the keyboard if this script is executed in
# the foreground. This is one of several SuSE keyboard bugs.
# Use at or a script in the background under SuSE 9.2.
# Due to the SuSE bugs this works "most of the time".
# With SuSE 9.3 there were no problems.
# 
# With debian some onboard ports, e. g. on Supermicro 370DL3, often do
# give no output. Use an ISA or PCI card if you use such a board. 


MAJOR=189
# io: -1 for the parallel port pci card, 888 for first parallel port, ...
IO=-1
DELAY=1000
WRITE=1

# read command line parameters
if [ $# -ge 2 ]; then
  IO=$2
fi
# IO == -1: set pci parallel port card (device 9805) io address (in kernel space this would require an expensive pci driver)
if [ $IO -eq -1 ]; then
  IO=0x$(lspci -vv -d :9805 | head -n 6 | grep "Region 0" | cut -d' ' -f 6)
fi
if [ $# -ge 3 ]; then
  DELAY=$3
fi
if [ $# -ge 4 ]; then
  WRITE=$4
fi

if test "$1" = "start"; then
  # remove the blocking modules (use USB printer instead of parallel port printer)
  rmmod -f lp
  rmmod -f parport_pc
  rmmod -f parport
  rm -f /dev/par0
  /sbin/insmod -f ./par0.ko par0_io=$IO  par0_delay=$DELAY par0write=$WRITE || exit 1
  MAJOR=`cat /proc/devices | awk '$2=="par0" { print $1 }'`
  mknod /dev/par0 c $MAJOR 0
  chmod a-x+w+r /dev/par0		
elif test "$1" = "stop"; then
  dd if=/dev/zero of=/dev/par0 bs=4 count=1
  /sbin/rmmod par0
  rm -f /dev/par0
elif test "$1" = "restart"; then
  $0 stop
  $0 start
else
  echo Syntax: par0.sh start\|stop\|restart
  exit 1
fi

exit 0

