1995-01-25 20:11:51 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1995 Jean-Marc Zucconi
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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
|
2002-06-02 20:05:59 +00:00
|
|
|
* derived from this software without specific prior written permission
|
1995-01-25 20:11:51 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
1999-09-25 18:24:47 +00:00
|
|
|
* $FreeBSD$
|
1995-01-25 20:11:51 +00:00
|
|
|
*/
|
|
|
|
|
1995-03-28 07:58:53 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
#include <sys/conf.h>
|
1998-03-28 10:33:27 +00:00
|
|
|
#include <sys/uio.h>
|
1999-12-05 19:51:40 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/joystick.h>
|
2001-12-05 09:08:23 +00:00
|
|
|
#include <dev/joy/joyvar.h>
|
1995-01-25 20:11:51 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
/* The game port can manage 4 buttons and 4 variable resistors (usually 2
|
1995-01-25 20:11:51 +00:00
|
|
|
* joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
|
|
|
|
* 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)
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1999-05-06 18:39:32 +00:00
|
|
|
#define joypart(d) (minor(d)&1)
|
|
|
|
#define UNIT(d) ((minor(d)>>1)&3)
|
1995-01-25 20:11:51 +00:00
|
|
|
#ifndef JOY_TIMEOUT
|
|
|
|
#define JOY_TIMEOUT 2000 /* 2 milliseconds */
|
|
|
|
#endif
|
|
|
|
|
1999-12-05 19:51:40 +00:00
|
|
|
#define JOY_SOFTC(unit) (struct joy_softc *) \
|
|
|
|
devclass_get_softc(joy_devclass,(unit))
|
1995-01-25 20:11:51 +00:00
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
#define CDEV_MAJOR 51
|
|
|
|
static d_open_t joyopen;
|
|
|
|
static d_close_t joyclose;
|
|
|
|
static d_read_t joyread;
|
|
|
|
static d_ioctl_t joyioctl;
|
|
|
|
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw joy_cdevsw = {
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = joyopen,
|
|
|
|
.d_close = joyclose,
|
|
|
|
.d_read = joyread,
|
|
|
|
.d_ioctl = joyioctl,
|
|
|
|
.d_name = "joy",
|
|
|
|
.d_maj = CDEV_MAJOR,
|
1999-05-30 16:53:49 +00:00
|
|
|
};
|
1995-12-08 11:19:42 +00:00
|
|
|
|
2002-01-08 18:27:49 +00:00
|
|
|
devclass_t joy_devclass;
|
1995-01-25 20:11:51 +00:00
|
|
|
|
2001-12-05 09:08:23 +00:00
|
|
|
int
|
|
|
|
joy_probe(device_t dev)
|
1995-01-25 20:11:51 +00:00
|
|
|
{
|
|
|
|
#ifdef WANT_JOYSTICK_CONNECTED
|
1999-12-05 19:51:40 +00:00
|
|
|
#ifdef notyet
|
2001-12-05 09:08:23 +00:00
|
|
|
outb(dev->id_iobase, 0xff);
|
|
|
|
DELAY(10000); /* 10 ms delay */
|
|
|
|
return (inb(dev->id_iobase) & 0x0f) != 0x0f;
|
|
|
|
#else
|
|
|
|
return (0);
|
1999-12-05 19:51:40 +00:00
|
|
|
#endif
|
1995-01-25 20:11:51 +00:00
|
|
|
#else
|
2001-12-05 09:08:23 +00:00
|
|
|
return (0);
|
1995-01-25 20:11:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-12-05 09:08:23 +00:00
|
|
|
int
|
|
|
|
joy_attach(device_t dev)
|
1995-01-25 20:11:51 +00:00
|
|
|
{
|
2001-12-05 09:08:23 +00:00
|
|
|
int unit = device_get_unit(dev);
|
|
|
|
struct joy_softc *joy = device_get_softc(dev);
|
|
|
|
|
|
|
|
joy->rid = 0;
|
|
|
|
joy->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &joy->rid, 0, ~0, 1,
|
|
|
|
RF_ACTIVE);
|
|
|
|
if (joy->res == NULL)
|
|
|
|
return ENXIO;
|
|
|
|
joy->bt = rman_get_bustag(joy->res);
|
|
|
|
joy->port = rman_get_bushandle(joy->res);
|
|
|
|
joy->timeout[0] = joy->timeout[1] = 0;
|
|
|
|
joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
|
|
|
|
return (0);
|
1995-01-25 20:11:51 +00:00
|
|
|
}
|
|
|
|
|
2001-12-05 09:08:23 +00:00
|
|
|
int
|
|
|
|
joy_detach(device_t dev)
|
|
|
|
{
|
|
|
|
struct joy_softc *joy = device_get_softc(dev);
|
1999-12-05 19:51:40 +00:00
|
|
|
|
2001-12-05 09:08:23 +00:00
|
|
|
if (joy->res != NULL)
|
|
|
|
bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
|
|
|
|
if (joy->d)
|
|
|
|
destroy_dev(joy->d);
|
|
|
|
return (0);
|
|
|
|
}
|
1999-12-05 19:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
joyopen(dev_t dev, int flags, int fmt, struct thread *td)
|
1995-01-25 20:11:51 +00:00
|
|
|
{
|
2001-12-05 09:08:23 +00:00
|
|
|
int i = joypart (dev);
|
|
|
|
struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
|
|
|
|
|
|
|
|
if (joy->timeout[i])
|
|
|
|
return (EBUSY);
|
|
|
|
joy->x_off[i] = joy->y_off[i] = 0;
|
|
|
|
joy->timeout[i] = JOY_TIMEOUT;
|
|
|
|
return (0);
|
1995-01-25 20:11:51 +00:00
|
|
|
}
|
1999-12-05 19:51:40 +00:00
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
joyclose(dev_t dev, int flags, int fmt, struct thread *td)
|
1995-01-25 20:11:51 +00:00
|
|
|
{
|
2001-12-05 09:08:23 +00:00
|
|
|
int i = joypart (dev);
|
|
|
|
struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
|
1995-01-25 20:11:51 +00:00
|
|
|
|
2001-12-05 09:08:23 +00:00
|
|
|
joy->timeout[i] = 0;
|
|
|
|
return (0);
|
1995-01-25 20:11:51 +00:00
|
|
|
}
|
|
|
|
|
1999-12-05 19:51:40 +00:00
|
|
|
static int
|
|
|
|
joyread(dev_t dev, struct uio *uio, int flag)
|
1995-01-25 20:11:51 +00:00
|
|
|
{
|
2001-12-05 09:08:23 +00:00
|
|
|
struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
|
|
|
|
bus_space_handle_t port = joy->port;
|
|
|
|
bus_space_tag_t bt = joy->bt;
|
|
|
|
struct timespec t, start, end;
|
|
|
|
int state = 0;
|
|
|
|
struct timespec x, y;
|
|
|
|
struct joystick c;
|
2002-05-30 07:13:40 +00:00
|
|
|
#ifndef __i386__
|
2001-12-05 09:08:23 +00:00
|
|
|
int s;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
2001-12-05 09:08:23 +00:00
|
|
|
s = splhigh();
|
1999-12-05 19:51:40 +00:00
|
|
|
#else
|
2001-12-05 09:08:23 +00:00
|
|
|
disable_intr ();
|
1999-12-05 19:51:40 +00:00
|
|
|
#endif
|
2001-12-05 09:08:23 +00:00
|
|
|
bus_space_write_1 (bt, port, 0, 0xff);
|
|
|
|
nanotime(&start);
|
|
|
|
end.tv_sec = 0;
|
|
|
|
end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
|
|
|
|
timespecadd(&end, &start);
|
|
|
|
t = start;
|
|
|
|
timespecclear(&x);
|
|
|
|
timespecclear(&y);
|
|
|
|
while (timespeccmp(&t, &end, <)) {
|
|
|
|
state = bus_space_read_1 (bt, port, 0);
|
|
|
|
if (joypart(dev) == 1)
|
|
|
|
state >>= 2;
|
|
|
|
nanotime(&t);
|
|
|
|
if (!timespecisset(&x) && !(state & 0x01))
|
|
|
|
x = t;
|
|
|
|
if (!timespecisset(&y) && !(state & 0x02))
|
|
|
|
y = t;
|
|
|
|
if (timespecisset(&x) && timespecisset(&y))
|
|
|
|
break;
|
|
|
|
}
|
2002-05-30 07:13:40 +00:00
|
|
|
#ifndef __i386__
|
2001-12-05 09:08:23 +00:00
|
|
|
splx(s);
|
1999-12-05 19:51:40 +00:00
|
|
|
#else
|
2001-12-05 09:08:23 +00:00
|
|
|
enable_intr ();
|
1999-12-05 19:51:40 +00:00
|
|
|
#endif
|
2001-12-05 09:08:23 +00:00
|
|
|
if (timespecisset(&x)) {
|
|
|
|
timespecsub(&x, &start);
|
|
|
|
c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
|
|
|
|
} else
|
|
|
|
c.x = 0x80000000;
|
|
|
|
if (timespecisset(&y)) {
|
|
|
|
timespecsub(&y, &start);
|
|
|
|
c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
|
|
|
|
} else
|
|
|
|
c.y = 0x80000000;
|
|
|
|
state >>= 4;
|
|
|
|
c.b1 = ~state & 1;
|
|
|
|
c.b2 = ~(state >> 1) & 1;
|
|
|
|
return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
|
1995-01-25 20:11:51 +00:00
|
|
|
}
|
1995-12-08 11:19:42 +00:00
|
|
|
|
1999-12-05 19:51:40 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
|
1995-01-25 20:11:51 +00:00
|
|
|
{
|
2001-12-05 09:08:23 +00:00
|
|
|
struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
|
|
|
|
int i = joypart (dev);
|
|
|
|
int x;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case JOY_SETTIMEOUT:
|
|
|
|
x = *(int *) data;
|
|
|
|
if (x < 1 || x > 10000) /* 10ms maximum! */
|
|
|
|
return EINVAL;
|
|
|
|
joy->timeout[i] = x;
|
|
|
|
break;
|
|
|
|
case JOY_GETTIMEOUT:
|
|
|
|
*(int *) data = joy->timeout[i];
|
|
|
|
break;
|
|
|
|
case JOY_SET_X_OFFSET:
|
|
|
|
joy->x_off[i] = *(int *) data;
|
|
|
|
break;
|
|
|
|
case JOY_SET_Y_OFFSET:
|
|
|
|
joy->y_off[i] = *(int *) data;
|
|
|
|
break;
|
|
|
|
case JOY_GET_X_OFFSET:
|
|
|
|
*(int *) data = joy->x_off[i];
|
|
|
|
break;
|
|
|
|
case JOY_GET_Y_OFFSET:
|
|
|
|
*(int *) data = joy->y_off[i];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ENOTTY);
|
|
|
|
}
|
|
|
|
return (0);
|
1995-01-25 20:11:51 +00:00
|
|
|
}
|