joy.c revision 152249
15897Sjmz/*- 25897Sjmz * Copyright (c) 1995 Jean-Marc Zucconi 35897Sjmz * All rights reserved. 45897Sjmz * 55897Sjmz * Redistribution and use in source and binary forms, with or without 65897Sjmz * modification, are permitted provided that the following conditions 75897Sjmz * are met: 85897Sjmz * 1. Redistributions of source code must retain the above copyright 95897Sjmz * notice, this list of conditions and the following disclaimer 105897Sjmz * in this position and unchanged. 115897Sjmz * 2. Redistributions in binary form must reproduce the above copyright 125897Sjmz * notice, this list of conditions and the following disclaimer in the 135897Sjmz * documentation and/or other materials provided with the distribution. 145897Sjmz * 3. The name of the author may not be used to endorse or promote products 1597748Sschweikh * derived from this software without specific prior written permission 165897Sjmz * 175897Sjmz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 185897Sjmz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 195897Sjmz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 205897Sjmz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 215897Sjmz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 225897Sjmz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235897Sjmz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245897Sjmz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255897Sjmz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 265897Sjmz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275897Sjmz * 285897Sjmz */ 295897Sjmz 30119418Sobrien#include <sys/cdefs.h> 31119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/joy/joy.c 152249 2005-11-09 20:26:00Z jylefort $"); 32119418Sobrien 337430Sbde#include <sys/param.h> 347430Sbde#include <sys/systm.h> 3512675Sjulian#include <sys/conf.h> 3634924Sbde#include <sys/uio.h> 3754156Speter#include <sys/kernel.h> 3854156Speter#include <sys/module.h> 3954156Speter#include <sys/bus.h> 4054156Speter#include <machine/bus.h> 4154156Speter#include <machine/resource.h> 4254156Speter#include <sys/rman.h> 4354156Speter#include <sys/time.h> 4454156Speter#include <sys/joystick.h> 4587384Simp#include <dev/joy/joyvar.h> 466734Sbde 478876Srgrimes/* The game port can manage 4 buttons and 4 variable resistors (usually 2 485897Sjmz * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201. 495897Sjmz * Getting the state of the buttons is done by reading the game port: 505897Sjmz * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2) 515897Sjmz * to bits 0-3. 525897Sjmz * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0 535897Sjmz * to get the value of a resistor, write the value 0xff at port and 545897Sjmz * wait until the corresponding bit returns to 0. 555897Sjmz */ 565897Sjmz 5746570Speter#define joypart(d) (minor(d)&1) 5846570Speter#define UNIT(d) ((minor(d)>>1)&3) 595897Sjmz#ifndef JOY_TIMEOUT 605897Sjmz#define JOY_TIMEOUT 2000 /* 2 milliseconds */ 615897Sjmz#endif 625897Sjmz 6354156Speter#define JOY_SOFTC(unit) (struct joy_softc *) \ 6454156Speter devclass_get_softc(joy_devclass,(unit)) 655897Sjmz 6612675Sjulianstatic d_open_t joyopen; 6712675Sjulianstatic d_close_t joyclose; 6812675Sjulianstatic d_read_t joyread; 6912675Sjulianstatic d_ioctl_t joyioctl; 7012675Sjulian 7147625Sphkstatic struct cdevsw joy_cdevsw = { 72126080Sphk .d_version = D_VERSION, 73126080Sphk .d_flags = D_NEEDGIANT, 74111815Sphk .d_open = joyopen, 75111815Sphk .d_close = joyclose, 76111815Sphk .d_read = joyread, 77111815Sphk .d_ioctl = joyioctl, 78111815Sphk .d_name = "joy", 7947625Sphk}; 8012675Sjulian 8189086Simpdevclass_t joy_devclass; 825897Sjmz 8387384Simpint 8487384Simpjoy_probe(device_t dev) 855897Sjmz{ 865897Sjmz#ifdef WANT_JOYSTICK_CONNECTED 8754156Speter#ifdef notyet 8887384Simp outb(dev->id_iobase, 0xff); 8987384Simp DELAY(10000); /* 10 ms delay */ 9087384Simp return (inb(dev->id_iobase) & 0x0f) != 0x0f; 9187384Simp#else 9287384Simp return (0); 9354156Speter#endif 945897Sjmz#else 9587384Simp return (0); 965897Sjmz#endif 975897Sjmz} 985897Sjmz 9987384Simpint 10087384Simpjoy_attach(device_t dev) 1015897Sjmz{ 10287384Simp int unit = device_get_unit(dev); 10387384Simp struct joy_softc *joy = device_get_softc(dev); 1045897Sjmz 10587384Simp joy->rid = 0; 106127135Snjl joy->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &joy->rid, 107152249Sjylefort RF_ACTIVE|RF_SHAREABLE); 10887384Simp if (joy->res == NULL) 10987384Simp return ENXIO; 11087384Simp joy->bt = rman_get_bustag(joy->res); 11187384Simp joy->port = rman_get_bushandle(joy->res); 11287384Simp joy->timeout[0] = joy->timeout[1] = 0; 113152249Sjylefort joy->d = make_dev(&joy_cdevsw, unit, 0, 0, 0600, "joy%d", unit); 11487384Simp return (0); 1155897Sjmz} 1165897Sjmz 11787384Simpint 11887384Simpjoy_detach(device_t dev) 11987384Simp{ 12087384Simp struct joy_softc *joy = device_get_softc(dev); 12154156Speter 12287384Simp if (joy->res != NULL) 12387384Simp bus_release_resource(dev, SYS_RES_IOPORT, joy->rid, joy->res); 12487384Simp if (joy->d) 12587384Simp destroy_dev(joy->d); 12687384Simp return (0); 12787384Simp} 12854156Speter 12954156Speter 13054156Speterstatic int 131130585Sphkjoyopen(struct cdev *dev, int flags, int fmt, struct thread *td) 1325897Sjmz{ 13387384Simp int i = joypart (dev); 13487384Simp struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 1355897Sjmz 13687384Simp if (joy->timeout[i]) 13787384Simp return (EBUSY); 13887384Simp joy->x_off[i] = joy->y_off[i] = 0; 13987384Simp joy->timeout[i] = JOY_TIMEOUT; 14087384Simp return (0); 1415897Sjmz} 14254156Speter 14354156Speterstatic int 144130585Sphkjoyclose(struct cdev *dev, int flags, int fmt, struct thread *td) 1455897Sjmz{ 14687384Simp int i = joypart (dev); 14787384Simp struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 1485897Sjmz 14987384Simp joy->timeout[i] = 0; 15087384Simp return (0); 1515897Sjmz} 1525897Sjmz 15354156Speterstatic int 154130585Sphkjoyread(struct cdev *dev, struct uio *uio, int flag) 1555897Sjmz{ 15687384Simp struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 15787384Simp bus_space_handle_t port = joy->port; 15887384Simp bus_space_tag_t bt = joy->bt; 15987384Simp struct timespec t, start, end; 16087384Simp int state = 0; 16187384Simp struct timespec x, y; 16287384Simp struct joystick c; 16397554Salfred#ifndef __i386__ 16487384Simp int s; 1658876Srgrimes 16687384Simp s = splhigh(); 16754156Speter#else 16887384Simp disable_intr (); 16954156Speter#endif 170152249Sjylefort nanotime(&t); 171152249Sjylefort end.tv_sec = 0; 172152249Sjylefort end.tv_nsec = joy->timeout[joypart(dev)] * 1000; 173152249Sjylefort timespecadd(&end, &t); 174152249Sjylefort for (; timespeccmp(&t, &end, <) && (bus_space_read_1(bt, port, 0) & 0x0f); nanotime(&t)) 175152249Sjylefort ; /* nothing */ 17687384Simp bus_space_write_1 (bt, port, 0, 0xff); 17787384Simp nanotime(&start); 17887384Simp end.tv_sec = 0; 17987384Simp end.tv_nsec = joy->timeout[joypart(dev)] * 1000; 18087384Simp timespecadd(&end, &start); 18187384Simp t = start; 18287384Simp timespecclear(&x); 18387384Simp timespecclear(&y); 18487384Simp while (timespeccmp(&t, &end, <)) { 18587384Simp state = bus_space_read_1 (bt, port, 0); 18687384Simp if (joypart(dev) == 1) 18787384Simp state >>= 2; 18887384Simp nanotime(&t); 18987384Simp if (!timespecisset(&x) && !(state & 0x01)) 19087384Simp x = t; 19187384Simp if (!timespecisset(&y) && !(state & 0x02)) 19287384Simp y = t; 19387384Simp if (timespecisset(&x) && timespecisset(&y)) 19487384Simp break; 19587384Simp } 19697554Salfred#ifndef __i386__ 19787384Simp splx(s); 19854156Speter#else 19987384Simp enable_intr (); 20054156Speter#endif 20187384Simp if (timespecisset(&x)) { 20287384Simp timespecsub(&x, &start); 20387384Simp c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000; 20487384Simp } else 20587384Simp c.x = 0x80000000; 20687384Simp if (timespecisset(&y)) { 20787384Simp timespecsub(&y, &start); 20887384Simp c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000; 20987384Simp } else 21087384Simp c.y = 0x80000000; 21187384Simp state >>= 4; 21287384Simp c.b1 = ~state & 1; 21387384Simp c.b2 = ~(state >> 1) & 1; 21487384Simp return (uiomove((caddr_t)&c, sizeof(struct joystick), uio)); 2155897Sjmz} 21612675Sjulian 21754156Speterstatic int 218130585Sphkjoyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 2195897Sjmz{ 22087384Simp struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 22187384Simp int i = joypart (dev); 22287384Simp int x; 2235897Sjmz 22487384Simp switch (cmd) { 22587384Simp case JOY_SETTIMEOUT: 22687384Simp x = *(int *) data; 22787384Simp if (x < 1 || x > 10000) /* 10ms maximum! */ 22887384Simp return EINVAL; 22987384Simp joy->timeout[i] = x; 23087384Simp break; 23187384Simp case JOY_GETTIMEOUT: 23287384Simp *(int *) data = joy->timeout[i]; 23387384Simp break; 23487384Simp case JOY_SET_X_OFFSET: 23587384Simp joy->x_off[i] = *(int *) data; 23687384Simp break; 23787384Simp case JOY_SET_Y_OFFSET: 23887384Simp joy->y_off[i] = *(int *) data; 23987384Simp break; 24087384Simp case JOY_GET_X_OFFSET: 24187384Simp *(int *) data = joy->x_off[i]; 24287384Simp break; 24387384Simp case JOY_GET_Y_OFFSET: 24487384Simp *(int *) data = joy->y_off[i]; 24587384Simp break; 24687384Simp default: 24787384Simp return (ENOTTY); 24887384Simp } 24987384Simp return (0); 2505897Sjmz} 251