lpt.c revision 1.64 1 1.64 perry /* $NetBSD: lpt.c,v 1.64 2005/02/27 00:27:02 perry Exp $ */
2 1.23 cgd
3 1.1 cgd /*
4 1.51 mycroft * Copyright (c) 1993, 1994 Charles M. Hannum.
5 1.1 cgd * Copyright (c) 1990 William F. Jolitz, TeleMuse
6 1.1 cgd * All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.64 perry * This software is a component of "386BSD" developed by
19 1.1 cgd * William F. Jolitz, TeleMuse.
20 1.1 cgd * 4. Neither the name of the developer nor the name "386BSD"
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.64 perry * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
25 1.64 perry * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
26 1.64 perry * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
27 1.64 perry * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
28 1.1 cgd * NOT MAKE USE OF THIS WORK.
29 1.1 cgd *
30 1.1 cgd * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
31 1.64 perry * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
32 1.64 perry * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
33 1.64 perry * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
34 1.64 perry * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
35 1.64 perry * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
36 1.64 perry * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
37 1.1 cgd * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
38 1.1 cgd *
39 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
40 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
43 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 1.1 cgd * SUCH DAMAGE.
50 1.1 cgd */
51 1.1 cgd
52 1.1 cgd /*
53 1.45 is * Device Driver for AT style parallel printer port
54 1.1 cgd */
55 1.58 lukem
56 1.58 lukem #include <sys/cdefs.h>
57 1.64 perry __KERNEL_RCSID(0, "$NetBSD: lpt.c,v 1.64 2005/02/27 00:27:02 perry Exp $");
58 1.1 cgd
59 1.9 mycroft #include <sys/param.h>
60 1.9 mycroft #include <sys/systm.h>
61 1.9 mycroft #include <sys/proc.h>
62 1.9 mycroft #include <sys/user.h>
63 1.47 thorpej #include <sys/malloc.h>
64 1.9 mycroft #include <sys/kernel.h>
65 1.9 mycroft #include <sys/ioctl.h>
66 1.9 mycroft #include <sys/uio.h>
67 1.11 mycroft #include <sys/device.h>
68 1.38 christos #include <sys/conf.h>
69 1.11 mycroft #include <sys/syslog.h>
70 1.1 cgd
71 1.39 mycroft #include <machine/bus.h>
72 1.37 cgd #include <machine/intr.h>
73 1.9 mycroft
74 1.45 is #include <dev/ic/lptreg.h>
75 1.45 is #include <dev/ic/lptvar.h>
76 1.1 cgd
77 1.11 mycroft #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
78 1.11 mycroft #define STEP hz/4
79 1.11 mycroft
80 1.11 mycroft #define LPTPRI (PZERO+8)
81 1.11 mycroft #define LPT_BSIZE 1024
82 1.1 cgd
83 1.45 is #define LPTDEBUG
84 1.45 is
85 1.44 mikel #ifndef LPTDEBUG
86 1.38 christos #define LPRINTF(a)
87 1.1 cgd #else
88 1.44 mikel #define LPRINTF(a) if (lptdebug) printf a
89 1.44 mikel int lptdebug = 0;
90 1.1 cgd #endif
91 1.1 cgd
92 1.59 gehenna extern struct cfdriver lpt_cd;
93 1.38 christos
94 1.59 gehenna dev_type_open(lptopen);
95 1.59 gehenna dev_type_close(lptclose);
96 1.59 gehenna dev_type_write(lptwrite);
97 1.59 gehenna dev_type_ioctl(lptioctl);
98 1.59 gehenna
99 1.59 gehenna const struct cdevsw lpt_cdevsw = {
100 1.59 gehenna lptopen, lptclose, noread, lptwrite, lptioctl,
101 1.60 jdolecek nostop, notty, nopoll, nommap, nokqfilter,
102 1.59 gehenna };
103 1.1 cgd
104 1.11 mycroft #define LPTUNIT(s) (minor(s) & 0x1f)
105 1.11 mycroft #define LPTFLAGS(s) (minor(s) & 0xe0)
106 1.1 cgd
107 1.15 mycroft void
108 1.45 is lpt_attach_subr(sc)
109 1.45 is struct lpt_softc *sc;
110 1.1 cgd {
111 1.42 thorpej bus_space_tag_t iot;
112 1.42 thorpej bus_space_handle_t ioh;
113 1.15 mycroft
114 1.6 mycroft sc->sc_state = 0;
115 1.32 cgd
116 1.45 is iot = sc->sc_iot;
117 1.45 is ioh = sc->sc_ioh;
118 1.32 cgd
119 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
120 1.49 cgd
121 1.56 thorpej callout_init(&sc->sc_wakeup_ch);
122 1.56 thorpej
123 1.49 cgd sc->sc_dev_ok = 1;
124 1.1 cgd }
125 1.1 cgd
126 1.1 cgd /*
127 1.11 mycroft * Reset the printer, then wait until it's selected and not busy.
128 1.1 cgd */
129 1.11 mycroft int
130 1.63 fvdl lptopen(dev, flag, mode, p)
131 1.1 cgd dev_t dev;
132 1.1 cgd int flag;
133 1.38 christos int mode;
134 1.63 fvdl struct proc *p;
135 1.1 cgd {
136 1.11 mycroft u_char flags = LPTFLAGS(dev);
137 1.11 mycroft struct lpt_softc *sc;
138 1.42 thorpej bus_space_tag_t iot;
139 1.42 thorpej bus_space_handle_t ioh;
140 1.11 mycroft u_char control;
141 1.11 mycroft int error;
142 1.11 mycroft int spin;
143 1.11 mycroft
144 1.57 thorpej sc = device_lookup(&lpt_cd, LPTUNIT(dev));
145 1.49 cgd if (!sc || !sc->sc_dev_ok)
146 1.11 mycroft return ENXIO;
147 1.11 mycroft
148 1.45 is #if 0 /* XXX what to do? */
149 1.25 mycroft if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
150 1.11 mycroft return ENXIO;
151 1.45 is #endif
152 1.11 mycroft
153 1.11 mycroft #ifdef DIAGNOSTIC
154 1.11 mycroft if (sc->sc_state)
155 1.41 christos printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
156 1.11 mycroft sc->sc_state);
157 1.11 mycroft #endif
158 1.11 mycroft
159 1.11 mycroft if (sc->sc_state)
160 1.11 mycroft return EBUSY;
161 1.1 cgd
162 1.11 mycroft sc->sc_state = LPT_INIT;
163 1.11 mycroft sc->sc_flags = flags;
164 1.44 mikel LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname,
165 1.44 mikel (unsigned)flags));
166 1.42 thorpej iot = sc->sc_iot;
167 1.32 cgd ioh = sc->sc_ioh;
168 1.11 mycroft
169 1.11 mycroft if ((flags & LPT_NOPRIME) == 0) {
170 1.11 mycroft /* assert INIT for 100 usec to start up printer */
171 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
172 1.14 mycroft delay(100);
173 1.1 cgd }
174 1.11 mycroft
175 1.11 mycroft control = LPC_SELECT | LPC_NINIT;
176 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control);
177 1.1 cgd
178 1.1 cgd /* wait till ready (printer running diagnostics) */
179 1.25 mycroft for (spin = 0; NOT_READY_ERR(); spin += STEP) {
180 1.11 mycroft if (spin >= TIMEOUT) {
181 1.1 cgd sc->sc_state = 0;
182 1.11 mycroft return EBUSY;
183 1.1 cgd }
184 1.1 cgd
185 1.1 cgd /* wait 1/4 second, give up if we get a signal */
186 1.38 christos error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
187 1.38 christos if (error != EWOULDBLOCK) {
188 1.1 cgd sc->sc_state = 0;
189 1.11 mycroft return error;
190 1.1 cgd }
191 1.1 cgd }
192 1.1 cgd
193 1.11 mycroft if ((flags & LPT_NOINTR) == 0)
194 1.11 mycroft control |= LPC_IENABLE;
195 1.11 mycroft if (flags & LPT_AUTOLF)
196 1.11 mycroft control |= LPC_AUTOLF;
197 1.11 mycroft sc->sc_control = control;
198 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control);
199 1.11 mycroft
200 1.47 thorpej sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK);
201 1.11 mycroft sc->sc_count = 0;
202 1.11 mycroft sc->sc_state = LPT_OPEN;
203 1.25 mycroft
204 1.25 mycroft if ((sc->sc_flags & LPT_NOINTR) == 0)
205 1.25 mycroft lptwakeup(sc);
206 1.11 mycroft
207 1.38 christos LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
208 1.11 mycroft return 0;
209 1.1 cgd }
210 1.1 cgd
211 1.11 mycroft int
212 1.46 is lptnotready(status, sc)
213 1.11 mycroft u_char status;
214 1.1 cgd struct lpt_softc *sc;
215 1.11 mycroft {
216 1.21 mycroft u_char new;
217 1.21 mycroft
218 1.25 mycroft status = (status ^ LPS_INVERT) & LPS_MASK;
219 1.21 mycroft new = status & ~sc->sc_laststatus;
220 1.21 mycroft sc->sc_laststatus = status;
221 1.1 cgd
222 1.52 perry if (sc->sc_state & LPT_OPEN) {
223 1.52 perry if (new & LPS_SELECT)
224 1.52 perry log(LOG_NOTICE,
225 1.52 perry "%s: offline\n", sc->sc_dev.dv_xname);
226 1.52 perry else if (new & LPS_NOPAPER)
227 1.52 perry log(LOG_NOTICE,
228 1.52 perry "%s: out of paper\n", sc->sc_dev.dv_xname);
229 1.52 perry else if (new & LPS_NERR)
230 1.52 perry log(LOG_NOTICE,
231 1.52 perry "%s: output error\n", sc->sc_dev.dv_xname);
232 1.52 perry }
233 1.64 perry
234 1.25 mycroft return status;
235 1.11 mycroft }
236 1.11 mycroft
237 1.11 mycroft void
238 1.25 mycroft lptwakeup(arg)
239 1.18 cgd void *arg;
240 1.18 cgd {
241 1.25 mycroft struct lpt_softc *sc = arg;
242 1.11 mycroft int s;
243 1.11 mycroft
244 1.50 is s = spllpt();
245 1.16 mycroft lptintr(sc);
246 1.13 mycroft splx(s);
247 1.11 mycroft
248 1.56 thorpej callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
249 1.1 cgd }
250 1.1 cgd
251 1.1 cgd /*
252 1.11 mycroft * Close the device, and free the local line buffer.
253 1.1 cgd */
254 1.36 mycroft int
255 1.63 fvdl lptclose(dev, flag, mode, p)
256 1.11 mycroft dev_t dev;
257 1.1 cgd int flag;
258 1.38 christos int mode;
259 1.63 fvdl struct proc *p;
260 1.1 cgd {
261 1.57 thorpej struct lpt_softc *sc = device_lookup(&lpt_cd, LPTUNIT(dev));
262 1.42 thorpej bus_space_tag_t iot = sc->sc_iot;
263 1.42 thorpej bus_space_handle_t ioh = sc->sc_ioh;
264 1.1 cgd
265 1.11 mycroft if (sc->sc_count)
266 1.46 is (void) lptpushbytes(sc);
267 1.1 cgd
268 1.25 mycroft if ((sc->sc_flags & LPT_NOINTR) == 0)
269 1.56 thorpej callout_stop(&sc->sc_wakeup_ch);
270 1.25 mycroft
271 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
272 1.1 cgd sc->sc_state = 0;
273 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
274 1.47 thorpej free(sc->sc_inbuf, M_DEVBUF);
275 1.11 mycroft
276 1.38 christos LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
277 1.11 mycroft return 0;
278 1.11 mycroft }
279 1.11 mycroft
280 1.11 mycroft int
281 1.46 is lptpushbytes(sc)
282 1.11 mycroft struct lpt_softc *sc;
283 1.11 mycroft {
284 1.42 thorpej bus_space_tag_t iot = sc->sc_iot;
285 1.42 thorpej bus_space_handle_t ioh = sc->sc_ioh;
286 1.11 mycroft int error;
287 1.11 mycroft
288 1.11 mycroft if (sc->sc_flags & LPT_NOINTR) {
289 1.11 mycroft int spin, tic;
290 1.11 mycroft u_char control = sc->sc_control;
291 1.11 mycroft
292 1.11 mycroft while (sc->sc_count > 0) {
293 1.11 mycroft spin = 0;
294 1.25 mycroft while (NOT_READY()) {
295 1.25 mycroft if (++spin < sc->sc_spinmax)
296 1.25 mycroft continue;
297 1.11 mycroft tic = 0;
298 1.12 mycroft /* adapt busy-wait algorithm */
299 1.12 mycroft sc->sc_spinmax++;
300 1.25 mycroft while (NOT_READY_ERR()) {
301 1.11 mycroft /* exponential backoff */
302 1.11 mycroft tic = tic + tic + 1;
303 1.11 mycroft if (tic > TIMEOUT)
304 1.11 mycroft tic = TIMEOUT;
305 1.11 mycroft error = tsleep((caddr_t)sc,
306 1.11 mycroft LPTPRI | PCATCH, "lptpsh", tic);
307 1.11 mycroft if (error != EWOULDBLOCK)
308 1.11 mycroft return error;
309 1.11 mycroft }
310 1.25 mycroft break;
311 1.11 mycroft }
312 1.11 mycroft
313 1.42 thorpej bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
314 1.61 rafal DELAY(1);
315 1.44 mikel bus_space_write_1(iot, ioh, lpt_control,
316 1.44 mikel control | LPC_STROBE);
317 1.61 rafal DELAY(1);
318 1.11 mycroft sc->sc_count--;
319 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control);
320 1.61 rafal DELAY(1);
321 1.11 mycroft
322 1.11 mycroft /* adapt busy-wait algorithm */
323 1.25 mycroft if (spin*2 + 16 < sc->sc_spinmax)
324 1.12 mycroft sc->sc_spinmax--;
325 1.11 mycroft }
326 1.11 mycroft } else {
327 1.11 mycroft int s;
328 1.11 mycroft
329 1.11 mycroft while (sc->sc_count > 0) {
330 1.11 mycroft /* if the printer is ready for a char, give it one */
331 1.11 mycroft if ((sc->sc_state & LPT_OBUSY) == 0) {
332 1.53 thorpej LPRINTF(("%s: write %lu\n", sc->sc_dev.dv_xname,
333 1.53 thorpej (u_long)sc->sc_count));
334 1.50 is s = spllpt();
335 1.16 mycroft (void) lptintr(sc);
336 1.11 mycroft splx(s);
337 1.11 mycroft }
338 1.38 christos error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
339 1.38 christos "lptwrite2", 0);
340 1.38 christos if (error)
341 1.11 mycroft return error;
342 1.11 mycroft }
343 1.11 mycroft }
344 1.11 mycroft return 0;
345 1.1 cgd }
346 1.1 cgd
347 1.64 perry /*
348 1.11 mycroft * Copy a line from user space to a local buffer, then call putc to get the
349 1.11 mycroft * chars moved to the output queue.
350 1.1 cgd */
351 1.36 mycroft int
352 1.38 christos lptwrite(dev, uio, flags)
353 1.1 cgd dev_t dev;
354 1.1 cgd struct uio *uio;
355 1.38 christos int flags;
356 1.1 cgd {
357 1.57 thorpej struct lpt_softc *sc = device_lookup(&lpt_cd, LPTUNIT(dev));
358 1.11 mycroft size_t n;
359 1.11 mycroft int error = 0;
360 1.11 mycroft
361 1.38 christos while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
362 1.47 thorpej uiomove(sc->sc_cp = sc->sc_inbuf, n, uio);
363 1.11 mycroft sc->sc_count = n;
364 1.46 is error = lptpushbytes(sc);
365 1.11 mycroft if (error) {
366 1.11 mycroft /*
367 1.11 mycroft * Return accurate residual if interrupted or timed
368 1.11 mycroft * out.
369 1.11 mycroft */
370 1.11 mycroft uio->uio_resid += sc->sc_count;
371 1.11 mycroft sc->sc_count = 0;
372 1.11 mycroft return error;
373 1.1 cgd }
374 1.1 cgd }
375 1.11 mycroft return 0;
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd /*
379 1.11 mycroft * Handle printer interrupts which occur when the printer is ready to accept
380 1.11 mycroft * another char.
381 1.1 cgd */
382 1.11 mycroft int
383 1.30 cgd lptintr(arg)
384 1.30 cgd void *arg;
385 1.1 cgd {
386 1.30 cgd struct lpt_softc *sc = arg;
387 1.42 thorpej bus_space_tag_t iot = sc->sc_iot;
388 1.42 thorpej bus_space_handle_t ioh = sc->sc_ioh;
389 1.1 cgd
390 1.13 mycroft #if 0
391 1.11 mycroft if ((sc->sc_state & LPT_OPEN) == 0)
392 1.11 mycroft return 0;
393 1.13 mycroft #endif
394 1.6 mycroft
395 1.11 mycroft /* is printer online and ready for output */
396 1.25 mycroft if (NOT_READY() && NOT_READY_ERR())
397 1.11 mycroft return 0;
398 1.6 mycroft
399 1.11 mycroft if (sc->sc_count) {
400 1.13 mycroft u_char control = sc->sc_control;
401 1.11 mycroft /* send char */
402 1.42 thorpej bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
403 1.55 perry DELAY(1);
404 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
405 1.55 perry DELAY(1);
406 1.13 mycroft sc->sc_count--;
407 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, control);
408 1.61 rafal DELAY(1);
409 1.11 mycroft sc->sc_state |= LPT_OBUSY;
410 1.11 mycroft } else
411 1.11 mycroft sc->sc_state &= ~LPT_OBUSY;
412 1.1 cgd
413 1.11 mycroft if (sc->sc_count == 0) {
414 1.1 cgd /* none, wake up the top half to get more */
415 1.1 cgd wakeup((caddr_t)sc);
416 1.11 mycroft }
417 1.11 mycroft
418 1.11 mycroft return 1;
419 1.1 cgd }
420 1.1 cgd
421 1.2 cgd int
422 1.63 fvdl lptioctl(dev, cmd, data, flag, p)
423 1.11 mycroft dev_t dev;
424 1.24 cgd u_long cmd;
425 1.11 mycroft caddr_t data;
426 1.11 mycroft int flag;
427 1.63 fvdl struct proc *p;
428 1.2 cgd {
429 1.11 mycroft int error = 0;
430 1.2 cgd
431 1.2 cgd switch (cmd) {
432 1.2 cgd default:
433 1.2 cgd error = ENODEV;
434 1.2 cgd }
435 1.2 cgd
436 1.11 mycroft return error;
437 1.2 cgd }
438