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