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$");
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
57183397Sed#define joypart(d) (dev2unit(d)&1)
585897Sjmz#ifndef JOY_TIMEOUT
595897Sjmz#define JOY_TIMEOUT   2000 /* 2 milliseconds */
605897Sjmz#endif
615897Sjmz
6212675Sjulianstatic	d_open_t	joyopen;
6312675Sjulianstatic	d_close_t	joyclose;
6412675Sjulianstatic	d_read_t	joyread;
6512675Sjulianstatic	d_ioctl_t	joyioctl;
6612675Sjulian
6747625Sphkstatic struct cdevsw joy_cdevsw = {
68126080Sphk	.d_version =	D_VERSION,
69126080Sphk	.d_flags =	D_NEEDGIANT,
70111815Sphk	.d_open =	joyopen,
71111815Sphk	.d_close =	joyclose,
72111815Sphk	.d_read =	joyread,
73111815Sphk	.d_ioctl =	joyioctl,
74111815Sphk	.d_name =	"joy",
7547625Sphk};
7612675Sjulian
7789086Simpdevclass_t joy_devclass;
785897Sjmz
7987384Simpint
8087384Simpjoy_probe(device_t dev)
815897Sjmz{
825897Sjmz#ifdef WANT_JOYSTICK_CONNECTED
8354156Speter#ifdef notyet
8487384Simp	outb(dev->id_iobase, 0xff);
8587384Simp	DELAY(10000); /*  10 ms delay */
8687384Simp	return (inb(dev->id_iobase) & 0x0f) != 0x0f;
8787384Simp#else
8887384Simp	return (0);
8954156Speter#endif
905897Sjmz#else
9187384Simp	return (0);
925897Sjmz#endif
935897Sjmz}
945897Sjmz
9587384Simpint
9687384Simpjoy_attach(device_t dev)
975897Sjmz{
9887384Simp	int	unit = device_get_unit(dev);
9987384Simp	struct joy_softc *joy = device_get_softc(dev);
1005897Sjmz
10187384Simp	joy->rid = 0;
102127135Snjl	joy->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &joy->rid,
103152249Sjylefort	    RF_ACTIVE|RF_SHAREABLE);
10487384Simp	if (joy->res == NULL)
10587384Simp		return ENXIO;
10687384Simp	joy->bt = rman_get_bustag(joy->res);
10787384Simp	joy->port = rman_get_bushandle(joy->res);
10887384Simp	joy->timeout[0] = joy->timeout[1] = 0;
109152249Sjylefort	joy->d = make_dev(&joy_cdevsw, unit, 0, 0, 0600, "joy%d", unit);
110191054Sed	joy->d->si_drv1 = joy;
11187384Simp	return (0);
1125897Sjmz}
1135897Sjmz
11487384Simpint
11587384Simpjoy_detach(device_t dev)
11687384Simp{
11787384Simp	struct joy_softc *joy = device_get_softc(dev);
11854156Speter
11987384Simp	if (joy->res != NULL)
12087384Simp		bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res);
12187384Simp	if (joy->d)
12287384Simp		destroy_dev(joy->d);
12387384Simp	return (0);
12487384Simp}
12554156Speter
12654156Speter
12754156Speterstatic int
128130585Sphkjoyopen(struct cdev *dev, int flags, int fmt, struct thread *td)
1295897Sjmz{
13087384Simp	int i = joypart (dev);
131191054Sed	struct joy_softc *joy = dev->si_drv1;
1325897Sjmz
13387384Simp	if (joy->timeout[i])
13487384Simp		return (EBUSY);
13587384Simp	joy->x_off[i] = joy->y_off[i] = 0;
13687384Simp	joy->timeout[i] = JOY_TIMEOUT;
13787384Simp	return (0);
1385897Sjmz}
13954156Speter
14054156Speterstatic int
141130585Sphkjoyclose(struct cdev *dev, int flags, int fmt, struct thread *td)
1425897Sjmz{
14387384Simp	int i = joypart (dev);
144191054Sed	struct joy_softc *joy = dev->si_drv1;
1455897Sjmz
14687384Simp	joy->timeout[i] = 0;
14787384Simp	return (0);
1485897Sjmz}
1495897Sjmz
15054156Speterstatic int
151130585Sphkjoyread(struct cdev *dev, struct uio *uio, int flag)
1525897Sjmz{
153191054Sed	struct joy_softc *joy = dev->si_drv1;
15487384Simp	bus_space_handle_t port = joy->port;
15587384Simp	bus_space_tag_t bt = joy->bt;
15687384Simp	struct timespec t, start, end;
15787384Simp	int state = 0;
15887384Simp	struct timespec x, y;
15987384Simp	struct joystick c;
16097554Salfred#ifndef __i386__
16187384Simp	int s;
1628876Srgrimes
16387384Simp	s = splhigh();
16454156Speter#else
16587384Simp	disable_intr ();
16654156Speter#endif
167152249Sjylefort	nanotime(&t);
168152249Sjylefort	end.tv_sec = 0;
169152249Sjylefort	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
170152249Sjylefort	timespecadd(&end, &t);
171152249Sjylefort	for (; timespeccmp(&t, &end, <) && (bus_space_read_1(bt, port, 0) & 0x0f); nanotime(&t))
172152249Sjylefort		;	/* nothing */
17387384Simp	bus_space_write_1 (bt, port, 0, 0xff);
17487384Simp	nanotime(&start);
17587384Simp	end.tv_sec = 0;
17687384Simp	end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
17787384Simp	timespecadd(&end, &start);
17887384Simp	t = start;
17987384Simp	timespecclear(&x);
18087384Simp	timespecclear(&y);
18187384Simp	while (timespeccmp(&t, &end, <)) {
18287384Simp		state = bus_space_read_1 (bt, port, 0);
18387384Simp		if (joypart(dev) == 1)
18487384Simp			state >>= 2;
18587384Simp		nanotime(&t);
18687384Simp		if (!timespecisset(&x) && !(state & 0x01))
18787384Simp			x = t;
18887384Simp		if (!timespecisset(&y) && !(state & 0x02))
18987384Simp			y = t;
19087384Simp		if (timespecisset(&x) && timespecisset(&y))
19187384Simp			break;
19287384Simp	}
19397554Salfred#ifndef __i386__
19487384Simp	splx(s);
19554156Speter#else
19687384Simp	enable_intr ();
19754156Speter#endif
19887384Simp	if (timespecisset(&x)) {
19987384Simp		timespecsub(&x, &start);
20087384Simp		c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
20187384Simp	} else
20287384Simp		c.x = 0x80000000;
20387384Simp	if (timespecisset(&y)) {
20487384Simp		timespecsub(&y, &start);
20587384Simp		c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
20687384Simp	} else
20787384Simp		c.y = 0x80000000;
20887384Simp	state >>= 4;
20987384Simp	c.b1 = ~state & 1;
21087384Simp	c.b2 = ~(state >> 1) & 1;
21187384Simp	return (uiomove((caddr_t)&c, sizeof(struct joystick), uio));
2125897Sjmz}
21312675Sjulian
21454156Speterstatic int
215130585Sphkjoyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
2165897Sjmz{
217191054Sed	struct joy_softc *joy = dev->si_drv1;
21887384Simp	int i = joypart (dev);
21987384Simp	int x;
2205897Sjmz
22187384Simp	switch (cmd) {
22287384Simp	case JOY_SETTIMEOUT:
22387384Simp		x = *(int *) data;
22487384Simp		if (x < 1 || x > 10000) /* 10ms maximum! */
22587384Simp			return EINVAL;
22687384Simp		joy->timeout[i] = x;
22787384Simp		break;
22887384Simp	case JOY_GETTIMEOUT:
22987384Simp		*(int *) data = joy->timeout[i];
23087384Simp		break;
23187384Simp	case JOY_SET_X_OFFSET:
23287384Simp		joy->x_off[i] = *(int *) data;
23387384Simp		break;
23487384Simp	case JOY_SET_Y_OFFSET:
23587384Simp		joy->y_off[i] = *(int *) data;
23687384Simp		break;
23787384Simp	case JOY_GET_X_OFFSET:
23887384Simp		*(int *) data = joy->x_off[i];
23987384Simp		break;
24087384Simp	case JOY_GET_Y_OFFSET:
24187384Simp		*(int *) data = joy->y_off[i];
24287384Simp		break;
24387384Simp	default:
24487384Simp		return (ENOTTY);
24587384Simp	}
24687384Simp	return (0);
2475897Sjmz}
248