Home | History | Annotate | Line # | Download | only in ic
joy.c revision 1.2.4.4
      1  1.2.4.4  nathanw /*	$NetBSD: joy.c,v 1.2.4.4 2002/11/11 22:09:50 nathanw Exp $	*/
      2  1.2.4.2  nathanw 
      3  1.2.4.2  nathanw /*-
      4  1.2.4.2  nathanw  * Copyright (c) 1995 Jean-Marc Zucconi
      5  1.2.4.2  nathanw  * All rights reserved.
      6  1.2.4.2  nathanw  *
      7  1.2.4.2  nathanw  * Ported to NetBSD by Matthieu Herrb <matthieu (at) laas.fr>
      8  1.2.4.2  nathanw  *
      9  1.2.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
     10  1.2.4.2  nathanw  * modification, are permitted provided that the following conditions
     11  1.2.4.2  nathanw  * are met:
     12  1.2.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     13  1.2.4.2  nathanw  *    notice, this list of conditions and the following disclaimer
     14  1.2.4.2  nathanw  *    in this position and unchanged.
     15  1.2.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.2.4.2  nathanw  * 3. The name of the author may not be used to endorse or promote products
     19  1.2.4.2  nathanw  *    derived from this software without specific prior written permission
     20  1.2.4.2  nathanw  *
     21  1.2.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.2.4.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.2.4.2  nathanw  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.2.4.2  nathanw  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.2.4.2  nathanw  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.2.4.2  nathanw  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.2.4.2  nathanw  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.2.4.2  nathanw  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.2.4.2  nathanw  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.2.4.2  nathanw  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.2.4.2  nathanw  *
     32  1.2.4.2  nathanw  */
     33  1.2.4.2  nathanw 
     34  1.2.4.2  nathanw #include <sys/cdefs.h>
     35  1.2.4.4  nathanw __KERNEL_RCSID(0, "$NetBSD: joy.c,v 1.2.4.4 2002/11/11 22:09:50 nathanw Exp $");
     36  1.2.4.2  nathanw 
     37  1.2.4.2  nathanw #include <sys/param.h>
     38  1.2.4.2  nathanw #include <sys/systm.h>
     39  1.2.4.2  nathanw #include <sys/kernel.h>
     40  1.2.4.2  nathanw #include <sys/device.h>
     41  1.2.4.2  nathanw #include <sys/errno.h>
     42  1.2.4.3  nathanw #include <sys/conf.h>
     43  1.2.4.4  nathanw #include <sys/event.h>
     44  1.2.4.2  nathanw 
     45  1.2.4.2  nathanw #include <machine/bus.h>
     46  1.2.4.2  nathanw 
     47  1.2.4.2  nathanw #include <machine/cpu.h>
     48  1.2.4.2  nathanw #include <machine/pio.h>
     49  1.2.4.2  nathanw #include <machine/joystick.h>
     50  1.2.4.2  nathanw 
     51  1.2.4.2  nathanw #include <dev/isa/isavar.h>
     52  1.2.4.2  nathanw #include <dev/isa/isareg.h>
     53  1.2.4.2  nathanw 
     54  1.2.4.2  nathanw #include <dev/ic/joyvar.h>
     55  1.2.4.2  nathanw 
     56  1.2.4.2  nathanw /*
     57  1.2.4.2  nathanw  * The game port can manage 4 buttons and 4 variable resistors (usually 2
     58  1.2.4.2  nathanw  * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
     59  1.2.4.2  nathanw  * Getting the state of the buttons is done by reading the game port;
     60  1.2.4.2  nathanw  * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
     61  1.2.4.2  nathanw  * to bits 0-3.  If button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5,
     62  1.2.4.2  nathanw  * 6, 7) is set to 0 to get the value of a resistor, write the value 0xff
     63  1.2.4.2  nathanw  * at port and wait until the corresponding bit returns to 0.
     64  1.2.4.2  nathanw  */
     65  1.2.4.2  nathanw 
     66  1.2.4.2  nathanw /*
     67  1.2.4.2  nathanw  * The formulae below only work if u is ``not too large''.  See also
     68  1.2.4.2  nathanw  * the discussion in microtime.s
     69  1.2.4.2  nathanw  */
     70  1.2.4.2  nathanw #define USEC2TICKS(u) 	(((u) * 19549) >> 14)
     71  1.2.4.2  nathanw #define TICKS2USEC(u) 	(((u) * 3433) >> 12)
     72  1.2.4.2  nathanw 
     73  1.2.4.2  nathanw 
     74  1.2.4.2  nathanw #define JOYPART(d) (minor(d) & 1)
     75  1.2.4.2  nathanw #define JOYUNIT(d) minor(d) >> 1 & 3
     76  1.2.4.2  nathanw 
     77  1.2.4.2  nathanw #ifndef JOY_TIMEOUT
     78  1.2.4.2  nathanw #define JOY_TIMEOUT   2000	/* 2 milliseconds */
     79  1.2.4.2  nathanw #endif
     80  1.2.4.2  nathanw 
     81  1.2.4.2  nathanw extern struct cfdriver joy_cd;
     82  1.2.4.2  nathanw 
     83  1.2.4.3  nathanw dev_type_open(joyopen);
     84  1.2.4.3  nathanw dev_type_close(joyclose);
     85  1.2.4.3  nathanw dev_type_read(joyread);
     86  1.2.4.3  nathanw dev_type_ioctl(joyioctl);
     87  1.2.4.3  nathanw 
     88  1.2.4.3  nathanw const struct cdevsw joy_cdevsw = {
     89  1.2.4.3  nathanw 	joyopen, joyclose, joyread, nowrite, joyioctl,
     90  1.2.4.4  nathanw 	nostop, notty, nopoll, nommap, nokqfilter,
     91  1.2.4.3  nathanw };
     92  1.2.4.3  nathanw 
     93  1.2.4.2  nathanw void
     94  1.2.4.2  nathanw joyattach(sc)
     95  1.2.4.2  nathanw 	struct joy_softc *sc;
     96  1.2.4.2  nathanw {
     97  1.2.4.2  nathanw 
     98  1.2.4.2  nathanw 	sc->timeout[0] = sc->timeout[1] = 0;
     99  1.2.4.2  nathanw 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff);
    100  1.2.4.2  nathanw 	DELAY(10000);		/* 10 ms delay */
    101  1.2.4.2  nathanw 	printf("%s: joystick %sconnected\n", sc->sc_dev.dv_xname,
    102  1.2.4.2  nathanw 	    (bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ?
    103  1.2.4.2  nathanw 	    "not " : "");
    104  1.2.4.2  nathanw 
    105  1.2.4.2  nathanw 	sc->sc_timer_freq = joy_timer_freq();
    106  1.2.4.2  nathanw }
    107  1.2.4.2  nathanw 
    108  1.2.4.2  nathanw int
    109  1.2.4.2  nathanw joyopen(dev, flag, mode, p)
    110  1.2.4.2  nathanw 	dev_t dev;
    111  1.2.4.2  nathanw 	int flag, mode;
    112  1.2.4.2  nathanw 	struct proc *p;
    113  1.2.4.2  nathanw {
    114  1.2.4.2  nathanw 	int unit = JOYUNIT(dev);
    115  1.2.4.2  nathanw 	int i = JOYPART(dev);
    116  1.2.4.2  nathanw 	struct joy_softc *sc;
    117  1.2.4.2  nathanw 
    118  1.2.4.2  nathanw 	if (unit >= joy_cd.cd_ndevs)
    119  1.2.4.2  nathanw 		return (ENXIO);
    120  1.2.4.2  nathanw 	sc = joy_cd.cd_devs[unit];
    121  1.2.4.2  nathanw 	if (sc == 0)
    122  1.2.4.2  nathanw 		return (ENXIO);
    123  1.2.4.2  nathanw 
    124  1.2.4.2  nathanw 	if (sc->timeout[i])
    125  1.2.4.2  nathanw 		return (EBUSY);
    126  1.2.4.2  nathanw 
    127  1.2.4.2  nathanw 	sc->x_off[i] = sc->y_off[i] = 0;
    128  1.2.4.2  nathanw 	sc->timeout[i] = JOY_TIMEOUT;
    129  1.2.4.2  nathanw 	return (0);
    130  1.2.4.2  nathanw }
    131  1.2.4.2  nathanw 
    132  1.2.4.2  nathanw int
    133  1.2.4.2  nathanw joyclose(dev, flag, mode, p)
    134  1.2.4.2  nathanw 	dev_t dev;
    135  1.2.4.2  nathanw 	int flag, mode;
    136  1.2.4.2  nathanw 	struct proc *p;
    137  1.2.4.2  nathanw {
    138  1.2.4.2  nathanw 	int unit = JOYUNIT(dev);
    139  1.2.4.2  nathanw 	int i = JOYPART(dev);
    140  1.2.4.2  nathanw 	struct joy_softc *sc = joy_cd.cd_devs[unit];
    141  1.2.4.2  nathanw 
    142  1.2.4.2  nathanw 	sc->timeout[i] = 0;
    143  1.2.4.2  nathanw 	return (0);
    144  1.2.4.2  nathanw }
    145  1.2.4.2  nathanw 
    146  1.2.4.2  nathanw int
    147  1.2.4.2  nathanw joyread(dev, uio, flag)
    148  1.2.4.2  nathanw 	dev_t dev;
    149  1.2.4.2  nathanw 	struct uio *uio;
    150  1.2.4.2  nathanw 	int flag;
    151  1.2.4.2  nathanw {
    152  1.2.4.2  nathanw 	int unit = JOYUNIT(dev);
    153  1.2.4.2  nathanw 	struct joy_softc *sc = joy_cd.cd_devs[unit];
    154  1.2.4.2  nathanw 	bus_space_tag_t iot = sc->sc_iot;
    155  1.2.4.2  nathanw 	bus_space_handle_t ioh = sc->sc_ioh;
    156  1.2.4.2  nathanw 	struct joystick c;
    157  1.2.4.2  nathanw 	int i, t0, t1, s;
    158  1.2.4.2  nathanw 	int state = 0, x = 0, y = 0;
    159  1.2.4.2  nathanw 
    160  1.2.4.2  nathanw 	s = splhigh();	/* XXX */
    161  1.2.4.2  nathanw 	bus_space_write_1(iot, ioh, 0, 0xff);
    162  1.2.4.2  nathanw 	t0 = joy_get_tick();
    163  1.2.4.2  nathanw 	t1 = t0;
    164  1.2.4.2  nathanw 	i = USEC2TICKS(sc->timeout[JOYPART(dev)]);
    165  1.2.4.2  nathanw 	while (t0 - t1 < i) {
    166  1.2.4.2  nathanw 		state = bus_space_read_1(iot, ioh, 0);
    167  1.2.4.2  nathanw 		if (JOYPART(dev) == 1)
    168  1.2.4.2  nathanw 			state >>= 2;
    169  1.2.4.2  nathanw 		t1 = joy_get_tick();
    170  1.2.4.2  nathanw 		if (t1 > t0)
    171  1.2.4.2  nathanw 			t1 -= sc->sc_timer_freq / hz;
    172  1.2.4.2  nathanw 		if (!x && !(state & 0x01))
    173  1.2.4.2  nathanw 			x = t1;
    174  1.2.4.2  nathanw 		if (!y && !(state & 0x02))
    175  1.2.4.2  nathanw 			y = t1;
    176  1.2.4.2  nathanw 		if (x && y)
    177  1.2.4.2  nathanw 			break;
    178  1.2.4.2  nathanw 	}
    179  1.2.4.2  nathanw 	splx(s);	/* XXX */
    180  1.2.4.2  nathanw 
    181  1.2.4.2  nathanw 	c.x = x ? sc->x_off[JOYPART(dev)] + TICKS2USEC(t0 - x) : 0x80000000;
    182  1.2.4.2  nathanw 	c.y = y ? sc->y_off[JOYPART(dev)] + TICKS2USEC(t0 - y) : 0x80000000;
    183  1.2.4.2  nathanw 	state >>= 4;
    184  1.2.4.2  nathanw 	c.b1 = ~state & 1;
    185  1.2.4.2  nathanw 	c.b2 = ~(state >> 1) & 1;
    186  1.2.4.2  nathanw 	return (uiomove(&c, sizeof(struct joystick), uio));
    187  1.2.4.2  nathanw }
    188  1.2.4.2  nathanw 
    189  1.2.4.2  nathanw int
    190  1.2.4.2  nathanw joyioctl(dev, cmd, data, flag, p)
    191  1.2.4.2  nathanw 	dev_t dev;
    192  1.2.4.2  nathanw 	u_long cmd;
    193  1.2.4.2  nathanw 	caddr_t data;
    194  1.2.4.2  nathanw 	int flag;
    195  1.2.4.2  nathanw 	struct proc *p;
    196  1.2.4.2  nathanw {
    197  1.2.4.2  nathanw 	int unit = JOYUNIT(dev);
    198  1.2.4.2  nathanw 	struct joy_softc *sc = joy_cd.cd_devs[unit];
    199  1.2.4.2  nathanw 	int i = JOYPART(dev);
    200  1.2.4.2  nathanw 	int x;
    201  1.2.4.2  nathanw 
    202  1.2.4.2  nathanw 	switch (cmd) {
    203  1.2.4.2  nathanw 	case JOY_SETTIMEOUT:
    204  1.2.4.2  nathanw 		x = *(int *) data;
    205  1.2.4.2  nathanw 		if (x < 1 || x > 10000)	/* 10ms maximum! */
    206  1.2.4.2  nathanw 			return (EINVAL);
    207  1.2.4.2  nathanw 		sc->timeout[i] = x;
    208  1.2.4.2  nathanw 		break;
    209  1.2.4.2  nathanw 	case JOY_GETTIMEOUT:
    210  1.2.4.2  nathanw 		*(int *) data = sc->timeout[i];
    211  1.2.4.2  nathanw 		break;
    212  1.2.4.2  nathanw 	case JOY_SET_X_OFFSET:
    213  1.2.4.2  nathanw 		sc->x_off[i] = *(int *) data;
    214  1.2.4.2  nathanw 		break;
    215  1.2.4.2  nathanw 	case JOY_SET_Y_OFFSET:
    216  1.2.4.2  nathanw 		sc->y_off[i] = *(int *) data;
    217  1.2.4.2  nathanw 		break;
    218  1.2.4.2  nathanw 	case JOY_GET_X_OFFSET:
    219  1.2.4.2  nathanw 		*(int *) data = sc->x_off[i];
    220  1.2.4.2  nathanw 		break;
    221  1.2.4.2  nathanw 	case JOY_GET_Y_OFFSET:
    222  1.2.4.2  nathanw 		*(int *) data = sc->y_off[i];
    223  1.2.4.2  nathanw 		break;
    224  1.2.4.2  nathanw 	default:
    225  1.2.4.2  nathanw 		return (ENXIO);
    226  1.2.4.2  nathanw 	}
    227  1.2.4.2  nathanw 	return 0;
    228  1.2.4.2  nathanw }
    229