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