xref: /freebsd/sys/dev/ppbus/ppi.c (revision 93b0017f88462f9af15368440324aa3be58eb18f)
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  */
290f210c92SNicolas Souchu #include "opt_ppb_1284.h"
300f210c92SNicolas Souchu 
31ed381522SMike Smith #include <sys/param.h>
32ed381522SMike Smith #include <sys/systm.h>
330f210c92SNicolas Souchu #include <sys/module.h>
340f210c92SNicolas Souchu #include <sys/bus.h>
35ed381522SMike Smith #include <sys/conf.h>
36ed381522SMike Smith #include <sys/kernel.h>
37bc35c174SNicolas Souchu #include <sys/uio.h>
386f34dba9SMike Smith #include <sys/fcntl.h>
39ed381522SMike Smith 
400f210c92SNicolas Souchu #include <machine/bus.h>
410f210c92SNicolas Souchu #include <machine/resource.h>
420f210c92SNicolas Souchu #include <sys/rman.h>
43bc35c174SNicolas Souchu 
44ed381522SMike Smith #include <dev/ppbus/ppbconf.h>
45bc35c174SNicolas Souchu #include <dev/ppbus/ppb_msq.h>
46bc35c174SNicolas Souchu 
47bc35c174SNicolas Souchu #ifdef PERIPH_1284
48bc35c174SNicolas Souchu #include <dev/ppbus/ppb_1284.h>
49bc35c174SNicolas Souchu #endif
50bc35c174SNicolas Souchu 
516f34dba9SMike Smith #include <dev/ppbus/ppi.h>
526f34dba9SMike Smith 
530f210c92SNicolas Souchu #include "ppbus_if.h"
540f210c92SNicolas Souchu 
550f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
560f210c92SNicolas Souchu 
57bc35c174SNicolas Souchu #define BUFSIZE		512
58ed381522SMike Smith 
59e51b0386SMike Smith struct ppi_data {
60e51b0386SMike Smith 
61e51b0386SMike Smith     int		ppi_unit;
626f34dba9SMike Smith     int		ppi_flags;
636f34dba9SMike Smith #define HAVE_PPBUS	(1<<0)
64bc35c174SNicolas Souchu #define HAD_PPBUS	(1<<1)
65bc35c174SNicolas Souchu 
66bc35c174SNicolas Souchu     int		ppi_count;
67bc35c174SNicolas Souchu     int		ppi_mode;			/* IEEE1284 mode */
68bc35c174SNicolas Souchu     char	ppi_buffer[BUFSIZE];
69e51b0386SMike Smith 
70c47eb96cSNick Hibma #ifdef PERIPH_1284
710f210c92SNicolas Souchu     struct resource *intr_resource;	/* interrupt resource */
720f210c92SNicolas Souchu     void *intr_cookie;			/* interrupt registration cookie */
73c47eb96cSNick Hibma #endif /* PERIPH_1284 */
74e51b0386SMike Smith };
75e51b0386SMike Smith 
760f210c92SNicolas Souchu #define DEVTOSOFTC(dev) \
770f210c92SNicolas Souchu 	((struct ppi_data *)device_get_softc(dev))
780f210c92SNicolas Souchu #define UNITOSOFTC(unit) \
790f210c92SNicolas Souchu 	((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
800f210c92SNicolas Souchu #define UNITODEVICE(unit) \
810f210c92SNicolas Souchu 	(devclass_get_device(ppi_devclass, (unit)))
82ed381522SMike Smith 
830f210c92SNicolas Souchu static devclass_t ppi_devclass;
84ed381522SMike Smith 
85ed381522SMike Smith static	d_open_t	ppiopen;
86ed381522SMike Smith static	d_close_t	ppiclose;
87ed381522SMike Smith static	d_ioctl_t	ppiioctl;
88bc35c174SNicolas Souchu static	d_write_t	ppiwrite;
89bc35c174SNicolas Souchu static	d_read_t	ppiread;
90ed381522SMike Smith 
914f013cf5SMike Smith #define CDEV_MAJOR 82
924e2f199eSPoul-Henning Kamp static struct cdevsw ppi_cdevsw = {
934e2f199eSPoul-Henning Kamp 	/* open */	ppiopen,
944e2f199eSPoul-Henning Kamp 	/* close */	ppiclose,
954e2f199eSPoul-Henning Kamp 	/* read */	ppiread,
964e2f199eSPoul-Henning Kamp 	/* write */	ppiwrite,
974e2f199eSPoul-Henning Kamp 	/* ioctl */	ppiioctl,
984e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
994e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1004e2f199eSPoul-Henning Kamp 	/* strategy */	nostrategy,
1014e2f199eSPoul-Henning Kamp 	/* name */	"ppi",
1024e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
1034e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1044e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1054e2f199eSPoul-Henning Kamp 	/* flags */	0,
1064e2f199eSPoul-Henning Kamp };
107ed381522SMike Smith 
108bc35c174SNicolas Souchu #ifdef PERIPH_1284
109bc35c174SNicolas Souchu 
110bc35c174SNicolas Souchu static void
1110f210c92SNicolas Souchu ppi_enable_intr(device_t ppidev)
112bc35c174SNicolas Souchu {
113bc35c174SNicolas Souchu 	char r;
1140f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
115bc35c174SNicolas Souchu 
1160f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1170f210c92SNicolas Souchu 	ppb_wctr(ppbus, r | IRQENABLE);
118bc35c174SNicolas Souchu 
119bc35c174SNicolas Souchu 	return;
120bc35c174SNicolas Souchu }
121bc35c174SNicolas Souchu 
122bc35c174SNicolas Souchu static void
1230f210c92SNicolas Souchu ppi_disable_intr(device_t ppidev)
124bc35c174SNicolas Souchu {
125bc35c174SNicolas Souchu 	char r;
1260f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
127bc35c174SNicolas Souchu 
1280f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1290f210c92SNicolas Souchu 	ppb_wctr(ppbus, r & ~IRQENABLE);
130bc35c174SNicolas Souchu 
131bc35c174SNicolas Souchu 	return;
132bc35c174SNicolas Souchu }
133bc35c174SNicolas Souchu 
134bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
135bc35c174SNicolas Souchu 
1360f063508SPeter Wemm static void
1370f063508SPeter Wemm ppi_identify(driver_t *driver, device_t parent)
1380f063508SPeter Wemm {
1390f063508SPeter Wemm 
140338cad62SBernd Walter 	BUS_ADD_CHILD(parent, 0, "ppi", -1);
1410f063508SPeter Wemm }
1420f063508SPeter Wemm 
143ed381522SMike Smith /*
1440f210c92SNicolas Souchu  * ppi_probe()
145ed381522SMike Smith  */
1460f210c92SNicolas Souchu static int
1470f210c92SNicolas Souchu ppi_probe(device_t dev)
148ed381522SMike Smith {
149ed381522SMike Smith 	struct ppi_data *ppi;
1502447bec8SPoul-Henning Kamp 
1510f210c92SNicolas Souchu 	/* probe is always ok */
1520f210c92SNicolas Souchu 	device_set_desc(dev, "Parallel I/O");
1530f210c92SNicolas Souchu 
1540f210c92SNicolas Souchu 	ppi = DEVTOSOFTC(dev);
155ed381522SMike Smith 	bzero(ppi, sizeof(struct ppi_data));
156ed381522SMike Smith 
1570f210c92SNicolas Souchu 	return (0);
158ed381522SMike Smith }
159ed381522SMike Smith 
160ed381522SMike Smith /*
1610f210c92SNicolas Souchu  * ppi_attach()
162ed381522SMike Smith  */
1630f210c92SNicolas Souchu static int
1640f210c92SNicolas Souchu ppi_attach(device_t dev)
1650f210c92SNicolas Souchu {
166c47eb96cSNick Hibma #ifdef PERIPH_1284
1670f210c92SNicolas Souchu 	uintptr_t irq;
1680f210c92SNicolas Souchu 	int zero = 0;
1690f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(dev);
170ed381522SMike Smith 
1710f210c92SNicolas Souchu 	/* retrive the irq */
1720f210c92SNicolas Souchu 	BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
1730f210c92SNicolas Souchu 
1740f210c92SNicolas Souchu 	/* declare our interrupt handler */
1750f210c92SNicolas Souchu 	ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
1760f210c92SNicolas Souchu 						&zero, irq, irq, 1, RF_ACTIVE);
177c47eb96cSNick Hibma #endif /* PERIPH_1284 */
1780f210c92SNicolas Souchu 
1790f210c92SNicolas Souchu 	make_dev(&ppi_cdevsw, device_get_unit(dev),	/* XXX cleanup */
1800f210c92SNicolas Souchu 		 UID_ROOT, GID_WHEEL,
1810f210c92SNicolas Souchu 		 0600, "ppi%d", device_get_unit(dev));
1820f210c92SNicolas Souchu 
1830f210c92SNicolas Souchu 	return (0);
184ed381522SMike Smith }
185ed381522SMike Smith 
186c47eb96cSNick Hibma #ifdef PERIPH_1284
187bc35c174SNicolas Souchu /*
188bc35c174SNicolas Souchu  * Cable
189bc35c174SNicolas Souchu  * -----
190bc35c174SNicolas Souchu  *
191bc35c174SNicolas Souchu  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
192bc35c174SNicolas Souchu  *
193bc35c174SNicolas Souchu  * nStrobe   <-> nAck		1  <-> 10
194bc35c174SNicolas Souchu  * nAutofd   <-> Busy		11 <-> 14
195bc35c174SNicolas Souchu  * nSelectin <-> Select		17 <-> 13
196bc35c174SNicolas Souchu  * nInit     <-> nFault		15 <-> 16
197bc35c174SNicolas Souchu  *
198bc35c174SNicolas Souchu  */
199ed381522SMike Smith static void
2000f210c92SNicolas Souchu ppiintr(void *arg)
201ed381522SMike Smith {
2020f210c92SNicolas Souchu 	device_t ppidev = (device_t)arg;
2030f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2040f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(ppidev);
205bc35c174SNicolas Souchu 
2060f210c92SNicolas Souchu 	ppi_disable_intr(ppidev);
207bc35c174SNicolas Souchu 
2080f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
209bc35c174SNicolas Souchu 
210bc35c174SNicolas Souchu 	/* accept IEEE1284 negociation then wakeup an waiting process to
211bc35c174SNicolas Souchu 	 * continue negociation at process level */
212bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
213bc35c174SNicolas Souchu 		/* Event 1 */
2140f210c92SNicolas Souchu 		if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
215bc35c174SNicolas Souchu 							(SELECT | nBUSY)) {
216bc35c174SNicolas Souchu 			/* IEEE1284 negociation */
217bc35c174SNicolas Souchu #ifdef DEBUG_1284
218bc35c174SNicolas Souchu 			printf("N");
219bc35c174SNicolas Souchu #endif
220bc35c174SNicolas Souchu 
221bc35c174SNicolas Souchu 			/* Event 2 - prepare for reading the ext. value */
2220f210c92SNicolas Souchu 			ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
223bc35c174SNicolas Souchu 
2240f210c92SNicolas Souchu 			ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
225bc35c174SNicolas Souchu 
226bc35c174SNicolas Souchu 		} else {
227bc35c174SNicolas Souchu #ifdef DEBUG_1284
2280f210c92SNicolas Souchu 			printf("0x%x", ppb_rstr(ppbus));
229bc35c174SNicolas Souchu #endif
2300f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
231bc35c174SNicolas Souchu 			break;
232bc35c174SNicolas Souchu 		}
233bc35c174SNicolas Souchu 
234bc35c174SNicolas Souchu 		/* wake up any process waiting for negociation from
235bc35c174SNicolas Souchu 		 * remote master host */
236bc35c174SNicolas Souchu 
237bc35c174SNicolas Souchu 		/* XXX should set a variable to warn the process about
238bc35c174SNicolas Souchu 		 * the interrupt */
239bc35c174SNicolas Souchu 
240bc35c174SNicolas Souchu 		wakeup(ppi);
241bc35c174SNicolas Souchu 		break;
242bc35c174SNicolas Souchu 	default:
243bc35c174SNicolas Souchu #ifdef DEBUG_1284
24499904c75SPeter Wemm 		printf("?%d", ppb_1284_get_state(ppbus));
245bc35c174SNicolas Souchu #endif
2460f210c92SNicolas Souchu 		ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
2470f210c92SNicolas Souchu 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
248bc35c174SNicolas Souchu 		break;
249bc35c174SNicolas Souchu 	}
250bc35c174SNicolas Souchu 
2510f210c92SNicolas Souchu 	ppi_enable_intr(ppidev);
252bc35c174SNicolas Souchu 
253ed381522SMike Smith 	return;
254ed381522SMike Smith }
255c47eb96cSNick Hibma #endif /* PERIPH_1284 */
256ed381522SMike Smith 
257ed381522SMike Smith static int
258b40ce416SJulian Elischer ppiopen(dev_t dev, int flags, int fmt, struct thread *td)
259ed381522SMike Smith {
260e51b0386SMike Smith 	u_int unit = minor(dev);
2610f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
2620f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
2630f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2646f34dba9SMike Smith 	int res;
265e51b0386SMike Smith 
2660f210c92SNicolas Souchu 	if (!ppi)
267e51b0386SMike Smith 		return (ENXIO);
268e51b0386SMike Smith 
269bc35c174SNicolas Souchu 	if (!(ppi->ppi_flags & HAVE_PPBUS)) {
2700f210c92SNicolas Souchu 		if ((res = ppb_request_bus(ppbus, ppidev,
271bc35c174SNicolas Souchu 			(flags & O_NONBLOCK) ? PPB_DONTWAIT :
272bc35c174SNicolas Souchu 						(PPB_WAIT | PPB_INTR))))
2736f34dba9SMike Smith 			return (res);
274e51b0386SMike Smith 
2756f34dba9SMike Smith 		ppi->ppi_flags |= HAVE_PPBUS;
2760f210c92SNicolas Souchu 
277c47eb96cSNick Hibma #ifdef PERIPH_1284
278c47eb96cSNick Hibma 		if (ppi->intr_resource) {
2790f210c92SNicolas Souchu 			/* register our interrupt handler */
2800f210c92SNicolas Souchu 			BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
2810f210c92SNicolas Souchu 				       INTR_TYPE_TTY, ppiintr, dev, &ppi->intr_cookie);
282bc35c174SNicolas Souchu 		}
283c47eb96cSNick Hibma #endif /* PERIPH_1284 */
284c47eb96cSNick Hibma 	}
285bc35c174SNicolas Souchu 	ppi->ppi_count += 1;
286bc35c174SNicolas Souchu 
2876f34dba9SMike Smith 	return (0);
288ed381522SMike Smith }
289ed381522SMike Smith 
290ed381522SMike Smith static int
291b40ce416SJulian Elischer ppiclose(dev_t dev, int flags, int fmt, struct thread *td)
292ed381522SMike Smith {
2936f34dba9SMike Smith 	u_int unit = minor(dev);
2940f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
2950f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
2960f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2976f34dba9SMike Smith 
298bc35c174SNicolas Souchu 	ppi->ppi_count --;
299bc35c174SNicolas Souchu 	if (!ppi->ppi_count) {
300bc35c174SNicolas Souchu 
301bc35c174SNicolas Souchu #ifdef PERIPH_1284
3020f210c92SNicolas Souchu 		switch (ppb_1284_get_state(ppbus)) {
303bc35c174SNicolas Souchu 		case PPB_PERIPHERAL_IDLE:
3040f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, 0);
305bc35c174SNicolas Souchu 			break;
306bc35c174SNicolas Souchu 		case PPB_REVERSE_IDLE:
307bc35c174SNicolas Souchu 		case PPB_EPP_IDLE:
308bc35c174SNicolas Souchu 		case PPB_ECP_FORWARD_IDLE:
309bc35c174SNicolas Souchu 		default:
3100f210c92SNicolas Souchu 			ppb_1284_terminate(ppbus);
311bc35c174SNicolas Souchu 			break;
312bc35c174SNicolas Souchu 		}
313bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
314bc35c174SNicolas Souchu 
3150f210c92SNicolas Souchu 		/* unregistration of interrupt forced by release */
3160f210c92SNicolas Souchu 		ppb_release_bus(ppbus, ppidev);
3170f210c92SNicolas Souchu 
3186f34dba9SMike Smith 		ppi->ppi_flags &= ~HAVE_PPBUS;
319bc35c174SNicolas Souchu 	}
320bc35c174SNicolas Souchu 
3216f34dba9SMike Smith 	return (0);
322ed381522SMike Smith }
323ed381522SMike Smith 
324bc35c174SNicolas Souchu /*
325bc35c174SNicolas Souchu  * ppiread()
326bc35c174SNicolas Souchu  *
327bc35c174SNicolas Souchu  * IEEE1284 compliant read.
328bc35c174SNicolas Souchu  *
329bc35c174SNicolas Souchu  * First, try negociation to BYTE then NIBBLE mode
330bc35c174SNicolas Souchu  * If no data is available, wait for it otherwise transfer as much as possible
331bc35c174SNicolas Souchu  */
332bc35c174SNicolas Souchu static int
333bc35c174SNicolas Souchu ppiread(dev_t dev, struct uio *uio, int ioflag)
334bc35c174SNicolas Souchu {
335bc35c174SNicolas Souchu #ifdef PERIPH_1284
336bc35c174SNicolas Souchu 	u_int unit = minor(dev);
3370f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
3380f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
3390f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
340bc35c174SNicolas Souchu 	int len, error = 0;
341bc35c174SNicolas Souchu 
3420f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
343bc35c174SNicolas Souchu 	case PPB_PERIPHERAL_IDLE:
3440f210c92SNicolas Souchu 		ppb_peripheral_terminate(ppbus, 0);
34593b0017fSPhilippe Charnier 		/* FALLTHROUGH */
346bc35c174SNicolas Souchu 
347bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
348bc35c174SNicolas Souchu 		/* if can't negociate NIBBLE mode then try BYTE mode,
349bc35c174SNicolas Souchu 		 * the peripheral may be a computer
350bc35c174SNicolas Souchu 		 */
3510f210c92SNicolas Souchu 		if ((ppb_1284_negociate(ppbus,
352bc35c174SNicolas Souchu 			ppi->ppi_mode = PPB_NIBBLE, 0))) {
353bc35c174SNicolas Souchu 
354bc35c174SNicolas Souchu 			/* XXX Wait 2 seconds to let the remote host some
355bc35c174SNicolas Souchu 			 * time to terminate its interrupt
356bc35c174SNicolas Souchu 			 */
357bc35c174SNicolas Souchu 			tsleep(ppi, PPBPRI, "ppiread", 2*hz);
358bc35c174SNicolas Souchu 
3590f210c92SNicolas Souchu 			if ((error = ppb_1284_negociate(ppbus,
360bc35c174SNicolas Souchu 				ppi->ppi_mode = PPB_BYTE, 0)))
361bc35c174SNicolas Souchu 				return (error);
362bc35c174SNicolas Souchu 		}
363bc35c174SNicolas Souchu 		break;
364bc35c174SNicolas Souchu 
365bc35c174SNicolas Souchu 	case PPB_REVERSE_IDLE:
366bc35c174SNicolas Souchu 	case PPB_EPP_IDLE:
367bc35c174SNicolas Souchu 	case PPB_ECP_FORWARD_IDLE:
368bc35c174SNicolas Souchu 	default:
369bc35c174SNicolas Souchu 		break;
370bc35c174SNicolas Souchu 	}
371bc35c174SNicolas Souchu 
372bc35c174SNicolas Souchu #ifdef DEBUG_1284
373bc35c174SNicolas Souchu 	printf("N");
374bc35c174SNicolas Souchu #endif
375bc35c174SNicolas Souchu 	/* read data */
376bc35c174SNicolas Souchu 	len = 0;
377bc35c174SNicolas Souchu 	while (uio->uio_resid) {
3780f210c92SNicolas Souchu 		if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
379bc35c174SNicolas Souchu 			ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
380bc35c174SNicolas Souchu 			&len))) {
381bc35c174SNicolas Souchu 			goto error;
382bc35c174SNicolas Souchu 		}
383bc35c174SNicolas Souchu 
384bc35c174SNicolas Souchu 		if (!len)
385bc35c174SNicolas Souchu 			goto error;		/* no more data */
386bc35c174SNicolas Souchu 
387bc35c174SNicolas Souchu #ifdef DEBUG_1284
388bc35c174SNicolas Souchu 		printf("d");
389bc35c174SNicolas Souchu #endif
390bc35c174SNicolas Souchu 		if ((error = uiomove(ppi->ppi_buffer, len, uio)))
391bc35c174SNicolas Souchu 			goto error;
392bc35c174SNicolas Souchu 	}
393bc35c174SNicolas Souchu 
394bc35c174SNicolas Souchu error:
395bc35c174SNicolas Souchu 
396bc35c174SNicolas Souchu #else /* PERIPH_1284 */
397bc35c174SNicolas Souchu 	int error = ENODEV;
398bc35c174SNicolas Souchu #endif
399bc35c174SNicolas Souchu 
400bc35c174SNicolas Souchu 	return (error);
401bc35c174SNicolas Souchu }
402bc35c174SNicolas Souchu 
403bc35c174SNicolas Souchu /*
404bc35c174SNicolas Souchu  * ppiwrite()
405bc35c174SNicolas Souchu  *
406bc35c174SNicolas Souchu  * IEEE1284 compliant write
407bc35c174SNicolas Souchu  *
408bc35c174SNicolas Souchu  * Actually, this is the peripheral side of a remote IEEE1284 read
409bc35c174SNicolas Souchu  *
410bc35c174SNicolas Souchu  * The first part of the negociation (IEEE1284 device detection) is
411bc35c174SNicolas Souchu  * done at interrupt level, then the remaining is done by the writing
412bc35c174SNicolas Souchu  * process
413bc35c174SNicolas Souchu  *
414bc35c174SNicolas Souchu  * Once negociation done, transfer data
415bc35c174SNicolas Souchu  */
416bc35c174SNicolas Souchu static int
417bc35c174SNicolas Souchu ppiwrite(dev_t dev, struct uio *uio, int ioflag)
418bc35c174SNicolas Souchu {
419bc35c174SNicolas Souchu #ifdef PERIPH_1284
420bc35c174SNicolas Souchu 	u_int unit = minor(dev);
4210f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
4220f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
4230f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
424bc35c174SNicolas Souchu 	int len, error = 0, sent;
425bc35c174SNicolas Souchu 
426bc35c174SNicolas Souchu #if 0
427bc35c174SNicolas Souchu 	int ret;
428bc35c174SNicolas Souchu 
429bc35c174SNicolas Souchu 	#define ADDRESS		MS_PARAM(0, 0, MS_TYP_PTR)
430bc35c174SNicolas Souchu 	#define LENGTH		MS_PARAM(0, 1, MS_TYP_INT)
431bc35c174SNicolas Souchu 
432bc35c174SNicolas Souchu 	struct ppb_microseq msq[] = {
433bc35c174SNicolas Souchu 		  { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
434bc35c174SNicolas Souchu 		  MS_RET(0)
435bc35c174SNicolas Souchu 	};
436bc35c174SNicolas Souchu 
437bc35c174SNicolas Souchu 	/* negociate ECP mode */
4380f210c92SNicolas Souchu 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
439bc35c174SNicolas Souchu 		printf("ppiwrite: ECP negociation failed\n");
440bc35c174SNicolas Souchu 	}
441bc35c174SNicolas Souchu 
442bc35c174SNicolas Souchu 	while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
443bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
444bc35c174SNicolas Souchu 
445bc35c174SNicolas Souchu 		ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
446bc35c174SNicolas Souchu 
4470f210c92SNicolas Souchu 		error = ppb_MS_microseq(ppbus, msq, &ret);
448bc35c174SNicolas Souchu 	}
449bc35c174SNicolas Souchu #endif
450bc35c174SNicolas Souchu 
451bc35c174SNicolas Souchu 	/* we have to be peripheral to be able to send data, so
452bc35c174SNicolas Souchu 	 * wait for the appropriate state
453bc35c174SNicolas Souchu 	 */
454bdcee5cfSNicolas Souchu  	if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
4550f210c92SNicolas Souchu 		ppb_1284_terminate(ppbus);
456bc35c174SNicolas Souchu 
457bdcee5cfSNicolas Souchu  	while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
458bc35c174SNicolas Souchu 		/* XXX should check a variable before sleeping */
459bc35c174SNicolas Souchu #ifdef DEBUG_1284
460bc35c174SNicolas Souchu 		printf("s");
461bc35c174SNicolas Souchu #endif
462bc35c174SNicolas Souchu 
4630f210c92SNicolas Souchu 		ppi_enable_intr(ppidev);
464bc35c174SNicolas Souchu 
465bc35c174SNicolas Souchu 		/* sleep until IEEE1284 negociation starts */
466bc35c174SNicolas Souchu 		error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
467bc35c174SNicolas Souchu 
468bc35c174SNicolas Souchu 		switch (error) {
469bc35c174SNicolas Souchu 		case 0:
470bc35c174SNicolas Souchu 			/* negociate peripheral side with BYTE mode */
4710f210c92SNicolas Souchu 			ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
472bc35c174SNicolas Souchu 			break;
473bc35c174SNicolas Souchu 		case EWOULDBLOCK:
474bc35c174SNicolas Souchu 			break;
475bc35c174SNicolas Souchu 		default:
476bc35c174SNicolas Souchu 			goto error;
477bc35c174SNicolas Souchu 		}
478bc35c174SNicolas Souchu 	}
479bc35c174SNicolas Souchu #ifdef DEBUG_1284
480bc35c174SNicolas Souchu 	printf("N");
481bc35c174SNicolas Souchu #endif
482bc35c174SNicolas Souchu 
483bc35c174SNicolas Souchu 	/* negociation done, write bytes to master host */
484d254af07SMatthew Dillon 	while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
485bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
4860f210c92SNicolas Souchu 		if ((error = byte_peripheral_write(ppbus,
487bc35c174SNicolas Souchu 						ppi->ppi_buffer, len, &sent)))
488bc35c174SNicolas Souchu 			goto error;
489bc35c174SNicolas Souchu #ifdef DEBUG_1284
490bc35c174SNicolas Souchu 		printf("d");
491bc35c174SNicolas Souchu #endif
492bc35c174SNicolas Souchu 	}
493bc35c174SNicolas Souchu 
494bc35c174SNicolas Souchu error:
495bc35c174SNicolas Souchu 
496bc35c174SNicolas Souchu #else /* PERIPH_1284 */
497bc35c174SNicolas Souchu 	int error = ENODEV;
498bc35c174SNicolas Souchu #endif
499bc35c174SNicolas Souchu 
500bc35c174SNicolas Souchu 	return (error);
501bc35c174SNicolas Souchu }
502bc35c174SNicolas Souchu 
503ed381522SMike Smith static int
504b40ce416SJulian Elischer ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
505ed381522SMike Smith {
5066f34dba9SMike Smith 	u_int unit = minor(dev);
5070f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
5080f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
5096f34dba9SMike Smith 	int error = 0;
5106f34dba9SMike Smith 	u_int8_t *val = (u_int8_t *)data;
5116f34dba9SMike Smith 
5126f34dba9SMike Smith 	switch (cmd) {
5136f34dba9SMike Smith 
5146f34dba9SMike Smith 	case PPIGDATA:			/* get data register */
5150f210c92SNicolas Souchu 		*val = ppb_rdtr(ppbus);
5166f34dba9SMike Smith 		break;
5176f34dba9SMike Smith 	case PPIGSTATUS:		/* get status bits */
5180f210c92SNicolas Souchu 		*val = ppb_rstr(ppbus);
5196f34dba9SMike Smith 		break;
5206f34dba9SMike Smith 	case PPIGCTRL:			/* get control bits */
5210f210c92SNicolas Souchu 		*val = ppb_rctr(ppbus);
5226f34dba9SMike Smith 		break;
52320240fa3SNicolas Souchu 	case PPIGEPPD:			/* get EPP data bits */
5240f210c92SNicolas Souchu 		*val = ppb_repp_D(ppbus);
5256f34dba9SMike Smith 		break;
5266f34dba9SMike Smith 	case PPIGECR:			/* get ECP bits */
5270f210c92SNicolas Souchu 		*val = ppb_recr(ppbus);
5286f34dba9SMike Smith 		break;
5296f34dba9SMike Smith 	case PPIGFIFO:			/* read FIFO */
5300f210c92SNicolas Souchu 		*val = ppb_rfifo(ppbus);
5316f34dba9SMike Smith 		break;
5326f34dba9SMike Smith 	case PPISDATA:			/* set data register */
5330f210c92SNicolas Souchu 		ppb_wdtr(ppbus, *val);
5346f34dba9SMike Smith 		break;
5356f34dba9SMike Smith 	case PPISSTATUS:		/* set status bits */
5360f210c92SNicolas Souchu 		ppb_wstr(ppbus, *val);
5376f34dba9SMike Smith 		break;
5386f34dba9SMike Smith 	case PPISCTRL:			/* set control bits */
5390f210c92SNicolas Souchu 		ppb_wctr(ppbus, *val);
5406f34dba9SMike Smith 		break;
54120240fa3SNicolas Souchu 	case PPISEPPD:			/* set EPP data bits */
5420f210c92SNicolas Souchu 		ppb_wepp_D(ppbus, *val);
5436f34dba9SMike Smith 		break;
5446f34dba9SMike Smith 	case PPISECR:			/* set ECP bits */
5450f210c92SNicolas Souchu 		ppb_wecr(ppbus, *val);
5466f34dba9SMike Smith 		break;
5476f34dba9SMike Smith 	case PPISFIFO:			/* write FIFO */
5480f210c92SNicolas Souchu 		ppb_wfifo(ppbus, *val);
5496f34dba9SMike Smith 		break;
55020240fa3SNicolas Souchu 	case PPIGEPPA:			/* get EPP address bits */
5510f210c92SNicolas Souchu 		*val = ppb_repp_A(ppbus);
55220240fa3SNicolas Souchu 		break;
55320240fa3SNicolas Souchu 	case PPISEPPA:			/* set EPP address bits */
5540f210c92SNicolas Souchu 		ppb_wepp_A(ppbus, *val);
55520240fa3SNicolas Souchu 		break;
5566f34dba9SMike Smith 	default:
5576f34dba9SMike Smith 		error = ENOTTY;
5586f34dba9SMike Smith 		break;
5596f34dba9SMike Smith 	}
5606f34dba9SMike Smith 
5616f34dba9SMike Smith 	return (error);
562ed381522SMike Smith }
563ed381522SMike Smith 
5640f063508SPeter Wemm static device_method_t ppi_methods[] = {
5650f063508SPeter Wemm 	/* device interface */
5660f063508SPeter Wemm 	DEVMETHOD(device_identify,	ppi_identify),
5670f063508SPeter Wemm 	DEVMETHOD(device_probe,		ppi_probe),
5680f063508SPeter Wemm 	DEVMETHOD(device_attach,	ppi_attach),
5690f210c92SNicolas Souchu 
5700f063508SPeter Wemm 	{ 0, 0 }
5710f063508SPeter Wemm };
5720f063508SPeter Wemm 
5730f063508SPeter Wemm static driver_t ppi_driver = {
5740f063508SPeter Wemm 	"ppi",
5750f063508SPeter Wemm 	ppi_methods,
5760f063508SPeter Wemm 	sizeof(struct ppi_data),
5770f063508SPeter Wemm };
5780f063508SPeter Wemm DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
579