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