ppi.c revision 1.37 1 /* $NetBSD: ppi.c,v 1.37 2007/07/12 20:39:01 he 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. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)ppi.c 8.1 (Berkeley) 6/16/93
68 */
69
70 /*
71 * Printer/Plotter HPIB interface
72 */
73
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: ppi.c,v 1.37 2007/07/12 20:39:01 he Exp $");
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/callout.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 #include "ioconf.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 static int ppimatch(struct device *, struct cfdata *, void *);
115 static void ppiattach(struct device *, struct device *, void *);
116
117 CFATTACH_DECL(ppi, sizeof(struct ppi_softc),
118 ppimatch, ppiattach, NULL, NULL);
119
120 static dev_type_open(ppiopen);
121 static dev_type_close(ppiclose);
122 static dev_type_read(ppiread);
123 static dev_type_write(ppiwrite);
124 static dev_type_ioctl(ppiioctl);
125
126 const struct cdevsw ppi_cdevsw = {
127 ppiopen, ppiclose, ppiread, ppiwrite, ppiioctl,
128 nostop, notty, nopoll, nommap, nokqfilter,
129 };
130
131 static void ppistart(void *);
132 static void ppinoop(void *);
133
134 static void ppitimo(void *);
135 static int ppirw(dev_t, struct uio *);
136 static int ppihztoms(int);
137 static int ppimstohz(int);
138
139 #define UNIT(x) minor(x)
140
141 #ifdef DEBUG
142 int ppidebug = 0x80;
143 #define PDB_FOLLOW 0x01
144 #define PDB_IO 0x02
145 #define PDB_NOCHECK 0x80
146 #endif
147
148 static int
149 ppimatch(struct device *parent, struct cfdata *match, void *aux)
150 {
151 struct hpibbus_attach_args *ha = aux;
152
153 /*
154 * The printer/plotter doesn't return an ID tag.
155 * The check below prevents us from matching a CS80
156 * device by mistake.
157 */
158 if (ha->ha_id & 0x200)
159 return 0;
160
161 /*
162 * To prevent matching all unused slots on the bus, we
163 * don't allow wildcarded locators.
164 */
165 if (match->hpibbuscf_slave == HPIBBUSCF_SLAVE_DEFAULT ||
166 match->hpibbuscf_punit == HPIBBUSCF_PUNIT_DEFAULT)
167 return 0;
168
169 return 1;
170 }
171
172 static void
173 ppiattach(struct device *parent, struct device *self, 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, 0);
183 callout_init(&sc->sc_start_ch, 0);
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 static void
196 ppinoop(void *arg)
197 {
198 /* Noop! */
199 }
200
201 int
202 ppiopen(dev_t dev, int flags, int fmt, struct lwp *l)
203 {
204 int unit = UNIT(dev);
205 struct ppi_softc *sc;
206
207 if (unit >= ppi_cd.cd_ndevs ||
208 (sc = ppi_cd.cd_devs[unit]) == NULL ||
209 (sc->sc_flags & PPIF_ALIVE) == 0)
210 return ENXIO;
211
212 #ifdef DEBUG
213 if (ppidebug & PDB_FOLLOW)
214 printf("ppiopen(%x, %x): flags %x\n",
215 dev, flags, sc->sc_flags);
216 #endif
217 if (sc->sc_flags & PPIF_OPEN)
218 return EBUSY;
219 sc->sc_flags |= PPIF_OPEN;
220 sc->sc_burst = PPI_BURST;
221 sc->sc_timo = ppimstohz(PPI_TIMO);
222 sc->sc_delay = ppimstohz(PPI_DELAY);
223 sc->sc_sec = -1;
224 return 0;
225 }
226
227 static int
228 ppiclose(dev_t dev, int flags, int fmt, struct lwp *l)
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 static void
243 ppistart(void *arg)
244 {
245 struct ppi_softc *sc = arg;
246
247 #ifdef DEBUG
248 if (ppidebug & PDB_FOLLOW)
249 printf("ppistart(%x)\n", device_unit(&sc->sc_dev));
250 #endif
251 sc->sc_flags &= ~PPIF_DELAY;
252 wakeup(sc);
253 }
254
255 static void
256 ppitimo(void *arg)
257 {
258 struct ppi_softc *sc = arg;
259
260 #ifdef DEBUG
261 if (ppidebug & PDB_FOLLOW)
262 printf("ppitimo(%x)\n", device_unit(&sc->sc_dev));
263 #endif
264 sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
265 wakeup(sc);
266 }
267
268 static int
269 ppiread(dev_t dev, struct uio *uio, int flags)
270 {
271
272 #ifdef DEBUG
273 if (ppidebug & PDB_FOLLOW)
274 printf("ppiread(%x, %p)\n", dev, uio);
275 #endif
276 return ppirw(dev, uio);
277 }
278
279 static int
280 ppiwrite(dev_t dev, struct uio *uio, int flags)
281 {
282
283 #ifdef DEBUG
284 if (ppidebug & PDB_FOLLOW)
285 printf("ppiwrite(%x, %p)\n", dev, uio);
286 #endif
287 return ppirw(dev, uio);
288 }
289
290 static int
291 ppirw(dev_t dev, struct uio *uio)
292 {
293 int unit = UNIT(dev);
294 struct ppi_softc *sc = ppi_cd.cd_devs[unit];
295 int s, s2, len, cnt;
296 char *cp;
297 int error = 0, gotdata = 0;
298 int buflen, ctlr, slave;
299 char *buf;
300
301 if (uio->uio_resid == 0)
302 return 0;
303
304 ctlr = device_unit(device_parent(&sc->sc_dev));
305 slave = sc->sc_slave;
306
307 #ifdef DEBUG
308 if (ppidebug & (PDB_FOLLOW|PDB_IO))
309 printf("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
310 dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
311 sc->sc_burst, sc->sc_timo, uio->uio_resid);
312 #endif
313 buflen = min(sc->sc_burst, uio->uio_resid);
314 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
315 sc->sc_flags |= PPIF_UIO;
316 if (sc->sc_timo > 0) {
317 sc->sc_flags |= PPIF_TIMO;
318 callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
319 }
320 len = cnt = 0;
321 while (uio->uio_resid > 0) {
322 len = min(buflen, uio->uio_resid);
323 cp = buf;
324 if (uio->uio_rw == UIO_WRITE) {
325 error = uiomove(cp, len, uio);
326 if (error)
327 break;
328 }
329 again:
330 s = splsoftclock();
331 s2 = splbio();
332 if ((sc->sc_flags & PPIF_UIO) &&
333 hpibreq(device_parent(&sc->sc_dev), &sc->sc_hq) == 0)
334 (void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
335 /*
336 * Check if we timed out during sleep or uiomove
337 */
338 splx(s2);
339 if ((sc->sc_flags & PPIF_UIO) == 0) {
340 #ifdef DEBUG
341 if (ppidebug & PDB_IO)
342 printf("ppirw: uiomove/sleep timo, flags %x\n",
343 sc->sc_flags);
344 #endif
345 if (sc->sc_flags & PPIF_TIMO) {
346 callout_stop(&sc->sc_timo_ch);
347 sc->sc_flags &= ~PPIF_TIMO;
348 }
349 splx(s);
350 break;
351 }
352 splx(s);
353 /*
354 * Perform the operation
355 */
356 if (uio->uio_rw == UIO_WRITE)
357 cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len);
358 else
359 cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len);
360 s = splbio();
361 hpibfree(device_parent(&sc->sc_dev), &sc->sc_hq);
362 #ifdef DEBUG
363 if (ppidebug & PDB_IO)
364 printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n",
365 uio->uio_rw == UIO_READ ? "recv" : "send",
366 ctlr, slave, sc->sc_sec, cp, len, cnt);
367 #endif
368 splx(s);
369 if (uio->uio_rw == UIO_READ) {
370 if (cnt) {
371 error = uiomove(cp, cnt, uio);
372 if (error)
373 break;
374 gotdata++;
375 }
376 /*
377 * Didn't get anything this time, but did in the past.
378 * Consider us done.
379 */
380 else if (gotdata)
381 break;
382 }
383 s = splsoftclock();
384 /*
385 * Operation timeout (or non-blocking), quit now.
386 */
387 if ((sc->sc_flags & PPIF_UIO) == 0) {
388 #ifdef DEBUG
389 if (ppidebug & PDB_IO)
390 printf("ppirw: timeout/done\n");
391 #endif
392 splx(s);
393 break;
394 }
395 /*
396 * Implement inter-read delay
397 */
398 if (sc->sc_delay > 0) {
399 sc->sc_flags |= PPIF_DELAY;
400 callout_reset(&sc->sc_start_ch, sc->sc_delay,
401 ppistart, sc);
402 error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0);
403 if (error) {
404 splx(s);
405 break;
406 }
407 }
408 splx(s);
409 /*
410 * Must not call uiomove again til we've used all data
411 * that we already grabbed.
412 */
413 if (uio->uio_rw == UIO_WRITE && cnt != len) {
414 cp += cnt;
415 len -= cnt;
416 cnt = 0;
417 goto again;
418 }
419 }
420 s = splsoftclock();
421 if (sc->sc_flags & PPIF_TIMO) {
422 callout_stop(&sc->sc_timo_ch);
423 sc->sc_flags &= ~PPIF_TIMO;
424 }
425 if (sc->sc_flags & PPIF_DELAY) {
426 callout_stop(&sc->sc_start_ch);
427 sc->sc_flags &= ~PPIF_DELAY;
428 }
429 splx(s);
430 /*
431 * Adjust for those chars that we uiomove'ed but never wrote
432 */
433 if (uio->uio_rw == UIO_WRITE && cnt != len) {
434 uio->uio_resid += (len - cnt);
435 #ifdef DEBUG
436 if (ppidebug & PDB_IO)
437 printf("ppirw: short write, adjust by %d\n",
438 len-cnt);
439 #endif
440 }
441 free(buf, M_DEVBUF);
442 #ifdef DEBUG
443 if (ppidebug & (PDB_FOLLOW|PDB_IO))
444 printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
445 #endif
446 return error;
447 }
448
449 static int
450 ppiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
451 {
452 struct ppi_softc *sc = ppi_cd.cd_devs[UNIT(dev)];
453 struct ppiparam *pp, *upp;
454 int error = 0;
455
456 switch (cmd) {
457 case PPIIOCGPARAM:
458 pp = &sc->sc_param;
459 upp = (struct ppiparam *)data;
460 upp->burst = pp->burst;
461 upp->timo = ppihztoms(pp->timo);
462 upp->delay = ppihztoms(pp->delay);
463 break;
464 case PPIIOCSPARAM:
465 pp = &sc->sc_param;
466 upp = (struct ppiparam *)data;
467 if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
468 upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
469 return EINVAL;
470 pp->burst = upp->burst;
471 pp->timo = ppimstohz(upp->timo);
472 pp->delay = ppimstohz(upp->delay);
473 break;
474 case PPIIOCSSEC:
475 sc->sc_sec = *(int *)data;
476 break;
477 default:
478 return EINVAL;
479 }
480 return error;
481 }
482
483 static int
484 ppihztoms(int h)
485 {
486 extern int hz;
487 int m = h;
488
489 if (m > 0)
490 m = m * 1000 / hz;
491 return m;
492 }
493
494 static int
495 ppimstohz(int m)
496 {
497 extern int hz;
498 int h = m;
499
500 if (h > 0) {
501 h = h * hz / 1000;
502 if (h == 0)
503 h = 1000 / hz;
504 }
505 return h;
506 }
507