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
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/module.h>
77 #include <sys/kernel.h>
79 #include <sys/syslog.h>
81 #include <machine/clock.h>
82 #include <machine/bus.h>
83 #include <machine/resource.h>
86 #include <machine/lpt.h>
89 #include <dev/ppbus/ppbconf.h>
90 #include <dev/ppbus/ppb_1284.h>
91 #include <dev/ppbus/lpt.h>
93 #include <dev/ppbus/ppbio.h>
98 #define lprintf(args) \
103 static int volatile lptflag = 1;
106 #define LPINITRDY 4 /* wait up to 4 seconds for a ready */
107 #define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */
108 #define LPTOUTMAX 1 /* maximal timeout 1 s */
109 #define LPPRI (PZERO+8)
111 #define BUFSTATSIZE 32
113 #define LPTUNIT(s) ((s)&0x03)
114 #define LPTFLAGS(s) ((s)&0xfc)
119 /* default case: negative prime, negative ack, handshake strobe,
123 #define LP_POS_INIT 0x04 /* if we are a postive init signal */
124 #define LP_POS_ACK 0x08 /* if we are a positive going ack */
125 #define LP_NO_PRIME 0x10 /* don't prime the printer at all */
126 #define LP_PRIMEOPEN 0x20 /* prime on every open */
127 #define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */
128 #define LP_BYPASS 0x80 /* bypass printer ready checks */
129 struct buf *sc_inbuf;
130 struct buf *sc_statbuf;
134 u_short sc_irq ; /* IRQ status of port */
135 #define LP_HAS_IRQ 0x01 /* we have an irq available */
136 #define LP_USE_IRQ 0x02 /* we are using our irq */
137 #define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */
138 #define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */
139 u_char sc_backoff ; /* time to call lptout() again */
141 struct resource *intr_resource; /* interrupt resource */
142 void *intr_cookie; /* interrupt registration cookie */
146 #define LPT_NAME "lpt" /* our official name */
148 static timeout_t lptout;
149 static int lpt_port_test(device_t dev, u_char data, u_char mask);
150 static int lpt_detect(device_t dev);
152 #define DEVTOSOFTC(dev) \
153 ((struct lpt_data *)device_get_softc(dev))
154 #define UNITOSOFTC(unit) \
155 ((struct lpt_data *)devclass_get_softc(lpt_devclass, (unit)))
156 #define UNITODEVICE(unit) \
157 (devclass_get_device(lpt_devclass, (unit)))
159 static int lpt_probe(device_t dev);
160 static int lpt_attach(device_t dev);
162 static void lptintr(device_t dev);
163 static void lpt_intr(void *arg); /* without spls */
165 static devclass_t lpt_devclass;
167 static device_method_t lpt_methods[] = {
168 /* device interface */
169 DEVMETHOD(device_probe, lpt_probe),
170 DEVMETHOD(device_attach, lpt_attach),
175 static driver_t lpt_driver = {
178 sizeof(struct lpt_data),
182 #define OPEN (1<<0) /* device is open */
183 #define ASLP (1<<1) /* awaiting draining of printer */
184 #define EERROR (1<<2) /* error was received from printer */
185 #define OBUSY (1<<3) /* printer is busy doing output */
186 #define LPTOUT (1<<4) /* timeout while not selected */
187 #define TOUT (1<<5) /* timeout while not selected */
188 #define LPTINIT (1<<6) /* waiting to initialize for open */
189 #define INTERRUPTED (1<<7) /* write call was interrupted */
191 #define HAVEBUS (1<<8) /* the driver owns the bus */
194 /* status masks to interrogate printer status */
195 #define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
196 #define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
198 /* Printer Ready condition - from lpa.c */
199 /* Only used in polling code */
200 #define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
201 #define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
202 #define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
204 #define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
205 #define MAX_SPIN 20 /* Max delay for device ready in usecs */
208 static d_open_t lptopen;
209 static d_close_t lptclose;
210 static d_write_t lptwrite;
211 static d_read_t lptread;
212 static d_ioctl_t lptioctl;
214 #define CDEV_MAJOR 16
215 static struct cdevsw lpt_cdevsw = {
217 /* close */ lptclose,
219 /* write */ lptwrite,
220 /* ioctl */ lptioctl,
223 /* strategy */ nostrategy,
225 /* maj */ CDEV_MAJOR,
233 lpt_request_ppbus(device_t dev, int how)
235 device_t ppbus = device_get_parent(dev);
236 struct lpt_data *sc = DEVTOSOFTC(dev);
239 if (sc->sc_state & HAVEBUS)
242 /* we have the bus only if the request succeded */
243 if ((error = ppb_request_bus(ppbus, dev, how)) == 0)
244 sc->sc_state |= HAVEBUS;
250 lpt_release_ppbus(device_t dev)
252 device_t ppbus = device_get_parent(dev);
253 struct lpt_data *sc = DEVTOSOFTC(dev);
256 if ((error = ppb_release_bus(ppbus, dev)) == 0)
257 sc->sc_state &= ~HAVEBUS;
263 * Internal routine to lptprobe to do port tests of one byte value
266 lpt_port_test(device_t ppbus, u_char data, u_char mask)
271 ppb_wdtr(ppbus, data);
275 temp = ppb_rdtr(ppbus) & mask;
277 while (temp != data && --timeout);
278 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
279 return (temp == data);
283 * Probe simplified by replacing multiple loops with a hardcoded
284 * test pattern - 1999/02/08 des@freebsd.org
286 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
287 * Based partially on Rod Grimes' printer probe
290 * 1) If no port address was given, use the bios detected ports
291 * and autodetect what ports the printers are on.
292 * 2) Otherwise, probe the data port at the address given,
293 * using the method in Rod Grimes' port probe.
294 * (Much code ripped off directly from Rod's probe.)
296 * Comments from Rod's probe:
298 * 1) You should be able to write to and read back the same value
299 * to the data port. Do an alternating zeros, alternating ones,
300 * walking zero, and walking one test to check for stuck bits.
302 * 2) You should be able to write to and read back the same value
303 * to the control port lower 5 bits, the upper 3 bits are reserved
304 * per the IBM PC technical reference manauls and different boards
305 * do different things with them. Do an alternating zeros, alternating
306 * ones, walking zero, and walking one test to check for stuck bits.
308 * Some printers drag the strobe line down when the are powered off
309 * so this bit has been masked out of the control port test.
311 * XXX Some printers may not like a fast pulse on init or strobe, I
312 * don't know at this point, if that becomes a problem these bits
313 * should be turned off in the mask byte for the control port test.
315 * We are finally left with a mask of 0x14, due to some printers
316 * being adamant about holding other bits high ........
318 * Before probing the control port, we write a 0 to the data port -
319 * If not, some printers chuck out garbage when the strobe line
322 * 3) Set the data and control ports to a value of 0
324 * This probe routine has been tested on Epson Lx-800, HP LJ3P,
325 * Epson FX-1170 and C.Itoh 8510RM
327 * Quick exit on fail added.
330 lpt_detect(device_t dev)
332 device_t ppbus = device_get_parent(dev);
334 static u_char testbyte[18] = {
335 0x55, /* alternating zeros */
336 0xaa, /* alternating ones */
337 0xfe, 0xfd, 0xfb, 0xf7,
338 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
339 0x01, 0x02, 0x04, 0x08,
340 0x10, 0x20, 0x40, 0x80 /* walking one */
342 int i, error, status;
344 status = 1; /* assume success */
346 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
347 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
352 for (i = 0; i < 18 && status; i++)
353 if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
359 /* write 0's to control and data ports */
363 lpt_release_ppbus(dev);
372 lpt_probe(device_t dev)
378 cdevsw_add(&lpt_cdevsw);
380 sc = DEVTOSOFTC(dev);
381 bzero(sc, sizeof(struct lpt_data));
384 * Now, try to detect the printer.
386 if (!lpt_detect(dev))
389 device_set_desc(dev, "Printer");
395 lpt_attach(device_t dev)
397 device_t ppbus = device_get_parent(dev);
398 struct lpt_data *sc = DEVTOSOFTC(dev);
399 int zero = 0, irq, unit = device_get_unit(dev);
402 sc->sc_primed = 0; /* not primed yet */
404 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
405 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
409 ppb_wctr(ppbus, LPC_NINIT);
411 /* check if we can use interrupt, should be done by ppc stuff */
412 lprintf(("oldirq %x\n", sc->sc_irq));
414 /* retrieve the ppbus irq */
415 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq);
418 /* declare our interrupt handler */
419 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
420 &zero, irq, irq, 1, RF_SHAREABLE);
422 if (sc->intr_resource) {
423 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
424 device_printf(dev, "Interrupt-driven port\n");
427 device_printf(dev, "Polled port\n");
429 lprintf(("irq %x %x\n", irq, sc->sc_irq));
431 lpt_release_ppbus(dev);
433 make_dev(&lpt_cdevsw, unit,
434 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
435 make_dev(&lpt_cdevsw, unit | LP_BYPASS,
436 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
443 device_t dev = (device_t)arg;
444 struct lpt_data *sc = DEVTOSOFTC(dev);
446 device_t ppbus = device_get_parent(dev);
449 lprintf(("T %x ", ppb_rstr(ppbus)));
450 if (sc->sc_state & OPEN) {
452 if (sc->sc_backoff > hz/LPTOUTMAX)
453 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
454 timeout(lptout, (caddr_t)dev, sc->sc_backoff);
456 sc->sc_state &= ~TOUT;
458 if (sc->sc_state & EERROR)
459 sc->sc_state &= ~EERROR;
462 * Avoid possible hangs due to missed interrupts
464 if (sc->sc_xfercnt) {
467 sc->sc_state &= ~OBUSY;
468 wakeup((caddr_t)dev);
473 * lptopen -- reset the printer, then wait until it's selected and not busy.
474 * If LP_BYPASS flag is selected, then we do not try to select the
475 * printer -- this is just used for passing ioctls.
479 lptopen(dev_t dev, int flags, int fmt, struct proc *p)
483 u_int unit = LPTUNIT(minor(dev));
484 struct lpt_data *sc = UNITOSOFTC(unit);
485 device_t lptdev = UNITODEVICE(unit);
486 device_t ppbus = device_get_parent(lptdev);
492 lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
495 sc->sc_state |= LPTINIT;
497 sc->sc_flags = LPTFLAGS(minor(dev));
499 /* Check for open with BYPASS flag set. */
500 if (sc->sc_flags & LP_BYPASS) {
505 /* request the ppbus only if we don't have it already */
506 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
507 /* give it a chance to try later */
513 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
515 /* set IRQ status according to ENABLE_IRQ flag
517 if (sc->sc_irq & LP_ENABLE_IRQ)
518 sc->sc_irq |= LP_USE_IRQ;
520 sc->sc_irq &= ~LP_USE_IRQ;
523 if ((sc->sc_flags & LP_NO_PRIME) == 0) {
524 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
531 ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
533 /* wait till ready (printer running diagnostics) */
536 /* ran out of waiting for the printer */
537 if (trys++ >= LPINITRDY*4) {
540 lprintf(("status %x\n", ppb_rstr(ppbus)));
542 lpt_release_ppbus(lptdev);
546 /* wait 1/4 second, give up if we get a signal */
547 if (tsleep((caddr_t)lptdev, LPPRI|PCATCH, "lptinit", hz/4) !=
552 lpt_release_ppbus(lptdev);
556 /* is printer online and ready for output */
557 } while ((ppb_rstr(ppbus) &
558 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
559 (LPS_SEL|LPS_NBSY|LPS_NERR));
561 sc->sc_control = LPC_SEL|LPC_NINIT;
562 if (sc->sc_flags & LP_AUTOLF)
563 sc->sc_control |= LPC_AUTOL;
565 /* enable interrupt if interrupt-driven */
566 if (sc->sc_irq & LP_USE_IRQ)
567 sc->sc_control |= LPC_ENA;
569 ppb_wctr(ppbus, sc->sc_control);
572 sc->sc_inbuf = geteblk(BUFSIZE);
573 sc->sc_statbuf = geteblk(BUFSTATSIZE);
577 /* release the ppbus */
578 lpt_release_ppbus(lptdev);
580 /* only use timeout if using interrupt */
581 lprintf(("irq %x\n", sc->sc_irq));
582 if (sc->sc_irq & LP_USE_IRQ) {
583 sc->sc_state |= TOUT;
584 timeout(lptout, (caddr_t)lptdev,
585 (sc->sc_backoff = hz/LPTOUTINITIAL));
588 lprintf(("opened.\n"));
593 * lptclose -- close the device, free the local line buffer.
595 * Check for interrupted write call added.
599 lptclose(dev_t dev, int flags, int fmt, struct proc *p)
601 u_int unit = LPTUNIT(minor(dev));
602 struct lpt_data *sc = UNITOSOFTC(unit);
603 device_t lptdev = UNITODEVICE(unit);
604 device_t ppbus = device_get_parent(lptdev);
607 if(sc->sc_flags & LP_BYPASS)
610 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
613 sc->sc_state &= ~OPEN;
615 /* if the last write was interrupted, don't complete it */
616 if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
617 while ((ppb_rstr(ppbus) &
618 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
619 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
620 /* wait 1/4 second, give up if we get a signal */
621 if (tsleep((caddr_t)lptdev, LPPRI|PCATCH,
622 "lpclose", hz) != EWOULDBLOCK)
625 ppb_wctr(ppbus, LPC_NINIT);
626 brelse(sc->sc_inbuf);
627 brelse(sc->sc_statbuf);
630 /* release the bus anyway
631 * unregistration of interrupt forced by release
633 lpt_release_ppbus(lptdev);
637 lprintf(("closed.\n"));
643 * Workhorse for actually spinning and writing bytes to printer
647 * This code is only used when we are polling the port
650 lpt_pushbytes(device_t dev)
652 struct lpt_data *sc = DEVTOSOFTC(dev);
653 device_t ppbus = device_get_parent(dev);
658 /* loop for every character .. */
659 while (sc->sc_xfercnt > 0) {
666 * Wait for printer ready.
667 * Loop 20 usecs testing BUSY bit, then sleep
668 * for exponentially increasing timeout. (vak)
670 for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
671 DELAY(1); /* XXX delay is NOT this accurate! */
672 if (spin >= MAX_SPIN) {
674 while (NOT_READY(ppbus)) {
676 * Now sleep, every cycle a
681 * But no more than 10 seconds. (vak)
685 err = tsleep((caddr_t)dev, LPPRI,
686 LPT_NAME "poll", tic);
687 if (err != EWOULDBLOCK) {
696 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
697 ppb_wctr(ppbus, sc->sc_control);
704 * lptread --retrieve printer status in IEEE1284 NIBBLE mode
708 lptread(dev_t dev, struct uio *uio, int ioflag)
710 u_int unit = LPTUNIT(minor(dev));
711 struct lpt_data *sc = UNITOSOFTC(unit);
712 device_t lptdev = UNITODEVICE(unit);
713 device_t ppbus = device_get_parent(lptdev);
716 if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0)))
719 /* read data in an other buffer, read/write may be simultaneous */
721 while (uio->uio_resid) {
722 if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
723 sc->sc_statbuf->b_data, min(BUFSTATSIZE,
724 uio->uio_resid), &len))) {
729 goto error; /* no more data */
731 if ((error = uiomove(sc->sc_statbuf->b_data, len, uio)))
736 ppb_1284_terminate(ppbus);
741 * lptwrite --copy a line from user space to a local buffer, then call
742 * putc to get the chars moved to the output queue.
744 * Flagging of interrupted write added.
748 lptwrite(dev_t dev, struct uio *uio, int ioflag)
752 u_int unit = LPTUNIT(minor(dev));
753 struct lpt_data *sc = UNITOSOFTC(unit);
754 device_t lptdev = UNITODEVICE(unit);
755 device_t ppbus = device_get_parent(lptdev);
757 if(sc->sc_flags & LP_BYPASS) {
758 /* we can't do writes in bypass mode */
762 /* request the ppbus only if we don't have it already */
763 /* XXX interrupt registration?! */
764 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0)
767 /* if interrupts are working, register the handler */
768 if (sc->sc_irq & LP_USE_IRQ) {
769 /* register our interrupt handler */
770 err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource,
771 INTR_TYPE_TTY, lpt_intr, lptdev,
774 device_printf(lptdev, "handler registration failed, polled mode.\n");
775 sc->sc_irq &= ~LP_USE_IRQ;
779 sc->sc_state &= ~INTERRUPTED;
780 while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
781 sc->sc_cp = sc->sc_inbuf->b_data ;
782 uiomove(sc->sc_cp, n, uio);
785 if (sc->sc_irq & LP_ENABLE_EXT) {
786 /* try any extended mode */
787 err = ppb_write(ppbus, sc->sc_cp,
791 /* if not all data was sent, we could rely
792 * on polling for the last bytes */
796 sc->sc_state |= INTERRUPTED;
799 /* advanced mode not avail */
800 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
805 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
807 /* if the printer is ready for a char, */
809 if ((sc->sc_state & OBUSY) == 0){
810 lprintf(("\nC %d. ", sc->sc_xfercnt));
814 if (sc->sc_state & OBUSY)
815 if ((err = tsleep((caddr_t)lptdev,
816 LPPRI|PCATCH, LPT_NAME "write", 0))) {
817 sc->sc_state |= INTERRUPTED;
822 /* check to see if we must do a polled write */
823 if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
826 err = lpt_pushbytes(lptdev);
833 /* we have not been interrupted, release the ppbus */
834 lpt_release_ppbus(lptdev);
840 * lpt_intr -- handle printer interrupts which occur when the printer is
841 * ready to accept another char.
843 * do checking for interrupted write call.
849 device_t lptdev = (device_t)arg;
850 device_t ppbus = device_get_parent(lptdev);
851 struct lpt_data *sc = DEVTOSOFTC(lptdev);
855 /* we must own the bus to use it */
856 if ((sc->sc_state & HAVEBUS) == 0)
860 * Is printer online and ready for output?
862 * Avoid falling back to lptout() too quickly. First spin-loop
863 * to see if the printer will become ready ``really soon now''.
865 for (i = 0; i < 100 &&
866 ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
868 if ((sts & RDY_MASK) == LP_READY) {
869 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
870 sc->sc_backoff = hz/LPTOUTINITIAL;
872 if (sc->sc_xfercnt) {
874 /*lprintf(("%x ", *sc->sc_cp)); */
875 ppb_wdtr(ppbus, *sc->sc_cp++) ;
876 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
878 ppb_wctr(ppbus, sc->sc_control);
880 /* any more data for printer */
881 if(--(sc->sc_xfercnt) > 0) return;
885 * No more data waiting for printer.
886 * Wakeup is not done if write call was interrupted.
888 sc->sc_state &= ~OBUSY;
890 if(!(sc->sc_state & INTERRUPTED))
894 } else { /* check for error */
895 if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
896 (sc->sc_state & OPEN))
897 sc->sc_state |= EERROR;
898 /* lptout() will jump in and try to restart. */
900 lprintf(("sts %x ", sts));
904 lptintr(device_t dev)
906 /* call the interrupt at required spl level */
916 lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
919 u_int unit = LPTUNIT(minor(dev));
920 struct lpt_data *sc = UNITOSOFTC(unit);
921 u_char old_sc_irq; /* old printer IRQ status */
925 if(sc->sc_irq & LP_HAS_IRQ) {
928 * If the IRQ status is changed,
929 * this will only be visible on the
932 * If interrupt status changes,
933 * this gets syslog'd.
935 old_sc_irq = sc->sc_irq;
936 switch(*(int*)data) {
938 sc->sc_irq &= (~LP_ENABLE_IRQ);
941 sc->sc_irq &= (~LP_ENABLE_EXT);
942 sc->sc_irq |= LP_ENABLE_IRQ;
945 /* classic irq based transfer and advanced
946 * modes are in conflict
948 sc->sc_irq &= (~LP_ENABLE_IRQ);
949 sc->sc_irq |= LP_ENABLE_EXT;
952 sc->sc_irq &= (~LP_ENABLE_EXT);
958 if (old_sc_irq != sc->sc_irq )
959 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
961 (sc->sc_irq & LP_ENABLE_IRQ)?
962 "interrupt-driven":"polled",
963 (sc->sc_irq & LP_ENABLE_EXT)?
964 "extended":"standard");
965 } else /* polled port */
975 DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0);