1 /* $NetBSD: joy.c,v 1.2 2002/02/03 23:32:10 pooka 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.2 2002/02/03 23:32:10 pooka 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 43 #include <machine/bus.h> 44 45 #include <machine/cpu.h> 46 #include <machine/pio.h> 47 #include <machine/joystick.h> 48 #include <machine/conf.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 int joyopen __P((dev_t, int, int, struct proc *)); 81 int joyclose __P((dev_t, int, int, struct proc *)); 82 83 extern struct cfdriver joy_cd; 84 85 void 86 joyattach(sc) 87 struct joy_softc *sc; 88 { 89 90 sc->timeout[0] = sc->timeout[1] = 0; 91 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, 0xff); 92 DELAY(10000); /* 10 ms delay */ 93 printf("%s: joystick %sconnected\n", sc->sc_dev.dv_xname, 94 (bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0) & 0x0f) == 0x0f ? 95 "not " : ""); 96 97 sc->sc_timer_freq = joy_timer_freq(); 98 } 99 100 int 101 joyopen(dev, flag, mode, p) 102 dev_t dev; 103 int flag, mode; 104 struct proc *p; 105 { 106 int unit = JOYUNIT(dev); 107 int i = JOYPART(dev); 108 struct joy_softc *sc; 109 110 if (unit >= joy_cd.cd_ndevs) 111 return (ENXIO); 112 sc = joy_cd.cd_devs[unit]; 113 if (sc == 0) 114 return (ENXIO); 115 116 if (sc->timeout[i]) 117 return (EBUSY); 118 119 sc->x_off[i] = sc->y_off[i] = 0; 120 sc->timeout[i] = JOY_TIMEOUT; 121 return (0); 122 } 123 124 int 125 joyclose(dev, flag, mode, p) 126 dev_t dev; 127 int flag, mode; 128 struct proc *p; 129 { 130 int unit = JOYUNIT(dev); 131 int i = JOYPART(dev); 132 struct joy_softc *sc = joy_cd.cd_devs[unit]; 133 134 sc->timeout[i] = 0; 135 return (0); 136 } 137 138 int 139 joyread(dev, uio, flag) 140 dev_t dev; 141 struct uio *uio; 142 int flag; 143 { 144 int unit = JOYUNIT(dev); 145 struct joy_softc *sc = joy_cd.cd_devs[unit]; 146 bus_space_tag_t iot = sc->sc_iot; 147 bus_space_handle_t ioh = sc->sc_ioh; 148 struct joystick c; 149 int i, t0, t1, s; 150 int state = 0, x = 0, y = 0; 151 152 s = splhigh(); /* XXX */ 153 bus_space_write_1(iot, ioh, 0, 0xff); 154 t0 = joy_get_tick(); 155 t1 = t0; 156 i = USEC2TICKS(sc->timeout[JOYPART(dev)]); 157 while (t0 - t1 < i) { 158 state = bus_space_read_1(iot, ioh, 0); 159 if (JOYPART(dev) == 1) 160 state >>= 2; 161 t1 = joy_get_tick(); 162 if (t1 > t0) 163 t1 -= sc->sc_timer_freq / hz; 164 if (!x && !(state & 0x01)) 165 x = t1; 166 if (!y && !(state & 0x02)) 167 y = t1; 168 if (x && y) 169 break; 170 } 171 splx(s); /* XXX */ 172 173 c.x = x ? sc->x_off[JOYPART(dev)] + TICKS2USEC(t0 - x) : 0x80000000; 174 c.y = y ? sc->y_off[JOYPART(dev)] + TICKS2USEC(t0 - y) : 0x80000000; 175 state >>= 4; 176 c.b1 = ~state & 1; 177 c.b2 = ~(state >> 1) & 1; 178 return (uiomove(&c, sizeof(struct joystick), uio)); 179 } 180 181 int 182 joyioctl(dev, cmd, data, flag, p) 183 dev_t dev; 184 u_long cmd; 185 caddr_t data; 186 int flag; 187 struct proc *p; 188 { 189 int unit = JOYUNIT(dev); 190 struct joy_softc *sc = joy_cd.cd_devs[unit]; 191 int i = JOYPART(dev); 192 int x; 193 194 switch (cmd) { 195 case JOY_SETTIMEOUT: 196 x = *(int *) data; 197 if (x < 1 || x > 10000) /* 10ms maximum! */ 198 return (EINVAL); 199 sc->timeout[i] = x; 200 break; 201 case JOY_GETTIMEOUT: 202 *(int *) data = sc->timeout[i]; 203 break; 204 case JOY_SET_X_OFFSET: 205 sc->x_off[i] = *(int *) data; 206 break; 207 case JOY_SET_Y_OFFSET: 208 sc->y_off[i] = *(int *) data; 209 break; 210 case JOY_GET_X_OFFSET: 211 *(int *) data = sc->x_off[i]; 212 break; 213 case JOY_GET_Y_OFFSET: 214 *(int *) data = sc->y_off[i]; 215 break; 216 default: 217 return (ENXIO); 218 } 219 return 0; 220 } 221