/*
* Copyright (c) 1982, 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ppi.c 7.3 (Berkeley) 12/16/90
*/
int
parclose(dev_t dev, int flags, int mode, struct lwp *l)
{
int unit = UNIT(dev);
int s;
struct par_softc *sc = device_lookup_private(&par_cd, unit);
sc->sc_flags &= ~(PARF_OPEN|PARF_OWRITE);
/* don't allow interrupts any longer */
s = spl1();
intio_set_sicilian_intr(intio_get_sicilian_intr() &
~SICILIAN_INTR_PAR);
splx(s);
int
parrw(dev_t dev, struct uio *uio)
{
int unit = UNIT(dev);
struct par_softc *sc = device_lookup_private(&par_cd, unit);
int len=0xdeadbeef; /* XXX: shutup gcc */
int s, cnt=0;
char *cp;
int error = 0;
int buflen;
char *buf;
if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
return EINVAL;
if (uio->uio_resid == 0)
return(0);
buflen = uimin(sc->sc_burst, uio->uio_resid);
buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
sc->sc_flags |= PARF_UIO;
if (sc->sc_timo > 0) {
sc->sc_flags |= PARF_TIMO;
callout_reset(&sc->sc_timo_ch, sc->sc_timo, partimo, sc);
}
while (uio->uio_resid > 0) {
len = uimin(buflen, uio->uio_resid);
cp = buf;
if (uio->uio_rw == UIO_WRITE) {
error = uiomove(cp, len, uio);
if (error)
break;
}
again:
s = splsoftclock();
/*
* Check if we timed out during sleep or uiomove
*/
if ((sc->sc_flags & PARF_UIO) == 0) {
#ifdef DEBUG
if (pardebug & PDB_IO)
printf("parrw: uiomove/sleep timo, flags %x\n",
sc->sc_flags);
#endif
if (sc->sc_flags & PARF_TIMO) {
callout_stop(&sc->sc_timo_ch);
sc->sc_flags &= ~PARF_TIMO;
}
splx(s);
break;
}
splx(s);
/*
* Perform the operation
*/
cnt = parsend(sc, cp, len);
if (cnt < 0) {
error = -cnt;
break;
}
s = splsoftclock();
/*
* Operation timeout (or non-blocking), quit now.
*/
if ((sc->sc_flags & PARF_UIO) == 0) {
#ifdef DEBUG
if (pardebug & PDB_IO)
printf("parrw: timeout/done\n");
#endif
splx(s);
break;
}
/*
* Implement inter-read delay
*/
if (sc->sc_delay > 0) {
sc->sc_flags |= PARF_DELAY;
callout_reset(&sc->sc_start_ch, sc->sc_delay,
parstart, sc);
error = tsleep(sc, PCATCH|(PZERO-1), "par-cdelay", 0);
if (error) {
splx(s);
break;
}
}
splx(s);
/*
* Must not call uiomove again til we've used all data
* that we already grabbed.
*/
if (uio->uio_rw == UIO_WRITE && cnt != len) {
cp += cnt;
len -= cnt;
cnt = 0;
goto again;
}
}
s = splsoftclock();
if (sc->sc_flags & PARF_TIMO) {
callout_stop(&sc->sc_timo_ch);
sc->sc_flags &= ~PARF_TIMO;
}
if (sc->sc_flags & PARF_DELAY) {
callout_stop(&sc->sc_start_ch);
sc->sc_flags &= ~PARF_DELAY;
}
splx(s);
/*
* Adjust for those chars that we uiomove'ed but never wrote
*/
/*
* XXXjdolecek: this len usage is wrong, this will be incorrect
* if the transfer size is longer than sc_burst
*/
if (uio->uio_rw == UIO_WRITE && cnt != len) {
uio->uio_resid += (len - cnt);
#ifdef DEBUG
if (pardebug & PDB_IO)
printf("parrw: short write, adjust by %d\n",
len-cnt);
#endif
}
free(buf, M_DEVBUF);
#ifdef DEBUG
if (pardebug & (PDB_FOLLOW|PDB_IO))
printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
#endif
return (error);
}
int
parioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct par_softc *sc = device_lookup_private(&par_cd, UNIT(dev));
struct parparam *pp, *upp;
int error = 0;
#ifdef DEBUG
if (pardebug & PDB_INTERRUPT)
printf("parintr %d(%s)\n", mask, mask ? "FLG" : "tout");
#endif
/* if invoked from timeout handler, mask will be 0,
* if from interrupt, it will contain the cia-icr mask,
* which is != 0
*/
if (mask) {
if (partimeout_pending)
callout_stop(&intr_callout);
if (parsend_pending)
parsend_pending = 0;
}
/* either way, there won't be a timeout pending any longer */
partimeout_pending = 0;
wakeup(parintr);
splx(s);
}
int
parsendch(struct par_softc *sc, u_char ch)
{
int error = 0;
int s;
/* if either offline, busy or out of paper, wait for that
condition to clear */
s = spl1();
while (!error
&& (parsend_pending
|| !(intio_get_sicilian_intr() & SICILIAN_STAT_PAR)))
{
/* wait a second, and try again */
callout_reset(&intr_callout, hz, parintr, 0);
partimeout_pending = 1;
/* this is essentially a flipflop to have us wait for the
first character being transmitted when trying to transmit
the second, etc. */
parsend_pending = 0;
/* it's quite important that a parallel putc can be
interrupted, given the possibility to lock a printer
in an offline condition.. */
if ((error = tsleep(parintr, PCATCH|(PZERO-1), "parsendch", 0))) {
#ifdef DEBUG
if (pardebug & PDB_INTERRUPT)
printf("parsendch interrupted, error = %d\n", error);
#endif
if (partimeout_pending)
callout_stop(&intr_callout);