par.c revision 1.40 1 1.40 dholland /* $NetBSD: par.c,v 1.40 2014/03/16 05:20:26 dholland Exp $ */
2 1.33 ad
3 1.33 ad /*
4 1.33 ad * Copyright (c) 1982, 1990 The Regents of the University of California.
5 1.33 ad * All rights reserved.
6 1.33 ad *
7 1.33 ad * Redistribution and use in source and binary forms, with or without
8 1.33 ad * modification, are permitted provided that the following conditions
9 1.33 ad * are met:
10 1.33 ad * 1. Redistributions of source code must retain the above copyright
11 1.33 ad * notice, this list of conditions and the following disclaimer.
12 1.33 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.33 ad * notice, this list of conditions and the following disclaimer in the
14 1.33 ad * documentation and/or other materials provided with the distribution.
15 1.33 ad * 3. Neither the name of the University nor the names of its contributors
16 1.33 ad * may be used to endorse or promote products derived from this software
17 1.33 ad * without specific prior written permission.
18 1.33 ad *
19 1.33 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.33 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.33 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.33 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.33 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.33 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.33 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.33 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.33 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.33 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.33 ad * SUCH DAMAGE.
30 1.33 ad *
31 1.33 ad * @(#)ppi.c 7.3 (Berkeley) 12/16/90
32 1.33 ad */
33 1.33 ad
34 1.33 ad /*
35 1.33 ad * parallel port interface
36 1.33 ad */
37 1.33 ad
38 1.33 ad #include <sys/cdefs.h>
39 1.40 dholland __KERNEL_RCSID(0, "$NetBSD: par.c,v 1.40 2014/03/16 05:20:26 dholland Exp $");
40 1.33 ad
41 1.33 ad #include <sys/param.h>
42 1.33 ad #include <sys/errno.h>
43 1.33 ad #include <sys/uio.h>
44 1.33 ad #include <sys/device.h>
45 1.33 ad #include <sys/malloc.h>
46 1.33 ad #include <sys/file.h>
47 1.33 ad #include <sys/systm.h>
48 1.33 ad #include <sys/callout.h>
49 1.33 ad #include <sys/proc.h>
50 1.33 ad #include <sys/conf.h>
51 1.33 ad #include <sys/kernel.h>
52 1.33 ad
53 1.33 ad #include <machine/bus.h>
54 1.33 ad #include <machine/cpu.h>
55 1.33 ad #include <machine/parioctl.h>
56 1.33 ad
57 1.33 ad #include <arch/x68k/dev/intiovar.h>
58 1.33 ad
59 1.33 ad struct par_softc {
60 1.37 isaki device_t sc_dev;
61 1.33 ad
62 1.33 ad bus_space_tag_t sc_bst;
63 1.33 ad bus_space_handle_t sc_bsh;
64 1.33 ad int sc_flags;
65 1.33 ad struct parparam sc_param;
66 1.33 ad #define sc_burst sc_param.burst
67 1.33 ad #define sc_timo sc_param.timo
68 1.33 ad #define sc_delay sc_param.delay
69 1.33 ad struct callout sc_timo_ch;
70 1.33 ad struct callout sc_start_ch;
71 1.33 ad } ;
72 1.33 ad
73 1.33 ad /* par registers */
74 1.33 ad #define PAR_DATA 1
75 1.33 ad #define PAR_STROBE 3
76 1.33 ad
77 1.33 ad /* sc_flags values */
78 1.33 ad #define PARF_ALIVE 0x01
79 1.33 ad #define PARF_OPEN 0x02
80 1.33 ad #define PARF_UIO 0x04
81 1.33 ad #define PARF_TIMO 0x08
82 1.33 ad #define PARF_DELAY 0x10
83 1.33 ad #define PARF_OREAD 0x40 /* no support */
84 1.33 ad #define PARF_OWRITE 0x80
85 1.33 ad
86 1.33 ad
87 1.33 ad void partimo(void *);
88 1.33 ad void parstart(void *);
89 1.33 ad void parintr(void *);
90 1.33 ad int parrw(dev_t, struct uio *);
91 1.33 ad int parhztoms(int);
92 1.33 ad int parmstohz(int);
93 1.33 ad int parsendch(struct par_softc *, u_char);
94 1.33 ad int parsend(struct par_softc *, u_char *, int);
95 1.33 ad
96 1.34 isaki static struct callout intr_callout;
97 1.33 ad
98 1.33 ad #define UNIT(x) minor(x)
99 1.33 ad
100 1.33 ad #ifdef DEBUG
101 1.33 ad #define PDB_FOLLOW 0x01
102 1.33 ad #define PDB_IO 0x02
103 1.33 ad #define PDB_INTERRUPT 0x04
104 1.33 ad #define PDB_NOCHECK 0x80
105 1.33 ad #ifdef PARDEBUG
106 1.33 ad int pardebug = PDB_FOLLOW | PDB_IO | PDB_INTERRUPT;
107 1.33 ad #else
108 1.33 ad int pardebug = 0;
109 1.33 ad #endif
110 1.33 ad #endif
111 1.33 ad
112 1.37 isaki int parmatch(device_t, cfdata_t, void *);
113 1.37 isaki void parattach(device_t, device_t, void *);
114 1.33 ad
115 1.37 isaki CFATTACH_DECL_NEW(par, sizeof(struct par_softc),
116 1.33 ad parmatch, parattach, NULL, NULL);
117 1.33 ad
118 1.33 ad extern struct cfdriver par_cd;
119 1.33 ad
120 1.33 ad static int par_attached;
121 1.33 ad
122 1.33 ad dev_type_open(paropen);
123 1.33 ad dev_type_close(parclose);
124 1.33 ad dev_type_write(parwrite);
125 1.33 ad dev_type_ioctl(parioctl);
126 1.33 ad
127 1.33 ad const struct cdevsw par_cdevsw = {
128 1.40 dholland .d_open = paropen,
129 1.40 dholland .d_close = parclose,
130 1.40 dholland .d_read = noread,
131 1.40 dholland .d_write = parwrite,
132 1.40 dholland .d_ioctl = parioctl,
133 1.40 dholland .d_stop = nostop,
134 1.40 dholland .d_tty = notty,
135 1.40 dholland .d_poll = nopoll,
136 1.40 dholland .d_mmap = nommap,
137 1.40 dholland .d_kqfilter = nokqfilter,
138 1.40 dholland .d_flag = 0
139 1.33 ad };
140 1.33 ad
141 1.33 ad int
142 1.39 tsutsui parmatch(device_t parent, cfdata_t cf, void *aux)
143 1.33 ad {
144 1.33 ad struct intio_attach_args *ia = aux;
145 1.33 ad
146 1.33 ad /* X680x0 has only one parallel port */
147 1.33 ad if (strcmp(ia->ia_name, "par") || par_attached)
148 1.33 ad return 0;
149 1.33 ad
150 1.33 ad if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
151 1.33 ad ia->ia_addr = 0xe8c000;
152 1.33 ad ia->ia_size = 0x2000;
153 1.39 tsutsui if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY))
154 1.33 ad return 0;
155 1.33 ad if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
156 1.33 ad ia->ia_intr = 99;
157 1.33 ad #if DIAGNOSTIC
158 1.33 ad if (ia->ia_intr != 99)
159 1.33 ad return 0;
160 1.33 ad #endif
161 1.33 ad
162 1.33 ad return 1;
163 1.33 ad }
164 1.33 ad
165 1.33 ad void
166 1.39 tsutsui parattach(device_t parent, device_t self, void *aux)
167 1.33 ad {
168 1.39 tsutsui struct par_softc *sc = device_private(self);
169 1.33 ad struct intio_attach_args *ia = aux;
170 1.33 ad int r;
171 1.33 ad
172 1.33 ad par_attached = 1;
173 1.33 ad
174 1.39 tsutsui sc->sc_dev = self;
175 1.33 ad sc->sc_flags = PARF_ALIVE;
176 1.37 isaki aprint_normal(": parallel port (write only, interrupt)\n");
177 1.33 ad ia->ia_size = 0x2000;
178 1.39 tsutsui r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
179 1.33 ad #ifdef DIAGNOSTIC
180 1.33 ad if (r)
181 1.33 ad panic("IO map for PAR corruption??");
182 1.33 ad #endif
183 1.33 ad sc->sc_bst = ia->ia_bst;
184 1.33 ad r = bus_space_map(sc->sc_bst,
185 1.33 ad ia->ia_addr, ia->ia_size,
186 1.33 ad BUS_SPACE_MAP_SHIFTED,
187 1.33 ad &sc->sc_bsh);
188 1.33 ad #ifdef DIAGNOSTIC
189 1.33 ad if (r)
190 1.33 ad panic("Cannot map IO space for PAR.");
191 1.33 ad #endif
192 1.33 ad
193 1.33 ad intio_set_sicilian_intr(intio_get_sicilian_intr() &
194 1.33 ad ~SICILIAN_INTR_PAR);
195 1.33 ad
196 1.33 ad intio_intr_establish(ia->ia_intr, "par",
197 1.33 ad (intio_intr_handler_t)parintr, (void *)1);
198 1.33 ad
199 1.33 ad callout_init(&sc->sc_timo_ch, 0);
200 1.33 ad callout_init(&sc->sc_start_ch, 0);
201 1.34 isaki callout_init(&intr_callout, 0);
202 1.33 ad }
203 1.33 ad
204 1.33 ad int
205 1.33 ad paropen(dev_t dev, int flags, int mode, struct lwp *l)
206 1.33 ad {
207 1.33 ad int unit = UNIT(dev);
208 1.33 ad struct par_softc *sc;
209 1.33 ad
210 1.36 tsutsui sc = device_lookup_private(&par_cd, unit);
211 1.33 ad if (sc == NULL || !(sc->sc_flags & PARF_ALIVE))
212 1.33 ad return(ENXIO);
213 1.33 ad if (sc->sc_flags & PARF_OPEN)
214 1.33 ad return(EBUSY);
215 1.33 ad /* X680x0 can't read */
216 1.33 ad if ((flags & FREAD) == FREAD)
217 1.33 ad return (EINVAL);
218 1.33 ad
219 1.33 ad sc->sc_flags |= PARF_OPEN;
220 1.33 ad
221 1.33 ad sc->sc_flags |= PARF_OWRITE;
222 1.33 ad
223 1.33 ad sc->sc_burst = PAR_BURST;
224 1.33 ad sc->sc_timo = parmstohz(PAR_TIMO);
225 1.33 ad sc->sc_delay = parmstohz(PAR_DELAY);
226 1.33 ad
227 1.33 ad return(0);
228 1.33 ad }
229 1.33 ad
230 1.33 ad int
231 1.33 ad parclose(dev_t dev, int flags, int mode, struct lwp *l)
232 1.33 ad {
233 1.33 ad int unit = UNIT(dev);
234 1.33 ad int s;
235 1.36 tsutsui struct par_softc *sc = device_lookup_private(&par_cd, unit);
236 1.33 ad
237 1.33 ad sc->sc_flags &= ~(PARF_OPEN|PARF_OWRITE);
238 1.33 ad
239 1.33 ad /* don't allow interrupts any longer */
240 1.33 ad s = spl1();
241 1.33 ad intio_set_sicilian_intr(intio_get_sicilian_intr() &
242 1.33 ad ~SICILIAN_INTR_PAR);
243 1.33 ad splx(s);
244 1.33 ad
245 1.33 ad return (0);
246 1.33 ad }
247 1.33 ad
248 1.33 ad void
249 1.33 ad parstart(void *arg)
250 1.33 ad {
251 1.33 ad struct par_softc *sc = arg;
252 1.33 ad #ifdef DEBUG
253 1.33 ad if (pardebug & PDB_FOLLOW)
254 1.37 isaki printf("parstart(%x)\n", device_unit(sc->sc_dev));
255 1.33 ad #endif
256 1.33 ad sc->sc_flags &= ~PARF_DELAY;
257 1.33 ad wakeup(sc);
258 1.33 ad }
259 1.33 ad
260 1.33 ad void
261 1.33 ad partimo(void *arg)
262 1.33 ad {
263 1.33 ad struct par_softc *sc = arg;
264 1.33 ad #ifdef DEBUG
265 1.33 ad if (pardebug & PDB_FOLLOW)
266 1.37 isaki printf("partimo(%x)\n", device_unit(sc->sc_dev));
267 1.33 ad #endif
268 1.33 ad sc->sc_flags &= ~(PARF_UIO|PARF_TIMO);
269 1.33 ad wakeup(sc);
270 1.33 ad }
271 1.33 ad
272 1.33 ad int
273 1.33 ad parwrite(dev_t dev, struct uio *uio, int flag)
274 1.33 ad {
275 1.33 ad
276 1.33 ad #ifdef DEBUG
277 1.33 ad if (pardebug & PDB_FOLLOW)
278 1.38 isaki printf("parwrite(%x, %p)\n", UNIT(dev), uio);
279 1.33 ad #endif
280 1.33 ad return (parrw(dev, uio));
281 1.33 ad }
282 1.33 ad
283 1.33 ad int
284 1.33 ad parrw(dev_t dev, struct uio *uio)
285 1.33 ad {
286 1.33 ad int unit = UNIT(dev);
287 1.36 tsutsui struct par_softc *sc = device_lookup_private(&par_cd, unit);
288 1.33 ad int len=0xdeadbeef; /* XXX: shutup gcc */
289 1.33 ad int s, cnt=0;
290 1.33 ad char *cp;
291 1.33 ad int error = 0;
292 1.33 ad int buflen;
293 1.33 ad char *buf;
294 1.33 ad
295 1.33 ad if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
296 1.33 ad return EINVAL;
297 1.33 ad
298 1.33 ad if (uio->uio_resid == 0)
299 1.33 ad return(0);
300 1.33 ad
301 1.33 ad buflen = min(sc->sc_burst, uio->uio_resid);
302 1.33 ad buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
303 1.33 ad sc->sc_flags |= PARF_UIO;
304 1.33 ad if (sc->sc_timo > 0) {
305 1.33 ad sc->sc_flags |= PARF_TIMO;
306 1.33 ad callout_reset(&sc->sc_timo_ch, sc->sc_timo, partimo, sc);
307 1.33 ad }
308 1.33 ad while (uio->uio_resid > 0) {
309 1.33 ad len = min(buflen, uio->uio_resid);
310 1.33 ad cp = buf;
311 1.33 ad if (uio->uio_rw == UIO_WRITE) {
312 1.33 ad error = uiomove(cp, len, uio);
313 1.33 ad if (error)
314 1.33 ad break;
315 1.33 ad }
316 1.33 ad again:
317 1.33 ad s = splsoftclock();
318 1.33 ad /*
319 1.33 ad * Check if we timed out during sleep or uiomove
320 1.33 ad */
321 1.33 ad if ((sc->sc_flags & PARF_UIO) == 0) {
322 1.33 ad #ifdef DEBUG
323 1.33 ad if (pardebug & PDB_IO)
324 1.33 ad printf("parrw: uiomove/sleep timo, flags %x\n",
325 1.33 ad sc->sc_flags);
326 1.33 ad #endif
327 1.33 ad if (sc->sc_flags & PARF_TIMO) {
328 1.33 ad callout_stop(&sc->sc_timo_ch);
329 1.33 ad sc->sc_flags &= ~PARF_TIMO;
330 1.33 ad }
331 1.33 ad splx(s);
332 1.33 ad break;
333 1.33 ad }
334 1.33 ad splx(s);
335 1.33 ad /*
336 1.33 ad * Perform the operation
337 1.33 ad */
338 1.33 ad cnt = parsend(sc, cp, len);
339 1.33 ad if (cnt < 0) {
340 1.33 ad error = -cnt;
341 1.33 ad break;
342 1.33 ad }
343 1.33 ad
344 1.33 ad s = splsoftclock();
345 1.33 ad /*
346 1.33 ad * Operation timeout (or non-blocking), quit now.
347 1.33 ad */
348 1.33 ad if ((sc->sc_flags & PARF_UIO) == 0) {
349 1.33 ad #ifdef DEBUG
350 1.33 ad if (pardebug & PDB_IO)
351 1.33 ad printf("parrw: timeout/done\n");
352 1.33 ad #endif
353 1.33 ad splx(s);
354 1.33 ad break;
355 1.33 ad }
356 1.33 ad /*
357 1.33 ad * Implement inter-read delay
358 1.33 ad */
359 1.33 ad if (sc->sc_delay > 0) {
360 1.33 ad sc->sc_flags |= PARF_DELAY;
361 1.33 ad callout_reset(&sc->sc_start_ch, sc->sc_delay,
362 1.33 ad parstart, sc);
363 1.33 ad error = tsleep(sc, PCATCH|(PZERO-1), "par-cdelay", 0);
364 1.33 ad if (error) {
365 1.33 ad splx(s);
366 1.33 ad break;
367 1.33 ad }
368 1.33 ad }
369 1.33 ad splx(s);
370 1.33 ad /*
371 1.33 ad * Must not call uiomove again til we've used all data
372 1.33 ad * that we already grabbed.
373 1.33 ad */
374 1.33 ad if (uio->uio_rw == UIO_WRITE && cnt != len) {
375 1.33 ad cp += cnt;
376 1.33 ad len -= cnt;
377 1.33 ad cnt = 0;
378 1.33 ad goto again;
379 1.33 ad }
380 1.33 ad }
381 1.33 ad s = splsoftclock();
382 1.33 ad if (sc->sc_flags & PARF_TIMO) {
383 1.33 ad callout_stop(&sc->sc_timo_ch);
384 1.33 ad sc->sc_flags &= ~PARF_TIMO;
385 1.33 ad }
386 1.33 ad if (sc->sc_flags & PARF_DELAY) {
387 1.33 ad callout_stop(&sc->sc_start_ch);
388 1.33 ad sc->sc_flags &= ~PARF_DELAY;
389 1.33 ad }
390 1.33 ad splx(s);
391 1.33 ad /*
392 1.33 ad * Adjust for those chars that we uiomove'ed but never wrote
393 1.33 ad */
394 1.33 ad /*
395 1.33 ad * XXXjdolecek: this len usage is wrong, this will be incorrect
396 1.33 ad * if the transfer size is longer than sc_burst
397 1.33 ad */
398 1.33 ad if (uio->uio_rw == UIO_WRITE && cnt != len) {
399 1.33 ad uio->uio_resid += (len - cnt);
400 1.33 ad #ifdef DEBUG
401 1.33 ad if (pardebug & PDB_IO)
402 1.33 ad printf("parrw: short write, adjust by %d\n",
403 1.33 ad len-cnt);
404 1.33 ad #endif
405 1.33 ad }
406 1.33 ad free(buf, M_DEVBUF);
407 1.33 ad #ifdef DEBUG
408 1.33 ad if (pardebug & (PDB_FOLLOW|PDB_IO))
409 1.33 ad printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
410 1.33 ad #endif
411 1.33 ad return (error);
412 1.33 ad }
413 1.33 ad
414 1.33 ad int
415 1.33 ad parioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
416 1.33 ad {
417 1.36 tsutsui struct par_softc *sc = device_lookup_private(&par_cd, UNIT(dev));
418 1.33 ad struct parparam *pp, *upp;
419 1.33 ad int error = 0;
420 1.33 ad
421 1.33 ad switch (cmd) {
422 1.33 ad case PARIOCGPARAM:
423 1.33 ad pp = &sc->sc_param;
424 1.33 ad upp = (struct parparam *)data;
425 1.33 ad upp->burst = pp->burst;
426 1.33 ad upp->timo = parhztoms(pp->timo);
427 1.33 ad upp->delay = parhztoms(pp->delay);
428 1.33 ad break;
429 1.33 ad
430 1.33 ad case PARIOCSPARAM:
431 1.33 ad pp = &sc->sc_param;
432 1.33 ad upp = (struct parparam *)data;
433 1.33 ad if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
434 1.33 ad upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
435 1.33 ad return(EINVAL);
436 1.33 ad pp->burst = upp->burst;
437 1.33 ad pp->timo = parmstohz(upp->timo);
438 1.33 ad pp->delay = parmstohz(upp->delay);
439 1.33 ad break;
440 1.33 ad
441 1.33 ad default:
442 1.33 ad return(EINVAL);
443 1.33 ad }
444 1.33 ad return (error);
445 1.33 ad }
446 1.33 ad
447 1.33 ad int
448 1.33 ad parhztoms(int h)
449 1.33 ad {
450 1.33 ad int m = h;
451 1.33 ad
452 1.33 ad if (m > 0)
453 1.33 ad m = m * 1000 / hz;
454 1.33 ad return(m);
455 1.33 ad }
456 1.33 ad
457 1.33 ad int
458 1.33 ad parmstohz(int m)
459 1.33 ad {
460 1.33 ad int h = m;
461 1.33 ad
462 1.33 ad if (h > 0) {
463 1.33 ad h = h * hz / 1000;
464 1.33 ad if (h == 0)
465 1.33 ad h = 1000 / hz;
466 1.33 ad }
467 1.33 ad return(h);
468 1.33 ad }
469 1.33 ad
470 1.33 ad /* stuff below here if for interrupt driven output of data thru
471 1.33 ad the parallel port. */
472 1.33 ad
473 1.33 ad int partimeout_pending;
474 1.33 ad int parsend_pending;
475 1.33 ad
476 1.33 ad void
477 1.33 ad parintr(void *arg)
478 1.33 ad {
479 1.33 ad int s, mask;
480 1.33 ad
481 1.33 ad mask = (int)arg;
482 1.33 ad s = splclock();
483 1.33 ad
484 1.33 ad intio_set_sicilian_intr(intio_get_sicilian_intr() &
485 1.33 ad ~SICILIAN_INTR_PAR);
486 1.33 ad
487 1.33 ad #ifdef DEBUG
488 1.33 ad if (pardebug & PDB_INTERRUPT)
489 1.33 ad printf("parintr %d(%s)\n", mask, mask ? "FLG" : "tout");
490 1.33 ad #endif
491 1.33 ad /* if invoked from timeout handler, mask will be 0,
492 1.33 ad * if from interrupt, it will contain the cia-icr mask,
493 1.33 ad * which is != 0
494 1.33 ad */
495 1.33 ad if (mask) {
496 1.33 ad if (partimeout_pending)
497 1.33 ad callout_stop(&intr_callout);
498 1.33 ad if (parsend_pending)
499 1.33 ad parsend_pending = 0;
500 1.33 ad }
501 1.33 ad
502 1.33 ad /* either way, there won't be a timeout pending any longer */
503 1.33 ad partimeout_pending = 0;
504 1.33 ad
505 1.33 ad wakeup(parintr);
506 1.33 ad splx(s);
507 1.33 ad }
508 1.33 ad
509 1.33 ad int
510 1.33 ad parsendch(struct par_softc *sc, u_char ch)
511 1.33 ad {
512 1.33 ad int error = 0;
513 1.33 ad int s;
514 1.33 ad
515 1.33 ad /* if either offline, busy or out of paper, wait for that
516 1.33 ad condition to clear */
517 1.33 ad s = spl1();
518 1.33 ad while (!error
519 1.33 ad && (parsend_pending
520 1.33 ad || !(intio_get_sicilian_intr() & SICILIAN_STAT_PAR)))
521 1.33 ad {
522 1.33 ad /* wait a second, and try again */
523 1.33 ad callout_reset(&intr_callout, hz, parintr, 0);
524 1.33 ad partimeout_pending = 1;
525 1.33 ad /* this is essentially a flipflop to have us wait for the
526 1.33 ad first character being transmitted when trying to transmit
527 1.33 ad the second, etc. */
528 1.33 ad parsend_pending = 0;
529 1.33 ad /* it's quite important that a parallel putc can be
530 1.33 ad interrupted, given the possibility to lock a printer
531 1.33 ad in an offline condition.. */
532 1.33 ad if ((error = tsleep(parintr, PCATCH|(PZERO-1), "parsendch", 0))) {
533 1.33 ad #ifdef DEBUG
534 1.33 ad if (pardebug & PDB_INTERRUPT)
535 1.33 ad printf("parsendch interrupted, error = %d\n", error);
536 1.33 ad #endif
537 1.33 ad if (partimeout_pending)
538 1.33 ad callout_stop(&intr_callout);
539 1.33 ad
540 1.33 ad partimeout_pending = 0;
541 1.33 ad }
542 1.33 ad }
543 1.33 ad
544 1.33 ad if (!error) {
545 1.33 ad #ifdef DEBUG
546 1.33 ad if (pardebug & PDB_INTERRUPT)
547 1.33 ad printf("#%d", ch);
548 1.33 ad #endif
549 1.33 ad bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_DATA, ch);
550 1.33 ad DELAY(1); /* (DELAY(1) == 1us) > 0.5us */
551 1.33 ad bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_STROBE, 0);
552 1.33 ad intio_set_sicilian_intr(intio_get_sicilian_intr() |
553 1.33 ad SICILIAN_INTR_PAR);
554 1.33 ad DELAY(1);
555 1.33 ad bus_space_write_1(sc->sc_bst, sc->sc_bsh, PAR_STROBE, 1);
556 1.33 ad parsend_pending = 1;
557 1.33 ad }
558 1.33 ad
559 1.33 ad splx(s);
560 1.33 ad
561 1.33 ad return error;
562 1.33 ad }
563 1.33 ad
564 1.33 ad
565 1.33 ad int
566 1.33 ad parsend(struct par_softc *sc, u_char *buf, int len)
567 1.33 ad {
568 1.33 ad int err, orig_len = len;
569 1.33 ad
570 1.33 ad for (; len; len--, buf++)
571 1.33 ad if ((err = parsendch(sc, *buf)))
572 1.33 ad return err < 0 ? -EINTR : -err;
573 1.33 ad
574 1.33 ad /* either all or nothing.. */
575 1.33 ad return orig_len;
576 1.33 ad }
577