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