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