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