lpt.c revision 1.7.4.13 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.13 1993/10/27 05:37:13 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 for (;;) {
193 data = 0x55; /* Alternating zeros */
194 if (!lpt_port_test(port, data, mask))
195 {printf("mask %x data %x failed\n", mask, data);
196 return 0;}
197
198 data = 0xaa; /* Alternating ones */
199 if (!lpt_port_test(port, data, mask))
200 {printf("mask %x data %x failed\n", mask, data);
201 return 0;}
202
203 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
204 data = ~(1 << i);
205 if (!lpt_port_test(port, data, mask))
206 {printf("mask %x data %x failed\n", mask, data);
207 return 0;}
208 }
209
210 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
211 data = (1 << i);
212 if (!lpt_port_test(port, data, mask))
213 {printf("mask %x data %x failed\n", mask, data);
214 return 0;}
215 }
216
217 if (port == iobase + lpt_data) {
218 port = iobase + lpt_control;
219 #if 1 /* XXX */
220 mask = 0x04;
221 #else
222 mask = 0x1e;
223 #endif
224 } else
225 break;
226 }
227 outb(iobase + lpt_data, 0);
228 outb(iobase + lpt_control, 0);
229
230 if (ia->ia_irq == IRQUNK)
231 #if 0 /* XXX */
232 ia->ia_irq = isa_discoverintr(lptforceintr, aux);
233 #else
234 ia->ia_irq = IRQNONE;
235 #endif
236 /* okay if we don't have an irq; will force polling */
237
238 ia->ia_iosize = LPT_NPORTS;
239 ia->ia_drq = DRQUNK;
240 ia->ia_msize = 0;
241 return 1;
242 }
243
244 #if 0 /* XXX */
245 static void
246 lptforceintr(aux)
247 void *aux;
248 {
249 struct isa_attach_args *ia = aux;
250 u_short iobase = ia->ia_iobase;
251
252 outb(iobase + lpt_control, LPC_IENABLE);
253 delay(100);
254 outb(iobase + lpt_control, 0);
255 }
256 #endif
257
258 static void
259 lptattach(parent, self, aux)
260 struct device *parent, *self;
261 void *aux;
262 {
263 struct isa_attach_args *ia = aux;
264 struct lpt_softc *sc = (struct lpt_softc *)self;
265 u_short iobase = ia->ia_iobase;
266 u_short irq = ia->ia_irq;
267
268 sc->sc_iobase = iobase;
269 sc->sc_irq = irq;
270 sc->sc_state = 0;
271 outb(iobase + lpt_control, LPC_NINIT);
272
273 printf(": %sparallel port\n", irq == IRQNONE ? "polled " : "");
274 isa_establish(&sc->sc_id, &sc->sc_dev);
275
276 if (irq != IRQNONE) {
277 sc->sc_ih.ih_fun = lptintr;
278 sc->sc_ih.ih_arg = sc;
279 intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
280 }
281 }
282
283 /*
284 * lptopen -- reset the printer, then wait until it's selected and not busy.
285 */
286 int
287 lptopen(dev, flag)
288 dev_t dev;
289 int flag;
290 {
291 int unit = LPTUNIT(minor(dev));
292 u_char flags = LPTFLAGS(minor(dev));
293 struct lpt_softc *sc;
294 u_short iobase;
295 u_char control;
296 int error;
297 int spin;
298
299 if (unit >= lptcd.cd_ndevs)
300 return ENXIO;
301 sc = lptcd.cd_devs[unit];
302 if (!sc)
303 return ENXIO;
304
305 if (sc->sc_irq == IRQNONE && (flags & LPT_NOINTR) == 0)
306 return ENXIO;
307
308 if (sc->sc_state)
309 return EBUSY;
310
311 #ifdef DIAGNOSTIC
312 if (sc->sc_state)
313 printf("%s: state=0x%x not zero\n", sc->sc_dev.dv_xname,
314 sc->sc_state);
315 #endif
316
317 sc->sc_state = LPT_INIT;
318
319 sc->sc_flags = flags;
320 lprintf("%s: open flags=0x%x\n", sc->sc_dev.dv_xname, flags);
321 iobase = sc->sc_iobase;
322
323 if ((flags & LPT_NOPRIME) == 0) {
324 /* assert INIT for 100 usec to start up printer */
325 outb(iobase + lpt_control, LPC_SELECT);
326 delay(100);
327 }
328
329 control = LPC_SELECT | LPC_NINIT;
330 outb(iobase + lpt_control, control);
331
332 /* wait till ready (printer running diagnostics) */
333 for (spin = 0; NOT_READY(); spin += STEP) {
334 if (spin >= TIMEOUT) {
335 sc->sc_state = 0;
336 return EBUSY;
337 }
338
339 /* wait 1/4 second, give up if we get a signal */
340 if (error = tsleep((caddr_t)sc,
341 LPTPRI | PCATCH, "lptopen", STEP) != EWOULDBLOCK) {
342 sc->sc_state = 0;
343 return error;
344 }
345 }
346
347 if ((flags & LPT_NOINTR) == 0)
348 control |= LPC_IENABLE;
349 if (flags & LPT_AUTOLF)
350 control |= LPC_AUTOLF;
351 sc->sc_control = control;
352 outb(iobase + lpt_control, control);
353
354 sc->sc_state = LPT_OPEN;
355 sc->sc_inbuf = geteblk(LPT_BSIZE);
356 sc->sc_count = 0;
357 lptout(sc);
358
359 lprintf("%s: opened\n", sc->sc_dev.dv_xname);
360 return 0;
361 }
362
363 static int
364 isready(status, sc)
365 u_char status;
366 struct lpt_softc *sc;
367 {
368 status ^= LPS_INVERT;
369
370 if (status & LPS_NOPAPER)
371 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
372 else if (status & LPS_SELECT)
373 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
374 else if (status & LPS_NERR)
375 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
376
377 return status;
378 }
379
380 static void
381 lptout(sc)
382 struct lpt_softc *sc;
383 {
384 int s;
385
386 if ((sc->sc_state & LPT_OPEN) == 0)
387 return;
388 if (sc->sc_flags & LPT_NOINTR)
389 return;
390
391 /*
392 * Avoid possible hangs due to missed interrupts
393 */
394 if (sc->sc_count) {
395 s = spltty();
396 (void) lptintr(sc);
397 splx(s);
398 } else {
399 sc->sc_state &= ~LPT_OBUSY;
400 wakeup((caddr_t)sc);
401 }
402
403 timeout((timeout_t)lptout, (caddr_t)sc, STEP);
404 }
405
406 /*
407 * lptclose -- close the device, free the local line buffer.
408 */
409 int
410 lptclose(dev, flag)
411 dev_t dev;
412 int flag;
413 {
414 int unit = LPTUNIT(minor(dev));
415 struct lpt_softc *sc = lptcd.cd_devs[unit];
416 u_short iobase = sc->sc_iobase;
417 int error;
418
419 if (error = pushbytes(sc))
420 return error;
421
422 outb(iobase + lpt_control, LPC_NINIT);
423 brelse(sc->sc_inbuf);
424 sc->sc_state = 0;
425
426 lprintf("%s: closed\n", sc->sc_dev.dv_xname);
427 return 0;
428 }
429
430 static int
431 pushbytes(sc)
432 struct lpt_softc *sc;
433 {
434 u_short iobase = sc->sc_iobase;
435 int error;
436
437 if (sc->sc_flags & LPT_NOINTR) {
438 int spin, tic;
439 u_char control = sc->sc_control;
440 u_char ch;
441
442 while (sc->sc_count > 0) {
443 spin = tic = 0;
444 while (NOT_READY())
445 if (++spin >= sc->sc_smax) {
446 /* exponential backoff */
447 tic = tic + tic + 1;
448 if (error = tsleep((caddr_t)sc,
449 LPTPRI | PCATCH, "lptwrite1", tic))
450 return error;
451 }
452
453 outb(iobase + lpt_data, *sc->sc_cp++);
454 outb(iobase + lpt_control, control | LPC_STROBE);
455 sc->sc_count--;
456 outb(iobase + lpt_control, control);
457
458 /* adapt busy-wait algorithm */
459 if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
460 sc->sc_smax++;
461 if (spin*2 < sc->sc_smax)
462 sc->sc_smax--;
463 }
464 } else {
465 int s;
466
467 while (sc->sc_count > 0) {
468 /* if the printer is ready for a char, give it one */
469 if ((sc->sc_state & LPT_OBUSY) == 0) {
470 lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
471 sc->sc_count);
472 s = spltty();
473 (void) lptintr(sc);
474 splx(s);
475 }
476 if (error = tsleep((caddr_t)sc,
477 LPTPRI | PCATCH, "lptwrite2", 0))
478 return error;
479 }
480 }
481 }
482
483 /*
484 * copy a line from user space to a local buffer, then call
485 * putc to get the chars moved to the output queue.
486 */
487 int
488 lptwrite(dev, uio)
489 dev_t dev;
490 struct uio *uio;
491 {
492 int unit = LPTUNIT(minor(dev));
493 struct lpt_softc *sc = lptcd.cd_devs[unit];
494 size_t n;
495 int error = 0;
496
497 while (n = MIN(LPT_BSIZE, uio->uio_resid)) {
498 uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
499 sc->sc_count = n;
500 error = pushbytes(sc);
501 if (error) {
502 /* return accurate residual if interrupted or
503 timed out */
504 uio->uio_resid += sc->sc_count;
505 sc->sc_count = 0;
506 return error;
507 }
508 }
509 return 0;
510 }
511
512 /*
513 * lptintr -- handle printer interrupts which occur when the printer is
514 * ready to accept another char.
515 */
516 static int
517 lptintr(arg)
518 void *arg;
519 {
520 struct lpt_softc *sc = (struct lpt_softc *)arg;
521 u_short iobase = sc->sc_iobase;
522 u_char control = sc->sc_control;
523 u_char status;
524
525 if ((sc->sc_state & LPT_OPEN) == 0)
526 return 0;
527
528 /* is printer online and ready for output */
529 if (NOT_READY())
530 return 0;
531
532 if (sc->sc_count) {
533 /* send char */
534 sc->sc_state |= LPT_OBUSY;
535 while (sc->sc_count && !NOT_READY()) {
536 outb(iobase + lpt_data, *sc->sc_cp++);
537 outb(iobase + lpt_control, control | LPC_STROBE);
538 sc->sc_count--;
539 outb(iobase + lpt_control, control);
540 }
541 } else {
542 /* none, wake up the top half to get more */
543 sc->sc_state &= ~LPT_OBUSY;
544 wakeup((caddr_t)sc);
545 }
546
547 return 1;
548 }
549
550 int
551 lptioctl(dev, cmd, data, flag)
552 dev_t dev;
553 int cmd;
554 caddr_t data;
555 int flag;
556 {
557 int error = 0;
558
559 switch (cmd) {
560 default:
561 error = ENODEV;
562 }
563
564 return error;
565 }
566