mse.c revision 7430
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.9 1994/11/08 05:41:34 jkh 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
184int
185mseprobe(idp)
186	register struct isa_device *idp;
187{
188	register struct mse_softc *sc = &mse_sc[idp->id_unit];
189	register int i;
190
191	/*
192	 * Check for each mouse type in the table.
193	 */
194	i = 0;
195	while (mse_types[i].m_type) {
196		if ((*mse_types[i].m_probe)(idp)) {
197			sc->sc_mousetype = mse_types[i].m_type;
198			sc->sc_enablemouse = mse_types[i].m_enable;
199			sc->sc_disablemouse = mse_types[i].m_disable;
200			sc->sc_getmouse = mse_types[i].m_get;
201			return (1);
202		}
203		i++;
204	}
205	return (0);
206}
207
208static struct kern_devconf kdc_mse[NMSE] = { {
209	0, 0, 0,		/* filled in by dev_attach */
210	"mse", 0, { MDDT_ISA, 0, "tty" },
211	isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
212	&kdc_isa0,		/* parent */
213	0,			/* parentdata */
214	DC_UNKNOWN,		/* not supported */
215	"ATI or Logitech bus mouse adapter"
216} };
217
218static inline void
219mse_registerdev(struct isa_device *id)
220{
221	if(id->id_unit)
222		kdc_mse[id->id_unit] = kdc_mse[0];
223	kdc_mse[id->id_unit].kdc_unit = id->id_unit;
224	kdc_mse[id->id_unit].kdc_isa = id;
225	dev_attach(&kdc_mse[id->id_unit]);
226}
227
228int
229mseattach(idp)
230	struct isa_device *idp;
231{
232	struct mse_softc *sc = &mse_sc[idp->id_unit];
233
234	sc->sc_port = idp->id_iobase;
235	mse_registerdev(idp);
236	return (1);
237}
238
239/*
240 * Exclusive open the mouse, initialize it and enable interrupts.
241 */
242int
243mseopen(dev, flag)
244	dev_t dev;
245	int flag;
246{
247	register struct mse_softc *sc;
248	int s;
249
250	if (MSE_UNIT(dev) >= NMSE)
251		return (ENXIO);
252	sc = &mse_sc[MSE_UNIT(dev)];
253	if (sc->sc_flags & MSESC_OPEN)
254		return (EBUSY);
255	sc->sc_flags |= MSESC_OPEN;
256	sc->sc_obuttons = sc->sc_buttons = 0x7;
257	sc->sc_deltax = sc->sc_deltay = 0;
258	sc->sc_bytesread = PROTOBYTES;
259
260	/*
261	 * Initialize mouse interface and enable interrupts.
262	 */
263	s = spltty();
264	(*sc->sc_enablemouse)(sc->sc_port);
265	splx(s);
266	return (0);
267}
268
269/*
270 * mseclose: just turn off mouse innterrupts.
271 */
272int
273mseclose(dev, flag)
274	dev_t dev;
275	int flag;
276{
277	struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
278	int s;
279
280	s = spltty();
281	(*sc->sc_disablemouse)(sc->sc_port);
282	sc->sc_flags &= ~MSESC_OPEN;
283	splx(s);
284	return(0);
285}
286
287/*
288 * mseread: return mouse info using the MSC serial protocol, but without
289 * using bytes 4 and 5.
290 * (Yes this is cheesy, but it makes the X386 server happy, so...)
291 */
292int
293mseread(dev, uio)
294	dev_t dev;
295	struct uio *uio;
296{
297	register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
298	int xfer, s, error;
299
300	/*
301	 * If there are no protocol bytes to be read, set up a new protocol
302	 * packet.
303	 */
304	s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
305	if (sc->sc_bytesread >= PROTOBYTES) {
306		while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
307		       (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
308			if (MSE_NBLOCKIO(dev)) {
309				splx(s);
310				return (0);
311			}
312			sc->sc_flags |= MSESC_WANT;
313			if (error = tsleep((caddr_t)sc, MSEPRI | PCATCH,
314				"mseread", 0)) {
315				splx(s);
316				return (error);
317			}
318		}
319
320		/*
321		 * Generate protocol bytes.
322		 * For some reason X386 expects 5 bytes but never uses
323		 * the fourth or fifth?
324		 */
325		sc->sc_bytes[0] = 0x80 | (sc->sc_buttons & ~0xf8);
326		if (sc->sc_deltax > 127)
327			sc->sc_deltax = 127;
328		if (sc->sc_deltax < -127)
329			sc->sc_deltax = -127;
330		sc->sc_deltay = -sc->sc_deltay;	/* Otherwise mousey goes wrong way */
331		if (sc->sc_deltay > 127)
332			sc->sc_deltay = 127;
333		if (sc->sc_deltay < -127)
334			sc->sc_deltay = -127;
335		sc->sc_bytes[1] = sc->sc_deltax;
336		sc->sc_bytes[2] = sc->sc_deltay;
337		sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
338		sc->sc_obuttons = sc->sc_buttons;
339		sc->sc_deltax = sc->sc_deltay = 0;
340		sc->sc_bytesread = 0;
341	}
342	splx(s);
343	xfer = min(uio->uio_resid, PROTOBYTES - sc->sc_bytesread);
344	if (error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio))
345		return (error);
346	sc->sc_bytesread += xfer;
347	return(0);
348}
349
350/*
351 * mseselect: check for mouse input to be processed.
352 */
353int
354mseselect(dev, rw, p)
355	dev_t dev;
356	int rw;
357	struct proc *p;
358{
359	register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
360	int s;
361
362	s = spltty();
363	if (sc->sc_bytesread != PROTOBYTES || sc->sc_deltax != 0 ||
364	    sc->sc_deltay != 0 || (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
365		splx(s);
366		return (1);
367	}
368
369	/*
370	 * Since this is an exclusive open device, any previous proc.
371	 * pointer is trash now, so we can just assign it.
372	 */
373	selrecord(p, &sc->sc_selp);
374	splx(s);
375	return (0);
376}
377
378/*
379 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
380 */
381void
382mseintr(unit)
383	int unit;
384{
385	register struct mse_softc *sc = &mse_sc[unit];
386	pid_t p;
387
388#ifdef DEBUG
389	static int mse_intrcnt = 0;
390	if((mse_intrcnt++ % 10000) == 0)
391		printf("mseintr\n");
392#endif /* DEBUG */
393	if ((sc->sc_flags & MSESC_OPEN) == 0)
394		return;
395
396	(*sc->sc_getmouse)(sc->sc_port, &sc->sc_deltax, &sc->sc_deltay, &sc->sc_buttons);
397
398	/*
399	 * If mouse state has changed, wake up anyone wanting to know.
400	 */
401	if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
402	    (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
403		if (sc->sc_flags & MSESC_WANT) {
404			sc->sc_flags &= ~MSESC_WANT;
405			wakeup((caddr_t)sc);
406		}
407		selwakeup(&sc->sc_selp);
408	}
409}
410
411/*
412 * Routines for the Logitech mouse.
413 */
414/*
415 * Test for a Logitech bus mouse and return 1 if it is.
416 * (until I know how to use the signature port properly, just disable
417 *  interrupts and return 1)
418 */
419static int
420mse_probelogi(idp)
421	register struct isa_device *idp;
422{
423
424	int sig;
425
426	outb(idp->id_iobase + MSE_PORTD, MSE_SETUP);
427		/* set the signature port */
428	outb(idp->id_iobase + MSE_PORTB, MSE_LOGI_SIG);
429
430	DELAY(30000); /* 30 ms delay */
431	sig = inb(idp->id_iobase + MSE_PORTB) & 0xFF;
432	if (sig == MSE_LOGI_SIG) {
433		outb(idp->id_iobase + MSE_PORTC, MSE_DISINTR);
434		return(1);
435	} else {
436		printf("mse%d: wrong signature %x\n",idp->id_unit,sig);
437		return(0);
438	}
439}
440
441/*
442 * Initialize Logitech mouse and enable interrupts.
443 */
444static void
445mse_enablelogi(port)
446	register u_int port;
447{
448	int dx, dy, but;
449
450	outb(port + MSE_PORTD, MSE_SETUP);
451	mse_getlogi(port, &dx, &dy, &but);
452}
453
454/*
455 * Disable interrupts for Logitech mouse.
456 */
457static void
458mse_disablelogi(port)
459	register u_int port;
460{
461
462	outb(port + MSE_PORTC, MSE_DISINTR);
463}
464
465/*
466 * Get the current dx, dy and button up/down state.
467 */
468static void
469mse_getlogi(port, dx, dy, but)
470	register u_int port;
471	int *dx;
472	int *dy;
473	int *but;
474{
475	register char x, y;
476
477	outb(port + MSE_PORTC, MSE_HOLD | MSE_RXLOW);
478	x = inb(port + MSE_PORTA);
479	*but = (x >> 5) & 0x7;
480	x &= 0xf;
481	outb(port + MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
482	x |= (inb(port + MSE_PORTA) << 4);
483	outb(port + MSE_PORTC, MSE_HOLD | MSE_RYLOW);
484	y = (inb(port + MSE_PORTA) & 0xf);
485	outb(port + MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
486	y |= (inb(port + MSE_PORTA) << 4);
487	*dx += x;
488	*dy += y;
489	outb(port + MSE_PORTC, MSE_INTREN);
490}
491
492/*
493 * Routines for the ATI Inport bus mouse.
494 */
495/*
496 * Test for a ATI Inport bus mouse and return 1 if it is.
497 * (do not enable interrupts)
498 */
499static int
500mse_probeati(idp)
501	register struct isa_device *idp;
502{
503	int i;
504
505	for (i = 0; i < 2; i++)
506		if (inb(idp->id_iobase + MSE_PORTC) == 0xde)
507			return (1);
508	return (0);
509}
510
511/*
512 * Initialize ATI Inport mouse and enable interrupts.
513 */
514static void
515mse_enableati(port)
516	register u_int port;
517{
518
519	outb(port + MSE_PORTA, MSE_INPORT_RESET);
520	outb(port + MSE_PORTA, MSE_INPORT_MODE);
521	outb(port + MSE_PORTB, MSE_INPORT_INTREN);
522}
523
524/*
525 * Disable interrupts for ATI Inport mouse.
526 */
527static void
528mse_disableati(port)
529	register u_int port;
530{
531
532	outb(port + MSE_PORTA, MSE_INPORT_MODE);
533	outb(port + MSE_PORTB, 0);
534}
535
536/*
537 * Get current dx, dy and up/down button state.
538 */
539static void
540mse_getati(port, dx, dy, but)
541	register u_int port;
542	int *dx;
543	int *dy;
544	int *but;
545{
546	register char byte;
547
548	outb(port + MSE_PORTA, MSE_INPORT_MODE);
549	outb(port + MSE_PORTB, MSE_INPORT_HOLD);
550	outb(port + MSE_PORTA, MSE_INPORT_STATUS);
551	*but = ~(inb(port + MSE_PORTB) & 0x7);
552	outb(port + MSE_PORTA, MSE_INPORT_DX);
553	byte = inb(port + MSE_PORTB);
554	*dx += byte;
555	outb(port + MSE_PORTA, MSE_INPORT_DY);
556	byte = inb(port + MSE_PORTB);
557	*dy += byte;
558	outb(port + MSE_PORTA, MSE_INPORT_MODE);
559	outb(port + MSE_PORTB, MSE_INPORT_INTREN);
560}
561#endif /* NMSE */
562