xref: /netbsd-src/sys/arch/hp300/dev/ppi.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: ppi.c,v 1.42 2008/06/18 12:25:13 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  *
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.42 2008/06/18 12:25:13 tsutsui 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 	struct ppi_softc *sc;
199 
200 	sc = device_lookup_private(&ppi_cd,UNIT(dev));
201 	if (sc == NULL)
202 		return ENXIO;
203 
204 	if ((sc->sc_flags & PPIF_ALIVE) == 0)
205 		return ENXIO;
206 
207 #ifdef DEBUG
208 	if (ppidebug & PDB_FOLLOW)
209 		printf("ppiopen(%x, %x): flags %x\n",
210 		       dev, flags, sc->sc_flags);
211 #endif
212 	if (sc->sc_flags & PPIF_OPEN)
213 		return EBUSY;
214 	sc->sc_flags |= PPIF_OPEN;
215 	sc->sc_burst = PPI_BURST;
216 	sc->sc_timo = ppimstohz(PPI_TIMO);
217 	sc->sc_delay = ppimstohz(PPI_DELAY);
218 	sc->sc_sec = -1;
219 	return 0;
220 }
221 
222 static int
223 ppiclose(dev_t dev, int flags, int fmt, struct lwp *l)
224 {
225 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
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 	struct ppi_softc *sc = device_lookup_private(&ppi_cd, UNIT(dev));
288 	int s, s2, len, cnt;
289 	char *cp;
290 	int error = 0, gotdata = 0;
291 	int buflen, ctlr, slave;
292 	char *buf;
293 
294 	if (uio->uio_resid == 0)
295 		return 0;
296 
297 	ctlr = device_unit(device_parent(sc->sc_dev));
298 	slave = sc->sc_slave;
299 
300 #ifdef DEBUG
301 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
302 		printf("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
303 		       dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
304 		       sc->sc_burst, sc->sc_timo, uio->uio_resid);
305 #endif
306 	buflen = min(sc->sc_burst, uio->uio_resid);
307 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
308 	sc->sc_flags |= PPIF_UIO;
309 	if (sc->sc_timo > 0) {
310 		sc->sc_flags |= PPIF_TIMO;
311 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
312 	}
313 	len = cnt = 0;
314 	while (uio->uio_resid > 0) {
315 		len = min(buflen, uio->uio_resid);
316 		cp = buf;
317 		if (uio->uio_rw == UIO_WRITE) {
318 			error = uiomove(cp, len, uio);
319 			if (error)
320 				break;
321 		}
322 again:
323 		s = splsoftclock();
324 		s2 = splbio();
325 		if ((sc->sc_flags & PPIF_UIO) &&
326 		    hpibreq(device_parent(sc->sc_dev), &sc->sc_hq) == 0)
327 			(void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
328 		/*
329 		 * Check if we timed out during sleep or uiomove
330 		 */
331 		splx(s2);
332 		if ((sc->sc_flags & PPIF_UIO) == 0) {
333 #ifdef DEBUG
334 			if (ppidebug & PDB_IO)
335 				printf("ppirw: uiomove/sleep timo, flags %x\n",
336 				       sc->sc_flags);
337 #endif
338 			if (sc->sc_flags & PPIF_TIMO) {
339 				callout_stop(&sc->sc_timo_ch);
340 				sc->sc_flags &= ~PPIF_TIMO;
341 			}
342 			splx(s);
343 			break;
344 		}
345 		splx(s);
346 		/*
347 		 * Perform the operation
348 		 */
349 		if (uio->uio_rw == UIO_WRITE)
350 			cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len);
351 		else
352 			cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len);
353 		s = splbio();
354 		hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
355 #ifdef DEBUG
356 		if (ppidebug & PDB_IO)
357 			printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n",
358 			       uio->uio_rw == UIO_READ ? "recv" : "send",
359 			       ctlr, slave, sc->sc_sec, cp, len, cnt);
360 #endif
361 		splx(s);
362 		if (uio->uio_rw == UIO_READ) {
363 			if (cnt) {
364 				error = uiomove(cp, cnt, uio);
365 				if (error)
366 					break;
367 				gotdata++;
368 			}
369 			/*
370 			 * Didn't get anything this time, but did in the past.
371 			 * Consider us done.
372 			 */
373 			else if (gotdata)
374 				break;
375 		}
376 		s = splsoftclock();
377 		/*
378 		 * Operation timeout (or non-blocking), quit now.
379 		 */
380 		if ((sc->sc_flags & PPIF_UIO) == 0) {
381 #ifdef DEBUG
382 			if (ppidebug & PDB_IO)
383 				printf("ppirw: timeout/done\n");
384 #endif
385 			splx(s);
386 			break;
387 		}
388 		/*
389 		 * Implement inter-read delay
390 		 */
391 		if (sc->sc_delay > 0) {
392 			sc->sc_flags |= PPIF_DELAY;
393 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
394 			    ppistart, sc);
395 			error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0);
396 			if (error) {
397 				splx(s);
398 				break;
399 			}
400 		}
401 		splx(s);
402 		/*
403 		 * Must not call uiomove again til we've used all data
404 		 * that we already grabbed.
405 		 */
406 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
407 			cp += cnt;
408 			len -= cnt;
409 			cnt = 0;
410 			goto again;
411 		}
412 	}
413 	s = splsoftclock();
414 	if (sc->sc_flags & PPIF_TIMO) {
415 		callout_stop(&sc->sc_timo_ch);
416 		sc->sc_flags &= ~PPIF_TIMO;
417 	}
418 	if (sc->sc_flags & PPIF_DELAY) {
419 		callout_stop(&sc->sc_start_ch);
420 		sc->sc_flags &= ~PPIF_DELAY;
421 	}
422 	splx(s);
423 	/*
424 	 * Adjust for those chars that we uiomove'ed but never wrote
425 	 */
426 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
427 		uio->uio_resid += (len - cnt);
428 #ifdef DEBUG
429 		if (ppidebug & PDB_IO)
430 			printf("ppirw: short write, adjust by %d\n",
431 			       len-cnt);
432 #endif
433 	}
434 	free(buf, M_DEVBUF);
435 #ifdef DEBUG
436 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
437 		printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
438 #endif
439 	return error;
440 }
441 
442 static int
443 ppiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
444 {
445 	struct ppi_softc *sc = device_lookup_private(&ppi_cd,UNIT(dev));
446 	struct ppiparam *pp, *upp;
447 	int error = 0;
448 
449 	switch (cmd) {
450 	case PPIIOCGPARAM:
451 		pp = &sc->sc_param;
452 		upp = (struct ppiparam *)data;
453 		upp->burst = pp->burst;
454 		upp->timo = ppihztoms(pp->timo);
455 		upp->delay = ppihztoms(pp->delay);
456 		break;
457 	case PPIIOCSPARAM:
458 		pp = &sc->sc_param;
459 		upp = (struct ppiparam *)data;
460 		if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
461 		    upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
462 			return EINVAL;
463 		pp->burst = upp->burst;
464 		pp->timo = ppimstohz(upp->timo);
465 		pp->delay = ppimstohz(upp->delay);
466 		break;
467 	case PPIIOCSSEC:
468 		sc->sc_sec = *(int *)data;
469 		break;
470 	default:
471 		return EINVAL;
472 	}
473 	return error;
474 }
475 
476 static int
477 ppihztoms(int h)
478 {
479 	extern int hz;
480 	int m = h;
481 
482 	if (m > 0)
483 		m = m * 1000 / hz;
484 	return m;
485 }
486 
487 static int
488 ppimstohz(int m)
489 {
490 	extern int hz;
491 	int h = m;
492 
493 	if (h > 0) {
494 		h = h * hz / 1000;
495 		if (h == 0)
496 			h = 1000 / hz;
497 	}
498 	return h;
499 }
500