joy.c revision 54156
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 155897Sjmz * derived from this software withough 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 * 2851658Sphk * $FreeBSD: head/sys/dev/joy/joy.c 54156 1999-12-05 19:51:40Z peter $ 2951658Sphk * 305897Sjmz */ 315897Sjmz 3252129Speter#include "joy.h" 3352129Speter 347430Sbde#include <sys/param.h> 357430Sbde#include <sys/systm.h> 3612675Sjulian#include <sys/conf.h> 3734924Sbde#include <sys/uio.h> 3854156Speter#include <sys/kernel.h> 3954156Speter#include <sys/module.h> 4054156Speter#include <sys/bus.h> 4154156Speter#include <machine/bus.h> 4254156Speter#include <machine/resource.h> 4354156Speter#include <sys/rman.h> 4454156Speter#include <sys/time.h> 4554156Speter#include <sys/joystick.h> 466734Sbde 4754156Speter#include <isa/isareg.h> 4854156Speter#include <isa/isavar.h> 4954156Speter#include "isa_if.h" 506734Sbde 518876Srgrimes/* The game port can manage 4 buttons and 4 variable resistors (usually 2 525897Sjmz * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201. 535897Sjmz * Getting the state of the buttons is done by reading the game port: 545897Sjmz * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2) 555897Sjmz * to bits 0-3. 565897Sjmz * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0 575897Sjmz * to get the value of a resistor, write the value 0xff at port and 585897Sjmz * wait until the corresponding bit returns to 0. 595897Sjmz */ 605897Sjmz 6146570Speter#define joypart(d) (minor(d)&1) 6246570Speter#define UNIT(d) ((minor(d)>>1)&3) 635897Sjmz#ifndef JOY_TIMEOUT 645897Sjmz#define JOY_TIMEOUT 2000 /* 2 milliseconds */ 655897Sjmz#endif 665897Sjmz 6754156Speterstruct joy_softc { 6854156Speter bus_space_tag_t bt; 6954156Speter bus_space_handle_t port; 705897Sjmz int x_off[2], y_off[2]; 715897Sjmz int timeout[2]; 7254156Speter}; 735897Sjmz 7454156Speter#define JOY_SOFTC(unit) (struct joy_softc *) \ 7554156Speter devclass_get_softc(joy_devclass,(unit)) 765897Sjmz 7754156Speterstatic int joy_probe (device_t); 7854156Speterstatic int joy_attach (device_t); 795897Sjmz 8012675Sjulian#define CDEV_MAJOR 51 8112675Sjulianstatic d_open_t joyopen; 8212675Sjulianstatic d_close_t joyclose; 8312675Sjulianstatic d_read_t joyread; 8412675Sjulianstatic d_ioctl_t joyioctl; 8512675Sjulian 8647625Sphkstatic struct cdevsw joy_cdevsw = { 8747625Sphk /* open */ joyopen, 8847625Sphk /* close */ joyclose, 8947625Sphk /* read */ joyread, 9047625Sphk /* write */ nowrite, 9147625Sphk /* ioctl */ joyioctl, 9247625Sphk /* poll */ nopoll, 9347625Sphk /* mmap */ nommap, 9447625Sphk /* strategy */ nostrategy, 9547625Sphk /* name */ "joy", 9647625Sphk /* maj */ CDEV_MAJOR, 9747625Sphk /* dump */ nodump, 9847625Sphk /* psize */ nopsize, 9947625Sphk /* flags */ 0, 10047625Sphk /* bmaj */ -1 10147625Sphk}; 10212675Sjulian 10354156Speterdevclass_t joy_devclass; 1045897Sjmz 10554156Speterstatic struct isa_pnp_id joy_ids[] = { 10654156Speter {0x0100630e, "CSC0001 PnP Joystick"}, /* CSC0001 */ 10754156Speter {0x01100002, "ALS0110 PnP Joystick"}, /* @P@1001 */ 10854156Speter {0x01100002, "ALS0120 PnP Joystick"}, /* @P@2001 */ 10954156Speter {0x01007316, "ESS0001 PnP Joystick"}, /* ESS0001 */ 11054156Speter {0x2fb0d041, "Generic PnP Joystick"}, /* PNPb02f */ 11154156Speter {0x2200a865, "YMH0022 PnP Joystick"}, /* YMH0022 */ 11254156Speter {0} 11354156Speter}; 1145897Sjmz 11512724Sphkstatic int 11654156Speterjoy_probe (device_t dev) 1175897Sjmz{ 11854156Speter if (ISA_PNP_PROBE(device_get_parent(dev), dev, joy_ids) == ENXIO) 11954156Speter return ENXIO; 1205897Sjmz#ifdef WANT_JOYSTICK_CONNECTED 12154156Speter#ifdef notyet 1225897Sjmz outb (dev->id_iobase, 0xff); 1235897Sjmz DELAY (10000); /* 10 ms delay */ 1245897Sjmz return (inb (dev->id_iobase) & 0x0f) != 0x0f; 12554156Speter#endif 1265897Sjmz#else 12754156Speter return 0; 1285897Sjmz#endif 1295897Sjmz} 1305897Sjmz 13112724Sphkstatic int 13254156Speterjoy_attach (device_t dev) 1335897Sjmz{ 13454156Speter int unit = device_get_unit(dev); 13554156Speter int rid = 0; 13654156Speter struct resource *res; 13754156Speter struct joy_softc *joy = device_get_softc(dev); 1385897Sjmz 13954156Speter res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE); 14054156Speter if (res == NULL) 14154156Speter return ENXIO; 14254156Speter joy->bt = rman_get_bustag(res); 14354156Speter joy->port = rman_get_bushandle(res); 14454156Speter joy->timeout[0] = joy->timeout[1] = 0; 14550254Sphk make_dev(&joy_cdevsw, 0, 0, 0, 0600, "joy%d", unit); 14654156Speter return 0; 1475897Sjmz} 1485897Sjmz 14954156Speterstatic device_method_t joy_methods[] = { 15054156Speter DEVMETHOD(device_probe, joy_probe), 15154156Speter DEVMETHOD(device_attach, joy_attach), 15254156Speter { 0, 0 } 15354156Speter}; 15454156Speter 15554156Speterstatic driver_t joy_isa_driver = { 15654156Speter "joy", 15754156Speter joy_methods, 15854156Speter sizeof (struct joy_softc) 15954156Speter}; 16054156Speter 16154156SpeterDRIVER_MODULE(joy, isa, joy_isa_driver, joy_devclass, 0, 0); 16254156Speter 16354156Speterstatic int 16454156Speterjoyopen(dev_t dev, int flags, int fmt, struct proc *p) 1655897Sjmz{ 1665897Sjmz int i = joypart (dev); 16754156Speter struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 1685897Sjmz 16954156Speter if (joy->timeout[i]) 1705897Sjmz return EBUSY; 17154156Speter joy->x_off[i] = joy->y_off[i] = 0; 17254156Speter joy->timeout[i] = JOY_TIMEOUT; 1735897Sjmz return 0; 1745897Sjmz} 17554156Speter 17654156Speterstatic int 17754156Speterjoyclose(dev_t dev, int flags, int fmt, struct proc *p) 1785897Sjmz{ 1795897Sjmz int i = joypart (dev); 18054156Speter struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 1815897Sjmz 18254156Speter joy->timeout[i] = 0; 1835897Sjmz return 0; 1845897Sjmz} 1855897Sjmz 18654156Speterstatic int 18754156Speterjoyread(dev_t dev, struct uio *uio, int flag) 1885897Sjmz{ 18954156Speter struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 19054156Speter bus_space_handle_t port = joy->port; 19154156Speter bus_space_tag_t bt = joy->bt; 19254156Speter struct timespec t, start, end; 19354156Speter int state = 0; 19454156Speter struct timespec x, y; 1956644Sjmz struct joystick c; 19654156Speter#ifndef i386 19754156Speter int s; 1988876Srgrimes 19954156Speter s = splhigh(); 20054156Speter#else 2015897Sjmz disable_intr (); 20254156Speter#endif 20354156Speter bus_space_write_1 (bt, port, 0, 0xff); 20454156Speter nanotime(&start); 20554156Speter end.tv_sec = 0; 20654156Speter end.tv_nsec = joy->timeout[joypart(dev)] * 1000; 20754156Speter timespecadd(&end, &start); 20854156Speter t = start; 20954156Speter timespecclear(&x); 21054156Speter timespecclear(&y); 21154156Speter while (timespeccmp(&t, &end, <)) { 21254156Speter state = bus_space_read_1 (bt, port, 0); 2138876Srgrimes if (joypart(dev) == 1) 2145897Sjmz state >>= 2; 21554156Speter nanotime(&t); 21654156Speter if (!timespecisset(&x) && !(state & 0x01)) 21754156Speter x = t; 21854156Speter if (!timespecisset(&y) && !(state & 0x02)) 21954156Speter y = t; 22054156Speter if (timespecisset(&x) && timespecisset(&y)) 2215897Sjmz break; 2225897Sjmz } 22354156Speter#ifndef i386 22454156Speter splx(s); 22554156Speter#else 2265897Sjmz enable_intr (); 22754156Speter#endif 22854156Speter if (timespecisset(&x)) { 22954156Speter timespecsub(&x, &start); 23054156Speter c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000; 23154156Speter } else 23254156Speter c.x = 0x80000000; 23354156Speter if (timespecisset(&y)) { 23454156Speter timespecsub(&y, &start); 23554156Speter c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000; 23654156Speter } else 23754156Speter c.y = 0x80000000; 2385897Sjmz state >>= 4; 2396644Sjmz c.b1 = ~state & 1; 2406644Sjmz c.b2 = ~(state >> 1) & 1; 2417430Sbde return uiomove ((caddr_t)&c, sizeof(struct joystick), uio); 2425897Sjmz} 24312675Sjulian 24454156Speterstatic int 24554156Speterjoyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 2465897Sjmz{ 24754156Speter struct joy_softc *joy = JOY_SOFTC(UNIT(dev)); 2485897Sjmz int i = joypart (dev); 2495897Sjmz int x; 2505897Sjmz 2515897Sjmz switch (cmd) { 2525897Sjmz case JOY_SETTIMEOUT: 2535897Sjmz x = *(int *) data; 2545897Sjmz if (x < 1 || x > 10000) /* 10ms maximum! */ 2555897Sjmz return EINVAL; 25654156Speter joy->timeout[i] = x; 2575897Sjmz break; 2585897Sjmz case JOY_GETTIMEOUT: 25954156Speter *(int *) data = joy->timeout[i]; 2605897Sjmz break; 2615897Sjmz case JOY_SET_X_OFFSET: 26254156Speter joy->x_off[i] = *(int *) data; 2635897Sjmz break; 2645897Sjmz case JOY_SET_Y_OFFSET: 26554156Speter joy->y_off[i] = *(int *) data; 2665897Sjmz break; 2675897Sjmz case JOY_GET_X_OFFSET: 26854156Speter *(int *) data = joy->x_off[i]; 2695897Sjmz break; 2705897Sjmz case JOY_GET_Y_OFFSET: 27154156Speter *(int *) data = joy->y_off[i]; 2725897Sjmz break; 2735897Sjmz default: 2745897Sjmz return ENXIO; 2755897Sjmz } 2765897Sjmz return 0; 2775897Sjmz} 278