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