joy.c revision 87384
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 withough 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 * $FreeBSD: head/sys/dev/joy/joy.c 87384 2001-12-05 09:08:23Z imp $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/conf.h>
34#include <sys/uio.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/bus.h>
38#include <machine/bus.h>
39#include <machine/resource.h>
40#include <sys/rman.h>
41#include <sys/time.h>
42#include <sys/joystick.h>
43#include <dev/joy/joyvar.h>
44
45/* The game port can manage 4 buttons and 4 variable resistors (usually 2
46 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
47 * Getting the state of the buttons is done by reading the game port:
48 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
49 * to bits 0-3.
50 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
51 * to get the value of a resistor, write the value 0xff at port and
52 * wait until the corresponding bit returns to 0.
53 */
54
55#define joypart(d) (minor(d)&1)
56#define UNIT(d) ((minor(d)>>1)&3)
57#ifndef JOY_TIMEOUT
58#define JOY_TIMEOUT   2000 /* 2 milliseconds */
59#endif
60
61#define JOY_SOFTC(unit) (struct joy_softc *) \
62        devclass_get_softc(joy_devclass,(unit))
63
64#define CDEV_MAJOR 51
65static	d_open_t	joyopen;
66static	d_close_t	joyclose;
67static	d_read_t	joyread;
68static	d_ioctl_t	joyioctl;
69
70static struct cdevsw joy_cdevsw = {
71	/* open */	joyopen,
72	/* close */	joyclose,
73	/* read */	joyread,
74	/* write */	nowrite,
75	/* ioctl */	joyioctl,
76	/* poll */	nopoll,
77	/* mmap */	nommap,
78	/* strategy */	nostrategy,
79	/* name */	"joy",
80	/* maj */	CDEV_MAJOR,
81	/* dump */	nodump,
82	/* psize */	nopsize,
83	/* flags */	0,
84};
85
86devclass_t joy_devclass;
87
88int
89joy_probe(device_t dev)
90{
91#ifdef WANT_JOYSTICK_CONNECTED
92#ifdef notyet
93	outb(dev->id_iobase, 0xff);
94	DELAY(10000); /*  10 ms delay */
95	return (inb(dev->id_iobase) & 0x0f) != 0x0f;
96#else
97	return (0);
98#endif
99#else
100	return (0);
101#endif
102}
103
104int
105joy_attach(device_t dev)
106{
107	int	unit = device_get_unit(dev);
108	struct joy_softc *joy = device_get_softc(dev);
109
110	joy->rid = 0;
111	joy->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &joy->rid, 0, ~0, 1,
112	    RF_ACTIVE);
113	if (joy->res == NULL)
114		return ENXIO;
115	joy->bt = rman_get_bustag(joy->res);
116	joy->port = rman_get_bushandle(joy->res);
117	joy->timeout[0] = joy->timeout[1] = 0;
118	joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
119	return (0);
120}
121
122int
123joy_detach(device_t dev)
124{
125	struct joy_softc *joy = device_get_softc(dev);
126
127	if (joy->res != NULL)
128		bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
129	if (joy->d)
130		destroy_dev(joy->d);
131	return (0);
132}
133
134
135static int
136joyopen(dev_t dev, int flags, int fmt, struct thread *td)
137{
138	int i = joypart (dev);
139	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
140
141	if (joy->timeout[i])
142		return (EBUSY);
143	joy->x_off[i] = joy->y_off[i] = 0;
144	joy->timeout[i] = JOY_TIMEOUT;
145	return (0);
146}
147
148static int
149joyclose(dev_t dev, int flags, int fmt, struct thread *td)
150{
151	int i = joypart (dev);
152	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
153
154	joy->timeout[i] = 0;
155	return (0);
156}
157
158static int
159joyread(dev_t dev, struct uio *uio, int flag)
160{
161	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
162	bus_space_handle_t port = joy->port;
163	bus_space_tag_t bt = joy->bt;
164	struct timespec t, start, end;
165	int state = 0;
166	struct timespec x, y;
167	struct joystick c;
168#ifndef i386
169	int s;
170
171	s = splhigh();
172#else
173	disable_intr ();
174#endif
175	bus_space_write_1 (bt, port, 0, 0xff);
176	nanotime(&start);
177	end.tv_sec = 0;
178	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
179	timespecadd(&end, &start);
180	t = start;
181	timespecclear(&x);
182	timespecclear(&y);
183	while (timespeccmp(&t, &end, <)) {
184		state = bus_space_read_1 (bt, port, 0);
185		if (joypart(dev) == 1)
186			state >>= 2;
187		nanotime(&t);
188		if (!timespecisset(&x) && !(state & 0x01))
189			x = t;
190		if (!timespecisset(&y) && !(state & 0x02))
191			y = t;
192		if (timespecisset(&x) && timespecisset(&y))
193			break;
194	}
195#ifndef i386
196	splx(s);
197#else
198	enable_intr ();
199#endif
200	if (timespecisset(&x)) {
201		timespecsub(&x, &start);
202		c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
203	} else
204		c.x = 0x80000000;
205	if (timespecisset(&y)) {
206		timespecsub(&y, &start);
207		c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
208	} else
209		c.y = 0x80000000;
210	state >>= 4;
211	c.b1 = ~state & 1;
212	c.b2 = ~(state >> 1) & 1;
213	return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
214}
215
216static int
217joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
218{
219	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
220	int i = joypart (dev);
221	int x;
222
223	switch (cmd) {
224	case JOY_SETTIMEOUT:
225		x = *(int *) data;
226		if (x < 1 || x > 10000) /* 10ms maximum! */
227			return EINVAL;
228		joy->timeout[i] = x;
229		break;
230	case JOY_GETTIMEOUT:
231		*(int *) data = joy->timeout[i];
232		break;
233	case JOY_SET_X_OFFSET:
234		joy->x_off[i] = *(int *) data;
235		break;
236	case JOY_SET_Y_OFFSET:
237		joy->y_off[i] = *(int *) data;
238		break;
239	case JOY_GET_X_OFFSET:
240		*(int *) data = joy->x_off[i];
241		break;
242	case JOY_GET_Y_OFFSET:
243		*(int *) data = joy->y_off[i];
244		break;
245	default:
246		return (ENOTTY);
247	}
248	return (0);
249}
250