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