Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.15
      1 /*
      2  * Copyright (c) 1993, 1994 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.15 1994/03/29 04:36:08 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/isa.h>
     71 #include <i386/isa/isavar.h>
     72 #include <i386/isa/lptreg.h>
     73 
     74 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     75 #define	STEP		hz/4
     76 
     77 #define	LPTPRI		(PZERO+8)
     78 #define	LPT_BSIZE	1024
     79 
     80 #ifndef DEBUG
     81 #define lprintf
     82 #else
     83 #define lprintf		if (lptdebug) printf
     84 int lptdebug = 1;
     85 #endif
     86 
     87 struct lpt_softc {
     88 	struct device sc_dev;
     89 
     90 	size_t sc_count;
     91 	struct buf *sc_inbuf;
     92 	u_char *sc_cp;
     93 	int sc_spinmax;
     94 	u_short sc_iobase;
     95 	u_short 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 };
    106 
    107 int lptprobe();
    108 void lptattach();
    109 int lptintr __P((int));
    110 
    111 struct cfdriver lptcd = {
    112 	NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc)
    113 };
    114 
    115 #define	LPTUNIT(s)	(minor(s) & 0x1f)
    116 #define	LPTFLAGS(s)	(minor(s) & 0xe0)
    117 
    118 #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
    119 #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
    120 #ifndef DIAGNOSTIC
    121 #define	NOT_READY()	((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK)
    122 #else
    123 #define	NOT_READY()	notready(inb(iobase + lpt_status), sc)
    124 static int notready __P((u_char, struct lpt_softc *));
    125 #endif
    126 
    127 static void lptout __P((struct lpt_softc *));
    128 static int pushbytes __P((struct lpt_softc *));
    129 
    130 /*
    131  * Internal routine to lptprobe to do port tests of one byte value.
    132  */
    133 int
    134 lpt_port_test(port, data, mask)
    135 	u_short port;
    136 	u_char data, mask;
    137 {
    138 	int timeout;
    139 	u_char temp;
    140 
    141 	data &= mask;
    142 	outb(port, data);
    143 	timeout = 1000;
    144 	do {
    145 		delay(10);
    146 		temp = inb(port) & mask;
    147 	} while (temp != data && --timeout);
    148 	lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", port, data,
    149 	    temp, timeout);
    150 	return (temp == data);
    151 }
    152 
    153 /*
    154  * Logic:
    155  *	1) You should be able to write to and read back the same value
    156  *	   to the data port.  Do an alternating zeros, alternating ones,
    157  *	   walking zero, and walking one test to check for stuck bits.
    158  *
    159  *	2) You should be able to write to and read back the same value
    160  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    161  *	   per the IBM PC technical reference manauls and different boards
    162  *	   do different things with them.  Do an alternating zeros, alternating
    163  *	   ones, walking zero, and walking one test to check for stuck bits.
    164  *
    165  *	   Some printers drag the strobe line down when the are powered off
    166  * 	   so this bit has been masked out of the control port test.
    167  *
    168  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    169  *	   don't know at this point, if that becomes a problem these bits
    170  *	   should be turned off in the mask byte for the control port test.
    171  *
    172  *	3) Set the data and control ports to a value of 0
    173  */
    174 int
    175 lptprobe(parent, self, aux)
    176 	struct device *parent, *self;
    177 	void *aux;
    178 {
    179 	struct isa_attach_args *ia = aux;
    180 	u_short iobase = ia->ia_iobase;
    181 	u_short port;
    182 	u_char mask, data;
    183 	int i;
    184 
    185 #ifdef DEBUG
    186 #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
    187 		    return 0;} while (0)
    188 #else
    189 #define	ABORT	return 0
    190 #endif
    191 
    192 	port = iobase + lpt_data;
    193 	mask = 0xff;
    194 
    195 	for (;;) {
    196 		data = 0x55;				/* Alternating zeros */
    197 		if (!lpt_port_test(port, data, mask))
    198 			ABORT;
    199 
    200 		data = 0xaa;				/* Alternating ones */
    201 		if (!lpt_port_test(port, data, mask))
    202 			ABORT;
    203 
    204 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    205 			data = ~(1 << i);
    206 			if (!lpt_port_test(port, data, mask))
    207 				ABORT;
    208 		}
    209 
    210 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    211 			data = (1 << i);
    212 			if (!lpt_port_test(port, data, mask))
    213 				ABORT;
    214 		}
    215 
    216 		if (port == iobase + lpt_data) {
    217 			port = iobase + lpt_control;
    218 #if 1 /* XXX */
    219 			mask = 0x04;
    220 #else
    221 			mask = 0x1e;
    222 #endif
    223 		} else
    224 			break;
    225 	}
    226 	outb(iobase + lpt_data, 0);
    227 	outb(iobase + lpt_control, 0);
    228 
    229 	ia->ia_iosize = LPT_NPORTS;
    230 	ia->ia_msize = 0;
    231 	return 1;
    232 }
    233 
    234 void
    235 lptattach(parent, self, aux)
    236 	struct device *parent, *self;
    237 	void *aux;
    238 {
    239 	struct lpt_softc *sc = (void *)self;
    240 	struct isa_attach_args *ia = aux;
    241 	u_short iobase = ia->ia_iobase;
    242 	u_short irq = ia->ia_irq;
    243 
    244 	if (irq)
    245 		printf("\n");
    246 	else
    247 		printf(": polled\n");
    248 
    249 	sc->sc_iobase = iobase;
    250 	sc->sc_irq = irq;
    251 	sc->sc_state = 0;
    252 	outb(iobase + lpt_control, LPC_NINIT);
    253 }
    254 
    255 /*
    256  * Reset the printer, then wait until it's selected and not busy.
    257  */
    258 int
    259 lptopen(dev, flag)
    260 	dev_t dev;
    261 	int flag;
    262 {
    263 	int unit = LPTUNIT(dev);
    264 	u_char flags = LPTFLAGS(dev);
    265 	struct lpt_softc *sc;
    266 	u_short iobase;
    267 	u_char control;
    268 	int error;
    269 	int spin;
    270 
    271 	if (unit >= lptcd.cd_ndevs)
    272 		return ENXIO;
    273 	sc = lptcd.cd_devs[unit];
    274 	if (!sc)
    275 		return ENXIO;
    276 
    277 	if (sc->sc_irq == 0 && (flags & LPT_NOINTR) == 0)
    278 		return ENXIO;
    279 
    280 #ifdef DIAGNOSTIC
    281 	if (sc->sc_state)
    282 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
    283 		    sc->sc_state);
    284 #endif
    285 
    286 	if (sc->sc_state)
    287 		return EBUSY;
    288 
    289 	sc->sc_state = LPT_INIT;
    290 	sc->sc_flags = flags;
    291 	lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
    292 	iobase = sc->sc_iobase;
    293 
    294 	if ((flags & LPT_NOPRIME) == 0) {
    295 		/* assert INIT for 100 usec to start up printer */
    296 		outb(iobase + lpt_control, LPC_SELECT);
    297 		delay(100);
    298 	}
    299 
    300 	control = LPC_SELECT | LPC_NINIT;
    301 	outb(iobase + lpt_control, control);
    302 
    303 	/* wait till ready (printer running diagnostics) */
    304 	for (spin = 0; NOT_READY(); spin += STEP) {
    305 		if (spin >= TIMEOUT) {
    306 			sc->sc_state = 0;
    307 			return EBUSY;
    308 		}
    309 
    310 		/* wait 1/4 second, give up if we get a signal */
    311 		if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen",
    312 		    STEP) != EWOULDBLOCK) {
    313 			sc->sc_state = 0;
    314 			return error;
    315 		}
    316 	}
    317 
    318 	if ((flags & LPT_NOINTR) == 0)
    319 		control |= LPC_IENABLE;
    320 	if (flags & LPT_AUTOLF)
    321 		control |= LPC_AUTOLF;
    322 	sc->sc_control = control;
    323 	outb(iobase + lpt_control, control);
    324 
    325 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    326 	sc->sc_count = 0;
    327 	sc->sc_state = LPT_OPEN;
    328 	lptout(sc);
    329 
    330 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
    331 	return 0;
    332 }
    333 
    334 #ifdef DIAGNOSTIC
    335 int
    336 notready(status, sc)
    337 	u_char status;
    338 	struct lpt_softc *sc;
    339 {
    340 	status ^= LPS_INVERT;
    341 
    342 	if (status & LPS_NOPAPER)
    343 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
    344 	else if (status & LPS_SELECT)
    345 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
    346 	else if (status & LPS_NERR)
    347 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
    348 
    349 	return status & LPS_MASK;
    350 }
    351 #endif
    352 
    353 void
    354 lptout(sc)
    355 	struct lpt_softc *sc;
    356 {
    357 	int s;
    358 
    359 	if (sc->sc_flags & LPT_NOINTR)
    360 		return;
    361 
    362 	s = spltty();
    363 	lptintr(sc->sc_dev.dv_unit);
    364 	splx(s);
    365 
    366 	timeout((timeout_t)lptout, (caddr_t)sc, STEP);
    367 }
    368 
    369 /*
    370  * Close the device, and free the local line buffer.
    371  */
    372 lptclose(dev, flag)
    373 	dev_t dev;
    374 	int flag;
    375 {
    376 	int unit = LPTUNIT(dev);
    377 	struct lpt_softc *sc = lptcd.cd_devs[unit];
    378 	u_short iobase = sc->sc_iobase;
    379 
    380 	if (sc->sc_count)
    381 		(void) pushbytes(sc);
    382 
    383 	outb(iobase + lpt_control, LPC_NINIT);
    384 	sc->sc_state = 0;
    385 	outb(iobase + lpt_control, LPC_NINIT);
    386 	brelse(sc->sc_inbuf);
    387 
    388 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
    389 	return 0;
    390 }
    391 
    392 int
    393 pushbytes(sc)
    394 	struct lpt_softc *sc;
    395 {
    396 	u_short iobase = sc->sc_iobase;
    397 	int error;
    398 
    399 	if (sc->sc_flags & LPT_NOINTR) {
    400 		int spin, tic;
    401 		u_char control = sc->sc_control;
    402 
    403 		while (sc->sc_count > 0) {
    404 			spin = 0;
    405 			while (NOT_READY() && spin++ < sc->sc_spinmax);
    406 			if (spin >= sc->sc_spinmax) {
    407 				tic = 0;
    408 				/* adapt busy-wait algorithm */
    409 				sc->sc_spinmax++;
    410 				while (NOT_READY()) {
    411 					/* exponential backoff */
    412 					tic = tic + tic + 1;
    413 					if (tic > TIMEOUT)
    414 						tic = TIMEOUT;
    415 					error = tsleep((caddr_t)sc,
    416 					    LPTPRI | PCATCH, "lptpsh", tic);
    417 					if (error != EWOULDBLOCK)
    418 						return error;
    419 				}
    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 < 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->sc_dev.dv_unit);
    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_un.b_addr, 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(unit)
    486 	int unit;
    487 {
    488 	struct lpt_softc *sc = lptcd.cd_devs[unit];
    489 	u_short iobase = sc->sc_iobase;
    490 
    491 #if 0
    492 	if ((sc->sc_state & LPT_OPEN) == 0)
    493 		return 0;
    494 #endif
    495 
    496 	/* is printer online and ready for output */
    497 	if (NOT_READY())
    498 		return 0;
    499 
    500 	if (sc->sc_count) {
    501 		u_char control = sc->sc_control;
    502 		/* send char */
    503 		outb(iobase + lpt_data, *sc->sc_cp++);
    504 		outb(iobase + lpt_control, control | LPC_STROBE);
    505 		sc->sc_count--;
    506 		outb(iobase + lpt_control, control);
    507 		sc->sc_state |= LPT_OBUSY;
    508 	} else
    509 		sc->sc_state &= ~LPT_OBUSY;
    510 
    511 	if (sc->sc_count == 0) {
    512 		/* none, wake up the top half to get more */
    513 		wakeup((caddr_t)sc);
    514 	}
    515 
    516 	return 1;
    517 }
    518 
    519 int
    520 lptioctl(dev, cmd, data, flag)
    521 	dev_t dev;
    522 	int cmd;
    523 	caddr_t data;
    524 	int flag;
    525 {
    526 	int error = 0;
    527 
    528 	switch (cmd) {
    529 	default:
    530 		error = ENODEV;
    531 	}
    532 
    533 	return error;
    534 }
    535