2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1990 William F. Jolitz, TeleMuse
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This software is a component of "386BSD" developed by
18 * William F. Jolitz, TeleMuse.
19 * 4. Neither the name of the developer nor the name "386BSD"
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
24 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
25 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
26 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
27 * NOT MAKE USE OF THIS WORK.
29 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
30 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
31 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
32 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
33 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
34 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
35 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
36 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
38 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * from: unknown origin, 386BSD 0.1
51 * From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
52 * From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
59 * Device Driver for AT parallel printer port
60 * Written by William Jolitz 12/18/90
64 * Updated for ppbus by Nicolas Souchu
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/module.h>
75 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78 #include <sys/malloc.h>
80 #include <machine/bus.h>
81 #include <machine/resource.h>
84 #include <dev/ppbus/lptio.h>
85 #include <dev/ppbus/ppbconf.h>
86 #include <dev/ppbus/ppb_1284.h>
87 #include <dev/ppbus/lpt.h>
89 #include <dev/ppbus/ppbio.h>
94 #define lprintf(args) \
99 static int volatile lptflag = 1;
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 LPPRI (PZERO+8)
107 #define BUFSTATSIZE 32
111 struct cdev *sc_cdev;
112 struct cdev *sc_cdev_bypass;
114 /* default case: negative prime, negative ack, handshake strobe,
118 #define LP_POS_INIT 0x04 /* if we are a positive 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 */
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 */
135 struct callout sc_timer;
137 struct resource *sc_intr_resource; /* interrupt resource */
138 void *sc_intr_cookie; /* interrupt cookie */
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))
150 static void lptintr(void *arg);
152 static devclass_t lpt_devclass;
156 #define OPEN (1<<0) /* device is open */
157 #define ASLP (1<<1) /* awaiting draining of printer */
158 #define EERROR (1<<2) /* error was received from printer */
159 #define OBUSY (1<<3) /* printer is busy doing output */
160 #define LPTOUT (1<<4) /* timeout while not selected */
161 #define TOUT (1<<5) /* timeout while not selected */
162 #define LPTINIT (1<<6) /* waiting to initialize for open */
163 #define INTERRUPTED (1<<7) /* write call was interrupted */
164 #define HAVEBUS (1<<8) /* the driver owns the bus */
166 /* status masks to interrogate printer status */
167 #define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
168 #define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
170 /* Printer Ready condition - from lpa.c */
171 /* Only used in polling code */
172 #define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
173 #define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
174 #define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
176 #define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
177 #define MAX_SPIN 20 /* Max delay for device ready in usecs */
180 static d_open_t lptopen;
181 static d_close_t lptclose;
182 static d_write_t lptwrite;
183 static d_read_t lptread;
184 static d_ioctl_t lptioctl;
186 static struct cdevsw lpt_cdevsw = {
187 .d_version = D_VERSION,
197 lpt_request_ppbus(device_t dev, int how)
199 device_t ppbus = device_get_parent(dev);
200 struct lpt_data *sc = DEVTOSOFTC(dev);
204 * We might already have the bus for a write(2) after an interrupted
207 ppb_assert_locked(ppbus);
208 if (sc->sc_state & HAVEBUS)
211 error = ppb_request_bus(ppbus, dev, how);
213 sc->sc_state |= HAVEBUS;
218 lpt_release_ppbus(device_t dev)
220 device_t ppbus = device_get_parent(dev);
221 struct lpt_data *sc = DEVTOSOFTC(dev);
224 ppb_assert_locked(ppbus);
225 if (sc->sc_state & HAVEBUS) {
226 error = ppb_release_bus(ppbus, dev);
228 sc->sc_state &= ~HAVEBUS;
234 * Internal routine to lptprobe to do port tests of one byte value
237 lpt_port_test(device_t ppbus, u_char data, u_char mask)
242 ppb_wdtr(ppbus, data);
246 temp = ppb_rdtr(ppbus) & mask;
248 while (temp != data && --timeout);
249 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
250 return (temp == data);
254 * Probe simplified by replacing multiple loops with a hardcoded
255 * test pattern - 1999/02/08 des@freebsd.org
257 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
258 * Based partially on Rod Grimes' printer probe
261 * 1) If no port address was given, use the bios detected ports
262 * and autodetect what ports the printers are on.
263 * 2) Otherwise, probe the data port at the address given,
264 * using the method in Rod Grimes' port probe.
265 * (Much code ripped off directly from Rod's probe.)
267 * Comments from Rod's probe:
269 * 1) You should be able to write to and read back the same value
270 * to the data port. Do an alternating zeros, alternating ones,
271 * walking zero, and walking one test to check for stuck bits.
273 * 2) You should be able to write to and read back the same value
274 * to the control port lower 5 bits, the upper 3 bits are reserved
275 * per the IBM PC technical reference manauls and different boards
276 * do different things with them. Do an alternating zeros, alternating
277 * ones, walking zero, and walking one test to check for stuck bits.
279 * Some printers drag the strobe line down when the are powered off
280 * so this bit has been masked out of the control port test.
282 * XXX Some printers may not like a fast pulse on init or strobe, I
283 * don't know at this point, if that becomes a problem these bits
284 * should be turned off in the mask byte for the control port test.
286 * We are finally left with a mask of 0x14, due to some printers
287 * being adamant about holding other bits high ........
289 * Before probing the control port, we write a 0 to the data port -
290 * If not, some printers chuck out garbage when the strobe line
293 * 3) Set the data and control ports to a value of 0
295 * This probe routine has been tested on Epson Lx-800, HP LJ3P,
296 * Epson FX-1170 and C.Itoh 8510RM
298 * Quick exit on fail added.
301 lpt_detect(device_t dev)
303 device_t ppbus = device_get_parent(dev);
305 static u_char testbyte[18] = {
306 0x55, /* alternating zeros */
307 0xaa, /* alternating ones */
308 0xfe, 0xfd, 0xfb, 0xf7,
309 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
310 0x01, 0x02, 0x04, 0x08,
311 0x10, 0x20, 0x40, 0x80 /* walking one */
313 int i, error, status;
315 status = 1; /* assume success */
318 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
320 device_printf(dev, "cannot alloc ppbus (%d)!\n", error);
324 for (i = 0; i < 18 && status; i++)
325 if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
330 /* write 0's to control and data ports */
334 lpt_release_ppbus(dev);
341 lpt_identify(driver_t *driver, device_t parent)
346 dev = device_find_child(parent, LPT_NAME, -1);
348 BUS_ADD_CHILD(parent, 0, LPT_NAME, -1);
355 lpt_probe(device_t dev)
358 if (!lpt_detect(dev))
361 device_set_desc(dev, "Printer");
367 lpt_attach(device_t dev)
369 device_t ppbus = device_get_parent(dev);
370 struct lpt_data *sc = DEVTOSOFTC(dev);
371 int rid = 0, unit = device_get_unit(dev);
374 sc->sc_primed = 0; /* not primed yet */
375 ppb_init_callout(ppbus, &sc->sc_timer, 0);
378 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
380 device_printf(dev, "cannot alloc ppbus (%d)!\n", error);
384 ppb_wctr(ppbus, LPC_NINIT);
385 lpt_release_ppbus(dev);
388 /* declare our interrupt handler */
389 sc->sc_intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
391 if (sc->sc_intr_resource) {
392 error = bus_setup_intr(dev, sc->sc_intr_resource,
393 INTR_TYPE_TTY | INTR_MPSAFE, NULL, lptintr, sc,
394 &sc->sc_intr_cookie);
396 bus_release_resource(dev, SYS_RES_IRQ, rid,
397 sc->sc_intr_resource);
399 "Unable to register interrupt handler\n");
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\n", sc->sc_irq));
410 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
411 sc->sc_statbuf = malloc(BUFSTATSIZE, M_DEVBUF, M_WAITOK);
413 sc->sc_cdev = make_dev(&lpt_cdevsw, unit,
414 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
415 sc->sc_cdev->si_drv1 = sc;
416 sc->sc_cdev->si_drv2 = 0;
417 sc->sc_cdev_bypass = make_dev(&lpt_cdevsw, unit,
418 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
419 sc->sc_cdev_bypass->si_drv1 = sc;
420 sc->sc_cdev_bypass->si_drv2 = (void *)LP_BYPASS;
425 lpt_detach(device_t dev)
427 struct lpt_data *sc = DEVTOSOFTC(dev);
428 device_t ppbus = device_get_parent(dev);
430 destroy_dev(sc->sc_cdev);
431 destroy_dev(sc->sc_cdev_bypass);
433 lpt_release_ppbus(dev);
435 callout_drain(&sc->sc_timer);
436 if (sc->sc_intr_resource != NULL) {
437 bus_teardown_intr(dev, sc->sc_intr_resource,
439 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr_resource);
441 free(sc->sc_inbuf, M_DEVBUF);
442 free(sc->sc_statbuf, M_DEVBUF);
450 struct lpt_data *sc = arg;
451 device_t dev = sc->sc_dev;
454 ppbus = device_get_parent(dev);
455 ppb_assert_locked(ppbus);
456 lprintf(("T %x ", ppb_rstr(ppbus)));
457 if (sc->sc_state & OPEN) {
459 if (sc->sc_backoff > hz/LPTOUTMAX)
460 sc->sc_backoff = hz/LPTOUTMAX;
461 callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
463 sc->sc_state &= ~TOUT;
465 if (sc->sc_state & EERROR)
466 sc->sc_state &= ~EERROR;
469 * Avoid possible hangs due to missed interrupts
471 if (sc->sc_xfercnt) {
474 sc->sc_state &= ~OBUSY;
480 * lptopen -- reset the printer, then wait until it's selected and not busy.
481 * If LP_BYPASS flag is selected, then we do not try to select the
482 * printer -- this is just used for passing ioctls.
486 lptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
489 struct lpt_data *sc = dev->si_drv1;
497 ppbus = device_get_parent(lptdev);
501 lprintf(("%s: still open %x\n", device_get_nameunit(lptdev),
506 sc->sc_state |= LPTINIT;
508 sc->sc_flags = (uintptr_t)dev->si_drv2;
510 /* Check for open with BYPASS flag set. */
511 if (sc->sc_flags & LP_BYPASS) {
517 /* request the ppbus only if we don't have it already */
518 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
519 /* give it a chance to try later */
525 lprintf(("%s flags 0x%x\n", device_get_nameunit(lptdev),
528 /* set IRQ status according to ENABLE_IRQ flag
530 if (sc->sc_irq & LP_ENABLE_IRQ)
531 sc->sc_irq |= LP_USE_IRQ;
533 sc->sc_irq &= ~LP_USE_IRQ;
536 if ((sc->sc_flags & LP_NO_PRIME) == 0) {
537 if ((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
544 ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
546 /* wait till ready (printer running diagnostics) */
549 /* ran out of waiting for the printer */
550 if (trys++ >= LPINITRDY*4) {
551 lprintf(("status %x\n", ppb_rstr(ppbus)));
553 lpt_release_ppbus(lptdev);
559 /* wait 1/4 second, give up if we get a signal */
560 if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lptinit",
561 hz / 4) != EWOULDBLOCK) {
562 lpt_release_ppbus(lptdev);
568 /* is printer online and ready for output */
569 } while ((ppb_rstr(ppbus) &
570 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
571 (LPS_SEL|LPS_NBSY|LPS_NERR));
573 sc->sc_control = LPC_SEL|LPC_NINIT;
574 if (sc->sc_flags & LP_AUTOLF)
575 sc->sc_control |= LPC_AUTOL;
577 /* enable interrupt if interrupt-driven */
578 if (sc->sc_irq & LP_USE_IRQ)
579 sc->sc_control |= LPC_ENA;
581 ppb_wctr(ppbus, sc->sc_control);
583 sc->sc_state &= ~LPTINIT;
584 sc->sc_state |= OPEN;
587 /* only use timeout if using interrupt */
588 lprintf(("irq %x\n", sc->sc_irq));
589 if (sc->sc_irq & LP_USE_IRQ) {
590 sc->sc_state |= TOUT;
591 sc->sc_backoff = hz / LPTOUTINITIAL;
592 callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
595 /* release the ppbus */
596 lpt_release_ppbus(lptdev);
599 lprintf(("opened.\n"));
604 * lptclose -- close the device, free the local line buffer.
606 * Check for interrupted write call added.
610 lptclose(struct cdev *dev, int flags, int fmt, struct thread *td)
612 struct lpt_data *sc = dev->si_drv1;
613 device_t lptdev = sc->sc_dev;
614 device_t ppbus = device_get_parent(lptdev);
618 if (sc->sc_flags & LP_BYPASS)
621 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
626 /* if the last write was interrupted, don't complete it */
627 if ((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
628 while ((ppb_rstr(ppbus) &
629 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
630 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
631 /* wait 1 second, give up if we get a signal */
632 if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lpclose",
636 sc->sc_state &= ~OPEN;
637 callout_stop(&sc->sc_timer);
638 ppb_wctr(ppbus, LPC_NINIT);
641 * unregistration of interrupt forced by release
643 lpt_release_ppbus(lptdev);
649 lprintf(("closed.\n"));
655 * Workhorse for actually spinning and writing bytes to printer
659 * This code is only used when we are polling the port
662 lpt_pushbytes(struct lpt_data *sc)
664 device_t dev = sc->sc_dev;
665 device_t ppbus = device_get_parent(dev);
669 ppb_assert_locked(ppbus);
671 /* loop for every character .. */
672 while (sc->sc_xfercnt > 0) {
679 * Wait for printer ready.
680 * Loop 20 usecs testing BUSY bit, then sleep
681 * for exponentially increasing timeout. (vak)
683 for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
684 DELAY(1); /* XXX delay is NOT this accurate! */
685 if (spin >= MAX_SPIN) {
687 while (NOT_READY(ppbus)) {
689 * Now sleep, every cycle a
694 * But no more than 10 seconds. (vak)
698 err = ppb_sleep(ppbus, dev, LPPRI,
699 LPT_NAME "poll", tic);
700 if (err != EWOULDBLOCK) {
709 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
710 ppb_wctr(ppbus, sc->sc_control);
717 * lptread --retrieve printer status in IEEE1284 NIBBLE mode
721 lptread(struct cdev *dev, struct uio *uio, int ioflag)
723 struct lpt_data *sc = dev->si_drv1;
724 device_t lptdev = sc->sc_dev;
725 device_t ppbus = device_get_parent(lptdev);
728 if (sc->sc_flags & LP_BYPASS) {
729 /* we can't do reads in bypass mode */
734 if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) {
739 /* read data in an other buffer, read/write may be simultaneous */
741 while (uio->uio_resid) {
742 if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
743 sc->sc_statbuf, min(BUFSTATSIZE,
744 uio->uio_resid), &len))) {
749 goto error; /* no more data */
752 error = uiomove(sc->sc_statbuf, len, uio);
759 ppb_1284_terminate(ppbus);
765 * lptwrite --copy a line from user space to a local buffer, then call
766 * putc to get the chars moved to the output queue.
768 * Flagging of interrupted write added.
772 lptwrite(struct cdev *dev, struct uio *uio, int ioflag)
776 struct lpt_data *sc = dev->si_drv1;
777 device_t lptdev = sc->sc_dev;
778 device_t ppbus = device_get_parent(lptdev);
780 if (sc->sc_flags & LP_BYPASS) {
781 /* we can't do writes in bypass mode */
785 /* request the ppbus only if we don't have it already */
787 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
792 sc->sc_state &= ~INTERRUPTED;
793 while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
794 sc->sc_cp = sc->sc_inbuf;
796 err = uiomove(sc->sc_cp, n, uio);
802 if (sc->sc_irq & LP_ENABLE_EXT) {
803 /* try any extended mode */
804 err = ppb_write(ppbus, sc->sc_cp,
808 /* if not all data was sent, we could rely
809 * on polling for the last bytes */
813 sc->sc_state |= INTERRUPTED;
817 /* advanced mode not avail */
819 "%s: advanced mode not avail, polling\n",
820 device_get_nameunit(sc->sc_dev));
826 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
828 /* if the printer is ready for a char, */
830 if ((sc->sc_state & OBUSY) == 0){
831 lprintf(("\nC %d. ", sc->sc_xfercnt));
835 if (sc->sc_state & OBUSY)
836 if ((err = ppb_sleep(ppbus, lptdev,
837 LPPRI|PCATCH, LPT_NAME "write", 0))) {
838 sc->sc_state |= INTERRUPTED;
844 /* check to see if we must do a polled write */
845 if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
848 err = lpt_pushbytes(sc);
857 /* we have not been interrupted, release the ppbus */
858 lpt_release_ppbus(lptdev);
865 * lptintr -- handle printer interrupts which occur when the printer is
866 * ready to accept another char.
868 * do checking for interrupted write call.
873 struct lpt_data *sc = arg;
874 device_t lptdev = sc->sc_dev;
875 device_t ppbus = device_get_parent(lptdev);
880 * Is printer online and ready for output?
882 * Avoid falling back to lptout() too quickly. First spin-loop
883 * to see if the printer will become ready ``really soon now''.
885 for (i = 0; i < 100 &&
886 ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
888 if ((sts & RDY_MASK) == LP_READY) {
889 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
890 sc->sc_backoff = hz / LPTOUTINITIAL;
892 if (sc->sc_xfercnt) {
894 /*lprintf(("%x ", *sc->sc_cp)); */
895 ppb_wdtr(ppbus, *sc->sc_cp++) ;
896 ppb_wctr(ppbus, sc->sc_control|LPC_STB);
898 ppb_wctr(ppbus, sc->sc_control);
900 /* any more data for printer */
901 if (--(sc->sc_xfercnt) > 0)
906 * No more data waiting for printer.
907 * Wakeup is not done if write call was not interrupted.
909 sc->sc_state &= ~OBUSY;
911 if (!(sc->sc_state & INTERRUPTED))
915 } else { /* check for error */
916 if (((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
917 (sc->sc_state & OPEN))
918 sc->sc_state |= EERROR;
919 /* lptout() will jump in and try to restart. */
921 lprintf(("sts %x ", sts));
925 lptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
928 struct lpt_data *sc = dev->si_drv1;
930 u_char old_sc_irq; /* old printer IRQ status */
934 ppbus = device_get_parent(sc->sc_dev);
936 if (sc->sc_irq & LP_HAS_IRQ) {
939 * If the IRQ status is changed,
940 * this will only be visible on the
943 * If interrupt status changes,
944 * this gets syslog'd.
946 old_sc_irq = sc->sc_irq;
947 switch (*(int*)data) {
949 sc->sc_irq &= (~LP_ENABLE_IRQ);
952 sc->sc_irq &= (~LP_ENABLE_EXT);
953 sc->sc_irq |= LP_ENABLE_IRQ;
956 /* classic irq based transfer and advanced
957 * modes are in conflict
959 sc->sc_irq &= (~LP_ENABLE_IRQ);
960 sc->sc_irq |= LP_ENABLE_EXT;
963 sc->sc_irq &= (~LP_ENABLE_EXT);
969 if (old_sc_irq != sc->sc_irq )
970 log(LOG_NOTICE, "%s: switched to %s %s mode\n",
971 device_get_nameunit(sc->sc_dev),
972 (sc->sc_irq & LP_ENABLE_IRQ)?
973 "interrupt-driven":"polled",
974 (sc->sc_irq & LP_ENABLE_EXT)?
975 "extended":"standard");
976 } else /* polled port */
987 static device_method_t lpt_methods[] = {
988 /* device interface */
989 DEVMETHOD(device_identify, lpt_identify),
990 DEVMETHOD(device_probe, lpt_probe),
991 DEVMETHOD(device_attach, lpt_attach),
992 DEVMETHOD(device_detach, lpt_detach),
997 static driver_t lpt_driver = {
1000 sizeof(struct lpt_data),
1003 DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0);
1004 MODULE_DEPEND(lpt, ppbus, 1, 1, 1);