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