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