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