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