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