lpt.c revision 1.14 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.14 1994/03/06 17:19:11 mycroft Exp $
50 */
51
52 /*
53 * Device Driver for AT parallel printer port
54 */
55
56 #include "lpt.h"
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/proc.h>
61 #include <sys/user.h>
62 #include <sys/buf.h>
63 #include <sys/kernel.h>
64 #include <sys/ioctl.h>
65 #include <sys/uio.h>
66 #include <sys/device.h>
67 #include <sys/syslog.h>
68
69 #include <machine/cpu.h>
70 #include <machine/pio.h>
71
72 #include <i386/isa/isa.h>
73 #include <i386/isa/isa_device.h>
74 #include <i386/isa/lptreg.h>
75
76 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
77 #define STEP hz/4
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
92 size_t sc_count;
93 struct buf *sc_inbuf;
94 u_char *sc_cp;
95 int sc_spinmax;
96 u_short sc_iobase;
97 u_short sc_irq;
98 u_char sc_state;
99 #define LPT_OPEN 0x01 /* device is open */
100 #define LPT_OBUSY 0x02 /* printer is busy doing output */
101 #define LPT_INIT 0x04 /* waiting to initialize for open */
102 u_char sc_flags;
103 #define LPT_AUTOLF 0x20 /* automatic LF on CR */
104 #define LPT_NOPRIME 0x40 /* don't prime on open */
105 #define LPT_NOINTR 0x80 /* do not use interrupt */
106 u_char sc_control;
107 } lpt_softc[NLPT];
108
109 int lptprobe __P((struct isa_device *));
110 int lptattach __P((struct isa_device *));
111 int lptintr __P((int));
112
113 struct isa_driver lptdriver = {
114 lptprobe, lptattach, "lpt"
115 };
116
117 #define LPTUNIT(s) (minor(s) & 0x1f)
118 #define LPTFLAGS(s) (minor(s) & 0xe0)
119
120 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
121 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
122 #ifndef DIAGNOSTIC
123 #define NOT_READY() ((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK)
124 #else
125 #define NOT_READY() notready(inb(iobase + lpt_status), sc)
126 static int notready __P((u_char, struct lpt_softc *));
127 #endif
128
129 static void lptout __P((struct lpt_softc *));
130 static int pushbytes __P((struct lpt_softc *));
131
132 /*
133 * Internal routine to lptprobe to do port tests of one byte value.
134 */
135 int
136 lpt_port_test(port, data, mask)
137 u_short port;
138 u_char data, mask;
139 {
140 int timeout;
141 u_char temp;
142
143 data &= mask;
144 outb(port, data);
145 timeout = 1000;
146 do {
147 delay(10);
148 temp = inb(port) & mask;
149 } while (temp != data && --timeout);
150 lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", port, data,
151 temp, timeout);
152 return (temp == data);
153 }
154
155 /*
156 * Logic:
157 * 1) You should be able to write to and read back the same value
158 * to the data port. Do an alternating zeros, alternating ones,
159 * walking zero, and walking one test to check for stuck bits.
160 *
161 * 2) You should be able to write to and read back the same value
162 * to the control port lower 5 bits, the upper 3 bits are reserved
163 * per the IBM PC technical reference manauls and different boards
164 * do different things with them. Do an alternating zeros, alternating
165 * ones, walking zero, and walking one test to check for stuck bits.
166 *
167 * Some printers drag the strobe line down when the are powered off
168 * so this bit has been masked out of the control port test.
169 *
170 * XXX Some printers may not like a fast pulse on init or strobe, I
171 * don't know at this point, if that becomes a problem these bits
172 * should be turned off in the mask byte for the control port test.
173 *
174 * 3) Set the data and control ports to a value of 0
175 */
176 int
177 lptprobe(isa_dev)
178 struct isa_device *isa_dev;
179 {
180 u_short iobase = isa_dev->id_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 return LPT_NPORTS;
230 }
231
232 int
233 lptattach(isa_dev)
234 struct isa_device *isa_dev;
235 {
236 struct lpt_softc *sc = &lpt_softc[isa_dev->id_unit];
237 u_short iobase = isa_dev->id_iobase;
238 u_short irq = isa_dev->id_irq;
239
240 /* XXX HACK */
241 sprintf(sc->sc_dev.dv_xname, "%s%d", lptdriver.name, isa_dev->id_unit);
242 sc->sc_dev.dv_unit = isa_dev->id_unit;
243
244 if (!irq)
245 printf("%s: polled\n", sc->sc_dev.dv_xname);
246
247 sc->sc_iobase = iobase;
248 sc->sc_irq = irq;
249 sc->sc_state = 0;
250 outb(iobase + lpt_control, LPC_NINIT);
251 }
252
253 /*
254 * Reset the printer, then wait until it's selected and not busy.
255 */
256 int
257 lptopen(dev, flag)
258 dev_t dev;
259 int flag;
260 {
261 int unit = LPTUNIT(dev);
262 u_char flags = LPTFLAGS(dev);
263 struct lpt_softc *sc;
264 u_short iobase;
265 u_char control;
266 int error;
267 int spin;
268
269 if (unit >= NLPT)
270 return ENXIO;
271 sc = &lpt_softc[unit];
272 if (!sc->sc_iobase)
273 return ENXIO;
274
275 if (sc->sc_irq == 0 && (flags & LPT_NOINTR) == 0)
276 return ENXIO;
277
278 #ifdef DIAGNOSTIC
279 if (sc->sc_state)
280 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
281 sc->sc_state);
282 #endif
283
284 if (sc->sc_state)
285 return EBUSY;
286
287 sc->sc_state = LPT_INIT;
288 sc->sc_flags = flags;
289 lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
290 iobase = sc->sc_iobase;
291
292 if ((flags & LPT_NOPRIME) == 0) {
293 /* assert INIT for 100 usec to start up printer */
294 outb(iobase + lpt_control, LPC_SELECT);
295 delay(100);
296 }
297
298 control = LPC_SELECT | LPC_NINIT;
299 outb(iobase + lpt_control, control);
300
301 /* wait till ready (printer running diagnostics) */
302 for (spin = 0; NOT_READY(); spin += STEP) {
303 if (spin >= TIMEOUT) {
304 sc->sc_state = 0;
305 return EBUSY;
306 }
307
308 /* wait 1/4 second, give up if we get a signal */
309 if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen",
310 STEP) != EWOULDBLOCK) {
311 sc->sc_state = 0;
312 return error;
313 }
314 }
315
316 if ((flags & LPT_NOINTR) == 0)
317 control |= LPC_IENABLE;
318 if (flags & LPT_AUTOLF)
319 control |= LPC_AUTOLF;
320 sc->sc_control = control;
321 outb(iobase + lpt_control, control);
322
323 sc->sc_inbuf = geteblk(LPT_BSIZE);
324 sc->sc_count = 0;
325 sc->sc_state = LPT_OPEN;
326 lptout(sc);
327
328 lprintf("%s: opened\n", sc->sc_dev.dv_xname);
329 return 0;
330 }
331
332 #ifdef DIAGNOSTIC
333 int
334 notready(status, sc)
335 u_char status;
336 struct lpt_softc *sc;
337 {
338 status ^= LPS_INVERT;
339
340 if (status & LPS_NOPAPER)
341 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
342 else if (status & LPS_SELECT)
343 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
344 else if (status & LPS_NERR)
345 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
346
347 return status & LPS_MASK;
348 }
349 #endif
350
351 void
352 lptout(sc)
353 struct lpt_softc *sc;
354 {
355 int s;
356
357 if (sc->sc_flags & LPT_NOINTR)
358 return;
359
360 s = spltty();
361 lptintr(sc->sc_dev.dv_unit);
362 splx(s);
363
364 timeout((timeout_t)lptout, (caddr_t)sc, STEP);
365 }
366
367 /*
368 * Close the device, and free the local line buffer.
369 */
370 lptclose(dev, flag)
371 dev_t dev;
372 int flag;
373 {
374 int unit = LPTUNIT(dev);
375 struct lpt_softc *sc = &lpt_softc[unit];
376 u_short iobase = sc->sc_iobase;
377
378 if (sc->sc_count)
379 (void) pushbytes(sc);
380
381 outb(iobase + lpt_control, LPC_NINIT);
382 sc->sc_state = 0;
383 outb(iobase + lpt_control, LPC_NINIT);
384 brelse(sc->sc_inbuf);
385
386 lprintf("%s: closed\n", sc->sc_dev.dv_xname);
387 return 0;
388 }
389
390 int
391 pushbytes(sc)
392 struct lpt_softc *sc;
393 {
394 u_short iobase = sc->sc_iobase;
395 int error;
396
397 if (sc->sc_flags & LPT_NOINTR) {
398 int spin, tic;
399 u_char control = sc->sc_control;
400
401 while (sc->sc_count > 0) {
402 spin = 0;
403 while (NOT_READY() && spin++ < sc->sc_spinmax);
404 if (spin >= sc->sc_spinmax) {
405 tic = 0;
406 /* adapt busy-wait algorithm */
407 sc->sc_spinmax++;
408 while (NOT_READY()) {
409 /* exponential backoff */
410 tic = tic + tic + 1;
411 if (tic > TIMEOUT)
412 tic = TIMEOUT;
413 error = tsleep((caddr_t)sc,
414 LPTPRI | PCATCH, "lptpsh", tic);
415 if (error != EWOULDBLOCK)
416 return error;
417 }
418 }
419
420 outb(iobase + lpt_data, *sc->sc_cp++);
421 outb(iobase + lpt_control, control | LPC_STROBE);
422 sc->sc_count--;
423 outb(iobase + lpt_control, control);
424
425 /* adapt busy-wait algorithm */
426 if (spin*2 < sc->sc_spinmax)
427 sc->sc_spinmax--;
428 }
429 } else {
430 int s;
431
432 while (sc->sc_count > 0) {
433 /* if the printer is ready for a char, give it one */
434 if ((sc->sc_state & LPT_OBUSY) == 0) {
435 lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
436 sc->sc_count);
437 s = spltty();
438 (void) lptintr(sc->sc_dev.dv_unit);
439 splx(s);
440 }
441 if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
442 "lptwrite2", 0))
443 return error;
444 }
445 }
446 return 0;
447 }
448
449 /*
450 * Copy a line from user space to a local buffer, then call putc to get the
451 * chars moved to the output queue.
452 */
453 lptwrite(dev, uio)
454 dev_t dev;
455 struct uio *uio;
456 {
457 struct lpt_softc *sc = &lpt_softc[LPTUNIT(dev)];
458 size_t n;
459 int error = 0;
460
461 while (n = min(LPT_BSIZE, uio->uio_resid)) {
462 uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
463 sc->sc_count = n;
464 error = pushbytes(sc);
465 if (error) {
466 /*
467 * Return accurate residual if interrupted or timed
468 * out.
469 */
470 uio->uio_resid += sc->sc_count;
471 sc->sc_count = 0;
472 return error;
473 }
474 }
475 return 0;
476 }
477
478 /*
479 * Handle printer interrupts which occur when the printer is ready to accept
480 * another char.
481 */
482 int
483 lptintr(unit)
484 int unit;
485 {
486 struct lpt_softc *sc = &lpt_softc[unit];
487 u_short iobase = sc->sc_iobase;
488
489 #if 0
490 if ((sc->sc_state & LPT_OPEN) == 0)
491 return 0;
492 #endif
493
494 /* is printer online and ready for output */
495 if (NOT_READY())
496 return 0;
497
498 if (sc->sc_count) {
499 u_char control = sc->sc_control;
500 /* send char */
501 outb(iobase + lpt_data, *sc->sc_cp++);
502 outb(iobase + lpt_control, control | LPC_STROBE);
503 sc->sc_count--;
504 outb(iobase + lpt_control, control);
505 sc->sc_state |= LPT_OBUSY;
506 } else
507 sc->sc_state &= ~LPT_OBUSY;
508
509 if (sc->sc_count == 0) {
510 /* none, wake up the top half to get more */
511 wakeup((caddr_t)sc);
512 }
513
514 return 1;
515 }
516
517 int
518 lptioctl(dev, cmd, data, flag)
519 dev_t dev;
520 int cmd;
521 caddr_t data;
522 int flag;
523 {
524 int error = 0;
525
526 switch (cmd) {
527 default:
528 error = ENODEV;
529 }
530
531 return error;
532 }
533