FreeBSD/Linux Kernel Cross Reference
sys/isa/joy.c
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$
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
44 #include <isa/isavar.h>
45 #include "isa_if.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 struct joy_softc {
64 bus_space_tag_t bt;
65 bus_space_handle_t port;
66 int x_off[2], y_off[2];
67 int timeout[2];
68 };
69
70 #define JOY_SOFTC(unit) (struct joy_softc *) \
71 devclass_get_softc(joy_devclass,(unit))
72
73 static int joy_probe (device_t);
74 static int joy_attach (device_t);
75
76 #define CDEV_MAJOR 51
77 static d_open_t joyopen;
78 static d_close_t joyclose;
79 static d_read_t joyread;
80 static d_ioctl_t joyioctl;
81
82 static struct cdevsw joy_cdevsw = {
83 /* open */ joyopen,
84 /* close */ joyclose,
85 /* read */ joyread,
86 /* write */ nowrite,
87 /* ioctl */ joyioctl,
88 /* poll */ nopoll,
89 /* mmap */ nommap,
90 /* strategy */ nostrategy,
91 /* name */ "joy",
92 /* maj */ CDEV_MAJOR,
93 /* dump */ nodump,
94 /* psize */ nopsize,
95 /* flags */ 0,
96 /* bmaj */ -1
97 };
98
99 devclass_t joy_devclass;
100
101 static struct isa_pnp_id joy_ids[] = {
102 {0x0100630e, "CSC0001 PnP Joystick"}, /* CSC0001 */
103 {0x0101630e, "CSC0101 PnP Joystick"}, /* CSC0101 */
104 {0x01100002, "ALS0110 PnP Joystick"}, /* @P@1001 */
105 {0x01200002, "ALS0120 PnP Joystick"}, /* @P@2001 */
106 {0x01007316, "ESS0001 PnP Joystick"}, /* ESS0001 */
107 {0x2fb0d041, "Generic PnP Joystick"}, /* PNPb02f */
108 {0x2200a865, "YMH0022 PnP Joystick"}, /* YMH0022 */
109 {0x82719304, NULL}, /* ADS7182 */
110 {0}
111 };
112
113 static int
114 joy_probe (device_t dev)
115 {
116 if (ISA_PNP_PROBE(device_get_parent(dev), dev, joy_ids) == ENXIO)
117 return ENXIO;
118 #ifdef WANT_JOYSTICK_CONNECTED
119 #ifdef notyet
120 outb (dev->id_iobase, 0xff);
121 DELAY (10000); /* 10 ms delay */
122 return (inb (dev->id_iobase) & 0x0f) != 0x0f;
123 #endif
124 #else
125 return 0;
126 #endif
127 }
128
129 static int
130 joy_attach (device_t dev)
131 {
132 int unit = device_get_unit(dev);
133 int rid = 0;
134 struct resource *res;
135 struct joy_softc *joy = device_get_softc(dev);
136
137 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
138 if (res == NULL)
139 return ENXIO;
140 joy->bt = rman_get_bustag(res);
141 joy->port = rman_get_bushandle(res);
142 joy->timeout[0] = joy->timeout[1] = 0;
143 make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
144 return 0;
145 }
146
147 static device_method_t joy_methods[] = {
148 DEVMETHOD(device_probe, joy_probe),
149 DEVMETHOD(device_attach, joy_attach),
150 { 0, 0 }
151 };
152
153 static driver_t joy_isa_driver = {
154 "joy",
155 joy_methods,
156 sizeof (struct joy_softc)
157 };
158
159 DRIVER_MODULE(joy, isa, joy_isa_driver, joy_devclass, 0, 0);
160
161 static int
162 joyopen(dev_t dev, int flags, int fmt, struct proc *p)
163 {
164 int i = joypart (dev);
165 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
166
167 if (joy->timeout[i])
168 return EBUSY;
169 joy->x_off[i] = joy->y_off[i] = 0;
170 joy->timeout[i] = JOY_TIMEOUT;
171 return 0;
172 }
173
174 static int
175 joyclose(dev_t dev, int flags, int fmt, struct proc *p)
176 {
177 int i = joypart (dev);
178 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
179
180 joy->timeout[i] = 0;
181 return 0;
182 }
183
184 static int
185 joyread(dev_t dev, struct uio *uio, int flag)
186 {
187 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
188 bus_space_handle_t port = joy->port;
189 bus_space_tag_t bt = joy->bt;
190 struct timespec t, start, end;
191 int state = 0;
192 struct timespec x, y;
193 struct joystick c;
194 #ifndef i386
195 int s;
196
197 s = splhigh();
198 #else
199 disable_intr ();
200 #endif
201 bus_space_write_1 (bt, port, 0, 0xff);
202 nanotime(&start);
203 end.tv_sec = 0;
204 end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
205 timespecadd(&end, &start);
206 t = start;
207 timespecclear(&x);
208 timespecclear(&y);
209 while (timespeccmp(&t, &end, <)) {
210 state = bus_space_read_1 (bt, port, 0);
211 if (joypart(dev) == 1)
212 state >>= 2;
213 nanotime(&t);
214 if (!timespecisset(&x) && !(state & 0x01))
215 x = t;
216 if (!timespecisset(&y) && !(state & 0x02))
217 y = t;
218 if (timespecisset(&x) && timespecisset(&y))
219 break;
220 }
221 #ifndef i386
222 splx(s);
223 #else
224 enable_intr ();
225 #endif
226 if (timespecisset(&x)) {
227 timespecsub(&x, &start);
228 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
229 } else
230 c.x = 0x80000000;
231 if (timespecisset(&y)) {
232 timespecsub(&y, &start);
233 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
234 } else
235 c.y = 0x80000000;
236 state >>= 4;
237 c.b1 = ~state & 1;
238 c.b2 = ~(state >> 1) & 1;
239 return uiomove ((caddr_t)&c, sizeof(struct joystick), uio);
240 }
241
242 static int
243 joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
244 {
245 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
246 int i = joypart (dev);
247 int x;
248
249 switch (cmd) {
250 case JOY_SETTIMEOUT:
251 x = *(int *) data;
252 if (x < 1 || x > 10000) /* 10ms maximum! */
253 return EINVAL;
254 joy->timeout[i] = x;
255 break;
256 case JOY_GETTIMEOUT:
257 *(int *) data = joy->timeout[i];
258 break;
259 case JOY_SET_X_OFFSET:
260 joy->x_off[i] = *(int *) data;
261 break;
262 case JOY_SET_Y_OFFSET:
263 joy->y_off[i] = *(int *) data;
264 break;
265 case JOY_GET_X_OFFSET:
266 *(int *) data = joy->x_off[i];
267 break;
268 case JOY_GET_Y_OFFSET:
269 *(int *) data = joy->y_off[i];
270 break;
271 default:
272 return ENXIO;
273 }
274 return 0;
275 }
Cache object: ff32c4f05e54e1b1189796e049b6db8c
|