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