lpt.c revision 1.7.4.10 1 /*
2 * Copyright (c) 1993 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.7.4.10 1993/10/16 06:39:42 mycroft Exp $
50 */
51
52 /*
53 * Device Driver for AT parallel printer port
54 */
55
56 #include "param.h"
57 #include "systm.h"
58 #include "proc.h"
59 #include "user.h"
60 #include "buf.h"
61 #include "kernel.h"
62 #include "ioctl.h"
63 #include "uio.h"
64 #include "sys/device.h"
65
66 #include "machine/cpu.h"
67 #include "machine/pio.h"
68
69 #include "i386/isa/isavar.h"
70 #include "i386/isa/icu.h"
71 #include "i386/isa/lptreg.h"
72
73 #define TIMEOUT hz*16 /* wait up to 4 seconds for a ready */
74 #define STEP hz/4
75
76 #define MAX_SPIN 255
77
78 #define LPTPRI (PZERO+8)
79 #define LPT_BSIZE 1024
80
81 #ifndef DEBUG
82 #define lprintf
83 #else
84 #define lprintf if (lptdebug) printf
85 int lptdebug = 1;
86 #endif
87
88 struct lpt_softc {
89 struct device sc_dev;
90 struct isadev sc_id;
91 struct intrhand sc_ih;
92
93 size_t sc_count;
94 struct buf *sc_inbuf;
95 u_char *sc_cp ;
96 u_short sc_iobase;
97 u_short sc_irq;
98 u_char sc_state;
99 /* bits for 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_smax;
109 };
110
111 static int lptprobe __P((struct device *, struct cfdata *, void *));
112 static void lptforceintr __P((void *));
113 static void lptattach __P((struct device *, struct device *, void *));
114 static int lptintr __P((void *));
115
116 struct cfdriver lptcd =
117 { NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc) };
118
119 #define LPTUNIT(s) ((s)&0x1f)
120 #define LPTFLAGS(s) ((s)&0xe0)
121
122 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
123 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
124 #define NOT_READY() ((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK)
125
126 static void lptout __P((struct lpt_softc *));
127 static int pushbytes __P((struct lpt_softc *));
128
129 /*
130 * Internal routine to lptprobe to do port tests of one byte value
131 */
132 int
133 lpt_port_test(port, data, mask)
134 u_short port;
135 u_char data, mask;
136 {
137 int timeout;
138 u_char temp;
139
140 data &= mask;
141 outb(port, data);
142 timeout = 100;
143 do {
144 temp = inb(port) & mask;
145 } while (temp != data && --timeout);
146 lprintf("lpt: port=0x%x out=0x%x in=0x%x\n", port, data, temp);
147 return (temp == data);
148 }
149
150 /*
151 * Logic:
152 * 1) You should be able to write to and read back the same value
153 * to the data port. Do an alternating zeros, alternating ones,
154 * walking zero, and walking one test to check for stuck bits.
155 *
156 * 2) You should be able to write to and read back the same value
157 * to the control port lower 5 bits, the upper 3 bits are reserved
158 * per the IBM PC technical reference manauls and different boards
159 * do different things with them. Do an alternating zeros, alternating
160 * ones, walking zero, and walking one test to check for stuck bits.
161 *
162 * Some printers drag the strobe line down when the are powered off
163 * so this bit has been masked out of the control port test.
164 *
165 * XXX Some printers may not like a fast pulse on init or strobe, I
166 * don't know at this point, if that becomes a problem these bits
167 * should be turned off in the mask byte for the control port test.
168 *
169 * 3) Set the data and control ports to a value of 0
170 */
171
172 static int
173 lptprobe(parent, cf, aux)
174 struct device *parent;
175 struct cfdata *cf;
176 void *aux;
177 {
178 struct isa_attach_args *ia = aux;
179 u_short iobase = ia->ia_iobase;
180 u_short port = iobase + lpt_data;
181 u_char mask = 0xff;
182 u_char data;
183 int i;
184
185 if (iobase == IOBASEUNK)
186 return 0;
187
188 for (;;) {
189 data = 0x55; /* Alternating zeros */
190 if (!lpt_port_test(port, data, mask))
191 return 0;
192
193 data = 0xaa; /* Alternating ones */
194 if (!lpt_port_test(port, data, mask))
195 return 0;
196
197 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
198 data = ~(1 << i);
199 if (!lpt_port_test(port, data, mask))
200 return 0;
201 }
202
203 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
204 data = (1 << i);
205 if (!lpt_port_test(port, data, mask))
206 return 0;
207 }
208
209 if (port == iobase + lpt_data) {
210 port = iobase + lpt_control;
211 mask = 0x1e;
212 } else
213 break;
214 }
215 outb(iobase + lpt_data, 0);
216 outb(iobase + lpt_control, 0);
217
218 if (ia->ia_irq == IRQUNK)
219 ia->ia_irq = isa_discoverintr(lptforceintr, aux);
220 /* okay if we don't have an irq; will force polling */
221
222 ia->ia_iosize = LPT_NPORTS;
223 ia->ia_drq = DRQUNK;
224 ia->ia_msize = 0;
225 return 1;
226 }
227
228 static void
229 lptattach(parent, self, aux)
230 struct device *parent, *self;
231 void *aux;
232 {
233 struct isa_attach_args *ia = aux;
234 struct lpt_softc *sc = (struct lpt_softc *)self;
235 u_short iobase = ia->ia_iobase;
236 u_short irq = ia->ia_irq;
237
238 sc->sc_iobase = iobase;
239 sc->sc_irq = irq;
240 sc->sc_state = 0;
241 outb(iobase + lpt_control, LPC_NINIT);
242
243 printf(": %sparallel port\n", irq == IRQNONE ? "polled " : "");
244 isa_establish(&sc->sc_id, &sc->sc_dev);
245
246 if (irq != IRQNONE) {
247 sc->sc_ih.ih_fun = lptintr;
248 sc->sc_ih.ih_arg = sc;
249 intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
250 }
251 }
252
253 /*
254 * lptopen -- 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(minor(dev));
262 u_char flags = LPTFLAGS(minor(dev));
263 struct lpt_softc *sc;
264 u_short iobase;
265 u_char control;
266 int error;
267 int spin;
268
269 if (unit >= lptcd.cd_ndevs)
270 return ENXIO;
271 sc = lptcd.cd_devs[unit];
272 if (!sc)
273 return ENXIO;
274
275 if (sc->sc_irq == IRQNONE && (flags & LPT_NOINTR) == 0)
276 return ENXIO;
277
278 if (sc->sc_state)
279 return EBUSY;
280
281 #ifdef DIAGNOSTIC
282 if (sc->sc_state)
283 printf("%s: state=0x%x not zero\n", sc->sc_dev.dv_xname,
284 sc->sc_state);
285 #endif
286
287 sc->sc_state = LPT_INIT;
288
289 sc->sc_flags = flags;
290 lprintf("%s: open flags=0x%x\n", sc->sc_dev.dv_xname, flags);
291 iobase = sc->sc_iobase;
292
293 if ((flags & LPT_NOPRIME) == 0) {
294 /* assert INIT for 100 usec to start up printer */
295 outb(iobase + lpt_control, LPC_SELECT);
296 delay(100);
297 }
298
299 control = LPC_SELECT | LPC_NINIT;
300 outb(iobase + lpt_control, control);
301
302 /* wait till ready (printer running diagnostics) */
303 for (spin = 0; NOT_READY(); spin += STEP) {
304 if (spin >= TIMEOUT) {
305 sc->sc_state = 0;
306 return EBUSY;
307 }
308
309 /* wait 1/4 second, give up if we get a signal */
310 if (error = tsleep((caddr_t)sc,
311 LPTPRI | PCATCH, "lptopen", STEP) != EWOULDBLOCK) {
312 sc->sc_state = 0;
313 return error;
314 }
315 }
316
317 if ((flags & LPT_NOINTR) == 0)
318 control |= LPC_IENABLE;
319 if (flags & LPT_AUTOLF)
320 control |= LPC_AUTOLF;
321 sc->sc_control = control;
322 outb(iobase + lpt_control, control);
323
324 sc->sc_state = LPT_OPEN;
325 sc->sc_inbuf = geteblk(LPT_BSIZE);
326 sc->sc_count = 0;
327 lptout(sc);
328
329 lprintf("%s: opened\n", sc->sc_dev.dv_xname);
330 return 0;
331 }
332
333 static void
334 lptout(sc)
335 struct lpt_softc *sc;
336 {
337 int s;
338
339 if ((sc->sc_state & LPT_OPEN) == 0)
340 return;
341 if (sc->sc_flags & LPT_NOINTR)
342 return;
343
344 /*
345 * Avoid possible hangs due to missed interrupts
346 */
347 if (sc->sc_count) {
348 s = spltty();
349 lptintr(sc);
350 splx(s);
351 } else {
352 sc->sc_state &= ~LPT_OBUSY;
353 wakeup((caddr_t)sc);
354 }
355
356 timeout((timeout_t)lptout, (caddr_t)sc, STEP);
357 }
358
359 /*
360 * lptclose -- close the device, free the local line buffer.
361 */
362 int
363 lptclose(dev, flag)
364 dev_t dev;
365 int flag;
366 {
367 int unit = LPTUNIT(minor(dev));
368 struct lpt_softc *sc = lptcd.cd_devs[unit];
369 u_short iobase = sc->sc_iobase;
370 int error;
371
372 if (error = pushbytes(sc))
373 return error;
374
375 outb(iobase + lpt_control, LPC_NINIT);
376 brelse(sc->sc_inbuf);
377 sc->sc_state = 0;
378
379 lprintf("%s: closed\n", sc->sc_dev.dv_xname);
380 return 0;
381 }
382
383 static int
384 pushbytes(sc)
385 struct lpt_softc *sc;
386 {
387 u_short iobase = sc->sc_iobase;
388 int error;
389
390 if (sc->sc_flags & LPT_NOINTR) {
391 int spin, tic;
392 u_char control = sc->sc_control;
393 u_char ch;
394
395 while (sc->sc_count > 0) {
396 spin = tic = 0;
397 while (NOT_READY())
398 if (++spin >= sc->sc_smax) {
399 /* exponential backoff */
400 tic = tic + tic + 1;
401 if (error = tsleep((caddr_t)sc,
402 LPTPRI | PCATCH, "lptwrite1", tic))
403 return error;
404 }
405
406 outb(iobase + lpt_data, *sc->sc_cp++);
407 outb(iobase + lpt_control, control | LPC_STROBE);
408 sc->sc_count--;
409 outb(iobase + lpt_control, control);
410
411 /* adapt busy-wait algorithm */
412 if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
413 sc->sc_smax++;
414 if (spin*2 < sc->sc_smax)
415 sc->sc_smax--;
416 }
417 } else {
418 int s;
419
420 while (sc->sc_count > 0) {
421 /* if the printer is ready for a char, give it one */
422 if ((sc->sc_state & LPT_OBUSY) == 0) {
423 lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
424 sc->sc_count);
425 s = spltty();
426 lptintr(sc);
427 splx(s);
428 }
429 if (error = tsleep((caddr_t)sc,
430 LPTPRI | PCATCH, "lptwrite2", 0))
431 return error;
432 }
433 }
434 }
435
436 /*
437 * copy a line from user space to a local buffer, then call
438 * putc to get the chars moved to the output queue.
439 */
440 int
441 lptwrite(dev, uio)
442 dev_t dev;
443 struct uio *uio;
444 {
445 int unit = LPTUNIT(minor(dev));
446 struct lpt_softc *sc = lptcd.cd_devs[unit];
447 size_t n;
448 int error = 0;
449
450 while (n = MIN(LPT_BSIZE, uio->uio_resid)) {
451 uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
452 sc->sc_count = n;
453 error = pushbytes(sc);
454 if (error) {
455 /* return accurate residual if interrupted or
456 timed out */
457 uio->uio_resid += sc->sc_count;
458 sc->sc_count = 0;
459 return error;
460 }
461 }
462 return 0;
463 }
464
465 /*
466 * lptintr -- handle printer interrupts which occur when the printer is
467 * ready to accept another char.
468 */
469 static int
470 lptintr(arg)
471 void *arg;
472 {
473 struct lpt_softc *sc = (struct lpt_softc *)arg;
474 u_short iobase = sc->sc_iobase;
475 u_char control = sc->sc_control;
476 u_char status;
477
478 if ((sc->sc_state & LPT_OPEN) == 0)
479 return 0;
480
481 /* is printer online and ready for output */
482 if (!NOT_READY()) {
483 if (sc->sc_count) {
484 /* send char */
485 sc->sc_state |= LPT_OBUSY;
486 while (sc->sc_count &&
487 (inb(iobase + lpt_status) & LPS_NBSY) == 0) {
488 outb(iobase + lpt_data, *sc->sc_cp++);
489 outb(iobase + lpt_control, control | LPC_STROBE);
490 sc->sc_count--;
491 outb(iobase + lpt_control, control);
492 }
493 } else {
494 /* none, wake up the top half to get more */
495 sc->sc_state &= ~LPT_OBUSY;
496 wakeup((caddr_t)sc);
497 }
498 }
499
500 return 1;
501 }
502
503 int
504 lptioctl(dev, cmd, data, flag)
505 dev_t dev;
506 int cmd;
507 caddr_t data;
508 int flag;
509 {
510 int error = 0;
511
512 switch (cmd) {
513 default:
514 error = ENODEV;
515 }
516
517 return error;
518 }
519