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