Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.7.4.17
      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.17 1993/10/29 21:35:20 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 = 100;
    147 	do {
    148 		temp = inb(port) & mask;
    149 	} while (temp != data && --timeout);
    150 	lprintf("lpt: port=0x%x out=0x%x in=0x%x\n", port, data, temp);
    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 
    176 static int
    177 lptprobe(parent, cf, aux)
    178 	struct device *parent;
    179 	struct cfdata *cf;
    180 	void *aux;
    181 {
    182 	struct	isa_attach_args *ia = aux;
    183 	u_short	iobase = ia->ia_iobase;
    184 	u_short	port = iobase + lpt_data;
    185 	u_char	mask = 0xff;
    186 	u_char	data;
    187 	int	i;
    188 
    189 	if (iobase == IOBASEUNK)
    190 		return 0;
    191 
    192 #ifdef DEBUG
    193 #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
    194 		    return 0;} while (0)
    195 #else
    196 #define	ABORT	return 0
    197 #endif
    198 
    199 	for (;;) {
    200 		data = 0x55;				/* Alternating zeros */
    201 		if (!lpt_port_test(port, data, mask))
    202 			ABORT;
    203 
    204 		data = 0xaa;				/* Alternating ones */
    205 		if (!lpt_port_test(port, data, mask))
    206 			ABORT;
    207 
    208 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    209 			data = ~(1 << i);
    210 			if (!lpt_port_test(port, data, mask))
    211 				ABORT;
    212 		}
    213 
    214 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    215 			data = (1 << i);
    216 			if (!lpt_port_test(port, data, mask))
    217 				ABORT;
    218 		}
    219 
    220 		if (port == iobase + lpt_data) {
    221 			port = iobase + lpt_control;
    222 #if 1 /* XXX */
    223 			mask = 0x04;
    224 #else
    225 			mask = 0x1e;
    226 #endif
    227 		} else
    228 			break;
    229 	}
    230 	outb(iobase + lpt_data, 0);
    231 	outb(iobase + lpt_control, 0);
    232 
    233 	if (ia->ia_irq == IRQUNK)
    234 #if 0 /* XXX */
    235 		ia->ia_irq = isa_discoverintr(lptforceintr, aux);
    236 #else
    237 		ia->ia_irq = IRQNONE;
    238 #endif
    239 	/* okay if we don't have an irq; will force polling */
    240 
    241 	ia->ia_iosize = LPT_NPORTS;
    242 	ia->ia_drq = DRQUNK;
    243 	ia->ia_msize = 0;
    244 	return 1;
    245 }
    246 
    247 #if 0 /* XXX */
    248 static void
    249 lptforceintr(aux)
    250 	void *aux;
    251 {
    252 	struct isa_attach_args *ia = aux;
    253 	u_short iobase = ia->ia_iobase;
    254 
    255 	outb(iobase + lpt_control, LPC_IENABLE);
    256 	delay(100);
    257 	outb(iobase + lpt_control, 0);
    258 }
    259 #endif
    260 
    261 static void
    262 lptattach(parent, self, aux)
    263 	struct device *parent, *self;
    264 	void *aux;
    265 {
    266 	struct	isa_attach_args *ia = aux;
    267 	struct	lpt_softc *sc = (struct lpt_softc *)self;
    268 	u_short	iobase = ia->ia_iobase;
    269 	u_short	irq = ia->ia_irq;
    270 
    271 	sc->sc_iobase = iobase;
    272 	sc->sc_irq = irq;
    273 	sc->sc_state = 0;
    274 	outb(iobase + lpt_control, LPC_NINIT);
    275 
    276 	printf(": %sparallel port\n", irq == IRQNONE ? "polled " : "");
    277 	isa_establish(&sc->sc_id, &sc->sc_dev);
    278 
    279 	if (irq != IRQNONE) {
    280 		sc->sc_ih.ih_fun = lptintr;
    281 		sc->sc_ih.ih_arg = sc;
    282 		intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    283 	}
    284 }
    285 
    286 /*
    287  * lptopen -- reset the printer, then wait until it's selected and not busy.
    288  */
    289 int
    290 lptopen(dev, flag)
    291 	dev_t dev;
    292 	int flag;
    293 {
    294 	int	unit = LPTUNIT(minor(dev));
    295 	u_char	flags = LPTFLAGS(minor(dev));
    296 	struct	lpt_softc *sc;
    297 	u_short	iobase;
    298 	u_char	control;
    299 	int	error;
    300 	int	spin;
    301 
    302 	if (unit >= lptcd.cd_ndevs)
    303 		return ENXIO;
    304 	sc = lptcd.cd_devs[unit];
    305 	if (!sc)
    306 		return ENXIO;
    307 
    308 	if (sc->sc_irq == IRQNONE && (flags & LPT_NOINTR) == 0)
    309 		return ENXIO;
    310 
    311 	if (sc->sc_state)
    312 		return EBUSY;
    313 
    314 #ifdef DIAGNOSTIC
    315 	if (sc->sc_state)
    316 		printf("%s: state=0x%x not zero\n", sc->sc_dev.dv_xname,
    317 		       sc->sc_state);
    318 #endif
    319 
    320 	sc->sc_state = LPT_INIT;
    321 
    322 	sc->sc_flags = flags;
    323 	lprintf("%s: open flags=0x%x\n", sc->sc_dev.dv_xname, flags);
    324 	iobase = sc->sc_iobase;
    325 
    326 	if ((flags & LPT_NOPRIME) == 0) {
    327 		/* assert INIT for 100 usec to start up printer */
    328 		outb(iobase + lpt_control, LPC_SELECT);
    329 		delay(100);
    330 	}
    331 
    332 	control = LPC_SELECT | LPC_NINIT;
    333 	outb(iobase + lpt_control, control);
    334 
    335 	/* wait till ready (printer running diagnostics) */
    336 	for (spin = 0; NOT_READY(); spin += STEP) {
    337 		if (spin >= TIMEOUT) {
    338 			sc->sc_state = 0;
    339 			return EBUSY;
    340 		}
    341 
    342 		/* wait 1/4 second, give up if we get a signal */
    343 		if (error = tsleep((caddr_t)sc,
    344 		    LPTPRI | PCATCH, "lptopen", STEP) != EWOULDBLOCK) {
    345 			sc->sc_state = 0;
    346 			return error;
    347 		}
    348 	}
    349 
    350 	if ((flags & LPT_NOINTR) == 0)
    351 		control |= LPC_IENABLE;
    352 	if (flags & LPT_AUTOLF)
    353 		control |= LPC_AUTOLF;
    354 	sc->sc_control = control;
    355 	outb(iobase + lpt_control, control);
    356 
    357 	sc->sc_state = LPT_OPEN;
    358 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    359 	sc->sc_count = 0;
    360 	lptout(sc);
    361 
    362 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
    363 	return 0;
    364 }
    365 
    366 static int
    367 notready(status, sc)
    368 	u_char status;
    369 	struct lpt_softc *sc;
    370 {
    371 	status ^= LPS_INVERT;
    372 
    373 	if (status & LPS_NOPAPER)
    374 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
    375 	else if (status & LPS_SELECT)
    376 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
    377 	else if (status & LPS_NERR)
    378 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
    379 
    380 	return status & LPS_MASK;
    381 }
    382 
    383 static void
    384 lptout(sc)
    385 	struct lpt_softc *sc;
    386 {
    387 	int	s;
    388 
    389 	if ((sc->sc_state & LPT_OPEN) == 0)
    390 		return;
    391 	if (sc->sc_flags & LPT_NOINTR)
    392 		return;
    393 
    394 	/*
    395 	 * Avoid possible hangs due to missed interrupts
    396 	 */
    397 	if (sc->sc_count) {
    398 		s = spltty();
    399 		(void) lptintr(sc);
    400 		splx(s);
    401 	} else {
    402 		sc->sc_state &= ~LPT_OBUSY;
    403 		wakeup((caddr_t)sc);
    404 	}
    405 
    406 	timeout((timeout_t)lptout, (caddr_t)sc, STEP);
    407 }
    408 
    409 /*
    410  * lptclose -- close the device, free the local line buffer.
    411  */
    412 int
    413 lptclose(dev, flag)
    414 	dev_t dev;
    415 	int flag;
    416 {
    417 	int	unit = LPTUNIT(minor(dev));
    418 	struct	lpt_softc *sc = lptcd.cd_devs[unit];
    419 	u_short	iobase = sc->sc_iobase;
    420 
    421 	(void) pushbytes(sc);
    422 
    423 	outb(iobase + lpt_control, LPC_NINIT);
    424 	brelse(sc->sc_inbuf);
    425 	sc->sc_state = 0;
    426 
    427 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
    428 	return 0;
    429 }
    430 
    431 static int
    432 pushbytes(sc)
    433 	struct lpt_softc *sc;
    434 {
    435 	u_short	iobase = sc->sc_iobase;
    436 	int	error;
    437 
    438 	if (sc->sc_flags & LPT_NOINTR) {
    439 		int	spin, tic;
    440 		u_char	control = sc->sc_control;
    441 		u_char	ch;
    442 
    443 		while (sc->sc_count > 0) {
    444 			spin = tic = 0;
    445 			while (NOT_READY())
    446 				if (++spin >= sc->sc_smax) {
    447 					/* exponential backoff */
    448 					tic = tic + tic + 1;
    449 					if (error = tsleep((caddr_t)sc,
    450 					    LPTPRI | PCATCH, "lptwrite1", tic))
    451 						return error;
    452 				}
    453 
    454 			outb(iobase + lpt_data, *sc->sc_cp++);
    455 			outb(iobase + lpt_control, control | LPC_STROBE);
    456 			sc->sc_count--;
    457 			outb(iobase + lpt_control, control);
    458 
    459 			/* adapt busy-wait algorithm */
    460 			if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
    461 				sc->sc_smax++;
    462 			if (spin*2 < sc->sc_smax)
    463 				sc->sc_smax--;
    464 		}
    465 	} else {
    466 		int	s;
    467 
    468 		while (sc->sc_count > 0) {
    469 			/* if the printer is ready for a char, give it one */
    470 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    471 				lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
    472 					sc->sc_count);
    473 				s = spltty();
    474 				(void) lptintr(sc);
    475 				splx(s);
    476 			}
    477 			if (error = tsleep((caddr_t)sc,
    478 			    LPTPRI | PCATCH, "lptwrite2", 0))
    479 				return error;
    480 		}
    481 	}
    482 	return 0;
    483 }
    484 
    485 /*
    486  * copy a line from user space to a local buffer, then call
    487  * putc to get the chars moved to the output queue.
    488  */
    489 int
    490 lptwrite(dev, uio)
    491 	dev_t dev;
    492 	struct uio *uio;
    493 {
    494 	int	unit = LPTUNIT(minor(dev));
    495 	struct	lpt_softc *sc = lptcd.cd_devs[unit];
    496 	size_t	n;
    497 	int	error = 0;
    498 
    499 	while (n = MIN(LPT_BSIZE, uio->uio_resid)) {
    500 		uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
    501 		sc->sc_count = n;
    502 		error = pushbytes(sc);
    503 		if (error) {
    504 			/* return accurate residual if interrupted or
    505 			   timed out */
    506 			uio->uio_resid += sc->sc_count;
    507 			sc->sc_count = 0;
    508 			return error;
    509 		}
    510 	}
    511 	return 0;
    512 }
    513 
    514 /*
    515  * lptintr -- handle printer interrupts which occur when the printer is
    516  * ready to accept another char.
    517  */
    518 static int
    519 lptintr(arg)
    520 	void *arg;
    521 {
    522 	struct	lpt_softc *sc = (struct lpt_softc *)arg;
    523 	u_short	iobase = sc->sc_iobase;
    524 	u_char	control = sc->sc_control;
    525 	u_char	status;
    526 
    527 	if ((sc->sc_state & LPT_OPEN) == 0)
    528 		return 0;
    529 
    530 	/* is printer online and ready for output */
    531 	if (NOT_READY())
    532 		return 0;
    533 
    534 	if (sc->sc_count) {
    535 		/* send char */
    536 		sc->sc_state |= LPT_OBUSY;
    537 		while (sc->sc_count && !NOT_READY()) {
    538 			outb(iobase + lpt_data, *sc->sc_cp++);
    539 			outb(iobase + lpt_control, control | LPC_STROBE);
    540 			sc->sc_count--;
    541 			outb(iobase + lpt_control, control);
    542 		}
    543 	} else {
    544 		/* none, wake up the top half to get more */
    545 		sc->sc_state &= ~LPT_OBUSY;
    546 		wakeup((caddr_t)sc);
    547 	}
    548 
    549 	return 1;
    550 }
    551 
    552 int
    553 lptioctl(dev, cmd, data, flag)
    554 	dev_t dev;
    555 	int cmd;
    556 	caddr_t data;
    557 	int flag;
    558 {
    559 	int	error = 0;
    560 
    561 	switch (cmd) {
    562 	    default:
    563 		error = ENODEV;
    564 	}
    565 
    566 	return error;
    567 }
    568