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