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