xref: /dragonfly/sys/dev/misc/joy/joy.c (revision 25a2db75)
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: src/sys/isa/joy.c,v 1.38.2.1 2001/09/01 05:55:31 murray Exp $
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/device.h>
35 #include <sys/uio.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/thread2.h>
41 #include <sys/time.h>
42 #include <sys/joystick.h>
43 
44 #include <bus/isa/isavar.h>
45 #include "isa_if.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 struct joy_softc {
64     bus_space_tag_t  bt;
65     bus_space_handle_t port;
66     int x_off[2], y_off[2];
67     int timeout[2];
68 };
69 
70 #define JOY_SOFTC(unit) (struct joy_softc *) \
71         devclass_get_softc(joy_devclass,(unit))
72 
73 static int joy_probe (device_t);
74 static int joy_attach (device_t);
75 
76 static	d_open_t	joyopen;
77 static	d_close_t	joyclose;
78 static	d_read_t	joyread;
79 static	d_ioctl_t	joyioctl;
80 
81 static struct dev_ops joy_ops = {
82 	{ "joy", 0, 0 },
83 	.d_open =	joyopen,
84 	.d_close =	joyclose,
85 	.d_read =	joyread,
86 	.d_ioctl =	joyioctl,
87 };
88 
89 devclass_t joy_devclass;
90 
91 static struct isa_pnp_id joy_ids[] = {
92     {0x0100630e, "CSC0001 PnP Joystick"},	/* CSC0001 */
93     {0x0101630e, "CSC0101 PnP Joystick"},	/* CSC0101 */
94     {0x01100002, "ALS0110 PnP Joystick"},	/* @P@1001 */
95     {0x01200002, "ALS0120 PnP Joystick"},	/* @P@2001 */
96     {0x01007316, "ESS0001 PnP Joystick"},	/* ESS0001 */
97     {0x2fb0d041, "Generic PnP Joystick"},	/* PNPb02f */
98     {0x2200a865, "YMH0022 PnP Joystick"},	/* YMH0022 */
99     {0x82719304, NULL},    			/* ADS7182 */
100     {0}
101 };
102 
103 static int
104 joy_probe (device_t dev)
105 {
106     if (ISA_PNP_PROBE(device_get_parent(dev), dev, joy_ids) == ENXIO)
107         return ENXIO;
108 #ifdef WANT_JOYSTICK_CONNECTED
109 #ifdef notyet
110     outb (dev->id_iobase, 0xff);
111     DELAY (10000); /*  10 ms delay */
112     return (inb (dev->id_iobase) & 0x0f) != 0x0f;
113 #endif
114 #else
115     return 0;
116 #endif
117 }
118 
119 static int
120 joy_attach (device_t dev)
121 {
122     int	unit = device_get_unit(dev);
123     int rid = 0;
124     struct resource *res;
125     struct joy_softc *joy = device_get_softc(dev);
126 
127     res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
128     if (res == NULL)
129         return ENXIO;
130     joy->bt = rman_get_bustag(res);
131     joy->port = rman_get_bushandle(res);
132     joy->timeout[0] = joy->timeout[1] = 0;
133     make_dev(&joy_ops, unit, 0, 0, 0600, "joy%d", unit);
134     return 0;
135 }
136 
137 static device_method_t joy_methods[] = {
138     DEVMETHOD(device_probe,	joy_probe),
139     DEVMETHOD(device_attach,	joy_attach),
140     DEVMETHOD_END
141 };
142 
143 static driver_t joy_isa_driver = {
144     "joy",
145     joy_methods,
146     sizeof (struct joy_softc)
147 };
148 
149 DRIVER_MODULE(joy, isa, joy_isa_driver, joy_devclass, NULL, NULL);
150 DRIVER_MODULE(joy, acpi, joy_isa_driver, joy_devclass, NULL, NULL);
151 
152 static int
153 joyopen(struct dev_open_args *ap)
154 {
155     cdev_t dev = ap->a_head.a_dev;
156     int i = joypart (dev);
157     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
158 
159     if (joy->timeout[i])
160 	return EBUSY;
161     joy->x_off[i] = joy->y_off[i] = 0;
162     joy->timeout[i] = JOY_TIMEOUT;
163     return 0;
164 }
165 
166 static int
167 joyclose(struct dev_close_args *ap)
168 {
169     cdev_t dev = ap->a_head.a_dev;
170     int i = joypart (dev);
171     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
172 
173     joy->timeout[i] = 0;
174     return 0;
175 }
176 
177 static int
178 joyread(struct dev_read_args *ap)
179 {
180     cdev_t dev = ap->a_head.a_dev;
181     struct uio *uio = ap->a_uio;
182     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
183     bus_space_handle_t port = joy->port;
184     bus_space_tag_t bt = joy->bt;
185     struct timespec t, start, end;
186     int state = 0;
187     struct timespec x, y;
188     struct joystick c;
189 
190     crit_enter();
191 
192     bus_space_write_1 (bt, port, 0, 0xff);
193     nanotime(&start);
194     end.tv_sec = 0;
195     end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
196     timespecadd(&end, &start);
197     t = start;
198     timespecclear(&x);
199     timespecclear(&y);
200     while (timespeccmp(&t, &end, <)) {
201 	state = bus_space_read_1 (bt, port, 0);
202 	if (joypart(dev) == 1)
203 	    state >>= 2;
204 	nanotime(&t);
205 	if (!timespecisset(&x) && !(state & 0x01))
206 	    x = t;
207 	if (!timespecisset(&y) && !(state & 0x02))
208 	    y = t;
209 	if (timespecisset(&x) && timespecisset(&y))
210 	    break;
211     }
212 
213     crit_exit();
214 
215     if (timespecisset(&x)) {
216 	timespecsub(&x, &start);
217 	c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
218     } else
219 	c.x = 0x80000000;
220     if (timespecisset(&y)) {
221 	timespecsub(&y, &start);
222 	c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
223     } else
224 	c.y = 0x80000000;
225     state >>= 4;
226     c.b1 = ~state & 1;
227     c.b2 = ~(state >> 1) & 1;
228     return uiomove ((caddr_t)&c, sizeof(struct joystick), uio);
229 }
230 
231 static int
232 joyioctl(struct dev_ioctl_args *ap)
233 {
234     cdev_t dev = ap->a_head.a_dev;
235     caddr_t data = ap->a_data;
236     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
237     int i = joypart (dev);
238     int x;
239 
240     switch (ap->a_cmd) {
241     case JOY_SETTIMEOUT:
242 	x = *(int *) data;
243 	if (x < 1 || x > 10000) /* 10ms maximum! */
244 	    return EINVAL;
245 	joy->timeout[i] = x;
246 	break;
247     case JOY_GETTIMEOUT:
248 	*(int *) data = joy->timeout[i];
249 	break;
250     case JOY_SET_X_OFFSET:
251 	joy->x_off[i] = *(int *) data;
252 	break;
253     case JOY_SET_Y_OFFSET:
254 	joy->y_off[i] = *(int *) data;
255 	break;
256     case JOY_GET_X_OFFSET:
257 	*(int *) data = joy->x_off[i];
258 	break;
259     case JOY_GET_Y_OFFSET:
260 	*(int *) data = joy->y_off[i];
261 	break;
262     default:
263 	return ENXIO;
264     }
265     return 0;
266 }
267