mse.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 789 行 · 第 1/2 页

C
789
字号
/* * Copyright 1992 by the University of Guelph * * Permission to use, copy and modify this * software and its documentation for any purpose and without * fee is hereby granted, provided that the above copyright * notice appear in all copies and that both that copyright * notice and this permission notice appear in supporting * documentation. * University of Guelph makes no representations about the suitability of * this software for any purpose.  It is provided "as is" * without express or implied warranty. * * $Id: mse.c,v 1.39 1998/10/22 05:58:39 bde Exp $ *//* * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and * the X386 port, courtesy of * Rick Macklem, rick@snowhite.cis.uoguelph.ca * Caveats: The driver currently uses spltty(), but doesn't use any * generic tty code. It could use splmse() (that only masks off the * bus mouse interrupt, but that would require hacking in i386/isa/icu.s. * (This may be worth the effort, since the Logitech generates 30/60 * interrupts/sec continuously while it is open.) * NB: The ATI has NOT been tested yet! *//* * Modification history: * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com) *   improved probe based on input from Logitech. * * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu) *   fixes to make it work with Microsoft InPort busmouse * * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu) *   added patches for new "select" interface * * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu) *   changed position of some spl()'s in mseread * * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu) *   limit maximum negative x/y value to -127 to work around XFree problem *   that causes spurious button pushes. */#include "mse.h"#if NMSE > 0#include "opt_devfs.h"#include <sys/param.h>#include <sys/systm.h>#include <sys/conf.h>#include <sys/kernel.h>#include <sys/poll.h>#include <sys/select.h>#include <sys/uio.h>#ifdef DEVFS#include <sys/devfsext.h>#endif /*DEVFS*/#include <machine/clock.h>#include <machine/mouse.h>#include <i386/isa/isa_device.h>/* driver configuration flags (config) */#define MSE_CONFIG_ACCEL	0x00f0  /* acceleration factor */#define MSE_CONFIG_FLAGS	(MSE_CONFIG_ACCEL)static int mseprobe(struct isa_device *);static int mseattach(struct isa_device *);struct	isa_driver msedriver = {	mseprobe, mseattach, "mse"};static	d_open_t	mseopen;static	d_close_t	mseclose;static	d_read_t	mseread;static  d_ioctl_t	mseioctl;static	d_poll_t	msepoll;#define CDEV_MAJOR 27static struct cdevsw mse_cdevsw = 	{ mseopen,	mseclose,	mseread,	nowrite,	/*27*/	  mseioctl,	nostop,		nullreset,	nodevtotty,/* mse */	  msepoll,	nommap,		NULL,	"mse",	NULL,	-1 };static ointhand2_t mseintr;/* * Software control structure for mouse. The sc_enablemouse(), * sc_disablemouse() and sc_getmouse() routines must be called spl'd(). */static struct mse_softc {	int	sc_flags;	int	sc_mousetype;	struct	selinfo sc_selp;	u_int	sc_port;	void	(*sc_enablemouse) __P((u_int port));	void	(*sc_disablemouse) __P((u_int port));	void	(*sc_getmouse) __P((u_int port, int *dx, int *dy, int *but));	int	sc_deltax;	int	sc_deltay;	int	sc_obuttons;	int	sc_buttons;	int	sc_bytesread;	u_char	sc_bytes[MOUSE_SYS_PACKETSIZE];	mousehw_t	hw;	mousemode_t	mode;	mousestatus_t	status;#ifdef DEVFS	void 	*devfs_token;	void	*n_devfs_token;#endif} mse_sc[NMSE];/* Flags */#define	MSESC_OPEN	0x1#define	MSESC_WANT	0x2/* and Mouse Types */#define	MSE_NONE	0	/* don't move this! */#define	MSE_LOGITECH	0x1#define	MSE_ATIINPORT	0x2#define	MSE_LOGI_SIG	0xA5#define	MSE_PORTA	0#define	MSE_PORTB	1#define	MSE_PORTC	2#define	MSE_PORTD	3#define	MSE_UNIT(dev)		(minor(dev) >> 1)#define	MSE_NBLOCKIO(dev)	(minor(dev) & 0x1)/* * Logitech bus mouse definitions */#define	MSE_SETUP	0x91	/* What does this mean? */				/* The definition for the control port */				/* is as follows: */				/* D7 	 =  Mode set flag (1 = active) 	*/				/* D6,D5 =  Mode selection (port A) 	*/				/* 	    00 = Mode 0 = Basic I/O 	*/				/* 	    01 = Mode 1 = Strobed I/O 	*/				/* 	    10 = Mode 2 = Bi-dir bus 	*/				/* D4	 =  Port A direction (1 = input)*/				/* D3	 =  Port C (upper 4 bits) 	*/				/*	    direction. (1 = input)	*/				/* D2	 =  Mode selection (port B & C) */				/*	    0 = Mode 0 = Basic I/O	*/				/*	    1 = Mode 1 = Strobed I/O	*/				/* D1	 =  Port B direction (1 = input)*/				/* D0	 =  Port C (lower 4 bits)	*/				/*	    direction. (1 = input)	*/				/* So 91 means Basic I/O on all 3 ports,*/				/* Port A is an input port, B is an 	*/				/* output port, C is split with upper	*/				/* 4 bits being an output port and lower*/				/* 4 bits an input port, and enable the */				/* sucker.				*/				/* Courtesy Intel 8255 databook. Lars   */#define	MSE_HOLD	0x80#define	MSE_RXLOW	0x00#define	MSE_RXHIGH	0x20#define	MSE_RYLOW	0x40#define	MSE_RYHIGH	0x60#define	MSE_DISINTR	0x10#define MSE_INTREN	0x00static int mse_probelogi __P((struct isa_device *idp));static void mse_disablelogi __P((u_int port));static void mse_getlogi __P((u_int port, int *dx, int *dy, int *but));static void mse_enablelogi __P((u_int port));/* * ATI Inport mouse definitions */#define	MSE_INPORT_RESET	0x80#define	MSE_INPORT_STATUS	0x00#define	MSE_INPORT_DX		0x01#define	MSE_INPORT_DY		0x02#define	MSE_INPORT_MODE		0x07#define	MSE_INPORT_HOLD		0x20#define	MSE_INPORT_INTREN	0x09static int mse_probeati __P((struct isa_device *idp));static void mse_enableati __P((u_int port));static void mse_disableati __P((u_int port));static void mse_getati __P((u_int port, int *dx, int *dy, int *but));#define	MSEPRI	(PZERO + 3)/* * Table of mouse types. * Keep the Logitech last, since I haven't figured out how to probe it * properly yet. (Someday I'll have the documentation.) */static struct mse_types {	int	m_type;		/* Type of bus mouse */	int	(*m_probe) __P((struct isa_device *idp));				/* Probe routine to test for it */	void	(*m_enable) __P((u_int port));				/* Start routine */	void	(*m_disable) __P((u_int port));				/* Disable interrupts routine */	void	(*m_get) __P((u_int port, int *dx, int *dy, int *but));				/* and get mouse status */	mousehw_t   m_hw;	/* buttons iftype type model hwid */	mousemode_t m_mode;	/* proto rate res accel level size mask */} mse_types[] = {	{ MSE_ATIINPORT, 	  mse_probeati, mse_enableati, mse_disableati, mse_getati,	  { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },	  { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },	{ MSE_LOGITECH, 	  mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,	  { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },	  { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },	{ 0, },};intmseprobe(idp)	register struct isa_device *idp;{	register struct mse_softc *sc = &mse_sc[idp->id_unit];	register int i;	/*	 * Check for each mouse type in the table.	 */	i = 0;	while (mse_types[i].m_type) {		if ((*mse_types[i].m_probe)(idp)) {			sc->sc_mousetype = mse_types[i].m_type;			sc->sc_enablemouse = mse_types[i].m_enable;			sc->sc_disablemouse = mse_types[i].m_disable;			sc->sc_getmouse = mse_types[i].m_get;			sc->hw = mse_types[i].m_hw;			sc->mode = mse_types[i].m_mode;			return (1);		}		i++;	}	return (0);}intmseattach(idp)	struct isa_device *idp;{	int unit = idp->id_unit;	struct mse_softc *sc = &mse_sc[unit];	idp->id_ointr = mseintr;	sc->sc_port = idp->id_iobase;	sc->mode.accelfactor = (idp->id_flags & MSE_CONFIG_ACCEL) >> 4;#ifdef	DEVFS	sc->devfs_token = 		devfs_add_devswf(&mse_cdevsw, unit << 1, DV_CHR, 0, 0, 				 0600, "mse%d", unit);	sc->n_devfs_token = 		devfs_add_devswf(&mse_cdevsw, (unit<<1)+1, DV_CHR,0, 0, 				 0600, "nmse%d", unit);#endif	return (1);}/* * Exclusive open the mouse, initialize it and enable interrupts. */static	intmseopen(dev, flags, fmt, p)	dev_t dev;	int flags;	int fmt;	struct proc *p;{	register struct mse_softc *sc;	int s;	if (MSE_UNIT(dev) >= NMSE)		return (ENXIO);	sc = &mse_sc[MSE_UNIT(dev)];	if (sc->sc_mousetype == MSE_NONE)		return (ENXIO);	if (sc->sc_flags & MSESC_OPEN)		return (EBUSY);	sc->sc_flags |= MSESC_OPEN;	sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;	sc->sc_deltax = sc->sc_deltay = 0;	sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;	sc->mode.level = 0;	sc->status.flags = 0;	sc->status.button = sc->status.obutton = 0;	sc->status.dx = sc->status.dy = sc->status.dz = 0;	/*	 * Initialize mouse interface and enable interrupts.	 */	s = spltty();	(*sc->sc_enablemouse)(sc->sc_port);	splx(s);	return (0);}/* * mseclose: just turn off mouse innterrupts. */static	intmseclose(dev, flags, fmt, p)	dev_t dev;	int flags;	int fmt;	struct proc *p;{	struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];	int s;	s = spltty();	(*sc->sc_disablemouse)(sc->sc_port);	sc->sc_flags &= ~MSESC_OPEN;	splx(s);	return(0);}/* * mseread: return mouse info using the MSC serial protocol, but without * using bytes 4 and 5. * (Yes this is cheesy, but it makes the X386 server happy, so...) */static	intmseread(dev, uio, ioflag)	dev_t dev;	struct uio *uio;	int ioflag;{	register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];	int xfer, s, error;	/*	 * If there are no protocol bytes to be read, set up a new protocol	 * packet.	 */	s = spltty(); /* XXX Should be its own spl, but where is imlXX() */	if (sc->sc_bytesread >= sc->mode.packetsize) {		while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&		       (sc->sc_obuttons ^ sc->sc_buttons) == 0) {			if (MSE_NBLOCKIO(dev)) {				splx(s);				return (0);			}			sc->sc_flags |= MSESC_WANT;			if (error = tsleep((caddr_t)sc, MSEPRI | PCATCH,				"mseread", 0)) {				splx(s);				return (error);			}		}		/*		 * Generate protocol bytes.		 * For some reason X386 expects 5 bytes but never uses		 * the fourth or fifth?		 */		sc->sc_bytes[0] = sc->mode.syncmask[1] 		    | (sc->sc_buttons & ~sc->mode.syncmask[0]);		if (sc->sc_deltax > 127)			sc->sc_deltax = 127;		if (sc->sc_deltax < -127)			sc->sc_deltax = -127;		sc->sc_deltay = -sc->sc_deltay;	/* Otherwise mousey goes wrong way */		if (sc->sc_deltay > 127)			sc->sc_deltay = 127;		if (sc->sc_deltay < -127)			sc->sc_deltay = -127;		sc->sc_bytes[1] = sc->sc_deltax;		sc->sc_bytes[2] = sc->sc_deltay;		sc->sc_bytes[3] = sc->sc_bytes[4] = 0;		sc->sc_bytes[5] = sc->sc_bytes[6] = 0;		sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;		sc->sc_obuttons = sc->sc_buttons;		sc->sc_deltax = sc->sc_deltay = 0;		sc->sc_bytesread = 0;	}	splx(s);	xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);	if (error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio))		return (error);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?