xref: /netbsd-src/sys/dev/ic/lpt.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: lpt.c,v 1.30 1995/04/17 12:09:17 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1993, 1994 Charles Hannum.
5  * Copyright (c) 1990 William F. Jolitz, TeleMuse
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This software is a component of "386BSD" developed by
19  *	William F. Jolitz, TeleMuse.
20  * 4. Neither the name of the developer nor the name "386BSD"
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
25  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
26  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
27  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
28  * NOT MAKE USE OF THIS WORK.
29  *
30  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
31  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
32  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
33  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
34  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
35  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
36  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
37  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  */
51 
52 /*
53  * Device Driver for AT parallel printer port
54  */
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/user.h>
60 #include <sys/buf.h>
61 #include <sys/kernel.h>
62 #include <sys/ioctl.h>
63 #include <sys/uio.h>
64 #include <sys/device.h>
65 #include <sys/syslog.h>
66 
67 #include <machine/cpu.h>
68 #include <machine/pio.h>
69 
70 #include <dev/isa/isavar.h>
71 #include <dev/isa/lptreg.h>
72 
73 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
74 #define	STEP		hz/4
75 
76 #define	LPTPRI		(PZERO+8)
77 #define	LPT_BSIZE	1024
78 
79 #if !defined(DEBUG) || !defined(notdef)
80 #define lprintf
81 #else
82 #define lprintf		if (lptdebug) printf
83 int lptdebug = 1;
84 #endif
85 
86 struct lpt_softc {
87 	struct device sc_dev;
88 	void *sc_ih;
89 
90 	size_t sc_count;
91 	struct buf *sc_inbuf;
92 	u_char *sc_cp;
93 	int sc_spinmax;
94 	int sc_iobase;
95 	int sc_irq;
96 	u_char sc_state;
97 #define	LPT_OPEN	0x01	/* device is open */
98 #define	LPT_OBUSY	0x02	/* printer is busy doing output */
99 #define	LPT_INIT	0x04	/* waiting to initialize for open */
100 	u_char sc_flags;
101 #define	LPT_AUTOLF	0x20	/* automatic LF on CR */
102 #define	LPT_NOPRIME	0x40	/* don't prime on open */
103 #define	LPT_NOINTR	0x80	/* do not use interrupt */
104 	u_char sc_control;
105 	u_char sc_laststatus;
106 };
107 
108 int lptprobe __P((struct device *, void *, void *));
109 void lptattach __P((struct device *, struct device *, void *));
110 int lptintr __P((void *));
111 
112 struct cfdriver lptcd = {
113 	NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc)
114 };
115 
116 #define	LPTUNIT(s)	(minor(s) & 0x1f)
117 #define	LPTFLAGS(s)	(minor(s) & 0xe0)
118 
119 #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
120 #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
121 #define	NOT_READY()	((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK)
122 #define	NOT_READY_ERR()	not_ready(inb(iobase + lpt_status), sc)
123 static int not_ready __P((u_char, struct lpt_softc *));
124 
125 static void lptwakeup __P((void *arg));
126 static int pushbytes __P((struct lpt_softc *));
127 
128 /*
129  * Internal routine to lptprobe to do port tests of one byte value.
130  */
131 int
132 lpt_port_test(port, data, mask)
133 	int port;
134 	u_char data, mask;
135 {
136 	int timeout;
137 	u_char temp;
138 
139 	data &= mask;
140 	outb(port, data);
141 	timeout = 1000;
142 	do {
143 		delay(10);
144 		temp = inb(port) & mask;
145 	} while (temp != data && --timeout);
146 	lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", port, data,
147 	    temp, timeout);
148 	return (temp == data);
149 }
150 
151 /*
152  * Logic:
153  *	1) You should be able to write to and read back the same value
154  *	   to the data port.  Do an alternating zeros, alternating ones,
155  *	   walking zero, and walking one test to check for stuck bits.
156  *
157  *	2) You should be able to write to and read back the same value
158  *	   to the control port lower 5 bits, the upper 3 bits are reserved
159  *	   per the IBM PC technical reference manauls and different boards
160  *	   do different things with them.  Do an alternating zeros, alternating
161  *	   ones, walking zero, and walking one test to check for stuck bits.
162  *
163  *	   Some printers drag the strobe line down when the are powered off
164  * 	   so this bit has been masked out of the control port test.
165  *
166  *	   XXX Some printers may not like a fast pulse on init or strobe, I
167  *	   don't know at this point, if that becomes a problem these bits
168  *	   should be turned off in the mask byte for the control port test.
169  *
170  *	3) Set the data and control ports to a value of 0
171  */
172 int
173 lptprobe(parent, match, aux)
174 	struct device *parent;
175 	void *match, *aux;
176 {
177 	struct isa_attach_args *ia = aux;
178 	int iobase = ia->ia_iobase;
179 	int port;
180 	u_char mask, data;
181 	int i;
182 
183 #ifdef DEBUG
184 #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
185 		    return 0;} while (0)
186 #else
187 #define	ABORT	return 0
188 #endif
189 
190 	port = iobase + lpt_data;
191 	mask = 0xff;
192 
193 	data = 0x55;				/* Alternating zeros */
194 	if (!lpt_port_test(port, data, mask))
195 		ABORT;
196 
197 	data = 0xaa;				/* Alternating ones */
198 	if (!lpt_port_test(port, data, mask))
199 		ABORT;
200 
201 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
202 		data = ~(1 << i);
203 		if (!lpt_port_test(port, data, mask))
204 			ABORT;
205 	}
206 
207 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
208 		data = (1 << i);
209 		if (!lpt_port_test(port, data, mask))
210 			ABORT;
211 	}
212 
213 	outb(iobase + lpt_data, 0);
214 	outb(iobase + lpt_control, 0);
215 
216 	ia->ia_iosize = LPT_NPORTS;
217 	ia->ia_msize = 0;
218 	return 1;
219 }
220 
221 void
222 lptattach(parent, self, aux)
223 	struct device *parent, *self;
224 	void *aux;
225 {
226 	struct lpt_softc *sc = (void *)self;
227 	struct isa_attach_args *ia = aux;
228 	int iobase = ia->ia_iobase;
229 
230 	if (ia->ia_irq != IRQUNK)
231 		printf("\n");
232 	else
233 		printf(": polled\n");
234 
235 	sc->sc_iobase = iobase;
236 	sc->sc_irq = ia->ia_irq;
237 	sc->sc_state = 0;
238 	outb(iobase + lpt_control, LPC_NINIT);
239 
240 	if (ia->ia_irq != IRQUNK)
241 		sc->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE,
242 		    ISA_IPL_NONE, lptintr, sc);
243 }
244 
245 /*
246  * Reset the printer, then wait until it's selected and not busy.
247  */
248 int
249 lptopen(dev, flag)
250 	dev_t dev;
251 	int flag;
252 {
253 	int unit = LPTUNIT(dev);
254 	u_char flags = LPTFLAGS(dev);
255 	struct lpt_softc *sc;
256 	int iobase;
257 	u_char control;
258 	int error;
259 	int spin;
260 
261 	if (unit >= lptcd.cd_ndevs)
262 		return ENXIO;
263 	sc = lptcd.cd_devs[unit];
264 	if (!sc)
265 		return ENXIO;
266 
267 	if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
268 		return ENXIO;
269 
270 #ifdef DIAGNOSTIC
271 	if (sc->sc_state)
272 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
273 		    sc->sc_state);
274 #endif
275 
276 	if (sc->sc_state)
277 		return EBUSY;
278 
279 	sc->sc_state = LPT_INIT;
280 	sc->sc_flags = flags;
281 	lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
282 	iobase = sc->sc_iobase;
283 
284 	if ((flags & LPT_NOPRIME) == 0) {
285 		/* assert INIT for 100 usec to start up printer */
286 		outb(iobase + lpt_control, LPC_SELECT);
287 		delay(100);
288 	}
289 
290 	control = LPC_SELECT | LPC_NINIT;
291 	outb(iobase + lpt_control, control);
292 
293 	/* wait till ready (printer running diagnostics) */
294 	for (spin = 0; NOT_READY_ERR(); spin += STEP) {
295 		if (spin >= TIMEOUT) {
296 			sc->sc_state = 0;
297 			return EBUSY;
298 		}
299 
300 		/* wait 1/4 second, give up if we get a signal */
301 		if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen",
302 		    STEP) != EWOULDBLOCK) {
303 			sc->sc_state = 0;
304 			return error;
305 		}
306 	}
307 
308 	if ((flags & LPT_NOINTR) == 0)
309 		control |= LPC_IENABLE;
310 	if (flags & LPT_AUTOLF)
311 		control |= LPC_AUTOLF;
312 	sc->sc_control = control;
313 	outb(iobase + lpt_control, control);
314 
315 	sc->sc_inbuf = geteblk(LPT_BSIZE);
316 	sc->sc_count = 0;
317 	sc->sc_state = LPT_OPEN;
318 
319 	if ((sc->sc_flags & LPT_NOINTR) == 0)
320 		lptwakeup(sc);
321 
322 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
323 	return 0;
324 }
325 
326 int
327 not_ready(status, sc)
328 	u_char status;
329 	struct lpt_softc *sc;
330 {
331 	u_char new;
332 
333 	status = (status ^ LPS_INVERT) & LPS_MASK;
334 	new = status & ~sc->sc_laststatus;
335 	sc->sc_laststatus = status;
336 
337 	if (new & LPS_SELECT)
338 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
339 	else if (new & LPS_NOPAPER)
340 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
341 	else if (new & LPS_NERR)
342 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
343 
344 	return status;
345 }
346 
347 void
348 lptwakeup(arg)
349 	void *arg;
350 {
351 	struct lpt_softc *sc = arg;
352 	int s;
353 
354 	s = spltty();
355 	lptintr(sc);
356 	splx(s);
357 
358 	timeout(lptwakeup, sc, STEP);
359 }
360 
361 /*
362  * Close the device, and free the local line buffer.
363  */
364 lptclose(dev, flag)
365 	dev_t dev;
366 	int flag;
367 {
368 	int unit = LPTUNIT(dev);
369 	struct lpt_softc *sc = lptcd.cd_devs[unit];
370 	int iobase = sc->sc_iobase;
371 
372 	if (sc->sc_count)
373 		(void) pushbytes(sc);
374 
375 	if ((sc->sc_flags & LPT_NOINTR) == 0)
376 		untimeout(lptwakeup, sc);
377 
378 	outb(iobase + lpt_control, LPC_NINIT);
379 	sc->sc_state = 0;
380 	outb(iobase + lpt_control, LPC_NINIT);
381 	brelse(sc->sc_inbuf);
382 
383 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
384 	return 0;
385 }
386 
387 int
388 pushbytes(sc)
389 	struct lpt_softc *sc;
390 {
391 	int iobase = sc->sc_iobase;
392 	int error;
393 
394 	if (sc->sc_flags & LPT_NOINTR) {
395 		int spin, tic;
396 		u_char control = sc->sc_control;
397 
398 		while (sc->sc_count > 0) {
399 			spin = 0;
400 			while (NOT_READY()) {
401 				if (++spin < sc->sc_spinmax)
402 					continue;
403 				tic = 0;
404 				/* adapt busy-wait algorithm */
405 				sc->sc_spinmax++;
406 				while (NOT_READY_ERR()) {
407 					/* exponential backoff */
408 					tic = tic + tic + 1;
409 					if (tic > TIMEOUT)
410 						tic = TIMEOUT;
411 					error = tsleep((caddr_t)sc,
412 					    LPTPRI | PCATCH, "lptpsh", tic);
413 					if (error != EWOULDBLOCK)
414 						return error;
415 				}
416 				break;
417 			}
418 
419 			outb(iobase + lpt_data, *sc->sc_cp++);
420 			outb(iobase + lpt_control, control | LPC_STROBE);
421 			sc->sc_count--;
422 			outb(iobase + lpt_control, control);
423 
424 			/* adapt busy-wait algorithm */
425 			if (spin*2 + 16 < sc->sc_spinmax)
426 				sc->sc_spinmax--;
427 		}
428 	} else {
429 		int s;
430 
431 		while (sc->sc_count > 0) {
432 			/* if the printer is ready for a char, give it one */
433 			if ((sc->sc_state & LPT_OBUSY) == 0) {
434 				lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
435 				    sc->sc_count);
436 				s = spltty();
437 				(void) lptintr(sc);
438 				splx(s);
439 			}
440 			if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
441 			    "lptwrite2", 0))
442 				return error;
443 		}
444 	}
445 	return 0;
446 }
447 
448 /*
449  * Copy a line from user space to a local buffer, then call putc to get the
450  * chars moved to the output queue.
451  */
452 lptwrite(dev, uio)
453 	dev_t dev;
454 	struct uio *uio;
455 {
456 	struct lpt_softc *sc = lptcd.cd_devs[LPTUNIT(dev)];
457 	size_t n;
458 	int error = 0;
459 
460 	while (n = min(LPT_BSIZE, uio->uio_resid)) {
461 		uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
462 		sc->sc_count = n;
463 		error = pushbytes(sc);
464 		if (error) {
465 			/*
466 			 * Return accurate residual if interrupted or timed
467 			 * out.
468 			 */
469 			uio->uio_resid += sc->sc_count;
470 			sc->sc_count = 0;
471 			return error;
472 		}
473 	}
474 	return 0;
475 }
476 
477 /*
478  * Handle printer interrupts which occur when the printer is ready to accept
479  * another char.
480  */
481 int
482 lptintr(arg)
483 	void *arg;
484 {
485 	struct lpt_softc *sc = arg;
486 	int iobase = sc->sc_iobase;
487 
488 #if 0
489 	if ((sc->sc_state & LPT_OPEN) == 0)
490 		return 0;
491 #endif
492 
493 	/* is printer online and ready for output */
494 	if (NOT_READY() && NOT_READY_ERR())
495 		return 0;
496 
497 	if (sc->sc_count) {
498 		u_char control = sc->sc_control;
499 		/* send char */
500 		outb(iobase + lpt_data, *sc->sc_cp++);
501 		outb(iobase + lpt_control, control | LPC_STROBE);
502 		sc->sc_count--;
503 		outb(iobase + lpt_control, control);
504 		sc->sc_state |= LPT_OBUSY;
505 	} else
506 		sc->sc_state &= ~LPT_OBUSY;
507 
508 	if (sc->sc_count == 0) {
509 		/* none, wake up the top half to get more */
510 		wakeup((caddr_t)sc);
511 	}
512 
513 	return 1;
514 }
515 
516 int
517 lptioctl(dev, cmd, data, flag)
518 	dev_t dev;
519 	u_long cmd;
520 	caddr_t data;
521 	int flag;
522 {
523 	int error = 0;
524 
525 	switch (cmd) {
526 	default:
527 		error = ENODEV;
528 	}
529 
530 	return error;
531 }
532