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