Home | History | Annotate | Line # | Download | only in dev
par.c revision 1.40
      1 /*	$NetBSD: par.c,v 1.40 2014/03/16 05:20:26 dholland Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ppi.c	7.3 (Berkeley) 12/16/90
     32  */
     33 
     34 /*
     35  * parallel port interface
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: par.c,v 1.40 2014/03/16 05:20:26 dholland Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/errno.h>
     43 #include <sys/uio.h>
     44 #include <sys/device.h>
     45 #include <sys/malloc.h>
     46 #include <sys/file.h>
     47 #include <sys/systm.h>
     48 #include <sys/callout.h>
     49 #include <sys/proc.h>
     50 #include <sys/conf.h>
     51 #include <sys/kernel.h>
     52 
     53 #include <machine/bus.h>
     54 #include <machine/cpu.h>
     55 #include <machine/parioctl.h>
     56 
     57 #include <arch/x68k/dev/intiovar.h>
     58 
     59 struct	par_softc {
     60 	device_t		sc_dev;
     61 
     62 	bus_space_tag_t		sc_bst;
     63 	bus_space_handle_t	sc_bsh;
     64 	int			sc_flags;
     65 	struct parparam		sc_param;
     66 #define sc_burst 	sc_param.burst
     67 #define sc_timo  	sc_param.timo
     68 #define sc_delay 	sc_param.delay
     69 	struct callout		sc_timo_ch;
     70 	struct callout		sc_start_ch;
     71 } ;
     72 
     73 /* par registers */
     74 #define PAR_DATA	1
     75 #define PAR_STROBE	3
     76 
     77 /* sc_flags values */
     78 #define	PARF_ALIVE	0x01
     79 #define	PARF_OPEN	0x02
     80 #define PARF_UIO	0x04
     81 #define PARF_TIMO	0x08
     82 #define PARF_DELAY	0x10
     83 #define PARF_OREAD	0x40	/* no support */
     84 #define PARF_OWRITE	0x80
     85 
     86 
     87 void partimo(void *);
     88 void parstart(void *);
     89 void parintr(void *);
     90 int parrw(dev_t, struct uio *);
     91 int parhztoms(int);
     92 int parmstohz(int);
     93 int parsendch(struct par_softc *, u_char);
     94 int parsend(struct par_softc *, u_char *, int);
     95 
     96 static struct callout intr_callout;
     97 
     98 #define UNIT(x)		minor(x)
     99 
    100 #ifdef DEBUG
    101 #define PDB_FOLLOW	0x01
    102 #define PDB_IO		0x02
    103 #define PDB_INTERRUPT   0x04
    104 #define PDB_NOCHECK	0x80
    105 #ifdef PARDEBUG
    106 int	pardebug = PDB_FOLLOW | PDB_IO | PDB_INTERRUPT;
    107 #else
    108 int	pardebug = 0;
    109 #endif
    110 #endif
    111 
    112 int parmatch(device_t, cfdata_t, void *);
    113 void parattach(device_t, device_t, void *);
    114 
    115 CFATTACH_DECL_NEW(par, sizeof(struct par_softc),
    116     parmatch, parattach, NULL, NULL);
    117 
    118 extern struct cfdriver par_cd;
    119 
    120 static int par_attached;
    121 
    122 dev_type_open(paropen);
    123 dev_type_close(parclose);
    124 dev_type_write(parwrite);
    125 dev_type_ioctl(parioctl);
    126 
    127 const struct cdevsw par_cdevsw = {
    128 	.d_open = paropen,
    129 	.d_close = parclose,
    130 	.d_read = noread,
    131 	.d_write = parwrite,
    132 	.d_ioctl = parioctl,
    133 	.d_stop = nostop,
    134 	.d_tty = notty,
    135 	.d_poll = nopoll,
    136 	.d_mmap = nommap,
    137 	.d_kqfilter = nokqfilter,
    138 	.d_flag = 0
    139 };
    140 
    141 int
    142 parmatch(device_t parent, cfdata_t cf, void *aux)
    143 {
    144 	struct intio_attach_args *ia = aux;
    145 
    146 	/* X680x0 has only one parallel port */
    147 	if (strcmp(ia->ia_name, "par") || par_attached)
    148 		return 0;
    149 
    150 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    151 		ia->ia_addr = 0xe8c000;
    152 	ia->ia_size = 0x2000;
    153 	if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY))
    154 		return 0;
    155 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
    156 		ia->ia_intr = 99;
    157 #if DIAGNOSTIC
    158 	if (ia->ia_intr != 99)
    159 		return 0;
    160 #endif
    161 
    162 	return 1;
    163 }
    164 
    165 void
    166 parattach(device_t parent, device_t self, void *aux)
    167 {
    168 	struct par_softc *sc = device_private(self);
    169 	struct intio_attach_args *ia = aux;
    170 	int r;
    171 
    172 	par_attached = 1;
    173 
    174 	sc->sc_dev = self;
    175 	sc->sc_flags = PARF_ALIVE;
    176 	aprint_normal(": parallel port (write only, interrupt)\n");
    177 	ia->ia_size = 0x2000;
    178 	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
    179 #ifdef DIAGNOSTIC
    180 	if (r)
    181 		panic("IO map for PAR corruption??");
    182 #endif
    183 	sc->sc_bst = ia->ia_bst;
    184 	r = bus_space_map(sc->sc_bst,
    185 			   ia->ia_addr, ia->ia_size,
    186 			   BUS_SPACE_MAP_SHIFTED,
    187 			   &sc->sc_bsh);
    188 #ifdef DIAGNOSTIC
    189 	if (r)
    190 		panic("Cannot map IO space for PAR.");
    191 #endif
    192 
    193 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    194 				~SICILIAN_INTR_PAR);
    195 
    196 	intio_intr_establish(ia->ia_intr, "par",
    197 			     (intio_intr_handler_t)parintr, (void *)1);
    198 
    199 	callout_init(&sc->sc_timo_ch, 0);
    200 	callout_init(&sc->sc_start_ch, 0);
    201 	callout_init(&intr_callout, 0);
    202 }
    203 
    204 int
    205 paropen(dev_t dev, int flags, int mode, struct lwp *l)
    206 {
    207 	int unit = UNIT(dev);
    208 	struct par_softc *sc;
    209 
    210 	sc = device_lookup_private(&par_cd, unit);
    211 	if (sc == NULL || !(sc->sc_flags & PARF_ALIVE))
    212 		return(ENXIO);
    213 	if (sc->sc_flags & PARF_OPEN)
    214 		return(EBUSY);
    215 	/* X680x0 can't read */
    216 	if ((flags & FREAD) == FREAD)
    217 		return (EINVAL);
    218 
    219 	sc->sc_flags |= PARF_OPEN;
    220 
    221 	sc->sc_flags |= PARF_OWRITE;
    222 
    223 	sc->sc_burst = PAR_BURST;
    224 	sc->sc_timo = parmstohz(PAR_TIMO);
    225 	sc->sc_delay = parmstohz(PAR_DELAY);
    226 
    227 	return(0);
    228 }
    229 
    230 int
    231 parclose(dev_t dev, int flags, int mode, struct lwp *l)
    232 {
    233 	int unit = UNIT(dev);
    234 	int s;
    235 	struct par_softc *sc = device_lookup_private(&par_cd, unit);
    236 
    237 	sc->sc_flags &= ~(PARF_OPEN|PARF_OWRITE);
    238 
    239 	/* don't allow interrupts any longer */
    240 	s = spl1();
    241 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    242 				~SICILIAN_INTR_PAR);
    243 	splx(s);
    244 
    245 	return (0);
    246 }
    247 
    248 void
    249 parstart(void *arg)
    250 {
    251 	struct par_softc *sc = arg;
    252 #ifdef DEBUG
    253 	if (pardebug & PDB_FOLLOW)
    254 		printf("parstart(%x)\n", device_unit(sc->sc_dev));
    255 #endif
    256 	sc->sc_flags &= ~PARF_DELAY;
    257 	wakeup(sc);
    258 }
    259 
    260 void
    261 partimo(void *arg)
    262 {
    263 	struct par_softc *sc = arg;
    264 #ifdef DEBUG
    265 	if (pardebug & PDB_FOLLOW)
    266 		printf("partimo(%x)\n", device_unit(sc->sc_dev));
    267 #endif
    268 	sc->sc_flags &= ~(PARF_UIO|PARF_TIMO);
    269 	wakeup(sc);
    270 }
    271 
    272 int
    273 parwrite(dev_t dev, struct uio *uio, int flag)
    274 {
    275 
    276 #ifdef DEBUG
    277 	if (pardebug & PDB_FOLLOW)
    278 		printf("parwrite(%x, %p)\n", UNIT(dev), uio);
    279 #endif
    280 	return (parrw(dev, uio));
    281 }
    282 
    283 int
    284 parrw(dev_t dev, struct uio *uio)
    285 {
    286 	int unit = UNIT(dev);
    287 	struct par_softc *sc = device_lookup_private(&par_cd, unit);
    288 	int len=0xdeadbeef;	/* XXX: shutup gcc */
    289 	int s, cnt=0;
    290 	char *cp;
    291 	int error = 0;
    292 	int buflen;
    293 	char *buf;
    294 
    295 	if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
    296 		return EINVAL;
    297 
    298 	if (uio->uio_resid == 0)
    299 		return(0);
    300 
    301 	buflen = min(sc->sc_burst, uio->uio_resid);
    302 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    303 	sc->sc_flags |= PARF_UIO;
    304 	if (sc->sc_timo > 0) {
    305 		sc->sc_flags |= PARF_TIMO;
    306 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, partimo, sc);
    307 	}
    308 	while (uio->uio_resid > 0) {
    309 		len = min(buflen, uio->uio_resid);
    310 		cp = buf;
    311 		if (uio->uio_rw == UIO_WRITE) {
    312 			error = uiomove(cp, len, uio);
    313 			if (error)
    314 				break;
    315 		}
    316 	      again:
    317 		s = splsoftclock();
    318 		/*
    319 		 * Check if we timed out during sleep or uiomove
    320 		 */
    321 		if ((sc->sc_flags & PARF_UIO) == 0) {
    322 #ifdef DEBUG
    323 			if (pardebug & PDB_IO)
    324 				printf("parrw: uiomove/sleep timo, flags %x\n",
    325 				       sc->sc_flags);
    326 #endif
    327 			if (sc->sc_flags & PARF_TIMO) {
    328 				callout_stop(&sc->sc_timo_ch);
    329 				sc->sc_flags &= ~PARF_TIMO;
    330 			}
    331 			splx(s);
    332 			break;
    333 		}
    334 		splx(s);
    335 		/*
    336 		 * Perform the operation
    337 		 */
    338 		cnt = parsend(sc, cp, len);
    339 		if (cnt < 0) {
    340 			error = -cnt;
    341 			break;
    342 		}
    343 
    344 		s = splsoftclock();
    345 		/*
    346 		 * Operation timeout (or non-blocking), quit now.
    347 		 */
    348 		if ((sc->sc_flags & PARF_UIO) == 0) {
    349 #ifdef DEBUG
    350 			if (pardebug & PDB_IO)
    351 				printf("parrw: timeout/done\n");
    352 #endif
    353 			splx(s);
    354 			break;
    355 		}
    356 		/*
    357 		 * Implement inter-read delay
    358 		 */
    359 		if (sc->sc_delay > 0) {
    360 			sc->sc_flags |= PARF_DELAY;
    361 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
    362 			    parstart, sc);
    363 			error = tsleep(sc, PCATCH|(PZERO-1), "par-cdelay", 0);
    364 			if (error) {
    365 				splx(s);
    366 				break;
    367 			}
    368 		}
    369 		splx(s);
    370 		/*
    371 		 * Must not call uiomove again til we've used all data
    372 		 * that we already grabbed.
    373 		 */
    374 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    375 			cp += cnt;
    376 			len -= cnt;
    377 			cnt = 0;
    378 			goto again;
    379 		}
    380 	}
    381 	s = splsoftclock();
    382 	if (sc->sc_flags & PARF_TIMO) {
    383 		callout_stop(&sc->sc_timo_ch);
    384 		sc->sc_flags &= ~PARF_TIMO;
    385 	}
    386 	if (sc->sc_flags & PARF_DELAY)	{
    387 		callout_stop(&sc->sc_start_ch);
    388 		sc->sc_flags &= ~PARF_DELAY;
    389 	}
    390 	splx(s);
    391 	/*
    392 	 * Adjust for those chars that we uiomove'ed but never wrote
    393 	 */
    394 	/*
    395 	 * XXXjdolecek: this len usage is wrong, this will be incorrect
    396 	 * if the transfer size is longer than sc_burst
    397 	 */
    398 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    399 		uio->uio_resid += (len - cnt);
    400 #ifdef DEBUG
    401 			if (pardebug & PDB_IO)
    402 				printf("parrw: short write, adjust by %d\n",
    403 				       len-cnt);
    404 #endif
    405 	}
    406 	free(buf, M_DEVBUF);
    407 #ifdef DEBUG
    408 	if (pardebug & (PDB_FOLLOW|PDB_IO))
    409 		printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
    410 #endif
    411 	return (error);
    412 }
    413 
    414 int
    415 parioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    416 {
    417 	struct par_softc *sc = device_lookup_private(&par_cd, UNIT(dev));
    418 	struct parparam *pp, *upp;
    419 	int error = 0;
    420 
    421 	switch (cmd) {
    422 	case PARIOCGPARAM:
    423 		pp = &sc->sc_param;
    424 		upp = (struct parparam *)data;
    425 		upp->burst = pp->burst;
    426 		upp->timo = parhztoms(pp->timo);
    427 		upp->delay = parhztoms(pp->delay);
    428 		break;
    429 
    430 	case PARIOCSPARAM:
    431 		pp = &sc->sc_param;
    432 		upp = (struct parparam *)data;
    433 		if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
    434 		    upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
    435 			return(EINVAL);
    436 		pp->burst = upp->burst;
    437 		pp->timo = parmstohz(upp->timo);
    438 		pp->delay = parmstohz(upp->delay);
    439 		break;
    440 
    441 	default:
    442 		return(EINVAL);
    443 	}
    444 	return (error);
    445 }
    446 
    447 int
    448 parhztoms(int h)
    449 {
    450 	int m = h;
    451 
    452 	if (m > 0)
    453 		m = m * 1000 / hz;
    454 	return(m);
    455 }
    456 
    457 int
    458 parmstohz(int m)
    459 {
    460 	int h = m;
    461 
    462 	if (h > 0) {
    463 		h = h * hz / 1000;
    464 		if (h == 0)
    465 			h = 1000 / hz;
    466 	}
    467 	return(h);
    468 }
    469 
    470 /* stuff below here if for interrupt driven output of data thru
    471    the parallel port. */
    472 
    473 int partimeout_pending;
    474 int parsend_pending;
    475 
    476 void
    477 parintr(void *arg)
    478 {
    479 	int s, mask;
    480 
    481 	mask = (int)arg;
    482 	s = splclock();
    483 
    484 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    485 				~SICILIAN_INTR_PAR);
    486 
    487 #ifdef DEBUG
    488 	if (pardebug & PDB_INTERRUPT)
    489 		printf("parintr %d(%s)\n", mask, mask ? "FLG" : "tout");
    490 #endif
    491 	/* if invoked from timeout handler, mask will be 0,
    492 	 * if from interrupt, it will contain the cia-icr mask,
    493 	 * which is != 0
    494 	 */
    495 	if (mask) {
    496 		if (partimeout_pending)
    497 			callout_stop(&intr_callout);
    498 		if (parsend_pending)
    499 			parsend_pending = 0;
    500 	}
    501 
    502 	/* either way, there won't be a timeout pending any longer */
    503 	partimeout_pending = 0;
    504 
    505 	wakeup(parintr);
    506 	splx(s);
    507 }
    508 
    509 int
    510 parsendch(struct par_softc *sc, u_char ch)
    511 {
    512 	int error = 0;
    513 	int s;
    514 
    515 	/* if either offline, busy or out of paper, wait for that
    516 	   condition to clear */
    517 	s = spl1();
    518 	while (!error
    519 	       && (parsend_pending
    520 		   || !(intio_get_sicilian_intr() & SICILIAN_STAT_PAR)))
    521 	{
    522 		/* wait a second, and try again */
    523 		callout_reset(&intr_callout, hz, parintr, 0);
    524 		partimeout_pending = 1;
    525 		/* this is essentially a flipflop to have us wait for the
    526 		   first character being transmitted when trying to transmit
    527 		   the second, etc. */
    528 		parsend_pending = 0;
    529 		/* it's quite important that a parallel putc can be
    530 		   interrupted, given the possibility to lock a printer
    531 		   in an offline condition.. */
    532 		if ((error = tsleep(parintr, PCATCH|(PZERO-1), "parsendch", 0))) {
    533 #ifdef DEBUG
    534 			if (pardebug & PDB_INTERRUPT)
    535 				printf("parsendch interrupted, error = %d\n", error);
    536 #endif
    537 			if (partimeout_pending)
    538 				callout_stop(&intr_callout);
    539 
    540 			partimeout_pending = 0;
    541 		}
    542 	}
    543 
    544 	if (!error) {
    545 #ifdef DEBUG
    546 		if (pardebug & PDB_INTERRUPT)
    547 			printf("#%d", ch);
    548 #endif
    549 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_DATA, ch);
    550 		DELAY(1);	/* (DELAY(1) == 1us) > 0.5us */
    551 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_STROBE, 0);
    552 		intio_set_sicilian_intr(intio_get_sicilian_intr() |
    553 					 SICILIAN_INTR_PAR);
    554 		DELAY(1);
    555 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_STROBE, 1);
    556 		parsend_pending = 1;
    557 	}
    558 
    559 	splx(s);
    560 
    561 	return error;
    562 }
    563 
    564 
    565 int
    566 parsend(struct par_softc *sc, u_char *buf, int len)
    567 {
    568 	int err, orig_len = len;
    569 
    570 	for (; len; len--, buf++)
    571 		if ((err = parsendch(sc, *buf)))
    572 			return err < 0 ? -EINTR : -err;
    573 
    574 	/* either all or nothing.. */
    575 	return orig_len;
    576 }
    577