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