*BSD News Article 23395


Return to BSD News archive

Xref: sserve comp.os.386bsd.questions:6544 comp.windows.x.i386unix:4607 comp.os.386bsd.misc:1388
Path: sserve!newshost.anu.edu.au!munnari.oz.au!constellation!osuunx.ucc.okstate.edu!moe.ksu.ksu.edu!vixen.cso.uiuc.edu!howland.reston.ans.net!usenet.ins.cwru.edu!eff!news.kei.com!bloom-beacon.mit.edu!ai-lab!life.ai.mit.edu!mycroft
From: mycroft@duality.gnu.ai.mit.edu (Charles Hannum)
Newsgroups: comp.os.386bsd.questions,comp.windows.x.i386unix,comp.os.386bsd.misc
Subject: Re: Mouse (ps/2 type) problem in XFree
Date: 06 Nov 1993 14:42:24 GMT
Organization: MIT Artificial Intelligence Lab
Lines: 505
Message-ID: <MYCROFT.93Nov6094224@duality.gnu.ai.mit.edu>
References: <1993Nov5.053024.19893@kuis.kyoto-u.ac.jp> <2bel7n$bhj@explorer.clark.net>
NNTP-Posting-Host: duality.ai.mit.edu
In-reply-to: ack@clark.net's message of 5 Nov 1993 17:48:23 -0500


In article <2bel7n$bhj@explorer.clark.net> ack@clark.net (Eric
S. Hvozda) writes:

   Failing that, perhaps I'll contact the author of the pms driver.

*I* am the author of that driver, and I have replied to *all* of the
requests for information about it I've seen on the NetBSD mailing
lists.

The fact of the matter is that the pms.c in 0.9 doesn't work.  It was
never tested, and I was quite certain it wouldn't work the first
time.

   If anyone out there has a ps/2 style connect mouse and you have it
   working, please contact me, I'd be interested in exchanging mail...

Shoji Yuen sent me the following version which works for him, but NOTE
THAT THIS INTERFACE IS GOING TO CHANGE IN NETBSD 1.0.  At that time,
it will have the same interface as the other bus mice, and you will
need to configure it as `BusMouse' in your Xconfig file.  The version
below requires you to use `PS/2'.


/*-
 * Copyright (c) 1992, 1993 Erik Forsberg.
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY ``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 I 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.
 */

/*
 *  Ported to 386bsd Oct 17, 1992 
 *  Sandi Donno, Computer Science, University of Cape Town, South Africa
 *  Please send bug reports to sandi@cs.uct.ac.za
 *
 *  Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
 *  although I was only partially successful in getting the alpha release
 *  of his "driver for the Logitech and ATI Inport Bus mice for use with 
 *  386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless 
 *  found his code to be an invaluable reference when porting this driver 
 *  to 386bsd.
 *
 *  Further modifications for latest 386BSD+patchkit and port to NetBSD,
 *  Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
 *
 *  Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
 *  Andrew Herbert - 12 June 1993 
 *
 *  Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
 *  - 13 June 1993
 *
 *  Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
 *  - 24 October 1993
 */

#include "pms.h"

#if NPMS > 0

#include "param.h"
#include "kernel.h"
#include "systm.h"
#include "buf.h"
#include "malloc.h"
#include "ioctl.h"
#include "tty.h"
#include "file.h"
#ifdef NetBSD
#include "select.h"
#endif
#include "proc.h"
#include "vnode.h"

#include "i386/include/mouse.h"
#include "i386/include/pio.h"		/* Julian's fast IO macros */
#include "i386/isa/isa_device.h"

#ifdef 0
#include "syslog.h"		/* For debugging */
#endif

#define DATA	0       /* Offset for data port, read-write */
#define CNTRL	4       /* Offset for control port, write-only */
#define STATUS	4	/* Offset for status port, read-only */

/* status bits */
#define	PMS_OUTPUT_ACK	0x02	/* output acknowledge */
/*#define	PMS_OUTPUT_ACK	0x21	/* output acknowledge */

/* controller commands */
#define	PMS_ENABLE	0xa8	/* enable auxiliary port */
#define	PMS_DISABLE	0xa7	/* disable auxiliary port */
#define	PMS_INT_ENABLE	0x47	/* enable controller interrupts */
#define	PMS_INT_DISABLE	0x65	/* disable controller interrupts */

/* m+use commands */
#define PMS_SET_SCALE11	0xe6	/* set 1:1 scaling */
#define PMS_SET_SCALE21 0xe7	/* set 2:1 scaling */
#define	PMS_SET_RES	0xe8	/* set resolution */
#define	PMS_GET_SCALE	0xe9	/* set scaling factor */
#define	PMS_SET_STREAM	0xea	/* set streaming mode */
#define	PMS_SET_SAMPLE	0xf3	/* set sampling rate */
#define	PMS_DEV_ENABLE	0xf4	/* mouse on */
#define	PMS_DEV_DISABLE	0xf5	/* mouse off */
#define	PMS_RESET	0xff	/* reset */

#define PMSUNIT(dev)	(minor(dev) >> 1)

#ifndef min
#define min(x,y) (x < y ? x : y)
#endif  min

int pmsprobe (struct isa_device *);
int pmsattach (struct isa_device *);

static int pmsaddr[NPMS];	/* Base I/O port addresses per unit */

#define MSBSZ	1024		/* Output queue size (pwr of 2 is best) */

struct ringbuf {
	int count, first, last;
	char queue[MSBSZ];
};

static struct pms_softc {	/* Driver status information */
	struct ringbuf inq;	/* Input queue */
#ifdef NetBSD
	struct selinfo rsel;
#else
	pid_t	rsel;		/* Process selecting for Input */
#endif
	unsigned char state;	/* Mouse driver state */
	unsigned char status;	/* Mouse button status */
	unsigned char button;	/* Previous mouse button status bits */
	int x, y;		/* accumulated motion in the X,Y axis */
} pms_softc[NPMS];

#define OPEN	1		/* Device is open */
#define ASLP	2		/* Waiting for mouse data */

struct isa_driver pmsdriver = { pmsprobe, pmsattach, "pms" };

#define AUX_PORT 0x60		/* AUX_PORT base (S.Yuen) */

static void pms_write_dev(int inport, u_char value)
{
	pms_poll_status();
	outb(inport+CNTRL, 0xd4);
	pms_poll_status();
	outb(inport+DATA,value);
}

static inline void pms_command(int ioport, u_char value)
{
	pms_poll_status();
	outb(ioport+CNTRL, 0x60);
	pms_poll_status();
	outb(ioport+DATA, value);
}

int pmsprobe(struct isa_device *dvp)
{
	/* XXX: Needs a real probe routine. */

	int ioport,c,unit;

	ioport=dvp->id_iobase;
	unit=dvp->id_unit;
	pms_write_dev(ioport,0xff); /* Reset aux device */
	pms_poll_status();
	outb(ioport+CNTRL,0xa9);
	pms_poll_status();
	outb(ioport+CNTRL,0xaa);
	c = inb(ioport+DATA);
	if(c&0x04) {
		printf("PS/2 AUX mouse is not found\n");
		pms_command(ioport,0x65);
		pmsaddr[unit] = 0;	/* Device not found */
		return(0);}
	printf("PS/2 AUX mouse found.  Installing driver\n");
	return (1);
}

int pmsattach(struct isa_device *dvp)
{
	int unit = dvp->id_unit;
	int ioport = dvp->id_iobase;
	struct pms_softc *sc = &pms_softc[unit];

	/* Save I/O base address */

	pmsaddr[unit] = ioport;

	/* Disable mouse interrupts */

	pms_poll_status();
	outb(ioport+CNTRL, PMS_ENABLE);
#ifdef 0
	pms_write(ioport, PMS_SET_RES);
	pms_write(ioport, 0x03);	/* 8 counts/mm */
	pms_write(ioport, PMS_SET_SCALE);
	pms_write(ioport, 0x02);	/* 2:1 */
	pms_write(ioport, PMS_SET_SCALE21);
	pms_write(ioport, PMS_SET_SAMPLE);
	pms_write(ioport, 0x64);	/* 100 samples/sec */
	pms_write(ioport, PMS_SET_STREAM);
#endif
	pms_poll_status();
	outb(ioport+CNTRL, PMS_DISABLE);
	pms_command(ioport, PMS_INT_DISABLE);

	/* Setup initial state */

	sc->state = 0;

	/* Done */

	return(0);
}

int pmsopen(dev_t dev, int flag, int fmt, struct proc *p)
{
	int unit = PMSUNIT(dev);
	struct pms_softc *sc;
	int ioport;

	/* Validate unit number */

	if (unit >= NPMS)
		return(ENXIO);

	/* Get device data */

	sc = &pms_softc[unit];
	ioport = pmsaddr[unit];

	/* If device does not exist */

	if (ioport == 0)
		return(ENXIO);

	/* Disallow multiple opens */
	if (sc->state & OPEN)
		return(EBUSY);

	/* Initialize state */

	sc->state |= OPEN;
#ifdef NetBSD
	sc->rsel.si_pid = 0;
	sc->rsel.si_coll = 0;
#else
	sc->rsel = 0;
#endif
	sc->status = 0;
	sc->button = 0;
	sc->x = 0;
	sc->y = 0;

	/* Allocate and initialize a ring buffer */

	sc->inq.count = sc->inq.first = sc->inq.last = 0;

	/* Enable Bus Mouse interrupts */

	pms_write_dev(ioport, PMS_DEV_ENABLE);
	pms_poll_status();
	outb(ioport+CNTRL, PMS_ENABLE);
	pms_command(ioport, PMS_INT_ENABLE);

	/* Successful open */

	return(0);
}

int pms_poll_status() /* 24 Oct. 1993 */
{

	while(inb(AUX_PORT+STATUS)&0x03) {
		if(inb(AUX_PORT+STATUS) & 0x2 == 0x2)
			inb(AUX_PORT+DATA);}
}
		

int pmsclose(dev_t dev, int flag, int fmt, struct proc *p)
{
	int unit, ioport;
	struct pms_softc *sc;

	/* Get unit and associated info */

	unit = PMSUNIT(dev);
	sc = &pms_softc[unit];
	ioport = pmsaddr[unit];

	/* Disable further mouse interrupts */

	pms_command(ioport,PMS_INT_DISABLE);
	pms_poll_status();
	outb(ioport+CNTRL,PMS_DISABLE );

	/* Complete the close */

	sc->state &= ~OPEN;

	/* close is almost always successful */

	return(0);
}

int pmsread(dev_t dev, struct uio *uio, int flag)
{
	int s;
	int error = 0;	/* keep compiler quiet, even though initialisation
			   is unnecessary */
	unsigned length;
	struct pms_softc *sc;
	unsigned char buffer[100];

	/* Get device information */

	sc = &pms_softc[PMSUNIT(dev)];

	/* Block until mouse activity occured */

	s = spltty();
	while (sc->inq.count == 0) {
		if (minor(dev) & 0x1) {
			splx(s);
			return(EWOULDBLOCK);
		}
		sc->state |= ASLP;
		error = tsleep((caddr_t)sc, PZERO | PCATCH, "pmsrea", 0);
		if (error != 0) {
			splx(s);
			return(error);
		}
	}

	/* Transfer as many chunks as possible */

	while (sc->inq.count > 0 && uio->uio_resid > 0) {
		length = min(sc->inq.count, uio->uio_resid);
		if (length > sizeof(buffer))
			length = sizeof(buffer);

		/* Remove a small chunk from input queue */

		if (sc->inq.first + length >= MSBSZ) {
			bcopy(&sc->inq.queue[sc->inq.first], 
		 	      buffer, MSBSZ - sc->inq.first);
			bcopy(sc->inq.queue, &buffer[MSBSZ-sc->inq.first], 
			      length - (MSBSZ - sc->inq.first));
		}
		else
			bcopy(&sc->inq.queue[sc->inq.first], buffer, length);
	
		sc->inq.first = (sc->inq.first + length) % MSBSZ;
		sc->inq.count -= length;

		/* Copy data to user process */

		error = uiomove(buffer, length, uio);
		if (error)
			break;
	}

	sc->x = sc->y = 0;

	/* Allow interrupts again */

	splx(s);
	return(error);
}

int pmsioctl(dev_t dev, caddr_t addr, int cmd, int flag, struct proc *p)
{
	struct pms_softc *sc;
	struct mouseinfo info;
	int s, error;

	/* Get device information */

	sc = &pms_softc[PMSUNIT(dev)];

	/* Perform IOCTL command */

	switch (cmd) {

	case MOUSEIOCREAD:

		/* Don't modify info while calculating */

		s = spltty();

		/* Build mouse status octet */

		info.status = sc->status;
		if (sc->x || sc->y)
			info.status |= MOVEMENT;

		/* Encode X and Y motion as good as we can */

		if (sc->x > 127)
			info.xmotion = 127;
		else if (sc->x < -128)
			info.xmotion = -128;
		else
			info.xmotion = sc->x;

		if (sc->y > 127)
			info.ymotion = 127;
		else if (sc->y < -128)
			info.ymotion = -128;
		else
			info.ymotion = sc->y;

		/* Reset historical information */

		sc->x = 0;
		sc->y = 0;
		sc->status &= ~BUTCHNGMASK;

		/* Allow interrupts and copy result buffer */

		splx(s);
		error = copyout(&info, addr, sizeof(struct mouseinfo));
		break;

	default:
		error = EINVAL;
		break;
		}

	/* Return error code */

	return(error);
}

void pmsintr(unit)
	int unit;
{
	struct pms_softc *sc = &pms_softc[unit];
	int ioport = pmsaddr[unit];

	sc->inq.queue[sc->inq.last++ % MSBSZ] = inb(ioport+DATA);
	sc->inq.count++;
	if (sc -> state & ASLP) {
		sc->state &= ~ASLP;
		wakeup((caddr_t)sc);
	}
#ifdef NetBSD
	selwakeup(&sc->rsel);
#else
	if (sc->rsel) {
		selwakeup(sc->rsel, 0);
		sc->rsel = 0;
	}
#endif
}

int pmsselect(dev_t dev, int rw, struct proc *p)
{
	int s, ret;
	struct pms_softc *sc = &pms_softc[PMSUNIT(dev)];

	/* Silly to select for output */

	if (rw == FWRITE)
		return(0);

	/* Return true if a mouse event available */

	s = spltty();
	if (sc->inq.count)
		ret = 1;
	else {
#ifdef NetBSD
		selrecord(p, &sc->rsel);
#else
		sc->rsel = p->p_pid;
#endif
		ret = 0;
	}
	splx(s);

	return(ret);
}
#endif