joy.c revision 12675
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>
3712675Sjulian#include <sys/conf.h>
3812675Sjulian#include <sys/kernel.h>
3912675Sjulian#ifdef DEVFS
4012675Sjulian#include <sys/devfsext.h>
4112675Sjulian#endif /*DEVFS*/
426734Sbde
436734Sbde#include <machine/joystick.h>
446734Sbde
455897Sjmz#include <i386/isa/isa.h>
466734Sbde#include <i386/isa/isa_device.h>
475897Sjmz#include <i386/isa/timerreg.h>
485897Sjmz
498876Srgrimes/* The game port can manage 4 buttons and 4 variable resistors (usually 2
505897Sjmz * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
515897Sjmz * Getting the state of the buttons is done by reading the game port:
525897Sjmz * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
535897Sjmz * to bits 0-3.
545897Sjmz * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
555897Sjmz * to get the value of a resistor, write the value 0xff at port and
565897Sjmz * wait until the corresponding bit returns to 0.
575897Sjmz */
585897Sjmz
595897Sjmz
608876Srgrimes/* the formulae below only work if u is  ``not too large''. See also
615993Sjmz * the discussion in microtime.s */
625998Sjmz#define usec2ticks(u) 	(((u) * 19549)>>14)
635998Sjmz#define ticks2usec(u) 	(((u) * 3433)>>12)
645897Sjmz
655897Sjmz
665897Sjmz#define joypart(d) minor(d)&1
675897Sjmz#define UNIT(d) minor(d)>>1&3
685897Sjmz#ifndef JOY_TIMEOUT
695897Sjmz#define JOY_TIMEOUT   2000 /* 2 milliseconds */
705897Sjmz#endif
715897Sjmz
725897Sjmzstatic struct {
735897Sjmz    int port;
745897Sjmz    int x_off[2], y_off[2];
755897Sjmz    int timeout[2];
7612675Sjulian#ifdef	DEVFS
7712675Sjulian    void	*devfs_token;
7812675Sjulian#endif
795897Sjmz} joy[NJOY];
805897Sjmz
815897Sjmz
825993Sjmzextern int timer0_max_count;
835897Sjmz
845897Sjmzint joyprobe (struct isa_device *), joyattach (struct isa_device *);
855897Sjmz
865897Sjmzstruct isa_driver joydriver = {joyprobe, joyattach, "joy"};
875897Sjmz
8812675Sjulian#define CDEV_MAJOR 51
8912675Sjulianstatic	d_open_t	joyopen;
9012675Sjulianstatic	d_close_t	joyclose;
9112675Sjulianstatic	d_read_t	joyread;
9212675Sjulianstatic	d_ioctl_t	joyioctl;
9312675Sjulian
9412675Sjulianstruct cdevsw joy_cdevsw =
9512675Sjulian	{ joyopen,	joyclose,	joyread,	nowrite,	/*51*/
9612675Sjulian	  joyioctl,	nostop,		nullreset,	nodevtotty,/*joystick */
9712675Sjulian	  seltrue,	nommap,		NULL,	"joy",	NULL,	-1 };
9812675Sjulian
995897Sjmzstatic int get_tick ();
1005897Sjmz
1015897Sjmz
1025897Sjmzint
1035897Sjmzjoyprobe (struct isa_device *dev)
1045897Sjmz{
1055897Sjmz#ifdef WANT_JOYSTICK_CONNECTED
1065897Sjmz    outb (dev->id_iobase, 0xff);
1075897Sjmz    DELAY (10000); /*  10 ms delay */
1085897Sjmz    return (inb (dev->id_iobase) & 0x0f) != 0x0f;
1095897Sjmz#else
1105897Sjmz    return 1;
1115897Sjmz#endif
1125897Sjmz}
1135897Sjmz
1145897Sjmzint
1155897Sjmzjoyattach (struct isa_device *dev)
1165897Sjmz{
11712675Sjulian    int	unit = dev->id_unit;
11812675Sjulian    char name[32];
1195897Sjmz
12012675Sjulian    joy[unit].port = dev->id_iobase;
12112675Sjulian    joy[unit].timeout[0] = joy[unit].timeout[1] = 0;
12212675Sjulian    printf("joy%d: joystick\n", unit);
12312675Sjulian#ifdef	DEVFS
12412675Sjulian    sprintf(name, "joy%d", unit);
12512675Sjulian    joy[dev->id_unit].devfs_token = devfs_add_devsw( "/", "joy",
12612675Sjulian						&joy_cdevsw, 0,
12712675Sjulian						DV_CHR, 0, 0, 0600);
12812675Sjulian#endif
1295897Sjmz    return 1;
1305897Sjmz}
1315897Sjmz
13212675Sjulianstatic	int
13310644Sbdejoyopen (dev_t dev, int flags, int fmt, struct proc *p)
1345897Sjmz{
1355897Sjmz    int unit = UNIT (dev);
1365897Sjmz    int i = joypart (dev);
1375897Sjmz
1385897Sjmz    if (joy[unit].timeout[i])
1395897Sjmz	return EBUSY;
1405897Sjmz    joy[unit].x_off[i] = joy[unit].y_off[i] = 0;
1415897Sjmz    joy[unit].timeout[i] = JOY_TIMEOUT;
1425897Sjmz    return 0;
1435897Sjmz}
14412675Sjulianstatic	int
14510644Sbdejoyclose (dev_t dev, int flags, int fmt, struct proc *p)
1465897Sjmz{
1475897Sjmz    int unit = UNIT (dev);
1485897Sjmz    int i = joypart (dev);
1495897Sjmz
1505897Sjmz    joy[unit].timeout[i] = 0;
1515897Sjmz    return 0;
1525897Sjmz}
1535897Sjmz
15412675Sjulianstatic	int
1555897Sjmzjoyread (dev_t dev, struct uio *uio, int flag)
1565897Sjmz{
1575897Sjmz    int unit = UNIT(dev);
1585897Sjmz    int port = joy[unit].port;
1595897Sjmz    int i, t0, t1;
1605897Sjmz    int state = 0, x = 0, y = 0;
1616644Sjmz    struct joystick c;
1628876Srgrimes
1635897Sjmz    disable_intr ();
1645897Sjmz    outb (port, 0xff);
1655897Sjmz    t0 = get_tick ();
1665897Sjmz    t1 = t0;
1675897Sjmz    i = usec2ticks(joy[unit].timeout[joypart(dev)]);
1685897Sjmz    while (t0-t1 < i) {
1695897Sjmz	state = inb (port);
1708876Srgrimes	if (joypart(dev) == 1)
1715897Sjmz	    state >>= 2;
1725897Sjmz	t1 = get_tick ();
1735897Sjmz	if (t1 > t0)
1745993Sjmz	    t1 -= timer0_max_count;
1755897Sjmz	if (!x && !(state & 0x01))
1765897Sjmz	    x = t1;
1775897Sjmz	if (!y && !(state & 0x02))
1785897Sjmz	    y =  t1;
1798876Srgrimes	if (x && y)
1805897Sjmz	    break;
1815897Sjmz    }
1825897Sjmz    enable_intr ();
1836644Sjmz    c.x = x ? joy[unit].x_off[joypart(dev)] + ticks2usec(t0-x) : 0x80000000;
1846644Sjmz    c.y = y ? joy[unit].y_off[joypart(dev)] + ticks2usec(t0-y) : 0x80000000;
1855897Sjmz    state >>= 4;
1866644Sjmz    c.b1 = ~state & 1;
1876644Sjmz    c.b2 = ~(state >> 1) & 1;
1887430Sbde    return uiomove ((caddr_t)&c, sizeof(struct joystick), uio);
1895897Sjmz}
19012675Sjulian
19112675Sjulianstatic	int
19212675Sjulianjoyioctl (dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
1935897Sjmz{
1945897Sjmz    int unit = UNIT (dev);
1955897Sjmz    int i = joypart (dev);
1965897Sjmz    int x;
1975897Sjmz
1985897Sjmz    switch (cmd) {
1995897Sjmz    case JOY_SETTIMEOUT:
2005897Sjmz	x = *(int *) data;
2015897Sjmz	if (x < 1 || x > 10000) /* 10ms maximum! */
2025897Sjmz	    return EINVAL;
2035897Sjmz	joy[unit].timeout[i] = x;
2045897Sjmz	break;
2055897Sjmz    case JOY_GETTIMEOUT:
2065897Sjmz	*(int *) data = joy[unit].timeout[i];
2075897Sjmz	break;
2085897Sjmz    case JOY_SET_X_OFFSET:
2095897Sjmz	joy[unit].x_off[i] = *(int *) data;
2105897Sjmz	break;
2115897Sjmz    case JOY_SET_Y_OFFSET:
2125897Sjmz	joy[unit].y_off[i] = *(int *) data;
2135897Sjmz	break;
2145897Sjmz    case JOY_GET_X_OFFSET:
2155897Sjmz	*(int *) data = joy[unit].x_off[i];
2165897Sjmz	break;
2175897Sjmz    case JOY_GET_Y_OFFSET:
2185897Sjmz	*(int *) data = joy[unit].y_off[i];
2195897Sjmz	break;
2205897Sjmz    default:
2215897Sjmz	return ENXIO;
2225897Sjmz    }
2235897Sjmz    return 0;
2245897Sjmz}
22512675Sjulian
2265897Sjmzstatic int
2275897Sjmzget_tick ()
2285897Sjmz{
2295897Sjmz    int low, high;
2305897Sjmz
2315897Sjmz    outb (TIMER_MODE, TIMER_SEL0);
2325897Sjmz    low = inb (TIMER_CNTR0);
2335897Sjmz    high = inb (TIMER_CNTR0);
2345897Sjmz
2355897Sjmz    return (high << 8) | low;
2365897Sjmz}
2375897Sjmz
23812502Sjulian
23912502Sjulianstatic joy_devsw_installed = 0;
24012502Sjulian
24112517Sjulianstatic void 	joy_drvinit(void *unused)
24212502Sjulian{
24312517Sjulian	dev_t dev;
24412517Sjulian
24512502Sjulian	if( ! joy_devsw_installed ) {
24612517Sjulian		dev = makedev(CDEV_MAJOR,0);
24712517Sjulian		cdevsw_add(&dev,&joy_cdevsw,NULL);
24812502Sjulian		joy_devsw_installed = 1;
24912517Sjulian    	}
25012502Sjulian}
25112517Sjulian
25212517SjulianSYSINIT(joydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,joy_drvinit,NULL)
25312517Sjulian
2545897Sjmz#endif /* NJOY > 0 */
255