xref: /openbsd-src/sys/dev/ic/lpt.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: lpt.c,v 1.7 2010/08/06 00:00:41 miod 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/ioctl.h>
63 #include <sys/uio.h>
64 #include <sys/device.h>
65 #include <sys/conf.h>
66 #include <sys/syslog.h>
67 
68 #include <machine/bus.h>
69 #include <machine/intr.h>
70 
71 #include <dev/ic/lptreg.h>
72 #include <dev/ic/lptvar.h>
73 
74 #include "lpt.h"
75 
76 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
77 #define	STEP		hz/4
78 
79 #define	LPTPRI		(PZERO+8)
80 #define	LPT_BSIZE	1024
81 
82 #if !defined(DEBUG) || !defined(notdef)
83 #define LPRINTF(a)
84 #else
85 #define LPRINTF(a)	if (lptdebug) printf a
86 int lptdebug = 1;
87 #endif
88 
89 /* XXX does not belong here */
90 cdev_decl(lpt);
91 
92 struct cfdriver lpt_cd = {
93 	NULL, "lpt", DV_TTY
94 };
95 
96 #define	LPTUNIT(s)	(minor(s) & 0x1f)
97 #define	LPTFLAGS(s)	(minor(s) & 0xe0)
98 
99 #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
100 #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
101 #define	NOT_READY() \
102     ((bus_space_read_1(iot, ioh, lpt_status) ^ LPS_INVERT) & LPS_MASK)
103 #define	NOT_READY_ERR() \
104     lpt_not_ready(bus_space_read_1(iot, ioh, lpt_status), sc)
105 
106 int	lpt_not_ready(u_int8_t, struct lpt_softc *);
107 void	lptwakeup(void *arg);
108 int	lptpushbytes(struct lpt_softc *);
109 
110 /*
111  * Internal routine to lptprobe to do port tests of one byte value.
112  */
113 int
114 lpt_port_test(iot, ioh, base, off, data, mask)
115 	bus_space_tag_t iot;
116 	bus_space_handle_t ioh;
117 	bus_addr_t base;
118 	bus_size_t off;
119 	u_int8_t data, mask;
120 {
121 	int timeout;
122 	u_int8_t temp;
123 
124 	data &= mask;
125 	bus_space_write_1(iot, ioh, off, data);
126 	timeout = 1000;
127 	do {
128 		delay(10);
129 		temp = bus_space_read_1(iot, ioh, off) & mask;
130 	} while (temp != data && --timeout);
131 	LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", base + off,
132 	    data, temp, timeout));
133 	return (temp == data);
134 }
135 
136 void
137 lpt_attach_common(sc)
138 	struct lpt_softc *sc;
139 {
140 	printf("\n");
141 
142 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, lpt_control, LPC_NINIT);
143 
144 	timeout_set(&sc->sc_wakeup_tmo, lptwakeup, sc);
145 }
146 
147 /*
148  * Reset the printer, then wait until it's selected and not busy.
149  */
150 int
151 lptopen(dev, flag, mode, p)
152 	dev_t dev;
153 	int flag;
154 	int mode;
155 	struct proc *p;
156 {
157 	int unit = LPTUNIT(dev);
158 	u_int8_t flags = LPTFLAGS(dev);
159 	struct lpt_softc *sc;
160 	bus_space_tag_t iot;
161 	bus_space_handle_t ioh;
162 	u_int8_t control;
163 	int error;
164 	int spin;
165 
166 	if (unit >= lpt_cd.cd_ndevs)
167 		return ENXIO;
168 	sc = lpt_cd.cd_devs[unit];
169 	if (!sc)
170 		return ENXIO;
171 
172 	sc->sc_flags = (sc->sc_flags & LPT_POLLED) | flags;
173 	if ((sc->sc_flags & (LPT_POLLED|LPT_NOINTR)) == LPT_POLLED)
174 		return ENXIO;
175 
176 #ifdef DIAGNOSTIC
177 	if (sc->sc_state)
178 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
179 		    sc->sc_state);
180 #endif
181 
182 	if (sc->sc_state)
183 		return EBUSY;
184 
185 	sc->sc_state = LPT_INIT;
186 	LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags));
187 	iot = sc->sc_iot;
188 	ioh = sc->sc_ioh;
189 
190 	if ((flags & LPT_NOPRIME) == 0) {
191 		/* assert INIT for 100 usec to start up printer */
192 		bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
193 		delay(100);
194 	}
195 
196 	control = LPC_SELECT | LPC_NINIT;
197 	bus_space_write_1(iot, ioh, lpt_control, control);
198 
199 	/* wait till ready (printer running diagnostics) */
200 	for (spin = 0; NOT_READY_ERR(); spin += STEP) {
201 		if (spin >= TIMEOUT) {
202 			sc->sc_state = 0;
203 			return EBUSY;
204 		}
205 
206 		/* wait 1/4 second, give up if we get a signal */
207 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
208 		if (error != EWOULDBLOCK) {
209 			sc->sc_state = 0;
210 			return error;
211 		}
212 	}
213 
214 	if ((flags & LPT_NOINTR) == 0)
215 		control |= LPC_IENABLE;
216 	if (flags & LPT_AUTOLF)
217 		control |= LPC_AUTOLF;
218 	sc->sc_control = control;
219 	bus_space_write_1(iot, ioh, lpt_control, control);
220 
221 	sc->sc_inbuf = geteblk(LPT_BSIZE);
222 	sc->sc_count = 0;
223 	sc->sc_state = LPT_OPEN;
224 
225 	if ((sc->sc_flags & LPT_NOINTR) == 0)
226 		lptwakeup(sc);
227 
228 	LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
229 	return 0;
230 }
231 
232 int
233 lpt_not_ready(status, sc)
234 	u_int8_t status;
235 	struct lpt_softc *sc;
236 {
237 	u_int8_t new;
238 
239 	status = (status ^ LPS_INVERT) & LPS_MASK;
240 	new = status & ~sc->sc_laststatus;
241 	sc->sc_laststatus = status;
242 
243 	if (new & LPS_SELECT)
244 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
245 	else if (new & LPS_NOPAPER)
246 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
247 	else if (new & LPS_NERR)
248 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
249 
250 	return status;
251 }
252 
253 void
254 lptwakeup(arg)
255 	void *arg;
256 {
257 	struct lpt_softc *sc = arg;
258 	int s;
259 
260 	s = spltty();
261 	lptintr(sc);
262 	splx(s);
263 
264 	timeout_add(&sc->sc_wakeup_tmo, STEP);
265 }
266 
267 /*
268  * Close the device, and free the local line buffer.
269  */
270 int
271 lptclose(dev, flag, mode, p)
272 	dev_t dev;
273 	int flag;
274 	int mode;
275 	struct proc *p;
276 {
277 	int unit = LPTUNIT(dev);
278 	struct lpt_softc *sc = lpt_cd.cd_devs[unit];
279 	bus_space_tag_t iot = sc->sc_iot;
280 	bus_space_handle_t ioh = sc->sc_ioh;
281 
282 	if (sc->sc_count)
283 		(void) lptpushbytes(sc);
284 
285 	if ((sc->sc_flags & LPT_NOINTR) == 0)
286 		timeout_del(&sc->sc_wakeup_tmo);
287 
288 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
289 	sc->sc_state = 0;
290 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
291 	brelse(sc->sc_inbuf);
292 
293 	LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
294 	return 0;
295 }
296 
297 int
298 lptpushbytes(sc)
299 	struct lpt_softc *sc;
300 {
301 	bus_space_tag_t iot = sc->sc_iot;
302 	bus_space_handle_t ioh = sc->sc_ioh;
303 	int error;
304 
305 	if (sc->sc_flags & LPT_NOINTR) {
306 		int spin, tic;
307 		u_int8_t control = sc->sc_control;
308 
309 		while (sc->sc_count > 0) {
310 			spin = 0;
311 			while (NOT_READY()) {
312 				if (++spin < sc->sc_spinmax)
313 					continue;
314 				tic = 0;
315 				/* adapt busy-wait algorithm */
316 				sc->sc_spinmax++;
317 				while (NOT_READY_ERR()) {
318 					/* exponential backoff */
319 					tic = tic + tic + 1;
320 					if (tic > TIMEOUT)
321 						tic = TIMEOUT;
322 					error = tsleep((caddr_t)sc,
323 					    LPTPRI | PCATCH, "lptpsh", tic);
324 					if (error != EWOULDBLOCK)
325 						return error;
326 				}
327 				break;
328 			}
329 
330 			bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
331 			bus_space_write_1(iot, ioh, lpt_control,
332 			    control | LPC_STROBE);
333 			sc->sc_count--;
334 			bus_space_write_1(iot, ioh, lpt_control, control);
335 
336 			/* adapt busy-wait algorithm */
337 			if (spin*2 + 16 < sc->sc_spinmax)
338 				sc->sc_spinmax--;
339 		}
340 	} else {
341 		int s;
342 
343 		while (sc->sc_count > 0) {
344 			/* if the printer is ready for a char, give it one */
345 			if ((sc->sc_state & LPT_OBUSY) == 0) {
346 				LPRINTF(("%s: write %d\n", sc->sc_dev.dv_xname,
347 				    sc->sc_count));
348 				s = spltty();
349 				(void) lptintr(sc);
350 				splx(s);
351 			}
352 			error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
353 			    "lptwrite2", 0);
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
366 lptwrite(dev, uio, flags)
367 	dev_t dev;
368 	struct uio *uio;
369 	int flags;
370 {
371 	struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)];
372 	size_t n;
373 	int error = 0;
374 
375 	while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
376 		error = uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
377 		if (error != 0)
378 			return error;
379 		sc->sc_count = n;
380 		error = lptpushbytes(sc);
381 		if (error) {
382 			/*
383 			 * Return accurate residual if interrupted or timed
384 			 * out.
385 			 */
386 			uio->uio_resid += sc->sc_count;
387 			sc->sc_count = 0;
388 			return error;
389 		}
390 	}
391 	return 0;
392 }
393 
394 /*
395  * Handle printer interrupts which occur when the printer is ready to accept
396  * another char.
397  */
398 int
399 lptintr(arg)
400 	void *arg;
401 {
402 	struct lpt_softc *sc = arg;
403 	bus_space_tag_t iot = sc->sc_iot;
404 	bus_space_handle_t ioh = sc->sc_ioh;
405 
406 	if (((sc->sc_state & LPT_OPEN) == 0 && sc->sc_count == 0) ||
407 	    (sc->sc_flags & LPT_NOINTR))
408 		return 0;
409 
410 	/* is printer online and ready for output */
411 	if (NOT_READY() && NOT_READY_ERR())
412 		return -1;
413 
414 	if (sc->sc_count) {
415 		u_int8_t control = sc->sc_control;
416 		/* send char */
417 		bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
418 		delay (50);
419 		bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
420 		sc->sc_count--;
421 		bus_space_write_1(iot, ioh, lpt_control, control);
422 		sc->sc_state |= LPT_OBUSY;
423 	} else
424 		sc->sc_state &= ~LPT_OBUSY;
425 
426 	if (sc->sc_count == 0) {
427 		/* none, wake up the top half to get more */
428 		wakeup((caddr_t)sc);
429 	}
430 
431 	return 1;
432 }
433 
434 int
435 lptioctl(dev, cmd, data, flag, p)
436 	dev_t dev;
437 	u_long cmd;
438 	caddr_t data;
439 	int flag;
440 	struct proc *p;
441 {
442 	int error = 0;
443 
444 	switch (cmd) {
445 	default:
446 		error = ENODEV;
447 	}
448 
449 	return error;
450 }
451