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