lpt.c revision 1.16 1 /* $NetBSD: lpt.c,v 1.16 2000/03/23 06:36:04 thorpej 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 /*ARGSUSED*/
137 static int
138 lpmatch(pdp, cfp, auxp)
139 struct device *pdp;
140 struct cfdata *cfp;
141 void *auxp;
142 {
143 if (!strcmp((char *)auxp, "lpt") && cfp->cf_unit == 0)
144 return (1);
145 return (0);
146 }
147
148 /*ARGSUSED*/
149 static void
150 lpattach(pdp, dp, auxp)
151 struct device *pdp, *dp;
152 void *auxp;
153 {
154 struct lpt_softc *sc = (void *)dp;
155
156 sc->sc_state = 0;
157
158 if (intr_establish(0, USER_VEC, 0, (hw_ifun_t)lpthwintr, sc) == NULL)
159 printf("lptattach: Can't establish interrupt\n");
160 ym2149_strobe(1);
161
162 printf("\n");
163
164 callout_init(&sc->sc_wakeup_ch);
165 }
166
167 /*
168 * Reset the printer, then wait until it's selected and not busy.
169 */
170 int
171 lpopen(dev, flag, mode, p)
172 dev_t dev;
173 int flag, mode;
174 struct proc *p;
175 {
176 int unit = LPTUNIT(dev);
177 u_char flags = LPTFLAGS(dev);
178 struct lpt_softc *sc;
179 int error;
180 int spin;
181 int sps;
182
183 if (unit >= lp_cd.cd_ndevs)
184 return ENXIO;
185 sc = lp_cd.cd_devs[unit];
186 if (!sc)
187 return ENXIO;
188
189 #ifdef DIAGNOSTIC
190 if (sc->sc_state)
191 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname,
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("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, 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((caddr_t)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("%s: opened\n", sc->sc_dev.dv_xname);
231 return 0;
232 }
233
234 void
235 lptwakeup(arg)
236 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, flag, mode, p)
250 dev_t dev;
251 int flag;
252 int mode;
253 struct proc *p;
254 {
255 int unit = LPTUNIT(dev);
256 struct lpt_softc *sc = lp_cd.cd_devs[unit];
257 int sps;
258
259 if (sc->sc_count)
260 (void) pushbytes(sc);
261
262 if ((sc->sc_flags & LPT_NOINTR) == 0) {
263 callout_stop(&sc->sc_wakeup_ch);
264
265 sps = splhigh();
266 MFP->mf_ierb &= ~IB_PBSY;
267 MFP->mf_imrb &= ~IB_PBSY;
268 splx(sps);
269 }
270
271 sc->sc_state = 0;
272 brelse(sc->sc_inbuf);
273
274 lprintf("%s: closed\n", sc->sc_dev.dv_xname);
275 return 0;
276 }
277
278 int
279 pushbytes(sc)
280 struct lpt_softc *sc;
281 {
282 int error;
283
284 if (sc->sc_flags & LPT_NOINTR) {
285 int spin, tic;
286
287 while (sc->sc_count > 0) {
288 spin = 0;
289 while (NOT_READY()) {
290 if (++spin < sc->sc_spinmax)
291 continue;
292 tic = 0;
293 /* adapt busy-wait algorithm */
294 sc->sc_spinmax++;
295 while (NOT_READY()) {
296 /* exponential backoff */
297 tic = tic + tic + 1;
298 if (tic > TIMEOUT)
299 tic = TIMEOUT;
300 error = tsleep((caddr_t)sc,
301 LPTPRI | PCATCH, "lptpsh", tic);
302 if (error != EWOULDBLOCK)
303 return error;
304 }
305 break;
306 }
307
308 ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
309 ym2149_strobe(0);
310 sc->sc_count--;
311 ym2149_strobe(1);
312
313 /* adapt busy-wait algorithm */
314 if (spin*2 + 16 < sc->sc_spinmax)
315 sc->sc_spinmax--;
316 }
317 } else {
318 while (sc->sc_count > 0) {
319 /* if the printer is ready for a char, give it one */
320 if ((sc->sc_state & LPT_OBUSY) == 0) {
321 lprintf("%s: write %d\n", sc->sc_dev.dv_xname,
322 sc->sc_count);
323 (void) lptpseudointr(sc);
324 }
325 if ((error = tsleep((caddr_t)sc, LPTPRI | PCATCH,
326 "lptwrite2", 0)) != 0)
327 return error;
328 }
329 }
330 return 0;
331 }
332
333 /*
334 * Copy a line from user space to a local buffer, then call putc to get the
335 * chars moved to the output queue.
336 */
337 int
338 lpwrite(dev, uio, flags)
339 dev_t dev;
340 struct uio *uio;
341 int flags;
342 {
343 struct lpt_softc *sc = lp_cd.cd_devs[LPTUNIT(dev)];
344 size_t n;
345 int error = 0;
346
347 while ((n = min(LPT_BSIZE, uio->uio_resid)) > 0) {
348 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio);
349 sc->sc_count = n;
350 error = pushbytes(sc);
351 if (error) {
352 /*
353 * Return accurate residual if interrupted or timed
354 * out.
355 */
356 uio->uio_resid += sc->sc_count;
357 sc->sc_count = 0;
358 return error;
359 }
360 }
361 return 0;
362 }
363
364 /*
365 * Handle printer interrupts which occur when the printer is ready to accept
366 * another char.
367 */
368 int
369 lptintr(sc)
370 struct lpt_softc *sc;
371 {
372 /* is printer online and ready for output */
373 if (NOT_READY())
374 return 0;
375
376 if (sc->sc_count) {
377
378 /* send char */
379 ym2149_write_ioport(YM_IOB, *sc->sc_cp++);
380 ym2149_strobe(0);
381 sc->sc_count--;
382 ym2149_strobe(1);
383 sc->sc_state |= LPT_OBUSY;
384 } else
385 sc->sc_state &= ~LPT_OBUSY;
386
387 if (sc->sc_count == 0) {
388 /* none, wake up the top half to get more */
389 wakeup((caddr_t)sc);
390 }
391
392 return 1;
393 }
394
395 static void
396 lptpseudointr(sc)
397 struct lpt_softc *sc;
398 {
399 int s;
400
401 s = spltty();
402 lptintr(sc);
403 splx(s);
404 }
405
406 int
407 lpthwintr(sc, sr)
408 struct lpt_softc *sc;
409 int sr;
410 {
411 if (!BASEPRI(sr))
412 add_sicallback((si_farg)lptpseudointr, sc, 0);
413 else lptpseudointr(sc);
414 return 1;
415 }
416
417 int
418 lpioctl(dev, cmd, data, flag, p)
419 dev_t dev;
420 u_long cmd;
421 caddr_t data;
422 int flag;
423 struct proc *p;
424 {
425 int error = 0;
426
427 switch (cmd) {
428 default:
429 error = ENODEV;
430 }
431
432 return error;
433 }
434