xref: /trueos/sys/dev/joy/joy.c (revision 9ed1a4b5ebe734049aa444f14884ed8ad4c23aee)
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) (dev2unit(d)&1)
58 #ifndef JOY_TIMEOUT
59 #define JOY_TIMEOUT   2000 /* 2 milliseconds */
60 #endif
61 
62 static	d_open_t	joyopen;
63 static	d_close_t	joyclose;
64 static	d_read_t	joyread;
65 static	d_ioctl_t	joyioctl;
66 
67 static struct cdevsw joy_cdevsw = {
68 	.d_version =	D_VERSION,
69 	.d_flags =	D_NEEDGIANT,
70 	.d_open =	joyopen,
71 	.d_close =	joyclose,
72 	.d_read =	joyread,
73 	.d_ioctl =	joyioctl,
74 	.d_name =	"joy",
75 };
76 
77 devclass_t joy_devclass;
78 
79 int
joy_probe(device_t dev)80 joy_probe(device_t dev)
81 {
82 #ifdef WANT_JOYSTICK_CONNECTED
83 #ifdef notyet
84 	outb(dev->id_iobase, 0xff);
85 	DELAY(10000); /*  10 ms delay */
86 	return (inb(dev->id_iobase) & 0x0f) != 0x0f;
87 #else
88 	return (0);
89 #endif
90 #else
91 	return (0);
92 #endif
93 }
94 
95 int
joy_attach(device_t dev)96 joy_attach(device_t dev)
97 {
98 	int	unit = device_get_unit(dev);
99 	struct joy_softc *joy = device_get_softc(dev);
100 
101 	joy->rid = 0;
102 	joy->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &joy->rid,
103 	    RF_ACTIVE|RF_SHAREABLE);
104 	if (joy->res == NULL)
105 		return ENXIO;
106 	joy->bt = rman_get_bustag(joy->res);
107 	joy->port = rman_get_bushandle(joy->res);
108 	joy->timeout[0] = joy->timeout[1] = 0;
109 	joy->d = make_dev(&joy_cdevsw, unit, 0, 0, 0600, "joy%d", unit);
110 	joy->d->si_drv1 = joy;
111 	return (0);
112 }
113 
114 int
joy_detach(device_t dev)115 joy_detach(device_t dev)
116 {
117 	struct joy_softc *joy = device_get_softc(dev);
118 
119 	if (joy->res != NULL)
120 		bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
121 	if (joy->d)
122 		destroy_dev(joy->d);
123 	return (0);
124 }
125 
126 
127 static int
joyopen(struct cdev * dev,int flags,int fmt,struct thread * td)128 joyopen(struct cdev *dev, int flags, int fmt, struct thread *td)
129 {
130 	int i = joypart (dev);
131 	struct joy_softc *joy = dev->si_drv1;
132 
133 	if (joy->timeout[i])
134 		return (EBUSY);
135 	joy->x_off[i] = joy->y_off[i] = 0;
136 	joy->timeout[i] = JOY_TIMEOUT;
137 	return (0);
138 }
139 
140 static int
joyclose(struct cdev * dev,int flags,int fmt,struct thread * td)141 joyclose(struct cdev *dev, int flags, int fmt, struct thread *td)
142 {
143 	int i = joypart (dev);
144 	struct joy_softc *joy = dev->si_drv1;
145 
146 	joy->timeout[i] = 0;
147 	return (0);
148 }
149 
150 static int
joyread(struct cdev * dev,struct uio * uio,int flag)151 joyread(struct cdev *dev, struct uio *uio, int flag)
152 {
153 	struct joy_softc *joy = dev->si_drv1;
154 	bus_space_handle_t port = joy->port;
155 	bus_space_tag_t bt = joy->bt;
156 	struct timespec t, start, end;
157 	int state = 0;
158 	struct timespec x, y;
159 	struct joystick c;
160 #ifndef __i386__
161 	int s;
162 
163 	s = splhigh();
164 #else
165 	disable_intr ();
166 #endif
167 	nanotime(&t);
168 	end.tv_sec = 0;
169 	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
170 	timespecadd(&end, &t);
171 	for (; timespeccmp(&t, &end, <) && (bus_space_read_1(bt, port, 0) & 0x0f); nanotime(&t))
172 		;	/* nothing */
173 	bus_space_write_1 (bt, port, 0, 0xff);
174 	nanotime(&start);
175 	end.tv_sec = 0;
176 	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
177 	timespecadd(&end, &start);
178 	t = start;
179 	timespecclear(&x);
180 	timespecclear(&y);
181 	while (timespeccmp(&t, &end, <)) {
182 		state = bus_space_read_1 (bt, port, 0);
183 		if (joypart(dev) == 1)
184 			state >>= 2;
185 		nanotime(&t);
186 		if (!timespecisset(&x) && !(state & 0x01))
187 			x = t;
188 		if (!timespecisset(&y) && !(state & 0x02))
189 			y = t;
190 		if (timespecisset(&x) && timespecisset(&y))
191 			break;
192 	}
193 #ifndef __i386__
194 	splx(s);
195 #else
196 	enable_intr ();
197 #endif
198 	if (timespecisset(&x)) {
199 		timespecsub(&x, &start);
200 		c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
201 	} else
202 		c.x = 0x80000000;
203 	if (timespecisset(&y)) {
204 		timespecsub(&y, &start);
205 		c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
206 	} else
207 		c.y = 0x80000000;
208 	state >>= 4;
209 	c.b1 = ~state & 1;
210 	c.b2 = ~(state >> 1) & 1;
211 	return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
212 }
213 
214 static int
joyioctl(struct cdev * dev,u_long cmd,caddr_t data,int flag,struct thread * td)215 joyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
216 {
217 	struct joy_softc *joy = dev->si_drv1;
218 	int i = joypart (dev);
219 	int x;
220 
221 	switch (cmd) {
222 	case JOY_SETTIMEOUT:
223 		x = *(int *) data;
224 		if (x < 1 || x > 10000) /* 10ms maximum! */
225 			return EINVAL;
226 		joy->timeout[i] = x;
227 		break;
228 	case JOY_GETTIMEOUT:
229 		*(int *) data = joy->timeout[i];
230 		break;
231 	case JOY_SET_X_OFFSET:
232 		joy->x_off[i] = *(int *) data;
233 		break;
234 	case JOY_SET_Y_OFFSET:
235 		joy->y_off[i] = *(int *) data;
236 		break;
237 	case JOY_GET_X_OFFSET:
238 		*(int *) data = joy->x_off[i];
239 		break;
240 	case JOY_GET_Y_OFFSET:
241 		*(int *) data = joy->y_off[i];
242 		break;
243 	default:
244 		return (ENOTTY);
245 	}
246 	return (0);
247 }
248