joy.c revision 127135
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 *
285897Sjmz */
295897Sjmz
30119418Sobrien#include <sys/cdefs.h>
31119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/joy/joy.c 127135 2004-03-17 17:50:55Z njl $");
32119418Sobrien
337430Sbde#include <sys/param.h>
347430Sbde#include <sys/systm.h>
3512675Sjulian#include <sys/conf.h>
3634924Sbde#include <sys/uio.h>
3754156Speter#include <sys/kernel.h>
3854156Speter#include <sys/module.h>
3954156Speter#include <sys/bus.h>
4054156Speter#include <machine/bus.h>
4154156Speter#include <machine/resource.h>
4254156Speter#include <sys/rman.h>
4354156Speter#include <sys/time.h>
4454156Speter#include <sys/joystick.h>
4587384Simp#include <dev/joy/joyvar.h>
466734Sbde
478876Srgrimes/* The game port can manage 4 buttons and 4 variable resistors (usually 2
485897Sjmz * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
495897Sjmz * Getting the state of the buttons is done by reading the game port:
505897Sjmz * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
515897Sjmz * to bits 0-3.
525897Sjmz * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
535897Sjmz * to get the value of a resistor, write the value 0xff at port and
545897Sjmz * wait until the corresponding bit returns to 0.
555897Sjmz */
565897Sjmz
5746570Speter#define joypart(d) (minor(d)&1)
5846570Speter#define UNIT(d) ((minor(d)>>1)&3)
595897Sjmz#ifndef JOY_TIMEOUT
605897Sjmz#define JOY_TIMEOUT   2000 /* 2 milliseconds */
615897Sjmz#endif
625897Sjmz
6354156Speter#define JOY_SOFTC(unit) (struct joy_softc *) \
6454156Speter        devclass_get_softc(joy_devclass,(unit))
655897Sjmz
6612675Sjulianstatic	d_open_t	joyopen;
6712675Sjulianstatic	d_close_t	joyclose;
6812675Sjulianstatic	d_read_t	joyread;
6912675Sjulianstatic	d_ioctl_t	joyioctl;
7012675Sjulian
7147625Sphkstatic struct cdevsw joy_cdevsw = {
72126080Sphk	.d_version =	D_VERSION,
73126080Sphk	.d_flags =	D_NEEDGIANT,
74111815Sphk	.d_open =	joyopen,
75111815Sphk	.d_close =	joyclose,
76111815Sphk	.d_read =	joyread,
77111815Sphk	.d_ioctl =	joyioctl,
78111815Sphk	.d_name =	"joy",
7947625Sphk};
8012675Sjulian
8189086Simpdevclass_t joy_devclass;
825897Sjmz
8387384Simpint
8487384Simpjoy_probe(device_t dev)
855897Sjmz{
865897Sjmz#ifdef WANT_JOYSTICK_CONNECTED
8754156Speter#ifdef notyet
8887384Simp	outb(dev->id_iobase, 0xff);
8987384Simp	DELAY(10000); /*  10 ms delay */
9087384Simp	return (inb(dev->id_iobase) & 0x0f) != 0x0f;
9187384Simp#else
9287384Simp	return (0);
9354156Speter#endif
945897Sjmz#else
9587384Simp	return (0);
965897Sjmz#endif
975897Sjmz}
985897Sjmz
9987384Simpint
10087384Simpjoy_attach(device_t dev)
1015897Sjmz{
10287384Simp	int	unit = device_get_unit(dev);
10387384Simp	struct joy_softc *joy = device_get_softc(dev);
1045897Sjmz
10587384Simp	joy->rid = 0;
106127135Snjl	joy->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &joy->rid,
10787384Simp	    RF_ACTIVE);
10887384Simp	if (joy->res == NULL)
10987384Simp		return ENXIO;
11087384Simp	joy->bt = rman_get_bustag(joy->res);
11187384Simp	joy->port = rman_get_bushandle(joy->res);
11287384Simp	joy->timeout[0] = joy->timeout[1] = 0;
11387384Simp	joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
11487384Simp	return (0);
1155897Sjmz}
1165897Sjmz
11787384Simpint
11887384Simpjoy_detach(device_t dev)
11987384Simp{
12087384Simp	struct joy_softc *joy = device_get_softc(dev);
12154156Speter
12287384Simp	if (joy->res != NULL)
12387384Simp		bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
12487384Simp	if (joy->d)
12587384Simp		destroy_dev(joy->d);
12687384Simp	return (0);
12787384Simp}
12854156Speter
12954156Speter
13054156Speterstatic int
13183366Sjulianjoyopen(dev_t dev, int flags, int fmt, struct thread *td)
1325897Sjmz{
13387384Simp	int i = joypart (dev);
13487384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
1355897Sjmz
13687384Simp	if (joy->timeout[i])
13787384Simp		return (EBUSY);
13887384Simp	joy->x_off[i] = joy->y_off[i] = 0;
13987384Simp	joy->timeout[i] = JOY_TIMEOUT;
14087384Simp	return (0);
1415897Sjmz}
14254156Speter
14354156Speterstatic int
14483366Sjulianjoyclose(dev_t dev, int flags, int fmt, struct thread *td)
1455897Sjmz{
14687384Simp	int i = joypart (dev);
14787384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
1485897Sjmz
14987384Simp	joy->timeout[i] = 0;
15087384Simp	return (0);
1515897Sjmz}
1525897Sjmz
15354156Speterstatic int
15454156Speterjoyread(dev_t dev, struct uio *uio, int flag)
1555897Sjmz{
15687384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
15787384Simp	bus_space_handle_t port = joy->port;
15887384Simp	bus_space_tag_t bt = joy->bt;
15987384Simp	struct timespec t, start, end;
16087384Simp	int state = 0;
16187384Simp	struct timespec x, y;
16287384Simp	struct joystick c;
16397554Salfred#ifndef __i386__
16487384Simp	int s;
1658876Srgrimes
16687384Simp	s = splhigh();
16754156Speter#else
16887384Simp	disable_intr ();
16954156Speter#endif
17087384Simp	bus_space_write_1 (bt, port, 0, 0xff);
17187384Simp	nanotime(&start);
17287384Simp	end.tv_sec = 0;
17387384Simp	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
17487384Simp	timespecadd(&end, &start);
17587384Simp	t = start;
17687384Simp	timespecclear(&x);
17787384Simp	timespecclear(&y);
17887384Simp	while (timespeccmp(&t, &end, <)) {
17987384Simp		state = bus_space_read_1 (bt, port, 0);
18087384Simp		if (joypart(dev) == 1)
18187384Simp			state >>= 2;
18287384Simp		nanotime(&t);
18387384Simp		if (!timespecisset(&x) && !(state & 0x01))
18487384Simp			x = t;
18587384Simp		if (!timespecisset(&y) && !(state & 0x02))
18687384Simp			y = t;
18787384Simp		if (timespecisset(&x) && timespecisset(&y))
18887384Simp			break;
18987384Simp	}
19097554Salfred#ifndef __i386__
19187384Simp	splx(s);
19254156Speter#else
19387384Simp	enable_intr ();
19454156Speter#endif
19587384Simp	if (timespecisset(&x)) {
19687384Simp		timespecsub(&x, &start);
19787384Simp		c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
19887384Simp	} else
19987384Simp		c.x = 0x80000000;
20087384Simp	if (timespecisset(&y)) {
20187384Simp		timespecsub(&y, &start);
20287384Simp		c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
20387384Simp	} else
20487384Simp		c.y = 0x80000000;
20587384Simp	state >>= 4;
20687384Simp	c.b1 = ~state & 1;
20787384Simp	c.b2 = ~(state >> 1) & 1;
20887384Simp	return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
2095897Sjmz}
21012675Sjulian
21154156Speterstatic int
21283366Sjulianjoyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
2135897Sjmz{
21487384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
21587384Simp	int i = joypart (dev);
21687384Simp	int x;
2175897Sjmz
21887384Simp	switch (cmd) {
21987384Simp	case JOY_SETTIMEOUT:
22087384Simp		x = *(int *) data;
22187384Simp		if (x < 1 || x > 10000) /* 10ms maximum! */
22287384Simp			return EINVAL;
22387384Simp		joy->timeout[i] = x;
22487384Simp		break;
22587384Simp	case JOY_GETTIMEOUT:
22687384Simp		*(int *) data = joy->timeout[i];
22787384Simp		break;
22887384Simp	case JOY_SET_X_OFFSET:
22987384Simp		joy->x_off[i] = *(int *) data;
23087384Simp		break;
23187384Simp	case JOY_SET_Y_OFFSET:
23287384Simp		joy->y_off[i] = *(int *) data;
23387384Simp		break;
23487384Simp	case JOY_GET_X_OFFSET:
23587384Simp		*(int *) data = joy->x_off[i];
23687384Simp		break;
23787384Simp	case JOY_GET_Y_OFFSET:
23887384Simp		*(int *) data = joy->y_off[i];
23987384Simp		break;
24087384Simp	default:
24187384Simp		return (ENOTTY);
24287384Simp	}
24387384Simp	return (0);
2445897Sjmz}
245