Home | History | Annotate | Line # | Download | only in ic
joy.c revision 1.14.20.1
      1  1.14.20.1    bouyer /*	$NetBSD: joy.c,v 1.14.20.1 2007/10/25 22:37:46 bouyer 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.14.20.1    bouyer __KERNEL_RCSID(0, "$NetBSD: joy.c,v 1.14.20.1 2007/10/25 22:37:46 bouyer 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.8  drochner #include <sys/vnode.h>
     45  1.14.20.1    bouyer #include <sys/bus.h>
     46        1.1  jdolecek 
     47        1.7  drochner #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.8  drochner #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.11  christos 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
     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.13  christos joydetach(struct joy_softc *sc, int flags)
     95        1.8  drochner {
     96        1.8  drochner 	int maj, mn;
     97        1.8  drochner 
     98        1.8  drochner 	maj = cdevsw_lookup_major(&joy_cdevsw);
     99       1.10   thorpej 	mn = device_unit(&sc->sc_dev) << 1;
    100        1.8  drochner 	vdevgone(maj, mn, mn, VCHR);
    101        1.8  drochner 	vdevgone(maj, mn + 1, mn + 1, VCHR);
    102        1.8  drochner 
    103        1.8  drochner 	return (0);
    104        1.8  drochner }
    105        1.8  drochner 
    106        1.8  drochner int
    107       1.13  christos joyopen(dev_t dev, int flag, int mode, struct lwp *l)
    108        1.1  jdolecek {
    109        1.1  jdolecek 	int unit = JOYUNIT(dev);
    110        1.1  jdolecek 	int i = JOYPART(dev);
    111        1.1  jdolecek 	struct joy_softc *sc;
    112        1.1  jdolecek 
    113        1.1  jdolecek 	if (unit >= joy_cd.cd_ndevs)
    114        1.1  jdolecek 		return (ENXIO);
    115        1.1  jdolecek 	sc = joy_cd.cd_devs[unit];
    116        1.1  jdolecek 	if (sc == 0)
    117        1.1  jdolecek 		return (ENXIO);
    118        1.1  jdolecek 
    119        1.1  jdolecek 	if (sc->timeout[i])
    120        1.1  jdolecek 		return (EBUSY);
    121        1.1  jdolecek 
    122        1.1  jdolecek 	sc->x_off[i] = sc->y_off[i] = 0;
    123        1.1  jdolecek 	sc->timeout[i] = JOY_TIMEOUT;
    124        1.1  jdolecek 	return (0);
    125        1.1  jdolecek }
    126        1.1  jdolecek 
    127        1.1  jdolecek int
    128       1.13  christos joyclose(dev_t dev, int flag, int mode,
    129       1.13  christos     struct lwp *l)
    130        1.1  jdolecek {
    131        1.1  jdolecek 	int unit = JOYUNIT(dev);
    132        1.1  jdolecek 	int i = JOYPART(dev);
    133        1.1  jdolecek 	struct joy_softc *sc = joy_cd.cd_devs[unit];
    134        1.1  jdolecek 
    135        1.1  jdolecek 	sc->timeout[i] = 0;
    136        1.1  jdolecek 	return (0);
    137        1.1  jdolecek }
    138        1.1  jdolecek 
    139        1.1  jdolecek int
    140       1.13  christos joyread(dev_t dev, struct uio *uio, int flag)
    141        1.1  jdolecek {
    142        1.1  jdolecek 	int unit = JOYUNIT(dev);
    143        1.1  jdolecek 	struct joy_softc *sc = joy_cd.cd_devs[unit];
    144        1.1  jdolecek 	bus_space_tag_t iot = sc->sc_iot;
    145        1.1  jdolecek 	bus_space_handle_t ioh = sc->sc_ioh;
    146        1.1  jdolecek 	struct joystick c;
    147        1.7  drochner 	int i, s;
    148        1.7  drochner 	struct timeval start, now, diff;
    149        1.1  jdolecek 	int state = 0, x = 0, y = 0;
    150        1.1  jdolecek 
    151        1.1  jdolecek 	s = splhigh();	/* XXX */
    152        1.1  jdolecek 	bus_space_write_1(iot, ioh, 0, 0xff);
    153        1.7  drochner 	microtime(&start);
    154        1.7  drochner 	now = start; /* structure assignment */
    155        1.7  drochner 	i = sc->timeout[JOYPART(dev)];
    156        1.7  drochner 	for (;;) {
    157        1.7  drochner 		timersub(&now, &start, &diff);
    158        1.7  drochner 		if (diff.tv_sec > 0 || diff.tv_usec > i)
    159        1.7  drochner 			break;
    160        1.1  jdolecek 		state = bus_space_read_1(iot, ioh, 0);
    161        1.1  jdolecek 		if (JOYPART(dev) == 1)
    162        1.1  jdolecek 			state >>= 2;
    163        1.1  jdolecek 		if (!x && !(state & 0x01))
    164        1.7  drochner 			x = diff.tv_usec;
    165        1.1  jdolecek 		if (!y && !(state & 0x02))
    166        1.7  drochner 			y = diff.tv_usec;
    167        1.1  jdolecek 		if (x && y)
    168        1.1  jdolecek 			break;
    169        1.7  drochner 		microtime(&now);
    170        1.1  jdolecek 	}
    171        1.1  jdolecek 	splx(s);	/* XXX */
    172        1.1  jdolecek 
    173        1.7  drochner 	c.x = x ? sc->x_off[JOYPART(dev)] + x : 0x80000000;
    174        1.7  drochner 	c.y = y ? sc->y_off[JOYPART(dev)] + y : 0x80000000;
    175        1.1  jdolecek 	state >>= 4;
    176        1.1  jdolecek 	c.b1 = ~state & 1;
    177        1.1  jdolecek 	c.b2 = ~(state >> 1) & 1;
    178        1.1  jdolecek 	return (uiomove(&c, sizeof(struct joystick), uio));
    179        1.1  jdolecek }
    180        1.1  jdolecek 
    181        1.1  jdolecek int
    182       1.14  christos joyioctl(dev_t dev, u_long cmd, void *data, int flag,
    183       1.13  christos     struct lwp *l)
    184        1.1  jdolecek {
    185        1.1  jdolecek 	int unit = JOYUNIT(dev);
    186        1.1  jdolecek 	struct joy_softc *sc = joy_cd.cd_devs[unit];
    187        1.1  jdolecek 	int i = JOYPART(dev);
    188        1.1  jdolecek 	int x;
    189        1.1  jdolecek 
    190        1.1  jdolecek 	switch (cmd) {
    191        1.1  jdolecek 	case JOY_SETTIMEOUT:
    192        1.1  jdolecek 		x = *(int *) data;
    193        1.1  jdolecek 		if (x < 1 || x > 10000)	/* 10ms maximum! */
    194        1.1  jdolecek 			return (EINVAL);
    195        1.1  jdolecek 		sc->timeout[i] = x;
    196        1.1  jdolecek 		break;
    197        1.1  jdolecek 	case JOY_GETTIMEOUT:
    198        1.1  jdolecek 		*(int *) data = sc->timeout[i];
    199        1.1  jdolecek 		break;
    200        1.1  jdolecek 	case JOY_SET_X_OFFSET:
    201        1.1  jdolecek 		sc->x_off[i] = *(int *) data;
    202        1.1  jdolecek 		break;
    203        1.1  jdolecek 	case JOY_SET_Y_OFFSET:
    204        1.1  jdolecek 		sc->y_off[i] = *(int *) data;
    205        1.1  jdolecek 		break;
    206        1.1  jdolecek 	case JOY_GET_X_OFFSET:
    207        1.1  jdolecek 		*(int *) data = sc->x_off[i];
    208        1.1  jdolecek 		break;
    209        1.1  jdolecek 	case JOY_GET_Y_OFFSET:
    210        1.1  jdolecek 		*(int *) data = sc->y_off[i];
    211        1.1  jdolecek 		break;
    212        1.1  jdolecek 	default:
    213        1.1  jdolecek 		return (ENXIO);
    214        1.1  jdolecek 	}
    215        1.1  jdolecek 	return 0;
    216        1.1  jdolecek }
    217