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