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