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