Deleted Added
full compact
joy.c (97748) joy.c (111815)
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 *
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 * $FreeBSD: head/sys/dev/joy/joy.c 97748 2002-06-02 20:05:59Z schweikh $
28 * $FreeBSD: head/sys/dev/joy/joy.c 111815 2003-03-03 12:15:54Z phk $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/conf.h>
34#include <sys/uio.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/bus.h>
38#include <machine/bus.h>
39#include <machine/resource.h>
40#include <sys/rman.h>
41#include <sys/time.h>
42#include <sys/joystick.h>
43#include <dev/joy/joyvar.h>
44
45/* The game port can manage 4 buttons and 4 variable resistors (usually 2
46 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
47 * Getting the state of the buttons is done by reading the game port:
48 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
49 * to bits 0-3.
50 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
51 * to get the value of a resistor, write the value 0xff at port and
52 * wait until the corresponding bit returns to 0.
53 */
54
55#define joypart(d) (minor(d)&1)
56#define UNIT(d) ((minor(d)>>1)&3)
57#ifndef JOY_TIMEOUT
58#define JOY_TIMEOUT 2000 /* 2 milliseconds */
59#endif
60
61#define JOY_SOFTC(unit) (struct joy_softc *) \
62 devclass_get_softc(joy_devclass,(unit))
63
64#define CDEV_MAJOR 51
65static d_open_t joyopen;
66static d_close_t joyclose;
67static d_read_t joyread;
68static d_ioctl_t joyioctl;
69
70static struct cdevsw joy_cdevsw = {
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/conf.h>
34#include <sys/uio.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/bus.h>
38#include <machine/bus.h>
39#include <machine/resource.h>
40#include <sys/rman.h>
41#include <sys/time.h>
42#include <sys/joystick.h>
43#include <dev/joy/joyvar.h>
44
45/* The game port can manage 4 buttons and 4 variable resistors (usually 2
46 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
47 * Getting the state of the buttons is done by reading the game port:
48 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
49 * to bits 0-3.
50 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
51 * to get the value of a resistor, write the value 0xff at port and
52 * wait until the corresponding bit returns to 0.
53 */
54
55#define joypart(d) (minor(d)&1)
56#define UNIT(d) ((minor(d)>>1)&3)
57#ifndef JOY_TIMEOUT
58#define JOY_TIMEOUT 2000 /* 2 milliseconds */
59#endif
60
61#define JOY_SOFTC(unit) (struct joy_softc *) \
62 devclass_get_softc(joy_devclass,(unit))
63
64#define CDEV_MAJOR 51
65static d_open_t joyopen;
66static d_close_t joyclose;
67static d_read_t joyread;
68static d_ioctl_t joyioctl;
69
70static struct cdevsw joy_cdevsw = {
71 /* open */ joyopen,
72 /* close */ joyclose,
73 /* read */ joyread,
74 /* write */ nowrite,
75 /* ioctl */ joyioctl,
76 /* poll */ nopoll,
77 /* mmap */ nommap,
78 /* strategy */ nostrategy,
79 /* name */ "joy",
80 /* maj */ CDEV_MAJOR,
81 /* dump */ nodump,
82 /* psize */ nopsize,
83 /* flags */ 0,
71 .d_open = joyopen,
72 .d_close = joyclose,
73 .d_read = joyread,
74 .d_ioctl = joyioctl,
75 .d_name = "joy",
76 .d_maj = CDEV_MAJOR,
84};
85
86devclass_t joy_devclass;
87
88int
89joy_probe(device_t dev)
90{
91#ifdef WANT_JOYSTICK_CONNECTED
92#ifdef notyet
93 outb(dev->id_iobase, 0xff);
94 DELAY(10000); /* 10 ms delay */
95 return (inb(dev->id_iobase) & 0x0f) != 0x0f;
96#else
97 return (0);
98#endif
99#else
100 return (0);
101#endif
102}
103
104int
105joy_attach(device_t dev)
106{
107 int unit = device_get_unit(dev);
108 struct joy_softc *joy = device_get_softc(dev);
109
110 joy->rid = 0;
111 joy->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &joy->rid, 0, ~0, 1,
112 RF_ACTIVE);
113 if (joy->res == NULL)
114 return ENXIO;
115 joy->bt = rman_get_bustag(joy->res);
116 joy->port = rman_get_bushandle(joy->res);
117 joy->timeout[0] = joy->timeout[1] = 0;
118 joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
119 return (0);
120}
121
122int
123joy_detach(device_t dev)
124{
125 struct joy_softc *joy = device_get_softc(dev);
126
127 if (joy->res != NULL)
128 bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
129 if (joy->d)
130 destroy_dev(joy->d);
131 return (0);
132}
133
134
135static int
136joyopen(dev_t dev, int flags, int fmt, struct thread *td)
137{
138 int i = joypart (dev);
139 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
140
141 if (joy->timeout[i])
142 return (EBUSY);
143 joy->x_off[i] = joy->y_off[i] = 0;
144 joy->timeout[i] = JOY_TIMEOUT;
145 return (0);
146}
147
148static int
149joyclose(dev_t dev, int flags, int fmt, struct thread *td)
150{
151 int i = joypart (dev);
152 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
153
154 joy->timeout[i] = 0;
155 return (0);
156}
157
158static int
159joyread(dev_t dev, struct uio *uio, int flag)
160{
161 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
162 bus_space_handle_t port = joy->port;
163 bus_space_tag_t bt = joy->bt;
164 struct timespec t, start, end;
165 int state = 0;
166 struct timespec x, y;
167 struct joystick c;
168#ifndef __i386__
169 int s;
170
171 s = splhigh();
172#else
173 disable_intr ();
174#endif
175 bus_space_write_1 (bt, port, 0, 0xff);
176 nanotime(&start);
177 end.tv_sec = 0;
178 end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
179 timespecadd(&end, &start);
180 t = start;
181 timespecclear(&x);
182 timespecclear(&y);
183 while (timespeccmp(&t, &end, <)) {
184 state = bus_space_read_1 (bt, port, 0);
185 if (joypart(dev) == 1)
186 state >>= 2;
187 nanotime(&t);
188 if (!timespecisset(&x) && !(state & 0x01))
189 x = t;
190 if (!timespecisset(&y) && !(state & 0x02))
191 y = t;
192 if (timespecisset(&x) && timespecisset(&y))
193 break;
194 }
195#ifndef __i386__
196 splx(s);
197#else
198 enable_intr ();
199#endif
200 if (timespecisset(&x)) {
201 timespecsub(&x, &start);
202 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
203 } else
204 c.x = 0x80000000;
205 if (timespecisset(&y)) {
206 timespecsub(&y, &start);
207 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
208 } else
209 c.y = 0x80000000;
210 state >>= 4;
211 c.b1 = ~state & 1;
212 c.b2 = ~(state >> 1) & 1;
213 return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
214}
215
216static int
217joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
218{
219 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
220 int i = joypart (dev);
221 int x;
222
223 switch (cmd) {
224 case JOY_SETTIMEOUT:
225 x = *(int *) data;
226 if (x < 1 || x > 10000) /* 10ms maximum! */
227 return EINVAL;
228 joy->timeout[i] = x;
229 break;
230 case JOY_GETTIMEOUT:
231 *(int *) data = joy->timeout[i];
232 break;
233 case JOY_SET_X_OFFSET:
234 joy->x_off[i] = *(int *) data;
235 break;
236 case JOY_SET_Y_OFFSET:
237 joy->y_off[i] = *(int *) data;
238 break;
239 case JOY_GET_X_OFFSET:
240 *(int *) data = joy->x_off[i];
241 break;
242 case JOY_GET_Y_OFFSET:
243 *(int *) data = joy->y_off[i];
244 break;
245 default:
246 return (ENOTTY);
247 }
248 return (0);
249}
77};
78
79devclass_t joy_devclass;
80
81int
82joy_probe(device_t dev)
83{
84#ifdef WANT_JOYSTICK_CONNECTED
85#ifdef notyet
86 outb(dev->id_iobase, 0xff);
87 DELAY(10000); /* 10 ms delay */
88 return (inb(dev->id_iobase) & 0x0f) != 0x0f;
89#else
90 return (0);
91#endif
92#else
93 return (0);
94#endif
95}
96
97int
98joy_attach(device_t dev)
99{
100 int unit = device_get_unit(dev);
101 struct joy_softc *joy = device_get_softc(dev);
102
103 joy->rid = 0;
104 joy->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &joy->rid, 0, ~0, 1,
105 RF_ACTIVE);
106 if (joy->res == NULL)
107 return ENXIO;
108 joy->bt = rman_get_bustag(joy->res);
109 joy->port = rman_get_bushandle(joy->res);
110 joy->timeout[0] = joy->timeout[1] = 0;
111 joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
112 return (0);
113}
114
115int
116joy_detach(device_t dev)
117{
118 struct joy_softc *joy = device_get_softc(dev);
119
120 if (joy->res != NULL)
121 bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
122 if (joy->d)
123 destroy_dev(joy->d);
124 return (0);
125}
126
127
128static int
129joyopen(dev_t dev, int flags, int fmt, struct thread *td)
130{
131 int i = joypart (dev);
132 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
133
134 if (joy->timeout[i])
135 return (EBUSY);
136 joy->x_off[i] = joy->y_off[i] = 0;
137 joy->timeout[i] = JOY_TIMEOUT;
138 return (0);
139}
140
141static int
142joyclose(dev_t dev, int flags, int fmt, struct thread *td)
143{
144 int i = joypart (dev);
145 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
146
147 joy->timeout[i] = 0;
148 return (0);
149}
150
151static int
152joyread(dev_t dev, struct uio *uio, int flag)
153{
154 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
155 bus_space_handle_t port = joy->port;
156 bus_space_tag_t bt = joy->bt;
157 struct timespec t, start, end;
158 int state = 0;
159 struct timespec x, y;
160 struct joystick c;
161#ifndef __i386__
162 int s;
163
164 s = splhigh();
165#else
166 disable_intr ();
167#endif
168 bus_space_write_1 (bt, port, 0, 0xff);
169 nanotime(&start);
170 end.tv_sec = 0;
171 end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
172 timespecadd(&end, &start);
173 t = start;
174 timespecclear(&x);
175 timespecclear(&y);
176 while (timespeccmp(&t, &end, <)) {
177 state = bus_space_read_1 (bt, port, 0);
178 if (joypart(dev) == 1)
179 state >>= 2;
180 nanotime(&t);
181 if (!timespecisset(&x) && !(state & 0x01))
182 x = t;
183 if (!timespecisset(&y) && !(state & 0x02))
184 y = t;
185 if (timespecisset(&x) && timespecisset(&y))
186 break;
187 }
188#ifndef __i386__
189 splx(s);
190#else
191 enable_intr ();
192#endif
193 if (timespecisset(&x)) {
194 timespecsub(&x, &start);
195 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
196 } else
197 c.x = 0x80000000;
198 if (timespecisset(&y)) {
199 timespecsub(&y, &start);
200 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
201 } else
202 c.y = 0x80000000;
203 state >>= 4;
204 c.b1 = ~state & 1;
205 c.b2 = ~(state >> 1) & 1;
206 return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
207}
208
209static int
210joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
211{
212 struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
213 int i = joypart (dev);
214 int x;
215
216 switch (cmd) {
217 case JOY_SETTIMEOUT:
218 x = *(int *) data;
219 if (x < 1 || x > 10000) /* 10ms maximum! */
220 return EINVAL;
221 joy->timeout[i] = x;
222 break;
223 case JOY_GETTIMEOUT:
224 *(int *) data = joy->timeout[i];
225 break;
226 case JOY_SET_X_OFFSET:
227 joy->x_off[i] = *(int *) data;
228 break;
229 case JOY_SET_Y_OFFSET:
230 joy->y_off[i] = *(int *) data;
231 break;
232 case JOY_GET_X_OFFSET:
233 *(int *) data = joy->x_off[i];
234 break;
235 case JOY_GET_Y_OFFSET:
236 *(int *) data = joy->y_off[i];
237 break;
238 default:
239 return (ENOTTY);
240 }
241 return (0);
242}