joy.c revision 54156
1/*- 2 * Copyright (c) 1995 Jean-Marc Zucconi 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software withough specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD: head/sys/dev/joy/joy.c 54156 1999-12-05 19:51:40Z peter $ 29 * 30 */ 31 32#include "joy.h" 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/conf.h> 37#include <sys/uio.h> 38#include <sys/kernel.h> 39#include <sys/module.h> 40#include <sys/bus.h> 41#include <machine/bus.h> 42#include <machine/resource.h> 43#include <sys/rman.h> 44#include <sys/time.h> 45#include <sys/joystick.h> 46 47#include <isa/isareg.h> 48#include <isa/isavar.h> 49#include "isa_if.h" 50 51/* The game port can manage 4 buttons and 4 variable resistors (usually 2 52 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201. 53 * Getting the state of the buttons is done by reading the game port: 54 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2) 55 * to bits 0-3. 56 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0 57 * to get the value of a resistor, write the value 0xff at port and 58 * wait until the corresponding bit returns to 0. 59 */ 60 61#define joypart(d) (minor(d)&1) 62#define UNIT(d) ((minor(d)>>1)&3) 63#ifndef JOY_TIMEOUT 64#define JOY_TIMEOUT 2000 /* 2 milliseconds */ 65#endif 66 67struct joy_softc { 68 bus_space_tag_t bt; 69 bus_space_handle_t port; 70 int x_off[2], y_off[2]; 71 int timeout[2]; 72}; 73 74#define JOY_SOFTC(unit) (struct joy_softc *) \ 75 devclass_get_softc(joy_devclass,(unit)) 76 77static int joy_probe (device_t); 78static int joy_attach (device_t); 79 80#define CDEV_MAJOR 51 81static d_open_t joyopen; 82static d_close_t joyclose; 83static d_read_t joyread; 84static d_ioctl_t joyioctl; 85 86static struct cdevsw joy_cdevsw = { 87 /* open */ joyopen, 88 /* close */ joyclose, 89 /* read */ joyread, 90 /* write */ nowrite, 91 /* ioctl */ joyioctl, 92 /* poll */ nopoll, 93 /* mmap */ nommap, 94 /* strategy */ nostrategy, 95 /* name */ "joy", 96 /* maj */ CDEV_MAJOR, 97 /* dump */ nodump, 98 /* psize */ nopsize, 99 /* flags */ 0, 100 /* bmaj */ -1 101}; 102 103devclass_t joy_devclass; 104 105static struct isa_pnp_id joy_ids[] = { 106 {0x0100630e, "CSC0001 PnP Joystick"}, /* CSC0001 */ 107 {0x01100002, "ALS0110 PnP Joystick"}, /* @P@1001 */ 108 {0x01100002, "ALS0120 PnP Joystick"}, /* @P@2001 */ 109 {0x01007316, "ESS0001 PnP Joystick"}, /* ESS0001 */ 110 {0x2fb0d041, "Generic PnP Joystick"}, /* PNPb02f */ 111 {0x2200a865, "YMH0022 PnP Joystick"}, /* YMH0022 */ 112 {0} 113}; 114 115static int 116joy_probe (device_t dev) 117{ 118 if (ISA_PNP_PROBE(device_get_parent(dev), dev, joy_ids) == ENXIO) 119 return ENXIO; 120#ifdef WANT_JOYSTICK_CONNECTED 121#ifdef notyet 122 outb (dev->id_iobase, 0xff); 123 DELAY (10000); /* 10 ms delay */ 124 return (inb (dev->id_iobase) & 0x0f) != 0x0f; 125#endif 126#else 127 return 0; 128#endif 129} 130 131static int 132joy_attach (device_t dev) 133{ 134 int unit = device_get_unit(dev); 135 int rid = 0; 136 struct resource *res; 137 struct joy_softc *joy = device_get_softc(dev); 138 139 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 140 if (res == NULL) 141 return ENXIO; 142 joy->bt = rman_get_bustag(res); 143 joy->port = rman_get_bushandle(res); 144 joy->timeout[0] = joy->timeout[1] = 0; 145 make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit); 146 return 0; 147} 148 149static device_method_t joy_methods[] = { 150 DEVMETHOD(device_probe, joy_probe), 151 DEVMETHOD(device_attach, joy_attach), 152 { 0, 0 } 153}; 154 155static driver_t joy_isa_driver = { 156 "joy", 157 joy_methods, 158 sizeof (struct joy_softc) 159}; 160 161DRIVER_MODULE(joy, isa, joy_isa_driver, joy_devclass, 0, 0); 162 163static int 164joyopen(dev_t dev, int flags, int fmt, struct proc *p) 165{ 166 int i = joypart (dev); 167 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 168 169 if (joy->timeout[i]) 170 return EBUSY; 171 joy->x_off[i] = joy->y_off[i] = 0; 172 joy->timeout[i] = JOY_TIMEOUT; 173 return 0; 174} 175 176static int 177joyclose(dev_t dev, int flags, int fmt, struct proc *p) 178{ 179 int i = joypart (dev); 180 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 181 182 joy->timeout[i] = 0; 183 return 0; 184} 185 186static int 187joyread(dev_t dev, struct uio *uio, int flag) 188{ 189 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 190 bus_space_handle_t port = joy->port; 191 bus_space_tag_t bt = joy->bt; 192 struct timespec t, start, end; 193 int state = 0; 194 struct timespec x, y; 195 struct joystick c; 196#ifndef i386 197 int s; 198 199 s = splhigh(); 200#else 201 disable_intr (); 202#endif 203 bus_space_write_1 (bt, port, 0, 0xff); 204 nanotime(&start); 205 end.tv_sec = 0; 206 end.tv_nsec = joy->timeout[joypart(dev)] * 1000; 207 timespecadd(&end, &start); 208 t = start; 209 timespecclear(&x); 210 timespecclear(&y); 211 while (timespeccmp(&t, &end, <)) { 212 state = bus_space_read_1 (bt, port, 0); 213 if (joypart(dev) == 1) 214 state >>= 2; 215 nanotime(&t); 216 if (!timespecisset(&x) && !(state & 0x01)) 217 x = t; 218 if (!timespecisset(&y) && !(state & 0x02)) 219 y = t; 220 if (timespecisset(&x) && timespecisset(&y)) 221 break; 222 } 223#ifndef i386 224 splx(s); 225#else 226 enable_intr (); 227#endif 228 if (timespecisset(&x)) { 229 timespecsub(&x, &start); 230 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000; 231 } else 232 c.x = 0x80000000; 233 if (timespecisset(&y)) { 234 timespecsub(&y, &start); 235 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000; 236 } else 237 c.y = 0x80000000; 238 state >>= 4; 239 c.b1 = ~state & 1; 240 c.b2 = ~(state >> 1) & 1; 241 return uiomove ((caddr_t)&c, sizeof(struct joystick), uio); 242} 243 244static int 245joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 246{ 247 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 248 int i = joypart (dev); 249 int x; 250 251 switch (cmd) { 252 case JOY_SETTIMEOUT: 253 x = *(int *) data; 254 if (x < 1 || x > 10000) /* 10ms maximum! */ 255 return EINVAL; 256 joy->timeout[i] = x; 257 break; 258 case JOY_GETTIMEOUT: 259 *(int *) data = joy->timeout[i]; 260 break; 261 case JOY_SET_X_OFFSET: 262 joy->x_off[i] = *(int *) data; 263 break; 264 case JOY_SET_Y_OFFSET: 265 joy->y_off[i] = *(int *) data; 266 break; 267 case JOY_GET_X_OFFSET: 268 *(int *) data = joy->x_off[i]; 269 break; 270 case JOY_GET_Y_OFFSET: 271 *(int *) data = joy->y_off[i]; 272 break; 273 default: 274 return ENXIO; 275 } 276 return 0; 277} 278