Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.7.4.10
      1       1.1      cgd /*
      2   1.7.4.9  mycroft  * Copyright (c) 1993 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.7.4.10  mycroft  *	$Id: lpt.c,v 1.7.4.10 1993/10/16 06:39:42 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 "param.h"
     57       1.2      cgd #include "systm.h"
     58       1.2      cgd #include "proc.h"
     59       1.1      cgd #include "user.h"
     60       1.1      cgd #include "buf.h"
     61       1.1      cgd #include "kernel.h"
     62       1.1      cgd #include "ioctl.h"
     63       1.1      cgd #include "uio.h"
     64   1.7.4.2  mycroft #include "sys/device.h"
     65   1.7.4.8  mycroft 
     66   1.7.4.5  mycroft #include "machine/cpu.h"
     67   1.7.4.8  mycroft #include "machine/pio.h"
     68       1.1      cgd 
     69   1.7.4.2  mycroft #include "i386/isa/isavar.h"
     70   1.7.4.4  mycroft #include "i386/isa/icu.h"
     71       1.1      cgd #include "i386/isa/lptreg.h"
     72       1.1      cgd 
     73   1.7.4.2  mycroft #define	TIMEOUT		hz*16	/* wait up to 4 seconds for a ready */
     74   1.7.4.2  mycroft #define	STEP		hz/4
     75   1.7.4.2  mycroft 
     76   1.7.4.2  mycroft #define	MAX_SPIN	255
     77       1.1      cgd 
     78   1.7.4.2  mycroft #define	LPTPRI		(PZERO+8)
     79   1.7.4.2  mycroft #define	LPT_BSIZE	1024
     80   1.7.4.2  mycroft 
     81       1.1      cgd #ifndef DEBUG
     82       1.1      cgd #define lprintf
     83       1.1      cgd #else
     84   1.7.4.2  mycroft #define lprintf		if (lptdebug) printf
     85   1.7.4.2  mycroft int lptdebug = 1;
     86       1.1      cgd #endif
     87       1.1      cgd 
     88       1.1      cgd struct lpt_softc {
     89   1.7.4.3  mycroft 	struct	device sc_dev;
     90   1.7.4.3  mycroft 	struct	isadev sc_id;
     91   1.7.4.3  mycroft 	struct	intrhand sc_ih;
     92   1.7.4.3  mycroft 
     93   1.7.4.2  mycroft 	size_t	sc_count;
     94       1.1      cgd 	struct	buf *sc_inbuf;
     95   1.7.4.2  mycroft 	u_char	*sc_cp ;
     96   1.7.4.2  mycroft 	u_short	sc_iobase;
     97   1.7.4.2  mycroft 	u_short	sc_irq;
     98   1.7.4.2  mycroft 	u_char	sc_state;
     99       1.1      cgd /* bits for state */
    100   1.7.4.2  mycroft #define	LPT_OPEN	0x01	/* device is open */
    101   1.7.4.2  mycroft #define	LPT_OBUSY	0x02	/* printer is busy doing output */
    102   1.7.4.2  mycroft #define LPT_INIT	0x04	/* waiting to initialize for open */
    103   1.7.4.2  mycroft 	u_char	sc_flags;
    104   1.7.4.2  mycroft #define	LPT_AUTOLF	0x20	/* automatic LF on CR */
    105   1.7.4.2  mycroft #define	LPT_NOPRIME	0x40	/* don't prime on open */
    106   1.7.4.2  mycroft #define	LPT_NOINTR	0x80	/* do not use interrupt */
    107   1.7.4.2  mycroft 	u_char	sc_control;
    108   1.7.4.2  mycroft 	u_char	sc_smax;
    109   1.7.4.2  mycroft };
    110       1.1      cgd 
    111   1.7.4.2  mycroft static int lptprobe __P((struct device *, struct cfdata *, void *));
    112   1.7.4.2  mycroft static void lptforceintr __P((void *));
    113   1.7.4.2  mycroft static void lptattach __P((struct device *, struct device *, void *));
    114   1.7.4.2  mycroft static int lptintr __P((void *));
    115   1.7.4.2  mycroft 
    116   1.7.4.2  mycroft struct	cfdriver lptcd =
    117  1.7.4.10  mycroft { NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc) };
    118   1.7.4.2  mycroft 
    119   1.7.4.2  mycroft #define	LPTUNIT(s)	((s)&0x1f)
    120   1.7.4.2  mycroft #define	LPTFLAGS(s)	((s)&0xe0)
    121   1.7.4.2  mycroft 
    122   1.7.4.6  mycroft #define	LPS_INVERT	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK)
    123   1.7.4.6  mycroft #define	LPS_MASK	(LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER)
    124   1.7.4.2  mycroft #define	NOT_READY()	((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK)
    125   1.7.4.2  mycroft 
    126   1.7.4.2  mycroft static void lptout __P((struct lpt_softc *));
    127   1.7.4.2  mycroft static int pushbytes __P((struct lpt_softc *));
    128   1.7.4.2  mycroft 
    129       1.2      cgd /*
    130       1.2      cgd  * Internal routine to lptprobe to do port tests of one byte value
    131       1.2      cgd  */
    132       1.2      cgd int
    133   1.7.4.1  mycroft lpt_port_test(port, data, mask)
    134   1.7.4.1  mycroft 	u_short port;
    135   1.7.4.1  mycroft 	u_char data, mask;
    136   1.7.4.1  mycroft {
    137   1.7.4.2  mycroft 	int	timeout;
    138   1.7.4.2  mycroft 	u_char	temp;
    139       1.2      cgd 
    140   1.7.4.2  mycroft 	data &= mask;
    141       1.2      cgd 	outb(port, data);
    142       1.5      cgd 	timeout = 100;
    143   1.7.4.2  mycroft 	do {
    144       1.5      cgd 		temp = inb(port) & mask;
    145   1.7.4.2  mycroft 	} while (temp != data && --timeout);
    146   1.7.4.2  mycroft 	lprintf("lpt: port=0x%x out=0x%x in=0x%x\n", port, data, temp);
    147       1.2      cgd 	return (temp == data);
    148   1.7.4.1  mycroft }
    149       1.2      cgd 
    150       1.2      cgd /*
    151       1.2      cgd  * Logic:
    152       1.2      cgd  *	1) You should be able to write to and read back the same value
    153       1.2      cgd  *	   to the data port.  Do an alternating zeros, alternating ones,
    154       1.2      cgd  *	   walking zero, and walking one test to check for stuck bits.
    155       1.2      cgd  *
    156       1.2      cgd  *	2) You should be able to write to and read back the same value
    157       1.2      cgd  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    158       1.2      cgd  *	   per the IBM PC technical reference manauls and different boards
    159       1.2      cgd  *	   do different things with them.  Do an alternating zeros, alternating
    160       1.2      cgd  *	   ones, walking zero, and walking one test to check for stuck bits.
    161       1.2      cgd  *
    162       1.2      cgd  *	   Some printers drag the strobe line down when the are powered off
    163       1.2      cgd  * 	   so this bit has been masked out of the control port test.
    164       1.2      cgd  *
    165       1.2      cgd  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    166       1.2      cgd  *	   don't know at this point, if that becomes a problem these bits
    167       1.2      cgd  *	   should be turned off in the mask byte for the control port test.
    168       1.2      cgd  *
    169       1.2      cgd  *	3) Set the data and control ports to a value of 0
    170       1.2      cgd  */
    171       1.2      cgd 
    172   1.7.4.2  mycroft static int
    173   1.7.4.2  mycroft lptprobe(parent, cf, aux)
    174   1.7.4.2  mycroft 	struct device *parent;
    175   1.7.4.2  mycroft 	struct cfdata *cf;
    176   1.7.4.2  mycroft 	void *aux;
    177   1.7.4.1  mycroft {
    178   1.7.4.2  mycroft 	struct	isa_attach_args *ia = aux;
    179   1.7.4.2  mycroft 	u_short	iobase = ia->ia_iobase;
    180   1.7.4.2  mycroft 	u_short	port = iobase + lpt_data;
    181   1.7.4.2  mycroft 	u_char	mask = 0xff;
    182       1.2      cgd 	u_char	data;
    183       1.2      cgd 	int	i;
    184       1.2      cgd 
    185   1.7.4.4  mycroft 	if (iobase == IOBASEUNK)
    186   1.7.4.4  mycroft 		return 0;
    187   1.7.4.4  mycroft 
    188   1.7.4.2  mycroft 	for (;;) {
    189       1.2      cgd 		data = 0x55;				/* Alternating zeros */
    190   1.7.4.2  mycroft 		if (!lpt_port_test(port, data, mask))
    191   1.7.4.2  mycroft 			return 0;
    192       1.2      cgd 
    193       1.2      cgd 		data = 0xaa;				/* Alternating ones */
    194   1.7.4.2  mycroft 		if (!lpt_port_test(port, data, mask))
    195   1.7.4.2  mycroft 			return 0;
    196       1.2      cgd 
    197   1.7.4.2  mycroft 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    198       1.2      cgd 			data = ~(1 << i);
    199   1.7.4.2  mycroft 			if (!lpt_port_test(port, data, mask))
    200   1.7.4.2  mycroft 				return 0;
    201   1.7.4.2  mycroft 		}
    202       1.2      cgd 
    203   1.7.4.2  mycroft 		for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    204       1.2      cgd 			data = (1 << i);
    205   1.7.4.2  mycroft 			if (!lpt_port_test(port, data, mask))
    206   1.7.4.2  mycroft 				return 0;
    207   1.7.4.2  mycroft 		}
    208       1.2      cgd 
    209   1.7.4.2  mycroft 		if (port == iobase + lpt_data) {
    210   1.7.4.2  mycroft 			port = iobase + lpt_control;
    211       1.2      cgd 			mask = 0x1e;
    212   1.7.4.2  mycroft 		} else
    213   1.7.4.2  mycroft 			break;
    214   1.7.4.1  mycroft 	}
    215   1.7.4.2  mycroft 	outb(iobase + lpt_data, 0);
    216   1.7.4.2  mycroft 	outb(iobase + lpt_control, 0);
    217   1.7.4.2  mycroft 
    218   1.7.4.4  mycroft 	if (ia->ia_irq == IRQUNK)
    219   1.7.4.4  mycroft 		ia->ia_irq = isa_discoverintr(lptforceintr, aux);
    220   1.7.4.4  mycroft 	/* okay if we don't have an irq; will force polling */
    221   1.7.4.2  mycroft 
    222   1.7.4.2  mycroft 	ia->ia_iosize = LPT_NPORTS;
    223   1.7.4.2  mycroft 	ia->ia_drq = DRQUNK;
    224   1.7.4.2  mycroft 	ia->ia_msize = 0;
    225   1.7.4.2  mycroft 	return 1;
    226   1.7.4.1  mycroft }
    227       1.1      cgd 
    228   1.7.4.2  mycroft static void
    229   1.7.4.2  mycroft lptattach(parent, self, aux)
    230   1.7.4.2  mycroft 	struct device *parent, *self;
    231   1.7.4.2  mycroft 	void *aux;
    232       1.1      cgd {
    233   1.7.4.2  mycroft 	struct	isa_attach_args *ia = aux;
    234   1.7.4.2  mycroft 	struct	lpt_softc *sc = (struct lpt_softc *)self;
    235   1.7.4.2  mycroft 	u_short	iobase = ia->ia_iobase;
    236   1.7.4.2  mycroft 	u_short	irq = ia->ia_irq;
    237       1.1      cgd 
    238   1.7.4.2  mycroft 	sc->sc_iobase = iobase;
    239   1.7.4.2  mycroft 	sc->sc_irq = irq;
    240       1.6  mycroft 	sc->sc_state = 0;
    241   1.7.4.2  mycroft 	outb(iobase + lpt_control, LPC_NINIT);
    242   1.7.4.2  mycroft 
    243   1.7.4.4  mycroft 	printf(": %sparallel port\n", irq == IRQNONE ? "polled " : "");
    244   1.7.4.4  mycroft 	isa_establish(&sc->sc_id, &sc->sc_dev);
    245   1.7.4.4  mycroft 
    246   1.7.4.4  mycroft 	if (irq != IRQNONE) {
    247   1.7.4.4  mycroft 		sc->sc_ih.ih_fun = lptintr;
    248   1.7.4.4  mycroft 		sc->sc_ih.ih_arg = sc;
    249   1.7.4.4  mycroft 		intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
    250   1.7.4.4  mycroft 	}
    251       1.1      cgd }
    252       1.1      cgd 
    253       1.1      cgd /*
    254       1.1      cgd  * lptopen -- reset the printer, then wait until it's selected and not busy.
    255       1.1      cgd  */
    256   1.7.4.1  mycroft int
    257       1.1      cgd lptopen(dev, flag)
    258       1.1      cgd 	dev_t dev;
    259       1.1      cgd 	int flag;
    260       1.1      cgd {
    261   1.7.4.2  mycroft 	int	unit = LPTUNIT(minor(dev));
    262   1.7.4.2  mycroft 	u_char	flags = LPTFLAGS(minor(dev));
    263   1.7.4.2  mycroft 	struct	lpt_softc *sc;
    264   1.7.4.2  mycroft 	u_short	iobase;
    265   1.7.4.2  mycroft 	u_char	control;
    266   1.7.4.2  mycroft 	int	error;
    267   1.7.4.6  mycroft 	int	spin;
    268   1.7.4.1  mycroft 
    269   1.7.4.2  mycroft 	if (unit >= lptcd.cd_ndevs)
    270   1.7.4.1  mycroft 		return ENXIO;
    271   1.7.4.3  mycroft 	sc = lptcd.cd_devs[unit];
    272   1.7.4.2  mycroft 	if (!sc)
    273   1.7.4.2  mycroft 		return ENXIO;
    274       1.1      cgd 
    275   1.7.4.4  mycroft 	if (sc->sc_irq == IRQNONE && (flags & LPT_NOINTR) == 0)
    276   1.7.4.2  mycroft 		return ENXIO;
    277   1.7.4.2  mycroft 
    278   1.7.4.2  mycroft 	if (sc->sc_state)
    279   1.7.4.2  mycroft 		return EBUSY;
    280   1.7.4.2  mycroft 
    281   1.7.4.2  mycroft #ifdef DIAGNOSTIC
    282   1.7.4.2  mycroft 	if (sc->sc_state)
    283  1.7.4.10  mycroft 		printf("%s: state=0x%x not zero\n", sc->sc_dev.dv_xname,
    284  1.7.4.10  mycroft 		       sc->sc_state);
    285   1.7.4.2  mycroft #endif
    286   1.7.4.2  mycroft 
    287   1.7.4.2  mycroft 	sc->sc_state = LPT_INIT;
    288   1.7.4.2  mycroft 
    289   1.7.4.2  mycroft 	sc->sc_flags = flags;
    290  1.7.4.10  mycroft 	lprintf("%s: open flags=0x%x\n", sc->sc_dev.dv_xname, flags);
    291   1.7.4.2  mycroft 	iobase = sc->sc_iobase;
    292       1.1      cgd 
    293   1.7.4.6  mycroft 	if ((flags & LPT_NOPRIME) == 0) {
    294   1.7.4.6  mycroft 		/* assert INIT for 100 usec to start up printer */
    295   1.7.4.6  mycroft 		outb(iobase + lpt_control, LPC_SELECT);
    296   1.7.4.6  mycroft 		delay(100);
    297   1.7.4.6  mycroft 	}
    298   1.7.4.2  mycroft 
    299   1.7.4.6  mycroft 	control = LPC_SELECT | LPC_NINIT;
    300   1.7.4.6  mycroft 	outb(iobase + lpt_control, control);
    301   1.7.4.2  mycroft 
    302       1.1      cgd 	/* wait till ready (printer running diagnostics) */
    303   1.7.4.6  mycroft 	for (spin = 0; NOT_READY(); spin += STEP) {
    304   1.7.4.6  mycroft 		if (spin >= TIMEOUT) {
    305       1.1      cgd 			sc->sc_state = 0;
    306   1.7.4.2  mycroft 			return EBUSY;
    307       1.1      cgd 		}
    308       1.1      cgd 
    309       1.1      cgd 		/* wait 1/4 second, give up if we get a signal */
    310   1.7.4.2  mycroft 		if (error = tsleep((caddr_t)sc,
    311   1.7.4.6  mycroft 		    LPTPRI | PCATCH, "lptopen", STEP) != EWOULDBLOCK) {
    312       1.1      cgd 			sc->sc_state = 0;
    313   1.7.4.2  mycroft 			return error;
    314       1.1      cgd 		}
    315       1.1      cgd 	}
    316   1.7.4.2  mycroft 
    317   1.7.4.6  mycroft 	if ((flags & LPT_NOINTR) == 0)
    318   1.7.4.6  mycroft 		control |= LPC_IENABLE;
    319   1.7.4.6  mycroft 	if (flags & LPT_AUTOLF)
    320   1.7.4.6  mycroft 		control |= LPC_AUTOLF;
    321   1.7.4.2  mycroft 	sc->sc_control = control;
    322   1.7.4.2  mycroft 	outb(iobase + lpt_control, control);
    323   1.7.4.2  mycroft 
    324   1.7.4.2  mycroft 	sc->sc_state = LPT_OPEN;
    325   1.7.4.2  mycroft 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    326   1.7.4.2  mycroft 	sc->sc_count = 0;
    327   1.7.4.2  mycroft 	lptout(sc);
    328       1.1      cgd 
    329  1.7.4.10  mycroft 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
    330   1.7.4.2  mycroft 	return 0;
    331       1.1      cgd }
    332       1.1      cgd 
    333   1.7.4.2  mycroft static void
    334   1.7.4.1  mycroft lptout(sc)
    335       1.1      cgd 	struct lpt_softc *sc;
    336   1.7.4.2  mycroft {
    337   1.7.4.2  mycroft 	int	s;
    338       1.1      cgd 
    339   1.7.4.2  mycroft 	if ((sc->sc_state & LPT_OPEN) == 0)
    340   1.7.4.2  mycroft 		return;
    341   1.7.4.2  mycroft 	if (sc->sc_flags & LPT_NOINTR)
    342   1.7.4.2  mycroft 		return;
    343       1.1      cgd 
    344       1.1      cgd 	/*
    345   1.7.4.2  mycroft 	 * Avoid possible hangs due to missed interrupts
    346       1.1      cgd 	 */
    347   1.7.4.2  mycroft 	if (sc->sc_count) {
    348   1.7.4.2  mycroft 		s = spltty();
    349   1.7.4.2  mycroft 		lptintr(sc);
    350   1.7.4.2  mycroft 		splx(s);
    351       1.1      cgd 	} else {
    352   1.7.4.2  mycroft 		sc->sc_state &= ~LPT_OBUSY;
    353       1.1      cgd 		wakeup((caddr_t)sc);
    354       1.1      cgd 	}
    355   1.7.4.2  mycroft 
    356   1.7.4.2  mycroft 	timeout((timeout_t)lptout, (caddr_t)sc, STEP);
    357       1.1      cgd }
    358       1.1      cgd 
    359       1.1      cgd /*
    360       1.1      cgd  * lptclose -- close the device, free the local line buffer.
    361       1.1      cgd  */
    362   1.7.4.1  mycroft int
    363       1.1      cgd lptclose(dev, flag)
    364   1.7.4.1  mycroft 	dev_t dev;
    365       1.1      cgd 	int flag;
    366       1.1      cgd {
    367   1.7.4.2  mycroft 	int	unit = LPTUNIT(minor(dev));
    368   1.7.4.3  mycroft 	struct	lpt_softc *sc = lptcd.cd_devs[unit];
    369   1.7.4.2  mycroft 	u_short	iobase = sc->sc_iobase;
    370   1.7.4.2  mycroft 	int	error;
    371       1.1      cgd 
    372   1.7.4.2  mycroft 	if (error = pushbytes(sc))
    373   1.7.4.2  mycroft 		return error;
    374       1.1      cgd 
    375   1.7.4.2  mycroft 	outb(iobase + lpt_control, LPC_NINIT);
    376       1.1      cgd 	brelse(sc->sc_inbuf);
    377   1.7.4.2  mycroft 	sc->sc_state = 0;
    378   1.7.4.2  mycroft 
    379  1.7.4.10  mycroft 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
    380   1.7.4.2  mycroft 	return 0;
    381   1.7.4.2  mycroft }
    382   1.7.4.2  mycroft 
    383   1.7.4.2  mycroft static int
    384   1.7.4.2  mycroft pushbytes(sc)
    385   1.7.4.2  mycroft 	struct lpt_softc *sc;
    386   1.7.4.2  mycroft {
    387   1.7.4.2  mycroft 	u_short	iobase = sc->sc_iobase;
    388   1.7.4.2  mycroft 	int	error;
    389   1.7.4.2  mycroft 
    390   1.7.4.2  mycroft 	if (sc->sc_flags & LPT_NOINTR) {
    391   1.7.4.2  mycroft 		int	spin, tic;
    392   1.7.4.2  mycroft 		u_char	control = sc->sc_control;
    393   1.7.4.2  mycroft 		u_char	ch;
    394   1.7.4.2  mycroft 
    395   1.7.4.2  mycroft 		while (sc->sc_count > 0) {
    396   1.7.4.2  mycroft 			spin = tic = 0;
    397   1.7.4.2  mycroft 			while (NOT_READY())
    398   1.7.4.2  mycroft 				if (++spin >= sc->sc_smax) {
    399   1.7.4.2  mycroft 					/* exponential backoff */
    400   1.7.4.2  mycroft 					tic = tic + tic + 1;
    401   1.7.4.2  mycroft 					if (error = tsleep((caddr_t)sc,
    402   1.7.4.6  mycroft 					    LPTPRI | PCATCH, "lptwrite1", tic))
    403   1.7.4.2  mycroft 						return error;
    404   1.7.4.2  mycroft 				}
    405   1.7.4.2  mycroft 
    406   1.7.4.2  mycroft 			outb(iobase + lpt_data, *sc->sc_cp++);
    407   1.7.4.6  mycroft 			outb(iobase + lpt_control, control | LPC_STROBE);
    408   1.7.4.2  mycroft 			sc->sc_count--;
    409   1.7.4.6  mycroft 			outb(iobase + lpt_control, control);
    410   1.7.4.2  mycroft 
    411   1.7.4.2  mycroft 			/* adapt busy-wait algorithm */
    412   1.7.4.2  mycroft 			if (spin >= sc->sc_smax && sc->sc_smax < MAX_SPIN)
    413   1.7.4.2  mycroft 				sc->sc_smax++;
    414   1.7.4.2  mycroft 			if (spin*2 < sc->sc_smax)
    415   1.7.4.2  mycroft 				sc->sc_smax--;
    416   1.7.4.2  mycroft 		}
    417   1.7.4.2  mycroft 	} else {
    418   1.7.4.2  mycroft 		int	s;
    419   1.7.4.2  mycroft 
    420   1.7.4.2  mycroft 		while (sc->sc_count > 0) {
    421   1.7.4.2  mycroft 			/* if the printer is ready for a char, give it one */
    422   1.7.4.2  mycroft 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    423  1.7.4.10  mycroft 				lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
    424  1.7.4.10  mycroft 					sc->sc_count);
    425   1.7.4.2  mycroft 				s = spltty();
    426   1.7.4.2  mycroft 				lptintr(sc);
    427   1.7.4.2  mycroft 				splx(s);
    428   1.7.4.2  mycroft 			}
    429   1.7.4.2  mycroft 			if (error = tsleep((caddr_t)sc,
    430   1.7.4.6  mycroft 			    LPTPRI | PCATCH, "lptwrite2", 0))
    431   1.7.4.2  mycroft 				return error;
    432   1.7.4.2  mycroft 		}
    433   1.7.4.2  mycroft 	}
    434       1.1      cgd }
    435       1.1      cgd 
    436       1.1      cgd /*
    437   1.7.4.2  mycroft  * copy a line from user space to a local buffer, then call
    438       1.1      cgd  * putc to get the chars moved to the output queue.
    439       1.1      cgd  */
    440   1.7.4.1  mycroft int
    441       1.1      cgd lptwrite(dev, uio)
    442       1.1      cgd 	dev_t dev;
    443       1.1      cgd 	struct uio *uio;
    444       1.1      cgd {
    445   1.7.4.2  mycroft 	int	unit = LPTUNIT(minor(dev));
    446   1.7.4.3  mycroft 	struct	lpt_softc *sc = lptcd.cd_devs[unit];
    447   1.7.4.2  mycroft 	size_t	n;
    448   1.7.4.2  mycroft 	int	error = 0;
    449   1.7.4.2  mycroft 
    450   1.7.4.2  mycroft 	while (n = MIN(LPT_BSIZE, uio->uio_resid)) {
    451   1.7.4.2  mycroft 		uiomove(sc->sc_cp = sc->sc_inbuf->b_un.b_addr, n, uio);
    452   1.7.4.2  mycroft 		sc->sc_count = n;
    453   1.7.4.2  mycroft 		error = pushbytes(sc);
    454   1.7.4.2  mycroft 		if (error) {
    455   1.7.4.2  mycroft 			/* return accurate residual if interrupted or
    456   1.7.4.2  mycroft 			   timed out */
    457   1.7.4.2  mycroft 			uio->uio_resid += sc->sc_count;
    458   1.7.4.2  mycroft 			sc->sc_count = 0;
    459   1.7.4.2  mycroft 			return error;
    460       1.1      cgd 		}
    461       1.1      cgd 	}
    462   1.7.4.2  mycroft 	return 0;
    463       1.1      cgd }
    464       1.1      cgd 
    465       1.1      cgd /*
    466       1.1      cgd  * lptintr -- handle printer interrupts which occur when the printer is
    467       1.1      cgd  * ready to accept another char.
    468       1.1      cgd  */
    469   1.7.4.2  mycroft static int
    470   1.7.4.2  mycroft lptintr(arg)
    471   1.7.4.2  mycroft 	void *arg;
    472       1.1      cgd {
    473   1.7.4.2  mycroft 	struct	lpt_softc *sc = (struct lpt_softc *)arg;
    474   1.7.4.2  mycroft 	u_short	iobase = sc->sc_iobase;
    475   1.7.4.2  mycroft 	u_char	control = sc->sc_control;
    476   1.7.4.2  mycroft 	u_char	status;
    477       1.6  mycroft 
    478   1.7.4.2  mycroft 	if ((sc->sc_state & LPT_OPEN) == 0)
    479   1.7.4.2  mycroft 		return 0;
    480       1.6  mycroft 
    481       1.1      cgd 	/* is printer online and ready for output */
    482   1.7.4.2  mycroft 	if (!NOT_READY()) {
    483   1.7.4.2  mycroft 		if (sc->sc_count) {
    484       1.1      cgd 			/* send char */
    485   1.7.4.2  mycroft 			sc->sc_state |= LPT_OBUSY;
    486   1.7.4.6  mycroft 			while (sc->sc_count &&
    487   1.7.4.6  mycroft 			       (inb(iobase + lpt_status) & LPS_NBSY) == 0) {
    488   1.7.4.6  mycroft 				outb(iobase + lpt_data, *sc->sc_cp++);
    489   1.7.4.6  mycroft 				outb(iobase + lpt_control, control | LPC_STROBE);
    490   1.7.4.6  mycroft 				sc->sc_count--;
    491   1.7.4.6  mycroft 				outb(iobase + lpt_control, control);
    492   1.7.4.6  mycroft 			}
    493   1.7.4.2  mycroft 		} else {
    494   1.7.4.2  mycroft 			/* none, wake up the top half to get more */
    495   1.7.4.2  mycroft 			sc->sc_state &= ~LPT_OBUSY;
    496   1.7.4.2  mycroft 			wakeup((caddr_t)sc);
    497       1.1      cgd 		}
    498   1.7.4.2  mycroft 	}
    499       1.1      cgd 
    500   1.7.4.2  mycroft 	return 1;
    501       1.1      cgd }
    502       1.1      cgd 
    503       1.2      cgd int
    504   1.7.4.1  mycroft lptioctl(dev, cmd, data, flag)
    505   1.7.4.1  mycroft 	dev_t dev;
    506   1.7.4.1  mycroft 	int cmd;
    507   1.7.4.1  mycroft 	caddr_t data;
    508   1.7.4.1  mycroft 	int flag;
    509       1.2      cgd {
    510   1.7.4.2  mycroft 	int	error = 0;
    511       1.2      cgd 
    512       1.2      cgd 	switch (cmd) {
    513   1.7.4.2  mycroft 	    default:
    514       1.2      cgd 		error = ENODEV;
    515       1.2      cgd 	}
    516       1.2      cgd 
    517   1.7.4.2  mycroft 	return error;
    518       1.2      cgd }
    519