lpt.c revision 1.32 1 /* $NetBSD: lpt.c,v 1.32 1996/03/08 22:17:58 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994 Charles Hannum.
5 * Copyright (c) 1990 William F. Jolitz, TeleMuse
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This software is a component of "386BSD" developed by
19 * William F. Jolitz, TeleMuse.
20 * 4. Neither the name of the developer nor the name "386BSD"
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
25 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
26 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
27 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
28 * NOT MAKE USE OF THIS WORK.
29 *
30 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
31 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
32 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
33 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
34 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
35 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
36 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
37 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
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/bus.h>
69
70 #include <dev/isa/isavar.h>
71 #include <dev/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 void *sc_ih;
89
90 size_t sc_count;
91 struct buf *sc_inbuf;
92 u_char *sc_cp;
93 int sc_spinmax;
94 int sc_iobase;
95 bus_chipset_tag_t sc_bc;
96 bus_io_handle_t sc_ioh;
97 int 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 u_char sc_laststatus;
108 };
109
110 int lptprobe __P((struct device *, void *, void *));
111 void lptattach __P((struct device *, struct device *, void *));
112 int lptintr __P((void *));
113
114 struct cfdriver lptcd = {
115 NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc)
116 };
117
118 #define LPTUNIT(s) (minor(s) & 0x1f)
119 #define LPTFLAGS(s) (minor(s) & 0xe0)
120
121 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
122 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
123 #define NOT_READY() ((bus_io_read_1(bc, ioh, lpt_status) ^ LPS_INVERT) & LPS_MASK)
124 #define NOT_READY_ERR() not_ready(bus_io_read_1(bc, ioh, lpt_status), sc)
125 static int not_ready __P((u_char, struct lpt_softc *));
126
127 static void lptwakeup __P((void *arg));
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(ioh, off, data, mask, base)
135 bus_io_handle_t ioh;
136 size_t off;
137 u_char data, mask;
138 u_long base;
139 {
140 int timeout;
141 u_char temp;
142
143 data &= mask;
144 bus_io_write_1(bc, ioh, off, data);
145 timeout = 1000;
146 do {
147 delay(10);
148 temp = bus_io_read_1(bc, ioh, off) & mask;
149 } while (temp != data && --timeout);
150 lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", base + off,
151 data, 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(parent, match, aux)
178 struct device *parent;
179 void *match, *aux;
180 {
181 struct isa_attach_args *ia = aux;
182 bus_chipset_tag_t bc;
183 bus_io_handle_t ioh;
184 u_long base;
185 u_char mask, data;
186 int i, rv;
187
188 #ifdef DEBUG
189 #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
190 goto out;} while (0)
191 #else
192 #define ABORT goto out
193 #endif
194
195 bc = ia->ia_bc;
196 base = ia->ia_iobase;
197 if (bus_io_map(bc, base, LPT_NPORTS, &ioh))
198 return 0;
199
200 rv = 0;
201 mask = 0xff;
202
203 data = 0x55; /* Alternating zeros */
204 if (!lpt_port_test(ioh, lpt_data, data, mask, base))
205 ABORT;
206
207 data = 0xaa; /* Alternating ones */
208 if (!lpt_port_test(ioh, lpt_data, data, mask, base))
209 ABORT;
210
211 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
212 data = ~(1 << i);
213 if (!lpt_port_test(ioh, lpt_data, data, mask, base))
214 ABORT;
215 }
216
217 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
218 data = (1 << i);
219 if (!lpt_port_test(ioh, lpt_data, data, mask, base))
220 ABORT;
221 }
222
223 bus_io_write_1(bc, ioh, lpt_data, 0);
224 bus_io_write_1(bc, ioh, lpt_control, 0);
225
226 ia->ia_iosize = LPT_NPORTS;
227 ia->ia_msize = 0;
228
229 rv = 1;
230
231 out:
232 bus_io_unmap(bc, ioh, LPT_NPORTS);
233 return rv;
234 }
235
236 void
237 lptattach(parent, self, aux)
238 struct device *parent, *self;
239 void *aux;
240 {
241 struct lpt_softc *sc = (void *)self;
242 struct isa_attach_args *ia = aux;
243 bus_chipset_tag_t bc;
244 bus_io_handle_t ioh;
245
246 if (ia->ia_irq != IRQUNK)
247 printf("\n");
248 else
249 printf(": polled\n");
250
251 sc->sc_iobase = ia->ia_iobase;
252 sc->sc_irq = ia->ia_irq;
253 sc->sc_state = 0;
254
255 bc = sc->sc_bc = ia->ia_bc;
256 if (bus_io_map(bc, sc->sc_iobase, LPT_NPORTS, &ioh))
257 panic("lptattach: couldn't map I/O ports");
258 sc->sc_ioh = ioh;
259
260 bus_io_write_1(bc, ioh, lpt_control, LPC_NINIT);
261
262 if (ia->ia_irq != IRQUNK)
263 sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_NONE,
264 lptintr, sc);
265 }
266
267 /*
268 * Reset the printer, then wait until it's selected and not busy.
269 */
270 int
271 lptopen(dev, flag)
272 dev_t dev;
273 int flag;
274 {
275 int unit = LPTUNIT(dev);
276 u_char flags = LPTFLAGS(dev);
277 struct lpt_softc *sc;
278 bus_chipset_tag_t bc;
279 bus_io_handle_t ioh;
280 u_char control;
281 int error;
282 int spin;
283
284 if (unit >= lptcd.cd_ndevs)
285 return ENXIO;
286 sc = lptcd.cd_devs[unit];
287 if (!sc)
288 return ENXIO;
289
290 if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
291 return ENXIO;
292
293 #ifdef DIAGNOSTIC
294 if (sc->sc_state)
295 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
296 sc->sc_state);
297 #endif
298
299 if (sc->sc_state)
300 return EBUSY;
301
302 sc->sc_state = LPT_INIT;
303 sc->sc_flags = flags;
304 lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
305 bc = sc->sc_bc;
306 ioh = sc->sc_ioh;
307
308 if ((flags & LPT_NOPRIME) == 0) {
309 /* assert INIT for 100 usec to start up printer */
310 bus_io_write_1(bc, ioh, lpt_control, LPC_SELECT);
311 delay(100);
312 }
313
314 control = LPC_SELECT | LPC_NINIT;
315 bus_io_write_1(bc, ioh, lpt_control, control);
316
317 /* wait till ready (printer running diagnostics) */
318 for (spin = 0; NOT_READY_ERR(); spin += STEP) {
319 if (spin >= TIMEOUT) {
320 sc->sc_state = 0;
321 return EBUSY;
322 }
323
324 /* wait 1/4 second, give up if we get a signal */
325 if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen",
326 STEP) != EWOULDBLOCK) {
327 sc->sc_state = 0;
328 return error;
329 }
330 }
331
332 if ((flags & LPT_NOINTR) == 0)
333 control |= LPC_IENABLE;
334 if (flags & LPT_AUTOLF)
335 control |= LPC_AUTOLF;
336 sc->sc_control = control;
337 bus_io_write_1(bc, ioh, lpt_control, control);
338
339 sc->sc_inbuf = geteblk(LPT_BSIZE);
340 sc->sc_count = 0;
341 sc->sc_state = LPT_OPEN;
342
343 if ((sc->sc_flags & LPT_NOINTR) == 0)
344 lptwakeup(sc);
345
346 lprintf("%s: opened\n", sc->sc_dev.dv_xname);
347 return 0;
348 }
349
350 int
351 not_ready(status, sc)
352 u_char status;
353 struct lpt_softc *sc;
354 {
355 u_char new;
356
357 status = (status ^ LPS_INVERT) & LPS_MASK;
358 new = status & ~sc->sc_laststatus;
359 sc->sc_laststatus = status;
360
361 if (new & LPS_SELECT)
362 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
363 else if (new & LPS_NOPAPER)
364 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
365 else if (new & LPS_NERR)
366 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
367
368 return status;
369 }
370
371 void
372 lptwakeup(arg)
373 void *arg;
374 {
375 struct lpt_softc *sc = arg;
376 int s;
377
378 s = spltty();
379 lptintr(sc);
380 splx(s);
381
382 timeout(lptwakeup, sc, STEP);
383 }
384
385 /*
386 * Close the device, and free the local line buffer.
387 */
388 lptclose(dev, flag)
389 dev_t dev;
390 int flag;
391 {
392 int unit = LPTUNIT(dev);
393 struct lpt_softc *sc = lptcd.cd_devs[unit];
394 bus_chipset_tag_t bc = sc->sc_bc;
395 bus_io_handle_t ioh = sc->sc_ioh;
396
397 if (sc->sc_count)
398 (void) pushbytes(sc);
399
400 if ((sc->sc_flags & LPT_NOINTR) == 0)
401 untimeout(lptwakeup, sc);
402
403 bus_io_write_1(bc, ioh, lpt_control, LPC_NINIT);
404 sc->sc_state = 0;
405 bus_io_write_1(bc, ioh, lpt_control, LPC_NINIT);
406 brelse(sc->sc_inbuf);
407
408 lprintf("%s: closed\n", sc->sc_dev.dv_xname);
409 return 0;
410 }
411
412 int
413 pushbytes(sc)
414 struct lpt_softc *sc;
415 {
416 bus_chipset_tag_t bc = sc->sc_bc;
417 bus_io_handle_t ioh = sc->sc_ioh;
418 int error;
419
420 if (sc->sc_flags & LPT_NOINTR) {
421 int spin, tic;
422 u_char control = sc->sc_control;
423
424 while (sc->sc_count > 0) {
425 spin = 0;
426 while (NOT_READY()) {
427 if (++spin < sc->sc_spinmax)
428 continue;
429 tic = 0;
430 /* adapt busy-wait algorithm */
431 sc->sc_spinmax++;
432 while (NOT_READY_ERR()) {
433 /* exponential backoff */
434 tic = tic + tic + 1;
435 if (tic > TIMEOUT)
436 tic = TIMEOUT;
437 error = tsleep((caddr_t)sc,
438 LPTPRI | PCATCH, "lptpsh", tic);
439 if (error != EWOULDBLOCK)
440 return error;
441 }
442 break;
443 }
444
445 bus_io_write_1(bc, ioh, lpt_data, *sc->sc_cp++);
446 bus_io_write_1(bc, ioh, lpt_control, control | LPC_STROBE);
447 sc->sc_count--;
448 bus_io_write_1(bc, ioh, lpt_control, control);
449
450 /* adapt busy-wait algorithm */
451 if (spin*2 + 16 < sc->sc_spinmax)
452 sc->sc_spinmax--;
453 }
454 } else {
455 int s;
456
457 while (sc->sc_count > 0) {
458 /* if the printer is ready for a char, give it one */
459 if ((sc->sc_state & LPT_OBUSY) == 0) {
460 lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
461 sc->sc_count);
462 s = spltty();
463 (void) lptintr(sc);
464 splx(s);
465 }
466 if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
467 "lptwrite2", 0))
468 return error;
469 }
470 }
471 return 0;
472 }
473
474 /*
475 * Copy a line from user space to a local buffer, then call putc to get the
476 * chars moved to the output queue.
477 */
478 lptwrite(dev, uio)
479 dev_t dev;
480 struct uio *uio;
481 {
482 struct lpt_softc *sc = lptcd.cd_devs[LPTUNIT(dev)];
483 size_t n;
484 int error = 0;
485
486 while (n = min(LPT_BSIZE, uio->uio_resid)) {
487 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
488 sc->sc_count = n;
489 error = pushbytes(sc);
490 if (error) {
491 /*
492 * Return accurate residual if interrupted or timed
493 * out.
494 */
495 uio->uio_resid += sc->sc_count;
496 sc->sc_count = 0;
497 return error;
498 }
499 }
500 return 0;
501 }
502
503 /*
504 * Handle printer interrupts which occur when the printer is ready to accept
505 * another char.
506 */
507 int
508 lptintr(arg)
509 void *arg;
510 {
511 struct lpt_softc *sc = arg;
512 bus_chipset_tag_t bc = sc->sc_bc;
513 bus_io_handle_t ioh = sc->sc_ioh;
514
515 #if 0
516 if ((sc->sc_state & LPT_OPEN) == 0)
517 return 0;
518 #endif
519
520 /* is printer online and ready for output */
521 if (NOT_READY() && NOT_READY_ERR())
522 return 0;
523
524 if (sc->sc_count) {
525 u_char control = sc->sc_control;
526 /* send char */
527 bus_io_write_1(bc, ioh, lpt_data, *sc->sc_cp++);
528 bus_io_write_1(bc, ioh, lpt_control, control | LPC_STROBE);
529 sc->sc_count--;
530 bus_io_write_1(bc, ioh, lpt_control, control);
531 sc->sc_state |= LPT_OBUSY;
532 } else
533 sc->sc_state &= ~LPT_OBUSY;
534
535 if (sc->sc_count == 0) {
536 /* none, wake up the top half to get more */
537 wakeup((caddr_t)sc);
538 }
539
540 return 1;
541 }
542
543 int
544 lptioctl(dev, cmd, data, flag)
545 dev_t dev;
546 u_long cmd;
547 caddr_t data;
548 int flag;
549 {
550 int error = 0;
551
552 switch (cmd) {
553 default:
554 error = ENODEV;
555 }
556
557 return error;
558 }
559