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