Home | History | Annotate | Line # | Download | only in ic
lptvar.h revision 1.11
      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.11 1994/02/18 19:50:56 mycroft Exp $
     50  */
     51 
     52 /*
     53  * Device Driver for AT parallel printer port
     54  */
     55 
     56 #include "lpt.h"
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/proc.h>
     61 #include <sys/user.h>
     62 #include <sys/buf.h>
     63 #include <sys/kernel.h>
     64 #include <sys/ioctl.h>
     65 #include <sys/uio.h>
     66 #include <sys/device.h>
     67 #include <sys/syslog.h>
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/pio.h>
     71 
     72 #include <i386/isa/isa.h>
     73 #include <i386/isa/isa_device.h>
     74 #include <i386/isa/lptreg.h>
     75 
     76 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     77 #define	STEP		hz/4
     78 
     79 #define	MAX_SPIN	255
     80 
     81 #define	LPTPRI		(PZERO+8)
     82 #define	LPT_BSIZE	1024
     83 
     84 #ifndef DEBUG
     85 #define lprintf
     86 #else
     87 #define lprintf		if (lptdebug) printf
     88 int lptdebug = 1;
     89 #endif
     90 
     91 struct lpt_softc {
     92 	struct device sc_dev;
     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 #define	LPT_OPEN	0x01	/* device is open */
    101 #define	LPT_OBUSY	0x02	/* printer is busy doing output */
    102 #define	LPT_INIT	0x04	/* waiting to initialize for open */
    103 	u_char sc_flags;
    104 #define	LPT_AUTOLF	0x20	/* automatic LF on CR */
    105 #define	LPT_NOPRIME	0x40	/* don't prime on open */
    106 #define	LPT_NOINTR	0x80	/* do not use interrupt */
    107 	u_char sc_control;
    108 	u_char sc_smax;
    109 } lpt_softc[NLPT];
    110 
    111 int lptprobe __P((struct isa_device *));
    112 int lptattach __P((struct isa_device *));
    113 int lptintr __P((int));
    114 
    115 struct isa_driver lptdriver = {
    116 	lptprobe, lptattach, "lpt"
    117 };
    118 
    119 #define	LPTUNIT(s)	(minor(s) & 0x1f)
    120 #define	LPTFLAGS(s)	(minor(s) & 0xe0)
    121 
    122 #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
    123 #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
    124 #define	NOT_READY()	notready(inb(iobase + lpt_status), sc)
    125 
    126 static int notready __P((u_char, struct lpt_softc *));
    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(isa_dev)
    176 	struct isa_device *isa_dev;
    177 {
    178 	u_short iobase = isa_dev->id_iobase;
    179 	u_short 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 	for (;;) {
    194 		data = 0x55;				/* Alternating zeros */
    195 		if (!lpt_port_test(port, data, mask))
    196 			ABORT;
    197 
    198 		data = 0xaa;				/* Alternating ones */
    199 		if (!lpt_port_test(port, data, mask))
    200 			ABORT;
    201 
    202 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    203 			data = ~(1 << i);
    204 			if (!lpt_port_test(port, data, mask))
    205 				ABORT;
    206 		}
    207 
    208 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    209 			data = (1 << i);
    210 			if (!lpt_port_test(port, data, mask))
    211 				ABORT;
    212 		}
    213 
    214 		if (port == iobase + lpt_data) {
    215 			port = iobase + lpt_control;
    216 #if 1 /* XXX */
    217 			mask = 0x04;
    218 #else
    219 			mask = 0x1e;
    220 #endif
    221 		} else
    222 			break;
    223 	}
    224 	outb(iobase + lpt_data, 0);
    225 	outb(iobase + lpt_control, 0);
    226 
    227 	return LPT_NPORTS;
    228 }
    229 
    230 int
    231 lptattach(isa_dev)
    232 	struct isa_device *isa_dev;
    233 {
    234 	struct lpt_softc *sc = &lpt_softc[isa_dev->id_unit];
    235 	u_short iobase = isa_dev->id_iobase;
    236 	u_short irq = isa_dev->id_irq;
    237 
    238 	/* XXX HACK */
    239 	sprintf(sc->sc_dev.dv_xname, "%s%d", lptdriver.name, isa_dev->id_unit);
    240 	sc->sc_dev.dv_unit = isa_dev->id_unit;
    241 
    242 	if (!irq)
    243 		printf("%s: polled\n", sc->sc_dev.dv_xname);
    244 
    245 	sc->sc_iobase = iobase;
    246 	sc->sc_irq = irq;
    247 	sc->sc_state = 0;
    248 	outb(iobase + lpt_control, LPC_NINIT);
    249 }
    250 
    251 /*
    252  * Reset the printer, then wait until it's selected and not busy.
    253  */
    254 int
    255 lptopen(dev, flag)
    256 	dev_t dev;
    257 	int flag;
    258 {
    259 	int unit = LPTUNIT(dev);
    260 	u_char flags = LPTFLAGS(dev);
    261 	struct lpt_softc *sc;
    262 	u_short iobase;
    263 	u_char control;
    264 	int error;
    265 	int spin;
    266 
    267 	if (unit >= NLPT)
    268 		return ENXIO;
    269 	sc = &lpt_softc[unit];
    270 	if (!sc->sc_iobase)
    271 		return ENXIO;
    272 
    273 	if (sc->sc_irq == 0 && (flags & LPT_NOINTR) == 0)
    274 		return ENXIO;
    275 
    276 #ifdef DIAGNOSTIC
    277 	if (sc->sc_state)
    278 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
    279 		    sc->sc_state);
    280 #endif
    281 
    282 	if (sc->sc_state)
    283 		return EBUSY;
    284 
    285 	sc->sc_state = LPT_INIT;
    286 	sc->sc_flags = flags;
    287 	lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
    288 	iobase = sc->sc_iobase;
    289 
    290 	if ((flags & LPT_NOPRIME) == 0) {
    291 		/* assert INIT for 100 usec to start up printer */
    292 		outb(iobase + lpt_control, LPC_SELECT);
    293 		DELAY(100);
    294 	}
    295 
    296 	control = LPC_SELECT | LPC_NINIT;
    297 	outb(iobase + lpt_control, control);
    298 
    299 	/* wait till ready (printer running diagnostics) */
    300 	for (spin = 0; NOT_READY(); spin += STEP) {
    301 		if (spin >= TIMEOUT) {
    302 			sc->sc_state = 0;
    303 			return EBUSY;
    304 		}
    305 
    306 		/* wait 1/4 second, give up if we get a signal */
    307 		if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen",
    308 		    STEP) != EWOULDBLOCK) {
    309 			sc->sc_state = 0;
    310 			return error;
    311 		}
    312 	}
    313 
    314 	if ((flags & LPT_NOINTR) == 0)
    315 		control |= LPC_IENABLE;
    316 	if (flags & LPT_AUTOLF)
    317 		control |= LPC_AUTOLF;
    318 	sc->sc_control = control;
    319 	outb(iobase + lpt_control, control);
    320 
    321 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    322 	sc->sc_count = 0;
    323 	sc->sc_state = LPT_OPEN;
    324 	lptout(sc);
    325 
    326 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
    327 	return 0;
    328 }
    329 
    330 int
    331 notready(status, sc)
    332 	u_char status;
    333 	struct lpt_softc *sc;
    334 {
    335 	status ^= LPS_INVERT;
    336 
    337 	if (status & LPS_NOPAPER)
    338 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
    339 	else if (status & LPS_SELECT)
    340 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
    341 	else if (status & LPS_NERR)
    342 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
    343 
    344 	return status & LPS_MASK;
    345 }
    346 
    347 void
    348 lptout(sc)
    349 	struct lpt_softc *sc;
    350 {
    351 	int s;
    352 
    353 	if ((sc->sc_state & LPT_OPEN) == 0)
    354 		return;
    355 	if (sc->sc_flags & LPT_NOINTR)
    356 		return;
    357 
    358 	/*
    359 	 * Avoid possible hangs do to missed interrupts.
    360 	 */
    361 	if (sc->sc_count) {
    362 		s = spltty();
    363 		lptintr(sc->sc_dev.dv_unit);
    364 		splx(s);
    365 	} else {
    366 		sc->sc_state &= ~LPT_OBUSY;
    367 		wakeup((caddr_t)sc);
    368 	}
    369 
    370 	timeout((timeout_t)lptout, (caddr_t)sc, STEP);
    371 }
    372 
    373 /*
    374  * Close the device, and free the local line buffer.
    375  */
    376 lptclose(dev, flag)
    377 	dev_t dev;
    378 	int flag;
    379 {
    380 	int unit = LPTUNIT(dev);
    381 	struct lpt_softc *sc = &lpt_softc[unit];
    382 	u_short iobase = sc->sc_iobase;
    383 
    384 	if (sc->sc_count)
    385 		(void) pushbytes(sc);
    386 
    387 	outb(iobase + lpt_control, LPC_NINIT);
    388 	sc->sc_state = 0;
    389 	outb(iobase + lpt_control, LPC_NINIT);
    390 	brelse(sc->sc_inbuf);
    391 
    392 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
    393 	return 0;
    394 }
    395 
    396 int
    397 pushbytes(sc)
    398 	struct lpt_softc *sc;
    399 {
    400 	u_short iobase = sc->sc_iobase;
    401 	int error;
    402 
    403 	if (sc->sc_flags & LPT_NOINTR) {
    404 		int spin, tic;
    405 		u_char control = sc->sc_control;
    406 
    407 		while (sc->sc_count > 0) {
    408 			spin = 0;
    409 			while (NOT_READY() && spin++ < sc->sc_smax);
    410 			if (spin >= sc->sc_smax) {
    411 				tic = 0;
    412 				if (sc->sc_smax < MAX_SPIN)
    413 					sc->sc_smax++;
    414 				while (NOT_READY()) {
    415 					/* exponential backoff */
    416 					tic = tic + tic + 1;
    417 					if (tic > TIMEOUT)
    418 						tic = TIMEOUT;
    419 					error = tsleep((caddr_t)sc,
    420 					    LPTPRI | PCATCH, "lptpsh", tic);
    421 					if (error != EWOULDBLOCK)
    422 						return error;
    423 				}
    424 			}
    425 
    426 			outb(iobase + lpt_data, *sc->sc_cp++);
    427 			outb(iobase + lpt_control, control | LPC_STROBE);
    428 			sc->sc_count--;
    429 			outb(iobase + lpt_control, control);
    430 
    431 			/* adapt busy-wait algorithm */
    432 			if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
    433 				sc->sc_smax++;
    434 			if (spin*2 < sc->sc_smax)
    435 				sc->sc_smax--;
    436 		}
    437 	} else {
    438 		int s;
    439 
    440 		while (sc->sc_count > 0) {
    441 			/* if the printer is ready for a char, give it one */
    442 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    443 				lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
    444 				    sc->sc_count);
    445 				s = spltty();
    446 				(void) lptintr(sc->sc_dev.dv_unit);
    447 				splx(s);
    448 			}
    449 			if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
    450 			    "lptwrite2", 0))
    451 				return error;
    452 		}
    453 	}
    454 	return 0;
    455 }
    456 
    457 /*
    458  * Copy a line from user space to a local buffer, then call putc to get the
    459  * chars moved to the output queue.
    460  */
    461 lptwrite(dev, uio)
    462 	dev_t dev;
    463 	struct uio *uio;
    464 {
    465 	struct lpt_softc *sc = &lpt_softc[LPTUNIT(dev)];
    466 	size_t n;
    467 	int error = 0;
    468 
    469 	while (n = min(LPT_BSIZE, uio->uio_resid)) {
    470 		uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
    471 		sc->sc_count = n;
    472 		error = pushbytes(sc);
    473 		if (error) {
    474 			/*
    475 			 * Return accurate residual if interrupted or timed
    476 			 * out.
    477 			 */
    478 			uio->uio_resid += sc->sc_count;
    479 			sc->sc_count = 0;
    480 			return error;
    481 		}
    482 	}
    483 	return 0;
    484 }
    485 
    486 /*
    487  * Handle printer interrupts which occur when the printer is ready to accept
    488  * another char.
    489  */
    490 int
    491 lptintr(unit)
    492 	int unit;
    493 {
    494 	struct lpt_softc *sc = &lpt_softc[unit];
    495 	u_short iobase = sc->sc_iobase;
    496 	u_char control = sc->sc_control;
    497 
    498 	if ((sc->sc_state & LPT_OPEN) == 0)
    499 		return 0;
    500 
    501 	/* is printer online and ready for output */
    502 	if (NOT_READY())
    503 		return 0;
    504 
    505 	if (sc->sc_count) {
    506 		/* send char */
    507 		sc->sc_state |= LPT_OBUSY;
    508 #if 0
    509 		while (sc->sc_count && !NOT_READY()) {
    510 #endif
    511 			outb(iobase + lpt_data, *sc->sc_cp++);
    512 			outb(iobase + lpt_control, control | LPC_STROBE);
    513 			sc->sc_count--;
    514 			outb(iobase + lpt_control, control);
    515 #if 0
    516 		}
    517 #endif
    518 	} else
    519 		sc->sc_state &= ~LPT_OBUSY;
    520 
    521 	if (sc->sc_count == 0) {
    522 		/* none, wake up the top half to get more */
    523 		wakeup((caddr_t)sc);
    524 	}
    525 
    526 	return 1;
    527 }
    528 
    529 int
    530 lptioctl(dev, cmd, data, flag)
    531 	dev_t dev;
    532 	int cmd;
    533 	caddr_t data;
    534 	int flag;
    535 {
    536 	int error = 0;
    537 
    538 	switch (cmd) {
    539 	default:
    540 		error = ENODEV;
    541 	}
    542 
    543 	return error;
    544 }
    545