xref: /openbsd-src/usr.sbin/wsmoused/mouse_protocols.c (revision 3a50f0a93a2072911d0ba6ababa815fb04bf9a71)
1*3a50f0a9Sjmc /* $OpenBSD: mouse_protocols.c,v 1.19 2022/12/28 21:30:19 jmc Exp $ */
2673a75b7Saaron 
3673a75b7Saaron /*
4673a75b7Saaron  * Copyright (c) 2001 Jean-Baptiste Marchand, Julien Montagne and Jerome Verdon
5673a75b7Saaron  *
6673a75b7Saaron  * Copyright (c) 1998 by Kazutaka Yokota
7673a75b7Saaron  *
8673a75b7Saaron  * Copyright (c) 1995 Michael Smith
9673a75b7Saaron  *
10673a75b7Saaron  * Copyright (c) 1993 by David Dawes <dawes@xfree86.org>
11673a75b7Saaron  *
12673a75b7Saaron  * Copyright (c) 1990,91 by Thomas Roell, Dinkelscherben, Germany.
13673a75b7Saaron  *
14673a75b7Saaron  * All rights reserved.
15673a75b7Saaron  *
16673a75b7Saaron  * Most of this code was taken from the FreeBSD moused daemon, written by
17673a75b7Saaron  * Michael Smith. The FreeBSD moused daemon already contained code from the
18673a75b7Saaron  * Xfree Project, written by David Dawes and Thomas Roell and Kazutaka Yokota.
19673a75b7Saaron  *
20673a75b7Saaron  * Adaptation to OpenBSD was done by Jean-Baptiste Marchand, Julien Montagne
21673a75b7Saaron  * and Jerome Verdon.
22673a75b7Saaron  *
23673a75b7Saaron  * Redistribution and use in source and binary forms, with or without
24673a75b7Saaron  * modification, are permitted provided that the following conditions
25673a75b7Saaron  * are met:
26673a75b7Saaron  * 1. Redistributions of source code must retain the above copyright
27673a75b7Saaron  *    notice, this list of conditions and the following disclaimer.
28673a75b7Saaron  * 2. Redistributions in binary form must reproduce the above copyright
29673a75b7Saaron  *    notice, this list of conditions and the following disclaimer in the
30673a75b7Saaron  *    documentation and/or other materials provided with the distribution.
31673a75b7Saaron  * 3. All advertising materials mentioning features or use of this software
32673a75b7Saaron  *    must display the following acknowledgement:
33673a75b7Saaron  *	This product includes software developed by
34673a75b7Saaron  *      David Dawes, Jean-Baptiste Marchand, Julien Montagne, Thomas Roell,
35673a75b7Saaron  *      Michael Smith, Jerome Verdon and Kazutaka Yokota.
36673a75b7Saaron  * 4. The name authors may not be used to endorse or promote products
37673a75b7Saaron  *    derived from this software without specific prior written permission.
38673a75b7Saaron  *
39673a75b7Saaron  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
40673a75b7Saaron  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41673a75b7Saaron  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42673a75b7Saaron  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43673a75b7Saaron  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44673a75b7Saaron  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45673a75b7Saaron  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46673a75b7Saaron  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47673a75b7Saaron  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48673a75b7Saaron  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49673a75b7Saaron  *
50673a75b7Saaron  *
51673a75b7Saaron  */
52673a75b7Saaron 
53673a75b7Saaron /* Support for non-wsmouse (i.e. serial mice) mice */
54673a75b7Saaron 
55673a75b7Saaron /*
56673a75b7Saaron  * Most of this code comes from the Xfree Project and are derived from two files
57673a75b7Saaron  * of Xfree86 3.3.6 with the following CVS tags :
58673a75b7Saaron  $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.21.2.24
59673a75b7Saaron  1999/12/11 19:00:42 hohndel Exp $
60673a75b7Saaron  and
61673a75b7Saaron  $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_PnPMouse.c,v 1.1.2.6
62673a75b7Saaron  1999/07/29 09:22:51 hohndel Exp $
63673a75b7Saaron  */
64673a75b7Saaron 
65673a75b7Saaron #include <sys/ioctl.h>
66673a75b7Saaron #include <sys/types.h>
67673a75b7Saaron #include <sys/time.h>
68673a75b7Saaron #include <sys/tty.h>
69673a75b7Saaron 
70673a75b7Saaron #include <ctype.h>
71673a75b7Saaron #include <err.h>
72673a75b7Saaron #include <errno.h>
73673a75b7Saaron #include <fcntl.h>
74673a75b7Saaron #include <unistd.h>
75673a75b7Saaron #include <signal.h>
76673a75b7Saaron #include <poll.h>
77673a75b7Saaron #include <stdio.h>
78673a75b7Saaron #include <string.h>
79673a75b7Saaron #include <stdlib.h>
80673a75b7Saaron #include <syslog.h>
81673a75b7Saaron 
82673a75b7Saaron #include "wsmoused.h"
83673a75b7Saaron #include "mouse_protocols.h"
84673a75b7Saaron 
85673a75b7Saaron extern int	debug;
86673a75b7Saaron extern int	nodaemon;
87673a75b7Saaron extern int	background;
88673a75b7Saaron extern mouse_t	mouse;
89673a75b7Saaron 
90673a75b7Saaron /* Cflags of each mouse protocol, ordered by P_XXX */
91be45c8afSmiod static const unsigned short mousecflags[] = {
929523400bSjsg 	(CS7 | CREAD | CLOCAL | HUPCL),	/* Microsoft */
93673a75b7Saaron 	(CS8 | CSTOPB | CREAD | CLOCAL | HUPCL),	/* MouseSystems */
94673a75b7Saaron 	(CS8 | CSTOPB | CREAD | CLOCAL | HUPCL),	/* Logitech */
95673a75b7Saaron 	(CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL),	/* MMSeries */
96673a75b7Saaron 	(CS7 | CREAD | CLOCAL | HUPCL),	/* MouseMan */
97673a75b7Saaron 	(CS8 | CREAD | CLOCAL | HUPCL),	/* MM HitTablet */
98673a75b7Saaron 	(CS7 | CREAD | CLOCAL | HUPCL),	/* GlidePoint */
99673a75b7Saaron 	(CS7 | CREAD | CLOCAL | HUPCL),	/* IntelliMouse */
100673a75b7Saaron 	(CS7 | CREAD | CLOCAL | HUPCL),	/* Thinking Mouse */
101673a75b7Saaron };
102673a75b7Saaron 
103673a75b7Saaron /* array ordered by P_XXX giving protocol properties */
104be45c8afSmiod static const unsigned char proto[][7] = {
105279fd192Sderaadt 	/* mask hd_id   dp_mask dp_id   bytes b4_mask b4_id */
1069523400bSjsg 	{0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00},	/* Microsoft */
107673a75b7Saaron 	{0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff},	/* MouseSystems */
108673a75b7Saaron 	{0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff},	/* Logitech */
109673a75b7Saaron 	{0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff},	/* MMSeries */
110673a75b7Saaron 	{0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00},	/* MouseMan */
111673a75b7Saaron 	{0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff},	/* MM HitTablet */
112673a75b7Saaron 	{0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00},	/* GlidePoint */
113673a75b7Saaron 	{0x40, 0x40, 0x40, 0x00, 3, ~0x3f, 0x00},	/* IntelliMouse */
114673a75b7Saaron 	{0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00},	/* ThinkingMouse */
115673a75b7Saaron };
116673a75b7Saaron 
117279fd192Sderaadt /*
118279fd192Sderaadt  * array ordered by P_XXX (mouse protocols) giving the protocol
119279fd192Sderaadt  * corresponding to the name of a mouse
120279fd192Sderaadt  */
121be45c8afSmiod const char *mouse_names[] = {
122673a75b7Saaron 	"microsoft",
123673a75b7Saaron 	"mousesystems",
124673a75b7Saaron 	"logitech",
125673a75b7Saaron 	"mmseries",
126673a75b7Saaron 	"mouseman",
127673a75b7Saaron 	"mmhitab",
128673a75b7Saaron 	"glidepoint",
129673a75b7Saaron 	"intellimouse",
130673a75b7Saaron 	"thinkingmouse",
131673a75b7Saaron 	NULL
132673a75b7Saaron };
133673a75b7Saaron 
134673a75b7Saaron /* protocol currently used */
135673a75b7Saaron static unsigned char cur_proto[7];
136673a75b7Saaron 
137673a75b7Saaron /* PnP EISA/product IDs */
138be45c8afSmiod static const symtab_t pnpprod[] = {
139673a75b7Saaron 	{"KML0001", P_THINKING},/* Kensignton ThinkingMouse */
140673a75b7Saaron 	{"MSH0001", P_IMSERIAL},/* MS IntelliMouse */
141673a75b7Saaron 	{"MSH0004", P_IMSERIAL},/* MS IntelliMouse TrackBall */
142673a75b7Saaron 	{"KYEEZ00", P_MS},	/* Genius EZScroll */
143673a75b7Saaron 	{"KYE0001", P_MS},	/* Genius PnP Mouse */
144673a75b7Saaron 	{"KYE0003", P_IMSERIAL},/* Genius NetMouse */
145673a75b7Saaron 	{"LGI800C", P_IMSERIAL},/* Logitech MouseMan (4 button model) */
146673a75b7Saaron 	{"LGI8050", P_IMSERIAL},/* Logitech MouseMan+ */
147673a75b7Saaron 	{"LGI8051", P_IMSERIAL},/* Logitech FirstMouse+ */
148673a75b7Saaron 	{"LGI8001", P_LOGIMAN},	/* Logitech serial */
149673a75b7Saaron 	{"PNP0F01", P_MS},	/* MS serial */
150673a75b7Saaron 	/*
151673a75b7Saaron 	 * XXX EzScroll returns PNP0F04 in the compatible device field; but it
152673a75b7Saaron 	 * doesn't look compatible...
153673a75b7Saaron 	 */
154673a75b7Saaron 	{"PNP0F04", P_MSC},	/* MouseSystems */
155673a75b7Saaron 	{"PNP0F05", P_MSC},	/* MouseSystems */
156673a75b7Saaron 	{"PNP0F08", P_LOGIMAN},	/* Logitech serial */
157673a75b7Saaron 	{"PNP0F09", P_MS},	/* MS BallPoint serial */
158673a75b7Saaron 	{"PNP0F0A", P_MS},	/* MS PnP serial */
159673a75b7Saaron 	{"PNP0F0B", P_MS},	/* MS PnP BallPoint serial */
160673a75b7Saaron 	{"PNP0F0C", P_MS},	/* MS serial comatible */
161673a75b7Saaron 	{"PNP0F0F", P_MS},	/* MS BallPoint comatible */
162673a75b7Saaron 	{"PNP0F17", P_LOGIMAN},	/* Logitech serial compat */
163673a75b7Saaron 	{NULL, -1},
164673a75b7Saaron };
165673a75b7Saaron 
166be45c8afSmiod static const symtab_t *
gettoken(const symtab_t * tab,char * s,int len)167be45c8afSmiod gettoken(const symtab_t * tab, char *s, int len)
168673a75b7Saaron {
169673a75b7Saaron 	int	i;
170673a75b7Saaron 
171673a75b7Saaron 	for (i = 0; tab[i].name != NULL; ++i) {
172673a75b7Saaron 		if (strncmp(tab[i].name, s, len) == 0)
173673a75b7Saaron 			break;
174673a75b7Saaron 	}
175673a75b7Saaron 	return &tab[i];
176673a75b7Saaron }
177673a75b7Saaron 
178be45c8afSmiod const char *
mouse_name(int type)179673a75b7Saaron mouse_name(int type)
180673a75b7Saaron {
181be45c8afSmiod 	return (type < 0 ||
182be45c8afSmiod 	    (uint)type >= sizeof(mouse_names) / sizeof(mouse_names[0])) ?
183279fd192Sderaadt 	    "unknown" : mouse_names[type];
184673a75b7Saaron }
185673a75b7Saaron 
186b47703b0Smiod void
SetMouseSpeed(int old,unsigned int cflag)1871265a493Sshadchin SetMouseSpeed(int old, unsigned int cflag)
188673a75b7Saaron {
189673a75b7Saaron 	struct termios tty;
190673a75b7Saaron 	char	*c;
191673a75b7Saaron 
192df69c215Sderaadt 	if (tcgetattr(mouse.mfd, &tty) == -1) {
193673a75b7Saaron 		debug("Warning: %s unable to get status of mouse fd (%s)\n",
194673a75b7Saaron 		    mouse.portname, strerror(errno));
195673a75b7Saaron 		return;
196673a75b7Saaron 	}
197673a75b7Saaron 	tty.c_iflag = IGNBRK | IGNPAR;
198673a75b7Saaron 	tty.c_oflag = 0;
199673a75b7Saaron 	tty.c_lflag = 0;
200673a75b7Saaron 	tty.c_cflag = (tcflag_t) cflag;
201673a75b7Saaron 	tty.c_cc[VTIME] = 0;
202673a75b7Saaron 	tty.c_cc[VMIN] = 1;
203673a75b7Saaron 
204279fd192Sderaadt 	switch (old) {
205673a75b7Saaron 	case 9600:
206673a75b7Saaron 		cfsetispeed(&tty, B9600);
207673a75b7Saaron 		cfsetospeed(&tty, B9600);
208673a75b7Saaron 		break;
209673a75b7Saaron 	case 4800:
210673a75b7Saaron 		cfsetispeed(&tty, B4800);
211673a75b7Saaron 		cfsetospeed(&tty, B4800);
212673a75b7Saaron 		break;
213673a75b7Saaron 	case 2400:
214673a75b7Saaron 		cfsetispeed(&tty, B2400);
215673a75b7Saaron 		cfsetospeed(&tty, B2400);
216673a75b7Saaron 		break;
217673a75b7Saaron 	case 1200:
218673a75b7Saaron 	default:
219673a75b7Saaron 		cfsetispeed(&tty, B1200);
220673a75b7Saaron 		cfsetospeed(&tty, B1200);
221673a75b7Saaron 	}
222673a75b7Saaron 
223df69c215Sderaadt 	if (tcsetattr(mouse.mfd, TCSADRAIN, &tty) == -1)
224b47703b0Smiod 		logerr(1, "unable to get mouse status. Exiting...\n");
225673a75b7Saaron 
226673a75b7Saaron 	c = "*n";
227673a75b7Saaron 	cfsetispeed(&tty, B1200);
228673a75b7Saaron 	cfsetospeed(&tty, B1200);
229673a75b7Saaron 
230279fd192Sderaadt 	if (mouse.proto == P_LOGIMAN || mouse.proto == P_LOGI) {
231279fd192Sderaadt 		if (write(mouse.mfd, c, 2) != 2)
232b47703b0Smiod 			logerr(1, "unable to write to mouse. Exiting...\n");
233673a75b7Saaron 	}
234673a75b7Saaron 	usleep(100000);
235673a75b7Saaron 
236df69c215Sderaadt 	if (tcsetattr(mouse.mfd, TCSADRAIN, &tty) == -1)
237b47703b0Smiod 		logerr(1, "unable to get mouse status. Exiting...\n");
238673a75b7Saaron }
239673a75b7Saaron 
240673a75b7Saaron int
FlushInput(int fd)241673a75b7Saaron FlushInput(int fd)
242673a75b7Saaron {
243673a75b7Saaron 	struct pollfd pfd[1];
244673a75b7Saaron 	char	c[4];
245673a75b7Saaron 
246673a75b7Saaron 	if (tcflush(fd, TCIFLUSH) == 0)
247673a75b7Saaron 		return 0;
248673a75b7Saaron 
249673a75b7Saaron 	pfd[0].fd = fd;
250673a75b7Saaron 	pfd[0].events = POLLIN;
251673a75b7Saaron 
252673a75b7Saaron 	while (poll(pfd, 1, 0) > 0)
253673a75b7Saaron 		read(fd, &c, sizeof(c));
254673a75b7Saaron 	return 0;
255673a75b7Saaron }
256673a75b7Saaron 
257673a75b7Saaron /*
258673a75b7Saaron  * Try to elicit a PnP ID as described in
259673a75b7Saaron  * Microsoft, Hayes: "Plug and Play External COM Device Specification,
260673a75b7Saaron  * rev 1.00", 1995.
261673a75b7Saaron  *
262673a75b7Saaron  * The routine does not fully implement the COM Enumerator as par Section
263673a75b7Saaron  * 2.1 of the document.  In particular, we don't have idle state in which
264673a75b7Saaron  * the driver software monitors the com port for dynamic connection or
265673a75b7Saaron  * removal of a device at the port, because `moused' simply quits if no
266673a75b7Saaron  * device is found.
267673a75b7Saaron  *
268673a75b7Saaron  * In addition, as PnP COM device enumeration procedure slightly has
269673a75b7Saaron  * changed since its first publication, devices which follow earlier
270673a75b7Saaron  * revisions of the above spec. may fail to respond if the rev 1.0
271673a75b7Saaron  * procedure is used. XXX
272673a75b7Saaron  */
273673a75b7Saaron static int
pnpgets(int mouse_fd,char * buf)274673a75b7Saaron pnpgets(int mouse_fd, char *buf)
275673a75b7Saaron {
276673a75b7Saaron 	struct pollfd   pfd[1];
277673a75b7Saaron 	int	i;
278673a75b7Saaron 	char	c;
279673a75b7Saaron 
280673a75b7Saaron 	pfd[0].fd = mouse_fd;
281673a75b7Saaron 	pfd[0].events = POLLIN;
282673a75b7Saaron 
283673a75b7Saaron #if 0
284673a75b7Saaron 	/*
285673a75b7Saaron 	 * This is the procedure described in rev 1.0 of PnP COM device spec.
286673a75b7Saaron 	 * Unfortunately, some devices which comform to earlier revisions of
287673a75b7Saaron 	 * the spec gets confused and do not return the ID string...
288673a75b7Saaron 	 */
289673a75b7Saaron 
290673a75b7Saaron 	/* port initialization (2.1.2) */
291673a75b7Saaron 	ioctl(mouse_fd, TIOCMGET, &i);
292673a75b7Saaron 	i |= TIOCM_DTR;		/* DTR = 1 */
293673a75b7Saaron 	i &= ~TIOCM_RTS;	/* RTS = 0 */
294673a75b7Saaron 	ioctl(mouse_fd, TIOCMSET, &i);
295673a75b7Saaron 	usleep(200000);
296673a75b7Saaron 	if ((ioctl(mouse_fd, TIOCMGET, &i) == -1) || ((i & TIOCM_DSR) == 0))
297673a75b7Saaron 		goto disconnect_idle;
298673a75b7Saaron 
299673a75b7Saaron 	/* port setup, 1st phase (2.1.3) */
3001265a493Sshadchin 	SetMouseSpeed(1200, (CS7 | CREAD | CLOCAL | HUPCL));
301673a75b7Saaron 	i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 0, RTS = 0 */
302673a75b7Saaron 	ioctl(mouse_fd, TIOCMBIC, &i);
303673a75b7Saaron 	usleep(200000);
304673a75b7Saaron 	i = TIOCM_DTR;		/* DTR = 1, RTS = 0 */
305673a75b7Saaron 	ioctl(mouse_fd, TIOCMBIS, &i);
306673a75b7Saaron 	usleep(200000);
307673a75b7Saaron 
308673a75b7Saaron 	/* wait for response, 1st phase (2.1.4) */
309673a75b7Saaron 	FlushInput(mouse_fd);
310673a75b7Saaron 	i = TIOCM_RTS;		/* DTR = 1, RTS = 1 */
311673a75b7Saaron 	ioctl(mouse_fd, TIOCMBIS, &i);
312673a75b7Saaron 
313673a75b7Saaron 	/* try to read something */
314673a75b7Saaron 	if (poll(pfd, 1, 200000 / 1000) <= 0) {
315673a75b7Saaron 		/* port setup, 2nd phase (2.1.5) */
316673a75b7Saaron 		i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 0, RTS = 0 */
317673a75b7Saaron 		ioctl(mouse_fd, TIOCMBIC, &i);
318673a75b7Saaron 		usleep(200000);
319673a75b7Saaron 
320*3a50f0a9Sjmc 		/* wait for response, 2nd phase (2.1.6) */
321673a75b7Saaron 		FlushInput(mouse_fd);
322673a75b7Saaron 		i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 1, RTS = 1 */
323673a75b7Saaron 		ioctl(mouse_fd, TIOCMBIS, &i);
324673a75b7Saaron 
325673a75b7Saaron 		/* try to read something */
326673a75b7Saaron 		if (poll(pfd, 1, 200000 / 1000) <= 0)
327673a75b7Saaron 			goto connect_idle;
328673a75b7Saaron 	}
329673a75b7Saaron #else
330673a75b7Saaron 
331673a75b7Saaron 	/*
332673a75b7Saaron 	 * This is a simplified procedure; it simply toggles RTS.
333673a75b7Saaron 	 */
3341265a493Sshadchin 	SetMouseSpeed(1200, (CS7 | CREAD | CLOCAL | HUPCL));
335673a75b7Saaron 
336673a75b7Saaron 	ioctl(mouse_fd, TIOCMGET, &i);
337673a75b7Saaron 	i |= TIOCM_DTR;		/* DTR = 1 */
338673a75b7Saaron 	i &= ~TIOCM_RTS;	/* RTS = 0 */
339673a75b7Saaron 	ioctl(mouse_fd, TIOCMSET, &i);
340673a75b7Saaron 	usleep(200000);
341673a75b7Saaron 
342673a75b7Saaron 	/* wait for response */
343673a75b7Saaron 	FlushInput(mouse_fd);
344673a75b7Saaron 	i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 1, RTS = 1 */
345673a75b7Saaron 	ioctl(mouse_fd, TIOCMBIS, &i);
346673a75b7Saaron 
347673a75b7Saaron 	/* try to read something */
348673a75b7Saaron 	if (poll(pfd, 1, 200000 / 1000) <= 0)
349673a75b7Saaron 		goto connect_idle;
350673a75b7Saaron #endif
351673a75b7Saaron 
352673a75b7Saaron 	/* collect PnP COM device ID (2.1.7) */
353673a75b7Saaron 	i = 0;
354279fd192Sderaadt 	usleep(200000);		/* the mouse must send `Begin ID' within
355279fd192Sderaadt 				 * 200msec */
356673a75b7Saaron 	while (read(mouse_fd, &c, 1) == 1) {
357673a75b7Saaron 		/* we may see "M", or "M3..." before `Begin ID' */
358673a75b7Saaron 		if ((c == 0x08) || (c == 0x28)) {	/* Begin ID */
359673a75b7Saaron 			buf[i++] = c;
360673a75b7Saaron 			break;
361673a75b7Saaron 		}
362673a75b7Saaron 	}
363673a75b7Saaron 	if (i <= 0) {
364673a75b7Saaron 		/* we haven't seen `Begin ID' in time... */
365673a75b7Saaron 		goto connect_idle;
366673a75b7Saaron 	}
367673a75b7Saaron 	++c;			/* make it `End ID' */
368673a75b7Saaron 	for (;;) {
369673a75b7Saaron 		if (poll(pfd, 1, 200000 / 1000) <= 0)
370673a75b7Saaron 			break;
371673a75b7Saaron 
372673a75b7Saaron 		read(mouse_fd, &buf[i], 1);
373673a75b7Saaron 		if (buf[i++] == c)	/* End ID */
374673a75b7Saaron 			break;
375673a75b7Saaron 		if (i >= 256)
376673a75b7Saaron 			break;
377673a75b7Saaron 	}
378673a75b7Saaron 	if (buf[i - 1] != c)
379673a75b7Saaron 		goto connect_idle;
380673a75b7Saaron 	return i;
381673a75b7Saaron 
382673a75b7Saaron #if 0
383673a75b7Saaron 	/*
384673a75b7Saaron 	 * According to PnP spec, we should set DTR = 1 and RTS = 0 while
385673a75b7Saaron 	 * in idle state.  But, `moused' shall set DTR = RTS = 1 and proceed,
386673a75b7Saaron 	 * assuming there is something at the port even if it didn't
387673a75b7Saaron 	 * respond to the PnP enumeration procedure.
388673a75b7Saaron 	 */
389673a75b7Saaron disconnect_idle:
390673a75b7Saaron 	i = TIOCM_DTR | TIOCM_RTS;	/* DTR = 1, RTS = 1 */
391673a75b7Saaron 	ioctl(mouse_fd, TIOCMBIS, &i);
392673a75b7Saaron #endif
393673a75b7Saaron 
394673a75b7Saaron connect_idle:
395673a75b7Saaron 	return 0;
396673a75b7Saaron }
397673a75b7Saaron 
398673a75b7Saaron /* pnpparse : parse a PnP string ID */
399673a75b7Saaron static int
pnpparse(pnpid_t * id,char * buf,int len)400673a75b7Saaron pnpparse(pnpid_t * id, char *buf, int len)
401673a75b7Saaron {
402673a75b7Saaron 	char	s[3];
403279fd192Sderaadt 	int	offset, sum = 0, i, j;
404673a75b7Saaron 
405673a75b7Saaron 	id->revision = 0;
406673a75b7Saaron 	id->eisaid = NULL;
407673a75b7Saaron 	id->serial = NULL;
408673a75b7Saaron 	id->class = NULL;
409673a75b7Saaron 	id->compat = NULL;
410673a75b7Saaron 	id->description = NULL;
411673a75b7Saaron 	id->neisaid = 0;
412673a75b7Saaron 	id->nserial = 0;
413673a75b7Saaron 	id->nclass = 0;
414673a75b7Saaron 	id->ncompat = 0;
415673a75b7Saaron 	id->ndescription = 0;
416673a75b7Saaron 
417673a75b7Saaron 	offset = 0x28 - buf[0];
418673a75b7Saaron 
419673a75b7Saaron 	/* calculate checksum */
420673a75b7Saaron 	for (i = 0; i < len - 3; ++i) {
421673a75b7Saaron 		sum += buf[i];
422673a75b7Saaron 		buf[i] += offset;
423673a75b7Saaron 	}
424673a75b7Saaron 	sum += buf[len - 1];
425673a75b7Saaron 	for (; i < len; ++i)
426673a75b7Saaron 		buf[i] += offset;
427673a75b7Saaron 	debug("Mouse: PnP ID string: '%*.*s'\n", len, len, buf);
428673a75b7Saaron 
429673a75b7Saaron 	/* revision */
430673a75b7Saaron 	buf[1] -= offset;
431673a75b7Saaron 	buf[2] -= offset;
432673a75b7Saaron 	id->revision = ((buf[1] & 0x3f) << 6) | (buf[2] & 0x3f);
433673a75b7Saaron 	debug("Mouse: PnP rev %d.%02d\n", id->revision / 100, id->revision % 100);
434673a75b7Saaron 
435be45c8afSmiod 	/* EISA vendor and product ID */
436673a75b7Saaron 	id->eisaid = &buf[3];
437673a75b7Saaron 	id->neisaid = 7;
438673a75b7Saaron 
439673a75b7Saaron 	/* option strings */
440673a75b7Saaron 	i = 10;
441673a75b7Saaron 	if (buf[i] == '\\') {
442673a75b7Saaron 		/* device serial # */
443673a75b7Saaron 		for (j = ++i; i < len; ++i) {
444673a75b7Saaron 			if (buf[i] == '\\')
445673a75b7Saaron 				break;
446673a75b7Saaron 		}
447673a75b7Saaron 		if (i >= len)
448673a75b7Saaron 			i -= 3;
449673a75b7Saaron 		if (i - j == 8) {
450673a75b7Saaron 			id->serial = &buf[j];
451673a75b7Saaron 			id->nserial = 8;
452673a75b7Saaron 		}
453673a75b7Saaron 	}
454673a75b7Saaron 	if (buf[i] == '\\') {
455673a75b7Saaron 		/* PnP class */
456673a75b7Saaron 		for (j = ++i; i < len; ++i) {
457673a75b7Saaron 			if (buf[i] == '\\')
458673a75b7Saaron 				break;
459673a75b7Saaron 		}
460673a75b7Saaron 		if (i >= len)
461673a75b7Saaron 			i -= 3;
462673a75b7Saaron 		if (i > j + 1) {
463673a75b7Saaron 			id->class = &buf[j];
464673a75b7Saaron 			id->nclass = i - j;
465673a75b7Saaron 		}
466673a75b7Saaron 	}
467673a75b7Saaron 	if (buf[i] == '\\') {
468673a75b7Saaron 		/* compatible driver */
469673a75b7Saaron 		for (j = ++i; i < len; ++i) {
470673a75b7Saaron 			if (buf[i] == '\\')
471673a75b7Saaron 				break;
472673a75b7Saaron 		}
473673a75b7Saaron 		/*
474673a75b7Saaron 		 * PnP COM spec prior to v0.96 allowed '*' in this field,
475673a75b7Saaron 		 * it's not allowed now; just ignore it.
476673a75b7Saaron 		 */
477673a75b7Saaron 		if (buf[j] == '*')
478673a75b7Saaron 			++j;
479673a75b7Saaron 		if (i >= len)
480673a75b7Saaron 			i -= 3;
481673a75b7Saaron 		if (i > j + 1) {
482673a75b7Saaron 			id->compat = &buf[j];
483673a75b7Saaron 			id->ncompat = i - j;
484673a75b7Saaron 		}
485673a75b7Saaron 	}
486673a75b7Saaron 	if (buf[i] == '\\') {
487673a75b7Saaron 		/* product description */
488673a75b7Saaron 		for (j = ++i; i < len; ++i) {
489673a75b7Saaron 			if (buf[i] == ';')
490673a75b7Saaron 				break;
491673a75b7Saaron 		}
492673a75b7Saaron 		if (i >= len)
493673a75b7Saaron 			i -= 3;
494673a75b7Saaron 		if (i > j + 1) {
495673a75b7Saaron 			id->description = &buf[j];
496673a75b7Saaron 			id->ndescription = i - j;
497673a75b7Saaron 		}
498673a75b7Saaron 	}
499673a75b7Saaron 	/* checksum exists if there are any optional fields */
500279fd192Sderaadt 	if ((id->nserial > 0) || (id->nclass > 0) ||
501279fd192Sderaadt 	    (id->ncompat > 0) || (id->ndescription > 0)) {
502673a75b7Saaron #if 0
503673a75b7Saaron 		debug("Mouse: PnP checksum: 0x%02X\n", sum);
504673a75b7Saaron #endif
505cdd39624Sderaadt 		snprintf(s, sizeof s, "%02X", sum & 0x0ff);
506673a75b7Saaron 		if (strncmp(s, &buf[len - 3], 2) != 0) {
507673a75b7Saaron #if 0
508673a75b7Saaron 			/*
509673a75b7Saaron 			 * Checksum error!!
510673a75b7Saaron 			 * I found some mice do not comply with the PnP COM device
511673a75b7Saaron 			 * spec regarding checksum... XXX
512673a75b7Saaron 			 */
513673a75b7Saaron 			return FALSE;
514673a75b7Saaron #endif
515673a75b7Saaron 		}
516673a75b7Saaron 	}
517673a75b7Saaron 	return 1;
518673a75b7Saaron }
519673a75b7Saaron 
520673a75b7Saaron /* pnpproto : return the prototype used, based on the PnP ID string */
521be45c8afSmiod static const symtab_t *
pnpproto(pnpid_t * id)522673a75b7Saaron pnpproto(pnpid_t * id)
523673a75b7Saaron {
524be45c8afSmiod 	const symtab_t *t;
525673a75b7Saaron 	int i, j;
526673a75b7Saaron 
527673a75b7Saaron 	if (id->nclass > 0)
528673a75b7Saaron 		if (strncmp(id->class, "MOUSE", id->nclass) != 0)
529673a75b7Saaron 			/* this is not a mouse! */
530673a75b7Saaron 			return NULL;
531673a75b7Saaron 
532673a75b7Saaron 	if (id->neisaid > 0) {
533673a75b7Saaron 		t = gettoken(pnpprod, id->eisaid, id->neisaid);
534673a75b7Saaron 		if (t->val != -1)
535673a75b7Saaron 			return t;
536673a75b7Saaron 	}
537673a75b7Saaron 	/*
538673a75b7Saaron 	 * The 'Compatible drivers' field may contain more than one
539673a75b7Saaron 	 * ID separated by ','.
540673a75b7Saaron 	 */
541673a75b7Saaron 	if (id->ncompat <= 0)
542673a75b7Saaron 		return NULL;
543673a75b7Saaron 	for (i = 0; i < id->ncompat; ++i) {
544673a75b7Saaron 		for (j = i; id->compat[i] != ','; ++i)
545673a75b7Saaron 			if (i >= id->ncompat)
546673a75b7Saaron 				break;
547673a75b7Saaron 		if (i > j) {
548673a75b7Saaron 			t = gettoken(pnpprod, id->compat + j, i - j);
549673a75b7Saaron 			if (t->val != -1)
550673a75b7Saaron 				return t;
551673a75b7Saaron 		}
552673a75b7Saaron 	}
553673a75b7Saaron 
554673a75b7Saaron 	return NULL;
555673a75b7Saaron }
556673a75b7Saaron 
557673a75b7Saaron /* mouse_init : init the mouse by writing appropriate sequences */
558673a75b7Saaron void
mouse_init(void)559673a75b7Saaron mouse_init(void)
560673a75b7Saaron {
561673a75b7Saaron 	struct pollfd   pfd[1];
562673a75b7Saaron 	char           *s;
563673a75b7Saaron 	char            c;
564673a75b7Saaron 	int             i;
565673a75b7Saaron 
566673a75b7Saaron 	pfd[0].fd = mouse.mfd;
567673a75b7Saaron 	pfd[0].events = POLLIN;
568673a75b7Saaron 
569673a75b7Saaron 	/**
570673a75b7Saaron 	 ** This comment is a little out of context here, but it contains
571673a75b7Saaron 	 ** some useful information...
572673a75b7Saaron 	 ********************************************************************
573673a75b7Saaron 	 **
574673a75b7Saaron 	 ** The following lines take care of the Logitech MouseMan protocols.
575673a75b7Saaron 	 **
576*3a50f0a9Sjmc 	 ** NOTE: There are different versions of both MouseMan and TrackMan!
577673a75b7Saaron 	 **       Hence I add another protocol P_LOGIMAN, which the user can
578673a75b7Saaron 	 **       specify as MouseMan in his XF86Config file. This entry was
579673a75b7Saaron 	 **       formerly handled as a special case of P_MS. However, people
580673a75b7Saaron 	 **       who don't have the middle button problem, can still specify
581673a75b7Saaron 	 **       Microsoft and use P_MS.
582673a75b7Saaron 	 **
583673a75b7Saaron 	 ** By default, these mice should use a 3 byte Microsoft protocol
584673a75b7Saaron 	 ** plus a 4th byte for the middle button. However, the mouse might
585673a75b7Saaron 	 ** have switched to a different protocol before we use it, so I send
586673a75b7Saaron 	 ** the proper sequence just in case.
587673a75b7Saaron 	 **
588673a75b7Saaron 	 ** NOTE: - all commands to (at least the European) MouseMan have to
589673a75b7Saaron 	 **	 be sent at 1200 Baud.
590673a75b7Saaron 	 **       - each command starts with a '*'.
591673a75b7Saaron 	 **       - whenever the MouseMan receives a '*', it will switch back
592673a75b7Saaron 	 **	 to 1200 Baud. Hence I have to select the desired protocol
593673a75b7Saaron 	 **	 first, then select the baud rate.
594673a75b7Saaron 	 **
595673a75b7Saaron 	 ** The protocols supported by the (European) MouseMan are:
596673a75b7Saaron 	 **   -  5 byte packed binary protocol, as with the Mouse Systems
597673a75b7Saaron 	 **      mouse. Selected by sequence "*U".
5989523400bSjsg 	 **   -  2 button 3 byte Microsoft compatible protocol. Selected
599673a75b7Saaron 	 **      by sequence "*V".
6009523400bSjsg 	 **   -  3 button 3+1 byte Microsoft compatible protocol (default).
601673a75b7Saaron 	 **      Selected by sequence "*X".
602673a75b7Saaron 	 **
603673a75b7Saaron 	 ** The following baud rates are supported:
604673a75b7Saaron 	 **   -  1200 Baud (default). Selected by sequence "*n".
605673a75b7Saaron 	 **   -  9600 Baud. Selected by sequence "*q".
606673a75b7Saaron 	 **
607673a75b7Saaron 	 ** Selecting a sample rate is no longer supported with the MouseMan!
608673a75b7Saaron 	 ** Some additional lines in xf86Config.c take care of ill configured
609673a75b7Saaron 	 ** baud rates and sample rates. (The user will get an error.)
610673a75b7Saaron 	 */
611673a75b7Saaron 
612673a75b7Saaron 	switch (mouse.proto) {
613673a75b7Saaron 
614673a75b7Saaron 	case P_LOGI:
615673a75b7Saaron 		/*
616673a75b7Saaron 		 * The baud rate selection command must be sent at the current
617673a75b7Saaron 		 * baud rate; try all likely settings
618673a75b7Saaron 		 */
6191265a493Sshadchin 		SetMouseSpeed(9600, mousecflags[mouse.proto]);
6201265a493Sshadchin 		SetMouseSpeed(4800, mousecflags[mouse.proto]);
6211265a493Sshadchin 		SetMouseSpeed(2400, mousecflags[mouse.proto]);
622ea4362f1Sderaadt #if 0
6231265a493Sshadchin 		SetMouseSpeed(1200, mousecflags[mouse.proto]);
624ea4362f1Sderaadt #endif
625673a75b7Saaron 		/* select MM series data format */
626673a75b7Saaron 		write(mouse.mfd, "S", 1);
6271265a493Sshadchin 		SetMouseSpeed(1200, mousecflags[P_MM]);
628673a75b7Saaron 		/* select report rate/frequency */
629279fd192Sderaadt 		if (mouse.rate <= 0)
630279fd192Sderaadt 			write(mouse.mfd, "O", 1);
631279fd192Sderaadt 		else if (mouse.rate <= 15)
632279fd192Sderaadt 			write(mouse.mfd, "J", 1);
633279fd192Sderaadt 		else if (mouse.rate <= 27)
634279fd192Sderaadt 			write(mouse.mfd, "K", 1);
635279fd192Sderaadt 		else if (mouse.rate <= 42)
636279fd192Sderaadt 			write(mouse.mfd, "L", 1);
637279fd192Sderaadt 		else if (mouse.rate <= 60)
638279fd192Sderaadt 			write(mouse.mfd, "R", 1);
639279fd192Sderaadt 		else if (mouse.rate <= 85)
640279fd192Sderaadt 			write(mouse.mfd, "M", 1);
641279fd192Sderaadt 		else if (mouse.rate <= 125)
642279fd192Sderaadt 			write(mouse.mfd, "Q", 1);
643279fd192Sderaadt 		else
644279fd192Sderaadt 			write(mouse.mfd, "N", 1);
645673a75b7Saaron 		break;
646673a75b7Saaron 
647673a75b7Saaron 	case P_LOGIMAN:
648673a75b7Saaron 		/* The command must always be sent at 1200 baud */
6491265a493Sshadchin 		SetMouseSpeed(1200, mousecflags[mouse.proto]);
650673a75b7Saaron 		write(mouse.mfd, "*X", 2);
6511265a493Sshadchin 		SetMouseSpeed(1200, mousecflags[mouse.proto]);
652673a75b7Saaron 		break;
653673a75b7Saaron 
654673a75b7Saaron 	case P_MMHIT:
6551265a493Sshadchin 		SetMouseSpeed(1200, mousecflags[mouse.proto]);
656673a75b7Saaron 
657673a75b7Saaron 		/*
658673a75b7Saaron 		 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings.
659673a75b7Saaron 		 * The tablet must be configured to be in MM mode, NO parity,
660673a75b7Saaron 		 * Binary Format.  xf86Info.sampleRate controls the sensativity
6616668a736Sguenther 		 * of the tablet.  We only use this tablet for its 4-button puck
662673a75b7Saaron 		 * so we don't run in "Absolute Mode"
663673a75b7Saaron 		 */
664673a75b7Saaron 		write(mouse.mfd, "z8", 2);	/* Set Parity = "NONE" */
665673a75b7Saaron 		usleep(50000);
666673a75b7Saaron 		write(mouse.mfd, "zb", 2);	/* Set Format = "Binary" */
667673a75b7Saaron 		usleep(50000);
668673a75b7Saaron 		write(mouse.mfd, "@", 1);	/* Set Report Mode = "Stream" */
669673a75b7Saaron 		usleep(50000);
670673a75b7Saaron 		write(mouse.mfd, "R", 1);	/* Set Output Rate = "45 rps" */
671673a75b7Saaron 		usleep(50000);
672673a75b7Saaron 		write(mouse.mfd, "I\x20", 2);	/* Set Incrememtal Mode "20" */
673673a75b7Saaron 		usleep(50000);
674673a75b7Saaron 		write(mouse.mfd, "E", 1);	/* Set Data Type = "Relative */
675673a75b7Saaron 		usleep(50000);
676673a75b7Saaron 
677673a75b7Saaron 		/* Resolution is in 'lines per inch' on the Hitachi tablet */
678279fd192Sderaadt 		if (mouse.resolution == MOUSE_RES_LOW)
679279fd192Sderaadt 			c = 'g';
680279fd192Sderaadt 		else if (mouse.resolution == MOUSE_RES_MEDIUMLOW)
681279fd192Sderaadt 			c = 'e';
682279fd192Sderaadt 		else if (mouse.resolution == MOUSE_RES_MEDIUMHIGH)
683279fd192Sderaadt 			c = 'h';
684279fd192Sderaadt 		else if (mouse.resolution == MOUSE_RES_HIGH)
685279fd192Sderaadt 			c = 'd';
686279fd192Sderaadt 		else if (mouse.resolution <= 40)
687279fd192Sderaadt 			c = 'g';
688279fd192Sderaadt 		else if (mouse.resolution <= 100)
689279fd192Sderaadt 			c = 'd';
690279fd192Sderaadt 		else if (mouse.resolution <= 200)
691279fd192Sderaadt 			c = 'e';
692279fd192Sderaadt 		else if (mouse.resolution <= 500)
693279fd192Sderaadt 			c = 'h';
694279fd192Sderaadt 		else if (mouse.resolution <= 1000)
695279fd192Sderaadt 			c = 'j';
696279fd192Sderaadt 		else
697279fd192Sderaadt 			c = 'd';
698673a75b7Saaron 		write(mouse.mfd, &c, 1);
699673a75b7Saaron 		usleep(50000);
700673a75b7Saaron 
701673a75b7Saaron 		write(mouse.mfd, "\021", 1);	/* Resume DATA output */
702673a75b7Saaron 		break;
703673a75b7Saaron 
704673a75b7Saaron 	case P_THINKING:
7051265a493Sshadchin 		SetMouseSpeed(1200, mousecflags[mouse.proto]);
706673a75b7Saaron 		/* the PnP ID string may be sent again, discard it */
707673a75b7Saaron 		usleep(200000);
708673a75b7Saaron 		i = FREAD;
709673a75b7Saaron 		ioctl(mouse.mfd, TIOCFLUSH, &i);
710673a75b7Saaron 		/* send the command to initialize the beast */
711673a75b7Saaron 		for (s = "E5E5"; *s; ++s) {
712673a75b7Saaron 			write(mouse.mfd, s, 1);
713673a75b7Saaron 
71429f11fc7Sfgsch 			if (poll(pfd, 1, INFTIM) <= 0)
715673a75b7Saaron 				break;
716673a75b7Saaron 			read(mouse.mfd, &c, 1);
717673a75b7Saaron 			debug("%c", c);
718673a75b7Saaron 			if (c != *s)
719673a75b7Saaron 				break;
720673a75b7Saaron 		}
721673a75b7Saaron 		break;
722673a75b7Saaron 
723673a75b7Saaron 	case P_MSC:
7241265a493Sshadchin 		SetMouseSpeed(1200, mousecflags[mouse.proto]);
725673a75b7Saaron #if 0
726673a75b7Saaron 		if (mouse.flags & ClearDTR) {
727673a75b7Saaron 			i = TIOCM_DTR;
728673a75b7Saaron 			ioctl(mouse.mfd, TIOCMBIC, &i);
729673a75b7Saaron 		}
730673a75b7Saaron 		if (mouse.flags & ClearRTS) {
731673a75b7Saaron 			i = TIOCM_RTS;
732673a75b7Saaron 			ioctl(mouse.mfd, TIOCMBIC, &i);
733673a75b7Saaron 		}
734673a75b7Saaron #endif
735673a75b7Saaron 		break;
736673a75b7Saaron 
737673a75b7Saaron 	default:
7381265a493Sshadchin 		SetMouseSpeed(1200, mousecflags[mouse.proto]);
739673a75b7Saaron 		break;
740673a75b7Saaron 	}
741673a75b7Saaron }
742673a75b7Saaron 
743673a75b7Saaron /* mouse_identify : identify the protocol used by the mouse */
744673a75b7Saaron int
mouse_identify(void)745673a75b7Saaron mouse_identify(void)
746673a75b7Saaron {
747279fd192Sderaadt 	char pnpbuf[256];	/* PnP identifier string may be up to
748279fd192Sderaadt 				 * 256 bytes long */
749673a75b7Saaron 	pnpid_t pnpid;
750be45c8afSmiod 	const symtab_t *t;
751673a75b7Saaron 	int len;
752673a75b7Saaron 
753673a75b7Saaron 	/* protocol has been specified with '-t' */
754673a75b7Saaron 	if (mouse.proto != P_UNKNOWN)
755673a75b7Saaron 		bcopy(proto[mouse.proto], cur_proto, sizeof(cur_proto));
756673a75b7Saaron 	else {
757673a75b7Saaron 		/* maybe this is an PnP mouse... */
758673a75b7Saaron 		if (mouse.flags & NoPnP)
759673a75b7Saaron 			return mouse.proto;
760673a75b7Saaron 		if (((len = pnpgets(mouse.mfd, pnpbuf)) <= 0)
761673a75b7Saaron 		    || !pnpparse(&pnpid, pnpbuf, len))
762673a75b7Saaron 			return mouse.proto;
763673a75b7Saaron 
764673a75b7Saaron 		debug("PnP serial mouse: '%*.*s' '%*.*s' '%*.*s'",
765673a75b7Saaron 		      pnpid.neisaid, pnpid.neisaid,
766673a75b7Saaron 		      pnpid.eisaid, pnpid.ncompat,
767673a75b7Saaron 		      pnpid.ncompat, pnpid.compat,
768673a75b7Saaron 		      pnpid.ndescription, pnpid.ndescription,
769673a75b7Saaron 		      pnpid.description);
770673a75b7Saaron 
771673a75b7Saaron 		/* we have a valid PnP serial device ID */
772673a75b7Saaron 		t = pnpproto(&pnpid);
773673a75b7Saaron 		if (t != NULL) {
774673a75b7Saaron 			mouse.proto = t->val;
775673a75b7Saaron 			bcopy(proto[mouse.proto], cur_proto, sizeof(cur_proto));
776279fd192Sderaadt 		} else
777673a75b7Saaron 			mouse.proto = P_UNKNOWN;
778673a75b7Saaron 
779673a75b7Saaron 	}
780673a75b7Saaron 
781673a75b7Saaron 	debug("proto params: %02x %02x %02x %02x %d %02x %02x",
782673a75b7Saaron 	      cur_proto[0], cur_proto[1], cur_proto[2], cur_proto[3],
783673a75b7Saaron 	      cur_proto[4], cur_proto[5], cur_proto[6]);
784673a75b7Saaron 
785673a75b7Saaron 	return mouse.proto;
786673a75b7Saaron }
787673a75b7Saaron 
788673a75b7Saaron /* mouse_protocol : decode bytes with the current mouse protocol */
789673a75b7Saaron int
mouse_protocol(u_char rBuf,mousestatus_t * act)790673a75b7Saaron mouse_protocol(u_char rBuf, mousestatus_t * act)
791673a75b7Saaron {
792673a75b7Saaron 	/* MOUSE_MSS_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
793279fd192Sderaadt 	static int      butmapmss[4] = {	/* Microsoft, MouseMan,
794279fd192Sderaadt 						 * GlidePoint, IntelliMouse,
795279fd192Sderaadt 						 * Thinking Mouse */
796673a75b7Saaron 		0,
797673a75b7Saaron 		MOUSE_BUTTON3DOWN,
798673a75b7Saaron 		MOUSE_BUTTON1DOWN,
799673a75b7Saaron 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
800673a75b7Saaron 	};
801279fd192Sderaadt 	static int      butmapmss2[4] = {	/* Microsoft, MouseMan,
802279fd192Sderaadt 						 * GlidePoint, Thinking Mouse */
803673a75b7Saaron 		0,
804673a75b7Saaron 		MOUSE_BUTTON4DOWN,
805673a75b7Saaron 		MOUSE_BUTTON2DOWN,
806673a75b7Saaron 		MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
807673a75b7Saaron 	};
808673a75b7Saaron 	/* MOUSE_INTELLI_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */
809279fd192Sderaadt 	static int      butmapintelli[4] = {	/* IntelliMouse, NetMouse,
810279fd192Sderaadt 						 * Mie Mouse, MouseMan+ */
811673a75b7Saaron 		0,
812673a75b7Saaron 		MOUSE_BUTTON2DOWN,
813673a75b7Saaron 		MOUSE_BUTTON4DOWN,
814673a75b7Saaron 		MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN,
815673a75b7Saaron 	};
816673a75b7Saaron 	/* MOUSE_MSC_BUTTON?UP -> MOUSE_BUTTON?DOWN */
817279fd192Sderaadt 	static int      butmapmsc[8] = {	/* MouseSystems, MMSeries,
818279fd192Sderaadt 						 * Logitech, Bus, sysmouse */
819673a75b7Saaron 		0,
820673a75b7Saaron 		MOUSE_BUTTON3DOWN,
821673a75b7Saaron 		MOUSE_BUTTON2DOWN,
822673a75b7Saaron 		MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
823673a75b7Saaron 		MOUSE_BUTTON1DOWN,
824673a75b7Saaron 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
825673a75b7Saaron 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
826673a75b7Saaron 		MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
827673a75b7Saaron 	};
828673a75b7Saaron 	/* for Hitachi tablet */
829673a75b7Saaron 	static int      butmaphit[8] = {	/* MM HitTablet */
830673a75b7Saaron 		0,
831673a75b7Saaron 		MOUSE_BUTTON3DOWN,
832673a75b7Saaron 		MOUSE_BUTTON2DOWN,
833673a75b7Saaron 		MOUSE_BUTTON1DOWN,
834673a75b7Saaron 		MOUSE_BUTTON4DOWN,
835673a75b7Saaron 		MOUSE_BUTTON5DOWN,
836673a75b7Saaron 		MOUSE_BUTTON6DOWN,
837673a75b7Saaron 		MOUSE_BUTTON7DOWN,
838673a75b7Saaron 	};
839673a75b7Saaron 	static int      pBufP = 0;
840673a75b7Saaron 	static unsigned char pBuf[8];
841673a75b7Saaron 
842673a75b7Saaron 	debug("received char 0x%x", (int) rBuf);
843673a75b7Saaron 
844673a75b7Saaron 	/*
845673a75b7Saaron          * Hack for resyncing: We check here for a package that is:
846673a75b7Saaron          *  a) illegal (detected by wrong data-package header)
847673a75b7Saaron          *  b) invalid (0x80 == -128 and that might be wrong for MouseSystems)
848673a75b7Saaron          *  c) bad header-package
849673a75b7Saaron          *
850be45c8afSmiod          * NOTE: b) is a violation of the MouseSystems-Protocol, since values of
851673a75b7Saaron          *       -128 are allowed, but since they are very seldom we can easily
852673a75b7Saaron          *       use them as package-header with no button pressed.
853673a75b7Saaron          * NOTE/2: On a PS/2 mouse any byte is valid as a data byte. Furthermore,
854673a75b7Saaron          *         0x80 is not valid as a header byte. For a PS/2 mouse we skip
855673a75b7Saaron          *         checking data bytes.
856673a75b7Saaron          *         For resyncing a PS/2 mouse we require the two most significant
857673a75b7Saaron          *         bits in the header byte to be 0. These are the overflow bits,
858673a75b7Saaron          *         and in case of an overflow we actually lose sync. Overflows
859673a75b7Saaron          *         are very rare, however, and we quickly gain sync again after
860673a75b7Saaron          *         an overflow condition. This is the best we can do. (Actually,
861673a75b7Saaron          *         we could use bit 0x08 in the header byte for resyncing, since
862673a75b7Saaron          *         that bit is supposed to be always on, but nobody told
863673a75b7Saaron          *         Microsoft...)
864673a75b7Saaron          */
865673a75b7Saaron 
866279fd192Sderaadt 	if (pBufP != 0 && ((rBuf & cur_proto[2]) != cur_proto[3] || rBuf == 0x80)) {
867673a75b7Saaron 		pBufP = 0;	/* skip package */
868673a75b7Saaron 	}
869673a75b7Saaron 	if (pBufP == 0 && (rBuf & cur_proto[0]) != cur_proto[1])
870673a75b7Saaron 		return 0;
871673a75b7Saaron 
872673a75b7Saaron 	/* is there an extra data byte? */
873279fd192Sderaadt 	if (pBufP >= cur_proto[4] && (rBuf & cur_proto[0]) != cur_proto[1]) {
874673a75b7Saaron 		/*
875673a75b7Saaron 		 * Hack for Logitech MouseMan Mouse - Middle button
876673a75b7Saaron 		 *
877673a75b7Saaron 		 * Unfortunately this mouse has variable length packets: the standard
878673a75b7Saaron 		 * Microsoft 3 byte packet plus an optional 4th byte whenever the
879673a75b7Saaron 		 * middle button status changes.
880673a75b7Saaron 		 *
881673a75b7Saaron 		 * We have already processed the standard packet with the movement
882673a75b7Saaron 		 * and button info.  Now post an event message with the old status
883673a75b7Saaron 		 * of the left and right buttons and the updated middle button.
884673a75b7Saaron 		 */
885673a75b7Saaron 
886673a75b7Saaron 		/*
887673a75b7Saaron 		 * Even worse, different MouseMen and TrackMen differ in the 4th
888673a75b7Saaron 		 * byte: some will send 0x00/0x20, others 0x01/0x21, or even
889673a75b7Saaron 		 * 0x02/0x22, so I have to strip off the lower bits.
890673a75b7Saaron 	         *
891673a75b7Saaron 	         * [JCH-96/01/21]
892673a75b7Saaron 	         * HACK for ALPS "fourth button". (It's bit 0x10 of the "fourth byte"
893673a75b7Saaron 	         * and it is activated by tapping the glidepad with the finger! 8^)
894673a75b7Saaron 	         * We map it to bit bit3, and the reverse map in xf86Events just has
895673a75b7Saaron 	         * to be extended so that it is identified as Button 4. The lower
896673a75b7Saaron 	         * half of the reverse-map may remain unchanged.
897673a75b7Saaron 		 */
898673a75b7Saaron 
899673a75b7Saaron 		/*
900673a75b7Saaron 		 * [KY-97/08/03]
901524c90a2Sjsg 		 * Receive the fourth byte only when preceding three bytes have
902673a75b7Saaron 		 * been detected (pBufP >= cur_proto[4]).  In the previous
903673a75b7Saaron 		 * versions, the test was pBufP == 0; thus, we may have mistakingly
904524c90a2Sjsg 		 * received a byte even if we didn't see anything preceding
905673a75b7Saaron 		 * the byte.
906673a75b7Saaron 		 */
907673a75b7Saaron 
908673a75b7Saaron 		if ((rBuf & cur_proto[5]) != cur_proto[6]) {
909673a75b7Saaron 			pBufP = 0;
910673a75b7Saaron 			return 0;
911673a75b7Saaron 		}
912673a75b7Saaron 		switch (mouse.proto) {
913673a75b7Saaron 
914673a75b7Saaron 			/*
915673a75b7Saaron 			 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie Mouse
916673a75b7Saaron 			 * always send the fourth byte, whereas the fourth byte is
917673a75b7Saaron 			 * optional for GlidePoint and ThinkingMouse. The fourth byte
918673a75b7Saaron 			 * is also optional for MouseMan+ and FirstMouse+ in their
919673a75b7Saaron 			 * native mode. It is always sent if they are in the IntelliMouse
920673a75b7Saaron 			 * compatible mode.
921673a75b7Saaron 			 */
922673a75b7Saaron 		case P_IMSERIAL:	/* IntelliMouse, NetMouse, Mie Mouse,
923279fd192Sderaadt 					 * MouseMan+ */
924673a75b7Saaron 			act->dx = act->dy = 0;
925673a75b7Saaron 			act->dz = (rBuf & 0x08) ? (rBuf & 0x0f) - 16 : (rBuf & 0x0f);
926673a75b7Saaron 			act->obutton = act->button;
927673a75b7Saaron 			act->button = butmapintelli[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
928673a75b7Saaron 				| (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
929673a75b7Saaron 			break;
930673a75b7Saaron 
931673a75b7Saaron 		default:
9322fc38322Smiod 			act->dx = act->dy = act->dz = act->dw = 0;
933673a75b7Saaron 			act->obutton = act->button;
934673a75b7Saaron 			act->button = butmapmss2[(rBuf & MOUSE_MSS_BUTTONS) >> 4]
935673a75b7Saaron 				| (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN));
936673a75b7Saaron 			break;
937673a75b7Saaron 		}
938673a75b7Saaron 
9392fc38322Smiod 		act->flags = ((act->dx || act->dy || act->dz || act->dw) ?
9402fc38322Smiod 		    MOUSE_POSCHANGED : 0) | (act->obutton ^ act->button);
941673a75b7Saaron 		pBufP = 0;
942673a75b7Saaron 		return act->flags;
943673a75b7Saaron 	}
944673a75b7Saaron 	if (pBufP >= cur_proto[4])
945673a75b7Saaron 		pBufP = 0;
946673a75b7Saaron 	pBuf[pBufP++] = rBuf;
947673a75b7Saaron 	if (pBufP != cur_proto[4])
948673a75b7Saaron 		return 0;
949673a75b7Saaron 
950673a75b7Saaron 	/*
951673a75b7Saaron          * assembly full package
952673a75b7Saaron          */
953673a75b7Saaron 
954673a75b7Saaron 	debug("assembled full packet (len %d) %x,%x,%x,%x,%x,%x,%x,%x",
955673a75b7Saaron 	    cur_proto[4],
956673a75b7Saaron 	    pBuf[0], pBuf[1], pBuf[2], pBuf[3],
957673a75b7Saaron 	    pBuf[4], pBuf[5], pBuf[6], pBuf[7]);
958673a75b7Saaron 
959673a75b7Saaron 	act->dz = 0;
9602fc38322Smiod 	act->dw = 0;
961673a75b7Saaron 	act->obutton = act->button;
962279fd192Sderaadt 	switch (mouse.proto) {
963673a75b7Saaron 	case P_MS:		/* Microsoft */
964673a75b7Saaron 	case P_LOGIMAN:	/* MouseMan/TrackMan */
965673a75b7Saaron 	case P_GLIDEPOINT:	/* GlidePoint */
966673a75b7Saaron 	case P_THINKING:	/* ThinkingMouse */
967673a75b7Saaron 	case P_IMSERIAL:	/* IntelliMouse, NetMouse, Mie Mouse,
968279fd192Sderaadt 				 * MouseMan+ */
969673a75b7Saaron 		act->button = (act->obutton & (MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN))
970673a75b7Saaron 		    | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4];
971673a75b7Saaron 		act->dx = (char) (((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
972673a75b7Saaron 		act->dy = (char) (((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
973673a75b7Saaron 		break;
974673a75b7Saaron 
975673a75b7Saaron 	case P_MSC:		/* MouseSystems Corp */
976673a75b7Saaron 		act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS];
977673a75b7Saaron 		act->dx = (char) (pBuf[1]) + (char) (pBuf[3]);
978673a75b7Saaron 		act->dy = -((char) (pBuf[2]) + (char) (pBuf[4]));
979673a75b7Saaron 		break;
980673a75b7Saaron 
981673a75b7Saaron 	case P_MMHIT:		/* MM HitTablet */
982673a75b7Saaron 		act->button = butmaphit[pBuf[0] & 0x07];
983673a75b7Saaron 		act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : -pBuf[1];
984673a75b7Saaron 		act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? -pBuf[2] : pBuf[2];
985673a75b7Saaron 		break;
986673a75b7Saaron 
987673a75b7Saaron 	case P_MM:		/* MM Series */
988673a75b7Saaron 	case P_LOGI:		/* Logitech Mice */
989673a75b7Saaron 		act->button = butmapmsc[pBuf[0] & MOUSE_MSC_BUTTONS];
990673a75b7Saaron 		act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : -pBuf[1];
991673a75b7Saaron 		act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? -pBuf[2] : pBuf[2];
992673a75b7Saaron 		break;
993673a75b7Saaron 
994279fd192Sderaadt 		/*
995279fd192Sderaadt 		 * XXX removed the code for BusMouse and PS/2 protocols which
996279fd192Sderaadt 		 * are now handled by wsmouse compatible mouse drivers XXX
997673a75b7Saaron 		 */
998673a75b7Saaron 
999673a75b7Saaron 	default:
1000673a75b7Saaron 		return 0;
1001673a75b7Saaron 	}
1002673a75b7Saaron 
1003673a75b7Saaron 	/*
1004673a75b7Saaron          * We don't reset pBufP here yet, as there may be an additional data
1005673a75b7Saaron          * byte in some protocols. See above.
1006673a75b7Saaron          */
1007673a75b7Saaron 	/* has something changed? */
10082fc38322Smiod 	act->flags = ((act->dx || act->dy || act->dz || act->dw) ?
10092fc38322Smiod 	    MOUSE_POSCHANGED : 0) | (act->obutton ^ act->button);
1010673a75b7Saaron 	return act->flags;
1011673a75b7Saaron }
1012