Home | History | Annotate | Line # | Download | only in dev
ppi.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1982, 1990 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  *
     33  1.2      cgd  *	from: @(#)ppi.c	7.3 (Berkeley) 12/16/90
     34  1.4  mycroft  *	$Id: ppi.c,v 1.4 1994/05/05 10:10:34 mycroft Exp $
     35  1.1      cgd  */
     36  1.1      cgd 
     37  1.1      cgd /*
     38  1.1      cgd  * Printer/Plotter HPIB interface
     39  1.1      cgd  */
     40  1.1      cgd 
     41  1.1      cgd #include "ppi.h"
     42  1.1      cgd #if NPPI > 0
     43  1.1      cgd 
     44  1.3  mycroft #include <sys/param.h>
     45  1.3  mycroft #include <sys/systm.h>
     46  1.3  mycroft #include <sys/errno.h>
     47  1.3  mycroft #include <sys/uio.h>
     48  1.3  mycroft #include <sys/malloc.h>
     49  1.1      cgd 
     50  1.3  mycroft #include <hp300/dev/device.h>
     51  1.3  mycroft #include <hp300/dev/ppiioctl.h>
     52  1.1      cgd 
     53  1.1      cgd int	ppiattach(), ppistart(), ppitimo();
     54  1.1      cgd struct	driver ppidriver = {
     55  1.1      cgd 	ppiattach, "ppi", ppistart,
     56  1.1      cgd };
     57  1.1      cgd 
     58  1.1      cgd struct	ppi_softc {
     59  1.1      cgd 	int	sc_flags;
     60  1.1      cgd 	struct	devqueue sc_dq;
     61  1.1      cgd 	struct	hp_device *sc_hd;
     62  1.1      cgd 	struct	ppiparam sc_param;
     63  1.1      cgd #define sc_burst sc_param.burst
     64  1.1      cgd #define sc_timo  sc_param.timo
     65  1.1      cgd #define sc_delay sc_param.delay
     66  1.1      cgd 	int	sc_sec;
     67  1.1      cgd } ppi_softc[NPPI];
     68  1.1      cgd 
     69  1.1      cgd /* sc_flags values */
     70  1.1      cgd #define	PPIF_ALIVE	0x01
     71  1.1      cgd #define	PPIF_OPEN	0x02
     72  1.1      cgd #define PPIF_UIO	0x04
     73  1.1      cgd #define PPIF_TIMO	0x08
     74  1.1      cgd #define PPIF_DELAY	0x10
     75  1.1      cgd 
     76  1.1      cgd #define UNIT(x)		minor(x)
     77  1.1      cgd 
     78  1.1      cgd #ifdef DEBUG
     79  1.1      cgd int	ppidebug = 0x80;
     80  1.1      cgd #define PDB_FOLLOW	0x01
     81  1.1      cgd #define PDB_IO		0x02
     82  1.1      cgd #define PDB_NOCHECK	0x80
     83  1.1      cgd #endif
     84  1.1      cgd 
     85  1.1      cgd ppiattach(hd)
     86  1.1      cgd 	register struct hp_device *hd;
     87  1.1      cgd {
     88  1.1      cgd 	register struct ppi_softc *sc = &ppi_softc[hd->hp_unit];
     89  1.1      cgd 
     90  1.1      cgd #ifdef DEBUG
     91  1.1      cgd 	if ((ppidebug & PDB_NOCHECK) == 0)
     92  1.1      cgd #endif
     93  1.1      cgd 	/*
     94  1.1      cgd 	 * XXX: the printer/plotter doesn't seem to really return
     95  1.1      cgd 	 * an ID but this will at least prevent us from mistaking
     96  1.1      cgd 	 * a cs80 disk or tape for a ppi device.
     97  1.1      cgd 	 */
     98  1.1      cgd 	if (hpibid(hd->hp_ctlr, hd->hp_slave) & 0x200)
     99  1.1      cgd 		return(0);
    100  1.1      cgd 	sc->sc_flags = PPIF_ALIVE;
    101  1.1      cgd 	sc->sc_dq.dq_ctlr = hd->hp_ctlr;
    102  1.1      cgd 	sc->sc_dq.dq_unit = hd->hp_unit;
    103  1.1      cgd 	sc->sc_dq.dq_slave = hd->hp_slave;
    104  1.1      cgd 	sc->sc_dq.dq_driver = &ppidriver;
    105  1.1      cgd 	sc->sc_hd = hd;
    106  1.1      cgd 	return(1);
    107  1.1      cgd }
    108  1.1      cgd 
    109  1.1      cgd ppiopen(dev, flags)
    110  1.1      cgd 	dev_t dev;
    111  1.1      cgd {
    112  1.1      cgd 	register int unit = UNIT(dev);
    113  1.1      cgd 	register struct ppi_softc *sc = &ppi_softc[unit];
    114  1.1      cgd 
    115  1.1      cgd 	if (unit >= NPPI || (sc->sc_flags & PPIF_ALIVE) == 0)
    116  1.1      cgd 		return(ENXIO);
    117  1.1      cgd #ifdef DEBUG
    118  1.1      cgd 	if (ppidebug & PDB_FOLLOW)
    119  1.1      cgd 		printf("ppiopen(%x, %x): flags %x\n",
    120  1.1      cgd 		       dev, flags, sc->sc_flags);
    121  1.1      cgd #endif
    122  1.1      cgd 	if (sc->sc_flags & PPIF_OPEN)
    123  1.1      cgd 		return(EBUSY);
    124  1.1      cgd 	sc->sc_flags |= PPIF_OPEN;
    125  1.1      cgd 	sc->sc_burst = PPI_BURST;
    126  1.1      cgd 	sc->sc_timo = ppimstohz(PPI_TIMO);
    127  1.1      cgd 	sc->sc_delay = ppimstohz(PPI_DELAY);
    128  1.1      cgd 	sc->sc_sec = -1;
    129  1.1      cgd 	return(0);
    130  1.1      cgd }
    131  1.1      cgd 
    132  1.1      cgd ppiclose(dev, flags)
    133  1.1      cgd 	dev_t dev;
    134  1.1      cgd {
    135  1.1      cgd 	register int unit = UNIT(dev);
    136  1.1      cgd 	register struct ppi_softc *sc = &ppi_softc[unit];
    137  1.1      cgd 
    138  1.1      cgd #ifdef DEBUG
    139  1.1      cgd 	if (ppidebug & PDB_FOLLOW)
    140  1.1      cgd 		printf("ppiclose(%x, %x): flags %x\n",
    141  1.1      cgd 		       dev, flags, sc->sc_flags);
    142  1.1      cgd #endif
    143  1.1      cgd 	sc->sc_flags &= ~PPIF_OPEN;
    144  1.1      cgd 	return(0);
    145  1.1      cgd }
    146  1.1      cgd 
    147  1.4  mycroft ppistart(arg)
    148  1.4  mycroft 	void *arg;
    149  1.1      cgd {
    150  1.4  mycroft 	int unit = (int)arg;
    151  1.4  mycroft 
    152  1.1      cgd #ifdef DEBUG
    153  1.1      cgd 	if (ppidebug & PDB_FOLLOW)
    154  1.1      cgd 		printf("ppistart(%x)\n", unit);
    155  1.1      cgd #endif
    156  1.1      cgd 	ppi_softc[unit].sc_flags &= ~PPIF_DELAY;
    157  1.1      cgd 	wakeup(&ppi_softc[unit]);
    158  1.1      cgd }
    159  1.1      cgd 
    160  1.4  mycroft ppitimo(arg)
    161  1.4  mycroft 	void *arg;
    162  1.1      cgd {
    163  1.4  mycroft 	int unit = (int)arg;
    164  1.4  mycroft 
    165  1.1      cgd #ifdef DEBUG
    166  1.1      cgd 	if (ppidebug & PDB_FOLLOW)
    167  1.1      cgd 		printf("ppitimo(%x)\n", unit);
    168  1.1      cgd #endif
    169  1.1      cgd 	ppi_softc[unit].sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
    170  1.1      cgd 	wakeup(&ppi_softc[unit]);
    171  1.1      cgd }
    172  1.1      cgd 
    173  1.1      cgd ppiread(dev, uio)
    174  1.1      cgd 	dev_t dev;
    175  1.1      cgd 	struct uio *uio;
    176  1.1      cgd {
    177  1.1      cgd 
    178  1.1      cgd #ifdef DEBUG
    179  1.1      cgd 	if (ppidebug & PDB_FOLLOW)
    180  1.1      cgd 		printf("ppiread(%x, %x)\n", dev, uio);
    181  1.1      cgd #endif
    182  1.1      cgd 	return (ppirw(dev, uio));
    183  1.1      cgd }
    184  1.1      cgd 
    185  1.1      cgd ppiwrite(dev, uio)
    186  1.1      cgd 	dev_t dev;
    187  1.1      cgd 	struct uio *uio;
    188  1.1      cgd {
    189  1.1      cgd 
    190  1.1      cgd #ifdef DEBUG
    191  1.1      cgd 	if (ppidebug & PDB_FOLLOW)
    192  1.1      cgd 		printf("ppiwrite(%x, %x)\n", dev, uio);
    193  1.1      cgd #endif
    194  1.1      cgd 	return (ppirw(dev, uio));
    195  1.1      cgd }
    196  1.1      cgd 
    197  1.1      cgd ppirw(dev, uio)
    198  1.1      cgd 	dev_t dev;
    199  1.1      cgd 	register struct uio *uio;
    200  1.1      cgd {
    201  1.1      cgd 	int unit = UNIT(dev);
    202  1.1      cgd 	register struct ppi_softc *sc = &ppi_softc[unit];
    203  1.1      cgd 	register int s, len, cnt;
    204  1.1      cgd 	register char *cp;
    205  1.1      cgd 	int error = 0, gotdata = 0;
    206  1.1      cgd 	int buflen;
    207  1.1      cgd 	char *buf;
    208  1.1      cgd 
    209  1.1      cgd 	if (uio->uio_resid == 0)
    210  1.1      cgd 		return(0);
    211  1.1      cgd 
    212  1.1      cgd #ifdef DEBUG
    213  1.1      cgd 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
    214  1.1      cgd 		printf("ppirw(%x, %x, %c): burst %d, timo %d, resid %x\n",
    215  1.1      cgd 		       dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
    216  1.1      cgd 		       sc->sc_burst, sc->sc_timo, uio->uio_resid);
    217  1.1      cgd #endif
    218  1.1      cgd 	buflen = MIN(sc->sc_burst, uio->uio_resid);
    219  1.1      cgd 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    220  1.1      cgd 	sc->sc_flags |= PPIF_UIO;
    221  1.1      cgd 	if (sc->sc_timo > 0) {
    222  1.1      cgd 		sc->sc_flags |= PPIF_TIMO;
    223  1.4  mycroft 		timeout(ppitimo, (void *)unit, sc->sc_timo);
    224  1.1      cgd 	}
    225  1.1      cgd 	while (uio->uio_resid > 0) {
    226  1.1      cgd 		len = MIN(buflen, uio->uio_resid);
    227  1.1      cgd 		cp = buf;
    228  1.1      cgd 		if (uio->uio_rw == UIO_WRITE) {
    229  1.1      cgd 			error = uiomove(cp, len, uio);
    230  1.1      cgd 			if (error)
    231  1.1      cgd 				break;
    232  1.1      cgd 		}
    233  1.1      cgd again:
    234  1.1      cgd 		s = splbio();
    235  1.1      cgd 		if ((sc->sc_flags & PPIF_UIO) && hpibreq(&sc->sc_dq) == 0)
    236  1.1      cgd 			sleep(sc, PRIBIO+1);
    237  1.1      cgd 		/*
    238  1.1      cgd 		 * Check if we timed out during sleep or uiomove
    239  1.1      cgd 		 */
    240  1.1      cgd 		(void) splsoftclock();
    241  1.1      cgd 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    242  1.1      cgd #ifdef DEBUG
    243  1.1      cgd 			if (ppidebug & PDB_IO)
    244  1.1      cgd 				printf("ppirw: uiomove/sleep timo, flags %x\n",
    245  1.1      cgd 				       sc->sc_flags);
    246  1.1      cgd #endif
    247  1.1      cgd 			if (sc->sc_flags & PPIF_TIMO) {
    248  1.4  mycroft 				untimeout(ppitimo, (void *)unit);
    249  1.1      cgd 				sc->sc_flags &= ~PPIF_TIMO;
    250  1.1      cgd 			}
    251  1.1      cgd 			splx(s);
    252  1.1      cgd 			break;
    253  1.1      cgd 		}
    254  1.1      cgd 		splx(s);
    255  1.1      cgd 		/*
    256  1.1      cgd 		 * Perform the operation
    257  1.1      cgd 		 */
    258  1.1      cgd 		if (uio->uio_rw == UIO_WRITE)
    259  1.1      cgd 			cnt = hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    260  1.1      cgd 				       sc->sc_sec, cp, len);
    261  1.1      cgd 		else
    262  1.1      cgd 			cnt = hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    263  1.1      cgd 				       sc->sc_sec, cp, len);
    264  1.1      cgd 		s = splbio();
    265  1.1      cgd 		hpibfree(&sc->sc_dq);
    266  1.1      cgd #ifdef DEBUG
    267  1.1      cgd 		if (ppidebug & PDB_IO)
    268  1.1      cgd 			printf("ppirw: %s(%d, %d, %x, %x, %d) -> %d\n",
    269  1.1      cgd 			       uio->uio_rw == UIO_READ ? "recv" : "send",
    270  1.1      cgd 			       sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
    271  1.1      cgd 			       sc->sc_sec, cp, len, cnt);
    272  1.1      cgd #endif
    273  1.1      cgd 		splx(s);
    274  1.1      cgd 		if (uio->uio_rw == UIO_READ) {
    275  1.1      cgd 			if (cnt) {
    276  1.1      cgd 				error = uiomove(cp, cnt, uio);
    277  1.1      cgd 				if (error)
    278  1.1      cgd 					break;
    279  1.1      cgd 				gotdata++;
    280  1.1      cgd 			}
    281  1.1      cgd 			/*
    282  1.1      cgd 			 * Didn't get anything this time, but did in the past.
    283  1.1      cgd 			 * Consider us done.
    284  1.1      cgd 			 */
    285  1.1      cgd 			else if (gotdata)
    286  1.1      cgd 				break;
    287  1.1      cgd 		}
    288  1.1      cgd 		s = splsoftclock();
    289  1.1      cgd 		/*
    290  1.1      cgd 		 * Operation timeout (or non-blocking), quit now.
    291  1.1      cgd 		 */
    292  1.1      cgd 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    293  1.1      cgd #ifdef DEBUG
    294  1.1      cgd 			if (ppidebug & PDB_IO)
    295  1.1      cgd 				printf("ppirw: timeout/done\n");
    296  1.1      cgd #endif
    297  1.1      cgd 			splx(s);
    298  1.1      cgd 			break;
    299  1.1      cgd 		}
    300  1.1      cgd 		/*
    301  1.1      cgd 		 * Implement inter-read delay
    302  1.1      cgd 		 */
    303  1.1      cgd 		if (sc->sc_delay > 0) {
    304  1.1      cgd 			sc->sc_flags |= PPIF_DELAY;
    305  1.4  mycroft 			timeout(ppistart, (void *)unit, sc->sc_delay);
    306  1.1      cgd 			error = tsleep(sc, PCATCH|PZERO+1, "hpib", 0);
    307  1.1      cgd 			if (error) {
    308  1.1      cgd 				splx(s);
    309  1.1      cgd 				break;
    310  1.1      cgd 			}
    311  1.1      cgd 		}
    312  1.1      cgd 		splx(s);
    313  1.1      cgd 		/*
    314  1.1      cgd 		 * Must not call uiomove again til we've used all data
    315  1.1      cgd 		 * that we already grabbed.
    316  1.1      cgd 		 */
    317  1.1      cgd 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    318  1.1      cgd 			cp += cnt;
    319  1.1      cgd 			len -= cnt;
    320  1.1      cgd 			cnt = 0;
    321  1.1      cgd 			goto again;
    322  1.1      cgd 		}
    323  1.1      cgd 	}
    324  1.1      cgd 	s = splsoftclock();
    325  1.1      cgd 	if (sc->sc_flags & PPIF_TIMO) {
    326  1.4  mycroft 		untimeout(ppitimo, (void *)unit);
    327  1.1      cgd 		sc->sc_flags &= ~PPIF_TIMO;
    328  1.1      cgd 	}
    329  1.1      cgd 	if (sc->sc_flags & PPIF_DELAY) {
    330  1.4  mycroft 		untimeout(ppistart, (void *)unit);
    331  1.1      cgd 		sc->sc_flags &= ~PPIF_DELAY;
    332  1.1      cgd 	}
    333  1.1      cgd 	splx(s);
    334  1.1      cgd 	/*
    335  1.1      cgd 	 * Adjust for those chars that we uiomove'ed but never wrote
    336  1.1      cgd 	 */
    337  1.1      cgd 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    338  1.1      cgd 		uio->uio_resid += (len - cnt);
    339  1.1      cgd #ifdef DEBUG
    340  1.1      cgd 		if (ppidebug & PDB_IO)
    341  1.1      cgd 			printf("ppirw: short write, adjust by %d\n",
    342  1.1      cgd 			       len-cnt);
    343  1.1      cgd #endif
    344  1.1      cgd 	}
    345  1.1      cgd 	free(buf, M_DEVBUF);
    346  1.1      cgd #ifdef DEBUG
    347  1.1      cgd 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
    348  1.1      cgd 		printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
    349  1.1      cgd #endif
    350  1.1      cgd 	return (error);
    351  1.1      cgd }
    352  1.1      cgd 
    353  1.3  mycroft ppiioctl(dev, cmd, data, flag, p)
    354  1.1      cgd 	dev_t dev;
    355  1.1      cgd 	int cmd;
    356  1.1      cgd 	caddr_t data;
    357  1.1      cgd 	int flag;
    358  1.3  mycroft 	struct proc *p;
    359  1.1      cgd {
    360  1.1      cgd 	struct ppi_softc *sc = &ppi_softc[UNIT(dev)];
    361  1.1      cgd 	struct ppiparam *pp, *upp;
    362  1.1      cgd 	int error = 0;
    363  1.1      cgd 
    364  1.1      cgd 	switch (cmd) {
    365  1.1      cgd 	case PPIIOCGPARAM:
    366  1.1      cgd 		pp = &sc->sc_param;
    367  1.1      cgd 		upp = (struct ppiparam *)data;
    368  1.1      cgd 		upp->burst = pp->burst;
    369  1.1      cgd 		upp->timo = ppihztoms(pp->timo);
    370  1.1      cgd 		upp->delay = ppihztoms(pp->delay);
    371  1.1      cgd 		break;
    372  1.1      cgd 	case PPIIOCSPARAM:
    373  1.1      cgd 		pp = &sc->sc_param;
    374  1.1      cgd 		upp = (struct ppiparam *)data;
    375  1.1      cgd 		if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
    376  1.1      cgd 		    upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
    377  1.1      cgd 			return(EINVAL);
    378  1.1      cgd 		pp->burst = upp->burst;
    379  1.1      cgd 		pp->timo = ppimstohz(upp->timo);
    380  1.1      cgd 		pp->delay = ppimstohz(upp->delay);
    381  1.1      cgd 		break;
    382  1.1      cgd 	case PPIIOCSSEC:
    383  1.1      cgd 		sc->sc_sec = *(int *)data;
    384  1.1      cgd 		break;
    385  1.1      cgd 	default:
    386  1.1      cgd 		return(EINVAL);
    387  1.1      cgd 	}
    388  1.1      cgd 	return (error);
    389  1.1      cgd }
    390  1.1      cgd 
    391  1.1      cgd ppihztoms(h)
    392  1.1      cgd 	int h;
    393  1.1      cgd {
    394  1.1      cgd 	extern int hz;
    395  1.1      cgd 	register int m = h;
    396  1.1      cgd 
    397  1.1      cgd 	if (m > 0)
    398  1.1      cgd 		m = m * 1000 / hz;
    399  1.1      cgd 	return(m);
    400  1.1      cgd }
    401  1.1      cgd 
    402  1.1      cgd ppimstohz(m)
    403  1.1      cgd 	int m;
    404  1.1      cgd {
    405  1.1      cgd 	extern int hz;
    406  1.1      cgd 	register int h = m;
    407  1.1      cgd 
    408  1.1      cgd 	if (h > 0) {
    409  1.1      cgd 		h = h * hz / 1000;
    410  1.1      cgd 		if (h == 0)
    411  1.1      cgd 			h = 1000 / hz;
    412  1.1      cgd 	}
    413  1.1      cgd 	return(h);
    414  1.1      cgd }
    415  1.1      cgd #endif
    416