mse.c revision 115703
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 115703 2003-06-02 16:32:55Z obrien $");
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/bus.h>
54#include <sys/poll.h>
55#include <sys/selinfo.h>
56#include <sys/uio.h>
57#include <sys/mouse.h>
58
59#include <machine/bus_pio.h>
60#include <machine/bus.h>
61#include <machine/clock.h>
62#include <machine/resource.h>
63#include <sys/rman.h>
64
65#include <isa/isavar.h>
66
67/* driver configuration flags (config) */
68#define MSE_CONFIG_ACCEL	0x00f0  /* acceleration factor */
69#define MSE_CONFIG_FLAGS	(MSE_CONFIG_ACCEL)
70
71/*
72 * Software control structure for mouse. The sc_enablemouse(),
73 * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
74 */
75typedef struct mse_softc {
76	int		sc_flags;
77	int		sc_mousetype;
78	struct selinfo	sc_selp;
79	struct resource	*sc_port;
80	struct resource	*sc_intr;
81	bus_space_tag_t	sc_iot;
82	bus_space_handle_t sc_ioh;
83	void		*sc_ih;
84	void		(*sc_enablemouse)(bus_space_tag_t t,
85			    bus_space_handle_t h);
86	void		(*sc_disablemouse)(bus_space_tag_t t,
87			    bus_space_handle_t h);
88	void		(*sc_getmouse)(bus_space_tag_t t, bus_space_handle_t h,
89			    int *dx, int *dy, int *but);
90	int		sc_deltax;
91	int		sc_deltay;
92	int		sc_obuttons;
93	int		sc_buttons;
94	int		sc_bytesread;
95	u_char		sc_bytes[MOUSE_SYS_PACKETSIZE];
96	struct		callout_handle sc_callout;
97	int		sc_watchdog;
98	dev_t		sc_dev;
99	dev_t		sc_ndev;
100	mousehw_t	hw;
101	mousemode_t	mode;
102	mousestatus_t	status;
103} mse_softc_t;
104
105static	devclass_t	mse_devclass;
106
107static	int		mse_probe(device_t dev);
108static	int		mse_attach(device_t dev);
109static	int		mse_detach(device_t dev);
110
111static	device_method_t	mse_methods[] = {
112	DEVMETHOD(device_probe,		mse_probe),
113	DEVMETHOD(device_attach,	mse_attach),
114	DEVMETHOD(device_detach,	mse_detach),
115	{ 0, 0 }
116};
117
118static	driver_t	mse_driver = {
119	"mse",
120	mse_methods,
121	sizeof(mse_softc_t),
122};
123
124DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
125
126static struct isa_pnp_id mse_ids[] = {
127	{ 0x000fd041, "Bus mouse" },			/* PNP0F00 */
128	{ 0x020fd041, "InPort mouse" },			/* PNP0F02 */
129	{ 0x0d0fd041, "InPort mouse compatible" },	/* PNP0F0D */
130	{ 0x110fd041, "Bus mouse compatible" },		/* PNP0F11 */
131	{ 0x150fd041, "Logitech bus mouse" },		/* PNP0F15 */
132	{ 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */
133	{ 0 }
134};
135
136static	d_open_t	mseopen;
137static	d_close_t	mseclose;
138static	d_read_t	mseread;
139static  d_ioctl_t	mseioctl;
140static	d_poll_t	msepoll;
141
142#define CDEV_MAJOR 27
143static struct cdevsw mse_cdevsw = {
144	.d_open =	mseopen,
145	.d_close =	mseclose,
146	.d_read =	mseread,
147	.d_ioctl =	mseioctl,
148	.d_poll =	msepoll,
149	.d_name =	"mse",
150	.d_maj =	CDEV_MAJOR,
151};
152
153static	void		mseintr(void *);
154static	timeout_t	msetimeout;
155
156/* Flags */
157#define	MSESC_OPEN	0x1
158#define	MSESC_WANT	0x2
159
160/* and Mouse Types */
161#define	MSE_NONE	0	/* don't move this! */
162#define	MSE_LOGITECH	0x1
163#define	MSE_ATIINPORT	0x2
164#define	MSE_LOGI_SIG	0xA5
165
166#define	MSE_PORTA	0
167#define	MSE_PORTB	1
168#define	MSE_PORTC	2
169#define	MSE_PORTD	3
170#define MSE_IOSIZE	4
171
172#define	MSE_UNIT(dev)		(minor(dev) >> 1)
173#define	MSE_NBLOCKIO(dev)	(minor(dev) & 0x1)
174
175/*
176 * Logitech bus mouse definitions
177 */
178#define	MSE_SETUP	0x91	/* What does this mean? */
179				/* The definition for the control port */
180				/* is as follows: */
181
182				/* D7 	 =  Mode set flag (1 = active) 	*/
183				/* D6,D5 =  Mode selection (port A) 	*/
184				/* 	    00 = Mode 0 = Basic I/O 	*/
185				/* 	    01 = Mode 1 = Strobed I/O 	*/
186				/* 	    10 = Mode 2 = Bi-dir bus 	*/
187				/* D4	 =  Port A direction (1 = input)*/
188				/* D3	 =  Port C (upper 4 bits) 	*/
189				/*	    direction. (1 = input)	*/
190				/* D2	 =  Mode selection (port B & C) */
191				/*	    0 = Mode 0 = Basic I/O	*/
192				/*	    1 = Mode 1 = Strobed I/O	*/
193				/* D1	 =  Port B direction (1 = input)*/
194				/* D0	 =  Port C (lower 4 bits)	*/
195				/*	    direction. (1 = input)	*/
196
197				/* So 91 means Basic I/O on all 3 ports,*/
198				/* Port A is an input port, B is an 	*/
199				/* output port, C is split with upper	*/
200				/* 4 bits being an output port and lower*/
201				/* 4 bits an input port, and enable the */
202				/* sucker.				*/
203				/* Courtesy Intel 8255 databook. Lars   */
204#define	MSE_HOLD	0x80
205#define	MSE_RXLOW	0x00
206#define	MSE_RXHIGH	0x20
207#define	MSE_RYLOW	0x40
208#define	MSE_RYHIGH	0x60
209#define	MSE_DISINTR	0x10
210#define MSE_INTREN	0x00
211
212static	int		mse_probelogi(device_t dev, mse_softc_t *sc);
213static	void		mse_disablelogi(bus_space_tag_t t,
214			    bus_space_handle_t h);
215static	void		mse_getlogi(bus_space_tag_t t, bus_space_handle_t h,
216			    int *dx, int *dy, int *but);
217static	void		mse_enablelogi(bus_space_tag_t t,
218			    bus_space_handle_t h);
219
220/*
221 * ATI Inport mouse definitions
222 */
223#define	MSE_INPORT_RESET	0x80
224#define	MSE_INPORT_STATUS	0x00
225#define	MSE_INPORT_DX		0x01
226#define	MSE_INPORT_DY		0x02
227#define	MSE_INPORT_MODE		0x07
228#define	MSE_INPORT_HOLD		0x20
229#define	MSE_INPORT_INTREN	0x09
230
231static	int		mse_probeati(device_t dev, mse_softc_t *sc);
232static	void		mse_enableati(bus_space_tag_t t, bus_space_handle_t h);
233static	void		mse_disableati(bus_space_tag_t t, bus_space_handle_t h);
234static	void		mse_getati(bus_space_tag_t t, bus_space_handle_t h,
235			    int *dx, int *dy, int *but);
236
237#define	MSEPRI	(PZERO + 3)
238
239/*
240 * Table of mouse types.
241 * Keep the Logitech last, since I haven't figured out how to probe it
242 * properly yet. (Someday I'll have the documentation.)
243 */
244static struct mse_types {
245	int	m_type;		/* Type of bus mouse */
246	int	(*m_probe)(device_t dev, mse_softc_t *sc);
247				/* Probe routine to test for it */
248	void	(*m_enable)(bus_space_tag_t t, bus_space_handle_t h);
249				/* Start routine */
250	void	(*m_disable)(bus_space_tag_t t, bus_space_handle_t h);
251				/* Disable interrupts routine */
252	void	(*m_get)(bus_space_tag_t t, bus_space_handle_t h, int *dx,
253		    int *dy, int *but);
254				/* and get mouse status */
255	mousehw_t   m_hw;	/* buttons iftype type model hwid */
256	mousemode_t m_mode;	/* proto rate res accel level size mask */
257} mse_types[] = {
258	{ MSE_ATIINPORT,
259	  mse_probeati, mse_enableati, mse_disableati, mse_getati,
260	  { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
261	  { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
262	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
263	{ MSE_LOGITECH,
264	  mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,
265	  { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
266	  { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
267	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
268	{ 0, },
269};
270
271static	int
272mse_probe(dev)
273	device_t dev;
274{
275	mse_softc_t *sc;
276	int error;
277	int rid;
278	int i;
279
280	/* check PnP IDs */
281	error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
282	if (error == ENXIO)
283		return error;
284
285	sc = device_get_softc(dev);
286	rid = 0;
287	sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
288					 MSE_IOSIZE, RF_ACTIVE);
289	if (sc->sc_port == NULL)
290		return ENXIO;
291	sc->sc_iot = rman_get_bustag(sc->sc_port);
292	sc->sc_ioh = rman_get_bushandle(sc->sc_port);
293
294	/*
295	 * Check for each mouse type in the table.
296	 */
297	i = 0;
298	while (mse_types[i].m_type) {
299		if ((*mse_types[i].m_probe)(dev, sc)) {
300			sc->sc_mousetype = mse_types[i].m_type;
301			sc->sc_enablemouse = mse_types[i].m_enable;
302			sc->sc_disablemouse = mse_types[i].m_disable;
303			sc->sc_getmouse = mse_types[i].m_get;
304			sc->hw = mse_types[i].m_hw;
305			sc->mode = mse_types[i].m_mode;
306			bus_release_resource(dev, SYS_RES_IOPORT, rid,
307					     sc->sc_port);
308			device_set_desc(dev, "Bus/InPort Mouse");
309			return 0;
310		}
311		i++;
312	}
313	bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
314	return ENXIO;
315}
316
317static	int
318mse_attach(dev)
319	device_t dev;
320{
321	mse_softc_t *sc;
322	int flags;
323	int unit;
324	int rid;
325
326	sc = device_get_softc(dev);
327	unit = device_get_unit(dev);
328
329	rid = 0;
330	sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
331					 MSE_IOSIZE, RF_ACTIVE);
332	if (sc->sc_port == NULL)
333		return ENXIO;
334	sc->sc_intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
335					 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		selwakeup(&sc->sc_selp);
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