xref: /netbsd/sys/arch/hp300/dev/ppi.c (revision c4a72b64)
1 /*	$NetBSD: ppi.c,v 1.24 2002/10/23 09:11:07 jdolecek 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/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: ppi.c,v 1.24 2002/10/23 09:11:07 jdolecek Exp $");
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/callout.h>
84 #include <sys/conf.h>
85 #include <sys/device.h>
86 #include <sys/errno.h>
87 #include <sys/malloc.h>
88 #include <sys/proc.h>
89 #include <sys/uio.h>
90 
91 #include <hp300/dev/hpibvar.h>
92 
93 #include <hp300/dev/ppiioctl.h>
94 
95 struct	ppi_softc {
96 	struct device sc_dev;
97 	int	sc_flags;
98 	struct	hpibqueue sc_hq;	/* HP-IB job queue entry */
99 	struct	ppiparam sc_param;
100 #define sc_burst sc_param.burst
101 #define sc_timo  sc_param.timo
102 #define sc_delay sc_param.delay
103 	int	sc_sec;
104 	int	sc_slave;		/* HP-IB slave address */
105 	struct	callout sc_timo_ch;
106 	struct	callout sc_start_ch;
107 };
108 
109 /* sc_flags values */
110 #define	PPIF_ALIVE	0x01
111 #define	PPIF_OPEN	0x02
112 #define PPIF_UIO	0x04
113 #define PPIF_TIMO	0x08
114 #define PPIF_DELAY	0x10
115 
116 int	ppimatch __P((struct device *, struct cfdata *, void *));
117 void	ppiattach __P((struct device *, struct device *, void *));
118 
119 CFATTACH_DECL(ppi, sizeof(struct ppi_softc),
120     ppimatch, ppiattach, NULL, NULL);
121 
122 extern struct cfdriver ppi_cd;
123 
124 dev_type_open(ppiopen);
125 dev_type_close(ppiclose);
126 dev_type_read(ppiread);
127 dev_type_write(ppiwrite);
128 dev_type_ioctl(ppiioctl);
129 
130 const struct cdevsw ppi_cdevsw = {
131 	ppiopen, ppiclose, ppiread, ppiwrite, ppiioctl,
132 	nostop, notty, nopoll, nommap, nokqfilter,
133 };
134 
135 void	ppistart __P((void *));
136 void	ppinoop __P((void *));
137 
138 void	ppitimo __P((void *));
139 int	ppirw __P((dev_t, struct uio *));
140 int	ppihztoms __P((int));
141 int	ppimstohz __P((int));
142 
143 #define UNIT(x)		minor(x)
144 
145 #ifdef DEBUG
146 int	ppidebug = 0x80;
147 #define PDB_FOLLOW	0x01
148 #define PDB_IO		0x02
149 #define PDB_NOCHECK	0x80
150 #endif
151 
152 int
153 ppimatch(parent, match, aux)
154 	struct device *parent;
155 	struct cfdata *match;
156 	void *aux;
157 {
158 	struct hpibbus_attach_args *ha = aux;
159 
160 	/*
161 	 * The printer/plotter doesn't return an ID tag.
162 	 * The check below prevents us from matching a CS80
163 	 * device by mistake.
164 	 */
165 	if (ha->ha_id & 0x200)
166 		return (0);
167 
168 	/*
169 	 * To prevent matching all unused slots on the bus, we
170 	 * don't allow wildcarded locators.
171 	 */
172 	if (match->hpibbuscf_slave == HPIBBUSCF_SLAVE_DEFAULT ||
173 	    match->hpibbuscf_punit == HPIBBUSCF_PUNIT_DEFAULT)
174 		return (0);
175 
176 	return (1);
177 }
178 
179 void
180 ppiattach(parent, self, aux)
181 	struct device *parent, *self;
182 	void *aux;
183 {
184 	struct ppi_softc *sc = (struct ppi_softc *)self;
185 	struct hpibbus_attach_args *ha = aux;
186 
187 	printf("\n");
188 
189 	sc->sc_slave = ha->ha_slave;
190 
191 	callout_init(&sc->sc_timo_ch);
192 	callout_init(&sc->sc_start_ch);
193 
194 	/* Initialize the hpib queue entry. */
195 	sc->sc_hq.hq_softc = sc;
196 	sc->sc_hq.hq_slave = sc->sc_slave;
197 	sc->sc_hq.hq_start = ppistart;
198 	sc->sc_hq.hq_go = ppinoop;
199 	sc->sc_hq.hq_intr = ppinoop;
200 
201 	sc->sc_flags = PPIF_ALIVE;
202 }
203 
204 void
205 ppinoop(arg)
206 	void *arg;
207 {
208 	/* Noop! */
209 }
210 
211 int
212 ppiopen(dev, flags, fmt, p)
213 	dev_t dev;
214 	int flags, fmt;
215 	struct proc *p;
216 {
217 	int unit = UNIT(dev);
218 	struct ppi_softc *sc;
219 
220 	if (unit >= ppi_cd.cd_ndevs ||
221 	    (sc = ppi_cd.cd_devs[unit]) == NULL ||
222 	    (sc->sc_flags & PPIF_ALIVE) == 0)
223 		return (ENXIO);
224 
225 #ifdef DEBUG
226 	if (ppidebug & PDB_FOLLOW)
227 		printf("ppiopen(%x, %x): flags %x\n",
228 		       dev, flags, sc->sc_flags);
229 #endif
230 	if (sc->sc_flags & PPIF_OPEN)
231 		return (EBUSY);
232 	sc->sc_flags |= PPIF_OPEN;
233 	sc->sc_burst = PPI_BURST;
234 	sc->sc_timo = ppimstohz(PPI_TIMO);
235 	sc->sc_delay = ppimstohz(PPI_DELAY);
236 	sc->sc_sec = -1;
237 	return(0);
238 }
239 
240 int
241 ppiclose(dev, flags, fmt, p)
242 	dev_t dev;
243 	int flags, fmt;
244 	struct proc *p;
245 {
246 	int unit = UNIT(dev);
247 	struct ppi_softc *sc = ppi_cd.cd_devs[unit];
248 
249 #ifdef DEBUG
250 	if (ppidebug & PDB_FOLLOW)
251 		printf("ppiclose(%x, %x): flags %x\n",
252 		       dev, flags, sc->sc_flags);
253 #endif
254 	sc->sc_flags &= ~PPIF_OPEN;
255 	return(0);
256 }
257 
258 void
259 ppistart(arg)
260 	void *arg;
261 {
262 	struct ppi_softc *sc = arg;
263 
264 #ifdef DEBUG
265 	if (ppidebug & PDB_FOLLOW)
266 		printf("ppistart(%x)\n", sc->sc_dev.dv_unit);
267 #endif
268 	sc->sc_flags &= ~PPIF_DELAY;
269 	wakeup(sc);
270 }
271 
272 void
273 ppitimo(arg)
274 	void *arg;
275 {
276 	struct ppi_softc *sc = arg;
277 
278 #ifdef DEBUG
279 	if (ppidebug & PDB_FOLLOW)
280 		printf("ppitimo(%x)\n", sc->sc_dev.dv_unit);
281 #endif
282 	sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
283 	wakeup(sc);
284 }
285 
286 int
287 ppiread(dev, uio, flags)
288 	dev_t dev;
289 	struct uio *uio;
290 	int flags;
291 {
292 
293 #ifdef DEBUG
294 	if (ppidebug & PDB_FOLLOW)
295 		printf("ppiread(%x, %p)\n", dev, uio);
296 #endif
297 	return (ppirw(dev, uio));
298 }
299 
300 int
301 ppiwrite(dev, uio, flags)
302 	dev_t dev;
303 	struct uio *uio;
304 	int flags;
305 {
306 
307 #ifdef DEBUG
308 	if (ppidebug & PDB_FOLLOW)
309 		printf("ppiwrite(%x, %p)\n", dev, uio);
310 #endif
311 	return (ppirw(dev, uio));
312 }
313 
314 int
315 ppirw(dev, uio)
316 	dev_t dev;
317 	struct uio *uio;
318 {
319 	int unit = UNIT(dev);
320 	struct ppi_softc *sc = ppi_cd.cd_devs[unit];
321 	int s, len, cnt;
322 	char *cp;
323 	int error = 0, gotdata = 0;
324 	int buflen, ctlr, slave;
325 	char *buf;
326 
327 	if (uio->uio_resid == 0)
328 		return(0);
329 
330 	ctlr = sc->sc_dev.dv_parent->dv_unit;
331 	slave = sc->sc_slave;
332 
333 #ifdef DEBUG
334 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
335 		printf("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
336 		       dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
337 		       sc->sc_burst, sc->sc_timo, uio->uio_resid);
338 #endif
339 	buflen = min(sc->sc_burst, uio->uio_resid);
340 	buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
341 	sc->sc_flags |= PPIF_UIO;
342 	if (sc->sc_timo > 0) {
343 		sc->sc_flags |= PPIF_TIMO;
344 		callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
345 	}
346 	len = cnt = 0;
347 	while (uio->uio_resid > 0) {
348 		len = min(buflen, uio->uio_resid);
349 		cp = buf;
350 		if (uio->uio_rw == UIO_WRITE) {
351 			error = uiomove(cp, len, uio);
352 			if (error)
353 				break;
354 		}
355 again:
356 		s = splbio();
357 		if ((sc->sc_flags & PPIF_UIO) &&
358 		    hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq) == 0)
359 			(void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
360 		/*
361 		 * Check if we timed out during sleep or uiomove
362 		 */
363 		(void) spllowersoftclock();
364 		if ((sc->sc_flags & PPIF_UIO) == 0) {
365 #ifdef DEBUG
366 			if (ppidebug & PDB_IO)
367 				printf("ppirw: uiomove/sleep timo, flags %x\n",
368 				       sc->sc_flags);
369 #endif
370 			if (sc->sc_flags & PPIF_TIMO) {
371 				callout_stop(&sc->sc_timo_ch);
372 				sc->sc_flags &= ~PPIF_TIMO;
373 			}
374 			splx(s);
375 			break;
376 		}
377 		splx(s);
378 		/*
379 		 * Perform the operation
380 		 */
381 		if (uio->uio_rw == UIO_WRITE)
382 			cnt = hpibsend(ctlr, slave, sc->sc_sec, cp, len);
383 		else
384 			cnt = hpibrecv(ctlr, slave, sc->sc_sec, cp, len);
385 		s = splbio();
386 		hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
387 #ifdef DEBUG
388 		if (ppidebug & PDB_IO)
389 			printf("ppirw: %s(%d, %d, %x, %p, %d) -> %d\n",
390 			       uio->uio_rw == UIO_READ ? "recv" : "send",
391 			       ctlr, slave, sc->sc_sec, cp, len, cnt);
392 #endif
393 		splx(s);
394 		if (uio->uio_rw == UIO_READ) {
395 			if (cnt) {
396 				error = uiomove(cp, cnt, uio);
397 				if (error)
398 					break;
399 				gotdata++;
400 			}
401 			/*
402 			 * Didn't get anything this time, but did in the past.
403 			 * Consider us done.
404 			 */
405 			else if (gotdata)
406 				break;
407 		}
408 		s = splsoftclock();
409 		/*
410 		 * Operation timeout (or non-blocking), quit now.
411 		 */
412 		if ((sc->sc_flags & PPIF_UIO) == 0) {
413 #ifdef DEBUG
414 			if (ppidebug & PDB_IO)
415 				printf("ppirw: timeout/done\n");
416 #endif
417 			splx(s);
418 			break;
419 		}
420 		/*
421 		 * Implement inter-read delay
422 		 */
423 		if (sc->sc_delay > 0) {
424 			sc->sc_flags |= PPIF_DELAY;
425 			callout_reset(&sc->sc_start_ch, sc->sc_delay,
426 			    ppistart, sc);
427 			error = tsleep(sc, (PCATCH|PZERO) + 1, "hpib", 0);
428 			if (error) {
429 				splx(s);
430 				break;
431 			}
432 		}
433 		splx(s);
434 		/*
435 		 * Must not call uiomove again til we've used all data
436 		 * that we already grabbed.
437 		 */
438 		if (uio->uio_rw == UIO_WRITE && cnt != len) {
439 			cp += cnt;
440 			len -= cnt;
441 			cnt = 0;
442 			goto again;
443 		}
444 	}
445 	s = splsoftclock();
446 	if (sc->sc_flags & PPIF_TIMO) {
447 		callout_stop(&sc->sc_timo_ch);
448 		sc->sc_flags &= ~PPIF_TIMO;
449 	}
450 	if (sc->sc_flags & PPIF_DELAY) {
451 		callout_stop(&sc->sc_start_ch);
452 		sc->sc_flags &= ~PPIF_DELAY;
453 	}
454 	splx(s);
455 	/*
456 	 * Adjust for those chars that we uiomove'ed but never wrote
457 	 */
458 	if (uio->uio_rw == UIO_WRITE && cnt != len) {
459 		uio->uio_resid += (len - cnt);
460 #ifdef DEBUG
461 		if (ppidebug & PDB_IO)
462 			printf("ppirw: short write, adjust by %d\n",
463 			       len-cnt);
464 #endif
465 	}
466 	free(buf, M_DEVBUF);
467 #ifdef DEBUG
468 	if (ppidebug & (PDB_FOLLOW|PDB_IO))
469 		printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
470 #endif
471 	return (error);
472 }
473 
474 int
475 ppiioctl(dev, cmd, data, flag, p)
476 	dev_t dev;
477 	u_long cmd;
478 	caddr_t data;
479 	int flag;
480 	struct proc *p;
481 {
482 	struct ppi_softc *sc = ppi_cd.cd_devs[UNIT(dev)];
483 	struct ppiparam *pp, *upp;
484 	int error = 0;
485 
486 	switch (cmd) {
487 	case PPIIOCGPARAM:
488 		pp = &sc->sc_param;
489 		upp = (struct ppiparam *)data;
490 		upp->burst = pp->burst;
491 		upp->timo = ppihztoms(pp->timo);
492 		upp->delay = ppihztoms(pp->delay);
493 		break;
494 	case PPIIOCSPARAM:
495 		pp = &sc->sc_param;
496 		upp = (struct ppiparam *)data;
497 		if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
498 		    upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
499 			return(EINVAL);
500 		pp->burst = upp->burst;
501 		pp->timo = ppimstohz(upp->timo);
502 		pp->delay = ppimstohz(upp->delay);
503 		break;
504 	case PPIIOCSSEC:
505 		sc->sc_sec = *(int *)data;
506 		break;
507 	default:
508 		return(EINVAL);
509 	}
510 	return (error);
511 }
512 
513 int
514 ppihztoms(h)
515 	int h;
516 {
517 	extern int hz;
518 	int m = h;
519 
520 	if (m > 0)
521 		m = m * 1000 / hz;
522 	return(m);
523 }
524 
525 int
526 ppimstohz(m)
527 	int m;
528 {
529 	extern int hz;
530 	int h = m;
531 
532 	if (h > 0) {
533 		h = h * hz / 1000;
534 		if (h == 0)
535 			h = 1000 / hz;
536 	}
537 	return(h);
538 }
539