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