1 /* $OpenBSD: mouse_protocols.c,v 1.4 2001/09/20 21:22:16 miod Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Jean-Baptiste Marchand, Julien Montagne and Jerome Verdon 5 * 6 * Copyright (c) 1998 by Kazutaka Yokota 7 * 8 * Copyright (c) 1995 Michael Smith 9 * 10 * Copyright (c) 1993 by David Dawes <dawes@xfree86.org> 11 * 12 * Copyright (c) 1990,91 by Thomas Roell, Dinkelscherben, Germany. 13 * 14 * All rights reserved. 15 * 16 * Most of this code was taken from the FreeBSD moused daemon, written by 17 * Michael Smith. The FreeBSD moused daemon already contained code from the 18 * Xfree Project, written by David Dawes and Thomas Roell and Kazutaka Yokota. 19 * 20 * Adaptation to OpenBSD was done by Jean-Baptiste Marchand, Julien Montagne 21 * and Jerome Verdon. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the above copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * This product includes software developed by 34 * David Dawes, Jean-Baptiste Marchand, Julien Montagne, Thomas Roell, 35 * Michael Smith, Jerome Verdon and Kazutaka Yokota. 36 * 4. The name authors may not be used to endorse or promote products 37 * derived from this software without specific prior written permission. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 40 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 42 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 43 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 45 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 46 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 48 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49 * 50 * 51 */ 52 53 /* Support for non-wsmouse (i.e. serial mice) mice */ 54 55 /* 56 * Most of this code comes from the Xfree Project and are derived from two files 57 * of Xfree86 3.3.6 with the following CVS tags : 58 $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_Mouse.c,v 3.21.2.24 59 1999/12/11 19:00:42 hohndel Exp $ 60 and 61 $XFree86: xc/programs/Xserver/hw/xfree86/common/xf86_PnPMouse.c,v 1.1.2.6 62 1999/07/29 09:22:51 hohndel Exp $ 63 */ 64 65 #include <sys/ioctl.h> 66 #include <sys/types.h> 67 #include <sys/time.h> 68 #include <sys/tty.h> 69 70 #include <ctype.h> 71 #include <err.h> 72 #include <errno.h> 73 #include <fcntl.h> 74 #include <unistd.h> 75 #include <setjmp.h> 76 #include <signal.h> 77 #include <poll.h> 78 #include <stdio.h> 79 #include <string.h> 80 #include <stdlib.h> 81 #include <syslog.h> 82 #include <varargs.h> 83 84 #include "wsmoused.h" 85 #include "mouse_protocols.h" 86 87 extern int debug; 88 extern int nodaemon; 89 extern int background; 90 extern mouse_t mouse; 91 92 /* Cflags of each mouse protocol, ordered by P_XXX */ 93 static unsigned short mousecflags[] = 94 { 95 (CS7 | CREAD | CLOCAL | HUPCL ), /* MicroSoft */ 96 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL ), /* MouseSystems */ 97 (CS8 | CSTOPB | CREAD | CLOCAL | HUPCL ), /* Logitech */ 98 (CS8 | PARENB | PARODD | CREAD | CLOCAL | HUPCL ), /* MMSeries */ 99 (CS7 | CREAD | CLOCAL | HUPCL ), /* MouseMan */ 100 (CS8 | CREAD | CLOCAL | HUPCL ), /* MM HitTablet */ 101 (CS7 | CREAD | CLOCAL | HUPCL ), /* GlidePoint */ 102 (CS7 | CREAD | CLOCAL | HUPCL ), /* IntelliMouse */ 103 (CS7 | CREAD | CLOCAL | HUPCL ), /* Thinking Mouse */ 104 }; 105 106 /* array ordered by P_XXX giving protocol properties */ 107 static unsigned char proto[][7] = { 108 /* hd_mask hd_id dp_mask dp_id bytes b4_mask b4_id */ 109 { 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00 }, /* MicroSoft */ 110 { 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff }, /* MouseSystems */ 111 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* Logitech */ 112 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* MMSeries */ 113 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* MouseMan */ 114 { 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff }, /* MM HitTablet */ 115 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* GlidePoint */ 116 { 0x40, 0x40, 0x40, 0x00, 3, ~0x3f, 0x00 }, /* IntelliMouse */ 117 { 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00 }, /* ThinkingMouse */ 118 }; 119 120 /* array ordered by P_XXX (mouse protocols) giving the protocol corresponding 121 to the name of a mouse */ 122 char *mouse_names[] = { 123 "microsoft", 124 "mousesystems", 125 "logitech", 126 "mmseries", 127 "mouseman", 128 "mmhitab", 129 "glidepoint", 130 "intellimouse", 131 "thinkingmouse", 132 NULL 133 }; 134 135 /* protocol currently used */ 136 static unsigned char cur_proto[7]; 137 138 /* PnP EISA/product IDs */ 139 static symtab_t pnpprod[] = { 140 { "KML0001", P_THINKING}, /* Kensignton ThinkingMouse */ 141 { "MSH0001", P_IMSERIAL}, /* MS IntelliMouse */ 142 { "MSH0004", P_IMSERIAL}, /* MS IntelliMouse TrackBall */ 143 { "KYEEZ00", P_MS}, /* Genius EZScroll */ 144 { "KYE0001", P_MS}, /* Genius PnP Mouse */ 145 { "KYE0003", P_IMSERIAL}, /* Genius NetMouse */ 146 { "LGI800C", P_IMSERIAL}, /* Logitech MouseMan (4 button model) */ 147 { "LGI8050", P_IMSERIAL}, /* Logitech MouseMan+ */ 148 { "LGI8051", P_IMSERIAL}, /* Logitech FirstMouse+ */ 149 { "LGI8001", P_LOGIMAN}, /* Logitech serial */ 150 { "PNP0F01", P_MS}, /* MS serial */ 151 /* 152 * XXX EzScroll returns PNP0F04 in the compatible device field; but it 153 * doesn't look compatible... 154 */ 155 { "PNP0F04", P_MSC}, /* MouseSystems */ 156 { "PNP0F05", P_MSC}, /* MouseSystems */ 157 { "PNP0F08", P_LOGIMAN}, /* Logitech serial */ 158 { "PNP0F09", P_MS}, /* MS BallPoint serial */ 159 { "PNP0F0A", P_MS}, /* MS PnP serial */ 160 { "PNP0F0B", P_MS}, /* MS PnP BallPoint serial */ 161 { "PNP0F0C", P_MS}, /* MS serial comatible */ 162 { "PNP0F0F", P_MS}, /* MS BallPoint comatible */ 163 { "PNP0F17", P_LOGIMAN}, /* Logitech serial compat */ 164 { NULL, -1 }, 165 }; 166 167 static symtab_t * 168 gettoken(symtab_t *tab, char *s, int len) 169 { 170 int i; 171 172 for (i = 0; tab[i].name != NULL; ++i) { 173 if (strncmp(tab[i].name, s, len) == 0) 174 break; 175 } 176 return &tab[i]; 177 } 178 179 char * 180 mouse_name(int type) 181 { 182 return (type == P_UNKNOWN 183 || (type > sizeof(mouse_names)/sizeof(mouse_names[0]) - 1)) 184 ? "unknown" : mouse_names[type]; 185 } 186 187 void 188 SetMouseSpeed(int old, int new, unsigned int cflag) 189 { 190 struct termios tty; 191 char *c; 192 193 if (!IS_SERIAL_DEV(mouse.portname)) 194 return; 195 196 if (tcgetattr(mouse.mfd, &tty) < 0) 197 { 198 debug("Warning: %s unable to get status of mouse fd (%s)\n", 199 mouse.portname, strerror(errno)); 200 return; 201 } 202 203 /* this will query the initial baudrate only once */ 204 if (mouse.old_baudrate < 0) { 205 switch (cfgetispeed(&tty)) 206 { 207 case B9600: 208 mouse.old_baudrate = 9600; 209 break; 210 case B4800: 211 mouse.old_baudrate = 4800; 212 break; 213 case B2400: 214 mouse.old_baudrate = 2400; 215 break; 216 case B1200: 217 default: 218 mouse.old_baudrate = 1200; 219 break; 220 } 221 } 222 223 tty.c_iflag = IGNBRK | IGNPAR; 224 tty.c_oflag = 0; 225 tty.c_lflag = 0; 226 tty.c_cflag = (tcflag_t)cflag; 227 tty.c_cc[VTIME] = 0; 228 tty.c_cc[VMIN] = 1; 229 230 switch (old) 231 { 232 case 9600: 233 cfsetispeed(&tty, B9600); 234 cfsetospeed(&tty, B9600); 235 break; 236 case 4800: 237 cfsetispeed(&tty, B4800); 238 cfsetospeed(&tty, B4800); 239 break; 240 case 2400: 241 cfsetispeed(&tty, B2400); 242 cfsetospeed(&tty, B2400); 243 break; 244 case 1200: 245 default: 246 cfsetispeed(&tty, B1200); 247 cfsetospeed(&tty, B1200); 248 } 249 250 if (tcsetattr(mouse.mfd, TCSADRAIN, &tty) < 0) { 251 logerr(1, "unable to get mouse status. Exiting...\n"); 252 } 253 254 switch (new) 255 { 256 case 9600: 257 c = "*q"; 258 cfsetispeed(&tty, B9600); 259 cfsetospeed(&tty, B9600); 260 break; 261 case 4800: 262 c = "*p"; 263 cfsetispeed(&tty, B4800); 264 cfsetospeed(&tty, B4800); 265 break; 266 case 2400: 267 c = "*o"; 268 cfsetispeed(&tty, B2400); 269 cfsetospeed(&tty, B2400); 270 break; 271 case 1200: 272 default: 273 c = "*n"; 274 cfsetispeed(&tty, B1200); 275 cfsetospeed(&tty, B1200); 276 } 277 278 if (mouse.proto == P_LOGIMAN || mouse.proto == P_LOGI) 279 { 280 if (write(mouse.mfd, c, 2) != 2) { 281 logerr(1, "unable to write to mouse. Exiting...\n"); 282 } 283 } 284 usleep(100000); 285 286 if (tcsetattr(mouse.mfd, TCSADRAIN, &tty) < 0) { 287 logerr(1, "unable to get mouse status. Exiting...\n"); 288 } 289 } 290 291 int 292 FlushInput(int fd) 293 { 294 struct pollfd pfd[1]; 295 char c[4]; 296 297 if (tcflush(fd, TCIFLUSH) == 0) 298 return 0; 299 300 pfd[0].fd = fd; 301 pfd[0].events = POLLIN; 302 303 while (poll(pfd, 1, 0) > 0) 304 read(fd, &c, sizeof(c)); 305 return 0; 306 } 307 308 /* 309 * Try to elicit a PnP ID as described in 310 * Microsoft, Hayes: "Plug and Play External COM Device Specification, 311 * rev 1.00", 1995. 312 * 313 * The routine does not fully implement the COM Enumerator as par Section 314 * 2.1 of the document. In particular, we don't have idle state in which 315 * the driver software monitors the com port for dynamic connection or 316 * removal of a device at the port, because `moused' simply quits if no 317 * device is found. 318 * 319 * In addition, as PnP COM device enumeration procedure slightly has 320 * changed since its first publication, devices which follow earlier 321 * revisions of the above spec. may fail to respond if the rev 1.0 322 * procedure is used. XXX 323 */ 324 325 static int 326 pnpgets(int mouse_fd, char *buf) 327 { 328 struct pollfd pfd[1]; 329 int i; 330 char c; 331 332 pfd[0].fd = mouse_fd; 333 pfd[0].events = POLLIN; 334 335 #if 0 336 /* 337 * This is the procedure described in rev 1.0 of PnP COM device spec. 338 * Unfortunately, some devices which comform to earlier revisions of 339 * the spec gets confused and do not return the ID string... 340 */ 341 342 /* port initialization (2.1.2) */ 343 ioctl(mouse_fd, TIOCMGET, &i); 344 i |= TIOCM_DTR; /* DTR = 1 */ 345 i &= ~TIOCM_RTS; /* RTS = 0 */ 346 ioctl(mouse_fd, TIOCMSET, &i); 347 usleep(200000); 348 if ((ioctl(mouse_fd, TIOCMGET, &i) == -1) || ((i & TIOCM_DSR) == 0)) 349 goto disconnect_idle; 350 351 /* port setup, 1st phase (2.1.3) */ 352 SetMouseSpeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL)); 353 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */ 354 ioctl(mouse_fd, TIOCMBIC, &i); 355 usleep(200000); 356 i = TIOCM_DTR; /* DTR = 1, RTS = 0 */ 357 ioctl(mouse_fd, TIOCMBIS, &i); 358 usleep(200000); 359 360 /* wait for response, 1st phase (2.1.4) */ 361 FlushInput(mouse_fd); 362 i = TIOCM_RTS; /* DTR = 1, RTS = 1 */ 363 ioctl(mouse_fd, TIOCMBIS, &i); 364 365 /* try to read something */ 366 if (poll(pfd, 1, 200000/1000) <= 0) { 367 368 /* port setup, 2nd phase (2.1.5) */ 369 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 0, RTS = 0 */ 370 ioctl(mouse_fd, TIOCMBIC, &i); 371 usleep(200000); 372 373 /* wait for respose, 2nd phase (2.1.6) */ 374 FlushInput(mouse_fd); 375 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */ 376 ioctl(mouse_fd, TIOCMBIS, &i); 377 378 /* try to read something */ 379 if (poll(pfd, 1, 200000/1000) <= 0) 380 goto connect_idle; 381 } 382 #else 383 384 /* 385 * This is a simplified procedure; it simply toggles RTS. 386 */ 387 SetMouseSpeed(1200, 1200, (CS7 | CREAD | CLOCAL | HUPCL)); 388 389 ioctl(mouse_fd, TIOCMGET, &i); 390 i |= TIOCM_DTR; /* DTR = 1 */ 391 i &= ~TIOCM_RTS; /* RTS = 0 */ 392 ioctl(mouse_fd, TIOCMSET, &i); 393 usleep(200000); 394 395 /* wait for response */ 396 FlushInput(mouse_fd); 397 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */ 398 ioctl(mouse_fd, TIOCMBIS, &i); 399 400 /* try to read something */ 401 if (poll(pfd, 1, 200000/1000) <= 0) 402 goto connect_idle; 403 #endif 404 405 /* collect PnP COM device ID (2.1.7) */ 406 i = 0; 407 usleep(200000); /* the mouse must send `Begin ID' within 200msec */ 408 while (read(mouse_fd, &c, 1) == 1) { 409 /* we may see "M", or "M3..." before `Begin ID' */ 410 if ((c == 0x08) || (c == 0x28)) { /* Begin ID */ 411 buf[i++] = c; 412 break; 413 } 414 } 415 if (i <= 0) { 416 /* we haven't seen `Begin ID' in time... */ 417 goto connect_idle; 418 } 419 420 ++c; /* make it `End ID' */ 421 for (;;) { 422 if (poll(pfd, 1, 200000/1000) <= 0) 423 break; 424 425 read(mouse_fd, &buf[i], 1); 426 if (buf[i++] == c) /* End ID */ 427 break; 428 if (i >= 256) 429 break; 430 } 431 if (buf[i - 1] != c) 432 goto connect_idle; 433 return i; 434 435 #if 0 436 /* 437 * According to PnP spec, we should set DTR = 1 and RTS = 0 while 438 * in idle state. But, `moused' shall set DTR = RTS = 1 and proceed, 439 * assuming there is something at the port even if it didn't 440 * respond to the PnP enumeration procedure. 441 */ 442 disconnect_idle: 443 i = TIOCM_DTR | TIOCM_RTS; /* DTR = 1, RTS = 1 */ 444 ioctl(mouse_fd, TIOCMBIS, &i); 445 #endif 446 447 connect_idle: 448 return 0; 449 } 450 451 /* pnpparse : parse a PnP string ID */ 452 static int 453 pnpparse(pnpid_t *id, char *buf, int len) 454 { 455 char s[3]; 456 int offset; 457 int sum = 0; 458 int i, j; 459 460 id->revision = 0; 461 id->eisaid = NULL; 462 id->serial = NULL; 463 id->class = NULL; 464 id->compat = NULL; 465 id->description = NULL; 466 id->neisaid = 0; 467 id->nserial = 0; 468 id->nclass = 0; 469 id->ncompat = 0; 470 id->ndescription = 0; 471 472 offset = 0x28 - buf[0]; 473 474 /* calculate checksum */ 475 for (i = 0; i < len - 3; ++i) { 476 sum += buf[i]; 477 buf[i] += offset; 478 } 479 sum += buf[len - 1]; 480 for (; i < len; ++i) 481 buf[i] += offset; 482 debug("Mouse: PnP ID string: '%*.*s'\n", len, len, buf); 483 484 /* revision */ 485 buf[1] -= offset; 486 buf[2] -= offset; 487 id->revision = ((buf[1] & 0x3f) << 6) | (buf[2] & 0x3f); 488 debug("Mouse: PnP rev %d.%02d\n", id->revision / 100, id->revision % 100); 489 490 /* EISA vender and product ID */ 491 id->eisaid = &buf[3]; 492 id->neisaid = 7; 493 494 /* option strings */ 495 i = 10; 496 if (buf[i] == '\\') { 497 /* device serial # */ 498 for (j = ++i; i < len; ++i) { 499 if (buf[i] == '\\') 500 break; 501 } 502 if (i >= len) 503 i -= 3; 504 if (i - j == 8) { 505 id->serial = &buf[j]; 506 id->nserial = 8; 507 } 508 } 509 if (buf[i] == '\\') { 510 /* PnP class */ 511 for (j = ++i; i < len; ++i) { 512 if (buf[i] == '\\') 513 break; 514 } 515 if (i >= len) 516 i -= 3; 517 if (i > j + 1) { 518 id->class = &buf[j]; 519 id->nclass = i - j; 520 } 521 } 522 if (buf[i] == '\\') { 523 /* compatible driver */ 524 for (j = ++i; i < len; ++i) { 525 if (buf[i] == '\\') 526 break; 527 } 528 /* 529 * PnP COM spec prior to v0.96 allowed '*' in this field, 530 * it's not allowed now; just ignore it. 531 */ 532 if (buf[j] == '*') 533 ++j; 534 if (i >= len) 535 i -= 3; 536 if (i > j + 1) { 537 id->compat = &buf[j]; 538 id->ncompat = i - j; 539 } 540 } 541 if (buf[i] == '\\') { 542 /* product description */ 543 for (j = ++i; i < len; ++i) { 544 if (buf[i] == ';') 545 break; 546 } 547 if (i >= len) 548 i -= 3; 549 if (i > j + 1) { 550 id->description = &buf[j]; 551 id->ndescription = i - j; 552 } 553 } 554 555 /* checksum exists if there are any optional fields */ 556 if ((id->nserial > 0) || (id->nclass > 0) 557 || (id->ncompat > 0) || (id->ndescription > 0)) { 558 #if 0 559 debug("Mouse: PnP checksum: 0x%02X\n", sum); 560 #endif 561 sprintf(s, "%02X", sum & 0x0ff); 562 if (strncmp(s, &buf[len - 3], 2) != 0) { 563 #if 0 564 /* 565 * Checksum error!! 566 * I found some mice do not comply with the PnP COM device 567 * spec regarding checksum... XXX 568 */ 569 return FALSE; 570 #endif 571 } 572 } 573 574 return 1; 575 } 576 577 /* pnpproto : return the prototype used, based on the PnP ID string */ 578 static symtab_t * 579 pnpproto(pnpid_t *id) 580 { 581 symtab_t *t; 582 int i, j; 583 584 if (id->nclass > 0) 585 if (strncmp(id->class, "MOUSE", id->nclass) != 0) 586 /* this is not a mouse! */ 587 return NULL; 588 589 if (id->neisaid > 0) { 590 t = gettoken(pnpprod, id->eisaid, id->neisaid); 591 if (t->val != -1) 592 return t; 593 } 594 /* 595 * The 'Compatible drivers' field may contain more than one 596 * ID separated by ','. 597 */ 598 if (id->ncompat <= 0) 599 return NULL; 600 for (i = 0; i < id->ncompat; ++i) { 601 for (j = i; id->compat[i] != ','; ++i) 602 if (i >= id->ncompat) 603 break; 604 if (i > j) { 605 t = gettoken(pnpprod, id->compat + j, i - j); 606 if (t->val != -1) 607 return t; 608 } 609 } 610 611 return NULL; 612 } 613 614 /* mouse_init : init the mouse by writing appropriate sequences */ 615 void 616 mouse_init(void) 617 { 618 struct pollfd pfd[1]; 619 char *s; 620 char c; 621 int i; 622 623 if (IS_WSMOUSE_DEV(mouse.portname)) { 624 wsmouse_init(); 625 return ; 626 } 627 628 pfd[0].fd = mouse.mfd; 629 pfd[0].events = POLLIN; 630 631 /** 632 ** This comment is a little out of context here, but it contains 633 ** some useful information... 634 ******************************************************************** 635 ** 636 ** The following lines take care of the Logitech MouseMan protocols. 637 ** 638 ** NOTE: There are diffrent versions of both MouseMan and TrackMan! 639 ** Hence I add another protocol P_LOGIMAN, which the user can 640 ** specify as MouseMan in his XF86Config file. This entry was 641 ** formerly handled as a special case of P_MS. However, people 642 ** who don't have the middle button problem, can still specify 643 ** Microsoft and use P_MS. 644 ** 645 ** By default, these mice should use a 3 byte Microsoft protocol 646 ** plus a 4th byte for the middle button. However, the mouse might 647 ** have switched to a different protocol before we use it, so I send 648 ** the proper sequence just in case. 649 ** 650 ** NOTE: - all commands to (at least the European) MouseMan have to 651 ** be sent at 1200 Baud. 652 ** - each command starts with a '*'. 653 ** - whenever the MouseMan receives a '*', it will switch back 654 ** to 1200 Baud. Hence I have to select the desired protocol 655 ** first, then select the baud rate. 656 ** 657 ** The protocols supported by the (European) MouseMan are: 658 ** - 5 byte packed binary protocol, as with the Mouse Systems 659 ** mouse. Selected by sequence "*U". 660 ** - 2 button 3 byte MicroSoft compatible protocol. Selected 661 ** by sequence "*V". 662 ** - 3 button 3+1 byte MicroSoft compatible protocol (default). 663 ** Selected by sequence "*X". 664 ** 665 ** The following baud rates are supported: 666 ** - 1200 Baud (default). Selected by sequence "*n". 667 ** - 9600 Baud. Selected by sequence "*q". 668 ** 669 ** Selecting a sample rate is no longer supported with the MouseMan! 670 ** Some additional lines in xf86Config.c take care of ill configured 671 ** baud rates and sample rates. (The user will get an error.) 672 */ 673 674 switch (mouse.proto) { 675 676 case P_LOGI: 677 /* 678 * The baud rate selection command must be sent at the current 679 * baud rate; try all likely settings 680 */ 681 SetMouseSpeed(9600, mouse.baudrate, mousecflags[mouse.proto]); 682 SetMouseSpeed(4800, mouse.baudrate, mousecflags[mouse.proto]); 683 SetMouseSpeed(2400, mouse.baudrate, mousecflags[mouse.proto]); 684 // SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]); 685 /* select MM series data format */ 686 write(mouse.mfd, "S", 1); 687 SetMouseSpeed(mouse.baudrate, mouse.baudrate, 688 mousecflags[P_MM]); 689 /* select report rate/frequency */ 690 if (mouse.rate <= 0) write(mouse.mfd, "O", 1); 691 else if (mouse.rate <= 15) write(mouse.mfd, "J", 1); 692 else if (mouse.rate <= 27) write(mouse.mfd, "K", 1); 693 else if (mouse.rate <= 42) write(mouse.mfd, "L", 1); 694 else if (mouse.rate <= 60) write(mouse.mfd, "R", 1); 695 else if (mouse.rate <= 85) write(mouse.mfd, "M", 1); 696 else if (mouse.rate <= 125) write(mouse.mfd, "Q", 1); 697 else write(mouse.mfd, "N", 1); 698 break; 699 700 case P_LOGIMAN: 701 /* The command must always be sent at 1200 baud */ 702 SetMouseSpeed(1200, 1200, mousecflags[mouse.proto]); 703 write(mouse.mfd, "*X", 2); 704 SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]); 705 break; 706 707 case P_MMHIT: 708 SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]); 709 710 /* 711 * Initialize Hitachi PUMA Plus - Model 1212E to desired settings. 712 * The tablet must be configured to be in MM mode, NO parity, 713 * Binary Format. xf86Info.sampleRate controls the sensativity 714 * of the tablet. We only use this tablet for it's 4-button puck 715 * so we don't run in "Absolute Mode" 716 */ 717 write(mouse.mfd, "z8", 2); /* Set Parity = "NONE" */ 718 usleep(50000); 719 write(mouse.mfd, "zb", 2); /* Set Format = "Binary" */ 720 usleep(50000); 721 write(mouse.mfd, "@", 1); /* Set Report Mode = "Stream" */ 722 usleep(50000); 723 write(mouse.mfd, "R", 1); /* Set Output Rate = "45 rps" */ 724 usleep(50000); 725 write(mouse.mfd, "I\x20", 2); /* Set Incrememtal Mode "20" */ 726 usleep(50000); 727 write(mouse.mfd, "E", 1); /* Set Data Type = "Relative */ 728 usleep(50000); 729 730 /* Resolution is in 'lines per inch' on the Hitachi tablet */ 731 if (mouse.resolution == MOUSE_RES_LOW) c = 'g'; 732 else if (mouse.resolution == MOUSE_RES_MEDIUMLOW) c = 'e'; 733 else if (mouse.resolution == MOUSE_RES_MEDIUMHIGH) c = 'h'; 734 else if (mouse.resolution == MOUSE_RES_HIGH) c = 'd'; 735 else if (mouse.resolution <= 40) c = 'g'; 736 else if (mouse.resolution <= 100) c = 'd'; 737 else if (mouse.resolution <= 200) c = 'e'; 738 else if (mouse.resolution <= 500) c = 'h'; 739 else if (mouse.resolution <= 1000) c = 'j'; 740 else c = 'd'; 741 write(mouse.mfd, &c, 1); 742 usleep(50000); 743 744 write(mouse.mfd, "\021", 1); /* Resume DATA output */ 745 break; 746 747 case P_THINKING: 748 SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]); 749 /* the PnP ID string may be sent again, discard it */ 750 usleep(200000); 751 i = FREAD; 752 ioctl(mouse.mfd, TIOCFLUSH, &i); 753 /* send the command to initialize the beast */ 754 for (s = "E5E5"; *s; ++s) { 755 write(mouse.mfd, s, 1); 756 757 if (poll(pfd, 1, INFTIM) <= 0) 758 break; 759 read(mouse.mfd, &c, 1); 760 debug("%c", c); 761 if (c != *s) 762 break; 763 } 764 break; 765 766 case P_MSC: 767 SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]); 768 #if 0 769 if (mouse.flags & ClearDTR) { 770 i = TIOCM_DTR; 771 ioctl(mouse.mfd, TIOCMBIC, &i); 772 } 773 if (mouse.flags & ClearRTS) { 774 i = TIOCM_RTS; 775 ioctl(mouse.mfd, TIOCMBIC, &i); 776 } 777 #endif 778 break; 779 780 default: 781 SetMouseSpeed(1200, mouse.baudrate, mousecflags[mouse.proto]); 782 break; 783 } 784 } 785 786 /* mouse_identify : identify the protocol used by the mouse */ 787 int 788 mouse_identify(void) 789 { 790 char pnpbuf[256];/* PnP identifier string may be up to 256 bytes long */ 791 pnpid_t pnpid; 792 symtab_t *t; 793 int len; 794 795 /* protocol has been specified with '-t' */ 796 if (mouse.proto != P_UNKNOWN) 797 bcopy(proto[mouse.proto], cur_proto, sizeof(cur_proto)); 798 else { 799 /* maybe this is an PnP mouse... */ 800 if (mouse.flags & NoPnP) 801 return mouse.proto; 802 if (((len = pnpgets(mouse.mfd, pnpbuf)) <= 0) 803 || !pnpparse(&pnpid, pnpbuf, len)) 804 return mouse.proto; 805 806 debug("PnP serial mouse: '%*.*s' '%*.*s' '%*.*s'", 807 pnpid.neisaid, pnpid.neisaid, 808 pnpid.eisaid, pnpid.ncompat, 809 pnpid.ncompat, pnpid.compat, 810 pnpid.ndescription, pnpid.ndescription, 811 pnpid.description); 812 813 /* we have a valid PnP serial device ID */ 814 t = pnpproto(&pnpid); 815 if (t != NULL) { 816 mouse.proto = t->val; 817 bcopy(proto[mouse.proto], cur_proto, sizeof(cur_proto)); 818 } 819 else 820 mouse.proto = P_UNKNOWN; 821 822 } 823 824 debug("proto params: %02x %02x %02x %02x %d %02x %02x", 825 cur_proto[0], cur_proto[1], cur_proto[2], cur_proto[3], 826 cur_proto[4], cur_proto[5], cur_proto[6]); 827 828 return mouse.proto; 829 } 830 831 /* mouse_protocol : decode bytes with the current mouse protocol */ 832 int 833 mouse_protocol(u_char rBuf, mousestatus_t *act) 834 { 835 /* MOUSE_MSS_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */ 836 static int butmapmss[4] = { /* Microsoft, MouseMan, GlidePoint, 837 IntelliMouse, Thinking Mouse */ 838 0, 839 MOUSE_BUTTON3DOWN, 840 MOUSE_BUTTON1DOWN, 841 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 842 }; 843 static int butmapmss2[4] = { /* Microsoft, MouseMan, GlidePoint, 844 Thinking Mouse */ 845 0, 846 MOUSE_BUTTON4DOWN, 847 MOUSE_BUTTON2DOWN, 848 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN, 849 }; 850 /* MOUSE_INTELLI_BUTTON?DOWN -> MOUSE_BUTTON?DOWN */ 851 static int butmapintelli[4] = { /* IntelliMouse, NetMouse, Mie Mouse, 852 MouseMan+ */ 853 0, 854 MOUSE_BUTTON2DOWN, 855 MOUSE_BUTTON4DOWN, 856 MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN, 857 }; 858 /* MOUSE_MSC_BUTTON?UP -> MOUSE_BUTTON?DOWN */ 859 static int butmapmsc[8] = { /* MouseSystems, MMSeries, Logitech, 860 Bus, sysmouse */ 861 0, 862 MOUSE_BUTTON3DOWN, 863 MOUSE_BUTTON2DOWN, 864 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 865 MOUSE_BUTTON1DOWN, 866 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 867 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 868 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 869 }; 870 /* for Hitachi tablet */ 871 static int butmaphit[8] = { /* MM HitTablet */ 872 0, 873 MOUSE_BUTTON3DOWN, 874 MOUSE_BUTTON2DOWN, 875 MOUSE_BUTTON1DOWN, 876 MOUSE_BUTTON4DOWN, 877 MOUSE_BUTTON5DOWN, 878 MOUSE_BUTTON6DOWN, 879 MOUSE_BUTTON7DOWN, 880 }; 881 /* for PS/2 VersaPad */ 882 static int butmapversaps2[8] = { /* VersaPad */ 883 0, 884 MOUSE_BUTTON3DOWN, 885 0, 886 MOUSE_BUTTON3DOWN, 887 MOUSE_BUTTON1DOWN, 888 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 889 MOUSE_BUTTON1DOWN, 890 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 891 }; 892 static int pBufP = 0; 893 static unsigned char pBuf[8]; 894 895 debug("received char 0x%x",(int)rBuf); 896 897 /* 898 * Hack for resyncing: We check here for a package that is: 899 * a) illegal (detected by wrong data-package header) 900 * b) invalid (0x80 == -128 and that might be wrong for MouseSystems) 901 * c) bad header-package 902 * 903 * NOTE: b) is a voilation of the MouseSystems-Protocol, since values of 904 * -128 are allowed, but since they are very seldom we can easily 905 * use them as package-header with no button pressed. 906 * NOTE/2: On a PS/2 mouse any byte is valid as a data byte. Furthermore, 907 * 0x80 is not valid as a header byte. For a PS/2 mouse we skip 908 * checking data bytes. 909 * For resyncing a PS/2 mouse we require the two most significant 910 * bits in the header byte to be 0. These are the overflow bits, 911 * and in case of an overflow we actually lose sync. Overflows 912 * are very rare, however, and we quickly gain sync again after 913 * an overflow condition. This is the best we can do. (Actually, 914 * we could use bit 0x08 in the header byte for resyncing, since 915 * that bit is supposed to be always on, but nobody told 916 * Microsoft...) 917 */ 918 919 if (pBufP != 0 && ((rBuf & cur_proto[2]) != cur_proto[3] || rBuf == 0x80)) 920 { 921 pBufP = 0; /* skip package */ 922 } 923 924 if (pBufP == 0 && (rBuf & cur_proto[0]) != cur_proto[1]) 925 return 0; 926 927 /* is there an extra data byte? */ 928 if (pBufP >= cur_proto[4] && (rBuf & cur_proto[0]) != cur_proto[1]) 929 { 930 /* 931 * Hack for Logitech MouseMan Mouse - Middle button 932 * 933 * Unfortunately this mouse has variable length packets: the standard 934 * Microsoft 3 byte packet plus an optional 4th byte whenever the 935 * middle button status changes. 936 * 937 * We have already processed the standard packet with the movement 938 * and button info. Now post an event message with the old status 939 * of the left and right buttons and the updated middle button. 940 */ 941 942 /* 943 * Even worse, different MouseMen and TrackMen differ in the 4th 944 * byte: some will send 0x00/0x20, others 0x01/0x21, or even 945 * 0x02/0x22, so I have to strip off the lower bits. 946 * 947 * [JCH-96/01/21] 948 * HACK for ALPS "fourth button". (It's bit 0x10 of the "fourth byte" 949 * and it is activated by tapping the glidepad with the finger! 8^) 950 * We map it to bit bit3, and the reverse map in xf86Events just has 951 * to be extended so that it is identified as Button 4. The lower 952 * half of the reverse-map may remain unchanged. 953 */ 954 955 /* 956 * [KY-97/08/03] 957 * Receive the fourth byte only when preceeding three bytes have 958 * been detected (pBufP >= cur_proto[4]). In the previous 959 * versions, the test was pBufP == 0; thus, we may have mistakingly 960 * received a byte even if we didn't see anything preceeding 961 * the byte. 962 */ 963 964 if ((rBuf & cur_proto[5]) != cur_proto[6]) { 965 pBufP = 0; 966 return 0; 967 } 968 969 switch (mouse.proto) { 970 971 /* 972 * IntelliMouse, NetMouse (including NetMouse Pro) and Mie Mouse 973 * always send the fourth byte, whereas the fourth byte is 974 * optional for GlidePoint and ThinkingMouse. The fourth byte 975 * is also optional for MouseMan+ and FirstMouse+ in their 976 * native mode. It is always sent if they are in the IntelliMouse 977 * compatible mode. 978 */ 979 case P_IMSERIAL: /* IntelliMouse, NetMouse, Mie Mouse, 980 MouseMan+ */ 981 act->dx = act->dy = 0; 982 act->dz = (rBuf & 0x08) ? (rBuf & 0x0f) - 16 : (rBuf & 0x0f); 983 act->obutton = act->button; 984 act->button = butmapintelli[(rBuf & MOUSE_MSS_BUTTONS) >> 4] 985 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)); 986 break; 987 988 default: 989 act->dx = act->dy = act->dz = 0; 990 act->obutton = act->button; 991 act->button = butmapmss2[(rBuf & MOUSE_MSS_BUTTONS) >> 4] 992 | (act->obutton & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)); 993 break; 994 } 995 996 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0) 997 | (act->obutton ^ act->button); 998 pBufP = 0; 999 return act->flags; 1000 } 1001 1002 if (pBufP >= cur_proto[4]) 1003 pBufP = 0; 1004 pBuf[pBufP++] = rBuf; 1005 if (pBufP != cur_proto[4]) 1006 return 0; 1007 1008 /* 1009 * assembly full package 1010 */ 1011 1012 debug("assembled full packet (len %d) %x,%x,%x,%x,%x,%x,%x,%x", 1013 cur_proto[4], 1014 pBuf[0], pBuf[1], pBuf[2], pBuf[3], 1015 pBuf[4], pBuf[5], pBuf[6], pBuf[7]); 1016 1017 act->dz = 0; 1018 act->obutton = act->button; 1019 switch (mouse.proto) 1020 { 1021 case P_MS: /* Microsoft */ 1022 case P_LOGIMAN: /* MouseMan/TrackMan */ 1023 case P_GLIDEPOINT: /* GlidePoint */ 1024 case P_THINKING: /* ThinkingMouse */ 1025 case P_IMSERIAL: /* IntelliMouse, NetMouse, Mie Mouse, 1026 MouseMan+ */ 1027 act->button = (act->obutton & (MOUSE_BUTTON2DOWN | MOUSE_BUTTON4DOWN)) 1028 | butmapmss[(pBuf[0] & MOUSE_MSS_BUTTONS) >> 4]; 1029 act->dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F)); 1030 act->dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F)); 1031 break; 1032 1033 case P_MSC: /* MouseSystems Corp */ 1034 act->button = butmapmsc[(~pBuf[0]) & MOUSE_MSC_BUTTONS]; 1035 act->dx = (char)(pBuf[1]) + (char)(pBuf[3]); 1036 act->dy = - ((char)(pBuf[2]) + (char)(pBuf[4])); 1037 break; 1038 1039 case P_MMHIT: /* MM HitTablet */ 1040 act->button = butmaphit[pBuf[0] & 0x07]; 1041 act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : - pBuf[1]; 1042 act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] : pBuf[2]; 1043 break; 1044 1045 case P_MM: /* MM Series */ 1046 case P_LOGI: /* Logitech Mice */ 1047 act->button = butmapmsc[pBuf[0] & MOUSE_MSC_BUTTONS]; 1048 act->dx = (pBuf[0] & MOUSE_MM_XPOSITIVE) ? pBuf[1] : - pBuf[1]; 1049 act->dy = (pBuf[0] & MOUSE_MM_YPOSITIVE) ? - pBuf[2] : pBuf[2]; 1050 break; 1051 1052 /* XXX 1053 * removed the code for BusMouse and PS/2 protocols 1054 * which are now handled by wsmouse compatible mouse drivers 1055 * XXX 1056 */ 1057 1058 default: 1059 return 0; 1060 } 1061 1062 /* 1063 * We don't reset pBufP here yet, as there may be an additional data 1064 * byte in some protocols. See above. 1065 */ 1066 1067 /* has something changed? */ 1068 act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0) 1069 | (act->obutton ^ act->button); 1070 1071 return act->flags; 1072 } 1073 1074