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