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