2 * Copyright (c) 1990 William F. Jolitz, TeleMuse
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
55 * Device Driver for AT parallel printer port
56 * Written by William Jolitz 12/18/90
60 * Updated for ppbus by Nicolas Souchu
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/module.h>
71 #include <sys/kernel.h>
73 #include <sys/syslog.h>
74 #include <sys/malloc.h>
76 #include <machine/bus.h>
77 #include <machine/resource.h>
80 #include <dev/ppbus/lptio.h>
81 #include <dev/ppbus/ppbconf.h>
82 #include <dev/ppbus/ppb_1284.h>
83 #include <dev/ppbus/lpt.h>
85 #include <dev/ppbus/ppbio.h>
90 #define lprintf(args) \
95 static int volatile lptflag = 1;
98 #define LPINITRDY 4 /* wait up to 4 seconds for a ready */
99 #define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */
100 #define LPTOUTMAX 1 /* maximal timeout 1 s */
101 #define LPPRI (PZERO+8)
103 #define BUFSTATSIZE 32
105 #define LPTUNIT(s) ((s)&0x03)
106 #define LPTFLAGS(s) ((s)&0xfc)
111 /* default case: negative prime, negative ack, handshake strobe,
115 #define LP_POS_INIT 0x04 /* if we are a postive init signal */
116 #define LP_POS_ACK 0x08 /* if we are a positive going ack */
117 #define LP_NO_PRIME 0x10 /* don't prime the printer at all */
118 #define LP_PRIMEOPEN 0x20 /* prime on every open */
119 #define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */
120 #define LP_BYPASS 0x80 /* bypass printer ready checks */
126 u_short sc_irq ; /* IRQ status of port */
127 #define LP_HAS_IRQ 0x01 /* we have an irq available */
128 #define LP_USE_IRQ 0x02 /* we are using our irq */
129 #define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */
130 #define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */
131 u_char sc_backoff ; /* time to call lptout() again */
133 struct resource *intr_resource; /* interrupt resource */
134 void *intr_cookie; /* interrupt registration cookie */
138 #define LPT_NAME "lpt" /* our official name */
140 static timeout_t lptout;
141 static int lpt_port_test(device_t dev, u_char data, u_char mask);
142 static int lpt_detect(device_t dev);
144 #define DEVTOSOFTC(dev) \
145 ((struct lpt_data *)device_get_softc(dev))
146 #define UNITOSOFTC(unit) \
147 ((struct lpt_data *)devclass_get_softc(lpt_devclass, (unit)))
148 #define UNITODEVICE(unit) \
149 (devclass_get_device(lpt_devclass, (unit)))
151 static void lptintr(device_t dev);
152 static void lpt_intr(void *arg); /* without spls */
154 static devclass_t lpt_devclass;
158 #define OPEN (1<<0) /* device is open */
159 #define ASLP (1<<1) /* awaiting draining of printer */
160 #define EERROR (1<<2) /* error was received from printer */
161 #define OBUSY (1<<3) /* printer is busy doing output */
162 #define LPTOUT (1<<4) /* timeout while not selected */
163 #define TOUT (1<<5) /* timeout while not selected */
164 #define LPTINIT (1<<6) /* waiting to initialize for open */
165 #define INTERRUPTED (1<<7) /* write call was interrupted */
167 #define HAVEBUS (1<<8) /* the driver owns the bus */
170 /* status masks to interrogate printer status */
171 #define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
172 #define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
174 /* Printer Ready condition - from lpa.c */
175 /* Only used in polling code */
176 #define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
177 #define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
178 #define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
180 #define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
181 #define MAX_SPIN 20 /* Max delay for device ready in usecs */
184 static d_open_t lptopen;
185 static d_close_t lptclose;
186 static d_write_t lptwrite;
187 static d_read_t lptread;
188 static d_ioctl_t lptioctl;
190 #define CDEV_MAJOR 16
191 static struct cdevsw lpt_cdevsw = {
193 /* close */ lptclose,
195 /* write */ lptwrite,
196 /* ioctl */ lptioctl,
199 /* strategy */ nostrategy,
201 /* maj */ CDEV_MAJOR,
208 lpt_request_ppbus(device_t dev, int how)
210 device_t ppbus = device_get_parent(dev);
211 struct lpt_data *sc = DEVTOSOFTC(dev);
214 if (sc->sc_state & HAVEBUS)
217 /* we have the bus only if the request succeded */
218 if ((error = ppb_request_bus(ppbus, dev, how)) == 0)
219 sc->sc_state |= HAVEBUS;
225 lpt_release_ppbus(device_t dev)
227 device_t ppbus = device_get_parent(dev);
228 struct lpt_data *sc = DEVTOSOFTC(dev);
231 if ((error = ppb_release_bus(ppbus, dev)) == 0)
232 sc->sc_state &= ~HAVEBUS;
238 * Internal routine to lptprobe to do port tests of one byte value
241 lpt_port_test(device_t ppbus, u_char data, u_char mask)
246 ppb_wdtr(ppbus, data);
250 temp = ppb_rdtr(ppbus) & mask;
252 while (temp != data && --timeout);
253 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
254 return (temp == data);
258 * Probe simplified by replacing multiple loops with a hardcoded
259 * test pattern - 1999/02/08 des@freebsd.org
261 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
262 * Based partially on Rod Grimes' printer probe
265 * 1) If no port address was given, use the bios detected ports
266 * and autodetect what ports the printers are on.
267 * 2) Otherwise, probe the data port at the address given,
268 * using the method in Rod Grimes' port probe.
269 * (Much code ripped off directly from Rod's probe.)
271 * Comments from Rod's probe:
273 * 1) You should be able to write to and read back the same value
274 * to the data port. Do an alternating zeros, alternating ones,
275 * walking zero, and walking one test to check for stuck bits.
277 * 2) You should be able to write to and read back the same value
278 * to the control port lower 5 bits, the upper 3 bits are reserved
279 * per the IBM PC technical reference manauls and different boards
280 * do different things with them. Do an alternating zeros, alternating
281 * ones, walking zero, and walking one test to check for stuck bits.
283 * Some printers drag the strobe line down when the are powered off
284 * so this bit has been masked out of the control port test.
286 * XXX Some printers may not like a fast pulse on init or strobe, I
287 * don't know at this point, if that becomes a problem these bits
288 * should be turned off in the mask byte for the control port test.
290 * We are finally left with a mask of 0x14, due to some printers
291 * being adamant about holding other bits high ........
293 * Before probing the control port, we write a 0 to the data port -
294 * If not, some printers chuck out garbage when the strobe line
297 * 3) Set the data and control ports to a value of 0
299 * This probe routine has been tested on Epson Lx-800, HP LJ3P,
300 * Epson FX-1170 and C.Itoh 8510RM
302 * Quick exit on fail added.
305 lpt_detect(device_t dev)
307 device_t ppbus = device_get_parent(dev);
309 static u_char testbyte[18] = {
310 0x55, /* alternating zeros */
311 0xaa, /* alternating ones */
312 0xfe, 0xfd, 0xfb, 0xf7,
313 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
314 0x01, 0x02, 0x04, 0x08,
315 0x10, 0x20, 0x40, 0x80 /* walking one */
317 int i, error, status;
319 status = 1; /* assume success */
321 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
322 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
327 for (i = 0; i < 18 && status; i++)
328 if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
334 /* write 0's to control and data ports */
338 lpt_release_ppbus(dev);
344 lpt_identify(driver_t *driver, device_t parent)
347 BUS_ADD_CHILD(parent, 0, LPT_NAME, -1);
354 lpt_probe(device_t dev)
358 sc = DEVTOSOFTC(dev);
359 bzero(sc, sizeof(struct lpt_data));
362 * Now, try to detect the printer.
364 if (!lpt_detect(dev))
367 device_set_desc(dev, "Printer");
373 lpt_attach(device_t dev)
375 device_t ppbus = device_get_parent(dev);
376 struct lpt_data *sc = DEVTOSOFTC(dev);
377 int zero = 0, unit = device_get_unit(dev);
381 sc->sc_primed = 0; /* not primed yet */
383 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
384 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
388 ppb_wctr(ppbus, LPC_NINIT);
390 /* check if we can use interrupt, should be done by ppc stuff */
391 lprintf(("oldirq %x\n", sc->sc_irq));
393 /* retrieve the ppbus irq */
394 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
397 /* declare our interrupt handler */
398 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
399 &zero, irq, irq, 1, RF_SHAREABLE);
401 if (sc->intr_resource) {
402 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
403 device_printf(dev, "Interrupt-driven port\n");
406 device_printf(dev, "Polled port\n");
408 lprintf(("irq %x %x\n", irq, sc->sc_irq));
410 lpt_release_ppbus(dev);
412 make_dev(&lpt_cdevsw, unit,
413 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
414 make_dev(&lpt_cdevsw, unit | LP_BYPASS,
415 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
422 device_t dev = (device_t)arg;
423 struct lpt_data *sc = DEVTOSOFTC(dev);
425 device_t ppbus = device_get_parent(dev);
428 lprintf(("T %x ", ppb_rstr(ppbus)));
429 if (sc->sc_state & OPEN) {
431 if (sc->sc_backoff > hz/LPTOUTMAX)
432 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
433 timeout(lptout, (caddr_t)dev, sc->sc_backoff);
435 sc->sc_state &= ~TOUT;
437 if (sc->sc_state & EERROR)
438 sc->sc_state &= ~EERROR;
441 * Avoid possible hangs due to missed interrupts
443 if (sc->sc_xfercnt) {
446 sc->sc_state &= ~OBUSY;
447 wakeup((caddr_t)dev);
452 * lptopen -- reset the printer, then wait until it's selected and not busy.
453 * If LP_BYPASS flag is selected, then we do not try to select the
454 * printer -- this is just used for passing ioctls.
458 lptopen(dev_t dev, int flags, int fmt, struct thread *td)
462 u_int unit = LPTUNIT(minor(dev));
463 struct lpt_data *sc = UNITOSOFTC(unit);
464 device_t lptdev = UNITODEVICE(unit);
465 device_t ppbus = device_get_parent(lptdev);
471 lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
474 sc->sc_state |= LPTINIT;
476 sc->sc_flags = LPTFLAGS(minor(dev));
478 /* Check for open with BYPASS flag set. */
479 if (sc->sc_flags & LP_BYPASS) {
484 /* request the ppbus only if we don't have it already */
485 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
486 /* give it a chance to try later */
492 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
494 /* set IRQ status according to ENABLE_IRQ flag
496 if (sc->sc_irq & LP_ENABLE_IRQ)
497 sc->sc_irq |= LP_USE_IRQ;
499 sc->sc_irq &= ~LP_USE_IRQ;
502 if ((sc->sc_flags & LP_NO_PRIME) == 0) {
503 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
510 ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
512 /* wait till ready (printer running diagnostics) */
515 /* ran out of waiting for the printer */
516 if (trys++ >= LPINITRDY*4) {
519 lprintf(("status %x\n", ppb_rstr(ppbus)));
521 lpt_release_ppbus(lptdev);
525 /* wait 1/4 second, give up if we get a signal */
526 if (tsleep((caddr_t)lptdev, LPPRI|PCATCH, "lptinit", hz/4) !=
531 lpt_release_ppbus(lptdev);
535 /* is printer online and ready for output */
536 } while ((ppb_rstr(ppbus) &
537 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
538 (LPS_SEL|LPS_NBSY|LPS_NERR));
540 sc->sc_control = LPC_SEL|LPC_NINIT;
541 if (sc->sc_flags & LP_AUTOLF)
542 sc->sc_control |= LPC_AUTOL;
544 /* enable interrupt if interrupt-driven */
545 if (sc->sc_irq & LP_USE_IRQ)
546 sc->sc_control |= LPC_ENA;
548 ppb_wctr(ppbus, sc->sc_control);
551 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
552 sc->sc_statbuf = malloc(BUFSTATSIZE, M_DEVBUF, M_WAITOK);
556 /* release the ppbus */
557 lpt_release_ppbus(lptdev);
559 /* only use timeout if using interrupt */
560 lprintf(("irq %x\n", sc->sc_irq));
561 if (sc->sc_irq & LP_USE_IRQ) {
562 sc->sc_state |= TOUT;
563 timeout(lptout, (caddr_t)lptdev,
564 (sc->sc_backoff = hz/LPTOUTINITIAL));
567 lprintf(("opened.\n"));
572 * lptclose -- close the device, free the local line buffer.
574 * Check for interrupted write call added.
578 lptclose(dev_t dev, int flags, int fmt, struct thread *td)
580 u_int unit = LPTUNIT(minor(dev));
581 struct lpt_data *sc = UNITOSOFTC(unit);
582 device_t lptdev = UNITODEVICE(unit);
583 device_t ppbus = device_get_parent(lptdev);
586 if(sc->sc_flags & LP_BYPASS)
589 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
592 sc->sc_state &= ~OPEN;
594 /* if the last write was interrupted, don't complete it */
595 if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
596 while ((ppb_rstr(ppbus) &
597 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
598 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
599 /* wait 1/4 second, give up if we get a signal */
600 if (tsleep((caddr_t)lptdev, LPPRI|PCATCH,
601 "lpclose", hz) != EWOULDBLOCK)
604 ppb_wctr(ppbus, LPC_NINIT);
605 free(sc->sc_inbuf, M_DEVBUF);
606 free(sc->sc_statbuf, M_DEVBUF);
609 /* release the bus anyway
610 * unregistration of interrupt forced by release
612 lpt_release_ppbus(lptdev);
616 lprintf(("closed.\n"));
622 * Workhorse for actually spinning and writing bytes to printer
626 * This code is only used when we are polling the port
629 lpt_pushbytes(device_t dev)
631 struct lpt_data *sc = DEVTOSOFTC(dev);
632 device_t ppbus = device_get_parent(dev);
637 /* loop for every character .. */
638 while (sc->sc_xfercnt > 0) {
645 * Wait for printer ready.
646 * Loop 20 usecs testing BUSY bit, then sleep
647 * for exponentially increasing timeout. (vak)
649 for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
650 DELAY(1); /* XXX delay is NOT this accurate! */
651 if (spin >= MAX_SPIN) {
653 while (NOT_READY(ppbus)) {
655 * Now sleep, every cycle a
660 * But no more than 10 seconds. (vak)
664 err = tsleep((caddr_t)dev, LPPRI,
665 LPT_NAME "poll", tic);
666 if (err != EWOULDBLOCK) {
675 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
676 ppb_wctr(ppbus, sc->sc_control);
683 * lptread --retrieve printer status in IEEE1284 NIBBLE mode
687 lptread(dev_t dev, struct uio *uio, int ioflag)
689 u_int unit = LPTUNIT(minor(dev));
690 struct lpt_data *sc = UNITOSOFTC(unit);
691 device_t lptdev = UNITODEVICE(unit);
692 device_t ppbus = device_get_parent(lptdev);
695 if (sc->sc_flags & LP_BYPASS) {
696 /* we can't do reads in bypass mode */
700 if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0)))
703 /* read data in an other buffer, read/write may be simultaneous */
705 while (uio->uio_resid) {
706 if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
707 sc->sc_statbuf, min(BUFSTATSIZE,
708 uio->uio_resid), &len))) {
713 goto error; /* no more data */
715 if ((error = uiomove(sc->sc_statbuf, len, uio)))
720 ppb_1284_terminate(ppbus);
725 * lptwrite --copy a line from user space to a local buffer, then call
726 * putc to get the chars moved to the output queue.
728 * Flagging of interrupted write added.
732 lptwrite(dev_t dev, struct uio *uio, int ioflag)
736 u_int unit = LPTUNIT(minor(dev));
737 struct lpt_data *sc = UNITOSOFTC(unit);
738 device_t lptdev = UNITODEVICE(unit);
739 device_t ppbus = device_get_parent(lptdev);
741 if(sc->sc_flags & LP_BYPASS) {
742 /* we can't do writes in bypass mode */
746 /* request the ppbus only if we don't have it already */
747 /* XXX interrupt registration?! */
748 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
751 /* if interrupts are working, register the handler */
752 if (sc->sc_irq & LP_USE_IRQ) {
753 /* register our interrupt handler */
754 err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource,
755 INTR_TYPE_TTY, lpt_intr, lptdev,
758 device_printf(lptdev, "handler registration failed, polled mode.\n");
759 sc->sc_irq &= ~LP_USE_IRQ;
763 sc->sc_state &= ~INTERRUPTED;
764 while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
765 sc->sc_cp = sc->sc_inbuf;
766 uiomove(sc->sc_cp, n, uio);
769 if (sc->sc_irq & LP_ENABLE_EXT) {
770 /* try any extended mode */
771 err = ppb_write(ppbus, sc->sc_cp,
775 /* if not all data was sent, we could rely
776 * on polling for the last bytes */
780 sc->sc_state |= INTERRUPTED;
783 /* advanced mode not avail */
784 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
789 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
791 /* if the printer is ready for a char, */
793 if ((sc->sc_state & OBUSY) == 0){
794 lprintf(("\nC %d. ", sc->sc_xfercnt));
798 if (sc->sc_state & OBUSY)
799 if ((err = tsleep((caddr_t)lptdev,
800 LPPRI|PCATCH, LPT_NAME "write", 0))) {
801 sc->sc_state |= INTERRUPTED;
806 /* check to see if we must do a polled write */
807 if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
810 err = lpt_pushbytes(lptdev);
817 /* we have not been interrupted, release the ppbus */
818 lpt_release_ppbus(lptdev);
824 * lpt_intr -- handle printer interrupts which occur when the printer is
825 * ready to accept another char.
827 * do checking for interrupted write call.
833 device_t lptdev = (device_t)arg;
834 device_t ppbus = device_get_parent(lptdev);
835 struct lpt_data *sc = DEVTOSOFTC(lptdev);
839 /* we must own the bus to use it */
840 if ((sc->sc_state & HAVEBUS) == 0)
844 * Is printer online and ready for output?
846 * Avoid falling back to lptout() too quickly. First spin-loop
847 * to see if the printer will become ready ``really soon now''.
849 for (i = 0; i < 100 &&
850 ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
852 if ((sts & RDY_MASK) == LP_READY) {
853 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
854 sc->sc_backoff = hz/LPTOUTINITIAL;
856 if (sc->sc_xfercnt) {
858 /*lprintf(("%x ", *sc->sc_cp)); */
859 ppb_wdtr(ppbus, *sc->sc_cp++) ;
860 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
862 ppb_wctr(ppbus, sc->sc_control);
864 /* any more data for printer */
865 if(--(sc->sc_xfercnt) > 0) return;
869 * No more data waiting for printer.
870 * Wakeup is not done if write call was not interrupted.
872 sc->sc_state &= ~OBUSY;
874 if(!(sc->sc_state & INTERRUPTED))
875 wakeup((caddr_t)lptdev);
878 } else { /* check for error */
879 if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
880 (sc->sc_state & OPEN))
881 sc->sc_state |= EERROR;
882 /* lptout() will jump in and try to restart. */
884 lprintf(("sts %x ", sts));
888 lptintr(device_t dev)
890 /* call the interrupt at required spl level */
900 lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
903 u_int unit = LPTUNIT(minor(dev));
904 struct lpt_data *sc = UNITOSOFTC(unit);
905 u_char old_sc_irq; /* old printer IRQ status */
909 if(sc->sc_irq & LP_HAS_IRQ) {
912 * If the IRQ status is changed,
913 * this will only be visible on the
916 * If interrupt status changes,
917 * this gets syslog'd.
919 old_sc_irq = sc->sc_irq;
920 switch(*(int*)data) {
922 sc->sc_irq &= (~LP_ENABLE_IRQ);
925 sc->sc_irq &= (~LP_ENABLE_EXT);
926 sc->sc_irq |= LP_ENABLE_IRQ;
929 /* classic irq based transfer and advanced
930 * modes are in conflict
932 sc->sc_irq &= (~LP_ENABLE_IRQ);
933 sc->sc_irq |= LP_ENABLE_EXT;
936 sc->sc_irq &= (~LP_ENABLE_EXT);
942 if (old_sc_irq != sc->sc_irq )
943 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
945 (sc->sc_irq & LP_ENABLE_IRQ)?
946 "interrupt-driven":"polled",
947 (sc->sc_irq & LP_ENABLE_EXT)?
948 "extended":"standard");
949 } else /* polled port */
959 static device_method_t lpt_methods[] = {
960 /* device interface */
961 DEVMETHOD(device_identify, lpt_identify),
962 DEVMETHOD(device_probe, lpt_probe),
963 DEVMETHOD(device_attach, lpt_attach),
968 static driver_t lpt_driver = {
971 sizeof(struct lpt_data),
974 DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0);