Home | History | Annotate | Line # | Download | only in ic
      1 /*	$NetBSD: lpt.c,v 1.83 2026/10/08 15:54:35 riastradh 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.83 2026/10/08 15:54:35 riastradh 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 #include "ioconf.h"
     77 
     78 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     79 #define	STEP		hz/4
     80 
     81 #define	LPTPRI		(PZERO+8)
     82 #define	LPT_BSIZE	1024
     83 
     84 #define LPTDEBUG
     85 
     86 #ifndef LPTDEBUG
     87 #define LPRINTF(a)
     88 #else
     89 #define LPRINTF(a)	if (lptdebug) printf a
     90 int lptdebug = 0;
     91 #endif
     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 	int s;
    162 
    163 	sc = device_lookup_private(&lpt_cd, LPTUNIT(dev));
    164 	if (!sc || !sc->sc_dev_ok)
    165 		return ENXIO;
    166 
    167 #if 0	/* XXX what to do? */
    168 	if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0)
    169 		return ENXIO;
    170 #endif
    171 
    172 #ifdef DIAGNOSTIC
    173 	if (sc->sc_state)
    174 		aprint_verbose_dev(sc->sc_dev, "stat=0x%x not zero\n",
    175 		    sc->sc_state);
    176 #endif
    177 
    178 	s = spltty();
    179 
    180 	if (sc->sc_state) {
    181 		error = EBUSY;
    182 		goto out;
    183 	}
    184 
    185 	sc->sc_state = LPT_INIT;
    186 	sc->sc_flags = flags;
    187 	LPRINTF(("%s: open: flags=0x%x\n", device_xname(sc->sc_dev),
    188 	    (unsigned)flags));
    189 	iot = sc->sc_iot;
    190 	ioh = sc->sc_ioh;
    191 
    192 	if ((flags & LPT_NOPRIME) == 0) {
    193 		/* assert INIT for 100 usec to start up printer */
    194 		bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT);
    195 		delay(100);
    196 	}
    197 
    198 	control = LPC_SELECT | LPC_NINIT;
    199 	bus_space_write_1(iot, ioh, lpt_control, control);
    200 
    201 	/* wait till ready (printer running diagnostics) */
    202 	for (spin = 0; NOT_READY_ERR(); spin += STEP) {
    203 		if (spin >= TIMEOUT) {
    204 			sc->sc_state = 0;
    205 			error = EBUSY;
    206 			goto out;
    207 		}
    208 
    209 		/* wait 1/4 second, give up if we get a signal */
    210 		error = tsleep((void *)sc, LPTPRI | PCATCH, "lptopen", STEP);
    211 		if (error != EWOULDBLOCK) {
    212 			sc->sc_state = 0;
    213 			goto out;
    214 		}
    215 	}
    216 
    217 	if ((flags & LPT_NOINTR) == 0)
    218 		control |= LPC_IENABLE;
    219 	if (flags & LPT_AUTOLF)
    220 		control |= LPC_AUTOLF;
    221 	sc->sc_control = control;
    222 	bus_space_write_1(iot, ioh, lpt_control, control);
    223 
    224 	sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK);
    225 	sc->sc_cp = NULL;
    226 	sc->sc_count = 0;
    227 	sc->sc_state = LPT_OPEN;
    228 
    229 	if ((sc->sc_flags & LPT_NOINTR) == 0)
    230 		lptwakeup(sc);
    231 
    232 	LPRINTF(("%s: opened\n", device_xname(sc->sc_dev)));
    233 	error = 0;
    234 
    235 out:	splx(s);
    236 	return error;
    237 }
    238 
    239 int
    240 lptnotready(u_char status, struct lpt_softc *sc)
    241 {
    242 	u_char new;
    243 
    244 	status = (status ^ LPS_INVERT) & LPS_MASK;
    245 	new = status & ~sc->sc_laststatus;
    246 	sc->sc_laststatus = status;
    247 
    248 	if (sc->sc_state & LPT_OPEN) {
    249 		if (new & LPS_SELECT)
    250 			log(LOG_NOTICE,
    251 			    "%s: offline\n", device_xname(sc->sc_dev));
    252 		else if (new & LPS_NOPAPER)
    253 			log(LOG_NOTICE,
    254 			    "%s: out of paper\n", device_xname(sc->sc_dev));
    255 		else if (new & LPS_NERR)
    256 			log(LOG_NOTICE,
    257 			    "%s: output error\n", device_xname(sc->sc_dev));
    258 	}
    259 
    260 	return status;
    261 }
    262 
    263 void
    264 lptwakeup(void *arg)
    265 {
    266 	struct lpt_softc *sc = arg;
    267 	int s;
    268 
    269 	s = splvm();
    270 	lptintr(sc);
    271 	splx(s);
    272 
    273 	callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
    274 }
    275 
    276 /*
    277  * Close the device, and free the local line buffer.
    278  */
    279 int
    280 lptclose(dev_t dev, int flag, int mode,
    281     struct lwp *l)
    282 {
    283 	struct lpt_softc *sc =
    284 	    device_lookup_private(&lpt_cd, LPTUNIT(dev));
    285 	bus_space_tag_t iot = sc->sc_iot;
    286 	bus_space_handle_t ioh = sc->sc_ioh;
    287 	int s;
    288 
    289 	s = spltty();
    290 	if (sc->sc_count)
    291 		(void) lptpushbytes(sc);
    292 	sc->sc_count = 0;
    293 
    294 	if ((sc->sc_flags & LPT_NOINTR) == 0)
    295 		callout_stop(&sc->sc_wakeup_ch);
    296 
    297 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    298 	sc->sc_state = 0;
    299 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
    300 	free(sc->sc_inbuf, M_DEVBUF);
    301 	sc->sc_cp = sc->sc_inbuf = NULL;
    302 	splx(s);
    303 
    304 	LPRINTF(("%s: closed\n", device_xname(sc->sc_dev)));
    305 	return 0;
    306 }
    307 
    308 int
    309 lptpushbytes(struct lpt_softc *sc)
    310 {
    311 	bus_space_tag_t iot = sc->sc_iot;
    312 	bus_space_handle_t ioh = sc->sc_ioh;
    313 	int error;
    314 
    315 	if (sc->sc_flags & LPT_NOINTR) {
    316 		int spin, tic;
    317 		u_char control = sc->sc_control;
    318 
    319 		while (sc->sc_count > 0) {
    320 			spin = 0;
    321 			while (NOT_READY()) {
    322 				if (++spin < sc->sc_spinmax)
    323 					continue;
    324 				tic = 0;
    325 				/* adapt busy-wait algorithm */
    326 				sc->sc_spinmax++;
    327 				while (NOT_READY_ERR()) {
    328 					/* exponential backoff */
    329 					tic = tic + tic + 1;
    330 					if (tic > TIMEOUT)
    331 						tic = TIMEOUT;
    332 					error = tsleep((void *)sc,
    333 					    LPTPRI | PCATCH, "lptpsh", tic);
    334 					if (error != EWOULDBLOCK)
    335 						return error;
    336 				}
    337 				break;
    338 			}
    339 
    340 			bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
    341 			DELAY(1);
    342 			bus_space_write_1(iot, ioh, lpt_control,
    343 			    control | LPC_STROBE);
    344 			DELAY(1);
    345 			sc->sc_count--;
    346 			bus_space_write_1(iot, ioh, lpt_control, control);
    347 			DELAY(1);
    348 
    349 			/* adapt busy-wait algorithm */
    350 			if (spin*2 + 16 < sc->sc_spinmax)
    351 				sc->sc_spinmax--;
    352 		}
    353 	} else {
    354 		int s;
    355 
    356 		while (sc->sc_count > 0) {
    357 			/* if the printer is ready for a char, give it one */
    358 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    359 				LPRINTF(("%s: write %lu\n",
    360 				    device_xname(sc->sc_dev),
    361 				    (u_long)sc->sc_count));
    362 				s = splvm();
    363 				(void) lptintr(sc);
    364 				splx(s);
    365 			}
    366 			error = tsleep((void *)sc, LPTPRI | PCATCH,
    367 			    "lptwrite2", 0);
    368 			if (error)
    369 				return error;
    370 		}
    371 	}
    372 	return 0;
    373 }
    374 
    375 /*
    376  * Copy a line from user space to a local buffer, then call putc to get the
    377  * chars moved to the output queue.
    378  */
    379 int
    380 lptwrite(dev_t dev, struct uio *uio, int flags)
    381 {
    382 	struct lpt_softc *sc =
    383 	    device_lookup_private(&lpt_cd, LPTUNIT(dev));
    384 	size_t n;
    385 	int s;
    386 	bool locked = false;
    387 	int error = 0;
    388 
    389 	s = spltty();
    390 	while (sc->sc_state & LPT_WRITING) {
    391 		error = tsleep(&sc->sc_state, LPTPRI | PCATCH, "lptwrite", 0);
    392 		if (error)
    393 			goto out;
    394 	}
    395 	sc->sc_state |= LPT_WRITING;
    396 	locked = true;
    397 
    398 	while ((n = uimin(LPT_BSIZE, uio->uio_resid)) != 0) {
    399 		uiomove(sc->sc_cp = sc->sc_inbuf, n, uio);
    400 		sc->sc_count = n;
    401 		error = lptpushbytes(sc);
    402 		if (error) {
    403 			/*
    404 			 * Return accurate residual if interrupted or timed
    405 			 * out.
    406 			 */
    407 			uio->uio_resid += sc->sc_count;
    408 			sc->sc_count = 0;
    409 			goto out;
    410 		}
    411 	}
    412 	error = 0;
    413 
    414 out:	if (locked) {
    415 		sc->sc_state &= ~LPT_WRITING;
    416 		wakeup(&sc->sc_state);
    417 	}
    418 	splx(s);
    419 	return error;
    420 }
    421 
    422 /*
    423  * Handle printer interrupts which occur when the printer is ready to accept
    424  * another char.
    425  */
    426 int
    427 lptintr(void *arg)
    428 {
    429 	struct lpt_softc *sc = arg;
    430 	bus_space_tag_t iot = sc->sc_iot;
    431 	bus_space_handle_t ioh = sc->sc_ioh;
    432 
    433 #if 0
    434 	if ((sc->sc_state & LPT_OPEN) == 0)
    435 		return 0;
    436 #endif
    437 
    438 	/* is printer online and ready for output */
    439 	if (NOT_READY() && NOT_READY_ERR())
    440 		return 0;
    441 
    442 	if (sc->sc_count) {
    443 		u_char control = sc->sc_control;
    444 		/* send char */
    445 		bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++);
    446 		DELAY(1);
    447 		bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE);
    448 		DELAY(1);
    449 		sc->sc_count--;
    450 		bus_space_write_1(iot, ioh, lpt_control, control);
    451 		DELAY(1);
    452 		sc->sc_state |= LPT_OBUSY;
    453 	} else
    454 		sc->sc_state &= ~LPT_OBUSY;
    455 
    456 	if (sc->sc_count == 0) {
    457 		/* none, wake up the top half to get more */
    458 		softint_schedule(sc->sc_sih);
    459 	}
    460 
    461 	return 1;
    462 }
    463 
    464 static void
    465 lptsoftintr(void *cookie)
    466 {
    467 
    468 	wakeup(cookie);
    469 }
    470 
    471 int
    472 lptioctl(dev_t dev, u_long cmd, void *data,
    473     int flag, struct lwp *l)
    474 {
    475 	return ENODEV;
    476 }
    477