Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.7.4.9
      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.9 1993/10/12 23:30:52 mycroft Exp $
     50  */
     51 
     52 /*
     53  * Device Driver for AT parallel printer port
     54  */
     55 
     56 #include "param.h"
     57 #include "systm.h"
     58 #include "proc.h"
     59 #include "user.h"
     60 #include "buf.h"
     61 #include "kernel.h"
     62 #include "ioctl.h"
     63 #include "uio.h"
     64 #include "sys/device.h"
     65 
     66 #include "machine/cpu.h"
     67 #include "machine/pio.h"
     68 
     69 #include "i386/isa/isavar.h"
     70 #include "i386/isa/icu.h"
     71 #include "i386/isa/lptreg.h"
     72 
     73 #define	TIMEOUT		hz*16	/* wait up to 4 seconds for a ready */
     74 #define	STEP		hz/4
     75 
     76 #define	MAX_SPIN	255
     77 
     78 #define	LPTPRI		(PZERO+8)
     79 #define	LPT_BSIZE	1024
     80 
     81 #ifndef DEBUG
     82 #define lprintf
     83 #else
     84 #define lprintf		if (lptdebug) printf
     85 int lptdebug = 1;
     86 #endif
     87 
     88 struct lpt_softc {
     89 	struct	device sc_dev;
     90 	struct	isadev sc_id;
     91 	struct	intrhand sc_ih;
     92 
     93 	size_t	sc_count;
     94 	struct	buf *sc_inbuf;
     95 	u_char	*sc_cp ;
     96 	u_short	sc_iobase;
     97 	u_short	sc_irq;
     98 	u_char	sc_state;
     99 /* bits for 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 };
    110 
    111 static int lptprobe __P((struct device *, struct cfdata *, void *));
    112 static void lptforceintr __P((void *));
    113 static void lptattach __P((struct device *, struct device *, void *));
    114 static int lptintr __P((void *));
    115 
    116 struct	cfdriver lptcd =
    117 { NULL, "lpt", lptprobe, lptattach, sizeof(struct lpt_softc) };
    118 
    119 #define	LPTUNIT(s)	((s)&0x1f)
    120 #define	LPTFLAGS(s)	((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()	((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK)
    125 
    126 static void lptout __P((struct lpt_softc *));
    127 static int pushbytes __P((struct lpt_softc *));
    128 
    129 /*
    130  * Internal routine to lptprobe to do port tests of one byte value
    131  */
    132 int
    133 lpt_port_test(port, data, mask)
    134 	u_short port;
    135 	u_char data, mask;
    136 {
    137 	int	timeout;
    138 	u_char	temp;
    139 
    140 	data &= mask;
    141 	outb(port, data);
    142 	timeout = 100;
    143 	do {
    144 		temp = inb(port) & mask;
    145 	} while (temp != data && --timeout);
    146 	lprintf("lpt: port=0x%x out=0x%x in=0x%x\n", port, data, temp);
    147 	return (temp == data);
    148 }
    149 
    150 /*
    151  * Logic:
    152  *	1) You should be able to write to and read back the same value
    153  *	   to the data port.  Do an alternating zeros, alternating ones,
    154  *	   walking zero, and walking one test to check for stuck bits.
    155  *
    156  *	2) You should be able to write to and read back the same value
    157  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    158  *	   per the IBM PC technical reference manauls and different boards
    159  *	   do different things with them.  Do an alternating zeros, alternating
    160  *	   ones, walking zero, and walking one test to check for stuck bits.
    161  *
    162  *	   Some printers drag the strobe line down when the are powered off
    163  * 	   so this bit has been masked out of the control port test.
    164  *
    165  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    166  *	   don't know at this point, if that becomes a problem these bits
    167  *	   should be turned off in the mask byte for the control port test.
    168  *
    169  *	3) Set the data and control ports to a value of 0
    170  */
    171 
    172 static int
    173 lptprobe(parent, cf, aux)
    174 	struct device *parent;
    175 	struct cfdata *cf;
    176 	void *aux;
    177 {
    178 	struct	isa_attach_args *ia = aux;
    179 	u_short	iobase = ia->ia_iobase;
    180 	u_short	port = iobase + lpt_data;
    181 	u_char	mask = 0xff;
    182 	u_char	data;
    183 	int	i;
    184 
    185 	if (iobase == IOBASEUNK)
    186 		return 0;
    187 
    188 	for (;;) {
    189 		data = 0x55;				/* Alternating zeros */
    190 		if (!lpt_port_test(port, data, mask))
    191 			return 0;
    192 
    193 		data = 0xaa;				/* Alternating ones */
    194 		if (!lpt_port_test(port, data, mask))
    195 			return 0;
    196 
    197 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    198 			data = ~(1 << i);
    199 			if (!lpt_port_test(port, data, mask))
    200 				return 0;
    201 		}
    202 
    203 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    204 			data = (1 << i);
    205 			if (!lpt_port_test(port, data, mask))
    206 				return 0;
    207 		}
    208 
    209 		if (port == iobase + lpt_data) {
    210 			port = iobase + lpt_control;
    211 			mask = 0x1e;
    212 		} else
    213 			break;
    214 	}
    215 	outb(iobase + lpt_data, 0);
    216 	outb(iobase + lpt_control, 0);
    217 
    218 	if (ia->ia_irq == IRQUNK)
    219 		ia->ia_irq = isa_discoverintr(lptforceintr, aux);
    220 	/* okay if we don't have an irq; will force polling */
    221 
    222 	ia->ia_iosize = LPT_NPORTS;
    223 	ia->ia_drq = DRQUNK;
    224 	ia->ia_msize = 0;
    225 	return 1;
    226 }
    227 
    228 static void
    229 lptattach(parent, self, aux)
    230 	struct device *parent, *self;
    231 	void *aux;
    232 {
    233 	struct	isa_attach_args *ia = aux;
    234 	struct	lpt_softc *sc = (struct lpt_softc *)self;
    235 	u_short	iobase = ia->ia_iobase;
    236 	u_short	irq = ia->ia_irq;
    237 
    238 	sc->sc_iobase = iobase;
    239 	sc->sc_irq = irq;
    240 	sc->sc_state = 0;
    241 	outb(iobase + lpt_control, LPC_NINIT);
    242 
    243 	printf(": %sparallel port\n", irq == IRQNONE ? "polled " : "");
    244 	isa_establish(&sc->sc_id, &sc->sc_dev);
    245 
    246 	if (irq != IRQNONE) {
    247 		sc->sc_ih.ih_fun = lptintr;
    248 		sc->sc_ih.ih_arg = sc;
    249 		intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    250 	}
    251 }
    252 
    253 /*
    254  * lptopen -- reset the printer, then wait until it's selected and not busy.
    255  */
    256 int
    257 lptopen(dev, flag)
    258 	dev_t dev;
    259 	int flag;
    260 {
    261 	int	unit = LPTUNIT(minor(dev));
    262 	u_char	flags = LPTFLAGS(minor(dev));
    263 	struct	lpt_softc *sc;
    264 	u_short	iobase;
    265 	u_char	control;
    266 	int	error;
    267 	int	spin;
    268 
    269 	if (unit >= lptcd.cd_ndevs)
    270 		return ENXIO;
    271 	sc = lptcd.cd_devs[unit];
    272 	if (!sc)
    273 		return ENXIO;
    274 
    275 	if (sc->sc_irq == IRQNONE && (flags & LPT_NOINTR) == 0)
    276 		return ENXIO;
    277 
    278 	if (sc->sc_state)
    279 		return EBUSY;
    280 
    281 #ifdef DIAGNOSTIC
    282 	if (sc->sc_state)
    283 		printf("lpt%d: state=0x%x not zero\n", unit, sc->sc_state);
    284 #endif
    285 
    286 	sc->sc_state = LPT_INIT;
    287 
    288 	sc->sc_flags = flags;
    289 	lprintf("lpt%d: open flags=0x%x\n", unit, flags);
    290 	iobase = sc->sc_iobase;
    291 
    292 	if ((flags & LPT_NOPRIME) == 0) {
    293 		/* assert INIT for 100 usec to start up printer */
    294 		outb(iobase + lpt_control, LPC_SELECT);
    295 		delay(100);
    296 	}
    297 
    298 	control = LPC_SELECT | LPC_NINIT;
    299 	outb(iobase + lpt_control, control);
    300 
    301 	/* wait till ready (printer running diagnostics) */
    302 	for (spin = 0; NOT_READY(); spin += STEP) {
    303 		if (spin >= TIMEOUT) {
    304 			sc->sc_state = 0;
    305 			return EBUSY;
    306 		}
    307 
    308 		/* wait 1/4 second, give up if we get a signal */
    309 		if (error = tsleep((caddr_t)sc,
    310 		    LPTPRI | PCATCH, "lptopen", STEP) != EWOULDBLOCK) {
    311 			sc->sc_state = 0;
    312 			return error;
    313 		}
    314 	}
    315 
    316 	if ((flags & LPT_NOINTR) == 0)
    317 		control |= LPC_IENABLE;
    318 	if (flags & LPT_AUTOLF)
    319 		control |= LPC_AUTOLF;
    320 	sc->sc_control = control;
    321 	outb(iobase + lpt_control, control);
    322 
    323 	sc->sc_state = LPT_OPEN;
    324 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    325 	sc->sc_count = 0;
    326 	lptout(sc);
    327 
    328 	lprintf("lpt%d: opened\n", unit);
    329 	return 0;
    330 }
    331 
    332 static void
    333 lptout(sc)
    334 	struct lpt_softc *sc;
    335 {
    336 	int	s;
    337 
    338 	if ((sc->sc_state & LPT_OPEN) == 0)
    339 		return;
    340 	if (sc->sc_flags & LPT_NOINTR)
    341 		return;
    342 
    343 	/*
    344 	 * Avoid possible hangs due to missed interrupts
    345 	 */
    346 	if (sc->sc_count) {
    347 		s = spltty();
    348 		lptintr(sc);
    349 		splx(s);
    350 	} else {
    351 		sc->sc_state &= ~LPT_OBUSY;
    352 		wakeup((caddr_t)sc);
    353 	}
    354 
    355 	timeout((timeout_t)lptout, (caddr_t)sc, STEP);
    356 }
    357 
    358 /*
    359  * lptclose -- close the device, free the local line buffer.
    360  */
    361 int
    362 lptclose(dev, flag)
    363 	dev_t dev;
    364 	int flag;
    365 {
    366 	int	unit = LPTUNIT(minor(dev));
    367 	struct	lpt_softc *sc = lptcd.cd_devs[unit];
    368 	u_short	iobase = sc->sc_iobase;
    369 	int	error;
    370 
    371 	if (error = pushbytes(sc))
    372 		return error;
    373 
    374 	outb(iobase + lpt_control, LPC_NINIT);
    375 	brelse(sc->sc_inbuf);
    376 	sc->sc_state = 0;
    377 
    378 	lprintf("lpt%d: closed\n", unit);
    379 	return 0;
    380 }
    381 
    382 static int
    383 pushbytes(sc)
    384 	struct lpt_softc *sc;
    385 {
    386 	u_short	iobase = sc->sc_iobase;
    387 	int	error;
    388 
    389 	if (sc->sc_flags & LPT_NOINTR) {
    390 		int	spin, tic;
    391 		u_char	control = sc->sc_control;
    392 		u_char	ch;
    393 
    394 		while (sc->sc_count > 0) {
    395 			spin = tic = 0;
    396 			while (NOT_READY())
    397 				if (++spin >= sc->sc_smax) {
    398 					/* exponential backoff */
    399 					tic = tic + tic + 1;
    400 					if (error = tsleep((caddr_t)sc,
    401 					    LPTPRI | PCATCH, "lptwrite1", tic))
    402 						return error;
    403 				}
    404 
    405 			outb(iobase + lpt_data, *sc->sc_cp++);
    406 			outb(iobase + lpt_control, control | LPC_STROBE);
    407 			sc->sc_count--;
    408 			outb(iobase + lpt_control, control);
    409 
    410 			/* adapt busy-wait algorithm */
    411 			if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
    412 				sc->sc_smax++;
    413 			if (spin*2 < sc->sc_smax)
    414 				sc->sc_smax--;
    415 		}
    416 	} else {
    417 		int	s;
    418 
    419 		while (sc->sc_count > 0) {
    420 			/* if the printer is ready for a char, give it one */
    421 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    422 				lprintf("lpt%d: write %d\n", sc->sc_count);
    423 				s = spltty();
    424 				lptintr(sc);
    425 				splx(s);
    426 			}
    427 			if (error = tsleep((caddr_t)sc,
    428 			    LPTPRI | PCATCH, "lptwrite2", 0))
    429 				return error;
    430 		}
    431 	}
    432 }
    433 
    434 /*
    435  * copy a line from user space to a local buffer, then call
    436  * putc to get the chars moved to the output queue.
    437  */
    438 int
    439 lptwrite(dev, uio)
    440 	dev_t dev;
    441 	struct uio *uio;
    442 {
    443 	int	unit = LPTUNIT(minor(dev));
    444 	struct	lpt_softc *sc = lptcd.cd_devs[unit];
    445 	size_t	n;
    446 	int	error = 0;
    447 
    448 	while (n = MIN(LPT_BSIZE, uio->uio_resid)) {
    449 		uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
    450 		sc->sc_count = n;
    451 		error = pushbytes(sc);
    452 		if (error) {
    453 			/* return accurate residual if interrupted or
    454 			   timed out */
    455 			uio->uio_resid += sc->sc_count;
    456 			sc->sc_count = 0;
    457 			return error;
    458 		}
    459 	}
    460 	return 0;
    461 }
    462 
    463 /*
    464  * lptintr -- handle printer interrupts which occur when the printer is
    465  * ready to accept another char.
    466  */
    467 static int
    468 lptintr(arg)
    469 	void *arg;
    470 {
    471 	struct	lpt_softc *sc = (struct lpt_softc *)arg;
    472 	u_short	iobase = sc->sc_iobase;
    473 	u_char	control = sc->sc_control;
    474 	u_char	status;
    475 
    476 	if ((sc->sc_state & LPT_OPEN) == 0)
    477 		return 0;
    478 
    479 	/* is printer online and ready for output */
    480 	if (!NOT_READY()) {
    481 		if (sc->sc_count) {
    482 			/* send char */
    483 			sc->sc_state |= LPT_OBUSY;
    484 			while (sc->sc_count &&
    485 			       (inb(iobase + lpt_status) & LPS_NBSY) == 0) {
    486 				outb(iobase + lpt_data, *sc->sc_cp++);
    487 				outb(iobase + lpt_control, control | LPC_STROBE);
    488 				sc->sc_count--;
    489 				outb(iobase + lpt_control, control);
    490 			}
    491 		} else {
    492 			/* none, wake up the top half to get more */
    493 			sc->sc_state &= ~LPT_OBUSY;
    494 			wakeup((caddr_t)sc);
    495 		}
    496 	}
    497 
    498 	return 1;
    499 }
    500 
    501 int
    502 lptioctl(dev, cmd, data, flag)
    503 	dev_t dev;
    504 	int cmd;
    505 	caddr_t data;
    506 	int flag;
    507 {
    508 	int	error = 0;
    509 
    510 	switch (cmd) {
    511 	    default:
    512 		error = ENODEV;
    513 	}
    514 
    515 	return error;
    516 }
    517