2 * Copyright (c) 1995 Jean-Marc Zucconi
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
44 #include <sys/joystick.h>
45 #include <dev/joy/joyvar.h>
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)
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.
57 #define joypart(d) (dev2unit(d)&1)
59 #define JOY_TIMEOUT 2000 /* 2 milliseconds */
62 static d_open_t joyopen;
63 static d_close_t joyclose;
64 static d_read_t joyread;
65 static d_ioctl_t joyioctl;
67 static struct cdevsw joy_cdevsw = {
68 .d_version = D_VERSION,
69 .d_flags = D_NEEDGIANT,
77 devclass_t joy_devclass;
80 joy_probe(device_t dev)
82 #ifdef WANT_JOYSTICK_CONNECTED
84 outb(dev->id_iobase, 0xff);
85 DELAY(10000); /* 10 ms delay */
86 return (inb(dev->id_iobase) & 0x0f) != 0x0f;
96 joy_attach(device_t dev)
98 int unit = device_get_unit(dev);
99 struct joy_softc *joy = device_get_softc(dev);
102 joy->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &joy->rid,
103 RF_ACTIVE|RF_SHAREABLE);
104 if (joy->res == NULL)
106 joy->bt = rman_get_bustag(joy->res);
107 joy->port = rman_get_bushandle(joy->res);
108 joy->timeout[0] = joy->timeout[1] = 0;
109 joy->d = make_dev(&joy_cdevsw, unit, 0, 0, 0600, "joy%d", unit);
110 joy->d->si_drv1 = joy;
115 joy_detach(device_t dev)
117 struct joy_softc *joy = device_get_softc(dev);
119 if (joy->res != NULL)
120 bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
128 joyopen(struct cdev *dev, int flags, int fmt, struct thread *td)
130 int i = joypart (dev);
131 struct joy_softc *joy = dev->si_drv1;
135 joy->x_off[i] = joy->y_off[i] = 0;
136 joy->timeout[i] = JOY_TIMEOUT;
141 joyclose(struct cdev *dev, int flags, int fmt, struct thread *td)
143 int i = joypart (dev);
144 struct joy_softc *joy = dev->si_drv1;
151 joyread(struct cdev *dev, struct uio *uio, int flag)
153 struct joy_softc *joy = dev->si_drv1;
154 bus_space_handle_t port = joy->port;
155 bus_space_tag_t bt = joy->bt;
156 struct timespec t, start, end;
158 struct timespec x, y;
169 end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
170 timespecadd(&end, &t);
171 for (; timespeccmp(&t, &end, <) && (bus_space_read_1(bt, port, 0) & 0x0f); nanotime(&t))
173 bus_space_write_1 (bt, port, 0, 0xff);
176 end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
177 timespecadd(&end, &start);
181 while (timespeccmp(&t, &end, <)) {
182 state = bus_space_read_1 (bt, port, 0);
183 if (joypart(dev) == 1)
186 if (!timespecisset(&x) && !(state & 0x01))
188 if (!timespecisset(&y) && !(state & 0x02))
190 if (timespecisset(&x) && timespecisset(&y))
198 if (timespecisset(&x)) {
199 timespecsub(&x, &start);
200 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
203 if (timespecisset(&y)) {
204 timespecsub(&y, &start);
205 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
210 c.b2 = ~(state >> 1) & 1;
211 return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
215 joyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
217 struct joy_softc *joy = dev->si_drv1;
218 int i = joypart (dev);
224 if (x < 1 || x > 10000) /* 10ms maximum! */
229 *(int *) data = joy->timeout[i];
231 case JOY_SET_X_OFFSET:
232 joy->x_off[i] = *(int *) data;
234 case JOY_SET_Y_OFFSET:
235 joy->y_off[i] = *(int *) data;
237 case JOY_GET_X_OFFSET:
238 *(int *) data = joy->x_off[i];
240 case JOY_GET_Y_OFFSET:
241 *(int *) data = joy->y_off[i];