xref: /netbsd-src/sys/dev/mvme/lpt_mvme.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: lpt_mvme.c,v 1.3 2002/10/23 09:13:28 jdolecek Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Steve C. Woodford.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1993, 1994 Charles M. Hannum.
41  * Copyright (c) 1990 William F. Jolitz, TeleMuse
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This software is a component of "386BSD" developed by
55  *	William F. Jolitz, TeleMuse.
56  * 4. Neither the name of the developer nor the name "386BSD"
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
61  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
62  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
63  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
64  * NOT MAKE USE OF THIS WORK.
65  *
66  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
67  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
68  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
69  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
70  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
71  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
72  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
73  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
74  *
75  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
76  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
79  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */
87 
88 /*
89  * Device Driver for an MVME68K/MVME88K board's parallel printer port
90  * This driver attaches above the board-specific back-end.
91  */
92 
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/proc.h>
96 #include <sys/user.h>
97 #include <sys/buf.h>
98 #include <sys/kernel.h>
99 #include <sys/ioctl.h>
100 #include <sys/uio.h>
101 #include <sys/device.h>
102 #include <sys/conf.h>
103 #include <sys/syslog.h>
104 
105 #include <machine/cpu.h>
106 #include <machine/bus.h>
107 
108 #include <dev/mvme/lptvar.h>
109 
110 
111 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
112 #define	STEP		hz/4
113 
114 #define	LPTPRI		(PZERO+8)
115 #define	LPT_BSIZE	1024
116 
117 #if !defined(DEBUG) || !defined(notdef)
118 #define LPRINTF(a)
119 #else
120 #define LPRINTF		if (lptdebug) printf a
121 int lptdebug = 1;
122 #endif
123 
124 #define	LPTUNIT(s)	(minor(s) & 0x0f)
125 #define	LPTFLAGS(s)	(minor(s) & 0xf0)
126 
127 static void lpt_wakeup __P((void *arg));
128 static int pushbytes __P((struct lpt_softc *));
129 
130 extern struct cfdriver lpt_cd;
131 
132 dev_type_open(lptopen);
133 dev_type_close(lptclose);
134 dev_type_write(lptwrite);
135 dev_type_ioctl(lptioctl);
136 
137 const struct cdevsw lpt_cdevsw = {
138 	lptopen, lptclose, noread, lptwrite, lptioctl,
139 	nostop, notty, nopoll, nommap, nokqfilter,
140 };
141 
142 void
143 lpt_attach_subr(sc)
144 	struct lpt_softc *sc;
145 {
146 
147 	sc->sc_state = 0;
148 	callout_init(&sc->sc_wakeup_ch);
149 }
150 
151 /*
152  * Reset the printer, then wait until it's selected and not busy.
153  */
154 int
155 lptopen(dev, flag, mode, p)
156 	dev_t dev;
157 	int flag;
158 	int mode;
159 	struct proc *p;
160 {
161 	int unit;
162 	u_char flags;
163 	struct lpt_softc *sc;
164 	int error;
165 	int spin;
166 
167 	unit = LPTUNIT(dev);
168 	flags = LPTFLAGS(dev);
169 
170 	if (unit >= lpt_cd.cd_ndevs)
171 		return (ENXIO);
172 	sc = lpt_cd.cd_devs[unit];
173 	if (!sc)
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 	sc->sc_flags = flags;
187 	LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags));
188 
189 	if ((flags & LPT_NOPRIME) == 0) {
190 		/* assert Input Prime for 100 usec to start up printer */
191 		(sc->sc_funcs->lf_iprime) (sc);
192 	}
193 
194 	/* select fast or slow strobe depending on minor device number */
195 	if (flags & LPT_FAST_STROBE)
196 		(sc->sc_funcs->lf_speed) (sc, LPT_STROBE_FAST);
197 	else
198 		(sc->sc_funcs->lf_speed) (sc, LPT_STROBE_SLOW);
199 
200 	/* wait till ready (printer running diagnostics) */
201 	for (spin = 0; (sc->sc_funcs->lf_notrdy) (sc, 1); spin += STEP) {
202 		if (spin >= TIMEOUT) {
203 			sc->sc_state = 0;
204 			return (EBUSY);
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 	sc->sc_inbuf = geteblk(LPT_BSIZE);
215 	sc->sc_count = 0;
216 	sc->sc_state = LPT_OPEN;
217 
218 	if ((sc->sc_flags & LPT_NOINTR) == 0)
219 		lpt_wakeup(sc);
220 
221 	(sc->sc_funcs->lf_open) (sc, sc->sc_flags & LPT_NOINTR);
222 
223 	LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
224 	return (0);
225 }
226 
227 void
228 lpt_wakeup(arg)
229 	void *arg;
230 {
231 	struct lpt_softc *sc;
232 	int s;
233 
234 	sc = arg;
235 
236 	s = spltty();
237 	lpt_intr(sc);
238 	splx(s);
239 
240 	callout_reset(&sc->sc_wakeup_ch, STEP, lpt_wakeup, sc);
241 }
242 
243 /*
244  * Close the device, and free the local line buffer.
245  */
246 int
247 lptclose(dev, flag, mode, p)
248 	dev_t dev;
249 	int flag;
250 	int mode;
251 	struct proc *p;
252 {
253 	struct lpt_softc *sc;
254 	int unit;
255 
256 	unit = LPTUNIT(dev);
257 	sc = lpt_cd.cd_devs[unit];
258 
259 	if (sc->sc_count)
260 		(void) pushbytes(sc);
261 
262 	if ((sc->sc_flags & LPT_NOINTR) == 0)
263 		callout_stop(&sc->sc_wakeup_ch);
264 
265 	(sc->sc_funcs->lf_close) (sc);
266 
267 	sc->sc_state = 0;
268 	brelse(sc->sc_inbuf);
269 
270 	LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
271 	return (0);
272 }
273 
274 int
275 pushbytes(sc)
276 	struct lpt_softc *sc;
277 {
278 	int s, error, spin, tic;
279 
280 	if (sc->sc_flags & LPT_NOINTR) {
281 		while (sc->sc_count > 0) {
282 			spin = 0;
283 			while ((sc->sc_funcs->lf_notrdy) (sc, 0)) {
284 				if (++spin < sc->sc_spinmax)
285 					continue;
286 				tic = 0;
287 				/* adapt busy-wait algorithm */
288 				sc->sc_spinmax++;
289 				while ((sc->sc_funcs->lf_notrdy) (sc, 1)) {
290 					/* exponential backoff */
291 					tic = tic + tic + 1;
292 					if (tic > TIMEOUT)
293 						tic = TIMEOUT;
294 					error = tsleep((caddr_t) sc,
295 					    LPTPRI | PCATCH, "lptpsh", tic);
296 					if (error != EWOULDBLOCK)
297 						return (error);
298 				}
299 				break;
300 			}
301 
302 			(sc->sc_funcs->lf_wrdata) (sc, *sc->sc_cp++);
303 			sc->sc_count--;
304 
305 			/* adapt busy-wait algorithm */
306 			if (spin * 2 + 16 < sc->sc_spinmax)
307 				sc->sc_spinmax--;
308 		}
309 	} else {
310 		while (sc->sc_count > 0) {
311 			/* if the printer is ready for a char, give it one */
312 			if ((sc->sc_state & LPT_OBUSY) == 0) {
313 				LPRINTF(("%s: write %d\n", sc->sc_dev.dv_xname,
314 					sc->sc_count));
315 				s = spltty();
316 				(void) lpt_intr(sc);
317 				splx(s);
318 			}
319 			error = tsleep((caddr_t) sc, LPTPRI | PCATCH,
320 			    "lptwrite2", 0);
321 			if (error)
322 				return (error);
323 		}
324 	}
325 	return (0);
326 }
327 
328 /*
329  * Copy a line from user space to a local buffer, then call putc to get the
330  * chars moved to the output queue.
331  */
332 int
333 lptwrite(dev, uio, flags)
334 	dev_t dev;
335 	struct uio *uio;
336 	int flags;
337 {
338 	struct lpt_softc *sc;
339 	size_t n;
340 	int error;
341 
342 	sc = lpt_cd.cd_devs[LPTUNIT(dev)];
343 	error = 0;
344 
345 	while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
346 		uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
347 		sc->sc_count = n;
348 		error = pushbytes(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 lpt_intr(sc)
368 	struct lpt_softc *sc;
369 {
370 
371 	if (sc->sc_count) {
372 		/* send char */
373 		(sc->sc_funcs->lf_wrdata) (sc, *sc->sc_cp++);
374 		sc->sc_count--;
375 		sc->sc_state |= LPT_OBUSY;
376 	} else
377 		sc->sc_state &= ~LPT_OBUSY;
378 
379 	if (sc->sc_count == 0) {
380 		/* none, wake up the top half to get more */
381 		wakeup((caddr_t) sc);
382 	}
383 
384 	return (1);
385 }
386 
387 /* ARGSUSED */
388 int
389 lptioctl(dev, cmd, data, flag, p)
390 	dev_t dev;
391 	u_long cmd;
392 	caddr_t data;
393 	int flag;
394 	struct proc *p;
395 {
396 
397 	return (ENODEV);
398 }
399