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 withough 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.
28 * $FreeBSD: src/sys/isa/joy.c,v 1.38.2.1 2001/09/01 05:55:31 murray Exp $
29 * $DragonFly: src/sys/dev/misc/joy/joy.c,v 1.2 2003/06/17 04:28:40 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
43 #include <sys/joystick.h>
45 #include <isa/isavar.h>
48 /* The game port can manage 4 buttons and 4 variable resistors (usually 2
49 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
50 * Getting the state of the buttons is done by reading the game port:
51 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
53 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
54 * to get the value of a resistor, write the value 0xff at port and
55 * wait until the corresponding bit returns to 0.
58 #define joypart(d) (minor(d)&1)
59 #define UNIT(d) ((minor(d)>>1)&3)
61 #define JOY_TIMEOUT 2000 /* 2 milliseconds */
66 bus_space_handle_t port;
67 int x_off[2], y_off[2];
71 #define JOY_SOFTC(unit) (struct joy_softc *) \
72 devclass_get_softc(joy_devclass,(unit))
74 static int joy_probe (device_t);
75 static int joy_attach (device_t);
78 static d_open_t joyopen;
79 static d_close_t joyclose;
80 static d_read_t joyread;
81 static d_ioctl_t joyioctl;
83 static struct cdevsw joy_cdevsw = {
91 /* strategy */ nostrategy,
100 devclass_t joy_devclass;
102 static struct isa_pnp_id joy_ids[] = {
103 {0x0100630e, "CSC0001 PnP Joystick"}, /* CSC0001 */
104 {0x0101630e, "CSC0101 PnP Joystick"}, /* CSC0101 */
105 {0x01100002, "ALS0110 PnP Joystick"}, /* @P@1001 */
106 {0x01200002, "ALS0120 PnP Joystick"}, /* @P@2001 */
107 {0x01007316, "ESS0001 PnP Joystick"}, /* ESS0001 */
108 {0x2fb0d041, "Generic PnP Joystick"}, /* PNPb02f */
109 {0x2200a865, "YMH0022 PnP Joystick"}, /* YMH0022 */
110 {0x82719304, NULL}, /* ADS7182 */
115 joy_probe (device_t dev)
117 if (ISA_PNP_PROBE(device_get_parent(dev), dev, joy_ids) == ENXIO)
119 #ifdef WANT_JOYSTICK_CONNECTED
121 outb (dev->id_iobase, 0xff);
122 DELAY (10000); /* 10 ms delay */
123 return (inb (dev->id_iobase) & 0x0f) != 0x0f;
131 joy_attach (device_t dev)
133 int unit = device_get_unit(dev);
135 struct resource *res;
136 struct joy_softc *joy = device_get_softc(dev);
138 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
141 joy->bt = rman_get_bustag(res);
142 joy->port = rman_get_bushandle(res);
143 joy->timeout[0] = joy->timeout[1] = 0;
144 make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
148 static device_method_t joy_methods[] = {
149 DEVMETHOD(device_probe, joy_probe),
150 DEVMETHOD(device_attach, joy_attach),
154 static driver_t joy_isa_driver = {
157 sizeof (struct joy_softc)
160 DRIVER_MODULE(joy, isa, joy_isa_driver, joy_devclass, 0, 0);
163 joyopen(dev_t dev, int flags, int fmt, struct proc *p)
165 int i = joypart (dev);
166 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
170 joy->x_off[i] = joy->y_off[i] = 0;
171 joy->timeout[i] = JOY_TIMEOUT;
176 joyclose(dev_t dev, int flags, int fmt, struct proc *p)
178 int i = joypart (dev);
179 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
186 joyread(dev_t dev, struct uio *uio, int flag)
188 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
189 bus_space_handle_t port = joy->port;
190 bus_space_tag_t bt = joy->bt;
191 struct timespec t, start, end;
193 struct timespec x, y;
202 bus_space_write_1 (bt, port, 0, 0xff);
205 end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
206 timespecadd(&end, &start);
210 while (timespeccmp(&t, &end, <)) {
211 state = bus_space_read_1 (bt, port, 0);
212 if (joypart(dev) == 1)
215 if (!timespecisset(&x) && !(state & 0x01))
217 if (!timespecisset(&y) && !(state & 0x02))
219 if (timespecisset(&x) && timespecisset(&y))
227 if (timespecisset(&x)) {
228 timespecsub(&x, &start);
229 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
232 if (timespecisset(&y)) {
233 timespecsub(&y, &start);
234 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
239 c.b2 = ~(state >> 1) & 1;
240 return uiomove ((caddr_t)&c, sizeof(struct joystick), uio);
244 joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
246 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
247 int i = joypart (dev);
253 if (x < 1 || x > 10000) /* 10ms maximum! */
258 *(int *) data = joy->timeout[i];
260 case JOY_SET_X_OFFSET:
261 joy->x_off[i] = *(int *) data;
263 case JOY_SET_Y_OFFSET:
264 joy->y_off[i] = *(int *) data;
266 case JOY_GET_X_OFFSET:
267 *(int *) data = joy->x_off[i];
269 case JOY_GET_Y_OFFSET:
270 *(int *) data = joy->y_off[i];