Linux 2.6.34-rc3
[pohmelfs.git] / drivers / input / mouse / psmouse-base.c
blobd8c0c8d6992c92936db326dc28920350b899272d
1 /*
2 * PS/2 mouse driver
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
6 */
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.
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>
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 #include "sentelic.h"
35 #define DRIVER_DESC "PS/2 mouse driver"
37 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
38 MODULE_DESCRIPTION(DRIVER_DESC);
39 MODULE_LICENSE("GPL");
41 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
42 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
43 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
44 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
45 #define param_set_proto_abbrev psmouse_set_maxproto
46 #define param_get_proto_abbrev psmouse_get_maxproto
47 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
48 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
50 static unsigned int psmouse_resolution = 200;
51 module_param_named(resolution, psmouse_resolution, uint, 0644);
52 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
54 static unsigned int psmouse_rate = 100;
55 module_param_named(rate, psmouse_rate, uint, 0644);
56 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
58 static unsigned int psmouse_smartscroll = 1;
59 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
60 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
62 static unsigned int psmouse_resetafter = 5;
63 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
64 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
66 static unsigned int psmouse_resync_time;
67 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
68 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
70 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
71 NULL,
72 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
73 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
74 (void *) offsetof(struct psmouse, rate),
75 psmouse_show_int_attr, psmouse_attr_set_rate);
76 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
77 (void *) offsetof(struct psmouse, resolution),
78 psmouse_show_int_attr, psmouse_attr_set_resolution);
79 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
80 (void *) offsetof(struct psmouse, resetafter),
81 psmouse_show_int_attr, psmouse_set_int_attr);
82 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
83 (void *) offsetof(struct psmouse, resync_time),
84 psmouse_show_int_attr, psmouse_set_int_attr);
86 static struct attribute *psmouse_attributes[] = {
87 &psmouse_attr_protocol.dattr.attr,
88 &psmouse_attr_rate.dattr.attr,
89 &psmouse_attr_resolution.dattr.attr,
90 &psmouse_attr_resetafter.dattr.attr,
91 &psmouse_attr_resync_time.dattr.attr,
92 NULL
95 static struct attribute_group psmouse_attribute_group = {
96 .attrs = psmouse_attributes,
100 * psmouse_mutex protects all operations changing state of mouse
101 * (connecting, disconnecting, changing rate or resolution via
102 * sysfs). We could use a per-device semaphore but since there
103 * rarely more than one PS/2 mouse connected and since semaphore
104 * is taken in "slow" paths it is not worth it.
106 static DEFINE_MUTEX(psmouse_mutex);
108 static struct workqueue_struct *kpsmoused_wq;
110 struct psmouse_protocol {
111 enum psmouse_type type;
112 bool maxproto;
113 const char *name;
114 const char *alias;
115 int (*detect)(struct psmouse *, bool);
116 int (*init)(struct psmouse *);
120 * psmouse_process_byte() analyzes the PS/2 data stream and reports
121 * relevant events to the input module once full packet has arrived.
124 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
126 struct input_dev *dev = psmouse->dev;
127 unsigned char *packet = psmouse->packet;
129 if (psmouse->pktcnt < psmouse->pktsize)
130 return PSMOUSE_GOOD_DATA;
133 * Full packet accumulated, process it
137 * Scroll wheel on IntelliMice, scroll buttons on NetMice
140 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
141 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
144 * Scroll wheel and buttons on IntelliMouse Explorer
147 if (psmouse->type == PSMOUSE_IMEX) {
148 switch (packet[3] & 0xC0) {
149 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
150 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
151 break;
152 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
153 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154 break;
155 case 0x00:
156 case 0xC0:
157 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
158 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
159 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
160 break;
165 * Extra buttons on Genius NewNet 3D
168 if (psmouse->type == PSMOUSE_GENPS) {
169 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
170 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
174 * Extra button on ThinkingMouse
176 if (psmouse->type == PSMOUSE_THINKPS) {
177 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
178 /* Without this bit of weirdness moving up gives wildly high Y changes. */
179 packet[1] |= (packet[0] & 0x40) << 1;
183 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
184 * byte.
186 if (psmouse->type == PSMOUSE_CORTRON) {
187 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
188 packet[0] |= 0x08;
192 * Generic PS/2 Mouse
195 input_report_key(dev, BTN_LEFT, packet[0] & 1);
196 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
197 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
199 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
200 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
202 input_sync(dev);
204 return PSMOUSE_FULL_PACKET;
207 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
208 unsigned long delay)
210 queue_delayed_work(kpsmoused_wq, work, delay);
214 * __psmouse_set_state() sets new psmouse state and resets all flags.
217 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
219 psmouse->state = new_state;
220 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
221 psmouse->ps2dev.flags = 0;
222 psmouse->last = jiffies;
227 * psmouse_set_state() sets new psmouse state and resets all flags and
228 * counters while holding serio lock so fighting with interrupt handler
229 * is not a concern.
232 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
234 serio_pause_rx(psmouse->ps2dev.serio);
235 __psmouse_set_state(psmouse, new_state);
236 serio_continue_rx(psmouse->ps2dev.serio);
240 * psmouse_handle_byte() processes one byte of the input data stream
241 * by calling corresponding protocol handler.
244 static int psmouse_handle_byte(struct psmouse *psmouse)
246 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
248 switch (rc) {
249 case PSMOUSE_BAD_DATA:
250 if (psmouse->state == PSMOUSE_ACTIVATED) {
251 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
252 psmouse->name, psmouse->phys, psmouse->pktcnt);
253 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
254 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
255 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
256 serio_reconnect(psmouse->ps2dev.serio);
257 return -1;
260 psmouse->pktcnt = 0;
261 break;
263 case PSMOUSE_FULL_PACKET:
264 psmouse->pktcnt = 0;
265 if (psmouse->out_of_sync_cnt) {
266 psmouse->out_of_sync_cnt = 0;
267 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
268 psmouse->name, psmouse->phys);
270 break;
272 case PSMOUSE_GOOD_DATA:
273 break;
275 return 0;
279 * psmouse_interrupt() handles incoming characters, either passing them
280 * for normal processing or gathering them as command response.
283 static irqreturn_t psmouse_interrupt(struct serio *serio,
284 unsigned char data, unsigned int flags)
286 struct psmouse *psmouse = serio_get_drvdata(serio);
288 if (psmouse->state == PSMOUSE_IGNORE)
289 goto out;
291 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
292 if (psmouse->state == PSMOUSE_ACTIVATED)
293 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
294 flags & SERIO_TIMEOUT ? " timeout" : "",
295 flags & SERIO_PARITY ? " bad parity" : "");
296 ps2_cmd_aborted(&psmouse->ps2dev);
297 goto out;
300 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
301 if (ps2_handle_ack(&psmouse->ps2dev, data))
302 goto out;
304 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
305 if (ps2_handle_response(&psmouse->ps2dev, data))
306 goto out;
308 if (psmouse->state <= PSMOUSE_RESYNCING)
309 goto out;
311 if (psmouse->state == PSMOUSE_ACTIVATED &&
312 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
313 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
314 psmouse->name, psmouse->phys, psmouse->pktcnt);
315 psmouse->badbyte = psmouse->packet[0];
316 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
317 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
318 goto out;
321 psmouse->packet[psmouse->pktcnt++] = data;
323 * Check if this is a new device announcement (0xAA 0x00)
325 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
326 if (psmouse->pktcnt == 1) {
327 psmouse->last = jiffies;
328 goto out;
331 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
332 (psmouse->type == PSMOUSE_HGPK &&
333 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
334 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
335 serio_reconnect(serio);
336 goto out;
339 * Not a new device, try processing first byte normally
341 psmouse->pktcnt = 1;
342 if (psmouse_handle_byte(psmouse))
343 goto out;
345 psmouse->packet[psmouse->pktcnt++] = data;
349 * See if we need to force resync because mouse was idle for too long
351 if (psmouse->state == PSMOUSE_ACTIVATED &&
352 psmouse->pktcnt == 1 && psmouse->resync_time &&
353 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
354 psmouse->badbyte = psmouse->packet[0];
355 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
356 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
357 goto out;
360 psmouse->last = jiffies;
361 psmouse_handle_byte(psmouse);
363 out:
364 return IRQ_HANDLED;
369 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
370 * using sliced syntax, understood by advanced devices, such as Logitech
371 * or Synaptics touchpads. The command is encoded as:
372 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
373 * is the command.
375 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
377 int i;
379 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
380 return -1;
382 for (i = 6; i >= 0; i -= 2) {
383 unsigned char d = (command >> i) & 3;
384 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
385 return -1;
388 return 0;
393 * psmouse_reset() resets the mouse into power-on state.
395 int psmouse_reset(struct psmouse *psmouse)
397 unsigned char param[2];
399 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
400 return -1;
402 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
403 return -1;
405 return 0;
410 * Genius NetMouse magic init.
412 static int genius_detect(struct psmouse *psmouse, bool set_properties)
414 struct ps2dev *ps2dev = &psmouse->ps2dev;
415 unsigned char param[4];
417 param[0] = 3;
418 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
419 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
420 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
421 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
422 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
424 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
425 return -1;
427 if (set_properties) {
428 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
429 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
430 __set_bit(BTN_SIDE, psmouse->dev->keybit);
431 __set_bit(REL_WHEEL, psmouse->dev->relbit);
433 psmouse->vendor = "Genius";
434 psmouse->name = "Mouse";
435 psmouse->pktsize = 4;
438 return 0;
442 * IntelliMouse magic init.
444 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
446 struct ps2dev *ps2dev = &psmouse->ps2dev;
447 unsigned char param[2];
449 param[0] = 200;
450 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
451 param[0] = 100;
452 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
453 param[0] = 80;
454 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
455 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
457 if (param[0] != 3)
458 return -1;
460 if (set_properties) {
461 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
462 __set_bit(REL_WHEEL, psmouse->dev->relbit);
464 if (!psmouse->vendor)
465 psmouse->vendor = "Generic";
466 if (!psmouse->name)
467 psmouse->name = "Wheel Mouse";
468 psmouse->pktsize = 4;
471 return 0;
475 * Try IntelliMouse/Explorer magic init.
477 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
479 struct ps2dev *ps2dev = &psmouse->ps2dev;
480 unsigned char param[2];
482 intellimouse_detect(psmouse, 0);
484 param[0] = 200;
485 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
486 param[0] = 200;
487 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
488 param[0] = 80;
489 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
490 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
492 if (param[0] != 4)
493 return -1;
495 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
496 param[0] = 200;
497 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
498 param[0] = 80;
499 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
500 param[0] = 40;
501 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
503 if (set_properties) {
504 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
505 __set_bit(REL_WHEEL, psmouse->dev->relbit);
506 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
507 __set_bit(BTN_SIDE, psmouse->dev->keybit);
508 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
510 if (!psmouse->vendor)
511 psmouse->vendor = "Generic";
512 if (!psmouse->name)
513 psmouse->name = "Explorer Mouse";
514 psmouse->pktsize = 4;
517 return 0;
521 * Kensington ThinkingMouse / ExpertMouse magic init.
523 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
525 struct ps2dev *ps2dev = &psmouse->ps2dev;
526 unsigned char param[2];
527 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
528 int i;
530 param[0] = 10;
531 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
532 param[0] = 0;
533 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
534 for (i = 0; i < ARRAY_SIZE(seq); i++) {
535 param[0] = seq[i];
536 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
538 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
540 if (param[0] != 2)
541 return -1;
543 if (set_properties) {
544 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
545 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
547 psmouse->vendor = "Kensington";
548 psmouse->name = "ThinkingMouse";
551 return 0;
555 * Bare PS/2 protocol "detection". Always succeeds.
557 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
559 if (set_properties) {
560 if (!psmouse->vendor)
561 psmouse->vendor = "Generic";
562 if (!psmouse->name)
563 psmouse->name = "Mouse";
566 * We have no way of figuring true number of buttons so let's
567 * assume that the device has 3.
569 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
572 return 0;
576 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
577 * must be forced by sysfs protocol writing.
579 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
581 if (set_properties) {
582 psmouse->vendor = "Cortron";
583 psmouse->name = "PS/2 Trackball";
585 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
586 __set_bit(BTN_SIDE, psmouse->dev->keybit);
589 return 0;
593 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
594 * the mouse may have.
597 static int psmouse_extensions(struct psmouse *psmouse,
598 unsigned int max_proto, bool set_properties)
600 bool synaptics_hardware = false;
603 * We always check for lifebook because it does not disturb mouse
604 * (it only checks DMI information).
606 if (lifebook_detect(psmouse, set_properties) == 0) {
607 if (max_proto > PSMOUSE_IMEX) {
608 if (!set_properties || lifebook_init(psmouse) == 0)
609 return PSMOUSE_LIFEBOOK;
614 * Try Kensington ThinkingMouse (we try first, because synaptics probe
615 * upsets the thinkingmouse).
618 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
619 return PSMOUSE_THINKPS;
622 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
623 * support is disabled in config - we need to know if it is synaptics so we
624 * can reset it properly after probing for intellimouse.
626 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
627 synaptics_hardware = true;
629 if (max_proto > PSMOUSE_IMEX) {
631 * Try activating protocol, but check if support is enabled first, since
632 * we try detecting Synaptics even when protocol is disabled.
634 if (synaptics_supported() &&
635 (!set_properties || synaptics_init(psmouse) == 0)) {
636 return PSMOUSE_SYNAPTICS;
640 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
641 * Unfortunately Logitech/Genius probes confuse some firmware versions so
642 * we'll have to skip them.
644 max_proto = PSMOUSE_IMEX;
647 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
649 synaptics_reset(psmouse);
653 * Try ALPS TouchPad
655 if (max_proto > PSMOUSE_IMEX) {
656 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
657 if (alps_detect(psmouse, set_properties) == 0) {
658 if (!set_properties || alps_init(psmouse) == 0)
659 return PSMOUSE_ALPS;
661 * Init failed, try basic relative protocols
663 max_proto = PSMOUSE_IMEX;
668 * Try OLPC HGPK touchpad.
670 if (max_proto > PSMOUSE_IMEX &&
671 hgpk_detect(psmouse, set_properties) == 0) {
672 if (!set_properties || hgpk_init(psmouse) == 0)
673 return PSMOUSE_HGPK;
675 * Init failed, try basic relative protocols
677 max_proto = PSMOUSE_IMEX;
681 * Try Elantech touchpad.
683 if (max_proto > PSMOUSE_IMEX &&
684 elantech_detect(psmouse, set_properties) == 0) {
685 if (!set_properties || elantech_init(psmouse) == 0)
686 return PSMOUSE_ELANTECH;
688 * Init failed, try basic relative protocols
690 max_proto = PSMOUSE_IMEX;
694 if (max_proto > PSMOUSE_IMEX) {
695 if (genius_detect(psmouse, set_properties) == 0)
696 return PSMOUSE_GENPS;
698 if (ps2pp_init(psmouse, set_properties) == 0)
699 return PSMOUSE_PS2PP;
701 if (trackpoint_detect(psmouse, set_properties) == 0)
702 return PSMOUSE_TRACKPOINT;
704 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
705 return PSMOUSE_TOUCHKIT_PS2;
709 * Try Finger Sensing Pad. We do it here because its probe upsets
710 * Trackpoint devices (causing TP_READ_ID command to time out).
712 if (max_proto > PSMOUSE_IMEX) {
713 if (fsp_detect(psmouse, set_properties) == 0) {
714 if (!set_properties || fsp_init(psmouse) == 0)
715 return PSMOUSE_FSP;
717 * Init failed, try basic relative protocols
719 max_proto = PSMOUSE_IMEX;
724 * Reset to defaults in case the device got confused by extended
725 * protocol probes. Note that we follow up with full reset because
726 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
728 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
729 psmouse_reset(psmouse);
731 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
732 return PSMOUSE_IMEX;
734 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
735 return PSMOUSE_IMPS;
738 * Okay, all failed, we have a standard mouse here. The number of the buttons
739 * is still a question, though. We assume 3.
741 ps2bare_detect(psmouse, set_properties);
743 if (synaptics_hardware) {
745 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
746 * We need to reset the touchpad because if there is a track point on the
747 * pass through port it could get disabled while probing for protocol
748 * extensions.
750 psmouse_reset(psmouse);
753 return PSMOUSE_PS2;
756 static const struct psmouse_protocol psmouse_protocols[] = {
758 .type = PSMOUSE_PS2,
759 .name = "PS/2",
760 .alias = "bare",
761 .maxproto = true,
762 .detect = ps2bare_detect,
764 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
766 .type = PSMOUSE_PS2PP,
767 .name = "PS2++",
768 .alias = "logitech",
769 .detect = ps2pp_init,
771 #endif
773 .type = PSMOUSE_THINKPS,
774 .name = "ThinkPS/2",
775 .alias = "thinkps",
776 .detect = thinking_detect,
779 .type = PSMOUSE_GENPS,
780 .name = "GenPS/2",
781 .alias = "genius",
782 .detect = genius_detect,
785 .type = PSMOUSE_IMPS,
786 .name = "ImPS/2",
787 .alias = "imps",
788 .maxproto = true,
789 .detect = intellimouse_detect,
792 .type = PSMOUSE_IMEX,
793 .name = "ImExPS/2",
794 .alias = "exps",
795 .maxproto = true,
796 .detect = im_explorer_detect,
798 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
800 .type = PSMOUSE_SYNAPTICS,
801 .name = "SynPS/2",
802 .alias = "synaptics",
803 .detect = synaptics_detect,
804 .init = synaptics_init,
806 #endif
807 #ifdef CONFIG_MOUSE_PS2_ALPS
809 .type = PSMOUSE_ALPS,
810 .name = "AlpsPS/2",
811 .alias = "alps",
812 .detect = alps_detect,
813 .init = alps_init,
815 #endif
816 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
818 .type = PSMOUSE_LIFEBOOK,
819 .name = "LBPS/2",
820 .alias = "lifebook",
821 .init = lifebook_init,
823 #endif
824 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
826 .type = PSMOUSE_TRACKPOINT,
827 .name = "TPPS/2",
828 .alias = "trackpoint",
829 .detect = trackpoint_detect,
831 #endif
832 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
834 .type = PSMOUSE_TOUCHKIT_PS2,
835 .name = "touchkitPS/2",
836 .alias = "touchkit",
837 .detect = touchkit_ps2_detect,
839 #endif
840 #ifdef CONFIG_MOUSE_PS2_OLPC
842 .type = PSMOUSE_HGPK,
843 .name = "OLPC HGPK",
844 .alias = "hgpk",
845 .detect = hgpk_detect,
847 #endif
848 #ifdef CONFIG_MOUSE_PS2_ELANTECH
850 .type = PSMOUSE_ELANTECH,
851 .name = "ETPS/2",
852 .alias = "elantech",
853 .detect = elantech_detect,
854 .init = elantech_init,
856 #endif
857 #ifdef CONFIG_MOUSE_PS2_SENTELIC
859 .type = PSMOUSE_FSP,
860 .name = "FSPPS/2",
861 .alias = "fsp",
862 .detect = fsp_detect,
863 .init = fsp_init,
865 #endif
867 .type = PSMOUSE_CORTRON,
868 .name = "CortronPS/2",
869 .alias = "cortps",
870 .detect = cortron_detect,
873 .type = PSMOUSE_AUTO,
874 .name = "auto",
875 .alias = "any",
876 .maxproto = true,
880 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
882 int i;
884 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
885 if (psmouse_protocols[i].type == type)
886 return &psmouse_protocols[i];
888 WARN_ON(1);
889 return &psmouse_protocols[0];
892 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
894 const struct psmouse_protocol *p;
895 int i;
897 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
898 p = &psmouse_protocols[i];
900 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
901 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
902 return &psmouse_protocols[i];
905 return NULL;
910 * psmouse_probe() probes for a PS/2 mouse.
913 static int psmouse_probe(struct psmouse *psmouse)
915 struct ps2dev *ps2dev = &psmouse->ps2dev;
916 unsigned char param[2];
919 * First, we check if it's a mouse. It should send 0x00 or 0x03
920 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
921 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
922 * ID queries, probably due to a firmware bug.
925 param[0] = 0xa5;
926 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
927 return -1;
929 if (param[0] != 0x00 && param[0] != 0x03 &&
930 param[0] != 0x04 && param[0] != 0xff)
931 return -1;
934 * Then we reset and disable the mouse so that it doesn't generate events.
937 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
938 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
940 return 0;
944 * Here we set the mouse resolution.
947 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
949 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
950 unsigned char p;
952 if (resolution == 0 || resolution > 200)
953 resolution = 200;
955 p = params[resolution / 50];
956 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
957 psmouse->resolution = 25 << p;
961 * Here we set the mouse report rate.
964 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
966 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
967 unsigned char r;
968 int i = 0;
970 while (rates[i] > rate) i++;
971 r = rates[i];
972 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
973 psmouse->rate = r;
977 * psmouse_initialize() initializes the mouse to a sane state.
980 static void psmouse_initialize(struct psmouse *psmouse)
983 * We set the mouse report rate, resolution and scaling.
986 if (psmouse_max_proto != PSMOUSE_PS2) {
987 psmouse->set_rate(psmouse, psmouse->rate);
988 psmouse->set_resolution(psmouse, psmouse->resolution);
989 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
994 * psmouse_activate() enables the mouse so that we get motion reports from it.
997 static void psmouse_activate(struct psmouse *psmouse)
999 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1000 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
1001 psmouse->ps2dev.serio->phys);
1003 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1008 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
1009 * reports from it unless we explicitly request it.
1012 static void psmouse_deactivate(struct psmouse *psmouse)
1014 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1015 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
1016 psmouse->ps2dev.serio->phys);
1018 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1022 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
1025 static int psmouse_poll(struct psmouse *psmouse)
1027 return ps2_command(&psmouse->ps2dev, psmouse->packet,
1028 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1033 * psmouse_resync() attempts to re-validate current protocol.
1036 static void psmouse_resync(struct work_struct *work)
1038 struct psmouse *parent = NULL, *psmouse =
1039 container_of(work, struct psmouse, resync_work.work);
1040 struct serio *serio = psmouse->ps2dev.serio;
1041 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1042 bool failed = false, enabled = false;
1043 int i;
1045 mutex_lock(&psmouse_mutex);
1047 if (psmouse->state != PSMOUSE_RESYNCING)
1048 goto out;
1050 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1051 parent = serio_get_drvdata(serio->parent);
1052 psmouse_deactivate(parent);
1056 * Some mice don't ACK commands sent while they are in the middle of
1057 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1058 * instead of ps2_command() which would wait for 200ms for an ACK
1059 * that may never come.
1060 * As an additional quirk ALPS touchpads may not only forget to ACK
1061 * disable command but will stop reporting taps, so if we see that
1062 * mouse at least once ACKs disable we will do full reconnect if ACK
1063 * is missing.
1065 psmouse->num_resyncs++;
1067 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1068 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1069 failed = true;
1070 } else
1071 psmouse->acks_disable_command = true;
1074 * Poll the mouse. If it was reset the packet will be shorter than
1075 * psmouse->pktsize and ps2_command will fail. We do not expect and
1076 * do not handle scenario when mouse "upgrades" its protocol while
1077 * disconnected since it would require additional delay. If we ever
1078 * see a mouse that does it we'll adjust the code.
1080 if (!failed) {
1081 if (psmouse->poll(psmouse))
1082 failed = true;
1083 else {
1084 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1085 for (i = 0; i < psmouse->pktsize; i++) {
1086 psmouse->pktcnt++;
1087 rc = psmouse->protocol_handler(psmouse);
1088 if (rc != PSMOUSE_GOOD_DATA)
1089 break;
1091 if (rc != PSMOUSE_FULL_PACKET)
1092 failed = true;
1093 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1097 * Now try to enable mouse. We try to do that even if poll failed and also
1098 * repeat our attempts 5 times, otherwise we may be left out with disabled
1099 * mouse.
1101 for (i = 0; i < 5; i++) {
1102 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1103 enabled = true;
1104 break;
1106 msleep(200);
1109 if (!enabled) {
1110 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1111 psmouse->ps2dev.serio->phys);
1112 failed = true;
1115 if (failed) {
1116 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1117 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1118 serio_reconnect(serio);
1119 } else
1120 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1122 if (parent)
1123 psmouse_activate(parent);
1124 out:
1125 mutex_unlock(&psmouse_mutex);
1129 * psmouse_cleanup() resets the mouse into power-on state.
1132 static void psmouse_cleanup(struct serio *serio)
1134 struct psmouse *psmouse = serio_get_drvdata(serio);
1135 struct psmouse *parent = NULL;
1137 mutex_lock(&psmouse_mutex);
1139 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1140 parent = serio_get_drvdata(serio->parent);
1141 psmouse_deactivate(parent);
1144 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1147 * Disable stream mode so cleanup routine can proceed undisturbed.
1149 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1150 printk(KERN_WARNING "psmouse.c: Failed to disable mouse on %s\n",
1151 psmouse->ps2dev.serio->phys);
1153 if (psmouse->cleanup)
1154 psmouse->cleanup(psmouse);
1157 * Reset the mouse to defaults (bare PS/2 protocol).
1159 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1162 * Some boxes, such as HP nx7400, get terribly confused if mouse
1163 * is not fully enabled before suspending/shutting down.
1165 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1167 if (parent) {
1168 if (parent->pt_deactivate)
1169 parent->pt_deactivate(parent);
1171 psmouse_activate(parent);
1174 mutex_unlock(&psmouse_mutex);
1178 * psmouse_disconnect() closes and frees.
1181 static void psmouse_disconnect(struct serio *serio)
1183 struct psmouse *psmouse, *parent = NULL;
1185 psmouse = serio_get_drvdata(serio);
1187 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1189 mutex_lock(&psmouse_mutex);
1191 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1193 /* make sure we don't have a resync in progress */
1194 mutex_unlock(&psmouse_mutex);
1195 flush_workqueue(kpsmoused_wq);
1196 mutex_lock(&psmouse_mutex);
1198 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1199 parent = serio_get_drvdata(serio->parent);
1200 psmouse_deactivate(parent);
1203 if (psmouse->disconnect)
1204 psmouse->disconnect(psmouse);
1206 if (parent && parent->pt_deactivate)
1207 parent->pt_deactivate(parent);
1209 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1211 serio_close(serio);
1212 serio_set_drvdata(serio, NULL);
1213 input_unregister_device(psmouse->dev);
1214 kfree(psmouse);
1216 if (parent)
1217 psmouse_activate(parent);
1219 mutex_unlock(&psmouse_mutex);
1222 static int psmouse_switch_protocol(struct psmouse *psmouse,
1223 const struct psmouse_protocol *proto)
1225 struct input_dev *input_dev = psmouse->dev;
1227 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1229 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1230 input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1231 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1232 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1234 psmouse->set_rate = psmouse_set_rate;
1235 psmouse->set_resolution = psmouse_set_resolution;
1236 psmouse->poll = psmouse_poll;
1237 psmouse->protocol_handler = psmouse_process_byte;
1238 psmouse->pktsize = 3;
1240 if (proto && (proto->detect || proto->init)) {
1241 if (proto->detect && proto->detect(psmouse, 1) < 0)
1242 return -1;
1244 if (proto->init && proto->init(psmouse) < 0)
1245 return -1;
1247 psmouse->type = proto->type;
1248 } else
1249 psmouse->type = psmouse_extensions(psmouse,
1250 psmouse_max_proto, true);
1253 * If mouse's packet size is 3 there is no point in polling the
1254 * device in hopes to detect protocol reset - we won't get less
1255 * than 3 bytes response anyhow.
1257 if (psmouse->pktsize == 3)
1258 psmouse->resync_time = 0;
1261 * Some smart KVMs fake response to POLL command returning just
1262 * 3 bytes and messing up our resync logic, so if initial poll
1263 * fails we won't try polling the device anymore. Hopefully
1264 * such KVM will maintain initially selected protocol.
1266 if (psmouse->resync_time && psmouse->poll(psmouse))
1267 psmouse->resync_time = 0;
1269 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1270 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1272 input_dev->name = psmouse->devname;
1273 input_dev->phys = psmouse->phys;
1274 input_dev->id.bustype = BUS_I8042;
1275 input_dev->id.vendor = 0x0002;
1276 input_dev->id.product = psmouse->type;
1277 input_dev->id.version = psmouse->model;
1279 return 0;
1283 * psmouse_connect() is a callback from the serio module when
1284 * an unhandled serio port is found.
1286 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1288 struct psmouse *psmouse, *parent = NULL;
1289 struct input_dev *input_dev;
1290 int retval = 0, error = -ENOMEM;
1292 mutex_lock(&psmouse_mutex);
1295 * If this is a pass-through port deactivate parent so the device
1296 * connected to this port can be successfully identified
1298 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1299 parent = serio_get_drvdata(serio->parent);
1300 psmouse_deactivate(parent);
1303 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1304 input_dev = input_allocate_device();
1305 if (!psmouse || !input_dev)
1306 goto err_free;
1308 ps2_init(&psmouse->ps2dev, serio);
1309 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1310 psmouse->dev = input_dev;
1311 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1313 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1315 serio_set_drvdata(serio, psmouse);
1317 error = serio_open(serio, drv);
1318 if (error)
1319 goto err_clear_drvdata;
1321 if (psmouse_probe(psmouse) < 0) {
1322 error = -ENODEV;
1323 goto err_close_serio;
1326 psmouse->rate = psmouse_rate;
1327 psmouse->resolution = psmouse_resolution;
1328 psmouse->resetafter = psmouse_resetafter;
1329 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1330 psmouse->smartscroll = psmouse_smartscroll;
1332 psmouse_switch_protocol(psmouse, NULL);
1334 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1335 psmouse_initialize(psmouse);
1337 error = input_register_device(psmouse->dev);
1338 if (error)
1339 goto err_protocol_disconnect;
1341 if (parent && parent->pt_activate)
1342 parent->pt_activate(parent);
1344 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1345 if (error)
1346 goto err_pt_deactivate;
1348 psmouse_activate(psmouse);
1350 out:
1351 /* If this is a pass-through port the parent needs to be re-activated */
1352 if (parent)
1353 psmouse_activate(parent);
1355 mutex_unlock(&psmouse_mutex);
1356 return retval;
1358 err_pt_deactivate:
1359 if (parent && parent->pt_deactivate)
1360 parent->pt_deactivate(parent);
1361 input_unregister_device(psmouse->dev);
1362 input_dev = NULL; /* so we don't try to free it below */
1363 err_protocol_disconnect:
1364 if (psmouse->disconnect)
1365 psmouse->disconnect(psmouse);
1366 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1367 err_close_serio:
1368 serio_close(serio);
1369 err_clear_drvdata:
1370 serio_set_drvdata(serio, NULL);
1371 err_free:
1372 input_free_device(input_dev);
1373 kfree(psmouse);
1375 retval = error;
1376 goto out;
1380 static int psmouse_reconnect(struct serio *serio)
1382 struct psmouse *psmouse = serio_get_drvdata(serio);
1383 struct psmouse *parent = NULL;
1384 struct serio_driver *drv = serio->drv;
1385 int rc = -1;
1387 if (!drv || !psmouse) {
1388 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1389 return -1;
1392 mutex_lock(&psmouse_mutex);
1394 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1395 parent = serio_get_drvdata(serio->parent);
1396 psmouse_deactivate(parent);
1399 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1401 if (psmouse->reconnect) {
1402 if (psmouse->reconnect(psmouse))
1403 goto out;
1404 } else if (psmouse_probe(psmouse) < 0 ||
1405 psmouse->type != psmouse_extensions(psmouse,
1406 psmouse_max_proto, false)) {
1407 goto out;
1410 /* ok, the device type (and capabilities) match the old one,
1411 * we can continue using it, complete intialization
1413 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1415 psmouse_initialize(psmouse);
1417 if (parent && parent->pt_activate)
1418 parent->pt_activate(parent);
1420 psmouse_activate(psmouse);
1421 rc = 0;
1423 out:
1424 /* If this is a pass-through port the parent waits to be activated */
1425 if (parent)
1426 psmouse_activate(parent);
1428 mutex_unlock(&psmouse_mutex);
1429 return rc;
1432 static struct serio_device_id psmouse_serio_ids[] = {
1434 .type = SERIO_8042,
1435 .proto = SERIO_ANY,
1436 .id = SERIO_ANY,
1437 .extra = SERIO_ANY,
1440 .type = SERIO_PS_PSTHRU,
1441 .proto = SERIO_ANY,
1442 .id = SERIO_ANY,
1443 .extra = SERIO_ANY,
1445 { 0 }
1448 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1450 static struct serio_driver psmouse_drv = {
1451 .driver = {
1452 .name = "psmouse",
1454 .description = DRIVER_DESC,
1455 .id_table = psmouse_serio_ids,
1456 .interrupt = psmouse_interrupt,
1457 .connect = psmouse_connect,
1458 .reconnect = psmouse_reconnect,
1459 .disconnect = psmouse_disconnect,
1460 .cleanup = psmouse_cleanup,
1463 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1464 char *buf)
1466 struct serio *serio = to_serio_port(dev);
1467 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1468 struct psmouse *psmouse;
1470 psmouse = serio_get_drvdata(serio);
1472 return attr->show(psmouse, attr->data, buf);
1475 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1476 const char *buf, size_t count)
1478 struct serio *serio = to_serio_port(dev);
1479 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1480 struct psmouse *psmouse, *parent = NULL;
1481 int retval;
1483 retval = mutex_lock_interruptible(&psmouse_mutex);
1484 if (retval)
1485 goto out;
1487 psmouse = serio_get_drvdata(serio);
1489 if (attr->protect) {
1490 if (psmouse->state == PSMOUSE_IGNORE) {
1491 retval = -ENODEV;
1492 goto out_unlock;
1495 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1496 parent = serio_get_drvdata(serio->parent);
1497 psmouse_deactivate(parent);
1500 psmouse_deactivate(psmouse);
1503 retval = attr->set(psmouse, attr->data, buf, count);
1505 if (attr->protect) {
1506 if (retval != -ENODEV)
1507 psmouse_activate(psmouse);
1509 if (parent)
1510 psmouse_activate(parent);
1513 out_unlock:
1514 mutex_unlock(&psmouse_mutex);
1515 out:
1516 return retval;
1519 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1521 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1523 return sprintf(buf, "%u\n", *field);
1526 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1528 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1529 unsigned long value;
1531 if (strict_strtoul(buf, 10, &value))
1532 return -EINVAL;
1534 if ((unsigned int)value != value)
1535 return -EINVAL;
1537 *field = value;
1539 return count;
1542 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1544 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1547 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1549 struct serio *serio = psmouse->ps2dev.serio;
1550 struct psmouse *parent = NULL;
1551 struct input_dev *old_dev, *new_dev;
1552 const struct psmouse_protocol *proto, *old_proto;
1553 int error;
1554 int retry = 0;
1556 proto = psmouse_protocol_by_name(buf, count);
1557 if (!proto)
1558 return -EINVAL;
1560 if (psmouse->type == proto->type)
1561 return count;
1563 new_dev = input_allocate_device();
1564 if (!new_dev)
1565 return -ENOMEM;
1567 while (serio->child) {
1568 if (++retry > 3) {
1569 printk(KERN_WARNING
1570 "psmouse: failed to destroy child port, "
1571 "protocol change aborted.\n");
1572 input_free_device(new_dev);
1573 return -EIO;
1576 mutex_unlock(&psmouse_mutex);
1577 serio_unregister_child_port(serio);
1578 mutex_lock(&psmouse_mutex);
1580 if (serio->drv != &psmouse_drv) {
1581 input_free_device(new_dev);
1582 return -ENODEV;
1585 if (psmouse->type == proto->type) {
1586 input_free_device(new_dev);
1587 return count; /* switched by other thread */
1591 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1592 parent = serio_get_drvdata(serio->parent);
1593 if (parent->pt_deactivate)
1594 parent->pt_deactivate(parent);
1597 old_dev = psmouse->dev;
1598 old_proto = psmouse_protocol_by_type(psmouse->type);
1600 if (psmouse->disconnect)
1601 psmouse->disconnect(psmouse);
1603 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1605 psmouse->dev = new_dev;
1606 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1608 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1609 psmouse_reset(psmouse);
1610 /* default to PSMOUSE_PS2 */
1611 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1614 psmouse_initialize(psmouse);
1615 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1617 error = input_register_device(psmouse->dev);
1618 if (error) {
1619 if (psmouse->disconnect)
1620 psmouse->disconnect(psmouse);
1622 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1623 input_free_device(new_dev);
1624 psmouse->dev = old_dev;
1625 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1626 psmouse_switch_protocol(psmouse, old_proto);
1627 psmouse_initialize(psmouse);
1628 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1630 return error;
1633 input_unregister_device(old_dev);
1635 if (parent && parent->pt_activate)
1636 parent->pt_activate(parent);
1638 return count;
1641 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1643 unsigned long value;
1645 if (strict_strtoul(buf, 10, &value))
1646 return -EINVAL;
1648 psmouse->set_rate(psmouse, value);
1649 return count;
1652 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1654 unsigned long value;
1656 if (strict_strtoul(buf, 10, &value))
1657 return -EINVAL;
1659 psmouse->set_resolution(psmouse, value);
1660 return count;
1664 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1666 const struct psmouse_protocol *proto;
1668 if (!val)
1669 return -EINVAL;
1671 proto = psmouse_protocol_by_name(val, strlen(val));
1673 if (!proto || !proto->maxproto)
1674 return -EINVAL;
1676 *((unsigned int *)kp->arg) = proto->type;
1678 return 0;
1681 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1683 int type = *((unsigned int *)kp->arg);
1685 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1688 static int __init psmouse_init(void)
1690 int err;
1692 lifebook_module_init();
1693 synaptics_module_init();
1695 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1696 if (!kpsmoused_wq) {
1697 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1698 return -ENOMEM;
1701 err = serio_register_driver(&psmouse_drv);
1702 if (err)
1703 destroy_workqueue(kpsmoused_wq);
1705 return err;
1708 static void __exit psmouse_exit(void)
1710 serio_unregister_driver(&psmouse_drv);
1711 destroy_workqueue(kpsmoused_wq);
1714 module_init(psmouse_init);
1715 module_exit(psmouse_exit);