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