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