Home | History | Annotate | Line # | Download | only in ic
lpt.c revision 1.63
      1 /*	$NetBSD: lpt.c,v 1.63 2003/06/29 22:30:13 fvdl 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/cdefs.h>
     57 __KERNEL_RCSID(0, "$NetBSD: lpt.c,v 1.63 2003/06/29 22:30:13 fvdl Exp $");
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/proc.h>
     62 #include <sys/user.h>
     63 #include <sys/malloc.h>
     64 #include <sys/kernel.h>
     65 #include <sys/ioctl.h>
     66 #include <sys/uio.h>
     67 #include <sys/device.h>
     68 #include <sys/conf.h>
     69 #include <sys/syslog.h>
     70 
     71 #include <machine/bus.h>
     72 #include <machine/intr.h>
     73 
     74 #include <dev/ic/lptreg.h>
     75 #include <dev/ic/lptvar.h>
     76 
     77 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     78 #define	STEP		hz/4
     79 
     80 #define	LPTPRI		(PZERO+8)
     81 #define	LPT_BSIZE	1024
     82 
     83 #define LPTDEBUG
     84 
     85 #ifndef LPTDEBUG
     86 #define LPRINTF(a)
     87 #else
     88 #define LPRINTF(a)	if (lptdebug) printf a
     89 int lptdebug = 0;
     90 #endif
     91 
     92 extern struct cfdriver lpt_cd;
     93 
     94 dev_type_open(lptopen);
     95 dev_type_close(lptclose);
     96 dev_type_write(lptwrite);
     97 dev_type_ioctl(lptioctl);
     98 
     99 const struct cdevsw lpt_cdevsw = {
    100 	lptopen, lptclose, noread, lptwrite, lptioctl,
    101 	nostop, notty, nopoll, nommap, nokqfilter,
    102 };
    103 
    104 #define	LPTUNIT(s)	(minor(s) & 0x1f)
    105 #define	LPTFLAGS(s)	(minor(s) & 0xe0)
    106 
    107 void
    108 lpt_attach_subr(sc)
    109 	struct lpt_softc *sc;
    110 {
    111 	bus_space_tag_t iot;
    112 	bus_space_handle_t ioh;
    113 
    114 	sc->sc_state = 0;
    115 
    116 	iot = sc->sc_iot;
    117 	ioh = sc->sc_ioh;
    118 
    119 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    120 
    121 	callout_init(&sc->sc_wakeup_ch);
    122 
    123 	sc->sc_dev_ok = 1;
    124 }
    125 
    126 /*
    127  * Reset the printer, then wait until it's selected and not busy.
    128  */
    129 int
    130 lptopen(dev, flag, mode, p)
    131 	dev_t dev;
    132 	int flag;
    133 	int mode;
    134 	struct proc *p;
    135 {
    136 	u_char flags = LPTFLAGS(dev);
    137 	struct lpt_softc *sc;
    138 	bus_space_tag_t iot;
    139 	bus_space_handle_t ioh;
    140 	u_char control;
    141 	int error;
    142 	int spin;
    143 
    144 	sc = device_lookup(&lpt_cd, LPTUNIT(dev));
    145 	if (!sc || !sc->sc_dev_ok)
    146 		return ENXIO;
    147 
    148 #if 0	/* XXX what to do? */
    149 	if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
    150 		return ENXIO;
    151 #endif
    152 
    153 #ifdef DIAGNOSTIC
    154 	if (sc->sc_state)
    155 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
    156 		    sc->sc_state);
    157 #endif
    158 
    159 	if (sc->sc_state)
    160 		return EBUSY;
    161 
    162 	sc->sc_state = LPT_INIT;
    163 	sc->sc_flags = flags;
    164 	LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname,
    165 	    (unsigned)flags));
    166 	iot = sc->sc_iot;
    167 	ioh = sc->sc_ioh;
    168 
    169 	if ((flags & LPT_NOPRIME) == 0) {
    170 		/* assert INIT for 100 usec to start up printer */
    171 		bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
    172 		delay(100);
    173 	}
    174 
    175 	control = LPC_SELECT | LPC_NINIT;
    176 	bus_space_write_1(iot, ioh, lpt_control, control);
    177 
    178 	/* wait till ready (printer running diagnostics) */
    179 	for (spin = 0; NOT_READY_ERR(); spin += STEP) {
    180 		if (spin >= TIMEOUT) {
    181 			sc->sc_state = 0;
    182 			return EBUSY;
    183 		}
    184 
    185 		/* wait 1/4 second, give up if we get a signal */
    186 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP);
    187 		if (error != EWOULDBLOCK) {
    188 			sc->sc_state = 0;
    189 			return error;
    190 		}
    191 	}
    192 
    193 	if ((flags & LPT_NOINTR) == 0)
    194 		control |= LPC_IENABLE;
    195 	if (flags & LPT_AUTOLF)
    196 		control |= LPC_AUTOLF;
    197 	sc->sc_control = control;
    198 	bus_space_write_1(iot, ioh, lpt_control, control);
    199 
    200 	sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK);
    201 	sc->sc_count = 0;
    202 	sc->sc_state = LPT_OPEN;
    203 
    204 	if ((sc->sc_flags & LPT_NOINTR) == 0)
    205 		lptwakeup(sc);
    206 
    207 	LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname));
    208 	return 0;
    209 }
    210 
    211 int
    212 lptnotready(status, sc)
    213 	u_char status;
    214 	struct lpt_softc *sc;
    215 {
    216 	u_char new;
    217 
    218 	status = (status ^ LPS_INVERT) & LPS_MASK;
    219 	new = status & ~sc->sc_laststatus;
    220 	sc->sc_laststatus = status;
    221 
    222 	if (sc->sc_state & LPT_OPEN) {
    223 		if (new & LPS_SELECT)
    224 			log(LOG_NOTICE,
    225 			    "%s: offline\n", sc->sc_dev.dv_xname);
    226 		else if (new & LPS_NOPAPER)
    227 			log(LOG_NOTICE,
    228 			    "%s: out of paper\n", sc->sc_dev.dv_xname);
    229 		else if (new & LPS_NERR)
    230 			log(LOG_NOTICE,
    231 			    "%s: output error\n", sc->sc_dev.dv_xname);
    232 	}
    233 
    234 	return status;
    235 }
    236 
    237 void
    238 lptwakeup(arg)
    239 	void *arg;
    240 {
    241 	struct lpt_softc *sc = arg;
    242 	int s;
    243 
    244 	s = spllpt();
    245 	lptintr(sc);
    246 	splx(s);
    247 
    248 	callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
    249 }
    250 
    251 /*
    252  * Close the device, and free the local line buffer.
    253  */
    254 int
    255 lptclose(dev, flag, mode, p)
    256 	dev_t dev;
    257 	int flag;
    258 	int mode;
    259 	struct proc *p;
    260 {
    261 	struct lpt_softc *sc = device_lookup(&lpt_cd, LPTUNIT(dev));
    262 	bus_space_tag_t iot = sc->sc_iot;
    263 	bus_space_handle_t ioh = sc->sc_ioh;
    264 
    265 	if (sc->sc_count)
    266 		(void) lptpushbytes(sc);
    267 
    268 	if ((sc->sc_flags & LPT_NOINTR) == 0)
    269 		callout_stop(&sc->sc_wakeup_ch);
    270 
    271 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    272 	sc->sc_state = 0;
    273 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    274 	free(sc->sc_inbuf, M_DEVBUF);
    275 
    276 	LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname));
    277 	return 0;
    278 }
    279 
    280 int
    281 lptpushbytes(sc)
    282 	struct lpt_softc *sc;
    283 {
    284 	bus_space_tag_t iot = sc->sc_iot;
    285 	bus_space_handle_t ioh = sc->sc_ioh;
    286 	int error;
    287 
    288 	if (sc->sc_flags & LPT_NOINTR) {
    289 		int spin, tic;
    290 		u_char control = sc->sc_control;
    291 
    292 		while (sc->sc_count > 0) {
    293 			spin = 0;
    294 			while (NOT_READY()) {
    295 				if (++spin < sc->sc_spinmax)
    296 					continue;
    297 				tic = 0;
    298 				/* adapt busy-wait algorithm */
    299 				sc->sc_spinmax++;
    300 				while (NOT_READY_ERR()) {
    301 					/* exponential backoff */
    302 					tic = tic + tic + 1;
    303 					if (tic > TIMEOUT)
    304 						tic = TIMEOUT;
    305 					error = tsleep((caddr_t)sc,
    306 					    LPTPRI | PCATCH, "lptpsh", tic);
    307 					if (error != EWOULDBLOCK)
    308 						return error;
    309 				}
    310 				break;
    311 			}
    312 
    313 			bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
    314 			DELAY(1);
    315 			bus_space_write_1(iot, ioh, lpt_control,
    316 			    control | LPC_STROBE);
    317 			DELAY(1);
    318 			sc->sc_count--;
    319 			bus_space_write_1(iot, ioh, lpt_control, control);
    320 			DELAY(1);
    321 
    322 			/* adapt busy-wait algorithm */
    323 			if (spin*2 + 16 < sc->sc_spinmax)
    324 				sc->sc_spinmax--;
    325 		}
    326 	} else {
    327 		int s;
    328 
    329 		while (sc->sc_count > 0) {
    330 			/* if the printer is ready for a char, give it one */
    331 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    332 				LPRINTF(("%s: write %lu\n", sc->sc_dev.dv_xname,
    333 				    (u_long)sc->sc_count));
    334 				s = spllpt();
    335 				(void) lptintr(sc);
    336 				splx(s);
    337 			}
    338 			error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
    339 			    "lptwrite2", 0);
    340 			if (error)
    341 				return error;
    342 		}
    343 	}
    344 	return 0;
    345 }
    346 
    347 /*
    348  * Copy a line from user space to a local buffer, then call putc to get the
    349  * chars moved to the output queue.
    350  */
    351 int
    352 lptwrite(dev, uio, flags)
    353 	dev_t dev;
    354 	struct uio *uio;
    355 	int flags;
    356 {
    357 	struct lpt_softc *sc = device_lookup(&lpt_cd, LPTUNIT(dev));
    358 	size_t n;
    359 	int error = 0;
    360 
    361 	while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) {
    362 		uiomove(sc->sc_cp = sc->sc_inbuf, n, uio);
    363 		sc->sc_count = n;
    364 		error = lptpushbytes(sc);
    365 		if (error) {
    366 			/*
    367 			 * Return accurate residual if interrupted or timed
    368 			 * out.
    369 			 */
    370 			uio->uio_resid += sc->sc_count;
    371 			sc->sc_count = 0;
    372 			return error;
    373 		}
    374 	}
    375 	return 0;
    376 }
    377 
    378 /*
    379  * Handle printer interrupts which occur when the printer is ready to accept
    380  * another char.
    381  */
    382 int
    383 lptintr(arg)
    384 	void *arg;
    385 {
    386 	struct lpt_softc *sc = arg;
    387 	bus_space_tag_t iot = sc->sc_iot;
    388 	bus_space_handle_t ioh = sc->sc_ioh;
    389 
    390 #if 0
    391 	if ((sc->sc_state & LPT_OPEN) == 0)
    392 		return 0;
    393 #endif
    394 
    395 	/* is printer online and ready for output */
    396 	if (NOT_READY() && NOT_READY_ERR())
    397 		return 0;
    398 
    399 	if (sc->sc_count) {
    400 		u_char control = sc->sc_control;
    401 		/* send char */
    402 		bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
    403 		DELAY(1);
    404 		bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
    405 		DELAY(1);
    406 		sc->sc_count--;
    407 		bus_space_write_1(iot, ioh, lpt_control, control);
    408 		DELAY(1);
    409 		sc->sc_state |= LPT_OBUSY;
    410 	} else
    411 		sc->sc_state &= ~LPT_OBUSY;
    412 
    413 	if (sc->sc_count == 0) {
    414 		/* none, wake up the top half to get more */
    415 		wakeup((caddr_t)sc);
    416 	}
    417 
    418 	return 1;
    419 }
    420 
    421 int
    422 lptioctl(dev, cmd, data, flag, p)
    423 	dev_t dev;
    424 	u_long cmd;
    425 	caddr_t data;
    426 	int flag;
    427 	struct proc *p;
    428 {
    429 	int error = 0;
    430 
    431 	switch (cmd) {
    432 	default:
    433 		error = ENODEV;
    434 	}
    435 
    436 	return error;
    437 }
    438