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