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