joy.c revision 5993
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 535993Sjmz/* the formulae below only work if u is ``not too large''. See also 545993Sjmz * the discussion in microtime.s */ 555993Sjmz#define usec2ticks(u) ((u) * 19549)>>14 565993Sjmz#define ticks2usec(u) ((u) * 3433)>>12 575897Sjmz 585897Sjmz 595897Sjmz#define joypart(d) minor(d)&1 605897Sjmz#define UNIT(d) minor(d)>>1&3 615897Sjmz#ifndef JOY_TIMEOUT 625897Sjmz#define JOY_TIMEOUT 2000 /* 2 milliseconds */ 635897Sjmz#endif 645897Sjmz 655897Sjmzstatic struct { 665897Sjmz int port; 675897Sjmz int x_off[2], y_off[2]; 685897Sjmz int timeout[2]; 695897Sjmz} joy[NJOY]; 705897Sjmz 715897Sjmz 725993Sjmzextern int timer0_max_count; 735897Sjmz 745897Sjmzint joyprobe (struct isa_device *), joyattach (struct isa_device *); 755897Sjmz 765897Sjmzstruct isa_driver joydriver = {joyprobe, joyattach, "joy"}; 775897Sjmz 785897Sjmzstatic int get_tick (); 795897Sjmz 805897Sjmz 815897Sjmzint 825897Sjmzjoyprobe (struct isa_device *dev) 835897Sjmz{ 845897Sjmz#ifdef WANT_JOYSTICK_CONNECTED 855897Sjmz outb (dev->id_iobase, 0xff); 865897Sjmz DELAY (10000); /* 10 ms delay */ 875897Sjmz return (inb (dev->id_iobase) & 0x0f) != 0x0f; 885897Sjmz#else 895897Sjmz return 1; 905897Sjmz#endif 915897Sjmz} 925897Sjmz 935897Sjmzint 945897Sjmzjoyattach (struct isa_device *dev) 955897Sjmz{ 965897Sjmz joy[dev->id_unit].port = dev->id_iobase; 975897Sjmz joy[dev->id_unit].timeout[0] = joy[dev->id_unit].timeout[1] = 0; 985897Sjmz printf("joy%d: joystick\n", dev->id_unit); 995897Sjmz 1005897Sjmz return 1; 1015897Sjmz} 1025897Sjmz 1035897Sjmzint 1045897Sjmzjoyopen (dev_t dev, int flag) 1055897Sjmz{ 1065897Sjmz int unit = UNIT (dev); 1075897Sjmz int i = joypart (dev); 1085897Sjmz 1095897Sjmz if (joy[unit].timeout[i]) 1105897Sjmz return EBUSY; 1115897Sjmz joy[unit].x_off[i] = joy[unit].y_off[i] = 0; 1125897Sjmz joy[unit].timeout[i] = JOY_TIMEOUT; 1135897Sjmz return 0; 1145897Sjmz} 1155897Sjmzint 1165897Sjmzjoyclose (dev_t dev, int flag) 1175897Sjmz{ 1185897Sjmz int unit = UNIT (dev); 1195897Sjmz int i = joypart (dev); 1205897Sjmz 1215897Sjmz joy[unit].timeout[i] = 0; 1225897Sjmz return 0; 1235897Sjmz} 1245897Sjmz 1255897Sjmzint 1265897Sjmzjoyread (dev_t dev, struct uio *uio, int flag) 1275897Sjmz{ 1285897Sjmz int unit = UNIT(dev); 1295897Sjmz int port = joy[unit].port; 1305897Sjmz int i, t0, t1; 1315897Sjmz int state = 0, x = 0, y = 0; 1325897Sjmz int c[4]; 1335897Sjmz 1345897Sjmz disable_intr (); 1355897Sjmz outb (port, 0xff); 1365897Sjmz t0 = get_tick (); 1375897Sjmz t1 = t0; 1385897Sjmz i = usec2ticks(joy[unit].timeout[joypart(dev)]); 1395897Sjmz while (t0-t1 < i) { 1405897Sjmz state = inb (port); 1415897Sjmz if (joypart(dev) == 1) 1425897Sjmz state >>= 2; 1435897Sjmz t1 = get_tick (); 1445897Sjmz if (t1 > t0) 1455993Sjmz t1 -= timer0_max_count; 1465897Sjmz if (!x && !(state & 0x01)) 1475897Sjmz x = t1; 1485897Sjmz if (!y && !(state & 0x02)) 1495897Sjmz y = t1; 1505897Sjmz if (x && y) 1515897Sjmz break; 1525897Sjmz } 1535897Sjmz enable_intr (); 1545993Sjmz c[0] = x ? joy[unit].x_off[joypart(dev)] + ticks2usec(t0-x) : 0x80000000; 1555993Sjmz c[1] = y ? joy[unit].y_off[joypart(dev)] + ticks2usec(t0-y) : 0x80000000; 1565897Sjmz state >>= 4; 1575897Sjmz c[2] = ~state & 1; 1585897Sjmz c[3] = ~(state >> 1) & 1; 1595897Sjmz return uiomove (c, 4*sizeof(int), uio); 1605897Sjmz} 1615897Sjmzint joyioctl (dev_t dev, int cmd, caddr_t data, int flag, struct proc *p) 1625897Sjmz{ 1635897Sjmz int unit = UNIT (dev); 1645897Sjmz int i = joypart (dev); 1655897Sjmz int x; 1665897Sjmz 1675897Sjmz switch (cmd) { 1685897Sjmz case JOY_SETTIMEOUT: 1695897Sjmz x = *(int *) data; 1705897Sjmz if (x < 1 || x > 10000) /* 10ms maximum! */ 1715897Sjmz return EINVAL; 1725897Sjmz joy[unit].timeout[i] = x; 1735897Sjmz break; 1745897Sjmz case JOY_GETTIMEOUT: 1755897Sjmz *(int *) data = joy[unit].timeout[i]; 1765897Sjmz break; 1775897Sjmz case JOY_SET_X_OFFSET: 1785897Sjmz joy[unit].x_off[i] = *(int *) data; 1795897Sjmz break; 1805897Sjmz case JOY_SET_Y_OFFSET: 1815897Sjmz joy[unit].y_off[i] = *(int *) data; 1825897Sjmz break; 1835897Sjmz case JOY_GET_X_OFFSET: 1845897Sjmz *(int *) data = joy[unit].x_off[i]; 1855897Sjmz break; 1865897Sjmz case JOY_GET_Y_OFFSET: 1875897Sjmz *(int *) data = joy[unit].y_off[i]; 1885897Sjmz break; 1895897Sjmz default: 1905897Sjmz return ENXIO; 1915897Sjmz } 1925897Sjmz return 0; 1935897Sjmz} 1945897Sjmzstatic int 1955897Sjmzget_tick () 1965897Sjmz{ 1975897Sjmz int low, high; 1985897Sjmz 1995897Sjmz outb (TIMER_MODE, TIMER_SEL0); 2005897Sjmz low = inb (TIMER_CNTR0); 2015897Sjmz high = inb (TIMER_CNTR0); 2025897Sjmz 2035897Sjmz return (high << 8) | low; 2045897Sjmz} 2055897Sjmz 2065897Sjmz#endif /* NJOY > 0 */ 207