NetBSD/sys/arch/i386/isa/joy.c

236 lines
5.8 KiB
C
Raw Normal View History

1998-01-12 21:59:04 +03:00
/* $NetBSD: joy.c,v 1.10 1998/01/12 18:59:12 thorpej Exp $ */
/*
* XXX This _really_ should be rewritten such that it doesn't
* XXX rely in the i386 timer!
*/
/*-
* Copyright (c) 1995 Jean-Marc Zucconi
* All rights reserved.
*
* Ported to NetBSD by Matthieu Herrb <matthieu@laas.fr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software withough specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
1996-04-01 10:39:17 +04:00
#include <sys/errno.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/pio.h>
#include <machine/cpufunc.h>
#include <machine/joystick.h>
1996-05-05 23:46:15 +04:00
#include <machine/conf.h>
#include <dev/isa/isavar.h>
#include <dev/isa/isareg.h>
#include <i386/isa/joyvar.h>
#include <i386/isa/timerreg.h> /* XXX XXX XXX */
1996-04-01 10:39:17 +04:00
/*
* The game port can manage 4 buttons and 4 variable resistors (usually 2
* joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
1996-04-01 10:39:17 +04:00
* Getting the state of the buttons is done by reading the game port;
* buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
1996-04-01 10:39:17 +04:00
* to bits 0-3. If button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5,
* 6, 7) is set to 0 to get the value of a resistor, write the value 0xff
* at port and wait until the corresponding bit returns to 0.
*/
1996-04-01 10:39:17 +04:00
/*
* The formulae below only work if u is ``not too large''. See also
* the discussion in microtime.s
*/
#define USEC2TICKS(u) (((u) * 19549) >> 14)
#define TICKS2USEC(u) (((u) * 3433) >> 12)
1996-05-05 23:46:15 +04:00
#define JOYPART(d) (minor(d) & 1)
1996-04-01 10:39:17 +04:00
#define JOYUNIT(d) minor(d) >> 1 & 3
#ifndef JOY_TIMEOUT
1996-04-01 10:39:17 +04:00
#define JOY_TIMEOUT 2000 /* 2 milliseconds */
#endif
1996-04-01 10:39:17 +04:00
int joyopen __P((dev_t, int, int, struct proc *));
int joyclose __P((dev_t, int, int, struct proc *));
static int get_tick __P((void));
1998-01-12 21:59:04 +03:00
extern struct cfdriver joy_cd;
void
joyattach(sc)
struct joy_softc *sc;
{
1996-04-01 10:39:17 +04:00
sc->timeout[0] = sc->timeout[1] = 0;
bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff);
1996-04-01 10:39:17 +04:00
DELAY(10000); /* 10 ms delay */
1997-10-31 10:59:52 +03:00
printf("%s: joystick %sconnected\n", sc->sc_dev.dv_xname,
(bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ?
"not " : "");
}
int
1996-04-01 10:39:17 +04:00
joyopen(dev, flag, mode, p)
dev_t dev;
int flag, mode;
struct proc *p;
{
1996-04-01 10:39:17 +04:00
int unit = JOYUNIT(dev);
int i = JOYPART(dev);
struct joy_softc *sc;
if (unit >= joy_cd.cd_ndevs)
return (ENXIO);
sc = joy_cd.cd_devs[unit];
if (sc == 0)
return (ENXIO);
1996-04-01 10:39:17 +04:00
if (sc->timeout[i])
1997-10-31 10:59:52 +03:00
return (EBUSY);
1996-04-01 10:39:17 +04:00
sc->x_off[i] = sc->y_off[i] = 0;
sc->timeout[i] = JOY_TIMEOUT;
1997-10-31 10:59:52 +03:00
return (0);
}
int
joyclose(dev, flag, mode, p)
dev_t dev;
int flag, mode;
struct proc *p;
{
1996-04-01 10:39:17 +04:00
int unit = JOYUNIT(dev);
int i = JOYPART(dev);
struct joy_softc *sc = joy_cd.cd_devs[unit];
1996-04-01 10:39:17 +04:00
sc->timeout[i] = 0;
1997-10-31 10:59:52 +03:00
return (0);
}
int
joyread(dev, uio, flag)
dev_t dev;
struct uio *uio;
int flag;
{
1996-04-01 10:39:17 +04:00
int unit = JOYUNIT(dev);
struct joy_softc *sc = joy_cd.cd_devs[unit];
1997-10-31 10:59:52 +03:00
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
1996-04-01 10:39:17 +04:00
struct joystick c;
int i, t0, t1;
int state = 0, x = 0, y = 0;
disable_intr();
bus_space_write_1(iot, ioh, 0, 0xff);
1996-04-01 10:39:17 +04:00
t0 = get_tick();
t1 = t0;
i = USEC2TICKS(sc->timeout[JOYPART(dev)]);
while (t0 - t1 < i) {
state = bus_space_read_1(iot, ioh, 0);
1996-04-01 10:39:17 +04:00
if (JOYPART(dev) == 1)
state >>= 2;
t1 = get_tick();
if (t1 > t0)
t1 -= TIMER_FREQ / hz;
if (!x && !(state & 0x01))
x = t1;
if (!y && !(state & 0x02))
y = t1;
if (x && y)
break;
}
enable_intr();
c.x = x ? sc->x_off[JOYPART(dev)] + TICKS2USEC(t0 - x) : 0x80000000;
c.y = y ? sc->y_off[JOYPART(dev)] + TICKS2USEC(t0 - y) : 0x80000000;
state >>= 4;
c.b1 = ~state & 1;
c.b2 = ~(state >> 1) & 1;
1997-10-31 10:59:52 +03:00
return (uiomove(&c, sizeof(struct joystick), uio));
}
int
joyioctl(dev, cmd, data, flag, p)
1996-04-01 10:39:17 +04:00
dev_t dev;
u_long cmd;
caddr_t data;
int flag;
struct proc *p;
{
1996-04-01 10:39:17 +04:00
int unit = JOYUNIT(dev);
struct joy_softc *sc = joy_cd.cd_devs[unit];
int i = JOYPART(dev);
int x;
switch (cmd) {
case JOY_SETTIMEOUT:
x = *(int *) data;
if (x < 1 || x > 10000) /* 10ms maximum! */
1997-10-31 10:59:52 +03:00
return (EINVAL);
1996-04-01 10:39:17 +04:00
sc->timeout[i] = x;
break;
case JOY_GETTIMEOUT:
*(int *) data = sc->timeout[i];
break;
case JOY_SET_X_OFFSET:
sc->x_off[i] = *(int *) data;
break;
case JOY_SET_Y_OFFSET:
sc->y_off[i] = *(int *) data;
break;
case JOY_GET_X_OFFSET:
*(int *) data = sc->x_off[i];
break;
case JOY_GET_Y_OFFSET:
*(int *) data = sc->y_off[i];
break;
default:
1997-10-31 10:59:52 +03:00
return (ENXIO);
1996-04-01 10:39:17 +04:00
}
return 0;
}
static int
1996-04-01 10:39:17 +04:00
get_tick()
{
1996-04-01 10:39:17 +04:00
int low, high;
1996-04-01 10:39:17 +04:00
outb(TIMER_MODE, TIMER_SEL0);
low = inb(TIMER_CNTR0);
high = inb(TIMER_CNTR0);
1997-10-31 10:59:52 +03:00
return ((high << 8) | low);
}