xref: /netbsd-src/sys/dev/ic/lpt.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: lpt.c,v 1.47 1997/12/07 16:09:36 thorpej 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 style 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/malloc.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 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
75 #define	STEP		hz/4
76 
77 #define	LPTPRI		(PZERO+8)
78 #define	LPT_BSIZE	1024
79 
80 #define LPTDEBUG
81 
82 #ifndef LPTDEBUG
83 #define LPRINTF(a)
84 #else
85 #define LPRINTF(a)	if (lptdebug) printf a
86 int lptdebug = 0;
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 void
100 lpt_attach_subr(sc)
101 	struct lpt_softc *sc;
102 {
103 	bus_space_tag_t iot;
104 	bus_space_handle_t ioh;
105 
106 	sc->sc_state = 0;
107 
108 	iot = sc->sc_iot;
109 	ioh = sc->sc_ioh;
110 
111 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
112 }
113 
114 /*
115  * Reset the printer, then wait until it's selected and not busy.
116  */
117 int
118 lptopen(dev, flag, mode, p)
119 	dev_t dev;
120 	int flag;
121 	int mode;
122 	struct proc *p;
123 {
124 	int unit = LPTUNIT(dev);
125 	u_char flags = LPTFLAGS(dev);
126 	struct lpt_softc *sc;
127 	bus_space_tag_t iot;
128 	bus_space_handle_t ioh;
129 	u_char control;
130 	int error;
131 	int spin;
132 
133 	if (unit >= lpt_cd.cd_ndevs)
134 		return ENXIO;
135 	sc = lpt_cd.cd_devs[unit];
136 	if (!sc)
137 		return ENXIO;
138 
139 #if 0	/* XXX what to do? */
140 	if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
141 		return ENXIO;
142 #endif
143 
144 #ifdef DIAGNOSTIC
145 	if (sc->sc_state)
146 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
147 		    sc->sc_state);
148 #endif
149 
150 	if (sc->sc_state)
151 		return EBUSY;
152 
153 	sc->sc_state = LPT_INIT;
154 	sc->sc_flags = flags;
155 	LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname,
156 	    (unsigned)flags));
157 	iot = sc->sc_iot;
158 	ioh = sc->sc_ioh;
159 
160 	if ((flags & LPT_NOPRIME) == 0) {
161 		/* assert INIT for 100 usec to start up printer */
162 		bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
163 		delay(100);
164 	}
165 
166 	control = LPC_SELECT | LPC_NINIT;
167 	bus_space_write_1(iot, ioh, lpt_control, control);
168 
169 	/* wait till ready (printer running diagnostics) */
170 	for (spin = 0; NOT_READY_ERR(); spin += STEP) {
171 		if (spin >= TIMEOUT) {
172 			sc->sc_state = 0;
173 			return EBUSY;
174 		}
175 
176 		/* wait 1/4 second, give up if we get a signal */
177 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
178 		if (error != EWOULDBLOCK) {
179 			sc->sc_state = 0;
180 			return error;
181 		}
182 	}
183 
184 	if ((flags & LPT_NOINTR) == 0)
185 		control |= LPC_IENABLE;
186 	if (flags & LPT_AUTOLF)
187 		control |= LPC_AUTOLF;
188 	sc->sc_control = control;
189 	bus_space_write_1(iot, ioh, lpt_control, control);
190 
191 	sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK);
192 	sc->sc_count = 0;
193 	sc->sc_state = LPT_OPEN;
194 
195 	if ((sc->sc_flags & LPT_NOINTR) == 0)
196 		lptwakeup(sc);
197 
198 	LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
199 	return 0;
200 }
201 
202 int
203 lptnotready(status, sc)
204 	u_char status;
205 	struct lpt_softc *sc;
206 {
207 	u_char new;
208 
209 	status = (status ^ LPS_INVERT) & LPS_MASK;
210 	new = status & ~sc->sc_laststatus;
211 	sc->sc_laststatus = status;
212 
213 	if (new & LPS_SELECT)
214 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
215 	else if (new & LPS_NOPAPER)
216 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
217 	else if (new & LPS_NERR)
218 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
219 
220 	return status;
221 }
222 
223 void
224 lptwakeup(arg)
225 	void *arg;
226 {
227 	struct lpt_softc *sc = arg;
228 	int s;
229 
230 	s = spltty();
231 	lptintr(sc);
232 	splx(s);
233 
234 	timeout(lptwakeup, sc, STEP);
235 }
236 
237 /*
238  * Close the device, and free the local line buffer.
239  */
240 int
241 lptclose(dev, flag, mode, p)
242 	dev_t dev;
243 	int flag;
244 	int mode;
245 	struct proc *p;
246 {
247 	int unit = LPTUNIT(dev);
248 	struct lpt_softc *sc = lpt_cd.cd_devs[unit];
249 	bus_space_tag_t iot = sc->sc_iot;
250 	bus_space_handle_t ioh = sc->sc_ioh;
251 
252 	if (sc->sc_count)
253 		(void) lptpushbytes(sc);
254 
255 	if ((sc->sc_flags & LPT_NOINTR) == 0)
256 		untimeout(lptwakeup, sc);
257 
258 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
259 	sc->sc_state = 0;
260 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
261 	free(sc->sc_inbuf, M_DEVBUF);
262 
263 	LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
264 	return 0;
265 }
266 
267 int
268 lptpushbytes(sc)
269 	struct lpt_softc *sc;
270 {
271 	bus_space_tag_t iot = sc->sc_iot;
272 	bus_space_handle_t ioh = sc->sc_ioh;
273 	int error;
274 
275 	if (sc->sc_flags & LPT_NOINTR) {
276 		int spin, tic;
277 		u_char control = sc->sc_control;
278 
279 		while (sc->sc_count > 0) {
280 			spin = 0;
281 			while (NOT_READY()) {
282 				if (++spin < sc->sc_spinmax)
283 					continue;
284 				tic = 0;
285 				/* adapt busy-wait algorithm */
286 				sc->sc_spinmax++;
287 				while (NOT_READY_ERR()) {
288 					/* exponential backoff */
289 					tic = tic + tic + 1;
290 					if (tic > TIMEOUT)
291 						tic = TIMEOUT;
292 					error = tsleep((caddr_t)sc,
293 					    LPTPRI | PCATCH, "lptpsh", tic);
294 					if (error != EWOULDBLOCK)
295 						return error;
296 				}
297 				break;
298 			}
299 
300 			bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
301 			bus_space_write_1(iot, ioh, lpt_control,
302 			    control | LPC_STROBE);
303 			sc->sc_count--;
304 			bus_space_write_1(iot, ioh, lpt_control, control);
305 
306 			/* adapt busy-wait algorithm */
307 			if (spin*2 + 16 < sc->sc_spinmax)
308 				sc->sc_spinmax--;
309 		}
310 	} else {
311 		int s;
312 
313 		while (sc->sc_count > 0) {
314 			/* if the printer is ready for a char, give it one */
315 			if ((sc->sc_state & LPT_OBUSY) == 0) {
316 				LPRINTF(("%s: write %u\n", sc->sc_dev.dv_xname,
317 				    sc->sc_count));
318 				s = spltty();
319 				(void) lptintr(sc);
320 				splx(s);
321 			}
322 			error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
323 			    "lptwrite2", 0);
324 			if (error)
325 				return error;
326 		}
327 	}
328 	return 0;
329 }
330 
331 /*
332  * Copy a line from user space to a local buffer, then call putc to get the
333  * chars moved to the output queue.
334  */
335 int
336 lptwrite(dev, uio, flags)
337 	dev_t dev;
338 	struct uio *uio;
339 	int flags;
340 {
341 	struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)];
342 	size_t n;
343 	int error = 0;
344 
345 	while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
346 		uiomove(sc->sc_cp = sc->sc_inbuf, n, uio);
347 		sc->sc_count = n;
348 		error = lptpushbytes(sc);
349 		if (error) {
350 			/*
351 			 * Return accurate residual if interrupted or timed
352 			 * out.
353 			 */
354 			uio->uio_resid += sc->sc_count;
355 			sc->sc_count = 0;
356 			return error;
357 		}
358 	}
359 	return 0;
360 }
361 
362 /*
363  * Handle printer interrupts which occur when the printer is ready to accept
364  * another char.
365  */
366 int
367 lptintr(arg)
368 	void *arg;
369 {
370 	struct lpt_softc *sc = arg;
371 	bus_space_tag_t iot = sc->sc_iot;
372 	bus_space_handle_t ioh = sc->sc_ioh;
373 
374 #if 0
375 	if ((sc->sc_state & LPT_OPEN) == 0)
376 		return 0;
377 #endif
378 
379 	/* is printer online and ready for output */
380 	if (NOT_READY() && NOT_READY_ERR())
381 		return 0;
382 
383 	if (sc->sc_count) {
384 		u_char control = sc->sc_control;
385 		/* send char */
386 		bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
387 		bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
388 		sc->sc_count--;
389 		bus_space_write_1(iot, ioh, lpt_control, control);
390 		sc->sc_state |= LPT_OBUSY;
391 	} else
392 		sc->sc_state &= ~LPT_OBUSY;
393 
394 	if (sc->sc_count == 0) {
395 		/* none, wake up the top half to get more */
396 		wakeup((caddr_t)sc);
397 	}
398 
399 	return 1;
400 }
401 
402 int
403 lptioctl(dev, cmd, data, flag, p)
404 	dev_t dev;
405 	u_long cmd;
406 	caddr_t data;
407 	int flag;
408 	struct proc *p;
409 {
410 	int error = 0;
411 
412 	switch (cmd) {
413 	default:
414 		error = ENODEV;
415 	}
416 
417 	return error;
418 }
419