lpt.c revision 67164
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: head/sys/dev/ppbus/lpt.c 67164 2000-10-15 14:19:01Z phk $
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#include "opt_lpt.h"
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/module.h>
69#include <sys/bus.h>
70#include <sys/conf.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/bus.h>
77#include <machine/resource.h>
78#include <sys/rman.h>
79
80#include <dev/ppbus/lptio.h>
81#include <dev/ppbus/ppbconf.h>
82#include <dev/ppbus/ppb_1284.h>
83#include <dev/ppbus/lpt.h>
84#include "ppbus_if.h"
85#include <dev/ppbus/ppbio.h>
86
87MALLOC_DEFINE(M_LPT, "lpt", "LPT buffers");
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)
97static 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#define	LPTUNIT(s)	((s)&0x03)
108#define	LPTFLAGS(s)	((s)&0xfc)
109
110struct lpt_data {
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	void	*sc_inbuf;
124	void	*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	struct resource *intr_resource;	/* interrupt resource */
136	void *intr_cookie;		/* interrupt registration cookie */
137
138};
139
140#define LPT_NAME	"lpt"		/* our official name */
141
142static timeout_t lptout;
143static int	lpt_port_test(device_t dev, u_char data, u_char mask);
144static int	lpt_detect(device_t dev);
145
146#define DEVTOSOFTC(dev) \
147	((struct lpt_data *)device_get_softc(dev))
148#define UNITOSOFTC(unit) \
149	((struct lpt_data *)devclass_get_softc(lpt_devclass, (unit)))
150#define UNITODEVICE(unit) \
151	(devclass_get_device(lpt_devclass, (unit)))
152
153static void lptintr(device_t dev);
154static void lpt_intr(void *arg);	/* without spls */
155
156static devclass_t lpt_devclass;
157
158
159/* bits for state */
160#define	OPEN		(1<<0)	/* device is open */
161#define	ASLP		(1<<1)	/* awaiting draining of printer */
162#define	EERROR		(1<<2)	/* error was received from printer */
163#define	OBUSY		(1<<3)	/* printer is busy doing output */
164#define LPTOUT		(1<<4)	/* timeout while not selected */
165#define TOUT		(1<<5)	/* timeout while not selected */
166#define LPTINIT		(1<<6)	/* waiting to initialize for open */
167#define INTERRUPTED	(1<<7)	/* write call was interrupted */
168
169#define HAVEBUS		(1<<8)	/* the driver owns the bus */
170
171
172/* status masks to interrogate printer status */
173#define RDY_MASK	(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)	/* ready ? */
174#define LP_READY	(LPS_SEL|LPS_NBSY|LPS_NERR)
175
176/* Printer Ready condition  - from lpa.c */
177/* Only used in polling code */
178#define	LPS_INVERT	(LPS_NBSY | LPS_NACK |           LPS_SEL | LPS_NERR)
179#define	LPS_MASK	(LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
180#define	NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
181
182#define	MAX_SLEEP	(hz*5)	/* Timeout while waiting for device ready */
183#define	MAX_SPIN	20	/* Max delay for device ready in usecs */
184
185
186static	d_open_t	lptopen;
187static	d_close_t	lptclose;
188static	d_write_t	lptwrite;
189static	d_read_t	lptread;
190static	d_ioctl_t	lptioctl;
191
192#define CDEV_MAJOR 16
193static struct cdevsw lpt_cdevsw = {
194	/* open */	lptopen,
195	/* close */	lptclose,
196	/* read */	lptread,
197	/* write */	lptwrite,
198	/* ioctl */	lptioctl,
199	/* poll */	nopoll,
200	/* mmap */	nommap,
201	/* strategy */	nostrategy,
202	/* name */	LPT_NAME,
203	/* maj */	CDEV_MAJOR,
204	/* dump */	nodump,
205	/* psize */	nopsize,
206	/* flags */	0,
207	/* bmaj */	-1
208};
209
210static int
211lpt_request_ppbus(device_t dev, int how)
212{
213	device_t ppbus = device_get_parent(dev);
214	struct lpt_data *sc = DEVTOSOFTC(dev);
215	int error;
216
217	if (sc->sc_state & HAVEBUS)
218		return (0);
219
220	/* we have the bus only if the request succeded */
221	if ((error = ppb_request_bus(ppbus, dev, how)) == 0)
222		sc->sc_state |= HAVEBUS;
223
224	return (error);
225}
226
227static int
228lpt_release_ppbus(device_t dev)
229{
230	device_t ppbus = device_get_parent(dev);
231	struct lpt_data *sc = DEVTOSOFTC(dev);
232	int error = 0;
233
234	if ((error = ppb_release_bus(ppbus, dev)) == 0)
235		sc->sc_state &= ~HAVEBUS;
236
237	return (error);
238}
239
240/*
241 * Internal routine to lptprobe to do port tests of one byte value
242 */
243static int
244lpt_port_test(device_t ppbus, u_char data, u_char mask)
245{
246	int	temp, timeout;
247
248	data = data & mask;
249	ppb_wdtr(ppbus, data);
250	timeout = 10000;
251	do {
252		DELAY(10);
253		temp = ppb_rdtr(ppbus) & mask;
254	}
255	while (temp != data && --timeout);
256	lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
257	return (temp == data);
258}
259
260/*
261 * Probe simplified by replacing multiple loops with a hardcoded
262 * test pattern - 1999/02/08 des@freebsd.org
263 *
264 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
265 * Based partially on Rod Grimes' printer probe
266 *
267 * Logic:
268 *	1) If no port address was given, use the bios detected ports
269 *	   and autodetect what ports the printers are on.
270 *	2) Otherwise, probe the data port at the address given,
271 *	   using the method in Rod Grimes' port probe.
272 *	   (Much code ripped off directly from Rod's probe.)
273 *
274 * Comments from Rod's probe:
275 * Logic:
276 *	1) You should be able to write to and read back the same value
277 *	   to the data port.  Do an alternating zeros, alternating ones,
278 *	   walking zero, and walking one test to check for stuck bits.
279 *
280 *	2) You should be able to write to and read back the same value
281 *	   to the control port lower 5 bits, the upper 3 bits are reserved
282 *	   per the IBM PC technical reference manauls and different boards
283 *	   do different things with them.  Do an alternating zeros, alternating
284 *	   ones, walking zero, and walking one test to check for stuck bits.
285 *
286 *	   Some printers drag the strobe line down when the are powered off
287 * 	   so this bit has been masked out of the control port test.
288 *
289 *	   XXX Some printers may not like a fast pulse on init or strobe, I
290 *	   don't know at this point, if that becomes a problem these bits
291 *	   should be turned off in the mask byte for the control port test.
292 *
293 *	   We are finally left with a mask of 0x14, due to some printers
294 *	   being adamant about holding other bits high ........
295 *
296 *	   Before probing the control port, we write a 0 to the data port -
297 *	   If not, some printers chuck out garbage when the strobe line
298 *	   gets toggled.
299 *
300 *	3) Set the data and control ports to a value of 0
301 *
302 *	This probe routine has been tested on Epson Lx-800, HP LJ3P,
303 *	Epson FX-1170 and C.Itoh 8510RM
304 *	printers.
305 *	Quick exit on fail added.
306 */
307static int
308lpt_detect(device_t dev)
309{
310	device_t ppbus = device_get_parent(dev);
311
312	static u_char	testbyte[18] = {
313		0x55,			/* alternating zeros */
314		0xaa,			/* alternating ones */
315		0xfe, 0xfd, 0xfb, 0xf7,
316		0xef, 0xdf, 0xbf, 0x7f,	/* walking zero */
317		0x01, 0x02, 0x04, 0x08,
318		0x10, 0x20, 0x40, 0x80	/* walking one */
319	};
320	int		i, error, status;
321
322	status = 1;				/* assume success */
323
324	if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
325		printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
326		status = 0;
327		goto end_probe;
328	}
329
330	for (i = 0; i < 18 && status; i++)
331		if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
332			status = 0;
333			goto end_probe;
334		}
335
336end_probe:
337	/* write 0's to control and data ports */
338	ppb_wdtr(ppbus, 0);
339	ppb_wctr(ppbus, 0);
340
341	lpt_release_ppbus(dev);
342
343	return (status);
344}
345
346static void
347lpt_identify(driver_t *driver, device_t parent)
348{
349
350	BUS_ADD_CHILD(parent, 0, LPT_NAME, 0);
351}
352
353/*
354 * lpt_probe()
355 */
356static int
357lpt_probe(device_t dev)
358{
359	struct lpt_data *sc;
360
361	sc = DEVTOSOFTC(dev);
362	bzero(sc, sizeof(struct lpt_data));
363
364	/*
365	 * Now, try to detect the printer.
366	 */
367	if (!lpt_detect(dev))
368		return (ENXIO);
369
370	device_set_desc(dev, "Printer");
371
372	return (0);
373}
374
375static int
376lpt_attach(device_t dev)
377{
378	device_t ppbus = device_get_parent(dev);
379	struct lpt_data *sc = DEVTOSOFTC(dev);
380	int zero = 0, unit = device_get_unit(dev);
381	int error;
382	uintptr_t irq;
383
384	sc->sc_primed = 0;	/* not primed yet */
385
386	if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
387		printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
388		return (0);
389	}
390
391	ppb_wctr(ppbus, LPC_NINIT);
392
393	/* check if we can use interrupt, should be done by ppc stuff */
394	lprintf(("oldirq %x\n", sc->sc_irq));
395
396	/* retrieve the ppbus irq */
397	BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
398
399	if (irq > 0) {
400		/* declare our interrupt handler */
401		sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
402						       &zero, irq, irq, 1, RF_SHAREABLE);
403	}
404	if (sc->intr_resource) {
405		sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
406		device_printf(dev, "Interrupt-driven port\n");
407	} else {
408		sc->sc_irq = 0;
409		device_printf(dev, "Polled port\n");
410	}
411	lprintf(("irq %x %x\n", irq, sc->sc_irq));
412
413	lpt_release_ppbus(dev);
414
415	make_dev(&lpt_cdevsw, unit,
416	    UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
417	make_dev(&lpt_cdevsw, unit | LP_BYPASS,
418	    UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
419	return (0);
420}
421
422static void
423lptout(void *arg)
424{
425	device_t dev = (device_t)arg;
426	struct lpt_data *sc = DEVTOSOFTC(dev);
427#ifdef LPT_DEBUG
428	device_t ppbus = device_get_parent(dev);
429#endif
430
431	lprintf(("T %x ", ppb_rstr(ppbus)));
432	if (sc->sc_state & OPEN) {
433		sc->sc_backoff++;
434		if (sc->sc_backoff > hz/LPTOUTMAX)
435			sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
436		timeout(lptout, (caddr_t)dev, sc->sc_backoff);
437	} else
438		sc->sc_state &= ~TOUT;
439
440	if (sc->sc_state & EERROR)
441		sc->sc_state &= ~EERROR;
442
443	/*
444	 * Avoid possible hangs due to missed interrupts
445	 */
446	if (sc->sc_xfercnt) {
447		lptintr(dev);
448	} else {
449		sc->sc_state &= ~OBUSY;
450		wakeup((caddr_t)dev);
451	}
452}
453
454/*
455 * lptopen -- reset the printer, then wait until it's selected and not busy.
456 *	If LP_BYPASS flag is selected, then we do not try to select the
457 *	printer -- this is just used for passing ioctls.
458 */
459
460static	int
461lptopen(dev_t dev, int flags, int fmt, struct proc *p)
462{
463	int s;
464	int trys, err;
465	u_int unit = LPTUNIT(minor(dev));
466	struct lpt_data *sc = UNITOSOFTC(unit);
467	device_t lptdev = UNITODEVICE(unit);
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 = LPTFLAGS(minor(dev));
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((caddr_t)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_LPT, M_WAITOK);
555	sc->sc_statbuf = malloc(BUFSTATSIZE, M_LPT, 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
580static	int
581lptclose(dev_t dev, int flags, int fmt, struct proc *p)
582{
583	u_int unit = LPTUNIT(minor(dev));
584	struct lpt_data *sc = UNITOSOFTC(unit);
585	device_t lptdev = UNITODEVICE(unit);
586        device_t ppbus = device_get_parent(lptdev);
587	int err;
588
589	if(sc->sc_flags & LP_BYPASS)
590		goto end_close;
591
592	if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
593		return (err);
594
595	sc->sc_state &= ~OPEN;
596
597	/* if the last write was interrupted, don't complete it */
598	if((!(sc->sc_state  & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
599		while ((ppb_rstr(ppbus) &
600			(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
601			(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
602			/* wait 1/4 second, give up if we get a signal */
603			if (tsleep((caddr_t)lptdev, LPPRI|PCATCH,
604				"lpclose", hz) != EWOULDBLOCK)
605				break;
606
607	ppb_wctr(ppbus, LPC_NINIT);
608	free(sc->sc_inbuf, M_LPT);
609	free(sc->sc_statbuf, M_LPT);
610
611end_close:
612	/* release the bus anyway
613	 * unregistration of interrupt forced by release
614	 */
615	lpt_release_ppbus(lptdev);
616
617	sc->sc_state = 0;
618	sc->sc_xfercnt = 0;
619	lprintf(("closed.\n"));
620	return(0);
621}
622
623/*
624 * lpt_pushbytes()
625 *	Workhorse for actually spinning and writing bytes to printer
626 *	Derived from lpa.c
627 *	Originally by ?
628 *
629 *	This code is only used when we are polling the port
630 */
631static int
632lpt_pushbytes(device_t dev)
633{
634	struct lpt_data *sc = DEVTOSOFTC(dev);
635	device_t ppbus = device_get_parent(dev);
636	int spin, err, tic;
637	char ch;
638
639	lprintf(("p"));
640	/* loop for every character .. */
641	while (sc->sc_xfercnt > 0) {
642		/* printer data */
643		ch = *(sc->sc_cp);
644		sc->sc_cp++;
645		sc->sc_xfercnt--;
646
647		/*
648		 * Wait for printer ready.
649		 * Loop 20 usecs testing BUSY bit, then sleep
650		 * for exponentially increasing timeout. (vak)
651		 */
652		for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
653			DELAY(1);	/* XXX delay is NOT this accurate! */
654		if (spin >= MAX_SPIN) {
655			tic = 0;
656			while (NOT_READY(ppbus)) {
657				/*
658				 * Now sleep, every cycle a
659				 * little longer ..
660				 */
661				tic = tic + tic + 1;
662				/*
663				 * But no more than 10 seconds. (vak)
664				 */
665				if (tic > MAX_SLEEP)
666					tic = MAX_SLEEP;
667				err = tsleep((caddr_t)dev, LPPRI,
668					LPT_NAME "poll", tic);
669				if (err != EWOULDBLOCK) {
670					return (err);
671				}
672			}
673		}
674
675		/* output data */
676		ppb_wdtr(ppbus, ch);
677		/* strobe */
678		ppb_wctr(ppbus, sc->sc_control|LPC_STB);
679		ppb_wctr(ppbus, sc->sc_control);
680
681	}
682	return(0);
683}
684
685/*
686 * lptread --retrieve printer status in IEEE1284 NIBBLE mode
687 */
688
689static int
690lptread(dev_t dev, struct uio *uio, int ioflag)
691{
692        u_int	unit = LPTUNIT(minor(dev));
693	struct lpt_data *sc = UNITOSOFTC(unit);
694	device_t lptdev = UNITODEVICE(unit);
695        device_t ppbus = device_get_parent(lptdev);
696	int error = 0, len;
697
698	if (sc->sc_flags & LP_BYPASS) {
699		/* we can't do reads in bypass mode */
700		return (EPERM);
701	}
702
703	if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0)))
704		return (error);
705
706	/* read data in an other buffer, read/write may be simultaneous */
707	len = 0;
708	while (uio->uio_resid) {
709		if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
710				sc->sc_statbuf, min(BUFSTATSIZE,
711				uio->uio_resid), &len))) {
712			goto error;
713		}
714
715		if (!len)
716			goto error;		/* no more data */
717
718		if ((error = uiomove(sc->sc_statbuf, len, uio)))
719			goto error;
720	}
721
722error:
723	ppb_1284_terminate(ppbus);
724	return (error);
725}
726
727/*
728 * lptwrite --copy a line from user space to a local buffer, then call
729 * putc to get the chars moved to the output queue.
730 *
731 * Flagging of interrupted write added.
732 */
733
734static	int
735lptwrite(dev_t dev, struct uio *uio, int ioflag)
736{
737	register 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			       INTR_TYPE_TTY, lpt_intr, lptdev,
759			       &sc->intr_cookie);
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					 LPPRI|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
833static void
834lpt_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
890static void
891lptintr(device_t dev)
892{
893	/* call the interrupt at required spl level */
894	int s = spltty();
895
896	lpt_intr(dev);
897
898	splx(s);
899	return;
900}
901
902static	int
903lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
904{
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 (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*)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
962static device_method_t lpt_methods[] = {
963	/* device interface */
964	DEVMETHOD(device_identify,	lpt_identify),
965	DEVMETHOD(device_probe,		lpt_probe),
966	DEVMETHOD(device_attach,	lpt_attach),
967
968	{ 0, 0 }
969};
970
971static driver_t lpt_driver = {
972	LPT_NAME,
973	lpt_methods,
974	sizeof(struct lpt_data),
975};
976
977DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0);
978