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