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