Home | History | Annotate | Line # | Download | only in gpib
ppi.c revision 1.19.12.2
      1 /*	$NetBSD: ppi.c,v 1.19.12.2 2014/05/22 11:40:21 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996-2003 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 GPIB interface
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: ppi.c,v 1.19.12.2 2014/05/22 11:40:21 yamt 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/malloc.h>
     76 #include <sys/proc.h>
     77 #include <sys/uio.h>
     78 
     79 #include <dev/gpib/gpibvar.h>
     80 
     81 #include <dev/gpib/ppiio.h>
     82 
     83 struct	ppi_softc {
     84 	device_t sc_dev;
     85 	gpib_chipset_tag_t sc_ic;
     86 	gpib_handle_t sc_hdl;
     87 
     88 	int sc_address;			/* GPIB address */
     89 	int	sc_flags;
     90 	int	sc_sec;
     91 	struct	ppiparam sc_param;
     92 #define sc_burst sc_param.burst
     93 #define sc_timo  sc_param.timo
     94 #define sc_delay sc_param.delay
     95 	struct	callout sc_timo_ch;
     96 	struct	callout sc_start_ch;
     97 };
     98 
     99 /* sc_flags values */
    100 #define	PPIF_ALIVE	0x01
    101 #define	PPIF_OPEN	0x02
    102 #define PPIF_UIO	0x04
    103 #define PPIF_TIMO	0x08
    104 #define PPIF_DELAY	0x10
    105 
    106 int	ppimatch(device_t, cfdata_t, void *);
    107 void	ppiattach(device_t, device_t, void *);
    108 
    109 CFATTACH_DECL_NEW(ppi, sizeof(struct ppi_softc),
    110 	ppimatch, ppiattach, NULL, NULL);
    111 
    112 extern struct cfdriver ppi_cd;
    113 
    114 void	ppicallback(void *, int);
    115 void	ppistart(void *);
    116 
    117 void	ppitimo(void *);
    118 int	ppirw(dev_t, struct uio *);
    119 int	ppihztoms(int);
    120 int	ppimstohz(int);
    121 
    122 dev_type_open(ppiopen);
    123 dev_type_close(ppiclose);
    124 dev_type_read(ppiread);
    125 dev_type_write(ppiwrite);
    126 dev_type_ioctl(ppiioctl);
    127 
    128 const struct cdevsw ppi_cdevsw = {
    129         .d_open = ppiopen,
    130 	.d_close = ppiclose,
    131 	.d_read = ppiread,
    132 	.d_write = ppiwrite,
    133 	.d_ioctl = ppiioctl,
    134         .d_stop = nostop,
    135 	.d_tty = notty,
    136 	.d_poll = nopoll,
    137 	.d_mmap = nommap,
    138 	.d_kqfilter = nokqfilter,
    139 	.d_flag = D_OTHER
    140 };
    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 #define DPRINTF(mask, str)	if (ppidebug & (mask)) printf str
    150 #else
    151 #define DPRINTF(mask, str)	/* nothing */
    152 #endif
    153 
    154 int
    155 ppimatch(device_t parent, cfdata_t match, void *aux)
    156 {
    157 
    158 	return (1);
    159 }
    160 
    161 void
    162 ppiattach(device_t parent, device_t self, void *aux)
    163 {
    164 	struct ppi_softc *sc = device_private(self);
    165 	struct gpib_attach_args *ga = aux;
    166 
    167 	printf("\n");
    168 
    169 	sc->sc_ic = ga->ga_ic;
    170 	sc->sc_address = ga->ga_address;
    171 
    172 	callout_init(&sc->sc_timo_ch, 0);
    173 	callout_init(&sc->sc_start_ch, 0);
    174 
    175 	if (gpibregister(sc->sc_ic, sc->sc_address, ppicallback, sc,
    176 	    &sc->sc_hdl)) {
    177 		aprint_error_dev(sc->sc_dev, "can't register callback\n");
    178 		return;
    179 	}
    180 
    181 	sc->sc_flags = PPIF_ALIVE;
    182 }
    183 
    184 int
    185 ppiopen(dev_t dev, int flags, int fmt, struct lwp *l)
    186 {
    187 	struct ppi_softc *sc;
    188 
    189 	sc = device_lookup_private(&ppi_cd, UNIT(dev));
    190 	if (sc == NULL)
    191 		return (ENXIO);
    192 
    193 	if ((sc->sc_flags & PPIF_ALIVE) == 0)
    194 		return (ENXIO);
    195 
    196 	DPRINTF(PDB_FOLLOW, ("ppiopen(%" PRIx64 ", %x): flags %x\n",
    197 	    dev, flags, sc->sc_flags));
    198 
    199 	if (sc->sc_flags & PPIF_OPEN)
    200 		return (EBUSY);
    201 	sc->sc_flags |= PPIF_OPEN;
    202 	sc->sc_burst = PPI_BURST;
    203 	sc->sc_timo = ppimstohz(PPI_TIMO);
    204 	sc->sc_delay = ppimstohz(PPI_DELAY);
    205 	sc->sc_sec = -1;
    206 	return (0);
    207 }
    208 
    209 int
    210 ppiclose(dev_t dev, int flags, int fmt, struct lwp *l)
    211 {
    212 	struct ppi_softc *sc;
    213 
    214 	sc = device_lookup_private(&ppi_cd, UNIT(dev));
    215 
    216 	DPRINTF(PDB_FOLLOW, ("ppiclose(%" PRIx64 ", %x): flags %x\n",
    217 		       dev, flags, sc->sc_flags));
    218 
    219 	sc->sc_flags &= ~PPIF_OPEN;
    220 	return (0);
    221 }
    222 
    223 void
    224 ppicallback(void *v, int action)
    225 {
    226 	struct ppi_softc *sc = v;
    227 
    228 	DPRINTF(PDB_FOLLOW, ("ppicallback: v=%p, action=%d\n", v, action));
    229 
    230 	switch (action) {
    231 	case GPIBCBF_START:
    232 		ppistart(sc);
    233 	case GPIBCBF_INTR:
    234 		/* no-op */
    235 		break;
    236 #ifdef DEBUG
    237 	default:
    238 		DPRINTF(PDB_FOLLOW, ("ppicallback: unknown action %d\n",
    239 		    action));
    240 		break;
    241 #endif
    242 	}
    243 }
    244 
    245 void
    246 ppistart(void *v)
    247 {
    248 	struct ppi_softc *sc = v;
    249 
    250 	DPRINTF(PDB_FOLLOW, ("ppistart(%x)\n", device_unit(sc->sc_dev)));
    251 
    252 	sc->sc_flags &= ~PPIF_DELAY;
    253 	wakeup(sc);
    254 }
    255 
    256 void
    257 ppitimo(void *arg)
    258 {
    259 	struct ppi_softc *sc = arg;
    260 
    261 	DPRINTF(PDB_FOLLOW, ("ppitimo(%x)\n", device_unit(sc->sc_dev)));
    262 
    263 	sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
    264 	wakeup(sc);
    265 }
    266 
    267 int
    268 ppiread(dev_t dev, struct uio *uio, int flags)
    269 {
    270 
    271 	DPRINTF(PDB_FOLLOW, ("ppiread(%" PRIx64 ", %p)\n", dev, uio));
    272 
    273 	return (ppirw(dev, uio));
    274 }
    275 
    276 int
    277 ppiwrite(dev_t dev, struct uio *uio, int flags)
    278 {
    279 
    280 	DPRINTF(PDB_FOLLOW, ("ppiwrite(%" PRIx64 ", %p)\n", dev, uio));
    281 
    282 	return (ppirw(dev, uio));
    283 }
    284 
    285 int
    286 ppirw(dev_t dev, struct uio *uio)
    287 {
    288 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
    289 	int s1, s2, len, cnt;
    290 	char *cp;
    291 	int error = 0, gotdata = 0;
    292 	int buflen, address;
    293 	char *buf;
    294 
    295 	if (uio->uio_resid == 0)
    296 		return (0);
    297 
    298 	address = sc->sc_address;
    299 
    300 	DPRINTF(PDB_FOLLOW|PDB_IO,
    301 	    ("ppirw(%" PRIx64 ", %p, %c): burst %d, timo %d, resid %x\n",
    302 	    dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
    303 	    sc->sc_burst, sc->sc_timo, uio->uio_resid));
    304 
    305 	buflen = min(sc->sc_burst, uio->uio_resid);
    306 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    307 	sc->sc_flags |= PPIF_UIO;
    308 	if (sc->sc_timo > 0) {
    309 		sc->sc_flags |= PPIF_TIMO;
    310 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
    311 	}
    312 	len = cnt = 0;
    313 	while (uio->uio_resid > 0) {
    314 		len = min(buflen, uio->uio_resid);
    315 		cp = buf;
    316 		if (uio->uio_rw == UIO_WRITE) {
    317 			error = uiomove(cp, len, uio);
    318 			if (error)
    319 				break;
    320 		}
    321 again:
    322 		s1 = splsoftclock();
    323 		s2 = splbio();
    324 		if (sc->sc_flags & PPIF_UIO) {
    325 			if (gpibrequest(sc->sc_ic, sc->sc_hdl) == 0)
    326 				(void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
    327 		}
    328 		/*
    329 		 * Check if we timed out during sleep or uiomove
    330 		 */
    331 		splx(s2);
    332 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    333 			DPRINTF(PDB_IO,
    334 			    ("ppirw: uiomove/sleep timo, flags %x\n",
    335 			    sc->sc_flags));
    336 			if (sc->sc_flags & PPIF_TIMO) {
    337 				callout_stop(&sc->sc_timo_ch);
    338 				sc->sc_flags &= ~PPIF_TIMO;
    339 			}
    340 			splx(s1);
    341 			break;
    342 		}
    343 		splx(s1);
    344 		/*
    345 		 * Perform the operation
    346 		 */
    347 		if (uio->uio_rw == UIO_WRITE)
    348 			cnt = gpibsend(sc->sc_ic, address, sc->sc_sec,
    349 			    cp, len);
    350 		else
    351 			cnt = gpibrecv(sc->sc_ic, address, sc->sc_sec,
    352 			    cp, len);
    353 		s1 = splbio();
    354 		gpibrelease(sc->sc_ic, sc->sc_hdl);
    355 		DPRINTF(PDB_IO, ("ppirw: %s(%d, %x, %p, %d) -> %d\n",
    356 		    uio->uio_rw == UIO_READ ? "recv" : "send",
    357 		    address, sc->sc_sec, cp, len, cnt));
    358 		splx(s1);
    359 		if (uio->uio_rw == UIO_READ) {
    360 			if (cnt) {
    361 				error = uiomove(cp, cnt, uio);
    362 				if (error)
    363 					break;
    364 				gotdata++;
    365 			}
    366 			/*
    367 			 * Didn't get anything this time, but did in the past.
    368 			 * Consider us done.
    369 			 */
    370 			else if (gotdata)
    371 				break;
    372 		}
    373 		s1 = splsoftclock();
    374 		/*
    375 		 * Operation timeout (or non-blocking), quit now.
    376 		 */
    377 		if ((sc->sc_flags & PPIF_UIO) == 0) {
    378 			DPRINTF(PDB_IO, ("ppirw: timeout/done\n"));
    379 			splx(s1);
    380 			break;
    381 		}
    382 		/*
    383 		 * Implement inter-read delay
    384 		 */
    385 		if (sc->sc_delay > 0) {
    386 			sc->sc_flags |= PPIF_DELAY;
    387 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
    388 			    ppistart, sc);
    389 			error = tsleep(sc, (PCATCH|PZERO) + 1, "gpib", 0);
    390 			if (error) {
    391 				splx(s1);
    392 				break;
    393 			}
    394 		}
    395 		splx(s1);
    396 		/*
    397 		 * Must not call uiomove again til we've used all data
    398 		 * that we already grabbed.
    399 		 */
    400 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    401 			cp += cnt;
    402 			len -= cnt;
    403 			cnt = 0;
    404 			goto again;
    405 		}
    406 	}
    407 	s1 = splsoftclock();
    408 	if (sc->sc_flags & PPIF_TIMO) {
    409 		callout_stop(&sc->sc_timo_ch);
    410 		sc->sc_flags &= ~PPIF_TIMO;
    411 	}
    412 	if (sc->sc_flags & PPIF_DELAY) {
    413 		callout_stop(&sc->sc_start_ch);
    414 		sc->sc_flags &= ~PPIF_DELAY;
    415 	}
    416 	splx(s1);
    417 	/*
    418 	 * Adjust for those chars that we uiomove'ed but never wrote
    419 	 */
    420 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    421 		uio->uio_resid += (len - cnt);
    422 		DPRINTF(PDB_IO, ("ppirw: short write, adjust by %d\n",
    423 		    len - cnt));
    424 	}
    425 	free(buf, M_DEVBUF);
    426 	DPRINTF(PDB_FOLLOW|PDB_IO, ("ppirw: return %d, resid %d\n",
    427 	    error, uio->uio_resid));
    428 	return (error);
    429 }
    430 
    431 int
    432 ppiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    433 {
    434 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
    435 	struct ppiparam *pp, *upp;
    436 	int error = 0;
    437 
    438 	switch (cmd) {
    439 	case PPIIOCGPARAM:
    440 		pp = &sc->sc_param;
    441 		upp = (struct ppiparam *)data;
    442 		upp->burst = pp->burst;
    443 		upp->timo = ppihztoms(pp->timo);
    444 		upp->delay = ppihztoms(pp->delay);
    445 		break;
    446 	case PPIIOCSPARAM:
    447 		pp = &sc->sc_param;
    448 		upp = (struct ppiparam *)data;
    449 		if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
    450 		    upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
    451 			return (EINVAL);
    452 		pp->burst = upp->burst;
    453 		pp->timo = ppimstohz(upp->timo);
    454 		pp->delay = ppimstohz(upp->delay);
    455 		break;
    456 	case PPIIOCSSEC:
    457 		sc->sc_sec = *(int *)data;
    458 		break;
    459 	default:
    460 		return (EINVAL);
    461 	}
    462 	return (error);
    463 }
    464 
    465 int
    466 ppihztoms(int h)
    467 {
    468 	extern int hz;
    469 	int m = h;
    470 
    471 	if (m > 0)
    472 		m = m * 1000 / hz;
    473 	return (m);
    474 }
    475 
    476 int
    477 ppimstohz(int m)
    478 {
    479 	extern int hz;
    480 	int h = m;
    481 
    482 	if (h > 0) {
    483 		h = h * hz / 1000;
    484 		if (h == 0)
    485 			h = 1000 / hz;
    486 	}
    487 	return (h);
    488 }
    489