joy.c revision 127135
1/*-
2 * Copyright (c) 1995 Jean-Marc Zucconi
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/dev/joy/joy.c 127135 2004-03-17 17:50:55Z njl $");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/conf.h>
36#include <sys/uio.h>
37#include <sys/kernel.h>
38#include <sys/module.h>
39#include <sys/bus.h>
40#include <machine/bus.h>
41#include <machine/resource.h>
42#include <sys/rman.h>
43#include <sys/time.h>
44#include <sys/joystick.h>
45#include <dev/joy/joyvar.h>
46
47/* The game port can manage 4 buttons and 4 variable resistors (usually 2
48 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
49 * Getting the state of the buttons is done by reading the game port:
50 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
51 * to bits 0-3.
52 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
53 * to get the value of a resistor, write the value 0xff at port and
54 * wait until the corresponding bit returns to 0.
55 */
56
57#define joypart(d) (minor(d)&1)
58#define UNIT(d) ((minor(d)>>1)&3)
59#ifndef JOY_TIMEOUT
60#define JOY_TIMEOUT   2000 /* 2 milliseconds */
61#endif
62
63#define JOY_SOFTC(unit) (struct joy_softc *) \
64        devclass_get_softc(joy_devclass,(unit))
65
66static	d_open_t	joyopen;
67static	d_close_t	joyclose;
68static	d_read_t	joyread;
69static	d_ioctl_t	joyioctl;
70
71static struct cdevsw joy_cdevsw = {
72	.d_version =	D_VERSION,
73	.d_flags =	D_NEEDGIANT,
74	.d_open =	joyopen,
75	.d_close =	joyclose,
76	.d_read =	joyread,
77	.d_ioctl =	joyioctl,
78	.d_name =	"joy",
79};
80
81devclass_t joy_devclass;
82
83int
84joy_probe(device_t dev)
85{
86#ifdef WANT_JOYSTICK_CONNECTED
87#ifdef notyet
88	outb(dev->id_iobase, 0xff);
89	DELAY(10000); /*  10 ms delay */
90	return (inb(dev->id_iobase) & 0x0f) != 0x0f;
91#else
92	return (0);
93#endif
94#else
95	return (0);
96#endif
97}
98
99int
100joy_attach(device_t dev)
101{
102	int	unit = device_get_unit(dev);
103	struct joy_softc *joy = device_get_softc(dev);
104
105	joy->rid = 0;
106	joy->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &joy->rid,
107	    RF_ACTIVE);
108	if (joy->res == NULL)
109		return ENXIO;
110	joy->bt = rman_get_bustag(joy->res);
111	joy->port = rman_get_bushandle(joy->res);
112	joy->timeout[0] = joy->timeout[1] = 0;
113	joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
114	return (0);
115}
116
117int
118joy_detach(device_t dev)
119{
120	struct joy_softc *joy = device_get_softc(dev);
121
122	if (joy->res != NULL)
123		bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
124	if (joy->d)
125		destroy_dev(joy->d);
126	return (0);
127}
128
129
130static int
131joyopen(dev_t dev, int flags, int fmt, struct thread *td)
132{
133	int i = joypart (dev);
134	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
135
136	if (joy->timeout[i])
137		return (EBUSY);
138	joy->x_off[i] = joy->y_off[i] = 0;
139	joy->timeout[i] = JOY_TIMEOUT;
140	return (0);
141}
142
143static int
144joyclose(dev_t dev, int flags, int fmt, struct thread *td)
145{
146	int i = joypart (dev);
147	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
148
149	joy->timeout[i] = 0;
150	return (0);
151}
152
153static int
154joyread(dev_t dev, struct uio *uio, int flag)
155{
156	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
157	bus_space_handle_t port = joy->port;
158	bus_space_tag_t bt = joy->bt;
159	struct timespec t, start, end;
160	int state = 0;
161	struct timespec x, y;
162	struct joystick c;
163#ifndef __i386__
164	int s;
165
166	s = splhigh();
167#else
168	disable_intr ();
169#endif
170	bus_space_write_1 (bt, port, 0, 0xff);
171	nanotime(&start);
172	end.tv_sec = 0;
173	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
174	timespecadd(&end, &start);
175	t = start;
176	timespecclear(&x);
177	timespecclear(&y);
178	while (timespeccmp(&t, &end, <)) {
179		state = bus_space_read_1 (bt, port, 0);
180		if (joypart(dev) == 1)
181			state >>= 2;
182		nanotime(&t);
183		if (!timespecisset(&x) && !(state & 0x01))
184			x = t;
185		if (!timespecisset(&y) && !(state & 0x02))
186			y = t;
187		if (timespecisset(&x) && timespecisset(&y))
188			break;
189	}
190#ifndef __i386__
191	splx(s);
192#else
193	enable_intr ();
194#endif
195	if (timespecisset(&x)) {
196		timespecsub(&x, &start);
197		c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
198	} else
199		c.x = 0x80000000;
200	if (timespecisset(&y)) {
201		timespecsub(&y, &start);
202		c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
203	} else
204		c.y = 0x80000000;
205	state >>= 4;
206	c.b1 = ~state & 1;
207	c.b2 = ~(state >> 1) & 1;
208	return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
209}
210
211static int
212joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
213{
214	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
215	int i = joypart (dev);
216	int x;
217
218	switch (cmd) {
219	case JOY_SETTIMEOUT:
220		x = *(int *) data;
221		if (x < 1 || x > 10000) /* 10ms maximum! */
222			return EINVAL;
223		joy->timeout[i] = x;
224		break;
225	case JOY_GETTIMEOUT:
226		*(int *) data = joy->timeout[i];
227		break;
228	case JOY_SET_X_OFFSET:
229		joy->x_off[i] = *(int *) data;
230		break;
231	case JOY_SET_Y_OFFSET:
232		joy->y_off[i] = *(int *) data;
233		break;
234	case JOY_GET_X_OFFSET:
235		*(int *) data = joy->x_off[i];
236		break;
237	case JOY_GET_Y_OFFSET:
238		*(int *) data = joy->y_off[i];
239		break;
240	default:
241		return (ENOTTY);
242	}
243	return (0);
244}
245