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