xref: /dragonfly/sys/dev/misc/lpt/lpt.c (revision 1d1731fa)
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  *	from: unknown origin, 386BSD 0.1
49  *	From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
50  *	From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
51  * $FreeBSD: src/sys/dev/ppbus/lpt.c,v 1.15.2.3 2000/07/07 00:30:40 obrien Exp $
52  * $DragonFly: src/sys/dev/misc/lpt/lpt.c,v 1.7 2003/08/07 21:16:56 dillon Exp $
53  */
54 
55 /*
56  * Device Driver for AT parallel printer port
57  * Written by William Jolitz 12/18/90
58  */
59 
60 /*
61  * Updated for ppbus by Nicolas Souchu
62  * [Mon Jul 28 1997]
63  */
64 
65 #include "opt_lpt.h"
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/module.h>
70 #include <sys/bus.h>
71 #include <sys/conf.h>
72 #include <sys/kernel.h>
73 #include <sys/uio.h>
74 #include <sys/syslog.h>
75 #include <sys/malloc.h>
76 
77 #include <machine/clock.h>
78 #include <machine/bus.h>
79 #include <machine/resource.h>
80 #include <sys/rman.h>
81 
82 #include "lptio.h"
83 #include <bus/ppbus/ppbconf.h>
84 #include <bus/ppbus/ppb_1284.h>
85 #include "lpt.h"
86 #include "ppbus_if.h"
87 #include <bus/ppbus/ppbio.h>
88 
89 MALLOC_DEFINE(M_LPT, "lpt", "LPT buffers");
90 
91 #ifndef LPT_DEBUG
92 #define lprintf(args)
93 #else
94 #define lprintf(args)						\
95 		do {						\
96 			if (lptflag)				\
97 				printf args;			\
98 		} while (0)
99 static int volatile lptflag = 1;
100 #endif
101 
102 #define	LPINITRDY	4	/* wait up to 4 seconds for a ready */
103 #define	LPTOUTINITIAL	10	/* initial timeout to wait for ready 1/10 s */
104 #define	LPTOUTMAX	1	/* maximal timeout 1 s */
105 #define	BUFSIZE		1024
106 #define	BUFSTATSIZE	32
107 
108 #define	LPTUNIT(s)	((s)&0x03)
109 #define	LPTFLAGS(s)	((s)&0xfc)
110 
111 struct lpt_data {
112 
113 	short	sc_state;
114 	/* default case: negative prime, negative ack, handshake strobe,
115 	   prime once */
116 	u_char	sc_control;
117 	char	sc_flags;
118 #define LP_POS_INIT	0x04	/* if we are a postive init signal */
119 #define LP_POS_ACK	0x08	/* if we are a positive going ack */
120 #define LP_NO_PRIME	0x10	/* don't prime the printer at all */
121 #define LP_PRIMEOPEN	0x20	/* prime on every open */
122 #define LP_AUTOLF	0x40	/* tell printer to do an automatic lf */
123 #define LP_BYPASS	0x80	/* bypass  printer ready checks */
124 	void	*sc_inbuf;
125 	void	*sc_statbuf;
126 	short	sc_xfercnt ;
127 	char	sc_primed;
128 	char	*sc_cp ;
129 	u_short	sc_irq ;	/* IRQ status of port */
130 #define LP_HAS_IRQ	0x01	/* we have an irq available */
131 #define LP_USE_IRQ	0x02	/* we are using our irq */
132 #define LP_ENABLE_IRQ	0x04	/* enable IRQ on open */
133 #define LP_ENABLE_EXT	0x10	/* we shall use advanced mode when possible */
134 	u_char	sc_backoff ;	/* time to call lptout() again */
135 
136 	struct resource *intr_resource;	/* interrupt resource */
137 	void *intr_cookie;		/* interrupt registration cookie */
138 
139 };
140 
141 #define LPT_NAME	"lpt"		/* our official name */
142 
143 static timeout_t lptout;
144 static int	lpt_port_test(device_t dev, u_char data, u_char mask);
145 static int	lpt_detect(device_t dev);
146 
147 #define DEVTOSOFTC(dev) \
148 	((struct lpt_data *)device_get_softc(dev))
149 #define UNITOSOFTC(unit) \
150 	((struct lpt_data *)devclass_get_softc(lpt_devclass, (unit)))
151 #define UNITODEVICE(unit) \
152 	(devclass_get_device(lpt_devclass, (unit)))
153 
154 static void lptintr(device_t dev);
155 static void lpt_intr(void *arg);	/* without spls */
156 
157 static devclass_t lpt_devclass;
158 
159 
160 /* bits for state */
161 #define	OPEN		(1<<0)	/* device is open */
162 #define	ASLP		(1<<1)	/* awaiting draining of printer */
163 #define	EERROR		(1<<2)	/* error was received from printer */
164 #define	OBUSY		(1<<3)	/* printer is busy doing output */
165 #define LPTOUT		(1<<4)	/* timeout while not selected */
166 #define TOUT		(1<<5)	/* timeout while not selected */
167 #define LPTINIT		(1<<6)	/* waiting to initialize for open */
168 #define INTERRUPTED	(1<<7)	/* write call was interrupted */
169 
170 #define HAVEBUS		(1<<8)	/* the driver owns the bus */
171 
172 
173 /* status masks to interrogate printer status */
174 #define RDY_MASK	(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)	/* ready ? */
175 #define LP_READY	(LPS_SEL|LPS_NBSY|LPS_NERR)
176 
177 /* Printer Ready condition  - from lpa.c */
178 /* Only used in polling code */
179 #define	LPS_INVERT	(LPS_NBSY | LPS_NACK |           LPS_SEL | LPS_NERR)
180 #define	LPS_MASK	(LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
181 #define	NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
182 
183 #define	MAX_SLEEP	(hz*5)	/* Timeout while waiting for device ready */
184 #define	MAX_SPIN	20	/* Max delay for device ready in usecs */
185 
186 
187 static	d_open_t	lptopen;
188 static	d_close_t	lptclose;
189 static	d_write_t	lptwrite;
190 static	d_read_t	lptread;
191 static	d_ioctl_t	lptioctl;
192 
193 #define CDEV_MAJOR 16
194 static struct cdevsw lpt_cdevsw = {
195 	/* name */	LPT_NAME,
196 	/* maj */	CDEV_MAJOR,
197 	/* flags */	0,
198 	/* port */	NULL,
199 	/* autoq */	0,
200 
201 	/* open */	lptopen,
202 	/* close */	lptclose,
203 	/* read */	lptread,
204 	/* write */	lptwrite,
205 	/* ioctl */	lptioctl,
206 	/* poll */	nopoll,
207 	/* mmap */	nommap,
208 	/* strategy */	nostrategy,
209 	/* dump */	nodump,
210 	/* psize */	nopsize
211 };
212 
213 static int
214 lpt_request_ppbus(device_t dev, int how)
215 {
216 	device_t ppbus = device_get_parent(dev);
217 	struct lpt_data *sc = DEVTOSOFTC(dev);
218 	int error;
219 
220 	if (sc->sc_state & HAVEBUS)
221 		return (0);
222 
223 	/* we have the bus only if the request succeded */
224 	if ((error = ppb_request_bus(ppbus, dev, how)) == 0)
225 		sc->sc_state |= HAVEBUS;
226 
227 	return (error);
228 }
229 
230 static int
231 lpt_release_ppbus(device_t dev)
232 {
233 	device_t ppbus = device_get_parent(dev);
234 	struct lpt_data *sc = DEVTOSOFTC(dev);
235 	int error = 0;
236 
237 	if ((error = ppb_release_bus(ppbus, dev)) == 0)
238 		sc->sc_state &= ~HAVEBUS;
239 
240 	return (error);
241 }
242 
243 /*
244  * Internal routine to lptprobe to do port tests of one byte value
245  */
246 static int
247 lpt_port_test(device_t ppbus, u_char data, u_char mask)
248 {
249 	int	temp, timeout;
250 
251 	data = data & mask;
252 	ppb_wdtr(ppbus, data);
253 	timeout = 10000;
254 	do {
255 		DELAY(10);
256 		temp = ppb_rdtr(ppbus) & mask;
257 	}
258 	while (temp != data && --timeout);
259 	lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
260 	return (temp == data);
261 }
262 
263 /*
264  * Probe simplified by replacing multiple loops with a hardcoded
265  * test pattern - 1999/02/08 des@freebsd.org
266  *
267  * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
268  * Based partially on Rod Grimes' printer probe
269  *
270  * Logic:
271  *	1) If no port address was given, use the bios detected ports
272  *	   and autodetect what ports the printers are on.
273  *	2) Otherwise, probe the data port at the address given,
274  *	   using the method in Rod Grimes' port probe.
275  *	   (Much code ripped off directly from Rod's probe.)
276  *
277  * Comments from Rod's probe:
278  * Logic:
279  *	1) You should be able to write to and read back the same value
280  *	   to the data port.  Do an alternating zeros, alternating ones,
281  *	   walking zero, and walking one test to check for stuck bits.
282  *
283  *	2) You should be able to write to and read back the same value
284  *	   to the control port lower 5 bits, the upper 3 bits are reserved
285  *	   per the IBM PC technical reference manauls and different boards
286  *	   do different things with them.  Do an alternating zeros, alternating
287  *	   ones, walking zero, and walking one test to check for stuck bits.
288  *
289  *	   Some printers drag the strobe line down when the are powered off
290  * 	   so this bit has been masked out of the control port test.
291  *
292  *	   XXX Some printers may not like a fast pulse on init or strobe, I
293  *	   don't know at this point, if that becomes a problem these bits
294  *	   should be turned off in the mask byte for the control port test.
295  *
296  *	   We are finally left with a mask of 0x14, due to some printers
297  *	   being adamant about holding other bits high ........
298  *
299  *	   Before probing the control port, we write a 0 to the data port -
300  *	   If not, some printers chuck out garbage when the strobe line
301  *	   gets toggled.
302  *
303  *	3) Set the data and control ports to a value of 0
304  *
305  *	This probe routine has been tested on Epson Lx-800, HP LJ3P,
306  *	Epson FX-1170 and C.Itoh 8510RM
307  *	printers.
308  *	Quick exit on fail added.
309  */
310 static int
311 lpt_detect(device_t dev)
312 {
313 	device_t ppbus = device_get_parent(dev);
314 
315 	static u_char	testbyte[18] = {
316 		0x55,			/* alternating zeros */
317 		0xaa,			/* alternating ones */
318 		0xfe, 0xfd, 0xfb, 0xf7,
319 		0xef, 0xdf, 0xbf, 0x7f,	/* walking zero */
320 		0x01, 0x02, 0x04, 0x08,
321 		0x10, 0x20, 0x40, 0x80	/* walking one */
322 	};
323 	int		i, error, status;
324 
325 	status = 1;				/* assume success */
326 
327 	if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
328 		printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
329 		status = 0;
330 		goto end_probe;
331 	}
332 
333 	for (i = 0; i < 18 && status; i++)
334 		if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
335 			status = 0;
336 			goto end_probe;
337 		}
338 
339 end_probe:
340 	/* write 0's to control and data ports */
341 	ppb_wdtr(ppbus, 0);
342 	ppb_wctr(ppbus, 0);
343 
344 	lpt_release_ppbus(dev);
345 
346 	return (status);
347 }
348 
349 static void
350 lpt_identify(driver_t *driver, device_t parent)
351 {
352 
353 	BUS_ADD_CHILD(parent, 0, LPT_NAME, 0);
354 }
355 
356 /*
357  * lpt_probe()
358  */
359 static int
360 lpt_probe(device_t dev)
361 {
362 	struct lpt_data *sc;
363 
364 	sc = DEVTOSOFTC(dev);
365 	bzero(sc, sizeof(struct lpt_data));
366 
367 	/*
368 	 * Now, try to detect the printer.
369 	 */
370 	if (!lpt_detect(dev))
371 		return (ENXIO);
372 
373 	device_set_desc(dev, "Printer");
374 
375 	return (0);
376 }
377 
378 static int
379 lpt_attach(device_t dev)
380 {
381 	device_t ppbus = device_get_parent(dev);
382 	struct lpt_data *sc = DEVTOSOFTC(dev);
383 	int zero = 0, unit = device_get_unit(dev);
384 	int error;
385 	uintptr_t irq;
386 
387 	sc->sc_primed = 0;	/* not primed yet */
388 
389 	if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
390 		printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
391 		return (0);
392 	}
393 
394 	ppb_wctr(ppbus, LPC_NINIT);
395 
396 	/* check if we can use interrupt, should be done by ppc stuff */
397 	lprintf(("oldirq %x\n", sc->sc_irq));
398 
399 	/* retrieve the ppbus irq */
400 	BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
401 
402 	if (irq > 0) {
403 		/* declare our interrupt handler */
404 		sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
405 						       &zero, irq, irq, 1, RF_SHAREABLE);
406 	}
407 	if (sc->intr_resource) {
408 		sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
409 		device_printf(dev, "Interrupt-driven port\n");
410 	} else {
411 		sc->sc_irq = 0;
412 		device_printf(dev, "Polled port\n");
413 	}
414 	lprintf(("irq %x %x\n", irq, sc->sc_irq));
415 
416 	lpt_release_ppbus(dev);
417 
418 	make_dev(&lpt_cdevsw, unit,
419 	    UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
420 	make_dev(&lpt_cdevsw, unit | LP_BYPASS,
421 	    UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
422 	return (0);
423 }
424 
425 static void
426 lptout(void *arg)
427 {
428 	device_t dev = (device_t)arg;
429 	struct lpt_data *sc = DEVTOSOFTC(dev);
430 #ifdef LPT_DEBUG
431 	device_t ppbus = device_get_parent(dev);
432 #endif
433 
434 	lprintf(("T %x ", ppb_rstr(ppbus)));
435 	if (sc->sc_state & OPEN) {
436 		sc->sc_backoff++;
437 		if (sc->sc_backoff > hz/LPTOUTMAX)
438 			sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
439 		timeout(lptout, (caddr_t)dev, sc->sc_backoff);
440 	} else
441 		sc->sc_state &= ~TOUT;
442 
443 	if (sc->sc_state & EERROR)
444 		sc->sc_state &= ~EERROR;
445 
446 	/*
447 	 * Avoid possible hangs due to missed interrupts
448 	 */
449 	if (sc->sc_xfercnt) {
450 		lptintr(dev);
451 	} else {
452 		sc->sc_state &= ~OBUSY;
453 		wakeup((caddr_t)dev);
454 	}
455 }
456 
457 /*
458  * lptopen -- reset the printer, then wait until it's selected and not busy.
459  *	If LP_BYPASS flag is selected, then we do not try to select the
460  *	printer -- this is just used for passing ioctls.
461  */
462 
463 static	int
464 lptopen(dev_t dev, int flags, int fmt, struct thread *p)
465 {
466 	int s;
467 	int trys, err;
468 	u_int unit = LPTUNIT(minor(dev));
469 	struct lpt_data *sc = UNITOSOFTC(unit);
470 	device_t lptdev = UNITODEVICE(unit);
471 	device_t ppbus = device_get_parent(lptdev);
472 
473 	if (!sc)
474 		return (ENXIO);
475 
476 	if (sc->sc_state) {
477 		lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
478 		return(EBUSY);
479 	} else
480 		sc->sc_state |= LPTINIT;
481 
482 	sc->sc_flags = LPTFLAGS(minor(dev));
483 
484 	/* Check for open with BYPASS flag set. */
485 	if (sc->sc_flags & LP_BYPASS) {
486 		sc->sc_state = OPEN;
487 		return(0);
488 	}
489 
490 	/* request the ppbus only if we don't have it already */
491 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
492 		/* give it a chance to try later */
493 		sc->sc_state = 0;
494 		return (err);
495 	}
496 
497 	s = spltty();
498 	lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
499 
500 	/* set IRQ status according to ENABLE_IRQ flag
501 	 */
502 	if (sc->sc_irq & LP_ENABLE_IRQ)
503 		sc->sc_irq |= LP_USE_IRQ;
504 	else
505 		sc->sc_irq &= ~LP_USE_IRQ;
506 
507 	/* init printer */
508 	if ((sc->sc_flags & LP_NO_PRIME) == 0) {
509 		if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
510 			ppb_wctr(ppbus, 0);
511 			sc->sc_primed++;
512 			DELAY(500);
513 		}
514 	}
515 
516 	ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
517 
518 	/* wait till ready (printer running diagnostics) */
519 	trys = 0;
520 	do {
521 		/* ran out of waiting for the printer */
522 		if (trys++ >= LPINITRDY*4) {
523 			splx(s);
524 			sc->sc_state = 0;
525 			lprintf(("status %x\n", ppb_rstr(ppbus)));
526 
527 			lpt_release_ppbus(lptdev);
528 			return (EBUSY);
529 		}
530 
531 		/* wait 1/4 second, give up if we get a signal */
532 		if (tsleep((caddr_t)lptdev, PCATCH, "lptinit", hz/4) !=
533 		    EWOULDBLOCK) {
534 			sc->sc_state = 0;
535 			splx(s);
536 
537 			lpt_release_ppbus(lptdev);
538 			return (EBUSY);
539 		}
540 
541 		/* is printer online and ready for output */
542 	} while ((ppb_rstr(ppbus) &
543 			(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
544 					(LPS_SEL|LPS_NBSY|LPS_NERR));
545 
546 	sc->sc_control = LPC_SEL|LPC_NINIT;
547 	if (sc->sc_flags & LP_AUTOLF)
548 		sc->sc_control |= LPC_AUTOL;
549 
550 	/* enable interrupt if interrupt-driven */
551 	if (sc->sc_irq & LP_USE_IRQ)
552 		sc->sc_control |= LPC_ENA;
553 
554 	ppb_wctr(ppbus, sc->sc_control);
555 
556 	sc->sc_state = OPEN;
557 	sc->sc_inbuf = malloc(BUFSIZE, M_LPT, M_WAITOK);
558 	sc->sc_statbuf = malloc(BUFSTATSIZE, M_LPT, M_WAITOK);
559 	sc->sc_xfercnt = 0;
560 	splx(s);
561 
562 	/* release the ppbus */
563 	lpt_release_ppbus(lptdev);
564 
565 	/* only use timeout if using interrupt */
566 	lprintf(("irq %x\n", sc->sc_irq));
567 	if (sc->sc_irq & LP_USE_IRQ) {
568 		sc->sc_state |= TOUT;
569 		timeout(lptout, (caddr_t)lptdev,
570 			 (sc->sc_backoff = hz/LPTOUTINITIAL));
571 	}
572 
573 	lprintf(("opened.\n"));
574 	return(0);
575 }
576 
577 /*
578  * lptclose -- close the device, free the local line buffer.
579  *
580  * Check for interrupted write call added.
581  */
582 
583 static	int
584 lptclose(dev_t dev, int flags, int fmt, struct thread *p)
585 {
586 	u_int unit = LPTUNIT(minor(dev));
587 	struct lpt_data *sc = UNITOSOFTC(unit);
588 	device_t lptdev = UNITODEVICE(unit);
589         device_t ppbus = device_get_parent(lptdev);
590 	int err;
591 
592 	if(sc->sc_flags & LP_BYPASS)
593 		goto end_close;
594 
595 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
596 		return (err);
597 
598 	sc->sc_state &= ~OPEN;
599 
600 	/* if the last write was interrupted, don't complete it */
601 	if((!(sc->sc_state  & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
602 		while ((ppb_rstr(ppbus) &
603 			(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
604 			(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
605 			/* wait 1/4 second, give up if we get a signal */
606 			if (tsleep((caddr_t)lptdev, PCATCH,
607 				"lpclose", hz) != EWOULDBLOCK)
608 				break;
609 
610 	ppb_wctr(ppbus, LPC_NINIT);
611 	free(sc->sc_inbuf, M_LPT);
612 	free(sc->sc_statbuf, M_LPT);
613 
614 end_close:
615 	/* release the bus anyway
616 	 * unregistration of interrupt forced by release
617 	 */
618 	lpt_release_ppbus(lptdev);
619 
620 	sc->sc_state = 0;
621 	sc->sc_xfercnt = 0;
622 	lprintf(("closed.\n"));
623 	return(0);
624 }
625 
626 /*
627  * lpt_pushbytes()
628  *	Workhorse for actually spinning and writing bytes to printer
629  *	Derived from lpa.c
630  *	Originally by ?
631  *
632  *	This code is only used when we are polling the port
633  */
634 static int
635 lpt_pushbytes(device_t dev)
636 {
637 	struct lpt_data *sc = DEVTOSOFTC(dev);
638 	device_t ppbus = device_get_parent(dev);
639 	int spin, err, tic;
640 	char ch;
641 
642 	lprintf(("p"));
643 	/* loop for every character .. */
644 	while (sc->sc_xfercnt > 0) {
645 		/* printer data */
646 		ch = *(sc->sc_cp);
647 		sc->sc_cp++;
648 		sc->sc_xfercnt--;
649 
650 		/*
651 		 * Wait for printer ready.
652 		 * Loop 20 usecs testing BUSY bit, then sleep
653 		 * for exponentially increasing timeout. (vak)
654 		 */
655 		for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
656 			DELAY(1);	/* XXX delay is NOT this accurate! */
657 		if (spin >= MAX_SPIN) {
658 			tic = 0;
659 			while (NOT_READY(ppbus)) {
660 				/*
661 				 * Now sleep, every cycle a
662 				 * little longer ..
663 				 */
664 				tic = tic + tic + 1;
665 				/*
666 				 * But no more than 10 seconds. (vak)
667 				 */
668 				if (tic > MAX_SLEEP)
669 					tic = MAX_SLEEP;
670 				err = tsleep((caddr_t)dev, 0,
671 					LPT_NAME "poll", tic);
672 				if (err != EWOULDBLOCK) {
673 					return (err);
674 				}
675 			}
676 		}
677 
678 		/* output data */
679 		ppb_wdtr(ppbus, ch);
680 		/* strobe */
681 		ppb_wctr(ppbus, sc->sc_control|LPC_STB);
682 		ppb_wctr(ppbus, sc->sc_control);
683 
684 	}
685 	return(0);
686 }
687 
688 /*
689  * lptread --retrieve printer status in IEEE1284 NIBBLE mode
690  */
691 
692 static int
693 lptread(dev_t dev, struct uio *uio, int ioflag)
694 {
695         u_int	unit = LPTUNIT(minor(dev));
696 	struct lpt_data *sc = UNITOSOFTC(unit);
697 	device_t lptdev = UNITODEVICE(unit);
698         device_t ppbus = device_get_parent(lptdev);
699 	int error = 0, len;
700 
701 	if (sc->sc_flags & LP_BYPASS) {
702 		/* we can't do reads in bypass mode */
703 		return (EPERM);
704 	}
705 
706 	if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0)))
707 		return (error);
708 
709 	/* read data in an other buffer, read/write may be simultaneous */
710 	len = 0;
711 	while (uio->uio_resid) {
712 		if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
713 				sc->sc_statbuf, min(BUFSTATSIZE,
714 				uio->uio_resid), &len))) {
715 			goto error;
716 		}
717 
718 		if (!len)
719 			goto error;		/* no more data */
720 
721 		if ((error = uiomove(sc->sc_statbuf, len, uio)))
722 			goto error;
723 	}
724 
725 error:
726 	ppb_1284_terminate(ppbus);
727 	return (error);
728 }
729 
730 /*
731  * lptwrite --copy a line from user space to a local buffer, then call
732  * putc to get the chars moved to the output queue.
733  *
734  * Flagging of interrupted write added.
735  */
736 
737 static	int
738 lptwrite(dev_t dev, struct uio *uio, int ioflag)
739 {
740 	unsigned n;
741 	int err;
742         u_int	unit = LPTUNIT(minor(dev));
743 	struct lpt_data *sc = UNITOSOFTC(unit);
744 	device_t lptdev = UNITODEVICE(unit);
745         device_t ppbus = device_get_parent(lptdev);
746 
747 	if(sc->sc_flags & LP_BYPASS) {
748 		/* we can't do writes in bypass mode */
749 		return(EPERM);
750 	}
751 
752 	/* request the ppbus only if we don't have it already */
753 	/* XXX interrupt registration?! */
754 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
755 		return (err);
756 
757 	/* if interrupts are working, register the handler */
758 	if (sc->sc_irq & LP_USE_IRQ) {
759 		/* register our interrupt handler */
760 		err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource,
761 			       INTR_TYPE_TTY, lpt_intr, lptdev,
762 			       &sc->intr_cookie);
763 		if (err) {
764 			device_printf(lptdev, "handler registration failed, polled mode.\n");
765 			sc->sc_irq &= ~LP_USE_IRQ;
766 		}
767 	}
768 
769 	sc->sc_state &= ~INTERRUPTED;
770 	while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
771 		sc->sc_cp = sc->sc_inbuf;
772 		uiomove(sc->sc_cp, n, uio);
773 		sc->sc_xfercnt = n ;
774 
775 		if (sc->sc_irq & LP_ENABLE_EXT) {
776 			/* try any extended mode */
777 			err = ppb_write(ppbus, sc->sc_cp,
778 					sc->sc_xfercnt, 0);
779 			switch (err) {
780 			case 0:
781 				/* if not all data was sent, we could rely
782 				 * on polling for the last bytes */
783 				sc->sc_xfercnt = 0;
784 				break;
785 			case EINTR:
786 				sc->sc_state |= INTERRUPTED;
787 				return(err);
788 			case EINVAL:
789 				/* advanced mode not avail */
790 				log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
791 				break;
792 			default:
793 				return(err);
794 			}
795 		} else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
796 			lprintf(("i"));
797 			/* if the printer is ready for a char, */
798 			/* give it one */
799 			if ((sc->sc_state & OBUSY) == 0){
800 				lprintf(("\nC %d. ", sc->sc_xfercnt));
801 				lptintr(lptdev);
802 			}
803 			lprintf(("W "));
804 			if (sc->sc_state & OBUSY)
805 				if ((err = tsleep((caddr_t)lptdev,
806 					 PCATCH, LPT_NAME "write", 0))) {
807 					sc->sc_state |= INTERRUPTED;
808 					return(err);
809 				}
810 		}
811 
812 		/* check to see if we must do a polled write */
813 		if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
814 			lprintf(("p"));
815 
816 			err = lpt_pushbytes(lptdev);
817 
818 			if (err)
819 				return(err);
820 		}
821 	}
822 
823 	/* we have not been interrupted, release the ppbus */
824 	lpt_release_ppbus(lptdev);
825 
826 	return(0);
827 }
828 
829 /*
830  * lpt_intr -- handle printer interrupts which occur when the printer is
831  * ready to accept another char.
832  *
833  * do checking for interrupted write call.
834  */
835 
836 static void
837 lpt_intr(void *arg)
838 {
839 	device_t lptdev = (device_t)arg;
840         device_t ppbus = device_get_parent(lptdev);
841 	struct lpt_data *sc = DEVTOSOFTC(lptdev);
842 	int sts = 0;
843 	int i;
844 
845 	/* we must own the bus to use it */
846 	if ((sc->sc_state & HAVEBUS) == 0)
847 		return;
848 
849 	/*
850 	 * Is printer online and ready for output?
851 	 *
852 	 * Avoid falling back to lptout() too quickly.  First spin-loop
853 	 * to see if the printer will become ready ``really soon now''.
854 	 */
855 	for (i = 0; i < 100 &&
856 	     ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
857 
858 	if ((sts & RDY_MASK) == LP_READY) {
859 		sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
860 		sc->sc_backoff = hz/LPTOUTINITIAL;
861 
862 		if (sc->sc_xfercnt) {
863 			/* send char */
864 			/*lprintf(("%x ", *sc->sc_cp)); */
865 			ppb_wdtr(ppbus, *sc->sc_cp++) ;
866 			ppb_wctr(ppbus, sc->sc_control|LPC_STB);
867 			/* DELAY(X) */
868 			ppb_wctr(ppbus, sc->sc_control);
869 
870 			/* any more data for printer */
871 			if(--(sc->sc_xfercnt) > 0) return;
872 		}
873 
874 		/*
875 		 * No more data waiting for printer.
876 		 * Wakeup is not done if write call was not interrupted.
877 		 */
878 		sc->sc_state &= ~OBUSY;
879 
880 		if(!(sc->sc_state & INTERRUPTED))
881 			wakeup((caddr_t)lptdev);
882 		lprintf(("w "));
883 		return;
884 	} else	{	/* check for error */
885 		if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
886 				(sc->sc_state & OPEN))
887 			sc->sc_state |= EERROR;
888 		/* lptout() will jump in and try to restart. */
889 	}
890 	lprintf(("sts %x ", sts));
891 }
892 
893 static void
894 lptintr(device_t dev)
895 {
896 	/* call the interrupt at required spl level */
897 	int s = spltty();
898 
899 	lpt_intr(dev);
900 
901 	splx(s);
902 	return;
903 }
904 
905 static	int
906 lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *p)
907 {
908 	int	error = 0;
909         u_int	unit = LPTUNIT(minor(dev));
910         struct	lpt_data *sc = UNITOSOFTC(unit);
911 	u_char	old_sc_irq;	/* old printer IRQ status */
912 
913 	switch (cmd) {
914 	case LPT_IRQ :
915 		if(sc->sc_irq & LP_HAS_IRQ) {
916 			/*
917 			 * NOTE:
918 			 * If the IRQ status is changed,
919 			 * this will only be visible on the
920 			 * next open.
921 			 *
922 			 * If interrupt status changes,
923 			 * this gets syslog'd.
924 			 */
925 			old_sc_irq = sc->sc_irq;
926 			switch(*(int*)data) {
927 			case 0:
928 				sc->sc_irq &= (~LP_ENABLE_IRQ);
929 				break;
930 			case 1:
931 				sc->sc_irq &= (~LP_ENABLE_EXT);
932 				sc->sc_irq |= LP_ENABLE_IRQ;
933 				break;
934 			case 2:
935 				/* classic irq based transfer and advanced
936 				 * modes are in conflict
937 				 */
938 				sc->sc_irq &= (~LP_ENABLE_IRQ);
939 				sc->sc_irq |= LP_ENABLE_EXT;
940 				break;
941 			case 3:
942 				sc->sc_irq &= (~LP_ENABLE_EXT);
943 				break;
944 			default:
945 				break;
946 			}
947 
948 			if (old_sc_irq != sc->sc_irq )
949 				log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
950 					unit,
951 					(sc->sc_irq & LP_ENABLE_IRQ)?
952 					"interrupt-driven":"polled",
953 					(sc->sc_irq & LP_ENABLE_EXT)?
954 					"extended":"standard");
955 		} else /* polled port */
956 			error = EOPNOTSUPP;
957 		break;
958 	default:
959 		error = ENODEV;
960 	}
961 
962 	return(error);
963 }
964 
965 static device_method_t lpt_methods[] = {
966 	/* device interface */
967 	DEVMETHOD(device_identify,	lpt_identify),
968 	DEVMETHOD(device_probe,		lpt_probe),
969 	DEVMETHOD(device_attach,	lpt_attach),
970 
971 	{ 0, 0 }
972 };
973 
974 static driver_t lpt_driver = {
975 	LPT_NAME,
976 	lpt_methods,
977 	sizeof(struct lpt_data),
978 };
979 
980 DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0);
981