xref: /freebsd/sys/dev/ppbus/ppi.c (revision 0f063508d7212aacd287ee0f19a6a1a2f9153fe7)
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 
40bc35c174SNicolas Souchu #include <machine/clock.h>
410f210c92SNicolas Souchu #include <machine/bus.h>
420f210c92SNicolas Souchu #include <machine/resource.h>
430f210c92SNicolas Souchu #include <sys/rman.h>
44bc35c174SNicolas Souchu 
45ed381522SMike Smith #include <dev/ppbus/ppbconf.h>
46bc35c174SNicolas Souchu #include <dev/ppbus/ppb_msq.h>
47bc35c174SNicolas Souchu 
48bc35c174SNicolas Souchu #ifdef PERIPH_1284
49bc35c174SNicolas Souchu #include <dev/ppbus/ppb_1284.h>
50bc35c174SNicolas Souchu #endif
51bc35c174SNicolas Souchu 
526f34dba9SMike Smith #include <dev/ppbus/ppi.h>
536f34dba9SMike Smith 
540f210c92SNicolas Souchu #include "ppbus_if.h"
550f210c92SNicolas Souchu 
560f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
570f210c92SNicolas Souchu 
58bc35c174SNicolas Souchu #define BUFSIZE		512
59ed381522SMike Smith 
60e51b0386SMike Smith struct ppi_data {
61e51b0386SMike Smith 
62e51b0386SMike Smith     int		ppi_unit;
636f34dba9SMike Smith     int		ppi_flags;
646f34dba9SMike Smith #define HAVE_PPBUS	(1<<0)
65bc35c174SNicolas Souchu #define HAD_PPBUS	(1<<1)
66bc35c174SNicolas Souchu 
67bc35c174SNicolas Souchu     int		ppi_count;
68bc35c174SNicolas Souchu     int		ppi_mode;			/* IEEE1284 mode */
69bc35c174SNicolas Souchu     char	ppi_buffer[BUFSIZE];
70e51b0386SMike Smith 
710f210c92SNicolas Souchu     struct resource *intr_resource;	/* interrupt resource */
720f210c92SNicolas Souchu     void *intr_cookie;			/* interrupt registration cookie */
73e51b0386SMike Smith };
74e51b0386SMike Smith 
750f210c92SNicolas Souchu #define DEVTOSOFTC(dev) \
760f210c92SNicolas Souchu 	((struct ppi_data *)device_get_softc(dev))
770f210c92SNicolas Souchu #define UNITOSOFTC(unit) \
780f210c92SNicolas Souchu 	((struct ppi_data *)devclass_get_softc(ppi_devclass, (unit)))
790f210c92SNicolas Souchu #define UNITODEVICE(unit) \
800f210c92SNicolas Souchu 	(devclass_get_device(ppi_devclass, (unit)))
81ed381522SMike Smith 
820f210c92SNicolas Souchu static devclass_t ppi_devclass;
83ed381522SMike Smith 
84ed381522SMike Smith static	d_open_t	ppiopen;
85ed381522SMike Smith static	d_close_t	ppiclose;
86ed381522SMike Smith static	d_ioctl_t	ppiioctl;
87bc35c174SNicolas Souchu static	d_write_t	ppiwrite;
88bc35c174SNicolas Souchu static	d_read_t	ppiread;
89ed381522SMike Smith 
904f013cf5SMike Smith #define CDEV_MAJOR 82
914e2f199eSPoul-Henning Kamp static struct cdevsw ppi_cdevsw = {
924e2f199eSPoul-Henning Kamp 	/* open */	ppiopen,
934e2f199eSPoul-Henning Kamp 	/* close */	ppiclose,
944e2f199eSPoul-Henning Kamp 	/* read */	ppiread,
954e2f199eSPoul-Henning Kamp 	/* write */	ppiwrite,
964e2f199eSPoul-Henning Kamp 	/* ioctl */	ppiioctl,
974e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
984e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
994e2f199eSPoul-Henning Kamp 	/* strategy */	nostrategy,
1004e2f199eSPoul-Henning Kamp 	/* name */	"ppi",
1014e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
1024e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1034e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1044e2f199eSPoul-Henning Kamp 	/* flags */	0,
1054e2f199eSPoul-Henning Kamp 	/* bmaj */	-1
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 
1400f063508SPeter Wemm 	BUS_ADD_CHILD(parent, 0, "ppi", 0);
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 {
1660f210c92SNicolas Souchu 	uintptr_t irq;
1670f210c92SNicolas Souchu 	int zero = 0;
1680f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(dev);
169ed381522SMike Smith 
1700f210c92SNicolas Souchu 	/* retrive the irq */
1710f210c92SNicolas Souchu 	BUS_READ_IVAR(device_get_parent(dev), dev, PPBUS_IVAR_IRQ, &irq);
1720f210c92SNicolas Souchu 
1730f210c92SNicolas Souchu 	/* declare our interrupt handler */
1740f210c92SNicolas Souchu 	ppi->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ,
1750f210c92SNicolas Souchu 						&zero, irq, irq, 1, RF_ACTIVE);
1760f210c92SNicolas Souchu 
1770f210c92SNicolas Souchu 	make_dev(&ppi_cdevsw, device_get_unit(dev),	/* XXX cleanup */
1780f210c92SNicolas Souchu 		 UID_ROOT, GID_WHEEL,
1790f210c92SNicolas Souchu 		 0600, "ppi%d", device_get_unit(dev));
1800f210c92SNicolas Souchu 
1810f210c92SNicolas Souchu 	return (0);
182ed381522SMike Smith }
183ed381522SMike Smith 
184bc35c174SNicolas Souchu /*
185bc35c174SNicolas Souchu  * Cable
186bc35c174SNicolas Souchu  * -----
187bc35c174SNicolas Souchu  *
188bc35c174SNicolas Souchu  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
189bc35c174SNicolas Souchu  *
190bc35c174SNicolas Souchu  * nStrobe   <-> nAck		1  <-> 10
191bc35c174SNicolas Souchu  * nAutofd   <-> Busy		11 <-> 14
192bc35c174SNicolas Souchu  * nSelectin <-> Select		17 <-> 13
193bc35c174SNicolas Souchu  * nInit     <-> nFault		15 <-> 16
194bc35c174SNicolas Souchu  *
195bc35c174SNicolas Souchu  */
196ed381522SMike Smith static void
1970f210c92SNicolas Souchu ppiintr(void *arg)
198ed381522SMike Smith {
199bc35c174SNicolas Souchu #ifdef PERIPH_1284
2000f210c92SNicolas Souchu 	device_t ppidev = (device_t)arg;
2010f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2020f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(ppidev);
203bc35c174SNicolas Souchu 
2040f210c92SNicolas Souchu 	ppi_disable_intr(ppidev);
205bc35c174SNicolas Souchu 
2060f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
207bc35c174SNicolas Souchu 
208bc35c174SNicolas Souchu 	/* accept IEEE1284 negociation then wakeup an waiting process to
209bc35c174SNicolas Souchu 	 * continue negociation at process level */
210bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
211bc35c174SNicolas Souchu 		/* Event 1 */
2120f210c92SNicolas Souchu 		if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
213bc35c174SNicolas Souchu 							(SELECT | nBUSY)) {
214bc35c174SNicolas Souchu 			/* IEEE1284 negociation */
215bc35c174SNicolas Souchu #ifdef DEBUG_1284
216bc35c174SNicolas Souchu 			printf("N");
217bc35c174SNicolas Souchu #endif
218bc35c174SNicolas Souchu 
219bc35c174SNicolas Souchu 			/* Event 2 - prepare for reading the ext. value */
2200f210c92SNicolas Souchu 			ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
221bc35c174SNicolas Souchu 
2220f210c92SNicolas Souchu 			ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
223bc35c174SNicolas Souchu 
224bc35c174SNicolas Souchu 		} else {
225bc35c174SNicolas Souchu #ifdef DEBUG_1284
2260f210c92SNicolas Souchu 			printf("0x%x", ppb_rstr(ppbus));
227bc35c174SNicolas Souchu #endif
2280f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
229bc35c174SNicolas Souchu 			break;
230bc35c174SNicolas Souchu 		}
231bc35c174SNicolas Souchu 
232bc35c174SNicolas Souchu 		/* wake up any process waiting for negociation from
233bc35c174SNicolas Souchu 		 * remote master host */
234bc35c174SNicolas Souchu 
235bc35c174SNicolas Souchu 		/* XXX should set a variable to warn the process about
236bc35c174SNicolas Souchu 		 * the interrupt */
237bc35c174SNicolas Souchu 
238bc35c174SNicolas Souchu 		wakeup(ppi);
239bc35c174SNicolas Souchu 		break;
240bc35c174SNicolas Souchu 	default:
241bc35c174SNicolas Souchu #ifdef DEBUG_1284
24299904c75SPeter Wemm 		printf("?%d", ppb_1284_get_state(ppbus));
243bc35c174SNicolas Souchu #endif
2440f210c92SNicolas Souchu 		ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
2450f210c92SNicolas Souchu 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
246bc35c174SNicolas Souchu 		break;
247bc35c174SNicolas Souchu 	}
248bc35c174SNicolas Souchu 
2490f210c92SNicolas Souchu 	ppi_enable_intr(ppidev);
250bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
251bc35c174SNicolas Souchu 
252ed381522SMike Smith 	return;
253ed381522SMike Smith }
254ed381522SMike Smith 
255ed381522SMike Smith static int
256ed381522SMike Smith ppiopen(dev_t dev, int flags, int fmt, struct proc *p)
257ed381522SMike Smith {
258e51b0386SMike Smith 	u_int unit = minor(dev);
2590f210c92SNicolas Souchu 	struct ppi_data *ppi = UNITOSOFTC(unit);
2600f210c92SNicolas Souchu 	device_t ppidev = UNITODEVICE(unit);
2610f210c92SNicolas Souchu         device_t ppbus = device_get_parent(ppidev);
2626f34dba9SMike Smith 	int res;
263e51b0386SMike Smith 
2640f210c92SNicolas Souchu 	if (!ppi)
265e51b0386SMike Smith 		return (ENXIO);
266e51b0386SMike Smith 
267bc35c174SNicolas Souchu 	if (!(ppi->ppi_flags & HAVE_PPBUS)) {
2680f210c92SNicolas Souchu 		if ((res = ppb_request_bus(ppbus, ppidev,
269bc35c174SNicolas Souchu 			(flags & O_NONBLOCK) ? PPB_DONTWAIT :
270bc35c174SNicolas Souchu 						(PPB_WAIT | PPB_INTR))))
2716f34dba9SMike Smith 			return (res);
272e51b0386SMike Smith 
2736f34dba9SMike Smith 		ppi->ppi_flags |= HAVE_PPBUS;
2740f210c92SNicolas Souchu 
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 	}
279bc35c174SNicolas Souchu 	ppi->ppi_count += 1;
280bc35c174SNicolas Souchu 
2816f34dba9SMike Smith 	return (0);
282ed381522SMike Smith }
283ed381522SMike Smith 
284ed381522SMike Smith static int
285ed381522SMike Smith ppiclose(dev_t dev, int flags, int fmt, struct proc *p)
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  *
323bc35c174SNicolas Souchu  * First, try negociation 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);
339bc35c174SNicolas Souchu 		/* fall throught */
340bc35c174SNicolas Souchu 
341bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
342bc35c174SNicolas Souchu 		/* if can't negociate 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  *
404bc35c174SNicolas Souchu  * The first part of the negociation (IEEE1284 device detection) is
405bc35c174SNicolas Souchu  * done at interrupt level, then the remaining is done by the writing
406bc35c174SNicolas Souchu  * process
407bc35c174SNicolas Souchu  *
408bc35c174SNicolas Souchu  * Once negociation 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 
431bc35c174SNicolas Souchu 	/* negociate ECP mode */
4320f210c92SNicolas Souchu 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
433bc35c174SNicolas Souchu 		printf("ppiwrite: ECP negociation 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 
459bc35c174SNicolas Souchu 		/* sleep until IEEE1284 negociation starts */
460bc35c174SNicolas Souchu 		error = tsleep(ppi, PCATCH | PPBPRI, "ppiwrite", 0);
461bc35c174SNicolas Souchu 
462bc35c174SNicolas Souchu 		switch (error) {
463bc35c174SNicolas Souchu 		case 0:
464bc35c174SNicolas Souchu 			/* negociate 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 
477bc35c174SNicolas Souchu 	/* negociation 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
498ecbb00a2SDoug Rabson ppiioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
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