xref: /openbsd/sys/dev/ic/lpt.c (revision 1edbe4cc)
1 /*	$OpenBSD: lpt.c,v 1.15 2020/01/15 16:43:13 cheloha 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/buf.h>
60 #include <sys/kernel.h>
61 #include <sys/uio.h>
62 #include <sys/device.h>
63 #include <sys/conf.h>
64 #include <sys/syslog.h>
65 
66 #include <machine/bus.h>
67 #include <machine/intr.h>
68 
69 #include <dev/ic/lptreg.h>
70 #include <dev/ic/lptvar.h>
71 
72 #include "lpt.h"
73 
74 #define	TIMEOUT		16000	/* wait up to 16 seconds for a ready */
75 #define	STEP		250	/* 1/4 seconds */
76 
77 #define	LPTPRI		(PZERO+8)
78 #define	LPT_BSIZE	1024
79 
80 #if !defined(DEBUG) || !defined(notdef)
81 #define LPRINTF(a)
82 #else
83 #define LPRINTF(a)	if (lptdebug) printf a
84 int lptdebug = 1;
85 #endif
86 
87 /* XXX does not belong here */
88 cdev_decl(lpt);
89 
90 struct cfdriver lpt_cd = {
91 	NULL, "lpt", DV_TTY
92 };
93 
94 #define	LPTUNIT(s)	(minor(s) & 0x1f)
95 #define	LPTFLAGS(s)	(minor(s) & 0xe0)
96 
97 #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
98 #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
99 #define	NOT_READY() \
100     ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, lpt_status) ^ LPS_INVERT) & LPS_MASK)
101 #define	NOT_READY_ERR() \
102     lpt_not_ready(bus_space_read_1(sc->sc_iot, sc->sc_ioh, lpt_status), sc)
103 
104 int	lpt_not_ready(u_int8_t, struct lpt_softc *);
105 void	lptwakeup(void *arg);
106 int	lptpushbytes(struct lpt_softc *);
107 
108 /*
109  * Internal routine to lptprobe to do port tests of one byte value.
110  */
111 int
lpt_port_test(bus_space_tag_t iot,bus_space_handle_t ioh,bus_addr_t base,bus_size_t off,u_int8_t data,u_int8_t mask)112 lpt_port_test(bus_space_tag_t iot, bus_space_handle_t ioh, bus_addr_t base,
113     bus_size_t off, u_int8_t data, u_int8_t mask)
114 {
115 	int timeout;
116 	u_int8_t temp;
117 
118 	data &= mask;
119 	bus_space_write_1(iot, ioh, off, data);
120 	timeout = 1000;
121 	do {
122 		delay(10);
123 		temp = bus_space_read_1(iot, ioh, off) & mask;
124 	} while (temp != data && --timeout);
125 	LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", base + off,
126 	    data, temp, timeout));
127 	return (temp == data);
128 }
129 
130 void
lpt_attach_common(struct lpt_softc * sc)131 lpt_attach_common(struct lpt_softc *sc)
132 {
133 	printf("\n");
134 
135 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, lpt_control, LPC_NINIT);
136 
137 	timeout_set(&sc->sc_wakeup_tmo, lptwakeup, sc);
138 }
139 
140 void
lpt_detach_common(struct lpt_softc * sc)141 lpt_detach_common(struct lpt_softc *sc)
142 {
143 	timeout_del(&sc->sc_wakeup_tmo);
144 	if (sc->sc_state != 0) {
145 		sc->sc_state = 0;
146 		wakeup(sc);
147 	}
148 }
149 
150 /*
151  * Reset the printer, then wait until it's selected and not busy.
152  */
153 int
lptopen(dev_t dev,int flag,int mode,struct proc * p)154 lptopen(dev_t dev, int flag, int mode, struct proc *p)
155 {
156 	int unit = LPTUNIT(dev);
157 	u_int8_t flags = LPTFLAGS(dev);
158 	struct lpt_softc *sc;
159 	u_int8_t control;
160 	int error;
161 	int spin;
162 
163 	if (unit >= lpt_cd.cd_ndevs)
164 		return ENXIO;
165 	sc = lpt_cd.cd_devs[unit];
166 	if (!sc)
167 		return ENXIO;
168 
169 	sc->sc_flags = (sc->sc_flags & LPT_POLLED) | flags;
170 	if ((sc->sc_flags & (LPT_POLLED|LPT_NOINTR)) == LPT_POLLED)
171 		return ENXIO;
172 
173 #ifdef DIAGNOSTIC
174 	if (sc->sc_state)
175 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
176 		    sc->sc_state);
177 #endif
178 
179 	if (sc->sc_state)
180 		return EBUSY;
181 
182 	sc->sc_state = LPT_INIT;
183 	LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags));
184 
185 	if ((flags & LPT_NOPRIME) == 0) {
186 		/* assert INIT for 100 usec to start up printer */
187 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, lpt_control, LPC_SELECT);
188 		delay(100);
189 	}
190 
191 	control = LPC_SELECT | LPC_NINIT;
192 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, lpt_control, control);
193 
194 	/* wait till ready (printer running diagnostics) */
195 	for (spin = 0; NOT_READY_ERR(); spin += STEP) {
196 		if (spin >= TIMEOUT) {
197 			sc->sc_state = 0;
198 			return EBUSY;
199 		}
200 
201 		/* wait 1/4 second, give up if we get a signal */
202 		error = tsleep_nsec(sc, LPTPRI | PCATCH, "lptopen",
203 		    MSEC_TO_NSEC(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
lpt_not_ready(u_int8_t status,struct lpt_softc * sc)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
lptwakeup(void * arg)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_msec(&sc->sc_wakeup_tmo, STEP);
261 }
262 
263 /*
264  * Close the device, and free the local line buffer.
265  */
266 int
lptclose(dev_t dev,int flag,int mode,struct proc * p)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
lptpushbytes(struct lpt_softc * sc)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 msecs, spin;
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 				msecs = 0;
308 				/* adapt busy-wait algorithm */
309 				sc->sc_spinmax++;
310 				while (NOT_READY_ERR()) {
311 					/* exponential backoff */
312 					msecs = msecs + msecs + 10;
313 					if (msecs > TIMEOUT)
314 						msecs = TIMEOUT;
315 					error = tsleep_nsec(sc,
316 					    LPTPRI | PCATCH, "lptpsh",
317 					    MSEC_TO_NSEC(msecs));
318 					if (sc->sc_state == 0)
319 						error = EIO;
320 					if (error != EWOULDBLOCK)
321 						return error;
322 				}
323 				break;
324 			}
325 
326 			bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
327 			bus_space_write_1(iot, ioh, lpt_control,
328 			    control | LPC_STROBE);
329 			sc->sc_count--;
330 			bus_space_write_1(iot, ioh, lpt_control, control);
331 
332 			/* adapt busy-wait algorithm */
333 			if (spin*2 + 16 < sc->sc_spinmax)
334 				sc->sc_spinmax--;
335 		}
336 	} else {
337 		int s;
338 
339 		while (sc->sc_count > 0) {
340 			/* if the printer is ready for a char, give it one */
341 			if ((sc->sc_state & LPT_OBUSY) == 0) {
342 				LPRINTF(("%s: write %d\n", sc->sc_dev.dv_xname,
343 				    sc->sc_count));
344 				s = spltty();
345 				(void) lptintr(sc);
346 				splx(s);
347 			}
348 			if (sc->sc_state == 0)
349 				return (EIO);
350 			error = tsleep_nsec(sc, LPTPRI | PCATCH,
351 			    "lptwrite2", INFSLP);
352 			if (sc->sc_state == 0)
353 				error = EIO;
354 			if (error)
355 				return error;
356 		}
357 	}
358 	return 0;
359 }
360 
361 /*
362  * Copy a line from user space to a local buffer, then call putc to get the
363  * chars moved to the output queue.
364  */
365 int
lptwrite(dev_t dev,struct uio * uio,int flags)366 lptwrite(dev_t dev, struct uio *uio, int flags)
367 {
368 	struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)];
369 	size_t n;
370 	int error = 0;
371 
372 	while ((n = ulmin(LPT_BSIZE, uio->uio_resid)) != 0) {
373 		error = uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
374 		if (error != 0)
375 			return error;
376 		sc->sc_count = n;
377 		error = lptpushbytes(sc);
378 		if (error) {
379 			/*
380 			 * Return accurate residual if interrupted or timed
381 			 * out.
382 			 */
383 			uio->uio_resid += sc->sc_count;
384 			sc->sc_count = 0;
385 			return error;
386 		}
387 	}
388 	return 0;
389 }
390 
391 /*
392  * Handle printer interrupts which occur when the printer is ready to accept
393  * another char.
394  */
395 int
lptintr(void * arg)396 lptintr(void *arg)
397 {
398 	struct lpt_softc *sc = arg;
399 	bus_space_tag_t iot = sc->sc_iot;
400 	bus_space_handle_t ioh = sc->sc_ioh;
401 
402 	if (((sc->sc_state & LPT_OPEN) == 0 && sc->sc_count == 0) ||
403 	    (sc->sc_flags & LPT_NOINTR))
404 		return 0;
405 
406 	/* is printer online and ready for output */
407 	if (NOT_READY() && NOT_READY_ERR())
408 		return -1;
409 
410 	if (sc->sc_count) {
411 		u_int8_t control = sc->sc_control;
412 		/* send char */
413 		bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
414 		delay (50);
415 		bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
416 		sc->sc_count--;
417 		bus_space_write_1(iot, ioh, lpt_control, control);
418 		sc->sc_state |= LPT_OBUSY;
419 	} else
420 		sc->sc_state &= ~LPT_OBUSY;
421 
422 	if (sc->sc_count == 0) {
423 		/* none, wake up the top half to get more */
424 		wakeup((caddr_t)sc);
425 	}
426 
427 	return 1;
428 }
429 
430 int
lpt_activate(struct device * self,int act)431 lpt_activate(struct device *self, int act)
432 {
433 	struct lpt_softc *sc = (struct lpt_softc *)self;
434 
435 	switch (act) {
436 	case DVACT_SUSPEND:
437 		timeout_del(&sc->sc_wakeup_tmo);
438 		break;
439 	case DVACT_RESUME:
440 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, lpt_control, LPC_NINIT);
441 
442 		if (sc->sc_state) {
443 			int spin;
444 
445 			if ((sc->sc_flags & LPT_NOPRIME) == 0) {
446 				/* assert INIT for 100 usec to start up printer */
447 				bus_space_write_1(sc->sc_iot, sc->sc_ioh,
448 				    lpt_control, LPC_SELECT);
449 				delay(100);
450 			}
451 
452 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, lpt_control,
453 			    LPC_SELECT | LPC_NINIT);
454 
455 			/* wait till ready (printer running diagnostics) */
456 			for (spin = 0; NOT_READY_ERR(); spin += STEP) {
457 				if (spin >= TIMEOUT) {
458 					sc->sc_state = 0;
459 					goto fail;
460 				}
461 
462 				/* wait 1/4 second, give up if we get a signal */
463 				delay(STEP * 1000);
464 			}
465 
466 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
467 			    lpt_control, sc->sc_control);
468 			wakeup(sc);
469 		}
470 fail:
471 		break;
472 	}
473 
474 	return (0);
475 }
476