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