par.c revision 1.23 1 /* $NetBSD: par.c,v 1.23 2005/01/18 07:12:15 chs 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.23 2005/01/18 07:12:15 chs 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(void *);
87 void parstart(void *);
88 void parintr(void *);
89 int parrw(dev_t, struct uio *);
90 int parhztoms(int);
91 int parmstohz(int);
92 int parsendch(struct par_softc *, u_char);
93 int parsend(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(struct device *, struct cfdata *, void *);
112 void parattach(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 static int par_attached;
120
121 dev_type_open(paropen);
122 dev_type_close(parclose);
123 dev_type_write(parwrite);
124 dev_type_ioctl(parioctl);
125
126 const struct cdevsw par_cdevsw = {
127 paropen, parclose, noread, parwrite, parioctl,
128 nostop, notty, nopoll, nommap, nokqfilter,
129 };
130
131 int
132 parmatch(struct device *pdp, struct cfdata *cfp, void *aux)
133 {
134 struct intio_attach_args *ia = aux;
135
136 /* X680x0 has only one parallel port */
137 if (strcmp(ia->ia_name, "par") || par_attached)
138 return 0;
139
140 if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
141 ia->ia_addr = 0xe8c000;
142 ia->ia_size = 0x2000;
143 if (intio_map_allocate_region (pdp, ia, INTIO_MAP_TESTONLY))
144 return 0;
145 if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
146 ia->ia_intr = 99;
147 #if DIAGNOSTIC
148 if (ia->ia_intr != 99)
149 return 0;
150 #endif
151
152 return 1;
153 }
154
155 void
156 parattach(struct device *pdp, struct device *dp, void *aux)
157 {
158 struct par_softc *sc = (struct par_softc *)dp;
159 struct intio_attach_args *ia = aux;
160 int r;
161
162 par_attached = 1;
163
164 sc->sc_flags = PARF_ALIVE;
165 printf(": parallel port (write only, interrupt)\n");
166 ia->ia_size = 0x2000;
167 r = intio_map_allocate_region (pdp, ia, INTIO_MAP_ALLOCATE);
168 #ifdef DIAGNOSTIC
169 if (r)
170 panic ("IO map for PAR corruption??");
171 #endif
172 sc->sc_bst = ia->ia_bst;
173 r = bus_space_map (sc->sc_bst,
174 ia->ia_addr, ia->ia_size,
175 BUS_SPACE_MAP_SHIFTED,
176 &sc->sc_bsh);
177 #ifdef DIAGNOSTIC
178 if (r)
179 panic ("Cannot map IO space for PAR.");
180 #endif
181
182 intio_set_sicilian_intr(intio_get_sicilian_intr() &
183 ~SICILIAN_INTR_PAR);
184
185 intio_intr_establish(ia->ia_intr, "par",
186 (intio_intr_handler_t) parintr, (void*) 1);
187
188 callout_init(&sc->sc_timo_ch);
189 callout_init(&sc->sc_start_ch);
190 }
191
192 int
193 paropen(dev_t dev, int flags, int mode, struct proc *p)
194 {
195 int unit = UNIT(dev);
196 struct par_softc *sc;
197
198 sc = device_lookup(&par_cd, unit);
199 if (sc == NULL || !(sc->sc_flags & PARF_ALIVE))
200 return(ENXIO);
201 if (sc->sc_flags & PARF_OPEN)
202 return(EBUSY);
203 /* X680x0 can't read */
204 if ((flags & FREAD) == FREAD)
205 return (EINVAL);
206
207 sc->sc_flags |= PARF_OPEN;
208
209 sc->sc_flags |= PARF_OWRITE;
210
211 sc->sc_burst = PAR_BURST;
212 sc->sc_timo = parmstohz(PAR_TIMO);
213 sc->sc_delay = parmstohz(PAR_DELAY);
214
215 return(0);
216 }
217
218 int
219 parclose(dev_t dev, int flags, int mode, struct proc *p)
220 {
221 int unit = UNIT(dev);
222 int s;
223 struct par_softc *sc = par_cd.cd_devs[unit];
224
225 sc->sc_flags &= ~(PARF_OPEN|PARF_OWRITE);
226
227 /* don't allow interrupts any longer */
228 s = spl1();
229 intio_set_sicilian_intr(intio_get_sicilian_intr() &
230 ~SICILIAN_INTR_PAR);
231 splx(s);
232
233 return (0);
234 }
235
236 void
237 parstart(void *arg)
238 {
239 struct par_softc *sc = arg;
240 #ifdef DEBUG
241 if (pardebug & PDB_FOLLOW)
242 printf("parstart(%x)\n", sc->sc_dev.dv_unit);
243 #endif
244 sc->sc_flags &= ~PARF_DELAY;
245 wakeup(sc);
246 }
247
248 void
249 partimo(void *arg)
250 {
251 struct par_softc *sc = arg;
252 #ifdef DEBUG
253 if (pardebug & PDB_FOLLOW)
254 printf("partimo(%x)\n", sc->sc_dev.dv_unit);
255 #endif
256 sc->sc_flags &= ~(PARF_UIO|PARF_TIMO);
257 wakeup(sc);
258 }
259
260 int
261 parwrite(dev_t dev, struct uio *uio, int flag)
262 {
263
264 #ifdef DEBUG
265 if (pardebug & PDB_FOLLOW)
266 printf("parwrite(%x, %p)\n", dev, uio);
267 #endif
268 return (parrw(dev, uio));
269 }
270
271 int
272 parrw(dev_t dev, struct uio *uio)
273 {
274 int unit = UNIT(dev);
275 struct par_softc *sc = par_cd.cd_devs[unit];
276 int len=0xdeadbeef; /* XXX: shutup gcc */
277 int s, cnt=0;
278 char *cp;
279 int error = 0;
280 int buflen;
281 char *buf;
282
283 if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ))
284 return EINVAL;
285
286 if (uio->uio_resid == 0)
287 return(0);
288
289 buflen = min(sc->sc_burst, uio->uio_resid);
290 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
291 sc->sc_flags |= PARF_UIO;
292 if (sc->sc_timo > 0) {
293 sc->sc_flags |= PARF_TIMO;
294 callout_reset(&sc->sc_timo_ch, sc->sc_timo, partimo, sc);
295 }
296 while (uio->uio_resid > 0) {
297 len = min(buflen, uio->uio_resid);
298 cp = buf;
299 if (uio->uio_rw == UIO_WRITE) {
300 error = uiomove(cp, len, uio);
301 if (error)
302 break;
303 }
304 again:
305 s = spl1();
306 /*
307 * Check if we timed out during sleep or uiomove
308 */
309 (void) spllowersoftclock();
310 if ((sc->sc_flags & PARF_UIO) == 0) {
311 #ifdef DEBUG
312 if (pardebug & PDB_IO)
313 printf("parrw: uiomove/sleep timo, flags %x\n",
314 sc->sc_flags);
315 #endif
316 if (sc->sc_flags & PARF_TIMO) {
317 callout_stop(&sc->sc_timo_ch);
318 sc->sc_flags &= ~PARF_TIMO;
319 }
320 splx(s);
321 break;
322 }
323 splx(s);
324 /*
325 * Perform the operation
326 */
327 cnt = parsend(sc, cp, len);
328 if (cnt < 0) {
329 error = -cnt;
330 break;
331 }
332
333 s = splsoftclock();
334 /*
335 * Operation timeout (or non-blocking), quit now.
336 */
337 if ((sc->sc_flags & PARF_UIO) == 0) {
338 #ifdef DEBUG
339 if (pardebug & PDB_IO)
340 printf("parrw: timeout/done\n");
341 #endif
342 splx(s);
343 break;
344 }
345 /*
346 * Implement inter-read delay
347 */
348 if (sc->sc_delay > 0) {
349 sc->sc_flags |= PARF_DELAY;
350 callout_reset(&sc->sc_start_ch, sc->sc_delay,
351 parstart, sc);
352 error = tsleep(sc, PCATCH|(PZERO-1), "par-cdelay", 0);
353 if (error) {
354 splx(s);
355 break;
356 }
357 }
358 splx(s);
359 /*
360 * Must not call uiomove again til we've used all data
361 * that we already grabbed.
362 */
363 if (uio->uio_rw == UIO_WRITE && cnt != len) {
364 cp += cnt;
365 len -= cnt;
366 cnt = 0;
367 goto again;
368 }
369 }
370 s = splsoftclock();
371 if (sc->sc_flags & PARF_TIMO) {
372 callout_stop(&sc->sc_timo_ch);
373 sc->sc_flags &= ~PARF_TIMO;
374 }
375 if (sc->sc_flags & PARF_DELAY) {
376 callout_stop(&sc->sc_start_ch);
377 sc->sc_flags &= ~PARF_DELAY;
378 }
379 splx(s);
380 /*
381 * Adjust for those chars that we uiomove'ed but never wrote
382 */
383 /*
384 * XXXjdolecek: this len usage is wrong, this will be incorrect
385 * if the transfer size is longer than sc_burst
386 */
387 if (uio->uio_rw == UIO_WRITE && cnt != len) {
388 uio->uio_resid += (len - cnt);
389 #ifdef DEBUG
390 if (pardebug & PDB_IO)
391 printf("parrw: short write, adjust by %d\n",
392 len-cnt);
393 #endif
394 }
395 free(buf, M_DEVBUF);
396 #ifdef DEBUG
397 if (pardebug & (PDB_FOLLOW|PDB_IO))
398 printf("parrw: return %d, resid %d\n", error, uio->uio_resid);
399 #endif
400 return (error);
401 }
402
403 int
404 parioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
405 {
406 struct par_softc *sc = par_cd.cd_devs[UNIT(dev)];
407 struct parparam *pp, *upp;
408 int error = 0;
409
410 switch (cmd) {
411 case PARIOCGPARAM:
412 pp = &sc->sc_param;
413 upp = (struct parparam *)data;
414 upp->burst = pp->burst;
415 upp->timo = parhztoms(pp->timo);
416 upp->delay = parhztoms(pp->delay);
417 break;
418
419 case PARIOCSPARAM:
420 pp = &sc->sc_param;
421 upp = (struct parparam *)data;
422 if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX ||
423 upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX)
424 return(EINVAL);
425 pp->burst = upp->burst;
426 pp->timo = parmstohz(upp->timo);
427 pp->delay = parmstohz(upp->delay);
428 break;
429
430 default:
431 return(EINVAL);
432 }
433 return (error);
434 }
435
436 int
437 parhztoms(int h)
438 {
439 extern int hz;
440 int m = h;
441
442 if (m > 0)
443 m = m * 1000 / hz;
444 return(m);
445 }
446
447 int
448 parmstohz(int m)
449 {
450 extern int hz;
451 int h = m;
452
453 if (h > 0) {
454 h = h * hz / 1000;
455 if (h == 0)
456 h = 1000 / hz;
457 }
458 return(h);
459 }
460
461 /* stuff below here if for interrupt driven output of data thru
462 the parallel port. */
463
464 int partimeout_pending;
465 int parsend_pending;
466
467 void
468 parintr(void *arg)
469 {
470 int s, mask;
471
472 mask = (int)arg;
473 s = splclock();
474
475 intio_set_sicilian_intr(intio_get_sicilian_intr() &
476 ~SICILIAN_INTR_PAR);
477
478 #ifdef DEBUG
479 if (pardebug & PDB_INTERRUPT)
480 printf ("parintr %d(%s)\n", mask, mask ? "FLG" : "tout");
481 #endif
482 /* if invoked from timeout handler, mask will be 0,
483 * if from interrupt, it will contain the cia-icr mask,
484 * which is != 0
485 */
486 if (mask) {
487 if (partimeout_pending)
488 callout_stop(&intr_callout);
489 if (parsend_pending)
490 parsend_pending = 0;
491 }
492
493 /* either way, there won't be a timeout pending any longer */
494 partimeout_pending = 0;
495
496 wakeup(parintr);
497 splx (s);
498 }
499
500 int
501 parsendch(struct par_softc *sc, u_char ch)
502 {
503 int error = 0;
504 int s;
505
506 /* if either offline, busy or out of paper, wait for that
507 condition to clear */
508 s = spl1();
509 while (!error
510 && (parsend_pending
511 || !(intio_get_sicilian_intr() & SICILIAN_STAT_PAR)))
512 {
513 extern int hz;
514
515 /* wait a second, and try again */
516 callout_reset(&intr_callout, hz, parintr, 0);
517 partimeout_pending = 1;
518 /* this is essentially a flipflop to have us wait for the
519 first character being transmitted when trying to transmit
520 the second, etc. */
521 parsend_pending = 0;
522 /* it's quite important that a parallel putc can be
523 interrupted, given the possibility to lock a printer
524 in an offline condition.. */
525 if ((error = tsleep (parintr, PCATCH|(PZERO-1), "parsendch", 0))) {
526 #ifdef DEBUG
527 if (pardebug & PDB_INTERRUPT)
528 printf ("parsendch interrupted, error = %d\n", error);
529 #endif
530 if (partimeout_pending)
531 callout_stop(&intr_callout);
532
533 partimeout_pending = 0;
534 }
535 }
536
537 if (!error) {
538 #ifdef DEBUG
539 if (pardebug & PDB_INTERRUPT)
540 printf ("#%d", ch);
541 #endif
542 bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_DATA, ch);
543 DELAY(1); /* (DELAY(1) == 1us) > 0.5us */
544 bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_STROBE, 0);
545 intio_set_sicilian_intr (intio_get_sicilian_intr() |
546 SICILIAN_INTR_PAR);
547 DELAY(1);
548 bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_STROBE, 1);
549 parsend_pending = 1;
550 }
551
552 splx (s);
553
554 return error;
555 }
556
557
558 int
559 parsend(struct par_softc *sc, u_char *buf, int len)
560 {
561 int err, orig_len = len;
562
563 for (; len; len--, buf++)
564 if ((err = parsendch (sc, *buf)))
565 return err < 0 ? -EINTR : -err;
566
567 /* either all or nothing.. */
568 return orig_len;
569 }
570