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