Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / dev / misc / joy / joy.c
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.8 2005/06/16 15:51:34 joerg 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/thread2.h>
43 #include <sys/time.h>
44 #include <sys/joystick.h>
45
46 #include <bus/isa/isavar.h>
47 #include "isa_if.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) (minor(d)&1)
60 #define UNIT(d) ((minor(d)>>1)&3)
61 #ifndef JOY_TIMEOUT
62 #define JOY_TIMEOUT   2000 /* 2 milliseconds */
63 #endif
64
65 struct joy_softc {
66     bus_space_tag_t  bt;
67     bus_space_handle_t port;
68     int x_off[2], y_off[2];
69     int timeout[2];
70 };
71
72 #define JOY_SOFTC(unit) (struct joy_softc *) \
73         devclass_get_softc(joy_devclass,(unit))
74
75 static int joy_probe (device_t);
76 static int joy_attach (device_t);
77
78 #define CDEV_MAJOR 51
79 static  d_open_t        joyopen;
80 static  d_close_t       joyclose;
81 static  d_read_t        joyread;
82 static  d_ioctl_t       joyioctl;
83
84 static struct cdevsw joy_cdevsw = {
85         /* name */      "joy",
86         /* maj */       CDEV_MAJOR,
87         /* flags */     0,
88         /* port */      NULL,
89         /* clone */     NULL,
90
91         /* open */      joyopen,
92         /* close */     joyclose,
93         /* read */      joyread,
94         /* write */     nowrite,
95         /* ioctl */     joyioctl,
96         /* poll */      nopoll,
97         /* mmap */      nommap,
98         /* strategy */  nostrategy,
99         /* dump */      nodump,
100         /* psize */     nopsize
101 };
102
103 devclass_t joy_devclass;
104
105 static struct isa_pnp_id joy_ids[] = {
106     {0x0100630e, "CSC0001 PnP Joystick"},       /* CSC0001 */
107     {0x0101630e, "CSC0101 PnP Joystick"},       /* CSC0101 */
108     {0x01100002, "ALS0110 PnP Joystick"},       /* @P@1001 */
109     {0x01200002, "ALS0120 PnP Joystick"},       /* @P@2001 */
110     {0x01007316, "ESS0001 PnP Joystick"},       /* ESS0001 */
111     {0x2fb0d041, "Generic PnP Joystick"},       /* PNPb02f */
112     {0x2200a865, "YMH0022 PnP Joystick"},       /* YMH0022 */
113     {0x82719304, NULL},                         /* ADS7182 */
114     {0}
115 };
116
117 static int
118 joy_probe (device_t dev)
119 {
120     if (ISA_PNP_PROBE(device_get_parent(dev), dev, joy_ids) == ENXIO)
121         return ENXIO;
122 #ifdef WANT_JOYSTICK_CONNECTED
123 #ifdef notyet
124     outb (dev->id_iobase, 0xff);
125     DELAY (10000); /*  10 ms delay */
126     return (inb (dev->id_iobase) & 0x0f) != 0x0f;
127 #endif
128 #else
129     return 0;
130 #endif
131 }
132
133 static int
134 joy_attach (device_t dev)
135 {
136     int unit = device_get_unit(dev);
137     int rid = 0;
138     struct resource *res;
139     struct joy_softc *joy = device_get_softc(dev);
140
141     res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
142     if (res == NULL)
143         return ENXIO;
144     joy->bt = rman_get_bustag(res);
145     joy->port = rman_get_bushandle(res);
146     joy->timeout[0] = joy->timeout[1] = 0;
147     cdevsw_add(&joy_cdevsw, -1, unit);
148     make_dev(&joy_cdevsw, unit, 0, 0, 0600, "joy%d", unit);
149     return 0;
150 }
151
152 static device_method_t joy_methods[] = {
153     DEVMETHOD(device_probe,     joy_probe),
154     DEVMETHOD(device_attach,    joy_attach),
155     { 0, 0 }
156 };
157
158 static driver_t joy_isa_driver = {
159     "joy",
160     joy_methods,
161     sizeof (struct joy_softc)
162 };
163
164 DRIVER_MODULE(joy, isa, joy_isa_driver, joy_devclass, 0, 0);
165
166 static int
167 joyopen(dev_t dev, int flags, int fmt, d_thread_t *td)
168 {
169     int i = joypart (dev);
170     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
171
172     if (joy->timeout[i])
173         return EBUSY;
174     joy->x_off[i] = joy->y_off[i] = 0;
175     joy->timeout[i] = JOY_TIMEOUT;
176     return 0;
177 }
178
179 static int
180 joyclose(dev_t dev, int flags, int fmt, d_thread_t *td)
181 {
182     int i = joypart (dev);
183     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
184
185     joy->timeout[i] = 0;
186     return 0;
187 }
188
189 static int
190 joyread(dev_t dev, struct uio *uio, int flag)
191 {
192     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
193     bus_space_handle_t port = joy->port;
194     bus_space_tag_t bt = joy->bt;
195     struct timespec t, start, end;
196     int state = 0;
197     struct timespec x, y;
198     struct joystick c;
199
200     crit_enter();
201
202     bus_space_write_1 (bt, port, 0, 0xff);
203     nanotime(&start);
204     end.tv_sec = 0;
205     end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
206     timespecadd(&end, &start);
207     t = start;
208     timespecclear(&x);
209     timespecclear(&y);
210     while (timespeccmp(&t, &end, <)) {
211         state = bus_space_read_1 (bt, port, 0);
212         if (joypart(dev) == 1)
213             state >>= 2;
214         nanotime(&t);
215         if (!timespecisset(&x) && !(state & 0x01))
216             x = t;
217         if (!timespecisset(&y) && !(state & 0x02))
218             y = t;
219         if (timespecisset(&x) && timespecisset(&y))
220             break;
221     }
222
223     crit_exit();
224
225     if (timespecisset(&x)) {
226         timespecsub(&x, &start);
227         c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
228     } else
229         c.x = 0x80000000;
230     if (timespecisset(&y)) {
231         timespecsub(&y, &start);
232         c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
233     } else
234         c.y = 0x80000000;
235     state >>= 4;
236     c.b1 = ~state & 1;
237     c.b2 = ~(state >> 1) & 1;
238     return uiomove ((caddr_t)&c, sizeof(struct joystick), uio);
239 }
240
241 static int
242 joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
243 {
244     struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
245     int i = joypart (dev);
246     int x;
247
248     switch (cmd) {
249     case JOY_SETTIMEOUT:
250         x = *(int *) data;
251         if (x < 1 || x > 10000) /* 10ms maximum! */
252             return EINVAL;
253         joy->timeout[i] = x;
254         break;
255     case JOY_GETTIMEOUT:
256         *(int *) data = joy->timeout[i];
257         break;
258     case JOY_SET_X_OFFSET:
259         joy->x_off[i] = *(int *) data;
260         break;
261     case JOY_SET_Y_OFFSET:
262         joy->y_off[i] = *(int *) data;
263         break;
264     case JOY_GET_X_OFFSET:
265         *(int *) data = joy->x_off[i];
266         break;
267     case JOY_GET_Y_OFFSET:
268         *(int *) data = joy->y_off[i];
269         break;
270     default:
271         return ENXIO;
272     }
273     return 0;
274 }