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