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