lpt.c revision 1.7.4.14 1 /*
2 * Copyright (c) 1993 Charles Hannum.
3 * Copyright (c) 1990 William F. Jolitz, TeleMuse
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This software is a component of "386BSD" developed by
17 * William F. Jolitz, TeleMuse.
18 * 4. Neither the name of the developer nor the name "386BSD"
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
23 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
24 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
25 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
26 * NOT MAKE USE OF THIS WORK.
27 *
28 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
29 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
30 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
31 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
32 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
33 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
34 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
35 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 *
49 * $Id: lpt.c,v 1.7.4.14 1993/10/29 20:03:39 mycroft Exp $
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/syslog.h>
66
67 #include <machine/cpu.h>
68 #include <machine/pio.h>
69
70 #include <i386/isa/isavar.h>
71 #include <i386/isa/icu.h>
72 #include <i386/isa/lptreg.h>
73
74 #define TIMEOUT hz*16 /* wait up to 4 seconds for a ready */
75 #define STEP hz/4
76
77 #define MAX_SPIN 255
78
79 #define LPTPRI (PZERO+8)
80 #define LPT_BSIZE 1024
81
82 #ifndef DEBUG
83 #define lprintf
84 #else
85 #define lprintf if (lptdebug) printf
86 int lptdebug = 1;
87 #endif
88
89 struct lpt_softc {
90 struct device sc_dev;
91 struct isadev sc_id;
92 struct intrhand sc_ih;
93
94 size_t sc_count;
95 struct buf *sc_inbuf;
96 u_char *sc_cp ;
97 u_short sc_iobase;
98 u_short sc_irq;
99 u_char sc_state;
100 /* bits for state */
101 #define LPT_OPEN 0x01 /* device is open */
102 #define LPT_OBUSY 0x02 /* printer is busy doing output */
103 #define LPT_INIT 0x04 /* waiting to initialize for open */
104 u_char sc_flags;
105 #define LPT_AUTOLF 0x20 /* automatic LF on CR */
106 #define LPT_NOPRIME 0x40 /* don't prime on open */
107 #define LPT_NOINTR 0x80 /* do not use interrupt */
108 u_char sc_control;
109 u_char sc_smax;
110 };
111
112 static int lptprobe __P((struct device *, struct cfdata *, void *));
113 #if 0 /* XXX */
114 static void lptforceintr __P((void *));
115 #endif
116 static void lptattach __P((struct device *, struct device *, void *));
117 static int lptintr __P((void *));
118
119 struct cfdriver lptcd =
120 { NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc) };
121
122 #define LPTUNIT(s) ((s)&0x1f)
123 #define LPTFLAGS(s) ((s)&0xe0)
124
125 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
126 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
127 #define NOT_READY() isready(inb(iobase + lpt_status), sc)
128
129 static int isready __P((u_char, struct lpt_softc *));
130 static void lptout __P((struct lpt_softc *));
131 static int pushbytes __P((struct lpt_softc *));
132
133 /*
134 * Internal routine to lptprobe to do port tests of one byte value
135 */
136 int
137 lpt_port_test(port, data, mask)
138 u_short port;
139 u_char data, mask;
140 {
141 int timeout;
142 u_char temp;
143
144 data &= mask;
145 outb(port, data);
146 timeout = 100;
147 do {
148 temp = inb(port) & mask;
149 } while (temp != data && --timeout);
150 lprintf("lpt: port=0x%x out=0x%x in=0x%x\n", port, data, temp);
151 return (temp == data);
152 }
153
154 /*
155 * Logic:
156 * 1) You should be able to write to and read back the same value
157 * to the data port. Do an alternating zeros, alternating ones,
158 * walking zero, and walking one test to check for stuck bits.
159 *
160 * 2) You should be able to write to and read back the same value
161 * to the control port lower 5 bits, the upper 3 bits are reserved
162 * per the IBM PC technical reference manauls and different boards
163 * do different things with them. Do an alternating zeros, alternating
164 * ones, walking zero, and walking one test to check for stuck bits.
165 *
166 * Some printers drag the strobe line down when the are powered off
167 * so this bit has been masked out of the control port test.
168 *
169 * XXX Some printers may not like a fast pulse on init or strobe, I
170 * don't know at this point, if that becomes a problem these bits
171 * should be turned off in the mask byte for the control port test.
172 *
173 * 3) Set the data and control ports to a value of 0
174 */
175
176 static int
177 lptprobe(parent, cf, aux)
178 struct device *parent;
179 struct cfdata *cf;
180 void *aux;
181 {
182 struct isa_attach_args *ia = aux;
183 u_short iobase = ia->ia_iobase;
184 u_short port = iobase + lpt_data;
185 u_char mask = 0xff;
186 u_char data;
187 int i;
188
189 if (iobase == IOBASEUNK)
190 return 0;
191
192 #ifdef DEBUG
193 #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
194 return 0;} while (0)
195 #else
196 #define ABORT return 0
197 #endif
198
199 for (;;) {
200 data = 0x55; /* Alternating zeros */
201 if (!lpt_port_test(port, data, mask))
202 ABORT;
203
204 data = 0xaa; /* Alternating ones */
205 if (!lpt_port_test(port, data, mask))
206 ABORT;
207
208 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
209 data = ~(1 << i);
210 if (!lpt_port_test(port, data, mask))
211 ABORT;
212 }
213
214 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
215 data = (1 << i);
216 if (!lpt_port_test(port, data, mask))
217 ABORT;
218 }
219
220 if (port == iobase + lpt_data) {
221 port = iobase + lpt_control;
222 #if 1 /* XXX */
223 mask = 0x04;
224 #else
225 mask = 0x1e;
226 #endif
227 } else
228 break;
229 }
230 outb(iobase + lpt_data, 0);
231 outb(iobase + lpt_control, 0);
232
233 if (ia->ia_irq == IRQUNK)
234 #if 0 /* XXX */
235 ia->ia_irq = isa_discoverintr(lptforceintr, aux);
236 #else
237 ia->ia_irq = IRQNONE;
238 #endif
239 /* okay if we don't have an irq; will force polling */
240
241 ia->ia_iosize = LPT_NPORTS;
242 ia->ia_drq = DRQUNK;
243 ia->ia_msize = 0;
244 return 1;
245 }
246
247 #if 0 /* XXX */
248 static void
249 lptforceintr(aux)
250 void *aux;
251 {
252 struct isa_attach_args *ia = aux;
253 u_short iobase = ia->ia_iobase;
254
255 outb(iobase + lpt_control, LPC_IENABLE);
256 delay(100);
257 outb(iobase + lpt_control, 0);
258 }
259 #endif
260
261 static void
262 lptattach(parent, self, aux)
263 struct device *parent, *self;
264 void *aux;
265 {
266 struct isa_attach_args *ia = aux;
267 struct lpt_softc *sc = (struct lpt_softc *)self;
268 u_short iobase = ia->ia_iobase;
269 u_short irq = ia->ia_irq;
270
271 sc->sc_iobase = iobase;
272 sc->sc_irq = irq;
273 sc->sc_state = 0;
274 outb(iobase + lpt_control, LPC_NINIT);
275
276 printf(": %sparallel port\n", irq == IRQNONE ? "polled " : "");
277 isa_establish(&sc->sc_id, &sc->sc_dev);
278
279 if (irq != IRQNONE) {
280 sc->sc_ih.ih_fun = lptintr;
281 sc->sc_ih.ih_arg = sc;
282 intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
283 }
284 }
285
286 /*
287 * lptopen -- reset the printer, then wait until it's selected and not busy.
288 */
289 int
290 lptopen(dev, flag)
291 dev_t dev;
292 int flag;
293 {
294 int unit = LPTUNIT(minor(dev));
295 u_char flags = LPTFLAGS(minor(dev));
296 struct lpt_softc *sc;
297 u_short iobase;
298 u_char control;
299 int error;
300 int spin;
301
302 if (unit >= lptcd.cd_ndevs)
303 return ENXIO;
304 sc = lptcd.cd_devs[unit];
305 if (!sc)
306 return ENXIO;
307
308 if (sc->sc_irq == IRQNONE && (flags & LPT_NOINTR) == 0)
309 return ENXIO;
310
311 if (sc->sc_state)
312 return EBUSY;
313
314 #ifdef DIAGNOSTIC
315 if (sc->sc_state)
316 printf("%s: state=0x%x not zero\n", sc->sc_dev.dv_xname,
317 sc->sc_state);
318 #endif
319
320 sc->sc_state = LPT_INIT;
321
322 sc->sc_flags = flags;
323 lprintf("%s: open flags=0x%x\n", sc->sc_dev.dv_xname, flags);
324 iobase = sc->sc_iobase;
325
326 if ((flags & LPT_NOPRIME) == 0) {
327 /* assert INIT for 100 usec to start up printer */
328 outb(iobase + lpt_control, LPC_SELECT);
329 delay(100);
330 }
331
332 control = LPC_SELECT | LPC_NINIT;
333 outb(iobase + lpt_control, control);
334
335 /* wait till ready (printer running diagnostics) */
336 for (spin = 0; NOT_READY(); spin += STEP) {
337 if (spin >= TIMEOUT) {
338 sc->sc_state = 0;
339 return EBUSY;
340 }
341
342 /* wait 1/4 second, give up if we get a signal */
343 if (error = tsleep((caddr_t)sc,
344 LPTPRI | PCATCH, "lptopen", STEP) != EWOULDBLOCK) {
345 sc->sc_state = 0;
346 return error;
347 }
348 }
349
350 if ((flags & LPT_NOINTR) == 0)
351 control |= LPC_IENABLE;
352 if (flags & LPT_AUTOLF)
353 control |= LPC_AUTOLF;
354 sc->sc_control = control;
355 outb(iobase + lpt_control, control);
356
357 sc->sc_state = LPT_OPEN;
358 sc->sc_inbuf = geteblk(LPT_BSIZE);
359 sc->sc_count = 0;
360 lptout(sc);
361
362 lprintf("%s: opened\n", sc->sc_dev.dv_xname);
363 return 0;
364 }
365
366 static int
367 isready(status, sc)
368 u_char status;
369 struct lpt_softc *sc;
370 {
371 status ^= LPS_INVERT;
372
373 if (status & LPS_NOPAPER)
374 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
375 else if (status & LPS_SELECT)
376 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
377 else if (status & LPS_NERR)
378 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
379
380 return status;
381 }
382
383 static void
384 lptout(sc)
385 struct lpt_softc *sc;
386 {
387 int s;
388
389 if ((sc->sc_state & LPT_OPEN) == 0)
390 return;
391 if (sc->sc_flags & LPT_NOINTR)
392 return;
393
394 /*
395 * Avoid possible hangs due to missed interrupts
396 */
397 if (sc->sc_count) {
398 s = spltty();
399 (void) lptintr(sc);
400 splx(s);
401 } else {
402 sc->sc_state &= ~LPT_OBUSY;
403 wakeup((caddr_t)sc);
404 }
405
406 timeout((timeout_t)lptout, (caddr_t)sc, STEP);
407 }
408
409 /*
410 * lptclose -- close the device, free the local line buffer.
411 */
412 int
413 lptclose(dev, flag)
414 dev_t dev;
415 int flag;
416 {
417 int unit = LPTUNIT(minor(dev));
418 struct lpt_softc *sc = lptcd.cd_devs[unit];
419 u_short iobase = sc->sc_iobase;
420 int error;
421
422 if (error = pushbytes(sc))
423 return error;
424
425 outb(iobase + lpt_control, LPC_NINIT);
426 brelse(sc->sc_inbuf);
427 sc->sc_state = 0;
428
429 lprintf("%s: closed\n", sc->sc_dev.dv_xname);
430 return 0;
431 }
432
433 static int
434 pushbytes(sc)
435 struct lpt_softc *sc;
436 {
437 u_short iobase = sc->sc_iobase;
438 int error;
439
440 if (sc->sc_flags & LPT_NOINTR) {
441 int spin, tic;
442 u_char control = sc->sc_control;
443 u_char ch;
444
445 while (sc->sc_count > 0) {
446 spin = tic = 0;
447 while (NOT_READY())
448 if (++spin >= sc->sc_smax) {
449 /* exponential backoff */
450 tic = tic + tic + 1;
451 if (error = tsleep((caddr_t)sc,
452 LPTPRI | PCATCH, "lptwrite1", tic))
453 return error;
454 }
455
456 outb(iobase + lpt_data, *sc->sc_cp++);
457 outb(iobase + lpt_control, control | LPC_STROBE);
458 sc->sc_count--;
459 outb(iobase + lpt_control, control);
460
461 /* adapt busy-wait algorithm */
462 if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
463 sc->sc_smax++;
464 if (spin*2 < sc->sc_smax)
465 sc->sc_smax--;
466 }
467 } else {
468 int s;
469
470 while (sc->sc_count > 0) {
471 /* if the printer is ready for a char, give it one */
472 if ((sc->sc_state & LPT_OBUSY) == 0) {
473 lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
474 sc->sc_count);
475 s = spltty();
476 (void) lptintr(sc);
477 splx(s);
478 }
479 if (error = tsleep((caddr_t)sc,
480 LPTPRI | PCATCH, "lptwrite2", 0))
481 return error;
482 }
483 }
484 }
485
486 /*
487 * copy a line from user space to a local buffer, then call
488 * putc to get the chars moved to the output queue.
489 */
490 int
491 lptwrite(dev, uio)
492 dev_t dev;
493 struct uio *uio;
494 {
495 int unit = LPTUNIT(minor(dev));
496 struct lpt_softc *sc = lptcd.cd_devs[unit];
497 size_t n;
498 int error = 0;
499
500 while (n = MIN(LPT_BSIZE, uio->uio_resid)) {
501 uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
502 sc->sc_count = n;
503 error = pushbytes(sc);
504 if (error) {
505 /* return accurate residual if interrupted or
506 timed out */
507 uio->uio_resid += sc->sc_count;
508 sc->sc_count = 0;
509 return error;
510 }
511 }
512 return 0;
513 }
514
515 /*
516 * lptintr -- handle printer interrupts which occur when the printer is
517 * ready to accept another char.
518 */
519 static int
520 lptintr(arg)
521 void *arg;
522 {
523 struct lpt_softc *sc = (struct lpt_softc *)arg;
524 u_short iobase = sc->sc_iobase;
525 u_char control = sc->sc_control;
526 u_char status;
527
528 if ((sc->sc_state & LPT_OPEN) == 0)
529 return 0;
530
531 /* is printer online and ready for output */
532 if (NOT_READY())
533 return 0;
534
535 if (sc->sc_count) {
536 /* send char */
537 sc->sc_state |= LPT_OBUSY;
538 while (sc->sc_count && !NOT_READY()) {
539 outb(iobase + lpt_data, *sc->sc_cp++);
540 outb(iobase + lpt_control, control | LPC_STROBE);
541 sc->sc_count--;
542 outb(iobase + lpt_control, control);
543 }
544 } else {
545 /* none, wake up the top half to get more */
546 sc->sc_state &= ~LPT_OBUSY;
547 wakeup((caddr_t)sc);
548 }
549
550 return 1;
551 }
552
553 int
554 lptioctl(dev, cmd, data, flag)
555 dev_t dev;
556 int cmd;
557 caddr_t data;
558 int flag;
559 {
560 int error = 0;
561
562 switch (cmd) {
563 default:
564 error = ENODEV;
565 }
566
567 return error;
568 }
569