joy.c (83366) | joy.c (87384) |
---|---|
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 --- 11 unchanged lines hidden (view full) --- 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 * | 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 --- 11 unchanged lines hidden (view full) --- 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 83366 2001-09-12 08:38:13Z julian $ | 28 * $FreeBSD: head/sys/dev/joy/joy.c 87384 2001-12-05 09:08:23Z imp $ |
29 */ 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/conf.h> 34#include <sys/uio.h> 35#include <sys/kernel.h> 36#include <sys/module.h> 37#include <sys/bus.h> 38#include <machine/bus.h> 39#include <machine/resource.h> 40#include <sys/rman.h> 41#include <sys/time.h> 42#include <sys/joystick.h> | 29 */ 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/conf.h> 34#include <sys/uio.h> 35#include <sys/kernel.h> 36#include <sys/module.h> 37#include <sys/bus.h> 38#include <machine/bus.h> 39#include <machine/resource.h> 40#include <sys/rman.h> 41#include <sys/time.h> 42#include <sys/joystick.h> |
43#include <dev/joy/joyvar.h> |
|
43 | 44 |
44#include <isa/isavar.h> 45#include "isa_if.h" 46 | |
47/* The game port can manage 4 buttons and 4 variable resistors (usually 2 48 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201. 49 * Getting the state of the buttons is done by reading the game port: 50 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2) 51 * to bits 0-3. 52 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0 53 * to get the value of a resistor, write the value 0xff at port and 54 * wait until the corresponding bit returns to 0. 55 */ 56 57#define joypart(d) (minor(d)&1) 58#define UNIT(d) ((minor(d)>>1)&3) 59#ifndef JOY_TIMEOUT 60#define JOY_TIMEOUT 2000 /* 2 milliseconds */ 61#endif 62 | 45/* The game port can manage 4 buttons and 4 variable resistors (usually 2 46 * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201. 47 * Getting the state of the buttons is done by reading the game port: 48 * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2) 49 * to bits 0-3. 50 * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0 51 * to get the value of a resistor, write the value 0xff at port and 52 * wait until the corresponding bit returns to 0. 53 */ 54 55#define joypart(d) (minor(d)&1) 56#define UNIT(d) ((minor(d)>>1)&3) 57#ifndef JOY_TIMEOUT 58#define JOY_TIMEOUT 2000 /* 2 milliseconds */ 59#endif 60 |
63struct joy_softc { 64 bus_space_tag_t bt; 65 bus_space_handle_t port; 66 int x_off[2], y_off[2]; 67 int timeout[2]; 68}; 69 | |
70#define JOY_SOFTC(unit) (struct joy_softc *) \ 71 devclass_get_softc(joy_devclass,(unit)) 72 | 61#define JOY_SOFTC(unit) (struct joy_softc *) \ 62 devclass_get_softc(joy_devclass,(unit)) 63 |
73static int joy_probe (device_t); 74static int joy_attach (device_t); 75 | |
76#define CDEV_MAJOR 51 77static d_open_t joyopen; 78static d_close_t joyclose; 79static d_read_t joyread; 80static d_ioctl_t joyioctl; 81 82static struct cdevsw joy_cdevsw = { 83 /* open */ joyopen, --- 8 unchanged lines hidden (view full) --- 92 /* maj */ CDEV_MAJOR, 93 /* dump */ nodump, 94 /* psize */ nopsize, 95 /* flags */ 0, 96}; 97 98devclass_t joy_devclass; 99 | 64#define CDEV_MAJOR 51 65static d_open_t joyopen; 66static d_close_t joyclose; 67static d_read_t joyread; 68static d_ioctl_t joyioctl; 69 70static struct cdevsw joy_cdevsw = { 71 /* open */ joyopen, --- 8 unchanged lines hidden (view full) --- 80 /* maj */ CDEV_MAJOR, 81 /* dump */ nodump, 82 /* psize */ nopsize, 83 /* flags */ 0, 84}; 85 86devclass_t joy_devclass; 87 |
100static struct isa_pnp_id joy_ids[] = { 101 {0x0100630e, "CSC0001 PnP Joystick"}, /* CSC0001 */ 102 {0x0101630e, "CSC0101 PnP Joystick"}, /* CSC0101 */ 103 {0x01100002, "ALS0110 PnP Joystick"}, /* @P@1001 */ 104 {0x01200002, "ALS0120 PnP Joystick"}, /* @P@2001 */ 105 {0x01007316, "ESS0001 PnP Joystick"}, /* ESS0001 */ 106 {0x2fb0d041, "Generic PnP Joystick"}, /* PNPb02f */ 107 {0x2200a865, "YMH0022 PnP Joystick"}, /* YMH0022 */ 108 {0x82719304, NULL}, /* ADS7182 */ 109 {0} 110}; 111 112static int 113joy_probe (device_t dev) | 88int 89joy_probe(device_t dev) |
114{ | 90{ |
115 if (ISA_PNP_PROBE(device_get_parent(dev), dev, joy_ids) == ENXIO) 116 return ENXIO; | |
117#ifdef WANT_JOYSTICK_CONNECTED 118#ifdef notyet | 91#ifdef WANT_JOYSTICK_CONNECTED 92#ifdef notyet |
119 outb (dev->id_iobase, 0xff); 120 DELAY (10000); /* 10 ms delay */ 121 return (inb (dev->id_iobase) & 0x0f) != 0x0f; | 93 outb(dev->id_iobase, 0xff); 94 DELAY(10000); /* 10 ms delay */ 95 return (inb(dev->id_iobase) & 0x0f) != 0x0f; 96#else 97 return (0); |
122#endif 123#else | 98#endif 99#else |
124 return 0; | 100 return (0); |
125#endif 126} 127 | 101#endif 102} 103 |
128static int 129joy_attach (device_t dev) | 104int 105joy_attach(device_t dev) |
130{ | 106{ |
131 int unit = device_get_unit(dev); 132 int rid = 0; 133 struct resource *res; 134 struct joy_softc *joy = device_get_softc(dev); | 107 int unit = device_get_unit(dev); 108 struct joy_softc *joy = device_get_softc(dev); |
135 | 109 |
136 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 137 if (res == NULL) 138 return ENXIO; 139 joy->bt = rman_get_bustag(res); 140 joy->port = rman_get_bushandle(res); 141 joy->timeout[0] = joy->timeout[1] = 0; 142 make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit); 143 return 0; | 110 joy->rid = 0; 111 joy->res = bus_alloc_resource(dev, SYS_RES_IOPORT, &joy->rid, 0, ~0, 1, 112 RF_ACTIVE); 113 if (joy->res == NULL) 114 return ENXIO; 115 joy->bt = rman_get_bustag(joy->res); 116 joy->port = rman_get_bushandle(joy->res); 117 joy->timeout[0] = joy->timeout[1] = 0; 118 joy->d = make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit); 119 return (0); |
144} 145 | 120} 121 |
146static device_method_t joy_methods[] = { 147 DEVMETHOD(device_probe, joy_probe), 148 DEVMETHOD(device_attach, joy_attach), 149 { 0, 0 } 150}; | 122int 123joy_detach(device_t dev) 124{ 125 struct joy_softc *joy = device_get_softc(dev); |
151 | 126 |
152static driver_t joy_isa_driver = { 153 "joy", 154 joy_methods, 155 sizeof (struct joy_softc) 156}; | 127 if (joy->res != NULL) 128 bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res); 129 if (joy->d) 130 destroy_dev(joy->d); 131 return (0); 132} |
157 | 133 |
158DRIVER_MODULE(joy, isa, joy_isa_driver, joy_devclass, 0, 0); 159DRIVER_MODULE(joy, acpi, joy_isa_driver, joy_devclass, 0, 0); | |
160 161static int 162joyopen(dev_t dev, int flags, int fmt, struct thread *td) 163{ | 134 135static int 136joyopen(dev_t dev, int flags, int fmt, struct thread *td) 137{ |
164 int i = joypart (dev); 165 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); | 138 int i = joypart (dev); 139 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); |
166 | 140 |
167 if (joy->timeout[i]) 168 return EBUSY; 169 joy->x_off[i] = joy->y_off[i] = 0; 170 joy->timeout[i] = JOY_TIMEOUT; 171 return 0; | 141 if (joy->timeout[i]) 142 return (EBUSY); 143 joy->x_off[i] = joy->y_off[i] = 0; 144 joy->timeout[i] = JOY_TIMEOUT; 145 return (0); |
172} 173 174static int 175joyclose(dev_t dev, int flags, int fmt, struct thread *td) 176{ | 146} 147 148static int 149joyclose(dev_t dev, int flags, int fmt, struct thread *td) 150{ |
177 int i = joypart (dev); 178 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); | 151 int i = joypart (dev); 152 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); |
179 | 153 |
180 joy->timeout[i] = 0; 181 return 0; | 154 joy->timeout[i] = 0; 155 return (0); |
182} 183 184static int 185joyread(dev_t dev, struct uio *uio, int flag) 186{ | 156} 157 158static int 159joyread(dev_t dev, struct uio *uio, int flag) 160{ |
187 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 188 bus_space_handle_t port = joy->port; 189 bus_space_tag_t bt = joy->bt; 190 struct timespec t, start, end; 191 int state = 0; 192 struct timespec x, y; 193 struct joystick c; | 161 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 162 bus_space_handle_t port = joy->port; 163 bus_space_tag_t bt = joy->bt; 164 struct timespec t, start, end; 165 int state = 0; 166 struct timespec x, y; 167 struct joystick c; |
194#ifndef i386 | 168#ifndef i386 |
195 int s; | 169 int s; |
196 | 170 |
197 s = splhigh(); | 171 s = splhigh(); |
198#else | 172#else |
199 disable_intr (); | 173 disable_intr (); |
200#endif | 174#endif |
201 bus_space_write_1 (bt, port, 0, 0xff); 202 nanotime(&start); 203 end.tv_sec = 0; 204 end.tv_nsec = joy->timeout[joypart(dev)] * 1000; 205 timespecadd(&end, &start); 206 t = start; 207 timespecclear(&x); 208 timespecclear(&y); 209 while (timespeccmp(&t, &end, <)) { 210 state = bus_space_read_1 (bt, port, 0); 211 if (joypart(dev) == 1) 212 state >>= 2; 213 nanotime(&t); 214 if (!timespecisset(&x) && !(state & 0x01)) 215 x = t; 216 if (!timespecisset(&y) && !(state & 0x02)) 217 y = t; 218 if (timespecisset(&x) && timespecisset(&y)) 219 break; 220 } | 175 bus_space_write_1 (bt, port, 0, 0xff); 176 nanotime(&start); 177 end.tv_sec = 0; 178 end.tv_nsec = joy->timeout[joypart(dev)] * 1000; 179 timespecadd(&end, &start); 180 t = start; 181 timespecclear(&x); 182 timespecclear(&y); 183 while (timespeccmp(&t, &end, <)) { 184 state = bus_space_read_1 (bt, port, 0); 185 if (joypart(dev) == 1) 186 state >>= 2; 187 nanotime(&t); 188 if (!timespecisset(&x) && !(state & 0x01)) 189 x = t; 190 if (!timespecisset(&y) && !(state & 0x02)) 191 y = t; 192 if (timespecisset(&x) && timespecisset(&y)) 193 break; 194 } |
221#ifndef i386 | 195#ifndef i386 |
222 splx(s); | 196 splx(s); |
223#else | 197#else |
224 enable_intr (); | 198 enable_intr (); |
225#endif | 199#endif |
226 if (timespecisset(&x)) { 227 timespecsub(&x, &start); 228 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000; 229 } else 230 c.x = 0x80000000; 231 if (timespecisset(&y)) { 232 timespecsub(&y, &start); 233 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000; 234 } else 235 c.y = 0x80000000; 236 state >>= 4; 237 c.b1 = ~state & 1; 238 c.b2 = ~(state >> 1) & 1; 239 return uiomove ((caddr_t)&c, sizeof(struct joystick), uio); | 200 if (timespecisset(&x)) { 201 timespecsub(&x, &start); 202 c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000; 203 } else 204 c.x = 0x80000000; 205 if (timespecisset(&y)) { 206 timespecsub(&y, &start); 207 c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000; 208 } else 209 c.y = 0x80000000; 210 state >>= 4; 211 c.b1 = ~state & 1; 212 c.b2 = ~(state >> 1) & 1; 213 return (uiomove((caddr_t)&c, sizeof(struct joystick), uio)); |
240} 241 242static int 243joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 244{ | 214} 215 216static int 217joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 218{ |
245 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 246 int i = joypart (dev); 247 int x; | 219 struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 220 int i = joypart (dev); 221 int x; |
248 | 222 |
249 switch (cmd) { 250 case JOY_SETTIMEOUT: 251 x = *(int *) data; 252 if (x < 1 || x > 10000) /* 10ms maximum! */ 253 return EINVAL; 254 joy->timeout[i] = x; 255 break; 256 case JOY_GETTIMEOUT: 257 *(int *) data = joy->timeout[i]; 258 break; 259 case JOY_SET_X_OFFSET: 260 joy->x_off[i] = *(int *) data; 261 break; 262 case JOY_SET_Y_OFFSET: 263 joy->y_off[i] = *(int *) data; 264 break; 265 case JOY_GET_X_OFFSET: 266 *(int *) data = joy->x_off[i]; 267 break; 268 case JOY_GET_Y_OFFSET: 269 *(int *) data = joy->y_off[i]; 270 break; 271 default: 272 return ENXIO; 273 } 274 return 0; | 223 switch (cmd) { 224 case JOY_SETTIMEOUT: 225 x = *(int *) data; 226 if (x < 1 || x > 10000) /* 10ms maximum! */ 227 return EINVAL; 228 joy->timeout[i] = x; 229 break; 230 case JOY_GETTIMEOUT: 231 *(int *) data = joy->timeout[i]; 232 break; 233 case JOY_SET_X_OFFSET: 234 joy->x_off[i] = *(int *) data; 235 break; 236 case JOY_SET_Y_OFFSET: 237 joy->y_off[i] = *(int *) data; 238 break; 239 case JOY_GET_X_OFFSET: 240 *(int *) data = joy->x_off[i]; 241 break; 242 case JOY_GET_Y_OFFSET: 243 *(int *) data = joy->y_off[i]; 244 break; 245 default: 246 return (ENOTTY); 247 } 248 return (0); |
275} | 249} |