lpt.c revision 1.7.4.12 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.12 1993/10/18 08:35:34 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 static void lptattach __P((struct device *, struct device *, void *));
114 static int lptintr __P((void *));
115
116 struct cfdriver lptcd =
117 { NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc) };
118
119 #define LPTUNIT(s) ((s)&0x1f)
120 #define LPTFLAGS(s) ((s)&0xe0)
121
122 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
123 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
124 #define NOT_READY() isready(inb(iobase + lpt_status), sc)
125
126 static int isready __P((u_char, struct lpt_softc *));
127 static void lptout __P((struct lpt_softc *));
128 static int pushbytes __P((struct lpt_softc *));
129
130 /*
131 * Internal routine to lptprobe to do port tests of one byte value
132 */
133 int
134 lpt_port_test(port, data, mask)
135 u_short port;
136 u_char data, mask;
137 {
138 int timeout;
139 u_char temp;
140
141 data &= mask;
142 outb(port, data);
143 timeout = 100;
144 do {
145 temp = inb(port) & mask;
146 } while (temp != data && --timeout);
147 lprintf("lpt: port=0x%x out=0x%x in=0x%x\n", port, data, temp);
148 return (temp == data);
149 }
150
151 /*
152 * Logic:
153 * 1) You should be able to write to and read back the same value
154 * to the data port. Do an alternating zeros, alternating ones,
155 * walking zero, and walking one test to check for stuck bits.
156 *
157 * 2) You should be able to write to and read back the same value
158 * to the control port lower 5 bits, the upper 3 bits are reserved
159 * per the IBM PC technical reference manauls and different boards
160 * do different things with them. Do an alternating zeros, alternating
161 * ones, walking zero, and walking one test to check for stuck bits.
162 *
163 * Some printers drag the strobe line down when the are powered off
164 * so this bit has been masked out of the control port test.
165 *
166 * XXX Some printers may not like a fast pulse on init or strobe, I
167 * don't know at this point, if that becomes a problem these bits
168 * should be turned off in the mask byte for the control port test.
169 *
170 * 3) Set the data and control ports to a value of 0
171 */
172
173 static int
174 lptprobe(parent, cf, aux)
175 struct device *parent;
176 struct cfdata *cf;
177 void *aux;
178 {
179 struct isa_attach_args *ia = aux;
180 u_short iobase = ia->ia_iobase;
181 u_short port = iobase + lpt_data;
182 u_char mask = 0xff;
183 u_char data;
184 int i;
185
186 if (iobase == IOBASEUNK)
187 return 0;
188
189 for (;;) {
190 data = 0x55; /* Alternating zeros */
191 if (!lpt_port_test(port, data, mask))
192 return 0;
193
194 data = 0xaa; /* Alternating ones */
195 if (!lpt_port_test(port, data, mask))
196 return 0;
197
198 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
199 data = ~(1 << i);
200 if (!lpt_port_test(port, data, mask))
201 return 0;
202 }
203
204 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
205 data = (1 << i);
206 if (!lpt_port_test(port, data, mask))
207 return 0;
208 }
209
210 if (port == iobase + lpt_data) {
211 port = iobase + lpt_control;
212 mask = 0x1e;
213 } else
214 break;
215 }
216 outb(iobase + lpt_data, 0);
217 outb(iobase + lpt_control, 0);
218
219 if (ia->ia_irq == IRQUNK)
220 ia->ia_irq = IRQNONE;
221 /* okay if we don't have an irq; will force polling */
222
223 ia->ia_iosize = LPT_NPORTS;
224 ia->ia_drq = DRQUNK;
225 ia->ia_msize = 0;
226 return 1;
227 }
228
229 static void
230 lptattach(parent, self, aux)
231 struct device *parent, *self;
232 void *aux;
233 {
234 struct isa_attach_args *ia = aux;
235 struct lpt_softc *sc = (struct lpt_softc *)self;
236 u_short iobase = ia->ia_iobase;
237 u_short irq = ia->ia_irq;
238
239 sc->sc_iobase = iobase;
240 sc->sc_irq = irq;
241 sc->sc_state = 0;
242 outb(iobase + lpt_control, LPC_NINIT);
243
244 printf(": %sparallel port\n", irq == IRQNONE ? "polled " : "");
245 isa_establish(&sc->sc_id, &sc->sc_dev);
246
247 if (irq != IRQNONE) {
248 sc->sc_ih.ih_fun = lptintr;
249 sc->sc_ih.ih_arg = sc;
250 intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
251 }
252 }
253
254 /*
255 * lptopen -- reset the printer, then wait until it's selected and not busy.
256 */
257 int
258 lptopen(dev, flag)
259 dev_t dev;
260 int flag;
261 {
262 int unit = LPTUNIT(minor(dev));
263 u_char flags = LPTFLAGS(minor(dev));
264 struct lpt_softc *sc;
265 u_short iobase;
266 u_char control;
267 int error;
268 int spin;
269
270 if (unit >= lptcd.cd_ndevs)
271 return ENXIO;
272 sc = lptcd.cd_devs[unit];
273 if (!sc)
274 return ENXIO;
275
276 if (sc->sc_irq == IRQNONE && (flags & LPT_NOINTR) == 0)
277 return ENXIO;
278
279 if (sc->sc_state)
280 return EBUSY;
281
282 #ifdef DIAGNOSTIC
283 if (sc->sc_state)
284 printf("%s: state=0x%x not zero\n", sc->sc_dev.dv_xname,
285 sc->sc_state);
286 #endif
287
288 sc->sc_state = LPT_INIT;
289
290 sc->sc_flags = flags;
291 lprintf("%s: open flags=0x%x\n", sc->sc_dev.dv_xname, flags);
292 iobase = sc->sc_iobase;
293
294 if ((flags & LPT_NOPRIME) == 0) {
295 /* assert INIT for 100 usec to start up printer */
296 outb(iobase + lpt_control, LPC_SELECT);
297 delay(100);
298 }
299
300 control = LPC_SELECT | LPC_NINIT;
301 outb(iobase + lpt_control, control);
302
303 /* wait till ready (printer running diagnostics) */
304 for (spin = 0; NOT_READY(); spin += STEP) {
305 if (spin >= TIMEOUT) {
306 sc->sc_state = 0;
307 return EBUSY;
308 }
309
310 /* wait 1/4 second, give up if we get a signal */
311 if (error = tsleep((caddr_t)sc,
312 LPTPRI | PCATCH, "lptopen", STEP) != EWOULDBLOCK) {
313 sc->sc_state = 0;
314 return error;
315 }
316 }
317
318 if ((flags & LPT_NOINTR) == 0)
319 control |= LPC_IENABLE;
320 if (flags & LPT_AUTOLF)
321 control |= LPC_AUTOLF;
322 sc->sc_control = control;
323 outb(iobase + lpt_control, control);
324
325 sc->sc_state = LPT_OPEN;
326 sc->sc_inbuf = geteblk(LPT_BSIZE);
327 sc->sc_count = 0;
328 lptout(sc);
329
330 lprintf("%s: opened\n", sc->sc_dev.dv_xname);
331 return 0;
332 }
333
334 static int
335 isready(status, sc)
336 u_char status;
337 struct lpt_softc *sc;
338 {
339 status ^= LPS_INVERT;
340
341 if (status & LPS_NOPAPER)
342 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
343 else if (status & LPS_SELECT)
344 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
345 else if (status & LPS_NERR)
346 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
347
348 return status;
349 }
350
351 static void
352 lptout(sc)
353 struct lpt_softc *sc;
354 {
355 int s;
356
357 if ((sc->sc_state & LPT_OPEN) == 0)
358 return;
359 if (sc->sc_flags & LPT_NOINTR)
360 return;
361
362 /*
363 * Avoid possible hangs due to missed interrupts
364 */
365 if (sc->sc_count) {
366 s = spltty();
367 (void) lptintr(sc);
368 splx(s);
369 } else {
370 sc->sc_state &= ~LPT_OBUSY;
371 wakeup((caddr_t)sc);
372 }
373
374 timeout((timeout_t)lptout, (caddr_t)sc, STEP);
375 }
376
377 /*
378 * lptclose -- close the device, free the local line buffer.
379 */
380 int
381 lptclose(dev, flag)
382 dev_t dev;
383 int flag;
384 {
385 int unit = LPTUNIT(minor(dev));
386 struct lpt_softc *sc = lptcd.cd_devs[unit];
387 u_short iobase = sc->sc_iobase;
388 int error;
389
390 if (error = pushbytes(sc))
391 return error;
392
393 outb(iobase + lpt_control, LPC_NINIT);
394 brelse(sc->sc_inbuf);
395 sc->sc_state = 0;
396
397 lprintf("%s: closed\n", sc->sc_dev.dv_xname);
398 return 0;
399 }
400
401 static int
402 pushbytes(sc)
403 struct lpt_softc *sc;
404 {
405 u_short iobase = sc->sc_iobase;
406 int error;
407
408 if (sc->sc_flags & LPT_NOINTR) {
409 int spin, tic;
410 u_char control = sc->sc_control;
411 u_char ch;
412
413 while (sc->sc_count > 0) {
414 spin = tic = 0;
415 while (NOT_READY())
416 if (++spin >= sc->sc_smax) {
417 /* exponential backoff */
418 tic = tic + tic + 1;
419 if (error = tsleep((caddr_t)sc,
420 LPTPRI | PCATCH, "lptwrite1", tic))
421 return error;
422 }
423
424 outb(iobase + lpt_data, *sc->sc_cp++);
425 outb(iobase + lpt_control, control | LPC_STROBE);
426 sc->sc_count--;
427 outb(iobase + lpt_control, control);
428
429 /* adapt busy-wait algorithm */
430 if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
431 sc->sc_smax++;
432 if (spin*2 < sc->sc_smax)
433 sc->sc_smax--;
434 }
435 } else {
436 int s;
437
438 while (sc->sc_count > 0) {
439 /* if the printer is ready for a char, give it one */
440 if ((sc->sc_state & LPT_OBUSY) == 0) {
441 lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
442 sc->sc_count);
443 s = spltty();
444 (void) lptintr(sc);
445 splx(s);
446 }
447 if (error = tsleep((caddr_t)sc,
448 LPTPRI | PCATCH, "lptwrite2", 0))
449 return error;
450 }
451 }
452 }
453
454 /*
455 * copy a line from user space to a local buffer, then call
456 * putc to get the chars moved to the output queue.
457 */
458 int
459 lptwrite(dev, uio)
460 dev_t dev;
461 struct uio *uio;
462 {
463 int unit = LPTUNIT(minor(dev));
464 struct lpt_softc *sc = lptcd.cd_devs[unit];
465 size_t n;
466 int error = 0;
467
468 while (n = MIN(LPT_BSIZE, uio->uio_resid)) {
469 uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
470 sc->sc_count = n;
471 error = pushbytes(sc);
472 if (error) {
473 /* return accurate residual if interrupted or
474 timed out */
475 uio->uio_resid += sc->sc_count;
476 sc->sc_count = 0;
477 return error;
478 }
479 }
480 return 0;
481 }
482
483 /*
484 * lptintr -- handle printer interrupts which occur when the printer is
485 * ready to accept another char.
486 */
487 static int
488 lptintr(arg)
489 void *arg;
490 {
491 struct lpt_softc *sc = (struct lpt_softc *)arg;
492 u_short iobase = sc->sc_iobase;
493 u_char control = sc->sc_control;
494 u_char status;
495
496 if ((sc->sc_state & LPT_OPEN) == 0)
497 return 0;
498
499 /* is printer online and ready for output */
500 if (NOT_READY())
501 return 0;
502
503 if (sc->sc_count) {
504 /* send char */
505 sc->sc_state |= LPT_OBUSY;
506 while (sc->sc_count && !NOT_READY()) {
507 outb(iobase + lpt_data, *sc->sc_cp++);
508 outb(iobase + lpt_control, control | LPC_STROBE);
509 sc->sc_count--;
510 outb(iobase + lpt_control, control);
511 }
512 } else {
513 /* none, wake up the top half to get more */
514 sc->sc_state &= ~LPT_OBUSY;
515 wakeup((caddr_t)sc);
516 }
517
518 return 1;
519 }
520
521 int
522 lptioctl(dev, cmd, data, flag)
523 dev_t dev;
524 int cmd;
525 caddr_t data;
526 int flag;
527 {
528 int error = 0;
529
530 switch (cmd) {
531 default:
532 error = ENODEV;
533 }
534
535 return error;
536 }
537