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