Home | History | Annotate | Line # | Download | only in gpib
ppi.c revision 1.23.4.1
      1  1.23.4.1  christos /*	$NetBSD: ppi.c,v 1.23.4.1 2019/06/10 22:07:08 christos Exp $	*/
      2       1.1  gmcgarry 
      3       1.1  gmcgarry /*-
      4       1.1  gmcgarry  * Copyright (c) 1996-2003 The NetBSD Foundation, Inc.
      5       1.1  gmcgarry  * All rights reserved.
      6       1.1  gmcgarry  *
      7       1.1  gmcgarry  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  gmcgarry  * by Jason R. Thorpe.
      9       1.1  gmcgarry  *
     10       1.1  gmcgarry  * Redistribution and use in source and binary forms, with or without
     11       1.1  gmcgarry  * modification, are permitted provided that the following conditions
     12       1.1  gmcgarry  * are met:
     13       1.1  gmcgarry  * 1. Redistributions of source code must retain the above copyright
     14       1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer.
     15       1.1  gmcgarry  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  gmcgarry  *    documentation and/or other materials provided with the distribution.
     18       1.1  gmcgarry  *
     19       1.1  gmcgarry  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  gmcgarry  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  gmcgarry  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  gmcgarry  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  gmcgarry  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  gmcgarry  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  gmcgarry  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  gmcgarry  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  gmcgarry  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  gmcgarry  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  gmcgarry  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  gmcgarry  */
     31       1.1  gmcgarry 
     32       1.1  gmcgarry /*
     33       1.1  gmcgarry  * Copyright (c) 1982, 1990, 1993
     34       1.1  gmcgarry  *	The Regents of the University of California.  All rights reserved.
     35       1.1  gmcgarry  *
     36       1.1  gmcgarry  * Redistribution and use in source and binary forms, with or without
     37       1.1  gmcgarry  * modification, are permitted provided that the following conditions
     38       1.1  gmcgarry  * are met:
     39       1.1  gmcgarry  * 1. Redistributions of source code must retain the above copyright
     40       1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer.
     41       1.1  gmcgarry  * 2. Redistributions in binary form must reproduce the above copyright
     42       1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer in the
     43       1.1  gmcgarry  *    documentation and/or other materials provided with the distribution.
     44       1.2       agc  * 3. Neither the name of the University nor the names of its contributors
     45       1.1  gmcgarry  *    may be used to endorse or promote products derived from this software
     46       1.1  gmcgarry  *    without specific prior written permission.
     47       1.1  gmcgarry  *
     48       1.1  gmcgarry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49       1.1  gmcgarry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50       1.1  gmcgarry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51       1.1  gmcgarry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52       1.1  gmcgarry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53       1.1  gmcgarry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54       1.1  gmcgarry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55       1.1  gmcgarry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56       1.1  gmcgarry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57       1.1  gmcgarry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58       1.1  gmcgarry  * SUCH DAMAGE.
     59       1.1  gmcgarry  *
     60       1.1  gmcgarry  *	@(#)ppi.c	8.1 (Berkeley) 6/16/93
     61       1.1  gmcgarry  */
     62       1.1  gmcgarry 
     63       1.1  gmcgarry /*
     64       1.1  gmcgarry  * Printer/Plotter GPIB interface
     65       1.1  gmcgarry  */
     66       1.1  gmcgarry 
     67       1.1  gmcgarry #include <sys/cdefs.h>
     68  1.23.4.1  christos __KERNEL_RCSID(0, "$NetBSD: ppi.c,v 1.23.4.1 2019/06/10 22:07:08 christos Exp $");
     69       1.1  gmcgarry 
     70       1.1  gmcgarry #include <sys/param.h>
     71       1.1  gmcgarry #include <sys/systm.h>
     72       1.1  gmcgarry #include <sys/callout.h>
     73       1.1  gmcgarry #include <sys/conf.h>
     74       1.1  gmcgarry #include <sys/device.h>
     75       1.1  gmcgarry #include <sys/malloc.h>
     76       1.1  gmcgarry #include <sys/proc.h>
     77       1.1  gmcgarry #include <sys/uio.h>
     78       1.1  gmcgarry 
     79       1.1  gmcgarry #include <dev/gpib/gpibvar.h>
     80       1.1  gmcgarry 
     81       1.1  gmcgarry #include <dev/gpib/ppiio.h>
     82       1.1  gmcgarry 
     83      1.23  riastrad #include "ioconf.h"
     84      1.23  riastrad 
     85       1.1  gmcgarry struct	ppi_softc {
     86      1.20       chs 	device_t sc_dev;
     87       1.1  gmcgarry 	gpib_chipset_tag_t sc_ic;
     88       1.1  gmcgarry 	gpib_handle_t sc_hdl;
     89       1.1  gmcgarry 
     90       1.1  gmcgarry 	int sc_address;			/* GPIB address */
     91       1.1  gmcgarry 	int	sc_flags;
     92       1.1  gmcgarry 	int	sc_sec;
     93       1.1  gmcgarry 	struct	ppiparam sc_param;
     94       1.1  gmcgarry #define sc_burst sc_param.burst
     95       1.1  gmcgarry #define sc_timo  sc_param.timo
     96       1.1  gmcgarry #define sc_delay sc_param.delay
     97       1.1  gmcgarry 	struct	callout sc_timo_ch;
     98       1.1  gmcgarry 	struct	callout sc_start_ch;
     99       1.1  gmcgarry };
    100       1.1  gmcgarry 
    101       1.1  gmcgarry /* sc_flags values */
    102       1.3     perry #define	PPIF_ALIVE	0x01
    103       1.3     perry #define	PPIF_OPEN	0x02
    104       1.1  gmcgarry #define PPIF_UIO	0x04
    105       1.1  gmcgarry #define PPIF_TIMO	0x08
    106       1.1  gmcgarry #define PPIF_DELAY	0x10
    107       1.1  gmcgarry 
    108      1.17    cegger int	ppimatch(device_t, cfdata_t, void *);
    109      1.17    cegger void	ppiattach(device_t, device_t, void *);
    110       1.1  gmcgarry 
    111      1.20       chs CFATTACH_DECL_NEW(ppi, sizeof(struct ppi_softc),
    112       1.1  gmcgarry 	ppimatch, ppiattach, NULL, NULL);
    113       1.1  gmcgarry 
    114       1.1  gmcgarry void	ppicallback(void *, int);
    115       1.1  gmcgarry void	ppistart(void *);
    116       1.1  gmcgarry 
    117       1.1  gmcgarry void	ppitimo(void *);
    118       1.1  gmcgarry int	ppirw(dev_t, struct uio *);
    119       1.1  gmcgarry int	ppihztoms(int);
    120       1.1  gmcgarry int	ppimstohz(int);
    121       1.1  gmcgarry 
    122       1.1  gmcgarry dev_type_open(ppiopen);
    123       1.1  gmcgarry dev_type_close(ppiclose);
    124       1.1  gmcgarry dev_type_read(ppiread);
    125       1.1  gmcgarry dev_type_write(ppiwrite);
    126       1.1  gmcgarry dev_type_ioctl(ppiioctl);
    127       1.1  gmcgarry 
    128       1.1  gmcgarry const struct cdevsw ppi_cdevsw = {
    129      1.21  dholland         .d_open = ppiopen,
    130      1.21  dholland 	.d_close = ppiclose,
    131      1.21  dholland 	.d_read = ppiread,
    132      1.21  dholland 	.d_write = ppiwrite,
    133      1.21  dholland 	.d_ioctl = ppiioctl,
    134      1.21  dholland         .d_stop = nostop,
    135      1.21  dholland 	.d_tty = notty,
    136      1.21  dholland 	.d_poll = nopoll,
    137      1.21  dholland 	.d_mmap = nommap,
    138      1.21  dholland 	.d_kqfilter = nokqfilter,
    139      1.22  dholland 	.d_discard = nodiscard,
    140      1.21  dholland 	.d_flag = D_OTHER
    141       1.1  gmcgarry };
    142       1.1  gmcgarry 
    143       1.1  gmcgarry #define UNIT(x)		minor(x)
    144       1.1  gmcgarry 
    145       1.1  gmcgarry #ifdef DEBUG
    146       1.1  gmcgarry int	ppidebug = 0x80;
    147       1.1  gmcgarry #define PDB_FOLLOW	0x01
    148       1.1  gmcgarry #define PDB_IO		0x02
    149       1.1  gmcgarry #define PDB_NOCHECK	0x80
    150       1.1  gmcgarry #define DPRINTF(mask, str)	if (ppidebug & (mask)) printf str
    151       1.1  gmcgarry #else
    152       1.1  gmcgarry #define DPRINTF(mask, str)	/* nothing */
    153       1.1  gmcgarry #endif
    154       1.1  gmcgarry 
    155       1.1  gmcgarry int
    156      1.17    cegger ppimatch(device_t parent, cfdata_t match, void *aux)
    157       1.1  gmcgarry {
    158       1.1  gmcgarry 
    159       1.1  gmcgarry 	return (1);
    160       1.1  gmcgarry }
    161       1.1  gmcgarry 
    162       1.1  gmcgarry void
    163      1.17    cegger ppiattach(device_t parent, device_t self, void *aux)
    164       1.1  gmcgarry {
    165       1.6   thorpej 	struct ppi_softc *sc = device_private(self);
    166       1.1  gmcgarry 	struct gpib_attach_args *ga = aux;
    167       1.1  gmcgarry 
    168       1.1  gmcgarry 	printf("\n");
    169       1.1  gmcgarry 
    170       1.1  gmcgarry 	sc->sc_ic = ga->ga_ic;
    171       1.1  gmcgarry 	sc->sc_address = ga->ga_address;
    172       1.1  gmcgarry 
    173      1.10        ad 	callout_init(&sc->sc_timo_ch, 0);
    174      1.10        ad 	callout_init(&sc->sc_start_ch, 0);
    175       1.1  gmcgarry 
    176       1.1  gmcgarry 	if (gpibregister(sc->sc_ic, sc->sc_address, ppicallback, sc,
    177       1.1  gmcgarry 	    &sc->sc_hdl)) {
    178      1.20       chs 		aprint_error_dev(sc->sc_dev, "can't register callback\n");
    179       1.1  gmcgarry 		return;
    180       1.1  gmcgarry 	}
    181       1.1  gmcgarry 
    182       1.1  gmcgarry 	sc->sc_flags = PPIF_ALIVE;
    183       1.1  gmcgarry }
    184       1.1  gmcgarry 
    185       1.1  gmcgarry int
    186      1.13    cegger ppiopen(dev_t dev, int flags, int fmt, struct lwp *l)
    187       1.1  gmcgarry {
    188       1.1  gmcgarry 	struct ppi_softc *sc;
    189       1.1  gmcgarry 
    190      1.13    cegger 	sc = device_lookup_private(&ppi_cd, UNIT(dev));
    191      1.13    cegger 	if (sc == NULL)
    192      1.13    cegger 		return (ENXIO);
    193      1.13    cegger 
    194      1.18   tsutsui 	if ((sc->sc_flags & PPIF_ALIVE) == 0)
    195       1.1  gmcgarry 		return (ENXIO);
    196       1.1  gmcgarry 
    197      1.19   tsutsui 	DPRINTF(PDB_FOLLOW, ("ppiopen(%" PRIx64 ", %x): flags %x\n",
    198       1.1  gmcgarry 	    dev, flags, sc->sc_flags));
    199       1.1  gmcgarry 
    200       1.1  gmcgarry 	if (sc->sc_flags & PPIF_OPEN)
    201       1.1  gmcgarry 		return (EBUSY);
    202       1.1  gmcgarry 	sc->sc_flags |= PPIF_OPEN;
    203       1.1  gmcgarry 	sc->sc_burst = PPI_BURST;
    204       1.1  gmcgarry 	sc->sc_timo = ppimstohz(PPI_TIMO);
    205       1.1  gmcgarry 	sc->sc_delay = ppimstohz(PPI_DELAY);
    206       1.1  gmcgarry 	sc->sc_sec = -1;
    207       1.1  gmcgarry 	return (0);
    208       1.1  gmcgarry }
    209       1.1  gmcgarry 
    210       1.1  gmcgarry int
    211      1.13    cegger ppiclose(dev_t dev, int flags, int fmt, struct lwp *l)
    212       1.1  gmcgarry {
    213      1.13    cegger 	struct ppi_softc *sc;
    214      1.13    cegger 
    215      1.13    cegger 	sc = device_lookup_private(&ppi_cd, UNIT(dev));
    216       1.1  gmcgarry 
    217      1.19   tsutsui 	DPRINTF(PDB_FOLLOW, ("ppiclose(%" PRIx64 ", %x): flags %x\n",
    218       1.1  gmcgarry 		       dev, flags, sc->sc_flags));
    219       1.1  gmcgarry 
    220       1.1  gmcgarry 	sc->sc_flags &= ~PPIF_OPEN;
    221       1.1  gmcgarry 	return (0);
    222       1.1  gmcgarry }
    223       1.1  gmcgarry 
    224       1.1  gmcgarry void
    225      1.14       dsl ppicallback(void *v, int action)
    226       1.1  gmcgarry {
    227       1.1  gmcgarry 	struct ppi_softc *sc = v;
    228       1.1  gmcgarry 
    229       1.1  gmcgarry 	DPRINTF(PDB_FOLLOW, ("ppicallback: v=%p, action=%d\n", v, action));
    230       1.1  gmcgarry 
    231       1.3     perry 	switch (action) {
    232       1.1  gmcgarry 	case GPIBCBF_START:
    233       1.1  gmcgarry 		ppistart(sc);
    234       1.1  gmcgarry 	case GPIBCBF_INTR:
    235       1.1  gmcgarry 		/* no-op */
    236       1.1  gmcgarry 		break;
    237       1.1  gmcgarry #ifdef DEBUG
    238       1.1  gmcgarry 	default:
    239       1.1  gmcgarry 		DPRINTF(PDB_FOLLOW, ("ppicallback: unknown action %d\n",
    240       1.1  gmcgarry 		    action));
    241       1.1  gmcgarry 		break;
    242       1.1  gmcgarry #endif
    243       1.1  gmcgarry 	}
    244       1.1  gmcgarry }
    245       1.1  gmcgarry 
    246       1.1  gmcgarry void
    247      1.13    cegger ppistart(void *v)
    248       1.1  gmcgarry {
    249       1.1  gmcgarry 	struct ppi_softc *sc = v;
    250       1.1  gmcgarry 
    251      1.20       chs 	DPRINTF(PDB_FOLLOW, ("ppistart(%x)\n", device_unit(sc->sc_dev)));
    252       1.1  gmcgarry 
    253       1.1  gmcgarry 	sc->sc_flags &= ~PPIF_DELAY;
    254       1.1  gmcgarry 	wakeup(sc);
    255       1.1  gmcgarry }
    256       1.1  gmcgarry 
    257       1.1  gmcgarry void
    258      1.13    cegger ppitimo(void *arg)
    259       1.1  gmcgarry {
    260       1.1  gmcgarry 	struct ppi_softc *sc = arg;
    261       1.1  gmcgarry 
    262      1.20       chs 	DPRINTF(PDB_FOLLOW, ("ppitimo(%x)\n", device_unit(sc->sc_dev)));
    263       1.1  gmcgarry 
    264       1.1  gmcgarry 	sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
    265       1.1  gmcgarry 	wakeup(sc);
    266       1.1  gmcgarry }
    267       1.1  gmcgarry 
    268       1.1  gmcgarry int
    269      1.13    cegger ppiread(dev_t dev, struct uio *uio, int flags)
    270       1.1  gmcgarry {
    271       1.1  gmcgarry 
    272      1.19   tsutsui 	DPRINTF(PDB_FOLLOW, ("ppiread(%" PRIx64 ", %p)\n", dev, uio));
    273       1.1  gmcgarry 
    274       1.1  gmcgarry 	return (ppirw(dev, uio));
    275       1.1  gmcgarry }
    276       1.1  gmcgarry 
    277       1.1  gmcgarry int
    278      1.13    cegger ppiwrite(dev_t dev, struct uio *uio, int flags)
    279       1.1  gmcgarry {
    280       1.1  gmcgarry 
    281      1.19   tsutsui 	DPRINTF(PDB_FOLLOW, ("ppiwrite(%" PRIx64 ", %p)\n", dev, uio));
    282       1.1  gmcgarry 
    283       1.1  gmcgarry 	return (ppirw(dev, uio));
    284       1.1  gmcgarry }
    285       1.1  gmcgarry 
    286       1.1  gmcgarry int
    287      1.13    cegger ppirw(dev_t dev, struct uio *uio)
    288       1.1  gmcgarry {
    289      1.13    cegger 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
    290       1.8        ad 	int s1, s2, len, cnt;
    291       1.1  gmcgarry 	char *cp;
    292       1.1  gmcgarry 	int error = 0, gotdata = 0;
    293       1.1  gmcgarry 	int buflen, address;
    294       1.1  gmcgarry 	char *buf;
    295       1.1  gmcgarry 
    296       1.1  gmcgarry 	if (uio->uio_resid == 0)
    297       1.1  gmcgarry 		return (0);
    298       1.1  gmcgarry 
    299       1.1  gmcgarry 	address = sc->sc_address;
    300       1.1  gmcgarry 
    301       1.1  gmcgarry 	DPRINTF(PDB_FOLLOW|PDB_IO,
    302      1.19   tsutsui 	    ("ppirw(%" PRIx64 ", %p, %c): burst %d, timo %d, resid %x\n",
    303       1.1  gmcgarry 	    dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
    304       1.1  gmcgarry 	    sc->sc_burst, sc->sc_timo, uio->uio_resid));
    305       1.1  gmcgarry 
    306  1.23.4.1  christos 	buflen = uimin(sc->sc_burst, uio->uio_resid);
    307       1.1  gmcgarry 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    308       1.1  gmcgarry 	sc->sc_flags |= PPIF_UIO;
    309       1.1  gmcgarry 	if (sc->sc_timo > 0) {
    310       1.1  gmcgarry 		sc->sc_flags |= PPIF_TIMO;
    311       1.1  gmcgarry 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
    312       1.1  gmcgarry 	}
    313       1.1  gmcgarry 	len = cnt = 0;
    314       1.1  gmcgarry 	while (uio->uio_resid > 0) {
    315  1.23.4.1  christos 		len = uimin(buflen, uio->uio_resid);
    316       1.1  gmcgarry 		cp = buf;
    317       1.1  gmcgarry 		if (uio->uio_rw == UIO_WRITE) {
    318       1.1  gmcgarry 			error = uiomove(cp, len, uio);
    319       1.1  gmcgarry 			if (error)
    320       1.1  gmcgarry 				break;
    321       1.1  gmcgarry 		}
    322       1.1  gmcgarry again:
    323       1.8        ad 		s1 = splsoftclock();
    324       1.8        ad 		s2 = splbio();
    325       1.1  gmcgarry 		if (sc->sc_flags & PPIF_UIO) {
    326       1.1  gmcgarry 			if (gpibrequest(sc->sc_ic, sc->sc_hdl) == 0)
    327       1.1  gmcgarry 				(void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
    328       1.1  gmcgarry 		}
    329       1.1  gmcgarry 		/*
    330       1.1  gmcgarry 		 * Check if we timed out during sleep or uiomove
    331       1.1  gmcgarry 		 */
    332       1.8        ad 		splx(s2);
    333       1.1  gmcgarry 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    334       1.1  gmcgarry 			DPRINTF(PDB_IO,
    335       1.1  gmcgarry 			    ("ppirw: uiomove/sleep timo, flags %x\n",
    336       1.1  gmcgarry 			    sc->sc_flags));
    337       1.1  gmcgarry 			if (sc->sc_flags & PPIF_TIMO) {
    338       1.1  gmcgarry 				callout_stop(&sc->sc_timo_ch);
    339       1.1  gmcgarry 				sc->sc_flags &= ~PPIF_TIMO;
    340       1.1  gmcgarry 			}
    341       1.8        ad 			splx(s1);
    342       1.1  gmcgarry 			break;
    343       1.1  gmcgarry 		}
    344       1.8        ad 		splx(s1);
    345       1.1  gmcgarry 		/*
    346       1.1  gmcgarry 		 * Perform the operation
    347       1.1  gmcgarry 		 */
    348       1.1  gmcgarry 		if (uio->uio_rw == UIO_WRITE)
    349       1.1  gmcgarry 			cnt = gpibsend(sc->sc_ic, address, sc->sc_sec,
    350       1.1  gmcgarry 			    cp, len);
    351       1.1  gmcgarry 		else
    352       1.1  gmcgarry 			cnt = gpibrecv(sc->sc_ic, address, sc->sc_sec,
    353       1.1  gmcgarry 			    cp, len);
    354       1.8        ad 		s1 = splbio();
    355       1.1  gmcgarry 		gpibrelease(sc->sc_ic, sc->sc_hdl);
    356       1.1  gmcgarry 		DPRINTF(PDB_IO, ("ppirw: %s(%d, %x, %p, %d) -> %d\n",
    357       1.1  gmcgarry 		    uio->uio_rw == UIO_READ ? "recv" : "send",
    358       1.1  gmcgarry 		    address, sc->sc_sec, cp, len, cnt));
    359       1.8        ad 		splx(s1);
    360       1.1  gmcgarry 		if (uio->uio_rw == UIO_READ) {
    361       1.1  gmcgarry 			if (cnt) {
    362       1.1  gmcgarry 				error = uiomove(cp, cnt, uio);
    363       1.1  gmcgarry 				if (error)
    364       1.1  gmcgarry 					break;
    365       1.1  gmcgarry 				gotdata++;
    366       1.1  gmcgarry 			}
    367       1.1  gmcgarry 			/*
    368       1.1  gmcgarry 			 * Didn't get anything this time, but did in the past.
    369       1.1  gmcgarry 			 * Consider us done.
    370       1.1  gmcgarry 			 */
    371       1.1  gmcgarry 			else if (gotdata)
    372       1.1  gmcgarry 				break;
    373       1.1  gmcgarry 		}
    374       1.8        ad 		s1 = splsoftclock();
    375       1.1  gmcgarry 		/*
    376       1.1  gmcgarry 		 * Operation timeout (or non-blocking), quit now.
    377       1.1  gmcgarry 		 */
    378       1.1  gmcgarry 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    379       1.1  gmcgarry 			DPRINTF(PDB_IO, ("ppirw: timeout/done\n"));
    380       1.8        ad 			splx(s1);
    381       1.1  gmcgarry 			break;
    382       1.1  gmcgarry 		}
    383       1.1  gmcgarry 		/*
    384       1.1  gmcgarry 		 * Implement inter-read delay
    385       1.1  gmcgarry 		 */
    386       1.1  gmcgarry 		if (sc->sc_delay > 0) {
    387       1.1  gmcgarry 			sc->sc_flags |= PPIF_DELAY;
    388       1.1  gmcgarry 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
    389       1.1  gmcgarry 			    ppistart, sc);
    390       1.1  gmcgarry 			error = tsleep(sc, (PCATCH|PZERO) + 1, "gpib", 0);
    391       1.1  gmcgarry 			if (error) {
    392       1.8        ad 				splx(s1);
    393       1.1  gmcgarry 				break;
    394       1.1  gmcgarry 			}
    395       1.1  gmcgarry 		}
    396       1.8        ad 		splx(s1);
    397       1.1  gmcgarry 		/*
    398       1.1  gmcgarry 		 * Must not call uiomove again til we've used all data
    399       1.1  gmcgarry 		 * that we already grabbed.
    400       1.1  gmcgarry 		 */
    401       1.1  gmcgarry 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    402       1.1  gmcgarry 			cp += cnt;
    403       1.1  gmcgarry 			len -= cnt;
    404       1.1  gmcgarry 			cnt = 0;
    405       1.1  gmcgarry 			goto again;
    406       1.1  gmcgarry 		}
    407       1.1  gmcgarry 	}
    408       1.8        ad 	s1 = splsoftclock();
    409       1.1  gmcgarry 	if (sc->sc_flags & PPIF_TIMO) {
    410       1.1  gmcgarry 		callout_stop(&sc->sc_timo_ch);
    411       1.1  gmcgarry 		sc->sc_flags &= ~PPIF_TIMO;
    412       1.1  gmcgarry 	}
    413       1.1  gmcgarry 	if (sc->sc_flags & PPIF_DELAY) {
    414       1.1  gmcgarry 		callout_stop(&sc->sc_start_ch);
    415       1.1  gmcgarry 		sc->sc_flags &= ~PPIF_DELAY;
    416       1.1  gmcgarry 	}
    417       1.8        ad 	splx(s1);
    418       1.1  gmcgarry 	/*
    419       1.1  gmcgarry 	 * Adjust for those chars that we uiomove'ed but never wrote
    420       1.1  gmcgarry 	 */
    421       1.1  gmcgarry 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    422       1.1  gmcgarry 		uio->uio_resid += (len - cnt);
    423       1.1  gmcgarry 		DPRINTF(PDB_IO, ("ppirw: short write, adjust by %d\n",
    424       1.1  gmcgarry 		    len - cnt));
    425       1.1  gmcgarry 	}
    426       1.1  gmcgarry 	free(buf, M_DEVBUF);
    427       1.1  gmcgarry 	DPRINTF(PDB_FOLLOW|PDB_IO, ("ppirw: return %d, resid %d\n",
    428       1.1  gmcgarry 	    error, uio->uio_resid));
    429       1.1  gmcgarry 	return (error);
    430       1.1  gmcgarry }
    431       1.1  gmcgarry 
    432       1.1  gmcgarry int
    433      1.13    cegger ppiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    434       1.1  gmcgarry {
    435      1.13    cegger 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
    436       1.1  gmcgarry 	struct ppiparam *pp, *upp;
    437       1.1  gmcgarry 	int error = 0;
    438       1.1  gmcgarry 
    439       1.1  gmcgarry 	switch (cmd) {
    440       1.1  gmcgarry 	case PPIIOCGPARAM:
    441       1.1  gmcgarry 		pp = &sc->sc_param;
    442       1.1  gmcgarry 		upp = (struct ppiparam *)data;
    443       1.1  gmcgarry 		upp->burst = pp->burst;
    444       1.1  gmcgarry 		upp->timo = ppihztoms(pp->timo);
    445       1.1  gmcgarry 		upp->delay = ppihztoms(pp->delay);
    446       1.1  gmcgarry 		break;
    447       1.1  gmcgarry 	case PPIIOCSPARAM:
    448       1.1  gmcgarry 		pp = &sc->sc_param;
    449       1.1  gmcgarry 		upp = (struct ppiparam *)data;
    450       1.1  gmcgarry 		if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
    451       1.1  gmcgarry 		    upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
    452       1.1  gmcgarry 			return (EINVAL);
    453       1.1  gmcgarry 		pp->burst = upp->burst;
    454       1.1  gmcgarry 		pp->timo = ppimstohz(upp->timo);
    455       1.1  gmcgarry 		pp->delay = ppimstohz(upp->delay);
    456       1.1  gmcgarry 		break;
    457       1.1  gmcgarry 	case PPIIOCSSEC:
    458       1.1  gmcgarry 		sc->sc_sec = *(int *)data;
    459       1.1  gmcgarry 		break;
    460       1.1  gmcgarry 	default:
    461       1.1  gmcgarry 		return (EINVAL);
    462       1.1  gmcgarry 	}
    463       1.1  gmcgarry 	return (error);
    464       1.1  gmcgarry }
    465       1.1  gmcgarry 
    466       1.1  gmcgarry int
    467      1.13    cegger ppihztoms(int h)
    468       1.1  gmcgarry {
    469       1.1  gmcgarry 	extern int hz;
    470       1.1  gmcgarry 	int m = h;
    471       1.1  gmcgarry 
    472       1.1  gmcgarry 	if (m > 0)
    473       1.1  gmcgarry 		m = m * 1000 / hz;
    474       1.1  gmcgarry 	return (m);
    475       1.1  gmcgarry }
    476       1.1  gmcgarry 
    477       1.1  gmcgarry int
    478      1.13    cegger ppimstohz(int m)
    479       1.1  gmcgarry {
    480       1.1  gmcgarry 	extern int hz;
    481       1.1  gmcgarry 	int h = m;
    482       1.1  gmcgarry 
    483       1.1  gmcgarry 	if (h > 0) {
    484       1.1  gmcgarry 		h = h * hz / 1000;
    485       1.1  gmcgarry 		if (h == 0)
    486       1.1  gmcgarry 			h = 1000 / hz;
    487       1.1  gmcgarry 	}
    488       1.1  gmcgarry 	return (h);
    489       1.1  gmcgarry }
    490