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