joy.c revision 5993
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
53/* the formulae below only work if u is  ``not too large''. See also
54 * the discussion in microtime.s */
55#define usec2ticks(u) 	((u) * 19549)>>14
56#define ticks2usec(u) 	((u) * 3433)>>12
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
65static struct {
66    int port;
67    int x_off[2], y_off[2];
68    int timeout[2];
69} joy[NJOY];
70
71
72extern int timer0_max_count;
73
74int joyprobe (struct isa_device *), joyattach (struct isa_device *);
75
76struct isa_driver joydriver = {joyprobe, joyattach, "joy"};
77
78static int get_tick ();
79
80
81int
82joyprobe (struct isa_device *dev)
83{
84#ifdef WANT_JOYSTICK_CONNECTED
85    outb (dev->id_iobase, 0xff);
86    DELAY (10000); /*  10 ms delay */
87    return (inb (dev->id_iobase) & 0x0f) != 0x0f;
88#else
89    return 1;
90#endif
91}
92
93int
94joyattach (struct isa_device *dev)
95{
96    joy[dev->id_unit].port = dev->id_iobase;
97    joy[dev->id_unit].timeout[0] = joy[dev->id_unit].timeout[1] = 0;
98    printf("joy%d: joystick\n", dev->id_unit);
99
100    return 1;
101}
102
103int
104joyopen (dev_t dev, int flag)
105{
106    int unit = UNIT (dev);
107    int i = joypart (dev);
108
109    if (joy[unit].timeout[i])
110	return EBUSY;
111    joy[unit].x_off[i] = joy[unit].y_off[i] = 0;
112    joy[unit].timeout[i] = JOY_TIMEOUT;
113    return 0;
114}
115int
116joyclose (dev_t dev, int flag)
117{
118    int unit = UNIT (dev);
119    int i = joypart (dev);
120
121    joy[unit].timeout[i] = 0;
122    return 0;
123}
124
125int
126joyread (dev_t dev, struct uio *uio, int flag)
127{
128    int unit = UNIT(dev);
129    int port = joy[unit].port;
130    int i, t0, t1;
131    int state = 0, x = 0, y = 0;
132    int c[4];
133
134    disable_intr ();
135    outb (port, 0xff);
136    t0 = get_tick ();
137    t1 = t0;
138    i = usec2ticks(joy[unit].timeout[joypart(dev)]);
139    while (t0-t1 < i) {
140	state = inb (port);
141	if (joypart(dev) == 1)
142	    state >>= 2;
143	t1 = get_tick ();
144	if (t1 > t0)
145	    t1 -= timer0_max_count;
146	if (!x && !(state & 0x01))
147	    x = t1;
148	if (!y && !(state & 0x02))
149	    y =  t1;
150	if (x && y)
151	    break;
152    }
153    enable_intr ();
154    c[0] = x ? joy[unit].x_off[joypart(dev)] + ticks2usec(t0-x) : 0x80000000;
155    c[1] = y ? joy[unit].y_off[joypart(dev)] + ticks2usec(t0-y) : 0x80000000;
156    state >>= 4;
157    c[2] = ~state & 1;
158    c[3] = ~(state >> 1) & 1;
159    return uiomove (c, 4*sizeof(int), uio);
160}
161int joyioctl (dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
162{
163    int unit = UNIT (dev);
164    int i = joypart (dev);
165    int x;
166
167    switch (cmd) {
168    case JOY_SETTIMEOUT:
169	x = *(int *) data;
170	if (x < 1 || x > 10000) /* 10ms maximum! */
171	    return EINVAL;
172	joy[unit].timeout[i] = x;
173	break;
174    case JOY_GETTIMEOUT:
175	*(int *) data = joy[unit].timeout[i];
176	break;
177    case JOY_SET_X_OFFSET:
178	joy[unit].x_off[i] = *(int *) data;
179	break;
180    case JOY_SET_Y_OFFSET:
181	joy[unit].y_off[i] = *(int *) data;
182	break;
183    case JOY_GET_X_OFFSET:
184	*(int *) data = joy[unit].x_off[i];
185	break;
186    case JOY_GET_Y_OFFSET:
187	*(int *) data = joy[unit].y_off[i];
188	break;
189    default:
190	return ENXIO;
191    }
192    return 0;
193}
194static int
195get_tick ()
196{
197    int low, high;
198
199    outb (TIMER_MODE, TIMER_SEL0);
200    low = inb (TIMER_CNTR0);
201    high = inb (TIMER_CNTR0);
202
203    return (high << 8) | low;
204}
205
206#endif /* NJOY > 0 */
207