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