4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
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/moduleparam.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/input.h>
20 #include <linux/serio.h>
21 #include <linux/init.h>
22 #include <linux/libps2.h>
23 #include <linux/mutex.h>
26 #include "synaptics.h"
27 #include "logips2pp.h"
30 #include "trackpoint.h"
32 #define DRIVER_DESC "PS/2 mouse driver"
34 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35 MODULE_DESCRIPTION(DRIVER_DESC);
36 MODULE_LICENSE("GPL");
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.");
47 static unsigned int psmouse_resolution = 200;
48 module_param_named(resolution, psmouse_resolution, uint, 0644);
49 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
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.");
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.");
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).");
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).");
67 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
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);
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,
92 static struct attribute_group psmouse_attribute_group = {
93 .attrs = psmouse_attributes,
96 __obsolete_setup("psmouse_noext");
97 __obsolete_setup("psmouse_resolution=");
98 __obsolete_setup("psmouse_smartscroll=");
99 __obsolete_setup("psmouse_resetafter=");
100 __obsolete_setup("psmouse_rate=");
103 * psmouse_mutex protects all operations changing state of mouse
104 * (connecting, disconnecting, changing rate or resolution via
105 * sysfs). We could use a per-device semaphore but since there
106 * rarely more than one PS/2 mouse connected and since semaphore
107 * is taken in "slow" paths it is not worth it.
109 static DEFINE_MUTEX(psmouse_mutex);
111 static struct workqueue_struct *kpsmoused_wq;
113 struct psmouse_protocol {
114 enum psmouse_type type;
118 int (*detect)(struct psmouse *, int);
119 int (*init)(struct psmouse *);
123 * psmouse_process_byte() analyzes the PS/2 data stream and reports
124 * relevant events to the input module once full packet has arrived.
127 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
129 struct input_dev *dev = psmouse->dev;
130 unsigned char *packet = psmouse->packet;
132 if (psmouse->pktcnt < psmouse->pktsize)
133 return PSMOUSE_GOOD_DATA;
136 * Full packet accumulated, process it
139 input_regs(dev, regs);
142 * Scroll wheel on IntelliMice, scroll buttons on NetMice
145 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
146 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
149 * Scroll wheel and buttons on IntelliMouse Explorer
152 if (psmouse->type == PSMOUSE_IMEX) {
153 switch (packet[3] & 0xC0) {
154 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
155 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
157 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
158 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
162 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
163 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
164 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
170 * Extra buttons on Genius NewNet 3D
173 if (psmouse->type == PSMOUSE_GENPS) {
174 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
175 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
179 * Extra button on ThinkingMouse
181 if (psmouse->type == PSMOUSE_THINKPS) {
182 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
183 /* Without this bit of weirdness moving up gives wildly high Y changes. */
184 packet[1] |= (packet[0] & 0x40) << 1;
191 input_report_key(dev, BTN_LEFT, packet[0] & 1);
192 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
193 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
195 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
196 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
200 return PSMOUSE_FULL_PACKET;
204 * __psmouse_set_state() sets new psmouse state and resets all flags.
207 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
209 psmouse->state = new_state;
210 psmouse->pktcnt = psmouse->out_of_sync = 0;
211 psmouse->ps2dev.flags = 0;
212 psmouse->last = jiffies;
217 * psmouse_set_state() sets new psmouse state and resets all flags and
218 * counters while holding serio lock so fighting with interrupt handler
222 static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
224 serio_pause_rx(psmouse->ps2dev.serio);
225 __psmouse_set_state(psmouse, new_state);
226 serio_continue_rx(psmouse->ps2dev.serio);
230 * psmouse_handle_byte() processes one byte of the input data stream
231 * by calling corresponding protocol handler.
234 static int psmouse_handle_byte(struct psmouse *psmouse, struct pt_regs *regs)
236 psmouse_ret_t rc = psmouse->protocol_handler(psmouse, regs);
239 case PSMOUSE_BAD_DATA:
240 if (psmouse->state == PSMOUSE_ACTIVATED) {
241 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
242 psmouse->name, psmouse->phys, psmouse->pktcnt);
243 if (++psmouse->out_of_sync == psmouse->resetafter) {
244 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
245 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
246 serio_reconnect(psmouse->ps2dev.serio);
253 case PSMOUSE_FULL_PACKET:
255 if (psmouse->out_of_sync) {
256 psmouse->out_of_sync = 0;
257 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
258 psmouse->name, psmouse->phys);
262 case PSMOUSE_GOOD_DATA:
269 * psmouse_interrupt() handles incoming characters, either passing them
270 * for normal processing or gathering them as command response.
273 static irqreturn_t psmouse_interrupt(struct serio *serio,
274 unsigned char data, unsigned int flags, struct pt_regs *regs)
276 struct psmouse *psmouse = serio_get_drvdata(serio);
278 if (psmouse->state == PSMOUSE_IGNORE)
281 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
282 if (psmouse->state == PSMOUSE_ACTIVATED)
283 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
284 flags & SERIO_TIMEOUT ? " timeout" : "",
285 flags & SERIO_PARITY ? " bad parity" : "");
286 ps2_cmd_aborted(&psmouse->ps2dev);
290 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
291 if (ps2_handle_ack(&psmouse->ps2dev, data))
294 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
295 if (ps2_handle_response(&psmouse->ps2dev, data))
298 if (psmouse->state <= PSMOUSE_RESYNCING)
301 if (psmouse->state == PSMOUSE_ACTIVATED &&
302 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
303 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
304 psmouse->name, psmouse->phys, psmouse->pktcnt);
305 psmouse->badbyte = psmouse->packet[0];
306 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
307 queue_work(kpsmoused_wq, &psmouse->resync_work);
311 psmouse->packet[psmouse->pktcnt++] = data;
313 * Check if this is a new device announcement (0xAA 0x00)
315 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
316 if (psmouse->pktcnt == 1) {
317 psmouse->last = jiffies;
321 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
322 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
323 serio_reconnect(serio);
327 * Not a new device, try processing first byte normally
330 if (psmouse_handle_byte(psmouse, regs))
333 psmouse->packet[psmouse->pktcnt++] = data;
337 * See if we need to force resync because mouse was idle for too long
339 if (psmouse->state == PSMOUSE_ACTIVATED &&
340 psmouse->pktcnt == 1 && psmouse->resync_time &&
341 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
342 psmouse->badbyte = psmouse->packet[0];
343 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
344 queue_work(kpsmoused_wq, &psmouse->resync_work);
348 psmouse->last = jiffies;
349 psmouse_handle_byte(psmouse, regs);
357 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
358 * using sliced syntax, understood by advanced devices, such as Logitech
359 * or Synaptics touchpads. The command is encoded as:
360 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
363 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
367 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
370 for (i = 6; i >= 0; i -= 2) {
371 unsigned char d = (command >> i) & 3;
372 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
381 * psmouse_reset() resets the mouse into power-on state.
383 int psmouse_reset(struct psmouse *psmouse)
385 unsigned char param[2];
387 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
390 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
398 * Genius NetMouse magic init.
400 static int genius_detect(struct psmouse *psmouse, int set_properties)
402 struct ps2dev *ps2dev = &psmouse->ps2dev;
403 unsigned char param[4];
406 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
407 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
408 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
409 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
410 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
412 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
415 if (set_properties) {
416 set_bit(BTN_EXTRA, psmouse->dev->keybit);
417 set_bit(BTN_SIDE, psmouse->dev->keybit);
418 set_bit(REL_WHEEL, psmouse->dev->relbit);
420 psmouse->vendor = "Genius";
421 psmouse->name = "Mouse";
422 psmouse->pktsize = 4;
429 * IntelliMouse magic init.
431 static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
433 struct ps2dev *ps2dev = &psmouse->ps2dev;
434 unsigned char param[2];
437 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
439 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
441 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
442 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
447 if (set_properties) {
448 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
449 set_bit(REL_WHEEL, psmouse->dev->relbit);
451 if (!psmouse->vendor) psmouse->vendor = "Generic";
452 if (!psmouse->name) psmouse->name = "Wheel Mouse";
453 psmouse->pktsize = 4;
460 * Try IntelliMouse/Explorer magic init.
462 static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
464 struct ps2dev *ps2dev = &psmouse->ps2dev;
465 unsigned char param[2];
467 intellimouse_detect(psmouse, 0);
470 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
472 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
474 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
475 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
480 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
482 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
484 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
486 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
488 if (set_properties) {
489 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
490 set_bit(REL_WHEEL, psmouse->dev->relbit);
491 set_bit(REL_HWHEEL, psmouse->dev->relbit);
492 set_bit(BTN_SIDE, psmouse->dev->keybit);
493 set_bit(BTN_EXTRA, psmouse->dev->keybit);
495 if (!psmouse->vendor) psmouse->vendor = "Generic";
496 if (!psmouse->name) psmouse->name = "Explorer Mouse";
497 psmouse->pktsize = 4;
504 * Kensington ThinkingMouse / ExpertMouse magic init.
506 static int thinking_detect(struct psmouse *psmouse, int set_properties)
508 struct ps2dev *ps2dev = &psmouse->ps2dev;
509 unsigned char param[2];
510 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
514 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
516 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
517 for (i = 0; i < ARRAY_SIZE(seq); i++) {
519 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
521 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
526 if (set_properties) {
527 set_bit(BTN_EXTRA, psmouse->dev->keybit);
529 psmouse->vendor = "Kensington";
530 psmouse->name = "ThinkingMouse";
537 * Bare PS/2 protocol "detection". Always succeeds.
539 static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
541 if (set_properties) {
542 if (!psmouse->vendor) psmouse->vendor = "Generic";
543 if (!psmouse->name) psmouse->name = "Mouse";
551 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
552 * the mouse may have.
555 static int psmouse_extensions(struct psmouse *psmouse,
556 unsigned int max_proto, int set_properties)
558 int synaptics_hardware = 0;
561 * We always check for lifebook because it does not disturb mouse
562 * (it only checks DMI information).
564 if (lifebook_detect(psmouse, set_properties) == 0) {
565 if (max_proto > PSMOUSE_IMEX) {
566 if (!set_properties || lifebook_init(psmouse) == 0)
567 return PSMOUSE_LIFEBOOK;
572 * Try Kensington ThinkingMouse (we try first, because synaptics probe
573 * upsets the thinkingmouse).
576 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
577 return PSMOUSE_THINKPS;
580 * Try Synaptics TouchPad
582 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
583 synaptics_hardware = 1;
585 if (max_proto > PSMOUSE_IMEX) {
586 if (!set_properties || synaptics_init(psmouse) == 0)
587 return PSMOUSE_SYNAPTICS;
589 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
590 * Unfortunately Logitech/Genius probes confuse some firmware versions so
591 * we'll have to skip them.
593 max_proto = PSMOUSE_IMEX;
596 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
598 synaptics_reset(psmouse);
604 if (max_proto > PSMOUSE_IMEX) {
605 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
606 if (alps_detect(psmouse, set_properties) == 0) {
607 if (!set_properties || alps_init(psmouse) == 0)
610 * Init failed, try basic relative protocols
612 max_proto = PSMOUSE_IMEX;
616 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
617 return PSMOUSE_GENPS;
619 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
620 return PSMOUSE_PS2PP;
622 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
623 return PSMOUSE_TRACKPOINT;
626 * Reset to defaults in case the device got confused by extended
627 * protocol probes. Note that we do full reset becuase some mice
628 * put themselves to sleep when see PSMOUSE_RESET_DIS.
630 psmouse_reset(psmouse);
632 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
635 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
639 * Okay, all failed, we have a standard mouse here. The number of the buttons
640 * is still a question, though. We assume 3.
642 ps2bare_detect(psmouse, set_properties);
644 if (synaptics_hardware) {
646 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
647 * We need to reset the touchpad because if there is a track point on the
648 * pass through port it could get disabled while probing for protocol
651 psmouse_reset(psmouse);
657 static const struct psmouse_protocol psmouse_protocols[] = {
663 .detect = ps2bare_detect,
666 .type = PSMOUSE_PS2PP,
669 .detect = ps2pp_init,
672 .type = PSMOUSE_THINKPS,
675 .detect = thinking_detect,
678 .type = PSMOUSE_GENPS,
681 .detect = genius_detect,
684 .type = PSMOUSE_IMPS,
688 .detect = intellimouse_detect,
691 .type = PSMOUSE_IMEX,
695 .detect = im_explorer_detect,
698 .type = PSMOUSE_SYNAPTICS,
700 .alias = "synaptics",
701 .detect = synaptics_detect,
702 .init = synaptics_init,
705 .type = PSMOUSE_ALPS,
708 .detect = alps_detect,
712 .type = PSMOUSE_LIFEBOOK,
715 .init = lifebook_init,
718 .type = PSMOUSE_TRACKPOINT,
720 .alias = "trackpoint",
721 .detect = trackpoint_detect,
724 .type = PSMOUSE_AUTO,
731 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
735 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
736 if (psmouse_protocols[i].type == type)
737 return &psmouse_protocols[i];
740 return &psmouse_protocols[0];
743 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
745 const struct psmouse_protocol *p;
748 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
749 p = &psmouse_protocols[i];
751 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
752 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
753 return &psmouse_protocols[i];
761 * psmouse_probe() probes for a PS/2 mouse.
764 static int psmouse_probe(struct psmouse *psmouse)
766 struct ps2dev *ps2dev = &psmouse->ps2dev;
767 unsigned char param[2];
770 * First, we check if it's a mouse. It should send 0x00 or 0x03
771 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
772 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
773 * ID queries, probably due to a firmware bug.
777 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
780 if (param[0] != 0x00 && param[0] != 0x03 &&
781 param[0] != 0x04 && param[0] != 0xff)
785 * Then we reset and disable the mouse so that it doesn't generate events.
788 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
789 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
795 * Here we set the mouse resolution.
798 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
800 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
803 if (resolution == 0 || resolution > 200)
806 p = params[resolution / 50];
807 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
808 psmouse->resolution = 25 << p;
812 * Here we set the mouse report rate.
815 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
817 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
821 while (rates[i] > rate) i++;
823 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
828 * psmouse_initialize() initializes the mouse to a sane state.
831 static void psmouse_initialize(struct psmouse *psmouse)
834 * We set the mouse into streaming mode.
837 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
840 * We set the mouse report rate, resolution and scaling.
843 if (psmouse_max_proto != PSMOUSE_PS2) {
844 psmouse->set_rate(psmouse, psmouse->rate);
845 psmouse->set_resolution(psmouse, psmouse->resolution);
846 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
851 * psmouse_activate() enables the mouse so that we get motion reports from it.
854 static void psmouse_activate(struct psmouse *psmouse)
856 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
857 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
858 psmouse->ps2dev.serio->phys);
860 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
865 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
866 * reports from it unless we explicitely request it.
869 static void psmouse_deactivate(struct psmouse *psmouse)
871 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
872 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
873 psmouse->ps2dev.serio->phys);
875 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
879 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
882 static int psmouse_poll(struct psmouse *psmouse)
884 return ps2_command(&psmouse->ps2dev, psmouse->packet,
885 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
890 * psmouse_resync() attempts to re-validate current protocol.
893 static void psmouse_resync(void *p)
895 struct psmouse *psmouse = p, *parent = NULL;
896 struct serio *serio = psmouse->ps2dev.serio;
897 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
898 int failed = 0, enabled = 0;
901 mutex_lock(&psmouse_mutex);
903 if (psmouse->state != PSMOUSE_RESYNCING)
906 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
907 parent = serio_get_drvdata(serio->parent);
908 psmouse_deactivate(parent);
912 * Some mice don't ACK commands sent while they are in the middle of
913 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
914 * instead of ps2_command() which would wait for 200ms for an ACK
915 * that may never come.
916 * As an additional quirk ALPS touchpads may not only forget to ACK
917 * disable command but will stop reporting taps, so if we see that
918 * mouse at least once ACKs disable we will do full reconnect if ACK
921 psmouse->num_resyncs++;
923 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
924 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
927 psmouse->acks_disable_command = 1;
930 * Poll the mouse. If it was reset the packet will be shorter than
931 * psmouse->pktsize and ps2_command will fail. We do not expect and
932 * do not handle scenario when mouse "upgrades" its protocol while
933 * disconnected since it would require additional delay. If we ever
934 * see a mouse that does it we'll adjust the code.
937 if (psmouse->poll(psmouse))
940 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
941 for (i = 0; i < psmouse->pktsize; i++) {
943 rc = psmouse->protocol_handler(psmouse, NULL);
944 if (rc != PSMOUSE_GOOD_DATA)
947 if (rc != PSMOUSE_FULL_PACKET)
949 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
953 * Now try to enable mouse. We try to do that even if poll failed and also
954 * repeat our attempts 5 times, otherwise we may be left out with disabled
957 for (i = 0; i < 5; i++) {
958 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
966 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
967 psmouse->ps2dev.serio->phys);
972 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
973 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
974 serio_reconnect(serio);
976 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
979 psmouse_activate(parent);
981 mutex_unlock(&psmouse_mutex);
985 * psmouse_cleanup() resets the mouse into power-on state.
988 static void psmouse_cleanup(struct serio *serio)
990 struct psmouse *psmouse = serio_get_drvdata(serio);
992 psmouse_reset(psmouse);
996 * psmouse_disconnect() closes and frees.
999 static void psmouse_disconnect(struct serio *serio)
1001 struct psmouse *psmouse, *parent = NULL;
1003 psmouse = serio_get_drvdata(serio);
1005 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1007 mutex_lock(&psmouse_mutex);
1009 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1011 /* make sure we don't have a resync in progress */
1012 mutex_unlock(&psmouse_mutex);
1013 flush_workqueue(kpsmoused_wq);
1014 mutex_lock(&psmouse_mutex);
1016 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1017 parent = serio_get_drvdata(serio->parent);
1018 psmouse_deactivate(parent);
1021 if (psmouse->disconnect)
1022 psmouse->disconnect(psmouse);
1024 if (parent && parent->pt_deactivate)
1025 parent->pt_deactivate(parent);
1027 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1030 serio_set_drvdata(serio, NULL);
1031 input_unregister_device(psmouse->dev);
1035 psmouse_activate(parent);
1037 mutex_unlock(&psmouse_mutex);
1040 static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
1042 struct input_dev *input_dev = psmouse->dev;
1044 input_dev->private = psmouse;
1045 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
1047 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1048 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1049 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
1051 psmouse->set_rate = psmouse_set_rate;
1052 psmouse->set_resolution = psmouse_set_resolution;
1053 psmouse->poll = psmouse_poll;
1054 psmouse->protocol_handler = psmouse_process_byte;
1055 psmouse->pktsize = 3;
1057 if (proto && (proto->detect || proto->init)) {
1058 if (proto->detect && proto->detect(psmouse, 1) < 0)
1061 if (proto->init && proto->init(psmouse) < 0)
1064 psmouse->type = proto->type;
1067 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1070 * If mouse's packet size is 3 there is no point in polling the
1071 * device in hopes to detect protocol reset - we won't get less
1072 * than 3 bytes response anyhow.
1074 if (psmouse->pktsize == 3)
1075 psmouse->resync_time = 0;
1078 * Some smart KVMs fake response to POLL command returning just
1079 * 3 bytes and messing up our resync logic, so if initial poll
1080 * fails we won't try polling the device anymore. Hopefully
1081 * such KVM will maintain initially selected protocol.
1083 if (psmouse->resync_time && psmouse->poll(psmouse))
1084 psmouse->resync_time = 0;
1086 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1087 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1089 input_dev->name = psmouse->devname;
1090 input_dev->phys = psmouse->phys;
1091 input_dev->id.bustype = BUS_I8042;
1092 input_dev->id.vendor = 0x0002;
1093 input_dev->id.product = psmouse->type;
1094 input_dev->id.version = psmouse->model;
1100 * psmouse_connect() is a callback from the serio module when
1101 * an unhandled serio port is found.
1103 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1105 struct psmouse *psmouse, *parent = NULL;
1106 struct input_dev *input_dev;
1107 int retval = -ENOMEM;
1109 mutex_lock(&psmouse_mutex);
1112 * If this is a pass-through port deactivate parent so the device
1113 * connected to this port can be successfully identified
1115 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1116 parent = serio_get_drvdata(serio->parent);
1117 psmouse_deactivate(parent);
1120 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1121 input_dev = input_allocate_device();
1122 if (!psmouse || !input_dev)
1125 ps2_init(&psmouse->ps2dev, serio);
1126 INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse);
1127 psmouse->dev = input_dev;
1128 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1130 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1132 serio_set_drvdata(serio, psmouse);
1134 retval = serio_open(serio, drv);
1138 if (psmouse_probe(psmouse) < 0) {
1144 psmouse->rate = psmouse_rate;
1145 psmouse->resolution = psmouse_resolution;
1146 psmouse->resetafter = psmouse_resetafter;
1147 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1148 psmouse->smartscroll = psmouse_smartscroll;
1150 psmouse_switch_protocol(psmouse, NULL);
1152 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1153 psmouse_initialize(psmouse);
1155 input_register_device(psmouse->dev);
1157 if (parent && parent->pt_activate)
1158 parent->pt_activate(parent);
1160 sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1162 psmouse_activate(psmouse);
1168 serio_set_drvdata(serio, NULL);
1169 input_free_device(input_dev);
1173 /* If this is a pass-through port the parent needs to be re-activated */
1175 psmouse_activate(parent);
1177 mutex_unlock(&psmouse_mutex);
1182 static int psmouse_reconnect(struct serio *serio)
1184 struct psmouse *psmouse = serio_get_drvdata(serio);
1185 struct psmouse *parent = NULL;
1186 struct serio_driver *drv = serio->drv;
1189 if (!drv || !psmouse) {
1190 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1194 mutex_lock(&psmouse_mutex);
1196 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1197 parent = serio_get_drvdata(serio->parent);
1198 psmouse_deactivate(parent);
1201 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1203 if (psmouse->reconnect) {
1204 if (psmouse->reconnect(psmouse))
1206 } else if (psmouse_probe(psmouse) < 0 ||
1207 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1210 /* ok, the device type (and capabilities) match the old one,
1211 * we can continue using it, complete intialization
1213 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1215 psmouse_initialize(psmouse);
1217 if (parent && parent->pt_activate)
1218 parent->pt_activate(parent);
1220 psmouse_activate(psmouse);
1224 /* If this is a pass-through port the parent waits to be activated */
1226 psmouse_activate(parent);
1228 mutex_unlock(&psmouse_mutex);
1232 static struct serio_device_id psmouse_serio_ids[] = {
1240 .type = SERIO_PS_PSTHRU,
1248 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1250 static struct serio_driver psmouse_drv = {
1254 .description = DRIVER_DESC,
1255 .id_table = psmouse_serio_ids,
1256 .interrupt = psmouse_interrupt,
1257 .connect = psmouse_connect,
1258 .reconnect = psmouse_reconnect,
1259 .disconnect = psmouse_disconnect,
1260 .cleanup = psmouse_cleanup,
1263 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1266 struct serio *serio = to_serio_port(dev);
1267 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1268 struct psmouse *psmouse;
1271 retval = serio_pin_driver(serio);
1275 if (serio->drv != &psmouse_drv) {
1280 psmouse = serio_get_drvdata(serio);
1282 retval = attr->show(psmouse, attr->data, buf);
1285 serio_unpin_driver(serio);
1289 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1290 const char *buf, size_t count)
1292 struct serio *serio = to_serio_port(dev);
1293 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1294 struct psmouse *psmouse, *parent = NULL;
1297 retval = serio_pin_driver(serio);
1301 if (serio->drv != &psmouse_drv) {
1306 retval = mutex_lock_interruptible(&psmouse_mutex);
1310 psmouse = serio_get_drvdata(serio);
1312 if (psmouse->state == PSMOUSE_IGNORE) {
1317 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1318 parent = serio_get_drvdata(serio->parent);
1319 psmouse_deactivate(parent);
1322 psmouse_deactivate(psmouse);
1324 retval = attr->set(psmouse, attr->data, buf, count);
1326 if (retval != -ENODEV)
1327 psmouse_activate(psmouse);
1330 psmouse_activate(parent);
1333 mutex_unlock(&psmouse_mutex);
1335 serio_unpin_driver(serio);
1339 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1341 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1343 return sprintf(buf, "%lu\n", *field);
1346 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1348 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1349 unsigned long value;
1352 value = simple_strtoul(buf, &rest, 10);
1361 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1363 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1366 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1368 struct serio *serio = psmouse->ps2dev.serio;
1369 struct psmouse *parent = NULL;
1370 struct input_dev *new_dev;
1371 const struct psmouse_protocol *proto;
1374 if (!(proto = psmouse_protocol_by_name(buf, count)))
1377 if (psmouse->type == proto->type)
1380 if (!(new_dev = input_allocate_device()))
1383 while (serio->child) {
1385 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
1386 input_free_device(new_dev);
1390 mutex_unlock(&psmouse_mutex);
1391 serio_unpin_driver(serio);
1392 serio_unregister_child_port(serio);
1393 serio_pin_driver_uninterruptible(serio);
1394 mutex_lock(&psmouse_mutex);
1396 if (serio->drv != &psmouse_drv) {
1397 input_free_device(new_dev);
1401 if (psmouse->type == proto->type) {
1402 input_free_device(new_dev);
1403 return count; /* switched by other thread */
1407 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1408 parent = serio_get_drvdata(serio->parent);
1409 if (parent->pt_deactivate)
1410 parent->pt_deactivate(parent);
1413 if (psmouse->disconnect)
1414 psmouse->disconnect(psmouse);
1416 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1417 input_unregister_device(psmouse->dev);
1419 psmouse->dev = new_dev;
1420 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1422 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1423 psmouse_reset(psmouse);
1424 /* default to PSMOUSE_PS2 */
1425 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1428 psmouse_initialize(psmouse);
1429 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1431 input_register_device(psmouse->dev);
1433 if (parent && parent->pt_activate)
1434 parent->pt_activate(parent);
1439 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1441 unsigned long value;
1444 value = simple_strtoul(buf, &rest, 10);
1448 psmouse->set_rate(psmouse, value);
1452 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1454 unsigned long value;
1457 value = simple_strtoul(buf, &rest, 10);
1461 psmouse->set_resolution(psmouse, value);
1466 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1468 const struct psmouse_protocol *proto;
1473 proto = psmouse_protocol_by_name(val, strlen(val));
1475 if (!proto || !proto->maxproto)
1478 *((unsigned int *)kp->arg) = proto->type;
1483 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1485 int type = *((unsigned int *)kp->arg);
1487 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
1490 static int __init psmouse_init(void)
1492 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1493 if (!kpsmoused_wq) {
1494 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1498 serio_register_driver(&psmouse_drv);
1503 static void __exit psmouse_exit(void)
1505 serio_unregister_driver(&psmouse_drv);
1506 destroy_workqueue(kpsmoused_wq);
1509 module_init(psmouse_init);
1510 module_exit(psmouse_exit);