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