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