1 /* $NetBSD: joy.c,v 1.3 2002/09/06 13:18:43 gehenna Exp $ */ 2 3 /*- 4 * Copyright (c) 1995 Jean-Marc Zucconi 5 * All rights reserved. 6 * 7 * Ported to NetBSD by Matthieu Herrb <matthieu@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.3 2002/09/06 13:18:43 gehenna 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 44 #include <machine/bus.h> 45 46 #include <machine/cpu.h> 47 #include <machine/pio.h> 48 #include <machine/joystick.h> 49 50 #include <dev/isa/isavar.h> 51 #include <dev/isa/isareg.h> 52 53 #include <dev/ic/joyvar.h> 54 55 /* 56 * The game port can manage 4 buttons and 4 variable resistors (usually 2 57 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201. 58 * Getting the state of the buttons is done by reading the game port; 59 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2) 60 * to bits 0-3. If button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 61 * 6, 7) is set to 0 to get the value of a resistor, write the value 0xff 62 * at port and wait until the corresponding bit returns to 0. 63 */ 64 65 /* 66 * The formulae below only work if u is ``not too large''. See also 67 * the discussion in microtime.s 68 */ 69 #define USEC2TICKS(u) (((u) * 19549) >> 14) 70 #define TICKS2USEC(u) (((u) * 3433) >> 12) 71 72 73 #define JOYPART(d) (minor(d) & 1) 74 #define JOYUNIT(d) minor(d) >> 1 & 3 75 76 #ifndef JOY_TIMEOUT 77 #define JOY_TIMEOUT 2000 /* 2 milliseconds */ 78 #endif 79 80 extern struct cfdriver joy_cd; 81 82 dev_type_open(joyopen); 83 dev_type_close(joyclose); 84 dev_type_read(joyread); 85 dev_type_ioctl(joyioctl); 86 87 const struct cdevsw joy_cdevsw = { 88 joyopen, joyclose, joyread, nowrite, joyioctl, 89 nostop, notty, nopoll, nommap, 90 }; 91 92 void 93 joyattach(sc) 94 struct joy_softc *sc; 95 { 96 97 sc->timeout[0] = sc->timeout[1] = 0; 98 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff); 99 DELAY(10000); /* 10 ms delay */ 100 printf("%s: joystick %sconnected\n", sc->sc_dev.dv_xname, 101 (bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ? 102 "not " : ""); 103 104 sc->sc_timer_freq = joy_timer_freq(); 105 } 106 107 int 108 joyopen(dev, flag, mode, p) 109 dev_t dev; 110 int flag, mode; 111 struct proc *p; 112 { 113 int unit = JOYUNIT(dev); 114 int i = JOYPART(dev); 115 struct joy_softc *sc; 116 117 if (unit >= joy_cd.cd_ndevs) 118 return (ENXIO); 119 sc = joy_cd.cd_devs[unit]; 120 if (sc == 0) 121 return (ENXIO); 122 123 if (sc->timeout[i]) 124 return (EBUSY); 125 126 sc->x_off[i] = sc->y_off[i] = 0; 127 sc->timeout[i] = JOY_TIMEOUT; 128 return (0); 129 } 130 131 int 132 joyclose(dev, flag, mode, p) 133 dev_t dev; 134 int flag, mode; 135 struct proc *p; 136 { 137 int unit = JOYUNIT(dev); 138 int i = JOYPART(dev); 139 struct joy_softc *sc = joy_cd.cd_devs[unit]; 140 141 sc->timeout[i] = 0; 142 return (0); 143 } 144 145 int 146 joyread(dev, uio, flag) 147 dev_t dev; 148 struct uio *uio; 149 int flag; 150 { 151 int unit = JOYUNIT(dev); 152 struct joy_softc *sc = joy_cd.cd_devs[unit]; 153 bus_space_tag_t iot = sc->sc_iot; 154 bus_space_handle_t ioh = sc->sc_ioh; 155 struct joystick c; 156 int i, t0, t1, s; 157 int state = 0, x = 0, y = 0; 158 159 s = splhigh(); /* XXX */ 160 bus_space_write_1(iot, ioh, 0, 0xff); 161 t0 = joy_get_tick(); 162 t1 = t0; 163 i = USEC2TICKS(sc->timeout[JOYPART(dev)]); 164 while (t0 - t1 < i) { 165 state = bus_space_read_1(iot, ioh, 0); 166 if (JOYPART(dev) == 1) 167 state >>= 2; 168 t1 = joy_get_tick(); 169 if (t1 > t0) 170 t1 -= sc->sc_timer_freq / hz; 171 if (!x && !(state & 0x01)) 172 x = t1; 173 if (!y && !(state & 0x02)) 174 y = t1; 175 if (x && y) 176 break; 177 } 178 splx(s); /* XXX */ 179 180 c.x = x ? sc->x_off[JOYPART(dev)] + TICKS2USEC(t0 - x) : 0x80000000; 181 c.y = y ? sc->y_off[JOYPART(dev)] + TICKS2USEC(t0 - y) : 0x80000000; 182 state >>= 4; 183 c.b1 = ~state & 1; 184 c.b2 = ~(state >> 1) & 1; 185 return (uiomove(&c, sizeof(struct joystick), uio)); 186 } 187 188 int 189 joyioctl(dev, cmd, data, flag, p) 190 dev_t dev; 191 u_long cmd; 192 caddr_t data; 193 int flag; 194 struct proc *p; 195 { 196 int unit = JOYUNIT(dev); 197 struct joy_softc *sc = joy_cd.cd_devs[unit]; 198 int i = JOYPART(dev); 199 int x; 200 201 switch (cmd) { 202 case JOY_SETTIMEOUT: 203 x = *(int *) data; 204 if (x < 1 || x > 10000) /* 10ms maximum! */ 205 return (EINVAL); 206 sc->timeout[i] = x; 207 break; 208 case JOY_GETTIMEOUT: 209 *(int *) data = sc->timeout[i]; 210 break; 211 case JOY_SET_X_OFFSET: 212 sc->x_off[i] = *(int *) data; 213 break; 214 case JOY_SET_Y_OFFSET: 215 sc->y_off[i] = *(int *) data; 216 break; 217 case JOY_GET_X_OFFSET: 218 *(int *) data = sc->x_off[i]; 219 break; 220 case JOY_GET_Y_OFFSET: 221 *(int *) data = sc->y_off[i]; 222 break; 223 default: 224 return (ENXIO); 225 } 226 return 0; 227 } 228