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