xref: /freebsd/sys/dev/ppbus/ppi.c (revision ae6b868a2adacd6235c403cd2ca8aa7ec0c14399)
1ed381522SMike Smith /*-
20f210c92SNicolas Souchu  * Copyright (c) 1997, 1998, 1999 Nicolas Souchu, Michael Smith
3ed381522SMike Smith  * All rights reserved.
4ed381522SMike Smith  *
5ed381522SMike Smith  * Redistribution and use in source and binary forms, with or without
6ed381522SMike Smith  * modification, are permitted provided that the following conditions
7ed381522SMike Smith  * are met:
8ed381522SMike Smith  * 1. Redistributions of source code must retain the above copyright
9ed381522SMike Smith  *    notice, this list of conditions and the following disclaimer.
10ed381522SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
11ed381522SMike Smith  *    notice, this list of conditions and the following disclaimer in the
12ed381522SMike Smith  *    documentation and/or other materials provided with the distribution.
13ed381522SMike Smith  *
14ed381522SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ed381522SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ed381522SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ed381522SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ed381522SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ed381522SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ed381522SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ed381522SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ed381522SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ed381522SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ed381522SMike Smith  * SUCH DAMAGE.
25ed381522SMike Smith  *
26ed381522SMike Smith  *
27ed381522SMike Smith  */
28aad970f1SDavid E. O'Brien 
29aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
30aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
310f210c92SNicolas Souchu #include "opt_ppb_1284.h"
320f210c92SNicolas Souchu 
33ed381522SMike Smith #include <sys/param.h>
34ed381522SMike Smith #include <sys/systm.h>
350f210c92SNicolas Souchu #include <sys/module.h>
360f210c92SNicolas Souchu #include <sys/bus.h>
37ed381522SMike Smith #include <sys/conf.h>
38ed381522SMike Smith #include <sys/kernel.h>
39bc35c174SNicolas Souchu #include <sys/uio.h>
406f34dba9SMike Smith #include <sys/fcntl.h>
41ed381522SMike Smith 
420f210c92SNicolas Souchu #include <machine/bus.h>
430f210c92SNicolas Souchu #include <machine/resource.h>
440f210c92SNicolas Souchu #include <sys/rman.h>
45bc35c174SNicolas Souchu 
46ed381522SMike Smith #include <dev/ppbus/ppbconf.h>
47bc35c174SNicolas Souchu #include <dev/ppbus/ppb_msq.h>
48bc35c174SNicolas Souchu 
49bc35c174SNicolas Souchu #ifdef PERIPH_1284
50bc35c174SNicolas Souchu #include <dev/ppbus/ppb_1284.h>
51bc35c174SNicolas Souchu #endif
52bc35c174SNicolas Souchu 
536f34dba9SMike Smith #include <dev/ppbus/ppi.h>
546f34dba9SMike Smith 
550f210c92SNicolas Souchu #include "ppbus_if.h"
560f210c92SNicolas Souchu 
570f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
580f210c92SNicolas Souchu 
59bc35c174SNicolas Souchu #define BUFSIZE		512
60ed381522SMike Smith 
61e51b0386SMike Smith struct ppi_data {
62ae6b868aSJohn Baldwin     device_t	ppi_device;
63ae6b868aSJohn Baldwin     struct cdev *ppi_cdev;
646f34dba9SMike Smith     int		ppi_flags;
656f34dba9SMike Smith #define HAVE_PPBUS	(1<<0)
66bc35c174SNicolas Souchu #define HAD_PPBUS	(1<<1)
67bc35c174SNicolas Souchu 
68bc35c174SNicolas Souchu     int		ppi_count;
69bc35c174SNicolas Souchu     int		ppi_mode;			/* IEEE1284 mode */
70bc35c174SNicolas Souchu     char	ppi_buffer[BUFSIZE];
71e51b0386SMike Smith 
72c47eb96cSNick Hibma #ifdef PERIPH_1284
730f210c92SNicolas Souchu     struct resource *intr_resource;	/* interrupt resource */
740f210c92SNicolas Souchu     void *intr_cookie;			/* interrupt registration cookie */
75c47eb96cSNick Hibma #endif /* PERIPH_1284 */
76e51b0386SMike Smith };
77e51b0386SMike Smith 
780f210c92SNicolas Souchu #define DEVTOSOFTC(dev) \
790f210c92SNicolas Souchu 	((struct ppi_data *)device_get_softc(dev))
80ed381522SMike Smith 
810f210c92SNicolas Souchu static devclass_t ppi_devclass;
82ed381522SMike Smith 
83ed381522SMike Smith static	d_open_t	ppiopen;
84ed381522SMike Smith static	d_close_t	ppiclose;
85ed381522SMike Smith static	d_ioctl_t	ppiioctl;
86bc35c174SNicolas Souchu static	d_write_t	ppiwrite;
87bc35c174SNicolas Souchu static	d_read_t	ppiread;
88ed381522SMike Smith 
894e2f199eSPoul-Henning Kamp static struct cdevsw ppi_cdevsw = {
90dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
91dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
927ac40f5fSPoul-Henning Kamp 	.d_open =	ppiopen,
937ac40f5fSPoul-Henning Kamp 	.d_close =	ppiclose,
947ac40f5fSPoul-Henning Kamp 	.d_read =	ppiread,
957ac40f5fSPoul-Henning Kamp 	.d_write =	ppiwrite,
967ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ppiioctl,
977ac40f5fSPoul-Henning Kamp 	.d_name =	"ppi",
984e2f199eSPoul-Henning Kamp };
99ed381522SMike Smith 
100bc35c174SNicolas Souchu #ifdef PERIPH_1284
101bc35c174SNicolas Souchu 
102bc35c174SNicolas Souchu static void
1030f210c92SNicolas Souchu ppi_enable_intr(device_t ppidev)
104bc35c174SNicolas Souchu {
105bc35c174SNicolas Souchu 	char r;
1060f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
107bc35c174SNicolas Souchu 
1080f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1090f210c92SNicolas Souchu 	ppb_wctr(ppbus, r | IRQENABLE);
110bc35c174SNicolas Souchu 
111bc35c174SNicolas Souchu 	return;
112bc35c174SNicolas Souchu }
113bc35c174SNicolas Souchu 
114bc35c174SNicolas Souchu static void
1150f210c92SNicolas Souchu ppi_disable_intr(device_t ppidev)
116bc35c174SNicolas Souchu {
117bc35c174SNicolas Souchu 	char r;
1180f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
119bc35c174SNicolas Souchu 
1200f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1210f210c92SNicolas Souchu 	ppb_wctr(ppbus, r & ~IRQENABLE);
122bc35c174SNicolas Souchu 
123bc35c174SNicolas Souchu 	return;
124bc35c174SNicolas Souchu }
125bc35c174SNicolas Souchu 
126bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
127bc35c174SNicolas Souchu 
1280f063508SPeter Wemm static void
1290f063508SPeter Wemm ppi_identify(driver_t *driver, device_t parent)
1300f063508SPeter Wemm {
1310f063508SPeter Wemm 
132a5c7e3bbSGuido van Rooij 	device_t dev;
133a5c7e3bbSGuido van Rooij 
13457fb5e60SJohn Baldwin 	dev = device_find_child(parent, "ppi", -1);
135a5c7e3bbSGuido van Rooij 	if (!dev)
136338cad62SBernd Walter 		BUS_ADD_CHILD(parent, 0, "ppi", -1);
1370f063508SPeter Wemm }
1380f063508SPeter Wemm 
139ed381522SMike Smith /*
1400f210c92SNicolas Souchu  * ppi_probe()
141ed381522SMike Smith  */
1420f210c92SNicolas Souchu static int
1430f210c92SNicolas Souchu ppi_probe(device_t dev)
144ed381522SMike Smith {
145ed381522SMike Smith 	struct ppi_data *ppi;
1462447bec8SPoul-Henning Kamp 
1470f210c92SNicolas Souchu 	/* probe is always ok */
1480f210c92SNicolas Souchu 	device_set_desc(dev, "Parallel I/O");
1490f210c92SNicolas Souchu 
1500f210c92SNicolas Souchu 	ppi = DEVTOSOFTC(dev);
151ed381522SMike Smith 
1520f210c92SNicolas Souchu 	return (0);
153ed381522SMike Smith }
154ed381522SMike Smith 
155ed381522SMike Smith /*
1560f210c92SNicolas Souchu  * ppi_attach()
157ed381522SMike Smith  */
1580f210c92SNicolas Souchu static int
1590f210c92SNicolas Souchu ppi_attach(device_t dev)
1600f210c92SNicolas Souchu {
161ae6b868aSJohn Baldwin 	struct ppi_data *ppi = DEVTOSOFTC(dev);
162c47eb96cSNick Hibma #ifdef PERIPH_1284
163ca3d3795SJohn Baldwin 	int rid = 0;
164ed381522SMike Smith 
1650f210c92SNicolas Souchu 	/* declare our interrupt handler */
166ca3d3795SJohn Baldwin 	ppi->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
167ca3d3795SJohn Baldwin 	    RF_ACTIVE);
168c47eb96cSNick Hibma #endif /* PERIPH_1284 */
1690f210c92SNicolas Souchu 
170ae6b868aSJohn Baldwin 	ppi->ppi_cdev = make_dev(&ppi_cdevsw, device_get_unit(dev),
1710f210c92SNicolas Souchu 		 UID_ROOT, GID_WHEEL,
1720f210c92SNicolas Souchu 		 0600, "ppi%d", device_get_unit(dev));
173ae6b868aSJohn Baldwin 	if (ppi->ppi_cdev == NULL) {
174ae6b868aSJohn Baldwin 		device_printf("Failed to create character device\n");
175ae6b868aSJohn Baldwin 		return (ENXIO);
176ae6b868aSJohn Baldwin 	}
177ae6b868aSJohn Baldwin 	ppi->ppi_cdev->si_drv1 = ppi;
178ae6b868aSJohn Baldwin 	ppi->ppi_device = dev;
1790f210c92SNicolas Souchu 
1800f210c92SNicolas Souchu 	return (0);
181ed381522SMike Smith }
182ed381522SMike Smith 
183c47eb96cSNick Hibma #ifdef PERIPH_1284
184bc35c174SNicolas Souchu /*
185bc35c174SNicolas Souchu  * Cable
186bc35c174SNicolas Souchu  * -----
187bc35c174SNicolas Souchu  *
188bc35c174SNicolas Souchu  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
189bc35c174SNicolas Souchu  *
190bc35c174SNicolas Souchu  * nStrobe   <-> nAck		1  <-> 10
191bc35c174SNicolas Souchu  * nAutofd   <-> Busy		11 <-> 14
192bc35c174SNicolas Souchu  * nSelectin <-> Select		17 <-> 13
193bc35c174SNicolas Souchu  * nInit     <-> nFault		15 <-> 16
194bc35c174SNicolas Souchu  *
195bc35c174SNicolas Souchu  */
196ed381522SMike Smith static void
1970f210c92SNicolas Souchu ppiintr(void *arg)
198ed381522SMike Smith {
1990f210c92SNicolas Souchu 	device_t ppidev = (device_t)arg;
2000f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2010f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(ppidev);
202bc35c174SNicolas Souchu 
2030f210c92SNicolas Souchu 	ppi_disable_intr(ppidev);
204bc35c174SNicolas Souchu 
2050f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
206bc35c174SNicolas Souchu 
207d64ada50SJens Schweikhardt 	/* accept IEEE1284 negotiation then wakeup a waiting process to
208d64ada50SJens Schweikhardt 	 * continue negotiation at process level */
209bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
210bc35c174SNicolas Souchu 		/* Event 1 */
2110f210c92SNicolas Souchu 		if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
212bc35c174SNicolas Souchu 							(SELECT | nBUSY)) {
213d64ada50SJens Schweikhardt 			/* IEEE1284 negotiation */
214bc35c174SNicolas Souchu #ifdef DEBUG_1284
215bc35c174SNicolas Souchu 			printf("N");
216bc35c174SNicolas Souchu #endif
217bc35c174SNicolas Souchu 
218bc35c174SNicolas Souchu 			/* Event 2 - prepare for reading the ext. value */
2190f210c92SNicolas Souchu 			ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
220bc35c174SNicolas Souchu 
2210f210c92SNicolas Souchu 			ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
222bc35c174SNicolas Souchu 
223bc35c174SNicolas Souchu 		} else {
224bc35c174SNicolas Souchu #ifdef DEBUG_1284
2250f210c92SNicolas Souchu 			printf("0x%x", ppb_rstr(ppbus));
226bc35c174SNicolas Souchu #endif
2270f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
228bc35c174SNicolas Souchu 			break;
229bc35c174SNicolas Souchu 		}
230bc35c174SNicolas Souchu 
231d64ada50SJens Schweikhardt 		/* wake up any process waiting for negotiation from
232bc35c174SNicolas Souchu 		 * remote master host */
233bc35c174SNicolas Souchu 
234bc35c174SNicolas Souchu 		/* XXX should set a variable to warn the process about
235bc35c174SNicolas Souchu 		 * the interrupt */
236bc35c174SNicolas Souchu 
237bc35c174SNicolas Souchu 		wakeup(ppi);
238bc35c174SNicolas Souchu 		break;
239bc35c174SNicolas Souchu 	default:
240bc35c174SNicolas Souchu #ifdef DEBUG_1284
24199904c75SPeter Wemm 		printf("?%d", ppb_1284_get_state(ppbus));
242bc35c174SNicolas Souchu #endif
2430f210c92SNicolas Souchu 		ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
2440f210c92SNicolas Souchu 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
245bc35c174SNicolas Souchu 		break;
246bc35c174SNicolas Souchu 	}
247bc35c174SNicolas Souchu 
2480f210c92SNicolas Souchu 	ppi_enable_intr(ppidev);
249bc35c174SNicolas Souchu 
250ed381522SMike Smith 	return;
251ed381522SMike Smith }
252c47eb96cSNick Hibma #endif /* PERIPH_1284 */
253ed381522SMike Smith 
254ed381522SMike Smith static int
25589c9c53dSPoul-Henning Kamp ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td)
256ed381522SMike Smith {
257ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
258ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
2590f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2606f34dba9SMike Smith 	int res;
261e51b0386SMike Smith 
262bc35c174SNicolas Souchu 	if (!(ppi->ppi_flags & HAVE_PPBUS)) {
2630f210c92SNicolas Souchu 		if ((res = ppb_request_bus(ppbus, ppidev,
264bc35c174SNicolas Souchu 			(flags & O_NONBLOCK) ? PPB_DONTWAIT :
265bc35c174SNicolas Souchu 						(PPB_WAIT | PPB_INTR))))
2666f34dba9SMike Smith 			return (res);
267e51b0386SMike Smith 
2686f34dba9SMike Smith 		ppi->ppi_flags |= HAVE_PPBUS;
2690f210c92SNicolas Souchu 
270c47eb96cSNick Hibma #ifdef PERIPH_1284
271c47eb96cSNick Hibma 		if (ppi->intr_resource) {
2720f210c92SNicolas Souchu 			/* register our interrupt handler */
27357fb5e60SJohn Baldwin 			bus_setup_intr(ppidev, ppi->intr_resource,
27421ab16bdSWarner Losh 				       INTR_TYPE_TTY, NULL, ppiintr, dev,
27521ab16bdSWarner Losh 				       &ppi->intr_cookie);
276bc35c174SNicolas Souchu 		}
277c47eb96cSNick Hibma #endif /* PERIPH_1284 */
278c47eb96cSNick Hibma 	}
279bc35c174SNicolas Souchu 	ppi->ppi_count += 1;
280bc35c174SNicolas Souchu 
2816f34dba9SMike Smith 	return (0);
282ed381522SMike Smith }
283ed381522SMike Smith 
284ed381522SMike Smith static int
28589c9c53dSPoul-Henning Kamp ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td)
286ed381522SMike Smith {
287ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
288ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
2890f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2906f34dba9SMike Smith 
291bc35c174SNicolas Souchu 	ppi->ppi_count --;
292bc35c174SNicolas Souchu 	if (!ppi->ppi_count) {
293bc35c174SNicolas Souchu 
294bc35c174SNicolas Souchu #ifdef PERIPH_1284
2950f210c92SNicolas Souchu 		switch (ppb_1284_get_state(ppbus)) {
296bc35c174SNicolas Souchu 		case PPB_PERIPHERAL_IDLE:
2970f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, 0);
298bc35c174SNicolas Souchu 			break;
299bc35c174SNicolas Souchu 		case PPB_REVERSE_IDLE:
300bc35c174SNicolas Souchu 		case PPB_EPP_IDLE:
301bc35c174SNicolas Souchu 		case PPB_ECP_FORWARD_IDLE:
302bc35c174SNicolas Souchu 		default:
3030f210c92SNicolas Souchu 			ppb_1284_terminate(ppbus);
304bc35c174SNicolas Souchu 			break;
305bc35c174SNicolas Souchu 		}
306bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
307bc35c174SNicolas Souchu 
3080f210c92SNicolas Souchu 		/* unregistration of interrupt forced by release */
3090f210c92SNicolas Souchu 		ppb_release_bus(ppbus, ppidev);
3100f210c92SNicolas Souchu 
3116f34dba9SMike Smith 		ppi->ppi_flags &= ~HAVE_PPBUS;
312bc35c174SNicolas Souchu 	}
313bc35c174SNicolas Souchu 
3146f34dba9SMike Smith 	return (0);
315ed381522SMike Smith }
316ed381522SMike Smith 
317bc35c174SNicolas Souchu /*
318bc35c174SNicolas Souchu  * ppiread()
319bc35c174SNicolas Souchu  *
320bc35c174SNicolas Souchu  * IEEE1284 compliant read.
321bc35c174SNicolas Souchu  *
322d64ada50SJens Schweikhardt  * First, try negotiation to BYTE then NIBBLE mode
323bc35c174SNicolas Souchu  * If no data is available, wait for it otherwise transfer as much as possible
324bc35c174SNicolas Souchu  */
325bc35c174SNicolas Souchu static int
32689c9c53dSPoul-Henning Kamp ppiread(struct cdev *dev, struct uio *uio, int ioflag)
327bc35c174SNicolas Souchu {
328bc35c174SNicolas Souchu #ifdef PERIPH_1284
329ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
330ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
3310f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
332bc35c174SNicolas Souchu 	int len, error = 0;
333bc35c174SNicolas Souchu 
3340f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
335bc35c174SNicolas Souchu 	case PPB_PERIPHERAL_IDLE:
3360f210c92SNicolas Souchu 		ppb_peripheral_terminate(ppbus, 0);
33793b0017fSPhilippe Charnier 		/* FALLTHROUGH */
338bc35c174SNicolas Souchu 
339bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
340d64ada50SJens Schweikhardt 		/* if can't negotiate NIBBLE mode then try BYTE mode,
341bc35c174SNicolas Souchu 		 * the peripheral may be a computer
342bc35c174SNicolas Souchu 		 */
3430f210c92SNicolas Souchu 		if ((ppb_1284_negociate(ppbus,
344bc35c174SNicolas Souchu 			ppi->ppi_mode = PPB_NIBBLE, 0))) {
345bc35c174SNicolas Souchu 
346bc35c174SNicolas Souchu 			/* XXX Wait 2 seconds to let the remote host some
347bc35c174SNicolas Souchu 			 * time to terminate its interrupt
348bc35c174SNicolas Souchu 			 */
349bc35c174SNicolas Souchu 			tsleep(ppi, PPBPRI, "ppiread", 2*hz);
350bc35c174SNicolas Souchu 
3510f210c92SNicolas Souchu 			if ((error = ppb_1284_negociate(ppbus,
352bc35c174SNicolas Souchu 				ppi->ppi_mode = PPB_BYTE, 0)))
353bc35c174SNicolas Souchu 				return (error);
354bc35c174SNicolas Souchu 		}
355bc35c174SNicolas Souchu 		break;
356bc35c174SNicolas Souchu 
357bc35c174SNicolas Souchu 	case PPB_REVERSE_IDLE:
358bc35c174SNicolas Souchu 	case PPB_EPP_IDLE:
359bc35c174SNicolas Souchu 	case PPB_ECP_FORWARD_IDLE:
360bc35c174SNicolas Souchu 	default:
361bc35c174SNicolas Souchu 		break;
362bc35c174SNicolas Souchu 	}
363bc35c174SNicolas Souchu 
364bc35c174SNicolas Souchu #ifdef DEBUG_1284
365bc35c174SNicolas Souchu 	printf("N");
366bc35c174SNicolas Souchu #endif
367bc35c174SNicolas Souchu 	/* read data */
368bc35c174SNicolas Souchu 	len = 0;
369bc35c174SNicolas Souchu 	while (uio->uio_resid) {
3700f210c92SNicolas Souchu 		if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
371bc35c174SNicolas Souchu 			ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
372bc35c174SNicolas Souchu 			&len))) {
373bc35c174SNicolas Souchu 			goto error;
374bc35c174SNicolas Souchu 		}
375bc35c174SNicolas Souchu 
376bc35c174SNicolas Souchu 		if (!len)
377bc35c174SNicolas Souchu 			goto error;		/* no more data */
378bc35c174SNicolas Souchu 
379bc35c174SNicolas Souchu #ifdef DEBUG_1284
380bc35c174SNicolas Souchu 		printf("d");
381bc35c174SNicolas Souchu #endif
382bc35c174SNicolas Souchu 		if ((error = uiomove(ppi->ppi_buffer, len, uio)))
383bc35c174SNicolas Souchu 			goto error;
384bc35c174SNicolas Souchu 	}
385bc35c174SNicolas Souchu 
386bc35c174SNicolas Souchu error:
387bc35c174SNicolas Souchu 
388bc35c174SNicolas Souchu #else /* PERIPH_1284 */
389bc35c174SNicolas Souchu 	int error = ENODEV;
390bc35c174SNicolas Souchu #endif
391bc35c174SNicolas Souchu 
392bc35c174SNicolas Souchu 	return (error);
393bc35c174SNicolas Souchu }
394bc35c174SNicolas Souchu 
395bc35c174SNicolas Souchu /*
396bc35c174SNicolas Souchu  * ppiwrite()
397bc35c174SNicolas Souchu  *
398bc35c174SNicolas Souchu  * IEEE1284 compliant write
399bc35c174SNicolas Souchu  *
400bc35c174SNicolas Souchu  * Actually, this is the peripheral side of a remote IEEE1284 read
401bc35c174SNicolas Souchu  *
402d64ada50SJens Schweikhardt  * The first part of the negotiation (IEEE1284 device detection) is
403bc35c174SNicolas Souchu  * done at interrupt level, then the remaining is done by the writing
404bc35c174SNicolas Souchu  * process
405bc35c174SNicolas Souchu  *
406d64ada50SJens Schweikhardt  * Once negotiation done, transfer data
407bc35c174SNicolas Souchu  */
408bc35c174SNicolas Souchu static int
40989c9c53dSPoul-Henning Kamp ppiwrite(struct cdev *dev, struct uio *uio, int ioflag)
410bc35c174SNicolas Souchu {
411bc35c174SNicolas Souchu #ifdef PERIPH_1284
412ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
413ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
4140f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
415bc35c174SNicolas Souchu 	int len, error = 0, sent;
416bc35c174SNicolas Souchu 
417bc35c174SNicolas Souchu #if 0
418bc35c174SNicolas Souchu 	int ret;
419bc35c174SNicolas Souchu 
420bc35c174SNicolas Souchu 	#define ADDRESS		MS_PARAM(0, 0, MS_TYP_PTR)
421bc35c174SNicolas Souchu 	#define LENGTH		MS_PARAM(0, 1, MS_TYP_INT)
422bc35c174SNicolas Souchu 
423bc35c174SNicolas Souchu 	struct ppb_microseq msq[] = {
424bc35c174SNicolas Souchu 		  { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
425bc35c174SNicolas Souchu 		  MS_RET(0)
426bc35c174SNicolas Souchu 	};
427bc35c174SNicolas Souchu 
428d64ada50SJens Schweikhardt 	/* negotiate ECP mode */
4290f210c92SNicolas Souchu 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
430d64ada50SJens Schweikhardt 		printf("ppiwrite: ECP negotiation failed\n");
431bc35c174SNicolas Souchu 	}
432bc35c174SNicolas Souchu 
433bc35c174SNicolas Souchu 	while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
434bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
435bc35c174SNicolas Souchu 
436bc35c174SNicolas Souchu 		ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
437bc35c174SNicolas Souchu 
4380f210c92SNicolas Souchu 		error = ppb_MS_microseq(ppbus, msq, &ret);
439bc35c174SNicolas Souchu 	}
440bc35c174SNicolas Souchu #endif
441bc35c174SNicolas Souchu 
442bc35c174SNicolas Souchu 	/* we have to be peripheral to be able to send data, so
443bc35c174SNicolas Souchu 	 * wait for the appropriate state
444bc35c174SNicolas Souchu 	 */
445bdcee5cfSNicolas Souchu  	if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
4460f210c92SNicolas Souchu 		ppb_1284_terminate(ppbus);
447bc35c174SNicolas Souchu 
448bdcee5cfSNicolas Souchu  	while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
449bc35c174SNicolas Souchu 		/* XXX should check a variable before sleeping */
450bc35c174SNicolas Souchu #ifdef DEBUG_1284
451bc35c174SNicolas Souchu 		printf("s");
452bc35c174SNicolas Souchu #endif
453bc35c174SNicolas Souchu 
4540f210c92SNicolas Souchu 		ppi_enable_intr(ppidev);
455bc35c174SNicolas Souchu 
456d64ada50SJens Schweikhardt 		/* sleep until IEEE1284 negotiation starts */
457bc35c174SNicolas Souchu 		error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
458bc35c174SNicolas Souchu 
459bc35c174SNicolas Souchu 		switch (error) {
460bc35c174SNicolas Souchu 		case 0:
461d64ada50SJens Schweikhardt 			/* negotiate peripheral side with BYTE mode */
4620f210c92SNicolas Souchu 			ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
463bc35c174SNicolas Souchu 			break;
464bc35c174SNicolas Souchu 		case EWOULDBLOCK:
465bc35c174SNicolas Souchu 			break;
466bc35c174SNicolas Souchu 		default:
467bc35c174SNicolas Souchu 			goto error;
468bc35c174SNicolas Souchu 		}
469bc35c174SNicolas Souchu 	}
470bc35c174SNicolas Souchu #ifdef DEBUG_1284
471bc35c174SNicolas Souchu 	printf("N");
472bc35c174SNicolas Souchu #endif
473bc35c174SNicolas Souchu 
474d64ada50SJens Schweikhardt 	/* negotiation done, write bytes to master host */
475d254af07SMatthew Dillon 	while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
476bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
4770f210c92SNicolas Souchu 		if ((error = byte_peripheral_write(ppbus,
478bc35c174SNicolas Souchu 						ppi->ppi_buffer, len, &sent)))
479bc35c174SNicolas Souchu 			goto error;
480bc35c174SNicolas Souchu #ifdef DEBUG_1284
481bc35c174SNicolas Souchu 		printf("d");
482bc35c174SNicolas Souchu #endif
483bc35c174SNicolas Souchu 	}
484bc35c174SNicolas Souchu 
485bc35c174SNicolas Souchu error:
486bc35c174SNicolas Souchu 
487bc35c174SNicolas Souchu #else /* PERIPH_1284 */
488bc35c174SNicolas Souchu 	int error = ENODEV;
489bc35c174SNicolas Souchu #endif
490bc35c174SNicolas Souchu 
491bc35c174SNicolas Souchu 	return (error);
492bc35c174SNicolas Souchu }
493bc35c174SNicolas Souchu 
494ed381522SMike Smith static int
49589c9c53dSPoul-Henning Kamp ppiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
496ed381522SMike Smith {
497ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
498ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
4996f34dba9SMike Smith 	int error = 0;
5006f34dba9SMike Smith 	u_int8_t *val = (u_int8_t *)data;
5016f34dba9SMike Smith 
5026f34dba9SMike Smith 	switch (cmd) {
5036f34dba9SMike Smith 
5046f34dba9SMike Smith 	case PPIGDATA:			/* get data register */
5050f210c92SNicolas Souchu 		*val = ppb_rdtr(ppbus);
5066f34dba9SMike Smith 		break;
5076f34dba9SMike Smith 	case PPIGSTATUS:		/* get status bits */
5080f210c92SNicolas Souchu 		*val = ppb_rstr(ppbus);
5096f34dba9SMike Smith 		break;
5106f34dba9SMike Smith 	case PPIGCTRL:			/* get control bits */
5110f210c92SNicolas Souchu 		*val = ppb_rctr(ppbus);
5126f34dba9SMike Smith 		break;
51320240fa3SNicolas Souchu 	case PPIGEPPD:			/* get EPP data bits */
5140f210c92SNicolas Souchu 		*val = ppb_repp_D(ppbus);
5156f34dba9SMike Smith 		break;
5166f34dba9SMike Smith 	case PPIGECR:			/* get ECP bits */
5170f210c92SNicolas Souchu 		*val = ppb_recr(ppbus);
5186f34dba9SMike Smith 		break;
5196f34dba9SMike Smith 	case PPIGFIFO:			/* read FIFO */
5200f210c92SNicolas Souchu 		*val = ppb_rfifo(ppbus);
5216f34dba9SMike Smith 		break;
5226f34dba9SMike Smith 	case PPISDATA:			/* set data register */
5230f210c92SNicolas Souchu 		ppb_wdtr(ppbus, *val);
5246f34dba9SMike Smith 		break;
5256f34dba9SMike Smith 	case PPISSTATUS:		/* set status bits */
5260f210c92SNicolas Souchu 		ppb_wstr(ppbus, *val);
5276f34dba9SMike Smith 		break;
5286f34dba9SMike Smith 	case PPISCTRL:			/* set control bits */
5290f210c92SNicolas Souchu 		ppb_wctr(ppbus, *val);
5306f34dba9SMike Smith 		break;
53120240fa3SNicolas Souchu 	case PPISEPPD:			/* set EPP data bits */
5320f210c92SNicolas Souchu 		ppb_wepp_D(ppbus, *val);
5336f34dba9SMike Smith 		break;
5346f34dba9SMike Smith 	case PPISECR:			/* set ECP bits */
5350f210c92SNicolas Souchu 		ppb_wecr(ppbus, *val);
5366f34dba9SMike Smith 		break;
5376f34dba9SMike Smith 	case PPISFIFO:			/* write FIFO */
5380f210c92SNicolas Souchu 		ppb_wfifo(ppbus, *val);
5396f34dba9SMike Smith 		break;
54020240fa3SNicolas Souchu 	case PPIGEPPA:			/* get EPP address bits */
5410f210c92SNicolas Souchu 		*val = ppb_repp_A(ppbus);
54220240fa3SNicolas Souchu 		break;
54320240fa3SNicolas Souchu 	case PPISEPPA:			/* set EPP address bits */
5440f210c92SNicolas Souchu 		ppb_wepp_A(ppbus, *val);
54520240fa3SNicolas Souchu 		break;
5466f34dba9SMike Smith 	default:
5476f34dba9SMike Smith 		error = ENOTTY;
5486f34dba9SMike Smith 		break;
5496f34dba9SMike Smith 	}
5506f34dba9SMike Smith 
5516f34dba9SMike Smith 	return (error);
552ed381522SMike Smith }
553ed381522SMike Smith 
5540f063508SPeter Wemm static device_method_t ppi_methods[] = {
5550f063508SPeter Wemm 	/* device interface */
5560f063508SPeter Wemm 	DEVMETHOD(device_identify,	ppi_identify),
5570f063508SPeter Wemm 	DEVMETHOD(device_probe,		ppi_probe),
5580f063508SPeter Wemm 	DEVMETHOD(device_attach,	ppi_attach),
5590f210c92SNicolas Souchu 
5600f063508SPeter Wemm 	{ 0, 0 }
5610f063508SPeter Wemm };
5620f063508SPeter Wemm 
5630f063508SPeter Wemm static driver_t ppi_driver = {
5640f063508SPeter Wemm 	"ppi",
5650f063508SPeter Wemm 	ppi_methods,
5660f063508SPeter Wemm 	sizeof(struct ppi_data),
5670f063508SPeter Wemm };
5680f063508SPeter Wemm DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
569f5fd5611SRuslan Ermilov MODULE_DEPEND(ppi, ppbus, 1, 1, 1);
570