bridge(4): document net.link.bridge.pfil_onlyip
[dragonfly.git] / sys / dev / misc / lpt / lpt.c
bloba90ee2bbccbfb30e6455216a8bd16ee9b44ff0d8
1 /*
2 * Copyright (c) 1990 William F. Jolitz, TeleMuse
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This software is a component of "386BSD" developed by
16 * William F. Jolitz, TeleMuse.
17 * 4. Neither the name of the developer nor the name "386BSD"
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
22 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
23 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
24 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
25 * NOT MAKE USE OF THIS WORK.
27 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
28 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
29 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
30 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
31 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
32 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
33 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
34 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
36 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
48 * from: unknown origin, 386BSD 0.1
49 * From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
50 * From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
51 * $FreeBSD: src/sys/dev/ppbus/lpt.c,v 1.15.2.3 2000/07/07 00:30:40 obrien Exp $
55 * Device Driver for AT parallel printer port
56 * Written by William Jolitz 12/18/90
60 * Updated for ppbus by Nicolas Souchu
61 * [Mon Jul 28 1997]
64 #include "opt_lpt.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/module.h>
69 #include <sys/bus.h>
70 #include <sys/conf.h>
71 #include <sys/device.h>
72 #include <sys/kernel.h>
73 #include <sys/uio.h>
74 #include <sys/syslog.h>
75 #include <sys/thread2.h>
76 #include <sys/malloc.h>
77 #include <sys/rman.h>
79 #include <machine/clock.h>
80 #include <machine/inttypes.h>
82 #include "lptio.h"
83 #include <bus/ppbus/ppbconf.h>
84 #include <bus/ppbus/ppb_1284.h>
85 #include "lpt.h"
86 #include "ppbus_if.h"
87 #include <bus/ppbus/ppbio.h>
89 MALLOC_DEFINE(M_LPT, "lpt", "LPT buffers");
91 #ifndef LPT_DEBUG
92 #define lprintf(args)
93 #else
94 #define lprintf(args) \
95 do { \
96 if (lptflag) \
97 kprintf args; \
98 } while (0)
99 static int volatile lptflag = 1;
100 #endif
102 #define LPINITRDY 4 /* wait up to 4 seconds for a ready */
103 #define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */
104 #define LPTOUTMAX 1 /* maximal timeout 1 s */
105 #define BUFSIZE 1024
106 #define BUFSTATSIZE 32
108 #define LPTUNIT(s) ((s)&0x03)
109 #define LPTFLAGS(s) ((s)&0xfc)
111 struct lpt_data {
112 short sc_state;
113 /* default case: negative prime, negative ack, handshake strobe,
114 prime once */
115 u_char sc_control;
116 char sc_flags;
117 #define LP_UNITMASK 0x03 /* up to 4 units */
118 #define LP_POS_INIT 0x04 /* if we are a postive init signal */
119 #define LP_POS_ACK 0x08 /* if we are a positive going ack */
120 #define LP_NO_PRIME 0x10 /* don't prime the printer at all */
121 #define LP_PRIMEOPEN 0x20 /* prime on every open */
122 #define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */
123 #define LP_BYPASS 0x80 /* bypass printer ready checks */
124 void *sc_inbuf;
125 void *sc_statbuf;
126 short sc_xfercnt ;
127 char sc_primed;
128 char *sc_cp ;
129 u_short sc_irq ; /* IRQ status of port */
130 #define LP_HAS_IRQ 0x01 /* we have an irq available */
131 #define LP_USE_IRQ 0x02 /* we are using our irq */
132 #define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */
133 #define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */
134 u_char sc_backoff ; /* time to call lptout() again */
136 struct resource *intr_resource; /* interrupt resource */
137 void *intr_cookie; /* interrupt registration cookie */
138 struct callout sc_callout;
141 #define LPT_NAME "lpt" /* our official name */
143 static timeout_t lptout;
144 static int lpt_port_test(device_t dev, u_char data, u_char mask);
145 static int lpt_detect(device_t dev);
147 #define DEVTOSOFTC(dev) \
148 ((struct lpt_data *)device_get_softc(dev))
149 #define UNITOSOFTC(unit) \
150 ((struct lpt_data *)devclass_get_softc(lpt_devclass, (unit)))
151 #define UNITODEVICE(unit) \
152 (devclass_get_device(lpt_devclass, (unit)))
154 static void lptintr(device_t dev);
155 static void lpt_intr(void *arg); /* without spls */
157 static devclass_t lpt_devclass;
160 /* bits for state */
161 #define OPEN (1<<0) /* device is open */
162 #define ASLP (1<<1) /* awaiting draining of printer */
163 #define EERROR (1<<2) /* error was received from printer */
164 #define OBUSY (1<<3) /* printer is busy doing output */
165 #define LPTOUT (1<<4) /* timeout while not selected */
166 #define TOUT (1<<5) /* timeout while not selected */
167 #define LPTINIT (1<<6) /* waiting to initialize for open */
168 #define INTERRUPTED (1<<7) /* write call was interrupted */
169 #define HAVEBUS (1<<8) /* the driver owns the bus */
171 /* status masks to interrogate printer status */
172 #define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
173 #define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
175 /* Printer Ready condition - from lpa.c */
176 /* Only used in polling code */
177 #define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
178 #define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
179 #define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
181 #define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
182 #define MAX_SPIN 20 /* Max delay for device ready in usecs */
185 static d_open_t lptopen;
186 static d_close_t lptclose;
187 static d_write_t lptwrite;
188 static d_read_t lptread;
189 static d_ioctl_t lptioctl;
191 static struct dev_ops lpt_ops = {
192 { LPT_NAME, 0, 0 },
193 .d_open = lptopen,
194 .d_close = lptclose,
195 .d_read = lptread,
196 .d_write = lptwrite,
197 .d_ioctl = lptioctl,
200 static int
201 lpt_request_ppbus(device_t dev, int how)
203 device_t ppbus = device_get_parent(dev);
204 struct lpt_data *sc = DEVTOSOFTC(dev);
205 int error;
207 if (sc->sc_state & HAVEBUS)
208 return (0);
210 /* we have the bus only if the request succeeded */
211 if ((error = ppb_request_bus(ppbus, dev, how)) == 0)
212 sc->sc_state |= HAVEBUS;
214 return (error);
217 static int
218 lpt_release_ppbus(device_t dev)
220 device_t ppbus = device_get_parent(dev);
221 struct lpt_data *sc = DEVTOSOFTC(dev);
222 int error = 0;
224 if ((error = ppb_release_bus(ppbus, dev)) == 0)
225 sc->sc_state &= ~HAVEBUS;
227 return (error);
231 * Internal routine to lptprobe to do port tests of one byte value
233 static int
234 lpt_port_test(device_t ppbus, u_char data, u_char mask)
236 int temp, timeout;
238 data = data & mask;
239 ppb_wdtr(ppbus, data);
240 timeout = 10000;
241 do {
242 DELAY(10);
243 temp = ppb_rdtr(ppbus) & mask;
245 while (temp != data && --timeout);
246 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
247 return (temp == data);
251 * Probe simplified by replacing multiple loops with a hardcoded
252 * test pattern - 1999/02/08 des@freebsd.org
254 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
255 * Based partially on Rod Grimes' printer probe
257 * Logic:
258 * 1) If no port address was given, use the bios detected ports
259 * and autodetect what ports the printers are on.
260 * 2) Otherwise, probe the data port at the address given,
261 * using the method in Rod Grimes' port probe.
262 * (Much code ripped off directly from Rod's probe.)
264 * Comments from Rod's probe:
265 * Logic:
266 * 1) You should be able to write to and read back the same value
267 * to the data port. Do an alternating zeros, alternating ones,
268 * walking zero, and walking one test to check for stuck bits.
270 * 2) You should be able to write to and read back the same value
271 * to the control port lower 5 bits, the upper 3 bits are reserved
272 * per the IBM PC technical reference manauls and different boards
273 * do different things with them. Do an alternating zeros, alternating
274 * ones, walking zero, and walking one test to check for stuck bits.
276 * Some printers drag the strobe line down when the are powered off
277 * so this bit has been masked out of the control port test.
279 * XXX Some printers may not like a fast pulse on init or strobe, I
280 * don't know at this point, if that becomes a problem these bits
281 * should be turned off in the mask byte for the control port test.
283 * We are finally left with a mask of 0x14, due to some printers
284 * being adamant about holding other bits high ........
286 * Before probing the control port, we write a 0 to the data port -
287 * If not, some printers chuck out garbage when the strobe line
288 * gets toggled.
290 * 3) Set the data and control ports to a value of 0
292 * This probe routine has been tested on Epson Lx-800, HP LJ3P,
293 * Epson FX-1170 and C.Itoh 8510RM
294 * printers.
295 * Quick exit on fail added.
297 static int
298 lpt_detect(device_t dev)
300 device_t ppbus = device_get_parent(dev);
302 static u_char testbyte[18] = {
303 0x55, /* alternating zeros */
304 0xaa, /* alternating ones */
305 0xfe, 0xfd, 0xfb, 0xf7,
306 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
307 0x01, 0x02, 0x04, 0x08,
308 0x10, 0x20, 0x40, 0x80 /* walking one */
310 int i, error, status;
312 status = 1; /* assume success */
314 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
315 kprintf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
316 status = 0;
317 goto end_probe;
320 for (i = 0; i < 18 && status; i++)
321 if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
322 status = 0;
323 goto end_probe;
326 end_probe:
327 /* write 0's to control and data ports */
328 ppb_wdtr(ppbus, 0);
329 ppb_wctr(ppbus, 0);
331 lpt_release_ppbus(dev);
333 return (status);
337 * lpt_probe()
339 static int
340 lpt_probe(device_t dev)
342 struct lpt_data *sc;
344 sc = DEVTOSOFTC(dev);
345 bzero(sc, sizeof(struct lpt_data));
348 * Now, try to detect the printer.
350 if (!lpt_detect(dev))
351 return (ENXIO);
353 device_set_desc(dev, "Printer");
355 return (0);
358 static int
359 lpt_attach(device_t dev)
361 device_t ppbus = device_get_parent(dev);
362 struct lpt_data *sc = DEVTOSOFTC(dev);
363 int zero = 0, unit = device_get_unit(dev);
364 int error;
365 uintptr_t irq;
367 sc->sc_primed = 0; /* not primed yet */
368 callout_init(&sc->sc_callout);
370 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
371 kprintf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
372 return (0);
375 ppb_wctr(ppbus, LPC_NINIT);
377 /* check if we can use interrupt, should be done by ppc stuff */
378 lprintf(("oldirq %x\n", sc->sc_irq));
380 /* retrieve the ppbus irq */
381 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
383 if (irq > 0) {
384 /* declare our interrupt handler */
385 sc->intr_resource = bus_alloc_legacy_irq_resource(dev, &zero,
386 irq, RF_SHAREABLE);
388 if (sc->intr_resource) {
389 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
390 device_printf(dev, "Interrupt-driven port\n");
391 } else {
392 sc->sc_irq = 0;
393 device_printf(dev, "Polled port\n");
395 lprintf(("irq %"PRIxPTR" %x\n", irq, sc->sc_irq));
397 lpt_release_ppbus(dev);
399 make_dev(&lpt_ops, unit, UID_ROOT, GID_WHEEL,
400 0600, LPT_NAME "%d", unit);
401 make_dev(&lpt_ops, unit | LP_BYPASS, UID_ROOT, GID_WHEEL,
402 0600, LPT_NAME "%d.ctl", unit);
403 return (0);
406 static void
407 lptout(void *arg)
409 device_t dev = (device_t)arg;
410 struct lpt_data *sc = DEVTOSOFTC(dev);
411 #ifdef LPT_DEBUG
412 device_t ppbus = device_get_parent(dev);
413 #endif
415 lprintf(("T %x ", ppb_rstr(ppbus)));
416 if (sc->sc_state & OPEN) {
417 sc->sc_backoff++;
418 if (sc->sc_backoff > hz/LPTOUTMAX)
419 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
420 callout_reset(&sc->sc_callout, sc->sc_backoff, lptout, dev);
421 } else {
422 sc->sc_state &= ~TOUT;
425 if (sc->sc_state & EERROR)
426 sc->sc_state &= ~EERROR;
429 * Avoid possible hangs due to missed interrupts
431 if (sc->sc_xfercnt) {
432 lptintr(dev);
433 } else {
434 sc->sc_state &= ~OBUSY;
435 wakeup((caddr_t)dev);
440 * lptopen -- reset the printer, then wait until it's selected and not busy.
441 * If LP_BYPASS flag is selected, then we do not try to select the
442 * printer -- this is just used for passing ioctls.
445 static int
446 lptopen(struct dev_open_args *ap)
448 cdev_t dev = ap->a_head.a_dev;
449 int trys, err;
450 u_int unit = LPTUNIT(minor(dev));
451 struct lpt_data *sc = UNITOSOFTC(unit);
452 device_t lptdev = UNITODEVICE(unit);
453 device_t ppbus = device_get_parent(lptdev);
455 if (!sc)
456 return (ENXIO);
458 if (sc->sc_state) {
459 lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
460 return(EBUSY);
461 } else
462 sc->sc_state |= LPTINIT;
464 sc->sc_flags = LPTFLAGS(minor(dev));
466 /* Check for open with BYPASS flag set. */
467 if (sc->sc_flags & LP_BYPASS) {
468 sc->sc_state = OPEN;
469 return(0);
472 /* request the ppbus only if we don't have it already */
473 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
474 /* give it a chance to try later */
475 sc->sc_state = 0;
476 return (err);
479 crit_enter();
480 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
482 /* set IRQ status according to ENABLE_IRQ flag
484 if (sc->sc_irq & LP_ENABLE_IRQ)
485 sc->sc_irq |= LP_USE_IRQ;
486 else
487 sc->sc_irq &= ~LP_USE_IRQ;
489 /* init printer */
490 if ((sc->sc_flags & LP_NO_PRIME) == 0) {
491 if ((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
492 ppb_wctr(ppbus, 0);
493 sc->sc_primed++;
494 DELAY(500);
498 ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
500 /* wait till ready (printer running diagnostics) */
501 trys = 0;
502 do {
503 /* ran out of waiting for the printer */
504 if (trys++ >= LPINITRDY*4) {
505 crit_exit();
506 sc->sc_state = 0;
507 lprintf(("status %x\n", ppb_rstr(ppbus)));
509 lpt_release_ppbus(lptdev);
510 return (EBUSY);
513 /* wait 1/4 second, give up if we get a signal */
514 if (tsleep((caddr_t)lptdev, PCATCH, "lptinit", hz/4) !=
515 EWOULDBLOCK) {
516 sc->sc_state = 0;
517 crit_exit();
519 lpt_release_ppbus(lptdev);
520 return (EBUSY);
523 /* is printer online and ready for output */
524 } while ((ppb_rstr(ppbus) &
525 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
526 (LPS_SEL|LPS_NBSY|LPS_NERR));
528 sc->sc_control = LPC_SEL|LPC_NINIT;
529 if (sc->sc_flags & LP_AUTOLF)
530 sc->sc_control |= LPC_AUTOL;
532 /* enable interrupt if interrupt-driven */
533 if (sc->sc_irq & LP_USE_IRQ)
534 sc->sc_control |= LPC_ENA;
536 ppb_wctr(ppbus, sc->sc_control);
538 sc->sc_state = OPEN;
539 sc->sc_inbuf = kmalloc(BUFSIZE, M_LPT, M_WAITOK);
540 sc->sc_statbuf = kmalloc(BUFSTATSIZE, M_LPT, M_WAITOK);
541 sc->sc_xfercnt = 0;
543 crit_exit();
545 /* release the ppbus */
546 lpt_release_ppbus(lptdev);
548 /* only use timeout if using interrupt */
549 lprintf(("irq %x\n", sc->sc_irq));
550 if (sc->sc_irq & LP_USE_IRQ) {
551 sc->sc_state |= TOUT;
552 sc->sc_backoff = hz / LPTOUTINITIAL;
553 callout_reset(&sc->sc_callout, sc->sc_backoff, lptout, lptdev);
556 lprintf(("opened.\n"));
557 return(0);
561 * lptclose -- close the device, free the local line buffer.
563 * Check for interrupted write call added.
566 static int
567 lptclose(struct dev_close_args *ap)
569 cdev_t dev = ap->a_head.a_dev;
570 u_int unit = LPTUNIT(minor(dev));
571 struct lpt_data *sc = UNITOSOFTC(unit);
572 device_t lptdev = UNITODEVICE(unit);
573 device_t ppbus = device_get_parent(lptdev);
574 int err;
576 if(sc->sc_flags & LP_BYPASS)
577 goto end_close;
579 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
580 return (err);
582 sc->sc_state &= ~OPEN;
584 /* if the last write was interrupted, don't complete it */
585 if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ)) {
586 while ((ppb_rstr(ppbus) &
587 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
588 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) {
589 /* wait 1/4 second, give up if we get a signal */
590 if (tsleep((caddr_t)lptdev, PCATCH,
591 "lpclose", hz) != EWOULDBLOCK) {
592 break;
596 callout_stop(&sc->sc_callout);
597 ppb_wctr(ppbus, LPC_NINIT);
598 kfree(sc->sc_inbuf, M_LPT);
599 kfree(sc->sc_statbuf, M_LPT);
601 end_close:
602 /* release the bus anyway
603 * unregistration of interrupt forced by release
605 lpt_release_ppbus(lptdev);
607 sc->sc_state = 0;
608 sc->sc_xfercnt = 0;
609 lprintf(("closed.\n"));
610 return(0);
614 * lpt_pushbytes()
615 * Workhorse for actually spinning and writing bytes to printer
616 * Derived from lpa.c
617 * Originally by ?
619 * This code is only used when we are polling the port
621 static int
622 lpt_pushbytes(device_t dev)
624 struct lpt_data *sc = DEVTOSOFTC(dev);
625 device_t ppbus = device_get_parent(dev);
626 int spin, err, tic;
627 char ch;
629 lprintf(("p"));
630 /* loop for every character .. */
631 while (sc->sc_xfercnt > 0) {
632 /* printer data */
633 ch = *(sc->sc_cp);
634 sc->sc_cp++;
635 sc->sc_xfercnt--;
638 * Wait for printer ready.
639 * Loop 20 usecs testing BUSY bit, then sleep
640 * for exponentially increasing timeout. (vak)
642 for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
643 DELAY(1); /* XXX delay is NOT this accurate! */
644 if (spin >= MAX_SPIN) {
645 tic = 0;
646 while (NOT_READY(ppbus)) {
648 * Now sleep, every cycle a
649 * little longer ..
651 tic = tic + tic + 1;
653 * But no more than 10 seconds. (vak)
655 if (tic > MAX_SLEEP)
656 tic = MAX_SLEEP;
657 err = tsleep((caddr_t)dev, 0,
658 LPT_NAME "poll", tic);
659 if (err != EWOULDBLOCK) {
660 return (err);
665 /* output data */
666 ppb_wdtr(ppbus, ch);
667 /* strobe */
668 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
669 ppb_wctr(ppbus, sc->sc_control);
672 return(0);
676 * lptread --retrieve printer status in IEEE1284 NIBBLE mode
679 static int
680 lptread(struct dev_read_args *ap)
682 cdev_t dev = ap->a_head.a_dev;
683 struct uio *uio = ap->a_uio;
684 u_int unit = LPTUNIT(minor(dev));
685 struct lpt_data *sc = UNITOSOFTC(unit);
686 device_t lptdev = UNITODEVICE(unit);
687 device_t ppbus = device_get_parent(lptdev);
688 int error = 0, len;
690 if (sc->sc_flags & LP_BYPASS) {
691 /* we can't do reads in bypass mode */
692 return (EPERM);
695 if ((error = ppb_1284_negotiate(ppbus, PPB_NIBBLE, 0)))
696 return (error);
698 /* read data in an other buffer, read/write may be simultaneous */
699 len = 0;
700 while (uio->uio_resid) {
701 error = ppb_1284_read(ppbus, PPB_NIBBLE, sc->sc_statbuf,
702 (int)szmin(BUFSTATSIZE, uio->uio_resid),
703 &len);
704 if (error)
705 goto error;
707 if (!len)
708 goto error; /* no more data */
710 if ((error = uiomove(sc->sc_statbuf, (size_t)len, uio)))
711 goto error;
714 error:
715 ppb_1284_terminate(ppbus);
716 return (error);
720 * lptwrite --copy a line from user space to a local buffer, then call
721 * putc to get the chars moved to the output queue.
723 * Flagging of interrupted write added.
726 static int
727 lptwrite(struct dev_write_args *ap)
729 cdev_t dev = ap->a_head.a_dev;
730 struct uio *uio = ap->a_uio;
731 unsigned n;
732 int err;
733 u_int unit = LPTUNIT(minor(dev));
734 struct lpt_data *sc = UNITOSOFTC(unit);
735 device_t lptdev = UNITODEVICE(unit);
736 device_t ppbus = device_get_parent(lptdev);
738 if (sc->sc_flags & LP_BYPASS) {
739 /* we can't do writes in bypass mode */
740 return (EPERM);
743 /* request the ppbus only if we don't have it already */
744 /* XXX interrupt registration?! */
745 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
746 return (err);
748 /* if interrupts are working, register the handler */
749 if (sc->sc_irq & LP_USE_IRQ) {
750 /* register our interrupt handler */
751 err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource,
752 0, lpt_intr, lptdev,
753 &sc->intr_cookie, NULL, NULL);
754 if (err) {
755 device_printf(lptdev, "handler registration failed, polled mode.\n");
756 sc->sc_irq &= ~LP_USE_IRQ;
760 sc->sc_state &= ~INTERRUPTED;
761 while ((n = (unsigned)szmin(BUFSIZE, uio->uio_resid)) != 0) {
762 sc->sc_cp = sc->sc_inbuf;
763 uiomove(sc->sc_cp, (size_t)n, uio);
764 sc->sc_xfercnt = n;
766 if (sc->sc_irq & LP_ENABLE_EXT) {
767 /* try any extended mode */
768 err = ppb_write(ppbus, sc->sc_cp,
769 sc->sc_xfercnt, 0);
770 switch (err) {
771 case 0:
772 /* if not all data was sent, we could rely
773 * on polling for the last bytes */
774 sc->sc_xfercnt = 0;
775 break;
776 case EINTR:
777 sc->sc_state |= INTERRUPTED;
778 return (err);
779 case EINVAL:
780 /* advanced mode not avail */
781 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
782 break;
783 default:
784 return (err);
786 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
787 lprintf(("i"));
788 /* if the printer is ready for a char, */
789 /* give it one */
790 if ((sc->sc_state & OBUSY) == 0){
791 lprintf(("\nC %d. ", sc->sc_xfercnt));
792 lptintr(lptdev);
794 lprintf(("W "));
795 if (sc->sc_state & OBUSY)
796 if ((err = tsleep((caddr_t)lptdev,
797 PCATCH, LPT_NAME "write", 0))) {
798 sc->sc_state |= INTERRUPTED;
799 return(err);
803 /* check to see if we must do a polled write */
804 if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
805 lprintf(("p"));
807 err = lpt_pushbytes(lptdev);
809 if (err)
810 return (err);
814 /* we have not been interrupted, release the ppbus */
815 lpt_release_ppbus(lptdev);
817 return (0);
821 * lpt_intr -- handle printer interrupts which occur when the printer is
822 * ready to accept another char.
824 * do checking for interrupted write call.
827 static void
828 lpt_intr(void *arg)
830 device_t lptdev = (device_t)arg;
831 device_t ppbus = device_get_parent(lptdev);
832 struct lpt_data *sc = DEVTOSOFTC(lptdev);
833 int sts = 0;
834 int i;
836 /* we must own the bus to use it */
837 if ((sc->sc_state & HAVEBUS) == 0)
838 return;
841 * Is printer online and ready for output?
843 * Avoid falling back to lptout() too quickly. First spin-loop
844 * to see if the printer will become ready ``really soon now''.
846 for (i = 0; i < 100 &&
847 ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
849 if ((sts & RDY_MASK) == LP_READY) {
850 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
851 sc->sc_backoff = hz / LPTOUTINITIAL;
853 if (sc->sc_xfercnt) {
854 /* send char */
855 /*lprintf(("%x ", *sc->sc_cp)); */
856 ppb_wdtr(ppbus, *sc->sc_cp++) ;
857 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
858 /* DELAY(X) */
859 ppb_wctr(ppbus, sc->sc_control);
861 /* any more data for printer */
862 if (--(sc->sc_xfercnt) > 0)
863 return;
867 * No more data waiting for printer.
868 * Wakeup is not done if write call was not interrupted.
870 sc->sc_state &= ~OBUSY;
872 if (!(sc->sc_state & INTERRUPTED))
873 wakeup((caddr_t)lptdev);
874 lprintf(("w "));
875 return;
876 } else { /* check for error */
877 if (((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
878 (sc->sc_state & OPEN))
879 sc->sc_state |= EERROR;
880 /* lptout() will jump in and try to restart. */
882 lprintf(("sts %x ", sts));
885 static void
886 lptintr(device_t dev)
888 /* call the interrupt at required spl level */
889 crit_enter();
891 lpt_intr(dev);
893 crit_exit();
896 static int
897 lptioctl(struct dev_ioctl_args *ap)
899 cdev_t dev = ap->a_head.a_dev;
900 int error = 0;
901 u_int unit = LPTUNIT(minor(dev));
902 struct lpt_data *sc = UNITOSOFTC(unit);
903 u_char old_sc_irq; /* old printer IRQ status */
905 switch (ap->a_cmd) {
906 case LPT_IRQ :
907 if (sc->sc_irq & LP_HAS_IRQ) {
909 * NOTE:
910 * If the IRQ status is changed,
911 * this will only be visible on the
912 * next open.
914 * If interrupt status changes,
915 * this gets syslog'd.
917 old_sc_irq = sc->sc_irq;
918 switch (*(int*)ap->a_data) {
919 case 0:
920 sc->sc_irq &= (~LP_ENABLE_IRQ);
921 break;
922 case 1:
923 sc->sc_irq &= (~LP_ENABLE_EXT);
924 sc->sc_irq |= LP_ENABLE_IRQ;
925 break;
926 case 2:
927 /* classic irq based transfer and advanced
928 * modes are in conflict
930 sc->sc_irq &= (~LP_ENABLE_IRQ);
931 sc->sc_irq |= LP_ENABLE_EXT;
932 break;
933 case 3:
934 sc->sc_irq &= (~LP_ENABLE_EXT);
935 break;
936 default:
937 break;
940 if (old_sc_irq != sc->sc_irq )
941 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
942 unit,
943 (sc->sc_irq & LP_ENABLE_IRQ)?
944 "interrupt-driven":"polled",
945 (sc->sc_irq & LP_ENABLE_EXT)?
946 "extended":"standard");
947 } else /* polled port */
948 error = EOPNOTSUPP;
949 break;
950 default:
951 error = ENODEV;
954 return(error);
958 * Because lpt is a static device that always exists under a ppbus device,
959 * and not scanned by the ppbus device, we need an identify function to
960 * install its device.
962 static device_method_t lpt_methods[] = {
963 /* device interface */
964 DEVMETHOD(device_identify, bus_generic_identify),
965 DEVMETHOD(device_probe, lpt_probe),
966 DEVMETHOD(device_attach, lpt_attach),
968 DEVMETHOD_END
971 static driver_t lpt_driver = {
972 LPT_NAME,
973 lpt_methods,
974 sizeof(struct lpt_data),
977 DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, NULL, NULL);