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