Home | History | Annotate | Line # | Download | only in dev
ppi.c revision 1.15
      1 /*	$NetBSD: ppi.c,v 1.15 1997/10/04 10:00:23 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1982, 1990, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)ppi.c	8.1 (Berkeley) 6/16/93
     72  */
     73 
     74 /*
     75  * Printer/Plotter HPIB interface
     76  */
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/conf.h>
     81 #include <sys/device.h>
     82 #include <sys/errno.h>
     83 #include <sys/malloc.h>
     84 #include <sys/proc.h>
     85 #include <sys/uio.h>
     86 
     87 #include <hp300/dev/hpibvar.h>
     88 
     89 #include <hp300/dev/ppiioctl.h>
     90 
     91 struct	ppi_softc {
     92 	struct device sc_dev;
     93 	int	sc_flags;
     94 	struct	hpibqueue sc_hq;	/* HP-IB job queue entry */
     95 	struct	ppiparam sc_param;
     96 #define sc_burst sc_param.burst
     97 #define sc_timo  sc_param.timo
     98 #define sc_delay sc_param.delay
     99 	int	sc_sec;
    100 	int	sc_slave;		/* HP-IB slave address */
    101 };
    102 
    103 /* sc_flags values */
    104 #define	PPIF_ALIVE	0x01
    105 #define	PPIF_OPEN	0x02
    106 #define PPIF_UIO	0x04
    107 #define PPIF_TIMO	0x08
    108 #define PPIF_DELAY	0x10
    109 
    110 int	ppimatch __P((struct device *, struct cfdata *, void *));
    111 void	ppiattach __P((struct device *, struct device *, void *));
    112 
    113 struct cfattach ppi_ca = {
    114 	sizeof(struct ppi_softc), ppimatch, ppiattach
    115 };
    116 
    117 struct cfdriver ppi_cd = {
    118 	NULL, "ppi", DV_DULL
    119 };
    120 
    121 void	ppistart __P((void *));
    122 void	ppinoop __P((void *));
    123 
    124 void	ppitimo __P((void *));
    125 int	ppirw __P((dev_t, struct uio *));
    126 int	ppihztoms __P((int));
    127 int	ppimstohz __P((int));
    128 
    129 bdev_decl(ppi);
    130 cdev_decl(ppi);
    131 
    132 #define UNIT(x)		minor(x)
    133 
    134 #ifdef DEBUG
    135 int	ppidebug = 0x80;
    136 #define PDB_FOLLOW	0x01
    137 #define PDB_IO		0x02
    138 #define PDB_NOCHECK	0x80
    139 #endif
    140 
    141 int
    142 ppimatch(parent, match, aux)
    143 	struct device *parent;
    144 	struct cfdata *match;
    145 	void *aux;
    146 {
    147 	struct hpibbus_attach_args *ha = aux;
    148 
    149 	/*
    150 	 * The printer/plotter doesn't return an ID tag.
    151 	 * The check below prevents us from matching a CS80
    152 	 * device by mistake.
    153 	 */
    154 	if (ha->ha_id & 0x200)
    155 		return (0);
    156 
    157 	/*
    158 	 * To prevent matching all unused slots on the bus, we
    159 	 * don't allow wildcarded locators.
    160 	 */
    161 	if (match->hpibbuscf_slave == HPIBBUSCF_SLAVE_DEFAULT ||
    162 	    match->hpibbuscf_punit == HPIBBUSCF_PUNIT_DEFAULT)
    163 		return (0);
    164 
    165 	return (1);
    166 }
    167 
    168 void
    169 ppiattach(parent, self, aux)
    170 	struct device *parent, *self;
    171 	void *aux;
    172 {
    173 	struct ppi_softc *sc = (struct ppi_softc *)self;
    174 	struct hpibbus_attach_args *ha = aux;
    175 
    176 	printf("\n");
    177 
    178 	sc->sc_slave = ha->ha_slave;
    179 
    180 	/* Initialize the hpib queue entry. */
    181 	sc->sc_hq.hq_softc = sc;
    182 	sc->sc_hq.hq_slave = sc->sc_slave;
    183 	sc->sc_hq.hq_start = ppistart;
    184 	sc->sc_hq.hq_go = ppinoop;
    185 	sc->sc_hq.hq_intr = ppinoop;
    186 
    187 	sc->sc_flags = PPIF_ALIVE;
    188 }
    189 
    190 void
    191 ppinoop(arg)
    192 	void *arg;
    193 {
    194 	/* Noop! */
    195 }
    196 
    197 int
    198 ppiopen(dev, flags, fmt, p)
    199 	dev_t dev;
    200 	int flags, fmt;
    201 	struct proc *p;
    202 {
    203 	int unit = UNIT(dev);
    204 	struct ppi_softc *sc;
    205 
    206 	if (unit >= ppi_cd.cd_ndevs ||
    207 	    (sc = ppi_cd.cd_devs[unit]) == NULL ||
    208 	    (sc->sc_flags & PPIF_ALIVE) == 0)
    209 		return (ENXIO);
    210 
    211 #ifdef DEBUG
    212 	if (ppidebug & PDB_FOLLOW)
    213 		printf("ppiopen(%x, %x): flags %x\n",
    214 		       dev, flags, sc->sc_flags);
    215 #endif
    216 	if (sc->sc_flags & PPIF_OPEN)
    217 		return (EBUSY);
    218 	sc->sc_flags |= PPIF_OPEN;
    219 	sc->sc_burst = PPI_BURST;
    220 	sc->sc_timo = ppimstohz(PPI_TIMO);
    221 	sc->sc_delay = ppimstohz(PPI_DELAY);
    222 	sc->sc_sec = -1;
    223 	return(0);
    224 }
    225 
    226 int
    227 ppiclose(dev, flags, fmt, p)
    228 	dev_t dev;
    229 	int flags, fmt;
    230 	struct proc *p;
    231 {
    232 	int unit = UNIT(dev);
    233 	struct ppi_softc *sc = ppi_cd.cd_devs[unit];
    234 
    235 #ifdef DEBUG
    236 	if (ppidebug & PDB_FOLLOW)
    237 		printf("ppiclose(%x, %x): flags %x\n",
    238 		       dev, flags, sc->sc_flags);
    239 #endif
    240 	sc->sc_flags &= ~PPIF_OPEN;
    241 	return(0);
    242 }
    243 
    244 void
    245 ppistart(arg)
    246 	void *arg;
    247 {
    248 	struct ppi_softc *sc = arg;
    249 
    250 #ifdef DEBUG
    251 	if (ppidebug & PDB_FOLLOW)
    252 		printf("ppistart(%x)\n", sc->sc_dev.dv_unit);
    253 #endif
    254 	sc->sc_flags &= ~PPIF_DELAY;
    255 	wakeup(sc);
    256 }
    257 
    258 void
    259 ppitimo(arg)
    260 	void *arg;
    261 {
    262 	struct ppi_softc *sc = arg;
    263 
    264 #ifdef DEBUG
    265 	if (ppidebug & PDB_FOLLOW)
    266 		printf("ppitimo(%x)\n", sc->sc_dev.dv_unit);
    267 #endif
    268 	sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
    269 	wakeup(sc);
    270 }
    271 
    272 int
    273 ppiread(dev, uio, flags)
    274 	dev_t dev;
    275 	struct uio *uio;
    276 	int flags;
    277 {
    278 
    279 #ifdef DEBUG
    280 	if (ppidebug & PDB_FOLLOW)
    281 		printf("ppiread(%x, %p)\n", dev, uio);
    282 #endif
    283 	return (ppirw(dev, uio));
    284 }
    285 
    286 int
    287 ppiwrite(dev, uio, flags)
    288 	dev_t dev;
    289 	struct uio *uio;
    290 	int flags;
    291 {
    292 
    293 #ifdef DEBUG
    294 	if (ppidebug & PDB_FOLLOW)
    295 		printf("ppiwrite(%x, %p)\n", dev, uio);
    296 #endif
    297 	return (ppirw(dev, uio));
    298 }
    299 
    300 int
    301 ppirw(dev, uio)
    302 	dev_t dev;
    303 	struct uio *uio;
    304 {
    305 	int unit = UNIT(dev);
    306 	struct ppi_softc *sc = ppi_cd.cd_devs[unit];
    307 	int s, len, cnt;
    308 	char *cp;
    309 	int error = 0, gotdata = 0;
    310 	int buflen, ctlr, slave;
    311 	char *buf;
    312 
    313 	if (uio->uio_resid == 0)
    314 		return(0);
    315 
    316 	ctlr = sc->sc_dev.dv_parent->dv_unit;
    317 	slave = sc->sc_slave;
    318 
    319 #ifdef DEBUG
    320 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
    321 		printf("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
    322 		       dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
    323 		       sc->sc_burst, sc->sc_timo, uio->uio_resid);
    324 #endif
    325 	buflen = min(sc->sc_burst, uio->uio_resid);
    326 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    327 	sc->sc_flags |= PPIF_UIO;
    328 	if (sc->sc_timo > 0) {
    329 		sc->sc_flags |= PPIF_TIMO;
    330 		timeout(ppitimo, sc, sc->sc_timo);
    331 	}
    332 	len = cnt = 0;
    333 	while (uio->uio_resid > 0) {
    334 		len = min(buflen, uio->uio_resid);
    335 		cp = buf;
    336 		if (uio->uio_rw == UIO_WRITE) {
    337 			error = uiomove(cp, len, uio);
    338 			if (error)
    339 				break;
    340 		}
    341 again:
    342 		s = splbio();
    343 		if ((sc->sc_flags & PPIF_UIO) &&
    344 		    hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq) == 0)
    345 			sleep(sc, PRIBIO+1);
    346 		/*
    347 		 * Check if we timed out during sleep or uiomove
    348 		 */
    349 		(void) splsoftclock();
    350 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    351 #ifdef DEBUG
    352 			if (ppidebug & PDB_IO)
    353 				printf("ppirw: uiomove/sleep timo, flags %x\n",
    354 				       sc->sc_flags);
    355 #endif
    356 			if (sc->sc_flags & PPIF_TIMO) {
    357 				untimeout(ppitimo, sc);
    358 				sc->sc_flags &= ~PPIF_TIMO;
    359 			}
    360 			splx(s);
    361 			break;
    362 		}
    363 		splx(s);
    364 		/*
    365 		 * Perform the operation
    366 		 */
    367 		if (uio->uio_rw == UIO_WRITE)
    368 			cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len);
    369 		else
    370 			cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len);
    371 		s = splbio();
    372 		hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
    373 #ifdef DEBUG
    374 		if (ppidebug & PDB_IO)
    375 			printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n",
    376 			       uio->uio_rw == UIO_READ ? "recv" : "send",
    377 			       ctlr, slave, sc->sc_sec, cp, len, cnt);
    378 #endif
    379 		splx(s);
    380 		if (uio->uio_rw == UIO_READ) {
    381 			if (cnt) {
    382 				error = uiomove(cp, cnt, uio);
    383 				if (error)
    384 					break;
    385 				gotdata++;
    386 			}
    387 			/*
    388 			 * Didn't get anything this time, but did in the past.
    389 			 * Consider us done.
    390 			 */
    391 			else if (gotdata)
    392 				break;
    393 		}
    394 		s = splsoftclock();
    395 		/*
    396 		 * Operation timeout (or non-blocking), quit now.
    397 		 */
    398 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    399 #ifdef DEBUG
    400 			if (ppidebug & PDB_IO)
    401 				printf("ppirw: timeout/done\n");
    402 #endif
    403 			splx(s);
    404 			break;
    405 		}
    406 		/*
    407 		 * Implement inter-read delay
    408 		 */
    409 		if (sc->sc_delay > 0) {
    410 			sc->sc_flags |= PPIF_DELAY;
    411 			timeout(ppistart, sc, sc->sc_delay);
    412 			error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0);
    413 			if (error) {
    414 				splx(s);
    415 				break;
    416 			}
    417 		}
    418 		splx(s);
    419 		/*
    420 		 * Must not call uiomove again til we've used all data
    421 		 * that we already grabbed.
    422 		 */
    423 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    424 			cp += cnt;
    425 			len -= cnt;
    426 			cnt = 0;
    427 			goto again;
    428 		}
    429 	}
    430 	s = splsoftclock();
    431 	if (sc->sc_flags & PPIF_TIMO) {
    432 		untimeout(ppitimo, sc);
    433 		sc->sc_flags &= ~PPIF_TIMO;
    434 	}
    435 	if (sc->sc_flags & PPIF_DELAY) {
    436 		untimeout(ppistart, sc);
    437 		sc->sc_flags &= ~PPIF_DELAY;
    438 	}
    439 	splx(s);
    440 	/*
    441 	 * Adjust for those chars that we uiomove'ed but never wrote
    442 	 */
    443 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    444 		uio->uio_resid += (len - cnt);
    445 #ifdef DEBUG
    446 		if (ppidebug & PDB_IO)
    447 			printf("ppirw: short write, adjust by %d\n",
    448 			       len-cnt);
    449 #endif
    450 	}
    451 	free(buf, M_DEVBUF);
    452 #ifdef DEBUG
    453 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
    454 		printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
    455 #endif
    456 	return (error);
    457 }
    458 
    459 int
    460 ppiioctl(dev, cmd, data, flag, p)
    461 	dev_t dev;
    462 	u_long cmd;
    463 	caddr_t data;
    464 	int flag;
    465 	struct proc *p;
    466 {
    467 	struct ppi_softc *sc = ppi_cd.cd_devs[UNIT(dev)];
    468 	struct ppiparam *pp, *upp;
    469 	int error = 0;
    470 
    471 	switch (cmd) {
    472 	case PPIIOCGPARAM:
    473 		pp = &sc->sc_param;
    474 		upp = (struct ppiparam *)data;
    475 		upp->burst = pp->burst;
    476 		upp->timo = ppihztoms(pp->timo);
    477 		upp->delay = ppihztoms(pp->delay);
    478 		break;
    479 	case PPIIOCSPARAM:
    480 		pp = &sc->sc_param;
    481 		upp = (struct ppiparam *)data;
    482 		if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
    483 		    upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
    484 			return(EINVAL);
    485 		pp->burst = upp->burst;
    486 		pp->timo = ppimstohz(upp->timo);
    487 		pp->delay = ppimstohz(upp->delay);
    488 		break;
    489 	case PPIIOCSSEC:
    490 		sc->sc_sec = *(int *)data;
    491 		break;
    492 	default:
    493 		return(EINVAL);
    494 	}
    495 	return (error);
    496 }
    497 
    498 int
    499 ppihztoms(h)
    500 	int h;
    501 {
    502 	extern int hz;
    503 	int m = h;
    504 
    505 	if (m > 0)
    506 		m = m * 1000 / hz;
    507 	return(m);
    508 }
    509 
    510 int
    511 ppimstohz(m)
    512 	int m;
    513 {
    514 	extern int hz;
    515 	int h = m;
    516 
    517 	if (h > 0) {
    518 		h = h * hz / 1000;
    519 		if (h == 0)
    520 			h = 1000 / hz;
    521 	}
    522 	return(h);
    523 }
    524