Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.12
      1   1.1      cgd /*
      2  1.11  mycroft  * Copyright (c) 1993, 1994 Charles Hannum.
      3   1.1      cgd  * Copyright (c) 1990 William F. Jolitz, TeleMuse
      4   1.1      cgd  * All rights reserved.
      5   1.1      cgd  *
      6   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      7   1.1      cgd  * modification, are permitted provided that the following conditions
      8   1.1      cgd  * are met:
      9   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     10   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     11   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     13   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     14   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     15   1.1      cgd  *    must display the following acknowledgement:
     16   1.1      cgd  *	This software is a component of "386BSD" developed by
     17   1.1      cgd  *	William F. Jolitz, TeleMuse.
     18   1.1      cgd  * 4. Neither the name of the developer nor the name "386BSD"
     19   1.1      cgd  *    may be used to endorse or promote products derived from this software
     20   1.1      cgd  *    without specific prior written permission.
     21   1.1      cgd  *
     22   1.1      cgd  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
     23   1.1      cgd  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
     24   1.1      cgd  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
     25   1.1      cgd  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
     26   1.1      cgd  * NOT MAKE USE OF THIS WORK.
     27   1.1      cgd  *
     28   1.1      cgd  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
     29   1.1      cgd  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
     30   1.1      cgd  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
     31   1.1      cgd  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
     32   1.1      cgd  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
     33   1.1      cgd  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
     34   1.1      cgd  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
     35   1.1      cgd  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
     36   1.1      cgd  *
     37   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
     38   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     39   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     40   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
     41   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     42   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     43   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     44   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     45   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     46   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     47   1.1      cgd  * SUCH DAMAGE.
     48   1.1      cgd  *
     49  1.12  mycroft  *	$Id: lpt.c,v 1.12 1994/02/19 02:43:53 mycroft Exp $
     50   1.1      cgd  */
     51   1.1      cgd 
     52   1.1      cgd /*
     53   1.1      cgd  * Device Driver for AT parallel printer port
     54   1.1      cgd  */
     55   1.1      cgd 
     56   1.1      cgd #include "lpt.h"
     57   1.1      cgd 
     58   1.9  mycroft #include <sys/param.h>
     59   1.9  mycroft #include <sys/systm.h>
     60   1.9  mycroft #include <sys/proc.h>
     61   1.9  mycroft #include <sys/user.h>
     62   1.9  mycroft #include <sys/buf.h>
     63   1.9  mycroft #include <sys/kernel.h>
     64   1.9  mycroft #include <sys/ioctl.h>
     65   1.9  mycroft #include <sys/uio.h>
     66  1.11  mycroft #include <sys/device.h>
     67  1.11  mycroft #include <sys/syslog.h>
     68   1.1      cgd 
     69  1.11  mycroft #include <machine/cpu.h>
     70   1.9  mycroft #include <machine/pio.h>
     71   1.9  mycroft 
     72   1.9  mycroft #include <i386/isa/isa.h>
     73   1.9  mycroft #include <i386/isa/isa_device.h>
     74   1.9  mycroft #include <i386/isa/lptreg.h>
     75   1.1      cgd 
     76  1.11  mycroft #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     77  1.11  mycroft #define	STEP		hz/4
     78  1.11  mycroft 
     79  1.11  mycroft #define	LPTPRI		(PZERO+8)
     80  1.11  mycroft #define	LPT_BSIZE	1024
     81   1.1      cgd 
     82   1.1      cgd #ifndef DEBUG
     83   1.1      cgd #define lprintf
     84   1.1      cgd #else
     85  1.11  mycroft #define lprintf		if (lptdebug) printf
     86  1.11  mycroft int lptdebug = 1;
     87   1.1      cgd #endif
     88   1.1      cgd 
     89  1.11  mycroft struct lpt_softc {
     90  1.11  mycroft 	struct device sc_dev;
     91   1.1      cgd 
     92  1.11  mycroft 	size_t sc_count;
     93  1.11  mycroft 	struct buf *sc_inbuf;
     94  1.11  mycroft 	u_char *sc_cp;
     95  1.12  mycroft 	int sc_spinmax;
     96  1.11  mycroft 	u_short sc_iobase;
     97  1.11  mycroft 	u_short sc_irq;
     98  1.11  mycroft 	u_char sc_state;
     99  1.11  mycroft #define	LPT_OPEN	0x01	/* device is open */
    100  1.11  mycroft #define	LPT_OBUSY	0x02	/* printer is busy doing output */
    101  1.11  mycroft #define	LPT_INIT	0x04	/* waiting to initialize for open */
    102  1.11  mycroft 	u_char sc_flags;
    103  1.11  mycroft #define	LPT_AUTOLF	0x20	/* automatic LF on CR */
    104  1.11  mycroft #define	LPT_NOPRIME	0x40	/* don't prime on open */
    105  1.11  mycroft #define	LPT_NOINTR	0x80	/* do not use interrupt */
    106  1.11  mycroft 	u_char sc_control;
    107  1.11  mycroft } lpt_softc[NLPT];
    108  1.11  mycroft 
    109  1.11  mycroft int lptprobe __P((struct isa_device *));
    110  1.11  mycroft int lptattach __P((struct isa_device *));
    111  1.11  mycroft int lptintr __P((int));
    112   1.1      cgd 
    113  1.11  mycroft struct isa_driver lptdriver = {
    114   1.1      cgd 	lptprobe, lptattach, "lpt"
    115   1.1      cgd };
    116   1.1      cgd 
    117  1.11  mycroft #define	LPTUNIT(s)	(minor(s) & 0x1f)
    118  1.11  mycroft #define	LPTFLAGS(s)	(minor(s) & 0xe0)
    119   1.1      cgd 
    120  1.11  mycroft #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
    121  1.11  mycroft #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
    122  1.11  mycroft #define	NOT_READY()	notready(inb(iobase + lpt_status), sc)
    123  1.11  mycroft 
    124  1.11  mycroft static int notready __P((u_char, struct lpt_softc *));
    125  1.11  mycroft static void lptout __P((struct lpt_softc *));
    126  1.11  mycroft static int pushbytes __P((struct lpt_softc *));
    127   1.1      cgd 
    128   1.2      cgd /*
    129  1.11  mycroft  * Internal routine to lptprobe to do port tests of one byte value.
    130   1.2      cgd  */
    131   1.2      cgd int
    132  1.11  mycroft lpt_port_test(port, data, mask)
    133  1.11  mycroft 	u_short port;
    134  1.11  mycroft 	u_char data, mask;
    135  1.11  mycroft {
    136  1.11  mycroft 	int timeout;
    137  1.11  mycroft 	u_char temp;
    138   1.2      cgd 
    139  1.11  mycroft 	data &= mask;
    140   1.2      cgd 	outb(port, data);
    141  1.11  mycroft 	timeout = 1000;
    142  1.11  mycroft 	do {
    143  1.11  mycroft 		DELAY(10);
    144   1.5      cgd 		temp = inb(port) & mask;
    145  1.11  mycroft 	} while (temp != data && --timeout);
    146  1.11  mycroft 	lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", port, data,
    147  1.11  mycroft 	    temp, timeout);
    148   1.2      cgd 	return (temp == data);
    149  1.11  mycroft }
    150   1.2      cgd 
    151   1.2      cgd /*
    152   1.2      cgd  * Logic:
    153   1.2      cgd  *	1) You should be able to write to and read back the same value
    154   1.2      cgd  *	   to the data port.  Do an alternating zeros, alternating ones,
    155   1.2      cgd  *	   walking zero, and walking one test to check for stuck bits.
    156   1.2      cgd  *
    157   1.2      cgd  *	2) You should be able to write to and read back the same value
    158   1.2      cgd  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    159   1.2      cgd  *	   per the IBM PC technical reference manauls and different boards
    160   1.2      cgd  *	   do different things with them.  Do an alternating zeros, alternating
    161   1.2      cgd  *	   ones, walking zero, and walking one test to check for stuck bits.
    162   1.2      cgd  *
    163   1.2      cgd  *	   Some printers drag the strobe line down when the are powered off
    164   1.2      cgd  * 	   so this bit has been masked out of the control port test.
    165   1.2      cgd  *
    166   1.2      cgd  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    167   1.2      cgd  *	   don't know at this point, if that becomes a problem these bits
    168   1.2      cgd  *	   should be turned off in the mask byte for the control port test.
    169   1.2      cgd  *
    170   1.2      cgd  *	3) Set the data and control ports to a value of 0
    171   1.2      cgd  */
    172   1.2      cgd int
    173  1.11  mycroft lptprobe(isa_dev)
    174  1.11  mycroft 	struct isa_device *isa_dev;
    175  1.11  mycroft {
    176  1.11  mycroft 	u_short iobase = isa_dev->id_iobase;
    177  1.11  mycroft 	u_short port;
    178  1.11  mycroft 	u_char mask, data;
    179  1.11  mycroft 	int i;
    180   1.2      cgd 
    181  1.11  mycroft #ifdef DEBUG
    182  1.11  mycroft #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
    183  1.11  mycroft 		    return 0;} while (0)
    184  1.11  mycroft #else
    185  1.11  mycroft #define	ABORT	return 0
    186  1.11  mycroft #endif
    187   1.2      cgd 
    188  1.11  mycroft 	port = iobase + lpt_data;
    189   1.2      cgd 	mask = 0xff;
    190  1.11  mycroft 
    191  1.11  mycroft 	for (;;) {
    192   1.2      cgd 		data = 0x55;				/* Alternating zeros */
    193  1.11  mycroft 		if (!lpt_port_test(port, data, mask))
    194  1.11  mycroft 			ABORT;
    195   1.2      cgd 
    196   1.2      cgd 		data = 0xaa;				/* Alternating ones */
    197  1.11  mycroft 		if (!lpt_port_test(port, data, mask))
    198  1.11  mycroft 			ABORT;
    199   1.2      cgd 
    200  1.11  mycroft 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    201   1.2      cgd 			data = ~(1 << i);
    202  1.11  mycroft 			if (!lpt_port_test(port, data, mask))
    203  1.11  mycroft 				ABORT;
    204  1.11  mycroft 		}
    205   1.2      cgd 
    206  1.11  mycroft 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    207   1.2      cgd 			data = (1 << i);
    208  1.11  mycroft 			if (!lpt_port_test(port, data, mask))
    209  1.11  mycroft 				ABORT;
    210  1.11  mycroft 		}
    211   1.2      cgd 
    212  1.11  mycroft 		if (port == iobase + lpt_data) {
    213  1.11  mycroft 			port = iobase + lpt_control;
    214  1.11  mycroft #if 1 /* XXX */
    215  1.11  mycroft 			mask = 0x04;
    216  1.11  mycroft #else
    217   1.2      cgd 			mask = 0x1e;
    218  1.11  mycroft #endif
    219  1.11  mycroft 		} else
    220  1.11  mycroft 			break;
    221   1.1      cgd 	}
    222  1.11  mycroft 	outb(iobase + lpt_data, 0);
    223  1.11  mycroft 	outb(iobase + lpt_control, 0);
    224  1.11  mycroft 
    225  1.11  mycroft 	return LPT_NPORTS;
    226  1.11  mycroft }
    227   1.1      cgd 
    228  1.11  mycroft int
    229  1.11  mycroft lptattach(isa_dev)
    230  1.11  mycroft 	struct isa_device *isa_dev;
    231   1.1      cgd {
    232  1.11  mycroft 	struct lpt_softc *sc = &lpt_softc[isa_dev->id_unit];
    233  1.11  mycroft 	u_short iobase = isa_dev->id_iobase;
    234  1.11  mycroft 	u_short irq = isa_dev->id_irq;
    235  1.11  mycroft 
    236  1.11  mycroft 	/* XXX HACK */
    237  1.11  mycroft 	sprintf(sc->sc_dev.dv_xname, "%s%d", lptdriver.name, isa_dev->id_unit);
    238  1.11  mycroft 	sc->sc_dev.dv_unit = isa_dev->id_unit;
    239  1.11  mycroft 
    240  1.11  mycroft 	if (!irq)
    241  1.11  mycroft 		printf("%s: polled\n", sc->sc_dev.dv_xname);
    242   1.1      cgd 
    243  1.11  mycroft 	sc->sc_iobase = iobase;
    244  1.11  mycroft 	sc->sc_irq = irq;
    245   1.6  mycroft 	sc->sc_state = 0;
    246  1.11  mycroft 	outb(iobase + lpt_control, LPC_NINIT);
    247   1.1      cgd }
    248   1.1      cgd 
    249   1.1      cgd /*
    250  1.11  mycroft  * Reset the printer, then wait until it's selected and not busy.
    251   1.1      cgd  */
    252  1.11  mycroft int
    253   1.1      cgd lptopen(dev, flag)
    254   1.1      cgd 	dev_t dev;
    255   1.1      cgd 	int flag;
    256   1.1      cgd {
    257  1.11  mycroft 	int unit = LPTUNIT(dev);
    258  1.11  mycroft 	u_char flags = LPTFLAGS(dev);
    259  1.11  mycroft 	struct lpt_softc *sc;
    260  1.11  mycroft 	u_short iobase;
    261  1.11  mycroft 	u_char control;
    262  1.11  mycroft 	int error;
    263  1.11  mycroft 	int spin;
    264  1.11  mycroft 
    265  1.11  mycroft 	if (unit >= NLPT)
    266  1.11  mycroft 		return ENXIO;
    267  1.11  mycroft 	sc = &lpt_softc[unit];
    268  1.11  mycroft 	if (!sc->sc_iobase)
    269  1.11  mycroft 		return ENXIO;
    270  1.11  mycroft 
    271  1.11  mycroft 	if (sc->sc_irq == 0 && (flags & LPT_NOINTR) == 0)
    272  1.11  mycroft 		return ENXIO;
    273  1.11  mycroft 
    274  1.11  mycroft #ifdef DIAGNOSTIC
    275  1.11  mycroft 	if (sc->sc_state)
    276  1.11  mycroft 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
    277  1.11  mycroft 		    sc->sc_state);
    278  1.11  mycroft #endif
    279  1.11  mycroft 
    280  1.11  mycroft 	if (sc->sc_state)
    281  1.11  mycroft 		return EBUSY;
    282   1.1      cgd 
    283  1.11  mycroft 	sc->sc_state = LPT_INIT;
    284  1.11  mycroft 	sc->sc_flags = flags;
    285  1.11  mycroft 	lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
    286  1.11  mycroft 	iobase = sc->sc_iobase;
    287  1.11  mycroft 
    288  1.11  mycroft 	if ((flags & LPT_NOPRIME) == 0) {
    289  1.11  mycroft 		/* assert INIT for 100 usec to start up printer */
    290  1.11  mycroft 		outb(iobase + lpt_control, LPC_SELECT);
    291  1.11  mycroft 		DELAY(100);
    292   1.1      cgd 	}
    293  1.11  mycroft 
    294  1.11  mycroft 	control = LPC_SELECT | LPC_NINIT;
    295  1.11  mycroft 	outb(iobase + lpt_control, control);
    296   1.1      cgd 
    297   1.1      cgd 	/* wait till ready (printer running diagnostics) */
    298  1.11  mycroft 	for (spin = 0; NOT_READY(); spin += STEP) {
    299  1.11  mycroft 		if (spin >= TIMEOUT) {
    300   1.1      cgd 			sc->sc_state = 0;
    301  1.11  mycroft 			return EBUSY;
    302   1.1      cgd 		}
    303   1.1      cgd 
    304   1.1      cgd 		/* wait 1/4 second, give up if we get a signal */
    305  1.11  mycroft 		if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen",
    306  1.11  mycroft 		    STEP) != EWOULDBLOCK) {
    307   1.1      cgd 			sc->sc_state = 0;
    308  1.11  mycroft 			return error;
    309   1.1      cgd 		}
    310   1.1      cgd 	}
    311   1.1      cgd 
    312  1.11  mycroft 	if ((flags & LPT_NOINTR) == 0)
    313  1.11  mycroft 		control |= LPC_IENABLE;
    314  1.11  mycroft 	if (flags & LPT_AUTOLF)
    315  1.11  mycroft 		control |= LPC_AUTOLF;
    316  1.11  mycroft 	sc->sc_control = control;
    317  1.11  mycroft 	outb(iobase + lpt_control, control);
    318  1.11  mycroft 
    319  1.11  mycroft 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    320  1.11  mycroft 	sc->sc_count = 0;
    321  1.11  mycroft 	sc->sc_state = LPT_OPEN;
    322  1.11  mycroft 	lptout(sc);
    323  1.11  mycroft 
    324  1.11  mycroft 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
    325  1.11  mycroft 	return 0;
    326   1.1      cgd }
    327   1.1      cgd 
    328  1.11  mycroft int
    329  1.11  mycroft notready(status, sc)
    330  1.11  mycroft 	u_char status;
    331   1.1      cgd 	struct lpt_softc *sc;
    332  1.11  mycroft {
    333  1.11  mycroft 	status ^= LPS_INVERT;
    334   1.1      cgd 
    335  1.11  mycroft 	if (status & LPS_NOPAPER)
    336  1.11  mycroft 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
    337  1.11  mycroft 	else if (status & LPS_SELECT)
    338  1.11  mycroft 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
    339  1.11  mycroft 	else if (status & LPS_NERR)
    340  1.11  mycroft 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
    341   1.1      cgd 
    342  1.11  mycroft 	return status & LPS_MASK;
    343  1.11  mycroft }
    344  1.11  mycroft 
    345  1.11  mycroft void
    346  1.11  mycroft lptout(sc)
    347  1.11  mycroft 	struct lpt_softc *sc;
    348  1.11  mycroft {
    349  1.11  mycroft 	int s;
    350  1.11  mycroft 
    351  1.11  mycroft 	if ((sc->sc_state & LPT_OPEN) == 0)
    352  1.11  mycroft 		return;
    353  1.11  mycroft 	if (sc->sc_flags & LPT_NOINTR)
    354  1.11  mycroft 		return;
    355   1.1      cgd 
    356   1.1      cgd 	/*
    357  1.11  mycroft 	 * Avoid possible hangs do to missed interrupts.
    358   1.1      cgd 	 */
    359  1.11  mycroft 	if (sc->sc_count) {
    360  1.11  mycroft 		s = spltty();
    361  1.11  mycroft 		lptintr(sc->sc_dev.dv_unit);
    362  1.11  mycroft 		splx(s);
    363   1.1      cgd 	} else {
    364  1.11  mycroft 		sc->sc_state &= ~LPT_OBUSY;
    365   1.1      cgd 		wakeup((caddr_t)sc);
    366   1.1      cgd 	}
    367  1.11  mycroft 
    368  1.11  mycroft 	timeout((timeout_t)lptout, (caddr_t)sc, STEP);
    369   1.1      cgd }
    370   1.1      cgd 
    371   1.1      cgd /*
    372  1.11  mycroft  * Close the device, and free the local line buffer.
    373   1.1      cgd  */
    374   1.1      cgd lptclose(dev, flag)
    375  1.11  mycroft 	dev_t dev;
    376   1.1      cgd 	int flag;
    377   1.1      cgd {
    378  1.11  mycroft 	int unit = LPTUNIT(dev);
    379  1.11  mycroft 	struct lpt_softc *sc = &lpt_softc[unit];
    380  1.11  mycroft 	u_short iobase = sc->sc_iobase;
    381   1.1      cgd 
    382  1.11  mycroft 	if (sc->sc_count)
    383  1.11  mycroft 		(void) pushbytes(sc);
    384   1.1      cgd 
    385  1.11  mycroft 	outb(iobase + lpt_control, LPC_NINIT);
    386   1.1      cgd 	sc->sc_state = 0;
    387  1.11  mycroft 	outb(iobase + lpt_control, LPC_NINIT);
    388   1.1      cgd 	brelse(sc->sc_inbuf);
    389  1.11  mycroft 
    390  1.11  mycroft 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
    391  1.11  mycroft 	return 0;
    392  1.11  mycroft }
    393  1.11  mycroft 
    394  1.11  mycroft int
    395  1.11  mycroft pushbytes(sc)
    396  1.11  mycroft 	struct lpt_softc *sc;
    397  1.11  mycroft {
    398  1.11  mycroft 	u_short iobase = sc->sc_iobase;
    399  1.11  mycroft 	int error;
    400  1.11  mycroft 
    401  1.11  mycroft 	if (sc->sc_flags & LPT_NOINTR) {
    402  1.11  mycroft 		int spin, tic;
    403  1.11  mycroft 		u_char control = sc->sc_control;
    404  1.11  mycroft 
    405  1.11  mycroft 		while (sc->sc_count > 0) {
    406  1.11  mycroft 			spin = 0;
    407  1.12  mycroft 			while (NOT_READY() && spin++ < sc->sc_spinmax);
    408  1.12  mycroft 			if (spin >= sc->sc_spinmax) {
    409  1.11  mycroft 				tic = 0;
    410  1.12  mycroft 				/* adapt busy-wait algorithm */
    411  1.12  mycroft 				sc->sc_spinmax++;
    412  1.11  mycroft 				while (NOT_READY()) {
    413  1.11  mycroft 					/* exponential backoff */
    414  1.11  mycroft 					tic = tic + tic + 1;
    415  1.11  mycroft 					if (tic > TIMEOUT)
    416  1.11  mycroft 						tic = TIMEOUT;
    417  1.11  mycroft 					error = tsleep((caddr_t)sc,
    418  1.11  mycroft 					    LPTPRI | PCATCH, "lptpsh", tic);
    419  1.11  mycroft 					if (error != EWOULDBLOCK)
    420  1.11  mycroft 						return error;
    421  1.11  mycroft 				}
    422  1.11  mycroft 			}
    423  1.11  mycroft 
    424  1.11  mycroft 			outb(iobase + lpt_data, *sc->sc_cp++);
    425  1.11  mycroft 			outb(iobase + lpt_control, control | LPC_STROBE);
    426  1.11  mycroft 			sc->sc_count--;
    427  1.11  mycroft 			outb(iobase + lpt_control, control);
    428  1.11  mycroft 
    429  1.11  mycroft 			/* adapt busy-wait algorithm */
    430  1.12  mycroft 			if (spin*2 < sc->sc_spinmax)
    431  1.12  mycroft 				sc->sc_spinmax--;
    432  1.11  mycroft 		}
    433  1.11  mycroft 	} else {
    434  1.11  mycroft 		int s;
    435  1.11  mycroft 
    436  1.11  mycroft 		while (sc->sc_count > 0) {
    437  1.11  mycroft 			/* if the printer is ready for a char, give it one */
    438  1.11  mycroft 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    439  1.11  mycroft 				lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
    440  1.11  mycroft 				    sc->sc_count);
    441  1.11  mycroft 				s = spltty();
    442  1.11  mycroft 				(void) lptintr(sc->sc_dev.dv_unit);
    443  1.11  mycroft 				splx(s);
    444  1.11  mycroft 			}
    445  1.11  mycroft 			if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
    446  1.11  mycroft 			    "lptwrite2", 0))
    447  1.11  mycroft 				return error;
    448  1.11  mycroft 		}
    449  1.11  mycroft 	}
    450  1.11  mycroft 	return 0;
    451   1.1      cgd }
    452   1.1      cgd 
    453   1.1      cgd /*
    454  1.11  mycroft  * Copy a line from user space to a local buffer, then call putc to get the
    455  1.11  mycroft  * chars moved to the output queue.
    456   1.1      cgd  */
    457   1.1      cgd lptwrite(dev, uio)
    458   1.1      cgd 	dev_t dev;
    459   1.1      cgd 	struct uio *uio;
    460   1.1      cgd {
    461  1.11  mycroft 	struct lpt_softc *sc = &lpt_softc[LPTUNIT(dev)];
    462  1.11  mycroft 	size_t n;
    463  1.11  mycroft 	int error = 0;
    464  1.11  mycroft 
    465  1.11  mycroft 	while (n = min(LPT_BSIZE, uio->uio_resid)) {
    466  1.11  mycroft 		uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
    467  1.11  mycroft 		sc->sc_count = n;
    468  1.11  mycroft 		error = pushbytes(sc);
    469  1.11  mycroft 		if (error) {
    470  1.11  mycroft 			/*
    471  1.11  mycroft 			 * Return accurate residual if interrupted or timed
    472  1.11  mycroft 			 * out.
    473  1.11  mycroft 			 */
    474  1.11  mycroft 			uio->uio_resid += sc->sc_count;
    475  1.11  mycroft 			sc->sc_count = 0;
    476  1.11  mycroft 			return error;
    477   1.1      cgd 		}
    478   1.1      cgd 	}
    479  1.11  mycroft 	return 0;
    480   1.1      cgd }
    481   1.1      cgd 
    482   1.1      cgd /*
    483  1.11  mycroft  * Handle printer interrupts which occur when the printer is ready to accept
    484  1.11  mycroft  * another char.
    485   1.1      cgd  */
    486  1.11  mycroft int
    487   1.1      cgd lptintr(unit)
    488  1.11  mycroft 	int unit;
    489   1.1      cgd {
    490  1.11  mycroft 	struct lpt_softc *sc = &lpt_softc[unit];
    491  1.11  mycroft 	u_short iobase = sc->sc_iobase;
    492  1.11  mycroft 	u_char control = sc->sc_control;
    493   1.1      cgd 
    494  1.11  mycroft 	if ((sc->sc_state & LPT_OPEN) == 0)
    495  1.11  mycroft 		return 0;
    496   1.6  mycroft 
    497  1.11  mycroft 	/* is printer online and ready for output */
    498  1.11  mycroft 	if (NOT_READY())
    499  1.11  mycroft 		return 0;
    500   1.6  mycroft 
    501  1.11  mycroft 	if (sc->sc_count) {
    502  1.11  mycroft 		/* send char */
    503  1.11  mycroft 		sc->sc_state |= LPT_OBUSY;
    504  1.11  mycroft #if 0
    505  1.11  mycroft 		while (sc->sc_count && !NOT_READY()) {
    506  1.11  mycroft #endif
    507  1.11  mycroft 			outb(iobase + lpt_data, *sc->sc_cp++);
    508  1.11  mycroft 			outb(iobase + lpt_control, control | LPC_STROBE);
    509  1.11  mycroft 			sc->sc_count--;
    510  1.11  mycroft 			outb(iobase + lpt_control, control);
    511  1.11  mycroft #if 0
    512   1.1      cgd 		}
    513  1.11  mycroft #endif
    514  1.11  mycroft 	} else
    515  1.11  mycroft 		sc->sc_state &= ~LPT_OBUSY;
    516   1.1      cgd 
    517  1.11  mycroft 	if (sc->sc_count == 0) {
    518   1.1      cgd 		/* none, wake up the top half to get more */
    519   1.1      cgd 		wakeup((caddr_t)sc);
    520  1.11  mycroft 	}
    521  1.11  mycroft 
    522  1.11  mycroft 	return 1;
    523   1.1      cgd }
    524   1.1      cgd 
    525   1.2      cgd int
    526  1.11  mycroft lptioctl(dev, cmd, data, flag)
    527  1.11  mycroft 	dev_t dev;
    528  1.11  mycroft 	int cmd;
    529  1.11  mycroft 	caddr_t data;
    530  1.11  mycroft 	int flag;
    531   1.2      cgd {
    532  1.11  mycroft 	int error = 0;
    533   1.2      cgd 
    534   1.2      cgd 	switch (cmd) {
    535   1.2      cgd 	default:
    536   1.2      cgd 		error = ENODEV;
    537   1.2      cgd 	}
    538   1.2      cgd 
    539  1.11  mycroft 	return error;
    540   1.2      cgd }
    541