xref: /freebsd/sys/dev/ppbus/ppi.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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 	if (ppi->intr_resource == NULL) {
177 		device_printf(dev, "can't allocate irq\n");
178 		return (ENOMEM);
179 	}
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 /*
189  * Cable
190  * -----
191  *
192  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
193  *
194  * nStrobe   <-> nAck		1  <-> 10
195  * nAutofd   <-> Busy		11 <-> 14
196  * nSelectin <-> Select		17 <-> 13
197  * nInit     <-> nFault		15 <-> 16
198  *
199  */
200 static void
201 ppiintr(void *arg)
202 {
203 #ifdef PERIPH_1284
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 #endif /* PERIPH_1284 */
255 
256 	return;
257 }
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 		/* register our interrupt handler */
280 		BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
281 			       INTR_TYPE_TTY, ppiintr, dev, &ppi->intr_cookie);
282 	}
283 	ppi->ppi_count += 1;
284 
285 	return (0);
286 }
287 
288 static int
289 ppiclose(dev_t dev, int flags, int fmt, struct proc *p)
290 {
291 	u_int unit = minor(dev);
292 	struct ppi_data *ppi = UNITOSOFTC(unit);
293 	device_t ppidev = UNITODEVICE(unit);
294         device_t ppbus = device_get_parent(ppidev);
295 
296 	ppi->ppi_count --;
297 	if (!ppi->ppi_count) {
298 
299 #ifdef PERIPH_1284
300 		switch (ppb_1284_get_state(ppbus)) {
301 		case PPB_PERIPHERAL_IDLE:
302 			ppb_peripheral_terminate(ppbus, 0);
303 			break;
304 		case PPB_REVERSE_IDLE:
305 		case PPB_EPP_IDLE:
306 		case PPB_ECP_FORWARD_IDLE:
307 		default:
308 			ppb_1284_terminate(ppbus);
309 			break;
310 		}
311 #endif /* PERIPH_1284 */
312 
313 		/* unregistration of interrupt forced by release */
314 		ppb_release_bus(ppbus, ppidev);
315 
316 		ppi->ppi_flags &= ~HAVE_PPBUS;
317 	}
318 
319 	return (0);
320 }
321 
322 /*
323  * ppiread()
324  *
325  * IEEE1284 compliant read.
326  *
327  * First, try negociation to BYTE then NIBBLE mode
328  * If no data is available, wait for it otherwise transfer as much as possible
329  */
330 static int
331 ppiread(dev_t dev, struct uio *uio, int ioflag)
332 {
333 #ifdef PERIPH_1284
334 	u_int unit = minor(dev);
335 	struct ppi_data *ppi = UNITOSOFTC(unit);
336 	device_t ppidev = UNITODEVICE(unit);
337         device_t ppbus = device_get_parent(ppidev);
338 	int len, error = 0;
339 
340 	switch (ppb_1284_get_state(ppbus)) {
341 	case PPB_PERIPHERAL_IDLE:
342 		ppb_peripheral_terminate(ppbus, 0);
343 		/* fall throught */
344 
345 	case PPB_FORWARD_IDLE:
346 		/* if can't negociate NIBBLE mode then try BYTE mode,
347 		 * the peripheral may be a computer
348 		 */
349 		if ((ppb_1284_negociate(ppbus,
350 			ppi->ppi_mode = PPB_NIBBLE, 0))) {
351 
352 			/* XXX Wait 2 seconds to let the remote host some
353 			 * time to terminate its interrupt
354 			 */
355 			tsleep(ppi, PPBPRI, "ppiread", 2*hz);
356 
357 			if ((error = ppb_1284_negociate(ppbus,
358 				ppi->ppi_mode = PPB_BYTE, 0)))
359 				return (error);
360 		}
361 		break;
362 
363 	case PPB_REVERSE_IDLE:
364 	case PPB_EPP_IDLE:
365 	case PPB_ECP_FORWARD_IDLE:
366 	default:
367 		break;
368 	}
369 
370 #ifdef DEBUG_1284
371 	printf("N");
372 #endif
373 	/* read data */
374 	len = 0;
375 	while (uio->uio_resid) {
376 		if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
377 			ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
378 			&len))) {
379 			goto error;
380 		}
381 
382 		if (!len)
383 			goto error;		/* no more data */
384 
385 #ifdef DEBUG_1284
386 		printf("d");
387 #endif
388 		if ((error = uiomove(ppi->ppi_buffer, len, uio)))
389 			goto error;
390 	}
391 
392 error:
393 
394 #else /* PERIPH_1284 */
395 	int error = ENODEV;
396 #endif
397 
398 	return (error);
399 }
400 
401 /*
402  * ppiwrite()
403  *
404  * IEEE1284 compliant write
405  *
406  * Actually, this is the peripheral side of a remote IEEE1284 read
407  *
408  * The first part of the negociation (IEEE1284 device detection) is
409  * done at interrupt level, then the remaining is done by the writing
410  * process
411  *
412  * Once negociation done, transfer data
413  */
414 static int
415 ppiwrite(dev_t dev, struct uio *uio, int ioflag)
416 {
417 #ifdef PERIPH_1284
418 	u_int unit = minor(dev);
419 	struct ppi_data *ppi = UNITOSOFTC(unit);
420 	device_t ppidev = UNITODEVICE(unit);
421         device_t ppbus = device_get_parent(ppidev);
422 	int len, error = 0, sent;
423 
424 #if 0
425 	int ret;
426 
427 	#define ADDRESS		MS_PARAM(0, 0, MS_TYP_PTR)
428 	#define LENGTH		MS_PARAM(0, 1, MS_TYP_INT)
429 
430 	struct ppb_microseq msq[] = {
431 		  { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
432 		  MS_RET(0)
433 	};
434 
435 	/* negociate ECP mode */
436 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
437 		printf("ppiwrite: ECP negociation failed\n");
438 	}
439 
440 	while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
441 		uiomove(ppi->ppi_buffer, len, uio);
442 
443 		ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
444 
445 		error = ppb_MS_microseq(ppbus, msq, &ret);
446 	}
447 #endif
448 
449 	/* we have to be peripheral to be able to send data, so
450 	 * wait for the appropriate state
451 	 */
452  	if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
453 		ppb_1284_terminate(ppbus);
454 
455  	while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
456 		/* XXX should check a variable before sleeping */
457 #ifdef DEBUG_1284
458 		printf("s");
459 #endif
460 
461 		ppi_enable_intr(ppidev);
462 
463 		/* sleep until IEEE1284 negociation starts */
464 		error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
465 
466 		switch (error) {
467 		case 0:
468 			/* negociate peripheral side with BYTE mode */
469 			ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
470 			break;
471 		case EWOULDBLOCK:
472 			break;
473 		default:
474 			goto error;
475 		}
476 	}
477 #ifdef DEBUG_1284
478 	printf("N");
479 #endif
480 
481 	/* negociation done, write bytes to master host */
482 	while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
483 		uiomove(ppi->ppi_buffer, len, uio);
484 		if ((error = byte_peripheral_write(ppbus,
485 						ppi->ppi_buffer, len, &sent)))
486 			goto error;
487 #ifdef DEBUG_1284
488 		printf("d");
489 #endif
490 	}
491 
492 error:
493 
494 #else /* PERIPH_1284 */
495 	int error = ENODEV;
496 #endif
497 
498 	return (error);
499 }
500 
501 static int
502 ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
503 {
504 	u_int unit = minor(dev);
505 	device_t ppidev = UNITODEVICE(unit);
506         device_t ppbus = device_get_parent(ppidev);
507 	int error = 0;
508 	u_int8_t *val = (u_int8_t *)data;
509 
510 	switch (cmd) {
511 
512 	case PPIGDATA:			/* get data register */
513 		*val = ppb_rdtr(ppbus);
514 		break;
515 	case PPIGSTATUS:		/* get status bits */
516 		*val = ppb_rstr(ppbus);
517 		break;
518 	case PPIGCTRL:			/* get control bits */
519 		*val = ppb_rctr(ppbus);
520 		break;
521 	case PPIGEPPD:			/* get EPP data bits */
522 		*val = ppb_repp_D(ppbus);
523 		break;
524 	case PPIGECR:			/* get ECP bits */
525 		*val = ppb_recr(ppbus);
526 		break;
527 	case PPIGFIFO:			/* read FIFO */
528 		*val = ppb_rfifo(ppbus);
529 		break;
530 	case PPISDATA:			/* set data register */
531 		ppb_wdtr(ppbus, *val);
532 		break;
533 	case PPISSTATUS:		/* set status bits */
534 		ppb_wstr(ppbus, *val);
535 		break;
536 	case PPISCTRL:			/* set control bits */
537 		ppb_wctr(ppbus, *val);
538 		break;
539 	case PPISEPPD:			/* set EPP data bits */
540 		ppb_wepp_D(ppbus, *val);
541 		break;
542 	case PPISECR:			/* set ECP bits */
543 		ppb_wecr(ppbus, *val);
544 		break;
545 	case PPISFIFO:			/* write FIFO */
546 		ppb_wfifo(ppbus, *val);
547 		break;
548 	case PPIGEPPA:			/* get EPP address bits */
549 		*val = ppb_repp_A(ppbus);
550 		break;
551 	case PPISEPPA:			/* set EPP address bits */
552 		ppb_wepp_A(ppbus, *val);
553 		break;
554 	default:
555 		error = ENOTTY;
556 		break;
557 	}
558 
559 	return (error);
560 }
561 
562 static device_method_t ppi_methods[] = {
563 	/* device interface */
564 	DEVMETHOD(device_identify,	ppi_identify),
565 	DEVMETHOD(device_probe,		ppi_probe),
566 	DEVMETHOD(device_attach,	ppi_attach),
567 
568 	{ 0, 0 }
569 };
570 
571 static driver_t ppi_driver = {
572 	"ppi",
573 	ppi_methods,
574 	sizeof(struct ppi_data),
575 };
576 DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
577