lpt.c revision 1.42 1 /* $NetBSD: lpt.c,v 1.42 1996/10/21 22:41:14 thorpej 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 #include <machine/bus.h>
69 #include <machine/intr.h>
70
71 #include <dev/isa/isavar.h>
72 #include <dev/isa/lptreg.h>
73
74 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
75 #define STEP hz/4
76
77 #define LPTPRI (PZERO+8)
78 #define LPT_BSIZE 1024
79
80 #if !defined(DEBUG) || !defined(notdef)
81 #define LPRINTF(a)
82 #else
83 #define LPRINTF if (lptdebug) printf a
84 int lptdebug = 1;
85 #endif
86
87 struct lpt_softc {
88 struct device sc_dev;
89 void *sc_ih;
90
91 size_t sc_count;
92 struct buf *sc_inbuf;
93 u_char *sc_cp;
94 int sc_spinmax;
95 int sc_iobase;
96 bus_space_tag_t sc_iot;
97 bus_space_handle_t sc_ioh;
98 int sc_irq;
99 u_char sc_state;
100 #define LPT_OPEN 0x01 /* device is open */
101 #define LPT_OBUSY 0x02 /* printer is busy doing output */
102 #define LPT_INIT 0x04 /* waiting to initialize for open */
103 u_char sc_flags;
104 #define LPT_AUTOLF 0x20 /* automatic LF on CR */
105 #define LPT_NOPRIME 0x40 /* don't prime on open */
106 #define LPT_NOINTR 0x80 /* do not use interrupt */
107 u_char sc_control;
108 u_char sc_laststatus;
109 };
110
111 /* XXX does not belong here */
112 cdev_decl(lpt);
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_space_read_1(iot, ioh, lpt_status) ^ LPS_INVERT) & LPS_MASK)
132 #define NOT_READY_ERR() not_ready(bus_space_read_1(iot, 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_space_tag_t, bus_space_handle_t, bus_addr_t,
139 bus_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(iot, ioh, base, off, data, mask)
146 bus_space_tag_t iot;
147 bus_space_handle_t ioh;
148 bus_addr_t base;
149 bus_size_t off;
150 u_char data, mask;
151 {
152 int timeout;
153 u_char temp;
154
155 data &= mask;
156 bus_space_write_1(iot, ioh, off, data);
157 timeout = 1000;
158 do {
159 delay(10);
160 temp = bus_space_read_1(iot, 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_space_tag_t iot;
195 bus_space_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 iot = ia->ia_iot;
208 base = ia->ia_iobase;
209 if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
210 return 0;
211
212 rv = 0;
213 mask = 0xff;
214
215 data = 0x55; /* Alternating zeros */
216 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
217 ABORT;
218
219 data = 0xaa; /* Alternating ones */
220 if (!lpt_port_test(iot, 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(iot, 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(iot, ioh, base, lpt_data, data, mask))
232 ABORT;
233 }
234
235 bus_space_write_1(iot, ioh, lpt_data, 0);
236 bus_space_write_1(iot, 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_space_unmap(iot, 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_space_tag_t iot;
256 bus_space_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 iot = sc->sc_iot = ia->ia_iot;
268 if (bus_space_map(iot, sc->sc_iobase, LPT_NPORTS, 0, &ioh))
269 panic("lptattach: couldn't map I/O ports");
270 sc->sc_ioh = ioh;
271
272 bus_space_write_1(iot, 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, mode, p)
284 dev_t dev;
285 int flag;
286 int mode;
287 struct proc *p;
288 {
289 int unit = LPTUNIT(dev);
290 u_char flags = LPTFLAGS(dev);
291 struct lpt_softc *sc;
292 bus_space_tag_t iot;
293 bus_space_handle_t ioh;
294 u_char control;
295 int error;
296 int spin;
297
298 if (unit >= lpt_cd.cd_ndevs)
299 return ENXIO;
300 sc = lpt_cd.cd_devs[unit];
301 if (!sc)
302 return ENXIO;
303
304 if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
305 return ENXIO;
306
307 #ifdef DIAGNOSTIC
308 if (sc->sc_state)
309 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
310 sc->sc_state);
311 #endif
312
313 if (sc->sc_state)
314 return EBUSY;
315
316 sc->sc_state = LPT_INIT;
317 sc->sc_flags = flags;
318 LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags));
319 iot = sc->sc_iot;
320 ioh = sc->sc_ioh;
321
322 if ((flags & LPT_NOPRIME) == 0) {
323 /* assert INIT for 100 usec to start up printer */
324 bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
325 delay(100);
326 }
327
328 control = LPC_SELECT | LPC_NINIT;
329 bus_space_write_1(iot, ioh, lpt_control, control);
330
331 /* wait till ready (printer running diagnostics) */
332 for (spin = 0; NOT_READY_ERR(); spin += STEP) {
333 if (spin >= TIMEOUT) {
334 sc->sc_state = 0;
335 return EBUSY;
336 }
337
338 /* wait 1/4 second, give up if we get a signal */
339 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
340 if (error != EWOULDBLOCK) {
341 sc->sc_state = 0;
342 return error;
343 }
344 }
345
346 if ((flags & LPT_NOINTR) == 0)
347 control |= LPC_IENABLE;
348 if (flags & LPT_AUTOLF)
349 control |= LPC_AUTOLF;
350 sc->sc_control = control;
351 bus_space_write_1(iot, ioh, lpt_control, control);
352
353 sc->sc_inbuf = geteblk(LPT_BSIZE);
354 sc->sc_count = 0;
355 sc->sc_state = LPT_OPEN;
356
357 if ((sc->sc_flags & LPT_NOINTR) == 0)
358 lptwakeup(sc);
359
360 LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
361 return 0;
362 }
363
364 int
365 not_ready(status, sc)
366 u_char status;
367 struct lpt_softc *sc;
368 {
369 u_char new;
370
371 status = (status ^ LPS_INVERT) & LPS_MASK;
372 new = status & ~sc->sc_laststatus;
373 sc->sc_laststatus = status;
374
375 if (new & LPS_SELECT)
376 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
377 else if (new & LPS_NOPAPER)
378 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
379 else if (new & LPS_NERR)
380 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
381
382 return status;
383 }
384
385 void
386 lptwakeup(arg)
387 void *arg;
388 {
389 struct lpt_softc *sc = arg;
390 int s;
391
392 s = spltty();
393 lptintr(sc);
394 splx(s);
395
396 timeout(lptwakeup, sc, STEP);
397 }
398
399 /*
400 * Close the device, and free the local line buffer.
401 */
402 int
403 lptclose(dev, flag, mode, p)
404 dev_t dev;
405 int flag;
406 int mode;
407 struct proc *p;
408 {
409 int unit = LPTUNIT(dev);
410 struct lpt_softc *sc = lpt_cd.cd_devs[unit];
411 bus_space_tag_t iot = sc->sc_iot;
412 bus_space_handle_t ioh = sc->sc_ioh;
413
414 if (sc->sc_count)
415 (void) pushbytes(sc);
416
417 if ((sc->sc_flags & LPT_NOINTR) == 0)
418 untimeout(lptwakeup, sc);
419
420 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
421 sc->sc_state = 0;
422 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
423 brelse(sc->sc_inbuf);
424
425 LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
426 return 0;
427 }
428
429 int
430 pushbytes(sc)
431 struct lpt_softc *sc;
432 {
433 bus_space_tag_t iot = sc->sc_iot;
434 bus_space_handle_t ioh = sc->sc_ioh;
435 int error;
436
437 if (sc->sc_flags & LPT_NOINTR) {
438 int spin, tic;
439 u_char control = sc->sc_control;
440
441 while (sc->sc_count > 0) {
442 spin = 0;
443 while (NOT_READY()) {
444 if (++spin < sc->sc_spinmax)
445 continue;
446 tic = 0;
447 /* adapt busy-wait algorithm */
448 sc->sc_spinmax++;
449 while (NOT_READY_ERR()) {
450 /* exponential backoff */
451 tic = tic + tic + 1;
452 if (tic > TIMEOUT)
453 tic = TIMEOUT;
454 error = tsleep((caddr_t)sc,
455 LPTPRI | PCATCH, "lptpsh", tic);
456 if (error != EWOULDBLOCK)
457 return error;
458 }
459 break;
460 }
461
462 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
463 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
464 sc->sc_count--;
465 bus_space_write_1(iot, ioh, lpt_control, control);
466
467 /* adapt busy-wait algorithm */
468 if (spin*2 + 16 < sc->sc_spinmax)
469 sc->sc_spinmax--;
470 }
471 } else {
472 int s;
473
474 while (sc->sc_count > 0) {
475 /* if the printer is ready for a char, give it one */
476 if ((sc->sc_state & LPT_OBUSY) == 0) {
477 LPRINTF(("%s: write %d\n", sc->sc_dev.dv_xname,
478 sc->sc_count));
479 s = spltty();
480 (void) lptintr(sc);
481 splx(s);
482 }
483 error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
484 "lptwrite2", 0);
485 if (error)
486 return error;
487 }
488 }
489 return 0;
490 }
491
492 /*
493 * Copy a line from user space to a local buffer, then call putc to get the
494 * chars moved to the output queue.
495 */
496 int
497 lptwrite(dev, uio, flags)
498 dev_t dev;
499 struct uio *uio;
500 int flags;
501 {
502 struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)];
503 size_t n;
504 int error = 0;
505
506 while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
507 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
508 sc->sc_count = n;
509 error = pushbytes(sc);
510 if (error) {
511 /*
512 * Return accurate residual if interrupted or timed
513 * out.
514 */
515 uio->uio_resid += sc->sc_count;
516 sc->sc_count = 0;
517 return error;
518 }
519 }
520 return 0;
521 }
522
523 /*
524 * Handle printer interrupts which occur when the printer is ready to accept
525 * another char.
526 */
527 int
528 lptintr(arg)
529 void *arg;
530 {
531 struct lpt_softc *sc = arg;
532 bus_space_tag_t iot = sc->sc_iot;
533 bus_space_handle_t ioh = sc->sc_ioh;
534
535 #if 0
536 if ((sc->sc_state & LPT_OPEN) == 0)
537 return 0;
538 #endif
539
540 /* is printer online and ready for output */
541 if (NOT_READY() && NOT_READY_ERR())
542 return 0;
543
544 if (sc->sc_count) {
545 u_char control = sc->sc_control;
546 /* send char */
547 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
548 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
549 sc->sc_count--;
550 bus_space_write_1(iot, ioh, lpt_control, control);
551 sc->sc_state |= LPT_OBUSY;
552 } else
553 sc->sc_state &= ~LPT_OBUSY;
554
555 if (sc->sc_count == 0) {
556 /* none, wake up the top half to get more */
557 wakeup((caddr_t)sc);
558 }
559
560 return 1;
561 }
562
563 int
564 lptioctl(dev, cmd, data, flag, p)
565 dev_t dev;
566 u_long cmd;
567 caddr_t data;
568 int flag;
569 struct proc *p;
570 {
571 int error = 0;
572
573 switch (cmd) {
574 default:
575 error = ENODEV;
576 }
577
578 return error;
579 }
580