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