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