xref: /netbsd-src/sys/dev/mvme/lpt_mvme.c (revision d1579b2d70337e1b895f03478838f880e450f6da)
1*d1579b2dSriastradh /*	$NetBSD: lpt_mvme.c,v 1.19 2018/09/03 16:29:32 riastradh Exp $	*/
208bde987Sscw 
308bde987Sscw /*-
408bde987Sscw  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
508bde987Sscw  * All rights reserved.
608bde987Sscw  *
708bde987Sscw  * This code is derived from software contributed to The NetBSD Foundation
808bde987Sscw  * by Steve C. Woodford.
908bde987Sscw  *
1008bde987Sscw  * Redistribution and use in source and binary forms, with or without
1108bde987Sscw  * modification, are permitted provided that the following conditions
1208bde987Sscw  * are met:
1308bde987Sscw  * 1. Redistributions of source code must retain the above copyright
1408bde987Sscw  *    notice, this list of conditions and the following disclaimer.
1508bde987Sscw  * 2. Redistributions in binary form must reproduce the above copyright
1608bde987Sscw  *    notice, this list of conditions and the following disclaimer in the
1708bde987Sscw  *    documentation and/or other materials provided with the distribution.
1808bde987Sscw  *
1908bde987Sscw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2008bde987Sscw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2108bde987Sscw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2208bde987Sscw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2308bde987Sscw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2408bde987Sscw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2508bde987Sscw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2608bde987Sscw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2708bde987Sscw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2808bde987Sscw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2908bde987Sscw  * POSSIBILITY OF SUCH DAMAGE.
3008bde987Sscw  */
3108bde987Sscw 
3208bde987Sscw /*
3308bde987Sscw  * Copyright (c) 1993, 1994 Charles M. Hannum.
3408bde987Sscw  * Copyright (c) 1990 William F. Jolitz, TeleMuse
3508bde987Sscw  * All rights reserved.
3608bde987Sscw  *
3708bde987Sscw  * Redistribution and use in source and binary forms, with or without
3808bde987Sscw  * modification, are permitted provided that the following conditions
3908bde987Sscw  * are met:
4008bde987Sscw  * 1. Redistributions of source code must retain the above copyright
4108bde987Sscw  *    notice, this list of conditions and the following disclaimer.
4208bde987Sscw  * 2. Redistributions in binary form must reproduce the above copyright
4308bde987Sscw  *    notice, this list of conditions and the following disclaimer in the
4408bde987Sscw  *    documentation and/or other materials provided with the distribution.
4508bde987Sscw  * 3. All advertising materials mentioning features or use of this software
4608bde987Sscw  *    must display the following acknowledgement:
4708bde987Sscw  *	This software is a component of "386BSD" developed by
4808bde987Sscw  *	William F. Jolitz, TeleMuse.
4908bde987Sscw  * 4. Neither the name of the developer nor the name "386BSD"
5008bde987Sscw  *    may be used to endorse or promote products derived from this software
5108bde987Sscw  *    without specific prior written permission.
5208bde987Sscw  *
5308bde987Sscw  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
5408bde987Sscw  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
5508bde987Sscw  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
5608bde987Sscw  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
5708bde987Sscw  * NOT MAKE USE OF THIS WORK.
5808bde987Sscw  *
5908bde987Sscw  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
6008bde987Sscw  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
6108bde987Sscw  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
6208bde987Sscw  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
6308bde987Sscw  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
6408bde987Sscw  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
6508bde987Sscw  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
6608bde987Sscw  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
6708bde987Sscw  *
6808bde987Sscw  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
6908bde987Sscw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7008bde987Sscw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7108bde987Sscw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
7208bde987Sscw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7308bde987Sscw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
7408bde987Sscw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
7508bde987Sscw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
7608bde987Sscw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
7708bde987Sscw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7808bde987Sscw  * SUCH DAMAGE.
7908bde987Sscw  */
8008bde987Sscw 
8108bde987Sscw /*
8208bde987Sscw  * Device Driver for an MVME68K/MVME88K board's parallel printer port
8308bde987Sscw  * This driver attaches above the board-specific back-end.
8408bde987Sscw  */
8508bde987Sscw 
86365cbd94Slukem #include <sys/cdefs.h>
87*d1579b2dSriastradh __KERNEL_RCSID(0, "$NetBSD: lpt_mvme.c,v 1.19 2018/09/03 16:29:32 riastradh Exp $");
88365cbd94Slukem 
8908bde987Sscw #include <sys/param.h>
9008bde987Sscw #include <sys/systm.h>
9108bde987Sscw #include <sys/proc.h>
9208bde987Sscw #include <sys/buf.h>
9308bde987Sscw #include <sys/kernel.h>
9408bde987Sscw #include <sys/ioctl.h>
9508bde987Sscw #include <sys/uio.h>
9608bde987Sscw #include <sys/device.h>
9708bde987Sscw #include <sys/conf.h>
9808bde987Sscw #include <sys/syslog.h>
9908bde987Sscw 
100a2a38285Sad #include <sys/cpu.h>
101a2a38285Sad #include <sys/bus.h>
10208bde987Sscw 
10308bde987Sscw #include <dev/mvme/lptvar.h>
10408bde987Sscw 
10508bde987Sscw 
10608bde987Sscw #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
10708bde987Sscw #define	STEP		hz/4
10808bde987Sscw 
10908bde987Sscw #define	LPTPRI		(PZERO+8)
11008bde987Sscw #define	LPT_BSIZE	1024
11108bde987Sscw 
11208bde987Sscw #if !defined(DEBUG) || !defined(notdef)
11308bde987Sscw #define LPRINTF(a)
11408bde987Sscw #else
1158ecf8999Scube #define LPRINTF		if (lptdebug) aprint_verbose_dev a
11608bde987Sscw int lptdebug = 1;
11708bde987Sscw #endif
11808bde987Sscw 
11908bde987Sscw #define	LPTUNIT(s)	(minor(s) & 0x0f)
12008bde987Sscw #define	LPTFLAGS(s)	(minor(s) & 0xf0)
12108bde987Sscw 
12218db93c7Sperry static void lpt_wakeup(void *arg);
12318db93c7Sperry static int pushbytes(struct lpt_softc *);
12408bde987Sscw 
12508bde987Sscw extern struct cfdriver lpt_cd;
12608bde987Sscw 
12777a6b82bSgehenna dev_type_open(lptopen);
12877a6b82bSgehenna dev_type_close(lptclose);
12977a6b82bSgehenna dev_type_write(lptwrite);
13077a6b82bSgehenna dev_type_ioctl(lptioctl);
13177a6b82bSgehenna 
13277a6b82bSgehenna const struct cdevsw lpt_cdevsw = {
133a68f9396Sdholland 	.d_open = lptopen,
134a68f9396Sdholland 	.d_close = lptclose,
135a68f9396Sdholland 	.d_read = noread,
136a68f9396Sdholland 	.d_write = lptwrite,
137a68f9396Sdholland 	.d_ioctl = lptioctl,
138a68f9396Sdholland 	.d_stop = nostop,
139a68f9396Sdholland 	.d_tty = notty,
140a68f9396Sdholland 	.d_poll = nopoll,
141a68f9396Sdholland 	.d_mmap = nommap,
142a68f9396Sdholland 	.d_kqfilter = nokqfilter,
143f9228f42Sdholland 	.d_discard = nodiscard,
144a68f9396Sdholland 	.d_flag = 0
14577a6b82bSgehenna };
14608bde987Sscw 
14708bde987Sscw void
lpt_attach_subr(struct lpt_softc * sc)148454af1c0Sdsl lpt_attach_subr(struct lpt_softc *sc)
14908bde987Sscw {
15008bde987Sscw 
15108bde987Sscw 	sc->sc_state = 0;
15288ab7da9Sad 	callout_init(&sc->sc_wakeup_ch, 0);
15308bde987Sscw }
15408bde987Sscw 
15508bde987Sscw /*
15608bde987Sscw  * Reset the printer, then wait until it's selected and not busy.
15708bde987Sscw  */
15808bde987Sscw int
lptopen(dev_t dev,int flag,int mode,struct lwp * l)1593bde75a1Scegger lptopen(dev_t dev, int flag, int mode, struct lwp *l)
16008bde987Sscw {
16108bde987Sscw 	u_char flags;
16208bde987Sscw 	struct lpt_softc *sc;
16308bde987Sscw 	int error;
16408bde987Sscw 	int spin;
16508bde987Sscw 
16608bde987Sscw 	flags = LPTFLAGS(dev);
16708bde987Sscw 
1683bde75a1Scegger 	sc = device_lookup_private(&lpt_cd, LPTUNIT(dev));
16908bde987Sscw 	if (!sc)
17008bde987Sscw 		return (ENXIO);
17108bde987Sscw 
17208bde987Sscw #ifdef DIAGNOSTIC
17308bde987Sscw 	if (sc->sc_state)
1748ecf8999Scube 		aprint_verbose_dev(sc->sc_dev, "stat=0x%x not zero\n",
17508bde987Sscw 		    sc->sc_state);
17608bde987Sscw #endif
17708bde987Sscw 
17808bde987Sscw 	if (sc->sc_state)
17908bde987Sscw 		return (EBUSY);
18008bde987Sscw 
18108bde987Sscw 	sc->sc_state = LPT_INIT;
18208bde987Sscw 	sc->sc_flags = flags;
1838ecf8999Scube 	LPRINTF((sc->sc_dev, "open: flags=0x%x\n", flags));
18408bde987Sscw 
18508bde987Sscw 	if ((flags & LPT_NOPRIME) == 0) {
18608bde987Sscw 		/* assert Input Prime for 100 usec to start up printer */
18708bde987Sscw 		(sc->sc_funcs->lf_iprime) (sc);
18808bde987Sscw 	}
18908bde987Sscw 
19008bde987Sscw 	/* select fast or slow strobe depending on minor device number */
19108bde987Sscw 	if (flags & LPT_FAST_STROBE)
19208bde987Sscw 		(sc->sc_funcs->lf_speed) (sc, LPT_STROBE_FAST);
19308bde987Sscw 	else
19408bde987Sscw 		(sc->sc_funcs->lf_speed) (sc, LPT_STROBE_SLOW);
19508bde987Sscw 
19608bde987Sscw 	/* wait till ready (printer running diagnostics) */
19708bde987Sscw 	for (spin = 0; (sc->sc_funcs->lf_notrdy) (sc, 1); spin += STEP) {
19808bde987Sscw 		if (spin >= TIMEOUT) {
19908bde987Sscw 			sc->sc_state = 0;
20008bde987Sscw 			return (EBUSY);
20108bde987Sscw 		}
20208bde987Sscw 		/* wait 1/4 second, give up if we get a signal */
20353524e44Schristos 		error = tsleep((void *) sc, LPTPRI | PCATCH, "lptopen", STEP);
20408bde987Sscw 		if (error != EWOULDBLOCK) {
20508bde987Sscw 			sc->sc_state = 0;
20608bde987Sscw 			return (error);
20708bde987Sscw 		}
20808bde987Sscw 	}
20908bde987Sscw 
21008bde987Sscw 	sc->sc_inbuf = geteblk(LPT_BSIZE);
21108bde987Sscw 	sc->sc_count = 0;
21208bde987Sscw 	sc->sc_state = LPT_OPEN;
21308bde987Sscw 
21408bde987Sscw 	if ((sc->sc_flags & LPT_NOINTR) == 0)
21508bde987Sscw 		lpt_wakeup(sc);
21608bde987Sscw 
21708bde987Sscw 	(sc->sc_funcs->lf_open) (sc, sc->sc_flags & LPT_NOINTR);
21808bde987Sscw 
2198ecf8999Scube 	LPRINTF((sc->sc_dev, "opened\n"));
22008bde987Sscw 	return (0);
22108bde987Sscw }
22208bde987Sscw 
22308bde987Sscw void
lpt_wakeup(void * arg)224454af1c0Sdsl lpt_wakeup(void *arg)
22508bde987Sscw {
22608bde987Sscw 	struct lpt_softc *sc;
22708bde987Sscw 	int s;
22808bde987Sscw 
22908bde987Sscw 	sc = arg;
23008bde987Sscw 
23108bde987Sscw 	s = spltty();
23208bde987Sscw 	lpt_intr(sc);
23308bde987Sscw 	splx(s);
23408bde987Sscw 
23508bde987Sscw 	callout_reset(&sc->sc_wakeup_ch, STEP, lpt_wakeup, sc);
23608bde987Sscw }
23708bde987Sscw 
23808bde987Sscw /*
23908bde987Sscw  * Close the device, and free the local line buffer.
24008bde987Sscw  */
24108bde987Sscw int
lptclose(dev_t dev,int flag,int mode,struct lwp * l)2423bde75a1Scegger lptclose(dev_t dev, int flag, int mode, struct lwp *l)
24308bde987Sscw {
24408bde987Sscw 	struct lpt_softc *sc;
24508bde987Sscw 
2463bde75a1Scegger 	sc = device_lookup_private(&lpt_cd, LPTUNIT(dev));
24708bde987Sscw 
24808bde987Sscw 	if (sc->sc_count)
24908bde987Sscw 		(void) pushbytes(sc);
25008bde987Sscw 
25108bde987Sscw 	if ((sc->sc_flags & LPT_NOINTR) == 0)
25208bde987Sscw 		callout_stop(&sc->sc_wakeup_ch);
25308bde987Sscw 
25408bde987Sscw 	(sc->sc_funcs->lf_close) (sc);
25508bde987Sscw 
25608bde987Sscw 	sc->sc_state = 0;
257c97e9c4eSad 	brelse(sc->sc_inbuf, 0);
25808bde987Sscw 
2598ecf8999Scube 	LPRINTF((sc->sc_dev, "%s: closed\n"));
26008bde987Sscw 	return (0);
26108bde987Sscw }
26208bde987Sscw 
26308bde987Sscw int
pushbytes(struct lpt_softc * sc)264454af1c0Sdsl pushbytes(struct lpt_softc *sc)
26508bde987Sscw {
26608bde987Sscw 	int s, error, spin, tic;
26708bde987Sscw 
26808bde987Sscw 	if (sc->sc_flags & LPT_NOINTR) {
26908bde987Sscw 		while (sc->sc_count > 0) {
27008bde987Sscw 			spin = 0;
27108bde987Sscw 			while ((sc->sc_funcs->lf_notrdy) (sc, 0)) {
27208bde987Sscw 				if (++spin < sc->sc_spinmax)
27308bde987Sscw 					continue;
27408bde987Sscw 				tic = 0;
27508bde987Sscw 				/* adapt busy-wait algorithm */
27608bde987Sscw 				sc->sc_spinmax++;
27708bde987Sscw 				while ((sc->sc_funcs->lf_notrdy) (sc, 1)) {
27808bde987Sscw 					/* exponential backoff */
27908bde987Sscw 					tic = tic + tic + 1;
28008bde987Sscw 					if (tic > TIMEOUT)
28108bde987Sscw 						tic = TIMEOUT;
28253524e44Schristos 					error = tsleep((void *) sc,
28308bde987Sscw 					    LPTPRI | PCATCH, "lptpsh", tic);
28408bde987Sscw 					if (error != EWOULDBLOCK)
28508bde987Sscw 						return (error);
28608bde987Sscw 				}
28708bde987Sscw 				break;
28808bde987Sscw 			}
28908bde987Sscw 
29008bde987Sscw 			(sc->sc_funcs->lf_wrdata) (sc, *sc->sc_cp++);
29108bde987Sscw 			sc->sc_count--;
29208bde987Sscw 
29308bde987Sscw 			/* adapt busy-wait algorithm */
29408bde987Sscw 			if (spin * 2 + 16 < sc->sc_spinmax)
29508bde987Sscw 				sc->sc_spinmax--;
29608bde987Sscw 		}
29708bde987Sscw 	} else {
29808bde987Sscw 		while (sc->sc_count > 0) {
29908bde987Sscw 			/* if the printer is ready for a char, give it one */
30008bde987Sscw 			if ((sc->sc_state & LPT_OBUSY) == 0) {
3018ecf8999Scube 				LPRINTF((sc->sc_dev, "write %d\n",
30208bde987Sscw 					sc->sc_count));
30308bde987Sscw 				s = spltty();
30408bde987Sscw 				(void) lpt_intr(sc);
30508bde987Sscw 				splx(s);
30608bde987Sscw 			}
30753524e44Schristos 			error = tsleep((void *) sc, LPTPRI | PCATCH,
30808bde987Sscw 			    "lptwrite2", 0);
30908bde987Sscw 			if (error)
31008bde987Sscw 				return (error);
31108bde987Sscw 		}
31208bde987Sscw 	}
31308bde987Sscw 	return (0);
31408bde987Sscw }
31508bde987Sscw 
31608bde987Sscw /*
31708bde987Sscw  * Copy a line from user space to a local buffer, then call putc to get the
31808bde987Sscw  * chars moved to the output queue.
31908bde987Sscw  */
32008bde987Sscw int
lptwrite(dev_t dev,struct uio * uio,int flags)3213bde75a1Scegger lptwrite(dev_t dev, struct uio *uio, int flags)
32208bde987Sscw {
32308bde987Sscw 	struct lpt_softc *sc;
32408bde987Sscw 	size_t n;
32508bde987Sscw 	int error;
32608bde987Sscw 
3273bde75a1Scegger 	sc = device_lookup_private(&lpt_cd, LPTUNIT(dev));
32808bde987Sscw 	error = 0;
32908bde987Sscw 
330*d1579b2dSriastradh 	while ((n = uimin(LPT_BSIZE, uio->uio_resid)) != 0) {
33108bde987Sscw 		uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
33208bde987Sscw 		sc->sc_count = n;
33308bde987Sscw 		error = pushbytes(sc);
33408bde987Sscw 		if (error) {
33508bde987Sscw 			/*
33608bde987Sscw 			 * Return accurate residual if interrupted or timed
33708bde987Sscw 			 * out.
33808bde987Sscw 			 */
33908bde987Sscw 			uio->uio_resid += sc->sc_count;
34008bde987Sscw 			sc->sc_count = 0;
34108bde987Sscw 			return (error);
34208bde987Sscw 		}
34308bde987Sscw 	}
34408bde987Sscw 	return (0);
34508bde987Sscw }
34608bde987Sscw 
34708bde987Sscw /*
34808bde987Sscw  * Handle printer interrupts which occur when the printer is ready to accept
34908bde987Sscw  * another char.
35008bde987Sscw  */
35108bde987Sscw int
lpt_intr(struct lpt_softc * sc)352454af1c0Sdsl lpt_intr(struct lpt_softc *sc)
35308bde987Sscw {
35408bde987Sscw 
35508bde987Sscw 	if (sc->sc_count) {
35608bde987Sscw 		/* send char */
35708bde987Sscw 		(sc->sc_funcs->lf_wrdata) (sc, *sc->sc_cp++);
35808bde987Sscw 		sc->sc_count--;
35908bde987Sscw 		sc->sc_state |= LPT_OBUSY;
36008bde987Sscw 	} else
36108bde987Sscw 		sc->sc_state &= ~LPT_OBUSY;
36208bde987Sscw 
36308bde987Sscw 	if (sc->sc_count == 0) {
36408bde987Sscw 		/* none, wake up the top half to get more */
36553524e44Schristos 		wakeup((void *) sc);
36608bde987Sscw 	}
36708bde987Sscw 
36808bde987Sscw 	return (1);
36908bde987Sscw }
37008bde987Sscw 
37108bde987Sscw /* ARGSUSED */
37208bde987Sscw int
lptioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)373454af1c0Sdsl lptioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
37408bde987Sscw {
37508bde987Sscw 
37608bde987Sscw 	return (ENODEV);
37708bde987Sscw }
378