Home | History | Annotate | Line # | Download | only in dev
lpt.c revision 1.33
      1  1.33   tsutsui /*	$NetBSD: lpt.c,v 1.33 2010/04/12 13:05:25 tsutsui 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.23     lukem 
     58  1.23     lukem #include <sys/cdefs.h>
     59  1.33   tsutsui __KERNEL_RCSID(0, "$NetBSD: lpt.c,v 1.33 2010/04/12 13:05:25 tsutsui Exp $");
     60   1.1       leo 
     61   1.1       leo #include <sys/param.h>
     62   1.1       leo #include <sys/systm.h>
     63  1.16   thorpej #include <sys/callout.h>
     64   1.1       leo #include <sys/proc.h>
     65   1.1       leo #include <sys/buf.h>
     66   1.1       leo #include <sys/kernel.h>
     67   1.1       leo #include <sys/ioctl.h>
     68   1.1       leo #include <sys/uio.h>
     69   1.1       leo #include <sys/device.h>
     70   1.1       leo #include <sys/conf.h>
     71   1.1       leo #include <sys/syslog.h>
     72   1.1       leo 
     73   1.1       leo #include <machine/cpu.h>
     74   1.1       leo #include <machine/iomap.h>
     75   1.1       leo #include <machine/mfp.h>
     76  1.31   tsutsui #include <machine/intr.h>
     77   1.1       leo 
     78   1.1       leo #include <atari/dev/ym2149reg.h>
     79   1.1       leo 
     80   1.1       leo #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     81   1.1       leo #define	STEP		hz/4
     82   1.1       leo 
     83   1.1       leo #define	LPTPRI		(PZERO+8)
     84   1.1       leo #define	LPT_BSIZE	1024
     85   1.1       leo 
     86   1.1       leo #if !defined(DEBUG) || !defined(notdef)
     87  1.29      cube #define lprintf		if (0) aprint_error_dev
     88   1.1       leo #else
     89  1.29      cube #define lprintf		if (lptdebug) aprint_error_dev
     90   1.1       leo int lptdebug = 1;
     91   1.1       leo #endif
     92   1.1       leo 
     93   1.1       leo struct lpt_softc {
     94  1.29      cube 	device_t	sc_dev;
     95  1.16   thorpej 	struct callout	sc_wakeup_ch;
     96   1.1       leo 	size_t		sc_count;
     97   1.1       leo 	struct buf	*sc_inbuf;
     98   1.1       leo 	u_char		*sc_cp;
     99   1.1       leo 	int		sc_spinmax;
    100   1.1       leo 	u_char		sc_state;
    101   1.1       leo #define	LPT_OPEN	0x01	/* device is open */
    102   1.1       leo #define	LPT_OBUSY	0x02	/* printer is busy doing output */
    103   1.1       leo #define	LPT_INIT	0x04	/* waiting to initialize for open */
    104   1.1       leo 	u_char		sc_flags;
    105   1.1       leo #define	LPT_AUTOLF	0x20	/* automatic LF on CR XXX: LWP - not yet... */
    106   1.1       leo #define	LPT_NOINTR	0x40	/* do not use interrupt */
    107  1.33   tsutsui 	void		*sc_sicookie;
    108   1.1       leo };
    109   1.1       leo 
    110   1.1       leo #define	LPTUNIT(s)	(minor(s) & 0x1f)
    111   1.1       leo #define	LPTFLAGS(s)	(minor(s) & 0xe0)
    112   1.1       leo #define	NOT_READY()	(MFP->mf_gpip & IO_PBSY)
    113   1.1       leo 
    114   1.1       leo /* {b,c}devsw[] function prototypes */
    115  1.11       leo dev_type_open(lpopen);
    116  1.11       leo dev_type_close(lpclose);
    117  1.11       leo dev_type_write(lpwrite);
    118  1.11       leo dev_type_ioctl(lpioctl);
    119   1.1       leo 
    120  1.29      cube static void lptwakeup (void *arg);
    121  1.29      cube static int pushbytes (struct lpt_softc *);
    122  1.33   tsutsui static void lptpseudointr (void *);
    123  1.29      cube int lptintr (struct lpt_softc *);
    124  1.33   tsutsui int lpthwintr (void *);
    125   1.1       leo 
    126   1.1       leo 
    127   1.1       leo /*
    128   1.1       leo  * Autoconfig stuff
    129   1.1       leo  */
    130  1.29      cube static void lpattach (device_t, device_t, void *);
    131  1.29      cube static int  lpmatch (device_t, cfdata_t , void *);
    132   1.1       leo 
    133  1.29      cube CFATTACH_DECL_NEW(lp, sizeof(struct lpt_softc),
    134  1.21   thorpej     lpmatch, lpattach, NULL, NULL);
    135   1.1       leo 
    136  1.13       leo extern struct cfdriver lp_cd;
    137  1.19   gehenna 
    138  1.19   gehenna const struct cdevsw lp_cdevsw = {
    139  1.19   gehenna 	lpopen, lpclose, noread, lpwrite, lpioctl,
    140  1.22  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
    141  1.19   gehenna };
    142   1.1       leo 
    143   1.1       leo /*ARGSUSED*/
    144   1.1       leo static	int
    145  1.29      cube lpmatch(device_t pdp, cfdata_t cfp, 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.29      cube lpattach(device_t pdp, device_t dp, void *auxp)
    159   1.1       leo {
    160  1.29      cube 	struct lpt_softc *sc = device_private(dp);
    161   1.1       leo 
    162  1.29      cube 	sc->sc_dev = dp;
    163   1.1       leo 	sc->sc_state = 0;
    164   1.1       leo 
    165  1.29      cube 	aprint_normal("\n");
    166  1.29      cube 
    167  1.10       leo 	if (intr_establish(0, USER_VEC, 0, (hw_ifun_t)lpthwintr, sc) == NULL)
    168  1.29      cube 		aprint_error_dev(dp, "Can't establish interrupt\n");
    169   1.1       leo 	ym2149_strobe(1);
    170  1.33   tsutsui 	sc->sc_sicookie = softint_establish(SOFTINT_SERIAL, lptpseudointr, sc);
    171   1.1       leo 
    172  1.26        ad 	callout_init(&sc->sc_wakeup_ch, 0);
    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.29      cube lpopen(dev_t dev, int flag, int mode, struct lwp *l)
    180   1.1       leo {
    181   1.1       leo 	u_char			flags = LPTFLAGS(dev);
    182   1.1       leo 	struct lpt_softc	*sc;
    183   1.1       leo 	int 			error;
    184   1.1       leo 	int			spin;
    185   1.1       leo 	int			sps;
    186   1.1       leo 
    187  1.30    cegger 	sc = device_lookup_private(&lp_cd, LPTUNIT(dev));
    188   1.1       leo 	if (!sc)
    189   1.1       leo 		return ENXIO;
    190   1.1       leo 
    191   1.1       leo #ifdef DIAGNOSTIC
    192   1.1       leo 	if (sc->sc_state)
    193  1.29      cube 		aprint_verbose_dev(sc->sc_dev, "stat=0x%x not zero\n",
    194   1.1       leo 		    sc->sc_state);
    195   1.1       leo #endif
    196   1.1       leo 
    197   1.1       leo 	if (sc->sc_state)
    198   1.1       leo 		return EBUSY;
    199   1.1       leo 
    200   1.1       leo 	sc->sc_state = LPT_INIT;
    201   1.1       leo 	sc->sc_flags = flags;
    202  1.29      cube 	lprintf(sc->sc_dev, "open: flags=0x%x\n", flags);
    203   1.1       leo 
    204   1.1       leo 	/* wait till ready (printer running diagnostics) */
    205   1.1       leo 	for (spin = 0; NOT_READY(); spin += STEP) {
    206   1.1       leo 		if (spin >= TIMEOUT) {
    207   1.1       leo 			sc->sc_state = 0;
    208   1.1       leo 			return EBUSY;
    209   1.1       leo 		}
    210   1.1       leo 
    211   1.1       leo 		/* wait 1/4 second, give up if we get a signal */
    212  1.25  christos 		if ((error = tsleep((void *)sc, LPTPRI | PCATCH, "lptopen",
    213   1.2       leo 		     STEP)) != EWOULDBLOCK) {
    214   1.1       leo 			sc->sc_state = 0;
    215   1.1       leo 			return error;
    216   1.1       leo 		}
    217   1.1       leo 	}
    218   1.1       leo 
    219   1.1       leo 	sc->sc_inbuf = geteblk(LPT_BSIZE);
    220   1.1       leo 	sc->sc_count = 0;
    221   1.1       leo 	sc->sc_state = LPT_OPEN;
    222   1.1       leo 
    223   1.1       leo 	if ((sc->sc_flags & LPT_NOINTR) == 0) {
    224   1.1       leo 		lptwakeup(sc);
    225   1.1       leo 
    226   1.1       leo 		sps = splhigh();
    227   1.1       leo 		MFP->mf_imrb |= IB_PBSY;
    228   1.1       leo 		MFP->mf_ierb |= IB_PBSY;
    229   1.1       leo 		splx(sps);
    230   1.1       leo 	}
    231   1.1       leo 
    232  1.29      cube 	lprintf(sc->sc_dev, "opened\n");
    233   1.1       leo 	return 0;
    234   1.1       leo }
    235   1.1       leo 
    236   1.1       leo void
    237  1.29      cube lptwakeup(void *arg)
    238   1.1       leo {
    239   1.1       leo 	struct lpt_softc *sc = arg;
    240   1.1       leo 
    241  1.10       leo 	lptpseudointr(sc);
    242   1.1       leo 
    243  1.16   thorpej 	callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc);
    244   1.1       leo }
    245   1.1       leo 
    246   1.1       leo /*
    247   1.1       leo  * Close the device, and free the local line buffer.
    248   1.1       leo  */
    249   1.1       leo int
    250  1.29      cube lpclose(dev_t dev, int flag, int mode, struct lwp *l)
    251   1.1       leo {
    252  1.30    cegger 	struct lpt_softc *sc = device_lookup_private(&lp_cd, LPTUNIT(dev));
    253   1.1       leo 	int		 sps;
    254   1.1       leo 
    255   1.1       leo 	if (sc->sc_count)
    256   1.1       leo 		(void) pushbytes(sc);
    257   1.1       leo 
    258   1.1       leo 	if ((sc->sc_flags & LPT_NOINTR) == 0) {
    259  1.16   thorpej 		callout_stop(&sc->sc_wakeup_ch);
    260   1.1       leo 
    261   1.1       leo 		sps = splhigh();
    262   1.1       leo 		MFP->mf_ierb &= ~IB_PBSY;
    263   1.1       leo 		MFP->mf_imrb &= ~IB_PBSY;
    264   1.1       leo 		splx(sps);
    265   1.1       leo 	}
    266   1.1       leo 
    267   1.1       leo 	sc->sc_state = 0;
    268  1.27        ad 	brelse(sc->sc_inbuf, 0);
    269   1.1       leo 
    270  1.29      cube 	lprintf(sc->sc_dev, "closed\n");
    271   1.1       leo 	return 0;
    272   1.1       leo }
    273   1.1       leo 
    274   1.1       leo int
    275  1.29      cube pushbytes(struct lpt_softc *sc)
    276   1.1       leo {
    277   1.1       leo 	int	error;
    278   1.1       leo 
    279   1.1       leo 	if (sc->sc_flags & LPT_NOINTR) {
    280   1.1       leo 		int spin, tic;
    281   1.1       leo 
    282   1.1       leo 		while (sc->sc_count > 0) {
    283   1.1       leo 			spin = 0;
    284   1.1       leo 			while (NOT_READY()) {
    285   1.1       leo 				if (++spin < sc->sc_spinmax)
    286   1.1       leo 					continue;
    287   1.1       leo 				tic = 0;
    288   1.1       leo 				/* adapt busy-wait algorithm */
    289   1.1       leo 				sc->sc_spinmax++;
    290   1.1       leo 				while (NOT_READY()) {
    291   1.1       leo 					/* exponential backoff */
    292   1.1       leo 					tic = tic + tic + 1;
    293   1.1       leo 					if (tic > TIMEOUT)
    294   1.1       leo 						tic = TIMEOUT;
    295  1.25  christos 					error = tsleep((void *)sc,
    296   1.1       leo 					    LPTPRI | PCATCH, "lptpsh", tic);
    297   1.1       leo 					if (error != EWOULDBLOCK)
    298   1.1       leo 						return error;
    299   1.1       leo 				}
    300   1.1       leo 				break;
    301   1.1       leo 			}
    302   1.1       leo 
    303   1.1       leo 			ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
    304   1.1       leo 			ym2149_strobe(0);
    305   1.1       leo 			sc->sc_count--;
    306   1.1       leo 			ym2149_strobe(1);
    307   1.1       leo 
    308   1.1       leo 			/* adapt busy-wait algorithm */
    309   1.1       leo 			if (spin*2 + 16 < sc->sc_spinmax)
    310   1.1       leo 				sc->sc_spinmax--;
    311   1.1       leo 		}
    312   1.1       leo 	} else {
    313   1.1       leo 		while (sc->sc_count > 0) {
    314   1.1       leo 			/* if the printer is ready for a char, give it one */
    315   1.1       leo 			if ((sc->sc_state & LPT_OBUSY) == 0) {
    316  1.29      cube 				lprintf(sc->sc_dev, "write %d\n",
    317   1.1       leo 				    sc->sc_count);
    318  1.10       leo 				(void) lptpseudointr(sc);
    319   1.1       leo 			}
    320  1.25  christos 			if ((error = tsleep((void *)sc, LPTPRI | PCATCH,
    321   1.2       leo 			     "lptwrite2", 0)) != 0)
    322   1.1       leo 				return error;
    323   1.1       leo 		}
    324   1.1       leo 	}
    325   1.1       leo 	return 0;
    326   1.1       leo }
    327   1.1       leo 
    328   1.1       leo /*
    329   1.1       leo  * Copy a line from user space to a local buffer, then call putc to get the
    330   1.1       leo  * chars moved to the output queue.
    331   1.1       leo  */
    332   1.1       leo int
    333  1.29      cube lpwrite(dev_t dev, struct uio *uio, int flags)
    334   1.1       leo {
    335  1.30    cegger 	struct lpt_softc *sc = device_lookup_private(&lp_cd,LPTUNIT(dev));
    336   1.1       leo 	size_t n;
    337   1.1       leo 	int error = 0;
    338   1.1       leo 
    339   1.2       leo 	while ((n = min(LPT_BSIZE, uio->uio_resid)) > 0) {
    340   1.1       leo 		uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
    341   1.1       leo 		sc->sc_count = n;
    342   1.1       leo 		error = pushbytes(sc);
    343   1.1       leo 		if (error) {
    344   1.1       leo 			/*
    345   1.1       leo 			 * Return accurate residual if interrupted or timed
    346   1.1       leo 			 * out.
    347   1.1       leo 			 */
    348   1.1       leo 			uio->uio_resid += sc->sc_count;
    349   1.1       leo 			sc->sc_count = 0;
    350   1.1       leo 			return error;
    351   1.1       leo 		}
    352   1.1       leo 	}
    353   1.1       leo 	return 0;
    354   1.1       leo }
    355   1.1       leo 
    356   1.1       leo /*
    357   1.1       leo  * Handle printer interrupts which occur when the printer is ready to accept
    358   1.1       leo  * another char.
    359   1.1       leo  */
    360   1.1       leo int
    361  1.29      cube lptintr(struct lpt_softc *sc)
    362   1.1       leo {
    363   1.1       leo 	/* is printer online and ready for output */
    364   1.1       leo 	if (NOT_READY())
    365   1.1       leo 		return 0;
    366   1.1       leo 
    367   1.1       leo 	if (sc->sc_count) {
    368   1.1       leo 
    369   1.1       leo 		/* send char */
    370   1.1       leo 		ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
    371   1.1       leo 		ym2149_strobe(0);
    372   1.1       leo 		sc->sc_count--;
    373   1.1       leo 		ym2149_strobe(1);
    374   1.1       leo 		sc->sc_state |= LPT_OBUSY;
    375   1.1       leo 	} else
    376   1.1       leo 		sc->sc_state &= ~LPT_OBUSY;
    377   1.1       leo 
    378   1.1       leo 	if (sc->sc_count == 0) {
    379   1.1       leo 		/* none, wake up the top half to get more */
    380  1.25  christos 		wakeup((void *)sc);
    381   1.1       leo 	}
    382   1.1       leo 
    383   1.7       leo 	return 1;
    384   1.7       leo }
    385   1.7       leo 
    386   1.7       leo static void
    387  1.33   tsutsui lptpseudointr(void *arg)
    388   1.7       leo {
    389  1.33   tsutsui 	struct lpt_softc *sc;
    390   1.7       leo 	int	s;
    391   1.7       leo 
    392  1.33   tsutsui 	sc = arg;
    393   1.7       leo 	s = spltty();
    394  1.10       leo 	lptintr(sc);
    395   1.7       leo 	splx(s);
    396   1.7       leo }
    397   1.7       leo 
    398   1.7       leo int
    399  1.33   tsutsui lpthwintr(void *arg)
    400   1.7       leo {
    401  1.33   tsutsui 	struct lpt_softc *sc;
    402  1.33   tsutsui 
    403  1.33   tsutsui 	sc = arg;
    404  1.33   tsutsui 	softint_schedule(sc->sc_sicookie);
    405   1.1       leo 	return 1;
    406   1.1       leo }
    407   1.1       leo 
    408   1.1       leo int
    409  1.29      cube lpioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    410   1.1       leo {
    411   1.1       leo 	int error = 0;
    412   1.1       leo 
    413   1.1       leo 	switch (cmd) {
    414   1.1       leo 	default:
    415   1.1       leo 		error = ENODEV;
    416   1.1       leo 	}
    417   1.1       leo 
    418   1.1       leo 	return error;
    419   1.1       leo }
    420