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