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