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