Home | History | Annotate | Line # | Download | only in dev
par.c revision 1.33
      1  1.33  ad /*	$NetBSD: par.c,v 1.33 2007/07/14 10:46:26 ad 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.33  ad __KERNEL_RCSID(0, "$NetBSD: par.c,v 1.33 2007/07/14 10:46:26 ad 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.33  ad static struct callout intr_callout = CALLOUT_INITIALIZER;
     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.33  ad 	struct par_softc *sc = (struct par_softc *)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.33  ad }
    192  1.33  ad 
    193  1.33  ad int
    194  1.33  ad paropen(dev_t dev, int flags, int mode, struct lwp *l)
    195  1.33  ad {
    196  1.33  ad 	int unit = UNIT(dev);
    197  1.33  ad 	struct par_softc *sc;
    198  1.33  ad 
    199  1.33  ad 	sc = device_lookup(&par_cd, unit);
    200  1.33  ad 	if (sc == NULL || !(sc->sc_flags & PARF_ALIVE))
    201  1.33  ad 		return(ENXIO);
    202  1.33  ad 	if (sc->sc_flags & PARF_OPEN)
    203  1.33  ad 		return(EBUSY);
    204  1.33  ad 	/* X680x0 can't read */
    205  1.33  ad 	if ((flags & FREAD) == FREAD)
    206  1.33  ad 		return (EINVAL);
    207  1.33  ad 
    208  1.33  ad 	sc->sc_flags |= PARF_OPEN;
    209  1.33  ad 
    210  1.33  ad 	sc->sc_flags |= PARF_OWRITE;
    211  1.33  ad 
    212  1.33  ad 	sc->sc_burst = PAR_BURST;
    213  1.33  ad 	sc->sc_timo = parmstohz(PAR_TIMO);
    214  1.33  ad 	sc->sc_delay = parmstohz(PAR_DELAY);
    215  1.33  ad 
    216  1.33  ad 	return(0);
    217  1.33  ad }
    218  1.33  ad 
    219  1.33  ad int
    220  1.33  ad parclose(dev_t dev, int flags, int mode, struct lwp *l)
    221  1.33  ad {
    222  1.33  ad 	int unit = UNIT(dev);
    223  1.33  ad 	int s;
    224  1.33  ad 	struct par_softc *sc = par_cd.cd_devs[unit];
    225  1.33  ad 
    226  1.33  ad 	sc->sc_flags &= ~(PARF_OPEN|PARF_OWRITE);
    227  1.33  ad 
    228  1.33  ad 	/* don't allow interrupts any longer */
    229  1.33  ad 	s = spl1();
    230  1.33  ad 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    231  1.33  ad 				~SICILIAN_INTR_PAR);
    232  1.33  ad 	splx(s);
    233  1.33  ad 
    234  1.33  ad 	return (0);
    235  1.33  ad }
    236  1.33  ad 
    237  1.33  ad void
    238  1.33  ad parstart(void *arg)
    239  1.33  ad {
    240  1.33  ad 	struct par_softc *sc = arg;
    241  1.33  ad #ifdef DEBUG
    242  1.33  ad 	if (pardebug & PDB_FOLLOW)
    243  1.33  ad 		printf("parstart(%x)\n", device_unit(&sc->sc_dev));
    244  1.33  ad #endif
    245  1.33  ad 	sc->sc_flags &= ~PARF_DELAY;
    246  1.33  ad 	wakeup(sc);
    247  1.33  ad }
    248  1.33  ad 
    249  1.33  ad void
    250  1.33  ad partimo(void *arg)
    251  1.33  ad {
    252  1.33  ad 	struct par_softc *sc = arg;
    253  1.33  ad #ifdef DEBUG
    254  1.33  ad 	if (pardebug & PDB_FOLLOW)
    255  1.33  ad 		printf("partimo(%x)\n", device_unit(&sc->sc_dev));
    256  1.33  ad #endif
    257  1.33  ad 	sc->sc_flags &= ~(PARF_UIO|PARF_TIMO);
    258  1.33  ad 	wakeup(sc);
    259  1.33  ad }
    260  1.33  ad 
    261  1.33  ad int
    262  1.33  ad parwrite(dev_t dev, struct uio *uio, int flag)
    263  1.33  ad {
    264  1.33  ad 
    265  1.33  ad #ifdef DEBUG
    266  1.33  ad 	if (pardebug & PDB_FOLLOW)
    267  1.33  ad 		printf("parwrite(%x, %p)\n", dev, uio);
    268  1.33  ad #endif
    269  1.33  ad 	return (parrw(dev, uio));
    270  1.33  ad }
    271  1.33  ad 
    272  1.33  ad int
    273  1.33  ad parrw(dev_t dev, struct uio *uio)
    274  1.33  ad {
    275  1.33  ad 	int unit = UNIT(dev);
    276  1.33  ad 	struct par_softc *sc = par_cd.cd_devs[unit];
    277  1.33  ad 	int len=0xdeadbeef;	/* XXX: shutup gcc */
    278  1.33  ad 	int s, cnt=0;
    279  1.33  ad 	char *cp;
    280  1.33  ad 	int error = 0;
    281  1.33  ad 	int buflen;
    282  1.33  ad 	char *buf;
    283  1.33  ad 
    284  1.33  ad 	if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
    285  1.33  ad 		return EINVAL;
    286  1.33  ad 
    287  1.33  ad 	if (uio->uio_resid == 0)
    288  1.33  ad 		return(0);
    289  1.33  ad 
    290  1.33  ad 	buflen = min(sc->sc_burst, uio->uio_resid);
    291  1.33  ad 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    292  1.33  ad 	sc->sc_flags |= PARF_UIO;
    293  1.33  ad 	if (sc->sc_timo > 0) {
    294  1.33  ad 		sc->sc_flags |= PARF_TIMO;
    295  1.33  ad 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, partimo, sc);
    296  1.33  ad 	}
    297  1.33  ad 	while (uio->uio_resid > 0) {
    298  1.33  ad 		len = min(buflen, uio->uio_resid);
    299  1.33  ad 		cp = buf;
    300  1.33  ad 		if (uio->uio_rw == UIO_WRITE) {
    301  1.33  ad 			error = uiomove(cp, len, uio);
    302  1.33  ad 			if (error)
    303  1.33  ad 				break;
    304  1.33  ad 		}
    305  1.33  ad 	      again:
    306  1.33  ad 		s = splsoftclock();
    307  1.33  ad 		/*
    308  1.33  ad 		 * Check if we timed out during sleep or uiomove
    309  1.33  ad 		 */
    310  1.33  ad 		if ((sc->sc_flags & PARF_UIO) == 0) {
    311  1.33  ad #ifdef DEBUG
    312  1.33  ad 			if (pardebug & PDB_IO)
    313  1.33  ad 				printf("parrw: uiomove/sleep timo, flags %x\n",
    314  1.33  ad 				       sc->sc_flags);
    315  1.33  ad #endif
    316  1.33  ad 			if (sc->sc_flags & PARF_TIMO) {
    317  1.33  ad 				callout_stop(&sc->sc_timo_ch);
    318  1.33  ad 				sc->sc_flags &= ~PARF_TIMO;
    319  1.33  ad 			}
    320  1.33  ad 			splx(s);
    321  1.33  ad 			break;
    322  1.33  ad 		}
    323  1.33  ad 		splx(s);
    324  1.33  ad 		/*
    325  1.33  ad 		 * Perform the operation
    326  1.33  ad 		 */
    327  1.33  ad 		cnt = parsend(sc, cp, len);
    328  1.33  ad 		if (cnt < 0) {
    329  1.33  ad 			error = -cnt;
    330  1.33  ad 			break;
    331  1.33  ad 		}
    332  1.33  ad 
    333  1.33  ad 		s = splsoftclock();
    334  1.33  ad 		/*
    335  1.33  ad 		 * Operation timeout (or non-blocking), quit now.
    336  1.33  ad 		 */
    337  1.33  ad 		if ((sc->sc_flags & PARF_UIO) == 0) {
    338  1.33  ad #ifdef DEBUG
    339  1.33  ad 			if (pardebug & PDB_IO)
    340  1.33  ad 				printf("parrw: timeout/done\n");
    341  1.33  ad #endif
    342  1.33  ad 			splx(s);
    343  1.33  ad 			break;
    344  1.33  ad 		}
    345  1.33  ad 		/*
    346  1.33  ad 		 * Implement inter-read delay
    347  1.33  ad 		 */
    348  1.33  ad 		if (sc->sc_delay > 0) {
    349  1.33  ad 			sc->sc_flags |= PARF_DELAY;
    350  1.33  ad 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
    351  1.33  ad 			    parstart, sc);
    352  1.33  ad 			error = tsleep(sc, PCATCH|(PZERO-1), "par-cdelay", 0);
    353  1.33  ad 			if (error) {
    354  1.33  ad 				splx(s);
    355  1.33  ad 				break;
    356  1.33  ad 			}
    357  1.33  ad 		}
    358  1.33  ad 		splx(s);
    359  1.33  ad 		/*
    360  1.33  ad 		 * Must not call uiomove again til we've used all data
    361  1.33  ad 		 * that we already grabbed.
    362  1.33  ad 		 */
    363  1.33  ad 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    364  1.33  ad 			cp += cnt;
    365  1.33  ad 			len -= cnt;
    366  1.33  ad 			cnt = 0;
    367  1.33  ad 			goto again;
    368  1.33  ad 		}
    369  1.33  ad 	}
    370  1.33  ad 	s = splsoftclock();
    371  1.33  ad 	if (sc->sc_flags & PARF_TIMO) {
    372  1.33  ad 		callout_stop(&sc->sc_timo_ch);
    373  1.33  ad 		sc->sc_flags &= ~PARF_TIMO;
    374  1.33  ad 	}
    375  1.33  ad 	if (sc->sc_flags & PARF_DELAY)	{
    376  1.33  ad 		callout_stop(&sc->sc_start_ch);
    377  1.33  ad 		sc->sc_flags &= ~PARF_DELAY;
    378  1.33  ad 	}
    379  1.33  ad 	splx(s);
    380  1.33  ad 	/*
    381  1.33  ad 	 * Adjust for those chars that we uiomove'ed but never wrote
    382  1.33  ad 	 */
    383  1.33  ad 	/*
    384  1.33  ad 	 * XXXjdolecek: this len usage is wrong, this will be incorrect
    385  1.33  ad 	 * if the transfer size is longer than sc_burst
    386  1.33  ad 	 */
    387  1.33  ad 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    388  1.33  ad 		uio->uio_resid += (len - cnt);
    389  1.33  ad #ifdef DEBUG
    390  1.33  ad 			if (pardebug & PDB_IO)
    391  1.33  ad 				printf("parrw: short write, adjust by %d\n",
    392  1.33  ad 				       len-cnt);
    393  1.33  ad #endif
    394  1.33  ad 	}
    395  1.33  ad 	free(buf, M_DEVBUF);
    396  1.33  ad #ifdef DEBUG
    397  1.33  ad 	if (pardebug & (PDB_FOLLOW|PDB_IO))
    398  1.33  ad 		printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
    399  1.33  ad #endif
    400  1.33  ad 	return (error);
    401  1.33  ad }
    402  1.33  ad 
    403  1.33  ad int
    404  1.33  ad parioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    405  1.33  ad {
    406  1.33  ad 	struct par_softc *sc = par_cd.cd_devs[UNIT(dev)];
    407  1.33  ad 	struct parparam *pp, *upp;
    408  1.33  ad 	int error = 0;
    409  1.33  ad 
    410  1.33  ad 	switch (cmd) {
    411  1.33  ad 	case PARIOCGPARAM:
    412  1.33  ad 		pp = &sc->sc_param;
    413  1.33  ad 		upp = (struct parparam *)data;
    414  1.33  ad 		upp->burst = pp->burst;
    415  1.33  ad 		upp->timo = parhztoms(pp->timo);
    416  1.33  ad 		upp->delay = parhztoms(pp->delay);
    417  1.33  ad 		break;
    418  1.33  ad 
    419  1.33  ad 	case PARIOCSPARAM:
    420  1.33  ad 		pp = &sc->sc_param;
    421  1.33  ad 		upp = (struct parparam *)data;
    422  1.33  ad 		if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
    423  1.33  ad 		    upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
    424  1.33  ad 			return(EINVAL);
    425  1.33  ad 		pp->burst = upp->burst;
    426  1.33  ad 		pp->timo = parmstohz(upp->timo);
    427  1.33  ad 		pp->delay = parmstohz(upp->delay);
    428  1.33  ad 		break;
    429  1.33  ad 
    430  1.33  ad 	default:
    431  1.33  ad 		return(EINVAL);
    432  1.33  ad 	}
    433  1.33  ad 	return (error);
    434  1.33  ad }
    435  1.33  ad 
    436  1.33  ad int
    437  1.33  ad parhztoms(int h)
    438  1.33  ad {
    439  1.33  ad 	int m = h;
    440  1.33  ad 
    441  1.33  ad 	if (m > 0)
    442  1.33  ad 		m = m * 1000 / hz;
    443  1.33  ad 	return(m);
    444  1.33  ad }
    445  1.33  ad 
    446  1.33  ad int
    447  1.33  ad parmstohz(int m)
    448  1.33  ad {
    449  1.33  ad 	int h = m;
    450  1.33  ad 
    451  1.33  ad 	if (h > 0) {
    452  1.33  ad 		h = h * hz / 1000;
    453  1.33  ad 		if (h == 0)
    454  1.33  ad 			h = 1000 / hz;
    455  1.33  ad 	}
    456  1.33  ad 	return(h);
    457  1.33  ad }
    458  1.33  ad 
    459  1.33  ad /* stuff below here if for interrupt driven output of data thru
    460  1.33  ad    the parallel port. */
    461  1.33  ad 
    462  1.33  ad int partimeout_pending;
    463  1.33  ad int parsend_pending;
    464  1.33  ad 
    465  1.33  ad void
    466  1.33  ad parintr(void *arg)
    467  1.33  ad {
    468  1.33  ad 	int s, mask;
    469  1.33  ad 
    470  1.33  ad 	mask = (int)arg;
    471  1.33  ad 	s = splclock();
    472  1.33  ad 
    473  1.33  ad 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    474  1.33  ad 				~SICILIAN_INTR_PAR);
    475  1.33  ad 
    476  1.33  ad #ifdef DEBUG
    477  1.33  ad 	if (pardebug & PDB_INTERRUPT)
    478  1.33  ad 		printf("parintr %d(%s)\n", mask, mask ? "FLG" : "tout");
    479  1.33  ad #endif
    480  1.33  ad 	/* if invoked from timeout handler, mask will be 0,
    481  1.33  ad 	 * if from interrupt, it will contain the cia-icr mask,
    482  1.33  ad 	 * which is != 0
    483  1.33  ad 	 */
    484  1.33  ad 	if (mask) {
    485  1.33  ad 		if (partimeout_pending)
    486  1.33  ad 			callout_stop(&intr_callout);
    487  1.33  ad 		if (parsend_pending)
    488  1.33  ad 			parsend_pending = 0;
    489  1.33  ad 	}
    490  1.33  ad 
    491  1.33  ad 	/* either way, there won't be a timeout pending any longer */
    492  1.33  ad 	partimeout_pending = 0;
    493  1.33  ad 
    494  1.33  ad 	wakeup(parintr);
    495  1.33  ad 	splx(s);
    496  1.33  ad }
    497  1.33  ad 
    498  1.33  ad int
    499  1.33  ad parsendch(struct par_softc *sc, u_char ch)
    500  1.33  ad {
    501  1.33  ad 	int error = 0;
    502  1.33  ad 	int s;
    503  1.33  ad 
    504  1.33  ad 	/* if either offline, busy or out of paper, wait for that
    505  1.33  ad 	   condition to clear */
    506  1.33  ad 	s = spl1();
    507  1.33  ad 	while (!error
    508  1.33  ad 	       && (parsend_pending
    509  1.33  ad 		   || !(intio_get_sicilian_intr() & SICILIAN_STAT_PAR)))
    510  1.33  ad 	{
    511  1.33  ad 		/* wait a second, and try again */
    512  1.33  ad 		callout_reset(&intr_callout, hz, parintr, 0);
    513  1.33  ad 		partimeout_pending = 1;
    514  1.33  ad 		/* this is essentially a flipflop to have us wait for the
    515  1.33  ad 		   first character being transmitted when trying to transmit
    516  1.33  ad 		   the second, etc. */
    517  1.33  ad 		parsend_pending = 0;
    518  1.33  ad 		/* it's quite important that a parallel putc can be
    519  1.33  ad 		   interrupted, given the possibility to lock a printer
    520  1.33  ad 		   in an offline condition.. */
    521  1.33  ad 		if ((error = tsleep(parintr, PCATCH|(PZERO-1), "parsendch", 0))) {
    522  1.33  ad #ifdef DEBUG
    523  1.33  ad 			if (pardebug & PDB_INTERRUPT)
    524  1.33  ad 				printf("parsendch interrupted, error = %d\n", error);
    525  1.33  ad #endif
    526  1.33  ad 			if (partimeout_pending)
    527  1.33  ad 				callout_stop(&intr_callout);
    528  1.33  ad 
    529  1.33  ad 			partimeout_pending = 0;
    530  1.33  ad 		}
    531  1.33  ad 	}
    532  1.33  ad 
    533  1.33  ad 	if (!error) {
    534  1.33  ad #ifdef DEBUG
    535  1.33  ad 		if (pardebug & PDB_INTERRUPT)
    536  1.33  ad 			printf("#%d", ch);
    537  1.33  ad #endif
    538  1.33  ad 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_DATA, ch);
    539  1.33  ad 		DELAY(1);	/* (DELAY(1) == 1us) > 0.5us */
    540  1.33  ad 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_STROBE, 0);
    541  1.33  ad 		intio_set_sicilian_intr(intio_get_sicilian_intr() |
    542  1.33  ad 					 SICILIAN_INTR_PAR);
    543  1.33  ad 		DELAY(1);
    544  1.33  ad 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_STROBE, 1);
    545  1.33  ad 		parsend_pending = 1;
    546  1.33  ad 	}
    547  1.33  ad 
    548  1.33  ad 	splx(s);
    549  1.33  ad 
    550  1.33  ad 	return error;
    551  1.33  ad }
    552  1.33  ad 
    553  1.33  ad 
    554  1.33  ad int
    555  1.33  ad parsend(struct par_softc *sc, u_char *buf, int len)
    556  1.33  ad {
    557  1.33  ad 	int err, orig_len = len;
    558  1.33  ad 
    559  1.33  ad 	for (; len; len--, buf++)
    560  1.33  ad 		if ((err = parsendch(sc, *buf)))
    561  1.33  ad 			return err < 0 ? -EINTR : -err;
    562  1.33  ad 
    563  1.33  ad 	/* either all or nothing.. */
    564  1.33  ad 	return orig_len;
    565  1.33  ad }
    566