xref: /freebsd/sys/dev/ppbus/ppi.c (revision 7ac40f5f59dea6e9ab4869e974bdd4026274e921)
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 = {
937ac40f5fSPoul-Henning Kamp 	.d_open =	ppiopen,
947ac40f5fSPoul-Henning Kamp 	.d_close =	ppiclose,
957ac40f5fSPoul-Henning Kamp 	.d_read =	ppiread,
967ac40f5fSPoul-Henning Kamp 	.d_write =	ppiwrite,
977ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ppiioctl,
987ac40f5fSPoul-Henning Kamp 	.d_name =	"ppi",
997ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
1004e2f199eSPoul-Henning Kamp };
101ed381522SMike Smith 
102bc35c174SNicolas Souchu #ifdef PERIPH_1284
103bc35c174SNicolas Souchu 
104bc35c174SNicolas Souchu static void
1050f210c92SNicolas Souchu ppi_enable_intr(device_t ppidev)
106bc35c174SNicolas Souchu {
107bc35c174SNicolas Souchu 	char r;
1080f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
109bc35c174SNicolas Souchu 
1100f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1110f210c92SNicolas Souchu 	ppb_wctr(ppbus, r | IRQENABLE);
112bc35c174SNicolas Souchu 
113bc35c174SNicolas Souchu 	return;
114bc35c174SNicolas Souchu }
115bc35c174SNicolas Souchu 
116bc35c174SNicolas Souchu static void
1170f210c92SNicolas Souchu ppi_disable_intr(device_t ppidev)
118bc35c174SNicolas Souchu {
119bc35c174SNicolas Souchu 	char r;
1200f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
121bc35c174SNicolas Souchu 
1220f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1230f210c92SNicolas Souchu 	ppb_wctr(ppbus, r & ~IRQENABLE);
124bc35c174SNicolas Souchu 
125bc35c174SNicolas Souchu 	return;
126bc35c174SNicolas Souchu }
127bc35c174SNicolas Souchu 
128bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
129bc35c174SNicolas Souchu 
1300f063508SPeter Wemm static void
1310f063508SPeter Wemm ppi_identify(driver_t *driver, device_t parent)
1320f063508SPeter Wemm {
1330f063508SPeter Wemm 
134338cad62SBernd Walter 	BUS_ADD_CHILD(parent, 0, "ppi", -1);
1350f063508SPeter Wemm }
1360f063508SPeter Wemm 
137ed381522SMike Smith /*
1380f210c92SNicolas Souchu  * ppi_probe()
139ed381522SMike Smith  */
1400f210c92SNicolas Souchu static int
1410f210c92SNicolas Souchu ppi_probe(device_t dev)
142ed381522SMike Smith {
143ed381522SMike Smith 	struct ppi_data *ppi;
1442447bec8SPoul-Henning Kamp 
1450f210c92SNicolas Souchu 	/* probe is always ok */
1460f210c92SNicolas Souchu 	device_set_desc(dev, "Parallel I/O");
1470f210c92SNicolas Souchu 
1480f210c92SNicolas Souchu 	ppi = DEVTOSOFTC(dev);
149ed381522SMike Smith 	bzero(ppi, sizeof(struct ppi_data));
150ed381522SMike Smith 
1510f210c92SNicolas Souchu 	return (0);
152ed381522SMike Smith }
153ed381522SMike Smith 
154ed381522SMike Smith /*
1550f210c92SNicolas Souchu  * ppi_attach()
156ed381522SMike Smith  */
1570f210c92SNicolas Souchu static int
1580f210c92SNicolas Souchu ppi_attach(device_t dev)
1590f210c92SNicolas Souchu {
160c47eb96cSNick Hibma #ifdef PERIPH_1284
1610f210c92SNicolas Souchu 	uintptr_t irq;
1620f210c92SNicolas Souchu 	int zero = 0;
1630f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(dev);
164ed381522SMike Smith 
1650f210c92SNicolas Souchu 	/* retrive the irq */
1660f210c92SNicolas Souchu 	BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
1670f210c92SNicolas Souchu 
1680f210c92SNicolas Souchu 	/* declare our interrupt handler */
1690f210c92SNicolas Souchu 	ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
1700f210c92SNicolas Souchu 						&zero, irq, irq, 1, RF_ACTIVE);
171c47eb96cSNick Hibma #endif /* PERIPH_1284 */
1720f210c92SNicolas Souchu 
1730f210c92SNicolas Souchu 	make_dev(&ppi_cdevsw, device_get_unit(dev),	/* XXX cleanup */
1740f210c92SNicolas Souchu 		 UID_ROOT, GID_WHEEL,
1750f210c92SNicolas Souchu 		 0600, "ppi%d", device_get_unit(dev));
1760f210c92SNicolas Souchu 
1770f210c92SNicolas Souchu 	return (0);
178ed381522SMike Smith }
179ed381522SMike Smith 
180c47eb96cSNick Hibma #ifdef PERIPH_1284
181bc35c174SNicolas Souchu /*
182bc35c174SNicolas Souchu  * Cable
183bc35c174SNicolas Souchu  * -----
184bc35c174SNicolas Souchu  *
185bc35c174SNicolas Souchu  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
186bc35c174SNicolas Souchu  *
187bc35c174SNicolas Souchu  * nStrobe   <-> nAck		1  <-> 10
188bc35c174SNicolas Souchu  * nAutofd   <-> Busy		11 <-> 14
189bc35c174SNicolas Souchu  * nSelectin <-> Select		17 <-> 13
190bc35c174SNicolas Souchu  * nInit     <-> nFault		15 <-> 16
191bc35c174SNicolas Souchu  *
192bc35c174SNicolas Souchu  */
193ed381522SMike Smith static void
1940f210c92SNicolas Souchu ppiintr(void *arg)
195ed381522SMike Smith {
1960f210c92SNicolas Souchu 	device_t ppidev = (device_t)arg;
1970f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
1980f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(ppidev);
199bc35c174SNicolas Souchu 
2000f210c92SNicolas Souchu 	ppi_disable_intr(ppidev);
201bc35c174SNicolas Souchu 
2020f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
203bc35c174SNicolas Souchu 
204d64ada50SJens Schweikhardt 	/* accept IEEE1284 negotiation then wakeup a waiting process to
205d64ada50SJens Schweikhardt 	 * continue negotiation at process level */
206bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
207bc35c174SNicolas Souchu 		/* Event 1 */
2080f210c92SNicolas Souchu 		if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
209bc35c174SNicolas Souchu 							(SELECT | nBUSY)) {
210d64ada50SJens Schweikhardt 			/* IEEE1284 negotiation */
211bc35c174SNicolas Souchu #ifdef DEBUG_1284
212bc35c174SNicolas Souchu 			printf("N");
213bc35c174SNicolas Souchu #endif
214bc35c174SNicolas Souchu 
215bc35c174SNicolas Souchu 			/* Event 2 - prepare for reading the ext. value */
2160f210c92SNicolas Souchu 			ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
217bc35c174SNicolas Souchu 
2180f210c92SNicolas Souchu 			ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
219bc35c174SNicolas Souchu 
220bc35c174SNicolas Souchu 		} else {
221bc35c174SNicolas Souchu #ifdef DEBUG_1284
2220f210c92SNicolas Souchu 			printf("0x%x", ppb_rstr(ppbus));
223bc35c174SNicolas Souchu #endif
2240f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
225bc35c174SNicolas Souchu 			break;
226bc35c174SNicolas Souchu 		}
227bc35c174SNicolas Souchu 
228d64ada50SJens Schweikhardt 		/* wake up any process waiting for negotiation from
229bc35c174SNicolas Souchu 		 * remote master host */
230bc35c174SNicolas Souchu 
231bc35c174SNicolas Souchu 		/* XXX should set a variable to warn the process about
232bc35c174SNicolas Souchu 		 * the interrupt */
233bc35c174SNicolas Souchu 
234bc35c174SNicolas Souchu 		wakeup(ppi);
235bc35c174SNicolas Souchu 		break;
236bc35c174SNicolas Souchu 	default:
237bc35c174SNicolas Souchu #ifdef DEBUG_1284
23899904c75SPeter Wemm 		printf("?%d", ppb_1284_get_state(ppbus));
239bc35c174SNicolas Souchu #endif
2400f210c92SNicolas Souchu 		ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
2410f210c92SNicolas Souchu 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
242bc35c174SNicolas Souchu 		break;
243bc35c174SNicolas Souchu 	}
244bc35c174SNicolas Souchu 
2450f210c92SNicolas Souchu 	ppi_enable_intr(ppidev);
246bc35c174SNicolas Souchu 
247ed381522SMike Smith 	return;
248ed381522SMike Smith }
249c47eb96cSNick Hibma #endif /* PERIPH_1284 */
250ed381522SMike Smith 
251ed381522SMike Smith static int
252b40ce416SJulian Elischer ppiopen(dev_t dev, int flags, int fmt, struct thread *td)
253ed381522SMike Smith {
254e51b0386SMike Smith 	u_int unit = minor(dev);
2550f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
2560f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
2570f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2586f34dba9SMike Smith 	int res;
259e51b0386SMike Smith 
2600f210c92SNicolas Souchu 	if (!ppi)
261e51b0386SMike Smith 		return (ENXIO);
262e51b0386SMike Smith 
263bc35c174SNicolas Souchu 	if (!(ppi->ppi_flags & HAVE_PPBUS)) {
2640f210c92SNicolas Souchu 		if ((res = ppb_request_bus(ppbus, ppidev,
265bc35c174SNicolas Souchu 			(flags & O_NONBLOCK) ? PPB_DONTWAIT :
266bc35c174SNicolas Souchu 						(PPB_WAIT | PPB_INTR))))
2676f34dba9SMike Smith 			return (res);
268e51b0386SMike Smith 
2696f34dba9SMike Smith 		ppi->ppi_flags |= HAVE_PPBUS;
2700f210c92SNicolas Souchu 
271c47eb96cSNick Hibma #ifdef PERIPH_1284
272c47eb96cSNick Hibma 		if (ppi->intr_resource) {
2730f210c92SNicolas Souchu 			/* register our interrupt handler */
2740f210c92SNicolas Souchu 			BUS_SETUP_INTR(device_get_parent(ppidev), ppidev, ppi->intr_resource,
2750f210c92SNicolas Souchu 				       INTR_TYPE_TTY, ppiintr, dev, &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
285b40ce416SJulian Elischer ppiclose(dev_t dev, int flags, int fmt, struct thread *td)
286ed381522SMike Smith {
2876f34dba9SMike Smith 	u_int unit = minor(dev);
2880f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
2890f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
2900f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2916f34dba9SMike Smith 
292bc35c174SNicolas Souchu 	ppi->ppi_count --;
293bc35c174SNicolas Souchu 	if (!ppi->ppi_count) {
294bc35c174SNicolas Souchu 
295bc35c174SNicolas Souchu #ifdef PERIPH_1284
2960f210c92SNicolas Souchu 		switch (ppb_1284_get_state(ppbus)) {
297bc35c174SNicolas Souchu 		case PPB_PERIPHERAL_IDLE:
2980f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, 0);
299bc35c174SNicolas Souchu 			break;
300bc35c174SNicolas Souchu 		case PPB_REVERSE_IDLE:
301bc35c174SNicolas Souchu 		case PPB_EPP_IDLE:
302bc35c174SNicolas Souchu 		case PPB_ECP_FORWARD_IDLE:
303bc35c174SNicolas Souchu 		default:
3040f210c92SNicolas Souchu 			ppb_1284_terminate(ppbus);
305bc35c174SNicolas Souchu 			break;
306bc35c174SNicolas Souchu 		}
307bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
308bc35c174SNicolas Souchu 
3090f210c92SNicolas Souchu 		/* unregistration of interrupt forced by release */
3100f210c92SNicolas Souchu 		ppb_release_bus(ppbus, ppidev);
3110f210c92SNicolas Souchu 
3126f34dba9SMike Smith 		ppi->ppi_flags &= ~HAVE_PPBUS;
313bc35c174SNicolas Souchu 	}
314bc35c174SNicolas Souchu 
3156f34dba9SMike Smith 	return (0);
316ed381522SMike Smith }
317ed381522SMike Smith 
318bc35c174SNicolas Souchu /*
319bc35c174SNicolas Souchu  * ppiread()
320bc35c174SNicolas Souchu  *
321bc35c174SNicolas Souchu  * IEEE1284 compliant read.
322bc35c174SNicolas Souchu  *
323d64ada50SJens Schweikhardt  * First, try negotiation to BYTE then NIBBLE mode
324bc35c174SNicolas Souchu  * If no data is available, wait for it otherwise transfer as much as possible
325bc35c174SNicolas Souchu  */
326bc35c174SNicolas Souchu static int
327bc35c174SNicolas Souchu ppiread(dev_t dev, struct uio *uio, int ioflag)
328bc35c174SNicolas Souchu {
329bc35c174SNicolas Souchu #ifdef PERIPH_1284
330bc35c174SNicolas Souchu 	u_int unit = minor(dev);
3310f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
3320f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
3330f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
334bc35c174SNicolas Souchu 	int len, error = 0;
335bc35c174SNicolas Souchu 
3360f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
337bc35c174SNicolas Souchu 	case PPB_PERIPHERAL_IDLE:
3380f210c92SNicolas Souchu 		ppb_peripheral_terminate(ppbus, 0);
33993b0017fSPhilippe Charnier 		/* FALLTHROUGH */
340bc35c174SNicolas Souchu 
341bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
342d64ada50SJens Schweikhardt 		/* if can't negotiate NIBBLE mode then try BYTE mode,
343bc35c174SNicolas Souchu 		 * the peripheral may be a computer
344bc35c174SNicolas Souchu 		 */
3450f210c92SNicolas Souchu 		if ((ppb_1284_negociate(ppbus,
346bc35c174SNicolas Souchu 			ppi->ppi_mode = PPB_NIBBLE, 0))) {
347bc35c174SNicolas Souchu 
348bc35c174SNicolas Souchu 			/* XXX Wait 2 seconds to let the remote host some
349bc35c174SNicolas Souchu 			 * time to terminate its interrupt
350bc35c174SNicolas Souchu 			 */
351bc35c174SNicolas Souchu 			tsleep(ppi, PPBPRI, "ppiread", 2*hz);
352bc35c174SNicolas Souchu 
3530f210c92SNicolas Souchu 			if ((error = ppb_1284_negociate(ppbus,
354bc35c174SNicolas Souchu 				ppi->ppi_mode = PPB_BYTE, 0)))
355bc35c174SNicolas Souchu 				return (error);
356bc35c174SNicolas Souchu 		}
357bc35c174SNicolas Souchu 		break;
358bc35c174SNicolas Souchu 
359bc35c174SNicolas Souchu 	case PPB_REVERSE_IDLE:
360bc35c174SNicolas Souchu 	case PPB_EPP_IDLE:
361bc35c174SNicolas Souchu 	case PPB_ECP_FORWARD_IDLE:
362bc35c174SNicolas Souchu 	default:
363bc35c174SNicolas Souchu 		break;
364bc35c174SNicolas Souchu 	}
365bc35c174SNicolas Souchu 
366bc35c174SNicolas Souchu #ifdef DEBUG_1284
367bc35c174SNicolas Souchu 	printf("N");
368bc35c174SNicolas Souchu #endif
369bc35c174SNicolas Souchu 	/* read data */
370bc35c174SNicolas Souchu 	len = 0;
371bc35c174SNicolas Souchu 	while (uio->uio_resid) {
3720f210c92SNicolas Souchu 		if ((error = ppb_1284_read(ppbus, ppi->ppi_mode,
373bc35c174SNicolas Souchu 			ppi->ppi_buffer, min(BUFSIZE, uio->uio_resid),
374bc35c174SNicolas Souchu 			&len))) {
375bc35c174SNicolas Souchu 			goto error;
376bc35c174SNicolas Souchu 		}
377bc35c174SNicolas Souchu 
378bc35c174SNicolas Souchu 		if (!len)
379bc35c174SNicolas Souchu 			goto error;		/* no more data */
380bc35c174SNicolas Souchu 
381bc35c174SNicolas Souchu #ifdef DEBUG_1284
382bc35c174SNicolas Souchu 		printf("d");
383bc35c174SNicolas Souchu #endif
384bc35c174SNicolas Souchu 		if ((error = uiomove(ppi->ppi_buffer, len, uio)))
385bc35c174SNicolas Souchu 			goto error;
386bc35c174SNicolas Souchu 	}
387bc35c174SNicolas Souchu 
388bc35c174SNicolas Souchu error:
389bc35c174SNicolas Souchu 
390bc35c174SNicolas Souchu #else /* PERIPH_1284 */
391bc35c174SNicolas Souchu 	int error = ENODEV;
392bc35c174SNicolas Souchu #endif
393bc35c174SNicolas Souchu 
394bc35c174SNicolas Souchu 	return (error);
395bc35c174SNicolas Souchu }
396bc35c174SNicolas Souchu 
397bc35c174SNicolas Souchu /*
398bc35c174SNicolas Souchu  * ppiwrite()
399bc35c174SNicolas Souchu  *
400bc35c174SNicolas Souchu  * IEEE1284 compliant write
401bc35c174SNicolas Souchu  *
402bc35c174SNicolas Souchu  * Actually, this is the peripheral side of a remote IEEE1284 read
403bc35c174SNicolas Souchu  *
404d64ada50SJens Schweikhardt  * The first part of the negotiation (IEEE1284 device detection) is
405bc35c174SNicolas Souchu  * done at interrupt level, then the remaining is done by the writing
406bc35c174SNicolas Souchu  * process
407bc35c174SNicolas Souchu  *
408d64ada50SJens Schweikhardt  * Once negotiation done, transfer data
409bc35c174SNicolas Souchu  */
410bc35c174SNicolas Souchu static int
411bc35c174SNicolas Souchu ppiwrite(dev_t dev, struct uio *uio, int ioflag)
412bc35c174SNicolas Souchu {
413bc35c174SNicolas Souchu #ifdef PERIPH_1284
414bc35c174SNicolas Souchu 	u_int unit = minor(dev);
4150f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
4160f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
4170f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
418bc35c174SNicolas Souchu 	int len, error = 0, sent;
419bc35c174SNicolas Souchu 
420bc35c174SNicolas Souchu #if 0
421bc35c174SNicolas Souchu 	int ret;
422bc35c174SNicolas Souchu 
423bc35c174SNicolas Souchu 	#define ADDRESS		MS_PARAM(0, 0, MS_TYP_PTR)
424bc35c174SNicolas Souchu 	#define LENGTH		MS_PARAM(0, 1, MS_TYP_INT)
425bc35c174SNicolas Souchu 
426bc35c174SNicolas Souchu 	struct ppb_microseq msq[] = {
427bc35c174SNicolas Souchu 		  { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
428bc35c174SNicolas Souchu 		  MS_RET(0)
429bc35c174SNicolas Souchu 	};
430bc35c174SNicolas Souchu 
431d64ada50SJens Schweikhardt 	/* negotiate ECP mode */
4320f210c92SNicolas Souchu 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
433d64ada50SJens Schweikhardt 		printf("ppiwrite: ECP negotiation failed\n");
434bc35c174SNicolas Souchu 	}
435bc35c174SNicolas Souchu 
436bc35c174SNicolas Souchu 	while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
437bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
438bc35c174SNicolas Souchu 
439bc35c174SNicolas Souchu 		ppb_MS_init_msq(msq, 2, ADDRESS, ppi->ppi_buffer, LENGTH, len);
440bc35c174SNicolas Souchu 
4410f210c92SNicolas Souchu 		error = ppb_MS_microseq(ppbus, msq, &ret);
442bc35c174SNicolas Souchu 	}
443bc35c174SNicolas Souchu #endif
444bc35c174SNicolas Souchu 
445bc35c174SNicolas Souchu 	/* we have to be peripheral to be able to send data, so
446bc35c174SNicolas Souchu 	 * wait for the appropriate state
447bc35c174SNicolas Souchu 	 */
448bdcee5cfSNicolas Souchu  	if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
4490f210c92SNicolas Souchu 		ppb_1284_terminate(ppbus);
450bc35c174SNicolas Souchu 
451bdcee5cfSNicolas Souchu  	while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
452bc35c174SNicolas Souchu 		/* XXX should check a variable before sleeping */
453bc35c174SNicolas Souchu #ifdef DEBUG_1284
454bc35c174SNicolas Souchu 		printf("s");
455bc35c174SNicolas Souchu #endif
456bc35c174SNicolas Souchu 
4570f210c92SNicolas Souchu 		ppi_enable_intr(ppidev);
458bc35c174SNicolas Souchu 
459d64ada50SJens Schweikhardt 		/* sleep until IEEE1284 negotiation starts */
460bc35c174SNicolas Souchu 		error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
461bc35c174SNicolas Souchu 
462bc35c174SNicolas Souchu 		switch (error) {
463bc35c174SNicolas Souchu 		case 0:
464d64ada50SJens Schweikhardt 			/* negotiate peripheral side with BYTE mode */
4650f210c92SNicolas Souchu 			ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
466bc35c174SNicolas Souchu 			break;
467bc35c174SNicolas Souchu 		case EWOULDBLOCK:
468bc35c174SNicolas Souchu 			break;
469bc35c174SNicolas Souchu 		default:
470bc35c174SNicolas Souchu 			goto error;
471bc35c174SNicolas Souchu 		}
472bc35c174SNicolas Souchu 	}
473bc35c174SNicolas Souchu #ifdef DEBUG_1284
474bc35c174SNicolas Souchu 	printf("N");
475bc35c174SNicolas Souchu #endif
476bc35c174SNicolas Souchu 
477d64ada50SJens Schweikhardt 	/* negotiation done, write bytes to master host */
478d254af07SMatthew Dillon 	while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
479bc35c174SNicolas Souchu 		uiomove(ppi->ppi_buffer, len, uio);
4800f210c92SNicolas Souchu 		if ((error = byte_peripheral_write(ppbus,
481bc35c174SNicolas Souchu 						ppi->ppi_buffer, len, &sent)))
482bc35c174SNicolas Souchu 			goto error;
483bc35c174SNicolas Souchu #ifdef DEBUG_1284
484bc35c174SNicolas Souchu 		printf("d");
485bc35c174SNicolas Souchu #endif
486bc35c174SNicolas Souchu 	}
487bc35c174SNicolas Souchu 
488bc35c174SNicolas Souchu error:
489bc35c174SNicolas Souchu 
490bc35c174SNicolas Souchu #else /* PERIPH_1284 */
491bc35c174SNicolas Souchu 	int error = ENODEV;
492bc35c174SNicolas Souchu #endif
493bc35c174SNicolas Souchu 
494bc35c174SNicolas Souchu 	return (error);
495bc35c174SNicolas Souchu }
496bc35c174SNicolas Souchu 
497ed381522SMike Smith static int
498b40ce416SJulian Elischer ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
499ed381522SMike Smith {
5006f34dba9SMike Smith 	u_int unit = minor(dev);
5010f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
5020f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
5036f34dba9SMike Smith 	int error = 0;
5046f34dba9SMike Smith 	u_int8_t *val = (u_int8_t *)data;
5056f34dba9SMike Smith 
5066f34dba9SMike Smith 	switch (cmd) {
5076f34dba9SMike Smith 
5086f34dba9SMike Smith 	case PPIGDATA:			/* get data register */
5090f210c92SNicolas Souchu 		*val = ppb_rdtr(ppbus);
5106f34dba9SMike Smith 		break;
5116f34dba9SMike Smith 	case PPIGSTATUS:		/* get status bits */
5120f210c92SNicolas Souchu 		*val = ppb_rstr(ppbus);
5136f34dba9SMike Smith 		break;
5146f34dba9SMike Smith 	case PPIGCTRL:			/* get control bits */
5150f210c92SNicolas Souchu 		*val = ppb_rctr(ppbus);
5166f34dba9SMike Smith 		break;
51720240fa3SNicolas Souchu 	case PPIGEPPD:			/* get EPP data bits */
5180f210c92SNicolas Souchu 		*val = ppb_repp_D(ppbus);
5196f34dba9SMike Smith 		break;
5206f34dba9SMike Smith 	case PPIGECR:			/* get ECP bits */
5210f210c92SNicolas Souchu 		*val = ppb_recr(ppbus);
5226f34dba9SMike Smith 		break;
5236f34dba9SMike Smith 	case PPIGFIFO:			/* read FIFO */
5240f210c92SNicolas Souchu 		*val = ppb_rfifo(ppbus);
5256f34dba9SMike Smith 		break;
5266f34dba9SMike Smith 	case PPISDATA:			/* set data register */
5270f210c92SNicolas Souchu 		ppb_wdtr(ppbus, *val);
5286f34dba9SMike Smith 		break;
5296f34dba9SMike Smith 	case PPISSTATUS:		/* set status bits */
5300f210c92SNicolas Souchu 		ppb_wstr(ppbus, *val);
5316f34dba9SMike Smith 		break;
5326f34dba9SMike Smith 	case PPISCTRL:			/* set control bits */
5330f210c92SNicolas Souchu 		ppb_wctr(ppbus, *val);
5346f34dba9SMike Smith 		break;
53520240fa3SNicolas Souchu 	case PPISEPPD:			/* set EPP data bits */
5360f210c92SNicolas Souchu 		ppb_wepp_D(ppbus, *val);
5376f34dba9SMike Smith 		break;
5386f34dba9SMike Smith 	case PPISECR:			/* set ECP bits */
5390f210c92SNicolas Souchu 		ppb_wecr(ppbus, *val);
5406f34dba9SMike Smith 		break;
5416f34dba9SMike Smith 	case PPISFIFO:			/* write FIFO */
5420f210c92SNicolas Souchu 		ppb_wfifo(ppbus, *val);
5436f34dba9SMike Smith 		break;
54420240fa3SNicolas Souchu 	case PPIGEPPA:			/* get EPP address bits */
5450f210c92SNicolas Souchu 		*val = ppb_repp_A(ppbus);
54620240fa3SNicolas Souchu 		break;
54720240fa3SNicolas Souchu 	case PPISEPPA:			/* set EPP address bits */
5480f210c92SNicolas Souchu 		ppb_wepp_A(ppbus, *val);
54920240fa3SNicolas Souchu 		break;
5506f34dba9SMike Smith 	default:
5516f34dba9SMike Smith 		error = ENOTTY;
5526f34dba9SMike Smith 		break;
5536f34dba9SMike Smith 	}
5546f34dba9SMike Smith 
5556f34dba9SMike Smith 	return (error);
556ed381522SMike Smith }
557ed381522SMike Smith 
5580f063508SPeter Wemm static device_method_t ppi_methods[] = {
5590f063508SPeter Wemm 	/* device interface */
5600f063508SPeter Wemm 	DEVMETHOD(device_identify,	ppi_identify),
5610f063508SPeter Wemm 	DEVMETHOD(device_probe,		ppi_probe),
5620f063508SPeter Wemm 	DEVMETHOD(device_attach,	ppi_attach),
5630f210c92SNicolas Souchu 
5640f063508SPeter Wemm 	{ 0, 0 }
5650f063508SPeter Wemm };
5660f063508SPeter Wemm 
5670f063508SPeter Wemm static driver_t ppi_driver = {
5680f063508SPeter Wemm 	"ppi",
5690f063508SPeter Wemm 	ppi_methods,
5700f063508SPeter Wemm 	sizeof(struct ppi_data),
5710f063508SPeter Wemm };
5720f063508SPeter Wemm DRIVER_MODULE(ppi, ppbus, ppi_driver, ppi_devclass, 0, 0);
573