Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.7.4.18
      1 /*
      2  * Copyright (c) 1993 Charles Hannum.
      3  * Copyright (c) 1990 William F. Jolitz, TeleMuse
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This software is a component of "386BSD" developed by
     17  *	William F. Jolitz, TeleMuse.
     18  * 4. Neither the name of the developer nor the name "386BSD"
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
     23  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
     24  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
     25  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
     26  * NOT MAKE USE OF THIS WORK.
     27  *
     28  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
     29  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
     30  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
     31  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
     32  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
     33  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
     34  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
     35  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
     36  *
     37  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
     38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
     41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     47  * SUCH DAMAGE.
     48  *
     49  *	$Id: lpt.c,v 1.7.4.18 1994/02/16 21:40:56 mycroft Exp $
     50  */
     51 
     52 /*
     53  * Device Driver for AT parallel printer port
     54  */
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/proc.h>
     59 #include <sys/user.h>
     60 #include <sys/buf.h>
     61 #include <sys/kernel.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/uio.h>
     64 #include <sys/device.h>
     65 #include <sys/syslog.h>
     66 
     67 #include <machine/cpu.h>
     68 #include <machine/pio.h>
     69 
     70 #include <i386/isa/isavar.h>
     71 #include <i386/isa/icu.h>
     72 #include <i386/isa/lptreg.h>
     73 
     74 #define	TIMEOUT		hz*16	/* wait up to 4 seconds for a ready */
     75 #define	STEP		hz/4
     76 
     77 #define	MAX_SPIN	255
     78 
     79 #define	LPTPRI		(PZERO+8)
     80 #define	LPT_BSIZE	1024
     81 
     82 #ifndef DEBUG
     83 #define lprintf
     84 #else
     85 #define lprintf		if (lptdebug) printf
     86 int lptdebug = 1;
     87 #endif
     88 
     89 struct lpt_softc {
     90 	struct	device sc_dev;
     91 	struct	isadev sc_id;
     92 	struct	intrhand sc_ih;
     93 
     94 	size_t	sc_count;
     95 	struct	buf *sc_inbuf;
     96 	u_char	*sc_cp ;
     97 	u_short	sc_iobase;
     98 	u_short	sc_irq;
     99 	u_char	sc_state;
    100 /* bits for state */
    101 #define	LPT_OPEN	0x01	/* device is open */
    102 #define	LPT_OBUSY	0x02	/* printer is busy doing output */
    103 #define LPT_INIT	0x04	/* waiting to initialize for open */
    104 	u_char	sc_flags;
    105 #define	LPT_AUTOLF	0x20	/* automatic LF on CR */
    106 #define	LPT_NOPRIME	0x40	/* don't prime on open */
    107 #define	LPT_NOINTR	0x80	/* do not use interrupt */
    108 	u_char	sc_control;
    109 	u_char	sc_smax;
    110 };
    111 
    112 static int lptprobe __P((struct device *, struct cfdata *, void *));
    113 #if 0 /* XXX */
    114 static void lptforceintr __P((void *));
    115 #endif
    116 static void lptattach __P((struct device *, struct device *, void *));
    117 static int lptintr __P((void *));
    118 
    119 struct	cfdriver lptcd =
    120 { NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc) };
    121 
    122 #define	LPTUNIT(s)	((s)&0x1f)
    123 #define	LPTFLAGS(s)	((s)&0xe0)
    124 
    125 #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
    126 #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
    127 #define	NOT_READY()	notready(inb(iobase + lpt_status), sc)
    128 
    129 static int notready __P((u_char, struct lpt_softc *));
    130 static void lptout __P((struct lpt_softc *));
    131 static int pushbytes __P((struct lpt_softc *));
    132 
    133 /*
    134  * Internal routine to lptprobe to do port tests of one byte value
    135  */
    136 int
    137 lpt_port_test(port, data, mask)
    138 	u_short port;
    139 	u_char data, mask;
    140 {
    141 	int	timeout;
    142 	u_char	temp;
    143 
    144 	data &= mask;
    145 	outb(port, data);
    146 	timeout = 1000;
    147 	do {
    148 		delay(10);
    149 		temp = inb(port) & mask;
    150 	} while (temp != data && --timeout);
    151 	lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", port, data,
    152 	    temp, timeout);
    153 	return (temp == data);
    154 }
    155 
    156 /*
    157  * Logic:
    158  *	1) You should be able to write to and read back the same value
    159  *	   to the data port.  Do an alternating zeros, alternating ones,
    160  *	   walking zero, and walking one test to check for stuck bits.
    161  *
    162  *	2) You should be able to write to and read back the same value
    163  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    164  *	   per the IBM PC technical reference manauls and different boards
    165  *	   do different things with them.  Do an alternating zeros, alternating
    166  *	   ones, walking zero, and walking one test to check for stuck bits.
    167  *
    168  *	   Some printers drag the strobe line down when the are powered off
    169  * 	   so this bit has been masked out of the control port test.
    170  *
    171  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    172  *	   don't know at this point, if that becomes a problem these bits
    173  *	   should be turned off in the mask byte for the control port test.
    174  *
    175  *	3) Set the data and control ports to a value of 0
    176  */
    177 
    178 static int
    179 lptprobe(parent, cf, aux)
    180 	struct device *parent;
    181 	struct cfdata *cf;
    182 	void *aux;
    183 {
    184 	struct	isa_attach_args *ia = aux;
    185 	u_short	iobase = ia->ia_iobase;
    186 	u_short	port = iobase + lpt_data;
    187 	u_char	mask = 0xff;
    188 	u_char	data;
    189 	int	i;
    190 
    191 	if (iobase == IOBASEUNK)
    192 		return 0;
    193 
    194 #ifdef DEBUG
    195 #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
    196 		    return 0;} while (0)
    197 #else
    198 #define	ABORT	return 0
    199 #endif
    200 
    201 	for (;;) {
    202 		data = 0x55;				/* Alternating zeros */
    203 		if (!lpt_port_test(port, data, mask))
    204 			ABORT;
    205 
    206 		data = 0xaa;				/* Alternating ones */
    207 		if (!lpt_port_test(port, data, mask))
    208 			ABORT;
    209 
    210 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    211 			data = ~(1 << i);
    212 			if (!lpt_port_test(port, data, mask))
    213 				ABORT;
    214 		}
    215 
    216 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    217 			data = (1 << i);
    218 			if (!lpt_port_test(port, data, mask))
    219 				ABORT;
    220 		}
    221 
    222 		if (port == iobase + lpt_data) {
    223 			port = iobase + lpt_control;
    224 #if 1 /* XXX */
    225 			mask = 0x04;
    226 #else
    227 			mask = 0x1e;
    228 #endif
    229 		} else
    230 			break;
    231 	}
    232 	outb(iobase + lpt_data, 0);
    233 	outb(iobase + lpt_control, 0);
    234 
    235 	if (ia->ia_irq == IRQUNK)
    236 #if 0 /* XXX */
    237 		ia->ia_irq = isa_discoverintr(lptforceintr, aux);
    238 #else
    239 		ia->ia_irq = IRQNONE;
    240 #endif
    241 	/* okay if we don't have an irq; will force polling */
    242 
    243 	ia->ia_iosize = LPT_NPORTS;
    244 	ia->ia_drq = DRQUNK;
    245 	ia->ia_msize = 0;
    246 	return 1;
    247 }
    248 
    249 #if 0 /* XXX */
    250 static void
    251 lptforceintr(aux)
    252 	void *aux;
    253 {
    254 	struct isa_attach_args *ia = aux;
    255 	u_short iobase = ia->ia_iobase;
    256 
    257 	outb(iobase + lpt_control, LPC_IENABLE);
    258 	delay(100);
    259 	outb(iobase + lpt_control, 0);
    260 }
    261 #endif
    262 
    263 static void
    264 lptattach(parent, self, aux)
    265 	struct device *parent, *self;
    266 	void *aux;
    267 {
    268 	struct	isa_attach_args *ia = aux;
    269 	struct	lpt_softc *sc = (struct lpt_softc *)self;
    270 	u_short	iobase = ia->ia_iobase;
    271 	u_short	irq = ia->ia_irq;
    272 
    273 	sc->sc_iobase = iobase;
    274 	sc->sc_irq = irq;
    275 	sc->sc_state = 0;
    276 	outb(iobase + lpt_control, LPC_NINIT);
    277 
    278 	printf(": %sparallel port\n", irq == IRQNONE ? "polled " : "");
    279 	isa_establish(&sc->sc_id, &sc->sc_dev);
    280 
    281 	if (irq != IRQNONE) {
    282 		sc->sc_ih.ih_fun = lptintr;
    283 		sc->sc_ih.ih_arg = sc;
    284 		intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    285 	}
    286 }
    287 
    288 /*
    289  * lptopen -- reset the printer, then wait until it's selected and not busy.
    290  */
    291 int
    292 lptopen(dev, flag)
    293 	dev_t dev;
    294 	int flag;
    295 {
    296 	int	unit = LPTUNIT(minor(dev));
    297 	u_char	flags = LPTFLAGS(minor(dev));
    298 	struct	lpt_softc *sc;
    299 	u_short	iobase;
    300 	u_char	control;
    301 	int	error;
    302 	int	spin;
    303 
    304 	if (unit >= lptcd.cd_ndevs)
    305 		return ENXIO;
    306 	sc = lptcd.cd_devs[unit];
    307 	if (!sc)
    308 		return ENXIO;
    309 
    310 	if (sc->sc_irq == IRQNONE && (flags & LPT_NOINTR) == 0)
    311 		return ENXIO;
    312 
    313 #ifdef DIAGNOSTIC
    314 	if (sc->sc_state)
    315 		printf("%s: state=0x%x not zero\n", sc->sc_dev.dv_xname,
    316 		    sc->sc_state);
    317 #endif
    318 
    319 	if (sc->sc_state)
    320 		return EBUSY;
    321 
    322 	sc->sc_state = LPT_INIT;
    323 
    324 	sc->sc_flags = flags;
    325 	lprintf("%s: open flags=0x%x\n", sc->sc_dev.dv_xname, flags);
    326 	iobase = sc->sc_iobase;
    327 
    328 	if ((flags & LPT_NOPRIME) == 0) {
    329 		/* assert INIT for 100 usec to start up printer */
    330 		outb(iobase + lpt_control, LPC_SELECT);
    331 		delay(100);
    332 	}
    333 
    334 	control = LPC_SELECT | LPC_NINIT;
    335 	outb(iobase + lpt_control, control);
    336 
    337 	/* wait till ready (printer running diagnostics) */
    338 	for (spin = 0; NOT_READY(); spin += STEP) {
    339 		if (spin >= TIMEOUT) {
    340 			sc->sc_state = 0;
    341 			return EBUSY;
    342 		}
    343 
    344 		/* wait 1/4 second, give up if we get a signal */
    345 		if (error = tsleep((caddr_t)sc,
    346 		    LPTPRI | PCATCH, "lptopen", STEP) != EWOULDBLOCK) {
    347 			sc->sc_state = 0;
    348 			return error;
    349 		}
    350 	}
    351 
    352 	if ((flags & LPT_NOINTR) == 0)
    353 		control |= LPC_IENABLE;
    354 	if (flags & LPT_AUTOLF)
    355 		control |= LPC_AUTOLF;
    356 	sc->sc_control = control;
    357 	outb(iobase + lpt_control, control);
    358 
    359 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    360 	sc->sc_count = 0;
    361 	sc->sc_state = LPT_OPEN;
    362 	lptout(sc);
    363 
    364 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
    365 	return 0;
    366 }
    367 
    368 static int
    369 notready(status, sc)
    370 	u_char status;
    371 	struct lpt_softc *sc;
    372 {
    373 	status ^= LPS_INVERT;
    374 
    375 	if (status & LPS_NOPAPER)
    376 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
    377 	else if (status & LPS_SELECT)
    378 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
    379 	else if (status & LPS_NERR)
    380 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
    381 
    382 	return status & LPS_MASK;
    383 }
    384 
    385 static void
    386 lptout(sc)
    387 	struct lpt_softc *sc;
    388 {
    389 	int	s;
    390 
    391 	if ((sc->sc_state & LPT_OPEN) == 0)
    392 		return;
    393 	if (sc->sc_flags & LPT_NOINTR)
    394 		return;
    395 
    396 	/*
    397 	 * Avoid possible hangs due to missed interrupts
    398 	 */
    399 	if (sc->sc_count) {
    400 		s = spltty();
    401 		(void) lptintr(sc);
    402 		splx(s);
    403 	} else {
    404 		sc->sc_state &= ~LPT_OBUSY;
    405 		wakeup((caddr_t)sc);
    406 	}
    407 
    408 	timeout((timeout_t)lptout, (caddr_t)sc, STEP);
    409 }
    410 
    411 /*
    412  * lptclose -- close the device, free the local line buffer.
    413  */
    414 int
    415 lptclose(dev, flag)
    416 	dev_t dev;
    417 	int flag;
    418 {
    419 	int	unit = LPTUNIT(minor(dev));
    420 	struct	lpt_softc *sc = lptcd.cd_devs[unit];
    421 	u_short	iobase = sc->sc_iobase;
    422 
    423 	(void) pushbytes(sc);
    424 
    425 	sc->sc_state = 0;
    426 	outb(iobase + lpt_control, LPC_NINIT);
    427 	brelse(sc->sc_inbuf);
    428 
    429 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
    430 	return 0;
    431 }
    432 
    433 static int
    434 pushbytes(sc)
    435 	struct lpt_softc *sc;
    436 {
    437 	u_short	iobase = sc->sc_iobase;
    438 	int	error;
    439 
    440 	if (sc->sc_flags & LPT_NOINTR) {
    441 		int	spin, tic;
    442 		u_char	control = sc->sc_control;
    443 		u_char	ch;
    444 
    445 		while (sc->sc_count > 0) {
    446 			spin = tic = 0;
    447 			while (NOT_READY())
    448 				if (++spin >= sc->sc_smax) {
    449 					/* exponential backoff */
    450 					tic = tic + tic + 1;
    451 					if (error = tsleep((caddr_t)sc,
    452 					    LPTPRI | PCATCH, "lptwrite1", tic))
    453 						return error;
    454 				}
    455 
    456 			outb(iobase + lpt_data, *sc->sc_cp++);
    457 			outb(iobase + lpt_control, control | LPC_STROBE);
    458 			sc->sc_count--;
    459 			outb(iobase + lpt_control, control);
    460 
    461 			/* adapt busy-wait algorithm */
    462 			if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
    463 				sc->sc_smax++;
    464 			if (spin*2 < sc->sc_smax)
    465 				sc->sc_smax--;
    466 		}
    467 	} else {
    468 		int	s;
    469 
    470 		while (sc->sc_count > 0) {
    471 			/* if the printer is ready for a char, give it one */
    472 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    473 				lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
    474 					sc->sc_count);
    475 				s = spltty();
    476 				(void) lptintr(sc);
    477 				splx(s);
    478 			}
    479 			if (error = tsleep((caddr_t)sc,
    480 			    LPTPRI | PCATCH, "lptwrite2", 0))
    481 				return error;
    482 		}
    483 	}
    484 	return 0;
    485 }
    486 
    487 /*
    488  * copy a line from user space to a local buffer, then call
    489  * putc to get the chars moved to the output queue.
    490  */
    491 int
    492 lptwrite(dev, uio)
    493 	dev_t dev;
    494 	struct uio *uio;
    495 {
    496 	int	unit = LPTUNIT(minor(dev));
    497 	struct	lpt_softc *sc = lptcd.cd_devs[unit];
    498 	size_t	n;
    499 	int	error = 0;
    500 
    501 	while (n = min(LPT_BSIZE, uio->uio_resid)) {
    502 		uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
    503 		sc->sc_count = n;
    504 		error = pushbytes(sc);
    505 		if (error) {
    506 			/* return accurate residual if interrupted or
    507 			   timed out */
    508 			uio->uio_resid += sc->sc_count;
    509 			sc->sc_count = 0;
    510 			return error;
    511 		}
    512 	}
    513 	return 0;
    514 }
    515 
    516 /*
    517  * lptintr -- handle printer interrupts which occur when the printer is
    518  * ready to accept another char.
    519  */
    520 static int
    521 lptintr(arg)
    522 	void *arg;
    523 {
    524 	struct	lpt_softc *sc = (struct lpt_softc *)arg;
    525 	u_short	iobase = sc->sc_iobase;
    526 	u_char	control = sc->sc_control;
    527 	u_char	status;
    528 
    529 	if ((sc->sc_state & LPT_OPEN) == 0)
    530 		return 0;
    531 
    532 	/* is printer online and ready for output */
    533 	if (NOT_READY())
    534 		return 0;
    535 
    536 	if (sc->sc_count) {
    537 		/* send char */
    538 		sc->sc_state |= LPT_OBUSY;
    539 		while (sc->sc_count && !NOT_READY()) {
    540 			outb(iobase + lpt_data, *sc->sc_cp++);
    541 			outb(iobase + lpt_control, control | LPC_STROBE);
    542 			sc->sc_count--;
    543 			outb(iobase + lpt_control, control);
    544 		}
    545 	} else {
    546 		/* none, wake up the top half to get more */
    547 		sc->sc_state &= ~LPT_OBUSY;
    548 		wakeup((caddr_t)sc);
    549 	}
    550 
    551 	return 1;
    552 }
    553 
    554 int
    555 lptioctl(dev, cmd, data, flag)
    556 	dev_t dev;
    557 	int cmd;
    558 	caddr_t data;
    559 	int flag;
    560 {
    561 	int	error = 0;
    562 
    563 	switch (cmd) {
    564 	default:
    565 		error = ENODEV;
    566 	}
    567 
    568 	return error;
    569 }
    570