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