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