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