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