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