Home | History | Annotate | Line # | Download | only in dev
par.c revision 1.22
      1 /*	$NetBSD: par.c,v 1.22 2004/12/13 02:14:14 chs 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.22 2004/12/13 02:14:14 chs 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 
     52 #include <machine/bus.h>
     53 #include <machine/cpu.h>
     54 #include <machine/parioctl.h>
     55 
     56 #include <arch/x68k/dev/intiovar.h>
     57 
     58 struct	par_softc {
     59 	struct device		sc_dev;
     60 
     61 	bus_space_tag_t		sc_bst;
     62 	bus_space_handle_t	sc_bsh;
     63 	int			sc_flags;
     64 	struct parparam		sc_param;
     65 #define sc_burst 	sc_param.burst
     66 #define sc_timo  	sc_param.timo
     67 #define sc_delay 	sc_param.delay
     68 	struct callout		sc_timo_ch;
     69 	struct callout		sc_start_ch;
     70 } ;
     71 
     72 /* par registers */
     73 #define PAR_DATA	1
     74 #define PAR_STROBE	3
     75 
     76 /* sc_flags values */
     77 #define	PARF_ALIVE	0x01
     78 #define	PARF_OPEN	0x02
     79 #define PARF_UIO	0x04
     80 #define PARF_TIMO	0x08
     81 #define PARF_DELAY	0x10
     82 #define PARF_OREAD	0x40	/* no support */
     83 #define PARF_OWRITE	0x80
     84 
     85 
     86 void partimo __P((void *));
     87 void parstart __P((void *);)
     88 void parintr __P((void *));
     89 int parrw __P((dev_t, struct uio *));
     90 int parhztoms __P((int));
     91 int parmstohz __P((int));
     92 int parsendch __P((struct par_softc*, u_char));
     93 int parsend __P((struct par_softc*, u_char *, int));
     94 
     95 static struct callout intr_callout = CALLOUT_INITIALIZER;
     96 
     97 #define UNIT(x)		minor(x)
     98 
     99 #ifdef DEBUG
    100 #define PDB_FOLLOW	0x01
    101 #define PDB_IO		0x02
    102 #define PDB_INTERRUPT   0x04
    103 #define PDB_NOCHECK	0x80
    104 #ifdef PARDEBUG
    105 int	pardebug = PDB_FOLLOW | PDB_IO | PDB_INTERRUPT;
    106 #else
    107 int	pardebug = 0;
    108 #endif
    109 #endif
    110 
    111 int parmatch __P((struct device *, struct cfdata *, void *));
    112 void parattach __P((struct device *, struct device *, void *));
    113 
    114 CFATTACH_DECL(par, sizeof(struct par_softc),
    115     parmatch, parattach, NULL, NULL);
    116 
    117 extern struct cfdriver par_cd;
    118 
    119 static int par_attached;
    120 
    121 dev_type_open(paropen);
    122 dev_type_close(parclose);
    123 dev_type_write(parwrite);
    124 dev_type_ioctl(parioctl);
    125 
    126 const struct cdevsw par_cdevsw = {
    127 	paropen, parclose, noread, parwrite, parioctl,
    128 	nostop, notty, nopoll, nommap, nokqfilter,
    129 };
    130 
    131 int
    132 parmatch(pdp, cfp, aux)
    133 	struct device *pdp;
    134 	struct cfdata *cfp;
    135 	void *aux;
    136 {
    137 	struct intio_attach_args *ia = aux;
    138 
    139 	/* X680x0 has only one parallel port */
    140 	if (strcmp(ia->ia_name, "par") || par_attached)
    141 		return 0;
    142 
    143 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    144 		ia->ia_addr = 0xe8c000;
    145 	ia->ia_size = 0x2000;
    146 	if (intio_map_allocate_region (pdp, ia, INTIO_MAP_TESTONLY))
    147 		return 0;
    148 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
    149 		ia->ia_intr = 99;
    150 #if DIAGNOSTIC
    151 	if (ia->ia_intr != 99)
    152 		return 0;
    153 #endif
    154 
    155 	return 1;
    156 }
    157 
    158 void
    159 parattach(pdp, dp, aux)
    160 	struct device *pdp, *dp;
    161 	void *aux;
    162 {
    163 	struct par_softc *sc = (struct par_softc *)dp;
    164 	struct intio_attach_args *ia = aux;
    165 	int r;
    166 
    167 	par_attached = 1;
    168 
    169 	sc->sc_flags = PARF_ALIVE;
    170 	printf(": parallel port (write only, interrupt)\n");
    171 	ia->ia_size = 0x2000;
    172 	r = intio_map_allocate_region (pdp, ia, INTIO_MAP_ALLOCATE);
    173 #ifdef DIAGNOSTIC
    174 	if (r)
    175 		panic ("IO map for PAR corruption??");
    176 #endif
    177 	sc->sc_bst = ia->ia_bst;
    178 	r = bus_space_map (sc->sc_bst,
    179 			   ia->ia_addr, ia->ia_size,
    180 			   BUS_SPACE_MAP_SHIFTED,
    181 			   &sc->sc_bsh);
    182 #ifdef DIAGNOSTIC
    183 	if (r)
    184 		panic ("Cannot map IO space for PAR.");
    185 #endif
    186 
    187 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    188 				~SICILIAN_INTR_PAR);
    189 
    190 	intio_intr_establish(ia->ia_intr, "par",
    191 			     (intio_intr_handler_t) parintr, (void*) 1);
    192 
    193 	callout_init(&sc->sc_timo_ch);
    194 	callout_init(&sc->sc_start_ch);
    195 }
    196 
    197 int
    198 paropen(dev, flags, mode, p)
    199 	dev_t dev;
    200 	int flags, mode;
    201 	struct proc *p;
    202 {
    203 	int unit = UNIT(dev);
    204 	struct par_softc *sc;
    205 
    206 	sc = device_lookup(&par_cd, unit);
    207 	if (sc == NULL || !(sc->sc_flags & PARF_ALIVE))
    208 		return(ENXIO);
    209 	if (sc->sc_flags & PARF_OPEN)
    210 		return(EBUSY);
    211 	/* X680x0 can't read */
    212 	if ((flags & FREAD) == FREAD)
    213 		return (EINVAL);
    214 
    215 	sc->sc_flags |= PARF_OPEN;
    216 
    217 	sc->sc_flags |= PARF_OWRITE;
    218 
    219 	sc->sc_burst = PAR_BURST;
    220 	sc->sc_timo = parmstohz(PAR_TIMO);
    221 	sc->sc_delay = parmstohz(PAR_DELAY);
    222 
    223 	return(0);
    224 }
    225 
    226 int
    227 parclose(dev, flags, mode, p)
    228 	dev_t dev;
    229 	int flags, mode;
    230 	struct proc *p;
    231 {
    232 	int unit = UNIT(dev);
    233 	int s;
    234 	struct par_softc *sc = par_cd.cd_devs[unit];
    235 
    236 	sc->sc_flags &= ~(PARF_OPEN|PARF_OWRITE);
    237 
    238 	/* don't allow interrupts any longer */
    239 	s = spl1();
    240 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    241 				~SICILIAN_INTR_PAR);
    242 	splx(s);
    243 
    244 	return (0);
    245 }
    246 
    247 void
    248 parstart(arg)
    249 	void *arg;
    250 {
    251 	struct par_softc *sc = arg;
    252 #ifdef DEBUG
    253 	if (pardebug & PDB_FOLLOW)
    254 		printf("parstart(%x)\n", sc->sc_dev.dv_unit);
    255 #endif
    256 	sc->sc_flags &= ~PARF_DELAY;
    257 	wakeup(sc);
    258 }
    259 
    260 void
    261 partimo(arg)
    262 	void *arg;
    263 {
    264 	struct par_softc *sc = arg;
    265 #ifdef DEBUG
    266 	if (pardebug & PDB_FOLLOW)
    267 		printf("partimo(%x)\n", sc->sc_dev.dv_unit);
    268 #endif
    269 	sc->sc_flags &= ~(PARF_UIO|PARF_TIMO);
    270 	wakeup(sc);
    271 }
    272 
    273 int
    274 parwrite(dev, uio, flag)
    275 	dev_t dev;
    276 	struct uio *uio;
    277 	int flag;
    278 {
    279 
    280 #ifdef DEBUG
    281 	if (pardebug & PDB_FOLLOW)
    282 		printf("parwrite(%x, %p)\n", dev, uio);
    283 #endif
    284 	return (parrw(dev, uio));
    285 }
    286 
    287 int
    288 parrw(dev, uio)
    289 	dev_t dev;
    290 	struct uio *uio;
    291 {
    292 	int unit = UNIT(dev);
    293 	struct par_softc *sc = par_cd.cd_devs[unit];
    294 	int len=0xdeadbeef;	/* XXX: shutup gcc */
    295 	int s, cnt=0;
    296 	char *cp;
    297 	int error = 0;
    298 	int buflen;
    299 	char *buf;
    300 
    301 	if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
    302 		return EINVAL;
    303 
    304 	if (uio->uio_resid == 0)
    305 		return(0);
    306 
    307 	buflen = min(sc->sc_burst, uio->uio_resid);
    308 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
    309 	sc->sc_flags |= PARF_UIO;
    310 	if (sc->sc_timo > 0) {
    311 		sc->sc_flags |= PARF_TIMO;
    312 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, partimo, sc);
    313 	}
    314 	while (uio->uio_resid > 0) {
    315 		len = min(buflen, uio->uio_resid);
    316 		cp = buf;
    317 		if (uio->uio_rw == UIO_WRITE) {
    318 			error = uiomove(cp, len, uio);
    319 			if (error)
    320 				break;
    321 		}
    322 	      again:
    323 		s = spl1();
    324 		/*
    325 		 * Check if we timed out during sleep or uiomove
    326 		 */
    327 		(void) spllowersoftclock();
    328 		if ((sc->sc_flags & PARF_UIO) == 0) {
    329 #ifdef DEBUG
    330 			if (pardebug & PDB_IO)
    331 				printf("parrw: uiomove/sleep timo, flags %x\n",
    332 				       sc->sc_flags);
    333 #endif
    334 			if (sc->sc_flags & PARF_TIMO) {
    335 				callout_stop(&sc->sc_timo_ch);
    336 				sc->sc_flags &= ~PARF_TIMO;
    337 			}
    338 			splx(s);
    339 			break;
    340 		}
    341 		splx(s);
    342 		/*
    343 		 * Perform the operation
    344 		 */
    345 		cnt = parsend(sc, cp, len);
    346 		if (cnt < 0) {
    347 			error = -cnt;
    348 			break;
    349 		}
    350 
    351 		s = splsoftclock();
    352 		/*
    353 		 * Operation timeout (or non-blocking), quit now.
    354 		 */
    355 		if ((sc->sc_flags & PARF_UIO) == 0) {
    356 #ifdef DEBUG
    357 			if (pardebug & PDB_IO)
    358 				printf("parrw: timeout/done\n");
    359 #endif
    360 			splx(s);
    361 			break;
    362 		}
    363 		/*
    364 		 * Implement inter-read delay
    365 		 */
    366 		if (sc->sc_delay > 0) {
    367 			sc->sc_flags |= PARF_DELAY;
    368 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
    369 			    parstart, sc);
    370 			error = tsleep(sc, PCATCH|(PZERO-1), "par-cdelay", 0);
    371 			if (error) {
    372 				splx(s);
    373 				break;
    374 			}
    375 		}
    376 		splx(s);
    377 		/*
    378 		 * Must not call uiomove again til we've used all data
    379 		 * that we already grabbed.
    380 		 */
    381 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
    382 			cp += cnt;
    383 			len -= cnt;
    384 			cnt = 0;
    385 			goto again;
    386 		}
    387 	}
    388 	s = splsoftclock();
    389 	if (sc->sc_flags & PARF_TIMO) {
    390 		callout_stop(&sc->sc_timo_ch);
    391 		sc->sc_flags &= ~PARF_TIMO;
    392 	}
    393 	if (sc->sc_flags & PARF_DELAY)	{
    394 		callout_stop(&sc->sc_start_ch);
    395 		sc->sc_flags &= ~PARF_DELAY;
    396 	}
    397 	splx(s);
    398 	/*
    399 	 * Adjust for those chars that we uiomove'ed but never wrote
    400 	 */
    401 	/*
    402 	 * XXXjdolecek: this len usage is wrong, this will be incorrect
    403 	 * if the transfer size is longer than sc_burst
    404 	 */
    405 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
    406 		uio->uio_resid += (len - cnt);
    407 #ifdef DEBUG
    408 			if (pardebug & PDB_IO)
    409 				printf("parrw: short write, adjust by %d\n",
    410 				       len-cnt);
    411 #endif
    412 	}
    413 	free(buf, M_DEVBUF);
    414 #ifdef DEBUG
    415 	if (pardebug & (PDB_FOLLOW|PDB_IO))
    416 		printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
    417 #endif
    418 	return (error);
    419 }
    420 
    421 int
    422 parioctl(dev, cmd, data, flag, p)
    423 	dev_t dev;
    424 	u_long cmd;
    425 	caddr_t data;
    426 	int flag;
    427 	struct proc *p;
    428 {
    429 	struct par_softc *sc = par_cd.cd_devs[UNIT(dev)];
    430 	struct parparam *pp, *upp;
    431 	int error = 0;
    432 
    433 	switch (cmd) {
    434 	      case PARIOCGPARAM:
    435 		pp = &sc->sc_param;
    436 		upp = (struct parparam *)data;
    437 		upp->burst = pp->burst;
    438 		upp->timo = parhztoms(pp->timo);
    439 		upp->delay = parhztoms(pp->delay);
    440 		break;
    441 
    442 	      case PARIOCSPARAM:
    443 		pp = &sc->sc_param;
    444 		upp = (struct parparam *)data;
    445 		if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
    446 		    upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
    447 			return(EINVAL);
    448 		pp->burst = upp->burst;
    449 		pp->timo = parmstohz(upp->timo);
    450 		pp->delay = parmstohz(upp->delay);
    451 		break;
    452 
    453 	      default:
    454 		return(EINVAL);
    455 	}
    456 	return (error);
    457 }
    458 
    459 int
    460 parhztoms(h)
    461 	int h;
    462 {
    463 	extern int hz;
    464 	int m = h;
    465 
    466 	if (m > 0)
    467 		m = m * 1000 / hz;
    468 	return(m);
    469 }
    470 
    471 int
    472 parmstohz(m)
    473 	int m;
    474 {
    475 	extern int hz;
    476 	int h = m;
    477 
    478 	if (h > 0) {
    479 		h = h * hz / 1000;
    480 		if (h == 0)
    481 			h = 1000 / hz;
    482 	}
    483 	return(h);
    484 }
    485 
    486 /* stuff below here if for interrupt driven output of data thru
    487    the parallel port. */
    488 
    489 int partimeout_pending;
    490 int parsend_pending;
    491 
    492 void
    493 parintr(arg)
    494 	void *arg;
    495 {
    496 	int s, mask;
    497 
    498 	mask = (int)arg;
    499 	s = splclock();
    500 
    501 	intio_set_sicilian_intr(intio_get_sicilian_intr() &
    502 				~SICILIAN_INTR_PAR);
    503 
    504 #ifdef DEBUG
    505 	if (pardebug & PDB_INTERRUPT)
    506 		printf ("parintr %d(%s)\n", mask, mask ? "FLG" : "tout");
    507 #endif
    508 	/* if invoked from timeout handler, mask will be 0,
    509 	 * if from interrupt, it will contain the cia-icr mask,
    510 	 * which is != 0
    511 	 */
    512 	if (mask) {
    513 		if (partimeout_pending)
    514 			callout_stop(&intr_callout);
    515 		if (parsend_pending)
    516 			parsend_pending = 0;
    517 	}
    518 
    519 	/* either way, there won't be a timeout pending any longer */
    520 	partimeout_pending = 0;
    521 
    522 	wakeup(parintr);
    523 	splx (s);
    524 }
    525 
    526 int
    527 parsendch(sc, ch)
    528 	struct par_softc *sc;
    529 	u_char ch;
    530 {
    531 	int error = 0;
    532 	int s;
    533 
    534 	/* if either offline, busy or out of paper, wait for that
    535 	   condition to clear */
    536 	s = spl1();
    537 	while (!error
    538 	       && (parsend_pending
    539 		   || !(intio_get_sicilian_intr() & SICILIAN_STAT_PAR)))
    540 		{
    541 			extern int hz;
    542 
    543 			/* wait a second, and try again */
    544 			callout_reset(&intr_callout, hz, parintr, 0);
    545 			partimeout_pending = 1;
    546 			/* this is essentially a flipflop to have us wait for the
    547 			   first character being transmitted when trying to transmit
    548 			   the second, etc. */
    549 			parsend_pending = 0;
    550 			/* it's quite important that a parallel putc can be
    551 			   interrupted, given the possibility to lock a printer
    552 			   in an offline condition.. */
    553 			if ((error = tsleep (parintr, PCATCH|(PZERO-1), "parsendch", 0))) {
    554 #ifdef DEBUG
    555 				if (pardebug & PDB_INTERRUPT)
    556 					printf ("parsendch interrupted, error = %d\n", error);
    557 #endif
    558 				if (partimeout_pending)
    559 					callout_stop(&intr_callout);
    560 
    561 				partimeout_pending = 0;
    562 			}
    563 		}
    564 
    565 	if (!error) {
    566 #ifdef DEBUG
    567 		if (pardebug & PDB_INTERRUPT)
    568 			printf ("#%d", ch);
    569 #endif
    570 		bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_DATA, ch);
    571 		DELAY(1);	/* (DELAY(1) == 1us) > 0.5us */
    572 		bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_STROBE, 0);
    573 		intio_set_sicilian_intr (intio_get_sicilian_intr() |
    574 					 SICILIAN_INTR_PAR);
    575 		DELAY(1);
    576 		bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_STROBE, 1);
    577 		parsend_pending = 1;
    578 	}
    579 
    580 	splx (s);
    581 
    582 	return error;
    583 }
    584 
    585 
    586 int
    587 parsend(sc, buf, len)
    588 	struct par_softc *sc;
    589 	u_char *buf;
    590 	int len;
    591 {
    592 	int err, orig_len = len;
    593 
    594 	for (; len; len--, buf++)
    595 		if ((err = parsendch (sc, *buf)))
    596 			return err < 0 ? -EINTR : -err;
    597 
    598 	/* either all or nothing.. */
    599 	return orig_len;
    600 }
    601