joy.c revision 5897
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> 345897Sjmz#include <sys/types.h> 355897Sjmz#include <i386/isa/isa.h> 365897Sjmz#include <i386/isa/timerreg.h> 375897Sjmz#include <i386/isa/isa_device.h> 385897Sjmz#include <machine/cpufunc.h> 395897Sjmz 405897Sjmz#include <i386/include/joystick.h> 415897Sjmz 425897Sjmz/* The game port can manage 4 buttons and 4 variable resistors (usually 2 435897Sjmz * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201. 445897Sjmz * Getting the state of the buttons is done by reading the game port: 455897Sjmz * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2) 465897Sjmz * to bits 0-3. 475897Sjmz * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0 485897Sjmz * to get the value of a resistor, write the value 0xff at port and 495897Sjmz * wait until the corresponding bit returns to 0. 505897Sjmz */ 515897Sjmz 525897Sjmz/* #defines below taken from clock.c */ 535897Sjmz#ifndef TIMER_FREQ 545897Sjmz#define TIMER_FREQ 1193182 555897Sjmz#endif 565897Sjmz#define usec2ticks(u) (u) * (TIMER_FREQ / 1000000) \ 575897Sjmz + (u) * ((TIMER_FREQ % 1000000) / 1000) / 1000 \ 585897Sjmz + (u) * (TIMER_FREQ % 1000) / 1000000 595897Sjmz 605897Sjmz 615897Sjmz 625897Sjmz#define joypart(d) minor(d)&1 635897Sjmz#define UNIT(d) minor(d)>>1&3 645897Sjmz#ifndef JOY_TIMEOUT 655897Sjmz#define JOY_TIMEOUT 2000 /* 2 milliseconds */ 665897Sjmz#endif 675897Sjmz 685897Sjmzstatic struct { 695897Sjmz int port; 705897Sjmz int x_off[2], y_off[2]; 715897Sjmz int timeout[2]; 725897Sjmz} joy[NJOY]; 735897Sjmz 745897Sjmz 755897Sjmzextern int hz; 765897Sjmz 775897Sjmzint joyprobe (struct isa_device *), joyattach (struct isa_device *); 785897Sjmz 795897Sjmzstruct isa_driver joydriver = {joyprobe, joyattach, "joy"}; 805897Sjmz 815897Sjmzstatic int get_tick (); 825897Sjmz 835897Sjmz 845897Sjmzint 855897Sjmzjoyprobe (struct isa_device *dev) 865897Sjmz{ 875897Sjmz#ifdef WANT_JOYSTICK_CONNECTED 885897Sjmz outb (dev->id_iobase, 0xff); 895897Sjmz DELAY (10000); /* 10 ms delay */ 905897Sjmz return (inb (dev->id_iobase) & 0x0f) != 0x0f; 915897Sjmz#else 925897Sjmz return 1; 935897Sjmz#endif 945897Sjmz} 955897Sjmz 965897Sjmzint 975897Sjmzjoyattach (struct isa_device *dev) 985897Sjmz{ 995897Sjmz joy[dev->id_unit].port = dev->id_iobase; 1005897Sjmz joy[dev->id_unit].timeout[0] = joy[dev->id_unit].timeout[1] = 0; 1015897Sjmz printf("joy%d: joystick\n", dev->id_unit); 1025897Sjmz 1035897Sjmz return 1; 1045897Sjmz} 1055897Sjmz 1065897Sjmzint 1075897Sjmzjoyopen (dev_t dev, int flag) 1085897Sjmz{ 1095897Sjmz int unit = UNIT (dev); 1105897Sjmz int i = joypart (dev); 1115897Sjmz 1125897Sjmz if (joy[unit].timeout[i]) 1135897Sjmz return EBUSY; 1145897Sjmz joy[unit].x_off[i] = joy[unit].y_off[i] = 0; 1155897Sjmz joy[unit].timeout[i] = JOY_TIMEOUT; 1165897Sjmz return 0; 1175897Sjmz} 1185897Sjmzint 1195897Sjmzjoyclose (dev_t dev, int flag) 1205897Sjmz{ 1215897Sjmz int unit = UNIT (dev); 1225897Sjmz int i = joypart (dev); 1235897Sjmz 1245897Sjmz joy[unit].timeout[i] = 0; 1255897Sjmz return 0; 1265897Sjmz} 1275897Sjmz 1285897Sjmzint 1295897Sjmzjoyread (dev_t dev, struct uio *uio, int flag) 1305897Sjmz{ 1315897Sjmz int unit = UNIT(dev); 1325897Sjmz int port = joy[unit].port; 1335897Sjmz int i, t0, t1; 1345897Sjmz int state = 0, x = 0, y = 0; 1355897Sjmz int c[4]; 1365897Sjmz 1375897Sjmz disable_intr (); 1385897Sjmz outb (port, 0xff); 1395897Sjmz t0 = get_tick (); 1405897Sjmz t1 = t0; 1415897Sjmz i = usec2ticks(joy[unit].timeout[joypart(dev)]); 1425897Sjmz while (t0-t1 < i) { 1435897Sjmz state = inb (port); 1445897Sjmz if (joypart(dev) == 1) 1455897Sjmz state >>= 2; 1465897Sjmz t1 = get_tick (); 1475897Sjmz if (t1 > t0) 1485897Sjmz t1 -= TIMER_FREQ/hz; 1495897Sjmz if (!x && !(state & 0x01)) 1505897Sjmz x = t1; 1515897Sjmz if (!y && !(state & 0x02)) 1525897Sjmz y = t1; 1535897Sjmz if (x && y) 1545897Sjmz break; 1555897Sjmz } 1565897Sjmz enable_intr (); 1575897Sjmz c[0] = x ? joy[unit].x_off[joypart(dev)] + (t0-x) : 0x80000000; 1585897Sjmz c[1] = y ? joy[unit].y_off[joypart(dev)] + (t0-y) : 0x80000000; 1595897Sjmz state >>= 4; 1605897Sjmz c[2] = ~state & 1; 1615897Sjmz c[3] = ~(state >> 1) & 1; 1625897Sjmz return uiomove (c, 4*sizeof(int), uio); 1635897Sjmz} 1645897Sjmzint joyioctl (dev_t dev, int cmd, caddr_t data, int flag, struct proc *p) 1655897Sjmz{ 1665897Sjmz int unit = UNIT (dev); 1675897Sjmz int i = joypart (dev); 1685897Sjmz int x; 1695897Sjmz 1705897Sjmz switch (cmd) { 1715897Sjmz case JOY_SETTIMEOUT: 1725897Sjmz x = *(int *) data; 1735897Sjmz if (x < 1 || x > 10000) /* 10ms maximum! */ 1745897Sjmz return EINVAL; 1755897Sjmz joy[unit].timeout[i] = x; 1765897Sjmz break; 1775897Sjmz case JOY_GETTIMEOUT: 1785897Sjmz *(int *) data = joy[unit].timeout[i]; 1795897Sjmz break; 1805897Sjmz case JOY_SET_X_OFFSET: 1815897Sjmz joy[unit].x_off[i] = *(int *) data; 1825897Sjmz break; 1835897Sjmz case JOY_SET_Y_OFFSET: 1845897Sjmz joy[unit].y_off[i] = *(int *) data; 1855897Sjmz break; 1865897Sjmz case JOY_GET_X_OFFSET: 1875897Sjmz *(int *) data = joy[unit].x_off[i]; 1885897Sjmz break; 1895897Sjmz case JOY_GET_Y_OFFSET: 1905897Sjmz *(int *) data = joy[unit].y_off[i]; 1915897Sjmz break; 1925897Sjmz default: 1935897Sjmz return ENXIO; 1945897Sjmz } 1955897Sjmz return 0; 1965897Sjmz} 1975897Sjmzstatic int 1985897Sjmzget_tick () 1995897Sjmz{ 2005897Sjmz int low, high; 2015897Sjmz 2025897Sjmz outb (TIMER_MODE, TIMER_SEL0); 2035897Sjmz low = inb (TIMER_CNTR0); 2045897Sjmz high = inb (TIMER_CNTR0); 2055897Sjmz 2065897Sjmz return (high << 8) | low; 2075897Sjmz} 2085897Sjmz 2095897Sjmz#endif /* NJOY > 0 */ 210