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