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