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