Home | History | Annotate | Line # | Download | only in dev
lpt.c revision 1.24.26.1
      1 /*	$NetBSD: lpt.c,v 1.24.26.1 2007/03/12 05:47:21 rmind Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 Leo Weppelman
      5  * Copyright (c) 1993, 1994 Charles M. Hannum.
      6  * Copyright (c) 1990 William F. Jolitz, TeleMuse
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This software is a component of "386BSD" developed by
     20  *	William F. Jolitz, TeleMuse.
     21  * 4. Neither the name of the developer nor the name "386BSD"
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Device Driver originally written for AT parallel printer port. Now
     40  * drives the printer port on the YM2149.
     41  *
     42  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
     43  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
     44  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
     45  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
     46  * NOT MAKE USE OF THIS WORK.
     47  *
     48  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
     49  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
     50  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
     51  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
     52  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
     53  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
     54  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
     55  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __KERNEL_RCSID(0, "$NetBSD: lpt.c,v 1.24.26.1 2007/03/12 05:47:21 rmind Exp $");
     60 
     61 #include <sys/param.h>
     62 #include <sys/systm.h>
     63 #include <sys/callout.h>
     64 #include <sys/proc.h>
     65 #include <sys/user.h>
     66 #include <sys/buf.h>
     67 #include <sys/kernel.h>
     68 #include <sys/ioctl.h>
     69 #include <sys/uio.h>
     70 #include <sys/device.h>
     71 #include <sys/conf.h>
     72 #include <sys/syslog.h>
     73 
     74 #include <machine/cpu.h>
     75 #include <machine/iomap.h>
     76 #include <machine/mfp.h>
     77 
     78 #include <atari/dev/ym2149reg.h>
     79 #include <atari/atari/intr.h>
     80 
     81 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     82 #define	STEP		hz/4
     83 
     84 #define	LPTPRI		(PZERO+8)
     85 #define	LPT_BSIZE	1024
     86 
     87 #if !defined(DEBUG) || !defined(notdef)
     88 #define lprintf		if (0) printf
     89 #else
     90 #define lprintf		if (lptdebug) printf
     91 int lptdebug = 1;
     92 #endif
     93 
     94 struct lpt_softc {
     95 	struct device	sc_dev;
     96 	struct callout	sc_wakeup_ch;
     97 	size_t		sc_count;
     98 	struct buf	*sc_inbuf;
     99 	u_char		*sc_cp;
    100 	int		sc_spinmax;
    101 	u_char		sc_state;
    102 #define	LPT_OPEN	0x01	/* device is open */
    103 #define	LPT_OBUSY	0x02	/* printer is busy doing output */
    104 #define	LPT_INIT	0x04	/* waiting to initialize for open */
    105 	u_char		sc_flags;
    106 #define	LPT_AUTOLF	0x20	/* automatic LF on CR XXX: LWP - not yet... */
    107 #define	LPT_NOINTR	0x40	/* do not use interrupt */
    108 };
    109 
    110 #define	LPTUNIT(s)	(minor(s) & 0x1f)
    111 #define	LPTFLAGS(s)	(minor(s) & 0xe0)
    112 #define	NOT_READY()	(MFP->mf_gpip & IO_PBSY)
    113 
    114 /* {b,c}devsw[] function prototypes */
    115 dev_type_open(lpopen);
    116 dev_type_close(lpclose);
    117 dev_type_write(lpwrite);
    118 dev_type_ioctl(lpioctl);
    119 
    120 static void lptwakeup __P((void *arg));
    121 static int pushbytes __P((struct lpt_softc *));
    122 static void lptpseudointr __P((struct lpt_softc *));
    123 int lptintr __P((struct lpt_softc *));
    124 int lpthwintr __P((struct lpt_softc *, int));
    125 
    126 
    127 /*
    128  * Autoconfig stuff
    129  */
    130 static void lpattach __P((struct device *, struct device *, void *));
    131 static int  lpmatch __P((struct device *, struct cfdata *, void *));
    132 
    133 CFATTACH_DECL(lp, sizeof(struct lpt_softc),
    134     lpmatch, lpattach, NULL, NULL);
    135 
    136 extern struct cfdriver lp_cd;
    137 
    138 const struct cdevsw lp_cdevsw = {
    139 	lpopen, lpclose, noread, lpwrite, lpioctl,
    140 	nostop, notty, nopoll, nommap, nokqfilter,
    141 };
    142 
    143 /*ARGSUSED*/
    144 static	int
    145 lpmatch(pdp, cfp, auxp)
    146 struct	device	*pdp;
    147 struct	cfdata	*cfp;
    148 void		*auxp;
    149 {
    150 	static int	lpt_matched = 0;
    151 
    152 	/* Match at most 1 lpt unit */
    153 	if (strcmp((char *)auxp, "lpt") || lpt_matched)
    154 		return 0;
    155 	lpt_matched = 1;
    156 	return (1);
    157 }
    158 
    159 /*ARGSUSED*/
    160 static void
    161 lpattach(pdp, dp, auxp)
    162 struct	device *pdp, *dp;
    163 void	*auxp;
    164 {
    165 	struct lpt_softc *sc = (void *)dp;
    166 
    167 	sc->sc_state = 0;
    168 
    169 	if (intr_establish(0, USER_VEC, 0, (hw_ifun_t)lpthwintr, sc) == NULL)
    170 		printf("lptattach: Can't establish interrupt\n");
    171 	ym2149_strobe(1);
    172 
    173 	printf("\n");
    174 
    175 	callout_init(&sc->sc_wakeup_ch);
    176 }
    177 
    178 /*
    179  * Reset the printer, then wait until it's selected and not busy.
    180  */
    181 int
    182 lpopen(dev, flag, mode, l)
    183 	dev_t		dev;
    184 	int		flag, mode;
    185 	struct lwp	*l;
    186 {
    187 	int			unit = LPTUNIT(dev);
    188 	u_char			flags = LPTFLAGS(dev);
    189 	struct lpt_softc	*sc;
    190 	int 			error;
    191 	int			spin;
    192 	int			sps;
    193 
    194 	if (unit >= lp_cd.cd_ndevs)
    195 		return ENXIO;
    196 	sc = lp_cd.cd_devs[unit];
    197 	if (!sc)
    198 		return ENXIO;
    199 
    200 #ifdef DIAGNOSTIC
    201 	if (sc->sc_state)
    202 		printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
    203 		    sc->sc_state);
    204 #endif
    205 
    206 	if (sc->sc_state)
    207 		return EBUSY;
    208 
    209 	sc->sc_state = LPT_INIT;
    210 	sc->sc_flags = flags;
    211 	lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags);
    212 
    213 	/* wait till ready (printer running diagnostics) */
    214 	for (spin = 0; NOT_READY(); spin += STEP) {
    215 		if (spin >= TIMEOUT) {
    216 			sc->sc_state = 0;
    217 			return EBUSY;
    218 		}
    219 
    220 		/* wait 1/4 second, give up if we get a signal */
    221 		if ((error = tsleep((void *)sc, LPTPRI | PCATCH, "lptopen",
    222 		     STEP)) != EWOULDBLOCK) {
    223 			sc->sc_state = 0;
    224 			return error;
    225 		}
    226 	}
    227 
    228 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    229 	sc->sc_count = 0;
    230 	sc->sc_state = LPT_OPEN;
    231 
    232 	if ((sc->sc_flags & LPT_NOINTR) == 0) {
    233 		lptwakeup(sc);
    234 
    235 		sps = splhigh();
    236 		MFP->mf_imrb |= IB_PBSY;
    237 		MFP->mf_ierb |= IB_PBSY;
    238 		splx(sps);
    239 	}
    240 
    241 	lprintf("%s: opened\n", sc->sc_dev.dv_xname);
    242 	return 0;
    243 }
    244 
    245 void
    246 lptwakeup(arg)
    247 	void *arg;
    248 {
    249 	struct lpt_softc *sc = arg;
    250 
    251 	lptpseudointr(sc);
    252 
    253 	callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
    254 }
    255 
    256 /*
    257  * Close the device, and free the local line buffer.
    258  */
    259 int
    260 lpclose(dev, flag, mode, l)
    261 	dev_t		dev;
    262 	int		flag;
    263 	int		mode;
    264 	struct lwp	*l;
    265 {
    266 	int		 unit = LPTUNIT(dev);
    267 	struct lpt_softc *sc = lp_cd.cd_devs[unit];
    268 	int		 sps;
    269 
    270 	if (sc->sc_count)
    271 		(void) pushbytes(sc);
    272 
    273 	if ((sc->sc_flags & LPT_NOINTR) == 0) {
    274 		callout_stop(&sc->sc_wakeup_ch);
    275 
    276 		sps = splhigh();
    277 		MFP->mf_ierb &= ~IB_PBSY;
    278 		MFP->mf_imrb &= ~IB_PBSY;
    279 		splx(sps);
    280 	}
    281 
    282 	sc->sc_state = 0;
    283 	brelse(sc->sc_inbuf);
    284 
    285 	lprintf("%s: closed\n", sc->sc_dev.dv_xname);
    286 	return 0;
    287 }
    288 
    289 int
    290 pushbytes(sc)
    291 	struct lpt_softc *sc;
    292 {
    293 	int	error;
    294 
    295 	if (sc->sc_flags & LPT_NOINTR) {
    296 		int spin, tic;
    297 
    298 		while (sc->sc_count > 0) {
    299 			spin = 0;
    300 			while (NOT_READY()) {
    301 				if (++spin < sc->sc_spinmax)
    302 					continue;
    303 				tic = 0;
    304 				/* adapt busy-wait algorithm */
    305 				sc->sc_spinmax++;
    306 				while (NOT_READY()) {
    307 					/* exponential backoff */
    308 					tic = tic + tic + 1;
    309 					if (tic > TIMEOUT)
    310 						tic = TIMEOUT;
    311 					error = tsleep((void *)sc,
    312 					    LPTPRI | PCATCH, "lptpsh", tic);
    313 					if (error != EWOULDBLOCK)
    314 						return error;
    315 				}
    316 				break;
    317 			}
    318 
    319 			ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
    320 			ym2149_strobe(0);
    321 			sc->sc_count--;
    322 			ym2149_strobe(1);
    323 
    324 			/* adapt busy-wait algorithm */
    325 			if (spin*2 + 16 < sc->sc_spinmax)
    326 				sc->sc_spinmax--;
    327 		}
    328 	} else {
    329 		while (sc->sc_count > 0) {
    330 			/* if the printer is ready for a char, give it one */
    331 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    332 				lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
    333 				    sc->sc_count);
    334 				(void) lptpseudointr(sc);
    335 			}
    336 			if ((error = tsleep((void *)sc, LPTPRI | PCATCH,
    337 			     "lptwrite2", 0)) != 0)
    338 				return error;
    339 		}
    340 	}
    341 	return 0;
    342 }
    343 
    344 /*
    345  * Copy a line from user space to a local buffer, then call putc to get the
    346  * chars moved to the output queue.
    347  */
    348 int
    349 lpwrite(dev, uio, flags)
    350 	dev_t		dev;
    351 	struct uio	*uio;
    352 	int		flags;
    353 {
    354 	struct lpt_softc *sc = lp_cd.cd_devs[LPTUNIT(dev)];
    355 	size_t n;
    356 	int error = 0;
    357 
    358 	while ((n = min(LPT_BSIZE, uio->uio_resid)) > 0) {
    359 		uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
    360 		sc->sc_count = n;
    361 		error = pushbytes(sc);
    362 		if (error) {
    363 			/*
    364 			 * Return accurate residual if interrupted or timed
    365 			 * out.
    366 			 */
    367 			uio->uio_resid += sc->sc_count;
    368 			sc->sc_count = 0;
    369 			return error;
    370 		}
    371 	}
    372 	return 0;
    373 }
    374 
    375 /*
    376  * Handle printer interrupts which occur when the printer is ready to accept
    377  * another char.
    378  */
    379 int
    380 lptintr(sc)
    381 struct lpt_softc *sc;
    382 {
    383 	/* is printer online and ready for output */
    384 	if (NOT_READY())
    385 		return 0;
    386 
    387 	if (sc->sc_count) {
    388 
    389 		/* send char */
    390 		ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
    391 		ym2149_strobe(0);
    392 		sc->sc_count--;
    393 		ym2149_strobe(1);
    394 		sc->sc_state |= LPT_OBUSY;
    395 	} else
    396 		sc->sc_state &= ~LPT_OBUSY;
    397 
    398 	if (sc->sc_count == 0) {
    399 		/* none, wake up the top half to get more */
    400 		wakeup((void *)sc);
    401 	}
    402 
    403 	return 1;
    404 }
    405 
    406 static void
    407 lptpseudointr(sc)
    408 struct lpt_softc *sc;
    409 {
    410 	int	s;
    411 
    412 	s = spltty();
    413 	lptintr(sc);
    414 	splx(s);
    415 }
    416 
    417 int
    418 lpthwintr(sc, sr)
    419 struct lpt_softc *sc;
    420 int		  sr;
    421 {
    422 	if (!BASEPRI(sr))
    423 		add_sicallback((si_farg)lptpseudointr, sc, 0);
    424 	else lptpseudointr(sc);
    425 	return 1;
    426 }
    427 
    428 int
    429 lpioctl(dev, cmd, data, flag, l)
    430 	dev_t		dev;
    431 	u_long		cmd;
    432 	void *		data;
    433 	int		flag;
    434 	struct lwp	*l;
    435 {
    436 	int error = 0;
    437 
    438 	switch (cmd) {
    439 	default:
    440 		error = ENODEV;
    441 	}
    442 
    443 	return error;
    444 }
    445