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