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