xref: /freebsd/sys/dev/ppbus/lpt.c (revision a521f2116473fbd8c09db395518f060a27d02334)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1990 William F. Jolitz, TeleMuse
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This software is a component of "386BSD" developed by
18  *	William F. Jolitz, TeleMuse.
19  * 4. Neither the name of the developer nor the name "386BSD"
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
24  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
25  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
26  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
27  * NOT MAKE USE OF THIS WORK.
28  *
29  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
30  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
31  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
32  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
33  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
34  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
35  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
36  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  *
50  *	from: unknown origin, 386BSD 0.1
51  *	From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
52  *	From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
53  */
54 
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57 
58 /*
59  * Device Driver for AT parallel printer port
60  * Written by William Jolitz 12/18/90
61  */
62 
63 /*
64  * Updated for ppbus by Nicolas Souchu
65  * [Mon Jul 28 1997]
66  */
67 
68 #include "opt_lpt.h"
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/module.h>
73 #include <sys/bus.h>
74 #include <sys/conf.h>
75 #include <sys/kernel.h>
76 #include <sys/uio.h>
77 #include <sys/syslog.h>
78 #include <sys/malloc.h>
79 
80 #include <machine/bus.h>
81 #include <machine/resource.h>
82 #include <sys/rman.h>
83 
84 #include <dev/ppbus/lptio.h>
85 #include <dev/ppbus/ppbconf.h>
86 #include <dev/ppbus/ppb_1284.h>
87 #include <dev/ppbus/lpt.h>
88 #include "ppbus_if.h"
89 #include <dev/ppbus/ppbio.h>
90 
91 #ifndef LPT_DEBUG
92 #define	lprintf(args)
93 #else
94 #define	lprintf(args)						\
95 		do {						\
96 			if (lptflag)				\
97 				printf args;			\
98 		} while (0)
99 static int volatile lptflag = 1;
100 #endif
101 
102 #define	LPINITRDY	4	/* wait up to 4 seconds for a ready */
103 #define	LPTOUTINITIAL	10	/* initial timeout to wait for ready 1/10 s */
104 #define	LPTOUTMAX	1	/* maximal timeout 1 s */
105 #define	LPPRI		(PZERO+8)
106 #define	BUFSIZE		1024
107 #define	BUFSTATSIZE	32
108 
109 struct lpt_data {
110 	device_t sc_dev;
111 	struct cdev *sc_cdev;
112 	struct cdev *sc_cdev_bypass;
113 	short	sc_state;
114 	/* default case: negative prime, negative ack, handshake strobe,
115 	   prime once */
116 	u_char	sc_control;
117 	char	sc_flags;
118 #define	LP_POS_INIT	0x04	/* if we are a positive init signal */
119 #define	LP_POS_ACK	0x08	/* if we are a positive going ack */
120 #define	LP_NO_PRIME	0x10	/* don't prime the printer at all */
121 #define	LP_PRIMEOPEN	0x20	/* prime on every open */
122 #define	LP_AUTOLF	0x40	/* tell printer to do an automatic lf */
123 #define	LP_BYPASS	0x80	/* bypass  printer ready checks */
124 	void	*sc_inbuf;
125 	void	*sc_statbuf;
126 	short	sc_xfercnt ;
127 	char	sc_primed;
128 	char	*sc_cp ;
129 	u_short	sc_irq ;	/* IRQ status of port */
130 #define	LP_HAS_IRQ	0x01	/* we have an irq available */
131 #define	LP_USE_IRQ	0x02	/* we are using our irq */
132 #define	LP_ENABLE_IRQ	0x04	/* enable IRQ on open */
133 #define	LP_ENABLE_EXT	0x10	/* we shall use advanced mode when possible */
134 	u_char	sc_backoff ;	/* time to call lptout() again */
135 	struct callout sc_timer;
136 
137 	struct resource *sc_intr_resource;	/* interrupt resource */
138 	void	*sc_intr_cookie;		/* interrupt cookie */
139 };
140 
141 #define	LPT_NAME	"lpt"		/* our official name */
142 
143 static callout_func_t lptout;
144 static int	lpt_port_test(device_t dev, u_char data, u_char mask);
145 static int	lpt_detect(device_t dev);
146 
147 #define	DEVTOSOFTC(dev) \
148 	((struct lpt_data *)device_get_softc(dev))
149 
150 static void lptintr(void *arg);
151 
152 static devclass_t lpt_devclass;
153 
154 /* 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 #define	HAVEBUS		(1<<8)	/* the driver owns the bus */
164 
165 /* status masks to interrogate printer status */
166 #define	RDY_MASK	(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)	/* ready ? */
167 #define	LP_READY	(LPS_SEL|LPS_NBSY|LPS_NERR)
168 
169 /* Printer Ready condition  - from lpa.c */
170 /* Only used in polling code */
171 #define	LPS_INVERT	(LPS_NBSY | LPS_NACK |           LPS_SEL | LPS_NERR)
172 #define	LPS_MASK	(LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
173 #define	NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
174 
175 #define	MAX_SLEEP	(hz*5)	/* Timeout while waiting for device ready */
176 #define	MAX_SPIN	20	/* Max delay for device ready in usecs */
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 manuals 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 	device_t ppbus;
451 
452 	ppbus = device_get_parent(dev);
453 	ppb_assert_locked(ppbus);
454 	lprintf(("T %x ", ppb_rstr(ppbus)));
455 	if (sc->sc_state & OPEN) {
456 		sc->sc_backoff++;
457 		if (sc->sc_backoff > hz/LPTOUTMAX)
458 			sc->sc_backoff = hz/LPTOUTMAX;
459 		callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
460 	} else
461 		sc->sc_state &= ~TOUT;
462 
463 	if (sc->sc_state & EERROR)
464 		sc->sc_state &= ~EERROR;
465 
466 	/*
467 	 * Avoid possible hangs due to missed interrupts
468 	 */
469 	if (sc->sc_xfercnt) {
470 		lptintr(sc);
471 	} else {
472 		sc->sc_state &= ~OBUSY;
473 		wakeup(dev);
474 	}
475 }
476 
477 /*
478  * lptopen -- reset the printer, then wait until it's selected and not busy.
479  *	If LP_BYPASS flag is selected, then we do not try to select the
480  *	printer -- this is just used for passing ioctls.
481  */
482 
483 static	int
484 lptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
485 {
486 	int trys, err;
487 	struct lpt_data *sc = dev->si_drv1;
488 	device_t lptdev;
489 	device_t ppbus;
490 
491 	if (!sc)
492 		return (ENXIO);
493 
494 	lptdev = sc->sc_dev;
495 	ppbus = device_get_parent(lptdev);
496 
497 	ppb_lock(ppbus);
498 	if (sc->sc_state) {
499 		lprintf(("%s: still open %x\n", device_get_nameunit(lptdev),
500 		    sc->sc_state));
501 		ppb_unlock(ppbus);
502 		return(EBUSY);
503 	} else
504 		sc->sc_state |= LPTINIT;
505 
506 	sc->sc_flags = (uintptr_t)dev->si_drv2;
507 
508 	/* Check for open with BYPASS flag set. */
509 	if (sc->sc_flags & LP_BYPASS) {
510 		sc->sc_state = OPEN;
511 		ppb_unlock(ppbus);
512 		return(0);
513 	}
514 
515 	/* request the ppbus only if we don't have it already */
516 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
517 		/* give it a chance to try later */
518 		sc->sc_state = 0;
519 		ppb_unlock(ppbus);
520 		return (err);
521 	}
522 
523 	lprintf(("%s flags 0x%x\n", device_get_nameunit(lptdev),
524 	    sc->sc_flags));
525 
526 	/* set IRQ status according to ENABLE_IRQ flag
527 	 */
528 	if (sc->sc_irq & LP_ENABLE_IRQ)
529 		sc->sc_irq |= LP_USE_IRQ;
530 	else
531 		sc->sc_irq &= ~LP_USE_IRQ;
532 
533 	/* init printer */
534 	if ((sc->sc_flags & LP_NO_PRIME) == 0) {
535 		if ((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
536 			ppb_wctr(ppbus, 0);
537 			sc->sc_primed++;
538 			DELAY(500);
539 		}
540 	}
541 
542 	ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
543 
544 	/* wait till ready (printer running diagnostics) */
545 	trys = 0;
546 	do {
547 		/* ran out of waiting for the printer */
548 		if (trys++ >= LPINITRDY*4) {
549 			lprintf(("status %x\n", ppb_rstr(ppbus)));
550 
551 			lpt_release_ppbus(lptdev);
552 			sc->sc_state = 0;
553 			ppb_unlock(ppbus);
554 			return (EBUSY);
555 		}
556 
557 		/* wait 1/4 second, give up if we get a signal */
558 		if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lptinit",
559 		    hz / 4) != EWOULDBLOCK) {
560 			lpt_release_ppbus(lptdev);
561 			sc->sc_state = 0;
562 			ppb_unlock(ppbus);
563 			return (EBUSY);
564 		}
565 
566 		/* is printer online and ready for output */
567 	} while ((ppb_rstr(ppbus) &
568 			(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
569 					(LPS_SEL|LPS_NBSY|LPS_NERR));
570 
571 	sc->sc_control = LPC_SEL|LPC_NINIT;
572 	if (sc->sc_flags & LP_AUTOLF)
573 		sc->sc_control |= LPC_AUTOL;
574 
575 	/* enable interrupt if interrupt-driven */
576 	if (sc->sc_irq & LP_USE_IRQ)
577 		sc->sc_control |= LPC_ENA;
578 
579 	ppb_wctr(ppbus, sc->sc_control);
580 
581 	sc->sc_state &= ~LPTINIT;
582 	sc->sc_state |= OPEN;
583 	sc->sc_xfercnt = 0;
584 
585 	/* only use timeout if using interrupt */
586 	lprintf(("irq %x\n", sc->sc_irq));
587 	if (sc->sc_irq & LP_USE_IRQ) {
588 		sc->sc_state |= TOUT;
589 		sc->sc_backoff = hz / LPTOUTINITIAL;
590 		callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
591 	}
592 
593 	/* release the ppbus */
594 	lpt_release_ppbus(lptdev);
595 	ppb_unlock(ppbus);
596 
597 	lprintf(("opened.\n"));
598 	return(0);
599 }
600 
601 /*
602  * lptclose -- close the device, free the local line buffer.
603  *
604  * Check for interrupted write call added.
605  */
606 
607 static	int
608 lptclose(struct cdev *dev, int flags, int fmt, struct thread *td)
609 {
610 	struct lpt_data *sc = dev->si_drv1;
611 	device_t lptdev = sc->sc_dev;
612 	device_t ppbus = device_get_parent(lptdev);
613 	int err;
614 
615 	ppb_lock(ppbus);
616 	if (sc->sc_flags & LP_BYPASS)
617 		goto end_close;
618 
619 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
620 		ppb_unlock(ppbus);
621 		return (err);
622 	}
623 
624 	/* if the last write was interrupted, don't complete it */
625 	if ((!(sc->sc_state  & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
626 		while ((ppb_rstr(ppbus) &
627 			(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
628 			(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
629 			/* wait 1 second, give up if we get a signal */
630 			if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lpclose",
631 			    hz) != EWOULDBLOCK)
632 				break;
633 
634 	sc->sc_state &= ~OPEN;
635 	callout_stop(&sc->sc_timer);
636 	ppb_wctr(ppbus, LPC_NINIT);
637 
638 	/*
639 	 * unregistration of interrupt forced by release
640 	 */
641 	lpt_release_ppbus(lptdev);
642 
643 end_close:
644 	sc->sc_state = 0;
645 	sc->sc_xfercnt = 0;
646 	ppb_unlock(ppbus);
647 	lprintf(("closed.\n"));
648 	return(0);
649 }
650 
651 /*
652  * lpt_pushbytes()
653  *	Workhorse for actually spinning and writing bytes to printer
654  *	Derived from lpa.c
655  *	Originally by ?
656  *
657  *	This code is only used when we are polling the port
658  */
659 static int
660 lpt_pushbytes(struct lpt_data *sc)
661 {
662 	device_t dev = sc->sc_dev;
663 	device_t ppbus = device_get_parent(dev);
664 	int spin, err, tic;
665 	char ch;
666 
667 	ppb_assert_locked(ppbus);
668 	lprintf(("p"));
669 	/* loop for every character .. */
670 	while (sc->sc_xfercnt > 0) {
671 		/* printer data */
672 		ch = *(sc->sc_cp);
673 		sc->sc_cp++;
674 		sc->sc_xfercnt--;
675 
676 		/*
677 		 * Wait for printer ready.
678 		 * Loop 20 usecs testing BUSY bit, then sleep
679 		 * for exponentially increasing timeout. (vak)
680 		 */
681 		for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
682 			DELAY(1);	/* XXX delay is NOT this accurate! */
683 		if (spin >= MAX_SPIN) {
684 			tic = 0;
685 			while (NOT_READY(ppbus)) {
686 				/*
687 				 * Now sleep, every cycle a
688 				 * little longer ..
689 				 */
690 				tic = tic + tic + 1;
691 				/*
692 				 * But no more than 10 seconds. (vak)
693 				 */
694 				if (tic > MAX_SLEEP)
695 					tic = MAX_SLEEP;
696 				err = ppb_sleep(ppbus, dev, LPPRI,
697 					LPT_NAME "poll", tic);
698 				if (err != EWOULDBLOCK) {
699 					return (err);
700 				}
701 			}
702 		}
703 
704 		/* output data */
705 		ppb_wdtr(ppbus, ch);
706 		/* strobe */
707 		ppb_wctr(ppbus, sc->sc_control|LPC_STB);
708 		ppb_wctr(ppbus, sc->sc_control);
709 	}
710 	return(0);
711 }
712 
713 /*
714  * lptread --retrieve printer status in IEEE1284 NIBBLE mode
715  */
716 
717 static int
718 lptread(struct cdev *dev, struct uio *uio, int ioflag)
719 {
720 	struct lpt_data *sc = dev->si_drv1;
721 	device_t lptdev = sc->sc_dev;
722 	device_t ppbus = device_get_parent(lptdev);
723 	int error = 0, len;
724 
725 	if (sc->sc_flags & LP_BYPASS) {
726 		/* we can't do reads in bypass mode */
727 		return (EPERM);
728 	}
729 
730 	ppb_lock(ppbus);
731 	if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) {
732 		ppb_unlock(ppbus);
733 		return (error);
734 	}
735 
736 	/* read data in an other buffer, read/write may be simultaneous */
737 	len = 0;
738 	while (uio->uio_resid) {
739 		if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
740 				sc->sc_statbuf, min(BUFSTATSIZE,
741 				uio->uio_resid), &len))) {
742 			goto error;
743 		}
744 
745 		if (!len)
746 			goto error;		/* no more data */
747 
748 		ppb_unlock(ppbus);
749 		error = uiomove(sc->sc_statbuf, len, uio);
750 		ppb_lock(ppbus);
751 		if (error)
752 			goto error;
753 	}
754 
755 error:
756 	ppb_1284_terminate(ppbus);
757 	ppb_unlock(ppbus);
758 	return (error);
759 }
760 
761 /*
762  * lptwrite --copy a line from user space to a local buffer, then call
763  * putc to get the chars moved to the output queue.
764  *
765  * Flagging of interrupted write added.
766  */
767 
768 static	int
769 lptwrite(struct cdev *dev, struct uio *uio, int ioflag)
770 {
771 	register unsigned n;
772 	int err;
773 	struct lpt_data *sc = dev->si_drv1;
774 	device_t lptdev = sc->sc_dev;
775 	device_t ppbus = device_get_parent(lptdev);
776 
777 	if (sc->sc_flags & LP_BYPASS) {
778 		/* we can't do writes in bypass mode */
779 		return (EPERM);
780 	}
781 
782 	/* request the ppbus only if we don't have it already */
783 	ppb_lock(ppbus);
784 	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
785 		ppb_unlock(ppbus);
786 		return (err);
787 	}
788 
789 	sc->sc_state &= ~INTERRUPTED;
790 	while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
791 		sc->sc_cp = sc->sc_inbuf;
792 		ppb_unlock(ppbus);
793 		err = uiomove(sc->sc_cp, n, uio);
794 		ppb_lock(ppbus);
795 		if (err)
796 			break;
797 		sc->sc_xfercnt = n;
798 
799 		if (sc->sc_irq & LP_ENABLE_EXT) {
800 			/* try any extended mode */
801 			err = ppb_write(ppbus, sc->sc_cp,
802 					sc->sc_xfercnt, 0);
803 			switch (err) {
804 			case 0:
805 				/* if not all data was sent, we could rely
806 				 * on polling for the last bytes */
807 				sc->sc_xfercnt = 0;
808 				break;
809 			case EINTR:
810 				sc->sc_state |= INTERRUPTED;
811 				ppb_unlock(ppbus);
812 				return (err);
813 			case EINVAL:
814 				/* advanced mode not avail */
815 				log(LOG_NOTICE,
816 				    "%s: advanced mode not avail, polling\n",
817 				    device_get_nameunit(sc->sc_dev));
818 				break;
819 			default:
820 				ppb_unlock(ppbus);
821 				return (err);
822 			}
823 		} else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
824 			lprintf(("i"));
825 			/* if the printer is ready for a char, */
826 			/* give it one */
827 			if ((sc->sc_state & OBUSY) == 0){
828 				lprintf(("\nC %d. ", sc->sc_xfercnt));
829 				lptintr(sc);
830 			}
831 			lprintf(("W "));
832 			if (sc->sc_state & OBUSY)
833 				if ((err = ppb_sleep(ppbus, lptdev,
834 					 LPPRI|PCATCH, LPT_NAME "write", 0))) {
835 					sc->sc_state |= INTERRUPTED;
836 					ppb_unlock(ppbus);
837 					return(err);
838 				}
839 		}
840 
841 		/* check to see if we must do a polled write */
842 		if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
843 			lprintf(("p"));
844 
845 			err = lpt_pushbytes(sc);
846 
847 			if (err) {
848 				ppb_unlock(ppbus);
849 				return (err);
850 			}
851 		}
852 	}
853 
854 	/* we have not been interrupted, release the ppbus */
855 	lpt_release_ppbus(lptdev);
856 	ppb_unlock(ppbus);
857 
858 	return (err);
859 }
860 
861 /*
862  * lptintr -- handle printer interrupts which occur when the printer is
863  * ready to accept another char.
864  *
865  * do checking for interrupted write call.
866  */
867 static void
868 lptintr(void *arg)
869 {
870 	struct lpt_data *sc = arg;
871 	device_t lptdev = sc->sc_dev;
872 	device_t ppbus = device_get_parent(lptdev);
873 	int sts = 0;
874 	int i;
875 
876 	/*
877 	 * Is printer online and ready for output?
878 	 *
879 	 * Avoid falling back to lptout() too quickly.  First spin-loop
880 	 * to see if the printer will become ready ``really soon now''.
881 	 */
882 	for (i = 0; i < 100 &&
883 	     ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
884 
885 	if ((sts & RDY_MASK) == LP_READY) {
886 		sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
887 		sc->sc_backoff = hz / LPTOUTINITIAL;
888 
889 		if (sc->sc_xfercnt) {
890 			/* send char */
891 			/*lprintf(("%x ", *sc->sc_cp)); */
892 			ppb_wdtr(ppbus, *sc->sc_cp++) ;
893 			ppb_wctr(ppbus, sc->sc_control|LPC_STB);
894 			/* DELAY(X) */
895 			ppb_wctr(ppbus, sc->sc_control);
896 
897 			/* any more data for printer */
898 			if (--(sc->sc_xfercnt) > 0)
899 				return;
900 		}
901 
902 		/*
903 		 * No more data waiting for printer.
904 		 * Wakeup is not done if write call was not interrupted.
905 		 */
906 		sc->sc_state &= ~OBUSY;
907 
908 		if (!(sc->sc_state & INTERRUPTED))
909 			wakeup(lptdev);
910 		lprintf(("w "));
911 		return;
912 	} else	{	/* check for error */
913 		if (((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
914 				(sc->sc_state & OPEN))
915 			sc->sc_state |= EERROR;
916 		/* lptout() will jump in and try to restart. */
917 	}
918 	lprintf(("sts %x ", sts));
919 }
920 
921 static	int
922 lptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
923 {
924 	int	error = 0;
925 	struct lpt_data *sc = dev->si_drv1;
926 	device_t ppbus;
927 	u_char	old_sc_irq;	/* old printer IRQ status */
928 
929 	switch (cmd) {
930 	case LPT_IRQ :
931 		ppbus = device_get_parent(sc->sc_dev);
932 		ppb_lock(ppbus);
933 		if (sc->sc_irq & LP_HAS_IRQ) {
934 			/*
935 			 * NOTE:
936 			 * If the IRQ status is changed,
937 			 * this will only be visible on the
938 			 * next open.
939 			 *
940 			 * If interrupt status changes,
941 			 * this gets syslog'd.
942 			 */
943 			old_sc_irq = sc->sc_irq;
944 			switch (*(int*)data) {
945 			case 0:
946 				sc->sc_irq &= (~LP_ENABLE_IRQ);
947 				break;
948 			case 1:
949 				sc->sc_irq &= (~LP_ENABLE_EXT);
950 				sc->sc_irq |= LP_ENABLE_IRQ;
951 				break;
952 			case 2:
953 				/* classic irq based transfer and advanced
954 				 * modes are in conflict
955 				 */
956 				sc->sc_irq &= (~LP_ENABLE_IRQ);
957 				sc->sc_irq |= LP_ENABLE_EXT;
958 				break;
959 			case 3:
960 				sc->sc_irq &= (~LP_ENABLE_EXT);
961 				break;
962 			default:
963 				break;
964 			}
965 
966 			if (old_sc_irq != sc->sc_irq )
967 				log(LOG_NOTICE, "%s: switched to %s %s mode\n",
968 					device_get_nameunit(sc->sc_dev),
969 					(sc->sc_irq & LP_ENABLE_IRQ)?
970 					"interrupt-driven":"polled",
971 					(sc->sc_irq & LP_ENABLE_EXT)?
972 					"extended":"standard");
973 		} else /* polled port */
974 			error = EOPNOTSUPP;
975 		ppb_unlock(ppbus);
976 		break;
977 	default:
978 		error = ENODEV;
979 	}
980 
981 	return(error);
982 }
983 
984 static device_method_t lpt_methods[] = {
985 	/* device interface */
986 	DEVMETHOD(device_identify,	lpt_identify),
987 	DEVMETHOD(device_probe,		lpt_probe),
988 	DEVMETHOD(device_attach,	lpt_attach),
989 	DEVMETHOD(device_detach,	lpt_detach),
990 	{ 0, 0 }
991 };
992 
993 static driver_t lpt_driver = {
994 	LPT_NAME,
995 	lpt_methods,
996 	sizeof(struct lpt_data),
997 };
998 
999 DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0);
1000 MODULE_DEPEND(lpt, ppbus, 1, 1, 1);
1001