Home | History | Annotate | Line # | Download | only in isa
lpt_isa.c revision 1.3
      1 /*
      2  * Copyright (c) 1990 William F. Jolitz, TeleMuse
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This software is a component of "386BSD" developed by
     16  *	William F. Jolitz, TeleMuse.
     17  * 4. Neither the name of the developer nor the name "386BSD"
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
     22  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
     23  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
     24  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
     25  * NOT MAKE USE OF THIS WORK.
     26  *
     27  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
     28  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
     29  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
     30  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
     31  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
     32  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
     33  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
     34  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
     35  *
     36  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
     37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
     40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     46  * SUCH DAMAGE.
     47  *
     48  *	$Id: lpt_isa.c,v 1.3 1993/05/18 18:19:04 cgd Exp $
     49  */
     50 
     51 /*
     52  * Device Driver for AT parallel printer port
     53  * Written by William Jolitz 12/18/90
     54  *
     55  * Hacked heavily by Rod Grimes and Eric Haug...
     56  */
     57 
     58 #include "lpt.h"
     59 #if NLPT > 0
     60 
     61 #include "param.h"
     62 #include "systm.h"
     63 #include "proc.h"
     64 #include "user.h"
     65 #include "buf.h"
     66 #include "kernel.h"
     67 #include "ioctl.h"
     68 #include "select.h"
     69 #include "tty.h"
     70 #include "uio.h"
     71 
     72 #include "i386/isa/isa.h"
     73 #include "i386/isa/isa_device.h"
     74 #include "i386/isa/lptreg.h"
     75 
     76 #define	LPINITRDY	4	/* wait up to 4 seconds for a ready */
     77 #define	LPTOUTTIME	4	/* wait up to 4 seconds for a ready */
     78 #define	LPPRI		(PZERO+8)
     79 #define	BUFSIZE		1024
     80 
     81 #ifndef DEBUG
     82 #define lprintf
     83 #else
     84 #define lprintf		if (lpflag) printf
     85 #endif
     86 
     87 int lptout();
     88 #ifdef DEBUG
     89 int lpflag = 1;
     90 #endif
     91 
     92 int 	lptprobe(), lptattach(), lptintr();
     93 
     94 struct	isa_driver lptdriver = {
     95 	lptprobe, lptattach, "lpt"
     96 };
     97 
     98 #define	LPTUNIT(s)	(((s)>>6)&0x3)
     99 #define	LPTFLAGS(s)	((s)&0x3f)
    100 
    101 struct lpt_softc {
    102 	short	sc_port;
    103 	short	sc_state;
    104 	/* default case: negative prime, negative ack, handshake strobe,
    105 	   prime once */
    106 	u_char	sc_control;
    107 	char	sc_flags;
    108 #define LP_POS_INIT	0x01	/* if we are a postive init signal */
    109 #define LP_POS_ACK	0x02	/* if we are a positive going ack */
    110 #define LP_NO_PRIME	0x04	/* don't prime the printer at all */
    111 #define LP_PRIMEOPEN	0x08	/* prime on every open */
    112 #define LP_AUTOLF	0x10	/* tell printer to do an automatic lf */
    113 #define LP_BYPASS	0x20	/* bypass  printer ready checks */
    114 	struct	buf *sc_inbuf;
    115 	short	sc_xfercnt ;
    116 	char	sc_primed;
    117 	char	*sc_cp ;
    118 } lpt_sc[NLPT] ;
    119 
    120 /* bits for state */
    121 #define	OPEN		(1<<0)	/* device is open */
    122 #define	ASLP		(1<<1)	/* awaiting draining of printer */
    123 #define	ERROR		(1<<2)	/* error was received from printer */
    124 #define	OBUSY		(1<<3)	/* printer is busy doing output */
    125 #define LPTOUT		(1<<4)	/* timeout while not selected	*/
    126 #define TOUT		(1<<5)	/* timeout while not selected	*/
    127 #define INIT		(1<<6)	/* waiting to initialize for open */
    128 
    129 /*
    130  * Internal routine to lptprobe to do port tests of one byte value
    131  */
    132 int
    133 lpt_port_test(short port, u_char data, u_char mask)
    134 	{
    135 	int	temp;
    136 
    137 	data = data & mask;
    138 	outb(port, data);
    139 	temp = inb(port) & mask;
    140 	lprintf("Port 0x%x\tout=%x\tin=%x\n", port, data, temp);
    141 	return (temp == data);
    142 	}
    143 
    144 /*
    145  * New lptprobe routine written by Rodney W. Grimes, 3/25/1993
    146  *
    147  * Logic:
    148  *	1) You should be able to write to and read back the same value
    149  *	   to the data port.  Do an alternating zeros, alternating ones,
    150  *	   walking zero, and walking one test to check for stuck bits.
    151  *
    152  *	2) You should be able to write to and read back the same value
    153  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    154  *	   per the IBM PC technical reference manauls and different boards
    155  *	   do different things with them.  Do an alternating zeros, alternating
    156  *	   ones, walking zero, and walking one test to check for stuck bits.
    157  *
    158  *	   Some printers drag the strobe line down when the are powered off
    159  * 	   so this bit has been masked out of the control port test.
    160  *
    161  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    162  *	   don't know at this point, if that becomes a problem these bits
    163  *	   should be turned off in the mask byte for the control port test.
    164  *
    165  *	3) Set the data and control ports to a value of 0
    166  */
    167 
    168 int
    169 lptprobe(struct isa_device *dvp)
    170 	{
    171 	int	status;
    172 	short	port;
    173 	u_char	data;
    174 	u_char	mask;
    175 	int	i;
    176 
    177 	status = IO_LPTSIZE;
    178 
    179 	port = dvp->id_iobase + lpt_data;
    180 	mask = 0xff;
    181 	while (mask != 0)
    182 	{
    183 		data = 0x55;				/* Alternating zeros */
    184 		if (!lpt_port_test(port, data, mask)) status = 0;
    185 
    186 		data = 0xaa;				/* Alternating ones */
    187 		if (!lpt_port_test(port, data, mask)) status = 0;
    188 
    189 		for (i = 0; i < 8; i++)			/* Walking zero */
    190 			{
    191 			data = ~(1 << i);
    192 			if (!lpt_port_test(port, data, mask)) status = 0;
    193 			}
    194 
    195 		for (i = 0; i < 8; i++)			/* Walking one */
    196 			{
    197 			data = (1 << i);
    198 			if (!lpt_port_test(port, data, mask)) status = 0;
    199 			}
    200 
    201 		if (port == dvp->id_iobase + lpt_data)
    202 			{
    203 			port = dvp->id_iobase + lpt_control;
    204 			mask = 0x1e;
    205 			}
    206 		else
    207 			mask = 0;
    208 		}
    209 	outb(dvp->id_iobase+lpt_data, 0);
    210 	outb(dvp->id_iobase+lpt_control, 0);
    211 	return (status);
    212 	}
    213 
    214 lptattach(isdp)
    215 	struct isa_device *isdp;
    216 {
    217 	struct	lpt_softc	*sc;
    218 
    219 	sc = lpt_sc + isdp->id_unit;
    220 	sc->sc_port = isdp->id_iobase;
    221 	outb(sc->sc_port+lpt_control, LPC_NINIT);
    222 	return (1);
    223 }
    224 
    225 /*
    226  * lptopen -- reset the printer, then wait until it's selected and not busy.
    227  */
    228 
    229 lptopen(dev, flag)
    230 	dev_t dev;
    231 	int flag;
    232 {
    233 	struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev));
    234 	int s;
    235 	int trys, port;
    236 
    237 	if (sc->sc_state) {
    238 lprintf("lp: still open\n") ;
    239 printf("still open %x\n", sc->sc_state);
    240 		return(EBUSY);
    241 	} else	sc->sc_state |= INIT;
    242 
    243 	s = spltty();
    244 	sc->sc_flags = LPTFLAGS(minor(dev));
    245 lprintf("lp flags 0x%x\n", sc->sc_flags);
    246 	port = sc->sc_port;
    247 
    248 	/* init printer */
    249 	if((sc->sc_flags & LP_NO_PRIME) == 0) {
    250 		if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
    251 			outb(port+lpt_control, 0);
    252 			sc->sc_primed++;
    253 			DELAY(500);
    254 		}
    255 	}
    256 	outb(port+lpt_control, LPC_SEL|LPC_NINIT);
    257 
    258 	/* wait till ready (printer running diagnostics) */
    259 	trys = 0;
    260 	do {
    261 		/* ran out of waiting for the printer */
    262 		if (trys++ >= LPINITRDY*4) {
    263 			splx(s);
    264 			sc->sc_state = 0;
    265 printf ("status %x\n", inb(port+lpt_status) );
    266 			return (EBUSY);
    267 		}
    268 
    269 		/* wait 1/4 second, give up if we get a signal */
    270 		if (tsleep (sc, LPPRI|PCATCH, "lptinit", hz/4) != EWOULDBLOCK) {
    271 			sc->sc_state = 0;
    272 			splx(s);
    273 			return (EBUSY);
    274 		}
    275 
    276 		/* is printer online and ready for output */
    277 	} while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
    278 			(LPS_SEL|LPS_NBSY|LPS_NERR));
    279 
    280 	if(sc->sc_flags&LP_AUTOLF) {
    281 		outb(port+lpt_control, LPC_SEL|LPC_NINIT|LPC_ENA|LPC_AUTOL);
    282 		sc->sc_control = LPC_SEL|LPC_NINIT|LPC_ENA|LPC_AUTOL;
    283 	} else {
    284 		outb(port+lpt_control, LPC_SEL|LPC_NINIT|LPC_ENA);
    285 		sc->sc_control = LPC_SEL|LPC_NINIT|LPC_ENA;
    286 	}
    287 
    288 	sc->sc_state = OPEN | TOUT;
    289 	sc->sc_inbuf = geteblk(BUFSIZE);
    290 	sc->sc_xfercnt = 0;
    291 	splx(s);
    292 	timeout (lptout, sc, hz/2);
    293 lprintf("opened.\n");
    294 	return(0);
    295 }
    296 
    297 lptout (sc)
    298 	struct lpt_softc *sc;
    299 {	int pl;
    300 
    301 lprintf ("T %x ", inb(sc->sc_port+lpt_status));
    302 	if (sc->sc_state&OPEN)
    303 		timeout (lptout, sc, hz/2);
    304 	else	sc->sc_state &= ~TOUT;
    305 
    306 	if (sc->sc_state & ERROR)
    307 		sc->sc_state &= ~ERROR;
    308 
    309 	/*
    310 	 * Avoid possible hangs do to missed interrupts
    311 	 */
    312 	if (sc->sc_xfercnt) {
    313 		pl = spltty();
    314 		lptintr(sc - lpt_sc);
    315 		splx(pl);
    316 	} else {
    317 		sc->sc_state &= ~OBUSY;
    318 		wakeup((caddr_t)sc);
    319 	}
    320 }
    321 
    322 /*
    323  * lptclose -- close the device, free the local line buffer.
    324  */
    325 
    326 lptclose(dev, flag)
    327 	int flag;
    328 {
    329 	struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev));
    330 	int port = sc->sc_port;
    331 
    332 	sc->sc_state &= ~OPEN;
    333 	while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
    334 			(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
    335 		/* wait 1/4 second, give up if we get a signal */
    336 		if (tsleep (sc, LPPRI|PCATCH, "lpclose", hz) != EWOULDBLOCK)
    337 			break;
    338 
    339 	sc->sc_state = 0;
    340 	sc->sc_xfercnt = 0;
    341 	outb(sc->sc_port+lpt_control, LPC_NINIT);
    342 	brelse(sc->sc_inbuf);
    343 lprintf("closed.\n");
    344 	return(0);
    345 }
    346 
    347 /*
    348  * lptwrite --copy a line from user space to a local buffer, then call
    349  * putc to get the chars moved to the output queue.
    350  */
    351 
    352 lptwrite(dev, uio)
    353 	dev_t dev;
    354 	struct uio *uio;
    355 {
    356 	register unsigned n;
    357 	int pl, err;
    358 	struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev));
    359 
    360 	while (n = MIN(BUFSIZE, uio->uio_resid)) {
    361 		sc->sc_cp = sc->sc_inbuf->b_un.b_addr ;
    362 		uiomove(sc->sc_cp, n, uio);
    363 		sc->sc_xfercnt = n ;
    364 		while (sc->sc_xfercnt > 0) {
    365 			/* if the printer is ready for a char, give it one */
    366 			if ((sc->sc_state & OBUSY) == 0){
    367 lprintf("\nC %d. ", sc->sc_xfercnt);
    368 				pl = spltty();
    369 				lptintr(sc - lpt_sc);
    370 				(void) splx(pl);
    371 			}
    372 lprintf("W ");
    373 			if (err = tsleep (sc, LPPRI|PCATCH, "lpwrite", 0))
    374 				return(err);
    375 		}
    376 	}
    377 	return(0);
    378 }
    379 
    380 /*
    381  * lptintr -- handle printer interrupts which occur when the printer is
    382  * ready to accept another char.
    383  */
    384 
    385 lptintr(unit)
    386 {
    387 	struct lpt_softc *sc = lpt_sc + unit;
    388 	int port = sc->sc_port,sts;
    389 
    390 	/* is printer online and ready for output */
    391 	if (((sts=inb(port+lpt_status)) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR/*|LPS_NACK*/)) ==
    392 			(LPS_SEL|LPS_NBSY|LPS_NERR)) {
    393 			/* is this a false interrupt ? */
    394 			if ((sc->sc_state & OBUSY)
    395 				&& (sts & LPS_NACK) == 0) return;
    396 		sc->sc_state |= OBUSY; sc->sc_state &= ~ERROR;
    397 
    398 		if (sc->sc_xfercnt) {
    399 			/* send char */
    400 /*lprintf("%x ", *sc->sc_cp); */
    401 			outb(port+lpt_data, *sc->sc_cp++) ; sc->sc_xfercnt-- ;
    402 			outb(port+lpt_control, sc->sc_control|LPC_STB);
    403 			/* DELAY(X) */
    404 			outb(port+lpt_control, sc->sc_control);
    405 		}
    406 
    407 		/* any more bytes for the printer? */
    408 		if (sc->sc_xfercnt > 0) return;
    409 
    410 		/* none, wake up the top half to get more */
    411 		sc->sc_state &= ~OBUSY;
    412 		wakeup((caddr_t)sc);
    413 lprintf("w ");
    414 return;
    415 	} else	sc->sc_state |= ERROR;
    416 lprintf("sts %x ", sts);
    417 }
    418 
    419 int
    420 lptioctl(dev_t dev, int cmd, caddr_t data, int flag)
    421 {
    422 	int	error;
    423 
    424 	error = 0;
    425 	switch (cmd) {
    426 #ifdef THISISASAMPLE
    427 	case XXX:
    428 		dothis; andthis; andthat;
    429 		error=x;
    430 		break;
    431 #endif /* THISISASAMPLE */
    432 	default:
    433 		error = ENODEV;
    434 	}
    435 
    436 	return(error);
    437 }
    438 
    439 #endif	/* NLPT */
    440