joy.c revision 111815
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 111815 2003-03-03 12:15:54Z phk $
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 = {
71111815Sphk	.d_open =	joyopen,
72111815Sphk	.d_close =	joyclose,
73111815Sphk	.d_read =	joyread,
74111815Sphk	.d_ioctl =	joyioctl,
75111815Sphk	.d_name =	"joy",
76111815Sphk	.d_maj =	CDEV_MAJOR,
7747625Sphk};
7812675Sjulian
7989086Simpdevclass_t joy_devclass;
805897Sjmz
8187384Simpint
8287384Simpjoy_probe(device_t dev)
835897Sjmz{
845897Sjmz#ifdef WANT_JOYSTICK_CONNECTED
8554156Speter#ifdef notyet
8687384Simp	outb(dev->id_iobase, 0xff);
8787384Simp	DELAY(10000); /*  10 ms delay */
8887384Simp	return (inb(dev->id_iobase) & 0x0f) != 0x0f;
8987384Simp#else
9087384Simp	return (0);
9154156Speter#endif
925897Sjmz#else
9387384Simp	return (0);
945897Sjmz#endif
955897Sjmz}
965897Sjmz
9787384Simpint
9887384Simpjoy_attach(device_t dev)
995897Sjmz{
10087384Simp	int	unit = device_get_unit(dev);
10187384Simp	struct joy_softc *joy = device_get_softc(dev);
1025897Sjmz
10387384Simp	joy->rid = 0;
10487384Simp	joy->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &joy->rid, 0, ~0, 1,
10587384Simp	    RF_ACTIVE);
10687384Simp	if (joy->res == NULL)
10787384Simp		return ENXIO;
10887384Simp	joy->bt = rman_get_bustag(joy->res);
10987384Simp	joy->port = rman_get_bushandle(joy->res);
11087384Simp	joy->timeout[0] = joy->timeout[1] = 0;
11187384Simp	joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit);
11287384Simp	return (0);
1135897Sjmz}
1145897Sjmz
11587384Simpint
11687384Simpjoy_detach(device_t dev)
11787384Simp{
11887384Simp	struct joy_softc *joy = device_get_softc(dev);
11954156Speter
12087384Simp	if (joy->res != NULL)
12187384Simp		bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
12287384Simp	if (joy->d)
12387384Simp		destroy_dev(joy->d);
12487384Simp	return (0);
12587384Simp}
12654156Speter
12754156Speter
12854156Speterstatic int
12983366Sjulianjoyopen(dev_t dev, int flags, int fmt, struct thread *td)
1305897Sjmz{
13187384Simp	int i = joypart (dev);
13287384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
1335897Sjmz
13487384Simp	if (joy->timeout[i])
13587384Simp		return (EBUSY);
13687384Simp	joy->x_off[i] = joy->y_off[i] = 0;
13787384Simp	joy->timeout[i] = JOY_TIMEOUT;
13887384Simp	return (0);
1395897Sjmz}
14054156Speter
14154156Speterstatic int
14283366Sjulianjoyclose(dev_t dev, int flags, int fmt, struct thread *td)
1435897Sjmz{
14487384Simp	int i = joypart (dev);
14587384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
1465897Sjmz
14787384Simp	joy->timeout[i] = 0;
14887384Simp	return (0);
1495897Sjmz}
1505897Sjmz
15154156Speterstatic int
15254156Speterjoyread(dev_t dev, struct uio *uio, int flag)
1535897Sjmz{
15487384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
15587384Simp	bus_space_handle_t port = joy->port;
15687384Simp	bus_space_tag_t bt = joy->bt;
15787384Simp	struct timespec t, start, end;
15887384Simp	int state = 0;
15987384Simp	struct timespec x, y;
16087384Simp	struct joystick c;
16197554Salfred#ifndef __i386__
16287384Simp	int s;
1638876Srgrimes
16487384Simp	s = splhigh();
16554156Speter#else
16687384Simp	disable_intr ();
16754156Speter#endif
16887384Simp	bus_space_write_1 (bt, port, 0, 0xff);
16987384Simp	nanotime(&start);
17087384Simp	end.tv_sec = 0;
17187384Simp	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
17287384Simp	timespecadd(&end, &start);
17387384Simp	t = start;
17487384Simp	timespecclear(&x);
17587384Simp	timespecclear(&y);
17687384Simp	while (timespeccmp(&t, &end, <)) {
17787384Simp		state = bus_space_read_1 (bt, port, 0);
17887384Simp		if (joypart(dev) == 1)
17987384Simp			state >>= 2;
18087384Simp		nanotime(&t);
18187384Simp		if (!timespecisset(&x) && !(state & 0x01))
18287384Simp			x = t;
18387384Simp		if (!timespecisset(&y) && !(state & 0x02))
18487384Simp			y = t;
18587384Simp		if (timespecisset(&x) && timespecisset(&y))
18687384Simp			break;
18787384Simp	}
18897554Salfred#ifndef __i386__
18987384Simp	splx(s);
19054156Speter#else
19187384Simp	enable_intr ();
19254156Speter#endif
19387384Simp	if (timespecisset(&x)) {
19487384Simp		timespecsub(&x, &start);
19587384Simp		c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
19687384Simp	} else
19787384Simp		c.x = 0x80000000;
19887384Simp	if (timespecisset(&y)) {
19987384Simp		timespecsub(&y, &start);
20087384Simp		c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
20187384Simp	} else
20287384Simp		c.y = 0x80000000;
20387384Simp	state >>= 4;
20487384Simp	c.b1 = ~state & 1;
20587384Simp	c.b2 = ~(state >> 1) & 1;
20687384Simp	return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
2075897Sjmz}
20812675Sjulian
20954156Speterstatic int
21083366Sjulianjoyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
2115897Sjmz{
21287384Simp	struct joy_softc *joy = JOY_SOFTC(UNIT(dev));
21387384Simp	int i = joypart (dev);
21487384Simp	int x;
2155897Sjmz
21687384Simp	switch (cmd) {
21787384Simp	case JOY_SETTIMEOUT:
21887384Simp		x = *(int *) data;
21987384Simp		if (x < 1 || x > 10000) /* 10ms maximum! */
22087384Simp			return EINVAL;
22187384Simp		joy->timeout[i] = x;
22287384Simp		break;
22387384Simp	case JOY_GETTIMEOUT:
22487384Simp		*(int *) data = joy->timeout[i];
22587384Simp		break;
22687384Simp	case JOY_SET_X_OFFSET:
22787384Simp		joy->x_off[i] = *(int *) data;
22887384Simp		break;
22987384Simp	case JOY_SET_Y_OFFSET:
23087384Simp		joy->y_off[i] = *(int *) data;
23187384Simp		break;
23287384Simp	case JOY_GET_X_OFFSET:
23387384Simp		*(int *) data = joy->x_off[i];
23487384Simp		break;
23587384Simp	case JOY_GET_Y_OFFSET:
23687384Simp		*(int *) data = joy->y_off[i];
23787384Simp		break;
23887384Simp	default:
23987384Simp		return (ENOTTY);
24087384Simp	}
24187384Simp	return (0);
2425897Sjmz}
243