Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.42
      1  1.42   thorpej /*	$NetBSD: lpt.c,v 1.42 1996/10/21 22:41:14 thorpej 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.38  christos #include <sys/conf.h>
     66  1.11   mycroft #include <sys/syslog.h>
     67   1.1       cgd 
     68  1.39   mycroft #include <machine/bus.h>
     69  1.37       cgd #include <machine/intr.h>
     70   1.9   mycroft 
     71  1.30       cgd #include <dev/isa/isavar.h>
     72  1.29       cgd #include <dev/isa/lptreg.h>
     73   1.1       cgd 
     74  1.11   mycroft #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     75  1.11   mycroft #define	STEP		hz/4
     76  1.11   mycroft 
     77  1.11   mycroft #define	LPTPRI		(PZERO+8)
     78  1.11   mycroft #define	LPT_BSIZE	1024
     79   1.1       cgd 
     80  1.20       cgd #if !defined(DEBUG) || !defined(notdef)
     81  1.38  christos #define LPRINTF(a)
     82   1.1       cgd #else
     83  1.41  christos #define LPRINTF		if (lptdebug) printf a
     84  1.11   mycroft int lptdebug = 1;
     85   1.1       cgd #endif
     86   1.1       cgd 
     87  1.11   mycroft struct lpt_softc {
     88  1.11   mycroft 	struct device sc_dev;
     89  1.30       cgd 	void *sc_ih;
     90   1.1       cgd 
     91  1.11   mycroft 	size_t sc_count;
     92  1.11   mycroft 	struct buf *sc_inbuf;
     93  1.11   mycroft 	u_char *sc_cp;
     94  1.12   mycroft 	int sc_spinmax;
     95  1.25   mycroft 	int sc_iobase;
     96  1.42   thorpej 	bus_space_tag_t sc_iot;
     97  1.42   thorpej 	bus_space_handle_t sc_ioh;
     98  1.25   mycroft 	int sc_irq;
     99  1.11   mycroft 	u_char sc_state;
    100  1.11   mycroft #define	LPT_OPEN	0x01	/* device is open */
    101  1.11   mycroft #define	LPT_OBUSY	0x02	/* printer is busy doing output */
    102  1.11   mycroft #define	LPT_INIT	0x04	/* waiting to initialize for open */
    103  1.11   mycroft 	u_char sc_flags;
    104  1.11   mycroft #define	LPT_AUTOLF	0x20	/* automatic LF on CR */
    105  1.11   mycroft #define	LPT_NOPRIME	0x40	/* don't prime on open */
    106  1.11   mycroft #define	LPT_NOINTR	0x80	/* do not use interrupt */
    107  1.11   mycroft 	u_char sc_control;
    108  1.21   mycroft 	u_char sc_laststatus;
    109  1.15   mycroft };
    110  1.11   mycroft 
    111  1.38  christos /* XXX does not belong here */
    112  1.38  christos cdev_decl(lpt);
    113  1.38  christos 
    114  1.25   mycroft int lptprobe __P((struct device *, void *, void *));
    115  1.25   mycroft void lptattach __P((struct device *, struct device *, void *));
    116  1.30       cgd int lptintr __P((void *));
    117   1.1       cgd 
    118  1.33   thorpej struct cfattach lpt_ca = {
    119  1.33   thorpej 	sizeof(struct lpt_softc), lptprobe, lptattach
    120  1.33   thorpej };
    121  1.33   thorpej 
    122  1.33   thorpej struct cfdriver lpt_cd = {
    123  1.33   thorpej 	NULL, "lpt", DV_TTY
    124   1.1       cgd };
    125   1.1       cgd 
    126  1.11   mycroft #define	LPTUNIT(s)	(minor(s) & 0x1f)
    127  1.11   mycroft #define	LPTFLAGS(s)	(minor(s) & 0xe0)
    128   1.1       cgd 
    129  1.11   mycroft #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
    130  1.11   mycroft #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
    131  1.42   thorpej #define	NOT_READY()	((bus_space_read_1(iot, ioh, lpt_status) ^ LPS_INVERT) & LPS_MASK)
    132  1.42   thorpej #define	NOT_READY_ERR()	not_ready(bus_space_read_1(iot, ioh, lpt_status), sc)
    133  1.25   mycroft static int not_ready __P((u_char, struct lpt_softc *));
    134  1.11   mycroft 
    135  1.25   mycroft static void lptwakeup __P((void *arg));
    136  1.11   mycroft static int pushbytes __P((struct lpt_softc *));
    137   1.1       cgd 
    138  1.42   thorpej int	lpt_port_test __P((bus_space_tag_t, bus_space_handle_t, bus_addr_t,
    139  1.42   thorpej 	    bus_size_t, u_char, u_char));
    140  1.34       cgd 
    141   1.2       cgd /*
    142  1.11   mycroft  * Internal routine to lptprobe to do port tests of one byte value.
    143   1.2       cgd  */
    144   1.2       cgd int
    145  1.42   thorpej lpt_port_test(iot, ioh, base, off, data, mask)
    146  1.42   thorpej 	bus_space_tag_t iot;
    147  1.42   thorpej 	bus_space_handle_t ioh;
    148  1.42   thorpej 	bus_addr_t base;
    149  1.42   thorpej 	bus_size_t off;
    150  1.11   mycroft 	u_char data, mask;
    151  1.11   mycroft {
    152  1.11   mycroft 	int timeout;
    153  1.11   mycroft 	u_char temp;
    154   1.2       cgd 
    155  1.11   mycroft 	data &= mask;
    156  1.42   thorpej 	bus_space_write_1(iot, ioh, off, data);
    157  1.11   mycroft 	timeout = 1000;
    158  1.11   mycroft 	do {
    159  1.14   mycroft 		delay(10);
    160  1.42   thorpej 		temp = bus_space_read_1(iot, ioh, off) & mask;
    161  1.11   mycroft 	} while (temp != data && --timeout);
    162  1.38  christos 	LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", base + off,
    163  1.38  christos 	    data, temp, timeout));
    164   1.2       cgd 	return (temp == data);
    165  1.11   mycroft }
    166   1.2       cgd 
    167   1.2       cgd /*
    168   1.2       cgd  * Logic:
    169   1.2       cgd  *	1) You should be able to write to and read back the same value
    170   1.2       cgd  *	   to the data port.  Do an alternating zeros, alternating ones,
    171   1.2       cgd  *	   walking zero, and walking one test to check for stuck bits.
    172   1.2       cgd  *
    173   1.2       cgd  *	2) You should be able to write to and read back the same value
    174   1.2       cgd  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    175   1.2       cgd  *	   per the IBM PC technical reference manauls and different boards
    176   1.2       cgd  *	   do different things with them.  Do an alternating zeros, alternating
    177   1.2       cgd  *	   ones, walking zero, and walking one test to check for stuck bits.
    178   1.2       cgd  *
    179   1.2       cgd  *	   Some printers drag the strobe line down when the are powered off
    180   1.2       cgd  * 	   so this bit has been masked out of the control port test.
    181   1.2       cgd  *
    182   1.2       cgd  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    183   1.2       cgd  *	   don't know at this point, if that becomes a problem these bits
    184   1.2       cgd  *	   should be turned off in the mask byte for the control port test.
    185   1.2       cgd  *
    186   1.2       cgd  *	3) Set the data and control ports to a value of 0
    187   1.2       cgd  */
    188   1.2       cgd int
    189  1.25   mycroft lptprobe(parent, match, aux)
    190  1.25   mycroft 	struct device *parent;
    191  1.25   mycroft 	void *match, *aux;
    192  1.11   mycroft {
    193  1.15   mycroft 	struct isa_attach_args *ia = aux;
    194  1.42   thorpej 	bus_space_tag_t iot;
    195  1.42   thorpej 	bus_space_handle_t ioh;
    196  1.32       cgd 	u_long base;
    197  1.11   mycroft 	u_char mask, data;
    198  1.32       cgd 	int i, rv;
    199   1.2       cgd 
    200  1.11   mycroft #ifdef DEBUG
    201  1.41  christos #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
    202  1.32       cgd 		    goto out;} while (0)
    203  1.11   mycroft #else
    204  1.32       cgd #define	ABORT	goto out
    205  1.11   mycroft #endif
    206   1.2       cgd 
    207  1.42   thorpej 	iot = ia->ia_iot;
    208  1.32       cgd 	base = ia->ia_iobase;
    209  1.42   thorpej 	if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
    210  1.32       cgd 		return 0;
    211  1.32       cgd 
    212  1.32       cgd 	rv = 0;
    213   1.2       cgd 	mask = 0xff;
    214  1.11   mycroft 
    215  1.28   mycroft 	data = 0x55;				/* Alternating zeros */
    216  1.42   thorpej 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    217  1.28   mycroft 		ABORT;
    218  1.28   mycroft 
    219  1.28   mycroft 	data = 0xaa;				/* Alternating ones */
    220  1.42   thorpej 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    221  1.28   mycroft 		ABORT;
    222  1.28   mycroft 
    223  1.28   mycroft 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    224  1.28   mycroft 		data = ~(1 << i);
    225  1.42   thorpej 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    226  1.11   mycroft 			ABORT;
    227  1.28   mycroft 	}
    228   1.2       cgd 
    229  1.28   mycroft 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    230  1.28   mycroft 		data = (1 << i);
    231  1.42   thorpej 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    232  1.11   mycroft 			ABORT;
    233  1.28   mycroft 	}
    234   1.2       cgd 
    235  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_data, 0);
    236  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_control, 0);
    237  1.11   mycroft 
    238  1.15   mycroft 	ia->ia_iosize = LPT_NPORTS;
    239  1.15   mycroft 	ia->ia_msize = 0;
    240  1.32       cgd 
    241  1.32       cgd 	rv = 1;
    242  1.32       cgd 
    243  1.32       cgd out:
    244  1.42   thorpej 	bus_space_unmap(iot, ioh, LPT_NPORTS);
    245  1.32       cgd 	return rv;
    246  1.11   mycroft }
    247   1.1       cgd 
    248  1.15   mycroft void
    249  1.15   mycroft lptattach(parent, self, aux)
    250  1.15   mycroft 	struct device *parent, *self;
    251  1.15   mycroft 	void *aux;
    252   1.1       cgd {
    253  1.15   mycroft 	struct lpt_softc *sc = (void *)self;
    254  1.15   mycroft 	struct isa_attach_args *ia = aux;
    255  1.42   thorpej 	bus_space_tag_t iot;
    256  1.42   thorpej 	bus_space_handle_t ioh;
    257  1.15   mycroft 
    258  1.25   mycroft 	if (ia->ia_irq != IRQUNK)
    259  1.41  christos 		printf("\n");
    260  1.15   mycroft 	else
    261  1.41  christos 		printf(": polled\n");
    262   1.1       cgd 
    263  1.32       cgd 	sc->sc_iobase = ia->ia_iobase;
    264  1.16   mycroft 	sc->sc_irq = ia->ia_irq;
    265   1.6   mycroft 	sc->sc_state = 0;
    266  1.32       cgd 
    267  1.42   thorpej 	iot = sc->sc_iot = ia->ia_iot;
    268  1.42   thorpej 	if (bus_space_map(iot, sc->sc_iobase, LPT_NPORTS, 0, &ioh))
    269  1.32       cgd 		panic("lptattach: couldn't map I/O ports");
    270  1.32       cgd 	sc->sc_ioh = ioh;
    271  1.32       cgd 
    272  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    273  1.16   mycroft 
    274  1.30       cgd 	if (ia->ia_irq != IRQUNK)
    275  1.37       cgd 		sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    276  1.37       cgd 		    IPL_TTY, lptintr, sc);
    277   1.1       cgd }
    278   1.1       cgd 
    279   1.1       cgd /*
    280  1.11   mycroft  * Reset the printer, then wait until it's selected and not busy.
    281   1.1       cgd  */
    282  1.11   mycroft int
    283  1.38  christos lptopen(dev, flag, mode, p)
    284   1.1       cgd 	dev_t dev;
    285   1.1       cgd 	int flag;
    286  1.38  christos 	int mode;
    287  1.38  christos 	struct proc *p;
    288   1.1       cgd {
    289  1.11   mycroft 	int unit = LPTUNIT(dev);
    290  1.11   mycroft 	u_char flags = LPTFLAGS(dev);
    291  1.11   mycroft 	struct lpt_softc *sc;
    292  1.42   thorpej 	bus_space_tag_t iot;
    293  1.42   thorpej 	bus_space_handle_t ioh;
    294  1.11   mycroft 	u_char control;
    295  1.11   mycroft 	int error;
    296  1.11   mycroft 	int spin;
    297  1.11   mycroft 
    298  1.33   thorpej 	if (unit >= lpt_cd.cd_ndevs)
    299  1.11   mycroft 		return ENXIO;
    300  1.33   thorpej 	sc = lpt_cd.cd_devs[unit];
    301  1.15   mycroft 	if (!sc)
    302  1.11   mycroft 		return ENXIO;
    303  1.11   mycroft 
    304  1.25   mycroft 	if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
    305  1.11   mycroft 		return ENXIO;
    306  1.11   mycroft 
    307  1.11   mycroft #ifdef DIAGNOSTIC
    308  1.11   mycroft 	if (sc->sc_state)
    309  1.41  christos 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
    310  1.11   mycroft 		    sc->sc_state);
    311  1.11   mycroft #endif
    312  1.11   mycroft 
    313  1.11   mycroft 	if (sc->sc_state)
    314  1.11   mycroft 		return EBUSY;
    315   1.1       cgd 
    316  1.11   mycroft 	sc->sc_state = LPT_INIT;
    317  1.11   mycroft 	sc->sc_flags = flags;
    318  1.38  christos 	LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags));
    319  1.42   thorpej 	iot = sc->sc_iot;
    320  1.32       cgd 	ioh = sc->sc_ioh;
    321  1.11   mycroft 
    322  1.11   mycroft 	if ((flags & LPT_NOPRIME) == 0) {
    323  1.11   mycroft 		/* assert INIT for 100 usec to start up printer */
    324  1.42   thorpej 		bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
    325  1.14   mycroft 		delay(100);
    326   1.1       cgd 	}
    327  1.11   mycroft 
    328  1.11   mycroft 	control = LPC_SELECT | LPC_NINIT;
    329  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_control, control);
    330   1.1       cgd 
    331   1.1       cgd 	/* wait till ready (printer running diagnostics) */
    332  1.25   mycroft 	for (spin = 0; NOT_READY_ERR(); spin += STEP) {
    333  1.11   mycroft 		if (spin >= TIMEOUT) {
    334   1.1       cgd 			sc->sc_state = 0;
    335  1.11   mycroft 			return EBUSY;
    336   1.1       cgd 		}
    337   1.1       cgd 
    338   1.1       cgd 		/* wait 1/4 second, give up if we get a signal */
    339  1.38  christos 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
    340  1.38  christos 		if (error != EWOULDBLOCK) {
    341   1.1       cgd 			sc->sc_state = 0;
    342  1.11   mycroft 			return error;
    343   1.1       cgd 		}
    344   1.1       cgd 	}
    345   1.1       cgd 
    346  1.11   mycroft 	if ((flags & LPT_NOINTR) == 0)
    347  1.11   mycroft 		control |= LPC_IENABLE;
    348  1.11   mycroft 	if (flags & LPT_AUTOLF)
    349  1.11   mycroft 		control |= LPC_AUTOLF;
    350  1.11   mycroft 	sc->sc_control = control;
    351  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_control, control);
    352  1.11   mycroft 
    353  1.11   mycroft 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    354  1.11   mycroft 	sc->sc_count = 0;
    355  1.11   mycroft 	sc->sc_state = LPT_OPEN;
    356  1.25   mycroft 
    357  1.25   mycroft 	if ((sc->sc_flags & LPT_NOINTR) == 0)
    358  1.25   mycroft 		lptwakeup(sc);
    359  1.11   mycroft 
    360  1.38  christos 	LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
    361  1.11   mycroft 	return 0;
    362   1.1       cgd }
    363   1.1       cgd 
    364  1.11   mycroft int
    365  1.25   mycroft not_ready(status, sc)
    366  1.11   mycroft 	u_char status;
    367   1.1       cgd 	struct lpt_softc *sc;
    368  1.11   mycroft {
    369  1.21   mycroft 	u_char new;
    370  1.21   mycroft 
    371  1.25   mycroft 	status = (status ^ LPS_INVERT) & LPS_MASK;
    372  1.21   mycroft 	new = status & ~sc->sc_laststatus;
    373  1.21   mycroft 	sc->sc_laststatus = status;
    374   1.1       cgd 
    375  1.27   mycroft 	if (new & LPS_SELECT)
    376  1.27   mycroft 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
    377  1.27   mycroft 	else if (new & LPS_NOPAPER)
    378  1.11   mycroft 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
    379  1.21   mycroft 	else if (new & LPS_NERR)
    380  1.11   mycroft 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
    381   1.1       cgd 
    382  1.25   mycroft 	return status;
    383  1.11   mycroft }
    384  1.11   mycroft 
    385  1.11   mycroft void
    386  1.25   mycroft lptwakeup(arg)
    387  1.18       cgd 	void *arg;
    388  1.18       cgd {
    389  1.25   mycroft 	struct lpt_softc *sc = arg;
    390  1.11   mycroft 	int s;
    391  1.11   mycroft 
    392  1.13   mycroft 	s = spltty();
    393  1.16   mycroft 	lptintr(sc);
    394  1.13   mycroft 	splx(s);
    395  1.11   mycroft 
    396  1.25   mycroft 	timeout(lptwakeup, sc, STEP);
    397   1.1       cgd }
    398   1.1       cgd 
    399   1.1       cgd /*
    400  1.11   mycroft  * Close the device, and free the local line buffer.
    401   1.1       cgd  */
    402  1.36   mycroft int
    403  1.38  christos lptclose(dev, flag, mode, p)
    404  1.11   mycroft 	dev_t dev;
    405   1.1       cgd 	int flag;
    406  1.38  christos 	int mode;
    407  1.38  christos 	struct proc *p;
    408   1.1       cgd {
    409  1.11   mycroft 	int unit = LPTUNIT(dev);
    410  1.33   thorpej 	struct lpt_softc *sc = lpt_cd.cd_devs[unit];
    411  1.42   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    412  1.42   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    413   1.1       cgd 
    414  1.11   mycroft 	if (sc->sc_count)
    415  1.11   mycroft 		(void) pushbytes(sc);
    416   1.1       cgd 
    417  1.25   mycroft 	if ((sc->sc_flags & LPT_NOINTR) == 0)
    418  1.25   mycroft 		untimeout(lptwakeup, sc);
    419  1.25   mycroft 
    420  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    421   1.1       cgd 	sc->sc_state = 0;
    422  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    423   1.1       cgd 	brelse(sc->sc_inbuf);
    424  1.11   mycroft 
    425  1.38  christos 	LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
    426  1.11   mycroft 	return 0;
    427  1.11   mycroft }
    428  1.11   mycroft 
    429  1.11   mycroft int
    430  1.11   mycroft pushbytes(sc)
    431  1.11   mycroft 	struct lpt_softc *sc;
    432  1.11   mycroft {
    433  1.42   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    434  1.42   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    435  1.11   mycroft 	int error;
    436  1.11   mycroft 
    437  1.11   mycroft 	if (sc->sc_flags & LPT_NOINTR) {
    438  1.11   mycroft 		int spin, tic;
    439  1.11   mycroft 		u_char control = sc->sc_control;
    440  1.11   mycroft 
    441  1.11   mycroft 		while (sc->sc_count > 0) {
    442  1.11   mycroft 			spin = 0;
    443  1.25   mycroft 			while (NOT_READY()) {
    444  1.25   mycroft 				if (++spin < sc->sc_spinmax)
    445  1.25   mycroft 					continue;
    446  1.11   mycroft 				tic = 0;
    447  1.12   mycroft 				/* adapt busy-wait algorithm */
    448  1.12   mycroft 				sc->sc_spinmax++;
    449  1.25   mycroft 				while (NOT_READY_ERR()) {
    450  1.11   mycroft 					/* exponential backoff */
    451  1.11   mycroft 					tic = tic + tic + 1;
    452  1.11   mycroft 					if (tic > TIMEOUT)
    453  1.11   mycroft 						tic = TIMEOUT;
    454  1.11   mycroft 					error = tsleep((caddr_t)sc,
    455  1.11   mycroft 					    LPTPRI | PCATCH, "lptpsh", tic);
    456  1.11   mycroft 					if (error != EWOULDBLOCK)
    457  1.11   mycroft 						return error;
    458  1.11   mycroft 				}
    459  1.25   mycroft 				break;
    460  1.11   mycroft 			}
    461  1.11   mycroft 
    462  1.42   thorpej 			bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
    463  1.42   thorpej 			bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
    464  1.11   mycroft 			sc->sc_count--;
    465  1.42   thorpej 			bus_space_write_1(iot, ioh, lpt_control, control);
    466  1.11   mycroft 
    467  1.11   mycroft 			/* adapt busy-wait algorithm */
    468  1.25   mycroft 			if (spin*2 + 16 < sc->sc_spinmax)
    469  1.12   mycroft 				sc->sc_spinmax--;
    470  1.11   mycroft 		}
    471  1.11   mycroft 	} else {
    472  1.11   mycroft 		int s;
    473  1.11   mycroft 
    474  1.11   mycroft 		while (sc->sc_count > 0) {
    475  1.11   mycroft 			/* if the printer is ready for a char, give it one */
    476  1.11   mycroft 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    477  1.38  christos 				LPRINTF(("%s: write %d\n", sc->sc_dev.dv_xname,
    478  1.38  christos 				    sc->sc_count));
    479  1.11   mycroft 				s = spltty();
    480  1.16   mycroft 				(void) lptintr(sc);
    481  1.11   mycroft 				splx(s);
    482  1.11   mycroft 			}
    483  1.38  christos 			error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
    484  1.38  christos 			    "lptwrite2", 0);
    485  1.38  christos 			if (error)
    486  1.11   mycroft 				return error;
    487  1.11   mycroft 		}
    488  1.11   mycroft 	}
    489  1.11   mycroft 	return 0;
    490   1.1       cgd }
    491   1.1       cgd 
    492   1.1       cgd /*
    493  1.11   mycroft  * Copy a line from user space to a local buffer, then call putc to get the
    494  1.11   mycroft  * chars moved to the output queue.
    495   1.1       cgd  */
    496  1.36   mycroft int
    497  1.38  christos lptwrite(dev, uio, flags)
    498   1.1       cgd 	dev_t dev;
    499   1.1       cgd 	struct uio *uio;
    500  1.38  christos 	int flags;
    501   1.1       cgd {
    502  1.33   thorpej 	struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)];
    503  1.11   mycroft 	size_t n;
    504  1.11   mycroft 	int error = 0;
    505  1.11   mycroft 
    506  1.38  christos 	while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
    507  1.22   mycroft 		uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
    508  1.11   mycroft 		sc->sc_count = n;
    509  1.11   mycroft 		error = pushbytes(sc);
    510  1.11   mycroft 		if (error) {
    511  1.11   mycroft 			/*
    512  1.11   mycroft 			 * Return accurate residual if interrupted or timed
    513  1.11   mycroft 			 * out.
    514  1.11   mycroft 			 */
    515  1.11   mycroft 			uio->uio_resid += sc->sc_count;
    516  1.11   mycroft 			sc->sc_count = 0;
    517  1.11   mycroft 			return error;
    518   1.1       cgd 		}
    519   1.1       cgd 	}
    520  1.11   mycroft 	return 0;
    521   1.1       cgd }
    522   1.1       cgd 
    523   1.1       cgd /*
    524  1.11   mycroft  * Handle printer interrupts which occur when the printer is ready to accept
    525  1.11   mycroft  * another char.
    526   1.1       cgd  */
    527  1.11   mycroft int
    528  1.30       cgd lptintr(arg)
    529  1.30       cgd 	void *arg;
    530   1.1       cgd {
    531  1.30       cgd 	struct lpt_softc *sc = arg;
    532  1.42   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    533  1.42   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    534   1.1       cgd 
    535  1.13   mycroft #if 0
    536  1.11   mycroft 	if ((sc->sc_state & LPT_OPEN) == 0)
    537  1.11   mycroft 		return 0;
    538  1.13   mycroft #endif
    539   1.6   mycroft 
    540  1.11   mycroft 	/* is printer online and ready for output */
    541  1.25   mycroft 	if (NOT_READY() && NOT_READY_ERR())
    542  1.11   mycroft 		return 0;
    543   1.6   mycroft 
    544  1.11   mycroft 	if (sc->sc_count) {
    545  1.13   mycroft 		u_char control = sc->sc_control;
    546  1.11   mycroft 		/* send char */
    547  1.42   thorpej 		bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
    548  1.42   thorpej 		bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
    549  1.13   mycroft 		sc->sc_count--;
    550  1.42   thorpej 		bus_space_write_1(iot, ioh, lpt_control, control);
    551  1.11   mycroft 		sc->sc_state |= LPT_OBUSY;
    552  1.11   mycroft 	} else
    553  1.11   mycroft 		sc->sc_state &= ~LPT_OBUSY;
    554   1.1       cgd 
    555  1.11   mycroft 	if (sc->sc_count == 0) {
    556   1.1       cgd 		/* none, wake up the top half to get more */
    557   1.1       cgd 		wakeup((caddr_t)sc);
    558  1.11   mycroft 	}
    559  1.11   mycroft 
    560  1.11   mycroft 	return 1;
    561   1.1       cgd }
    562   1.1       cgd 
    563   1.2       cgd int
    564  1.38  christos lptioctl(dev, cmd, data, flag, p)
    565  1.11   mycroft 	dev_t dev;
    566  1.24       cgd 	u_long cmd;
    567  1.11   mycroft 	caddr_t data;
    568  1.11   mycroft 	int flag;
    569  1.38  christos 	struct proc *p;
    570   1.2       cgd {
    571  1.11   mycroft 	int error = 0;
    572   1.2       cgd 
    573   1.2       cgd 	switch (cmd) {
    574   1.2       cgd 	default:
    575   1.2       cgd 		error = ENODEV;
    576   1.2       cgd 	}
    577   1.2       cgd 
    578  1.11   mycroft 	return error;
    579   1.2       cgd }
    580