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