mse.c revision 82554
1/*
2 * Copyright 1992 by the University of Guelph
3 *
4 * Permission to use, copy and modify this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation.
10 * University of Guelph makes no representations about the suitability of
11 * this software for any purpose.  It is provided "as is"
12 * without express or implied warranty.
13 *
14 * $FreeBSD: head/sys/dev/mse/mse.c 82554 2001-08-30 09:16:53Z msmith $
15 */
16/*
17 * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
18 * the X386 port, courtesy of
19 * Rick Macklem, rick@snowhite.cis.uoguelph.ca
20 * Caveats: The driver currently uses spltty(), but doesn't use any
21 * generic tty code. It could use splmse() (that only masks off the
22 * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
23 * (This may be worth the effort, since the Logitech generates 30/60
24 * interrupts/sec continuously while it is open.)
25 * NB: The ATI has NOT been tested yet!
26 */
27
28/*
29 * Modification history:
30 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
31 *   improved probe based on input from Logitech.
32 *
33 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
34 *   fixes to make it work with Microsoft InPort busmouse
35 *
36 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
37 *   added patches for new "select" interface
38 *
39 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
40 *   changed position of some spl()'s in mseread
41 *
42 * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
43 *   limit maximum negative x/y value to -127 to work around XFree problem
44 *   that causes spurious button pushes.
45 */
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/conf.h>
50#include <sys/kernel.h>
51#include <sys/bus.h>
52#include <sys/poll.h>
53#include <sys/selinfo.h>
54#include <sys/uio.h>
55#include <sys/mouse.h>
56
57#include <machine/bus_pio.h>
58#include <machine/bus.h>
59#include <machine/clock.h>
60#include <machine/resource.h>
61#include <sys/rman.h>
62
63#include <isa/isavar.h>
64
65/* driver configuration flags (config) */
66#define MSE_CONFIG_ACCEL	0x00f0  /* acceleration factor */
67#define MSE_CONFIG_FLAGS	(MSE_CONFIG_ACCEL)
68
69/*
70 * Software control structure for mouse. The sc_enablemouse(),
71 * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
72 */
73typedef struct mse_softc {
74	int		sc_flags;
75	int		sc_mousetype;
76	struct selinfo	sc_selp;
77	struct resource	*sc_port;
78	struct resource	*sc_intr;
79	bus_space_tag_t	sc_iot;
80	bus_space_handle_t sc_ioh;
81	void		*sc_ih;
82	void		(*sc_enablemouse) __P((bus_space_tag_t t,
83					       bus_space_handle_t h));
84	void		(*sc_disablemouse) __P((bus_space_tag_t t,
85						bus_space_handle_t h));
86	void		(*sc_getmouse) __P((bus_space_tag_t t,
87					    bus_space_handle_t h,
88					    int *dx, int *dy, int *but));
89	int		sc_deltax;
90	int		sc_deltay;
91	int		sc_obuttons;
92	int		sc_buttons;
93	int		sc_bytesread;
94	u_char		sc_bytes[MOUSE_SYS_PACKETSIZE];
95	struct		callout_handle sc_callout;
96	int		sc_watchdog;
97	dev_t		sc_dev;
98	dev_t		sc_ndev;
99	mousehw_t	hw;
100	mousemode_t	mode;
101	mousestatus_t	status;
102} mse_softc_t;
103
104static	devclass_t	mse_devclass;
105
106static	int		mse_probe __P((device_t dev));
107static	int		mse_attach __P((device_t dev));
108static	int		mse_detach __P((device_t dev));
109
110static	device_method_t	mse_methods[] = {
111	DEVMETHOD(device_probe,		mse_probe),
112	DEVMETHOD(device_attach,	mse_attach),
113	DEVMETHOD(device_detach,	mse_detach),
114	{ 0, 0 }
115};
116
117static	driver_t	mse_driver = {
118	"mse",
119	mse_methods,
120	sizeof(mse_softc_t),
121};
122
123DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
124
125static struct isa_pnp_id mse_ids[] = {
126	{ 0x000fd041, "Bus mouse" },			/* PNP0F00 */
127	{ 0x020fd041, "InPort mouse" },			/* PNP0F02 */
128	{ 0x0d0fd041, "InPort mouse compatible" },	/* PNP0F0D */
129	{ 0x110fd041, "Bus mouse compatible" },		/* PNP0F11 */
130	{ 0x150fd041, "Logitech bus mouse" },		/* PNP0F15 */
131	{ 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */
132	{ 0 }
133};
134
135static	d_open_t	mseopen;
136static	d_close_t	mseclose;
137static	d_read_t	mseread;
138static  d_ioctl_t	mseioctl;
139static	d_poll_t	msepoll;
140
141#define CDEV_MAJOR 27
142static struct cdevsw mse_cdevsw = {
143	/* open */	mseopen,
144	/* close */	mseclose,
145	/* read */	mseread,
146	/* write */	nowrite,
147	/* ioctl */	mseioctl,
148	/* poll */	msepoll,
149	/* mmap */	nommap,
150	/* strategy */	nostrategy,
151	/* name */	"mse",
152	/* maj */	CDEV_MAJOR,
153	/* dump */	nodump,
154	/* psize */	nopsize,
155	/* flags */	0,
156};
157
158static	void		mseintr __P((void *));
159static	timeout_t	msetimeout;
160
161/* Flags */
162#define	MSESC_OPEN	0x1
163#define	MSESC_WANT	0x2
164
165/* and Mouse Types */
166#define	MSE_NONE	0	/* don't move this! */
167#define	MSE_LOGITECH	0x1
168#define	MSE_ATIINPORT	0x2
169#define	MSE_LOGI_SIG	0xA5
170
171#define	MSE_PORTA	0
172#define	MSE_PORTB	1
173#define	MSE_PORTC	2
174#define	MSE_PORTD	3
175#define MSE_IOSIZE	4
176
177#define	MSE_UNIT(dev)		(minor(dev) >> 1)
178#define	MSE_NBLOCKIO(dev)	(minor(dev) & 0x1)
179
180/*
181 * Logitech bus mouse definitions
182 */
183#define	MSE_SETUP	0x91	/* What does this mean? */
184				/* The definition for the control port */
185				/* is as follows: */
186
187				/* D7 	 =  Mode set flag (1 = active) 	*/
188				/* D6,D5 =  Mode selection (port A) 	*/
189				/* 	    00 = Mode 0 = Basic I/O 	*/
190				/* 	    01 = Mode 1 = Strobed I/O 	*/
191				/* 	    10 = Mode 2 = Bi-dir bus 	*/
192				/* D4	 =  Port A direction (1 = input)*/
193				/* D3	 =  Port C (upper 4 bits) 	*/
194				/*	    direction. (1 = input)	*/
195				/* D2	 =  Mode selection (port B & C) */
196				/*	    0 = Mode 0 = Basic I/O	*/
197				/*	    1 = Mode 1 = Strobed I/O	*/
198				/* D1	 =  Port B direction (1 = input)*/
199				/* D0	 =  Port C (lower 4 bits)	*/
200				/*	    direction. (1 = input)	*/
201
202				/* So 91 means Basic I/O on all 3 ports,*/
203				/* Port A is an input port, B is an 	*/
204				/* output port, C is split with upper	*/
205				/* 4 bits being an output port and lower*/
206				/* 4 bits an input port, and enable the */
207				/* sucker.				*/
208				/* Courtesy Intel 8255 databook. Lars   */
209#define	MSE_HOLD	0x80
210#define	MSE_RXLOW	0x00
211#define	MSE_RXHIGH	0x20
212#define	MSE_RYLOW	0x40
213#define	MSE_RYHIGH	0x60
214#define	MSE_DISINTR	0x10
215#define MSE_INTREN	0x00
216
217static	int		mse_probelogi __P((device_t dev, mse_softc_t *sc));
218static	void		mse_disablelogi __P((bus_space_tag_t t,
219					     bus_space_handle_t h));
220static	void		mse_getlogi __P((bus_space_tag_t t,
221					 bus_space_handle_t h,
222					 int *dx, int *dy, int *but));
223static	void		mse_enablelogi __P((bus_space_tag_t t,
224					    bus_space_handle_t h));
225
226/*
227 * ATI Inport mouse definitions
228 */
229#define	MSE_INPORT_RESET	0x80
230#define	MSE_INPORT_STATUS	0x00
231#define	MSE_INPORT_DX		0x01
232#define	MSE_INPORT_DY		0x02
233#define	MSE_INPORT_MODE		0x07
234#define	MSE_INPORT_HOLD		0x20
235#define	MSE_INPORT_INTREN	0x09
236
237static	int		mse_probeati __P((device_t dev, mse_softc_t *sc));
238static	void		mse_enableati __P((bus_space_tag_t t,
239					   bus_space_handle_t h));
240static	void		mse_disableati __P((bus_space_tag_t t,
241					    bus_space_handle_t h));
242static	void		mse_getati __P((bus_space_tag_t t,
243					bus_space_handle_t h,
244					int *dx, int *dy, int *but));
245
246#define	MSEPRI	(PZERO + 3)
247
248/*
249 * Table of mouse types.
250 * Keep the Logitech last, since I haven't figured out how to probe it
251 * properly yet. (Someday I'll have the documentation.)
252 */
253static struct mse_types {
254	int	m_type;		/* Type of bus mouse */
255	int	(*m_probe) __P((device_t dev, mse_softc_t *sc));
256				/* Probe routine to test for it */
257	void	(*m_enable) __P((bus_space_tag_t t, bus_space_handle_t h));
258				/* Start routine */
259	void	(*m_disable) __P((bus_space_tag_t t, bus_space_handle_t h));
260				/* Disable interrupts routine */
261	void	(*m_get) __P((bus_space_tag_t t, bus_space_handle_t h,
262			      int *dx, int *dy, int *but));
263				/* and get mouse status */
264	mousehw_t   m_hw;	/* buttons iftype type model hwid */
265	mousemode_t m_mode;	/* proto rate res accel level size mask */
266} mse_types[] = {
267	{ MSE_ATIINPORT,
268	  mse_probeati, mse_enableati, mse_disableati, mse_getati,
269	  { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
270	  { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
271	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
272	{ MSE_LOGITECH,
273	  mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,
274	  { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
275	  { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
276	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
277	{ 0, },
278};
279
280static	int
281mse_probe(dev)
282	device_t dev;
283{
284	mse_softc_t *sc;
285	int error;
286	int rid;
287	int i;
288
289	/* check PnP IDs */
290	error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
291	if (error != ENOENT)
292		return error;
293
294	sc = device_get_softc(dev);
295	rid = 0;
296	sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
297					 MSE_IOSIZE, RF_ACTIVE);
298	if (sc->sc_port == NULL)
299		return ENXIO;
300	sc->sc_iot = rman_get_bustag(sc->sc_port);
301	sc->sc_ioh = rman_get_bushandle(sc->sc_port);
302
303	/*
304	 * Check for each mouse type in the table.
305	 */
306	i = 0;
307	while (mse_types[i].m_type) {
308		if ((*mse_types[i].m_probe)(dev, sc)) {
309			sc->sc_mousetype = mse_types[i].m_type;
310			sc->sc_enablemouse = mse_types[i].m_enable;
311			sc->sc_disablemouse = mse_types[i].m_disable;
312			sc->sc_getmouse = mse_types[i].m_get;
313			sc->hw = mse_types[i].m_hw;
314			sc->mode = mse_types[i].m_mode;
315			bus_release_resource(dev, SYS_RES_IOPORT, rid,
316					     sc->sc_port);
317			device_set_desc(dev, "Bus/InPort Mouse");
318			return 0;
319		}
320		i++;
321	}
322	bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
323	return ENXIO;
324}
325
326static	int
327mse_attach(dev)
328	device_t dev;
329{
330	mse_softc_t *sc;
331	int flags;
332	int unit;
333	int rid;
334
335	sc = device_get_softc(dev);
336	unit = device_get_unit(dev);
337
338	rid = 0;
339	sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
340					 MSE_IOSIZE, RF_ACTIVE);
341	if (sc->sc_port == NULL)
342		return ENXIO;
343	sc->sc_intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
344					 RF_ACTIVE);
345	if (sc->sc_intr == NULL) {
346		bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
347		return ENXIO;
348	}
349	sc->sc_iot = rman_get_bustag(sc->sc_port);
350	sc->sc_ioh = rman_get_bushandle(sc->sc_port);
351
352	if (BUS_SETUP_INTR(device_get_parent(dev), dev, sc->sc_intr,
353			   INTR_TYPE_TTY, mseintr, sc, &sc->sc_ih)) {
354		bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
355		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
356		return ENXIO;
357	}
358
359	flags = device_get_flags(dev);
360	sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
361	callout_handle_init(&sc->sc_callout);
362
363	sc->sc_dev = make_dev(&mse_cdevsw, unit << 1, 0, 0, 0600,
364			      "mse%d", unit);
365	sc->sc_ndev = make_dev(&mse_cdevsw, (unit<<1)+1, 0, 0, 0600,
366			       "nmse%d", unit);
367
368	return 0;
369}
370
371static	int
372mse_detach(dev)
373	device_t dev;
374{
375	mse_softc_t *sc;
376	int rid;
377
378	sc = device_get_softc(dev);
379	if (sc->sc_flags & MSESC_OPEN)
380		return EBUSY;
381
382	rid = 0;
383	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
384	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
385	bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
386
387	destroy_dev(sc->sc_dev);
388	destroy_dev(sc->sc_ndev);
389
390	return 0;
391}
392
393/*
394 * Exclusive open the mouse, initialize it and enable interrupts.
395 */
396static	int
397mseopen(dev, flags, fmt, p)
398	dev_t dev;
399	int flags;
400	int fmt;
401	struct proc *p;
402{
403	mse_softc_t *sc;
404	int s;
405
406	sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
407	if (sc == NULL)
408		return (ENXIO);
409	if (sc->sc_mousetype == MSE_NONE)
410		return (ENXIO);
411	if (sc->sc_flags & MSESC_OPEN)
412		return (EBUSY);
413	sc->sc_flags |= MSESC_OPEN;
414	sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
415	sc->sc_deltax = sc->sc_deltay = 0;
416	sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
417	sc->sc_watchdog = FALSE;
418	sc->sc_callout = timeout(msetimeout, dev, hz*2);
419	sc->mode.level = 0;
420	sc->status.flags = 0;
421	sc->status.button = sc->status.obutton = 0;
422	sc->status.dx = sc->status.dy = sc->status.dz = 0;
423
424	/*
425	 * Initialize mouse interface and enable interrupts.
426	 */
427	s = spltty();
428	(*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
429	splx(s);
430	return (0);
431}
432
433/*
434 * mseclose: just turn off mouse innterrupts.
435 */
436static	int
437mseclose(dev, flags, fmt, p)
438	dev_t dev;
439	int flags;
440	int fmt;
441	struct proc *p;
442{
443	mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
444	int s;
445
446	untimeout(msetimeout, dev, sc->sc_callout);
447	callout_handle_init(&sc->sc_callout);
448	s = spltty();
449	(*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
450	sc->sc_flags &= ~MSESC_OPEN;
451	splx(s);
452	return(0);
453}
454
455/*
456 * mseread: return mouse info using the MSC serial protocol, but without
457 * using bytes 4 and 5.
458 * (Yes this is cheesy, but it makes the X386 server happy, so...)
459 */
460static	int
461mseread(dev, uio, ioflag)
462	dev_t dev;
463	struct uio *uio;
464	int ioflag;
465{
466	mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
467	int xfer, s, error;
468
469	/*
470	 * If there are no protocol bytes to be read, set up a new protocol
471	 * packet.
472	 */
473	s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
474	if (sc->sc_bytesread >= sc->mode.packetsize) {
475		while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
476		       (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
477			if (MSE_NBLOCKIO(dev)) {
478				splx(s);
479				return (0);
480			}
481			sc->sc_flags |= MSESC_WANT;
482			error = tsleep((caddr_t)sc, MSEPRI | PCATCH,
483				"mseread", 0);
484			if (error) {
485				splx(s);
486				return (error);
487			}
488		}
489
490		/*
491		 * Generate protocol bytes.
492		 * For some reason X386 expects 5 bytes but never uses
493		 * the fourth or fifth?
494		 */
495		sc->sc_bytes[0] = sc->mode.syncmask[1]
496		    | (sc->sc_buttons & ~sc->mode.syncmask[0]);
497		if (sc->sc_deltax > 127)
498			sc->sc_deltax = 127;
499		if (sc->sc_deltax < -127)
500			sc->sc_deltax = -127;
501		sc->sc_deltay = -sc->sc_deltay;	/* Otherwise mousey goes wrong way */
502		if (sc->sc_deltay > 127)
503			sc->sc_deltay = 127;
504		if (sc->sc_deltay < -127)
505			sc->sc_deltay = -127;
506		sc->sc_bytes[1] = sc->sc_deltax;
507		sc->sc_bytes[2] = sc->sc_deltay;
508		sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
509		sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
510		sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
511		sc->sc_obuttons = sc->sc_buttons;
512		sc->sc_deltax = sc->sc_deltay = 0;
513		sc->sc_bytesread = 0;
514	}
515	splx(s);
516	xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
517	error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
518	if (error)
519		return (error);
520	sc->sc_bytesread += xfer;
521	return(0);
522}
523
524/*
525 * mseioctl: process ioctl commands.
526 */
527static int
528mseioctl(dev, cmd, addr, flag, p)
529	dev_t dev;
530	u_long cmd;
531	caddr_t addr;
532	int flag;
533	struct proc *p;
534{
535	mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
536	mousestatus_t status;
537	int err = 0;
538	int s;
539
540	switch (cmd) {
541
542	case MOUSE_GETHWINFO:
543		s = spltty();
544		*(mousehw_t *)addr = sc->hw;
545		if (sc->mode.level == 0)
546			((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
547		splx(s);
548		break;
549
550	case MOUSE_GETMODE:
551		s = spltty();
552		*(mousemode_t *)addr = sc->mode;
553		switch (sc->mode.level) {
554		case 0:
555			break;
556		case 1:
557			((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
558	    		((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
559	    		((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
560			break;
561		}
562		splx(s);
563		break;
564
565	case MOUSE_SETMODE:
566		switch (((mousemode_t *)addr)->level) {
567		case 0:
568		case 1:
569			break;
570		default:
571			return (EINVAL);
572		}
573		if (((mousemode_t *)addr)->accelfactor < -1)
574			return (EINVAL);
575		else if (((mousemode_t *)addr)->accelfactor >= 0)
576			sc->mode.accelfactor =
577			    ((mousemode_t *)addr)->accelfactor;
578		sc->mode.level = ((mousemode_t *)addr)->level;
579		switch (sc->mode.level) {
580		case 0:
581			sc->sc_bytesread = sc->mode.packetsize
582			    = MOUSE_MSC_PACKETSIZE;
583			break;
584		case 1:
585			sc->sc_bytesread = sc->mode.packetsize
586			    = MOUSE_SYS_PACKETSIZE;
587			break;
588		}
589		break;
590
591	case MOUSE_GETLEVEL:
592		*(int *)addr = sc->mode.level;
593		break;
594
595	case MOUSE_SETLEVEL:
596		switch (*(int *)addr) {
597		case 0:
598			sc->mode.level = *(int *)addr;
599			sc->sc_bytesread = sc->mode.packetsize
600			    = MOUSE_MSC_PACKETSIZE;
601			break;
602		case 1:
603			sc->mode.level = *(int *)addr;
604			sc->sc_bytesread = sc->mode.packetsize
605			    = MOUSE_SYS_PACKETSIZE;
606			break;
607		default:
608			return (EINVAL);
609		}
610		break;
611
612	case MOUSE_GETSTATUS:
613		s = spltty();
614		status = sc->status;
615		sc->status.flags = 0;
616		sc->status.obutton = sc->status.button;
617		sc->status.button = 0;
618		sc->status.dx = 0;
619		sc->status.dy = 0;
620		sc->status.dz = 0;
621		splx(s);
622		*(mousestatus_t *)addr = status;
623		break;
624
625	case MOUSE_READSTATE:
626	case MOUSE_READDATA:
627		return (ENODEV);
628
629#if (defined(MOUSE_GETVARS))
630	case MOUSE_GETVARS:
631	case MOUSE_SETVARS:
632		return (ENODEV);
633#endif
634
635	default:
636		return (ENOTTY);
637	}
638	return (err);
639}
640
641/*
642 * msepoll: check for mouse input to be processed.
643 */
644static	int
645msepoll(dev, events, p)
646	dev_t dev;
647	int events;
648	struct proc *p;
649{
650	mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
651	int s;
652	int revents = 0;
653
654	s = spltty();
655	if (events & (POLLIN | POLLRDNORM)) {
656		if (sc->sc_bytesread != sc->mode.packetsize ||
657		    sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
658		    (sc->sc_obuttons ^ sc->sc_buttons) != 0)
659			revents |= events & (POLLIN | POLLRDNORM);
660		else {
661			/*
662			 * Since this is an exclusive open device, any previous
663			 * proc pointer is trash now, so we can just assign it.
664			 */
665			selrecord(p, &sc->sc_selp);
666		}
667	}
668	splx(s);
669	return (revents);
670}
671
672/*
673 * msetimeout: watchdog timer routine.
674 */
675static void
676msetimeout(arg)
677	void *arg;
678{
679	dev_t dev;
680	mse_softc_t *sc;
681
682	dev = (dev_t)arg;
683	sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
684	if (sc->sc_watchdog) {
685		if (bootverbose)
686			printf("mse%d: lost interrupt?\n", MSE_UNIT(dev));
687		mseintr(sc);
688	}
689	sc->sc_watchdog = TRUE;
690	sc->sc_callout = timeout(msetimeout, dev, hz);
691}
692
693/*
694 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
695 */
696static void
697mseintr(arg)
698	void *arg;
699{
700	/*
701	 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
702	 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
703	 */
704	static int butmap[8] = {
705		0,
706		MOUSE_BUTTON3DOWN,
707		MOUSE_BUTTON2DOWN,
708		MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
709		MOUSE_BUTTON1DOWN,
710		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
711		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
712        	MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
713	};
714	mse_softc_t *sc = arg;
715	int dx, dy, but;
716	int sign;
717
718#ifdef DEBUG
719	static int mse_intrcnt = 0;
720	if((mse_intrcnt++ % 10000) == 0)
721		printf("mseintr\n");
722#endif /* DEBUG */
723	if ((sc->sc_flags & MSESC_OPEN) == 0)
724		return;
725
726	(*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
727	if (sc->mode.accelfactor > 0) {
728		sign = (dx < 0);
729		dx = dx * dx / sc->mode.accelfactor;
730		if (dx == 0)
731			dx = 1;
732		if (sign)
733			dx = -dx;
734		sign = (dy < 0);
735		dy = dy * dy / sc->mode.accelfactor;
736		if (dy == 0)
737			dy = 1;
738		if (sign)
739			dy = -dy;
740	}
741	sc->sc_deltax += dx;
742	sc->sc_deltay += dy;
743	sc->sc_buttons = but;
744
745	but = butmap[~but & MOUSE_MSC_BUTTONS];
746	sc->status.dx += dx;
747	sc->status.dy += dy;
748	sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
749	    | (sc->status.button ^ but);
750	sc->status.button = but;
751
752	sc->sc_watchdog = FALSE;
753
754	/*
755	 * If mouse state has changed, wake up anyone wanting to know.
756	 */
757	if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
758	    (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
759		if (sc->sc_flags & MSESC_WANT) {
760			sc->sc_flags &= ~MSESC_WANT;
761			wakeup((caddr_t)sc);
762		}
763		selwakeup(&sc->sc_selp);
764	}
765}
766
767/*
768 * Routines for the Logitech mouse.
769 */
770/*
771 * Test for a Logitech bus mouse and return 1 if it is.
772 * (until I know how to use the signature port properly, just disable
773 *  interrupts and return 1)
774 */
775static int
776mse_probelogi(dev, sc)
777	device_t dev;
778	mse_softc_t *sc;
779{
780
781	int sig;
782
783	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP);
784		/* set the signature port */
785	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG);
786
787	DELAY(30000); /* 30 ms delay */
788	sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF;
789	if (sig == MSE_LOGI_SIG) {
790		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC,
791				  MSE_DISINTR);
792		return(1);
793	} else {
794		if (bootverbose)
795			device_printf(dev, "wrong signature %x\n", sig);
796		return(0);
797	}
798}
799
800/*
801 * Initialize Logitech mouse and enable interrupts.
802 */
803static void
804mse_enablelogi(tag, handle)
805	bus_space_tag_t tag;
806	bus_space_handle_t handle;
807{
808	int dx, dy, but;
809
810	bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP);
811	mse_getlogi(tag, handle, &dx, &dy, &but);
812}
813
814/*
815 * Disable interrupts for Logitech mouse.
816 */
817static void
818mse_disablelogi(tag, handle)
819	bus_space_tag_t tag;
820	bus_space_handle_t handle;
821{
822
823	bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR);
824}
825
826/*
827 * Get the current dx, dy and button up/down state.
828 */
829static void
830mse_getlogi(tag, handle, dx, dy, but)
831	bus_space_tag_t tag;
832	bus_space_handle_t handle;
833	int *dx;
834	int *dy;
835	int *but;
836{
837	register char x, y;
838
839	bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
840	x = bus_space_read_1(tag, handle, MSE_PORTA);
841	*but = (x >> 5) & MOUSE_MSC_BUTTONS;
842	x &= 0xf;
843	bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
844	x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
845	bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
846	y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf);
847	bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
848	y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
849	*dx = x;
850	*dy = y;
851	bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN);
852}
853
854/*
855 * Routines for the ATI Inport bus mouse.
856 */
857/*
858 * Test for a ATI Inport bus mouse and return 1 if it is.
859 * (do not enable interrupts)
860 */
861static int
862mse_probeati(dev, sc)
863	device_t dev;
864	mse_softc_t *sc;
865{
866	int i;
867
868	for (i = 0; i < 2; i++)
869		if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde)
870			return (1);
871	return (0);
872}
873
874/*
875 * Initialize ATI Inport mouse and enable interrupts.
876 */
877static void
878mse_enableati(tag, handle)
879	bus_space_tag_t tag;
880	bus_space_handle_t handle;
881{
882
883	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET);
884	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
885	bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
886}
887
888/*
889 * Disable interrupts for ATI Inport mouse.
890 */
891static void
892mse_disableati(tag, handle)
893	bus_space_tag_t tag;
894	bus_space_handle_t handle;
895{
896
897	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
898	bus_space_write_1(tag, handle, MSE_PORTB, 0);
899}
900
901/*
902 * Get current dx, dy and up/down button state.
903 */
904static void
905mse_getati(tag, handle, dx, dy, but)
906	bus_space_tag_t tag;
907	bus_space_handle_t handle;
908	int *dx;
909	int *dy;
910	int *but;
911{
912	register char byte;
913
914	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
915	bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD);
916	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS);
917	*but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS;
918	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX);
919	byte = bus_space_read_1(tag, handle, MSE_PORTB);
920	*dx = byte;
921	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY);
922	byte = bus_space_read_1(tag, handle, MSE_PORTB);
923	*dy = byte;
924	bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
925	bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
926}
927