xref: /freebsd/sys/dev/ppbus/lpt.c (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1990 William F. Jolitz, TeleMuse
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This software is a component of "386BSD" developed by
18  *	William F. Jolitz, TeleMuse.
19  * 4. Neither the name of the developer nor the name "386BSD"
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
24  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
25  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
26  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
27  * NOT MAKE USE OF THIS WORK.
28  *
29  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
30  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
31  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
32  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
33  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
34  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
35  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
36  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  *
50  *	from: unknown origin, 386BSD 0.1
51  *	From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
52  *	From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
53  */
54 
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57 
58 /*
59  * Device Driver for AT parallel printer port
60  * Written by William Jolitz 12/18/90
61  */
62 
63 /*
64  * Updated for ppbus by Nicolas Souchu
65  * [Mon Jul 28 1997]
66  */
67 
68 #include "opt_lpt.h"
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/module.h>
73 #include <sys/bus.h>
74 #include <sys/conf.h>
75 #include <sys/kernel.h>
76 #include <sys/uio.h>
77 #include <sys/syslog.h>
78 #include <sys/malloc.h>
79 
80 #include <machine/bus.h>
81 #include <machine/resource.h>
82 #include <sys/rman.h>
83 
84 #include <dev/ppbus/lptio.h>
85 #include <dev/ppbus/ppbconf.h>
86 #include <dev/ppbus/ppb_1284.h>
87 #include <dev/ppbus/lpt.h>
88 #include "ppbus_if.h"
89 #include <dev/ppbus/ppbio.h>
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 struct lpt_data {
110 	device_t sc_dev;
111 	struct cdev *sc_cdev;
112 	struct cdev *sc_cdev_bypass;
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 positive 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 	struct callout sc_timer;
136 
137 	struct resource *sc_intr_resource;	/* interrupt resource */
138 	void	*sc_intr_cookie;		/* interrupt cookie */
139 };
140 
141 #define	LPT_NAME	"lpt"		/* our official name */
142 
143 static callout_func_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 
150 static void lptintr(void *arg);
151 
152 /* bits for state */
153 #define	OPEN		(1<<0)	/* device is open */
154 #define	ASLP		(1<<1)	/* awaiting draining of printer */
155 #define	EERROR		(1<<2)	/* error was received from printer */
156 #define	OBUSY		(1<<3)	/* printer is busy doing output */
157 #define	LPTOUT		(1<<4)	/* timeout while not selected */
158 #define	TOUT		(1<<5)	/* timeout while not selected */
159 #define	LPTINIT		(1<<6)	/* waiting to initialize for open */
160 #define	INTERRUPTED	(1<<7)	/* write call was interrupted */
161 #define	HAVEBUS		(1<<8)	/* the driver owns the bus */
162 
163 /* status masks to interrogate printer status */
164 #define	RDY_MASK	(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)	/* ready ? */
165 #define	LP_READY	(LPS_SEL|LPS_NBSY|LPS_NERR)
166 
167 /* Printer Ready condition  - from lpa.c */
168 /* Only used in polling code */
169 #define	LPS_INVERT	(LPS_NBSY | LPS_NACK |           LPS_SEL | LPS_NERR)
170 #define	LPS_MASK	(LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
171 #define	NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
172 
173 #define	MAX_SLEEP	(hz*5)	/* Timeout while waiting for device ready */
174 #define	MAX_SPIN	20	/* Max delay for device ready in usecs */
175 
176 static	d_open_t	lptopen;
177 static	d_close_t	lptclose;
178 static	d_write_t	lptwrite;
179 static	d_read_t	lptread;
180 static	d_ioctl_t	lptioctl;
181 
182 static struct cdevsw lpt_cdevsw = {
183 	.d_version =	D_VERSION,
184 	.d_open =	lptopen,
185 	.d_close =	lptclose,
186 	.d_read =	lptread,
187 	.d_write =	lptwrite,
188 	.d_ioctl =	lptioctl,
189 	.d_name =	LPT_NAME,
190 };
191 
192 static int
193 lpt_request_ppbus(device_t dev, int how)
194 {
195 	device_t ppbus = device_get_parent(dev);
196 	struct lpt_data *sc = DEVTOSOFTC(dev);
197 	int error;
198 
199 	/*
200 	 * We might already have the bus for a write(2) after an interrupted
201 	 * write(2) call.
202 	 */
203 	ppb_assert_locked(ppbus);
204 	if (sc->sc_state & HAVEBUS)
205 		return (0);
206 
207 	error = ppb_request_bus(ppbus, dev, how);
208 	if (error == 0)
209 		sc->sc_state |= HAVEBUS;
210 	return (error);
211 }
212 
213 static int
214 lpt_release_ppbus(device_t dev)
215 {
216 	device_t ppbus = device_get_parent(dev);
217 	struct lpt_data *sc = DEVTOSOFTC(dev);
218 	int error = 0;
219 
220 	ppb_assert_locked(ppbus);
221 	if (sc->sc_state & HAVEBUS) {
222 		error = ppb_release_bus(ppbus, dev);
223 		if (error == 0)
224 			sc->sc_state &= ~HAVEBUS;
225 	}
226 	return (error);
227 }
228 
229 /*
230  * Internal routine to lptprobe to do port tests of one byte value
231  */
232 static int
233 lpt_port_test(device_t ppbus, u_char data, u_char mask)
234 {
235 	int	temp, timeout;
236 
237 	data = data & mask;
238 	ppb_wdtr(ppbus, data);
239 	timeout = 10000;
240 	do {
241 		DELAY(10);
242 		temp = ppb_rdtr(ppbus) & mask;
243 	}
244 	while (temp != data && --timeout);
245 	lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
246 	return (temp == data);
247 }
248 
249 /*
250  * Probe simplified by replacing multiple loops with a hardcoded
251  * test pattern - 1999/02/08 des@freebsd.org
252  *
253  * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
254  * Based partially on Rod Grimes' printer probe
255  *
256  * Logic:
257  *	1) If no port address was given, use the bios detected ports
258  *	   and autodetect what ports the printers are on.
259  *	2) Otherwise, probe the data port at the address given,
260  *	   using the method in Rod Grimes' port probe.
261  *	   (Much code ripped off directly from Rod's probe.)
262  *
263  * Comments from Rod's probe:
264  * Logic:
265  *	1) You should be able to write to and read back the same value
266  *	   to the data port.  Do an alternating zeros, alternating ones,
267  *	   walking zero, and walking one test to check for stuck bits.
268  *
269  *	2) You should be able to write to and read back the same value
270  *	   to the control port lower 5 bits, the upper 3 bits are reserved
271  *	   per the IBM PC technical reference manuals and different boards
272  *	   do different things with them.  Do an alternating zeros, alternating
273  *	   ones, walking zero, and walking one test to check for stuck bits.
274  *
275  *	   Some printers drag the strobe line down when the are powered off
276  * 	   so this bit has been masked out of the control port test.
277  *
278  *	   XXX Some printers may not like a fast pulse on init or strobe, I
279  *	   don't know at this point, if that becomes a problem these bits
280  *	   should be turned off in the mask byte for the control port test.
281  *
282  *	   We are finally left with a mask of 0x14, due to some printers
283  *	   being adamant about holding other bits high ........
284  *
285  *	   Before probing the control port, we write a 0 to the data port -
286  *	   If not, some printers chuck out garbage when the strobe line
287  *	   gets toggled.
288  *
289  *	3) Set the data and control ports to a value of 0
290  *
291  *	This probe routine has been tested on Epson Lx-800, HP LJ3P,
292  *	Epson FX-1170 and C.Itoh 8510RM
293  *	printers.
294  *	Quick exit on fail added.
295  */
296 static int
297 lpt_detect(device_t dev)
298 {
299 	device_t ppbus = device_get_parent(dev);
300 
301 	static u_char	testbyte[18] = {
302 		0x55,			/* alternating zeros */
303 		0xaa,			/* alternating ones */
304 		0xfe, 0xfd, 0xfb, 0xf7,
305 		0xef, 0xdf, 0xbf, 0x7f,	/* walking zero */
306 		0x01, 0x02, 0x04, 0x08,
307 		0x10, 0x20, 0x40, 0x80	/* walking one */
308 	};
309 	int		i, error, status;
310 
311 	status = 1;				/* assume success */
312 
313 	ppb_lock(ppbus);
314 	if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
315 		ppb_unlock(ppbus);
316 		device_printf(dev, "cannot alloc ppbus (%d)!\n", error);
317 		return (0);
318 	}
319 
320 	for (i = 0; i < 18 && status; i++)
321 		if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
322 			status = 0;
323 			break;
324 		}
325 
326 	/* write 0's to control and data ports */
327 	ppb_wdtr(ppbus, 0);
328 	ppb_wctr(ppbus, 0);
329 
330 	lpt_release_ppbus(dev);
331 	ppb_unlock(ppbus);
332 
333 	return (status);
334 }
335 
336 static void
337 lpt_identify(driver_t *driver, device_t parent)
338 {
339 
340 	device_t dev;
341 
342 	dev = device_find_child(parent, LPT_NAME, -1);
343 	if (!dev)
344 		BUS_ADD_CHILD(parent, 0, LPT_NAME, -1);
345 }
346 
347 /*
348  * lpt_probe()
349  */
350 static int
351 lpt_probe(device_t dev)
352 {
353 
354 	if (!lpt_detect(dev))
355 		return (ENXIO);
356 
357 	device_set_desc(dev, "Printer");
358 
359 	return (0);
360 }
361 
362 static int
363 lpt_attach(device_t dev)
364 {
365 	device_t ppbus = device_get_parent(dev);
366 	struct lpt_data *sc = DEVTOSOFTC(dev);
367 	int rid = 0, unit = device_get_unit(dev);
368 	int error;
369 
370 	sc->sc_primed = 0;	/* not primed yet */
371 	ppb_init_callout(ppbus, &sc->sc_timer, 0);
372 
373 	ppb_lock(ppbus);
374 	if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
375 		ppb_unlock(ppbus);
376 		device_printf(dev, "cannot alloc ppbus (%d)!\n", error);
377 		return (0);
378 	}
379 
380 	ppb_wctr(ppbus, LPC_NINIT);
381 	lpt_release_ppbus(dev);
382 	ppb_unlock(ppbus);
383 
384 	/* declare our interrupt handler */
385 	sc->sc_intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
386 	    RF_SHAREABLE);
387 	if (sc->sc_intr_resource) {
388 		error = bus_setup_intr(dev, sc->sc_intr_resource,
389 		    INTR_TYPE_TTY | INTR_MPSAFE, NULL, lptintr, sc,
390 		    &sc->sc_intr_cookie);
391 		if (error) {
392 			bus_release_resource(dev, SYS_RES_IRQ, rid,
393 			    sc->sc_intr_resource);
394 			device_printf(dev,
395 			    "Unable to register interrupt handler\n");
396 			return (error);
397 		}
398 		sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
399 		device_printf(dev, "Interrupt-driven port\n");
400 	} else {
401 		sc->sc_irq = 0;
402 		device_printf(dev, "Polled port\n");
403 	}
404 	lprintf(("irq %x\n", sc->sc_irq));
405 
406 	sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
407 	sc->sc_statbuf = malloc(BUFSTATSIZE, M_DEVBUF, M_WAITOK);
408 	sc->sc_dev = dev;
409 	sc->sc_cdev = make_dev(&lpt_cdevsw, unit,
410 	    UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
411 	sc->sc_cdev->si_drv1 = sc;
412 	sc->sc_cdev->si_drv2 = 0;
413 	sc->sc_cdev_bypass = make_dev(&lpt_cdevsw, unit,
414 	    UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
415 	sc->sc_cdev_bypass->si_drv1 = sc;
416 	sc->sc_cdev_bypass->si_drv2 = (void *)LP_BYPASS;
417 	return (0);
418 }
419 
420 static int
421 lpt_detach(device_t dev)
422 {
423 	struct lpt_data *sc = DEVTOSOFTC(dev);
424 	device_t ppbus = device_get_parent(dev);
425 
426 	destroy_dev(sc->sc_cdev);
427 	destroy_dev(sc->sc_cdev_bypass);
428 	ppb_lock(ppbus);
429 	lpt_release_ppbus(dev);
430 	ppb_unlock(ppbus);
431 	callout_drain(&sc->sc_timer);
432 	if (sc->sc_intr_resource != NULL) {
433 		bus_teardown_intr(dev, sc->sc_intr_resource,
434 		    sc->sc_intr_cookie);
435 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr_resource);
436 	}
437 	free(sc->sc_inbuf, M_DEVBUF);
438 	free(sc->sc_statbuf, M_DEVBUF);
439 
440 	return (0);
441 }
442 
443 static void
444 lptout(void *arg)
445 {
446 	struct lpt_data *sc = arg;
447 	device_t dev = sc->sc_dev;
448 	device_t ppbus __unused;
449 
450 	ppbus = device_get_parent(dev);
451 	ppb_assert_locked(ppbus);
452 	lprintf(("T %x ", ppb_rstr(ppbus)));
453 	if (sc->sc_state & OPEN) {
454 		sc->sc_backoff++;
455 		if (sc->sc_backoff > hz/LPTOUTMAX)
456 			sc->sc_backoff = hz/LPTOUTMAX;
457 		callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
458 	} else
459 		sc->sc_state &= ~TOUT;
460 
461 	if (sc->sc_state & EERROR)
462 		sc->sc_state &= ~EERROR;
463 
464 	/*
465 	 * Avoid possible hangs due to missed interrupts
466 	 */
467 	if (sc->sc_xfercnt) {
468 		lptintr(sc);
469 	} else {
470 		sc->sc_state &= ~OBUSY;
471 		wakeup(dev);
472 	}
473 }
474 
475 /*
476  * lptopen -- reset the printer, then wait until it's selected and not busy.
477  *	If LP_BYPASS flag is selected, then we do not try to select the
478  *	printer -- this is just used for passing ioctls.
479  */
480 
481 static	int
482 lptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
483 {
484 	int trys, err;
485 	struct lpt_data *sc = dev->si_drv1;
486 	device_t lptdev;
487 	device_t ppbus;
488 
489 	if (!sc)
490 		return (ENXIO);
491 
492 	lptdev = sc->sc_dev;
493 	ppbus = device_get_parent(lptdev);
494 
495 	ppb_lock(ppbus);
496 	if (sc->sc_state) {
497 		lprintf(("%s: still open %x\n", device_get_nameunit(lptdev),
498 		    sc->sc_state));
499 		ppb_unlock(ppbus);
500 		return(EBUSY);
501 	} else
502 		sc->sc_state |= LPTINIT;
503 
504 	sc->sc_flags = (uintptr_t)dev->si_drv2;
505 
506 	/* Check for open with BYPASS flag set. */
507 	if (sc->sc_flags & LP_BYPASS) {
508 		sc->sc_state = OPEN;
509 		ppb_unlock(ppbus);
510 		return(0);
511 	}
512 
513 	/* request the ppbus only if we don't have it already */
514 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
515 		/* give it a chance to try later */
516 		sc->sc_state = 0;
517 		ppb_unlock(ppbus);
518 		return (err);
519 	}
520 
521 	lprintf(("%s flags 0x%x\n", device_get_nameunit(lptdev),
522 	    sc->sc_flags));
523 
524 	/* set IRQ status according to ENABLE_IRQ flag
525 	 */
526 	if (sc->sc_irq & LP_ENABLE_IRQ)
527 		sc->sc_irq |= LP_USE_IRQ;
528 	else
529 		sc->sc_irq &= ~LP_USE_IRQ;
530 
531 	/* init printer */
532 	if ((sc->sc_flags & LP_NO_PRIME) == 0) {
533 		if ((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
534 			ppb_wctr(ppbus, 0);
535 			sc->sc_primed++;
536 			DELAY(500);
537 		}
538 	}
539 
540 	ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
541 
542 	/* wait till ready (printer running diagnostics) */
543 	trys = 0;
544 	do {
545 		/* ran out of waiting for the printer */
546 		if (trys++ >= LPINITRDY*4) {
547 			lprintf(("status %x\n", ppb_rstr(ppbus)));
548 
549 			lpt_release_ppbus(lptdev);
550 			sc->sc_state = 0;
551 			ppb_unlock(ppbus);
552 			return (EBUSY);
553 		}
554 
555 		/* wait 1/4 second, give up if we get a signal */
556 		if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lptinit",
557 		    hz / 4) != EWOULDBLOCK) {
558 			lpt_release_ppbus(lptdev);
559 			sc->sc_state = 0;
560 			ppb_unlock(ppbus);
561 			return (EBUSY);
562 		}
563 
564 		/* is printer online and ready for output */
565 	} while ((ppb_rstr(ppbus) &
566 			(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
567 					(LPS_SEL|LPS_NBSY|LPS_NERR));
568 
569 	sc->sc_control = LPC_SEL|LPC_NINIT;
570 	if (sc->sc_flags & LP_AUTOLF)
571 		sc->sc_control |= LPC_AUTOL;
572 
573 	/* enable interrupt if interrupt-driven */
574 	if (sc->sc_irq & LP_USE_IRQ)
575 		sc->sc_control |= LPC_ENA;
576 
577 	ppb_wctr(ppbus, sc->sc_control);
578 
579 	sc->sc_state &= ~LPTINIT;
580 	sc->sc_state |= OPEN;
581 	sc->sc_xfercnt = 0;
582 
583 	/* only use timeout if using interrupt */
584 	lprintf(("irq %x\n", sc->sc_irq));
585 	if (sc->sc_irq & LP_USE_IRQ) {
586 		sc->sc_state |= TOUT;
587 		sc->sc_backoff = hz / LPTOUTINITIAL;
588 		callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
589 	}
590 
591 	/* release the ppbus */
592 	lpt_release_ppbus(lptdev);
593 	ppb_unlock(ppbus);
594 
595 	lprintf(("opened.\n"));
596 	return(0);
597 }
598 
599 /*
600  * lptclose -- close the device, free the local line buffer.
601  *
602  * Check for interrupted write call added.
603  */
604 
605 static	int
606 lptclose(struct cdev *dev, int flags, int fmt, struct thread *td)
607 {
608 	struct lpt_data *sc = dev->si_drv1;
609 	device_t lptdev = sc->sc_dev;
610 	device_t ppbus = device_get_parent(lptdev);
611 	int err;
612 
613 	ppb_lock(ppbus);
614 	if (sc->sc_flags & LP_BYPASS)
615 		goto end_close;
616 
617 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
618 		ppb_unlock(ppbus);
619 		return (err);
620 	}
621 
622 	/* if the last write was interrupted, don't complete it */
623 	if ((!(sc->sc_state  & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
624 		while ((ppb_rstr(ppbus) &
625 			(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
626 			(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
627 			/* wait 1 second, give up if we get a signal */
628 			if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lpclose",
629 			    hz) != EWOULDBLOCK)
630 				break;
631 
632 	sc->sc_state &= ~OPEN;
633 	callout_stop(&sc->sc_timer);
634 	ppb_wctr(ppbus, LPC_NINIT);
635 
636 	/*
637 	 * unregistration of interrupt forced by release
638 	 */
639 	lpt_release_ppbus(lptdev);
640 
641 end_close:
642 	sc->sc_state = 0;
643 	sc->sc_xfercnt = 0;
644 	ppb_unlock(ppbus);
645 	lprintf(("closed.\n"));
646 	return(0);
647 }
648 
649 /*
650  * lpt_pushbytes()
651  *	Workhorse for actually spinning and writing bytes to printer
652  *	Derived from lpa.c
653  *	Originally by ?
654  *
655  *	This code is only used when we are polling the port
656  */
657 static int
658 lpt_pushbytes(struct lpt_data *sc)
659 {
660 	device_t dev = sc->sc_dev;
661 	device_t ppbus = device_get_parent(dev);
662 	int spin, err, tic;
663 	char ch;
664 
665 	ppb_assert_locked(ppbus);
666 	lprintf(("p"));
667 	/* loop for every character .. */
668 	while (sc->sc_xfercnt > 0) {
669 		/* printer data */
670 		ch = *(sc->sc_cp);
671 		sc->sc_cp++;
672 		sc->sc_xfercnt--;
673 
674 		/*
675 		 * Wait for printer ready.
676 		 * Loop 20 usecs testing BUSY bit, then sleep
677 		 * for exponentially increasing timeout. (vak)
678 		 */
679 		for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
680 			DELAY(1);	/* XXX delay is NOT this accurate! */
681 		if (spin >= MAX_SPIN) {
682 			tic = 0;
683 			while (NOT_READY(ppbus)) {
684 				/*
685 				 * Now sleep, every cycle a
686 				 * little longer ..
687 				 */
688 				tic = tic + tic + 1;
689 				/*
690 				 * But no more than 10 seconds. (vak)
691 				 */
692 				if (tic > MAX_SLEEP)
693 					tic = MAX_SLEEP;
694 				err = ppb_sleep(ppbus, dev, LPPRI,
695 					LPT_NAME "poll", tic);
696 				if (err != EWOULDBLOCK) {
697 					return (err);
698 				}
699 			}
700 		}
701 
702 		/* output data */
703 		ppb_wdtr(ppbus, ch);
704 		/* strobe */
705 		ppb_wctr(ppbus, sc->sc_control|LPC_STB);
706 		ppb_wctr(ppbus, sc->sc_control);
707 	}
708 	return(0);
709 }
710 
711 /*
712  * lptread --retrieve printer status in IEEE1284 NIBBLE mode
713  */
714 
715 static int
716 lptread(struct cdev *dev, struct uio *uio, int ioflag)
717 {
718 	struct lpt_data *sc = dev->si_drv1;
719 	device_t lptdev = sc->sc_dev;
720 	device_t ppbus = device_get_parent(lptdev);
721 	int error = 0, len;
722 
723 	if (sc->sc_flags & LP_BYPASS) {
724 		/* we can't do reads in bypass mode */
725 		return (EPERM);
726 	}
727 
728 	ppb_lock(ppbus);
729 	if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) {
730 		ppb_unlock(ppbus);
731 		return (error);
732 	}
733 
734 	/* read data in an other buffer, read/write may be simultaneous */
735 	len = 0;
736 	while (uio->uio_resid) {
737 		if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
738 				sc->sc_statbuf, min(BUFSTATSIZE,
739 				uio->uio_resid), &len))) {
740 			goto error;
741 		}
742 
743 		if (!len)
744 			goto error;		/* no more data */
745 
746 		ppb_unlock(ppbus);
747 		error = uiomove(sc->sc_statbuf, len, uio);
748 		ppb_lock(ppbus);
749 		if (error)
750 			goto error;
751 	}
752 
753 error:
754 	ppb_1284_terminate(ppbus);
755 	ppb_unlock(ppbus);
756 	return (error);
757 }
758 
759 /*
760  * lptwrite --copy a line from user space to a local buffer, then call
761  * putc to get the chars moved to the output queue.
762  *
763  * Flagging of interrupted write added.
764  */
765 
766 static	int
767 lptwrite(struct cdev *dev, struct uio *uio, int ioflag)
768 {
769 	register unsigned n;
770 	int err;
771 	struct lpt_data *sc = dev->si_drv1;
772 	device_t lptdev = sc->sc_dev;
773 	device_t ppbus = device_get_parent(lptdev);
774 
775 	if (sc->sc_flags & LP_BYPASS) {
776 		/* we can't do writes in bypass mode */
777 		return (EPERM);
778 	}
779 
780 	/* request the ppbus only if we don't have it already */
781 	ppb_lock(ppbus);
782 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
783 		ppb_unlock(ppbus);
784 		return (err);
785 	}
786 
787 	sc->sc_state &= ~INTERRUPTED;
788 	while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
789 		sc->sc_cp = sc->sc_inbuf;
790 		ppb_unlock(ppbus);
791 		err = uiomove(sc->sc_cp, n, uio);
792 		ppb_lock(ppbus);
793 		if (err)
794 			break;
795 		sc->sc_xfercnt = n;
796 
797 		if (sc->sc_irq & LP_ENABLE_EXT) {
798 			/* try any extended mode */
799 			err = ppb_write(ppbus, sc->sc_cp,
800 					sc->sc_xfercnt, 0);
801 			switch (err) {
802 			case 0:
803 				/* if not all data was sent, we could rely
804 				 * on polling for the last bytes */
805 				sc->sc_xfercnt = 0;
806 				break;
807 			case EINTR:
808 				sc->sc_state |= INTERRUPTED;
809 				ppb_unlock(ppbus);
810 				return (err);
811 			case EINVAL:
812 				/* advanced mode not avail */
813 				log(LOG_NOTICE,
814 				    "%s: advanced mode not avail, polling\n",
815 				    device_get_nameunit(sc->sc_dev));
816 				break;
817 			default:
818 				ppb_unlock(ppbus);
819 				return (err);
820 			}
821 		} else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
822 			lprintf(("i"));
823 			/* if the printer is ready for a char, */
824 			/* give it one */
825 			if ((sc->sc_state & OBUSY) == 0){
826 				lprintf(("\nC %d. ", sc->sc_xfercnt));
827 				lptintr(sc);
828 			}
829 			lprintf(("W "));
830 			if (sc->sc_state & OBUSY)
831 				if ((err = ppb_sleep(ppbus, lptdev,
832 					 LPPRI|PCATCH, LPT_NAME "write", 0))) {
833 					sc->sc_state |= INTERRUPTED;
834 					ppb_unlock(ppbus);
835 					return(err);
836 				}
837 		}
838 
839 		/* check to see if we must do a polled write */
840 		if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
841 			lprintf(("p"));
842 
843 			err = lpt_pushbytes(sc);
844 
845 			if (err) {
846 				ppb_unlock(ppbus);
847 				return (err);
848 			}
849 		}
850 	}
851 
852 	/* we have not been interrupted, release the ppbus */
853 	lpt_release_ppbus(lptdev);
854 	ppb_unlock(ppbus);
855 
856 	return (err);
857 }
858 
859 /*
860  * lptintr -- handle printer interrupts which occur when the printer is
861  * ready to accept another char.
862  *
863  * do checking for interrupted write call.
864  */
865 static void
866 lptintr(void *arg)
867 {
868 	struct lpt_data *sc = arg;
869 	device_t lptdev = sc->sc_dev;
870 	device_t ppbus = device_get_parent(lptdev);
871 	int sts = 0;
872 	int i;
873 
874 	/*
875 	 * Is printer online and ready for output?
876 	 *
877 	 * Avoid falling back to lptout() too quickly.  First spin-loop
878 	 * to see if the printer will become ready ``really soon now''.
879 	 */
880 	for (i = 0; i < 100 &&
881 	     ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
882 
883 	if ((sts & RDY_MASK) == LP_READY) {
884 		sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
885 		sc->sc_backoff = hz / LPTOUTINITIAL;
886 
887 		if (sc->sc_xfercnt) {
888 			/* send char */
889 			/*lprintf(("%x ", *sc->sc_cp)); */
890 			ppb_wdtr(ppbus, *sc->sc_cp++) ;
891 			ppb_wctr(ppbus, sc->sc_control|LPC_STB);
892 			/* DELAY(X) */
893 			ppb_wctr(ppbus, sc->sc_control);
894 
895 			/* any more data for printer */
896 			if (--(sc->sc_xfercnt) > 0)
897 				return;
898 		}
899 
900 		/*
901 		 * No more data waiting for printer.
902 		 * Wakeup is not done if write call was not interrupted.
903 		 */
904 		sc->sc_state &= ~OBUSY;
905 
906 		if (!(sc->sc_state & INTERRUPTED))
907 			wakeup(lptdev);
908 		lprintf(("w "));
909 		return;
910 	} else	{	/* check for error */
911 		if (((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
912 				(sc->sc_state & OPEN))
913 			sc->sc_state |= EERROR;
914 		/* lptout() will jump in and try to restart. */
915 	}
916 	lprintf(("sts %x ", sts));
917 }
918 
919 static	int
920 lptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
921 {
922 	int	error = 0;
923 	struct lpt_data *sc = dev->si_drv1;
924 	device_t ppbus;
925 	u_char	old_sc_irq;	/* old printer IRQ status */
926 
927 	switch (cmd) {
928 	case LPT_IRQ :
929 		ppbus = device_get_parent(sc->sc_dev);
930 		ppb_lock(ppbus);
931 		if (sc->sc_irq & LP_HAS_IRQ) {
932 			/*
933 			 * NOTE:
934 			 * If the IRQ status is changed,
935 			 * this will only be visible on the
936 			 * next open.
937 			 *
938 			 * If interrupt status changes,
939 			 * this gets syslog'd.
940 			 */
941 			old_sc_irq = sc->sc_irq;
942 			switch (*(int*)data) {
943 			case 0:
944 				sc->sc_irq &= (~LP_ENABLE_IRQ);
945 				break;
946 			case 1:
947 				sc->sc_irq &= (~LP_ENABLE_EXT);
948 				sc->sc_irq |= LP_ENABLE_IRQ;
949 				break;
950 			case 2:
951 				/* classic irq based transfer and advanced
952 				 * modes are in conflict
953 				 */
954 				sc->sc_irq &= (~LP_ENABLE_IRQ);
955 				sc->sc_irq |= LP_ENABLE_EXT;
956 				break;
957 			case 3:
958 				sc->sc_irq &= (~LP_ENABLE_EXT);
959 				break;
960 			default:
961 				break;
962 			}
963 
964 			if (old_sc_irq != sc->sc_irq )
965 				log(LOG_NOTICE, "%s: switched to %s %s mode\n",
966 					device_get_nameunit(sc->sc_dev),
967 					(sc->sc_irq & LP_ENABLE_IRQ)?
968 					"interrupt-driven":"polled",
969 					(sc->sc_irq & LP_ENABLE_EXT)?
970 					"extended":"standard");
971 		} else /* polled port */
972 			error = EOPNOTSUPP;
973 		ppb_unlock(ppbus);
974 		break;
975 	default:
976 		error = ENODEV;
977 	}
978 
979 	return(error);
980 }
981 
982 static device_method_t lpt_methods[] = {
983 	/* device interface */
984 	DEVMETHOD(device_identify,	lpt_identify),
985 	DEVMETHOD(device_probe,		lpt_probe),
986 	DEVMETHOD(device_attach,	lpt_attach),
987 	DEVMETHOD(device_detach,	lpt_detach),
988 	{ 0, 0 }
989 };
990 
991 static driver_t lpt_driver = {
992 	LPT_NAME,
993 	lpt_methods,
994 	sizeof(struct lpt_data),
995 };
996 
997 DRIVER_MODULE(lpt, ppbus, lpt_driver, 0, 0);
998 MODULE_DEPEND(lpt, ppbus, 1, 1, 1);
999