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