joy.c revision 5897
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 */
29#include "joy.h"
30
31#if NJOY > 0
32
33#include <errno.h>
34#include <sys/types.h>
35#include <i386/isa/isa.h>
36#include <i386/isa/timerreg.h>
37#include <i386/isa/isa_device.h>
38#include <machine/cpufunc.h>
39
40#include <i386/include/joystick.h>
41
42/* The game port can manage 4 buttons and 4 variable resistors (usually 2
43 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
44 * Getting the state of the buttons is done by reading the game port:
45 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
46 * to bits 0-3.
47 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
48 * to get the value of a resistor, write the value 0xff at port and
49 * wait until the corresponding bit returns to 0.
50 */
51
52/* #defines below taken from clock.c */
53#ifndef TIMER_FREQ
54#define	TIMER_FREQ	1193182
55#endif
56#define usec2ticks(u) 	(u) * (TIMER_FREQ / 1000000) \
57                        + (u) * ((TIMER_FREQ % 1000000) / 1000) / 1000 \
58		        + (u) * (TIMER_FREQ % 1000) / 1000000
59
60
61
62#define joypart(d) minor(d)&1
63#define UNIT(d) minor(d)>>1&3
64#ifndef JOY_TIMEOUT
65#define JOY_TIMEOUT   2000 /* 2 milliseconds */
66#endif
67
68static struct {
69    int port;
70    int x_off[2], y_off[2];
71    int timeout[2];
72} joy[NJOY];
73
74
75extern int hz;
76
77int joyprobe (struct isa_device *), joyattach (struct isa_device *);
78
79struct isa_driver joydriver = {joyprobe, joyattach, "joy"};
80
81static int get_tick ();
82
83
84int
85joyprobe (struct isa_device *dev)
86{
87#ifdef WANT_JOYSTICK_CONNECTED
88    outb (dev->id_iobase, 0xff);
89    DELAY (10000); /*  10 ms delay */
90    return (inb (dev->id_iobase) & 0x0f) != 0x0f;
91#else
92    return 1;
93#endif
94}
95
96int
97joyattach (struct isa_device *dev)
98{
99    joy[dev->id_unit].port = dev->id_iobase;
100    joy[dev->id_unit].timeout[0] = joy[dev->id_unit].timeout[1] = 0;
101    printf("joy%d: joystick\n", dev->id_unit);
102
103    return 1;
104}
105
106int
107joyopen (dev_t dev, int flag)
108{
109    int unit = UNIT (dev);
110    int i = joypart (dev);
111
112    if (joy[unit].timeout[i])
113	return EBUSY;
114    joy[unit].x_off[i] = joy[unit].y_off[i] = 0;
115    joy[unit].timeout[i] = JOY_TIMEOUT;
116    return 0;
117}
118int
119joyclose (dev_t dev, int flag)
120{
121    int unit = UNIT (dev);
122    int i = joypart (dev);
123
124    joy[unit].timeout[i] = 0;
125    return 0;
126}
127
128int
129joyread (dev_t dev, struct uio *uio, int flag)
130{
131    int unit = UNIT(dev);
132    int port = joy[unit].port;
133    int i, t0, t1;
134    int state = 0, x = 0, y = 0;
135    int c[4];
136
137    disable_intr ();
138    outb (port, 0xff);
139    t0 = get_tick ();
140    t1 = t0;
141    i = usec2ticks(joy[unit].timeout[joypart(dev)]);
142    while (t0-t1 < i) {
143	state = inb (port);
144	if (joypart(dev) == 1)
145	    state >>= 2;
146	t1 = get_tick ();
147	if (t1 > t0)
148	    t1 -= TIMER_FREQ/hz;
149	if (!x && !(state & 0x01))
150	    x = t1;
151	if (!y && !(state & 0x02))
152	    y =  t1;
153	if (x && y)
154	    break;
155    }
156    enable_intr ();
157    c[0] = x ? joy[unit].x_off[joypart(dev)] + (t0-x) : 0x80000000;
158    c[1] = y ? joy[unit].y_off[joypart(dev)] + (t0-y) : 0x80000000;
159    state >>= 4;
160    c[2] = ~state & 1;
161    c[3] = ~(state >> 1) & 1;
162    return uiomove (c, 4*sizeof(int), uio);
163}
164int joyioctl (dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
165{
166    int unit = UNIT (dev);
167    int i = joypart (dev);
168    int x;
169
170    switch (cmd) {
171    case JOY_SETTIMEOUT:
172	x = *(int *) data;
173	if (x < 1 || x > 10000) /* 10ms maximum! */
174	    return EINVAL;
175	joy[unit].timeout[i] = x;
176	break;
177    case JOY_GETTIMEOUT:
178	*(int *) data = joy[unit].timeout[i];
179	break;
180    case JOY_SET_X_OFFSET:
181	joy[unit].x_off[i] = *(int *) data;
182	break;
183    case JOY_SET_Y_OFFSET:
184	joy[unit].y_off[i] = *(int *) data;
185	break;
186    case JOY_GET_X_OFFSET:
187	*(int *) data = joy[unit].x_off[i];
188	break;
189    case JOY_GET_Y_OFFSET:
190	*(int *) data = joy[unit].y_off[i];
191	break;
192    default:
193	return ENXIO;
194    }
195    return 0;
196}
197static int
198get_tick ()
199{
200    int low, high;
201
202    outb (TIMER_MODE, TIMER_SEL0);
203    low = inb (TIMER_CNTR0);
204    high = inb (TIMER_CNTR0);
205
206    return (high << 8) | low;
207}
208
209#endif /* NJOY > 0 */
210