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