ppi.c revision 1.17 1 /* $NetBSD: ppi.c,v 1.17 1999/08/05 18:08:10 thorpej 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/conf.h>
81 #include <sys/device.h>
82 #include <sys/errno.h>
83 #include <sys/malloc.h>
84 #include <sys/proc.h>
85 #include <sys/uio.h>
86
87 #include <hp300/dev/hpibvar.h>
88
89 #include <hp300/dev/ppiioctl.h>
90
91 struct ppi_softc {
92 struct device sc_dev;
93 int sc_flags;
94 struct hpibqueue sc_hq; /* HP-IB job queue entry */
95 struct ppiparam sc_param;
96 #define sc_burst sc_param.burst
97 #define sc_timo sc_param.timo
98 #define sc_delay sc_param.delay
99 int sc_sec;
100 int sc_slave; /* HP-IB slave address */
101 };
102
103 /* sc_flags values */
104 #define PPIF_ALIVE 0x01
105 #define PPIF_OPEN 0x02
106 #define PPIF_UIO 0x04
107 #define PPIF_TIMO 0x08
108 #define PPIF_DELAY 0x10
109
110 int ppimatch __P((struct device *, struct cfdata *, void *));
111 void ppiattach __P((struct device *, struct device *, void *));
112
113 struct cfattach ppi_ca = {
114 sizeof(struct ppi_softc), ppimatch, ppiattach
115 };
116
117 extern struct cfdriver ppi_cd;
118
119 void ppistart __P((void *));
120 void ppinoop __P((void *));
121
122 void ppitimo __P((void *));
123 int ppirw __P((dev_t, struct uio *));
124 int ppihztoms __P((int));
125 int ppimstohz __P((int));
126
127 bdev_decl(ppi);
128 cdev_decl(ppi);
129
130 #define UNIT(x) minor(x)
131
132 #ifdef DEBUG
133 int ppidebug = 0x80;
134 #define PDB_FOLLOW 0x01
135 #define PDB_IO 0x02
136 #define PDB_NOCHECK 0x80
137 #endif
138
139 int
140 ppimatch(parent, match, aux)
141 struct device *parent;
142 struct cfdata *match;
143 void *aux;
144 {
145 struct hpibbus_attach_args *ha = aux;
146
147 /*
148 * The printer/plotter doesn't return an ID tag.
149 * The check below prevents us from matching a CS80
150 * device by mistake.
151 */
152 if (ha->ha_id & 0x200)
153 return (0);
154
155 /*
156 * To prevent matching all unused slots on the bus, we
157 * don't allow wildcarded locators.
158 */
159 if (match->hpibbuscf_slave == HPIBBUSCF_SLAVE_DEFAULT ||
160 match->hpibbuscf_punit == HPIBBUSCF_PUNIT_DEFAULT)
161 return (0);
162
163 return (1);
164 }
165
166 void
167 ppiattach(parent, self, aux)
168 struct device *parent, *self;
169 void *aux;
170 {
171 struct ppi_softc *sc = (struct ppi_softc *)self;
172 struct hpibbus_attach_args *ha = aux;
173
174 printf("\n");
175
176 sc->sc_slave = ha->ha_slave;
177
178 /* Initialize the hpib queue entry. */
179 sc->sc_hq.hq_softc = sc;
180 sc->sc_hq.hq_slave = sc->sc_slave;
181 sc->sc_hq.hq_start = ppistart;
182 sc->sc_hq.hq_go = ppinoop;
183 sc->sc_hq.hq_intr = ppinoop;
184
185 sc->sc_flags = PPIF_ALIVE;
186 }
187
188 void
189 ppinoop(arg)
190 void *arg;
191 {
192 /* Noop! */
193 }
194
195 int
196 ppiopen(dev, flags, fmt, p)
197 dev_t dev;
198 int flags, fmt;
199 struct proc *p;
200 {
201 int unit = UNIT(dev);
202 struct ppi_softc *sc;
203
204 if (unit >= ppi_cd.cd_ndevs ||
205 (sc = ppi_cd.cd_devs[unit]) == NULL ||
206 (sc->sc_flags & PPIF_ALIVE) == 0)
207 return (ENXIO);
208
209 #ifdef DEBUG
210 if (ppidebug & PDB_FOLLOW)
211 printf("ppiopen(%x, %x): flags %x\n",
212 dev, flags, sc->sc_flags);
213 #endif
214 if (sc->sc_flags & PPIF_OPEN)
215 return (EBUSY);
216 sc->sc_flags |= PPIF_OPEN;
217 sc->sc_burst = PPI_BURST;
218 sc->sc_timo = ppimstohz(PPI_TIMO);
219 sc->sc_delay = ppimstohz(PPI_DELAY);
220 sc->sc_sec = -1;
221 return(0);
222 }
223
224 int
225 ppiclose(dev, flags, fmt, p)
226 dev_t dev;
227 int flags, fmt;
228 struct proc *p;
229 {
230 int unit = UNIT(dev);
231 struct ppi_softc *sc = ppi_cd.cd_devs[unit];
232
233 #ifdef DEBUG
234 if (ppidebug & PDB_FOLLOW)
235 printf("ppiclose(%x, %x): flags %x\n",
236 dev, flags, sc->sc_flags);
237 #endif
238 sc->sc_flags &= ~PPIF_OPEN;
239 return(0);
240 }
241
242 void
243 ppistart(arg)
244 void *arg;
245 {
246 struct ppi_softc *sc = arg;
247
248 #ifdef DEBUG
249 if (ppidebug & PDB_FOLLOW)
250 printf("ppistart(%x)\n", sc->sc_dev.dv_unit);
251 #endif
252 sc->sc_flags &= ~PPIF_DELAY;
253 wakeup(sc);
254 }
255
256 void
257 ppitimo(arg)
258 void *arg;
259 {
260 struct ppi_softc *sc = arg;
261
262 #ifdef DEBUG
263 if (ppidebug & PDB_FOLLOW)
264 printf("ppitimo(%x)\n", sc->sc_dev.dv_unit);
265 #endif
266 sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
267 wakeup(sc);
268 }
269
270 int
271 ppiread(dev, uio, flags)
272 dev_t dev;
273 struct uio *uio;
274 int flags;
275 {
276
277 #ifdef DEBUG
278 if (ppidebug & PDB_FOLLOW)
279 printf("ppiread(%x, %p)\n", dev, uio);
280 #endif
281 return (ppirw(dev, uio));
282 }
283
284 int
285 ppiwrite(dev, uio, flags)
286 dev_t dev;
287 struct uio *uio;
288 int flags;
289 {
290
291 #ifdef DEBUG
292 if (ppidebug & PDB_FOLLOW)
293 printf("ppiwrite(%x, %p)\n", dev, uio);
294 #endif
295 return (ppirw(dev, uio));
296 }
297
298 int
299 ppirw(dev, uio)
300 dev_t dev;
301 struct uio *uio;
302 {
303 int unit = UNIT(dev);
304 struct ppi_softc *sc = ppi_cd.cd_devs[unit];
305 int s, len, cnt;
306 char *cp;
307 int error = 0, gotdata = 0;
308 int buflen, ctlr, slave;
309 char *buf;
310
311 if (uio->uio_resid == 0)
312 return(0);
313
314 ctlr = sc->sc_dev.dv_parent->dv_unit;
315 slave = sc->sc_slave;
316
317 #ifdef DEBUG
318 if (ppidebug & (PDB_FOLLOW|PDB_IO))
319 printf("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
320 dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
321 sc->sc_burst, sc->sc_timo, uio->uio_resid);
322 #endif
323 buflen = min(sc->sc_burst, uio->uio_resid);
324 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
325 sc->sc_flags |= PPIF_UIO;
326 if (sc->sc_timo > 0) {
327 sc->sc_flags |= PPIF_TIMO;
328 timeout(ppitimo, sc, sc->sc_timo);
329 }
330 len = cnt = 0;
331 while (uio->uio_resid > 0) {
332 len = min(buflen, uio->uio_resid);
333 cp = buf;
334 if (uio->uio_rw == UIO_WRITE) {
335 error = uiomove(cp, len, uio);
336 if (error)
337 break;
338 }
339 again:
340 s = splbio();
341 if ((sc->sc_flags & PPIF_UIO) &&
342 hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq) == 0)
343 sleep(sc, PRIBIO+1);
344 /*
345 * Check if we timed out during sleep or uiomove
346 */
347 (void) spllowersoftclock();
348 if ((sc->sc_flags & PPIF_UIO) == 0) {
349 #ifdef DEBUG
350 if (ppidebug & PDB_IO)
351 printf("ppirw: uiomove/sleep timo, flags %x\n",
352 sc->sc_flags);
353 #endif
354 if (sc->sc_flags & PPIF_TIMO) {
355 untimeout(ppitimo, sc);
356 sc->sc_flags &= ~PPIF_TIMO;
357 }
358 splx(s);
359 break;
360 }
361 splx(s);
362 /*
363 * Perform the operation
364 */
365 if (uio->uio_rw == UIO_WRITE)
366 cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len);
367 else
368 cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len);
369 s = splbio();
370 hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
371 #ifdef DEBUG
372 if (ppidebug & PDB_IO)
373 printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n",
374 uio->uio_rw == UIO_READ ? "recv" : "send",
375 ctlr, slave, sc->sc_sec, cp, len, cnt);
376 #endif
377 splx(s);
378 if (uio->uio_rw == UIO_READ) {
379 if (cnt) {
380 error = uiomove(cp, cnt, uio);
381 if (error)
382 break;
383 gotdata++;
384 }
385 /*
386 * Didn't get anything this time, but did in the past.
387 * Consider us done.
388 */
389 else if (gotdata)
390 break;
391 }
392 s = splsoftclock();
393 /*
394 * Operation timeout (or non-blocking), quit now.
395 */
396 if ((sc->sc_flags & PPIF_UIO) == 0) {
397 #ifdef DEBUG
398 if (ppidebug & PDB_IO)
399 printf("ppirw: timeout/done\n");
400 #endif
401 splx(s);
402 break;
403 }
404 /*
405 * Implement inter-read delay
406 */
407 if (sc->sc_delay > 0) {
408 sc->sc_flags |= PPIF_DELAY;
409 timeout(ppistart, sc, sc->sc_delay);
410 error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0);
411 if (error) {
412 splx(s);
413 break;
414 }
415 }
416 splx(s);
417 /*
418 * Must not call uiomove again til we've used all data
419 * that we already grabbed.
420 */
421 if (uio->uio_rw == UIO_WRITE && cnt != len) {
422 cp += cnt;
423 len -= cnt;
424 cnt = 0;
425 goto again;
426 }
427 }
428 s = splsoftclock();
429 if (sc->sc_flags & PPIF_TIMO) {
430 untimeout(ppitimo, sc);
431 sc->sc_flags &= ~PPIF_TIMO;
432 }
433 if (sc->sc_flags & PPIF_DELAY) {
434 untimeout(ppistart, sc);
435 sc->sc_flags &= ~PPIF_DELAY;
436 }
437 splx(s);
438 /*
439 * Adjust for those chars that we uiomove'ed but never wrote
440 */
441 if (uio->uio_rw == UIO_WRITE && cnt != len) {
442 uio->uio_resid += (len - cnt);
443 #ifdef DEBUG
444 if (ppidebug & PDB_IO)
445 printf("ppirw: short write, adjust by %d\n",
446 len-cnt);
447 #endif
448 }
449 free(buf, M_DEVBUF);
450 #ifdef DEBUG
451 if (ppidebug & (PDB_FOLLOW|PDB_IO))
452 printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
453 #endif
454 return (error);
455 }
456
457 int
458 ppiioctl(dev, cmd, data, flag, p)
459 dev_t dev;
460 u_long cmd;
461 caddr_t data;
462 int flag;
463 struct proc *p;
464 {
465 struct ppi_softc *sc = ppi_cd.cd_devs[UNIT(dev)];
466 struct ppiparam *pp, *upp;
467 int error = 0;
468
469 switch (cmd) {
470 case PPIIOCGPARAM:
471 pp = &sc->sc_param;
472 upp = (struct ppiparam *)data;
473 upp->burst = pp->burst;
474 upp->timo = ppihztoms(pp->timo);
475 upp->delay = ppihztoms(pp->delay);
476 break;
477 case PPIIOCSPARAM:
478 pp = &sc->sc_param;
479 upp = (struct ppiparam *)data;
480 if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
481 upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
482 return(EINVAL);
483 pp->burst = upp->burst;
484 pp->timo = ppimstohz(upp->timo);
485 pp->delay = ppimstohz(upp->delay);
486 break;
487 case PPIIOCSSEC:
488 sc->sc_sec = *(int *)data;
489 break;
490 default:
491 return(EINVAL);
492 }
493 return (error);
494 }
495
496 int
497 ppihztoms(h)
498 int h;
499 {
500 extern int hz;
501 int m = h;
502
503 if (m > 0)
504 m = m * 1000 / hz;
505 return(m);
506 }
507
508 int
509 ppimstohz(m)
510 int m;
511 {
512 extern int hz;
513 int h = m;
514
515 if (h > 0) {
516 h = h * hz / 1000;
517 if (h == 0)
518 h = 1000 / hz;
519 }
520 return(h);
521 }
522