Home | History | Annotate | Line # | Download | only in dev
ppi.c revision 1.45
      1 /*	$NetBSD: ppi.c,v 1.45 2014/07/25 08:10:33 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.45 2014/07/25 08:10:33 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_discard = nodiscard,
    131 	.d_flag = 0
    132 };
    133 
    134 static void	ppistart(void *);
    135 static void	ppinoop(void *);
    136 
    137 static void	ppitimo(void *);
    138 static int	ppirw(dev_t, struct uio *);
    139 static int	ppihztoms(int);
    140 static int	ppimstohz(int);
    141 
    142 #define UNIT(x)		minor(x)
    143 
    144 #ifdef DEBUG
    145 int	ppidebug = 0x80;
    146 #define PDB_FOLLOW	0x01
    147 #define PDB_IO		0x02
    148 #define PDB_NOCHECK	0x80
    149 #endif
    150 
    151 static int
    152 ppimatch(device_t parent, cfdata_t cf, void *aux)
    153 {
    154 	struct hpibbus_attach_args *ha = aux;
    155 
    156 	/*
    157 	 * The printer/plotter doesn't return an ID tag.
    158 	 * The check below prevents us from matching a CS80
    159 	 * device by mistake.
    160 	 */
    161 	if (ha->ha_id & 0x200)
    162 		return 0;
    163 
    164 	/*
    165 	 * To prevent matching all unused slots on the bus, we
    166 	 * don't allow wildcarded locators.
    167 	 */
    168 	if (cf->hpibbuscf_slave == HPIBBUSCF_SLAVE_DEFAULT ||
    169 	    cf->hpibbuscf_punit == HPIBBUSCF_PUNIT_DEFAULT)
    170 		return 0;
    171 
    172 	return 1;
    173 }
    174 
    175 static void
    176 ppiattach(device_t parent, device_t self, void *aux)
    177 {
    178 	struct ppi_softc *sc = device_private(self);
    179 	struct hpibbus_attach_args *ha = aux;
    180 
    181 	sc->sc_dev = self;
    182 	aprint_normal("\n");
    183 
    184 	sc->sc_slave = ha->ha_slave;
    185 
    186 	callout_init(&sc->sc_timo_ch, 0);
    187 	callout_init(&sc->sc_start_ch, 0);
    188 
    189 	/* Initialize the hpib queue entry. */
    190 	sc->sc_hq.hq_softc = sc;
    191 	sc->sc_hq.hq_slave = sc->sc_slave;
    192 	sc->sc_hq.hq_start = ppistart;
    193 	sc->sc_hq.hq_go = ppinoop;
    194 	sc->sc_hq.hq_intr = ppinoop;
    195 
    196 	sc->sc_flags = PPIF_ALIVE;
    197 }
    198 
    199 static void
    200 ppinoop(void *arg)
    201 {
    202 	/* Noop! */
    203 }
    204 
    205 int
    206 ppiopen(dev_t dev, int flags, int fmt, struct lwp *l)
    207 {
    208 	struct ppi_softc *sc;
    209 
    210 	sc = device_lookup_private(&ppi_cd,UNIT(dev));
    211 	if (sc == NULL)
    212 		return ENXIO;
    213 
    214 	if ((sc->sc_flags & PPIF_ALIVE) == 0)
    215 		return ENXIO;
    216 
    217 #ifdef DEBUG
    218 	if (ppidebug & PDB_FOLLOW)
    219 		printf("ppiopen(%"PRIx64", %x): flags %x\n",
    220 		       dev, flags, sc->sc_flags);
    221 #endif
    222 	if (sc->sc_flags & PPIF_OPEN)
    223 		return EBUSY;
    224 	sc->sc_flags |= PPIF_OPEN;
    225 	sc->sc_burst = PPI_BURST;
    226 	sc->sc_timo = ppimstohz(PPI_TIMO);
    227 	sc->sc_delay = ppimstohz(PPI_DELAY);
    228 	sc->sc_sec = -1;
    229 	return 0;
    230 }
    231 
    232 static int
    233 ppiclose(dev_t dev, int flags, int fmt, struct lwp *l)
    234 {
    235 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
    236 
    237 #ifdef DEBUG
    238 	if (ppidebug & PDB_FOLLOW)
    239 		printf("ppiclose(%"PRIx64", %x): flags %x\n",
    240 		       dev, flags, sc->sc_flags);
    241 #endif
    242 	sc->sc_flags &= ~PPIF_OPEN;
    243 	return 0;
    244 }
    245 
    246 static void
    247 ppistart(void *arg)
    248 {
    249 	struct ppi_softc *sc = arg;
    250 
    251 #ifdef DEBUG
    252 	if (ppidebug & PDB_FOLLOW)
    253 		printf("ppistart(%x)\n", device_unit(sc->sc_dev));
    254 #endif
    255 	sc->sc_flags &= ~PPIF_DELAY;
    256 	wakeup(sc);
    257 }
    258 
    259 static void
    260 ppitimo(void *arg)
    261 {
    262 	struct ppi_softc *sc = arg;
    263 
    264 #ifdef DEBUG
    265 	if (ppidebug & PDB_FOLLOW)
    266 		printf("ppitimo(%x)\n", device_unit(sc->sc_dev));
    267 #endif
    268 	sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
    269 	wakeup(sc);
    270 }
    271 
    272 static int
    273 ppiread(dev_t dev, struct uio *uio, int flags)
    274 {
    275 
    276 #ifdef DEBUG
    277 	if (ppidebug & PDB_FOLLOW)
    278 		printf("ppiread(%"PRIx64", %p)\n", dev, uio);
    279 #endif
    280 	return ppirw(dev, uio);
    281 }
    282 
    283 static int
    284 ppiwrite(dev_t dev, struct uio *uio, int flags)
    285 {
    286 
    287 #ifdef DEBUG
    288 	if (ppidebug & PDB_FOLLOW)
    289 		printf("ppiwrite(%"PRIx64", %p)\n", dev, uio);
    290 #endif
    291 	return ppirw(dev, uio);
    292 }
    293 
    294 static int
    295 ppirw(dev_t dev, struct uio *uio)
    296 {
    297 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
    298 	int s, s2, len, cnt;
    299 	char *cp;
    300 	int error = 0, gotdata = 0;
    301 	int buflen, ctlr, slave;
    302 	char *buf;
    303 
    304 	if (uio->uio_resid == 0)
    305 		return 0;
    306 
    307 	ctlr = device_unit(device_parent(sc->sc_dev));
    308 	slave = sc->sc_slave;
    309 
    310 #ifdef DEBUG
    311 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
    312 		printf("ppirw(%"PRIx64", %p, %c): burst %d, timo %d, resid %x\n",
    313 		       dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
    314 		       sc->sc_burst, sc->sc_timo, uio->uio_resid);
    315 #endif
    316 	buflen = min(sc->sc_burst, uio->uio_resid);
    317 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    318 	sc->sc_flags |= PPIF_UIO;
    319 	if (sc->sc_timo > 0) {
    320 		sc->sc_flags |= PPIF_TIMO;
    321 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
    322 	}
    323 	len = cnt = 0;
    324 	while (uio->uio_resid > 0) {
    325 		len = min(buflen, uio->uio_resid);
    326 		cp = buf;
    327 		if (uio->uio_rw == UIO_WRITE) {
    328 			error = uiomove(cp, len, uio);
    329 			if (error)
    330 				break;
    331 		}
    332 again:
    333 		s = splsoftclock();
    334 		s2 = splbio();
    335 		if ((sc->sc_flags & PPIF_UIO) &&
    336 		    hpibreq(device_parent(sc->sc_dev), &sc->sc_hq) == 0)
    337 			(void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
    338 		/*
    339 		 * Check if we timed out during sleep or uiomove
    340 		 */
    341 		splx(s2);
    342 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    343 #ifdef DEBUG
    344 			if (ppidebug & PDB_IO)
    345 				printf("ppirw: uiomove/sleep timo, flags %x\n",
    346 				       sc->sc_flags);
    347 #endif
    348 			if (sc->sc_flags & PPIF_TIMO) {
    349 				callout_stop(&sc->sc_timo_ch);
    350 				sc->sc_flags &= ~PPIF_TIMO;
    351 			}
    352 			splx(s);
    353 			break;
    354 		}
    355 		splx(s);
    356 		/*
    357 		 * Perform the operation
    358 		 */
    359 		if (uio->uio_rw == UIO_WRITE)
    360 			cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len);
    361 		else
    362 			cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len);
    363 		s = splbio();
    364 		hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
    365 #ifdef DEBUG
    366 		if (ppidebug & PDB_IO)
    367 			printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n",
    368 			       uio->uio_rw == UIO_READ ? "recv" : "send",
    369 			       ctlr, slave, sc->sc_sec, cp, len, cnt);
    370 #endif
    371 		splx(s);
    372 		if (uio->uio_rw == UIO_READ) {
    373 			if (cnt) {
    374 				error = uiomove(cp, cnt, uio);
    375 				if (error)
    376 					break;
    377 				gotdata++;
    378 			}
    379 			/*
    380 			 * Didn't get anything this time, but did in the past.
    381 			 * Consider us done.
    382 			 */
    383 			else if (gotdata)
    384 				break;
    385 		}
    386 		s = splsoftclock();
    387 		/*
    388 		 * Operation timeout (or non-blocking), quit now.
    389 		 */
    390 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    391 #ifdef DEBUG
    392 			if (ppidebug & PDB_IO)
    393 				printf("ppirw: timeout/done\n");
    394 #endif
    395 			splx(s);
    396 			break;
    397 		}
    398 		/*
    399 		 * Implement inter-read delay
    400 		 */
    401 		if (sc->sc_delay > 0) {
    402 			sc->sc_flags |= PPIF_DELAY;
    403 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
    404 			    ppistart, sc);
    405 			error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0);
    406 			if (error) {
    407 				splx(s);
    408 				break;
    409 			}
    410 		}
    411 		splx(s);
    412 		/*
    413 		 * Must not call uiomove again til we've used all data
    414 		 * that we already grabbed.
    415 		 */
    416 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    417 			cp += cnt;
    418 			len -= cnt;
    419 			cnt = 0;
    420 			goto again;
    421 		}
    422 	}
    423 	s = splsoftclock();
    424 	if (sc->sc_flags & PPIF_TIMO) {
    425 		callout_stop(&sc->sc_timo_ch);
    426 		sc->sc_flags &= ~PPIF_TIMO;
    427 	}
    428 	if (sc->sc_flags & PPIF_DELAY) {
    429 		callout_stop(&sc->sc_start_ch);
    430 		sc->sc_flags &= ~PPIF_DELAY;
    431 	}
    432 	splx(s);
    433 	/*
    434 	 * Adjust for those chars that we uiomove'ed but never wrote
    435 	 */
    436 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    437 		uio->uio_resid += (len - cnt);
    438 #ifdef DEBUG
    439 		if (ppidebug & PDB_IO)
    440 			printf("ppirw: short write, adjust by %d\n",
    441 			       len-cnt);
    442 #endif
    443 	}
    444 	free(buf, M_DEVBUF);
    445 #ifdef DEBUG
    446 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
    447 		printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
    448 #endif
    449 	return error;
    450 }
    451 
    452 static int
    453 ppiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    454 {
    455 	struct ppi_softc *sc = device_lookup_private(&ppi_cd,UNIT(dev));
    456 	struct ppiparam *pp, *upp;
    457 	int error = 0;
    458 
    459 	switch (cmd) {
    460 	case PPIIOCGPARAM:
    461 		pp = &sc->sc_param;
    462 		upp = (struct ppiparam *)data;
    463 		upp->burst = pp->burst;
    464 		upp->timo = ppihztoms(pp->timo);
    465 		upp->delay = ppihztoms(pp->delay);
    466 		break;
    467 	case PPIIOCSPARAM:
    468 		pp = &sc->sc_param;
    469 		upp = (struct ppiparam *)data;
    470 		if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
    471 		    upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
    472 			return EINVAL;
    473 		pp->burst = upp->burst;
    474 		pp->timo = ppimstohz(upp->timo);
    475 		pp->delay = ppimstohz(upp->delay);
    476 		break;
    477 	case PPIIOCSSEC:
    478 		sc->sc_sec = *(int *)data;
    479 		break;
    480 	default:
    481 		return EINVAL;
    482 	}
    483 	return error;
    484 }
    485 
    486 static int
    487 ppihztoms(int h)
    488 {
    489 	extern int hz;
    490 	int m = h;
    491 
    492 	if (m > 0)
    493 		m = m * 1000 / hz;
    494 	return m;
    495 }
    496 
    497 static int
    498 ppimstohz(int m)
    499 {
    500 	extern int hz;
    501 	int h = m;
    502 
    503 	if (h > 0) {
    504 		h = h * hz / 1000;
    505 		if (h == 0)
    506 			h = 1000 / hz;
    507 	}
    508 	return h;
    509 }
    510