Home | History | Annotate | Line # | Download | only in ic
lptvar.h revision 1.16
      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: lptvar.h,v 1.16 1994/04/07 06:51:01 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 	struct intrhand sc_ih;
     90 
     91 	size_t sc_count;
     92 	struct buf *sc_inbuf;
     93 	u_char *sc_cp;
     94 	int sc_spinmax;
     95 	u_short sc_iobase;
     96 	u_short sc_irq;
     97 	u_char sc_state;
     98 #define	LPT_OPEN	0x01	/* device is open */
     99 #define	LPT_OBUSY	0x02	/* printer is busy doing output */
    100 #define	LPT_INIT	0x04	/* waiting to initialize for open */
    101 	u_char sc_flags;
    102 #define	LPT_AUTOLF	0x20	/* automatic LF on CR */
    103 #define	LPT_NOPRIME	0x40	/* don't prime on open */
    104 #define	LPT_NOINTR	0x80	/* do not use interrupt */
    105 	u_char sc_control;
    106 };
    107 
    108 int lptprobe();
    109 void lptattach();
    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 #ifndef DIAGNOSTIC
    122 #define	NOT_READY()	((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK)
    123 #else
    124 #define	NOT_READY()	notready(inb(iobase + lpt_status), sc)
    125 static int notready __P((u_char, struct lpt_softc *));
    126 #endif
    127 
    128 static void lptout __P((struct lpt_softc *));
    129 static int pushbytes __P((struct lpt_softc *));
    130 
    131 /*
    132  * Internal routine to lptprobe to do port tests of one byte value.
    133  */
    134 int
    135 lpt_port_test(port, data, mask)
    136 	u_short port;
    137 	u_char data, mask;
    138 {
    139 	int timeout;
    140 	u_char temp;
    141 
    142 	data &= mask;
    143 	outb(port, data);
    144 	timeout = 1000;
    145 	do {
    146 		delay(10);
    147 		temp = inb(port) & mask;
    148 	} while (temp != data && --timeout);
    149 	lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", port, data,
    150 	    temp, timeout);
    151 	return (temp == data);
    152 }
    153 
    154 /*
    155  * Logic:
    156  *	1) You should be able to write to and read back the same value
    157  *	   to the data port.  Do an alternating zeros, alternating ones,
    158  *	   walking zero, and walking one test to check for stuck bits.
    159  *
    160  *	2) You should be able to write to and read back the same value
    161  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    162  *	   per the IBM PC technical reference manauls and different boards
    163  *	   do different things with them.  Do an alternating zeros, alternating
    164  *	   ones, walking zero, and walking one test to check for stuck bits.
    165  *
    166  *	   Some printers drag the strobe line down when the are powered off
    167  * 	   so this bit has been masked out of the control port test.
    168  *
    169  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    170  *	   don't know at this point, if that becomes a problem these bits
    171  *	   should be turned off in the mask byte for the control port test.
    172  *
    173  *	3) Set the data and control ports to a value of 0
    174  */
    175 int
    176 lptprobe(parent, self, aux)
    177 	struct device *parent, *self;
    178 	void *aux;
    179 {
    180 	struct isa_attach_args *ia = aux;
    181 	u_short iobase = ia->ia_iobase;
    182 	u_short port;
    183 	u_char mask, data;
    184 	int i;
    185 
    186 #ifdef DEBUG
    187 #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
    188 		    return 0;} while (0)
    189 #else
    190 #define	ABORT	return 0
    191 #endif
    192 
    193 	port = iobase + lpt_data;
    194 	mask = 0xff;
    195 
    196 	for (;;) {
    197 		data = 0x55;				/* Alternating zeros */
    198 		if (!lpt_port_test(port, data, mask))
    199 			ABORT;
    200 
    201 		data = 0xaa;				/* Alternating ones */
    202 		if (!lpt_port_test(port, data, mask))
    203 			ABORT;
    204 
    205 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    206 			data = ~(1 << i);
    207 			if (!lpt_port_test(port, data, mask))
    208 				ABORT;
    209 		}
    210 
    211 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    212 			data = (1 << i);
    213 			if (!lpt_port_test(port, data, mask))
    214 				ABORT;
    215 		}
    216 
    217 		if (port == iobase + lpt_data) {
    218 			port = iobase + lpt_control;
    219 #if 1 /* XXX */
    220 			mask = 0x04;
    221 #else
    222 			mask = 0x1e;
    223 #endif
    224 		} else
    225 			break;
    226 	}
    227 	outb(iobase + lpt_data, 0);
    228 	outb(iobase + lpt_control, 0);
    229 
    230 	ia->ia_iosize = LPT_NPORTS;
    231 	ia->ia_msize = 0;
    232 	return 1;
    233 }
    234 
    235 void
    236 lptattach(parent, self, aux)
    237 	struct device *parent, *self;
    238 	void *aux;
    239 {
    240 	struct lpt_softc *sc = (void *)self;
    241 	struct isa_attach_args *ia = aux;
    242 	u_short iobase = ia->ia_iobase;
    243 
    244 	if (ia->ia_irq)
    245 		printf("\n");
    246 	else
    247 		printf(": polled\n");
    248 
    249 	sc->sc_iobase = iobase;
    250 	sc->sc_irq = ia->ia_irq;
    251 	sc->sc_state = 0;
    252 	outb(iobase + lpt_control, LPC_NINIT);
    253 
    254 	if (ia->ia_irq) {
    255 		sc->sc_ih.ih_fun = lptintr;
    256 		sc->sc_ih.ih_arg = sc;
    257 		sc->sc_ih.ih_level = IPL_NONE;
    258 		intr_establish(ia->ia_irq, &sc->sc_ih);
    259 	}
    260 }
    261 
    262 /*
    263  * Reset the printer, then wait until it's selected and not busy.
    264  */
    265 int
    266 lptopen(dev, flag)
    267 	dev_t dev;
    268 	int flag;
    269 {
    270 	int unit = LPTUNIT(dev);
    271 	u_char flags = LPTFLAGS(dev);
    272 	struct lpt_softc *sc;
    273 	u_short iobase;
    274 	u_char control;
    275 	int error;
    276 	int spin;
    277 
    278 	if (unit >= lptcd.cd_ndevs)
    279 		return ENXIO;
    280 	sc = lptcd.cd_devs[unit];
    281 	if (!sc)
    282 		return ENXIO;
    283 
    284 	if (sc->sc_irq == 0 && (flags & LPT_NOINTR) == 0)
    285 		return ENXIO;
    286 
    287 #ifdef DIAGNOSTIC
    288 	if (sc->sc_state)
    289 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
    290 		    sc->sc_state);
    291 #endif
    292 
    293 	if (sc->sc_state)
    294 		return EBUSY;
    295 
    296 	sc->sc_state = LPT_INIT;
    297 	sc->sc_flags = flags;
    298 	lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
    299 	iobase = sc->sc_iobase;
    300 
    301 	if ((flags & LPT_NOPRIME) == 0) {
    302 		/* assert INIT for 100 usec to start up printer */
    303 		outb(iobase + lpt_control, LPC_SELECT);
    304 		delay(100);
    305 	}
    306 
    307 	control = LPC_SELECT | LPC_NINIT;
    308 	outb(iobase + lpt_control, control);
    309 
    310 	/* wait till ready (printer running diagnostics) */
    311 	for (spin = 0; NOT_READY(); spin += STEP) {
    312 		if (spin >= TIMEOUT) {
    313 			sc->sc_state = 0;
    314 			return EBUSY;
    315 		}
    316 
    317 		/* wait 1/4 second, give up if we get a signal */
    318 		if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen",
    319 		    STEP) != EWOULDBLOCK) {
    320 			sc->sc_state = 0;
    321 			return error;
    322 		}
    323 	}
    324 
    325 	if ((flags & LPT_NOINTR) == 0)
    326 		control |= LPC_IENABLE;
    327 	if (flags & LPT_AUTOLF)
    328 		control |= LPC_AUTOLF;
    329 	sc->sc_control = control;
    330 	outb(iobase + lpt_control, control);
    331 
    332 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    333 	sc->sc_count = 0;
    334 	sc->sc_state = LPT_OPEN;
    335 	lptout(sc);
    336 
    337 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
    338 	return 0;
    339 }
    340 
    341 #ifdef DIAGNOSTIC
    342 int
    343 notready(status, sc)
    344 	u_char status;
    345 	struct lpt_softc *sc;
    346 {
    347 	status ^= LPS_INVERT;
    348 
    349 	if (status & LPS_NOPAPER)
    350 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
    351 	else if (status & LPS_SELECT)
    352 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
    353 	else if (status & LPS_NERR)
    354 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
    355 
    356 	return status & LPS_MASK;
    357 }
    358 #endif
    359 
    360 void
    361 lptout(sc)
    362 	struct lpt_softc *sc;
    363 {
    364 	int s;
    365 
    366 	if (sc->sc_flags & LPT_NOINTR)
    367 		return;
    368 
    369 	s = spltty();
    370 	lptintr(sc);
    371 	splx(s);
    372 
    373 	timeout((timeout_t)lptout, (caddr_t)sc, STEP);
    374 }
    375 
    376 /*
    377  * Close the device, and free the local line buffer.
    378  */
    379 lptclose(dev, flag)
    380 	dev_t dev;
    381 	int flag;
    382 {
    383 	int unit = LPTUNIT(dev);
    384 	struct lpt_softc *sc = lptcd.cd_devs[unit];
    385 	u_short iobase = sc->sc_iobase;
    386 
    387 	if (sc->sc_count)
    388 		(void) pushbytes(sc);
    389 
    390 	outb(iobase + lpt_control, LPC_NINIT);
    391 	sc->sc_state = 0;
    392 	outb(iobase + lpt_control, LPC_NINIT);
    393 	brelse(sc->sc_inbuf);
    394 
    395 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
    396 	return 0;
    397 }
    398 
    399 int
    400 pushbytes(sc)
    401 	struct lpt_softc *sc;
    402 {
    403 	u_short iobase = sc->sc_iobase;
    404 	int error;
    405 
    406 	if (sc->sc_flags & LPT_NOINTR) {
    407 		int spin, tic;
    408 		u_char control = sc->sc_control;
    409 
    410 		while (sc->sc_count > 0) {
    411 			spin = 0;
    412 			while (NOT_READY() && spin++ < sc->sc_spinmax);
    413 			if (spin >= sc->sc_spinmax) {
    414 				tic = 0;
    415 				/* adapt busy-wait algorithm */
    416 				sc->sc_spinmax++;
    417 				while (NOT_READY()) {
    418 					/* exponential backoff */
    419 					tic = tic + tic + 1;
    420 					if (tic > TIMEOUT)
    421 						tic = TIMEOUT;
    422 					error = tsleep((caddr_t)sc,
    423 					    LPTPRI | PCATCH, "lptpsh", tic);
    424 					if (error != EWOULDBLOCK)
    425 						return error;
    426 				}
    427 			}
    428 
    429 			outb(iobase + lpt_data, *sc->sc_cp++);
    430 			outb(iobase + lpt_control, control | LPC_STROBE);
    431 			sc->sc_count--;
    432 			outb(iobase + lpt_control, control);
    433 
    434 			/* adapt busy-wait algorithm */
    435 			if (spin*2 < sc->sc_spinmax)
    436 				sc->sc_spinmax--;
    437 		}
    438 	} else {
    439 		int s;
    440 
    441 		while (sc->sc_count > 0) {
    442 			/* if the printer is ready for a char, give it one */
    443 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    444 				lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
    445 				    sc->sc_count);
    446 				s = spltty();
    447 				(void) lptintr(sc);
    448 				splx(s);
    449 			}
    450 			if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
    451 			    "lptwrite2", 0))
    452 				return error;
    453 		}
    454 	}
    455 	return 0;
    456 }
    457 
    458 /*
    459  * Copy a line from user space to a local buffer, then call putc to get the
    460  * chars moved to the output queue.
    461  */
    462 lptwrite(dev, uio)
    463 	dev_t dev;
    464 	struct uio *uio;
    465 {
    466 	struct lpt_softc *sc = lptcd.cd_devs[LPTUNIT(dev)];
    467 	size_t n;
    468 	int error = 0;
    469 
    470 	while (n = min(LPT_BSIZE, uio->uio_resid)) {
    471 		uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
    472 		sc->sc_count = n;
    473 		error = pushbytes(sc);
    474 		if (error) {
    475 			/*
    476 			 * Return accurate residual if interrupted or timed
    477 			 * out.
    478 			 */
    479 			uio->uio_resid += sc->sc_count;
    480 			sc->sc_count = 0;
    481 			return error;
    482 		}
    483 	}
    484 	return 0;
    485 }
    486 
    487 /*
    488  * Handle printer interrupts which occur when the printer is ready to accept
    489  * another char.
    490  */
    491 int
    492 lptintr(sc)
    493 	struct lpt_softc *sc;
    494 {
    495 	u_short iobase = sc->sc_iobase;
    496 
    497 #if 0
    498 	if ((sc->sc_state & LPT_OPEN) == 0)
    499 		return 0;
    500 #endif
    501 
    502 	/* is printer online and ready for output */
    503 	if (NOT_READY())
    504 		return 0;
    505 
    506 	if (sc->sc_count) {
    507 		u_char control = sc->sc_control;
    508 		/* send char */
    509 		outb(iobase + lpt_data, *sc->sc_cp++);
    510 		outb(iobase + lpt_control, control | LPC_STROBE);
    511 		sc->sc_count--;
    512 		outb(iobase + lpt_control, control);
    513 		sc->sc_state |= LPT_OBUSY;
    514 	} else
    515 		sc->sc_state &= ~LPT_OBUSY;
    516 
    517 	if (sc->sc_count == 0) {
    518 		/* none, wake up the top half to get more */
    519 		wakeup((caddr_t)sc);
    520 	}
    521 
    522 	return 1;
    523 }
    524 
    525 int
    526 lptioctl(dev, cmd, data, flag)
    527 	dev_t dev;
    528 	int cmd;
    529 	caddr_t data;
    530 	int flag;
    531 {
    532 	int error = 0;
    533 
    534 	switch (cmd) {
    535 	default:
    536 		error = ENODEV;
    537 	}
    538 
    539 	return error;
    540 }
    541