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