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