xref: /freebsd/sys/dev/ppbus/ppi.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1997, 1998, 1999 Nicolas Souchu, Michael Smith
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 #include "opt_ppb_1284.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/sx.h>
42 #include <sys/uio.h>
43 #include <sys/fcntl.h>
44 
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <sys/rman.h>
48 
49 #include <dev/ppbus/ppbconf.h>
50 #include <dev/ppbus/ppb_msq.h>
51 
52 #ifdef PERIPH_1284
53 #include <sys/malloc.h>
54 #include <dev/ppbus/ppb_1284.h>
55 #endif
56 
57 #include <dev/ppbus/ppi.h>
58 
59 #include "ppbus_if.h"
60 
61 #include <dev/ppbus/ppbio.h>
62 
63 #define BUFSIZE		512
64 
65 struct ppi_data {
66     device_t	ppi_device;
67     struct cdev *ppi_cdev;
68     struct sx	ppi_lock;
69     int		ppi_flags;
70 #define HAVE_PPBUS	(1<<0)
71 
72     int		ppi_mode;			/* IEEE1284 mode */
73     char	ppi_buffer[BUFSIZE];
74 
75 #ifdef PERIPH_1284
76     struct resource *intr_resource;	/* interrupt resource */
77     void *intr_cookie;			/* interrupt registration cookie */
78 #endif /* PERIPH_1284 */
79 };
80 
81 #define DEVTOSOFTC(dev) \
82 	((struct ppi_data *)device_get_softc(dev))
83 
84 #ifdef PERIPH_1284
85 static void	ppiintr(void *arg);
86 #endif
87 
88 static	d_open_t	ppiopen;
89 static	d_close_t	ppiclose;
90 static	d_ioctl_t	ppiioctl;
91 static	d_write_t	ppiwrite;
92 static	d_read_t	ppiread;
93 
94 static struct cdevsw ppi_cdevsw = {
95 	.d_version =	D_VERSION,
96 	.d_open =	ppiopen,
97 	.d_close =	ppiclose,
98 	.d_read =	ppiread,
99 	.d_write =	ppiwrite,
100 	.d_ioctl =	ppiioctl,
101 	.d_name =	"ppi",
102 };
103 
104 #ifdef PERIPH_1284
105 
106 static void
107 ppi_enable_intr(device_t ppidev)
108 {
109 	char r;
110 	device_t ppbus = device_get_parent(ppidev);
111 
112 	r = ppb_rctr(ppbus);
113 	ppb_wctr(ppbus, r | IRQENABLE);
114 
115 	return;
116 }
117 
118 static void
119 ppi_disable_intr(device_t ppidev)
120 {
121 	char r;
122 	device_t ppbus = device_get_parent(ppidev);
123 
124 	r = ppb_rctr(ppbus);
125 	ppb_wctr(ppbus, r & ~IRQENABLE);
126 
127 	return;
128 }
129 
130 #endif /* PERIPH_1284 */
131 
132 static void
133 ppi_identify(driver_t *driver, device_t parent)
134 {
135 
136 	device_t dev;
137 
138 	dev = device_find_child(parent, "ppi", -1);
139 	if (!dev)
140 		BUS_ADD_CHILD(parent, 0, "ppi", -1);
141 }
142 
143 /*
144  * ppi_probe()
145  */
146 static int
147 ppi_probe(device_t dev)
148 {
149 	/* probe is always ok */
150 	device_set_desc(dev, "Parallel I/O");
151 
152 	return (0);
153 }
154 
155 /*
156  * ppi_attach()
157  */
158 static int
159 ppi_attach(device_t dev)
160 {
161 	struct ppi_data *ppi = DEVTOSOFTC(dev);
162 #ifdef PERIPH_1284
163 	int error, rid = 0;
164 
165 	/* declare our interrupt handler */
166 	ppi->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
167 	    RF_ACTIVE);
168 	if (ppi->intr_resource) {
169 		/* register our interrupt handler */
170 		error = bus_setup_intr(dev, ppi->intr_resource,
171 		    INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppiintr, dev,
172 		    &ppi->intr_cookie);
173 		if (error) {
174 			bus_release_resource(dev, SYS_RES_IRQ, rid,
175 			    ppi->intr_resource);
176 			device_printf(dev,
177 			    "Unable to register interrupt handler\n");
178 			return (error);
179 		}
180 	}
181 #endif /* PERIPH_1284 */
182 
183 	sx_init(&ppi->ppi_lock, "ppi");
184 	ppi->ppi_cdev = make_dev(&ppi_cdevsw, device_get_unit(dev),
185 		 UID_ROOT, GID_WHEEL,
186 		 0600, "ppi%d", device_get_unit(dev));
187 	if (ppi->ppi_cdev == NULL) {
188 		device_printf(dev, "Failed to create character device\n");
189 		return (ENXIO);
190 	}
191 	ppi->ppi_cdev->si_drv1 = ppi;
192 	ppi->ppi_device = dev;
193 
194 	return (0);
195 }
196 
197 static int
198 ppi_detach(device_t dev)
199 {
200 	struct ppi_data *ppi = DEVTOSOFTC(dev);
201 
202 	destroy_dev(ppi->ppi_cdev);
203 #ifdef PERIPH_1284
204 	if (ppi->intr_resource != NULL) {
205 		bus_teardown_intr(dev, ppi->intr_resource, ppi->intr_cookie);
206 		bus_release_resource(dev, SYS_RES_IRQ, 0, ppi->intr_resource);
207 	}
208 #endif
209 	sx_destroy(&ppi->ppi_lock);
210 	return (0);
211 }
212 
213 #ifdef PERIPH_1284
214 /*
215  * Cable
216  * -----
217  *
218  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
219  *
220  * nStrobe   <-> nAck		1  <-> 10
221  * nAutofd   <-> Busy		11 <-> 14
222  * nSelectin <-> Select		17 <-> 13
223  * nInit     <-> nFault		15 <-> 16
224  *
225  */
226 static void
227 ppiintr(void *arg)
228 {
229 	device_t ppidev = (device_t)arg;
230 	device_t ppbus = device_get_parent(ppidev);
231 	struct ppi_data *ppi = DEVTOSOFTC(ppidev);
232 
233 	ppb_assert_locked(ppbus);
234 	ppi_disable_intr(ppidev);
235 
236 	switch (ppb_1284_get_state(ppbus)) {
237 	/* accept IEEE1284 negotiation then wakeup a waiting process to
238 	 * continue negotiation at process level */
239 	case PPB_FORWARD_IDLE:
240 		/* Event 1 */
241 		if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
242 							(SELECT | nBUSY)) {
243 			/* IEEE1284 negotiation */
244 #ifdef DEBUG_1284
245 			printf("N");
246 #endif
247 
248 			/* Event 2 - prepare for reading the ext. value */
249 			ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
250 
251 			ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
252 
253 		} else {
254 #ifdef DEBUG_1284
255 			printf("0x%x", ppb_rstr(ppbus));
256 #endif
257 			ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
258 			break;
259 		}
260 
261 		/* wake up any process waiting for negotiation from
262 		 * remote master host */
263 
264 		/* XXX should set a variable to warn the process about
265 		 * the interrupt */
266 
267 		wakeup(ppi);
268 		break;
269 	default:
270 #ifdef DEBUG_1284
271 		printf("?%d", ppb_1284_get_state(ppbus));
272 #endif
273 		ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
274 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
275 		break;
276 	}
277 
278 	ppi_enable_intr(ppidev);
279 
280 	return;
281 }
282 #endif /* PERIPH_1284 */
283 
284 static int
285 ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td)
286 {
287 	struct ppi_data *ppi = dev->si_drv1;
288 	device_t ppidev = ppi->ppi_device;
289 	device_t ppbus = device_get_parent(ppidev);
290 	int res;
291 
292 	sx_xlock(&ppi->ppi_lock);
293 	if (!(ppi->ppi_flags & HAVE_PPBUS)) {
294 		ppb_lock(ppbus);
295 		res = ppb_request_bus(ppbus, ppidev,
296 		    (flags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT | PPB_INTR);
297 		ppb_unlock(ppbus);
298 		if (res) {
299 			sx_xunlock(&ppi->ppi_lock);
300 			return (res);
301 		}
302 
303 		ppi->ppi_flags |= HAVE_PPBUS;
304 	}
305 	sx_xunlock(&ppi->ppi_lock);
306 
307 	return (0);
308 }
309 
310 static int
311 ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td)
312 {
313 	struct ppi_data *ppi = dev->si_drv1;
314 	device_t ppidev = ppi->ppi_device;
315 	device_t ppbus = device_get_parent(ppidev);
316 
317 	sx_xlock(&ppi->ppi_lock);
318 	ppb_lock(ppbus);
319 #ifdef PERIPH_1284
320 	switch (ppb_1284_get_state(ppbus)) {
321 	case PPB_PERIPHERAL_IDLE:
322 		ppb_peripheral_terminate(ppbus, 0);
323 		break;
324 	case PPB_REVERSE_IDLE:
325 	case PPB_EPP_IDLE:
326 	case PPB_ECP_FORWARD_IDLE:
327 	default:
328 		ppb_1284_terminate(ppbus);
329 		break;
330 	}
331 #endif /* PERIPH_1284 */
332 
333 	/* unregistration of interrupt forced by release */
334 	ppb_release_bus(ppbus, ppidev);
335 	ppb_unlock(ppbus);
336 
337 	ppi->ppi_flags &= ~HAVE_PPBUS;
338 	sx_xunlock(&ppi->ppi_lock);
339 
340 	return (0);
341 }
342 
343 /*
344  * ppiread()
345  *
346  * IEEE1284 compliant read.
347  *
348  * First, try negotiation to BYTE then NIBBLE mode
349  * If no data is available, wait for it otherwise transfer as much as possible
350  */
351 static int
352 ppiread(struct cdev *dev, struct uio *uio, int ioflag)
353 {
354 #ifdef PERIPH_1284
355 	struct ppi_data *ppi = dev->si_drv1;
356 	device_t ppidev = ppi->ppi_device;
357 	device_t ppbus = device_get_parent(ppidev);
358 	int len, error = 0;
359 	char *buffer;
360 
361 	buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
362 
363 	ppb_lock(ppbus);
364 	switch (ppb_1284_get_state(ppbus)) {
365 	case PPB_PERIPHERAL_IDLE:
366 		ppb_peripheral_terminate(ppbus, 0);
367 		/* FALLTHROUGH */
368 
369 	case PPB_FORWARD_IDLE:
370 		/* if can't negotiate NIBBLE mode then try BYTE mode,
371 		 * the peripheral may be a computer
372 		 */
373 		if ((ppb_1284_negociate(ppbus,
374 			ppi->ppi_mode = PPB_NIBBLE, 0))) {
375 			/* XXX Wait 2 seconds to let the remote host some
376 			 * time to terminate its interrupt
377 			 */
378 			ppb_sleep(ppbus, ppi, PPBPRI, "ppiread", 2 * hz);
379 
380 			if ((error = ppb_1284_negociate(ppbus,
381 			    ppi->ppi_mode = PPB_BYTE, 0))) {
382 				ppb_unlock(ppbus);
383 				free(buffer, M_DEVBUF);
384 				return (error);
385 			}
386 		}
387 		break;
388 
389 	case PPB_REVERSE_IDLE:
390 	case PPB_EPP_IDLE:
391 	case PPB_ECP_FORWARD_IDLE:
392 	default:
393 		break;
394 	}
395 
396 #ifdef DEBUG_1284
397 	printf("N");
398 #endif
399 	/* read data */
400 	len = 0;
401 	while (uio->uio_resid) {
402 		error = ppb_1284_read(ppbus, ppi->ppi_mode,
403 		    buffer, min(BUFSIZE, uio->uio_resid), &len);
404 		ppb_unlock(ppbus);
405 		if (error)
406 			goto error;
407 
408 		if (!len)
409 			goto error;		/* no more data */
410 
411 #ifdef DEBUG_1284
412 		printf("d");
413 #endif
414 		if ((error = uiomove(buffer, len, uio)))
415 			goto error;
416 		ppb_lock(ppbus);
417 	}
418 	ppb_unlock(ppbus);
419 
420 error:
421 	free(buffer, M_DEVBUF);
422 #else /* PERIPH_1284 */
423 	int error = ENODEV;
424 #endif
425 
426 	return (error);
427 }
428 
429 /*
430  * ppiwrite()
431  *
432  * IEEE1284 compliant write
433  *
434  * Actually, this is the peripheral side of a remote IEEE1284 read
435  *
436  * The first part of the negotiation (IEEE1284 device detection) is
437  * done at interrupt level, then the remaining is done by the writing
438  * process
439  *
440  * Once negotiation done, transfer data
441  */
442 static int
443 ppiwrite(struct cdev *dev, struct uio *uio, int ioflag)
444 {
445 #ifdef PERIPH_1284
446 	struct ppi_data *ppi = dev->si_drv1;
447 	device_t ppidev = ppi->ppi_device;
448 	device_t ppbus = device_get_parent(ppidev);
449 	int len, error = 0, sent;
450 	char *buffer;
451 
452 #if 0
453 	int ret;
454 
455 	#define ADDRESS		MS_PARAM(0, 0, MS_TYP_PTR)
456 	#define LENGTH		MS_PARAM(0, 1, MS_TYP_INT)
457 
458 	struct ppb_microseq msq[] = {
459 		  { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
460 		  MS_RET(0)
461 	};
462 
463 	buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
464 	ppb_lock(ppbus);
465 
466 	/* negotiate ECP mode */
467 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
468 		printf("ppiwrite: ECP negotiation failed\n");
469 	}
470 
471 	while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
472 		ppb_unlock(ppbus);
473 		uiomove(buffer, len, uio);
474 
475 		ppb_MS_init_msq(msq, 2, ADDRESS, buffer, LENGTH, len);
476 
477 		ppb_lock(ppbus);
478 		error = ppb_MS_microseq(ppbus, msq, &ret);
479 	}
480 #else
481 	buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
482 	ppb_lock(ppbus);
483 #endif
484 
485 	/* we have to be peripheral to be able to send data, so
486 	 * wait for the appropriate state
487 	 */
488  	if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
489 		ppb_1284_terminate(ppbus);
490 
491  	while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
492 		/* XXX should check a variable before sleeping */
493 #ifdef DEBUG_1284
494 		printf("s");
495 #endif
496 
497 		ppi_enable_intr(ppidev);
498 
499 		/* sleep until IEEE1284 negotiation starts */
500 		error = ppb_sleep(ppbus, ppi, PCATCH | PPBPRI, "ppiwrite", 0);
501 
502 		switch (error) {
503 		case 0:
504 			/* negotiate peripheral side with BYTE mode */
505 			ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
506 			break;
507 		case EWOULDBLOCK:
508 			break;
509 		default:
510 			goto error;
511 		}
512 	}
513 #ifdef DEBUG_1284
514 	printf("N");
515 #endif
516 
517 	/* negotiation done, write bytes to master host */
518 	while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
519 		ppb_unlock(ppbus);
520 		uiomove(buffer, len, uio);
521 		ppb_lock(ppbus);
522 		if ((error = byte_peripheral_write(ppbus,
523 						buffer, len, &sent)))
524 			goto error;
525 #ifdef DEBUG_1284
526 		printf("d");
527 #endif
528 	}
529 
530 error:
531 	ppb_unlock(ppbus);
532 	free(buffer, M_DEVBUF);
533 #else /* PERIPH_1284 */
534 	int error = ENODEV;
535 #endif
536 
537 	return (error);
538 }
539 
540 static int
541 ppiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
542 {
543 	struct ppi_data *ppi = dev->si_drv1;
544 	device_t ppidev = ppi->ppi_device;
545 	device_t ppbus = device_get_parent(ppidev);
546 	int error = 0;
547 	u_int8_t *val = (u_int8_t *)data;
548 
549 	ppb_lock(ppbus);
550 	switch (cmd) {
551 	case PPIGDATA:			/* get data register */
552 		*val = ppb_rdtr(ppbus);
553 		break;
554 	case PPIGSTATUS:		/* get status bits */
555 		*val = ppb_rstr(ppbus);
556 		break;
557 	case PPIGCTRL:			/* get control bits */
558 		*val = ppb_rctr(ppbus);
559 		break;
560 	case PPIGEPPD:			/* get EPP data bits */
561 		*val = ppb_repp_D(ppbus);
562 		break;
563 	case PPIGECR:			/* get ECP bits */
564 		*val = ppb_recr(ppbus);
565 		break;
566 	case PPIGFIFO:			/* read FIFO */
567 		*val = ppb_rfifo(ppbus);
568 		break;
569 	case PPISDATA:			/* set data register */
570 		ppb_wdtr(ppbus, *val);
571 		break;
572 	case PPISSTATUS:		/* set status bits */
573 		ppb_wstr(ppbus, *val);
574 		break;
575 	case PPISCTRL:			/* set control bits */
576 		ppb_wctr(ppbus, *val);
577 		break;
578 	case PPISEPPD:			/* set EPP data bits */
579 		ppb_wepp_D(ppbus, *val);
580 		break;
581 	case PPISECR:			/* set ECP bits */
582 		ppb_wecr(ppbus, *val);
583 		break;
584 	case PPISFIFO:			/* write FIFO */
585 		ppb_wfifo(ppbus, *val);
586 		break;
587 	case PPIGEPPA:			/* get EPP address bits */
588 		*val = ppb_repp_A(ppbus);
589 		break;
590 	case PPISEPPA:			/* set EPP address bits */
591 		ppb_wepp_A(ppbus, *val);
592 		break;
593 	default:
594 		error = ENOTTY;
595 		break;
596 	}
597 	ppb_unlock(ppbus);
598 
599 	return (error);
600 }
601 
602 static device_method_t ppi_methods[] = {
603 	/* device interface */
604 	DEVMETHOD(device_identify,	ppi_identify),
605 	DEVMETHOD(device_probe,		ppi_probe),
606 	DEVMETHOD(device_attach,	ppi_attach),
607 	DEVMETHOD(device_detach,	ppi_detach),
608 	{ 0, 0 }
609 };
610 
611 static driver_t ppi_driver = {
612 	"ppi",
613 	ppi_methods,
614 	sizeof(struct ppi_data),
615 };
616 
617 DRIVER_MODULE(ppi, ppbus, ppi_driver, 0, 0);
618 MODULE_DEPEND(ppi, ppbus, 1, 1, 1);
619