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