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