xref: /netbsd-src/sys/dev/gpib/ppi.c (revision 4b71a66d0f279143147d63ebfcfd8a59499a3684)
1 /*	$NetBSD: ppi.c,v 1.12 2008/04/28 20:23:48 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996-2003 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 GPIB interface
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: ppi.c,v 1.12 2008/04/28 20:23:48 martin 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/malloc.h>
76 #include <sys/proc.h>
77 #include <sys/uio.h>
78 
79 #include <dev/gpib/gpibvar.h>
80 
81 #include <dev/gpib/ppiio.h>
82 
83 struct	ppi_softc {
84 	struct device sc_dev;
85 	gpib_chipset_tag_t sc_ic;
86 	gpib_handle_t sc_hdl;
87 
88 	int sc_address;			/* GPIB address */
89 	int	sc_flags;
90 	int	sc_sec;
91 	struct	ppiparam sc_param;
92 #define sc_burst sc_param.burst
93 #define sc_timo  sc_param.timo
94 #define sc_delay sc_param.delay
95 	struct	callout sc_timo_ch;
96 	struct	callout sc_start_ch;
97 };
98 
99 /* sc_flags values */
100 #define	PPIF_ALIVE	0x01
101 #define	PPIF_OPEN	0x02
102 #define PPIF_UIO	0x04
103 #define PPIF_TIMO	0x08
104 #define PPIF_DELAY	0x10
105 
106 int	ppimatch(struct device *, struct cfdata *, void *);
107 void	ppiattach(struct device *, struct device *, void *);
108 
109 CFATTACH_DECL(ppi, sizeof(struct ppi_softc),
110 	ppimatch, ppiattach, NULL, NULL);
111 
112 extern struct cfdriver ppi_cd;
113 
114 void	ppicallback(void *, int);
115 void	ppistart(void *);
116 
117 void	ppitimo(void *);
118 int	ppirw(dev_t, struct uio *);
119 int	ppihztoms(int);
120 int	ppimstohz(int);
121 
122 dev_type_open(ppiopen);
123 dev_type_close(ppiclose);
124 dev_type_read(ppiread);
125 dev_type_write(ppiwrite);
126 dev_type_ioctl(ppiioctl);
127 
128 const struct cdevsw ppi_cdevsw = {
129         ppiopen, ppiclose, ppiread, ppiwrite, ppiioctl,
130         nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
131 };
132 
133 #define UNIT(x)		minor(x)
134 
135 #ifdef DEBUG
136 int	ppidebug = 0x80;
137 #define PDB_FOLLOW	0x01
138 #define PDB_IO		0x02
139 #define PDB_NOCHECK	0x80
140 #define DPRINTF(mask, str)	if (ppidebug & (mask)) printf str
141 #else
142 #define DPRINTF(mask, str)	/* nothing */
143 #endif
144 
145 int
146 ppimatch(parent, match, aux)
147 	struct device *parent;
148 	struct cfdata *match;
149 	void *aux;
150 {
151 
152 	return (1);
153 }
154 
155 void
156 ppiattach(parent, self, aux)
157 	struct device *parent, *self;
158 	void *aux;
159 {
160 	struct ppi_softc *sc = device_private(self);
161 	struct gpib_attach_args *ga = aux;
162 
163 	printf("\n");
164 
165 	sc->sc_ic = ga->ga_ic;
166 	sc->sc_address = ga->ga_address;
167 
168 	callout_init(&sc->sc_timo_ch, 0);
169 	callout_init(&sc->sc_start_ch, 0);
170 
171 	if (gpibregister(sc->sc_ic, sc->sc_address, ppicallback, sc,
172 	    &sc->sc_hdl)) {
173 		aprint_error_dev(&sc->sc_dev, "can't register callback\n");
174 		return;
175 	}
176 
177 	sc->sc_flags = PPIF_ALIVE;
178 }
179 
180 int
181 ppiopen(dev, flags, fmt, l)
182 	dev_t dev;
183 	int flags, fmt;
184 	struct lwp *l;
185 {
186 	int unit = UNIT(dev);
187 	struct ppi_softc *sc;
188 
189 	if (unit >= ppi_cd.cd_ndevs ||
190 	    (sc = ppi_cd.cd_devs[unit]) == NULL ||
191 	    (sc->sc_flags & PPIF_ALIVE) == 0)
192 		return (ENXIO);
193 
194 	DPRINTF(PDB_FOLLOW, ("ppiopen(%x, %x): flags %x\n",
195 	    dev, flags, sc->sc_flags));
196 
197 	if (sc->sc_flags & PPIF_OPEN)
198 		return (EBUSY);
199 	sc->sc_flags |= PPIF_OPEN;
200 	sc->sc_burst = PPI_BURST;
201 	sc->sc_timo = ppimstohz(PPI_TIMO);
202 	sc->sc_delay = ppimstohz(PPI_DELAY);
203 	sc->sc_sec = -1;
204 	return (0);
205 }
206 
207 int
208 ppiclose(dev, flags, fmt, l)
209 	dev_t dev;
210 	int flags, fmt;
211 	struct lwp *l;
212 {
213 	int unit = UNIT(dev);
214 	struct ppi_softc *sc = ppi_cd.cd_devs[unit];
215 
216 	DPRINTF(PDB_FOLLOW, ("ppiclose(%x, %x): flags %x\n",
217 		       dev, flags, sc->sc_flags));
218 
219 	sc->sc_flags &= ~PPIF_OPEN;
220 	return (0);
221 }
222 
223 void
224 ppicallback(v, action)
225 	void *v;
226 	int action;
227 {
228 	struct ppi_softc *sc = v;
229 
230 	DPRINTF(PDB_FOLLOW, ("ppicallback: v=%p, action=%d\n", v, action));
231 
232 	switch (action) {
233 	case GPIBCBF_START:
234 		ppistart(sc);
235 	case GPIBCBF_INTR:
236 		/* no-op */
237 		break;
238 #ifdef DEBUG
239 	default:
240 		DPRINTF(PDB_FOLLOW, ("ppicallback: unknown action %d\n",
241 		    action));
242 		break;
243 #endif
244 	}
245 }
246 
247 void
248 ppistart(v)
249 	void *v;
250 {
251 	struct ppi_softc *sc = v;
252 
253 	DPRINTF(PDB_FOLLOW, ("ppistart(%x)\n", device_unit(&sc->sc_dev)));
254 
255 	sc->sc_flags &= ~PPIF_DELAY;
256 	wakeup(sc);
257 }
258 
259 void
260 ppitimo(arg)
261 	void *arg;
262 {
263 	struct ppi_softc *sc = arg;
264 
265 	DPRINTF(PDB_FOLLOW, ("ppitimo(%x)\n", device_unit(&sc->sc_dev)));
266 
267 	sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
268 	wakeup(sc);
269 }
270 
271 int
272 ppiread(dev, uio, flags)
273 	dev_t dev;
274 	struct uio *uio;
275 	int flags;
276 {
277 
278 	DPRINTF(PDB_FOLLOW, ("ppiread(%x, %p)\n", dev, uio));
279 
280 	return (ppirw(dev, uio));
281 }
282 
283 int
284 ppiwrite(dev, uio, flags)
285 	dev_t dev;
286 	struct uio *uio;
287 	int flags;
288 {
289 
290 	DPRINTF(PDB_FOLLOW, ("ppiwrite(%x, %p)\n", dev, uio));
291 
292 	return (ppirw(dev, uio));
293 }
294 
295 int
296 ppirw(dev, uio)
297 	dev_t dev;
298 	struct uio *uio;
299 {
300 	int unit = UNIT(dev);
301 	struct ppi_softc *sc = ppi_cd.cd_devs[unit];
302 	int s1, s2, len, cnt;
303 	char *cp;
304 	int error = 0, gotdata = 0;
305 	int buflen, address;
306 	char *buf;
307 
308 	if (uio->uio_resid == 0)
309 		return (0);
310 
311 	address = sc->sc_address;
312 
313 	DPRINTF(PDB_FOLLOW|PDB_IO,
314 	    ("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
315 	    dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
316 	    sc->sc_burst, sc->sc_timo, uio->uio_resid));
317 
318 	buflen = min(sc->sc_burst, uio->uio_resid);
319 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
320 	sc->sc_flags |= PPIF_UIO;
321 	if (sc->sc_timo > 0) {
322 		sc->sc_flags |= PPIF_TIMO;
323 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
324 	}
325 	len = cnt = 0;
326 	while (uio->uio_resid > 0) {
327 		len = min(buflen, uio->uio_resid);
328 		cp = buf;
329 		if (uio->uio_rw == UIO_WRITE) {
330 			error = uiomove(cp, len, uio);
331 			if (error)
332 				break;
333 		}
334 again:
335 		s1 = splsoftclock();
336 		s2 = splbio();
337 		if (sc->sc_flags & PPIF_UIO) {
338 			if (gpibrequest(sc->sc_ic, sc->sc_hdl) == 0)
339 				(void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
340 		}
341 		/*
342 		 * Check if we timed out during sleep or uiomove
343 		 */
344 		splx(s2);
345 		if ((sc->sc_flags & PPIF_UIO) == 0) {
346 			DPRINTF(PDB_IO,
347 			    ("ppirw: uiomove/sleep timo, flags %x\n",
348 			    sc->sc_flags));
349 			if (sc->sc_flags & PPIF_TIMO) {
350 				callout_stop(&sc->sc_timo_ch);
351 				sc->sc_flags &= ~PPIF_TIMO;
352 			}
353 			splx(s1);
354 			break;
355 		}
356 		splx(s1);
357 		/*
358 		 * Perform the operation
359 		 */
360 		if (uio->uio_rw == UIO_WRITE)
361 			cnt = gpibsend(sc->sc_ic, address, sc->sc_sec,
362 			    cp, len);
363 		else
364 			cnt = gpibrecv(sc->sc_ic, address, sc->sc_sec,
365 			    cp, len);
366 		s1 = splbio();
367 		gpibrelease(sc->sc_ic, sc->sc_hdl);
368 		DPRINTF(PDB_IO, ("ppirw: %s(%d, %x, %p, %d) -> %d\n",
369 		    uio->uio_rw == UIO_READ ? "recv" : "send",
370 		    address, sc->sc_sec, cp, len, cnt));
371 		splx(s1);
372 		if (uio->uio_rw == UIO_READ) {
373 			if (cnt) {
374 				error = uiomove(cp, cnt, uio);
375 				if (error)
376 					break;
377 				gotdata++;
378 			}
379 			/*
380 			 * Didn't get anything this time, but did in the past.
381 			 * Consider us done.
382 			 */
383 			else if (gotdata)
384 				break;
385 		}
386 		s1 = splsoftclock();
387 		/*
388 		 * Operation timeout (or non-blocking), quit now.
389 		 */
390 		if ((sc->sc_flags & PPIF_UIO) == 0) {
391 			DPRINTF(PDB_IO, ("ppirw: timeout/done\n"));
392 			splx(s1);
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, "gpib", 0);
403 			if (error) {
404 				splx(s1);
405 				break;
406 			}
407 		}
408 		splx(s1);
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 	s1 = 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(s1);
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 		DPRINTF(PDB_IO, ("ppirw: short write, adjust by %d\n",
436 		    len - cnt));
437 	}
438 	free(buf, M_DEVBUF);
439 	DPRINTF(PDB_FOLLOW|PDB_IO, ("ppirw: return %d, resid %d\n",
440 	    error, uio->uio_resid));
441 	return (error);
442 }
443 
444 int
445 ppiioctl(dev, cmd, data, flag, l)
446 	dev_t dev;
447 	u_long cmd;
448 	void *data;
449 	int flag;
450 	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 int
484 ppihztoms(h)
485 	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 int
496 ppimstohz(m)
497 	int m;
498 {
499 	extern int hz;
500 	int h = m;
501 
502 	if (h > 0) {
503 		h = h * hz / 1000;
504 		if (h == 0)
505 			h = 1000 / hz;
506 	}
507 	return (h);
508 }
509