joy.c revision 10644
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 * 285897Sjmz */ 295897Sjmz#include "joy.h" 305897Sjmz 315897Sjmz#if NJOY > 0 325897Sjmz 335897Sjmz#include <errno.h> 346734Sbde 357430Sbde#include <sys/param.h> 367430Sbde#include <sys/systm.h> 376734Sbde 386734Sbde#include <machine/joystick.h> 396734Sbde 405897Sjmz#include <i386/isa/isa.h> 416734Sbde#include <i386/isa/isa_device.h> 425897Sjmz#include <i386/isa/timerreg.h> 435897Sjmz 448876Srgrimes/* The game port can manage 4 buttons and 4 variable resistors (usually 2 455897Sjmz * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201. 465897Sjmz * Getting the state of the buttons is done by reading the game port: 475897Sjmz * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2) 485897Sjmz * to bits 0-3. 495897Sjmz * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0 505897Sjmz * to get the value of a resistor, write the value 0xff at port and 515897Sjmz * wait until the corresponding bit returns to 0. 525897Sjmz */ 535897Sjmz 545897Sjmz 558876Srgrimes/* the formulae below only work if u is ``not too large''. See also 565993Sjmz * the discussion in microtime.s */ 575998Sjmz#define usec2ticks(u) (((u) * 19549)>>14) 585998Sjmz#define ticks2usec(u) (((u) * 3433)>>12) 595897Sjmz 605897Sjmz 615897Sjmz#define joypart(d) minor(d)&1 625897Sjmz#define UNIT(d) minor(d)>>1&3 635897Sjmz#ifndef JOY_TIMEOUT 645897Sjmz#define JOY_TIMEOUT 2000 /* 2 milliseconds */ 655897Sjmz#endif 665897Sjmz 675897Sjmzstatic struct { 685897Sjmz int port; 695897Sjmz int x_off[2], y_off[2]; 705897Sjmz int timeout[2]; 715897Sjmz} joy[NJOY]; 725897Sjmz 735897Sjmz 745993Sjmzextern int timer0_max_count; 755897Sjmz 765897Sjmzint joyprobe (struct isa_device *), joyattach (struct isa_device *); 775897Sjmz 785897Sjmzstruct isa_driver joydriver = {joyprobe, joyattach, "joy"}; 795897Sjmz 805897Sjmzstatic int get_tick (); 815897Sjmz 825897Sjmz 835897Sjmzint 845897Sjmzjoyprobe (struct isa_device *dev) 855897Sjmz{ 865897Sjmz#ifdef WANT_JOYSTICK_CONNECTED 875897Sjmz outb (dev->id_iobase, 0xff); 885897Sjmz DELAY (10000); /* 10 ms delay */ 895897Sjmz return (inb (dev->id_iobase) & 0x0f) != 0x0f; 905897Sjmz#else 915897Sjmz return 1; 925897Sjmz#endif 935897Sjmz} 945897Sjmz 955897Sjmzint 965897Sjmzjoyattach (struct isa_device *dev) 975897Sjmz{ 985897Sjmz joy[dev->id_unit].port = dev->id_iobase; 995897Sjmz joy[dev->id_unit].timeout[0] = joy[dev->id_unit].timeout[1] = 0; 1005897Sjmz printf("joy%d: joystick\n", dev->id_unit); 1015897Sjmz 1025897Sjmz return 1; 1035897Sjmz} 1045897Sjmz 1055897Sjmzint 10610644Sbdejoyopen (dev_t dev, int flags, int fmt, struct proc *p) 1075897Sjmz{ 1085897Sjmz int unit = UNIT (dev); 1095897Sjmz int i = joypart (dev); 1105897Sjmz 1115897Sjmz if (joy[unit].timeout[i]) 1125897Sjmz return EBUSY; 1135897Sjmz joy[unit].x_off[i] = joy[unit].y_off[i] = 0; 1145897Sjmz joy[unit].timeout[i] = JOY_TIMEOUT; 1155897Sjmz return 0; 1165897Sjmz} 1178876Srgrimesint 11810644Sbdejoyclose (dev_t dev, int flags, int fmt, struct proc *p) 1195897Sjmz{ 1205897Sjmz int unit = UNIT (dev); 1215897Sjmz int i = joypart (dev); 1225897Sjmz 1235897Sjmz joy[unit].timeout[i] = 0; 1245897Sjmz return 0; 1255897Sjmz} 1265897Sjmz 1275897Sjmzint 1285897Sjmzjoyread (dev_t dev, struct uio *uio, int flag) 1295897Sjmz{ 1305897Sjmz int unit = UNIT(dev); 1315897Sjmz int port = joy[unit].port; 1325897Sjmz int i, t0, t1; 1335897Sjmz int state = 0, x = 0, y = 0; 1346644Sjmz struct joystick c; 1358876Srgrimes 1365897Sjmz disable_intr (); 1375897Sjmz outb (port, 0xff); 1385897Sjmz t0 = get_tick (); 1395897Sjmz t1 = t0; 1405897Sjmz i = usec2ticks(joy[unit].timeout[joypart(dev)]); 1415897Sjmz while (t0-t1 < i) { 1425897Sjmz state = inb (port); 1438876Srgrimes if (joypart(dev) == 1) 1445897Sjmz state >>= 2; 1455897Sjmz t1 = get_tick (); 1465897Sjmz if (t1 > t0) 1475993Sjmz t1 -= timer0_max_count; 1485897Sjmz if (!x && !(state & 0x01)) 1495897Sjmz x = t1; 1505897Sjmz if (!y && !(state & 0x02)) 1515897Sjmz y = t1; 1528876Srgrimes if (x && y) 1535897Sjmz break; 1545897Sjmz } 1555897Sjmz enable_intr (); 1566644Sjmz c.x = x ? joy[unit].x_off[joypart(dev)] + ticks2usec(t0-x) : 0x80000000; 1576644Sjmz c.y = y ? joy[unit].y_off[joypart(dev)] + ticks2usec(t0-y) : 0x80000000; 1585897Sjmz state >>= 4; 1596644Sjmz c.b1 = ~state & 1; 1606644Sjmz c.b2 = ~(state >> 1) & 1; 1617430Sbde return uiomove ((caddr_t)&c, sizeof(struct joystick), uio); 1625897Sjmz} 1635897Sjmzint joyioctl (dev_t dev, int cmd, caddr_t data, int flag, struct proc *p) 1645897Sjmz{ 1655897Sjmz int unit = UNIT (dev); 1665897Sjmz int i = joypart (dev); 1675897Sjmz int x; 1685897Sjmz 1695897Sjmz switch (cmd) { 1705897Sjmz case JOY_SETTIMEOUT: 1715897Sjmz x = *(int *) data; 1725897Sjmz if (x < 1 || x > 10000) /* 10ms maximum! */ 1735897Sjmz return EINVAL; 1745897Sjmz joy[unit].timeout[i] = x; 1755897Sjmz break; 1765897Sjmz case JOY_GETTIMEOUT: 1775897Sjmz *(int *) data = joy[unit].timeout[i]; 1785897Sjmz break; 1795897Sjmz case JOY_SET_X_OFFSET: 1805897Sjmz joy[unit].x_off[i] = *(int *) data; 1815897Sjmz break; 1825897Sjmz case JOY_SET_Y_OFFSET: 1835897Sjmz joy[unit].y_off[i] = *(int *) data; 1845897Sjmz break; 1855897Sjmz case JOY_GET_X_OFFSET: 1865897Sjmz *(int *) data = joy[unit].x_off[i]; 1875897Sjmz break; 1885897Sjmz case JOY_GET_Y_OFFSET: 1895897Sjmz *(int *) data = joy[unit].y_off[i]; 1905897Sjmz break; 1915897Sjmz default: 1925897Sjmz return ENXIO; 1935897Sjmz } 1945897Sjmz return 0; 1955897Sjmz} 1965897Sjmzstatic int 1975897Sjmzget_tick () 1985897Sjmz{ 1995897Sjmz int low, high; 2005897Sjmz 2015897Sjmz outb (TIMER_MODE, TIMER_SEL0); 2025897Sjmz low = inb (TIMER_CNTR0); 2035897Sjmz high = inb (TIMER_CNTR0); 2045897Sjmz 2055897Sjmz return (high << 8) | low; 2065897Sjmz} 2075897Sjmz 2085897Sjmz#endif /* NJOY > 0 */ 209