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