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