1 /* 2 * Copyright 2011, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * The alps_model_info struct and all the hardware specs are taken from the 6 * linux driver, thanks a lot! 7 * 8 * Authors: 9 * Clemens Zeidler (haiku@Clemens-Zeidler.de) 10 */ 11 12 13 #include "ps2_alps.h" 14 15 #include <stdlib.h> 16 #include <string.h> 17 18 #include "ps2_service.h" 19 20 21 static int32 generate_event(timer* timer); 22 23 24 const bigtime_t kEventInterval = 1000 * 50; 25 26 27 class EventProducer { 28 public: 29 EventProducer() 30 { 31 fFired = false; 32 } 33 34 status_t 35 FireEvent(alps_cookie* cookie, uint8* package) 36 { 37 fCookie = cookie; 38 memcpy(fLastPackage, package, sizeof(uint8) * PS2_PACKET_ALPS); 39 40 status_t status = add_timer(&fEventTimer, &generate_event, 41 kEventInterval, B_ONE_SHOT_RELATIVE_TIMER); 42 if (status == B_OK) 43 fFired = true; 44 return status; 45 } 46 47 bool 48 CancelEvent() 49 { 50 if (!fFired) 51 return false; 52 fFired = false; 53 return cancel_timer(&fEventTimer); 54 } 55 56 void 57 InjectEvent() 58 { 59 if (packet_buffer_write(fCookie->ring_buffer, fLastPackage, 60 PS2_PACKET_ALPS) != PS2_PACKET_ALPS) { 61 // buffer is full, drop new data 62 return; 63 } 64 release_sem_etc(fCookie->sem, 1, B_DO_NOT_RESCHEDULE); 65 } 66 67 private: 68 bool fFired; 69 uint8 fLastPackage[PS2_PACKET_ALPS]; 70 timer fEventTimer; 71 alps_cookie* fCookie; 72 }; 73 74 75 static EventProducer gEventProducer; 76 77 78 static int32 79 generate_event(timer* timer) 80 { 81 gEventProducer.InjectEvent(); 82 return 0; 83 } 84 85 86 const char* kALPSPath[4] = { 87 "input/touchpad/ps2/alps_0", 88 "input/touchpad/ps2/alps_1", 89 "input/touchpad/ps2/alps_2", 90 "input/touchpad/ps2/alps_3" 91 }; 92 93 94 typedef struct alps_model_info { 95 uint8 id[3]; 96 uint8 firstByte; 97 uint8 maskFirstByte; 98 uint8 flags; 99 } alps_model_info; 100 101 102 #define ALPS_OLDPROTO 0x01 // old style input 103 #define ALPS_DUALPOINT 0x02 // touchpad has trackstick 104 #define ALPS_PASS 0x04 // device has a pass-through port 105 106 #define ALPS_WHEEL 0x08 // hardware wheel present 107 #define ALPS_FW_BK_1 0x10 // front & back buttons present 108 #define ALPS_FW_BK_2 0x20 // front & back buttons present 109 #define ALPS_FOUR_BUTTONS 0x40 // 4 direction button present 110 #define ALPS_PS2_INTERLEAVED 0x80 // 3-byte PS/2 packet interleaved with 111 // 6-byte ALPS packet 112 113 static const struct alps_model_info gALPSModelInfos[] = { 114 {{0x32, 0x02, 0x14}, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT}, 115 // Toshiba Salellite Pro M10 116 // {{0x33, 0x02, 0x0a}, 0x88, 0xf8, ALPS_OLDPROTO}, 117 // UMAX-530T 118 {{0x53, 0x02, 0x0a}, 0xf8, 0xf8, 0}, 119 {{0x53, 0x02, 0x14}, 0xf8, 0xf8, 0}, 120 {{0x60, 0x03, 0xc8}, 0xf8, 0xf8, 0}, 121 // HP ze1115 122 {{0x63, 0x02, 0x0a}, 0xf8, 0xf8, 0}, 123 {{0x63, 0x02, 0x14}, 0xf8, 0xf8, 0}, 124 {{0x63, 0x02, 0x28}, 0xf8, 0xf8, ALPS_FW_BK_2}, 125 // Fujitsu Siemens S6010 126 // {{0x63, 0x02, 0x3c}, 0x8f, 0x8f, ALPS_WHEEL}, 127 // Toshiba Satellite S2400-103 128 {{0x63, 0x02, 0x50}, 0xef, 0xef, ALPS_FW_BK_1}, 129 // NEC Versa L320 130 {{0x63, 0x02, 0x64}, 0xf8, 0xf8, 0}, 131 {{0x63, 0x03, 0xc8}, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT}, 132 // Dell Latitude D800 133 {{0x73, 0x00, 0x0a}, 0xf8, 0xf8, ALPS_DUALPOINT}, 134 // ThinkPad R61 8918-5QG, x301 135 {{0x73, 0x02, 0x0a}, 0xf8, 0xf8, 0}, 136 {{0x73, 0x02, 0x14}, 0xf8, 0xf8, ALPS_FW_BK_2}, 137 // Ahtec Laptop 138 {{0x20, 0x02, 0x0e}, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT}, 139 // XXX 140 {{0x22, 0x02, 0x0a}, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT}, 141 {{0x22, 0x02, 0x14}, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT}, 142 // Dell Latitude D600 143 // {{0x62, 0x02, 0x14}, 0xcf, 0xcf, ALPS_PASS | ALPS_DUALPOINT 144 // | ALPS_PS2_INTERLEAVED}, 145 // Dell Latitude E5500, E6400, E6500, Precision M4400 146 {{0x73, 0x02, 0x50}, 0xcf, 0xcf, ALPS_FOUR_BUTTONS}, 147 // Dell Vostro 1400 148 // {{0x52, 0x01, 0x14}, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT 149 // | ALPS_PS2_INTERLEAVED}, 150 // Toshiba Tecra A11-11L 151 {{0, 0, 0}, 0, 0, 0} 152 }; 153 154 155 static alps_model_info* sFoundModel = NULL; 156 157 158 #define PS2_MOUSE_CMD_SET_SCALE11 0xe6 159 #define PS2_MOUSE_CMD_SET_SCALE21 0xe7 160 #define PS2_MOUSE_CMD_SET_RES 0xe8 161 #define PS2_MOUSE_CMD_GET_INFO 0xe9 162 #define PS2_MOUSE_CMD_SET_STREAM 0xea 163 #define PS2_MOUSE_CMD_SET_POLL 0xf0 164 #define PS2_MOUSE_CMD_SET_RATE 0xf3 165 166 167 // touchpad proportions 168 #define EDGE_MOTION_WIDTH 55 169 // increase the touchpad size a little bit 170 #define AREA_START_X 40 171 #define AREA_END_X 987 172 #define AREA_START_Y 40 173 #define AREA_END_Y 734 174 175 #define MIN_PRESSURE 15 176 #define REAL_MAX_PRESSURE 70 177 #define MAX_PRESSURE 115 178 179 180 #define ALPS_HISTORY_SIZE 256 181 182 183 static hardware_specs gHardwareSpecs; 184 185 186 /* Data taken from linux driver: 187 ALPS absolute Mode - new format 188 byte 0: 1 ? ? ? 1 ? ? ? 189 byte 1: 0 x6 x5 x4 x3 x2 x1 x0 190 byte 2: 0 x10 x9 x8 x7 ? fin ges 191 byte 3: 0 y9 y8 y7 1 M R L 192 byte 4: 0 y6 y5 y4 y3 y2 y1 y0 193 byte 5: 0 z6 z5 z4 z3 z2 z1 z0 194 */ 195 static status_t 196 get_alps_movment(alps_cookie *cookie, mouse_movement *movement) 197 { 198 status_t status; 199 touch_event event; 200 uint8 event_buffer[PS2_PACKET_ALPS]; 201 202 status = acquire_sem_etc(cookie->sem, 1, B_CAN_INTERRUPT, 0); 203 if (status < B_OK) 204 return status; 205 206 if (!cookie->dev->active) { 207 TRACE("ALPS: read_event: Error device no longer active\n"); 208 return B_ERROR; 209 } 210 211 if (packet_buffer_read(cookie->ring_buffer, event_buffer, 212 cookie->dev->packet_size) != cookie->dev->packet_size) { 213 TRACE("ALPS: error copying buffer\n"); 214 return B_ERROR; 215 } 216 217 event.buttons = event_buffer[3] & 7; 218 event.zPressure = event_buffer[5]; 219 220 // finger on touchpad 221 if (event_buffer[2] & 0x2) { 222 // finger with normal width 223 event.wValue = 4; 224 } else { 225 event.wValue = 3; 226 } 227 228 // tab gesture 229 if (event_buffer[2] & 0x1) { 230 event.zPressure = 60; 231 event.wValue = 4; 232 } 233 // if hardware tab gesture is off a z pressure of 16 is reported 234 if (cookie->previousZ == 0 && event.wValue == 4 && event.zPressure == 16) 235 event.zPressure = 60; 236 237 cookie->previousZ = event.zPressure; 238 239 event.xPosition = event_buffer[1] | ((event_buffer[2] & 0x78) << 4); 240 event.yPosition = event_buffer[4] | ((event_buffer[3] & 0x70) << 3); 241 242 // check for trackpoint even (z pressure 127) 243 if (sFoundModel->flags & ALPS_DUALPOINT && event.zPressure == 127) { 244 movement->xdelta = event.xPosition > 383 ? event.xPosition - 768 245 : event.xPosition; 246 movement->ydelta = event.yPosition > 255 247 ? event.yPosition - 512 : event.yPosition; 248 movement->wheel_xdelta = 0; 249 movement->wheel_ydelta = 0; 250 movement->buttons = event.buttons; 251 } else { 252 event.yPosition = AREA_END_Y - (event.yPosition - AREA_START_Y); 253 status = cookie->movementMaker.EventToMovement(&event, movement); 254 } 255 256 if (cookie->movementMaker.WasEdgeMotion() 257 || cookie->movementMaker.TapDragStarted()) { 258 gEventProducer.FireEvent(cookie, event_buffer); 259 } 260 261 return status; 262 } 263 264 265 static void 266 default_settings(touchpad_settings *set) 267 { 268 memcpy(set, &kDefaultTouchpadSettings, sizeof(touchpad_settings)); 269 } 270 271 272 status_t 273 probe_alps(ps2_dev* dev) 274 { 275 int i; 276 uint8 val[3]; 277 TRACE("ALPS: probe\n"); 278 279 val[0] = 0; 280 if (ps2_dev_command(dev, PS2_MOUSE_CMD_SET_RES, val, 1, NULL, 0) != B_OK 281 || ps2_dev_command(dev, PS2_MOUSE_CMD_SET_SCALE11, NULL, 0, NULL, 0) 282 != B_OK 283 || ps2_dev_command(dev, PS2_MOUSE_CMD_SET_SCALE11, NULL, 0, NULL, 0) 284 != B_OK 285 || ps2_dev_command(dev, PS2_MOUSE_CMD_SET_SCALE11, NULL, 0, NULL, 0) 286 != B_OK) 287 return B_ERROR; 288 289 if (ps2_dev_command(dev, PS2_MOUSE_CMD_GET_INFO, NULL, 0, val, 3) 290 != B_OK) 291 return B_ERROR; 292 293 if (val[0] != 0 || val[1] != 0 || (val[2] != 10 && val[2] != 100)) 294 return B_ERROR; 295 296 val[0] = 0; 297 if (ps2_dev_command(dev, PS2_MOUSE_CMD_SET_RES, val, 1, NULL, 0) != B_OK 298 || ps2_dev_command(dev, PS2_MOUSE_CMD_SET_SCALE21, NULL, 0, NULL, 0) 299 != B_OK 300 || ps2_dev_command(dev, PS2_MOUSE_CMD_SET_SCALE21, NULL, 0, NULL, 0) 301 != B_OK 302 || ps2_dev_command(dev, PS2_MOUSE_CMD_SET_SCALE21, NULL, 0, NULL, 0) 303 != B_OK) 304 return B_ERROR; 305 306 if (ps2_dev_command(dev, PS2_MOUSE_CMD_GET_INFO, NULL, 0, val, 3) 307 != B_OK) 308 return B_ERROR; 309 310 for (i = 0; ; i++) { 311 const alps_model_info* info = &gALPSModelInfos[i]; 312 if (info->id[0] == 0) { 313 INFO("ALPS not supported: %2.2x %2.2x %2.2x\n", val[0], val[1], 314 val[2]); 315 return B_ERROR; 316 } 317 318 if (info->id[0] == val[0] && info->id[1] == val[1] 319 && info->id[2] == val[2]) { 320 sFoundModel = (alps_model_info*)info; 321 INFO("ALPS found: %2.2x %2.2x %2.2x\n", val[0], val[1], val[2]); 322 break; 323 } 324 } 325 326 dev->name = kALPSPath[dev->idx]; 327 dev->packet_size = PS2_PACKET_ALPS; 328 329 return B_OK; 330 } 331 332 333 status_t 334 switch_hardware_tab(ps2_dev* dev, bool on) 335 { 336 uint8 val[3]; 337 uint8 arg = 0x00; 338 uint8 command = PS2_MOUSE_CMD_SET_RES; 339 if (on) { 340 arg = 0x0A; 341 command = PS2_MOUSE_CMD_SET_RATE; 342 } 343 if (ps2_dev_command(dev, PS2_MOUSE_CMD_GET_INFO, NULL, 0, val, 3) != B_OK 344 || ps2_dev_command(dev, PS2_CMD_DISABLE, NULL, 0, NULL, 0) != B_OK 345 || ps2_dev_command(dev, PS2_CMD_DISABLE, NULL, 0, NULL, 0) != B_OK 346 || ps2_dev_command(dev, command, &arg, 1, NULL, 0) != B_OK) 347 return B_ERROR; 348 349 return B_OK; 350 } 351 352 353 status_t 354 enable_passthrough(ps2_dev* dev, bool on) 355 { 356 uint8 command = PS2_MOUSE_CMD_SET_SCALE11; 357 if (on) 358 command = PS2_MOUSE_CMD_SET_SCALE21; 359 360 if (ps2_dev_command(dev, command, NULL, 0, NULL, 0) != B_OK 361 || ps2_dev_command(dev, command, NULL, 0, NULL, 0) != B_OK 362 || ps2_dev_command(dev, command, NULL, 0, NULL, 0) != B_OK 363 || ps2_dev_command(dev, PS2_CMD_DISABLE, NULL, 0, NULL, 0) != B_OK) 364 return B_ERROR; 365 366 return B_OK; 367 } 368 369 370 status_t 371 alps_open(const char *name, uint32 flags, void **_cookie) 372 { 373 ps2_dev* dev; 374 int i; 375 for (dev = NULL, i = 0; i < PS2_DEVICE_COUNT; i++) { 376 if (0 == strcmp(ps2_device[i].name, name)) { 377 dev = &ps2_device[i]; 378 break; 379 } 380 } 381 382 if (dev == NULL) { 383 TRACE("ps2: dev = NULL\n"); 384 return B_ERROR; 385 } 386 387 if (atomic_or(&dev->flags, PS2_FLAG_OPEN) & PS2_FLAG_OPEN) 388 return B_BUSY; 389 390 alps_cookie* cookie = (alps_cookie*)malloc(sizeof(alps_cookie)); 391 if (cookie == NULL) 392 goto err1; 393 memset(cookie, 0, sizeof(*cookie)); 394 395 cookie->movementMaker.Init(); 396 cookie->previousZ = 0; 397 *_cookie = cookie; 398 399 cookie->dev = dev; 400 dev->cookie = cookie; 401 dev->disconnect = &alps_disconnect; 402 dev->handle_int = &alps_handle_int; 403 404 default_settings(&cookie->settings); 405 406 gHardwareSpecs.edgeMotionWidth = EDGE_MOTION_WIDTH; 407 408 gHardwareSpecs.areaStartX = AREA_START_X; 409 gHardwareSpecs.areaEndX = AREA_END_X; 410 gHardwareSpecs.areaStartY = AREA_START_Y; 411 gHardwareSpecs.areaEndY = AREA_END_Y; 412 413 gHardwareSpecs.minPressure = MIN_PRESSURE; 414 gHardwareSpecs.realMaxPressure = REAL_MAX_PRESSURE; 415 gHardwareSpecs.maxPressure = MAX_PRESSURE; 416 417 cookie->movementMaker.SetSettings(&cookie->settings); 418 cookie->movementMaker.SetSpecs(&gHardwareSpecs); 419 420 dev->packet_size = PS2_PACKET_ALPS; 421 422 cookie->ring_buffer = create_packet_buffer( 423 ALPS_HISTORY_SIZE * dev->packet_size); 424 if (cookie->ring_buffer == NULL) { 425 TRACE("ALPS: can't allocate mouse actions buffer\n"); 426 goto err2; 427 } 428 // create the mouse semaphore, used for synchronization between 429 // the interrupt handler and the read operation 430 cookie->sem = create_sem(0, "ps2_alps_sem"); 431 if (cookie->sem < 0) { 432 TRACE("ALPS: failed creating semaphore!\n"); 433 goto err3; 434 } 435 436 if ((sFoundModel->flags & ALPS_PASS) != 0 437 && enable_passthrough(dev, true) != B_OK) 438 goto err4; 439 440 // switch tap mode off 441 if (switch_hardware_tab(dev, false) != B_OK) 442 goto err4; 443 444 // init the alps device to absolut mode 445 if (ps2_dev_command(dev, PS2_CMD_DISABLE, NULL, 0, NULL, 0) != B_OK 446 || ps2_dev_command(dev, PS2_CMD_DISABLE, NULL, 0, NULL, 0) != B_OK 447 || ps2_dev_command(dev, PS2_CMD_DISABLE, NULL, 0, NULL, 0) != B_OK 448 || ps2_dev_command(dev, PS2_CMD_DISABLE, NULL, 0, NULL, 0) != B_OK 449 || ps2_dev_command(dev, PS2_CMD_ENABLE, NULL, 0, NULL, 0) != B_OK) 450 goto err4; 451 452 if ((sFoundModel->flags & ALPS_PASS) != 0 453 && enable_passthrough(dev, false) != B_OK) 454 goto err4; 455 456 if (ps2_dev_command(dev, PS2_MOUSE_CMD_SET_STREAM, NULL, 0, NULL, 0) != B_OK) 457 goto err4; 458 459 if (ps2_dev_command(dev, PS2_CMD_ENABLE, NULL, 0, NULL, 0) != B_OK) 460 goto err4; 461 462 atomic_or(&dev->flags, PS2_FLAG_ENABLED); 463 464 TRACE("ALPS: open %s success\n", name); 465 return B_OK; 466 467 err4: 468 delete_sem(cookie->sem); 469 err3: 470 delete_packet_buffer(cookie->ring_buffer); 471 err2: 472 free(cookie); 473 err1: 474 atomic_and(&dev->flags, ~PS2_FLAG_OPEN); 475 476 TRACE("ALPS: open %s failed\n", name); 477 return B_ERROR; 478 } 479 480 481 status_t 482 alps_close(void *_cookie) 483 { 484 gEventProducer.CancelEvent(); 485 486 alps_cookie *cookie = (alps_cookie*)_cookie; 487 488 ps2_dev_command_timeout(cookie->dev, PS2_CMD_DISABLE, NULL, 0, NULL, 0, 489 150000); 490 491 delete_packet_buffer(cookie->ring_buffer); 492 delete_sem(cookie->sem); 493 494 atomic_and(&cookie->dev->flags, ~PS2_FLAG_OPEN); 495 atomic_and(&cookie->dev->flags, ~PS2_FLAG_ENABLED); 496 497 // Reset the touchpad so it generate standard ps2 packets instead of 498 // extended ones. If not, BeOS is confused with such packets when rebooting 499 // without a complete shutdown. 500 status_t status = ps2_reset_mouse(cookie->dev); 501 if (status != B_OK) { 502 INFO("ps2: reset failed\n"); 503 return B_ERROR; 504 } 505 506 TRACE("ALPS: close %s done\n", cookie->dev->name); 507 return B_OK; 508 } 509 510 511 status_t 512 alps_freecookie(void *_cookie) 513 { 514 free(_cookie); 515 return B_OK; 516 } 517 518 519 status_t 520 alps_ioctl(void *_cookie, uint32 op, void *buffer, size_t length) 521 { 522 alps_cookie *cookie = (alps_cookie*)_cookie; 523 mouse_movement movement; 524 status_t status; 525 526 switch (op) { 527 case MS_READ: 528 TRACE("ALPS: MS_READ get event\n"); 529 if ((status = get_alps_movment(cookie, &movement)) != B_OK) 530 return status; 531 return user_memcpy(buffer, &movement, sizeof(movement)); 532 533 case MS_IS_TOUCHPAD: 534 TRACE("ALPS: MS_IS_TOUCHPAD\n"); 535 return B_OK; 536 537 case MS_SET_TOUCHPAD_SETTINGS: 538 TRACE("ALPS: MS_SET_TOUCHPAD_SETTINGS"); 539 user_memcpy(&cookie->settings, buffer, sizeof(touchpad_settings)); 540 return B_OK; 541 542 case MS_SET_CLICKSPEED: 543 TRACE("ALPS: ioctl MS_SETCLICK (set click speed)\n"); 544 return user_memcpy(&cookie->movementMaker.click_speed, buffer, 545 sizeof(bigtime_t)); 546 547 default: 548 TRACE("ALPS: unknown opcode: %ld\n", op); 549 return B_BAD_VALUE; 550 } 551 } 552 553 554 static status_t 555 alps_read(void* cookie, off_t pos, void* buffer, size_t* _length) 556 { 557 *_length = 0; 558 return B_NOT_ALLOWED; 559 } 560 561 562 static status_t 563 alps_write(void* cookie, off_t pos, const void* buffer, size_t* _length) 564 { 565 *_length = 0; 566 return B_NOT_ALLOWED; 567 } 568 569 570 int32 571 alps_handle_int(ps2_dev* dev) 572 { 573 alps_cookie* cookie = (alps_cookie*)dev->cookie; 574 575 // we got a real event cancel the fake event 576 gEventProducer.CancelEvent(); 577 578 uint8 val; 579 val = cookie->dev->history[0].data; 580 if (cookie->packet_index == 0 581 && (val & sFoundModel->maskFirstByte) != sFoundModel->firstByte) { 582 INFO("ALPS: bad header, trying resync\n"); 583 cookie->packet_index = 0; 584 return B_UNHANDLED_INTERRUPT; 585 } 586 587 // data packages starting with a 0 588 if (cookie->packet_index > 1 && (val & 0x80)) { 589 INFO("ALPS: bad package data, trying resync\n"); 590 cookie->packet_index = 0; 591 return B_UNHANDLED_INTERRUPT; 592 } 593 594 cookie->buffer[cookie->packet_index] = val; 595 596 cookie->packet_index++; 597 if (cookie->packet_index >= 6) { 598 cookie->packet_index = 0; 599 600 if (packet_buffer_write(cookie->ring_buffer, 601 cookie->buffer, cookie->dev->packet_size) 602 != cookie->dev->packet_size) { 603 // buffer is full, drop new data 604 return B_HANDLED_INTERRUPT; 605 } 606 release_sem_etc(cookie->sem, 1, B_DO_NOT_RESCHEDULE); 607 608 return B_INVOKE_SCHEDULER; 609 } 610 611 return B_HANDLED_INTERRUPT; 612 } 613 614 615 void 616 alps_disconnect(ps2_dev *dev) 617 { 618 alps_cookie *cookie = (alps_cookie*)dev->cookie; 619 // the mouse device might not be opened at this point 620 INFO("ALPS: alps_disconnect %s\n", dev->name); 621 if ((dev->flags & PS2_FLAG_OPEN) != 0) 622 release_sem(cookie->sem); 623 } 624 625 626 device_hooks gALPSDeviceHooks = { 627 alps_open, 628 alps_close, 629 alps_freecookie, 630 alps_ioctl, 631 alps_read, 632 alps_write, 633 }; 634 635