xref: /freebsd/sys/dev/ppbus/ppi.c (revision aad970f1fee9a2a3e5a0f880be9b87c6193b3bd1)
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 {
62e51b0386SMike Smith 
63e51b0386SMike Smith     int		ppi_unit;
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))
800f210c92SNicolas Souchu #define UNITOSOFTC(unit) \
810f210c92SNicolas Souchu 	((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
820f210c92SNicolas Souchu #define UNITODEVICE(unit) \
830f210c92SNicolas Souchu 	(devclass_get_device(ppi_devclass, (unit)))
84ed381522SMike Smith 
850f210c92SNicolas Souchu static devclass_t ppi_devclass;
86ed381522SMike Smith 
87ed381522SMike Smith static	d_open_t	ppiopen;
88ed381522SMike Smith static	d_close_t	ppiclose;
89ed381522SMike Smith static	d_ioctl_t	ppiioctl;
90bc35c174SNicolas Souchu static	d_write_t	ppiwrite;
91bc35c174SNicolas Souchu static	d_read_t	ppiread;
92ed381522SMike Smith 
934f013cf5SMike Smith #define CDEV_MAJOR 82
944e2f199eSPoul-Henning Kamp static struct cdevsw ppi_cdevsw = {
957ac40f5fSPoul-Henning Kamp 	.d_open =	ppiopen,
967ac40f5fSPoul-Henning Kamp 	.d_close =	ppiclose,
977ac40f5fSPoul-Henning Kamp 	.d_read =	ppiread,
987ac40f5fSPoul-Henning Kamp 	.d_write =	ppiwrite,
997ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ppiioctl,
1007ac40f5fSPoul-Henning Kamp 	.d_name =	"ppi",
1017ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
1024e2f199eSPoul-Henning Kamp };
103ed381522SMike Smith 
104bc35c174SNicolas Souchu #ifdef PERIPH_1284
105bc35c174SNicolas Souchu 
106bc35c174SNicolas Souchu static void
1070f210c92SNicolas Souchu ppi_enable_intr(device_t ppidev)
108bc35c174SNicolas Souchu {
109bc35c174SNicolas Souchu 	char r;
1100f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
111bc35c174SNicolas Souchu 
1120f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1130f210c92SNicolas Souchu 	ppb_wctr(ppbus, r | IRQENABLE);
114bc35c174SNicolas Souchu 
115bc35c174SNicolas Souchu 	return;
116bc35c174SNicolas Souchu }
117bc35c174SNicolas Souchu 
118bc35c174SNicolas Souchu static void
1190f210c92SNicolas Souchu ppi_disable_intr(device_t ppidev)
120bc35c174SNicolas Souchu {
121bc35c174SNicolas Souchu 	char r;
1220f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
123bc35c174SNicolas Souchu 
1240f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1250f210c92SNicolas Souchu 	ppb_wctr(ppbus, r & ~IRQENABLE);
126bc35c174SNicolas Souchu 
127bc35c174SNicolas Souchu 	return;
128bc35c174SNicolas Souchu }
129bc35c174SNicolas Souchu 
130bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
131bc35c174SNicolas Souchu 
1320f063508SPeter Wemm static void
1330f063508SPeter Wemm ppi_identify(driver_t *driver, device_t parent)
1340f063508SPeter Wemm {
1350f063508SPeter Wemm 
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 	bzero(ppi, sizeof(struct ppi_data));
152ed381522SMike Smith 
1530f210c92SNicolas Souchu 	return (0);
154ed381522SMike Smith }
155ed381522SMike Smith 
156ed381522SMike Smith /*
1570f210c92SNicolas Souchu  * ppi_attach()
158ed381522SMike Smith  */
1590f210c92SNicolas Souchu static int
1600f210c92SNicolas Souchu ppi_attach(device_t dev)
1610f210c92SNicolas Souchu {
162c47eb96cSNick Hibma #ifdef PERIPH_1284
1630f210c92SNicolas Souchu 	uintptr_t irq;
1640f210c92SNicolas Souchu 	int zero = 0;
1650f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(dev);
166ed381522SMike Smith 
1670f210c92SNicolas Souchu 	/* retrive the irq */
1680f210c92SNicolas Souchu 	BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
1690f210c92SNicolas Souchu 
1700f210c92SNicolas Souchu 	/* declare our interrupt handler */
1710f210c92SNicolas Souchu 	ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
1720f210c92SNicolas Souchu 						&zero, irq, irq, 1, RF_ACTIVE);
173c47eb96cSNick Hibma #endif /* PERIPH_1284 */
1740f210c92SNicolas Souchu 
1750f210c92SNicolas Souchu 	make_dev(&ppi_cdevsw, device_get_unit(dev),	/* XXX cleanup */
1760f210c92SNicolas Souchu 		 UID_ROOT, GID_WHEEL,
1770f210c92SNicolas Souchu 		 0600, "ppi%d", device_get_unit(dev));
1780f210c92SNicolas Souchu 
1790f210c92SNicolas Souchu 	return (0);
180ed381522SMike Smith }
181ed381522SMike Smith 
182c47eb96cSNick Hibma #ifdef PERIPH_1284
183bc35c174SNicolas Souchu /*
184bc35c174SNicolas Souchu  * Cable
185bc35c174SNicolas Souchu  * -----
186bc35c174SNicolas Souchu  *
187bc35c174SNicolas Souchu  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
188bc35c174SNicolas Souchu  *
189bc35c174SNicolas Souchu  * nStrobe   <-> nAck		1  <-> 10
190bc35c174SNicolas Souchu  * nAutofd   <-> Busy		11 <-> 14
191bc35c174SNicolas Souchu  * nSelectin <-> Select		17 <-> 13
192bc35c174SNicolas Souchu  * nInit     <-> nFault		15 <-> 16
193bc35c174SNicolas Souchu  *
194bc35c174SNicolas Souchu  */
195ed381522SMike Smith static void
1960f210c92SNicolas Souchu ppiintr(void *arg)
197ed381522SMike Smith {
1980f210c92SNicolas Souchu 	device_t ppidev = (device_t)arg;
1990f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2000f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(ppidev);
201bc35c174SNicolas Souchu 
2020f210c92SNicolas Souchu 	ppi_disable_intr(ppidev);
203bc35c174SNicolas Souchu 
2040f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
205bc35c174SNicolas Souchu 
206d64ada50SJens Schweikhardt 	/* accept IEEE1284 negotiation then wakeup a waiting process to
207d64ada50SJens Schweikhardt 	 * continue negotiation at process level */
208bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
209bc35c174SNicolas Souchu 		/* Event 1 */
2100f210c92SNicolas Souchu 		if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
211bc35c174SNicolas Souchu 							(SELECT | nBUSY)) {
212d64ada50SJens Schweikhardt 			/* IEEE1284 negotiation */
213bc35c174SNicolas Souchu #ifdef DEBUG_1284
214bc35c174SNicolas Souchu 			printf("N");
215bc35c174SNicolas Souchu #endif
216bc35c174SNicolas Souchu 
217bc35c174SNicolas Souchu 			/* Event 2 - prepare for reading the ext. value */
2180f210c92SNicolas Souchu 			ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
219bc35c174SNicolas Souchu 
2200f210c92SNicolas Souchu 			ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
221bc35c174SNicolas Souchu 
222bc35c174SNicolas Souchu 		} else {
223bc35c174SNicolas Souchu #ifdef DEBUG_1284
2240f210c92SNicolas Souchu 			printf("0x%x", ppb_rstr(ppbus));
225bc35c174SNicolas Souchu #endif
2260f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
227bc35c174SNicolas Souchu 			break;
228bc35c174SNicolas Souchu 		}
229bc35c174SNicolas Souchu 
230d64ada50SJens Schweikhardt 		/* wake up any process waiting for negotiation from
231bc35c174SNicolas Souchu 		 * remote master host */
232bc35c174SNicolas Souchu 
233bc35c174SNicolas Souchu 		/* XXX should set a variable to warn the process about
234bc35c174SNicolas Souchu 		 * the interrupt */
235bc35c174SNicolas Souchu 
236bc35c174SNicolas Souchu 		wakeup(ppi);
237bc35c174SNicolas Souchu 		break;
238bc35c174SNicolas Souchu 	default:
239bc35c174SNicolas Souchu #ifdef DEBUG_1284
24099904c75SPeter Wemm 		printf("?%d", ppb_1284_get_state(ppbus));
241bc35c174SNicolas Souchu #endif
2420f210c92SNicolas Souchu 		ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
2430f210c92SNicolas Souchu 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
244bc35c174SNicolas Souchu 		break;
245bc35c174SNicolas Souchu 	}
246bc35c174SNicolas Souchu 
2470f210c92SNicolas Souchu 	ppi_enable_intr(ppidev);
248bc35c174SNicolas Souchu 
249ed381522SMike Smith 	return;
250ed381522SMike Smith }
251c47eb96cSNick Hibma #endif /* PERIPH_1284 */
252ed381522SMike Smith 
253ed381522SMike Smith static int
254b40ce416SJulian Elischer ppiopen(dev_t dev, int flags, int fmt, struct thread *td)
255ed381522SMike Smith {
256e51b0386SMike Smith 	u_int unit = minor(dev);
2570f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
2580f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
2590f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2606f34dba9SMike Smith 	int res;
261e51b0386SMike Smith 
2620f210c92SNicolas Souchu 	if (!ppi)
263e51b0386SMike Smith 		return (ENXIO);
264e51b0386SMike Smith 
265bc35c174SNicolas Souchu 	if (!(ppi->ppi_flags & HAVE_PPBUS)) {
2660f210c92SNicolas Souchu 		if ((res = ppb_request_bus(ppbus, ppidev,
267bc35c174SNicolas Souchu 			(flags & O_NONBLOCK) ? PPB_DONTWAIT :
268bc35c174SNicolas Souchu 						(PPB_WAIT | PPB_INTR))))
2696f34dba9SMike Smith 			return (res);
270e51b0386SMike Smith 
2716f34dba9SMike Smith 		ppi->ppi_flags |= HAVE_PPBUS;
2720f210c92SNicolas Souchu 
273c47eb96cSNick Hibma #ifdef PERIPH_1284
274c47eb96cSNick Hibma 		if (ppi->intr_resource) {
2750f210c92SNicolas Souchu 			/* register our interrupt handler */
2760f210c92SNicolas Souchu 			BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
2770f210c92SNicolas Souchu 				       INTR_TYPE_TTY, ppiintr, dev, &ppi->intr_cookie);
278bc35c174SNicolas Souchu 		}
279c47eb96cSNick Hibma #endif /* PERIPH_1284 */
280c47eb96cSNick Hibma 	}
281bc35c174SNicolas Souchu 	ppi->ppi_count += 1;
282bc35c174SNicolas Souchu 
2836f34dba9SMike Smith 	return (0);
284ed381522SMike Smith }
285ed381522SMike Smith 
286ed381522SMike Smith static int
287b40ce416SJulian Elischer ppiclose(dev_t dev, int flags, int fmt, struct thread *td)
288ed381522SMike Smith {
2896f34dba9SMike Smith 	u_int unit = minor(dev);
2900f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
2910f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
2920f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2936f34dba9SMike Smith 
294bc35c174SNicolas Souchu 	ppi->ppi_count --;
295bc35c174SNicolas Souchu 	if (!ppi->ppi_count) {
296bc35c174SNicolas Souchu 
297bc35c174SNicolas Souchu #ifdef PERIPH_1284
2980f210c92SNicolas Souchu 		switch (ppb_1284_get_state(ppbus)) {
299bc35c174SNicolas Souchu 		case PPB_PERIPHERAL_IDLE:
3000f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, 0);
301bc35c174SNicolas Souchu 			break;
302bc35c174SNicolas Souchu 		case PPB_REVERSE_IDLE:
303bc35c174SNicolas Souchu 		case PPB_EPP_IDLE:
304bc35c174SNicolas Souchu 		case PPB_ECP_FORWARD_IDLE:
305bc35c174SNicolas Souchu 		default:
3060f210c92SNicolas Souchu 			ppb_1284_terminate(ppbus);
307bc35c174SNicolas Souchu 			break;
308bc35c174SNicolas Souchu 		}
309bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
310bc35c174SNicolas Souchu 
3110f210c92SNicolas Souchu 		/* unregistration of interrupt forced by release */
3120f210c92SNicolas Souchu 		ppb_release_bus(ppbus, ppidev);
3130f210c92SNicolas Souchu 
3146f34dba9SMike Smith 		ppi->ppi_flags &= ~HAVE_PPBUS;
315bc35c174SNicolas Souchu 	}
316bc35c174SNicolas Souchu 
3176f34dba9SMike Smith 	return (0);
318ed381522SMike Smith }
319ed381522SMike Smith 
320bc35c174SNicolas Souchu /*
321bc35c174SNicolas Souchu  * ppiread()
322bc35c174SNicolas Souchu  *
323bc35c174SNicolas Souchu  * IEEE1284 compliant read.
324bc35c174SNicolas Souchu  *
325d64ada50SJens Schweikhardt  * First, try negotiation to BYTE then NIBBLE mode
326bc35c174SNicolas Souchu  * If no data is available, wait for it otherwise transfer as much as possible
327bc35c174SNicolas Souchu  */
328bc35c174SNicolas Souchu static int
329bc35c174SNicolas Souchu ppiread(dev_t dev, struct uio *uio, int ioflag)
330bc35c174SNicolas Souchu {
331bc35c174SNicolas Souchu #ifdef PERIPH_1284
332bc35c174SNicolas Souchu 	u_int unit = minor(dev);
3330f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
3340f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
3350f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
336bc35c174SNicolas Souchu 	int len, error = 0;
337bc35c174SNicolas Souchu 
3380f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
339bc35c174SNicolas Souchu 	case PPB_PERIPHERAL_IDLE:
3400f210c92SNicolas Souchu 		ppb_peripheral_terminate(ppbus, 0);
34193b0017fSPhilippe Charnier 		/* FALLTHROUGH */
342bc35c174SNicolas Souchu 
343bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
344d64ada50SJens Schweikhardt 		/* if can't negotiate NIBBLE mode then try BYTE mode,
345bc35c174SNicolas Souchu 		 * the peripheral may be a computer
346bc35c174SNicolas Souchu 		 */
3470f210c92SNicolas Souchu 		if ((ppb_1284_negociate(ppbus,
348bc35c174SNicolas Souchu 			ppi->ppi_mode = PPB_NIBBLE, 0))) {
349bc35c174SNicolas Souchu 
350bc35c174SNicolas Souchu 			/* XXX Wait 2 seconds to let the remote host some
351bc35c174SNicolas Souchu 			 * time to terminate its interrupt
352bc35c174SNicolas Souchu 			 */
353bc35c174SNicolas Souchu 			tsleep(ppi, PPBPRI, "ppiread", 2*hz);
354bc35c174SNicolas Souchu 
3550f210c92SNicolas Souchu 			if ((error = ppb_1284_negociate(ppbus,
356bc35c174SNicolas Souchu 				ppi->ppi_mode = PPB_BYTE, 0)))
357bc35c174SNicolas Souchu 				return (error);
358bc35c174SNicolas Souchu 		}
359bc35c174SNicolas Souchu 		break;
360bc35c174SNicolas Souchu 
361bc35c174SNicolas Souchu 	case PPB_REVERSE_IDLE:
362bc35c174SNicolas Souchu 	case PPB_EPP_IDLE:
363bc35c174SNicolas Souchu 	case PPB_ECP_FORWARD_IDLE:
364bc35c174SNicolas Souchu 	default:
365bc35c174SNicolas Souchu 		break;
366bc35c174SNicolas Souchu 	}
367bc35c174SNicolas Souchu 
368bc35c174SNicolas Souchu #ifdef DEBUG_1284
369bc35c174SNicolas Souchu 	printf("N");
370bc35c174SNicolas Souchu #endif
371bc35c174SNicolas Souchu 	/* read data */
372bc35c174SNicolas Souchu 	len = 0;
373bc35c174SNicolas Souchu 	while (uio->uio_resid) {
3740f210c92SNicolas Souchu 		if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
375bc35c174SNicolas Souchu 			ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
376bc35c174SNicolas Souchu 			&len))) {
377bc35c174SNicolas Souchu 			goto error;
378bc35c174SNicolas Souchu 		}
379bc35c174SNicolas Souchu 
380bc35c174SNicolas Souchu 		if (!len)
381bc35c174SNicolas Souchu 			goto error;		/* no more data */
382bc35c174SNicolas Souchu 
383bc35c174SNicolas Souchu #ifdef DEBUG_1284
384bc35c174SNicolas Souchu 		printf("d");
385bc35c174SNicolas Souchu #endif
386bc35c174SNicolas Souchu 		if ((error = uiomove(ppi->ppi_buffer, len, uio)))
387bc35c174SNicolas Souchu 			goto error;
388bc35c174SNicolas Souchu 	}
389bc35c174SNicolas Souchu 
390bc35c174SNicolas Souchu error:
391bc35c174SNicolas Souchu 
392bc35c174SNicolas Souchu #else /* PERIPH_1284 */
393bc35c174SNicolas Souchu 	int error = ENODEV;
394bc35c174SNicolas Souchu #endif
395bc35c174SNicolas Souchu 
396bc35c174SNicolas Souchu 	return (error);
397bc35c174SNicolas Souchu }
398bc35c174SNicolas Souchu 
399bc35c174SNicolas Souchu /*
400bc35c174SNicolas Souchu  * ppiwrite()
401bc35c174SNicolas Souchu  *
402bc35c174SNicolas Souchu  * IEEE1284 compliant write
403bc35c174SNicolas Souchu  *
404bc35c174SNicolas Souchu  * Actually, this is the peripheral side of a remote IEEE1284 read
405bc35c174SNicolas Souchu  *
406d64ada50SJens Schweikhardt  * The first part of the negotiation (IEEE1284 device detection) is
407bc35c174SNicolas Souchu  * done at interrupt level, then the remaining is done by the writing
408bc35c174SNicolas Souchu  * process
409bc35c174SNicolas Souchu  *
410d64ada50SJens Schweikhardt  * Once negotiation done, transfer data
411bc35c174SNicolas Souchu  */
412bc35c174SNicolas Souchu static int
413bc35c174SNicolas Souchu ppiwrite(dev_t dev, struct uio *uio, int ioflag)
414bc35c174SNicolas Souchu {
415bc35c174SNicolas Souchu #ifdef PERIPH_1284
416bc35c174SNicolas Souchu 	u_int unit = minor(dev);
4170f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
4180f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
4190f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
420bc35c174SNicolas Souchu 	int len, error = 0, sent;
421bc35c174SNicolas Souchu 
422bc35c174SNicolas Souchu #if 0
423bc35c174SNicolas Souchu 	int ret;
424bc35c174SNicolas Souchu 
425bc35c174SNicolas Souchu 	#define ADDRESS		MS_PARAM(0, 0, MS_TYP_PTR)
426bc35c174SNicolas Souchu 	#define LENGTH		MS_PARAM(0, 1, MS_TYP_INT)
427bc35c174SNicolas Souchu 
428bc35c174SNicolas Souchu 	struct ppb_microseq msq[] = {
429bc35c174SNicolas Souchu 		  { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
430bc35c174SNicolas Souchu 		  MS_RET(0)
431bc35c174SNicolas Souchu 	};
432bc35c174SNicolas Souchu 
433d64ada50SJens Schweikhardt 	/* negotiate ECP mode */
4340f210c92SNicolas Souchu 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
435d64ada50SJens Schweikhardt 		printf("ppiwrite: ECP negotiation failed\n");
436bc35c174SNicolas Souchu 	}
437bc35c174SNicolas Souchu 
438bc35c174SNicolas Souchu 	while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
439bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
440bc35c174SNicolas Souchu 
441bc35c174SNicolas Souchu 		ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
442bc35c174SNicolas Souchu 
4430f210c92SNicolas Souchu 		error = ppb_MS_microseq(ppbus, msq, &ret);
444bc35c174SNicolas Souchu 	}
445bc35c174SNicolas Souchu #endif
446bc35c174SNicolas Souchu 
447bc35c174SNicolas Souchu 	/* we have to be peripheral to be able to send data, so
448bc35c174SNicolas Souchu 	 * wait for the appropriate state
449bc35c174SNicolas Souchu 	 */
450bdcee5cfSNicolas Souchu  	if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
4510f210c92SNicolas Souchu 		ppb_1284_terminate(ppbus);
452bc35c174SNicolas Souchu 
453bdcee5cfSNicolas Souchu  	while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
454bc35c174SNicolas Souchu 		/* XXX should check a variable before sleeping */
455bc35c174SNicolas Souchu #ifdef DEBUG_1284
456bc35c174SNicolas Souchu 		printf("s");
457bc35c174SNicolas Souchu #endif
458bc35c174SNicolas Souchu 
4590f210c92SNicolas Souchu 		ppi_enable_intr(ppidev);
460bc35c174SNicolas Souchu 
461d64ada50SJens Schweikhardt 		/* sleep until IEEE1284 negotiation starts */
462bc35c174SNicolas Souchu 		error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
463bc35c174SNicolas Souchu 
464bc35c174SNicolas Souchu 		switch (error) {
465bc35c174SNicolas Souchu 		case 0:
466d64ada50SJens Schweikhardt 			/* negotiate peripheral side with BYTE mode */
4670f210c92SNicolas Souchu 			ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
468bc35c174SNicolas Souchu 			break;
469bc35c174SNicolas Souchu 		case EWOULDBLOCK:
470bc35c174SNicolas Souchu 			break;
471bc35c174SNicolas Souchu 		default:
472bc35c174SNicolas Souchu 			goto error;
473bc35c174SNicolas Souchu 		}
474bc35c174SNicolas Souchu 	}
475bc35c174SNicolas Souchu #ifdef DEBUG_1284
476bc35c174SNicolas Souchu 	printf("N");
477bc35c174SNicolas Souchu #endif
478bc35c174SNicolas Souchu 
479d64ada50SJens Schweikhardt 	/* negotiation done, write bytes to master host */
480d254af07SMatthew Dillon 	while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
481bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
4820f210c92SNicolas Souchu 		if ((error = byte_peripheral_write(ppbus,
483bc35c174SNicolas Souchu 						ppi->ppi_buffer, len, &sent)))
484bc35c174SNicolas Souchu 			goto error;
485bc35c174SNicolas Souchu #ifdef DEBUG_1284
486bc35c174SNicolas Souchu 		printf("d");
487bc35c174SNicolas Souchu #endif
488bc35c174SNicolas Souchu 	}
489bc35c174SNicolas Souchu 
490bc35c174SNicolas Souchu error:
491bc35c174SNicolas Souchu 
492bc35c174SNicolas Souchu #else /* PERIPH_1284 */
493bc35c174SNicolas Souchu 	int error = ENODEV;
494bc35c174SNicolas Souchu #endif
495bc35c174SNicolas Souchu 
496bc35c174SNicolas Souchu 	return (error);
497bc35c174SNicolas Souchu }
498bc35c174SNicolas Souchu 
499ed381522SMike Smith static int
500b40ce416SJulian Elischer ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
501ed381522SMike Smith {
5026f34dba9SMike Smith 	u_int unit = minor(dev);
5030f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
5040f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
5056f34dba9SMike Smith 	int error = 0;
5066f34dba9SMike Smith 	u_int8_t *val = (u_int8_t *)data;
5076f34dba9SMike Smith 
5086f34dba9SMike Smith 	switch (cmd) {
5096f34dba9SMike Smith 
5106f34dba9SMike Smith 	case PPIGDATA:			/* get data register */
5110f210c92SNicolas Souchu 		*val = ppb_rdtr(ppbus);
5126f34dba9SMike Smith 		break;
5136f34dba9SMike Smith 	case PPIGSTATUS:		/* get status bits */
5140f210c92SNicolas Souchu 		*val = ppb_rstr(ppbus);
5156f34dba9SMike Smith 		break;
5166f34dba9SMike Smith 	case PPIGCTRL:			/* get control bits */
5170f210c92SNicolas Souchu 		*val = ppb_rctr(ppbus);
5186f34dba9SMike Smith 		break;
51920240fa3SNicolas Souchu 	case PPIGEPPD:			/* get EPP data bits */
5200f210c92SNicolas Souchu 		*val = ppb_repp_D(ppbus);
5216f34dba9SMike Smith 		break;
5226f34dba9SMike Smith 	case PPIGECR:			/* get ECP bits */
5230f210c92SNicolas Souchu 		*val = ppb_recr(ppbus);
5246f34dba9SMike Smith 		break;
5256f34dba9SMike Smith 	case PPIGFIFO:			/* read FIFO */
5260f210c92SNicolas Souchu 		*val = ppb_rfifo(ppbus);
5276f34dba9SMike Smith 		break;
5286f34dba9SMike Smith 	case PPISDATA:			/* set data register */
5290f210c92SNicolas Souchu 		ppb_wdtr(ppbus, *val);
5306f34dba9SMike Smith 		break;
5316f34dba9SMike Smith 	case PPISSTATUS:		/* set status bits */
5320f210c92SNicolas Souchu 		ppb_wstr(ppbus, *val);
5336f34dba9SMike Smith 		break;
5346f34dba9SMike Smith 	case PPISCTRL:			/* set control bits */
5350f210c92SNicolas Souchu 		ppb_wctr(ppbus, *val);
5366f34dba9SMike Smith 		break;
53720240fa3SNicolas Souchu 	case PPISEPPD:			/* set EPP data bits */
5380f210c92SNicolas Souchu 		ppb_wepp_D(ppbus, *val);
5396f34dba9SMike Smith 		break;
5406f34dba9SMike Smith 	case PPISECR:			/* set ECP bits */
5410f210c92SNicolas Souchu 		ppb_wecr(ppbus, *val);
5426f34dba9SMike Smith 		break;
5436f34dba9SMike Smith 	case PPISFIFO:			/* write FIFO */
5440f210c92SNicolas Souchu 		ppb_wfifo(ppbus, *val);
5456f34dba9SMike Smith 		break;
54620240fa3SNicolas Souchu 	case PPIGEPPA:			/* get EPP address bits */
5470f210c92SNicolas Souchu 		*val = ppb_repp_A(ppbus);
54820240fa3SNicolas Souchu 		break;
54920240fa3SNicolas Souchu 	case PPISEPPA:			/* set EPP address bits */
5500f210c92SNicolas Souchu 		ppb_wepp_A(ppbus, *val);
55120240fa3SNicolas Souchu 		break;
5526f34dba9SMike Smith 	default:
5536f34dba9SMike Smith 		error = ENOTTY;
5546f34dba9SMike Smith 		break;
5556f34dba9SMike Smith 	}
5566f34dba9SMike Smith 
5576f34dba9SMike Smith 	return (error);
558ed381522SMike Smith }
559ed381522SMike Smith 
5600f063508SPeter Wemm static device_method_t ppi_methods[] = {
5610f063508SPeter Wemm 	/* device interface */
5620f063508SPeter Wemm 	DEVMETHOD(device_identify,	ppi_identify),
5630f063508SPeter Wemm 	DEVMETHOD(device_probe,		ppi_probe),
5640f063508SPeter Wemm 	DEVMETHOD(device_attach,	ppi_attach),
5650f210c92SNicolas Souchu 
5660f063508SPeter Wemm 	{ 0, 0 }
5670f063508SPeter Wemm };
5680f063508SPeter Wemm 
5690f063508SPeter Wemm static driver_t ppi_driver = {
5700f063508SPeter Wemm 	"ppi",
5710f063508SPeter Wemm 	ppi_methods,
5720f063508SPeter Wemm 	sizeof(struct ppi_data),
5730f063508SPeter Wemm };
5740f063508SPeter Wemm DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
575