ppi.c revision 1.39 1 /* $NetBSD: ppi.c,v 1.39 2008/03/29 06:47:08 tsutsui 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.39 2008/03/29 06:47:08 tsutsui 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 device_t 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(device_t, cfdata_t, void *);
115 static void ppiattach(device_t, device_t, void *);
116
117 CFATTACH_DECL_NEW(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(device_t parent, cfdata_t cf, 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 (cf->hpibbuscf_slave == HPIBBUSCF_SLAVE_DEFAULT ||
166 cf->hpibbuscf_punit == HPIBBUSCF_PUNIT_DEFAULT)
167 return 0;
168
169 return 1;
170 }
171
172 static void
173 ppiattach(device_t parent, device_t self, void *aux)
174 {
175 struct ppi_softc *sc = device_private(self);
176 struct hpibbus_attach_args *ha = aux;
177
178 sc->sc_dev = self;
179 aprint_normal("\n");
180
181 sc->sc_slave = ha->ha_slave;
182
183 callout_init(&sc->sc_timo_ch, 0);
184 callout_init(&sc->sc_start_ch, 0);
185
186 /* Initialize the hpib queue entry. */
187 sc->sc_hq.hq_softc = sc;
188 sc->sc_hq.hq_slave = sc->sc_slave;
189 sc->sc_hq.hq_start = ppistart;
190 sc->sc_hq.hq_go = ppinoop;
191 sc->sc_hq.hq_intr = ppinoop;
192
193 sc->sc_flags = PPIF_ALIVE;
194 }
195
196 static void
197 ppinoop(void *arg)
198 {
199 /* Noop! */
200 }
201
202 int
203 ppiopen(dev_t dev, int flags, int fmt, struct lwp *l)
204 {
205 int unit = UNIT(dev);
206 struct ppi_softc *sc;
207
208 if (unit >= ppi_cd.cd_ndevs ||
209 (sc = device_private(ppi_cd.cd_devs[unit])) == NULL ||
210 (sc->sc_flags & PPIF_ALIVE) == 0)
211 return ENXIO;
212
213 #ifdef DEBUG
214 if (ppidebug & PDB_FOLLOW)
215 printf("ppiopen(%x, %x): flags %x\n",
216 dev, flags, sc->sc_flags);
217 #endif
218 if (sc->sc_flags & PPIF_OPEN)
219 return EBUSY;
220 sc->sc_flags |= PPIF_OPEN;
221 sc->sc_burst = PPI_BURST;
222 sc->sc_timo = ppimstohz(PPI_TIMO);
223 sc->sc_delay = ppimstohz(PPI_DELAY);
224 sc->sc_sec = -1;
225 return 0;
226 }
227
228 static int
229 ppiclose(dev_t dev, int flags, int fmt, struct lwp *l)
230 {
231 int unit = UNIT(dev);
232 struct ppi_softc *sc = device_private(ppi_cd.cd_devs[unit]);
233
234 #ifdef DEBUG
235 if (ppidebug & PDB_FOLLOW)
236 printf("ppiclose(%x, %x): flags %x\n",
237 dev, flags, sc->sc_flags);
238 #endif
239 sc->sc_flags &= ~PPIF_OPEN;
240 return 0;
241 }
242
243 static void
244 ppistart(void *arg)
245 {
246 struct ppi_softc *sc = arg;
247
248 #ifdef DEBUG
249 if (ppidebug & PDB_FOLLOW)
250 printf("ppistart(%x)\n", device_unit(sc->sc_dev));
251 #endif
252 sc->sc_flags &= ~PPIF_DELAY;
253 wakeup(sc);
254 }
255
256 static void
257 ppitimo(void *arg)
258 {
259 struct ppi_softc *sc = arg;
260
261 #ifdef DEBUG
262 if (ppidebug & PDB_FOLLOW)
263 printf("ppitimo(%x)\n", device_unit(sc->sc_dev));
264 #endif
265 sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
266 wakeup(sc);
267 }
268
269 static int
270 ppiread(dev_t dev, struct uio *uio, int flags)
271 {
272
273 #ifdef DEBUG
274 if (ppidebug & PDB_FOLLOW)
275 printf("ppiread(%x, %p)\n", dev, uio);
276 #endif
277 return ppirw(dev, uio);
278 }
279
280 static int
281 ppiwrite(dev_t dev, struct uio *uio, int flags)
282 {
283
284 #ifdef DEBUG
285 if (ppidebug & PDB_FOLLOW)
286 printf("ppiwrite(%x, %p)\n", dev, uio);
287 #endif
288 return ppirw(dev, uio);
289 }
290
291 static int
292 ppirw(dev_t dev, struct uio *uio)
293 {
294 int unit = UNIT(dev);
295 struct ppi_softc *sc = device_private(ppi_cd.cd_devs[unit]);
296 int s, s2, len, cnt;
297 char *cp;
298 int error = 0, gotdata = 0;
299 int buflen, ctlr, slave;
300 char *buf;
301
302 if (uio->uio_resid == 0)
303 return 0;
304
305 ctlr = device_unit(device_parent(sc->sc_dev));
306 slave = sc->sc_slave;
307
308 #ifdef DEBUG
309 if (ppidebug & (PDB_FOLLOW|PDB_IO))
310 printf("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
311 dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
312 sc->sc_burst, sc->sc_timo, uio->uio_resid);
313 #endif
314 buflen = min(sc->sc_burst, uio->uio_resid);
315 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
316 sc->sc_flags |= PPIF_UIO;
317 if (sc->sc_timo > 0) {
318 sc->sc_flags |= PPIF_TIMO;
319 callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
320 }
321 len = cnt = 0;
322 while (uio->uio_resid > 0) {
323 len = min(buflen, uio->uio_resid);
324 cp = buf;
325 if (uio->uio_rw == UIO_WRITE) {
326 error = uiomove(cp, len, uio);
327 if (error)
328 break;
329 }
330 again:
331 s = splsoftclock();
332 s2 = splbio();
333 if ((sc->sc_flags & PPIF_UIO) &&
334 hpibreq(device_parent(sc->sc_dev), &sc->sc_hq) == 0)
335 (void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
336 /*
337 * Check if we timed out during sleep or uiomove
338 */
339 splx(s2);
340 if ((sc->sc_flags & PPIF_UIO) == 0) {
341 #ifdef DEBUG
342 if (ppidebug & PDB_IO)
343 printf("ppirw: uiomove/sleep timo, flags %x\n",
344 sc->sc_flags);
345 #endif
346 if (sc->sc_flags & PPIF_TIMO) {
347 callout_stop(&sc->sc_timo_ch);
348 sc->sc_flags &= ~PPIF_TIMO;
349 }
350 splx(s);
351 break;
352 }
353 splx(s);
354 /*
355 * Perform the operation
356 */
357 if (uio->uio_rw == UIO_WRITE)
358 cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len);
359 else
360 cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len);
361 s = splbio();
362 hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
363 #ifdef DEBUG
364 if (ppidebug & PDB_IO)
365 printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n",
366 uio->uio_rw == UIO_READ ? "recv" : "send",
367 ctlr, slave, sc->sc_sec, cp, len, cnt);
368 #endif
369 splx(s);
370 if (uio->uio_rw == UIO_READ) {
371 if (cnt) {
372 error = uiomove(cp, cnt, uio);
373 if (error)
374 break;
375 gotdata++;
376 }
377 /*
378 * Didn't get anything this time, but did in the past.
379 * Consider us done.
380 */
381 else if (gotdata)
382 break;
383 }
384 s = splsoftclock();
385 /*
386 * Operation timeout (or non-blocking), quit now.
387 */
388 if ((sc->sc_flags & PPIF_UIO) == 0) {
389 #ifdef DEBUG
390 if (ppidebug & PDB_IO)
391 printf("ppirw: timeout/done\n");
392 #endif
393 splx(s);
394 break;
395 }
396 /*
397 * Implement inter-read delay
398 */
399 if (sc->sc_delay > 0) {
400 sc->sc_flags |= PPIF_DELAY;
401 callout_reset(&sc->sc_start_ch, sc->sc_delay,
402 ppistart, sc);
403 error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0);
404 if (error) {
405 splx(s);
406 break;
407 }
408 }
409 splx(s);
410 /*
411 * Must not call uiomove again til we've used all data
412 * that we already grabbed.
413 */
414 if (uio->uio_rw == UIO_WRITE && cnt != len) {
415 cp += cnt;
416 len -= cnt;
417 cnt = 0;
418 goto again;
419 }
420 }
421 s = splsoftclock();
422 if (sc->sc_flags & PPIF_TIMO) {
423 callout_stop(&sc->sc_timo_ch);
424 sc->sc_flags &= ~PPIF_TIMO;
425 }
426 if (sc->sc_flags & PPIF_DELAY) {
427 callout_stop(&sc->sc_start_ch);
428 sc->sc_flags &= ~PPIF_DELAY;
429 }
430 splx(s);
431 /*
432 * Adjust for those chars that we uiomove'ed but never wrote
433 */
434 if (uio->uio_rw == UIO_WRITE && cnt != len) {
435 uio->uio_resid += (len - cnt);
436 #ifdef DEBUG
437 if (ppidebug & PDB_IO)
438 printf("ppirw: short write, adjust by %d\n",
439 len-cnt);
440 #endif
441 }
442 free(buf, M_DEVBUF);
443 #ifdef DEBUG
444 if (ppidebug & (PDB_FOLLOW|PDB_IO))
445 printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
446 #endif
447 return error;
448 }
449
450 static int
451 ppiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
452 {
453 struct ppi_softc *sc = device_private(ppi_cd.cd_devs[UNIT(dev)]);
454 struct ppiparam *pp, *upp;
455 int error = 0;
456
457 switch (cmd) {
458 case PPIIOCGPARAM:
459 pp = &sc->sc_param;
460 upp = (struct ppiparam *)data;
461 upp->burst = pp->burst;
462 upp->timo = ppihztoms(pp->timo);
463 upp->delay = ppihztoms(pp->delay);
464 break;
465 case PPIIOCSPARAM:
466 pp = &sc->sc_param;
467 upp = (struct ppiparam *)data;
468 if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
469 upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
470 return EINVAL;
471 pp->burst = upp->burst;
472 pp->timo = ppimstohz(upp->timo);
473 pp->delay = ppimstohz(upp->delay);
474 break;
475 case PPIIOCSSEC:
476 sc->sc_sec = *(int *)data;
477 break;
478 default:
479 return EINVAL;
480 }
481 return error;
482 }
483
484 static int
485 ppihztoms(int h)
486 {
487 extern int hz;
488 int m = h;
489
490 if (m > 0)
491 m = m * 1000 / hz;
492 return m;
493 }
494
495 static int
496 ppimstohz(int m)
497 {
498 extern int hz;
499 int h = m;
500
501 if (h > 0) {
502 h = h * hz / 1000;
503 if (h == 0)
504 h = 1000 / hz;
505 }
506 return h;
507 }
508