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