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