xref: /freebsd/sys/dev/ppbus/ppi.c (revision 685dc743)
1ed381522SMike Smith /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
40f210c92SNicolas Souchu  * Copyright (c) 1997, 1998, 1999 Nicolas Souchu, Michael Smith
5ed381522SMike Smith  * All rights reserved.
6ed381522SMike Smith  *
7ed381522SMike Smith  * Redistribution and use in source and binary forms, with or without
8ed381522SMike Smith  * modification, are permitted provided that the following conditions
9ed381522SMike Smith  * are met:
10ed381522SMike Smith  * 1. Redistributions of source code must retain the above copyright
11ed381522SMike Smith  *    notice, this list of conditions and the following disclaimer.
12ed381522SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
13ed381522SMike Smith  *    notice, this list of conditions and the following disclaimer in the
14ed381522SMike Smith  *    documentation and/or other materials provided with the distribution.
15ed381522SMike Smith  *
16ed381522SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ed381522SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ed381522SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ed381522SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ed381522SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ed381522SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ed381522SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ed381522SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ed381522SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ed381522SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ed381522SMike Smith  * SUCH DAMAGE.
27ed381522SMike Smith  *
28ed381522SMike Smith  *
29ed381522SMike Smith  */
30aad970f1SDavid E. O'Brien 
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
320f210c92SNicolas Souchu #include "opt_ppb_1284.h"
330f210c92SNicolas Souchu 
34ed381522SMike Smith #include <sys/param.h>
35ed381522SMike Smith #include <sys/systm.h>
360f210c92SNicolas Souchu #include <sys/module.h>
370f210c92SNicolas Souchu #include <sys/bus.h>
38ed381522SMike Smith #include <sys/conf.h>
39ed381522SMike Smith #include <sys/kernel.h>
402067d312SJohn Baldwin #include <sys/lock.h>
412067d312SJohn Baldwin #include <sys/sx.h>
42bc35c174SNicolas Souchu #include <sys/uio.h>
436f34dba9SMike Smith #include <sys/fcntl.h>
44ed381522SMike Smith 
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
532067d312SJohn Baldwin #include <sys/malloc.h>
54bc35c174SNicolas Souchu #include <dev/ppbus/ppb_1284.h>
55bc35c174SNicolas Souchu #endif
56bc35c174SNicolas Souchu 
576f34dba9SMike Smith #include <dev/ppbus/ppi.h>
586f34dba9SMike Smith 
590f210c92SNicolas Souchu #include "ppbus_if.h"
600f210c92SNicolas Souchu 
610f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
620f210c92SNicolas Souchu 
63bc35c174SNicolas Souchu #define BUFSIZE		512
64ed381522SMike Smith 
65e51b0386SMike Smith struct ppi_data {
66ae6b868aSJohn Baldwin     device_t	ppi_device;
67ae6b868aSJohn Baldwin     struct cdev *ppi_cdev;
682067d312SJohn Baldwin     struct sx	ppi_lock;
696f34dba9SMike Smith     int		ppi_flags;
706f34dba9SMike Smith #define HAVE_PPBUS	(1<<0)
71bc35c174SNicolas Souchu 
72bc35c174SNicolas Souchu     int		ppi_mode;			/* IEEE1284 mode */
73bc35c174SNicolas Souchu     char	ppi_buffer[BUFSIZE];
74e51b0386SMike Smith 
75c47eb96cSNick Hibma #ifdef PERIPH_1284
760f210c92SNicolas Souchu     struct resource *intr_resource;	/* interrupt resource */
770f210c92SNicolas Souchu     void *intr_cookie;			/* interrupt registration cookie */
78c47eb96cSNick Hibma #endif /* PERIPH_1284 */
79e51b0386SMike Smith };
80e51b0386SMike Smith 
810f210c92SNicolas Souchu #define DEVTOSOFTC(dev) \
820f210c92SNicolas Souchu 	((struct ppi_data *)device_get_softc(dev))
83ed381522SMike Smith 
842067d312SJohn Baldwin #ifdef PERIPH_1284
852067d312SJohn Baldwin static void	ppiintr(void *arg);
862067d312SJohn Baldwin #endif
872067d312SJohn Baldwin 
88ed381522SMike Smith static	d_open_t	ppiopen;
89ed381522SMike Smith static	d_close_t	ppiclose;
90ed381522SMike Smith static	d_ioctl_t	ppiioctl;
91bc35c174SNicolas Souchu static	d_write_t	ppiwrite;
92bc35c174SNicolas Souchu static	d_read_t	ppiread;
93ed381522SMike Smith 
944e2f199eSPoul-Henning Kamp static struct cdevsw ppi_cdevsw = {
95dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
967ac40f5fSPoul-Henning Kamp 	.d_open =	ppiopen,
977ac40f5fSPoul-Henning Kamp 	.d_close =	ppiclose,
987ac40f5fSPoul-Henning Kamp 	.d_read =	ppiread,
997ac40f5fSPoul-Henning Kamp 	.d_write =	ppiwrite,
1007ac40f5fSPoul-Henning Kamp 	.d_ioctl =	ppiioctl,
1017ac40f5fSPoul-Henning Kamp 	.d_name =	"ppi",
1024e2f199eSPoul-Henning Kamp };
103ed381522SMike Smith 
104bc35c174SNicolas Souchu #ifdef PERIPH_1284
105bc35c174SNicolas Souchu 
106bc35c174SNicolas Souchu static void
ppi_enable_intr(device_t ppidev)1070f210c92SNicolas Souchu ppi_enable_intr(device_t ppidev)
108bc35c174SNicolas Souchu {
109bc35c174SNicolas Souchu 	char r;
1100f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
111bc35c174SNicolas Souchu 
1120f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1130f210c92SNicolas Souchu 	ppb_wctr(ppbus, r | IRQENABLE);
114bc35c174SNicolas Souchu 
115bc35c174SNicolas Souchu 	return;
116bc35c174SNicolas Souchu }
117bc35c174SNicolas Souchu 
118bc35c174SNicolas Souchu static void
ppi_disable_intr(device_t ppidev)1190f210c92SNicolas Souchu ppi_disable_intr(device_t ppidev)
120bc35c174SNicolas Souchu {
121bc35c174SNicolas Souchu 	char r;
1220f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
123bc35c174SNicolas Souchu 
1240f210c92SNicolas Souchu 	r = ppb_rctr(ppbus);
1250f210c92SNicolas Souchu 	ppb_wctr(ppbus, r & ~IRQENABLE);
126bc35c174SNicolas Souchu 
127bc35c174SNicolas Souchu 	return;
128bc35c174SNicolas Souchu }
129bc35c174SNicolas Souchu 
130bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
131bc35c174SNicolas Souchu 
1320f063508SPeter Wemm static void
ppi_identify(driver_t * driver,device_t parent)1330f063508SPeter Wemm ppi_identify(driver_t *driver, device_t parent)
1340f063508SPeter Wemm {
1350f063508SPeter Wemm 
136a5c7e3bbSGuido van Rooij 	device_t dev;
137a5c7e3bbSGuido van Rooij 
13857fb5e60SJohn Baldwin 	dev = device_find_child(parent, "ppi", -1);
139a5c7e3bbSGuido van Rooij 	if (!dev)
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
ppi_probe(device_t dev)1470f210c92SNicolas Souchu ppi_probe(device_t dev)
148ed381522SMike Smith {
1490f210c92SNicolas Souchu 	/* probe is always ok */
1500f210c92SNicolas Souchu 	device_set_desc(dev, "Parallel I/O");
1510f210c92SNicolas Souchu 
1520f210c92SNicolas Souchu 	return (0);
153ed381522SMike Smith }
154ed381522SMike Smith 
155ed381522SMike Smith /*
1560f210c92SNicolas Souchu  * ppi_attach()
157ed381522SMike Smith  */
1580f210c92SNicolas Souchu static int
ppi_attach(device_t dev)1590f210c92SNicolas Souchu ppi_attach(device_t dev)
1600f210c92SNicolas Souchu {
161ae6b868aSJohn Baldwin 	struct ppi_data *ppi = DEVTOSOFTC(dev);
162c47eb96cSNick Hibma #ifdef PERIPH_1284
1632067d312SJohn Baldwin 	int error, rid = 0;
164ed381522SMike Smith 
1650f210c92SNicolas Souchu 	/* declare our interrupt handler */
166ca3d3795SJohn Baldwin 	ppi->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
167ca3d3795SJohn Baldwin 	    RF_ACTIVE);
1682067d312SJohn Baldwin 	if (ppi->intr_resource) {
1692067d312SJohn Baldwin 		/* register our interrupt handler */
1702067d312SJohn Baldwin 		error = bus_setup_intr(dev, ppi->intr_resource,
1712067d312SJohn Baldwin 		    INTR_TYPE_TTY | INTR_MPSAFE, NULL, ppiintr, dev,
1722067d312SJohn Baldwin 		    &ppi->intr_cookie);
1732067d312SJohn Baldwin 		if (error) {
1742067d312SJohn Baldwin 			bus_release_resource(dev, SYS_RES_IRQ, rid,
1752067d312SJohn Baldwin 			    ppi->intr_resource);
1762067d312SJohn Baldwin 			device_printf(dev,
1772067d312SJohn Baldwin 			    "Unable to register interrupt handler\n");
1782067d312SJohn Baldwin 			return (error);
1792067d312SJohn Baldwin 		}
1802067d312SJohn Baldwin 	}
181c47eb96cSNick Hibma #endif /* PERIPH_1284 */
1820f210c92SNicolas Souchu 
1832067d312SJohn Baldwin 	sx_init(&ppi->ppi_lock, "ppi");
184ae6b868aSJohn Baldwin 	ppi->ppi_cdev = make_dev(&ppi_cdevsw, device_get_unit(dev),
1850f210c92SNicolas Souchu 		 UID_ROOT, GID_WHEEL,
1860f210c92SNicolas Souchu 		 0600, "ppi%d", device_get_unit(dev));
187ae6b868aSJohn Baldwin 	if (ppi->ppi_cdev == NULL) {
188f32b2a16SJohn Baldwin 		device_printf(dev, "Failed to create character device\n");
189ae6b868aSJohn Baldwin 		return (ENXIO);
190ae6b868aSJohn Baldwin 	}
191ae6b868aSJohn Baldwin 	ppi->ppi_cdev->si_drv1 = ppi;
192ae6b868aSJohn Baldwin 	ppi->ppi_device = dev;
1930f210c92SNicolas Souchu 
1940f210c92SNicolas Souchu 	return (0);
195ed381522SMike Smith }
196ed381522SMike Smith 
1972067d312SJohn Baldwin static int
ppi_detach(device_t dev)1982067d312SJohn Baldwin ppi_detach(device_t dev)
1992067d312SJohn Baldwin {
2002067d312SJohn Baldwin 	struct ppi_data *ppi = DEVTOSOFTC(dev);
2012067d312SJohn Baldwin 
2022067d312SJohn Baldwin 	destroy_dev(ppi->ppi_cdev);
2032067d312SJohn Baldwin #ifdef PERIPH_1284
2042067d312SJohn Baldwin 	if (ppi->intr_resource != NULL) {
2052067d312SJohn Baldwin 		bus_teardown_intr(dev, ppi->intr_resource, ppi->intr_cookie);
2062067d312SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, 0, ppi->intr_resource);
2072067d312SJohn Baldwin 	}
2082067d312SJohn Baldwin #endif
2092067d312SJohn Baldwin 	sx_destroy(&ppi->ppi_lock);
2102067d312SJohn Baldwin 	return (0);
2112067d312SJohn Baldwin }
2122067d312SJohn Baldwin 
213c47eb96cSNick Hibma #ifdef PERIPH_1284
214bc35c174SNicolas Souchu /*
215bc35c174SNicolas Souchu  * Cable
216bc35c174SNicolas Souchu  * -----
217bc35c174SNicolas Souchu  *
218bc35c174SNicolas Souchu  * Use an IEEE1284 compliant (DB25/DB25) cable with the following tricks:
219bc35c174SNicolas Souchu  *
220bc35c174SNicolas Souchu  * nStrobe   <-> nAck		1  <-> 10
221bc35c174SNicolas Souchu  * nAutofd   <-> Busy		11 <-> 14
222bc35c174SNicolas Souchu  * nSelectin <-> Select		17 <-> 13
223bc35c174SNicolas Souchu  * nInit     <-> nFault		15 <-> 16
224bc35c174SNicolas Souchu  *
225bc35c174SNicolas Souchu  */
226ed381522SMike Smith static void
ppiintr(void * arg)2270f210c92SNicolas Souchu ppiintr(void *arg)
228ed381522SMike Smith {
2290f210c92SNicolas Souchu 	device_t ppidev = (device_t)arg;
2300f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
2310f210c92SNicolas Souchu 	struct ppi_data *ppi = DEVTOSOFTC(ppidev);
232bc35c174SNicolas Souchu 
2332067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
2340f210c92SNicolas Souchu 	ppi_disable_intr(ppidev);
235bc35c174SNicolas Souchu 
2360f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
237d64ada50SJens Schweikhardt 	/* accept IEEE1284 negotiation then wakeup a waiting process to
238d64ada50SJens Schweikhardt 	 * continue negotiation at process level */
239bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
240bc35c174SNicolas Souchu 		/* Event 1 */
2410f210c92SNicolas Souchu 		if ((ppb_rstr(ppbus) & (SELECT | nBUSY)) ==
242bc35c174SNicolas Souchu 							(SELECT | nBUSY)) {
243d64ada50SJens Schweikhardt 			/* IEEE1284 negotiation */
244bc35c174SNicolas Souchu #ifdef DEBUG_1284
245bc35c174SNicolas Souchu 			printf("N");
246bc35c174SNicolas Souchu #endif
247bc35c174SNicolas Souchu 
248bc35c174SNicolas Souchu 			/* Event 2 - prepare for reading the ext. value */
2490f210c92SNicolas Souchu 			ppb_wctr(ppbus, (PCD | STROBE | nINIT) & ~SELECTIN);
250bc35c174SNicolas Souchu 
2510f210c92SNicolas Souchu 			ppb_1284_set_state(ppbus, PPB_NEGOCIATION);
252bc35c174SNicolas Souchu 
253bc35c174SNicolas Souchu 		} else {
254bc35c174SNicolas Souchu #ifdef DEBUG_1284
2550f210c92SNicolas Souchu 			printf("0x%x", ppb_rstr(ppbus));
256bc35c174SNicolas Souchu #endif
2570f210c92SNicolas Souchu 			ppb_peripheral_terminate(ppbus, PPB_DONTWAIT);
258bc35c174SNicolas Souchu 			break;
259bc35c174SNicolas Souchu 		}
260bc35c174SNicolas Souchu 
261d64ada50SJens Schweikhardt 		/* wake up any process waiting for negotiation from
262bc35c174SNicolas Souchu 		 * remote master host */
263bc35c174SNicolas Souchu 
264bc35c174SNicolas Souchu 		/* XXX should set a variable to warn the process about
265bc35c174SNicolas Souchu 		 * the interrupt */
266bc35c174SNicolas Souchu 
267bc35c174SNicolas Souchu 		wakeup(ppi);
268bc35c174SNicolas Souchu 		break;
269bc35c174SNicolas Souchu 	default:
270bc35c174SNicolas Souchu #ifdef DEBUG_1284
27199904c75SPeter Wemm 		printf("?%d", ppb_1284_get_state(ppbus));
272bc35c174SNicolas Souchu #endif
2730f210c92SNicolas Souchu 		ppb_1284_set_state(ppbus, PPB_FORWARD_IDLE);
2740f210c92SNicolas Souchu 		ppb_set_mode(ppbus, PPB_COMPATIBLE);
275bc35c174SNicolas Souchu 		break;
276bc35c174SNicolas Souchu 	}
277bc35c174SNicolas Souchu 
2780f210c92SNicolas Souchu 	ppi_enable_intr(ppidev);
279bc35c174SNicolas Souchu 
280ed381522SMike Smith 	return;
281ed381522SMike Smith }
282c47eb96cSNick Hibma #endif /* PERIPH_1284 */
283ed381522SMike Smith 
284ed381522SMike Smith static int
ppiopen(struct cdev * dev,int flags,int fmt,struct thread * td)28589c9c53dSPoul-Henning Kamp ppiopen(struct cdev *dev, int flags, int fmt, struct thread *td)
286ed381522SMike Smith {
287ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
288ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
2890f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
2906f34dba9SMike Smith 	int res;
291e51b0386SMike Smith 
2922067d312SJohn Baldwin 	sx_xlock(&ppi->ppi_lock);
293bc35c174SNicolas Souchu 	if (!(ppi->ppi_flags & HAVE_PPBUS)) {
2942067d312SJohn Baldwin 		ppb_lock(ppbus);
2952067d312SJohn Baldwin 		res = ppb_request_bus(ppbus, ppidev,
2962067d312SJohn Baldwin 		    (flags & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT | PPB_INTR);
2972067d312SJohn Baldwin 		ppb_unlock(ppbus);
2982067d312SJohn Baldwin 		if (res) {
2992067d312SJohn Baldwin 			sx_xunlock(&ppi->ppi_lock);
3006f34dba9SMike Smith 			return (res);
3012067d312SJohn Baldwin 		}
302e51b0386SMike Smith 
3036f34dba9SMike Smith 		ppi->ppi_flags |= HAVE_PPBUS;
304bc35c174SNicolas Souchu 	}
3052067d312SJohn Baldwin 	sx_xunlock(&ppi->ppi_lock);
306bc35c174SNicolas Souchu 
3076f34dba9SMike Smith 	return (0);
308ed381522SMike Smith }
309ed381522SMike Smith 
310ed381522SMike Smith static int
ppiclose(struct cdev * dev,int flags,int fmt,struct thread * td)31189c9c53dSPoul-Henning Kamp ppiclose(struct cdev *dev, int flags, int fmt, struct thread *td)
312ed381522SMike Smith {
313ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
314ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
3150f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
3166f34dba9SMike Smith 
3172067d312SJohn Baldwin 	sx_xlock(&ppi->ppi_lock);
3182067d312SJohn Baldwin 	ppb_lock(ppbus);
319bc35c174SNicolas Souchu #ifdef PERIPH_1284
3200f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
321bc35c174SNicolas Souchu 	case PPB_PERIPHERAL_IDLE:
3220f210c92SNicolas Souchu 		ppb_peripheral_terminate(ppbus, 0);
323bc35c174SNicolas Souchu 		break;
324bc35c174SNicolas Souchu 	case PPB_REVERSE_IDLE:
325bc35c174SNicolas Souchu 	case PPB_EPP_IDLE:
326bc35c174SNicolas Souchu 	case PPB_ECP_FORWARD_IDLE:
327bc35c174SNicolas Souchu 	default:
3280f210c92SNicolas Souchu 		ppb_1284_terminate(ppbus);
329bc35c174SNicolas Souchu 		break;
330bc35c174SNicolas Souchu 	}
331bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
332bc35c174SNicolas Souchu 
3330f210c92SNicolas Souchu 	/* unregistration of interrupt forced by release */
3340f210c92SNicolas Souchu 	ppb_release_bus(ppbus, ppidev);
3352067d312SJohn Baldwin 	ppb_unlock(ppbus);
3360f210c92SNicolas Souchu 
3376f34dba9SMike Smith 	ppi->ppi_flags &= ~HAVE_PPBUS;
3382067d312SJohn Baldwin 	sx_xunlock(&ppi->ppi_lock);
339bc35c174SNicolas Souchu 
3406f34dba9SMike Smith 	return (0);
341ed381522SMike Smith }
342ed381522SMike Smith 
343bc35c174SNicolas Souchu /*
344bc35c174SNicolas Souchu  * ppiread()
345bc35c174SNicolas Souchu  *
346bc35c174SNicolas Souchu  * IEEE1284 compliant read.
347bc35c174SNicolas Souchu  *
348d64ada50SJens Schweikhardt  * First, try negotiation to BYTE then NIBBLE mode
349bc35c174SNicolas Souchu  * If no data is available, wait for it otherwise transfer as much as possible
350bc35c174SNicolas Souchu  */
351bc35c174SNicolas Souchu static int
ppiread(struct cdev * dev,struct uio * uio,int ioflag)35289c9c53dSPoul-Henning Kamp ppiread(struct cdev *dev, struct uio *uio, int ioflag)
353bc35c174SNicolas Souchu {
354bc35c174SNicolas Souchu #ifdef PERIPH_1284
355ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
356ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
3570f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
358bc35c174SNicolas Souchu 	int len, error = 0;
3592067d312SJohn Baldwin 	char *buffer;
360bc35c174SNicolas Souchu 
3612067d312SJohn Baldwin 	buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
3622067d312SJohn Baldwin 
3632067d312SJohn Baldwin 	ppb_lock(ppbus);
3640f210c92SNicolas Souchu 	switch (ppb_1284_get_state(ppbus)) {
365bc35c174SNicolas Souchu 	case PPB_PERIPHERAL_IDLE:
3660f210c92SNicolas Souchu 		ppb_peripheral_terminate(ppbus, 0);
36793b0017fSPhilippe Charnier 		/* FALLTHROUGH */
368bc35c174SNicolas Souchu 
369bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
370d64ada50SJens Schweikhardt 		/* if can't negotiate NIBBLE mode then try BYTE mode,
371bc35c174SNicolas Souchu 		 * the peripheral may be a computer
372bc35c174SNicolas Souchu 		 */
3730f210c92SNicolas Souchu 		if ((ppb_1284_negociate(ppbus,
374bc35c174SNicolas Souchu 			ppi->ppi_mode = PPB_NIBBLE, 0))) {
375bc35c174SNicolas Souchu 			/* XXX Wait 2 seconds to let the remote host some
376bc35c174SNicolas Souchu 			 * time to terminate its interrupt
377bc35c174SNicolas Souchu 			 */
3782067d312SJohn Baldwin 			ppb_sleep(ppbus, ppi, PPBPRI, "ppiread", 2 * hz);
379bc35c174SNicolas Souchu 
3800f210c92SNicolas Souchu 			if ((error = ppb_1284_negociate(ppbus,
3812067d312SJohn Baldwin 			    ppi->ppi_mode = PPB_BYTE, 0))) {
3822067d312SJohn Baldwin 				ppb_unlock(ppbus);
3832067d312SJohn Baldwin 				free(buffer, M_DEVBUF);
384bc35c174SNicolas Souchu 				return (error);
385bc35c174SNicolas Souchu 			}
3862067d312SJohn Baldwin 		}
387bc35c174SNicolas Souchu 		break;
388bc35c174SNicolas Souchu 
389bc35c174SNicolas Souchu 	case PPB_REVERSE_IDLE:
390bc35c174SNicolas Souchu 	case PPB_EPP_IDLE:
391bc35c174SNicolas Souchu 	case PPB_ECP_FORWARD_IDLE:
392bc35c174SNicolas Souchu 	default:
393bc35c174SNicolas Souchu 		break;
394bc35c174SNicolas Souchu 	}
395bc35c174SNicolas Souchu 
396bc35c174SNicolas Souchu #ifdef DEBUG_1284
397bc35c174SNicolas Souchu 	printf("N");
398bc35c174SNicolas Souchu #endif
399bc35c174SNicolas Souchu 	/* read data */
400bc35c174SNicolas Souchu 	len = 0;
401bc35c174SNicolas Souchu 	while (uio->uio_resid) {
4022067d312SJohn Baldwin 		error = ppb_1284_read(ppbus, ppi->ppi_mode,
4032067d312SJohn Baldwin 		    buffer, min(BUFSIZE, uio->uio_resid), &len);
4042067d312SJohn Baldwin 		ppb_unlock(ppbus);
4052067d312SJohn Baldwin 		if (error)
406bc35c174SNicolas Souchu 			goto error;
407bc35c174SNicolas Souchu 
408bc35c174SNicolas Souchu 		if (!len)
409bc35c174SNicolas Souchu 			goto error;		/* no more data */
410bc35c174SNicolas Souchu 
411bc35c174SNicolas Souchu #ifdef DEBUG_1284
412bc35c174SNicolas Souchu 		printf("d");
413bc35c174SNicolas Souchu #endif
4142067d312SJohn Baldwin 		if ((error = uiomove(buffer, len, uio)))
415bc35c174SNicolas Souchu 			goto error;
4162067d312SJohn Baldwin 		ppb_lock(ppbus);
417bc35c174SNicolas Souchu 	}
4182067d312SJohn Baldwin 	ppb_unlock(ppbus);
419bc35c174SNicolas Souchu 
420bc35c174SNicolas Souchu error:
4212067d312SJohn Baldwin 	free(buffer, M_DEVBUF);
422bc35c174SNicolas Souchu #else /* PERIPH_1284 */
423bc35c174SNicolas Souchu 	int error = ENODEV;
424bc35c174SNicolas Souchu #endif
425bc35c174SNicolas Souchu 
426bc35c174SNicolas Souchu 	return (error);
427bc35c174SNicolas Souchu }
428bc35c174SNicolas Souchu 
429bc35c174SNicolas Souchu /*
430bc35c174SNicolas Souchu  * ppiwrite()
431bc35c174SNicolas Souchu  *
432bc35c174SNicolas Souchu  * IEEE1284 compliant write
433bc35c174SNicolas Souchu  *
434bc35c174SNicolas Souchu  * Actually, this is the peripheral side of a remote IEEE1284 read
435bc35c174SNicolas Souchu  *
436d64ada50SJens Schweikhardt  * The first part of the negotiation (IEEE1284 device detection) is
437bc35c174SNicolas Souchu  * done at interrupt level, then the remaining is done by the writing
438bc35c174SNicolas Souchu  * process
439bc35c174SNicolas Souchu  *
440d64ada50SJens Schweikhardt  * Once negotiation done, transfer data
441bc35c174SNicolas Souchu  */
442bc35c174SNicolas Souchu static int
ppiwrite(struct cdev * dev,struct uio * uio,int ioflag)44389c9c53dSPoul-Henning Kamp ppiwrite(struct cdev *dev, struct uio *uio, int ioflag)
444bc35c174SNicolas Souchu {
445bc35c174SNicolas Souchu #ifdef PERIPH_1284
446ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
447ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
4480f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(ppidev);
449bc35c174SNicolas Souchu 	int len, error = 0, sent;
4502067d312SJohn Baldwin 	char *buffer;
451bc35c174SNicolas Souchu 
452bc35c174SNicolas Souchu #if 0
453bc35c174SNicolas Souchu 	int ret;
454bc35c174SNicolas Souchu 
455bc35c174SNicolas Souchu 	#define ADDRESS		MS_PARAM(0, 0, MS_TYP_PTR)
456bc35c174SNicolas Souchu 	#define LENGTH		MS_PARAM(0, 1, MS_TYP_INT)
457bc35c174SNicolas Souchu 
458bc35c174SNicolas Souchu 	struct ppb_microseq msq[] = {
459bc35c174SNicolas Souchu 		  { MS_OP_PUT, { MS_UNKNOWN, MS_UNKNOWN, MS_UNKNOWN } },
460bc35c174SNicolas Souchu 		  MS_RET(0)
461bc35c174SNicolas Souchu 	};
462bc35c174SNicolas Souchu 
4632067d312SJohn Baldwin 	buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
4642067d312SJohn Baldwin 	ppb_lock(ppbus);
4652067d312SJohn Baldwin 
466d64ada50SJens Schweikhardt 	/* negotiate ECP mode */
4670f210c92SNicolas Souchu 	if (ppb_1284_negociate(ppbus, PPB_ECP, 0)) {
468d64ada50SJens Schweikhardt 		printf("ppiwrite: ECP negotiation failed\n");
469bc35c174SNicolas Souchu 	}
470bc35c174SNicolas Souchu 
471bc35c174SNicolas Souchu 	while (!error && (len = min(uio->uio_resid, BUFSIZE))) {
4722067d312SJohn Baldwin 		ppb_unlock(ppbus);
4732067d312SJohn Baldwin 		uiomove(buffer, len, uio);
474bc35c174SNicolas Souchu 
4752067d312SJohn Baldwin 		ppb_MS_init_msq(msq, 2, ADDRESS, buffer, LENGTH, len);
476bc35c174SNicolas Souchu 
4772067d312SJohn Baldwin 		ppb_lock(ppbus);
4780f210c92SNicolas Souchu 		error = ppb_MS_microseq(ppbus, msq, &ret);
479bc35c174SNicolas Souchu 	}
4802067d312SJohn Baldwin #else
4812067d312SJohn Baldwin 	buffer = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
4822067d312SJohn Baldwin 	ppb_lock(ppbus);
483bc35c174SNicolas Souchu #endif
484bc35c174SNicolas Souchu 
485bc35c174SNicolas Souchu 	/* we have to be peripheral to be able to send data, so
486bc35c174SNicolas Souchu 	 * wait for the appropriate state
487bc35c174SNicolas Souchu 	 */
488bdcee5cfSNicolas Souchu  	if (ppb_1284_get_state(ppbus) < PPB_PERIPHERAL_NEGOCIATION)
4890f210c92SNicolas Souchu 		ppb_1284_terminate(ppbus);
490bc35c174SNicolas Souchu 
491bdcee5cfSNicolas Souchu  	while (ppb_1284_get_state(ppbus) != PPB_PERIPHERAL_IDLE) {
492bc35c174SNicolas Souchu 		/* XXX should check a variable before sleeping */
493bc35c174SNicolas Souchu #ifdef DEBUG_1284
494bc35c174SNicolas Souchu 		printf("s");
495bc35c174SNicolas Souchu #endif
496bc35c174SNicolas Souchu 
4970f210c92SNicolas Souchu 		ppi_enable_intr(ppidev);
498bc35c174SNicolas Souchu 
499d64ada50SJens Schweikhardt 		/* sleep until IEEE1284 negotiation starts */
5002067d312SJohn Baldwin 		error = ppb_sleep(ppbus, ppi, PCATCH | PPBPRI, "ppiwrite", 0);
501bc35c174SNicolas Souchu 
502bc35c174SNicolas Souchu 		switch (error) {
503bc35c174SNicolas Souchu 		case 0:
504d64ada50SJens Schweikhardt 			/* negotiate peripheral side with BYTE mode */
5050f210c92SNicolas Souchu 			ppb_peripheral_negociate(ppbus, PPB_BYTE, 0);
506bc35c174SNicolas Souchu 			break;
507bc35c174SNicolas Souchu 		case EWOULDBLOCK:
508bc35c174SNicolas Souchu 			break;
509bc35c174SNicolas Souchu 		default:
510bc35c174SNicolas Souchu 			goto error;
511bc35c174SNicolas Souchu 		}
512bc35c174SNicolas Souchu 	}
513bc35c174SNicolas Souchu #ifdef DEBUG_1284
514bc35c174SNicolas Souchu 	printf("N");
515bc35c174SNicolas Souchu #endif
516bc35c174SNicolas Souchu 
517d64ada50SJens Schweikhardt 	/* negotiation done, write bytes to master host */
518d254af07SMatthew Dillon 	while ((len = min(uio->uio_resid, BUFSIZE)) != 0) {
5192067d312SJohn Baldwin 		ppb_unlock(ppbus);
5202067d312SJohn Baldwin 		uiomove(buffer, len, uio);
5212067d312SJohn Baldwin 		ppb_lock(ppbus);
5220f210c92SNicolas Souchu 		if ((error = byte_peripheral_write(ppbus,
5232067d312SJohn Baldwin 						buffer, len, &sent)))
524bc35c174SNicolas Souchu 			goto error;
525bc35c174SNicolas Souchu #ifdef DEBUG_1284
526bc35c174SNicolas Souchu 		printf("d");
527bc35c174SNicolas Souchu #endif
528bc35c174SNicolas Souchu 	}
529bc35c174SNicolas Souchu 
530bc35c174SNicolas Souchu error:
5312067d312SJohn Baldwin 	ppb_unlock(ppbus);
5322067d312SJohn Baldwin 	free(buffer, M_DEVBUF);
533bc35c174SNicolas Souchu #else /* PERIPH_1284 */
534bc35c174SNicolas Souchu 	int error = ENODEV;
535bc35c174SNicolas Souchu #endif
536bc35c174SNicolas Souchu 
537bc35c174SNicolas Souchu 	return (error);
538bc35c174SNicolas Souchu }
539bc35c174SNicolas Souchu 
540ed381522SMike Smith static int
ppiioctl(struct cdev * dev,u_long cmd,caddr_t data,int flags,struct thread * td)54189c9c53dSPoul-Henning Kamp ppiioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
542ed381522SMike Smith {
543ae6b868aSJohn Baldwin 	struct ppi_data *ppi = dev->si_drv1;
544ae6b868aSJohn Baldwin 	device_t ppidev = ppi->ppi_device;
545f32b2a16SJohn Baldwin 	device_t ppbus = device_get_parent(ppidev);
5466f34dba9SMike Smith 	int error = 0;
5476f34dba9SMike Smith 	u_int8_t *val = (u_int8_t *)data;
5486f34dba9SMike Smith 
5492067d312SJohn Baldwin 	ppb_lock(ppbus);
5506f34dba9SMike Smith 	switch (cmd) {
5516f34dba9SMike Smith 	case PPIGDATA:			/* get data register */
5520f210c92SNicolas Souchu 		*val = ppb_rdtr(ppbus);
5536f34dba9SMike Smith 		break;
5546f34dba9SMike Smith 	case PPIGSTATUS:		/* get status bits */
5550f210c92SNicolas Souchu 		*val = ppb_rstr(ppbus);
5566f34dba9SMike Smith 		break;
5576f34dba9SMike Smith 	case PPIGCTRL:			/* get control bits */
5580f210c92SNicolas Souchu 		*val = ppb_rctr(ppbus);
5596f34dba9SMike Smith 		break;
56020240fa3SNicolas Souchu 	case PPIGEPPD:			/* get EPP data bits */
5610f210c92SNicolas Souchu 		*val = ppb_repp_D(ppbus);
5626f34dba9SMike Smith 		break;
5636f34dba9SMike Smith 	case PPIGECR:			/* get ECP bits */
5640f210c92SNicolas Souchu 		*val = ppb_recr(ppbus);
5656f34dba9SMike Smith 		break;
5666f34dba9SMike Smith 	case PPIGFIFO:			/* read FIFO */
5670f210c92SNicolas Souchu 		*val = ppb_rfifo(ppbus);
5686f34dba9SMike Smith 		break;
5696f34dba9SMike Smith 	case PPISDATA:			/* set data register */
5700f210c92SNicolas Souchu 		ppb_wdtr(ppbus, *val);
5716f34dba9SMike Smith 		break;
5726f34dba9SMike Smith 	case PPISSTATUS:		/* set status bits */
5730f210c92SNicolas Souchu 		ppb_wstr(ppbus, *val);
5746f34dba9SMike Smith 		break;
5756f34dba9SMike Smith 	case PPISCTRL:			/* set control bits */
5760f210c92SNicolas Souchu 		ppb_wctr(ppbus, *val);
5776f34dba9SMike Smith 		break;
57820240fa3SNicolas Souchu 	case PPISEPPD:			/* set EPP data bits */
5790f210c92SNicolas Souchu 		ppb_wepp_D(ppbus, *val);
5806f34dba9SMike Smith 		break;
5816f34dba9SMike Smith 	case PPISECR:			/* set ECP bits */
5820f210c92SNicolas Souchu 		ppb_wecr(ppbus, *val);
5836f34dba9SMike Smith 		break;
5846f34dba9SMike Smith 	case PPISFIFO:			/* write FIFO */
5850f210c92SNicolas Souchu 		ppb_wfifo(ppbus, *val);
5866f34dba9SMike Smith 		break;
58720240fa3SNicolas Souchu 	case PPIGEPPA:			/* get EPP address bits */
5880f210c92SNicolas Souchu 		*val = ppb_repp_A(ppbus);
58920240fa3SNicolas Souchu 		break;
59020240fa3SNicolas Souchu 	case PPISEPPA:			/* set EPP address bits */
5910f210c92SNicolas Souchu 		ppb_wepp_A(ppbus, *val);
59220240fa3SNicolas Souchu 		break;
5936f34dba9SMike Smith 	default:
5946f34dba9SMike Smith 		error = ENOTTY;
5956f34dba9SMike Smith 		break;
5966f34dba9SMike Smith 	}
5972067d312SJohn Baldwin 	ppb_unlock(ppbus);
5986f34dba9SMike Smith 
5996f34dba9SMike Smith 	return (error);
600ed381522SMike Smith }
601ed381522SMike Smith 
6020f063508SPeter Wemm static device_method_t ppi_methods[] = {
6030f063508SPeter Wemm 	/* device interface */
6040f063508SPeter Wemm 	DEVMETHOD(device_identify,	ppi_identify),
6050f063508SPeter Wemm 	DEVMETHOD(device_probe,		ppi_probe),
6060f063508SPeter Wemm 	DEVMETHOD(device_attach,	ppi_attach),
6072067d312SJohn Baldwin 	DEVMETHOD(device_detach,	ppi_detach),
6080f063508SPeter Wemm 	{ 0, 0 }
6090f063508SPeter Wemm };
6100f063508SPeter Wemm 
6110f063508SPeter Wemm static driver_t ppi_driver = {
6120f063508SPeter Wemm 	"ppi",
6130f063508SPeter Wemm 	ppi_methods,
6140f063508SPeter Wemm 	sizeof(struct ppi_data),
6150f063508SPeter Wemm };
6166d588090SJohn Baldwin 
6176d588090SJohn Baldwin DRIVER_MODULE(ppi, ppbus, ppi_driver, 0, 0);
618f5fd5611SRuslan Ermilov MODULE_DEPEND(ppi, ppbus, 1, 1, 1);
619