Home | History | Annotate | Line # | Download | only in dev
par.c revision 1.1.1.1
      1  1.1  mw /*
      2  1.1  mw  * Copyright (c) 1982, 1990 The Regents of the University of California.
      3  1.1  mw  * All rights reserved.
      4  1.1  mw  *
      5  1.1  mw  * Redistribution and use in source and binary forms, with or without
      6  1.1  mw  * modification, are permitted provided that the following conditions
      7  1.1  mw  * are met:
      8  1.1  mw  * 1. Redistributions of source code must retain the above copyright
      9  1.1  mw  *    notice, this list of conditions and the following disclaimer.
     10  1.1  mw  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  mw  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  mw  *    documentation and/or other materials provided with the distribution.
     13  1.1  mw  * 3. All advertising materials mentioning features or use of this software
     14  1.1  mw  *    must display the following acknowledgement:
     15  1.1  mw  *	This product includes software developed by the University of
     16  1.1  mw  *	California, Berkeley and its contributors.
     17  1.1  mw  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  mw  *    may be used to endorse or promote products derived from this software
     19  1.1  mw  *    without specific prior written permission.
     20  1.1  mw  *
     21  1.1  mw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  mw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  mw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  mw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  mw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  mw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  mw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  mw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  mw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  mw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  mw  * SUCH DAMAGE.
     32  1.1  mw  *
     33  1.1  mw  *	from: @(#)ppi.c	7.3 (Berkeley) 12/16/90
     34  1.1  mw  *	$Id: par.c,v 1.1.1.1 1993/09/02 16:53:41 mw Exp $
     35  1.1  mw  */
     36  1.1  mw 
     37  1.1  mw /*
     38  1.1  mw  * parallel port interface
     39  1.1  mw 
     40  1.1  mw    XXXX NOT YET WORKING  XXXXX
     41  1.1  mw  */
     42  1.1  mw 
     43  1.1  mw #include "ppi.h"
     44  1.1  mw #if NPAR > 0
     45  1.1  mw 
     46  1.1  mw #include "sys/param.h"
     47  1.1  mw #include "sys/errno.h"
     48  1.1  mw #include "sys/uio.h"
     49  1.1  mw #include "sys/malloc.h"
     50  1.1  mw 
     51  1.1  mw #include "device.h"
     52  1.1  mw #include "parioctl.h"
     53  1.1  mw 
     54  1.1  mw int	parattach(), parstart(), partimo(), parintr();
     55  1.1  mw struct	driver pardriver = {
     56  1.1  mw 	parattach, "par", parstart,
     57  1.1  mw };
     58  1.1  mw 
     59  1.1  mw struct	par_softc {
     60  1.1  mw 	int	sc_flags;
     61  1.1  mw 	struct	amiga_device *sc_ad;
     62  1.1  mw 	struct	parparam sc_param;
     63  1.1  mw #define sc_burst sc_param.burst
     64  1.1  mw #define sc_timo  sc_param.timo
     65  1.1  mw #define sc_delay sc_param.delay
     66  1.1  mw } par_softc[NPPI];
     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.1  mw int	pardebug = 0x80;
     81  1.1  mw #define PDB_FOLLOW	0x01
     82  1.1  mw #define PDB_IO		0x02
     83  1.1  mw #define PDB_NOCHECK	0x80
     84  1.1  mw #endif
     85  1.1  mw 
     86  1.1  mw parattach(ad)
     87  1.1  mw      register struct amiga_device *ad;
     88  1.1  mw {
     89  1.1  mw   register struct par_softc *sc = &par_softc[ad->amiga_unit];
     90  1.1  mw 
     91  1.1  mw #ifdef DEBUG
     92  1.1  mw   if ((pardebug & PDB_NOCHECK) == 0)
     93  1.1  mw #endif
     94  1.1  mw     sc->sc_flags = PARF_ALIVE;
     95  1.1  mw   sc->sc_ad = ad;
     96  1.1  mw   return(1);
     97  1.1  mw }
     98  1.1  mw 
     99  1.1  mw paropen(dev, flags)
    100  1.1  mw      dev_t dev;
    101  1.1  mw {
    102  1.1  mw   register int unit = UNIT(dev);
    103  1.1  mw   register struct par_softc *sc = &par_softc[unit];
    104  1.1  mw 
    105  1.1  mw   if (unit >= NPAR || (sc->sc_flags & PARF_ALIVE) == 0)
    106  1.1  mw     return(ENXIO);
    107  1.1  mw #ifdef DEBUG
    108  1.1  mw   if (pardebug & PDB_FOLLOW)
    109  1.1  mw     printf("paropen(%x, %x): flags %x\n",
    110  1.1  mw 	   dev, flags, sc->sc_flags);
    111  1.1  mw #endif
    112  1.1  mw   if (sc->sc_flags & PARF_OPEN)
    113  1.1  mw     return(EBUSY);
    114  1.1  mw   /* can either read or write, but not both */
    115  1.1  mw   if ((flags & (FREAD|FWRITE)) == (FREAD|FWRITE))
    116  1.1  mw     return EINVAL;
    117  1.1  mw 
    118  1.1  mw   sc->sc_flags |= PARF_OPEN;
    119  1.1  mw   if (flags & FREAD)
    120  1.1  mw     sc->sc_flags |= PARF_OREAD;
    121  1.1  mw   else
    122  1.1  mw     sc->sc_flags |= PARF_OWRITE;
    123  1.1  mw   sc->sc_burst = PAR_BURST;
    124  1.1  mw   sc->sc_timo = parmstohz(PAR_TIMO);
    125  1.1  mw   sc->sc_delay = parmstohz(PAR_DELAY);
    126  1.1  mw   /* enable interrupts for CIAA-FLG */
    127  1.1  mw   ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_FLG;
    128  1.1  mw   return(0);
    129  1.1  mw }
    130  1.1  mw 
    131  1.1  mw parclose(dev, flags)
    132  1.1  mw      dev_t dev;
    133  1.1  mw {
    134  1.1  mw   register int unit = UNIT(dev);
    135  1.1  mw   register struct par_softc *sc = &par_softc[unit];
    136  1.1  mw 
    137  1.1  mw #ifdef DEBUG
    138  1.1  mw   if (pardebug & PDB_FOLLOW)
    139  1.1  mw     printf("parclose(%x, %x): flags %x\n",
    140  1.1  mw 	   dev, flags, sc->sc_flags);
    141  1.1  mw #endif
    142  1.1  mw   sc->sc_flags &= ~(PARF_OPEN|PARF_OREAD|PARF_OWRITE);
    143  1.1  mw   /* don't allow interrupts for CIAA-FLG any longer */
    144  1.1  mw   ciaa.icr = CIA_ICR_FLG;
    145  1.1  mw   return(0);
    146  1.1  mw }
    147  1.1  mw 
    148  1.1  mw parstart(unit)
    149  1.1  mw      int unit;
    150  1.1  mw {
    151  1.1  mw #ifdef DEBUG
    152  1.1  mw   if (pardebug & PDB_FOLLOW)
    153  1.1  mw     printf("parstart(%x)\n", unit);
    154  1.1  mw #endif
    155  1.1  mw   par_softc[unit].sc_flags &= ~PARF_DELAY;
    156  1.1  mw   wakeup(&par_softc[unit]);
    157  1.1  mw }
    158  1.1  mw 
    159  1.1  mw partimo(unit)
    160  1.1  mw      int unit;
    161  1.1  mw {
    162  1.1  mw #ifdef DEBUG
    163  1.1  mw   if (pardebug & PDB_FOLLOW)
    164  1.1  mw     printf("partimo(%x)\n", unit);
    165  1.1  mw #endif
    166  1.1  mw   par_softc[unit].sc_flags &= ~(PARF_UIO|PARF_TIMO);
    167  1.1  mw   wakeup(&par_softc[unit]);
    168  1.1  mw }
    169  1.1  mw 
    170  1.1  mw parread(dev, uio)
    171  1.1  mw      dev_t dev;
    172  1.1  mw      struct uio *uio;
    173  1.1  mw {
    174  1.1  mw 
    175  1.1  mw #ifdef DEBUG
    176  1.1  mw   if (pardebug & PDB_FOLLOW)
    177  1.1  mw     printf("parread(%x, %x)\n", dev, uio);
    178  1.1  mw #endif
    179  1.1  mw   return (parrw(dev, uio));
    180  1.1  mw }
    181  1.1  mw 
    182  1.1  mw parwrite(dev, uio)
    183  1.1  mw      dev_t dev;
    184  1.1  mw      struct uio *uio;
    185  1.1  mw {
    186  1.1  mw 
    187  1.1  mw #ifdef DEBUG
    188  1.1  mw   if (pardebug & PDB_FOLLOW)
    189  1.1  mw     printf("parwrite(%x, %x)\n", dev, uio);
    190  1.1  mw #endif
    191  1.1  mw   return (parrw(dev, uio));
    192  1.1  mw }
    193  1.1  mw 
    194  1.1  mw parrw(dev, uio)
    195  1.1  mw      dev_t dev;
    196  1.1  mw      register struct uio *uio;
    197  1.1  mw {
    198  1.1  mw   int unit = UNIT(dev);
    199  1.1  mw   register struct par_softc *sc = &par_softc[unit];
    200  1.1  mw   register int s, len, cnt;
    201  1.1  mw   register char *cp;
    202  1.1  mw   int error = 0, gotdata = 0;
    203  1.1  mw   int buflen;
    204  1.1  mw   char *buf;
    205  1.1  mw 
    206  1.1  mw   if ((sc->sc_flags & PARF_OREAD) ^^ (uio->uio_rw == UIO_READ))
    207  1.1  mw     return EINVAL;
    208  1.1  mw 
    209  1.1  mw   if (uio->uio_resid == 0)
    210  1.1  mw     return(0);
    211  1.1  mw 
    212  1.1  mw #ifdef DEBUG
    213  1.1  mw   if (pardebug & (PDB_FOLLOW|PDB_IO))
    214  1.1  mw     printf("parrw(%x, %x, %c): burst %d, timo %d, resid %x\n",
    215  1.1  mw 	   dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
    216  1.1  mw 	   sc->sc_burst, sc->sc_timo, uio->uio_resid);
    217  1.1  mw #endif
    218  1.1  mw   buflen = MIN(sc->sc_burst, uio->uio_resid);
    219  1.1  mw   buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    220  1.1  mw   sc->sc_flags |= PARF_UIO;
    221  1.1  mw   if (sc->sc_timo > 0) {
    222  1.1  mw     sc->sc_flags |= PARF_TIMO;
    223  1.1  mw     timeout(partimo, unit, sc->sc_timo);
    224  1.1  mw   }
    225  1.1  mw   while (uio->uio_resid > 0) {
    226  1.1  mw     len = MIN(buflen, uio->uio_resid);
    227  1.1  mw     cp = buf;
    228  1.1  mw     if (uio->uio_rw == UIO_WRITE) {
    229  1.1  mw       error = uiomove(cp, len, uio);
    230  1.1  mw       if (error)
    231  1.1  mw 	break;
    232  1.1  mw     }
    233  1.1  mw   again:
    234  1.1  mw     s = splbio();
    235  1.1  mw #if 0
    236  1.1  mw     if ((sc->sc_flags & PARF_UIO) && hpibreq(&sc->sc_dq) == 0)
    237  1.1  mw       sleep(sc, PRIBIO+1);
    238  1.1  mw #endif
    239  1.1  mw     /*
    240  1.1  mw      * Check if we timed out during sleep or uiomove
    241  1.1  mw      */
    242  1.1  mw     (void) splsoftclock();
    243  1.1  mw     if ((sc->sc_flags & PARF_UIO) == 0) {
    244  1.1  mw #ifdef DEBUG
    245  1.1  mw       if (pardebug & PDB_IO)
    246  1.1  mw 	printf("parrw: uiomove/sleep timo, flags %x\n",
    247  1.1  mw 	       sc->sc_flags);
    248  1.1  mw #endif
    249  1.1  mw       if (sc->sc_flags & PARF_TIMO) {
    250  1.1  mw 	untimeout(partimo, unit);
    251  1.1  mw 	sc->sc_flags &= ~PARF_TIMO;
    252  1.1  mw       }
    253  1.1  mw       splx(s);
    254  1.1  mw       break;
    255  1.1  mw     }
    256  1.1  mw     splx(s);
    257  1.1  mw     /*
    258  1.1  mw      * Perform the operation
    259  1.1  mw      */
    260  1.1  mw     if (uio->uio_rw == UIO_WRITE)
    261  1.1  mw       cnt = hpibsend(sc->sc_ad->amiga_ctlr, sc->sc_ad->amiga_slave,
    262  1.1  mw 		     sc->sc_sec, cp, len);
    263  1.1  mw     else
    264  1.1  mw       cnt = hpibrecv(sc->sc_ad->amiga_ctlr, sc->sc_ad->amiga_slave,
    265  1.1  mw 		     sc->sc_sec, cp, len);
    266  1.1  mw     s = splbio();
    267  1.1  mw     hpibfree(&sc->sc_dq);
    268  1.1  mw #ifdef DEBUG
    269  1.1  mw     if (pardebug & PDB_IO)
    270  1.1  mw       printf("parrw: %s(%d, %d, %x, %x, %d) -> %d\n",
    271  1.1  mw 	     uio->uio_rw == UIO_READ ? "recv" : "send",
    272  1.1  mw 	     sc->sc_ad->amiga_ctlr, sc->sc_ad->amiga_slave,
    273  1.1  mw 	     sc->sc_sec, cp, len, cnt);
    274  1.1  mw #endif
    275  1.1  mw     splx(s);
    276  1.1  mw     if (uio->uio_rw == UIO_READ) {
    277  1.1  mw       if (cnt) {
    278  1.1  mw 	error = uiomove(cp, cnt, uio);
    279  1.1  mw 	if (error)
    280  1.1  mw 	  break;
    281  1.1  mw 	gotdata++;
    282  1.1  mw       }
    283  1.1  mw       /*
    284  1.1  mw        * Didn't get anything this time, but did in the past.
    285  1.1  mw        * Consider us done.
    286  1.1  mw        */
    287  1.1  mw       else if (gotdata)
    288  1.1  mw 	break;
    289  1.1  mw     }
    290  1.1  mw     s = splsoftclock();
    291  1.1  mw     /*
    292  1.1  mw      * Operation timeout (or non-blocking), quit now.
    293  1.1  mw      */
    294  1.1  mw     if ((sc->sc_flags & PARF_UIO) == 0) {
    295  1.1  mw #ifdef DEBUG
    296  1.1  mw       if (pardebug & PDB_IO)
    297  1.1  mw 	printf("parrw: timeout/done\n");
    298  1.1  mw #endif
    299  1.1  mw       splx(s);
    300  1.1  mw       break;
    301  1.1  mw     }
    302  1.1  mw     /*
    303  1.1  mw      * Implement inter-read delay
    304  1.1  mw      */
    305  1.1  mw     if (sc->sc_delay > 0) {
    306  1.1  mw       sc->sc_flags |= PARF_DELAY;
    307  1.1  mw       timeout(parstart, unit, sc->sc_delay);
    308  1.1  mw       error = tsleep(sc, PCATCH|PZERO-1, "par-cdelay", 0);
    309  1.1  mw       if (error) {
    310  1.1  mw 	splx(s);
    311  1.1  mw 	break;
    312  1.1  mw       }
    313  1.1  mw     }
    314  1.1  mw     splx(s);
    315  1.1  mw     /*
    316  1.1  mw      * Must not call uiomove again til we've used all data
    317  1.1  mw      * that we already grabbed.
    318  1.1  mw      */
    319  1.1  mw     if (uio->uio_rw == UIO_WRITE && cnt != len) {
    320  1.1  mw       cp += cnt;
    321  1.1  mw       len -= cnt;
    322  1.1  mw       cnt = 0;
    323  1.1  mw       goto again;
    324  1.1  mw     }
    325  1.1  mw   }
    326  1.1  mw   s = splsoftclock();
    327  1.1  mw   if (sc->sc_flags & PARF_TIMO) {
    328  1.1  mw     untimeout(partimo, unit);
    329  1.1  mw     sc->sc_flags &= ~PARF_TIMO;
    330  1.1  mw   }
    331  1.1  mw   if (sc->sc_flags & PARF_DELAY) {
    332  1.1  mw     untimeout(parstart, unit);
    333  1.1  mw     sc->sc_flags &= ~PARF_DELAY;
    334  1.1  mw   }
    335  1.1  mw   splx(s);
    336  1.1  mw   /*
    337  1.1  mw    * Adjust for those chars that we uiomove'ed but never wrote
    338  1.1  mw    */
    339  1.1  mw   if (uio->uio_rw == UIO_WRITE && cnt != len) {
    340  1.1  mw     uio->uio_resid += (len - cnt);
    341  1.1  mw #ifdef DEBUG
    342  1.1  mw     if (pardebug & PDB_IO)
    343  1.1  mw       printf("parrw: short write, adjust by %d\n",
    344  1.1  mw 	     len-cnt);
    345  1.1  mw #endif
    346  1.1  mw   }
    347  1.1  mw   free(buf, M_DEVBUF);
    348  1.1  mw #ifdef DEBUG
    349  1.1  mw   if (pardebug & (PDB_FOLLOW|PDB_IO))
    350  1.1  mw     printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
    351  1.1  mw #endif
    352  1.1  mw   return (error);
    353  1.1  mw }
    354  1.1  mw 
    355  1.1  mw parioctl(dev, cmd, data, flag)
    356  1.1  mw dev_t dev;
    357  1.1  mw int cmd;
    358  1.1  mw caddr_t data;
    359  1.1  mw int flag;
    360  1.1  mw {
    361  1.1  mw   struct par_softc *sc = &par_softc[UNIT(dev)];
    362  1.1  mw   struct parparam *pp, *upp;
    363  1.1  mw   int error = 0;
    364  1.1  mw 
    365  1.1  mw   switch (cmd) {
    366  1.1  mw   case PARIOCGPARAM:
    367  1.1  mw     pp = &sc->sc_param;
    368  1.1  mw     upp = (struct parparam *)data;
    369  1.1  mw     upp->burst = pp->burst;
    370  1.1  mw     upp->timo = parhztoms(pp->timo);
    371  1.1  mw     upp->delay = parhztoms(pp->delay);
    372  1.1  mw     break;
    373  1.1  mw   case PARIOCSPARAM:
    374  1.1  mw     pp = &sc->sc_param;
    375  1.1  mw     upp = (struct parparam *)data;
    376  1.1  mw     if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
    377  1.1  mw 	upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
    378  1.1  mw       return(EINVAL);
    379  1.1  mw     pp->burst = upp->burst;
    380  1.1  mw     pp->timo = parmstohz(upp->timo);
    381  1.1  mw     pp->delay = parmstohz(upp->delay);
    382  1.1  mw     break;
    383  1.1  mw   default:
    384  1.1  mw     return(EINVAL);
    385  1.1  mw   }
    386  1.1  mw   return (error);
    387  1.1  mw }
    388  1.1  mw 
    389  1.1  mw parhztoms(h)
    390  1.1  mw int h;
    391  1.1  mw {
    392  1.1  mw   extern int hz;
    393  1.1  mw   register int m = h;
    394  1.1  mw 
    395  1.1  mw   if (m > 0)
    396  1.1  mw     m = m * 1000 / hz;
    397  1.1  mw   return(m);
    398  1.1  mw }
    399  1.1  mw 
    400  1.1  mw parmstohz(m)
    401  1.1  mw int m;
    402  1.1  mw {
    403  1.1  mw   extern int hz;
    404  1.1  mw   register int h = m;
    405  1.1  mw 
    406  1.1  mw   if (h > 0) {
    407  1.1  mw     h = h * hz / 1000;
    408  1.1  mw     if (h == 0)
    409  1.1  mw       h = 1000 / hz;
    410  1.1  mw   }
    411  1.1  mw   return(h);
    412  1.1  mw }
    413  1.1  mw 
    414  1.1  mw void
    415  1.1  mw parintr (mask)
    416  1.1  mw      int mask;
    417  1.1  mw {
    418  1.1  mw   wakeup (parintr);
    419  1.1  mw }
    420  1.1  mw 
    421  1.1  mw int
    422  1.1  mw parsend (buf, len)
    423  1.1  mw      u_char *buf;
    424  1.1  mw      int len;
    425  1.1  mw {
    426  1.1  mw   /* make sure I/O lines are setup right for output */
    427  1.1  mw 
    428  1.1  mw   /* control lines set to input */
    429  1.1  mw   ciab.ddra &= ~(CIAB_PRA_SEL|CIAB_PRA_POUT|CIAB_PRA_BUSY);
    430  1.1  mw   /* data lines to output */
    431  1.1  mw   ciaa.ddrb = 0xff;
    432  1.1  mw 
    433  1.1  mw   while (len)
    434  1.1  mw     {
    435  1.1  mw 
    436  1.1  mw     }
    437  1.1  mw }
    438  1.1  mw 
    439  1.1  mw 
    440  1.1  mw 
    441  1.1  mw 
    442  1.1  mw 
    443  1.1  mw 
    444  1.1  mw #endif
    445