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