Home | History | Annotate | Line # | Download | only in dev
par.c revision 1.12.4.1
      1  1.12.4.1      fvdl /*	$NetBSD: par.c,v 1.12.4.1 2001/10/10 11:56:48 fvdl Exp $	*/
      2       1.1       oki 
      3       1.1       oki /*
      4       1.1       oki  * Copyright (c) 1982, 1990 The Regents of the University of California.
      5       1.1       oki  * All rights reserved.
      6       1.1       oki  *
      7       1.1       oki  * Redistribution and use in source and binary forms, with or without
      8       1.1       oki  * modification, are permitted provided that the following conditions
      9       1.1       oki  * are met:
     10       1.1       oki  * 1. Redistributions of source code must retain the above copyright
     11       1.1       oki  *    notice, this list of conditions and the following disclaimer.
     12       1.1       oki  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       oki  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       oki  *    documentation and/or other materials provided with the distribution.
     15       1.1       oki  * 3. All advertising materials mentioning features or use of this software
     16       1.1       oki  *    must display the following acknowledgement:
     17       1.1       oki  *	This product includes software developed by the University of
     18       1.1       oki  *	California, Berkeley and its contributors.
     19       1.1       oki  * 4. Neither the name of the University nor the names of its contributors
     20       1.1       oki  *    may be used to endorse or promote products derived from this software
     21       1.1       oki  *    without specific prior written permission.
     22       1.1       oki  *
     23       1.1       oki  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1       oki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1       oki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1       oki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1       oki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1       oki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1       oki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1       oki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1       oki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1       oki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1       oki  * SUCH DAMAGE.
     34       1.1       oki  *
     35       1.1       oki  *	@(#)ppi.c	7.3 (Berkeley) 12/16/90
     36       1.1       oki  */
     37       1.1       oki 
     38       1.1       oki /*
     39       1.1       oki  * parallel port interface
     40       1.1       oki  */
     41       1.1       oki 
     42       1.1       oki #include <sys/param.h>
     43       1.1       oki #include <sys/errno.h>
     44       1.1       oki #include <sys/uio.h>
     45       1.1       oki #include <sys/device.h>
     46       1.1       oki #include <sys/malloc.h>
     47       1.1       oki #include <sys/file.h>
     48       1.1       oki #include <sys/systm.h>
     49      1.10   thorpej #include <sys/callout.h>
     50       1.2       oki #include <sys/proc.h>
     51       1.5       oki #include <sys/conf.h>
     52  1.12.4.1      fvdl #include <sys/vnode.h>
     53       1.1       oki 
     54      1.11   minoura #include <machine/bus.h>
     55      1.11   minoura #include <machine/cpu.h>
     56       1.7   minoura #include <machine/parioctl.h>
     57       1.7   minoura 
     58      1.11   minoura #include <arch/x68k/dev/intiovar.h>
     59       1.1       oki 
     60      1.11   minoura struct	par_softc {
     61      1.11   minoura 	struct device		sc_dev;
     62       1.1       oki 
     63      1.11   minoura 	bus_space_tag_t		sc_bst;
     64      1.11   minoura 	bus_space_handle_t	sc_bsh;
     65      1.11   minoura 	int			sc_flags;
     66      1.11   minoura 	struct parparam		sc_param;
     67      1.11   minoura #define sc_burst 	sc_param.burst
     68      1.11   minoura #define sc_timo  	sc_param.timo
     69      1.11   minoura #define sc_delay 	sc_param.delay
     70      1.11   minoura 	struct callout		sc_timo_ch;
     71      1.11   minoura 	struct callout		sc_start_ch;
     72       1.1       oki } ;
     73       1.1       oki 
     74      1.11   minoura /* par registers */
     75      1.11   minoura #define PAR_DATA	1
     76      1.11   minoura #define PAR_STROBE	3
     77      1.11   minoura 
     78       1.1       oki /* sc_flags values */
     79       1.1       oki #define	PARF_ALIVE	0x01
     80       1.1       oki #define	PARF_OPEN	0x02
     81       1.1       oki #define PARF_UIO	0x04
     82       1.1       oki #define PARF_TIMO	0x08
     83       1.1       oki #define PARF_DELAY	0x10
     84       1.1       oki #define PARF_OREAD	0x40	/* no support */
     85       1.1       oki #define PARF_OWRITE	0x80
     86       1.1       oki 
     87      1.11   minoura 
     88      1.11   minoura void partimo __P((void *));
     89      1.11   minoura void parstart __P((void *);)
     90      1.11   minoura void parintr __P((void *));
     91  1.12.4.1      fvdl int parrw __P((struct vnode *, struct uio *));
     92      1.11   minoura int parhztoms __P((int));
     93      1.11   minoura int parmstohz __P((int));
     94      1.11   minoura int parsendch __P((struct par_softc*, u_char));
     95      1.11   minoura int parsend __P((struct par_softc*, u_char *, int));
     96      1.11   minoura 
     97      1.10   thorpej static struct callout intr_callout = CALLOUT_INITIALIZER;
     98      1.10   thorpej 
     99       1.1       oki #define UNIT(x)		minor(x)
    100       1.1       oki 
    101       1.1       oki #ifdef DEBUG
    102       1.1       oki #define PDB_FOLLOW	0x01
    103       1.1       oki #define PDB_IO		0x02
    104       1.1       oki #define PDB_INTERRUPT   0x04
    105       1.1       oki #define PDB_NOCHECK	0x80
    106      1.11   minoura #ifdef PARDEBUG
    107       1.1       oki int	pardebug = PDB_FOLLOW | PDB_IO | PDB_INTERRUPT;
    108       1.1       oki #else
    109       1.1       oki int	pardebug = 0;
    110       1.1       oki #endif
    111       1.1       oki #endif
    112       1.1       oki 
    113       1.5       oki cdev_decl(par);
    114       1.5       oki 
    115       1.1       oki int parmatch __P((struct device *, struct cfdata *, void *));
    116       1.1       oki void parattach __P((struct device *, struct device *, void *));
    117       1.1       oki 
    118       1.1       oki struct cfattach par_ca = {
    119      1.11   minoura 	sizeof(struct par_softc), parmatch, parattach
    120       1.1       oki };
    121       1.1       oki 
    122       1.6   thorpej extern struct cfdriver par_cd;
    123       1.1       oki 
    124       1.1       oki int
    125       1.1       oki parmatch(pdp, cfp, aux)
    126       1.1       oki 	struct device *pdp;
    127       1.1       oki 	struct cfdata *cfp;
    128       1.1       oki 	void *aux;
    129       1.1       oki {
    130      1.11   minoura 	struct intio_attach_args *ia = aux;
    131      1.11   minoura 
    132       1.1       oki 	/* X680x0 has only one parallel port */
    133      1.11   minoura 	if (strcmp(ia->ia_name, "par") || cfp->cf_unit > 0)
    134      1.11   minoura 		return 0;
    135      1.11   minoura 
    136      1.11   minoura 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    137      1.11   minoura 		ia->ia_addr = 0xe8c000;
    138      1.11   minoura 	ia->ia_size = 0x2000;
    139      1.11   minoura 	if (intio_map_allocate_region (pdp, ia, INTIO_MAP_TESTONLY))
    140       1.1       oki 		return 0;
    141      1.12   minoura 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
    142      1.12   minoura 		ia->ia_intr = 99;
    143      1.12   minoura #if DIAGNOSTIC
    144      1.12   minoura 	if (ia->ia_intr != 99)
    145      1.12   minoura 		return 0;
    146      1.12   minoura #endif
    147      1.11   minoura 
    148       1.1       oki 	return 1;
    149       1.1       oki }
    150       1.1       oki 
    151       1.1       oki void
    152       1.1       oki parattach(pdp, dp, aux)
    153       1.1       oki 	struct device *pdp, *dp;
    154       1.1       oki 	void *aux;
    155       1.1       oki {
    156       1.1       oki 	register struct par_softc *sc = (struct par_softc *)dp;
    157      1.11   minoura 	struct intio_attach_args *ia = aux;
    158      1.11   minoura 	int r;
    159       1.1       oki 
    160       1.1       oki 	sc->sc_flags = PARF_ALIVE;
    161       1.4  christos 	printf(": parallel port (write only, interrupt)\n");
    162      1.11   minoura 	ia->ia_size = 0x2000;
    163      1.11   minoura 	r = intio_map_allocate_region (pdp, ia, INTIO_MAP_ALLOCATE);
    164      1.11   minoura #ifdef DIAGNOSTIC
    165      1.11   minoura 	if (r)
    166      1.11   minoura 		panic ("IO map for PAR corruption??");
    167      1.11   minoura #endif
    168      1.11   minoura 	sc->sc_bst = ia->ia_bst;
    169      1.11   minoura 	r = bus_space_map (sc->sc_bst,
    170      1.11   minoura 			   ia->ia_addr, ia->ia_size,
    171      1.11   minoura 			   BUS_SPACE_MAP_SHIFTED,
    172      1.11   minoura 			   &sc->sc_bsh);
    173      1.11   minoura #ifdef DIAGNOSTIC
    174      1.11   minoura 	if (r)
    175      1.11   minoura 		panic ("Cannot map IO space for PAR.");
    176      1.11   minoura #endif
    177      1.11   minoura 
    178      1.11   minoura 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    179      1.11   minoura 				~SICILIAN_INTR_PAR);
    180      1.12   minoura 
    181      1.12   minoura 	intio_intr_establish(ia->ia_intr, "par",
    182      1.12   minoura 			     (intio_intr_handler_t) parintr, (void*) 1);
    183      1.10   thorpej 
    184      1.10   thorpej 	callout_init(&sc->sc_timo_ch);
    185      1.10   thorpej 	callout_init(&sc->sc_start_ch);
    186       1.1       oki }
    187       1.1       oki 
    188       1.2       oki int
    189  1.12.4.1      fvdl paropen(devvp, flags, mode, p)
    190  1.12.4.1      fvdl 	struct vnode *devvp;
    191       1.5       oki 	int flags, mode;
    192       1.5       oki 	struct proc *p;
    193       1.1       oki {
    194  1.12.4.1      fvdl 	dev_t dev = vdev_rdev(devvp);
    195       1.1       oki 	register int unit = UNIT(dev);
    196      1.11   minoura 	register struct par_softc *sc;
    197       1.1       oki 
    198      1.11   minoura 	if (unit != 0)
    199      1.11   minoura 		return(ENXIO);
    200      1.11   minoura 	sc = par_cd.cd_devs[unit];
    201      1.11   minoura 	if (!(sc->sc_flags & PARF_ALIVE))
    202       1.1       oki 		return(ENXIO);
    203       1.1       oki 	if (sc->sc_flags & PARF_OPEN)
    204       1.1       oki 		return(EBUSY);
    205       1.1       oki 	/* X680x0 can't read */
    206       1.1       oki 	if ((flags & FREAD) == FREAD)
    207       1.1       oki 		return (EINVAL);
    208  1.12.4.1      fvdl 
    209  1.12.4.1      fvdl 	vdev_setprivdata(devvp, sc);
    210       1.1       oki 
    211       1.1       oki 	sc->sc_flags |= PARF_OPEN;
    212       1.1       oki 
    213       1.1       oki 	sc->sc_flags |= PARF_OWRITE;
    214       1.1       oki 
    215       1.1       oki 	sc->sc_burst = PAR_BURST;
    216       1.1       oki 	sc->sc_timo = parmstohz(PAR_TIMO);
    217       1.1       oki 	sc->sc_delay = parmstohz(PAR_DELAY);
    218      1.11   minoura 
    219       1.1       oki 	return(0);
    220       1.1       oki }
    221       1.1       oki 
    222       1.2       oki int
    223  1.12.4.1      fvdl parclose(devvp, flags, mode, p)
    224  1.12.4.1      fvdl 	struct vnode *devvp;
    225       1.5       oki 	int flags, mode;
    226       1.5       oki 	struct proc *p;
    227       1.1       oki {
    228       1.1       oki 	int s;
    229  1.12.4.1      fvdl 	struct par_softc *sc = vdev_privdata(devvp);
    230       1.1       oki 
    231       1.1       oki 	sc->sc_flags &= ~(PARF_OPEN|PARF_OWRITE);
    232       1.1       oki 
    233       1.1       oki 	/* don't allow interrupts any longer */
    234       1.1       oki 	s = spl1();
    235      1.11   minoura 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    236      1.11   minoura 				~SICILIAN_INTR_PAR);
    237       1.1       oki 	splx(s);
    238       1.1       oki 
    239       1.2       oki 	return (0);
    240       1.1       oki }
    241       1.1       oki 
    242       1.1       oki void
    243       1.5       oki parstart(arg)
    244       1.5       oki 	void *arg;
    245       1.1       oki {
    246      1.10   thorpej 	struct par_softc *sc = arg;
    247       1.1       oki #ifdef DEBUG
    248       1.1       oki 	if (pardebug & PDB_FOLLOW)
    249      1.10   thorpej 		printf("parstart(%x)\n", sc->sc_dev.dv_unit);
    250       1.1       oki #endif
    251       1.1       oki 	sc->sc_flags &= ~PARF_DELAY;
    252       1.1       oki 	wakeup(sc);
    253       1.1       oki }
    254       1.1       oki 
    255       1.1       oki void
    256       1.5       oki partimo(arg)
    257       1.5       oki 	void *arg;
    258       1.1       oki {
    259      1.10   thorpej 	struct par_softc *sc = arg;
    260       1.1       oki #ifdef DEBUG
    261       1.1       oki 	if (pardebug & PDB_FOLLOW)
    262      1.10   thorpej 		printf("partimo(%x)\n", sc->sc_dev.dv_unit);
    263       1.1       oki #endif
    264       1.1       oki 	sc->sc_flags &= ~(PARF_UIO|PARF_TIMO);
    265       1.1       oki 	wakeup(sc);
    266       1.1       oki }
    267       1.1       oki 
    268       1.2       oki int
    269  1.12.4.1      fvdl parwrite(devvp, uio, flag)
    270  1.12.4.1      fvdl 	struct vnode *devvp;
    271       1.1       oki 	struct uio *uio;
    272       1.5       oki 	int flag;
    273       1.1       oki {
    274       1.1       oki 
    275       1.1       oki #ifdef DEBUG
    276       1.1       oki 	if (pardebug & PDB_FOLLOW)
    277  1.12.4.1      fvdl 		printf("parwrite(%x, %p)\n", vdev_rdev(devvp), uio);
    278       1.1       oki #endif
    279  1.12.4.1      fvdl 	return (parrw(devvp, uio));
    280       1.1       oki }
    281       1.1       oki 
    282       1.2       oki int
    283  1.12.4.1      fvdl parrw(devvp, uio)
    284  1.12.4.1      fvdl 	struct vnode *devvp;
    285       1.1       oki 	register struct uio *uio;
    286       1.1       oki {
    287  1.12.4.1      fvdl 	register struct par_softc *sc = vdev_privdata(devvp);
    288       1.1       oki 	register int s, len, cnt;
    289       1.1       oki 	register char *cp;
    290       1.8   minoura 	int error = 0;
    291       1.1       oki 	int buflen;
    292       1.1       oki 	char *buf;
    293       1.1       oki 
    294       1.1       oki 	if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
    295       1.1       oki 		return EINVAL;
    296       1.1       oki 
    297       1.1       oki 	if (uio->uio_resid == 0)
    298       1.1       oki 		return(0);
    299       1.1       oki 
    300       1.1       oki 	buflen = min(sc->sc_burst, uio->uio_resid);
    301       1.1       oki 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    302       1.1       oki 	sc->sc_flags |= PARF_UIO;
    303       1.1       oki 	if (sc->sc_timo > 0) {
    304       1.1       oki 		sc->sc_flags |= PARF_TIMO;
    305      1.10   thorpej 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, partimo, sc);
    306       1.1       oki 	}
    307       1.1       oki 	while (uio->uio_resid > 0) {
    308       1.1       oki 		len = min(buflen, uio->uio_resid);
    309       1.1       oki 		cp = buf;
    310       1.1       oki 		if (uio->uio_rw == UIO_WRITE) {
    311       1.1       oki 			error = uiomove(cp, len, uio);
    312       1.1       oki 			if (error)
    313       1.1       oki 				break;
    314       1.1       oki 		}
    315       1.1       oki 	      again:
    316       1.1       oki 		s = spl1();
    317       1.1       oki 		/*
    318       1.1       oki 		 * Check if we timed out during sleep or uiomove
    319       1.1       oki 		 */
    320       1.9   thorpej 		(void) spllowersoftclock();
    321       1.1       oki 		if ((sc->sc_flags & PARF_UIO) == 0) {
    322       1.1       oki #ifdef DEBUG
    323       1.1       oki 			if (pardebug & PDB_IO)
    324       1.4  christos 				printf("parrw: uiomove/sleep timo, flags %x\n",
    325       1.1       oki 				       sc->sc_flags);
    326       1.1       oki #endif
    327       1.1       oki 			if (sc->sc_flags & PARF_TIMO) {
    328      1.10   thorpej 				callout_stop(&sc->sc_timo_ch);
    329       1.1       oki 				sc->sc_flags &= ~PARF_TIMO;
    330       1.1       oki 			}
    331       1.1       oki 			splx(s);
    332       1.1       oki 			break;
    333       1.1       oki 		}
    334       1.1       oki 		splx(s);
    335       1.1       oki 		/*
    336       1.1       oki 		 * Perform the operation
    337       1.1       oki 		 */
    338      1.11   minoura 		cnt = parsend(sc, cp, len);
    339       1.1       oki 		if (cnt < 0) {
    340       1.1       oki 			error = -cnt;
    341       1.1       oki 			break;
    342       1.1       oki 		}
    343       1.1       oki 
    344       1.1       oki 		s = splsoftclock();
    345       1.1       oki 		/*
    346       1.1       oki 		 * Operation timeout (or non-blocking), quit now.
    347       1.1       oki 		 */
    348       1.1       oki 		if ((sc->sc_flags & PARF_UIO) == 0) {
    349       1.1       oki #ifdef DEBUG
    350       1.1       oki 			if (pardebug & PDB_IO)
    351       1.4  christos 				printf("parrw: timeout/done\n");
    352       1.1       oki #endif
    353       1.1       oki 			splx(s);
    354       1.1       oki 			break;
    355       1.1       oki 		}
    356       1.1       oki 		/*
    357       1.1       oki 		 * Implement inter-read delay
    358       1.1       oki 		 */
    359       1.1       oki 		if (sc->sc_delay > 0) {
    360       1.1       oki 			sc->sc_flags |= PARF_DELAY;
    361      1.10   thorpej 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
    362      1.10   thorpej 			    parstart, sc);
    363       1.8   minoura 			error = tsleep(sc, PCATCH|(PZERO-1), "par-cdelay", 0);
    364       1.1       oki 			if (error) {
    365       1.1       oki 				splx(s);
    366       1.1       oki 				break;
    367       1.1       oki 			}
    368       1.1       oki 		}
    369       1.1       oki 		splx(s);
    370       1.1       oki 		/*
    371       1.1       oki 		 * Must not call uiomove again til we've used all data
    372       1.1       oki 		 * that we already grabbed.
    373       1.1       oki 		 */
    374       1.1       oki 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    375       1.1       oki 			cp += cnt;
    376       1.1       oki 			len -= cnt;
    377       1.1       oki 			cnt = 0;
    378       1.1       oki 			goto again;
    379       1.1       oki 		}
    380       1.1       oki 	}
    381       1.1       oki 	s = splsoftclock();
    382       1.1       oki 	if (sc->sc_flags & PARF_TIMO) {
    383      1.10   thorpej 		callout_stop(&sc->sc_timo_ch);
    384       1.1       oki 		sc->sc_flags &= ~PARF_TIMO;
    385       1.1       oki 	}
    386       1.1       oki 	if (sc->sc_flags & PARF_DELAY)	{
    387      1.10   thorpej 		callout_stop(&sc->sc_start_ch);
    388       1.1       oki 		sc->sc_flags &= ~PARF_DELAY;
    389       1.1       oki 	}
    390       1.1       oki 	splx(s);
    391       1.1       oki 	/*
    392       1.1       oki 	 * Adjust for those chars that we uiomove'ed but never wrote
    393       1.1       oki 	 */
    394       1.1       oki 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    395       1.1       oki 		uio->uio_resid += (len - cnt);
    396       1.1       oki #ifdef DEBUG
    397       1.1       oki 			if (pardebug & PDB_IO)
    398       1.4  christos 				printf("parrw: short write, adjust by %d\n",
    399       1.1       oki 				       len-cnt);
    400       1.1       oki #endif
    401       1.1       oki 	}
    402       1.1       oki 	free(buf, M_DEVBUF);
    403       1.1       oki #ifdef DEBUG
    404       1.1       oki 	if (pardebug & (PDB_FOLLOW|PDB_IO))
    405       1.4  christos 		printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
    406       1.1       oki #endif
    407       1.1       oki 	return (error);
    408       1.1       oki }
    409       1.1       oki 
    410       1.1       oki int
    411  1.12.4.1      fvdl parioctl(devvp, cmd, data, flag, p)
    412  1.12.4.1      fvdl 	struct vnode *devvp;
    413       1.1       oki 	u_long cmd;
    414       1.1       oki 	caddr_t data;
    415       1.1       oki 	int flag;
    416       1.1       oki 	struct proc *p;
    417       1.1       oki {
    418  1.12.4.1      fvdl 	struct par_softc *sc = vdev_privdata(devvp);
    419       1.1       oki 	struct parparam *pp, *upp;
    420       1.1       oki 	int error = 0;
    421       1.1       oki 
    422       1.1       oki 	switch (cmd) {
    423       1.1       oki 	      case PARIOCGPARAM:
    424       1.1       oki 		pp = &sc->sc_param;
    425       1.1       oki 		upp = (struct parparam *)data;
    426       1.1       oki 		upp->burst = pp->burst;
    427       1.1       oki 		upp->timo = parhztoms(pp->timo);
    428       1.1       oki 		upp->delay = parhztoms(pp->delay);
    429       1.1       oki 		break;
    430       1.1       oki 
    431       1.1       oki 	      case PARIOCSPARAM:
    432       1.1       oki 		pp = &sc->sc_param;
    433       1.1       oki 		upp = (struct parparam *)data;
    434       1.1       oki 		if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
    435       1.1       oki 		    upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
    436       1.1       oki 			return(EINVAL);
    437       1.1       oki 		pp->burst = upp->burst;
    438       1.1       oki 		pp->timo = parmstohz(upp->timo);
    439       1.1       oki 		pp->delay = parmstohz(upp->delay);
    440       1.1       oki 		break;
    441       1.1       oki 
    442       1.1       oki 	      default:
    443       1.1       oki 		return(EINVAL);
    444       1.1       oki 	}
    445       1.1       oki 	return (error);
    446       1.1       oki }
    447       1.1       oki 
    448       1.1       oki int
    449       1.1       oki parhztoms(h)
    450       1.1       oki 	int h;
    451       1.1       oki {
    452       1.1       oki 	extern int hz;
    453       1.1       oki 	register int m = h;
    454       1.1       oki 
    455       1.1       oki 	if (m > 0)
    456       1.1       oki 		m = m * 1000 / hz;
    457       1.1       oki 	return(m);
    458       1.1       oki }
    459       1.1       oki 
    460       1.1       oki int
    461       1.1       oki parmstohz(m)
    462       1.1       oki 	int m;
    463       1.1       oki {
    464       1.1       oki 	extern int hz;
    465       1.1       oki 	register int h = m;
    466       1.1       oki 
    467       1.1       oki 	if (h > 0) {
    468       1.1       oki 		h = h * hz / 1000;
    469       1.1       oki 		if (h == 0)
    470       1.1       oki 			h = 1000 / hz;
    471       1.1       oki 	}
    472       1.1       oki 	return(h);
    473       1.1       oki }
    474       1.1       oki 
    475       1.1       oki /* stuff below here if for interrupt driven output of data thru
    476       1.1       oki    the parallel port. */
    477       1.1       oki 
    478       1.1       oki int partimeout_pending;
    479       1.1       oki int parsend_pending;
    480       1.1       oki 
    481       1.1       oki void
    482       1.2       oki parintr(arg)
    483       1.1       oki 	void *arg;
    484       1.1       oki {
    485       1.1       oki 	int s, mask;
    486       1.1       oki 
    487       1.1       oki 	mask = (int)arg;
    488       1.1       oki 	s = splclock();
    489       1.1       oki 
    490      1.11   minoura 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    491      1.11   minoura 				~SICILIAN_INTR_PAR);
    492       1.1       oki 
    493       1.1       oki #ifdef DEBUG
    494       1.1       oki 	if (pardebug & PDB_INTERRUPT)
    495       1.4  christos 		printf ("parintr %d(%s)\n", mask, mask ? "FLG" : "tout");
    496       1.1       oki #endif
    497       1.1       oki 	/* if invoked from timeout handler, mask will be 0,
    498       1.1       oki 	 * if from interrupt, it will contain the cia-icr mask,
    499       1.1       oki 	 * which is != 0
    500       1.1       oki 	 */
    501       1.1       oki 	if (mask) {
    502       1.1       oki 		if (partimeout_pending)
    503      1.10   thorpej 			callout_stop(&intr_callout);
    504       1.1       oki 		if (parsend_pending)
    505       1.1       oki 			parsend_pending = 0;
    506       1.1       oki 	}
    507       1.1       oki 
    508       1.1       oki 	/* either way, there won't be a timeout pending any longer */
    509       1.1       oki 	partimeout_pending = 0;
    510       1.1       oki 
    511       1.1       oki 	wakeup(parintr);
    512       1.1       oki 	splx (s);
    513       1.1       oki }
    514       1.1       oki 
    515       1.1       oki int
    516      1.11   minoura parsendch(sc, ch)
    517      1.11   minoura 	struct par_softc *sc;
    518       1.1       oki 	u_char ch;
    519       1.1       oki {
    520       1.1       oki 	int error = 0;
    521       1.1       oki 	int s;
    522       1.1       oki 
    523       1.1       oki 	/* if either offline, busy or out of paper, wait for that
    524       1.1       oki 	   condition to clear */
    525       1.1       oki 	s = spl1();
    526       1.1       oki 	while (!error
    527       1.1       oki 	       && (parsend_pending
    528      1.11   minoura 		   || !(intio_get_sicilian_intr() & SICILIAN_STAT_PAR)))
    529       1.1       oki 		{
    530       1.1       oki 			extern int hz;
    531       1.1       oki 
    532       1.1       oki 			/* wait a second, and try again */
    533      1.10   thorpej 			callout_reset(&intr_callout, hz, parintr, 0);
    534       1.1       oki 			partimeout_pending = 1;
    535       1.1       oki 			/* this is essentially a flipflop to have us wait for the
    536       1.1       oki 			   first character being transmitted when trying to transmit
    537       1.1       oki 			   the second, etc. */
    538       1.1       oki 			parsend_pending = 0;
    539       1.1       oki 			/* it's quite important that a parallel putc can be
    540       1.1       oki 			   interrupted, given the possibility to lock a printer
    541       1.1       oki 			   in an offline condition.. */
    542       1.8   minoura 			if ((error = tsleep (parintr, PCATCH|(PZERO-1), "parsendch", 0))) {
    543       1.1       oki #ifdef DEBUG
    544       1.1       oki 				if (pardebug & PDB_INTERRUPT)
    545       1.4  christos 					printf ("parsendch interrupted, error = %d\n", error);
    546       1.1       oki #endif
    547       1.1       oki 				if (partimeout_pending)
    548      1.10   thorpej 					callout_stop(&intr_callout);
    549       1.1       oki 
    550       1.1       oki 				partimeout_pending = 0;
    551       1.1       oki 			}
    552       1.1       oki 		}
    553       1.1       oki 
    554       1.2       oki 	if (!error) {
    555       1.1       oki #ifdef DEBUG
    556       1.1       oki 		if (pardebug & PDB_INTERRUPT)
    557       1.4  christos 			printf ("#%d", ch);
    558       1.1       oki #endif
    559      1.11   minoura 		bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_DATA, ch);
    560       1.1       oki 		DELAY(1);	/* (DELAY(1) == 1us) > 0.5us */
    561      1.11   minoura 		bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_STROBE, 0);
    562      1.11   minoura 		intio_set_sicilian_intr (intio_get_sicilian_intr() |
    563      1.11   minoura 					 SICILIAN_INTR_PAR);
    564       1.1       oki 		DELAY(1);
    565      1.11   minoura 		bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_STROBE, 1);
    566       1.1       oki 		parsend_pending = 1;
    567       1.1       oki 	}
    568       1.1       oki 
    569       1.1       oki 	splx (s);
    570       1.1       oki 
    571       1.1       oki 	return error;
    572       1.1       oki }
    573       1.1       oki 
    574       1.1       oki 
    575       1.1       oki int
    576      1.11   minoura parsend(sc, buf, len)
    577      1.11   minoura 	struct par_softc *sc;
    578       1.1       oki 	u_char *buf;
    579       1.1       oki 	int len;
    580       1.1       oki {
    581       1.1       oki 	int err, orig_len = len;
    582       1.1       oki 
    583       1.1       oki 	for (; len; len--, buf++)
    584      1.11   minoura 		if ((err = parsendch (sc, *buf)))
    585       1.1       oki 			return err < 0 ? -EINTR : -err;
    586       1.1       oki 
    587       1.1       oki 	/* either all or nothing.. */
    588       1.1       oki 	return orig_len;
    589       1.1       oki }
    590