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