xref: /dragonfly/sys/dev/misc/joy/joy.c (revision 71126e33)
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 withough 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: src/sys/isa/joy.c,v 1.38.2.1 2001/09/01 05:55:31 murray Exp $
29  * $DragonFly: src/sys/dev/misc/joy/joy.c,v 1.7 2004/05/19 22:52:42 dillon Exp $
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/uio.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 #include <sys/time.h>
43 #include <sys/joystick.h>
44 
45 #include <bus/isa/isavar.h>
46 #include "isa_if.h"
47 
48 /* The game port can manage 4 buttons and 4 variable resistors (usually 2
49  * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
50  * Getting the state of the buttons is done by reading the game port:
51  * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
52  * to bits 0-3.
53  * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
54  * to get the value of a resistor, write the value 0xff at port and
55  * wait until the corresponding bit returns to 0.
56  */
57 
58 #define joypart(d) (minor(d)&1)
59 #define UNIT(d) ((minor(d)>>1)&3)
60 #ifndef JOY_TIMEOUT
61 #define JOY_TIMEOUT   2000 /* 2 milliseconds */
62 #endif
63 
64 struct joy_softc {
65     bus_space_tag_t  bt;
66     bus_space_handle_t port;
67     int x_off[2], y_off[2];
68     int timeout[2];
69 };
70 
71 #define JOY_SOFTC(unit) (struct joy_softc *) \
72         devclass_get_softc(joy_devclass,(unit))
73 
74 static int joy_probe (device_t);
75 static int joy_attach (device_t);
76 
77 #define CDEV_MAJOR 51
78 static	d_open_t	joyopen;
79 static	d_close_t	joyclose;
80 static	d_read_t	joyread;
81 static	d_ioctl_t	joyioctl;
82 
83 static struct cdevsw joy_cdevsw = {
84 	/* name */	"joy",
85 	/* maj */	CDEV_MAJOR,
86 	/* flags */	0,
87 	/* port */	NULL,
88 	/* clone */	NULL,
89 
90 	/* open */	joyopen,
91 	/* close */	joyclose,
92 	/* read */	joyread,
93 	/* write */	nowrite,
94 	/* ioctl */	joyioctl,
95 	/* poll */	nopoll,
96 	/* mmap */	nommap,
97 	/* strategy */	nostrategy,
98 	/* dump */	nodump,
99 	/* psize */	nopsize
100 };
101 
102 devclass_t joy_devclass;
103 
104 static struct isa_pnp_id joy_ids[] = {
105     {0x0100630e, "CSC0001 PnP Joystick"},	/* CSC0001 */
106     {0x0101630e, "CSC0101 PnP Joystick"},	/* CSC0101 */
107     {0x01100002, "ALS0110 PnP Joystick"},	/* @P@1001 */
108     {0x01200002, "ALS0120 PnP Joystick"},	/* @P@2001 */
109     {0x01007316, "ESS0001 PnP Joystick"},	/* ESS0001 */
110     {0x2fb0d041, "Generic PnP Joystick"},	/* PNPb02f */
111     {0x2200a865, "YMH0022 PnP Joystick"},	/* YMH0022 */
112     {0x82719304, NULL},    			/* ADS7182 */
113     {0}
114 };
115 
116 static int
117 joy_probe (device_t dev)
118 {
119     if (ISA_PNP_PROBE(device_get_parent(dev), dev, joy_ids) == ENXIO)
120         return ENXIO;
121 #ifdef WANT_JOYSTICK_CONNECTED
122 #ifdef notyet
123     outb (dev->id_iobase, 0xff);
124     DELAY (10000); /*  10 ms delay */
125     return (inb (dev->id_iobase) & 0x0f) != 0x0f;
126 #endif
127 #else
128     return 0;
129 #endif
130 }
131 
132 static int
133 joy_attach (device_t dev)
134 {
135     int	unit = device_get_unit(dev);
136     int rid = 0;
137     struct resource *res;
138     struct joy_softc *joy = device_get_softc(dev);
139 
140     res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
141     if (res == NULL)
142         return ENXIO;
143     joy->bt = rman_get_bustag(res);
144     joy->port = rman_get_bushandle(res);
145     joy->timeout[0] = joy->timeout[1] = 0;
146     cdevsw_add(&joy_cdevsw, -1, unit);
147     make_dev(&joy_cdevsw, unit, 0, 0, 0600, "joy%d", unit);
148     return 0;
149 }
150 
151 static device_method_t joy_methods[] = {
152     DEVMETHOD(device_probe,	joy_probe),
153     DEVMETHOD(device_attach,	joy_attach),
154     { 0, 0 }
155 };
156 
157 static driver_t joy_isa_driver = {
158     "joy",
159     joy_methods,
160     sizeof (struct joy_softc)
161 };
162 
163 DRIVER_MODULE(joy, isa, joy_isa_driver, joy_devclass, 0, 0);
164 
165 static int
166 joyopen(dev_t dev, int flags, int fmt, d_thread_t *td)
167 {
168     int i = joypart (dev);
169     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
170 
171     if (joy->timeout[i])
172 	return EBUSY;
173     joy->x_off[i] = joy->y_off[i] = 0;
174     joy->timeout[i] = JOY_TIMEOUT;
175     return 0;
176 }
177 
178 static int
179 joyclose(dev_t dev, int flags, int fmt, d_thread_t *td)
180 {
181     int i = joypart (dev);
182     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
183 
184     joy->timeout[i] = 0;
185     return 0;
186 }
187 
188 static int
189 joyread(dev_t dev, struct uio *uio, int flag)
190 {
191     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
192     bus_space_handle_t port = joy->port;
193     bus_space_tag_t bt = joy->bt;
194     struct timespec t, start, end;
195     int state = 0;
196     struct timespec x, y;
197     struct joystick c;
198 #ifndef i386
199     int s;
200 
201     s = splhigh();
202 #else
203     disable_intr ();
204 #endif
205     bus_space_write_1 (bt, port, 0, 0xff);
206     nanotime(&start);
207     end.tv_sec = 0;
208     end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
209     timespecadd(&end, &start);
210     t = start;
211     timespecclear(&x);
212     timespecclear(&y);
213     while (timespeccmp(&t, &end, <)) {
214 	state = bus_space_read_1 (bt, port, 0);
215 	if (joypart(dev) == 1)
216 	    state >>= 2;
217 	nanotime(&t);
218 	if (!timespecisset(&x) && !(state & 0x01))
219 	    x = t;
220 	if (!timespecisset(&y) && !(state & 0x02))
221 	    y = t;
222 	if (timespecisset(&x) && timespecisset(&y))
223 	    break;
224     }
225 #ifndef i386
226     splx(s);
227 #else
228     enable_intr ();
229 #endif
230     if (timespecisset(&x)) {
231 	timespecsub(&x, &start);
232 	c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
233     } else
234 	c.x = 0x80000000;
235     if (timespecisset(&y)) {
236 	timespecsub(&y, &start);
237 	c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
238     } else
239 	c.y = 0x80000000;
240     state >>= 4;
241     c.b1 = ~state & 1;
242     c.b2 = ~(state >> 1) & 1;
243     return uiomove ((caddr_t)&c, sizeof(struct joystick), uio);
244 }
245 
246 static int
247 joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
248 {
249     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
250     int i = joypart (dev);
251     int x;
252 
253     switch (cmd) {
254     case JOY_SETTIMEOUT:
255 	x = *(int *) data;
256 	if (x < 1 || x > 10000) /* 10ms maximum! */
257 	    return EINVAL;
258 	joy->timeout[i] = x;
259 	break;
260     case JOY_GETTIMEOUT:
261 	*(int *) data = joy->timeout[i];
262 	break;
263     case JOY_SET_X_OFFSET:
264 	joy->x_off[i] = *(int *) data;
265 	break;
266     case JOY_SET_Y_OFFSET:
267 	joy->y_off[i] = *(int *) data;
268 	break;
269     case JOY_GET_X_OFFSET:
270 	*(int *) data = joy->x_off[i];
271 	break;
272     case JOY_GET_Y_OFFSET:
273 	*(int *) data = joy->y_off[i];
274 	break;
275     default:
276 	return ENXIO;
277     }
278     return 0;
279 }
280