lpt_isa.c revision 1.44 1 /* $NetBSD: lpt_isa.c,v 1.44 1997/09/02 01:37:19 mikel 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 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/buf.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/isa/isavar.h>
72 #include <dev/isa/lptreg.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 #ifndef LPTDEBUG
81 #define LPRINTF(a)
82 #else
83 #define LPRINTF(a) if (lptdebug) printf a
84 int lptdebug = 0;
85 #endif
86
87 struct lpt_softc {
88 struct device sc_dev;
89 void *sc_ih;
90
91 size_t sc_count;
92 struct buf *sc_inbuf;
93 u_char *sc_cp;
94 int sc_spinmax;
95 int sc_iobase;
96 bus_space_tag_t sc_iot;
97 bus_space_handle_t sc_ioh;
98 int sc_irq;
99 u_char sc_state;
100 #define LPT_OPEN 0x01 /* device is open */
101 #define LPT_OBUSY 0x02 /* printer is busy doing output */
102 #define LPT_INIT 0x04 /* waiting to initialize for open */
103 u_char sc_flags;
104 #define LPT_AUTOLF 0x20 /* automatic LF on CR */
105 #define LPT_NOPRIME 0x40 /* don't prime on open */
106 #define LPT_NOINTR 0x80 /* do not use interrupt */
107 u_char sc_control;
108 u_char sc_laststatus;
109 };
110
111 /* XXX does not belong here */
112 cdev_decl(lpt);
113
114 #ifdef __BROKEN_INDIRECT_CONFIG
115 int lptprobe __P((struct device *, void *, void *));
116 #else
117 int lptprobe __P((struct device *, struct cfdata *, void *));
118 #endif
119 void lptattach __P((struct device *, struct device *, void *));
120 int lptintr __P((void *));
121
122 struct cfattach lpt_ca = {
123 sizeof(struct lpt_softc), lptprobe, lptattach
124 };
125
126 struct cfdriver lpt_cd = {
127 NULL, "lpt", DV_TTY
128 };
129
130 #define LPTUNIT(s) (minor(s) & 0x1f)
131 #define LPTFLAGS(s) (minor(s) & 0xe0)
132
133 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
134 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
135 #define NOT_READY() ((bus_space_read_1(iot, ioh, lpt_status) ^ LPS_INVERT) & LPS_MASK)
136 #define NOT_READY_ERR() not_ready(bus_space_read_1(iot, ioh, lpt_status), sc)
137 static int not_ready __P((u_char, struct lpt_softc *));
138
139 static void lptwakeup __P((void *arg));
140 static int pushbytes __P((struct lpt_softc *));
141
142 int lpt_port_test __P((bus_space_tag_t, bus_space_handle_t, bus_addr_t,
143 bus_size_t, u_char, u_char));
144
145 /*
146 * Internal routine to lptprobe to do port tests of one byte value.
147 */
148 int
149 lpt_port_test(iot, ioh, base, off, data, mask)
150 bus_space_tag_t iot;
151 bus_space_handle_t ioh;
152 bus_addr_t base;
153 bus_size_t off;
154 u_char data, mask;
155 {
156 int timeout;
157 u_char temp;
158
159 data &= mask;
160 bus_space_write_1(iot, ioh, off, data);
161 timeout = 1000;
162 do {
163 delay(10);
164 temp = bus_space_read_1(iot, ioh, off) & mask;
165 } while (temp != data && --timeout);
166 LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
167 (unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
168 return (temp == data);
169 }
170
171 /*
172 * Logic:
173 * 1) You should be able to write to and read back the same value
174 * to the data port. Do an alternating zeros, alternating ones,
175 * walking zero, and walking one test to check for stuck bits.
176 *
177 * 2) You should be able to write to and read back the same value
178 * to the control port lower 5 bits, the upper 3 bits are reserved
179 * per the IBM PC technical reference manauls and different boards
180 * do different things with them. Do an alternating zeros, alternating
181 * ones, walking zero, and walking one test to check for stuck bits.
182 *
183 * Some printers drag the strobe line down when the are powered off
184 * so this bit has been masked out of the control port test.
185 *
186 * XXX Some printers may not like a fast pulse on init or strobe, I
187 * don't know at this point, if that becomes a problem these bits
188 * should be turned off in the mask byte for the control port test.
189 *
190 * 3) Set the data and control ports to a value of 0
191 */
192 int
193 lptprobe(parent, match, aux)
194 struct device *parent;
195 #ifdef __BROKEN_INDIRECT_CONFIG
196 void *match;
197 #else
198 struct cfdata *match;
199 #endif
200 void *aux;
201 {
202 struct isa_attach_args *ia = aux;
203 bus_space_tag_t iot;
204 bus_space_handle_t ioh;
205 u_long base;
206 u_char mask, data;
207 int i, rv;
208
209 #ifdef DEBUG
210 #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
211 goto out;} while (0)
212 #else
213 #define ABORT goto out
214 #endif
215
216 iot = ia->ia_iot;
217 base = ia->ia_iobase;
218 if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
219 return 0;
220
221 rv = 0;
222 mask = 0xff;
223
224 data = 0x55; /* Alternating zeros */
225 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
226 ABORT;
227
228 data = 0xaa; /* Alternating ones */
229 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
230 ABORT;
231
232 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
233 data = ~(1 << i);
234 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
235 ABORT;
236 }
237
238 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
239 data = (1 << i);
240 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
241 ABORT;
242 }
243
244 bus_space_write_1(iot, ioh, lpt_data, 0);
245 bus_space_write_1(iot, ioh, lpt_control, 0);
246
247 ia->ia_iosize = LPT_NPORTS;
248 ia->ia_msize = 0;
249
250 rv = 1;
251
252 out:
253 bus_space_unmap(iot, ioh, LPT_NPORTS);
254 return rv;
255 }
256
257 void
258 lptattach(parent, self, aux)
259 struct device *parent, *self;
260 void *aux;
261 {
262 struct lpt_softc *sc = (void *)self;
263 struct isa_attach_args *ia = aux;
264 bus_space_tag_t iot;
265 bus_space_handle_t ioh;
266
267 if (ia->ia_irq != IRQUNK)
268 printf("\n");
269 else
270 printf(": polled\n");
271
272 sc->sc_iobase = ia->ia_iobase;
273 sc->sc_irq = ia->ia_irq;
274 sc->sc_state = 0;
275
276 iot = sc->sc_iot = ia->ia_iot;
277 if (bus_space_map(iot, sc->sc_iobase, LPT_NPORTS, 0, &ioh))
278 panic("lptattach: couldn't map I/O ports");
279 sc->sc_ioh = ioh;
280
281 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
282
283 if (ia->ia_irq != IRQUNK)
284 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
285 IPL_TTY, lptintr, sc);
286 }
287
288 /*
289 * Reset the printer, then wait until it's selected and not busy.
290 */
291 int
292 lptopen(dev, flag, mode, p)
293 dev_t dev;
294 int flag;
295 int mode;
296 struct proc *p;
297 {
298 int unit = LPTUNIT(dev);
299 u_char flags = LPTFLAGS(dev);
300 struct lpt_softc *sc;
301 bus_space_tag_t iot;
302 bus_space_handle_t ioh;
303 u_char control;
304 int error;
305 int spin;
306
307 if (unit >= lpt_cd.cd_ndevs)
308 return ENXIO;
309 sc = lpt_cd.cd_devs[unit];
310 if (!sc)
311 return ENXIO;
312
313 if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
314 return ENXIO;
315
316 #ifdef DIAGNOSTIC
317 if (sc->sc_state)
318 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
319 sc->sc_state);
320 #endif
321
322 if (sc->sc_state)
323 return EBUSY;
324
325 sc->sc_state = LPT_INIT;
326 sc->sc_flags = flags;
327 LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname,
328 (unsigned)flags));
329 iot = sc->sc_iot;
330 ioh = sc->sc_ioh;
331
332 if ((flags & LPT_NOPRIME) == 0) {
333 /* assert INIT for 100 usec to start up printer */
334 bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
335 delay(100);
336 }
337
338 control = LPC_SELECT | LPC_NINIT;
339 bus_space_write_1(iot, ioh, lpt_control, control);
340
341 /* wait till ready (printer running diagnostics) */
342 for (spin = 0; NOT_READY_ERR(); spin += STEP) {
343 if (spin >= TIMEOUT) {
344 sc->sc_state = 0;
345 return EBUSY;
346 }
347
348 /* wait 1/4 second, give up if we get a signal */
349 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
350 if (error != EWOULDBLOCK) {
351 sc->sc_state = 0;
352 return error;
353 }
354 }
355
356 if ((flags & LPT_NOINTR) == 0)
357 control |= LPC_IENABLE;
358 if (flags & LPT_AUTOLF)
359 control |= LPC_AUTOLF;
360 sc->sc_control = control;
361 bus_space_write_1(iot, ioh, lpt_control, control);
362
363 sc->sc_inbuf = geteblk(LPT_BSIZE);
364 sc->sc_count = 0;
365 sc->sc_state = LPT_OPEN;
366
367 if ((sc->sc_flags & LPT_NOINTR) == 0)
368 lptwakeup(sc);
369
370 LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
371 return 0;
372 }
373
374 int
375 not_ready(status, sc)
376 u_char status;
377 struct lpt_softc *sc;
378 {
379 u_char new;
380
381 status = (status ^ LPS_INVERT) & LPS_MASK;
382 new = status & ~sc->sc_laststatus;
383 sc->sc_laststatus = status;
384
385 if (new & LPS_SELECT)
386 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
387 else if (new & LPS_NOPAPER)
388 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
389 else if (new & LPS_NERR)
390 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
391
392 return status;
393 }
394
395 void
396 lptwakeup(arg)
397 void *arg;
398 {
399 struct lpt_softc *sc = arg;
400 int s;
401
402 s = spltty();
403 lptintr(sc);
404 splx(s);
405
406 timeout(lptwakeup, sc, STEP);
407 }
408
409 /*
410 * Close the device, and free the local line buffer.
411 */
412 int
413 lptclose(dev, flag, mode, p)
414 dev_t dev;
415 int flag;
416 int mode;
417 struct proc *p;
418 {
419 int unit = LPTUNIT(dev);
420 struct lpt_softc *sc = lpt_cd.cd_devs[unit];
421 bus_space_tag_t iot = sc->sc_iot;
422 bus_space_handle_t ioh = sc->sc_ioh;
423
424 if (sc->sc_count)
425 (void) pushbytes(sc);
426
427 if ((sc->sc_flags & LPT_NOINTR) == 0)
428 untimeout(lptwakeup, sc);
429
430 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
431 sc->sc_state = 0;
432 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
433 brelse(sc->sc_inbuf);
434
435 LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
436 return 0;
437 }
438
439 int
440 pushbytes(sc)
441 struct lpt_softc *sc;
442 {
443 bus_space_tag_t iot = sc->sc_iot;
444 bus_space_handle_t ioh = sc->sc_ioh;
445 int error;
446
447 if (sc->sc_flags & LPT_NOINTR) {
448 int spin, tic;
449 u_char control = sc->sc_control;
450
451 while (sc->sc_count > 0) {
452 spin = 0;
453 while (NOT_READY()) {
454 if (++spin < sc->sc_spinmax)
455 continue;
456 tic = 0;
457 /* adapt busy-wait algorithm */
458 sc->sc_spinmax++;
459 while (NOT_READY_ERR()) {
460 /* exponential backoff */
461 tic = tic + tic + 1;
462 if (tic > TIMEOUT)
463 tic = TIMEOUT;
464 error = tsleep((caddr_t)sc,
465 LPTPRI | PCATCH, "lptpsh", tic);
466 if (error != EWOULDBLOCK)
467 return error;
468 }
469 break;
470 }
471
472 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
473 bus_space_write_1(iot, ioh, lpt_control,
474 control | LPC_STROBE);
475 sc->sc_count--;
476 bus_space_write_1(iot, ioh, lpt_control, control);
477
478 /* adapt busy-wait algorithm */
479 if (spin*2 + 16 < sc->sc_spinmax)
480 sc->sc_spinmax--;
481 }
482 } else {
483 int s;
484
485 while (sc->sc_count > 0) {
486 /* if the printer is ready for a char, give it one */
487 if ((sc->sc_state & LPT_OBUSY) == 0) {
488 LPRINTF(("%s: write %u\n", sc->sc_dev.dv_xname,
489 sc->sc_count));
490 s = spltty();
491 (void) lptintr(sc);
492 splx(s);
493 }
494 error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
495 "lptwrite2", 0);
496 if (error)
497 return error;
498 }
499 }
500 return 0;
501 }
502
503 /*
504 * Copy a line from user space to a local buffer, then call putc to get the
505 * chars moved to the output queue.
506 */
507 int
508 lptwrite(dev, uio, flags)
509 dev_t dev;
510 struct uio *uio;
511 int flags;
512 {
513 struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)];
514 size_t n;
515 int error = 0;
516
517 while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
518 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
519 sc->sc_count = n;
520 error = pushbytes(sc);
521 if (error) {
522 /*
523 * Return accurate residual if interrupted or timed
524 * out.
525 */
526 uio->uio_resid += sc->sc_count;
527 sc->sc_count = 0;
528 return error;
529 }
530 }
531 return 0;
532 }
533
534 /*
535 * Handle printer interrupts which occur when the printer is ready to accept
536 * another char.
537 */
538 int
539 lptintr(arg)
540 void *arg;
541 {
542 struct lpt_softc *sc = arg;
543 bus_space_tag_t iot = sc->sc_iot;
544 bus_space_handle_t ioh = sc->sc_ioh;
545
546 #if 0
547 if ((sc->sc_state & LPT_OPEN) == 0)
548 return 0;
549 #endif
550
551 /* is printer online and ready for output */
552 if (NOT_READY() && NOT_READY_ERR())
553 return 0;
554
555 if (sc->sc_count) {
556 u_char control = sc->sc_control;
557 /* send char */
558 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
559 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
560 sc->sc_count--;
561 bus_space_write_1(iot, ioh, lpt_control, control);
562 sc->sc_state |= LPT_OBUSY;
563 } else
564 sc->sc_state &= ~LPT_OBUSY;
565
566 if (sc->sc_count == 0) {
567 /* none, wake up the top half to get more */
568 wakeup((caddr_t)sc);
569 }
570
571 return 1;
572 }
573
574 int
575 lptioctl(dev, cmd, data, flag, p)
576 dev_t dev;
577 u_long cmd;
578 caddr_t data;
579 int flag;
580 struct proc *p;
581 {
582 int error = 0;
583
584 switch (cmd) {
585 default:
586 error = ENODEV;
587 }
588
589 return error;
590 }
591