xref: /netbsd-src/sys/dev/gpib/ppi.c (revision 454af1c0e885cbba6b59706391f770d646303f8c)
1 /*	$NetBSD: ppi.c,v 1.14 2009/03/14 15:36:17 dsl 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.14 2009/03/14 15:36:17 dsl 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(struct device *parent, struct cfdata *match, void *aux)
147 {
148 
149 	return (1);
150 }
151 
152 void
153 ppiattach(parent, self, aux)
154 	struct device *parent, *self;
155 	void *aux;
156 {
157 	struct ppi_softc *sc = device_private(self);
158 	struct gpib_attach_args *ga = aux;
159 
160 	printf("\n");
161 
162 	sc->sc_ic = ga->ga_ic;
163 	sc->sc_address = ga->ga_address;
164 
165 	callout_init(&sc->sc_timo_ch, 0);
166 	callout_init(&sc->sc_start_ch, 0);
167 
168 	if (gpibregister(sc->sc_ic, sc->sc_address, ppicallback, sc,
169 	    &sc->sc_hdl)) {
170 		aprint_error_dev(&sc->sc_dev, "can't register callback\n");
171 		return;
172 	}
173 
174 	sc->sc_flags = PPIF_ALIVE;
175 }
176 
177 int
178 ppiopen(dev_t dev, int flags, int fmt, struct lwp *l)
179 {
180 	struct ppi_softc *sc;
181 
182 	sc = device_lookup_private(&ppi_cd, UNIT(dev));
183 	if (sc == NULL)
184 		return (ENXIO);
185 
186 	if (sc->sc_flags & PPIF_ALIVE) == 0)
187 		return (ENXIO);
188 
189 	DPRINTF(PDB_FOLLOW, ("ppiopen(%x, %x): flags %x\n",
190 	    dev, flags, sc->sc_flags));
191 
192 	if (sc->sc_flags & PPIF_OPEN)
193 		return (EBUSY);
194 	sc->sc_flags |= PPIF_OPEN;
195 	sc->sc_burst = PPI_BURST;
196 	sc->sc_timo = ppimstohz(PPI_TIMO);
197 	sc->sc_delay = ppimstohz(PPI_DELAY);
198 	sc->sc_sec = -1;
199 	return (0);
200 }
201 
202 int
203 ppiclose(dev_t dev, int flags, int fmt, struct lwp *l)
204 {
205 	struct ppi_softc *sc;
206 
207 	sc = device_lookup_private(&ppi_cd, UNIT(dev));
208 
209 	DPRINTF(PDB_FOLLOW, ("ppiclose(%x, %x): flags %x\n",
210 		       dev, flags, sc->sc_flags));
211 
212 	sc->sc_flags &= ~PPIF_OPEN;
213 	return (0);
214 }
215 
216 void
217 ppicallback(void *v, int action)
218 {
219 	struct ppi_softc *sc = v;
220 
221 	DPRINTF(PDB_FOLLOW, ("ppicallback: v=%p, action=%d\n", v, action));
222 
223 	switch (action) {
224 	case GPIBCBF_START:
225 		ppistart(sc);
226 	case GPIBCBF_INTR:
227 		/* no-op */
228 		break;
229 #ifdef DEBUG
230 	default:
231 		DPRINTF(PDB_FOLLOW, ("ppicallback: unknown action %d\n",
232 		    action));
233 		break;
234 #endif
235 	}
236 }
237 
238 void
239 ppistart(void *v)
240 {
241 	struct ppi_softc *sc = v;
242 
243 	DPRINTF(PDB_FOLLOW, ("ppistart(%x)\n", device_unit(&sc->sc_dev)));
244 
245 	sc->sc_flags &= ~PPIF_DELAY;
246 	wakeup(sc);
247 }
248 
249 void
250 ppitimo(void *arg)
251 {
252 	struct ppi_softc *sc = arg;
253 
254 	DPRINTF(PDB_FOLLOW, ("ppitimo(%x)\n", device_unit(&sc->sc_dev)));
255 
256 	sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
257 	wakeup(sc);
258 }
259 
260 int
261 ppiread(dev_t dev, struct uio *uio, int flags)
262 {
263 
264 	DPRINTF(PDB_FOLLOW, ("ppiread(%x, %p)\n", dev, uio));
265 
266 	return (ppirw(dev, uio));
267 }
268 
269 int
270 ppiwrite(dev_t dev, struct uio *uio, int flags)
271 {
272 
273 	DPRINTF(PDB_FOLLOW, ("ppiwrite(%x, %p)\n", dev, uio));
274 
275 	return (ppirw(dev, uio));
276 }
277 
278 int
279 ppirw(dev_t dev, struct uio *uio)
280 {
281 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
282 	int s1, s2, len, cnt;
283 	char *cp;
284 	int error = 0, gotdata = 0;
285 	int buflen, address;
286 	char *buf;
287 
288 	if (uio->uio_resid == 0)
289 		return (0);
290 
291 	address = sc->sc_address;
292 
293 	DPRINTF(PDB_FOLLOW|PDB_IO,
294 	    ("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
295 	    dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
296 	    sc->sc_burst, sc->sc_timo, uio->uio_resid));
297 
298 	buflen = min(sc->sc_burst, uio->uio_resid);
299 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
300 	sc->sc_flags |= PPIF_UIO;
301 	if (sc->sc_timo > 0) {
302 		sc->sc_flags |= PPIF_TIMO;
303 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
304 	}
305 	len = cnt = 0;
306 	while (uio->uio_resid > 0) {
307 		len = min(buflen, uio->uio_resid);
308 		cp = buf;
309 		if (uio->uio_rw == UIO_WRITE) {
310 			error = uiomove(cp, len, uio);
311 			if (error)
312 				break;
313 		}
314 again:
315 		s1 = splsoftclock();
316 		s2 = splbio();
317 		if (sc->sc_flags & PPIF_UIO) {
318 			if (gpibrequest(sc->sc_ic, sc->sc_hdl) == 0)
319 				(void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
320 		}
321 		/*
322 		 * Check if we timed out during sleep or uiomove
323 		 */
324 		splx(s2);
325 		if ((sc->sc_flags & PPIF_UIO) == 0) {
326 			DPRINTF(PDB_IO,
327 			    ("ppirw: uiomove/sleep timo, flags %x\n",
328 			    sc->sc_flags));
329 			if (sc->sc_flags & PPIF_TIMO) {
330 				callout_stop(&sc->sc_timo_ch);
331 				sc->sc_flags &= ~PPIF_TIMO;
332 			}
333 			splx(s1);
334 			break;
335 		}
336 		splx(s1);
337 		/*
338 		 * Perform the operation
339 		 */
340 		if (uio->uio_rw == UIO_WRITE)
341 			cnt = gpibsend(sc->sc_ic, address, sc->sc_sec,
342 			    cp, len);
343 		else
344 			cnt = gpibrecv(sc->sc_ic, address, sc->sc_sec,
345 			    cp, len);
346 		s1 = splbio();
347 		gpibrelease(sc->sc_ic, sc->sc_hdl);
348 		DPRINTF(PDB_IO, ("ppirw: %s(%d, %x, %p, %d) -> %d\n",
349 		    uio->uio_rw == UIO_READ ? "recv" : "send",
350 		    address, sc->sc_sec, cp, len, cnt));
351 		splx(s1);
352 		if (uio->uio_rw == UIO_READ) {
353 			if (cnt) {
354 				error = uiomove(cp, cnt, uio);
355 				if (error)
356 					break;
357 				gotdata++;
358 			}
359 			/*
360 			 * Didn't get anything this time, but did in the past.
361 			 * Consider us done.
362 			 */
363 			else if (gotdata)
364 				break;
365 		}
366 		s1 = splsoftclock();
367 		/*
368 		 * Operation timeout (or non-blocking), quit now.
369 		 */
370 		if ((sc->sc_flags & PPIF_UIO) == 0) {
371 			DPRINTF(PDB_IO, ("ppirw: timeout/done\n"));
372 			splx(s1);
373 			break;
374 		}
375 		/*
376 		 * Implement inter-read delay
377 		 */
378 		if (sc->sc_delay > 0) {
379 			sc->sc_flags |= PPIF_DELAY;
380 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
381 			    ppistart, sc);
382 			error = tsleep(sc, (PCATCH|PZERO) + 1, "gpib", 0);
383 			if (error) {
384 				splx(s1);
385 				break;
386 			}
387 		}
388 		splx(s1);
389 		/*
390 		 * Must not call uiomove again til we've used all data
391 		 * that we already grabbed.
392 		 */
393 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
394 			cp += cnt;
395 			len -= cnt;
396 			cnt = 0;
397 			goto again;
398 		}
399 	}
400 	s1 = splsoftclock();
401 	if (sc->sc_flags & PPIF_TIMO) {
402 		callout_stop(&sc->sc_timo_ch);
403 		sc->sc_flags &= ~PPIF_TIMO;
404 	}
405 	if (sc->sc_flags & PPIF_DELAY) {
406 		callout_stop(&sc->sc_start_ch);
407 		sc->sc_flags &= ~PPIF_DELAY;
408 	}
409 	splx(s1);
410 	/*
411 	 * Adjust for those chars that we uiomove'ed but never wrote
412 	 */
413 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
414 		uio->uio_resid += (len - cnt);
415 		DPRINTF(PDB_IO, ("ppirw: short write, adjust by %d\n",
416 		    len - cnt));
417 	}
418 	free(buf, M_DEVBUF);
419 	DPRINTF(PDB_FOLLOW|PDB_IO, ("ppirw: return %d, resid %d\n",
420 	    error, uio->uio_resid));
421 	return (error);
422 }
423 
424 int
425 ppiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
426 {
427 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
428 	struct ppiparam *pp, *upp;
429 	int error = 0;
430 
431 	switch (cmd) {
432 	case PPIIOCGPARAM:
433 		pp = &sc->sc_param;
434 		upp = (struct ppiparam *)data;
435 		upp->burst = pp->burst;
436 		upp->timo = ppihztoms(pp->timo);
437 		upp->delay = ppihztoms(pp->delay);
438 		break;
439 	case PPIIOCSPARAM:
440 		pp = &sc->sc_param;
441 		upp = (struct ppiparam *)data;
442 		if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
443 		    upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
444 			return (EINVAL);
445 		pp->burst = upp->burst;
446 		pp->timo = ppimstohz(upp->timo);
447 		pp->delay = ppimstohz(upp->delay);
448 		break;
449 	case PPIIOCSSEC:
450 		sc->sc_sec = *(int *)data;
451 		break;
452 	default:
453 		return (EINVAL);
454 	}
455 	return (error);
456 }
457 
458 int
459 ppihztoms(int h)
460 {
461 	extern int hz;
462 	int m = h;
463 
464 	if (m > 0)
465 		m = m * 1000 / hz;
466 	return (m);
467 }
468 
469 int
470 ppimstohz(int m)
471 {
472 	extern int hz;
473 	int h = m;
474 
475 	if (h > 0) {
476 		h = h * hz / 1000;
477 		if (h == 0)
478 			h = 1000 / hz;
479 	}
480 	return (h);
481 }
482