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