Home | History | Annotate | Line # | Download | only in ic
joy.c revision 1.16.14.2
      1 /*	$NetBSD: joy.c,v 1.16.14.2 2008/04/05 23:33:21 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.2 2008/04/05 23:33:21 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 	int maj;
     84 	device_t dev = sc->sc_dev;
     85 
     86 	sc->timeout[0] = sc->timeout[1] = 0;
     87 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff);
     88 	DELAY(10000);		/* 10 ms delay */
     89 	aprint_normal_dev(sc->sc_dev, "joystick %sconnected\n",
     90 	    (bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ?
     91 	    "not " : "");
     92 
     93 	maj = cdevsw_lookup_major(&joy_cdevsw);
     94 	device_register_name(makedev(maj, device_unit(dev)), dev, true,
     95 	    DEV_OTHER, device_xname(dev));
     96 }
     97 
     98 int
     99 joydetach(struct joy_softc *sc, int flags)
    100 {
    101 	int maj, mn;
    102 
    103 	device_unregister_all(sc->sc_dev);
    104 
    105 	maj = cdevsw_lookup_major(&joy_cdevsw);
    106 	mn = device_unit(sc->sc_dev) << 1;
    107 	vdevgone(maj, mn, mn, VCHR);
    108 	vdevgone(maj, mn + 1, mn + 1, VCHR);
    109 
    110 	return 0;
    111 }
    112 
    113 int
    114 joyopen(dev_t dev, int flag, int mode, struct lwp *l)
    115 {
    116 	int unit = JOYUNIT(dev);
    117 	int i = JOYPART(dev);
    118 	struct joy_softc *sc;
    119 
    120 	sc = device_lookup_private(&joy_cd, unit);
    121 	if (sc == NULL)
    122 		return ENXIO;
    123 
    124 	if (sc->timeout[i])
    125 		return EBUSY;
    126 
    127 	sc->x_off[i] = sc->y_off[i] = 0;
    128 	sc->timeout[i] = JOY_TIMEOUT;
    129 	return 0;
    130 }
    131 
    132 int
    133 joyclose(dev_t dev, int flag, int mode, struct lwp *l)
    134 {
    135 	int unit = JOYUNIT(dev);
    136 	int i = JOYPART(dev);
    137 	struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
    138 
    139 	sc->timeout[i] = 0;
    140 	return 0;
    141 }
    142 
    143 int
    144 joyread(dev_t dev, struct uio *uio, int flag)
    145 {
    146 	int unit = JOYUNIT(dev);
    147 	struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
    148 	bus_space_tag_t iot = sc->sc_iot;
    149 	bus_space_handle_t ioh = sc->sc_ioh;
    150 	struct joystick c;
    151 	int i, s;
    152 	struct timeval start, now, diff;
    153 	int state = 0, x = 0, y = 0;
    154 
    155 	s = splhigh();	/* XXX */
    156 	bus_space_write_1(iot, ioh, 0, 0xff);
    157 	microtime(&start);
    158 	now = start; /* structure assignment */
    159 	i = sc->timeout[JOYPART(dev)];
    160 	for (;;) {
    161 		timersub(&now, &start, &diff);
    162 		if (diff.tv_sec > 0 || diff.tv_usec > i)
    163 			break;
    164 		state = bus_space_read_1(iot, ioh, 0);
    165 		if (JOYPART(dev) == 1)
    166 			state >>= 2;
    167 		if (!x && !(state & 0x01))
    168 			x = diff.tv_usec;
    169 		if (!y && !(state & 0x02))
    170 			y = diff.tv_usec;
    171 		if (x && y)
    172 			break;
    173 		microtime(&now);
    174 	}
    175 	splx(s);	/* XXX */
    176 
    177 	c.x = x ? sc->x_off[JOYPART(dev)] + x : 0x80000000;
    178 	c.y = y ? sc->y_off[JOYPART(dev)] + y : 0x80000000;
    179 	state >>= 4;
    180 	c.b1 = ~state & 1;
    181 	c.b2 = ~(state >> 1) & 1;
    182 	return uiomove(&c, sizeof(struct joystick), uio);
    183 }
    184 
    185 int
    186 joyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    187 {
    188 	int unit = JOYUNIT(dev);
    189 	struct joy_softc *sc = device_lookup_private(&joy_cd, unit);
    190 	int i = JOYPART(dev);
    191 	int x;
    192 
    193 	switch (cmd) {
    194 	case JOY_SETTIMEOUT:
    195 		x = *(int *)data;
    196 		if (x < 1 || x > 10000)	/* 10ms maximum! */
    197 			return EINVAL;
    198 		sc->timeout[i] = x;
    199 		break;
    200 	case JOY_GETTIMEOUT:
    201 		*(int *)data = sc->timeout[i];
    202 		break;
    203 	case JOY_SET_X_OFFSET:
    204 		sc->x_off[i] = *(int *)data;
    205 		break;
    206 	case JOY_SET_Y_OFFSET:
    207 		sc->y_off[i] = *(int *)data;
    208 		break;
    209 	case JOY_GET_X_OFFSET:
    210 		*(int *)data = sc->x_off[i];
    211 		break;
    212 	case JOY_GET_Y_OFFSET:
    213 		*(int *)data = sc->y_off[i];
    214 		break;
    215 	default:
    216 		return ENXIO;
    217 	}
    218 	return 0;
    219 }
    220