1 /* 2 * PS/2 mouse driver 3 * 4 * Copyright (c) 1999-2002 Vojtech Pavlik 5 * Copyright (c) 2003-2004 Dmitry Torokhov 6 */ 7 8 /* 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License version 2 as published by 11 * the Free Software Foundation. 12 */ 13 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 #define psmouse_fmt(fmt) fmt 16 17 #include <linux/delay.h> 18 #include <linux/module.h> 19 #include <linux/slab.h> 20 #include <linux/interrupt.h> 21 #include <linux/input.h> 22 #include <linux/serio.h> 23 #include <linux/init.h> 24 #include <linux/libps2.h> 25 #include <linux/mutex.h> 26 27 #include "psmouse.h" 28 #include "synaptics.h" 29 #include "logips2pp.h" 30 #include "alps.h" 31 #include "hgpk.h" 32 #include "lifebook.h" 33 #include "trackpoint.h" 34 #include "touchkit_ps2.h" 35 #include "elantech.h" 36 #include "sentelic.h" 37 #include "cypress_ps2.h" 38 #include "focaltech.h" 39 #include "vmmouse.h" 40 #include "byd.h" 41 42 #define DRIVER_DESC "PS/2 mouse driver" 43 44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); 45 MODULE_DESCRIPTION(DRIVER_DESC); 46 MODULE_LICENSE("GPL"); 47 48 static unsigned int psmouse_max_proto = PSMOUSE_AUTO; 49 static int psmouse_set_maxproto(const char *val, const struct kernel_param *); 50 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp); 51 static const struct kernel_param_ops param_ops_proto_abbrev = { 52 .set = psmouse_set_maxproto, 53 .get = psmouse_get_maxproto, 54 }; 55 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int) 56 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644); 57 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches."); 58 59 static unsigned int psmouse_resolution = 200; 60 module_param_named(resolution, psmouse_resolution, uint, 0644); 61 MODULE_PARM_DESC(resolution, "Resolution, in dpi."); 62 63 static unsigned int psmouse_rate = 100; 64 module_param_named(rate, psmouse_rate, uint, 0644); 65 MODULE_PARM_DESC(rate, "Report rate, in reports per second."); 66 67 static bool psmouse_smartscroll = true; 68 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644); 69 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled."); 70 71 static unsigned int psmouse_resetafter = 5; 72 module_param_named(resetafter, psmouse_resetafter, uint, 0644); 73 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never)."); 74 75 static unsigned int psmouse_resync_time; 76 module_param_named(resync_time, psmouse_resync_time, uint, 0644); 77 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never)."); 78 79 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO, 80 NULL, 81 psmouse_attr_show_protocol, psmouse_attr_set_protocol); 82 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO, 83 (void *) offsetof(struct psmouse, rate), 84 psmouse_show_int_attr, psmouse_attr_set_rate); 85 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO, 86 (void *) offsetof(struct psmouse, resolution), 87 psmouse_show_int_attr, psmouse_attr_set_resolution); 88 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO, 89 (void *) offsetof(struct psmouse, resetafter), 90 psmouse_show_int_attr, psmouse_set_int_attr); 91 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO, 92 (void *) offsetof(struct psmouse, resync_time), 93 psmouse_show_int_attr, psmouse_set_int_attr); 94 95 static struct attribute *psmouse_attributes[] = { 96 &psmouse_attr_protocol.dattr.attr, 97 &psmouse_attr_rate.dattr.attr, 98 &psmouse_attr_resolution.dattr.attr, 99 &psmouse_attr_resetafter.dattr.attr, 100 &psmouse_attr_resync_time.dattr.attr, 101 NULL 102 }; 103 104 static struct attribute_group psmouse_attribute_group = { 105 .attrs = psmouse_attributes, 106 }; 107 108 /* 109 * psmouse_mutex protects all operations changing state of mouse 110 * (connecting, disconnecting, changing rate or resolution via 111 * sysfs). We could use a per-device semaphore but since there 112 * rarely more than one PS/2 mouse connected and since semaphore 113 * is taken in "slow" paths it is not worth it. 114 */ 115 static DEFINE_MUTEX(psmouse_mutex); 116 117 static struct workqueue_struct *kpsmoused_wq; 118 119 struct psmouse_protocol { 120 enum psmouse_type type; 121 bool maxproto; 122 bool ignore_parity; /* Protocol should ignore parity errors from KBC */ 123 bool try_passthru; /* Try protocol also on passthrough ports */ 124 const char *name; 125 const char *alias; 126 int (*detect)(struct psmouse *, bool); 127 int (*init)(struct psmouse *); 128 }; 129 130 static void psmouse_report_standard_buttons(struct input_dev *dev, u8 buttons) 131 { 132 input_report_key(dev, BTN_LEFT, buttons & BIT(0)); 133 input_report_key(dev, BTN_MIDDLE, buttons & BIT(2)); 134 input_report_key(dev, BTN_RIGHT, buttons & BIT(1)); 135 } 136 137 /* 138 * psmouse_process_byte() analyzes the PS/2 data stream and reports 139 * relevant events to the input module once full packet has arrived. 140 */ 141 psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse) 142 { 143 struct input_dev *dev = psmouse->dev; 144 unsigned char *packet = psmouse->packet; 145 146 if (psmouse->pktcnt < psmouse->pktsize) 147 return PSMOUSE_GOOD_DATA; 148 149 /* Full packet accumulated, process it */ 150 151 switch (psmouse->type) { 152 case PSMOUSE_IMPS: 153 /* IntelliMouse has scroll wheel */ 154 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]); 155 break; 156 157 case PSMOUSE_IMEX: 158 /* Scroll wheel and buttons on IntelliMouse Explorer */ 159 switch (packet[3] & 0xC0) { 160 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */ 161 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31)); 162 break; 163 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */ 164 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31)); 165 break; 166 case 0x00: 167 case 0xC0: 168 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7)); 169 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1); 170 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1); 171 break; 172 } 173 break; 174 175 case PSMOUSE_GENPS: 176 /* Report scroll buttons on NetMice */ 177 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]); 178 179 /* Extra buttons on Genius NewNet 3D */ 180 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1); 181 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1); 182 break; 183 184 case PSMOUSE_THINKPS: 185 /* Extra button on ThinkingMouse */ 186 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1); 187 188 /* 189 * Without this bit of weirdness moving up gives wildly 190 * high Y changes. 191 */ 192 packet[1] |= (packet[0] & 0x40) << 1; 193 break; 194 195 case PSMOUSE_CORTRON: 196 /* 197 * Cortron PS2 Trackball reports SIDE button in the 198 * 4th bit of the first byte. 199 */ 200 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1); 201 packet[0] |= 0x08; 202 break; 203 204 default: 205 break; 206 } 207 208 /* Generic PS/2 Mouse */ 209 psmouse_report_standard_buttons(dev, 210 packet[0] | psmouse->extra_buttons); 211 212 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0); 213 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0); 214 215 input_sync(dev); 216 217 return PSMOUSE_FULL_PACKET; 218 } 219 220 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work, 221 unsigned long delay) 222 { 223 queue_delayed_work(kpsmoused_wq, work, delay); 224 } 225 226 /* 227 * __psmouse_set_state() sets new psmouse state and resets all flags. 228 */ 229 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state) 230 { 231 psmouse->state = new_state; 232 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0; 233 psmouse->ps2dev.flags = 0; 234 psmouse->last = jiffies; 235 } 236 237 /* 238 * psmouse_set_state() sets new psmouse state and resets all flags and 239 * counters while holding serio lock so fighting with interrupt handler 240 * is not a concern. 241 */ 242 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state) 243 { 244 serio_pause_rx(psmouse->ps2dev.serio); 245 __psmouse_set_state(psmouse, new_state); 246 serio_continue_rx(psmouse->ps2dev.serio); 247 } 248 249 /* 250 * psmouse_handle_byte() processes one byte of the input data stream 251 * by calling corresponding protocol handler. 252 */ 253 static int psmouse_handle_byte(struct psmouse *psmouse) 254 { 255 psmouse_ret_t rc = psmouse->protocol_handler(psmouse); 256 257 switch (rc) { 258 case PSMOUSE_BAD_DATA: 259 if (psmouse->state == PSMOUSE_ACTIVATED) { 260 psmouse_warn(psmouse, 261 "%s at %s lost sync at byte %d\n", 262 psmouse->name, psmouse->phys, 263 psmouse->pktcnt); 264 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) { 265 __psmouse_set_state(psmouse, PSMOUSE_IGNORE); 266 psmouse_notice(psmouse, 267 "issuing reconnect request\n"); 268 serio_reconnect(psmouse->ps2dev.serio); 269 return -1; 270 } 271 } 272 psmouse->pktcnt = 0; 273 break; 274 275 case PSMOUSE_FULL_PACKET: 276 psmouse->pktcnt = 0; 277 if (psmouse->out_of_sync_cnt) { 278 psmouse->out_of_sync_cnt = 0; 279 psmouse_notice(psmouse, 280 "%s at %s - driver resynced.\n", 281 psmouse->name, psmouse->phys); 282 } 283 break; 284 285 case PSMOUSE_GOOD_DATA: 286 break; 287 } 288 return 0; 289 } 290 291 static void psmouse_handle_oob_data(struct psmouse *psmouse, u8 data) 292 { 293 switch (psmouse->oob_data_type) { 294 case PSMOUSE_OOB_NONE: 295 psmouse->oob_data_type = data; 296 break; 297 298 case PSMOUSE_OOB_EXTRA_BTNS: 299 psmouse_report_standard_buttons(psmouse->dev, data); 300 input_sync(psmouse->dev); 301 302 psmouse->extra_buttons = data; 303 psmouse->oob_data_type = PSMOUSE_OOB_NONE; 304 break; 305 306 default: 307 psmouse_warn(psmouse, 308 "unknown OOB_DATA type: 0x%02x\n", 309 psmouse->oob_data_type); 310 psmouse->oob_data_type = PSMOUSE_OOB_NONE; 311 break; 312 } 313 } 314 315 /* 316 * psmouse_interrupt() handles incoming characters, either passing them 317 * for normal processing or gathering them as command response. 318 */ 319 static irqreturn_t psmouse_interrupt(struct serio *serio, 320 unsigned char data, unsigned int flags) 321 { 322 struct psmouse *psmouse = serio_get_drvdata(serio); 323 324 if (psmouse->state == PSMOUSE_IGNORE) 325 goto out; 326 327 if (unlikely((flags & SERIO_TIMEOUT) || 328 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) { 329 330 if (psmouse->state == PSMOUSE_ACTIVATED) 331 psmouse_warn(psmouse, 332 "bad data from KBC -%s%s\n", 333 flags & SERIO_TIMEOUT ? " timeout" : "", 334 flags & SERIO_PARITY ? " bad parity" : ""); 335 ps2_cmd_aborted(&psmouse->ps2dev); 336 goto out; 337 } 338 339 if (flags & SERIO_OOB_DATA) { 340 psmouse_handle_oob_data(psmouse, data); 341 goto out; 342 } 343 344 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK)) 345 if (ps2_handle_ack(&psmouse->ps2dev, data)) 346 goto out; 347 348 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD)) 349 if (ps2_handle_response(&psmouse->ps2dev, data)) 350 goto out; 351 352 if (psmouse->state <= PSMOUSE_RESYNCING) 353 goto out; 354 355 if (psmouse->state == PSMOUSE_ACTIVATED && 356 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) { 357 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n", 358 psmouse->name, psmouse->phys, psmouse->pktcnt); 359 psmouse->badbyte = psmouse->packet[0]; 360 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING); 361 psmouse_queue_work(psmouse, &psmouse->resync_work, 0); 362 goto out; 363 } 364 365 psmouse->packet[psmouse->pktcnt++] = data; 366 367 /* Check if this is a new device announcement (0xAA 0x00) */ 368 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) { 369 if (psmouse->pktcnt == 1) { 370 psmouse->last = jiffies; 371 goto out; 372 } 373 374 if (psmouse->packet[1] == PSMOUSE_RET_ID || 375 (psmouse->type == PSMOUSE_HGPK && 376 psmouse->packet[1] == PSMOUSE_RET_BAT)) { 377 __psmouse_set_state(psmouse, PSMOUSE_IGNORE); 378 serio_reconnect(serio); 379 goto out; 380 } 381 382 /* Not a new device, try processing first byte normally */ 383 psmouse->pktcnt = 1; 384 if (psmouse_handle_byte(psmouse)) 385 goto out; 386 387 psmouse->packet[psmouse->pktcnt++] = data; 388 } 389 390 /* 391 * See if we need to force resync because mouse was idle for 392 * too long. 393 */ 394 if (psmouse->state == PSMOUSE_ACTIVATED && 395 psmouse->pktcnt == 1 && psmouse->resync_time && 396 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) { 397 psmouse->badbyte = psmouse->packet[0]; 398 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING); 399 psmouse_queue_work(psmouse, &psmouse->resync_work, 0); 400 goto out; 401 } 402 403 psmouse->last = jiffies; 404 psmouse_handle_byte(psmouse); 405 406 out: 407 return IRQ_HANDLED; 408 } 409 410 /* 411 * psmouse_sliced_command() sends an extended PS/2 command to the mouse 412 * using sliced syntax, understood by advanced devices, such as Logitech 413 * or Synaptics touchpads. The command is encoded as: 414 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu 415 * is the command. 416 */ 417 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command) 418 { 419 int i; 420 421 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) 422 return -1; 423 424 for (i = 6; i >= 0; i -= 2) { 425 unsigned char d = (command >> i) & 3; 426 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES)) 427 return -1; 428 } 429 430 return 0; 431 } 432 433 /* 434 * psmouse_reset() resets the mouse into power-on state. 435 */ 436 int psmouse_reset(struct psmouse *psmouse) 437 { 438 unsigned char param[2]; 439 440 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT)) 441 return -1; 442 443 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID) 444 return -1; 445 446 return 0; 447 } 448 449 /* 450 * Here we set the mouse resolution. 451 */ 452 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution) 453 { 454 static const unsigned char params[] = { 0, 1, 2, 2, 3 }; 455 unsigned char p; 456 457 if (resolution == 0 || resolution > 200) 458 resolution = 200; 459 460 p = params[resolution / 50]; 461 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES); 462 psmouse->resolution = 25 << p; 463 } 464 465 /* 466 * Here we set the mouse report rate. 467 */ 468 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate) 469 { 470 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 }; 471 unsigned char r; 472 int i = 0; 473 474 while (rates[i] > rate) i++; 475 r = rates[i]; 476 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE); 477 psmouse->rate = r; 478 } 479 480 /* 481 * Here we set the mouse scaling. 482 */ 483 static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale) 484 { 485 ps2_command(&psmouse->ps2dev, NULL, 486 scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 : 487 PSMOUSE_CMD_SETSCALE11); 488 } 489 490 /* 491 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it. 492 */ 493 static int psmouse_poll(struct psmouse *psmouse) 494 { 495 return ps2_command(&psmouse->ps2dev, psmouse->packet, 496 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)); 497 } 498 499 static bool psmouse_check_pnp_id(const char *id, const char * const ids[]) 500 { 501 int i; 502 503 for (i = 0; ids[i]; i++) 504 if (!strcasecmp(id, ids[i])) 505 return true; 506 507 return false; 508 } 509 510 /* 511 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids. 512 */ 513 bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[]) 514 { 515 struct serio *serio = psmouse->ps2dev.serio; 516 char *p, *fw_id_copy, *save_ptr; 517 bool found = false; 518 519 if (strncmp(serio->firmware_id, "PNP: ", 5)) 520 return false; 521 522 fw_id_copy = kstrndup(&serio->firmware_id[5], 523 sizeof(serio->firmware_id) - 5, 524 GFP_KERNEL); 525 if (!fw_id_copy) 526 return false; 527 528 save_ptr = fw_id_copy; 529 while ((p = strsep(&fw_id_copy, " ")) != NULL) { 530 if (psmouse_check_pnp_id(p, ids)) { 531 found = true; 532 break; 533 } 534 } 535 536 kfree(save_ptr); 537 return found; 538 } 539 540 /* 541 * Genius NetMouse magic init. 542 */ 543 static int genius_detect(struct psmouse *psmouse, bool set_properties) 544 { 545 struct ps2dev *ps2dev = &psmouse->ps2dev; 546 unsigned char param[4]; 547 548 param[0] = 3; 549 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES); 550 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11); 551 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11); 552 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11); 553 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO); 554 555 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55) 556 return -1; 557 558 if (set_properties) { 559 __set_bit(BTN_MIDDLE, psmouse->dev->keybit); 560 __set_bit(BTN_EXTRA, psmouse->dev->keybit); 561 __set_bit(BTN_SIDE, psmouse->dev->keybit); 562 __set_bit(REL_WHEEL, psmouse->dev->relbit); 563 564 psmouse->vendor = "Genius"; 565 psmouse->name = "Mouse"; 566 psmouse->pktsize = 4; 567 } 568 569 return 0; 570 } 571 572 /* 573 * IntelliMouse magic init. 574 */ 575 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties) 576 { 577 struct ps2dev *ps2dev = &psmouse->ps2dev; 578 unsigned char param[2]; 579 580 param[0] = 200; 581 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 582 param[0] = 100; 583 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 584 param[0] = 80; 585 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 586 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID); 587 588 if (param[0] != 3) 589 return -1; 590 591 if (set_properties) { 592 __set_bit(BTN_MIDDLE, psmouse->dev->keybit); 593 __set_bit(REL_WHEEL, psmouse->dev->relbit); 594 595 if (!psmouse->vendor) 596 psmouse->vendor = "Generic"; 597 if (!psmouse->name) 598 psmouse->name = "Wheel Mouse"; 599 psmouse->pktsize = 4; 600 } 601 602 return 0; 603 } 604 605 /* 606 * Try IntelliMouse/Explorer magic init. 607 */ 608 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties) 609 { 610 struct ps2dev *ps2dev = &psmouse->ps2dev; 611 unsigned char param[2]; 612 613 intellimouse_detect(psmouse, 0); 614 615 param[0] = 200; 616 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 617 param[0] = 200; 618 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 619 param[0] = 80; 620 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 621 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID); 622 623 if (param[0] != 4) 624 return -1; 625 626 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */ 627 param[0] = 200; 628 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 629 param[0] = 80; 630 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 631 param[0] = 40; 632 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 633 634 if (set_properties) { 635 __set_bit(BTN_MIDDLE, psmouse->dev->keybit); 636 __set_bit(REL_WHEEL, psmouse->dev->relbit); 637 __set_bit(REL_HWHEEL, psmouse->dev->relbit); 638 __set_bit(BTN_SIDE, psmouse->dev->keybit); 639 __set_bit(BTN_EXTRA, psmouse->dev->keybit); 640 641 if (!psmouse->vendor) 642 psmouse->vendor = "Generic"; 643 if (!psmouse->name) 644 psmouse->name = "Explorer Mouse"; 645 psmouse->pktsize = 4; 646 } 647 648 return 0; 649 } 650 651 /* 652 * Kensington ThinkingMouse / ExpertMouse magic init. 653 */ 654 static int thinking_detect(struct psmouse *psmouse, bool set_properties) 655 { 656 struct ps2dev *ps2dev = &psmouse->ps2dev; 657 unsigned char param[2]; 658 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 659 int i; 660 661 param[0] = 10; 662 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 663 param[0] = 0; 664 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES); 665 for (i = 0; i < ARRAY_SIZE(seq); i++) { 666 param[0] = seq[i]; 667 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); 668 } 669 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID); 670 671 if (param[0] != 2) 672 return -1; 673 674 if (set_properties) { 675 __set_bit(BTN_MIDDLE, psmouse->dev->keybit); 676 __set_bit(BTN_EXTRA, psmouse->dev->keybit); 677 678 psmouse->vendor = "Kensington"; 679 psmouse->name = "ThinkingMouse"; 680 } 681 682 return 0; 683 } 684 685 /* 686 * Bare PS/2 protocol "detection". Always succeeds. 687 */ 688 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties) 689 { 690 if (set_properties) { 691 if (!psmouse->vendor) 692 psmouse->vendor = "Generic"; 693 if (!psmouse->name) 694 psmouse->name = "Mouse"; 695 696 /* 697 * We have no way of figuring true number of buttons so let's 698 * assume that the device has 3. 699 */ 700 __set_bit(BTN_MIDDLE, psmouse->dev->keybit); 701 } 702 703 return 0; 704 } 705 706 /* 707 * Cortron PS/2 protocol detection. There's no special way to detect it, so it 708 * must be forced by sysfs protocol writing. 709 */ 710 static int cortron_detect(struct psmouse *psmouse, bool set_properties) 711 { 712 if (set_properties) { 713 psmouse->vendor = "Cortron"; 714 psmouse->name = "PS/2 Trackball"; 715 716 __set_bit(BTN_MIDDLE, psmouse->dev->keybit); 717 __set_bit(BTN_SIDE, psmouse->dev->keybit); 718 } 719 720 return 0; 721 } 722 723 static const struct psmouse_protocol psmouse_protocols[] = { 724 { 725 .type = PSMOUSE_PS2, 726 .name = "PS/2", 727 .alias = "bare", 728 .maxproto = true, 729 .ignore_parity = true, 730 .detect = ps2bare_detect, 731 .try_passthru = true, 732 }, 733 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP 734 { 735 .type = PSMOUSE_PS2PP, 736 .name = "PS2++", 737 .alias = "logitech", 738 .detect = ps2pp_detect, 739 }, 740 #endif 741 { 742 .type = PSMOUSE_THINKPS, 743 .name = "ThinkPS/2", 744 .alias = "thinkps", 745 .detect = thinking_detect, 746 }, 747 #ifdef CONFIG_MOUSE_PS2_CYPRESS 748 { 749 .type = PSMOUSE_CYPRESS, 750 .name = "CyPS/2", 751 .alias = "cypress", 752 .detect = cypress_detect, 753 .init = cypress_init, 754 }, 755 #endif 756 { 757 .type = PSMOUSE_GENPS, 758 .name = "GenPS/2", 759 .alias = "genius", 760 .detect = genius_detect, 761 }, 762 { 763 .type = PSMOUSE_IMPS, 764 .name = "ImPS/2", 765 .alias = "imps", 766 .maxproto = true, 767 .ignore_parity = true, 768 .detect = intellimouse_detect, 769 .try_passthru = true, 770 }, 771 { 772 .type = PSMOUSE_IMEX, 773 .name = "ImExPS/2", 774 .alias = "exps", 775 .maxproto = true, 776 .ignore_parity = true, 777 .detect = im_explorer_detect, 778 .try_passthru = true, 779 }, 780 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS 781 { 782 .type = PSMOUSE_SYNAPTICS, 783 .name = "SynPS/2", 784 .alias = "synaptics", 785 .detect = synaptics_detect, 786 .init = synaptics_init, 787 }, 788 { 789 .type = PSMOUSE_SYNAPTICS_RELATIVE, 790 .name = "SynRelPS/2", 791 .alias = "synaptics-relative", 792 .detect = synaptics_detect, 793 .init = synaptics_init_relative, 794 }, 795 #endif 796 #ifdef CONFIG_MOUSE_PS2_ALPS 797 { 798 .type = PSMOUSE_ALPS, 799 .name = "AlpsPS/2", 800 .alias = "alps", 801 .detect = alps_detect, 802 .init = alps_init, 803 }, 804 #endif 805 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK 806 { 807 .type = PSMOUSE_LIFEBOOK, 808 .name = "LBPS/2", 809 .alias = "lifebook", 810 .detect = lifebook_detect, 811 .init = lifebook_init, 812 }, 813 #endif 814 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT 815 { 816 .type = PSMOUSE_TRACKPOINT, 817 .name = "TPPS/2", 818 .alias = "trackpoint", 819 .detect = trackpoint_detect, 820 .try_passthru = true, 821 }, 822 #endif 823 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT 824 { 825 .type = PSMOUSE_TOUCHKIT_PS2, 826 .name = "touchkitPS/2", 827 .alias = "touchkit", 828 .detect = touchkit_ps2_detect, 829 }, 830 #endif 831 #ifdef CONFIG_MOUSE_PS2_OLPC 832 { 833 .type = PSMOUSE_HGPK, 834 .name = "OLPC HGPK", 835 .alias = "hgpk", 836 .detect = hgpk_detect, 837 }, 838 #endif 839 #ifdef CONFIG_MOUSE_PS2_ELANTECH 840 { 841 .type = PSMOUSE_ELANTECH, 842 .name = "ETPS/2", 843 .alias = "elantech", 844 .detect = elantech_detect, 845 .init = elantech_init, 846 }, 847 #endif 848 #ifdef CONFIG_MOUSE_PS2_SENTELIC 849 { 850 .type = PSMOUSE_FSP, 851 .name = "FSPPS/2", 852 .alias = "fsp", 853 .detect = fsp_detect, 854 .init = fsp_init, 855 }, 856 #endif 857 { 858 .type = PSMOUSE_CORTRON, 859 .name = "CortronPS/2", 860 .alias = "cortps", 861 .detect = cortron_detect, 862 }, 863 #ifdef CONFIG_MOUSE_PS2_FOCALTECH 864 { 865 .type = PSMOUSE_FOCALTECH, 866 .name = "FocalTechPS/2", 867 .alias = "focaltech", 868 .detect = focaltech_detect, 869 .init = focaltech_init, 870 }, 871 #endif 872 #ifdef CONFIG_MOUSE_PS2_VMMOUSE 873 { 874 .type = PSMOUSE_VMMOUSE, 875 .name = VMMOUSE_PSNAME, 876 .alias = "vmmouse", 877 .detect = vmmouse_detect, 878 .init = vmmouse_init, 879 }, 880 #endif 881 #ifdef CONFIG_MOUSE_PS2_BYD 882 { 883 .type = PSMOUSE_BYD, 884 .name = "BYDPS/2", 885 .alias = "byd", 886 .detect = byd_detect, 887 .init = byd_init, 888 }, 889 #endif 890 { 891 .type = PSMOUSE_AUTO, 892 .name = "auto", 893 .alias = "any", 894 .maxproto = true, 895 }, 896 }; 897 898 static const struct psmouse_protocol *__psmouse_protocol_by_type(enum psmouse_type type) 899 { 900 int i; 901 902 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) 903 if (psmouse_protocols[i].type == type) 904 return &psmouse_protocols[i]; 905 906 return NULL; 907 } 908 909 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type) 910 { 911 const struct psmouse_protocol *proto; 912 913 proto = __psmouse_protocol_by_type(type); 914 if (proto) 915 return proto; 916 917 WARN_ON(1); 918 return &psmouse_protocols[0]; 919 } 920 921 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len) 922 { 923 const struct psmouse_protocol *p; 924 int i; 925 926 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) { 927 p = &psmouse_protocols[i]; 928 929 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) || 930 (strlen(p->alias) == len && !strncmp(p->alias, name, len))) 931 return &psmouse_protocols[i]; 932 } 933 934 return NULL; 935 } 936 937 /* 938 * Apply default settings to the psmouse structure. Most of them will 939 * be overridden by individual protocol initialization routines. 940 */ 941 static void psmouse_apply_defaults(struct psmouse *psmouse) 942 { 943 struct input_dev *input_dev = psmouse->dev; 944 945 memset(input_dev->evbit, 0, sizeof(input_dev->evbit)); 946 memset(input_dev->keybit, 0, sizeof(input_dev->keybit)); 947 memset(input_dev->relbit, 0, sizeof(input_dev->relbit)); 948 memset(input_dev->absbit, 0, sizeof(input_dev->absbit)); 949 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit)); 950 951 __set_bit(EV_KEY, input_dev->evbit); 952 __set_bit(EV_REL, input_dev->evbit); 953 954 __set_bit(BTN_LEFT, input_dev->keybit); 955 __set_bit(BTN_RIGHT, input_dev->keybit); 956 957 __set_bit(REL_X, input_dev->relbit); 958 __set_bit(REL_Y, input_dev->relbit); 959 960 __set_bit(INPUT_PROP_POINTER, input_dev->propbit); 961 962 psmouse->set_rate = psmouse_set_rate; 963 psmouse->set_resolution = psmouse_set_resolution; 964 psmouse->set_scale = psmouse_set_scale; 965 psmouse->poll = psmouse_poll; 966 psmouse->protocol_handler = psmouse_process_byte; 967 psmouse->pktsize = 3; 968 psmouse->reconnect = NULL; 969 psmouse->disconnect = NULL; 970 psmouse->cleanup = NULL; 971 psmouse->pt_activate = NULL; 972 psmouse->pt_deactivate = NULL; 973 } 974 975 static bool psmouse_try_protocol(struct psmouse *psmouse, 976 enum psmouse_type type, 977 unsigned int *max_proto, 978 bool set_properties, bool init_allowed) 979 { 980 const struct psmouse_protocol *proto; 981 982 proto = __psmouse_protocol_by_type(type); 983 if (!proto) 984 return false; 985 986 if (psmouse->ps2dev.serio->id.type == SERIO_PS_PSTHRU && 987 !proto->try_passthru) { 988 return false; 989 } 990 991 if (set_properties) 992 psmouse_apply_defaults(psmouse); 993 994 if (proto->detect(psmouse, set_properties) != 0) 995 return false; 996 997 if (set_properties && proto->init && init_allowed) { 998 if (proto->init(psmouse) != 0) { 999 /* 1000 * We detected device, but init failed. Adjust 1001 * max_proto so we only try standard protocols. 1002 */ 1003 if (*max_proto > PSMOUSE_IMEX) 1004 *max_proto = PSMOUSE_IMEX; 1005 1006 return false; 1007 } 1008 } 1009 1010 return true; 1011 } 1012 1013 /* 1014 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol 1015 * the mouse may have. 1016 */ 1017 static int psmouse_extensions(struct psmouse *psmouse, 1018 unsigned int max_proto, bool set_properties) 1019 { 1020 bool synaptics_hardware = false; 1021 1022 /* 1023 * Always check for focaltech, this is safe as it uses pnp-id 1024 * matching. 1025 */ 1026 if (psmouse_try_protocol(psmouse, PSMOUSE_FOCALTECH, 1027 &max_proto, set_properties, false)) { 1028 if (max_proto > PSMOUSE_IMEX && 1029 IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH) && 1030 (!set_properties || focaltech_init(psmouse) == 0)) { 1031 return PSMOUSE_FOCALTECH; 1032 } 1033 /* 1034 * Restrict psmouse_max_proto so that psmouse_initialize() 1035 * does not try to reset rate and resolution, because even 1036 * that upsets the device. 1037 * This also causes us to basically fall through to basic 1038 * protocol detection, where we fully reset the mouse, 1039 * and set it up as bare PS/2 protocol device. 1040 */ 1041 psmouse_max_proto = max_proto = PSMOUSE_PS2; 1042 } 1043 1044 /* 1045 * We always check for LifeBook because it does not disturb mouse 1046 * (it only checks DMI information). 1047 */ 1048 if (psmouse_try_protocol(psmouse, PSMOUSE_LIFEBOOK, &max_proto, 1049 set_properties, max_proto > PSMOUSE_IMEX)) 1050 return PSMOUSE_LIFEBOOK; 1051 1052 if (psmouse_try_protocol(psmouse, PSMOUSE_VMMOUSE, &max_proto, 1053 set_properties, max_proto > PSMOUSE_IMEX)) 1054 return PSMOUSE_VMMOUSE; 1055 1056 /* 1057 * Try Kensington ThinkingMouse (we try first, because Synaptics 1058 * probe upsets the ThinkingMouse). 1059 */ 1060 if (max_proto > PSMOUSE_IMEX && 1061 psmouse_try_protocol(psmouse, PSMOUSE_THINKPS, &max_proto, 1062 set_properties, true)) { 1063 return PSMOUSE_THINKPS; 1064 } 1065 1066 /* 1067 * Try Synaptics TouchPad. Note that probing is done even if 1068 * Synaptics protocol support is disabled in config - we need to 1069 * know if it is Synaptics so we can reset it properly after 1070 * probing for IntelliMouse. 1071 */ 1072 if (max_proto > PSMOUSE_PS2 && 1073 psmouse_try_protocol(psmouse, PSMOUSE_SYNAPTICS, &max_proto, 1074 set_properties, false)) { 1075 synaptics_hardware = true; 1076 1077 if (max_proto > PSMOUSE_IMEX) { 1078 /* 1079 * Try activating protocol, but check if support is 1080 * enabled first, since we try detecting Synaptics 1081 * even when protocol is disabled. 1082 */ 1083 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) && 1084 (!set_properties || synaptics_init(psmouse) == 0)) { 1085 return PSMOUSE_SYNAPTICS; 1086 } 1087 1088 /* 1089 * Some Synaptics touchpads can emulate extended 1090 * protocols (like IMPS/2). Unfortunately 1091 * Logitech/Genius probes confuse some firmware 1092 * versions so we'll have to skip them. 1093 */ 1094 max_proto = PSMOUSE_IMEX; 1095 } 1096 1097 /* 1098 * Make sure that touchpad is in relative mode, gestures 1099 * (taps) are enabled. 1100 */ 1101 synaptics_reset(psmouse); 1102 } 1103 1104 /* 1105 * Try Cypress Trackpad. We must try it before Finger Sensing Pad 1106 * because Finger Sensing Pad probe upsets some modules of Cypress 1107 * Trackpads. 1108 */ 1109 if (max_proto > PSMOUSE_IMEX && 1110 psmouse_try_protocol(psmouse, PSMOUSE_CYPRESS, &max_proto, 1111 set_properties, true)) { 1112 return PSMOUSE_CYPRESS; 1113 } 1114 1115 /* Try ALPS TouchPad */ 1116 if (max_proto > PSMOUSE_IMEX) { 1117 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS); 1118 if (psmouse_try_protocol(psmouse, PSMOUSE_ALPS, 1119 &max_proto, set_properties, true)) 1120 return PSMOUSE_ALPS; 1121 } 1122 1123 /* Try OLPC HGPK touchpad */ 1124 if (max_proto > PSMOUSE_IMEX && 1125 psmouse_try_protocol(psmouse, PSMOUSE_HGPK, &max_proto, 1126 set_properties, true)) { 1127 return PSMOUSE_HGPK; 1128 } 1129 1130 /* Try Elantech touchpad */ 1131 if (max_proto > PSMOUSE_IMEX && 1132 psmouse_try_protocol(psmouse, PSMOUSE_ELANTECH, 1133 &max_proto, set_properties, true)) { 1134 return PSMOUSE_ELANTECH; 1135 } 1136 1137 if (max_proto > PSMOUSE_IMEX) { 1138 if (psmouse_try_protocol(psmouse, PSMOUSE_GENPS, 1139 &max_proto, set_properties, true)) 1140 return PSMOUSE_GENPS; 1141 1142 if (psmouse_try_protocol(psmouse, PSMOUSE_PS2PP, 1143 &max_proto, set_properties, true)) 1144 return PSMOUSE_PS2PP; 1145 1146 if (psmouse_try_protocol(psmouse, PSMOUSE_TRACKPOINT, 1147 &max_proto, set_properties, true)) 1148 return PSMOUSE_TRACKPOINT; 1149 1150 if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2, 1151 &max_proto, set_properties, true)) 1152 return PSMOUSE_TOUCHKIT_PS2; 1153 } 1154 1155 /* 1156 * Try Finger Sensing Pad. We do it here because its probe upsets 1157 * Trackpoint devices (causing TP_READ_ID command to time out). 1158 */ 1159 if (max_proto > PSMOUSE_IMEX && 1160 psmouse_try_protocol(psmouse, PSMOUSE_FSP, 1161 &max_proto, set_properties, true)) { 1162 return PSMOUSE_FSP; 1163 } 1164 1165 /* 1166 * Reset to defaults in case the device got confused by extended 1167 * protocol probes. Note that we follow up with full reset because 1168 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS. 1169 */ 1170 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS); 1171 psmouse_reset(psmouse); 1172 1173 if (max_proto >= PSMOUSE_IMEX && 1174 psmouse_try_protocol(psmouse, PSMOUSE_IMEX, 1175 &max_proto, set_properties, true)) { 1176 return PSMOUSE_IMEX; 1177 } 1178 1179 if (max_proto >= PSMOUSE_IMPS && 1180 psmouse_try_protocol(psmouse, PSMOUSE_IMPS, 1181 &max_proto, set_properties, true)) { 1182 return PSMOUSE_IMPS; 1183 } 1184 1185 /* 1186 * Okay, all failed, we have a standard mouse here. The number of 1187 * the buttons is still a question, though. We assume 3. 1188 */ 1189 psmouse_try_protocol(psmouse, PSMOUSE_PS2, 1190 &max_proto, set_properties, true); 1191 1192 if (synaptics_hardware) { 1193 /* 1194 * We detected Synaptics hardware but it did not respond to 1195 * IMPS/2 probes. We need to reset the touchpad because if 1196 * there is a track point on the pass through port it could 1197 * get disabled while probing for protocol extensions. 1198 */ 1199 psmouse_reset(psmouse); 1200 } 1201 1202 return PSMOUSE_PS2; 1203 } 1204 1205 /* 1206 * psmouse_probe() probes for a PS/2 mouse. 1207 */ 1208 static int psmouse_probe(struct psmouse *psmouse) 1209 { 1210 struct ps2dev *ps2dev = &psmouse->ps2dev; 1211 unsigned char param[2]; 1212 1213 /* 1214 * First, we check if it's a mouse. It should send 0x00 or 0x03 in 1215 * case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer. 1216 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and 1217 * subsequent ID queries, probably due to a firmware bug. 1218 */ 1219 param[0] = 0xa5; 1220 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID)) 1221 return -1; 1222 1223 if (param[0] != 0x00 && param[0] != 0x03 && 1224 param[0] != 0x04 && param[0] != 0xff) 1225 return -1; 1226 1227 /* 1228 * Then we reset and disable the mouse so that it doesn't generate 1229 * events. 1230 */ 1231 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS)) 1232 psmouse_warn(psmouse, "Failed to reset mouse on %s\n", 1233 ps2dev->serio->phys); 1234 1235 return 0; 1236 } 1237 1238 /* 1239 * psmouse_initialize() initializes the mouse to a sane state. 1240 */ 1241 static void psmouse_initialize(struct psmouse *psmouse) 1242 { 1243 /* 1244 * We set the mouse report rate, resolution and scaling. 1245 */ 1246 if (psmouse_max_proto != PSMOUSE_PS2) { 1247 psmouse->set_rate(psmouse, psmouse->rate); 1248 psmouse->set_resolution(psmouse, psmouse->resolution); 1249 psmouse->set_scale(psmouse, PSMOUSE_SCALE11); 1250 } 1251 } 1252 1253 /* 1254 * psmouse_activate() enables the mouse so that we get motion reports from it. 1255 */ 1256 int psmouse_activate(struct psmouse *psmouse) 1257 { 1258 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { 1259 psmouse_warn(psmouse, "Failed to enable mouse on %s\n", 1260 psmouse->ps2dev.serio->phys); 1261 return -1; 1262 } 1263 1264 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); 1265 return 0; 1266 } 1267 1268 /* 1269 * psmouse_deactivate() puts the mouse into poll mode so that we don't get 1270 * motion reports from it unless we explicitly request it. 1271 */ 1272 int psmouse_deactivate(struct psmouse *psmouse) 1273 { 1274 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) { 1275 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n", 1276 psmouse->ps2dev.serio->phys); 1277 return -1; 1278 } 1279 1280 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); 1281 return 0; 1282 } 1283 1284 /* 1285 * psmouse_resync() attempts to re-validate current protocol. 1286 */ 1287 static void psmouse_resync(struct work_struct *work) 1288 { 1289 struct psmouse *parent = NULL, *psmouse = 1290 container_of(work, struct psmouse, resync_work.work); 1291 struct serio *serio = psmouse->ps2dev.serio; 1292 psmouse_ret_t rc = PSMOUSE_GOOD_DATA; 1293 bool failed = false, enabled = false; 1294 int i; 1295 1296 mutex_lock(&psmouse_mutex); 1297 1298 if (psmouse->state != PSMOUSE_RESYNCING) 1299 goto out; 1300 1301 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { 1302 parent = serio_get_drvdata(serio->parent); 1303 psmouse_deactivate(parent); 1304 } 1305 1306 /* 1307 * Some mice don't ACK commands sent while they are in the middle of 1308 * transmitting motion packet. To avoid delay we use ps2_sendbyte() 1309 * instead of ps2_command() which would wait for 200ms for an ACK 1310 * that may never come. 1311 * As an additional quirk ALPS touchpads may not only forget to ACK 1312 * disable command but will stop reporting taps, so if we see that 1313 * mouse at least once ACKs disable we will do full reconnect if ACK 1314 * is missing. 1315 */ 1316 psmouse->num_resyncs++; 1317 1318 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) { 1319 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command) 1320 failed = true; 1321 } else 1322 psmouse->acks_disable_command = true; 1323 1324 /* 1325 * Poll the mouse. If it was reset the packet will be shorter than 1326 * psmouse->pktsize and ps2_command will fail. We do not expect and 1327 * do not handle scenario when mouse "upgrades" its protocol while 1328 * disconnected since it would require additional delay. If we ever 1329 * see a mouse that does it we'll adjust the code. 1330 */ 1331 if (!failed) { 1332 if (psmouse->poll(psmouse)) 1333 failed = true; 1334 else { 1335 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); 1336 for (i = 0; i < psmouse->pktsize; i++) { 1337 psmouse->pktcnt++; 1338 rc = psmouse->protocol_handler(psmouse); 1339 if (rc != PSMOUSE_GOOD_DATA) 1340 break; 1341 } 1342 if (rc != PSMOUSE_FULL_PACKET) 1343 failed = true; 1344 psmouse_set_state(psmouse, PSMOUSE_RESYNCING); 1345 } 1346 } 1347 1348 /* 1349 * Now try to enable mouse. We try to do that even if poll failed 1350 * and also repeat our attempts 5 times, otherwise we may be left 1351 * out with disabled mouse. 1352 */ 1353 for (i = 0; i < 5; i++) { 1354 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { 1355 enabled = true; 1356 break; 1357 } 1358 msleep(200); 1359 } 1360 1361 if (!enabled) { 1362 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n", 1363 psmouse->ps2dev.serio->phys); 1364 failed = true; 1365 } 1366 1367 if (failed) { 1368 psmouse_set_state(psmouse, PSMOUSE_IGNORE); 1369 psmouse_info(psmouse, 1370 "resync failed, issuing reconnect request\n"); 1371 serio_reconnect(serio); 1372 } else 1373 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); 1374 1375 if (parent) 1376 psmouse_activate(parent); 1377 out: 1378 mutex_unlock(&psmouse_mutex); 1379 } 1380 1381 /* 1382 * psmouse_cleanup() resets the mouse into power-on state. 1383 */ 1384 static void psmouse_cleanup(struct serio *serio) 1385 { 1386 struct psmouse *psmouse = serio_get_drvdata(serio); 1387 struct psmouse *parent = NULL; 1388 1389 mutex_lock(&psmouse_mutex); 1390 1391 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { 1392 parent = serio_get_drvdata(serio->parent); 1393 psmouse_deactivate(parent); 1394 } 1395 1396 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); 1397 1398 /* 1399 * Disable stream mode so cleanup routine can proceed undisturbed. 1400 */ 1401 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) 1402 psmouse_warn(psmouse, "Failed to disable mouse on %s\n", 1403 psmouse->ps2dev.serio->phys); 1404 1405 if (psmouse->cleanup) 1406 psmouse->cleanup(psmouse); 1407 1408 /* 1409 * Reset the mouse to defaults (bare PS/2 protocol). 1410 */ 1411 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS); 1412 1413 /* 1414 * Some boxes, such as HP nx7400, get terribly confused if mouse 1415 * is not fully enabled before suspending/shutting down. 1416 */ 1417 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE); 1418 1419 if (parent) { 1420 if (parent->pt_deactivate) 1421 parent->pt_deactivate(parent); 1422 1423 psmouse_activate(parent); 1424 } 1425 1426 mutex_unlock(&psmouse_mutex); 1427 } 1428 1429 /* 1430 * psmouse_disconnect() closes and frees. 1431 */ 1432 static void psmouse_disconnect(struct serio *serio) 1433 { 1434 struct psmouse *psmouse, *parent = NULL; 1435 1436 psmouse = serio_get_drvdata(serio); 1437 1438 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group); 1439 1440 mutex_lock(&psmouse_mutex); 1441 1442 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); 1443 1444 /* make sure we don't have a resync in progress */ 1445 mutex_unlock(&psmouse_mutex); 1446 flush_workqueue(kpsmoused_wq); 1447 mutex_lock(&psmouse_mutex); 1448 1449 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { 1450 parent = serio_get_drvdata(serio->parent); 1451 psmouse_deactivate(parent); 1452 } 1453 1454 if (psmouse->disconnect) 1455 psmouse->disconnect(psmouse); 1456 1457 if (parent && parent->pt_deactivate) 1458 parent->pt_deactivate(parent); 1459 1460 psmouse_set_state(psmouse, PSMOUSE_IGNORE); 1461 1462 serio_close(serio); 1463 serio_set_drvdata(serio, NULL); 1464 input_unregister_device(psmouse->dev); 1465 kfree(psmouse); 1466 1467 if (parent) 1468 psmouse_activate(parent); 1469 1470 mutex_unlock(&psmouse_mutex); 1471 } 1472 1473 static int psmouse_switch_protocol(struct psmouse *psmouse, 1474 const struct psmouse_protocol *proto) 1475 { 1476 const struct psmouse_protocol *selected_proto; 1477 struct input_dev *input_dev = psmouse->dev; 1478 1479 input_dev->dev.parent = &psmouse->ps2dev.serio->dev; 1480 1481 if (proto && (proto->detect || proto->init)) { 1482 psmouse_apply_defaults(psmouse); 1483 1484 if (proto->detect && proto->detect(psmouse, true) < 0) 1485 return -1; 1486 1487 if (proto->init && proto->init(psmouse) < 0) 1488 return -1; 1489 1490 psmouse->type = proto->type; 1491 selected_proto = proto; 1492 } else { 1493 psmouse->type = psmouse_extensions(psmouse, 1494 psmouse_max_proto, true); 1495 selected_proto = psmouse_protocol_by_type(psmouse->type); 1496 } 1497 1498 psmouse->ignore_parity = selected_proto->ignore_parity; 1499 1500 /* 1501 * If mouse's packet size is 3 there is no point in polling the 1502 * device in hopes to detect protocol reset - we won't get less 1503 * than 3 bytes response anyhow. 1504 */ 1505 if (psmouse->pktsize == 3) 1506 psmouse->resync_time = 0; 1507 1508 /* 1509 * Some smart KVMs fake response to POLL command returning just 1510 * 3 bytes and messing up our resync logic, so if initial poll 1511 * fails we won't try polling the device anymore. Hopefully 1512 * such KVM will maintain initially selected protocol. 1513 */ 1514 if (psmouse->resync_time && psmouse->poll(psmouse)) 1515 psmouse->resync_time = 0; 1516 1517 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s", 1518 selected_proto->name, psmouse->vendor, psmouse->name); 1519 1520 input_dev->name = psmouse->devname; 1521 input_dev->phys = psmouse->phys; 1522 input_dev->id.bustype = BUS_I8042; 1523 input_dev->id.vendor = 0x0002; 1524 input_dev->id.product = psmouse->type; 1525 input_dev->id.version = psmouse->model; 1526 1527 return 0; 1528 } 1529 1530 /* 1531 * psmouse_connect() is a callback from the serio module when 1532 * an unhandled serio port is found. 1533 */ 1534 static int psmouse_connect(struct serio *serio, struct serio_driver *drv) 1535 { 1536 struct psmouse *psmouse, *parent = NULL; 1537 struct input_dev *input_dev; 1538 int retval = 0, error = -ENOMEM; 1539 1540 mutex_lock(&psmouse_mutex); 1541 1542 /* 1543 * If this is a pass-through port deactivate parent so the device 1544 * connected to this port can be successfully identified 1545 */ 1546 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { 1547 parent = serio_get_drvdata(serio->parent); 1548 psmouse_deactivate(parent); 1549 } 1550 1551 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL); 1552 input_dev = input_allocate_device(); 1553 if (!psmouse || !input_dev) 1554 goto err_free; 1555 1556 ps2_init(&psmouse->ps2dev, serio); 1557 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync); 1558 psmouse->dev = input_dev; 1559 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys); 1560 1561 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); 1562 1563 serio_set_drvdata(serio, psmouse); 1564 1565 error = serio_open(serio, drv); 1566 if (error) 1567 goto err_clear_drvdata; 1568 1569 /* give PT device some time to settle down before probing */ 1570 if (serio->id.type == SERIO_PS_PSTHRU) 1571 usleep_range(10000, 15000); 1572 1573 if (psmouse_probe(psmouse) < 0) { 1574 error = -ENODEV; 1575 goto err_close_serio; 1576 } 1577 1578 psmouse->rate = psmouse_rate; 1579 psmouse->resolution = psmouse_resolution; 1580 psmouse->resetafter = psmouse_resetafter; 1581 psmouse->resync_time = parent ? 0 : psmouse_resync_time; 1582 psmouse->smartscroll = psmouse_smartscroll; 1583 1584 psmouse_switch_protocol(psmouse, NULL); 1585 1586 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); 1587 psmouse_initialize(psmouse); 1588 1589 error = input_register_device(psmouse->dev); 1590 if (error) 1591 goto err_protocol_disconnect; 1592 1593 if (parent && parent->pt_activate) 1594 parent->pt_activate(parent); 1595 1596 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group); 1597 if (error) 1598 goto err_pt_deactivate; 1599 1600 psmouse_activate(psmouse); 1601 1602 out: 1603 /* If this is a pass-through port the parent needs to be re-activated */ 1604 if (parent) 1605 psmouse_activate(parent); 1606 1607 mutex_unlock(&psmouse_mutex); 1608 return retval; 1609 1610 err_pt_deactivate: 1611 if (parent && parent->pt_deactivate) 1612 parent->pt_deactivate(parent); 1613 input_unregister_device(psmouse->dev); 1614 input_dev = NULL; /* so we don't try to free it below */ 1615 err_protocol_disconnect: 1616 if (psmouse->disconnect) 1617 psmouse->disconnect(psmouse); 1618 psmouse_set_state(psmouse, PSMOUSE_IGNORE); 1619 err_close_serio: 1620 serio_close(serio); 1621 err_clear_drvdata: 1622 serio_set_drvdata(serio, NULL); 1623 err_free: 1624 input_free_device(input_dev); 1625 kfree(psmouse); 1626 1627 retval = error; 1628 goto out; 1629 } 1630 1631 static int psmouse_reconnect(struct serio *serio) 1632 { 1633 struct psmouse *psmouse = serio_get_drvdata(serio); 1634 struct psmouse *parent = NULL; 1635 unsigned char type; 1636 int rc = -1; 1637 1638 mutex_lock(&psmouse_mutex); 1639 1640 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { 1641 parent = serio_get_drvdata(serio->parent); 1642 psmouse_deactivate(parent); 1643 } 1644 1645 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); 1646 1647 if (psmouse->reconnect) { 1648 if (psmouse->reconnect(psmouse)) 1649 goto out; 1650 } else { 1651 psmouse_reset(psmouse); 1652 1653 if (psmouse_probe(psmouse) < 0) 1654 goto out; 1655 1656 type = psmouse_extensions(psmouse, psmouse_max_proto, false); 1657 if (psmouse->type != type) 1658 goto out; 1659 } 1660 1661 /* 1662 * OK, the device type (and capabilities) match the old one, 1663 * we can continue using it, complete initialization 1664 */ 1665 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); 1666 1667 psmouse_initialize(psmouse); 1668 1669 if (parent && parent->pt_activate) 1670 parent->pt_activate(parent); 1671 1672 psmouse_activate(psmouse); 1673 rc = 0; 1674 1675 out: 1676 /* If this is a pass-through port the parent waits to be activated */ 1677 if (parent) 1678 psmouse_activate(parent); 1679 1680 mutex_unlock(&psmouse_mutex); 1681 return rc; 1682 } 1683 1684 static struct serio_device_id psmouse_serio_ids[] = { 1685 { 1686 .type = SERIO_8042, 1687 .proto = SERIO_ANY, 1688 .id = SERIO_ANY, 1689 .extra = SERIO_ANY, 1690 }, 1691 { 1692 .type = SERIO_PS_PSTHRU, 1693 .proto = SERIO_ANY, 1694 .id = SERIO_ANY, 1695 .extra = SERIO_ANY, 1696 }, 1697 { 0 } 1698 }; 1699 1700 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids); 1701 1702 static struct serio_driver psmouse_drv = { 1703 .driver = { 1704 .name = "psmouse", 1705 }, 1706 .description = DRIVER_DESC, 1707 .id_table = psmouse_serio_ids, 1708 .interrupt = psmouse_interrupt, 1709 .connect = psmouse_connect, 1710 .reconnect = psmouse_reconnect, 1711 .disconnect = psmouse_disconnect, 1712 .cleanup = psmouse_cleanup, 1713 }; 1714 1715 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr, 1716 char *buf) 1717 { 1718 struct serio *serio = to_serio_port(dev); 1719 struct psmouse_attribute *attr = to_psmouse_attr(devattr); 1720 struct psmouse *psmouse; 1721 1722 psmouse = serio_get_drvdata(serio); 1723 1724 return attr->show(psmouse, attr->data, buf); 1725 } 1726 1727 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr, 1728 const char *buf, size_t count) 1729 { 1730 struct serio *serio = to_serio_port(dev); 1731 struct psmouse_attribute *attr = to_psmouse_attr(devattr); 1732 struct psmouse *psmouse, *parent = NULL; 1733 int retval; 1734 1735 retval = mutex_lock_interruptible(&psmouse_mutex); 1736 if (retval) 1737 goto out; 1738 1739 psmouse = serio_get_drvdata(serio); 1740 1741 if (attr->protect) { 1742 if (psmouse->state == PSMOUSE_IGNORE) { 1743 retval = -ENODEV; 1744 goto out_unlock; 1745 } 1746 1747 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { 1748 parent = serio_get_drvdata(serio->parent); 1749 psmouse_deactivate(parent); 1750 } 1751 1752 psmouse_deactivate(psmouse); 1753 } 1754 1755 retval = attr->set(psmouse, attr->data, buf, count); 1756 1757 if (attr->protect) { 1758 if (retval != -ENODEV) 1759 psmouse_activate(psmouse); 1760 1761 if (parent) 1762 psmouse_activate(parent); 1763 } 1764 1765 out_unlock: 1766 mutex_unlock(&psmouse_mutex); 1767 out: 1768 return retval; 1769 } 1770 1771 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf) 1772 { 1773 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset); 1774 1775 return sprintf(buf, "%u\n", *field); 1776 } 1777 1778 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count) 1779 { 1780 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset); 1781 unsigned int value; 1782 int err; 1783 1784 err = kstrtouint(buf, 10, &value); 1785 if (err) 1786 return err; 1787 1788 *field = value; 1789 1790 return count; 1791 } 1792 1793 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf) 1794 { 1795 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name); 1796 } 1797 1798 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count) 1799 { 1800 struct serio *serio = psmouse->ps2dev.serio; 1801 struct psmouse *parent = NULL; 1802 struct input_dev *old_dev, *new_dev; 1803 const struct psmouse_protocol *proto, *old_proto; 1804 int error; 1805 int retry = 0; 1806 1807 proto = psmouse_protocol_by_name(buf, count); 1808 if (!proto) 1809 return -EINVAL; 1810 1811 if (psmouse->type == proto->type) 1812 return count; 1813 1814 new_dev = input_allocate_device(); 1815 if (!new_dev) 1816 return -ENOMEM; 1817 1818 while (!list_empty(&serio->children)) { 1819 if (++retry > 3) { 1820 psmouse_warn(psmouse, 1821 "failed to destroy children ports, protocol change aborted.\n"); 1822 input_free_device(new_dev); 1823 return -EIO; 1824 } 1825 1826 mutex_unlock(&psmouse_mutex); 1827 serio_unregister_child_port(serio); 1828 mutex_lock(&psmouse_mutex); 1829 1830 if (serio->drv != &psmouse_drv) { 1831 input_free_device(new_dev); 1832 return -ENODEV; 1833 } 1834 1835 if (psmouse->type == proto->type) { 1836 input_free_device(new_dev); 1837 return count; /* switched by other thread */ 1838 } 1839 } 1840 1841 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { 1842 parent = serio_get_drvdata(serio->parent); 1843 if (parent->pt_deactivate) 1844 parent->pt_deactivate(parent); 1845 } 1846 1847 old_dev = psmouse->dev; 1848 old_proto = psmouse_protocol_by_type(psmouse->type); 1849 1850 if (psmouse->disconnect) 1851 psmouse->disconnect(psmouse); 1852 1853 psmouse_set_state(psmouse, PSMOUSE_IGNORE); 1854 1855 psmouse->dev = new_dev; 1856 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); 1857 1858 if (psmouse_switch_protocol(psmouse, proto) < 0) { 1859 psmouse_reset(psmouse); 1860 /* default to PSMOUSE_PS2 */ 1861 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]); 1862 } 1863 1864 psmouse_initialize(psmouse); 1865 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); 1866 1867 error = input_register_device(psmouse->dev); 1868 if (error) { 1869 if (psmouse->disconnect) 1870 psmouse->disconnect(psmouse); 1871 1872 psmouse_set_state(psmouse, PSMOUSE_IGNORE); 1873 input_free_device(new_dev); 1874 psmouse->dev = old_dev; 1875 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); 1876 psmouse_switch_protocol(psmouse, old_proto); 1877 psmouse_initialize(psmouse); 1878 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); 1879 1880 return error; 1881 } 1882 1883 input_unregister_device(old_dev); 1884 1885 if (parent && parent->pt_activate) 1886 parent->pt_activate(parent); 1887 1888 return count; 1889 } 1890 1891 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count) 1892 { 1893 unsigned int value; 1894 int err; 1895 1896 err = kstrtouint(buf, 10, &value); 1897 if (err) 1898 return err; 1899 1900 psmouse->set_rate(psmouse, value); 1901 return count; 1902 } 1903 1904 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count) 1905 { 1906 unsigned int value; 1907 int err; 1908 1909 err = kstrtouint(buf, 10, &value); 1910 if (err) 1911 return err; 1912 1913 psmouse->set_resolution(psmouse, value); 1914 return count; 1915 } 1916 1917 1918 static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp) 1919 { 1920 const struct psmouse_protocol *proto; 1921 1922 if (!val) 1923 return -EINVAL; 1924 1925 proto = psmouse_protocol_by_name(val, strlen(val)); 1926 1927 if (!proto || !proto->maxproto) 1928 return -EINVAL; 1929 1930 *((unsigned int *)kp->arg) = proto->type; 1931 1932 return 0; 1933 } 1934 1935 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp) 1936 { 1937 int type = *((unsigned int *)kp->arg); 1938 1939 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name); 1940 } 1941 1942 static int __init psmouse_init(void) 1943 { 1944 int err; 1945 1946 lifebook_module_init(); 1947 synaptics_module_init(); 1948 hgpk_module_init(); 1949 1950 kpsmoused_wq = alloc_ordered_workqueue("kpsmoused", 0); 1951 if (!kpsmoused_wq) { 1952 pr_err("failed to create kpsmoused workqueue\n"); 1953 return -ENOMEM; 1954 } 1955 1956 err = serio_register_driver(&psmouse_drv); 1957 if (err) 1958 destroy_workqueue(kpsmoused_wq); 1959 1960 return err; 1961 } 1962 1963 static void __exit psmouse_exit(void) 1964 { 1965 serio_unregister_driver(&psmouse_drv); 1966 destroy_workqueue(kpsmoused_wq); 1967 } 1968 1969 module_init(psmouse_init); 1970 module_exit(psmouse_exit); 1971