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