joy.c revision 1.20 1 1.20 dholland /* $NetBSD: joy.c,v 1.20 2014/07/25 08:10:37 dholland Exp $ */
2 1.18 jmcneill
3 1.18 jmcneill /*-
4 1.18 jmcneill * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.18 jmcneill * All rights reserved.
6 1.18 jmcneill *
7 1.18 jmcneill * This code is derived from software developed for The NetBSD Foundation
8 1.18 jmcneill * by Andrew Doran.
9 1.18 jmcneill *
10 1.18 jmcneill * Redistribution and use in source and binary forms, with or without
11 1.18 jmcneill * modification, are permitted provided that the following conditions
12 1.18 jmcneill * are met:
13 1.18 jmcneill * 1. Redistributions of source code must retain the above copyright
14 1.18 jmcneill * notice, this list of conditions and the following disclaimer.
15 1.18 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
16 1.18 jmcneill * notice, this list of conditions and the following disclaimer in the
17 1.18 jmcneill * documentation and/or other materials provided with the distribution.
18 1.18 jmcneill *
19 1.18 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.18 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.18 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.18 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.18 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.18 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.18 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.18 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.18 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.18 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.18 jmcneill * POSSIBILITY OF SUCH DAMAGE.
30 1.18 jmcneill */
31 1.1 jdolecek
32 1.1 jdolecek /*-
33 1.1 jdolecek * Copyright (c) 1995 Jean-Marc Zucconi
34 1.1 jdolecek * All rights reserved.
35 1.1 jdolecek *
36 1.1 jdolecek * Ported to NetBSD by Matthieu Herrb <matthieu (at) laas.fr>
37 1.1 jdolecek *
38 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
39 1.1 jdolecek * modification, are permitted provided that the following conditions
40 1.1 jdolecek * are met:
41 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
42 1.1 jdolecek * notice, this list of conditions and the following disclaimer
43 1.1 jdolecek * in this position and unchanged.
44 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
45 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
46 1.1 jdolecek * documentation and/or other materials provided with the distribution.
47 1.1 jdolecek * 3. The name of the author may not be used to endorse or promote products
48 1.1 jdolecek * derived from this software without specific prior written permission
49 1.1 jdolecek *
50 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 1.1 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 1.1 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 1.1 jdolecek * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 1.1 jdolecek * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 1.1 jdolecek * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 1.1 jdolecek * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 1.1 jdolecek * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 1.1 jdolecek * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 1.1 jdolecek * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 1.1 jdolecek *
61 1.1 jdolecek */
62 1.1 jdolecek
63 1.1 jdolecek #include <sys/cdefs.h>
64 1.20 dholland __KERNEL_RCSID(0, "$NetBSD: joy.c,v 1.20 2014/07/25 08:10:37 dholland Exp $");
65 1.1 jdolecek
66 1.1 jdolecek #include <sys/param.h>
67 1.1 jdolecek #include <sys/systm.h>
68 1.1 jdolecek #include <sys/kernel.h>
69 1.1 jdolecek #include <sys/device.h>
70 1.1 jdolecek #include <sys/errno.h>
71 1.3 gehenna #include <sys/conf.h>
72 1.4 jdolecek #include <sys/event.h>
73 1.8 drochner #include <sys/vnode.h>
74 1.15 ad #include <sys/bus.h>
75 1.18 jmcneill #include <sys/joystick.h>
76 1.1 jdolecek
77 1.1 jdolecek #include <dev/ic/joyvar.h>
78 1.1 jdolecek
79 1.1 jdolecek /*
80 1.1 jdolecek * The game port can manage 4 buttons and 4 variable resistors (usually 2
81 1.1 jdolecek * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
82 1.1 jdolecek * Getting the state of the buttons is done by reading the game port;
83 1.1 jdolecek * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
84 1.1 jdolecek * to bits 0-3. If button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5,
85 1.1 jdolecek * 6, 7) is set to 0 to get the value of a resistor, write the value 0xff
86 1.1 jdolecek * at port and wait until the corresponding bit returns to 0.
87 1.1 jdolecek */
88 1.1 jdolecek
89 1.1 jdolecek
90 1.1 jdolecek #define JOYPART(d) (minor(d) & 1)
91 1.8 drochner #define JOYUNIT(d) (minor(d) >> 1)
92 1.1 jdolecek
93 1.1 jdolecek #ifndef JOY_TIMEOUT
94 1.1 jdolecek #define JOY_TIMEOUT 2000 /* 2 milliseconds */
95 1.1 jdolecek #endif
96 1.1 jdolecek
97 1.3 gehenna extern struct cfdriver joy_cd;
98 1.1 jdolecek
99 1.18 jmcneill static dev_type_open(joyopen);
100 1.18 jmcneill static dev_type_close(joyclose);
101 1.18 jmcneill static dev_type_read(joyread);
102 1.18 jmcneill static dev_type_ioctl(joyioctl);
103 1.3 gehenna
104 1.3 gehenna const struct cdevsw joy_cdevsw = {
105 1.19 dholland .d_open = joyopen,
106 1.19 dholland .d_close = joyclose,
107 1.19 dholland .d_read = joyread,
108 1.19 dholland .d_write = nowrite,
109 1.19 dholland .d_ioctl = joyioctl,
110 1.19 dholland .d_stop = nostop,
111 1.19 dholland .d_tty = notty,
112 1.19 dholland .d_poll = nopoll,
113 1.19 dholland .d_mmap = nommap,
114 1.19 dholland .d_kqfilter = nokqfilter,
115 1.20 dholland .d_discard = nodiscard,
116 1.19 dholland .d_flag = D_OTHER | D_MPSAFE
117 1.3 gehenna };
118 1.1 jdolecek
119 1.1 jdolecek void
120 1.17 xtraeme joyattach(struct joy_softc *sc)
121 1.1 jdolecek {
122 1.18 jmcneill
123 1.18 jmcneill if (sc->sc_lock == NULL) {
124 1.18 jmcneill panic("joyattach: no lock");
125 1.18 jmcneill }
126 1.18 jmcneill
127 1.18 jmcneill sc->timeout[0] = 0;
128 1.18 jmcneill sc->timeout[1] = 0;
129 1.18 jmcneill
130 1.18 jmcneill mutex_enter(sc->sc_lock);
131 1.1 jdolecek bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff);
132 1.1 jdolecek DELAY(10000); /* 10 ms delay */
133 1.17 xtraeme aprint_normal_dev(sc->sc_dev, "joystick %sconnected\n",
134 1.1 jdolecek (bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ?
135 1.1 jdolecek "not " : "");
136 1.18 jmcneill mutex_exit(sc->sc_lock);
137 1.1 jdolecek }
138 1.1 jdolecek
139 1.1 jdolecek int
140 1.13 christos joydetach(struct joy_softc *sc, int flags)
141 1.8 drochner {
142 1.8 drochner int maj, mn;
143 1.8 drochner
144 1.8 drochner maj = cdevsw_lookup_major(&joy_cdevsw);
145 1.17 xtraeme mn = device_unit(sc->sc_dev) << 1;
146 1.8 drochner vdevgone(maj, mn, mn, VCHR);
147 1.8 drochner vdevgone(maj, mn + 1, mn + 1, VCHR);
148 1.8 drochner
149 1.17 xtraeme return 0;
150 1.8 drochner }
151 1.8 drochner
152 1.18 jmcneill static int
153 1.13 christos joyopen(dev_t dev, int flag, int mode, struct lwp *l)
154 1.1 jdolecek {
155 1.1 jdolecek int unit = JOYUNIT(dev);
156 1.1 jdolecek int i = JOYPART(dev);
157 1.1 jdolecek struct joy_softc *sc;
158 1.1 jdolecek
159 1.17 xtraeme sc = device_lookup_private(&joy_cd, unit);
160 1.17 xtraeme if (sc == NULL)
161 1.17 xtraeme return ENXIO;
162 1.1 jdolecek
163 1.18 jmcneill mutex_enter(sc->sc_lock);
164 1.18 jmcneill if (sc->timeout[i]) {
165 1.18 jmcneill mutex_exit(sc->sc_lock);
166 1.17 xtraeme return EBUSY;
167 1.18 jmcneill }
168 1.1 jdolecek sc->x_off[i] = sc->y_off[i] = 0;
169 1.1 jdolecek sc->timeout[i] = JOY_TIMEOUT;
170 1.18 jmcneill mutex_exit(sc->sc_lock);
171 1.17 xtraeme return 0;
172 1.1 jdolecek }
173 1.1 jdolecek
174 1.18 jmcneill static int
175 1.17 xtraeme joyclose(dev_t dev, int flag, int mode, struct lwp *l)
176 1.1 jdolecek {
177 1.1 jdolecek int unit = JOYUNIT(dev);
178 1.1 jdolecek int i = JOYPART(dev);
179 1.17 xtraeme struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
180 1.1 jdolecek
181 1.18 jmcneill mutex_enter(sc->sc_lock);
182 1.1 jdolecek sc->timeout[i] = 0;
183 1.18 jmcneill mutex_exit(sc->sc_lock);
184 1.17 xtraeme return 0;
185 1.1 jdolecek }
186 1.1 jdolecek
187 1.18 jmcneill static int
188 1.13 christos joyread(dev_t dev, struct uio *uio, int flag)
189 1.1 jdolecek {
190 1.1 jdolecek int unit = JOYUNIT(dev);
191 1.17 xtraeme struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
192 1.1 jdolecek bus_space_tag_t iot = sc->sc_iot;
193 1.1 jdolecek bus_space_handle_t ioh = sc->sc_ioh;
194 1.1 jdolecek struct joystick c;
195 1.7 drochner struct timeval start, now, diff;
196 1.18 jmcneill int state = 0, x = 0, y = 0, i;
197 1.1 jdolecek
198 1.18 jmcneill mutex_enter(sc->sc_lock);
199 1.1 jdolecek bus_space_write_1(iot, ioh, 0, 0xff);
200 1.7 drochner microtime(&start);
201 1.7 drochner now = start; /* structure assignment */
202 1.7 drochner i = sc->timeout[JOYPART(dev)];
203 1.7 drochner for (;;) {
204 1.7 drochner timersub(&now, &start, &diff);
205 1.7 drochner if (diff.tv_sec > 0 || diff.tv_usec > i)
206 1.7 drochner break;
207 1.1 jdolecek state = bus_space_read_1(iot, ioh, 0);
208 1.1 jdolecek if (JOYPART(dev) == 1)
209 1.1 jdolecek state >>= 2;
210 1.1 jdolecek if (!x && !(state & 0x01))
211 1.7 drochner x = diff.tv_usec;
212 1.1 jdolecek if (!y && !(state & 0x02))
213 1.7 drochner y = diff.tv_usec;
214 1.1 jdolecek if (x && y)
215 1.1 jdolecek break;
216 1.7 drochner microtime(&now);
217 1.1 jdolecek }
218 1.18 jmcneill mutex_exit(sc->sc_lock);
219 1.1 jdolecek
220 1.7 drochner c.x = x ? sc->x_off[JOYPART(dev)] + x : 0x80000000;
221 1.7 drochner c.y = y ? sc->y_off[JOYPART(dev)] + y : 0x80000000;
222 1.1 jdolecek state >>= 4;
223 1.1 jdolecek c.b1 = ~state & 1;
224 1.1 jdolecek c.b2 = ~(state >> 1) & 1;
225 1.17 xtraeme return uiomove(&c, sizeof(struct joystick), uio);
226 1.1 jdolecek }
227 1.1 jdolecek
228 1.18 jmcneill static int
229 1.17 xtraeme joyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
230 1.1 jdolecek {
231 1.1 jdolecek int unit = JOYUNIT(dev);
232 1.17 xtraeme struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
233 1.18 jmcneill int i = JOYPART(dev), x, error;
234 1.1 jdolecek
235 1.18 jmcneill mutex_enter(sc->sc_lock);
236 1.18 jmcneill error = 0;
237 1.1 jdolecek switch (cmd) {
238 1.1 jdolecek case JOY_SETTIMEOUT:
239 1.17 xtraeme x = *(int *)data;
240 1.18 jmcneill if (x < 1 || x > 10000) { /* 10ms maximum! */
241 1.18 jmcneill error = EINVAL;
242 1.18 jmcneill break;
243 1.18 jmcneill }
244 1.1 jdolecek sc->timeout[i] = x;
245 1.1 jdolecek break;
246 1.1 jdolecek case JOY_GETTIMEOUT:
247 1.17 xtraeme *(int *)data = sc->timeout[i];
248 1.1 jdolecek break;
249 1.1 jdolecek case JOY_SET_X_OFFSET:
250 1.17 xtraeme sc->x_off[i] = *(int *)data;
251 1.1 jdolecek break;
252 1.1 jdolecek case JOY_SET_Y_OFFSET:
253 1.17 xtraeme sc->y_off[i] = *(int *)data;
254 1.1 jdolecek break;
255 1.1 jdolecek case JOY_GET_X_OFFSET:
256 1.17 xtraeme *(int *)data = sc->x_off[i];
257 1.1 jdolecek break;
258 1.1 jdolecek case JOY_GET_Y_OFFSET:
259 1.17 xtraeme *(int *)data = sc->y_off[i];
260 1.1 jdolecek break;
261 1.1 jdolecek default:
262 1.18 jmcneill error = ENXIO;
263 1.18 jmcneill break;
264 1.1 jdolecek }
265 1.18 jmcneill mutex_exit(sc->sc_lock);
266 1.18 jmcneill return error;
267 1.1 jdolecek }
268