xref: /freebsd/sys/dev/ppbus/ppi.c (revision 0f210c922b80eef92f841effdded7996a621a9da)
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  *
26c3aac50fSPeter Wemm  * $FreeBSD$
27ed381522SMike Smith  *
28ed381522SMike Smith  */
29e51b0386SMike Smith #include "ppi.h"
30e51b0386SMike Smith 
31e51b0386SMike Smith #if NPPI > 0
32e51b0386SMike Smith 
330f210c92SNicolas Souchu #include "opt_ppb_1284.h"
340f210c92SNicolas Souchu 
35ed381522SMike Smith #include <sys/param.h>
36ed381522SMike Smith #include <sys/systm.h>
370f210c92SNicolas Souchu #include <sys/module.h>
380f210c92SNicolas Souchu #include <sys/bus.h>
39ed381522SMike Smith #include <sys/conf.h>
40ed381522SMike Smith #include <sys/kernel.h>
41bc35c174SNicolas Souchu #include <sys/uio.h>
426f34dba9SMike Smith #include <sys/fcntl.h>
43ed381522SMike Smith 
44bc35c174SNicolas Souchu #include <machine/clock.h>
450f210c92SNicolas Souchu #include <machine/bus.h>
460f210c92SNicolas Souchu #include <machine/resource.h>
470f210c92SNicolas Souchu #include <sys/rman.h>
48bc35c174SNicolas Souchu 
49ed381522SMike Smith #include <dev/ppbus/ppbconf.h>
50bc35c174SNicolas Souchu #include <dev/ppbus/ppb_msq.h>
51bc35c174SNicolas Souchu 
52bc35c174SNicolas Souchu #ifdef PERIPH_1284
53bc35c174SNicolas Souchu #include <dev/ppbus/ppb_1284.h>
54bc35c174SNicolas Souchu #endif
55bc35c174SNicolas Souchu 
566f34dba9SMike Smith #include <dev/ppbus/ppi.h>
576f34dba9SMike Smith 
580f210c92SNicolas Souchu #include "ppbus_if.h"
590f210c92SNicolas Souchu 
600f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
610f210c92SNicolas Souchu 
62bc35c174SNicolas Souchu #define BUFSIZE		512
63ed381522SMike Smith 
64e51b0386SMike Smith struct ppi_data {
65e51b0386SMike Smith 
66e51b0386SMike Smith     int		ppi_unit;
676f34dba9SMike Smith     int		ppi_flags;
686f34dba9SMike Smith #define HAVE_PPBUS	(1<<0)
69bc35c174SNicolas Souchu #define HAD_PPBUS	(1<<1)
70bc35c174SNicolas Souchu 
71bc35c174SNicolas Souchu     int		ppi_count;
72bc35c174SNicolas Souchu     int		ppi_mode;			/* IEEE1284 mode */
73bc35c174SNicolas Souchu     char	ppi_buffer[BUFSIZE];
74e51b0386SMike Smith 
750f210c92SNicolas Souchu     struct resource *intr_resource;	/* interrupt resource */
760f210c92SNicolas Souchu     void *intr_cookie;			/* interrupt registration cookie */
77e51b0386SMike Smith };
78e51b0386SMike Smith 
790f210c92SNicolas Souchu #define DEVTOSOFTC(dev) \
800f210c92SNicolas Souchu 	((struct ppi_data *)device_get_softc(dev))
810f210c92SNicolas Souchu #define UNITOSOFTC(unit) \
820f210c92SNicolas Souchu 	((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
830f210c92SNicolas Souchu #define UNITODEVICE(unit) \
840f210c92SNicolas Souchu 	(devclass_get_device(ppi_devclass, (unit)))
85ed381522SMike Smith 
860f210c92SNicolas Souchu static int ppi_probe(device_t);
870f210c92SNicolas Souchu static int ppi_attach(device_t);
880f210c92SNicolas Souchu static void ppiintr(void *arg);
89ed381522SMike Smith 
900f210c92SNicolas Souchu static devclass_t ppi_devclass;
91ed381522SMike Smith 
920f210c92SNicolas Souchu static device_method_t ppi_methods[] = {
930f210c92SNicolas Souchu 	/* device interface */
940f210c92SNicolas Souchu 	DEVMETHOD(device_probe,		ppi_probe),
950f210c92SNicolas Souchu 	DEVMETHOD(device_attach,	ppi_attach),
960f210c92SNicolas Souchu 
970f210c92SNicolas Souchu 	{ 0, 0 }
98ed381522SMike Smith };
990f210c92SNicolas Souchu 
1000f210c92SNicolas Souchu static driver_t ppi_driver = {
1010f210c92SNicolas Souchu 	"ppi",
1020f210c92SNicolas Souchu 	ppi_methods,
1030f210c92SNicolas Souchu 	sizeof(struct ppi_data),
1040f210c92SNicolas Souchu };
105ed381522SMike Smith 
106ed381522SMike Smith static	d_open_t	ppiopen;
107ed381522SMike Smith static	d_close_t	ppiclose;
108ed381522SMike Smith static	d_ioctl_t	ppiioctl;
109bc35c174SNicolas Souchu static	d_write_t	ppiwrite;
110bc35c174SNicolas Souchu static	d_read_t	ppiread;
111ed381522SMike Smith 
1124f013cf5SMike Smith #define CDEV_MAJOR 82
1134e2f199eSPoul-Henning Kamp static struct cdevsw ppi_cdevsw = {
1144e2f199eSPoul-Henning Kamp 	/* open */	ppiopen,
1154e2f199eSPoul-Henning Kamp 	/* close */	ppiclose,
1164e2f199eSPoul-Henning Kamp 	/* read */	ppiread,
1174e2f199eSPoul-Henning Kamp 	/* write */	ppiwrite,
1184e2f199eSPoul-Henning Kamp 	/* ioctl */	ppiioctl,
1194e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
1204e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1214e2f199eSPoul-Henning Kamp 	/* strategy */	nostrategy,
1224e2f199eSPoul-Henning Kamp 	/* name */	"ppi",
1234e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
1244e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1254e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1264e2f199eSPoul-Henning Kamp 	/* flags */	0,
1274e2f199eSPoul-Henning Kamp 	/* bmaj */	-1
1284e2f199eSPoul-Henning Kamp };
129ed381522SMike Smith 
130bc35c174SNicolas Souchu #ifdef PERIPH_1284
131bc35c174SNicolas Souchu 
132bc35c174SNicolas Souchu static void
1330f210c92SNicolas Souchu ppi_enable_intr(device_t ppidev)
134bc35c174SNicolas Souchu {
135bc35c174SNicolas Souchu 	char r;
1360f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
137bc35c174SNicolas Souchu 
1380f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1390f210c92SNicolas Souchu 	ppb_wctr(ppbus, r | IRQENABLE);
140bc35c174SNicolas Souchu 
141bc35c174SNicolas Souchu 	return;
142bc35c174SNicolas Souchu }
143bc35c174SNicolas Souchu 
144bc35c174SNicolas Souchu static void
1450f210c92SNicolas Souchu ppi_disable_intr(device_t ppidev)
146bc35c174SNicolas Souchu {
147bc35c174SNicolas Souchu 	char r;
1480f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
149bc35c174SNicolas Souchu 
1500f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1510f210c92SNicolas Souchu 	ppb_wctr(ppbus, r & ~IRQENABLE);
152bc35c174SNicolas Souchu 
153bc35c174SNicolas Souchu 	return;
154bc35c174SNicolas Souchu }
155bc35c174SNicolas Souchu 
156bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
157bc35c174SNicolas Souchu 
158ed381522SMike Smith /*
1590f210c92SNicolas Souchu  * ppi_probe()
160ed381522SMike Smith  */
1610f210c92SNicolas Souchu static int
1620f210c92SNicolas Souchu ppi_probe(device_t dev)
163ed381522SMike Smith {
164ed381522SMike Smith 	struct ppi_data *ppi;
1652447bec8SPoul-Henning Kamp 	static int once;
1662447bec8SPoul-Henning Kamp 
1670f210c92SNicolas Souchu 	/* probe is always ok */
1680f210c92SNicolas Souchu 	device_set_desc(dev, "Parallel I/O");
1690f210c92SNicolas Souchu 
1702447bec8SPoul-Henning Kamp 	if (!once++)
1712447bec8SPoul-Henning Kamp 		cdevsw_add(&ppi_cdevsw);
172ed381522SMike Smith 
1730f210c92SNicolas Souchu 	ppi = DEVTOSOFTC(dev);
174ed381522SMike Smith 	bzero(ppi, sizeof(struct ppi_data));
175ed381522SMike Smith 
1760f210c92SNicolas Souchu 	return (0);
177ed381522SMike Smith }
178ed381522SMike Smith 
179ed381522SMike Smith /*
1800f210c92SNicolas Souchu  * ppi_attach()
181ed381522SMike Smith  */
1820f210c92SNicolas Souchu static int
1830f210c92SNicolas Souchu ppi_attach(device_t dev)
1840f210c92SNicolas Souchu {
1850f210c92SNicolas Souchu 	uintptr_t irq;
1860f210c92SNicolas Souchu 	int zero = 0;
1870f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(dev);
188ed381522SMike Smith 
1890f210c92SNicolas Souchu 	/* retrive the irq */
1900f210c92SNicolas Souchu 	BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
1910f210c92SNicolas Souchu 
1920f210c92SNicolas Souchu 	/* declare our interrupt handler */
1930f210c92SNicolas Souchu 	ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
1940f210c92SNicolas Souchu 						&zero, irq, irq, 1, RF_ACTIVE);
1950f210c92SNicolas Souchu 
1960f210c92SNicolas Souchu 	make_dev(&ppi_cdevsw, device_get_unit(dev),	/* XXX cleanup */
1970f210c92SNicolas Souchu 		 UID_ROOT, GID_WHEEL,
1980f210c92SNicolas Souchu 		 0600, "ppi%d", device_get_unit(dev));
1990f210c92SNicolas Souchu 
2000f210c92SNicolas Souchu 	return (0);
201ed381522SMike Smith }
202ed381522SMike Smith 
203bc35c174SNicolas Souchu /*
204bc35c174SNicolas Souchu  * Cable
205bc35c174SNicolas Souchu  * -----
206bc35c174SNicolas Souchu  *
207bc35c174SNicolas Souchu  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
208bc35c174SNicolas Souchu  *
209bc35c174SNicolas Souchu  * nStrobe   <-> nAck		1  <-> 10
210bc35c174SNicolas Souchu  * nAutofd   <-> Busy		11 <-> 14
211bc35c174SNicolas Souchu  * nSelectin <-> Select		17 <-> 13
212bc35c174SNicolas Souchu  * nInit     <-> nFault		15 <-> 16
213bc35c174SNicolas Souchu  *
214bc35c174SNicolas Souchu  */
215ed381522SMike Smith static void
2160f210c92SNicolas Souchu ppiintr(void *arg)
217ed381522SMike Smith {
218bc35c174SNicolas Souchu #ifdef PERIPH_1284
2190f210c92SNicolas Souchu 	device_t ppidev = (device_t)arg;
2200f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2210f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(ppidev);
222bc35c174SNicolas Souchu 
2230f210c92SNicolas Souchu 	ppi_disable_intr(ppidev);
224bc35c174SNicolas Souchu 
2250f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
226bc35c174SNicolas Souchu 
227bc35c174SNicolas Souchu 	/* accept IEEE1284 negociation then wakeup an waiting process to
228bc35c174SNicolas Souchu 	 * continue negociation at process level */
229bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
230bc35c174SNicolas Souchu 		/* Event 1 */
2310f210c92SNicolas Souchu 		if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
232bc35c174SNicolas Souchu 							(SELECT | nBUSY)) {
233bc35c174SNicolas Souchu 			/* IEEE1284 negociation */
234bc35c174SNicolas Souchu #ifdef DEBUG_1284
235bc35c174SNicolas Souchu 			printf("N");
236bc35c174SNicolas Souchu #endif
237bc35c174SNicolas Souchu 
238bc35c174SNicolas Souchu 			/* Event 2 - prepare for reading the ext. value */
2390f210c92SNicolas Souchu 			ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
240bc35c174SNicolas Souchu 
2410f210c92SNicolas Souchu 			ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
242bc35c174SNicolas Souchu 
243bc35c174SNicolas Souchu 		} else {
244bc35c174SNicolas Souchu #ifdef DEBUG_1284
2450f210c92SNicolas Souchu 			printf("0x%x", ppb_rstr(ppbus));
246bc35c174SNicolas Souchu #endif
2470f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
248bc35c174SNicolas Souchu 			break;
249bc35c174SNicolas Souchu 		}
250bc35c174SNicolas Souchu 
251bc35c174SNicolas Souchu 		/* wake up any process waiting for negociation from
252bc35c174SNicolas Souchu 		 * remote master host */
253bc35c174SNicolas Souchu 
254bc35c174SNicolas Souchu 		/* XXX should set a variable to warn the process about
255bc35c174SNicolas Souchu 		 * the interrupt */
256bc35c174SNicolas Souchu 
257bc35c174SNicolas Souchu 		wakeup(ppi);
258bc35c174SNicolas Souchu 		break;
259bc35c174SNicolas Souchu 	default:
260bc35c174SNicolas Souchu #ifdef DEBUG_1284
2610f210c92SNicolas Souchu 		printf("?%d", ppb_1284_get_state(ppbus);
262bc35c174SNicolas Souchu #endif
2630f210c92SNicolas Souchu 		ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
2640f210c92SNicolas Souchu 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
265bc35c174SNicolas Souchu 		break;
266bc35c174SNicolas Souchu 	}
267bc35c174SNicolas Souchu 
2680f210c92SNicolas Souchu 	ppi_enable_intr(ppidev);
269bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
270bc35c174SNicolas Souchu 
271ed381522SMike Smith 	return;
272ed381522SMike Smith }
273ed381522SMike Smith 
274ed381522SMike Smith static int
275ed381522SMike Smith ppiopen(dev_t dev, int flags, int fmt, struct proc *p)
276ed381522SMike Smith {
277e51b0386SMike Smith 	u_int unit = minor(dev);
2780f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
2790f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
2800f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2816f34dba9SMike Smith 	int res;
282e51b0386SMike Smith 
2830f210c92SNicolas Souchu 	if (!ppi)
284e51b0386SMike Smith 		return (ENXIO);
285e51b0386SMike Smith 
286bc35c174SNicolas Souchu 	if (!(ppi->ppi_flags & HAVE_PPBUS)) {
2870f210c92SNicolas Souchu 		if ((res = ppb_request_bus(ppbus, ppidev,
288bc35c174SNicolas Souchu 			(flags & O_NONBLOCK) ? PPB_DONTWAIT :
289bc35c174SNicolas Souchu 						(PPB_WAIT | PPB_INTR))))
2906f34dba9SMike Smith 			return (res);
291e51b0386SMike Smith 
2926f34dba9SMike Smith 		ppi->ppi_flags |= HAVE_PPBUS;
2930f210c92SNicolas Souchu 
2940f210c92SNicolas Souchu 		/* register our interrupt handler */
2950f210c92SNicolas Souchu 		BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
2960f210c92SNicolas Souchu 			       INTR_TYPE_TTY, ppiintr, dev, &ppi->intr_cookie);
297bc35c174SNicolas Souchu 	}
298bc35c174SNicolas Souchu 	ppi->ppi_count += 1;
299bc35c174SNicolas Souchu 
3006f34dba9SMike Smith 	return (0);
301ed381522SMike Smith }
302ed381522SMike Smith 
303ed381522SMike Smith static int
304ed381522SMike Smith ppiclose(dev_t dev, int flags, int fmt, struct proc *p)
305ed381522SMike Smith {
3066f34dba9SMike Smith 	u_int unit = minor(dev);
3070f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
3080f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
3090f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
3106f34dba9SMike Smith 
311bc35c174SNicolas Souchu 	ppi->ppi_count --;
312bc35c174SNicolas Souchu 	if (!ppi->ppi_count) {
313bc35c174SNicolas Souchu 
314bc35c174SNicolas Souchu #ifdef PERIPH_1284
3150f210c92SNicolas Souchu 		switch (ppb_1284_get_state(ppbus)) {
316bc35c174SNicolas Souchu 		case PPB_PERIPHERAL_IDLE:
3170f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, 0);
318bc35c174SNicolas Souchu 			break;
319bc35c174SNicolas Souchu 		case PPB_REVERSE_IDLE:
320bc35c174SNicolas Souchu 		case PPB_EPP_IDLE:
321bc35c174SNicolas Souchu 		case PPB_ECP_FORWARD_IDLE:
322bc35c174SNicolas Souchu 		default:
3230f210c92SNicolas Souchu 			ppb_1284_terminate(ppbus);
324bc35c174SNicolas Souchu 			break;
325bc35c174SNicolas Souchu 		}
326bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
327bc35c174SNicolas Souchu 
3280f210c92SNicolas Souchu 		/* unregistration of interrupt forced by release */
3290f210c92SNicolas Souchu 		ppb_release_bus(ppbus, ppidev);
3300f210c92SNicolas Souchu 
3316f34dba9SMike Smith 		ppi->ppi_flags &= ~HAVE_PPBUS;
332bc35c174SNicolas Souchu 	}
333bc35c174SNicolas Souchu 
3346f34dba9SMike Smith 	return (0);
335ed381522SMike Smith }
336ed381522SMike Smith 
337bc35c174SNicolas Souchu /*
338bc35c174SNicolas Souchu  * ppiread()
339bc35c174SNicolas Souchu  *
340bc35c174SNicolas Souchu  * IEEE1284 compliant read.
341bc35c174SNicolas Souchu  *
342bc35c174SNicolas Souchu  * First, try negociation to BYTE then NIBBLE mode
343bc35c174SNicolas Souchu  * If no data is available, wait for it otherwise transfer as much as possible
344bc35c174SNicolas Souchu  */
345bc35c174SNicolas Souchu static int
346bc35c174SNicolas Souchu ppiread(dev_t dev, struct uio *uio, int ioflag)
347bc35c174SNicolas Souchu {
348bc35c174SNicolas Souchu #ifdef PERIPH_1284
349bc35c174SNicolas Souchu 	u_int unit = minor(dev);
3500f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
3510f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
3520f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
353bc35c174SNicolas Souchu 	int len, error = 0;
354bc35c174SNicolas Souchu 
3550f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
356bc35c174SNicolas Souchu 	case PPB_PERIPHERAL_IDLE:
3570f210c92SNicolas Souchu 		ppb_peripheral_terminate(ppbus, 0);
358bc35c174SNicolas Souchu 		/* fall throught */
359bc35c174SNicolas Souchu 
360bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
361bc35c174SNicolas Souchu 		/* if can't negociate NIBBLE mode then try BYTE mode,
362bc35c174SNicolas Souchu 		 * the peripheral may be a computer
363bc35c174SNicolas Souchu 		 */
3640f210c92SNicolas Souchu 		if ((ppb_1284_negociate(ppbus,
365bc35c174SNicolas Souchu 			ppi->ppi_mode = PPB_NIBBLE, 0))) {
366bc35c174SNicolas Souchu 
367bc35c174SNicolas Souchu 			/* XXX Wait 2 seconds to let the remote host some
368bc35c174SNicolas Souchu 			 * time to terminate its interrupt
369bc35c174SNicolas Souchu 			 */
370bc35c174SNicolas Souchu 			tsleep(ppi, PPBPRI, "ppiread", 2*hz);
371bc35c174SNicolas Souchu 
3720f210c92SNicolas Souchu 			if ((error = ppb_1284_negociate(ppbus,
373bc35c174SNicolas Souchu 				ppi->ppi_mode = PPB_BYTE, 0)))
374bc35c174SNicolas Souchu 				return (error);
375bc35c174SNicolas Souchu 		}
376bc35c174SNicolas Souchu 		break;
377bc35c174SNicolas Souchu 
378bc35c174SNicolas Souchu 	case PPB_REVERSE_IDLE:
379bc35c174SNicolas Souchu 	case PPB_EPP_IDLE:
380bc35c174SNicolas Souchu 	case PPB_ECP_FORWARD_IDLE:
381bc35c174SNicolas Souchu 	default:
382bc35c174SNicolas Souchu 		break;
383bc35c174SNicolas Souchu 	}
384bc35c174SNicolas Souchu 
385bc35c174SNicolas Souchu #ifdef DEBUG_1284
386bc35c174SNicolas Souchu 	printf("N");
387bc35c174SNicolas Souchu #endif
388bc35c174SNicolas Souchu 	/* read data */
389bc35c174SNicolas Souchu 	len = 0;
390bc35c174SNicolas Souchu 	while (uio->uio_resid) {
3910f210c92SNicolas Souchu 		if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
392bc35c174SNicolas Souchu 			ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
393bc35c174SNicolas Souchu 			&len))) {
394bc35c174SNicolas Souchu 			goto error;
395bc35c174SNicolas Souchu 		}
396bc35c174SNicolas Souchu 
397bc35c174SNicolas Souchu 		if (!len)
398bc35c174SNicolas Souchu 			goto error;		/* no more data */
399bc35c174SNicolas Souchu 
400bc35c174SNicolas Souchu #ifdef DEBUG_1284
401bc35c174SNicolas Souchu 		printf("d");
402bc35c174SNicolas Souchu #endif
403bc35c174SNicolas Souchu 		if ((error = uiomove(ppi->ppi_buffer, len, uio)))
404bc35c174SNicolas Souchu 			goto error;
405bc35c174SNicolas Souchu 	}
406bc35c174SNicolas Souchu 
407bc35c174SNicolas Souchu error:
408bc35c174SNicolas Souchu 
409bc35c174SNicolas Souchu #else /* PERIPH_1284 */
410bc35c174SNicolas Souchu 	int error = ENODEV;
411bc35c174SNicolas Souchu #endif
412bc35c174SNicolas Souchu 
413bc35c174SNicolas Souchu 	return (error);
414bc35c174SNicolas Souchu }
415bc35c174SNicolas Souchu 
416bc35c174SNicolas Souchu /*
417bc35c174SNicolas Souchu  * ppiwrite()
418bc35c174SNicolas Souchu  *
419bc35c174SNicolas Souchu  * IEEE1284 compliant write
420bc35c174SNicolas Souchu  *
421bc35c174SNicolas Souchu  * Actually, this is the peripheral side of a remote IEEE1284 read
422bc35c174SNicolas Souchu  *
423bc35c174SNicolas Souchu  * The first part of the negociation (IEEE1284 device detection) is
424bc35c174SNicolas Souchu  * done at interrupt level, then the remaining is done by the writing
425bc35c174SNicolas Souchu  * process
426bc35c174SNicolas Souchu  *
427bc35c174SNicolas Souchu  * Once negociation done, transfer data
428bc35c174SNicolas Souchu  */
429bc35c174SNicolas Souchu static int
430bc35c174SNicolas Souchu ppiwrite(dev_t dev, struct uio *uio, int ioflag)
431bc35c174SNicolas Souchu {
432bc35c174SNicolas Souchu #ifdef PERIPH_1284
433bc35c174SNicolas Souchu 	u_int unit = minor(dev);
4340f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
4350f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
4360f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
437bc35c174SNicolas Souchu 	int len, error = 0, sent;
438bc35c174SNicolas Souchu 
439bc35c174SNicolas Souchu #if 0
440bc35c174SNicolas Souchu 	int ret;
441bc35c174SNicolas Souchu 
442bc35c174SNicolas Souchu 	#define ADDRESS		MS_PARAM(0, 0, MS_TYP_PTR)
443bc35c174SNicolas Souchu 	#define LENGTH		MS_PARAM(0, 1, MS_TYP_INT)
444bc35c174SNicolas Souchu 
445bc35c174SNicolas Souchu 	struct ppb_microseq msq[] = {
446bc35c174SNicolas Souchu 		  { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
447bc35c174SNicolas Souchu 		  MS_RET(0)
448bc35c174SNicolas Souchu 	};
449bc35c174SNicolas Souchu 
450bc35c174SNicolas Souchu 	/* negociate ECP mode */
4510f210c92SNicolas Souchu 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
452bc35c174SNicolas Souchu 		printf("ppiwrite: ECP negociation failed\n");
453bc35c174SNicolas Souchu 	}
454bc35c174SNicolas Souchu 
455bc35c174SNicolas Souchu 	while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
456bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
457bc35c174SNicolas Souchu 
458bc35c174SNicolas Souchu 		ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
459bc35c174SNicolas Souchu 
4600f210c92SNicolas Souchu 		error = ppb_MS_microseq(ppbus, msq, &ret);
461bc35c174SNicolas Souchu 	}
462bc35c174SNicolas Souchu #endif
463bc35c174SNicolas Souchu 
464bc35c174SNicolas Souchu 	/* we have to be peripheral to be able to send data, so
465bc35c174SNicolas Souchu 	 * wait for the appropriate state
466bc35c174SNicolas Souchu 	 */
467bc35c174SNicolas Souchu 	if (ppb->state < PPB_PERIPHERAL_NEGOCIATION)
4680f210c92SNicolas Souchu 		ppb_1284_terminate(ppbus);
469bc35c174SNicolas Souchu 
470bc35c174SNicolas Souchu 	while (ppb->state != PPB_PERIPHERAL_IDLE) {
471bc35c174SNicolas Souchu 		/* XXX should check a variable before sleeping */
472bc35c174SNicolas Souchu #ifdef DEBUG_1284
473bc35c174SNicolas Souchu 		printf("s");
474bc35c174SNicolas Souchu #endif
475bc35c174SNicolas Souchu 
4760f210c92SNicolas Souchu 		ppi_enable_intr(ppidev);
477bc35c174SNicolas Souchu 
478bc35c174SNicolas Souchu 		/* sleep until IEEE1284 negociation starts */
479bc35c174SNicolas Souchu 		error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
480bc35c174SNicolas Souchu 
481bc35c174SNicolas Souchu 		switch (error) {
482bc35c174SNicolas Souchu 		case 0:
483bc35c174SNicolas Souchu 			/* negociate peripheral side with BYTE mode */
4840f210c92SNicolas Souchu 			ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
485bc35c174SNicolas Souchu 			break;
486bc35c174SNicolas Souchu 		case EWOULDBLOCK:
487bc35c174SNicolas Souchu 			break;
488bc35c174SNicolas Souchu 		default:
489bc35c174SNicolas Souchu 			goto error;
490bc35c174SNicolas Souchu 		}
491bc35c174SNicolas Souchu 	}
492bc35c174SNicolas Souchu #ifdef DEBUG_1284
493bc35c174SNicolas Souchu 	printf("N");
494bc35c174SNicolas Souchu #endif
495bc35c174SNicolas Souchu 
496bc35c174SNicolas Souchu 	/* negociation done, write bytes to master host */
497d254af07SMatthew Dillon 	while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
498bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
4990f210c92SNicolas Souchu 		if ((error = byte_peripheral_write(ppbus,
500bc35c174SNicolas Souchu 						ppi->ppi_buffer, len, &sent)))
501bc35c174SNicolas Souchu 			goto error;
502bc35c174SNicolas Souchu #ifdef DEBUG_1284
503bc35c174SNicolas Souchu 		printf("d");
504bc35c174SNicolas Souchu #endif
505bc35c174SNicolas Souchu 	}
506bc35c174SNicolas Souchu 
507bc35c174SNicolas Souchu error:
508bc35c174SNicolas Souchu 
509bc35c174SNicolas Souchu #else /* PERIPH_1284 */
510bc35c174SNicolas Souchu 	int error = ENODEV;
511bc35c174SNicolas Souchu #endif
512bc35c174SNicolas Souchu 
513bc35c174SNicolas Souchu 	return (error);
514bc35c174SNicolas Souchu }
515bc35c174SNicolas Souchu 
516ed381522SMike Smith static int
517ecbb00a2SDoug Rabson ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
518ed381522SMike Smith {
5196f34dba9SMike Smith 	u_int unit = minor(dev);
5200f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
5210f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
5226f34dba9SMike Smith 	int error = 0;
5236f34dba9SMike Smith 	u_int8_t *val = (u_int8_t *)data;
5246f34dba9SMike Smith 
5256f34dba9SMike Smith 	switch (cmd) {
5266f34dba9SMike Smith 
5276f34dba9SMike Smith 	case PPIGDATA:			/* get data register */
5280f210c92SNicolas Souchu 		*val = ppb_rdtr(ppbus);
5296f34dba9SMike Smith 		break;
5306f34dba9SMike Smith 	case PPIGSTATUS:		/* get status bits */
5310f210c92SNicolas Souchu 		*val = ppb_rstr(ppbus);
5326f34dba9SMike Smith 		break;
5336f34dba9SMike Smith 	case PPIGCTRL:			/* get control bits */
5340f210c92SNicolas Souchu 		*val = ppb_rctr(ppbus);
5356f34dba9SMike Smith 		break;
53620240fa3SNicolas Souchu 	case PPIGEPPD:			/* get EPP data bits */
5370f210c92SNicolas Souchu 		*val = ppb_repp_D(ppbus);
5386f34dba9SMike Smith 		break;
5396f34dba9SMike Smith 	case PPIGECR:			/* get ECP bits */
5400f210c92SNicolas Souchu 		*val = ppb_recr(ppbus);
5416f34dba9SMike Smith 		break;
5426f34dba9SMike Smith 	case PPIGFIFO:			/* read FIFO */
5430f210c92SNicolas Souchu 		*val = ppb_rfifo(ppbus);
5446f34dba9SMike Smith 		break;
5456f34dba9SMike Smith 	case PPISDATA:			/* set data register */
5460f210c92SNicolas Souchu 		ppb_wdtr(ppbus, *val);
5476f34dba9SMike Smith 		break;
5486f34dba9SMike Smith 	case PPISSTATUS:		/* set status bits */
5490f210c92SNicolas Souchu 		ppb_wstr(ppbus, *val);
5506f34dba9SMike Smith 		break;
5516f34dba9SMike Smith 	case PPISCTRL:			/* set control bits */
5520f210c92SNicolas Souchu 		ppb_wctr(ppbus, *val);
5536f34dba9SMike Smith 		break;
55420240fa3SNicolas Souchu 	case PPISEPPD:			/* set EPP data bits */
5550f210c92SNicolas Souchu 		ppb_wepp_D(ppbus, *val);
5566f34dba9SMike Smith 		break;
5576f34dba9SMike Smith 	case PPISECR:			/* set ECP bits */
5580f210c92SNicolas Souchu 		ppb_wecr(ppbus, *val);
5596f34dba9SMike Smith 		break;
5606f34dba9SMike Smith 	case PPISFIFO:			/* write FIFO */
5610f210c92SNicolas Souchu 		ppb_wfifo(ppbus, *val);
5626f34dba9SMike Smith 		break;
56320240fa3SNicolas Souchu 	case PPIGEPPA:			/* get EPP address bits */
5640f210c92SNicolas Souchu 		*val = ppb_repp_A(ppbus);
56520240fa3SNicolas Souchu 		break;
56620240fa3SNicolas Souchu 	case PPISEPPA:			/* set EPP address bits */
5670f210c92SNicolas Souchu 		ppb_wepp_A(ppbus, *val);
56820240fa3SNicolas Souchu 		break;
5696f34dba9SMike Smith 	default:
5706f34dba9SMike Smith 		error = ENOTTY;
5716f34dba9SMike Smith 		break;
5726f34dba9SMike Smith 	}
5736f34dba9SMike Smith 
5746f34dba9SMike Smith 	return (error);
575ed381522SMike Smith }
576ed381522SMike Smith 
5770f210c92SNicolas Souchu DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
5780f210c92SNicolas Souchu 
579e51b0386SMike Smith #endif /* NPPI */
580