joy.c revision 97748
15897Sjmz/*-
25897Sjmz * Copyright (c) 1995 Jean-Marc Zucconi
35897Sjmz * All rights reserved.
45897Sjmz *
55897Sjmz * Redistribution and use in source and binary forms, with or without
65897Sjmz * modification, are permitted provided that the following conditions
75897Sjmz * are met:
85897Sjmz * 1. Redistributions of source code must retain the above copyright
95897Sjmz *    notice, this list of conditions and the following disclaimer
105897Sjmz *    in this position and unchanged.
115897Sjmz * 2. Redistributions in binary form must reproduce the above copyright
125897Sjmz *    notice, this list of conditions and the following disclaimer in the
135897Sjmz *    documentation and/or other materials provided with the distribution.
145897Sjmz * 3. The name of the author may not be used to endorse or promote products
1597748Sschweikh *    derived from this software without specific prior written permission
165897Sjmz *
175897Sjmz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
185897Sjmz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
195897Sjmz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
205897Sjmz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
215897Sjmz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
225897Sjmz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235897Sjmz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245897Sjmz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255897Sjmz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
265897Sjmz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275897Sjmz *
2851658Sphk * $FreeBSD: head/sys/dev/joy/joy.c 97748 2002-06-02 20:05:59Z schweikh $
295897Sjmz */
305897Sjmz
317430Sbde#include <sys/param.h>
327430Sbde#include <sys/systm.h>
3312675Sjulian#include <sys/conf.h>
3434924Sbde#include <sys/uio.h>
3554156Speter#include <sys/kernel.h>
3654156Speter#include <sys/module.h>
3754156Speter#include <sys/bus.h>
3854156Speter#include <machine/bus.h>
3954156Speter#include <machine/resource.h>
4054156Speter#include <sys/rman.h>
4154156Speter#include <sys/time.h>
4254156Speter#include <sys/joystick.h>
4387384Simp#include <dev/joy/joyvar.h>
446734Sbde
458876Srgrimes/* The game port can manage 4 buttons and 4 variable resistors (usually 2
465897Sjmz * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
475897Sjmz * Getting the state of the buttons is done by reading the game port:
485897Sjmz * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
495897Sjmz * to bits 0-3.
505897Sjmz * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
515897Sjmz * to get the value of a resistor, write the value 0xff at port and
525897Sjmz * wait until the corresponding bit returns to 0.
535897Sjmz */
545897Sjmz
5546570Speter#define joypart(d) (minor(d)&1)
5646570Speter#define UNIT(d) ((minor(d)>>1)&3)
575897Sjmz#ifndef JOY_TIMEOUT
585897Sjmz#define JOY_TIMEOUT   2000 /* 2 milliseconds */
595897Sjmz#endif
605897Sjmz
6154156Speter#define JOY_SOFTC(unit) (struct joy_softc *) \
6254156Speter        devclass_get_softc(joy_devclass,(unit))
635897Sjmz
6412675Sjulian#define CDEV_MAJOR 51
6512675Sjulianstatic	d_open_t	joyopen;
6612675Sjulianstatic	d_close_t	joyclose;
6712675Sjulianstatic	d_read_t	joyread;
6812675Sjulianstatic	d_ioctl_t	joyioctl;
6912675Sjulian
7047625Sphkstatic struct cdevsw joy_cdevsw = {
7147625Sphk	/* open */	joyopen,
7247625Sphk	/* close */	joyclose,
7347625Sphk	/* read */	joyread,
7447625Sphk	/* write */	nowrite,
7547625Sphk	/* ioctl */	joyioctl,
7647625Sphk	/* poll */	nopoll,
7747625Sphk	/* mmap */	nommap,
7847625Sphk	/* strategy */	nostrategy,
7947625Sphk	/* name */	"joy",
8047625Sphk	/* maj */	CDEV_MAJOR,
8147625Sphk	/* dump */	nodump,
8247625Sphk	/* psize */	nopsize,
8347625Sphk	/* flags */	0,
8447625Sphk};
8512675Sjulian
8689086Simpdevclass_t joy_devclass;
875897Sjmz
8887384Simpint
8987384Simpjoy_probe(device_t dev)
905897Sjmz{
915897Sjmz#ifdef WANT_JOYSTICK_CONNECTED
9254156Speter#ifdef notyet
9387384Simp	outb(dev->id_iobase, 0xff);
9487384Simp	DELAY(10000); /*  10 ms delay */
9587384Simp	return (inb(dev->id_iobase) & 0x0f) != 0x0f;
9687384Simp#else
9787384Simp	return (0);
9854156Speter#endif
995897Sjmz#else
10087384Simp	return (0);
1015897Sjmz#endif
1025897Sjmz}
1035897Sjmz
10487384Simpint
10587384Simpjoy_attach(device_t dev)
1065897Sjmz{
10787384Simp	int	unit = device_get_unit(dev);
10887384Simp	struct joy_softc *joy = device_get_softc(dev);
1095897Sjmz
11087384Simp	joy->rid = 0;
11187384Simp	joy->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &joy->rid, 0, ~0, 1,
11287384Simp	    RF_ACTIVE);
11387384Simp	if (joy->res == NULL)
11487384Simp		return ENXIO;
11587384Simp	joy->bt = rman_get_bustag(joy->res);
11687384Simp	joy->port = rman_get_bushandle(joy->res);
11787384Simp	joy->timeout[0] = joy->timeout[1] = 0;
11887384Simp	joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
11987384Simp	return (0);
1205897Sjmz}
1215897Sjmz
12287384Simpint
12387384Simpjoy_detach(device_t dev)
12487384Simp{
12587384Simp	struct joy_softc *joy = device_get_softc(dev);
12654156Speter
12787384Simp	if (joy->res != NULL)
12887384Simp		bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
12987384Simp	if (joy->d)
13087384Simp		destroy_dev(joy->d);
13187384Simp	return (0);
13287384Simp}
13354156Speter
13454156Speter
13554156Speterstatic int
13683366Sjulianjoyopen(dev_t dev, int flags, int fmt, struct thread *td)
1375897Sjmz{
13887384Simp	int i = joypart (dev);
13987384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
1405897Sjmz
14187384Simp	if (joy->timeout[i])
14287384Simp		return (EBUSY);
14387384Simp	joy->x_off[i] = joy->y_off[i] = 0;
14487384Simp	joy->timeout[i] = JOY_TIMEOUT;
14587384Simp	return (0);
1465897Sjmz}
14754156Speter
14854156Speterstatic int
14983366Sjulianjoyclose(dev_t dev, int flags, int fmt, struct thread *td)
1505897Sjmz{
15187384Simp	int i = joypart (dev);
15287384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
1535897Sjmz
15487384Simp	joy->timeout[i] = 0;
15587384Simp	return (0);
1565897Sjmz}
1575897Sjmz
15854156Speterstatic int
15954156Speterjoyread(dev_t dev, struct uio *uio, int flag)
1605897Sjmz{
16187384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
16287384Simp	bus_space_handle_t port = joy->port;
16387384Simp	bus_space_tag_t bt = joy->bt;
16487384Simp	struct timespec t, start, end;
16587384Simp	int state = 0;
16687384Simp	struct timespec x, y;
16787384Simp	struct joystick c;
16897554Salfred#ifndef __i386__
16987384Simp	int s;
1708876Srgrimes
17187384Simp	s = splhigh();
17254156Speter#else
17387384Simp	disable_intr ();
17454156Speter#endif
17587384Simp	bus_space_write_1 (bt, port, 0, 0xff);
17687384Simp	nanotime(&start);
17787384Simp	end.tv_sec = 0;
17887384Simp	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
17987384Simp	timespecadd(&end, &start);
18087384Simp	t = start;
18187384Simp	timespecclear(&x);
18287384Simp	timespecclear(&y);
18387384Simp	while (timespeccmp(&t, &end, <)) {
18487384Simp		state = bus_space_read_1 (bt, port, 0);
18587384Simp		if (joypart(dev) == 1)
18687384Simp			state >>= 2;
18787384Simp		nanotime(&t);
18887384Simp		if (!timespecisset(&x) && !(state & 0x01))
18987384Simp			x = t;
19087384Simp		if (!timespecisset(&y) && !(state & 0x02))
19187384Simp			y = t;
19287384Simp		if (timespecisset(&x) && timespecisset(&y))
19387384Simp			break;
19487384Simp	}
19597554Salfred#ifndef __i386__
19687384Simp	splx(s);
19754156Speter#else
19887384Simp	enable_intr ();
19954156Speter#endif
20087384Simp	if (timespecisset(&x)) {
20187384Simp		timespecsub(&x, &start);
20287384Simp		c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
20387384Simp	} else
20487384Simp		c.x = 0x80000000;
20587384Simp	if (timespecisset(&y)) {
20687384Simp		timespecsub(&y, &start);
20787384Simp		c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
20887384Simp	} else
20987384Simp		c.y = 0x80000000;
21087384Simp	state >>= 4;
21187384Simp	c.b1 = ~state & 1;
21287384Simp	c.b2 = ~(state >> 1) & 1;
21387384Simp	return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
2145897Sjmz}
21512675Sjulian
21654156Speterstatic int
21783366Sjulianjoyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
2185897Sjmz{
21987384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
22087384Simp	int i = joypart (dev);
22187384Simp	int x;
2225897Sjmz
22387384Simp	switch (cmd) {
22487384Simp	case JOY_SETTIMEOUT:
22587384Simp		x = *(int *) data;
22687384Simp		if (x < 1 || x > 10000) /* 10ms maximum! */
22787384Simp			return EINVAL;
22887384Simp		joy->timeout[i] = x;
22987384Simp		break;
23087384Simp	case JOY_GETTIMEOUT:
23187384Simp		*(int *) data = joy->timeout[i];
23287384Simp		break;
23387384Simp	case JOY_SET_X_OFFSET:
23487384Simp		joy->x_off[i] = *(int *) data;
23587384Simp		break;
23687384Simp	case JOY_SET_Y_OFFSET:
23787384Simp		joy->y_off[i] = *(int *) data;
23887384Simp		break;
23987384Simp	case JOY_GET_X_OFFSET:
24087384Simp		*(int *) data = joy->x_off[i];
24187384Simp		break;
24287384Simp	case JOY_GET_Y_OFFSET:
24387384Simp		*(int *) data = joy->y_off[i];
24487384Simp		break;
24587384Simp	default:
24687384Simp		return (ENOTTY);
24787384Simp	}
24887384Simp	return (0);
2495897Sjmz}
250