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