mse.c revision 31254
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 * $Id: mse.c,v 1.34 1997/09/14 03:19:10 peter Exp $
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 "mse.h"
48#if NMSE > 0
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/conf.h>
52#include <sys/kernel.h>
53#include <sys/poll.h>
54#include <sys/select.h>
55#include <sys/uio.h>
56#ifdef DEVFS
57#include <sys/devfsext.h>
58#endif /*DEVFS*/
59
60#include <machine/clock.h>
61
62#include <i386/isa/isa_device.h>
63
64static int mseprobe(struct isa_device *);
65static int mseattach(struct isa_device *);
66
67struct	isa_driver msedriver = {
68	mseprobe, mseattach, "mse"
69};
70
71static	d_open_t	mseopen;
72static	d_close_t	mseclose;
73static	d_read_t	mseread;
74static	d_poll_t	msepoll;
75
76#define CDEV_MAJOR 27
77static struct cdevsw mse_cdevsw =
78	{ mseopen,	mseclose,	mseread,	nowrite,	/*27*/
79	  noioc,	nostop,		nullreset,	nodevtotty,/* mse */
80	  msepoll,	nommap,		NULL,	"mse",	NULL,	-1 };
81
82
83/*
84 * Software control structure for mouse. The sc_enablemouse(),
85 * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
86 */
87#define	PROTOBYTES	5
88static struct mse_softc {
89	int	sc_flags;
90	int	sc_mousetype;
91	struct	selinfo sc_selp;
92	u_int	sc_port;
93	void	(*sc_enablemouse) __P((u_int port));
94	void	(*sc_disablemouse) __P((u_int port));
95	void	(*sc_getmouse) __P((u_int port, int *dx, int *dy, int *but));
96	int	sc_deltax;
97	int	sc_deltay;
98	int	sc_obuttons;
99	int	sc_buttons;
100	int	sc_bytesread;
101	u_char	sc_bytes[PROTOBYTES];
102#ifdef DEVFS
103	void 	*devfs_token;
104	void	*n_devfs_token;
105#endif
106} mse_sc[NMSE];
107
108/* Flags */
109#define	MSESC_OPEN	0x1
110#define	MSESC_WANT	0x2
111
112/* and Mouse Types */
113#define	MSE_NONE	0	/* don't move this! */
114#define	MSE_LOGITECH	0x1
115#define	MSE_ATIINPORT	0x2
116#define	MSE_LOGI_SIG	0xA5
117
118#define	MSE_PORTA	0
119#define	MSE_PORTB	1
120#define	MSE_PORTC	2
121#define	MSE_PORTD	3
122
123#define	MSE_UNIT(dev)		(minor(dev) >> 1)
124#define	MSE_NBLOCKIO(dev)	(minor(dev) & 0x1)
125
126/*
127 * Logitech bus mouse definitions
128 */
129#define	MSE_SETUP	0x91	/* What does this mean? */
130				/* The definition for the control port */
131				/* is as follows: */
132
133				/* D7 	 =  Mode set flag (1 = active) 	*/
134				/* D6,D5 =  Mode selection (port A) 	*/
135				/* 	    00 = Mode 0 = Basic I/O 	*/
136				/* 	    01 = Mode 1 = Strobed I/O 	*/
137				/* 	    10 = Mode 2 = Bi-dir bus 	*/
138				/* D4	 =  Port A direction (1 = input)*/
139				/* D3	 =  Port C (upper 4 bits) 	*/
140				/*	    direction. (1 = input)	*/
141				/* D2	 =  Mode selection (port B & C) */
142				/*	    0 = Mode 0 = Basic I/O	*/
143				/*	    1 = Mode 1 = Strobed I/O	*/
144				/* D1	 =  Port B direction (1 = input)*/
145				/* D0	 =  Port C (lower 4 bits)	*/
146				/*	    direction. (1 = input)	*/
147
148				/* So 91 means Basic I/O on all 3 ports,*/
149				/* Port A is an input port, B is an 	*/
150				/* output port, C is split with upper	*/
151				/* 4 bits being an output port and lower*/
152				/* 4 bits an input port, and enable the */
153				/* sucker.				*/
154				/* Courtesy Intel 8255 databook. Lars   */
155#define	MSE_HOLD	0x80
156#define	MSE_RXLOW	0x00
157#define	MSE_RXHIGH	0x20
158#define	MSE_RYLOW	0x40
159#define	MSE_RYHIGH	0x60
160#define	MSE_DISINTR	0x10
161#define MSE_INTREN	0x00
162
163static int mse_probelogi __P((struct isa_device *idp));
164static void mse_disablelogi __P((u_int port));
165static void mse_getlogi __P((u_int port, int *dx, int *dy, int *but));
166static void mse_enablelogi __P((u_int port));
167
168/*
169 * ATI Inport mouse definitions
170 */
171#define	MSE_INPORT_RESET	0x80
172#define	MSE_INPORT_STATUS	0x00
173#define	MSE_INPORT_DX		0x01
174#define	MSE_INPORT_DY		0x02
175#define	MSE_INPORT_MODE		0x07
176#define	MSE_INPORT_HOLD		0x20
177#define	MSE_INPORT_INTREN	0x09
178
179static int mse_probeati __P((struct isa_device *idp));
180static void mse_enableati __P((u_int port));
181static void mse_disableati __P((u_int port));
182static void mse_getati __P((u_int port, int *dx, int *dy, int *but));
183
184#define	MSEPRI	(PZERO + 3)
185
186/*
187 * Table of mouse types.
188 * Keep the Logitech last, since I haven't figured out how to probe it
189 * properly yet. (Someday I'll have the documentation.)
190 */
191static struct mse_types {
192	int	m_type;		/* Type of bus mouse */
193	int	(*m_probe) __P((struct isa_device *idp));
194				/* Probe routine to test for it */
195	void	(*m_enable) __P((u_int port));
196				/* Start routine */
197	void	(*m_disable) __P((u_int port));
198				/* Disable interrupts routine */
199	void	(*m_get) __P((u_int port, int *dx, int *dy, int *but));
200				/* and get mouse status */
201} mse_types[] = {
202	{ MSE_ATIINPORT, mse_probeati, mse_enableati, mse_disableati, mse_getati },
203	{ MSE_LOGITECH, mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi },
204	{ 0, },
205};
206
207int
208mseprobe(idp)
209	register struct isa_device *idp;
210{
211	register struct mse_softc *sc = &mse_sc[idp->id_unit];
212	register int i;
213
214	/*
215	 * Check for each mouse type in the table.
216	 */
217	i = 0;
218	while (mse_types[i].m_type) {
219		if ((*mse_types[i].m_probe)(idp)) {
220			sc->sc_mousetype = mse_types[i].m_type;
221			sc->sc_enablemouse = mse_types[i].m_enable;
222			sc->sc_disablemouse = mse_types[i].m_disable;
223			sc->sc_getmouse = mse_types[i].m_get;
224			return (1);
225		}
226		i++;
227	}
228	return (0);
229}
230
231int
232mseattach(idp)
233	struct isa_device *idp;
234{
235	int unit = idp->id_unit;
236	struct mse_softc *sc = &mse_sc[unit];
237
238	sc->sc_port = idp->id_iobase;
239#ifdef	DEVFS
240	sc->devfs_token =
241		devfs_add_devswf(&mse_cdevsw, unit << 1, DV_CHR, 0, 0,
242				 0600, "mse%d", unit);
243	sc->n_devfs_token =
244		devfs_add_devswf(&mse_cdevsw, (unit<<1)+1, DV_CHR,0, 0,
245				 0600, "nmse%d", unit);
246#endif
247	return (1);
248}
249
250/*
251 * Exclusive open the mouse, initialize it and enable interrupts.
252 */
253static	int
254mseopen(dev, flags, fmt, p)
255	dev_t dev;
256	int flags;
257	int fmt;
258	struct proc *p;
259{
260	register struct mse_softc *sc;
261	int s;
262
263	if (MSE_UNIT(dev) >= NMSE)
264		return (ENXIO);
265	sc = &mse_sc[MSE_UNIT(dev)];
266	if (sc->sc_mousetype == MSE_NONE)
267		return (ENXIO);
268	if (sc->sc_flags & MSESC_OPEN)
269		return (EBUSY);
270	sc->sc_flags |= MSESC_OPEN;
271	sc->sc_obuttons = sc->sc_buttons = 0x7;
272	sc->sc_deltax = sc->sc_deltay = 0;
273	sc->sc_bytesread = PROTOBYTES;
274
275	/*
276	 * Initialize mouse interface and enable interrupts.
277	 */
278	s = spltty();
279	(*sc->sc_enablemouse)(sc->sc_port);
280	splx(s);
281	return (0);
282}
283
284/*
285 * mseclose: just turn off mouse innterrupts.
286 */
287static	int
288mseclose(dev, flags, fmt, p)
289	dev_t dev;
290	int flags;
291	int fmt;
292	struct proc *p;
293{
294	struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
295	int s;
296
297	s = spltty();
298	(*sc->sc_disablemouse)(sc->sc_port);
299	sc->sc_flags &= ~MSESC_OPEN;
300	splx(s);
301	return(0);
302}
303
304/*
305 * mseread: return mouse info using the MSC serial protocol, but without
306 * using bytes 4 and 5.
307 * (Yes this is cheesy, but it makes the X386 server happy, so...)
308 */
309static	int
310mseread(dev, uio, ioflag)
311	dev_t dev;
312	struct uio *uio;
313	int ioflag;
314{
315	register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
316	int xfer, s, error;
317
318	/*
319	 * If there are no protocol bytes to be read, set up a new protocol
320	 * packet.
321	 */
322	s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
323	if (sc->sc_bytesread >= PROTOBYTES) {
324		while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
325		       (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
326			if (MSE_NBLOCKIO(dev)) {
327				splx(s);
328				return (0);
329			}
330			sc->sc_flags |= MSESC_WANT;
331			if (error = tsleep((caddr_t)sc, MSEPRI | PCATCH,
332				"mseread", 0)) {
333				splx(s);
334				return (error);
335			}
336		}
337
338		/*
339		 * Generate protocol bytes.
340		 * For some reason X386 expects 5 bytes but never uses
341		 * the fourth or fifth?
342		 */
343		sc->sc_bytes[0] = 0x80 | (sc->sc_buttons & ~0xf8);
344		if (sc->sc_deltax > 127)
345			sc->sc_deltax = 127;
346		if (sc->sc_deltax < -127)
347			sc->sc_deltax = -127;
348		sc->sc_deltay = -sc->sc_deltay;	/* Otherwise mousey goes wrong way */
349		if (sc->sc_deltay > 127)
350			sc->sc_deltay = 127;
351		if (sc->sc_deltay < -127)
352			sc->sc_deltay = -127;
353		sc->sc_bytes[1] = sc->sc_deltax;
354		sc->sc_bytes[2] = sc->sc_deltay;
355		sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
356		sc->sc_obuttons = sc->sc_buttons;
357		sc->sc_deltax = sc->sc_deltay = 0;
358		sc->sc_bytesread = 0;
359	}
360	splx(s);
361	xfer = min(uio->uio_resid, PROTOBYTES - sc->sc_bytesread);
362	if (error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio))
363		return (error);
364	sc->sc_bytesread += xfer;
365	return(0);
366}
367
368/*
369 * msepoll: check for mouse input to be processed.
370 */
371static	int
372msepoll(dev, events, p)
373	dev_t dev;
374	int events;
375	struct proc *p;
376{
377	register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
378	int s;
379	int revents = 0;
380
381	s = spltty();
382	if (events & (POLLIN | POLLRDNORM))
383		if (sc->sc_bytesread != PROTOBYTES || sc->sc_deltax != 0 ||
384		    sc->sc_deltay != 0 ||
385		    (sc->sc_obuttons ^ sc->sc_buttons) != 0)
386			revents |= events & (POLLIN | POLLRDNORM);
387		else {
388			/*
389			 * Since this is an exclusive open device, any previous
390			 * proc pointer is trash now, so we can just assign it.
391			 */
392			selrecord(p, &sc->sc_selp);
393		}
394
395	splx(s);
396	return (revents);
397}
398
399/*
400 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
401 */
402void
403mseintr(unit)
404	int unit;
405{
406	register struct mse_softc *sc = &mse_sc[unit];
407
408#ifdef DEBUG
409	static int mse_intrcnt = 0;
410	if((mse_intrcnt++ % 10000) == 0)
411		printf("mseintr\n");
412#endif /* DEBUG */
413	if ((sc->sc_flags & MSESC_OPEN) == 0)
414		return;
415
416	(*sc->sc_getmouse)(sc->sc_port, &sc->sc_deltax, &sc->sc_deltay, &sc->sc_buttons);
417
418	/*
419	 * If mouse state has changed, wake up anyone wanting to know.
420	 */
421	if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
422	    (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
423		if (sc->sc_flags & MSESC_WANT) {
424			sc->sc_flags &= ~MSESC_WANT;
425			wakeup((caddr_t)sc);
426		}
427		selwakeup(&sc->sc_selp);
428	}
429}
430
431/*
432 * Routines for the Logitech mouse.
433 */
434/*
435 * Test for a Logitech bus mouse and return 1 if it is.
436 * (until I know how to use the signature port properly, just disable
437 *  interrupts and return 1)
438 */
439static int
440mse_probelogi(idp)
441	register struct isa_device *idp;
442{
443
444	int sig;
445
446	outb(idp->id_iobase + MSE_PORTD, MSE_SETUP);
447		/* set the signature port */
448	outb(idp->id_iobase + MSE_PORTB, MSE_LOGI_SIG);
449
450	DELAY(30000); /* 30 ms delay */
451	sig = inb(idp->id_iobase + MSE_PORTB) & 0xFF;
452	if (sig == MSE_LOGI_SIG) {
453		outb(idp->id_iobase + MSE_PORTC, MSE_DISINTR);
454		return(1);
455	} else {
456		if (bootverbose)
457			printf("mse%d: wrong signature %x\n",idp->id_unit,sig);
458		return(0);
459	}
460}
461
462/*
463 * Initialize Logitech mouse and enable interrupts.
464 */
465static void
466mse_enablelogi(port)
467	register u_int port;
468{
469	int dx, dy, but;
470
471	outb(port + MSE_PORTD, MSE_SETUP);
472	mse_getlogi(port, &dx, &dy, &but);
473}
474
475/*
476 * Disable interrupts for Logitech mouse.
477 */
478static void
479mse_disablelogi(port)
480	register u_int port;
481{
482
483	outb(port + MSE_PORTC, MSE_DISINTR);
484}
485
486/*
487 * Get the current dx, dy and button up/down state.
488 */
489static void
490mse_getlogi(port, dx, dy, but)
491	register u_int port;
492	int *dx;
493	int *dy;
494	int *but;
495{
496	register char x, y;
497
498	outb(port + MSE_PORTC, MSE_HOLD | MSE_RXLOW);
499	x = inb(port + MSE_PORTA);
500	*but = (x >> 5) & 0x7;
501	x &= 0xf;
502	outb(port + MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
503	x |= (inb(port + MSE_PORTA) << 4);
504	outb(port + MSE_PORTC, MSE_HOLD | MSE_RYLOW);
505	y = (inb(port + MSE_PORTA) & 0xf);
506	outb(port + MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
507	y |= (inb(port + MSE_PORTA) << 4);
508	*dx += x;
509	*dy += y;
510	outb(port + MSE_PORTC, MSE_INTREN);
511}
512
513/*
514 * Routines for the ATI Inport bus mouse.
515 */
516/*
517 * Test for a ATI Inport bus mouse and return 1 if it is.
518 * (do not enable interrupts)
519 */
520static int
521mse_probeati(idp)
522	register struct isa_device *idp;
523{
524	int i;
525
526	for (i = 0; i < 2; i++)
527		if (inb(idp->id_iobase + MSE_PORTC) == 0xde)
528			return (1);
529	return (0);
530}
531
532/*
533 * Initialize ATI Inport mouse and enable interrupts.
534 */
535static void
536mse_enableati(port)
537	register u_int port;
538{
539
540	outb(port + MSE_PORTA, MSE_INPORT_RESET);
541	outb(port + MSE_PORTA, MSE_INPORT_MODE);
542	outb(port + MSE_PORTB, MSE_INPORT_INTREN);
543}
544
545/*
546 * Disable interrupts for ATI Inport mouse.
547 */
548static void
549mse_disableati(port)
550	register u_int port;
551{
552
553	outb(port + MSE_PORTA, MSE_INPORT_MODE);
554	outb(port + MSE_PORTB, 0);
555}
556
557/*
558 * Get current dx, dy and up/down button state.
559 */
560static void
561mse_getati(port, dx, dy, but)
562	register u_int port;
563	int *dx;
564	int *dy;
565	int *but;
566{
567	register char byte;
568
569	outb(port + MSE_PORTA, MSE_INPORT_MODE);
570	outb(port + MSE_PORTB, MSE_INPORT_HOLD);
571	outb(port + MSE_PORTA, MSE_INPORT_STATUS);
572	*but = ~(inb(port + MSE_PORTB) & 0x7);
573	outb(port + MSE_PORTA, MSE_INPORT_DX);
574	byte = inb(port + MSE_PORTB);
575	*dx += byte;
576	outb(port + MSE_PORTA, MSE_INPORT_DY);
577	byte = inb(port + MSE_PORTB);
578	*dy += byte;
579	outb(port + MSE_PORTA, MSE_INPORT_MODE);
580	outb(port + MSE_PORTB, MSE_INPORT_INTREN);
581}
582
583static mse_devsw_installed = 0;
584
585static void 	mse_drvinit(void *unused)
586{
587	dev_t dev;
588
589	if( ! mse_devsw_installed ) {
590		dev = makedev(CDEV_MAJOR, 0);
591		cdevsw_add(&dev,&mse_cdevsw, NULL);
592		mse_devsw_installed = 1;
593    	}
594}
595
596SYSINIT(msedev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,mse_drvinit,NULL)
597
598
599#endif /* NMSE */
600