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