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