ppi.c revision 1.19.6.1 1 /* $NetBSD: ppi.c,v 1.19.6.1 2001/10/10 11:56:05 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1982, 1990, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)ppi.c 8.1 (Berkeley) 6/16/93
72 */
73
74 /*
75 * Printer/Plotter HPIB interface
76 */
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/callout.h>
81 #include <sys/conf.h>
82 #include <sys/device.h>
83 #include <sys/errno.h>
84 #include <sys/malloc.h>
85 #include <sys/proc.h>
86 #include <sys/uio.h>
87 #include <sys/vnode.h>
88
89 #include <hp300/dev/hpibvar.h>
90
91 #include <hp300/dev/ppiioctl.h>
92
93 struct ppi_softc {
94 struct device sc_dev;
95 int sc_flags;
96 struct hpibqueue sc_hq; /* HP-IB job queue entry */
97 struct ppiparam sc_param;
98 #define sc_burst sc_param.burst
99 #define sc_timo sc_param.timo
100 #define sc_delay sc_param.delay
101 int sc_sec;
102 int sc_slave; /* HP-IB slave address */
103 struct callout sc_timo_ch;
104 struct callout sc_start_ch;
105 };
106
107 /* sc_flags values */
108 #define PPIF_ALIVE 0x01
109 #define PPIF_OPEN 0x02
110 #define PPIF_UIO 0x04
111 #define PPIF_TIMO 0x08
112 #define PPIF_DELAY 0x10
113
114 int ppimatch __P((struct device *, struct cfdata *, void *));
115 void ppiattach __P((struct device *, struct device *, void *));
116
117 struct cfattach ppi_ca = {
118 sizeof(struct ppi_softc), ppimatch, ppiattach
119 };
120
121 extern struct cfdriver ppi_cd;
122
123 void ppistart __P((void *));
124 void ppinoop __P((void *));
125
126 void ppitimo __P((void *));
127 int ppirw __P((struct vnode *, struct uio *));
128 int ppihztoms __P((int));
129 int ppimstohz __P((int));
130
131 bdev_decl(ppi);
132 cdev_decl(ppi);
133
134 #define UNIT(x) minor(x)
135
136 #ifdef DEBUG
137 int ppidebug = 0x80;
138 #define PDB_FOLLOW 0x01
139 #define PDB_IO 0x02
140 #define PDB_NOCHECK 0x80
141 #endif
142
143 int
144 ppimatch(parent, match, aux)
145 struct device *parent;
146 struct cfdata *match;
147 void *aux;
148 {
149 struct hpibbus_attach_args *ha = aux;
150
151 /*
152 * The printer/plotter doesn't return an ID tag.
153 * The check below prevents us from matching a CS80
154 * device by mistake.
155 */
156 if (ha->ha_id & 0x200)
157 return (0);
158
159 /*
160 * To prevent matching all unused slots on the bus, we
161 * don't allow wildcarded locators.
162 */
163 if (match->hpibbuscf_slave == HPIBBUSCF_SLAVE_DEFAULT ||
164 match->hpibbuscf_punit == HPIBBUSCF_PUNIT_DEFAULT)
165 return (0);
166
167 return (1);
168 }
169
170 void
171 ppiattach(parent, self, aux)
172 struct device *parent, *self;
173 void *aux;
174 {
175 struct ppi_softc *sc = (struct ppi_softc *)self;
176 struct hpibbus_attach_args *ha = aux;
177
178 printf("\n");
179
180 sc->sc_slave = ha->ha_slave;
181
182 callout_init(&sc->sc_timo_ch);
183 callout_init(&sc->sc_start_ch);
184
185 /* Initialize the hpib queue entry. */
186 sc->sc_hq.hq_softc = sc;
187 sc->sc_hq.hq_slave = sc->sc_slave;
188 sc->sc_hq.hq_start = ppistart;
189 sc->sc_hq.hq_go = ppinoop;
190 sc->sc_hq.hq_intr = ppinoop;
191
192 sc->sc_flags = PPIF_ALIVE;
193 }
194
195 void
196 ppinoop(arg)
197 void *arg;
198 {
199 /* Noop! */
200 }
201
202 int
203 ppiopen(devvp, flags, fmt, p)
204 struct vnode *devvp;
205 int flags, fmt;
206 struct proc *p;
207 {
208 dev_t dev = vdev_rdev(devvp);
209 int unit = UNIT(dev);
210 struct ppi_softc *sc;
211
212 if (unit >= ppi_cd.cd_ndevs ||
213 (sc = ppi_cd.cd_devs[unit]) == NULL ||
214 (sc->sc_flags & PPIF_ALIVE) == 0)
215 return (ENXIO);
216
217 #ifdef DEBUG
218 if (ppidebug & PDB_FOLLOW)
219 printf("ppiopen(%x, %x): flags %x\n",
220 dev, flags, sc->sc_flags);
221 #endif
222 if (sc->sc_flags & PPIF_OPEN)
223 return (EBUSY);
224
225 vdev_setprivdata(devvp, sc);
226
227 sc->sc_flags |= PPIF_OPEN;
228 sc->sc_burst = PPI_BURST;
229 sc->sc_timo = ppimstohz(PPI_TIMO);
230 sc->sc_delay = ppimstohz(PPI_DELAY);
231 sc->sc_sec = -1;
232 return(0);
233 }
234
235 int
236 ppiclose(devvp, flags, fmt, p)
237 struct vnode *devvp;
238 int flags, fmt;
239 struct proc *p;
240 {
241 struct ppi_softc *sc = vdev_privdata(devvp);
242
243 #ifdef DEBUG
244 if (ppidebug & PDB_FOLLOW)
245 printf("ppiclose(%x, %x): flags %x\n",
246 dev, flags, sc->sc_flags);
247 #endif
248 sc->sc_flags &= ~PPIF_OPEN;
249 return(0);
250 }
251
252 void
253 ppistart(arg)
254 void *arg;
255 {
256 struct ppi_softc *sc = arg;
257
258 #ifdef DEBUG
259 if (ppidebug & PDB_FOLLOW)
260 printf("ppistart(%x)\n", sc->sc_dev.dv_unit);
261 #endif
262 sc->sc_flags &= ~PPIF_DELAY;
263 wakeup(sc);
264 }
265
266 void
267 ppitimo(arg)
268 void *arg;
269 {
270 struct ppi_softc *sc = arg;
271
272 #ifdef DEBUG
273 if (ppidebug & PDB_FOLLOW)
274 printf("ppitimo(%x)\n", sc->sc_dev.dv_unit);
275 #endif
276 sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
277 wakeup(sc);
278 }
279
280 int
281 ppiread(devvp, uio, flags)
282 struct vnode *devvp;
283 struct uio *uio;
284 int flags;
285 {
286
287 #ifdef DEBUG
288 if (ppidebug & PDB_FOLLOW)
289 printf("ppiread(%x, %p)\n", vdev_rdev(devvp), uio);
290 #endif
291 return (ppirw(devvp, uio));
292 }
293
294 int
295 ppiwrite(devvp, uio, flags)
296 struct vnode *devvp;
297 struct uio *uio;
298 int flags;
299 {
300
301 #ifdef DEBUG
302 if (ppidebug & PDB_FOLLOW)
303 printf("ppiwrite(%x, %p)\n", vdev_rdev(devvp), uio);
304 #endif
305 return (ppirw(devvp, uio));
306 }
307
308 int
309 ppirw(devvp, uio)
310 struct vnode *devvp;
311 struct uio *uio;
312 {
313 struct ppi_softc *sc = vdev_privdata(devvp);
314 int s, len, cnt;
315 char *cp;
316 int error = 0, gotdata = 0;
317 int buflen, ctlr, slave;
318 char *buf;
319
320 if (uio->uio_resid == 0)
321 return(0);
322
323 ctlr = sc->sc_dev.dv_parent->dv_unit;
324 slave = sc->sc_slave;
325
326 #ifdef DEBUG
327 if (ppidebug & (PDB_FOLLOW|PDB_IO))
328 printf("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
329 vdev_rdev(devvp), uio,
330 uio->uio_rw == UIO_READ ? 'R' : 'W',
331 sc->sc_burst, sc->sc_timo, uio->uio_resid);
332 #endif
333 buflen = min(sc->sc_burst, uio->uio_resid);
334 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
335 sc->sc_flags |= PPIF_UIO;
336 if (sc->sc_timo > 0) {
337 sc->sc_flags |= PPIF_TIMO;
338 callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
339 }
340 len = cnt = 0;
341 while (uio->uio_resid > 0) {
342 len = min(buflen, uio->uio_resid);
343 cp = buf;
344 if (uio->uio_rw == UIO_WRITE) {
345 error = uiomove(cp, len, uio);
346 if (error)
347 break;
348 }
349 again:
350 s = splbio();
351 if ((sc->sc_flags & PPIF_UIO) &&
352 hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq) == 0)
353 (void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
354 /*
355 * Check if we timed out during sleep or uiomove
356 */
357 (void) spllowersoftclock();
358 if ((sc->sc_flags & PPIF_UIO) == 0) {
359 #ifdef DEBUG
360 if (ppidebug & PDB_IO)
361 printf("ppirw: uiomove/sleep timo, flags %x\n",
362 sc->sc_flags);
363 #endif
364 if (sc->sc_flags & PPIF_TIMO) {
365 callout_stop(&sc->sc_timo_ch);
366 sc->sc_flags &= ~PPIF_TIMO;
367 }
368 splx(s);
369 break;
370 }
371 splx(s);
372 /*
373 * Perform the operation
374 */
375 if (uio->uio_rw == UIO_WRITE)
376 cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len);
377 else
378 cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len);
379 s = splbio();
380 hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
381 #ifdef DEBUG
382 if (ppidebug & PDB_IO)
383 printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n",
384 uio->uio_rw == UIO_READ ? "recv" : "send",
385 ctlr, slave, sc->sc_sec, cp, len, cnt);
386 #endif
387 splx(s);
388 if (uio->uio_rw == UIO_READ) {
389 if (cnt) {
390 error = uiomove(cp, cnt, uio);
391 if (error)
392 break;
393 gotdata++;
394 }
395 /*
396 * Didn't get anything this time, but did in the past.
397 * Consider us done.
398 */
399 else if (gotdata)
400 break;
401 }
402 s = splsoftclock();
403 /*
404 * Operation timeout (or non-blocking), quit now.
405 */
406 if ((sc->sc_flags & PPIF_UIO) == 0) {
407 #ifdef DEBUG
408 if (ppidebug & PDB_IO)
409 printf("ppirw: timeout/done\n");
410 #endif
411 splx(s);
412 break;
413 }
414 /*
415 * Implement inter-read delay
416 */
417 if (sc->sc_delay > 0) {
418 sc->sc_flags |= PPIF_DELAY;
419 callout_reset(&sc->sc_start_ch, sc->sc_delay,
420 ppistart, sc);
421 error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0);
422 if (error) {
423 splx(s);
424 break;
425 }
426 }
427 splx(s);
428 /*
429 * Must not call uiomove again til we've used all data
430 * that we already grabbed.
431 */
432 if (uio->uio_rw == UIO_WRITE && cnt != len) {
433 cp += cnt;
434 len -= cnt;
435 cnt = 0;
436 goto again;
437 }
438 }
439 s = splsoftclock();
440 if (sc->sc_flags & PPIF_TIMO) {
441 callout_stop(&sc->sc_timo_ch);
442 sc->sc_flags &= ~PPIF_TIMO;
443 }
444 if (sc->sc_flags & PPIF_DELAY) {
445 callout_stop(&sc->sc_start_ch);
446 sc->sc_flags &= ~PPIF_DELAY;
447 }
448 splx(s);
449 /*
450 * Adjust for those chars that we uiomove'ed but never wrote
451 */
452 if (uio->uio_rw == UIO_WRITE && cnt != len) {
453 uio->uio_resid += (len - cnt);
454 #ifdef DEBUG
455 if (ppidebug & PDB_IO)
456 printf("ppirw: short write, adjust by %d\n",
457 len-cnt);
458 #endif
459 }
460 free(buf, M_DEVBUF);
461 #ifdef DEBUG
462 if (ppidebug & (PDB_FOLLOW|PDB_IO))
463 printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
464 #endif
465 return (error);
466 }
467
468 int
469 ppiioctl(devvp, cmd, data, flag, p)
470 struct vnode *devvp;
471 u_long cmd;
472 caddr_t data;
473 int flag;
474 struct proc *p;
475 {
476 struct ppi_softc *sc = vdev_privdata(devvp);
477 struct ppiparam *pp, *upp;
478 int error = 0;
479
480 switch (cmd) {
481 case PPIIOCGPARAM:
482 pp = &sc->sc_param;
483 upp = (struct ppiparam *)data;
484 upp->burst = pp->burst;
485 upp->timo = ppihztoms(pp->timo);
486 upp->delay = ppihztoms(pp->delay);
487 break;
488 case PPIIOCSPARAM:
489 pp = &sc->sc_param;
490 upp = (struct ppiparam *)data;
491 if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
492 upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
493 return(EINVAL);
494 pp->burst = upp->burst;
495 pp->timo = ppimstohz(upp->timo);
496 pp->delay = ppimstohz(upp->delay);
497 break;
498 case PPIIOCSSEC:
499 sc->sc_sec = *(int *)data;
500 break;
501 default:
502 return(EINVAL);
503 }
504 return (error);
505 }
506
507 int
508 ppihztoms(h)
509 int h;
510 {
511 extern int hz;
512 int m = h;
513
514 if (m > 0)
515 m = m * 1000 / hz;
516 return(m);
517 }
518
519 int
520 ppimstohz(m)
521 int m;
522 {
523 extern int hz;
524 int h = m;
525
526 if (h > 0) {
527 h = h * hz / 1000;
528 if (h == 0)
529 h = 1000 / hz;
530 }
531 return(h);
532 }
533