par.c revision 1.15 1 1.15 christos /* $NetBSD: par.c,v 1.15 1996/10/13 03:07:28 christos Exp $ */
2 1.9 cgd
3 1.1 mw /*
4 1.1 mw * Copyright (c) 1982, 1990 The Regents of the University of California.
5 1.1 mw * All rights reserved.
6 1.1 mw *
7 1.1 mw * Redistribution and use in source and binary forms, with or without
8 1.1 mw * modification, are permitted provided that the following conditions
9 1.1 mw * are met:
10 1.1 mw * 1. Redistributions of source code must retain the above copyright
11 1.1 mw * notice, this list of conditions and the following disclaimer.
12 1.1 mw * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mw * notice, this list of conditions and the following disclaimer in the
14 1.1 mw * documentation and/or other materials provided with the distribution.
15 1.1 mw * 3. All advertising materials mentioning features or use of this software
16 1.1 mw * must display the following acknowledgement:
17 1.1 mw * This product includes software developed by the University of
18 1.1 mw * California, Berkeley and its contributors.
19 1.1 mw * 4. Neither the name of the University nor the names of its contributors
20 1.1 mw * may be used to endorse or promote products derived from this software
21 1.1 mw * without specific prior written permission.
22 1.1 mw *
23 1.1 mw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 mw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 mw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 mw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 mw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 mw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 mw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 mw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 mw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 mw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 mw * SUCH DAMAGE.
34 1.1 mw *
35 1.10 cgd * @(#)ppi.c 7.3 (Berkeley) 12/16/90
36 1.1 mw */
37 1.1 mw
38 1.1 mw /*
39 1.1 mw * parallel port interface
40 1.1 mw */
41 1.1 mw
42 1.2 mw #include "par.h"
43 1.1 mw #if NPAR > 0
44 1.1 mw
45 1.6 chopps #include <sys/param.h>
46 1.6 chopps #include <sys/errno.h>
47 1.6 chopps #include <sys/uio.h>
48 1.7 chopps #include <sys/device.h>
49 1.6 chopps #include <sys/malloc.h>
50 1.6 chopps #include <sys/file.h>
51 1.6 chopps #include <sys/systm.h>
52 1.13 veego #include <sys/proc.h>
53 1.1 mw
54 1.7 chopps #include <amiga/amiga/device.h>
55 1.7 chopps #include <amiga/amiga/cia.h>
56 1.6 chopps #include <amiga/dev/parioctl.h>
57 1.1 mw
58 1.13 veego #include <sys/conf.h>
59 1.13 veego #include <machine/conf.h>
60 1.1 mw
61 1.1 mw struct par_softc {
62 1.1 mw int sc_flags;
63 1.1 mw struct parparam sc_param;
64 1.1 mw #define sc_burst sc_param.burst
65 1.1 mw #define sc_timo sc_param.timo
66 1.1 mw #define sc_delay sc_param.delay
67 1.7 chopps } *par_softcp;
68 1.7 chopps
69 1.7 chopps #define getparsp(x) (x > 0 ? NULL : par_softcp)
70 1.1 mw
71 1.1 mw /* sc_flags values */
72 1.1 mw #define PARF_ALIVE 0x01
73 1.1 mw #define PARF_OPEN 0x02
74 1.1 mw #define PARF_UIO 0x04
75 1.1 mw #define PARF_TIMO 0x08
76 1.1 mw #define PARF_DELAY 0x10
77 1.1 mw #define PARF_OREAD 0x40
78 1.1 mw #define PARF_OWRITE 0x80
79 1.1 mw
80 1.1 mw #define UNIT(x) minor(x)
81 1.1 mw
82 1.1 mw #ifdef DEBUG
83 1.2 mw int pardebug = 0;
84 1.1 mw #define PDB_FOLLOW 0x01
85 1.1 mw #define PDB_IO 0x02
86 1.2 mw #define PDB_INTERRUPT 0x04
87 1.1 mw #define PDB_NOCHECK 0x80
88 1.1 mw #endif
89 1.1 mw
90 1.13 veego int parrw __P((dev_t, struct uio *));
91 1.13 veego int parhztoms __P((int));
92 1.13 veego int parmstohz __P((int));
93 1.13 veego int parsend __P((u_char *, int));
94 1.13 veego int parreceive __P((u_char *, int));
95 1.13 veego int parsendch __P((u_char));
96 1.13 veego
97 1.7 chopps void partimo __P((void *));
98 1.7 chopps void parstart __P((void *));
99 1.7 chopps void parintr __P((void *));
100 1.7 chopps
101 1.7 chopps void parattach __P((struct device *, struct device *, void *));
102 1.12 thorpej int parmatch __P((struct device *, void *, void *));
103 1.7 chopps
104 1.12 thorpej struct cfattach par_ca = {
105 1.12 thorpej sizeof(struct device), parmatch, parattach
106 1.12 thorpej };
107 1.12 thorpej
108 1.12 thorpej struct cfdriver par_cd = {
109 1.12 thorpej NULL, "par", DV_DULL, NULL, 0
110 1.12 thorpej };
111 1.7 chopps
112 1.7 chopps /*ARGSUSED*/
113 1.7 chopps int
114 1.12 thorpej parmatch(pdp, match, auxp)
115 1.7 chopps struct device *pdp;
116 1.12 thorpej void *match, *auxp;
117 1.7 chopps {
118 1.12 thorpej struct cfdata *cfp = match;
119 1.12 thorpej
120 1.7 chopps if (matchname((char *)auxp, "par") && cfp->cf_unit == 0)
121 1.7 chopps return(1);
122 1.7 chopps return(0);
123 1.7 chopps }
124 1.7 chopps
125 1.7 chopps void
126 1.7 chopps parattach(pdp, dp, auxp)
127 1.7 chopps struct device *pdp, *dp;
128 1.7 chopps void *auxp;
129 1.1 mw {
130 1.7 chopps par_softcp = (struct par_softc *)dp;
131 1.1 mw
132 1.1 mw #ifdef DEBUG
133 1.7 chopps if ((pardebug & PDB_NOCHECK) == 0)
134 1.1 mw #endif
135 1.7 chopps par_softcp->sc_flags = PARF_ALIVE;
136 1.15 christos printf("\n");
137 1.1 mw }
138 1.1 mw
139 1.13 veego int
140 1.13 veego paropen(dev, flags, mode, p)
141 1.7 chopps dev_t dev;
142 1.13 veego int flags;
143 1.13 veego int mode;
144 1.13 veego struct proc *p;
145 1.1 mw {
146 1.7 chopps int unit = UNIT(dev);
147 1.7 chopps struct par_softc *sc = getparsp(unit);
148 1.1 mw
149 1.7 chopps if (unit >= NPAR || (sc->sc_flags & PARF_ALIVE) == 0)
150 1.7 chopps return(ENXIO);
151 1.1 mw #ifdef DEBUG
152 1.7 chopps if (pardebug & PDB_FOLLOW) {
153 1.15 christos printf("paropen(%x, %x): flags %x, ",
154 1.7 chopps dev, flags, sc->sc_flags);
155 1.15 christos printf ("port = $%x\n", ((ciab.pra ^ CIAB_PRA_SEL)
156 1.7 chopps & (CIAB_PRA_SEL|CIAB_PRA_BUSY|CIAB_PRA_POUT)));
157 1.7 chopps }
158 1.1 mw #endif
159 1.7 chopps if (sc->sc_flags & PARF_OPEN)
160 1.7 chopps return(EBUSY);
161 1.7 chopps /* can either read or write, but not both */
162 1.7 chopps if ((flags & (FREAD|FWRITE)) == (FREAD|FWRITE))
163 1.7 chopps return EINVAL;
164 1.1 mw
165 1.7 chopps sc->sc_flags |= PARF_OPEN;
166 1.7 chopps
167 1.7 chopps if (flags & FREAD)
168 1.7 chopps sc->sc_flags |= PARF_OREAD;
169 1.7 chopps else
170 1.7 chopps sc->sc_flags |= PARF_OWRITE;
171 1.7 chopps
172 1.7 chopps sc->sc_burst = PAR_BURST;
173 1.7 chopps sc->sc_timo = parmstohz(PAR_TIMO);
174 1.7 chopps sc->sc_delay = parmstohz(PAR_DELAY);
175 1.7 chopps /* enable interrupts for CIAA-FLG */
176 1.7 chopps ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_FLG;
177 1.7 chopps return(0);
178 1.1 mw }
179 1.1 mw
180 1.13 veego int
181 1.13 veego parclose(dev, flags, mode, p)
182 1.13 veego dev_t dev;
183 1.13 veego int flags;
184 1.13 veego int mode;
185 1.13 veego struct proc *p;
186 1.1 mw {
187 1.7 chopps int unit = UNIT(dev);
188 1.7 chopps struct par_softc *sc = getparsp(unit);
189 1.1 mw
190 1.1 mw #ifdef DEBUG
191 1.1 mw if (pardebug & PDB_FOLLOW)
192 1.15 christos printf("parclose(%x, %x): flags %x\n",
193 1.1 mw dev, flags, sc->sc_flags);
194 1.1 mw #endif
195 1.1 mw sc->sc_flags &= ~(PARF_OPEN|PARF_OREAD|PARF_OWRITE);
196 1.1 mw /* don't allow interrupts for CIAA-FLG any longer */
197 1.1 mw ciaa.icr = CIA_ICR_FLG;
198 1.1 mw return(0);
199 1.1 mw }
200 1.1 mw
201 1.7 chopps void
202 1.7 chopps parstart(arg)
203 1.7 chopps void *arg;
204 1.1 mw {
205 1.7 chopps struct par_softc *sc;
206 1.7 chopps int unit;
207 1.7 chopps
208 1.7 chopps unit = (int)arg;
209 1.7 chopps sc = getparsp(unit);
210 1.1 mw #ifdef DEBUG
211 1.7 chopps if (pardebug & PDB_FOLLOW)
212 1.15 christos printf("parstart(%x)\n", unit);
213 1.1 mw #endif
214 1.7 chopps sc->sc_flags &= ~PARF_DELAY;
215 1.7 chopps wakeup(sc);
216 1.1 mw }
217 1.1 mw
218 1.7 chopps void
219 1.7 chopps partimo(arg)
220 1.7 chopps void *arg;
221 1.1 mw {
222 1.7 chopps struct par_softc *sc;
223 1.7 chopps int unit;
224 1.7 chopps
225 1.7 chopps unit = (int) arg;
226 1.7 chopps sc = getparsp(unit);
227 1.1 mw #ifdef DEBUG
228 1.7 chopps if (pardebug & PDB_FOLLOW)
229 1.15 christos printf("partimo(%x)\n", unit);
230 1.1 mw #endif
231 1.7 chopps sc->sc_flags &= ~(PARF_UIO|PARF_TIMO);
232 1.7 chopps wakeup(sc);
233 1.1 mw }
234 1.1 mw
235 1.13 veego int
236 1.13 veego parread(dev, uio, flags)
237 1.13 veego dev_t dev;
238 1.13 veego struct uio *uio;
239 1.13 veego int flags;
240 1.1 mw {
241 1.1 mw
242 1.1 mw #ifdef DEBUG
243 1.13 veego if (pardebug & PDB_FOLLOW)
244 1.15 christos printf("parread(%x, %p)\n", dev, uio);
245 1.1 mw #endif
246 1.13 veego return (parrw(dev, uio));
247 1.1 mw }
248 1.1 mw
249 1.13 veego
250 1.13 veego int
251 1.13 veego parwrite(dev, uio, flags)
252 1.13 veego dev_t dev;
253 1.13 veego struct uio *uio;
254 1.13 veego int flags;
255 1.1 mw {
256 1.1 mw
257 1.1 mw #ifdef DEBUG
258 1.13 veego if (pardebug & PDB_FOLLOW)
259 1.15 christos printf("parwrite(%x, %p)\n", dev, uio);
260 1.1 mw #endif
261 1.13 veego return (parrw(dev, uio));
262 1.1 mw }
263 1.1 mw
264 1.13 veego
265 1.13 veego int
266 1.1 mw parrw(dev, uio)
267 1.1 mw dev_t dev;
268 1.1 mw register struct uio *uio;
269 1.1 mw {
270 1.1 mw int unit = UNIT(dev);
271 1.7 chopps register struct par_softc *sc = getparsp(unit);
272 1.1 mw register int s, len, cnt;
273 1.1 mw register char *cp;
274 1.1 mw int error = 0, gotdata = 0;
275 1.1 mw int buflen;
276 1.1 mw char *buf;
277 1.1 mw
278 1.13 veego len = 0;
279 1.13 veego cnt = 0;
280 1.2 mw if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
281 1.1 mw return EINVAL;
282 1.1 mw
283 1.1 mw if (uio->uio_resid == 0)
284 1.1 mw return(0);
285 1.1 mw
286 1.1 mw #ifdef DEBUG
287 1.1 mw if (pardebug & (PDB_FOLLOW|PDB_IO))
288 1.15 christos printf("parrw(%x, %p, %c): burst %d, timo %d, resid %x\n",
289 1.1 mw dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
290 1.1 mw sc->sc_burst, sc->sc_timo, uio->uio_resid);
291 1.1 mw #endif
292 1.8 chopps buflen = min(sc->sc_burst, uio->uio_resid);
293 1.1 mw buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
294 1.1 mw sc->sc_flags |= PARF_UIO;
295 1.2 mw if (sc->sc_timo > 0)
296 1.2 mw {
297 1.2 mw sc->sc_flags |= PARF_TIMO;
298 1.7 chopps timeout(partimo, (void *)unit, sc->sc_timo);
299 1.1 mw }
300 1.2 mw while (uio->uio_resid > 0)
301 1.2 mw {
302 1.8 chopps len = min(buflen, uio->uio_resid);
303 1.2 mw cp = buf;
304 1.2 mw if (uio->uio_rw == UIO_WRITE)
305 1.2 mw {
306 1.2 mw error = uiomove(cp, len, uio);
307 1.2 mw if (error)
308 1.2 mw break;
309 1.2 mw }
310 1.2 mw again:
311 1.2 mw s = splbio();
312 1.2 mw #if 0
313 1.2 mw if ((sc->sc_flags & PARF_UIO) && hpibreq(&sc->sc_dq) == 0)
314 1.2 mw sleep(sc, PRIBIO+1);
315 1.2 mw #endif
316 1.2 mw /*
317 1.2 mw * Check if we timed out during sleep or uiomove
318 1.2 mw */
319 1.2 mw (void) splsoftclock();
320 1.2 mw if ((sc->sc_flags & PARF_UIO) == 0)
321 1.2 mw {
322 1.2 mw #ifdef DEBUG
323 1.2 mw if (pardebug & PDB_IO)
324 1.15 christos printf("parrw: uiomove/sleep timo, flags %x\n",
325 1.2 mw sc->sc_flags);
326 1.2 mw #endif
327 1.2 mw if (sc->sc_flags & PARF_TIMO)
328 1.2 mw {
329 1.7 chopps untimeout(partimo, (void *)unit);
330 1.2 mw sc->sc_flags &= ~PARF_TIMO;
331 1.2 mw }
332 1.2 mw splx(s);
333 1.2 mw break;
334 1.2 mw }
335 1.2 mw splx(s);
336 1.2 mw /*
337 1.2 mw * Perform the operation
338 1.2 mw */
339 1.2 mw if (uio->uio_rw == UIO_WRITE)
340 1.2 mw cnt = parsend (cp, len);
341 1.2 mw else
342 1.2 mw cnt = parreceive (cp, len);
343 1.2 mw
344 1.2 mw if (cnt < 0)
345 1.2 mw {
346 1.2 mw error = -cnt;
347 1.2 mw break;
348 1.2 mw }
349 1.2 mw
350 1.2 mw s = splbio();
351 1.1 mw #if 0
352 1.2 mw hpibfree(&sc->sc_dq);
353 1.1 mw #endif
354 1.1 mw #ifdef DEBUG
355 1.1 mw if (pardebug & PDB_IO)
356 1.15 christos printf("parrw: %s(%p, %d) -> %d\n",
357 1.2 mw uio->uio_rw == UIO_READ ? "recv" : "send", cp, len, cnt);
358 1.1 mw #endif
359 1.1 mw splx(s);
360 1.2 mw if (uio->uio_rw == UIO_READ)
361 1.2 mw {
362 1.2 mw if (cnt)
363 1.2 mw {
364 1.2 mw error = uiomove(cp, cnt, uio);
365 1.2 mw if (error)
366 1.2 mw break;
367 1.2 mw gotdata++;
368 1.2 mw }
369 1.2 mw /*
370 1.2 mw * Didn't get anything this time, but did in the past.
371 1.2 mw * Consider us done.
372 1.2 mw */
373 1.2 mw else if (gotdata)
374 1.2 mw break;
375 1.2 mw }
376 1.2 mw s = splsoftclock();
377 1.1 mw /*
378 1.2 mw * Operation timeout (or non-blocking), quit now.
379 1.1 mw */
380 1.2 mw if ((sc->sc_flags & PARF_UIO) == 0)
381 1.2 mw {
382 1.1 mw #ifdef DEBUG
383 1.2 mw if (pardebug & PDB_IO)
384 1.15 christos printf("parrw: timeout/done\n");
385 1.1 mw #endif
386 1.2 mw splx(s);
387 1.2 mw break;
388 1.2 mw }
389 1.2 mw /*
390 1.2 mw * Implement inter-read delay
391 1.2 mw */
392 1.2 mw if (sc->sc_delay > 0)
393 1.2 mw {
394 1.2 mw sc->sc_flags |= PARF_DELAY;
395 1.7 chopps timeout(parstart, (void *)unit, sc->sc_delay);
396 1.13 veego error = tsleep(sc, PCATCH | (PZERO - 1), "par-cdelay", 0);
397 1.2 mw if (error)
398 1.2 mw {
399 1.2 mw splx(s);
400 1.2 mw break;
401 1.2 mw }
402 1.2 mw }
403 1.1 mw splx(s);
404 1.2 mw /*
405 1.2 mw * Must not call uiomove again til we've used all data
406 1.2 mw * that we already grabbed.
407 1.2 mw */
408 1.2 mw if (uio->uio_rw == UIO_WRITE && cnt != len)
409 1.2 mw {
410 1.2 mw cp += cnt;
411 1.2 mw len -= cnt;
412 1.2 mw cnt = 0;
413 1.2 mw goto again;
414 1.2 mw }
415 1.1 mw }
416 1.2 mw s = splsoftclock();
417 1.2 mw if (sc->sc_flags & PARF_TIMO)
418 1.2 mw {
419 1.7 chopps untimeout(partimo, (void *)unit);
420 1.2 mw sc->sc_flags &= ~PARF_TIMO;
421 1.1 mw }
422 1.2 mw if (sc->sc_flags & PARF_DELAY)
423 1.2 mw {
424 1.7 chopps untimeout(parstart, (void *)unit);
425 1.2 mw sc->sc_flags &= ~PARF_DELAY;
426 1.1 mw }
427 1.1 mw splx(s);
428 1.1 mw /*
429 1.1 mw * Adjust for those chars that we uiomove'ed but never wrote
430 1.1 mw */
431 1.2 mw if (uio->uio_rw == UIO_WRITE && cnt != len)
432 1.2 mw {
433 1.2 mw uio->uio_resid += (len - cnt);
434 1.1 mw #ifdef DEBUG
435 1.2 mw if (pardebug & PDB_IO)
436 1.15 christos printf("parrw: short write, adjust by %d\n",
437 1.2 mw len-cnt);
438 1.1 mw #endif
439 1.2 mw }
440 1.1 mw free(buf, M_DEVBUF);
441 1.1 mw #ifdef DEBUG
442 1.1 mw if (pardebug & (PDB_FOLLOW|PDB_IO))
443 1.15 christos printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
444 1.1 mw #endif
445 1.1 mw return (error);
446 1.1 mw }
447 1.1 mw
448 1.2 mw int
449 1.5 chopps parioctl(dev, cmd, data, flag, p)
450 1.5 chopps dev_t dev;
451 1.11 chopps u_long cmd;
452 1.5 chopps caddr_t data;
453 1.5 chopps int flag;
454 1.5 chopps struct proc *p;
455 1.1 mw {
456 1.7 chopps struct par_softc *sc = getparsp(UNIT(dev));
457 1.1 mw struct parparam *pp, *upp;
458 1.1 mw int error = 0;
459 1.1 mw
460 1.2 mw switch (cmd)
461 1.2 mw {
462 1.2 mw case PARIOCGPARAM:
463 1.2 mw pp = &sc->sc_param;
464 1.2 mw upp = (struct parparam *)data;
465 1.2 mw upp->burst = pp->burst;
466 1.2 mw upp->timo = parhztoms(pp->timo);
467 1.2 mw upp->delay = parhztoms(pp->delay);
468 1.2 mw break;
469 1.2 mw
470 1.2 mw case PARIOCSPARAM:
471 1.2 mw pp = &sc->sc_param;
472 1.2 mw upp = (struct parparam *)data;
473 1.2 mw if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
474 1.2 mw upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
475 1.2 mw return(EINVAL);
476 1.2 mw pp->burst = upp->burst;
477 1.2 mw pp->timo = parmstohz(upp->timo);
478 1.2 mw pp->delay = parmstohz(upp->delay);
479 1.2 mw break;
480 1.2 mw
481 1.2 mw default:
482 1.1 mw return(EINVAL);
483 1.2 mw }
484 1.1 mw return (error);
485 1.1 mw }
486 1.1 mw
487 1.2 mw int
488 1.1 mw parhztoms(h)
489 1.2 mw int h;
490 1.1 mw {
491 1.1 mw extern int hz;
492 1.1 mw register int m = h;
493 1.1 mw
494 1.1 mw if (m > 0)
495 1.1 mw m = m * 1000 / hz;
496 1.1 mw return(m);
497 1.1 mw }
498 1.1 mw
499 1.2 mw int
500 1.1 mw parmstohz(m)
501 1.2 mw int m;
502 1.1 mw {
503 1.1 mw extern int hz;
504 1.1 mw register int h = m;
505 1.1 mw
506 1.1 mw if (h > 0) {
507 1.1 mw h = h * hz / 1000;
508 1.1 mw if (h == 0)
509 1.1 mw h = 1000 / hz;
510 1.1 mw }
511 1.1 mw return(h);
512 1.1 mw }
513 1.1 mw
514 1.2 mw /* stuff below here if for interrupt driven output of data thru
515 1.2 mw the parallel port. */
516 1.2 mw
517 1.2 mw int partimeout_pending;
518 1.2 mw int parsend_pending;
519 1.2 mw
520 1.1 mw void
521 1.7 chopps parintr(arg)
522 1.7 chopps void *arg;
523 1.1 mw {
524 1.7 chopps int s, mask;
525 1.7 chopps
526 1.7 chopps mask = (int)arg;
527 1.7 chopps s = splclock();
528 1.7 chopps
529 1.2 mw #ifdef DEBUG
530 1.7 chopps if (pardebug & PDB_INTERRUPT)
531 1.15 christos printf("parintr %s\n", mask ? "FLG" : "tout");
532 1.7 chopps #endif
533 1.7 chopps /*
534 1.7 chopps * if invoked from timeout handler, mask will be 0,
535 1.7 chopps * if from interrupt, it will contain the cia-icr mask,
536 1.7 chopps * which is != 0
537 1.7 chopps */
538 1.7 chopps if (mask) {
539 1.7 chopps if (partimeout_pending)
540 1.7 chopps untimeout(parintr, 0);
541 1.7 chopps if (parsend_pending)
542 1.7 chopps parsend_pending = 0;
543 1.7 chopps }
544 1.2 mw
545 1.7 chopps /* either way, there won't be a timeout pending any longer */
546 1.7 chopps partimeout_pending = 0;
547 1.2 mw
548 1.7 chopps wakeup(parintr);
549 1.7 chopps splx(s);
550 1.2 mw }
551 1.2 mw
552 1.2 mw int
553 1.2 mw parsendch (ch)
554 1.2 mw u_char ch;
555 1.2 mw {
556 1.2 mw int error = 0;
557 1.2 mw int s;
558 1.2 mw
559 1.2 mw /* if either offline, busy or out of paper, wait for that
560 1.2 mw condition to clear */
561 1.2 mw s = splclock();
562 1.2 mw while (!error
563 1.2 mw && (parsend_pending
564 1.2 mw || ((ciab.pra ^ CIAB_PRA_SEL)
565 1.2 mw & (CIAB_PRA_SEL|CIAB_PRA_BUSY|CIAB_PRA_POUT))))
566 1.2 mw {
567 1.2 mw extern int hz;
568 1.2 mw
569 1.2 mw #ifdef DEBUG
570 1.2 mw if (pardebug & PDB_INTERRUPT)
571 1.15 christos printf ("parsendch, port = $%x\n",
572 1.2 mw ((ciab.pra ^ CIAB_PRA_SEL)
573 1.2 mw & (CIAB_PRA_SEL|CIAB_PRA_BUSY|CIAB_PRA_POUT)));
574 1.2 mw #endif
575 1.2 mw /* wait a second, and try again */
576 1.7 chopps timeout(parintr, 0, hz);
577 1.2 mw partimeout_pending = 1;
578 1.2 mw /* this is essentially a flipflop to have us wait for the
579 1.2 mw first character being transmitted when trying to transmit
580 1.2 mw the second, etc. */
581 1.2 mw parsend_pending = 0;
582 1.2 mw /* it's quite important that a parallel putc can be
583 1.2 mw interrupted, given the possibility to lock a printer
584 1.2 mw in an offline condition.. */
585 1.13 veego if ((error = tsleep(parintr, PCATCH | (PZERO - 1), "parsendch", 0)) > 0)
586 1.2 mw {
587 1.2 mw #ifdef DEBUG
588 1.2 mw if (pardebug & PDB_INTERRUPT)
589 1.15 christos printf ("parsendch interrupted, error = %d\n", error);
590 1.2 mw #endif
591 1.2 mw if (partimeout_pending)
592 1.7 chopps untimeout(parintr, 0);
593 1.2 mw
594 1.2 mw partimeout_pending = 0;
595 1.2 mw }
596 1.2 mw }
597 1.2 mw
598 1.2 mw if (! error)
599 1.2 mw {
600 1.2 mw #ifdef DEBUG
601 1.2 mw if (pardebug & PDB_INTERRUPT)
602 1.15 christos printf ("#%d", ch);
603 1.2 mw #endif
604 1.2 mw ciaa.prb = ch;
605 1.2 mw parsend_pending = 1;
606 1.2 mw }
607 1.2 mw
608 1.2 mw splx (s);
609 1.2 mw
610 1.2 mw return error;
611 1.1 mw }
612 1.1 mw
613 1.2 mw
614 1.1 mw int
615 1.1 mw parsend (buf, len)
616 1.1 mw u_char *buf;
617 1.1 mw int len;
618 1.1 mw {
619 1.2 mw int err, orig_len = len;
620 1.2 mw
621 1.1 mw /* make sure I/O lines are setup right for output */
622 1.1 mw
623 1.1 mw /* control lines set to input */
624 1.1 mw ciab.ddra &= ~(CIAB_PRA_SEL|CIAB_PRA_POUT|CIAB_PRA_BUSY);
625 1.1 mw /* data lines to output */
626 1.1 mw ciaa.ddrb = 0xff;
627 1.1 mw
628 1.2 mw for (; len; len--, buf++)
629 1.13 veego if ((err = parsendch (*buf)) != 0)
630 1.2 mw return err < 0 ? -EINTR : -err;
631 1.2 mw
632 1.2 mw /* either all or nothing.. */
633 1.2 mw return orig_len;
634 1.1 mw }
635 1.1 mw
636 1.1 mw
637 1.1 mw
638 1.2 mw int
639 1.2 mw parreceive (buf, len)
640 1.2 mw u_char *buf;
641 1.2 mw int len;
642 1.2 mw {
643 1.2 mw /* oh deary me, something's gotta be left to be implemented
644 1.2 mw later... */
645 1.2 mw return 0;
646 1.2 mw }
647 1.1 mw
648 1.1 mw
649 1.1 mw #endif
650