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