2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1995 Jean-Marc Zucconi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
46 #include <sys/joystick.h>
47 #include <dev/joy/joyvar.h>
49 /* The game port can manage 4 buttons and 4 variable resistors (usually 2
50 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
51 * Getting the state of the buttons is done by reading the game port:
52 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
54 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
55 * to get the value of a resistor, write the value 0xff at port and
56 * wait until the corresponding bit returns to 0.
59 #define joypart(d) (dev2unit(d)&1)
61 #define JOY_TIMEOUT 2000 /* 2 milliseconds */
64 static d_open_t joyopen;
65 static d_close_t joyclose;
66 static d_read_t joyread;
67 static d_ioctl_t joyioctl;
69 static struct cdevsw joy_cdevsw = {
70 .d_version = D_VERSION,
71 .d_flags = D_NEEDGIANT,
79 devclass_t joy_devclass;
82 joy_probe(device_t dev)
84 #ifdef WANT_JOYSTICK_CONNECTED
86 outb(dev->id_iobase, 0xff);
87 DELAY(10000); /* 10 ms delay */
88 return (inb(dev->id_iobase) & 0x0f) != 0x0f;
98 joy_attach(device_t dev)
100 int unit = device_get_unit(dev);
101 struct joy_softc *joy = device_get_softc(dev);
104 joy->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &joy->rid,
105 RF_ACTIVE|RF_SHAREABLE);
106 if (joy->res == NULL)
108 joy->bt = rman_get_bustag(joy->res);
109 joy->port = rman_get_bushandle(joy->res);
110 joy->timeout[0] = joy->timeout[1] = 0;
111 joy->d = make_dev(&joy_cdevsw, unit, 0, 0, 0600, "joy%d", unit);
112 joy->d->si_drv1 = joy;
113 gone_in_dev(dev, 12, "joy(4) driver");
119 joy_detach(device_t dev)
121 struct joy_softc *joy = device_get_softc(dev);
123 if (joy->res != NULL)
124 bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
132 joyopen(struct cdev *dev, int flags, int fmt, struct thread *td)
134 int i = joypart (dev);
135 struct joy_softc *joy = dev->si_drv1;
139 joy->x_off[i] = joy->y_off[i] = 0;
140 joy->timeout[i] = JOY_TIMEOUT;
145 joyclose(struct cdev *dev, int flags, int fmt, struct thread *td)
147 int i = joypart (dev);
148 struct joy_softc *joy = dev->si_drv1;
155 joyread(struct cdev *dev, struct uio *uio, int flag)
157 struct joy_softc *joy = dev->si_drv1;
158 bus_space_handle_t port = joy->port;
159 bus_space_tag_t bt = joy->bt;
160 struct timespec t, start, end;
162 struct timespec x, y;
173 end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
174 timespecadd(&end, &t);
175 for (; timespeccmp(&t, &end, <) && (bus_space_read_1(bt, port, 0) & 0x0f); nanotime(&t))
177 bus_space_write_1 (bt, port, 0, 0xff);
180 end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
181 timespecadd(&end, &start);
185 while (timespeccmp(&t, &end, <)) {
186 state = bus_space_read_1 (bt, port, 0);
187 if (joypart(dev) == 1)
190 if (!timespecisset(&x) && !(state & 0x01))
192 if (!timespecisset(&y) && !(state & 0x02))
194 if (timespecisset(&x) && timespecisset(&y))
202 if (timespecisset(&x)) {
203 timespecsub(&x, &start);
204 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
207 if (timespecisset(&y)) {
208 timespecsub(&y, &start);
209 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
214 c.b2 = ~(state >> 1) & 1;
215 return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
219 joyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
221 struct joy_softc *joy = dev->si_drv1;
222 int i = joypart (dev);
228 if (x < 1 || x > 10000) /* 10ms maximum! */
233 *(int *) data = joy->timeout[i];
235 case JOY_SET_X_OFFSET:
236 joy->x_off[i] = *(int *) data;
238 case JOY_SET_Y_OFFSET:
239 joy->y_off[i] = *(int *) data;
241 case JOY_GET_X_OFFSET:
242 *(int *) data = joy->x_off[i];
244 case JOY_GET_Y_OFFSET:
245 *(int *) data = joy->y_off[i];