joy.c revision 1.18 1 1.18 jmcneill /* $NetBSD: joy.c,v 1.18 2011/11/23 23:07:32 jmcneill 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.18 jmcneill __KERNEL_RCSID(0, "$NetBSD: joy.c,v 1.18 2011/11/23 23:07:32 jmcneill 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.3 gehenna joyopen, joyclose, joyread, nowrite, joyioctl,
106 1.18 jmcneill nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE,
107 1.3 gehenna };
108 1.1 jdolecek
109 1.1 jdolecek void
110 1.17 xtraeme joyattach(struct joy_softc *sc)
111 1.1 jdolecek {
112 1.18 jmcneill
113 1.18 jmcneill if (sc->sc_lock == NULL) {
114 1.18 jmcneill panic("joyattach: no lock");
115 1.18 jmcneill }
116 1.18 jmcneill
117 1.18 jmcneill sc->timeout[0] = 0;
118 1.18 jmcneill sc->timeout[1] = 0;
119 1.18 jmcneill
120 1.18 jmcneill mutex_enter(sc->sc_lock);
121 1.1 jdolecek bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff);
122 1.1 jdolecek DELAY(10000); /* 10 ms delay */
123 1.17 xtraeme aprint_normal_dev(sc->sc_dev, "joystick %sconnected\n",
124 1.1 jdolecek (bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ?
125 1.1 jdolecek "not " : "");
126 1.18 jmcneill mutex_exit(sc->sc_lock);
127 1.1 jdolecek }
128 1.1 jdolecek
129 1.1 jdolecek int
130 1.13 christos joydetach(struct joy_softc *sc, int flags)
131 1.8 drochner {
132 1.8 drochner int maj, mn;
133 1.8 drochner
134 1.8 drochner maj = cdevsw_lookup_major(&joy_cdevsw);
135 1.17 xtraeme mn = device_unit(sc->sc_dev) << 1;
136 1.8 drochner vdevgone(maj, mn, mn, VCHR);
137 1.8 drochner vdevgone(maj, mn + 1, mn + 1, VCHR);
138 1.8 drochner
139 1.17 xtraeme return 0;
140 1.8 drochner }
141 1.8 drochner
142 1.18 jmcneill static int
143 1.13 christos joyopen(dev_t dev, int flag, int mode, struct lwp *l)
144 1.1 jdolecek {
145 1.1 jdolecek int unit = JOYUNIT(dev);
146 1.1 jdolecek int i = JOYPART(dev);
147 1.1 jdolecek struct joy_softc *sc;
148 1.1 jdolecek
149 1.17 xtraeme sc = device_lookup_private(&joy_cd, unit);
150 1.17 xtraeme if (sc == NULL)
151 1.17 xtraeme return ENXIO;
152 1.1 jdolecek
153 1.18 jmcneill mutex_enter(sc->sc_lock);
154 1.18 jmcneill if (sc->timeout[i]) {
155 1.18 jmcneill mutex_exit(sc->sc_lock);
156 1.17 xtraeme return EBUSY;
157 1.18 jmcneill }
158 1.1 jdolecek sc->x_off[i] = sc->y_off[i] = 0;
159 1.1 jdolecek sc->timeout[i] = JOY_TIMEOUT;
160 1.18 jmcneill mutex_exit(sc->sc_lock);
161 1.17 xtraeme return 0;
162 1.1 jdolecek }
163 1.1 jdolecek
164 1.18 jmcneill static int
165 1.17 xtraeme joyclose(dev_t dev, int flag, int mode, struct lwp *l)
166 1.1 jdolecek {
167 1.1 jdolecek int unit = JOYUNIT(dev);
168 1.1 jdolecek int i = JOYPART(dev);
169 1.17 xtraeme struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
170 1.1 jdolecek
171 1.18 jmcneill mutex_enter(sc->sc_lock);
172 1.1 jdolecek sc->timeout[i] = 0;
173 1.18 jmcneill mutex_exit(sc->sc_lock);
174 1.17 xtraeme return 0;
175 1.1 jdolecek }
176 1.1 jdolecek
177 1.18 jmcneill static int
178 1.13 christos joyread(dev_t dev, struct uio *uio, int flag)
179 1.1 jdolecek {
180 1.1 jdolecek int unit = JOYUNIT(dev);
181 1.17 xtraeme struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
182 1.1 jdolecek bus_space_tag_t iot = sc->sc_iot;
183 1.1 jdolecek bus_space_handle_t ioh = sc->sc_ioh;
184 1.1 jdolecek struct joystick c;
185 1.7 drochner struct timeval start, now, diff;
186 1.18 jmcneill int state = 0, x = 0, y = 0, i;
187 1.1 jdolecek
188 1.18 jmcneill mutex_enter(sc->sc_lock);
189 1.1 jdolecek bus_space_write_1(iot, ioh, 0, 0xff);
190 1.7 drochner microtime(&start);
191 1.7 drochner now = start; /* structure assignment */
192 1.7 drochner i = sc->timeout[JOYPART(dev)];
193 1.7 drochner for (;;) {
194 1.7 drochner timersub(&now, &start, &diff);
195 1.7 drochner if (diff.tv_sec > 0 || diff.tv_usec > i)
196 1.7 drochner break;
197 1.1 jdolecek state = bus_space_read_1(iot, ioh, 0);
198 1.1 jdolecek if (JOYPART(dev) == 1)
199 1.1 jdolecek state >>= 2;
200 1.1 jdolecek if (!x && !(state & 0x01))
201 1.7 drochner x = diff.tv_usec;
202 1.1 jdolecek if (!y && !(state & 0x02))
203 1.7 drochner y = diff.tv_usec;
204 1.1 jdolecek if (x && y)
205 1.1 jdolecek break;
206 1.7 drochner microtime(&now);
207 1.1 jdolecek }
208 1.18 jmcneill mutex_exit(sc->sc_lock);
209 1.1 jdolecek
210 1.7 drochner c.x = x ? sc->x_off[JOYPART(dev)] + x : 0x80000000;
211 1.7 drochner c.y = y ? sc->y_off[JOYPART(dev)] + y : 0x80000000;
212 1.1 jdolecek state >>= 4;
213 1.1 jdolecek c.b1 = ~state & 1;
214 1.1 jdolecek c.b2 = ~(state >> 1) & 1;
215 1.17 xtraeme return uiomove(&c, sizeof(struct joystick), uio);
216 1.1 jdolecek }
217 1.1 jdolecek
218 1.18 jmcneill static int
219 1.17 xtraeme joyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
220 1.1 jdolecek {
221 1.1 jdolecek int unit = JOYUNIT(dev);
222 1.17 xtraeme struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
223 1.18 jmcneill int i = JOYPART(dev), x, error;
224 1.1 jdolecek
225 1.18 jmcneill mutex_enter(sc->sc_lock);
226 1.18 jmcneill error = 0;
227 1.1 jdolecek switch (cmd) {
228 1.1 jdolecek case JOY_SETTIMEOUT:
229 1.17 xtraeme x = *(int *)data;
230 1.18 jmcneill if (x < 1 || x > 10000) { /* 10ms maximum! */
231 1.18 jmcneill error = EINVAL;
232 1.18 jmcneill break;
233 1.18 jmcneill }
234 1.1 jdolecek sc->timeout[i] = x;
235 1.1 jdolecek break;
236 1.1 jdolecek case JOY_GETTIMEOUT:
237 1.17 xtraeme *(int *)data = sc->timeout[i];
238 1.1 jdolecek break;
239 1.1 jdolecek case JOY_SET_X_OFFSET:
240 1.17 xtraeme sc->x_off[i] = *(int *)data;
241 1.1 jdolecek break;
242 1.1 jdolecek case JOY_SET_Y_OFFSET:
243 1.17 xtraeme sc->y_off[i] = *(int *)data;
244 1.1 jdolecek break;
245 1.1 jdolecek case JOY_GET_X_OFFSET:
246 1.17 xtraeme *(int *)data = sc->x_off[i];
247 1.1 jdolecek break;
248 1.1 jdolecek case JOY_GET_Y_OFFSET:
249 1.17 xtraeme *(int *)data = sc->y_off[i];
250 1.1 jdolecek break;
251 1.1 jdolecek default:
252 1.18 jmcneill error = ENXIO;
253 1.18 jmcneill break;
254 1.1 jdolecek }
255 1.18 jmcneill mutex_exit(sc->sc_lock);
256 1.18 jmcneill return error;
257 1.1 jdolecek }
258