]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/joy/joy.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / joy / joy.c
1 /*-
2  * Copyright (c) 1995 Jean-Marc Zucconi
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/uio.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/rman.h>
43 #include <sys/time.h>
44 #include <sys/joystick.h>
45 #include <dev/joy/joyvar.h>
46
47 /* The game port can manage 4 buttons and 4 variable resistors (usually 2
48  * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
49  * Getting the state of the buttons is done by reading the game port:
50  * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
51  * to bits 0-3.
52  * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
53  * to get the value of a resistor, write the value 0xff at port and
54  * wait until the corresponding bit returns to 0.
55  */
56
57 #define joypart(d) (minor(d)&1)
58 #define UNIT(d) ((minor(d)>>1)&3)
59 #ifndef JOY_TIMEOUT
60 #define JOY_TIMEOUT   2000 /* 2 milliseconds */
61 #endif
62
63 #define JOY_SOFTC(unit) (struct joy_softc *) \
64         devclass_get_softc(joy_devclass,(unit))
65
66 static  d_open_t        joyopen;
67 static  d_close_t       joyclose;
68 static  d_read_t        joyread;
69 static  d_ioctl_t       joyioctl;
70
71 static struct cdevsw joy_cdevsw = {
72         .d_version =    D_VERSION,
73         .d_flags =      D_NEEDGIANT,
74         .d_open =       joyopen,
75         .d_close =      joyclose,
76         .d_read =       joyread,
77         .d_ioctl =      joyioctl,
78         .d_name =       "joy",
79 };
80
81 devclass_t joy_devclass;
82
83 int
84 joy_probe(device_t dev)
85 {
86 #ifdef WANT_JOYSTICK_CONNECTED
87 #ifdef notyet
88         outb(dev->id_iobase, 0xff);
89         DELAY(10000); /*  10 ms delay */
90         return (inb(dev->id_iobase) & 0x0f) != 0x0f;
91 #else
92         return (0);
93 #endif
94 #else
95         return (0);
96 #endif
97 }
98
99 int
100 joy_attach(device_t dev)
101 {
102         int     unit = device_get_unit(dev);
103         struct joy_softc *joy = device_get_softc(dev);
104
105         joy->rid = 0;
106         joy->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &joy->rid,
107             RF_ACTIVE|RF_SHAREABLE);
108         if (joy->res == NULL)
109                 return ENXIO;
110         joy->bt = rman_get_bustag(joy->res);
111         joy->port = rman_get_bushandle(joy->res);
112         joy->timeout[0] = joy->timeout[1] = 0;
113         joy->d = make_dev(&joy_cdevsw, unit, 0, 0, 0600, "joy%d", unit);
114         return (0);
115 }
116
117 int
118 joy_detach(device_t dev)
119 {
120         struct joy_softc *joy = device_get_softc(dev);
121
122         if (joy->res != NULL)
123                 bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
124         if (joy->d)
125                 destroy_dev(joy->d);
126         return (0);
127 }
128
129
130 static int
131 joyopen(struct cdev *dev, int flags, int fmt, struct thread *td)
132 {
133         int i = joypart (dev);
134         struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
135
136         if (joy->timeout[i])
137                 return (EBUSY);
138         joy->x_off[i] = joy->y_off[i] = 0;
139         joy->timeout[i] = JOY_TIMEOUT;
140         return (0);
141 }
142
143 static int
144 joyclose(struct cdev *dev, int flags, int fmt, struct thread *td)
145 {
146         int i = joypart (dev);
147         struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
148
149         joy->timeout[i] = 0;
150         return (0);
151 }
152
153 static int
154 joyread(struct cdev *dev, struct uio *uio, int flag)
155 {
156         struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
157         bus_space_handle_t port = joy->port;
158         bus_space_tag_t bt = joy->bt;
159         struct timespec t, start, end;
160         int state = 0;
161         struct timespec x, y;
162         struct joystick c;
163 #ifndef __i386__
164         int s;
165
166         s = splhigh();
167 #else
168         disable_intr ();
169 #endif
170         nanotime(&t);
171         end.tv_sec = 0;
172         end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
173         timespecadd(&end, &t);
174         for (; timespeccmp(&t, &end, <) && (bus_space_read_1(bt, port, 0) & 0x0f); nanotime(&t))
175                 ;       /* nothing */
176         bus_space_write_1 (bt, port, 0, 0xff);
177         nanotime(&start);
178         end.tv_sec = 0;
179         end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
180         timespecadd(&end, &start);
181         t = start;
182         timespecclear(&x);
183         timespecclear(&y);
184         while (timespeccmp(&t, &end, <)) {
185                 state = bus_space_read_1 (bt, port, 0);
186                 if (joypart(dev) == 1)
187                         state >>= 2;
188                 nanotime(&t);
189                 if (!timespecisset(&x) && !(state & 0x01))
190                         x = t;
191                 if (!timespecisset(&y) && !(state & 0x02))
192                         y = t;
193                 if (timespecisset(&x) && timespecisset(&y))
194                         break;
195         }
196 #ifndef __i386__
197         splx(s);
198 #else
199         enable_intr ();
200 #endif
201         if (timespecisset(&x)) {
202                 timespecsub(&x, &start);
203                 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
204         } else
205                 c.x = 0x80000000;
206         if (timespecisset(&y)) {
207                 timespecsub(&y, &start);
208                 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
209         } else
210                 c.y = 0x80000000;
211         state >>= 4;
212         c.b1 = ~state & 1;
213         c.b2 = ~(state >> 1) & 1;
214         return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
215 }
216
217 static int
218 joyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
219 {
220         struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
221         int i = joypart (dev);
222         int x;
223
224         switch (cmd) {
225         case JOY_SETTIMEOUT:
226                 x = *(int *) data;
227                 if (x < 1 || x > 10000) /* 10ms maximum! */
228                         return EINVAL;
229                 joy->timeout[i] = x;
230                 break;
231         case JOY_GETTIMEOUT:
232                 *(int *) data = joy->timeout[i];
233                 break;
234         case JOY_SET_X_OFFSET:
235                 joy->x_off[i] = *(int *) data;
236                 break;
237         case JOY_SET_Y_OFFSET:
238                 joy->y_off[i] = *(int *) data;
239                 break;
240         case JOY_GET_X_OFFSET:
241                 *(int *) data = joy->x_off[i];
242                 break;
243         case JOY_GET_Y_OFFSET:
244                 *(int *) data = joy->y_off[i];
245                 break;
246         default:
247                 return (ENOTTY);
248         }
249         return (0);
250 }