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