lpt.c revision 1.22 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.22 1994/06/16 01:08:21 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 #if !defined(DEBUG) || !defined(notdef)
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 u_char sc_laststatus;
106 };
107
108 int lptprobe();
109 void lptattach();
110 int lptintr __P((struct lpt_softc *));
111
112 struct cfdriver lptcd = {
113 NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc)
114 };
115
116 #define LPTUNIT(s) (minor(s) & 0x1f)
117 #define LPTFLAGS(s) (minor(s) & 0xe0)
118
119 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
120 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
121 #ifndef DIAGNOSTIC
122 #define NOT_READY() ((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK)
123 #else
124 #define NOT_READY() notready(inb(iobase + lpt_status), sc)
125 static int notready __P((u_char, struct lpt_softc *));
126 #endif
127
128 static void lptout __P((void *arg));
129 static int pushbytes __P((struct lpt_softc *));
130
131 /*
132 * Internal routine to lptprobe to do port tests of one byte value.
133 */
134 int
135 lpt_port_test(port, data, mask)
136 u_short port;
137 u_char data, mask;
138 {
139 int timeout;
140 u_char temp;
141
142 data &= mask;
143 outb(port, data);
144 timeout = 1000;
145 do {
146 delay(10);
147 temp = inb(port) & mask;
148 } while (temp != data && --timeout);
149 lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", port, data,
150 temp, timeout);
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 int
176 lptprobe(parent, self, aux)
177 struct device *parent, *self;
178 void *aux;
179 {
180 struct isa_attach_args *ia = aux;
181 u_short iobase = ia->ia_iobase;
182 u_short port;
183 u_char mask, data;
184 int i;
185
186 #ifdef DEBUG
187 #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
188 return 0;} while (0)
189 #else
190 #define ABORT return 0
191 #endif
192
193 port = iobase + lpt_data;
194 mask = 0xff;
195
196 for (;;) {
197 data = 0x55; /* Alternating zeros */
198 if (!lpt_port_test(port, data, mask))
199 ABORT;
200
201 data = 0xaa; /* Alternating ones */
202 if (!lpt_port_test(port, data, mask))
203 ABORT;
204
205 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
206 data = ~(1 << i);
207 if (!lpt_port_test(port, data, mask))
208 ABORT;
209 }
210
211 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
212 data = (1 << i);
213 if (!lpt_port_test(port, data, mask))
214 ABORT;
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 ia->ia_iosize = LPT_NPORTS;
231 ia->ia_msize = 0;
232 return 1;
233 }
234
235 void
236 lptattach(parent, self, aux)
237 struct device *parent, *self;
238 void *aux;
239 {
240 struct lpt_softc *sc = (void *)self;
241 struct isa_attach_args *ia = aux;
242 u_short iobase = ia->ia_iobase;
243
244 if (ia->ia_irq)
245 printf("\n");
246 else
247 printf(": polled\n");
248
249 sc->sc_iobase = iobase;
250 sc->sc_irq = ia->ia_irq;
251 sc->sc_state = 0;
252 outb(iobase + lpt_control, LPC_NINIT);
253
254 if (ia->ia_irq) {
255 sc->sc_ih.ih_fun = lptintr;
256 sc->sc_ih.ih_arg = sc;
257 sc->sc_ih.ih_level = IPL_NONE;
258 intr_establish(ia->ia_irq, &sc->sc_ih);
259 }
260 }
261
262 /*
263 * Reset the printer, then wait until it's selected and not busy.
264 */
265 int
266 lptopen(dev, flag)
267 dev_t dev;
268 int flag;
269 {
270 int unit = LPTUNIT(dev);
271 u_char flags = LPTFLAGS(dev);
272 struct lpt_softc *sc;
273 u_short iobase;
274 u_char control;
275 int error;
276 int spin;
277
278 if (unit >= lptcd.cd_ndevs)
279 return ENXIO;
280 sc = lptcd.cd_devs[unit];
281 if (!sc)
282 return ENXIO;
283
284 if (sc->sc_irq == 0 && (flags & LPT_NOINTR) == 0)
285 return ENXIO;
286
287 #ifdef DIAGNOSTIC
288 if (sc->sc_state)
289 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
290 sc->sc_state);
291 #endif
292
293 if (sc->sc_state)
294 return EBUSY;
295
296 sc->sc_state = LPT_INIT;
297 sc->sc_flags = flags;
298 lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
299 iobase = sc->sc_iobase;
300
301 if ((flags & LPT_NOPRIME) == 0) {
302 /* assert INIT for 100 usec to start up printer */
303 outb(iobase + lpt_control, LPC_SELECT);
304 delay(100);
305 }
306
307 control = LPC_SELECT | LPC_NINIT;
308 outb(iobase + lpt_control, control);
309
310 /* wait till ready (printer running diagnostics) */
311 for (spin = 0; NOT_READY(); spin += STEP) {
312 if (spin >= TIMEOUT) {
313 sc->sc_state = 0;
314 return EBUSY;
315 }
316
317 /* wait 1/4 second, give up if we get a signal */
318 if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen",
319 STEP) != EWOULDBLOCK) {
320 sc->sc_state = 0;
321 return error;
322 }
323 }
324
325 if ((flags & LPT_NOINTR) == 0)
326 control |= LPC_IENABLE;
327 if (flags & LPT_AUTOLF)
328 control |= LPC_AUTOLF;
329 sc->sc_control = control;
330 outb(iobase + lpt_control, control);
331
332 sc->sc_inbuf = geteblk(LPT_BSIZE);
333 sc->sc_count = 0;
334 sc->sc_state = LPT_OPEN;
335 lptout(sc);
336
337 lprintf("%s: opened\n", sc->sc_dev.dv_xname);
338 return 0;
339 }
340
341 #ifdef DIAGNOSTIC
342 int
343 notready(status, sc)
344 u_char status;
345 struct lpt_softc *sc;
346 {
347 u_char new;
348
349 status ^= LPS_INVERT;
350 new = status & ~sc->sc_laststatus;
351 sc->sc_laststatus = status;
352
353 if (new & LPS_NOPAPER)
354 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
355 else if (new & LPS_SELECT)
356 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
357 else if (new & LPS_NERR)
358 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
359
360 return status & LPS_MASK;
361 }
362 #endif
363
364 void
365 lptout(arg)
366 void *arg;
367 {
368 struct lpt_softc *sc;
369 int s;
370
371 sc = (struct lpt_softc *)arg;
372 if (sc->sc_flags & LPT_NOINTR)
373 return;
374
375 s = spltty();
376 lptintr(sc);
377 splx(s);
378
379 timeout(lptout, sc, STEP);
380 }
381
382 /*
383 * Close the device, and free the local line buffer.
384 */
385 lptclose(dev, flag)
386 dev_t dev;
387 int flag;
388 {
389 int unit = LPTUNIT(dev);
390 struct lpt_softc *sc = lptcd.cd_devs[unit];
391 u_short iobase = sc->sc_iobase;
392
393 if (sc->sc_count)
394 (void) pushbytes(sc);
395
396 outb(iobase + lpt_control, LPC_NINIT);
397 sc->sc_state = 0;
398 outb(iobase + lpt_control, LPC_NINIT);
399 brelse(sc->sc_inbuf);
400
401 lprintf("%s: closed\n", sc->sc_dev.dv_xname);
402 return 0;
403 }
404
405 int
406 pushbytes(sc)
407 struct lpt_softc *sc;
408 {
409 u_short iobase = sc->sc_iobase;
410 int error;
411
412 if (sc->sc_flags & LPT_NOINTR) {
413 int spin, tic;
414 u_char control = sc->sc_control;
415
416 while (sc->sc_count > 0) {
417 spin = 0;
418 while (NOT_READY() && spin++ < sc->sc_spinmax);
419 if (spin >= sc->sc_spinmax) {
420 tic = 0;
421 /* adapt busy-wait algorithm */
422 sc->sc_spinmax++;
423 while (NOT_READY()) {
424 /* exponential backoff */
425 tic = tic + tic + 1;
426 if (tic > TIMEOUT)
427 tic = TIMEOUT;
428 error = tsleep((caddr_t)sc,
429 LPTPRI | PCATCH, "lptpsh", tic);
430 if (error != EWOULDBLOCK)
431 return error;
432 }
433 }
434
435 outb(iobase + lpt_data, *sc->sc_cp++);
436 outb(iobase + lpt_control, control | LPC_STROBE);
437 sc->sc_count--;
438 outb(iobase + lpt_control, control);
439
440 /* adapt busy-wait algorithm */
441 if (spin*2 < sc->sc_spinmax)
442 sc->sc_spinmax--;
443 }
444 } else {
445 int s;
446
447 while (sc->sc_count > 0) {
448 /* if the printer is ready for a char, give it one */
449 if ((sc->sc_state & LPT_OBUSY) == 0) {
450 lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
451 sc->sc_count);
452 s = spltty();
453 (void) lptintr(sc);
454 splx(s);
455 }
456 if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
457 "lptwrite2", 0))
458 return error;
459 }
460 }
461 return 0;
462 }
463
464 /*
465 * Copy a line from user space to a local buffer, then call putc to get the
466 * chars moved to the output queue.
467 */
468 lptwrite(dev, uio)
469 dev_t dev;
470 struct uio *uio;
471 {
472 struct lpt_softc *sc = lptcd.cd_devs[LPTUNIT(dev)];
473 size_t n;
474 int error = 0;
475
476 while (n = min(LPT_BSIZE, uio->uio_resid)) {
477 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
478 sc->sc_count = n;
479 error = pushbytes(sc);
480 if (error) {
481 /*
482 * Return accurate residual if interrupted or timed
483 * out.
484 */
485 uio->uio_resid += sc->sc_count;
486 sc->sc_count = 0;
487 return error;
488 }
489 }
490 return 0;
491 }
492
493 /*
494 * Handle printer interrupts which occur when the printer is ready to accept
495 * another char.
496 */
497 int
498 lptintr(sc)
499 struct lpt_softc *sc;
500 {
501 u_short iobase = sc->sc_iobase;
502
503 #if 0
504 if ((sc->sc_state & LPT_OPEN) == 0)
505 return 0;
506 #endif
507
508 /* is printer online and ready for output */
509 if (NOT_READY())
510 return 0;
511
512 if (sc->sc_count) {
513 u_char control = sc->sc_control;
514 /* send char */
515 outb(iobase + lpt_data, *sc->sc_cp++);
516 outb(iobase + lpt_control, control | LPC_STROBE);
517 sc->sc_count--;
518 outb(iobase + lpt_control, control);
519 sc->sc_state |= LPT_OBUSY;
520 } else
521 sc->sc_state &= ~LPT_OBUSY;
522
523 if (sc->sc_count == 0) {
524 /* none, wake up the top half to get more */
525 wakeup((caddr_t)sc);
526 }
527
528 return 1;
529 }
530
531 int
532 lptioctl(dev, cmd, data, flag)
533 dev_t dev;
534 int cmd;
535 caddr_t data;
536 int flag;
537 {
538 int error = 0;
539
540 switch (cmd) {
541 default:
542 error = ENODEV;
543 }
544
545 return error;
546 }
547