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