Subject: port-i386/2267: replace port-i386/1916 - add a joystick driver
To: None <gnats-bugs@NetBSD.ORG>
From: Matthieu Herrb <matthieu@abel.laas.fr>
List: netbsd-bugs
Date: 03/27/1996 08:53:17
>Number:         2267
>Category:       port-i386
>Synopsis:       replace port-i386/1916 - add a joystick driver
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    gnats-admin (GNATS administrator)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Wed Mar 27 03:20:03 1996
>Last-Modified:
>Originator:     Matthieu Herrb
>Organization:
	LAAS/CNRS Toulouse, France
>Release:        NetBSD-current 22/03/96
>Environment:
System: NetBSD cougar 1.1B NetBSD 1.1B (COUGAR) #2: Sun Mar 24 09:50:37 MET 1996 matthieu@cougar:/usr/src/sys/arch/i386/compile/COUGAR i386


>Description:
	There is no joystick device driver in NetBSD/i386. This patch
	is a port of the FreeBSD driver.
	It replaces port-i386/1916 which doesn't apply on 1.1B
>How-To-Repeat:
	N/A
>Fix:
*** /dev/null	Sun Mar 24 10:00:03 1996
--- sys/arch/i386/isa/joy.c	Sat Mar 23 15:51:26 1996
***************
*** 0 ****
--- 1,267 ----
+ /*-
+  * Copyright (c) 1995 Jean-Marc Zucconi
+  * All rights reserved.
+  *
+  * Ported to NetBSD by Matthieu Herrb <matthieu@laas.fr>
+  *
+  * Redistribution and use in source and binary forms, with or without
+  * modification, are permitted provided that the following conditions
+  * are met:
+  * 1. Redistributions of source code must retain the above copyright
+  *    notice, this list of conditions and the following disclaimer
+  *    in this position and unchanged.
+  * 2. Redistributions in binary form must reproduce the above copyright
+  *    notice, this list of conditions and the following disclaimer in the
+  *    documentation and/or other materials provided with the distribution.
+  * 3. The name of the author may not be used to endorse or promote products
+  *    derived from this software withough specific prior written permission
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+  *
+  */
+ 
+ #include <errno.h>
+ 
+ #include <sys/param.h>
+ #include <sys/systm.h>
+ #include <sys/kernel.h>
+ #include <sys/device.h>
+ 
+ #include <machine/cpu.h>
+ #include <machine/pio.h>
+ #include <machine/cpufunc.h>
+ 
+ #include <machine/joystick.h>
+ 
+ #include <dev/isa/isavar.h>
+ #include <dev/isa/isareg.h>
+ #include <i386/isa/timerreg.h>
+ 
+ /* The game port can manage 4 buttons and 4 variable resistors (usually 2
+  * joysticks, each with 2 buttons and 2 pots.) via the port at address 0x201.
+  * Getting the state of the buttons is done by reading the game port:
+  * buttons 1-4 correspond to bits 4-7 and resistors 1-4 (X1, Y1, X2, Y2)
+  * to bits 0-3.
+  * if button 1 (resp 2, 3, 4) is pressed, the bit 4 (resp 5, 6, 7) is set to 0
+  * to get the value of a resistor, write the value 0xff at port and
+  * wait until the corresponding bit returns to 0.
+  */
+ 
+ 
+ /* the formulae below only work if u is  ``not too large''. See also
+  * the discussion in microtime.s */
+ #define usec2ticks(u) 	(((u) * 19549)>>14)
+ #define ticks2usec(u) 	(((u) * 3433)>>12)
+ 
+ 
+ #define joypart(d) minor(d)&1
+ #define JOYUNIT(d) minor(d)>>1&3
+ 
+ #ifndef JOY_TIMEOUT
+ #define JOY_TIMEOUT   2000 /* 2 milliseconds */
+ #endif
+ 
+ #define JOY_NPORTS    1
+ 
+ struct joy_softc {
+     struct device sc_dev;
+     int port;
+     int x_off[2], y_off[2];
+     int timeout[2];
+ };
+ 
+ 
+ int joyprobe __P((struct device *, void *, void *));
+ void joyattach __P((struct device *, struct device *, void *));
+ int joyopen __P((dev_t, int, int, struct proc *));
+ int joyclose __P((dev_t, int, int, struct proc *));
+ static int get_tick __P((void));
+ 
+ struct cfattach joy_ca = {
+     sizeof(struct joy_softc), joyprobe, joyattach
+ };
+ struct cfdriver joy_cd = {
+     NULL, "joy", DV_DULL
+ };
+ 
+ 
+ int
+ joyprobe (parent, match, aux)
+     struct device *parent;
+     void *match, *aux;
+ 
+ {
+     struct isa_attach_args *ia = aux;
+     int iobase = ia->ia_iobase;
+ 
+ #ifdef WANT_JOYSTICK_CONNECTED
+     outb (iobase, 0xff);
+     DELAY (10000); /*  10 ms delay */
+     return (inb (iobase) & 0x0f) != 0x0f;
+ #else
+     ia->ia_iosize = JOY_NPORTS;
+     ia->ia_msize = 0;
+     return 1;
+ #endif
+ }
+ 
+ void
+ joyattach (parent, self, aux)
+     struct device *parent, *self;
+     void *aux;
+ {
+     struct joy_softc *sc = (void *)self;
+     struct isa_attach_args *ia = aux;
+     int	unit = sc->sc_dev.dv_unit;
+     int iobase = ia->ia_iobase;
+     
+     sc->port = iobase;
+     sc->timeout[0] = sc->timeout[1] = 0;
+     outb (iobase, 0xff);
+     DELAY (10000); /*  10 ms delay */
+     printf(": joystick%sconnected\n", 
+ 	   (inb (iobase) & 0x0f) == 0x0f ? " not " : " ");
+ }
+ 
+ int
+ joyopen (dev, flag, mode, p)
+     dev_t dev;
+     int flag, mode;
+     struct proc *p;
+ {
+     int unit = JOYUNIT (dev);
+     int i = joypart (dev);
+     struct joy_softc *sc;
+ 
+     if (unit >= joy_cd.cd_ndevs) {
+ 	return(ENXIO);
+     }
+     sc = joy_cd.cd_devs[unit];
+ 
+     if (sc->timeout[i]) {
+ 	return EBUSY;
+     }
+     sc->x_off[i] = sc->y_off[i] = 0;
+     sc->timeout[i] = JOY_TIMEOUT;
+     return 0;
+ }
+ 
+ int
+ joyclose(dev, flag, mode, p)
+ 	dev_t dev;
+ 	int flag, mode;
+ 	struct proc *p;
+ {
+     int unit = JOYUNIT (dev);
+     int i = joypart (dev);
+     struct joy_softc *sc = joy_cd.cd_devs[unit];
+ 
+     sc->timeout[i] = 0;
+     return 0;
+ }
+ 
+ int
+ joyread(dev, uio, flag)
+ 	dev_t dev;
+ 	struct uio *uio;
+ 	int flag;
+ {
+     int unit = JOYUNIT(dev);
+     struct joy_softc *sc = joy_cd.cd_devs[unit];
+     int port = sc->port;
+     int i, t0, t1;
+     int state = 0, x = 0, y = 0;
+     struct joystick c;
+ 
+     disable_intr ();
+     outb (port, 0xff);
+     t0 = get_tick ();
+     t1 = t0;
+     i = usec2ticks(sc->timeout[joypart(dev)]);
+     while (t0-t1 < i) {
+ 	state = inb (port);
+ 	if (joypart(dev) == 1)
+ 	    state >>= 2;
+ 	t1 = get_tick ();
+ 	if (t1 > t0)
+ 	    t1 -= TIMER_FREQ/hz;
+ 	if (!x && !(state & 0x01))
+ 	    x = t1;
+ 	if (!y && !(state & 0x02))
+ 	    y =  t1;
+ 	if (x && y)
+ 	    break;
+     }
+     enable_intr ();
+     c.x = x ? sc->x_off[joypart(dev)] + ticks2usec(t0-x) : 0x80000000;
+     c.y = y ? sc->y_off[joypart(dev)] + ticks2usec(t0-y) : 0x80000000;
+     state >>= 4;
+     c.b1 = ~state & 1;
+     c.b2 = ~(state >> 1) & 1;
+     return uiomove ((caddr_t)&c, sizeof(struct joystick), uio);
+ }
+ 
+ int
+ joyioctl(dev, cmd, data, flag, p)
+     dev_t dev;
+     u_long cmd;
+     caddr_t data;
+     int flag;
+     struct proc *p;
+ {
+     int unit = JOYUNIT (dev);
+     int i = joypart (dev);
+     struct joy_softc *sc = joy_cd.cd_devs[unit];
+     int x;
+     
+     switch (cmd) {
+       case JOY_SETTIMEOUT:
+ 	x = *(int *) data;
+ 	if (x < 1 || x > 10000) /* 10ms maximum! */
+ 	    return EINVAL;
+ 	sc->timeout[i] = x;
+ 	break;
+       case JOY_GETTIMEOUT:
+ 	*(int *) data = sc->timeout[i];
+       break;
+       case JOY_SET_X_OFFSET:
+ 	sc->x_off[i] = *(int *) data;
+ 	break;
+       case JOY_SET_Y_OFFSET:
+ 	sc->y_off[i] = *(int *) data;
+ 	break;
+       case JOY_GET_X_OFFSET:
+ 	*(int *) data = sc->x_off[i];
+       break;
+       case JOY_GET_Y_OFFSET:
+ 	*(int *) data = sc->y_off[i];
+       break;
+       default:
+ 	return ENXIO;
+     }
+     return 0;
+ }
+ 
+ static int
+ get_tick ()
+ {
+     int low, high;
+ 
+     outb (TIMER_MODE, TIMER_SEL0);
+     low = inb (TIMER_CNTR0);
+     high = inb (TIMER_CNTR0);
+ 
+     return (high << 8) | low;
+ }
+ 
+ 
+ 
*** /dev/null	Sun Mar 24 10:00:03 1996
--- sys/arch/i386/include/joystick.h	Sat Jan 13 20:02:43 1996
***************
*** 0 ****
--- 1,21 ----
+ #ifndef _JOY_IOCTL_H_
+ #define _JOY_IOCTL_H_
+ 
+ #include <sys/types.h>
+ #include <sys/ioctl.h>
+ 
+ struct joystick {
+     int x;
+     int y;
+     int b1;
+     int b2;
+ };
+ 
+ #define JOY_SETTIMEOUT    _IOW('J', 1, int)    /* set timeout */
+ #define JOY_GETTIMEOUT    _IOR('J', 2, int)    /* get timeout */
+ #define JOY_SET_X_OFFSET  _IOW('J', 3, int)    /* set offset on X-axis */
+ #define JOY_SET_Y_OFFSET  _IOW('J', 4, int)    /* set offset on X-axis */
+ #define JOY_GET_X_OFFSET  _IOR('J', 5, int)    /* get offset on X-axis */
+ #define JOY_GET_Y_OFFSET  _IOR('J', 6, int)    /* get offset on Y-axis */
+ 
+ #endif /* _JOY_IOCTL_H_ */
*** /dev/null	Sun Mar 24 10:00:03 1996
--- share/man/man4/man4.i386/joy.4	Sat Jan 13 20:02:45 1996
***************
*** 0 ****
--- 1,114 ----
+ .\"
+ .\" Copyright (c) 1996 Matthieu Herrb
+ .\" All rights reserved.
+ .\"
+ .\" Redistribution and use in source and binary forms, with or without
+ .\" modification, are permitted provided that the following conditions
+ .\" are met:
+ .\" 1. Redistributions of source code must retain the above copyright
+ .\"    notice, this list of conditions and the following disclaimer.
+ .\" 2. Redistributions in binary form must reproduce the above copyright
+ .\"    notice, this list of conditions and the following disclaimer in the
+ .\"    documentation and/or other materials provided with the distribution.
+ .\" 3. All advertising materials mentioning features or use of this software
+ .\"    must display the following acknowledgement:
+ .\"      This product includes software developed by Christopher G. Demetriou.
+ .\" 3. The name of the author may not be used to endorse or promote products
+ .\"    derived from this software without specific prior written permission
+ .\"
+ .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ .\"
+ .\"     $Id$
+ .\"
+ .Dd January 7, 1996
+ .Dt JOY 4 i386
+ .Os "NetBSD 1.1A"
+ .Sh NAME
+ .Nm joy
+ .Nd
+ Games adapter driver
+ .Sh SYNOPSIS
+ .Cd "joy0 at isa? port 0x201"
+ .Sh DESCRIPTION
+ This driver provides access to the games adapter. The lower bit in the
+ minor device number selects the joystick: 0 is the first joystick and
+ 1 is the second.
+ .Pp
+ The game control adapter allows up to two joysticks to be attached to
+ the system. The adapter plus the driver convert the present resistive
+ value to a relative joystick position. On receipt of an output signal,
+ four timing circuits are started. By determining the time required for
+ the circuit to time-out (a function of the resistance), the paddle
+ position can be determined. The adapter could be used as a general
+ purpose I/O card with four analog (resistive) inputs plus four digital
+ input points. 
+ .Pp
+ Applications may call ioctl() on a game adapter driver file descriptor
+ to set and get the offets of the two potentiometers and the maximum
+ time-out value for the circuit. The
+ ioctl() commands are listed in
+ .Pa Aq machine/joystick.h
+ and currently are:
+ .Pp
+ .Bl -tag -width JOY_GET_X_OFFSET -compact
+ .It JOY_SETTIMEOUT
+ Sets the maximum time-out for the adapater.
+ .It JOY_GETTIMEOUT
+ Returns the current maximun time-out.
+ .It JOY_SET_X_OFFSET
+ Sets an offset on X value.
+ .It JOY_GET_X_OFFSET
+ Returns the current X offset.
+ .It JOY_SET_Y_OFFSET
+ Sets an offset on Y value.
+ .It JOY_GET_Y_OFFSET
+ Returns the current Y offset.
+ .El
+ .Pp
+ All this commands take an integer parameter.
+ .Pp
+ Read() on the file descriptor returns a 
+ .Fa joystick 
+ structure:
+ .Bd -literal -offset indent
+ struct joystick {
+ 	int x;
+ 	int y;
+ 	int b1;
+ 	int b2;
+ };
+ .Ed
+ .Pp
+ The fields have the following functions:
+ .Bl -tag -width b1
+ .It Fa x
+ The current X coordinate of the joystick (or position of paddle 1)
+ .It Fa y
+ The current Y coordinate of the joystick (or position of paddle 2)
+ .It Fa b1
+ The current state of button 1
+ .It Fa b2
+ The current state of button 2
+ .El
+ .Sh FILES
+ .Bl -tag -width Pa -compact
+ .It Pa /dev/joy0
+ first joystick
+ .br
+ .It Pa /dev/joy1
+ second joystick
+ .El
+ .Sh AUTHORS
+ .Pp
+ Jean-Marc Zucconi wrote the FreeBSD driver. Matthieu Herrb ported it
+ to NetBSD and wrote this manual page.
+ 
*** /sys/arch/i386/conf/files.i386~	Mon Mar 18 13:26:37 1996
--- sys/arch/i386/conf/files.i386	Sat Mar 23 14:05:49 1996
***************
*** 130,135 ****
--- 130,140 ----
  attach	le at pci with le_pci
  file	dev/isa/if_le.c			le
  
+ # Game adapter (joystick)
+ device	joy
+ attach	joy at isa
+ file	arch/i386/isa/joy.c		joy needs-flag
+ 
  #
  # EISA-only drivers
  #
*** sys/arch/i386/i386/conf.c~	Fri Mar 15 13:36:24 1996
--- sys/arch/i386/i386/conf.c	Sat Mar 23 14:03:13 1996
***************
*** 117,122 ****
--- 117,123 ----
  	(dev_type_write((*))) enodev, dev_init(c,n,ioctl), \
  	(dev_type_stop((*))) enodev, 0, seltrue, \
  	(dev_type_mmap((*))) enodev }
+ #define cdev_joy_init cdev_ss_init
  
  cdev_decl(cn);
  cdev_decl(ctty);
***************
*** 179,184 ****
--- 180,187 ----
  cdev_decl(audio);
  cdev_decl(svr4_net);
  cdev_decl(ccd);
+ #include "joy.h"
+ cdev_decl(joy);
  
  struct cdevsw	cdevsw[] =
  {
***************
*** 210,216 ****
  	cdev_bpftun_init(NBPFILTER,bpf),/* 23: Berkeley packet filter */
  	cdev_notdef(),			/* 24 */
  	cdev_notdef(),			/* 25 */
! 	cdev_notdef(),			/* 26 */
  	cdev_spkr_init(NSPKR,spkr),	/* 27: PC speaker */
  	cdev_lkm_init(NLKM,lkm),	/* 28: loadable module driver */
  	cdev_lkm_dummy(),		/* 29 */
--- 213,219 ----
  	cdev_bpftun_init(NBPFILTER,bpf),/* 23: Berkeley packet filter */
  	cdev_notdef(),			/* 24 */
  	cdev_notdef(),			/* 25 */
! 	cdev_joy_init(NJOY,joy),        /* 26: joystick */
  	cdev_spkr_init(NSPKR,spkr),	/* 27: PC speaker */
  	cdev_lkm_init(NLKM,lkm),	/* 28: loadable module driver */
  	cdev_lkm_dummy(),		/* 29 */

>Audit-Trail:
>Unformatted: