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