Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.57
      1 /*	$NetBSD: lpt.c,v 1.57 2000/07/06 01:47:37 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993, 1994 Charles M. Hannum.
      5  * Copyright (c) 1990 William F. Jolitz, TeleMuse
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This software is a component of "386BSD" developed by
     19  *	William F. Jolitz, TeleMuse.
     20  * 4. Neither the name of the developer nor the name "386BSD"
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
     25  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
     26  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
     27  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
     28  * NOT MAKE USE OF THIS WORK.
     29  *
     30  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
     31  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
     32  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
     33  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
     34  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
     35  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
     36  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
     37  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
     38  *
     39  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
     40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
     43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     49  * SUCH DAMAGE.
     50  */
     51 
     52 /*
     53  * Device Driver for AT style parallel printer port
     54  */
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/proc.h>
     59 #include <sys/user.h>
     60 #include <sys/malloc.h>
     61 #include <sys/kernel.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/uio.h>
     64 #include <sys/device.h>
     65 #include <sys/conf.h>
     66 #include <sys/syslog.h>
     67 
     68 #include <machine/bus.h>
     69 #include <machine/intr.h>
     70 
     71 #include <dev/ic/lptreg.h>
     72 #include <dev/ic/lptvar.h>
     73 
     74 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     75 #define	STEP		hz/4
     76 
     77 #define	LPTPRI		(PZERO+8)
     78 #define	LPT_BSIZE	1024
     79 
     80 #define LPTDEBUG
     81 
     82 #ifndef LPTDEBUG
     83 #define LPRINTF(a)
     84 #else
     85 #define LPRINTF(a)	if (lptdebug) printf a
     86 int lptdebug = 0;
     87 #endif
     88 
     89 /* XXX does not belong here */
     90 cdev_decl(lpt);
     91 
     92 extern struct cfdriver lpt_cd;
     93 
     94 #define	LPTUNIT(s)	(minor(s) & 0x1f)
     95 #define	LPTFLAGS(s)	(minor(s) & 0xe0)
     96 
     97 void
     98 lpt_attach_subr(sc)
     99 	struct lpt_softc *sc;
    100 {
    101 	bus_space_tag_t iot;
    102 	bus_space_handle_t ioh;
    103 
    104 	sc->sc_state = 0;
    105 
    106 	iot = sc->sc_iot;
    107 	ioh = sc->sc_ioh;
    108 
    109 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    110 
    111 	callout_init(&sc->sc_wakeup_ch);
    112 
    113 	sc->sc_dev_ok = 1;
    114 }
    115 
    116 /*
    117  * Reset the printer, then wait until it's selected and not busy.
    118  */
    119 int
    120 lptopen(dev, flag, mode, p)
    121 	dev_t dev;
    122 	int flag;
    123 	int mode;
    124 	struct proc *p;
    125 {
    126 	u_char flags = LPTFLAGS(dev);
    127 	struct lpt_softc *sc;
    128 	bus_space_tag_t iot;
    129 	bus_space_handle_t ioh;
    130 	u_char control;
    131 	int error;
    132 	int spin;
    133 
    134 	sc = device_lookup(&lpt_cd, LPTUNIT(dev));
    135 	if (!sc || !sc->sc_dev_ok)
    136 		return ENXIO;
    137 
    138 #if 0	/* XXX what to do? */
    139 	if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
    140 		return ENXIO;
    141 #endif
    142 
    143 #ifdef DIAGNOSTIC
    144 	if (sc->sc_state)
    145 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
    146 		    sc->sc_state);
    147 #endif
    148 
    149 	if (sc->sc_state)
    150 		return EBUSY;
    151 
    152 	sc->sc_state = LPT_INIT;
    153 	sc->sc_flags = flags;
    154 	LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname,
    155 	    (unsigned)flags));
    156 	iot = sc->sc_iot;
    157 	ioh = sc->sc_ioh;
    158 
    159 	if ((flags & LPT_NOPRIME) == 0) {
    160 		/* assert INIT for 100 usec to start up printer */
    161 		bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
    162 		delay(100);
    163 	}
    164 
    165 	control = LPC_SELECT | LPC_NINIT;
    166 	bus_space_write_1(iot, ioh, lpt_control, control);
    167 
    168 	/* wait till ready (printer running diagnostics) */
    169 	for (spin = 0; NOT_READY_ERR(); spin += STEP) {
    170 		if (spin >= TIMEOUT) {
    171 			sc->sc_state = 0;
    172 			return EBUSY;
    173 		}
    174 
    175 		/* wait 1/4 second, give up if we get a signal */
    176 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
    177 		if (error != EWOULDBLOCK) {
    178 			sc->sc_state = 0;
    179 			return error;
    180 		}
    181 	}
    182 
    183 	if ((flags & LPT_NOINTR) == 0)
    184 		control |= LPC_IENABLE;
    185 	if (flags & LPT_AUTOLF)
    186 		control |= LPC_AUTOLF;
    187 	sc->sc_control = control;
    188 	bus_space_write_1(iot, ioh, lpt_control, control);
    189 
    190 	sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK);
    191 	sc->sc_count = 0;
    192 	sc->sc_state = LPT_OPEN;
    193 
    194 	if ((sc->sc_flags & LPT_NOINTR) == 0)
    195 		lptwakeup(sc);
    196 
    197 	LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
    198 	return 0;
    199 }
    200 
    201 int
    202 lptnotready(status, sc)
    203 	u_char status;
    204 	struct lpt_softc *sc;
    205 {
    206 	u_char new;
    207 
    208 	status = (status ^ LPS_INVERT) & LPS_MASK;
    209 	new = status & ~sc->sc_laststatus;
    210 	sc->sc_laststatus = status;
    211 
    212 	if (sc->sc_state & LPT_OPEN) {
    213 		if (new & LPS_SELECT)
    214 			log(LOG_NOTICE,
    215 			    "%s: offline\n", sc->sc_dev.dv_xname);
    216 		else if (new & LPS_NOPAPER)
    217 			log(LOG_NOTICE,
    218 			    "%s: out of paper\n", sc->sc_dev.dv_xname);
    219 		else if (new & LPS_NERR)
    220 			log(LOG_NOTICE,
    221 			    "%s: output error\n", sc->sc_dev.dv_xname);
    222 	}
    223 
    224 	return status;
    225 }
    226 
    227 void
    228 lptwakeup(arg)
    229 	void *arg;
    230 {
    231 	struct lpt_softc *sc = arg;
    232 	int s;
    233 
    234 	s = spllpt();
    235 	lptintr(sc);
    236 	splx(s);
    237 
    238 	callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
    239 }
    240 
    241 /*
    242  * Close the device, and free the local line buffer.
    243  */
    244 int
    245 lptclose(dev, flag, mode, p)
    246 	dev_t dev;
    247 	int flag;
    248 	int mode;
    249 	struct proc *p;
    250 {
    251 	struct lpt_softc *sc = device_lookup(&lpt_cd, LPTUNIT(dev));
    252 	bus_space_tag_t iot = sc->sc_iot;
    253 	bus_space_handle_t ioh = sc->sc_ioh;
    254 
    255 	if (sc->sc_count)
    256 		(void) lptpushbytes(sc);
    257 
    258 	if ((sc->sc_flags & LPT_NOINTR) == 0)
    259 		callout_stop(&sc->sc_wakeup_ch);
    260 
    261 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    262 	sc->sc_state = 0;
    263 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    264 	free(sc->sc_inbuf, M_DEVBUF);
    265 
    266 	LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
    267 	return 0;
    268 }
    269 
    270 int
    271 lptpushbytes(sc)
    272 	struct lpt_softc *sc;
    273 {
    274 	bus_space_tag_t iot = sc->sc_iot;
    275 	bus_space_handle_t ioh = sc->sc_ioh;
    276 	int error;
    277 
    278 	if (sc->sc_flags & LPT_NOINTR) {
    279 		int spin, tic;
    280 		u_char control = sc->sc_control;
    281 
    282 		while (sc->sc_count > 0) {
    283 			spin = 0;
    284 			while (NOT_READY()) {
    285 				if (++spin < sc->sc_spinmax)
    286 					continue;
    287 				tic = 0;
    288 				/* adapt busy-wait algorithm */
    289 				sc->sc_spinmax++;
    290 				while (NOT_READY_ERR()) {
    291 					/* exponential backoff */
    292 					tic = tic + tic + 1;
    293 					if (tic > TIMEOUT)
    294 						tic = TIMEOUT;
    295 					error = tsleep((caddr_t)sc,
    296 					    LPTPRI | PCATCH, "lptpsh", tic);
    297 					if (error != EWOULDBLOCK)
    298 						return error;
    299 				}
    300 				break;
    301 			}
    302 
    303 			bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
    304 			bus_space_write_1(iot, ioh, lpt_control,
    305 			    control | LPC_STROBE);
    306 			sc->sc_count--;
    307 			bus_space_write_1(iot, ioh, lpt_control, control);
    308 
    309 			/* adapt busy-wait algorithm */
    310 			if (spin*2 + 16 < sc->sc_spinmax)
    311 				sc->sc_spinmax--;
    312 		}
    313 	} else {
    314 		int s;
    315 
    316 		while (sc->sc_count > 0) {
    317 			/* if the printer is ready for a char, give it one */
    318 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    319 				LPRINTF(("%s: write %lu\n", sc->sc_dev.dv_xname,
    320 				    (u_long)sc->sc_count));
    321 				s = spllpt();
    322 				(void) lptintr(sc);
    323 				splx(s);
    324 			}
    325 			error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
    326 			    "lptwrite2", 0);
    327 			if (error)
    328 				return error;
    329 		}
    330 	}
    331 	return 0;
    332 }
    333 
    334 /*
    335  * Copy a line from user space to a local buffer, then call putc to get the
    336  * chars moved to the output queue.
    337  */
    338 int
    339 lptwrite(dev, uio, flags)
    340 	dev_t dev;
    341 	struct uio *uio;
    342 	int flags;
    343 {
    344 	struct lpt_softc *sc = device_lookup(&lpt_cd, LPTUNIT(dev));
    345 	size_t n;
    346 	int error = 0;
    347 
    348 	while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
    349 		uiomove(sc->sc_cp = sc->sc_inbuf, n, uio);
    350 		sc->sc_count = n;
    351 		error = lptpushbytes(sc);
    352 		if (error) {
    353 			/*
    354 			 * Return accurate residual if interrupted or timed
    355 			 * out.
    356 			 */
    357 			uio->uio_resid += sc->sc_count;
    358 			sc->sc_count = 0;
    359 			return error;
    360 		}
    361 	}
    362 	return 0;
    363 }
    364 
    365 /*
    366  * Handle printer interrupts which occur when the printer is ready to accept
    367  * another char.
    368  */
    369 int
    370 lptintr(arg)
    371 	void *arg;
    372 {
    373 	struct lpt_softc *sc = arg;
    374 	bus_space_tag_t iot = sc->sc_iot;
    375 	bus_space_handle_t ioh = sc->sc_ioh;
    376 
    377 #if 0
    378 	if ((sc->sc_state & LPT_OPEN) == 0)
    379 		return 0;
    380 #endif
    381 
    382 	/* is printer online and ready for output */
    383 	if (NOT_READY() && NOT_READY_ERR())
    384 		return 0;
    385 
    386 	if (sc->sc_count) {
    387 		u_char control = sc->sc_control;
    388 		/* send char */
    389 		bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
    390 		DELAY(1);
    391 		bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
    392 		DELAY(1);
    393 		sc->sc_count--;
    394 		bus_space_write_1(iot, ioh, lpt_control, control);
    395 		sc->sc_state |= LPT_OBUSY;
    396 	} else
    397 		sc->sc_state &= ~LPT_OBUSY;
    398 
    399 	if (sc->sc_count == 0) {
    400 		/* none, wake up the top half to get more */
    401 		wakeup((caddr_t)sc);
    402 	}
    403 
    404 	return 1;
    405 }
    406 
    407 int
    408 lptioctl(dev, cmd, data, flag, p)
    409 	dev_t dev;
    410 	u_long cmd;
    411 	caddr_t data;
    412 	int flag;
    413 	struct proc *p;
    414 {
    415 	int error = 0;
    416 
    417 	switch (cmd) {
    418 	default:
    419 		error = ENODEV;
    420 	}
    421 
    422 	return error;
    423 }
    424