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