Home | History | Annotate | Line # | Download | only in dev
par.c revision 1.18
      1  1.18   thorpej /*	$NetBSD: par.c,v 1.18 1999/08/05 18:08:09 thorpej 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.12   thorpej 
    116   1.7    chopps 	if (matchname((char *)auxp, "par") && cfp->cf_unit == 0)
    117   1.7    chopps 		return(1);
    118   1.7    chopps 	return(0);
    119   1.7    chopps }
    120   1.7    chopps 
    121   1.7    chopps void
    122   1.7    chopps parattach(pdp, dp, auxp)
    123   1.7    chopps 	struct device *pdp, *dp;
    124   1.7    chopps 	void *auxp;
    125   1.1        mw {
    126   1.7    chopps 	par_softcp = (struct par_softc *)dp;
    127   1.1        mw 
    128   1.1        mw #ifdef DEBUG
    129   1.7    chopps 	if ((pardebug & PDB_NOCHECK) == 0)
    130   1.1        mw #endif
    131   1.7    chopps 		par_softcp->sc_flags = PARF_ALIVE;
    132  1.15  christos 	printf("\n");
    133   1.1        mw }
    134   1.1        mw 
    135  1.13     veego int
    136  1.13     veego paropen(dev, flags, mode, p)
    137   1.7    chopps 	dev_t dev;
    138  1.13     veego 	int flags;
    139  1.13     veego 	int mode;
    140  1.13     veego 	struct proc *p;
    141   1.1        mw {
    142   1.7    chopps 	int unit = UNIT(dev);
    143   1.7    chopps 	struct par_softc *sc = getparsp(unit);
    144   1.1        mw 
    145   1.7    chopps 	if (unit >= NPAR || (sc->sc_flags & PARF_ALIVE) == 0)
    146   1.7    chopps 		return(ENXIO);
    147   1.1        mw #ifdef DEBUG
    148   1.7    chopps 	if (pardebug & PDB_FOLLOW) {
    149  1.15  christos 		printf("paropen(%x, %x): flags %x, ",
    150   1.7    chopps 		    dev, flags, sc->sc_flags);
    151  1.15  christos 		printf ("port = $%x\n", ((ciab.pra ^ CIAB_PRA_SEL)
    152   1.7    chopps 		    & (CIAB_PRA_SEL|CIAB_PRA_BUSY|CIAB_PRA_POUT)));
    153   1.7    chopps 	}
    154   1.1        mw #endif
    155   1.7    chopps 	if (sc->sc_flags & PARF_OPEN)
    156   1.7    chopps 		return(EBUSY);
    157   1.7    chopps 	/* can either read or write, but not both */
    158   1.7    chopps 	if ((flags & (FREAD|FWRITE)) == (FREAD|FWRITE))
    159   1.7    chopps 		return EINVAL;
    160   1.1        mw 
    161   1.7    chopps 	sc->sc_flags |= PARF_OPEN;
    162   1.7    chopps 
    163   1.7    chopps 	if (flags & FREAD)
    164   1.7    chopps 		sc->sc_flags |= PARF_OREAD;
    165   1.7    chopps 	else
    166   1.7    chopps 		sc->sc_flags |= PARF_OWRITE;
    167   1.7    chopps 
    168   1.7    chopps 	sc->sc_burst = PAR_BURST;
    169   1.7    chopps 	sc->sc_timo = parmstohz(PAR_TIMO);
    170   1.7    chopps 	sc->sc_delay = parmstohz(PAR_DELAY);
    171   1.7    chopps 	/* enable interrupts for CIAA-FLG */
    172   1.7    chopps 	ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_FLG;
    173   1.7    chopps 	return(0);
    174   1.1        mw }
    175   1.1        mw 
    176  1.13     veego int
    177  1.13     veego parclose(dev, flags, mode, p)
    178  1.13     veego 	dev_t dev;
    179  1.13     veego 	int flags;
    180  1.13     veego 	int mode;
    181  1.13     veego 	struct proc *p;
    182   1.1        mw {
    183   1.7    chopps   int unit = UNIT(dev);
    184   1.7    chopps   struct par_softc *sc = getparsp(unit);
    185   1.1        mw 
    186   1.1        mw #ifdef DEBUG
    187   1.1        mw   if (pardebug & PDB_FOLLOW)
    188  1.15  christos     printf("parclose(%x, %x): flags %x\n",
    189   1.1        mw 	   dev, flags, sc->sc_flags);
    190   1.1        mw #endif
    191   1.1        mw   sc->sc_flags &= ~(PARF_OPEN|PARF_OREAD|PARF_OWRITE);
    192   1.1        mw   /* don't allow interrupts for CIAA-FLG any longer */
    193   1.1        mw   ciaa.icr = CIA_ICR_FLG;
    194   1.1        mw   return(0);
    195   1.1        mw }
    196   1.1        mw 
    197   1.7    chopps void
    198   1.7    chopps parstart(arg)
    199   1.7    chopps 	void *arg;
    200   1.1        mw {
    201   1.7    chopps 	struct par_softc *sc;
    202   1.7    chopps 	int unit;
    203   1.7    chopps 
    204   1.7    chopps 	unit = (int)arg;
    205   1.7    chopps 	sc = getparsp(unit);
    206   1.1        mw #ifdef DEBUG
    207   1.7    chopps 	if (pardebug & PDB_FOLLOW)
    208  1.15  christos 		printf("parstart(%x)\n", unit);
    209   1.1        mw #endif
    210   1.7    chopps 	sc->sc_flags &= ~PARF_DELAY;
    211   1.7    chopps 	wakeup(sc);
    212   1.1        mw }
    213   1.1        mw 
    214   1.7    chopps void
    215   1.7    chopps partimo(arg)
    216   1.7    chopps 	void *arg;
    217   1.1        mw {
    218   1.7    chopps 	struct par_softc *sc;
    219   1.7    chopps 	int unit;
    220   1.7    chopps 
    221   1.7    chopps 	unit = (int) arg;
    222   1.7    chopps 	sc = getparsp(unit);
    223   1.1        mw #ifdef DEBUG
    224   1.7    chopps 	if (pardebug & PDB_FOLLOW)
    225  1.15  christos 		printf("partimo(%x)\n", unit);
    226   1.1        mw #endif
    227   1.7    chopps 	sc->sc_flags &= ~(PARF_UIO|PARF_TIMO);
    228   1.7    chopps 	wakeup(sc);
    229   1.1        mw }
    230   1.1        mw 
    231  1.13     veego int
    232  1.13     veego parread(dev, uio, flags)
    233  1.13     veego 	dev_t dev;
    234  1.13     veego 	struct uio *uio;
    235  1.13     veego 	int flags;
    236   1.1        mw {
    237   1.1        mw 
    238   1.1        mw #ifdef DEBUG
    239  1.13     veego 	if (pardebug & PDB_FOLLOW)
    240  1.15  christos 		printf("parread(%x, %p)\n", dev, uio);
    241   1.1        mw #endif
    242  1.13     veego 	return (parrw(dev, uio));
    243   1.1        mw }
    244   1.1        mw 
    245  1.13     veego 
    246  1.13     veego int
    247  1.13     veego parwrite(dev, uio, flags)
    248  1.13     veego 	dev_t dev;
    249  1.13     veego 	struct uio *uio;
    250  1.13     veego 	int flags;
    251   1.1        mw {
    252   1.1        mw 
    253   1.1        mw #ifdef DEBUG
    254  1.13     veego 	if (pardebug & PDB_FOLLOW)
    255  1.15  christos 		printf("parwrite(%x, %p)\n", dev, uio);
    256   1.1        mw #endif
    257  1.13     veego 	return (parrw(dev, uio));
    258   1.1        mw }
    259   1.1        mw 
    260  1.13     veego 
    261  1.13     veego int
    262   1.1        mw parrw(dev, uio)
    263   1.1        mw      dev_t dev;
    264   1.1        mw      register struct uio *uio;
    265   1.1        mw {
    266   1.1        mw   int unit = UNIT(dev);
    267   1.7    chopps   register struct par_softc *sc = getparsp(unit);
    268   1.1        mw   register int s, len, cnt;
    269   1.1        mw   register char *cp;
    270   1.1        mw   int error = 0, gotdata = 0;
    271   1.1        mw   int buflen;
    272   1.1        mw   char *buf;
    273   1.1        mw 
    274  1.13     veego   len = 0;
    275  1.13     veego   cnt = 0;
    276   1.2        mw   if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
    277   1.1        mw     return EINVAL;
    278   1.1        mw 
    279   1.1        mw   if (uio->uio_resid == 0)
    280   1.1        mw     return(0);
    281   1.1        mw 
    282   1.1        mw #ifdef DEBUG
    283   1.1        mw   if (pardebug & (PDB_FOLLOW|PDB_IO))
    284  1.15  christos     printf("parrw(%x, %p, %c): burst %d, timo %d, resid %x\n",
    285   1.1        mw 	   dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
    286   1.1        mw 	   sc->sc_burst, sc->sc_timo, uio->uio_resid);
    287   1.1        mw #endif
    288   1.8    chopps   buflen = min(sc->sc_burst, uio->uio_resid);
    289   1.1        mw   buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    290   1.1        mw   sc->sc_flags |= PARF_UIO;
    291   1.2        mw   if (sc->sc_timo > 0)
    292   1.2        mw     {
    293   1.2        mw       sc->sc_flags |= PARF_TIMO;
    294   1.7    chopps       timeout(partimo, (void *)unit, sc->sc_timo);
    295   1.1        mw     }
    296   1.2        mw   while (uio->uio_resid > 0)
    297   1.2        mw     {
    298   1.8    chopps       len = min(buflen, uio->uio_resid);
    299   1.2        mw       cp = buf;
    300   1.2        mw       if (uio->uio_rw == UIO_WRITE)
    301   1.2        mw 	{
    302   1.2        mw 	  error = uiomove(cp, len, uio);
    303   1.2        mw 	  if (error)
    304   1.2        mw 	    break;
    305   1.2        mw 	}
    306   1.2        mw again:
    307   1.2        mw       s = splbio();
    308   1.2        mw #if 0
    309   1.2        mw       if ((sc->sc_flags & PARF_UIO) && hpibreq(&sc->sc_dq) == 0)
    310   1.2        mw 	sleep(sc, PRIBIO+1);
    311   1.2        mw #endif
    312   1.2        mw       /*
    313   1.2        mw        * Check if we timed out during sleep or uiomove
    314   1.2        mw        */
    315  1.18   thorpej       (void) spllowersoftclock();
    316   1.2        mw       if ((sc->sc_flags & PARF_UIO) == 0)
    317   1.2        mw 	{
    318   1.2        mw #ifdef DEBUG
    319   1.2        mw 	  if (pardebug & PDB_IO)
    320  1.15  christos 	    printf("parrw: uiomove/sleep timo, flags %x\n",
    321   1.2        mw 		   sc->sc_flags);
    322   1.2        mw #endif
    323   1.2        mw 	  if (sc->sc_flags & PARF_TIMO)
    324   1.2        mw 	    {
    325   1.7    chopps 	      untimeout(partimo, (void *)unit);
    326   1.2        mw 	      sc->sc_flags &= ~PARF_TIMO;
    327   1.2        mw 	    }
    328   1.2        mw 	  splx(s);
    329   1.2        mw 	  break;
    330   1.2        mw 	}
    331   1.2        mw       splx(s);
    332   1.2        mw       /*
    333   1.2        mw        * Perform the operation
    334   1.2        mw        */
    335   1.2        mw       if (uio->uio_rw == UIO_WRITE)
    336   1.2        mw 	cnt = parsend (cp, len);
    337   1.2        mw       else
    338   1.2        mw 	cnt = parreceive (cp, len);
    339   1.2        mw 
    340   1.2        mw       if (cnt < 0)
    341   1.2        mw 	{
    342   1.2        mw 	  error = -cnt;
    343   1.2        mw 	  break;
    344   1.2        mw 	}
    345   1.2        mw 
    346   1.2        mw       s = splbio();
    347   1.1        mw #if 0
    348   1.2        mw       hpibfree(&sc->sc_dq);
    349   1.1        mw #endif
    350   1.1        mw #ifdef DEBUG
    351   1.1        mw       if (pardebug & PDB_IO)
    352  1.15  christos 	printf("parrw: %s(%p, %d) -> %d\n",
    353   1.2        mw 	       uio->uio_rw == UIO_READ ? "recv" : "send", cp, len, cnt);
    354   1.1        mw #endif
    355   1.1        mw       splx(s);
    356   1.2        mw       if (uio->uio_rw == UIO_READ)
    357   1.2        mw 	{
    358   1.2        mw 	  if (cnt)
    359   1.2        mw 	    {
    360   1.2        mw 	      error = uiomove(cp, cnt, uio);
    361   1.2        mw 	      if (error)
    362   1.2        mw 		break;
    363   1.2        mw 	      gotdata++;
    364   1.2        mw 	    }
    365   1.2        mw 	  /*
    366   1.2        mw 	   * Didn't get anything this time, but did in the past.
    367   1.2        mw 	   * Consider us done.
    368   1.2        mw 	   */
    369   1.2        mw 	  else if (gotdata)
    370   1.2        mw 	    break;
    371   1.2        mw 	}
    372   1.2        mw       s = splsoftclock();
    373   1.1        mw       /*
    374   1.2        mw        * Operation timeout (or non-blocking), quit now.
    375   1.1        mw        */
    376   1.2        mw       if ((sc->sc_flags & PARF_UIO) == 0)
    377   1.2        mw 	{
    378   1.1        mw #ifdef DEBUG
    379   1.2        mw 	  if (pardebug & PDB_IO)
    380  1.15  christos 	    printf("parrw: timeout/done\n");
    381   1.1        mw #endif
    382   1.2        mw 	  splx(s);
    383   1.2        mw 	  break;
    384   1.2        mw 	}
    385   1.2        mw       /*
    386   1.2        mw        * Implement inter-read delay
    387   1.2        mw        */
    388   1.2        mw       if (sc->sc_delay > 0)
    389   1.2        mw 	{
    390   1.2        mw 	  sc->sc_flags |= PARF_DELAY;
    391   1.7    chopps 	  timeout(parstart, (void *)unit, sc->sc_delay);
    392  1.13     veego 	  error = tsleep(sc, PCATCH | (PZERO - 1), "par-cdelay", 0);
    393   1.2        mw 	  if (error)
    394   1.2        mw 	    {
    395   1.2        mw 	      splx(s);
    396   1.2        mw 	      break;
    397   1.2        mw 	    }
    398   1.2        mw 	}
    399   1.1        mw       splx(s);
    400   1.2        mw       /*
    401   1.2        mw        * Must not call uiomove again til we've used all data
    402   1.2        mw        * that we already grabbed.
    403   1.2        mw        */
    404   1.2        mw       if (uio->uio_rw == UIO_WRITE && cnt != len)
    405   1.2        mw 	{
    406   1.2        mw 	  cp += cnt;
    407   1.2        mw 	  len -= cnt;
    408   1.2        mw 	  cnt = 0;
    409   1.2        mw 	  goto again;
    410   1.2        mw 	}
    411   1.1        mw     }
    412   1.2        mw   s = splsoftclock();
    413   1.2        mw   if (sc->sc_flags & PARF_TIMO)
    414   1.2        mw     {
    415   1.7    chopps       untimeout(partimo, (void *)unit);
    416   1.2        mw       sc->sc_flags &= ~PARF_TIMO;
    417   1.1        mw     }
    418   1.2        mw   if (sc->sc_flags & PARF_DELAY)
    419   1.2        mw     {
    420   1.7    chopps       untimeout(parstart, (void *)unit);
    421   1.2        mw       sc->sc_flags &= ~PARF_DELAY;
    422   1.1        mw     }
    423   1.1        mw   splx(s);
    424   1.1        mw   /*
    425   1.1        mw    * Adjust for those chars that we uiomove'ed but never wrote
    426   1.1        mw    */
    427   1.2        mw   if (uio->uio_rw == UIO_WRITE && cnt != len)
    428   1.2        mw     {
    429   1.2        mw       uio->uio_resid += (len - cnt);
    430   1.1        mw #ifdef DEBUG
    431   1.2        mw       if (pardebug & PDB_IO)
    432  1.15  christos 	printf("parrw: short write, adjust by %d\n",
    433   1.2        mw 	       len-cnt);
    434   1.1        mw #endif
    435   1.2        mw     }
    436   1.1        mw   free(buf, M_DEVBUF);
    437   1.1        mw #ifdef DEBUG
    438   1.1        mw   if (pardebug & (PDB_FOLLOW|PDB_IO))
    439  1.15  christos     printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
    440   1.1        mw #endif
    441   1.1        mw   return (error);
    442   1.1        mw }
    443   1.1        mw 
    444   1.2        mw int
    445   1.5    chopps parioctl(dev, cmd, data, flag, p)
    446   1.5    chopps 	dev_t dev;
    447  1.11    chopps 	u_long cmd;
    448   1.5    chopps 	caddr_t data;
    449   1.5    chopps 	int flag;
    450   1.5    chopps 	struct proc *p;
    451   1.1        mw {
    452   1.7    chopps   struct par_softc *sc = getparsp(UNIT(dev));
    453   1.1        mw   struct parparam *pp, *upp;
    454   1.1        mw   int error = 0;
    455   1.1        mw 
    456   1.2        mw   switch (cmd)
    457   1.2        mw     {
    458   1.2        mw     case PARIOCGPARAM:
    459   1.2        mw       pp = &sc->sc_param;
    460   1.2        mw       upp = (struct parparam *)data;
    461   1.2        mw       upp->burst = pp->burst;
    462   1.2        mw       upp->timo = parhztoms(pp->timo);
    463   1.2        mw       upp->delay = parhztoms(pp->delay);
    464   1.2        mw       break;
    465   1.2        mw 
    466   1.2        mw     case PARIOCSPARAM:
    467   1.2        mw       pp = &sc->sc_param;
    468   1.2        mw       upp = (struct parparam *)data;
    469   1.2        mw       if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
    470   1.2        mw 	  upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
    471   1.2        mw 	return(EINVAL);
    472   1.2        mw       pp->burst = upp->burst;
    473   1.2        mw       pp->timo = parmstohz(upp->timo);
    474   1.2        mw       pp->delay = parmstohz(upp->delay);
    475   1.2        mw       break;
    476   1.2        mw 
    477   1.2        mw     default:
    478   1.1        mw       return(EINVAL);
    479   1.2        mw     }
    480   1.1        mw   return (error);
    481   1.1        mw }
    482   1.1        mw 
    483   1.2        mw int
    484   1.1        mw parhztoms(h)
    485   1.2        mw      int h;
    486   1.1        mw {
    487   1.1        mw   extern int hz;
    488   1.1        mw   register int m = h;
    489   1.1        mw 
    490   1.1        mw   if (m > 0)
    491   1.1        mw     m = m * 1000 / hz;
    492   1.1        mw   return(m);
    493   1.1        mw }
    494   1.1        mw 
    495   1.2        mw int
    496   1.1        mw parmstohz(m)
    497   1.2        mw      int m;
    498   1.1        mw {
    499   1.1        mw   extern int hz;
    500   1.1        mw   register int h = m;
    501   1.1        mw 
    502   1.1        mw   if (h > 0) {
    503   1.1        mw     h = h * hz / 1000;
    504   1.1        mw     if (h == 0)
    505   1.1        mw       h = 1000 / hz;
    506   1.1        mw   }
    507   1.1        mw   return(h);
    508   1.1        mw }
    509   1.1        mw 
    510   1.2        mw /* stuff below here if for interrupt driven output of data thru
    511   1.2        mw    the parallel port. */
    512   1.2        mw 
    513   1.2        mw int partimeout_pending;
    514   1.2        mw int parsend_pending;
    515   1.2        mw 
    516   1.1        mw void
    517   1.7    chopps parintr(arg)
    518   1.7    chopps 	void *arg;
    519   1.1        mw {
    520   1.7    chopps 	int s, mask;
    521   1.7    chopps 
    522   1.7    chopps 	mask = (int)arg;
    523   1.7    chopps 	s = splclock();
    524   1.7    chopps 
    525   1.2        mw #ifdef DEBUG
    526   1.7    chopps 	if (pardebug & PDB_INTERRUPT)
    527  1.15  christos 		printf("parintr %s\n", mask ? "FLG" : "tout");
    528   1.7    chopps #endif
    529   1.7    chopps 	/*
    530   1.7    chopps 	 * if invoked from timeout handler, mask will be 0,
    531   1.7    chopps 	 * if from interrupt, it will contain the cia-icr mask,
    532   1.7    chopps 	 * which is != 0
    533   1.7    chopps 	 */
    534   1.7    chopps 	if (mask) {
    535   1.7    chopps 		if (partimeout_pending)
    536   1.7    chopps 			untimeout(parintr, 0);
    537   1.7    chopps 		if (parsend_pending)
    538   1.7    chopps 			parsend_pending = 0;
    539   1.7    chopps 	}
    540   1.2        mw 
    541   1.7    chopps 	/* either way, there won't be a timeout pending any longer */
    542   1.7    chopps 	partimeout_pending = 0;
    543   1.2        mw 
    544   1.7    chopps 	wakeup(parintr);
    545   1.7    chopps 	splx(s);
    546   1.2        mw }
    547   1.2        mw 
    548   1.2        mw int
    549   1.2        mw parsendch (ch)
    550   1.2        mw      u_char ch;
    551   1.2        mw {
    552   1.2        mw   int error = 0;
    553   1.2        mw   int s;
    554   1.2        mw 
    555   1.2        mw   /* if either offline, busy or out of paper, wait for that
    556   1.2        mw      condition to clear */
    557   1.2        mw   s = splclock();
    558   1.2        mw   while (!error
    559   1.2        mw 	 && (parsend_pending
    560   1.2        mw 	     || ((ciab.pra ^ CIAB_PRA_SEL)
    561   1.2        mw 		 & (CIAB_PRA_SEL|CIAB_PRA_BUSY|CIAB_PRA_POUT))))
    562   1.2        mw     {
    563   1.2        mw       extern int hz;
    564   1.2        mw 
    565   1.2        mw #ifdef DEBUG
    566   1.2        mw       if (pardebug & PDB_INTERRUPT)
    567  1.15  christos 	printf ("parsendch, port = $%x\n",
    568   1.2        mw 		((ciab.pra ^ CIAB_PRA_SEL)
    569   1.2        mw 		 & (CIAB_PRA_SEL|CIAB_PRA_BUSY|CIAB_PRA_POUT)));
    570   1.2        mw #endif
    571   1.2        mw       /* wait a second, and try again */
    572   1.7    chopps       timeout(parintr, 0, hz);
    573   1.2        mw       partimeout_pending = 1;
    574   1.2        mw       /* this is essentially a flipflop to have us wait for the
    575   1.2        mw 	 first character being transmitted when trying to transmit
    576   1.2        mw 	 the second, etc. */
    577   1.2        mw       parsend_pending = 0;
    578   1.2        mw       /* it's quite important that a parallel putc can be
    579   1.2        mw 	 interrupted, given the possibility to lock a printer
    580   1.2        mw 	 in an offline condition.. */
    581  1.13     veego       if ((error = tsleep(parintr, PCATCH | (PZERO - 1), "parsendch", 0)) > 0)
    582   1.2        mw 	{
    583   1.2        mw #ifdef DEBUG
    584   1.2        mw 	  if (pardebug & PDB_INTERRUPT)
    585  1.15  christos 	    printf ("parsendch interrupted, error = %d\n", error);
    586   1.2        mw #endif
    587   1.2        mw 	  if (partimeout_pending)
    588   1.7    chopps 	    untimeout(parintr, 0);
    589   1.2        mw 
    590   1.2        mw 	  partimeout_pending = 0;
    591   1.2        mw 	}
    592   1.2        mw     }
    593   1.2        mw 
    594   1.2        mw   if (! error)
    595   1.2        mw     {
    596   1.2        mw #ifdef DEBUG
    597   1.2        mw       if (pardebug & PDB_INTERRUPT)
    598  1.15  christos 	printf ("#%d", ch);
    599   1.2        mw #endif
    600   1.2        mw       ciaa.prb = ch;
    601   1.2        mw       parsend_pending = 1;
    602   1.2        mw     }
    603   1.2        mw 
    604   1.2        mw   splx (s);
    605   1.2        mw 
    606   1.2        mw   return error;
    607   1.1        mw }
    608   1.1        mw 
    609   1.2        mw 
    610   1.1        mw int
    611   1.1        mw parsend (buf, len)
    612   1.1        mw      u_char *buf;
    613   1.1        mw      int len;
    614   1.1        mw {
    615   1.2        mw   int err, orig_len = len;
    616   1.2        mw 
    617   1.1        mw   /* make sure I/O lines are setup right for output */
    618   1.1        mw 
    619   1.1        mw   /* control lines set to input */
    620   1.1        mw   ciab.ddra &= ~(CIAB_PRA_SEL|CIAB_PRA_POUT|CIAB_PRA_BUSY);
    621   1.1        mw   /* data lines to output */
    622   1.1        mw   ciaa.ddrb = 0xff;
    623   1.1        mw 
    624   1.2        mw   for (; len; len--, buf++)
    625  1.13     veego     if ((err = parsendch (*buf)) != 0)
    626   1.2        mw       return err < 0 ? -EINTR : -err;
    627   1.2        mw 
    628   1.2        mw   /* either all or nothing.. */
    629   1.2        mw   return orig_len;
    630   1.1        mw }
    631   1.1        mw 
    632   1.1        mw 
    633   1.1        mw 
    634   1.2        mw int
    635   1.2        mw parreceive (buf, len)
    636   1.2        mw      u_char *buf;
    637   1.2        mw      int len;
    638   1.2        mw {
    639   1.2        mw   /* oh deary me, something's gotta be left to be implemented
    640   1.2        mw      later... */
    641   1.2        mw   return 0;
    642   1.2        mw }
    643   1.1        mw 
    644   1.1        mw 
    645   1.1        mw #endif
    646