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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #define psmouse_fmt(fmt) fmt
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
21 #include <linux/input.h>
22 #include <linux/serio.h>
23 #include <linux/init.h>
24 #include <linux/libps2.h>
25 #include <linux/mutex.h>
28 #include "synaptics.h"
29 #include "logips2pp.h"
33 #include "trackpoint.h"
34 #include "touchkit_ps2.h"
37 #include "cypress_ps2.h"
38 #include "focaltech.h"
42 #define DRIVER_DESC "PS/2 mouse driver"
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
45 MODULE_DESCRIPTION(DRIVER_DESC
);
46 MODULE_LICENSE("GPL");
48 static unsigned int psmouse_max_proto
= PSMOUSE_AUTO
;
49 static int psmouse_set_maxproto(const char *val
, const struct kernel_param
*);
50 static int psmouse_get_maxproto(char *buffer
, const struct kernel_param
*kp
);
51 static const struct kernel_param_ops param_ops_proto_abbrev
= {
52 .set
= psmouse_set_maxproto
,
53 .get
= psmouse_get_maxproto
,
55 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
56 module_param_named(proto
, psmouse_max_proto
, proto_abbrev
, 0644);
57 MODULE_PARM_DESC(proto
, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
59 static unsigned int psmouse_resolution
= 200;
60 module_param_named(resolution
, psmouse_resolution
, uint
, 0644);
61 MODULE_PARM_DESC(resolution
, "Resolution, in dpi.");
63 static unsigned int psmouse_rate
= 100;
64 module_param_named(rate
, psmouse_rate
, uint
, 0644);
65 MODULE_PARM_DESC(rate
, "Report rate, in reports per second.");
67 static bool psmouse_smartscroll
= true;
68 module_param_named(smartscroll
, psmouse_smartscroll
, bool, 0644);
69 MODULE_PARM_DESC(smartscroll
, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
71 static unsigned int psmouse_resetafter
= 5;
72 module_param_named(resetafter
, psmouse_resetafter
, uint
, 0644);
73 MODULE_PARM_DESC(resetafter
, "Reset device after so many bad packets (0 = never).");
75 static unsigned int psmouse_resync_time
;
76 module_param_named(resync_time
, psmouse_resync_time
, uint
, 0644);
77 MODULE_PARM_DESC(resync_time
, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
79 PSMOUSE_DEFINE_ATTR(protocol
, S_IWUSR
| S_IRUGO
,
81 psmouse_attr_show_protocol
, psmouse_attr_set_protocol
);
82 PSMOUSE_DEFINE_ATTR(rate
, S_IWUSR
| S_IRUGO
,
83 (void *) offsetof(struct psmouse
, rate
),
84 psmouse_show_int_attr
, psmouse_attr_set_rate
);
85 PSMOUSE_DEFINE_ATTR(resolution
, S_IWUSR
| S_IRUGO
,
86 (void *) offsetof(struct psmouse
, resolution
),
87 psmouse_show_int_attr
, psmouse_attr_set_resolution
);
88 PSMOUSE_DEFINE_ATTR(resetafter
, S_IWUSR
| S_IRUGO
,
89 (void *) offsetof(struct psmouse
, resetafter
),
90 psmouse_show_int_attr
, psmouse_set_int_attr
);
91 PSMOUSE_DEFINE_ATTR(resync_time
, S_IWUSR
| S_IRUGO
,
92 (void *) offsetof(struct psmouse
, resync_time
),
93 psmouse_show_int_attr
, psmouse_set_int_attr
);
95 static struct attribute
*psmouse_attributes
[] = {
96 &psmouse_attr_protocol
.dattr
.attr
,
97 &psmouse_attr_rate
.dattr
.attr
,
98 &psmouse_attr_resolution
.dattr
.attr
,
99 &psmouse_attr_resetafter
.dattr
.attr
,
100 &psmouse_attr_resync_time
.dattr
.attr
,
104 static struct attribute_group psmouse_attribute_group
= {
105 .attrs
= psmouse_attributes
,
109 * psmouse_mutex protects all operations changing state of mouse
110 * (connecting, disconnecting, changing rate or resolution via
111 * sysfs). We could use a per-device semaphore but since there
112 * rarely more than one PS/2 mouse connected and since semaphore
113 * is taken in "slow" paths it is not worth it.
115 static DEFINE_MUTEX(psmouse_mutex
);
117 static struct workqueue_struct
*kpsmoused_wq
;
119 struct psmouse_protocol
{
120 enum psmouse_type type
;
122 bool ignore_parity
; /* Protocol should ignore parity errors from KBC */
123 bool try_passthru
; /* Try protocol also on passthrough ports */
126 int (*detect
)(struct psmouse
*, bool);
127 int (*init
)(struct psmouse
*);
131 * psmouse_process_byte() analyzes the PS/2 data stream and reports
132 * relevant events to the input module once full packet has arrived.
134 psmouse_ret_t
psmouse_process_byte(struct psmouse
*psmouse
)
136 struct input_dev
*dev
= psmouse
->dev
;
137 unsigned char *packet
= psmouse
->packet
;
139 if (psmouse
->pktcnt
< psmouse
->pktsize
)
140 return PSMOUSE_GOOD_DATA
;
142 /* Full packet accumulated, process it */
144 switch (psmouse
->type
) {
146 /* IntelliMouse has scroll wheel */
147 input_report_rel(dev
, REL_WHEEL
, -(signed char) packet
[3]);
151 /* Scroll wheel and buttons on IntelliMouse Explorer */
152 switch (packet
[3] & 0xC0) {
153 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
154 input_report_rel(dev
, REL_WHEEL
, (int) (packet
[3] & 32) - (int) (packet
[3] & 31));
156 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
157 input_report_rel(dev
, REL_HWHEEL
, (int) (packet
[3] & 32) - (int) (packet
[3] & 31));
161 input_report_rel(dev
, REL_WHEEL
, (int) (packet
[3] & 8) - (int) (packet
[3] & 7));
162 input_report_key(dev
, BTN_SIDE
, (packet
[3] >> 4) & 1);
163 input_report_key(dev
, BTN_EXTRA
, (packet
[3] >> 5) & 1);
169 /* Report scroll buttons on NetMice */
170 input_report_rel(dev
, REL_WHEEL
, -(signed char) packet
[3]);
172 /* Extra buttons on Genius NewNet 3D */
173 input_report_key(dev
, BTN_SIDE
, (packet
[0] >> 6) & 1);
174 input_report_key(dev
, BTN_EXTRA
, (packet
[0] >> 7) & 1);
177 case PSMOUSE_THINKPS
:
178 /* Extra button on ThinkingMouse */
179 input_report_key(dev
, BTN_EXTRA
, (packet
[0] >> 3) & 1);
182 * Without this bit of weirdness moving up gives wildly
185 packet
[1] |= (packet
[0] & 0x40) << 1;
188 case PSMOUSE_CORTRON
:
190 * Cortron PS2 Trackball reports SIDE button in the
191 * 4th bit of the first byte.
193 input_report_key(dev
, BTN_SIDE
, (packet
[0] >> 3) & 1);
201 /* Generic PS/2 Mouse */
202 input_report_key(dev
, BTN_LEFT
, packet
[0] & 1);
203 input_report_key(dev
, BTN_MIDDLE
, (packet
[0] >> 2) & 1);
204 input_report_key(dev
, BTN_RIGHT
, (packet
[0] >> 1) & 1);
206 input_report_rel(dev
, REL_X
, packet
[1] ? (int) packet
[1] - (int) ((packet
[0] << 4) & 0x100) : 0);
207 input_report_rel(dev
, REL_Y
, packet
[2] ? (int) ((packet
[0] << 3) & 0x100) - (int) packet
[2] : 0);
211 return PSMOUSE_FULL_PACKET
;
214 void psmouse_queue_work(struct psmouse
*psmouse
, struct delayed_work
*work
,
217 queue_delayed_work(kpsmoused_wq
, work
, delay
);
221 * __psmouse_set_state() sets new psmouse state and resets all flags.
223 static inline void __psmouse_set_state(struct psmouse
*psmouse
, enum psmouse_state new_state
)
225 psmouse
->state
= new_state
;
226 psmouse
->pktcnt
= psmouse
->out_of_sync_cnt
= 0;
227 psmouse
->ps2dev
.flags
= 0;
228 psmouse
->last
= jiffies
;
232 * psmouse_set_state() sets new psmouse state and resets all flags and
233 * counters while holding serio lock so fighting with interrupt handler
236 void psmouse_set_state(struct psmouse
*psmouse
, enum psmouse_state new_state
)
238 serio_pause_rx(psmouse
->ps2dev
.serio
);
239 __psmouse_set_state(psmouse
, new_state
);
240 serio_continue_rx(psmouse
->ps2dev
.serio
);
244 * psmouse_handle_byte() processes one byte of the input data stream
245 * by calling corresponding protocol handler.
247 static int psmouse_handle_byte(struct psmouse
*psmouse
)
249 psmouse_ret_t rc
= psmouse
->protocol_handler(psmouse
);
252 case PSMOUSE_BAD_DATA
:
253 if (psmouse
->state
== PSMOUSE_ACTIVATED
) {
254 psmouse_warn(psmouse
,
255 "%s at %s lost sync at byte %d\n",
256 psmouse
->name
, psmouse
->phys
,
258 if (++psmouse
->out_of_sync_cnt
== psmouse
->resetafter
) {
259 __psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
260 psmouse_notice(psmouse
,
261 "issuing reconnect request\n");
262 serio_reconnect(psmouse
->ps2dev
.serio
);
269 case PSMOUSE_FULL_PACKET
:
271 if (psmouse
->out_of_sync_cnt
) {
272 psmouse
->out_of_sync_cnt
= 0;
273 psmouse_notice(psmouse
,
274 "%s at %s - driver resynced.\n",
275 psmouse
->name
, psmouse
->phys
);
279 case PSMOUSE_GOOD_DATA
:
286 * psmouse_interrupt() handles incoming characters, either passing them
287 * for normal processing or gathering them as command response.
289 static irqreturn_t
psmouse_interrupt(struct serio
*serio
,
290 unsigned char data
, unsigned int flags
)
292 struct psmouse
*psmouse
= serio_get_drvdata(serio
);
294 if (psmouse
->state
== PSMOUSE_IGNORE
)
297 if (unlikely((flags
& SERIO_TIMEOUT
) ||
298 ((flags
& SERIO_PARITY
) && !psmouse
->ignore_parity
))) {
300 if (psmouse
->state
== PSMOUSE_ACTIVATED
)
301 psmouse_warn(psmouse
,
302 "bad data from KBC -%s%s\n",
303 flags
& SERIO_TIMEOUT
? " timeout" : "",
304 flags
& SERIO_PARITY
? " bad parity" : "");
305 ps2_cmd_aborted(&psmouse
->ps2dev
);
309 if (unlikely(psmouse
->ps2dev
.flags
& PS2_FLAG_ACK
))
310 if (ps2_handle_ack(&psmouse
->ps2dev
, data
))
313 if (unlikely(psmouse
->ps2dev
.flags
& PS2_FLAG_CMD
))
314 if (ps2_handle_response(&psmouse
->ps2dev
, data
))
317 if (psmouse
->state
<= PSMOUSE_RESYNCING
)
320 if (psmouse
->state
== PSMOUSE_ACTIVATED
&&
321 psmouse
->pktcnt
&& time_after(jiffies
, psmouse
->last
+ HZ
/2)) {
322 psmouse_info(psmouse
, "%s at %s lost synchronization, throwing %d bytes away.\n",
323 psmouse
->name
, psmouse
->phys
, psmouse
->pktcnt
);
324 psmouse
->badbyte
= psmouse
->packet
[0];
325 __psmouse_set_state(psmouse
, PSMOUSE_RESYNCING
);
326 psmouse_queue_work(psmouse
, &psmouse
->resync_work
, 0);
330 psmouse
->packet
[psmouse
->pktcnt
++] = data
;
332 /* Check if this is a new device announcement (0xAA 0x00) */
333 if (unlikely(psmouse
->packet
[0] == PSMOUSE_RET_BAT
&& psmouse
->pktcnt
<= 2)) {
334 if (psmouse
->pktcnt
== 1) {
335 psmouse
->last
= jiffies
;
339 if (psmouse
->packet
[1] == PSMOUSE_RET_ID
||
340 (psmouse
->type
== PSMOUSE_HGPK
&&
341 psmouse
->packet
[1] == PSMOUSE_RET_BAT
)) {
342 __psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
343 serio_reconnect(serio
);
347 /* Not a new device, try processing first byte normally */
349 if (psmouse_handle_byte(psmouse
))
352 psmouse
->packet
[psmouse
->pktcnt
++] = data
;
356 * See if we need to force resync because mouse was idle for
359 if (psmouse
->state
== PSMOUSE_ACTIVATED
&&
360 psmouse
->pktcnt
== 1 && psmouse
->resync_time
&&
361 time_after(jiffies
, psmouse
->last
+ psmouse
->resync_time
* HZ
)) {
362 psmouse
->badbyte
= psmouse
->packet
[0];
363 __psmouse_set_state(psmouse
, PSMOUSE_RESYNCING
);
364 psmouse_queue_work(psmouse
, &psmouse
->resync_work
, 0);
368 psmouse
->last
= jiffies
;
369 psmouse_handle_byte(psmouse
);
376 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
377 * using sliced syntax, understood by advanced devices, such as Logitech
378 * or Synaptics touchpads. The command is encoded as:
379 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
382 int psmouse_sliced_command(struct psmouse
*psmouse
, unsigned char command
)
386 if (ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
))
389 for (i
= 6; i
>= 0; i
-= 2) {
390 unsigned char d
= (command
>> i
) & 3;
391 if (ps2_command(&psmouse
->ps2dev
, &d
, PSMOUSE_CMD_SETRES
))
399 * psmouse_reset() resets the mouse into power-on state.
401 int psmouse_reset(struct psmouse
*psmouse
)
403 unsigned char param
[2];
405 if (ps2_command(&psmouse
->ps2dev
, param
, PSMOUSE_CMD_RESET_BAT
))
408 if (param
[0] != PSMOUSE_RET_BAT
&& param
[1] != PSMOUSE_RET_ID
)
415 * Here we set the mouse resolution.
417 void psmouse_set_resolution(struct psmouse
*psmouse
, unsigned int resolution
)
419 static const unsigned char params
[] = { 0, 1, 2, 2, 3 };
422 if (resolution
== 0 || resolution
> 200)
425 p
= params
[resolution
/ 50];
426 ps2_command(&psmouse
->ps2dev
, &p
, PSMOUSE_CMD_SETRES
);
427 psmouse
->resolution
= 25 << p
;
431 * Here we set the mouse report rate.
433 static void psmouse_set_rate(struct psmouse
*psmouse
, unsigned int rate
)
435 static const unsigned char rates
[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
439 while (rates
[i
] > rate
) i
++;
441 ps2_command(&psmouse
->ps2dev
, &r
, PSMOUSE_CMD_SETRATE
);
446 * Here we set the mouse scaling.
448 static void psmouse_set_scale(struct psmouse
*psmouse
, enum psmouse_scale scale
)
450 ps2_command(&psmouse
->ps2dev
, NULL
,
451 scale
== PSMOUSE_SCALE21
? PSMOUSE_CMD_SETSCALE21
:
452 PSMOUSE_CMD_SETSCALE11
);
456 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
458 static int psmouse_poll(struct psmouse
*psmouse
)
460 return ps2_command(&psmouse
->ps2dev
, psmouse
->packet
,
461 PSMOUSE_CMD_POLL
| (psmouse
->pktsize
<< 8));
464 static bool psmouse_check_pnp_id(const char *id
, const char * const ids
[])
468 for (i
= 0; ids
[i
]; i
++)
469 if (!strcasecmp(id
, ids
[i
]))
476 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
478 bool psmouse_matches_pnp_id(struct psmouse
*psmouse
, const char * const ids
[])
480 struct serio
*serio
= psmouse
->ps2dev
.serio
;
481 char *p
, *fw_id_copy
, *save_ptr
;
484 if (strncmp(serio
->firmware_id
, "PNP: ", 5))
487 fw_id_copy
= kstrndup(&serio
->firmware_id
[5],
488 sizeof(serio
->firmware_id
) - 5,
493 save_ptr
= fw_id_copy
;
494 while ((p
= strsep(&fw_id_copy
, " ")) != NULL
) {
495 if (psmouse_check_pnp_id(p
, ids
)) {
506 * Genius NetMouse magic init.
508 static int genius_detect(struct psmouse
*psmouse
, bool set_properties
)
510 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
511 unsigned char param
[4];
514 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
);
515 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
);
516 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
);
517 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
);
518 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETINFO
);
520 if (param
[0] != 0x00 || param
[1] != 0x33 || param
[2] != 0x55)
523 if (set_properties
) {
524 __set_bit(BTN_MIDDLE
, psmouse
->dev
->keybit
);
525 __set_bit(BTN_EXTRA
, psmouse
->dev
->keybit
);
526 __set_bit(BTN_SIDE
, psmouse
->dev
->keybit
);
527 __set_bit(REL_WHEEL
, psmouse
->dev
->relbit
);
529 psmouse
->vendor
= "Genius";
530 psmouse
->name
= "Mouse";
531 psmouse
->pktsize
= 4;
538 * IntelliMouse magic init.
540 static int intellimouse_detect(struct psmouse
*psmouse
, bool set_properties
)
542 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
543 unsigned char param
[2];
546 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
548 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
550 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
551 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETID
);
556 if (set_properties
) {
557 __set_bit(BTN_MIDDLE
, psmouse
->dev
->keybit
);
558 __set_bit(REL_WHEEL
, psmouse
->dev
->relbit
);
560 if (!psmouse
->vendor
)
561 psmouse
->vendor
= "Generic";
563 psmouse
->name
= "Wheel Mouse";
564 psmouse
->pktsize
= 4;
571 * Try IntelliMouse/Explorer magic init.
573 static int im_explorer_detect(struct psmouse
*psmouse
, bool set_properties
)
575 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
576 unsigned char param
[2];
578 intellimouse_detect(psmouse
, 0);
581 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
583 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
585 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
586 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETID
);
591 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
593 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
595 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
597 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
599 if (set_properties
) {
600 __set_bit(BTN_MIDDLE
, psmouse
->dev
->keybit
);
601 __set_bit(REL_WHEEL
, psmouse
->dev
->relbit
);
602 __set_bit(REL_HWHEEL
, psmouse
->dev
->relbit
);
603 __set_bit(BTN_SIDE
, psmouse
->dev
->keybit
);
604 __set_bit(BTN_EXTRA
, psmouse
->dev
->keybit
);
606 if (!psmouse
->vendor
)
607 psmouse
->vendor
= "Generic";
609 psmouse
->name
= "Explorer Mouse";
610 psmouse
->pktsize
= 4;
617 * Kensington ThinkingMouse / ExpertMouse magic init.
619 static int thinking_detect(struct psmouse
*psmouse
, bool set_properties
)
621 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
622 unsigned char param
[2];
623 static const unsigned char seq
[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
627 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
629 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
);
630 for (i
= 0; i
< ARRAY_SIZE(seq
); i
++) {
632 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
634 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETID
);
639 if (set_properties
) {
640 __set_bit(BTN_MIDDLE
, psmouse
->dev
->keybit
);
641 __set_bit(BTN_EXTRA
, psmouse
->dev
->keybit
);
643 psmouse
->vendor
= "Kensington";
644 psmouse
->name
= "ThinkingMouse";
651 * Bare PS/2 protocol "detection". Always succeeds.
653 static int ps2bare_detect(struct psmouse
*psmouse
, bool set_properties
)
655 if (set_properties
) {
656 if (!psmouse
->vendor
)
657 psmouse
->vendor
= "Generic";
659 psmouse
->name
= "Mouse";
662 * We have no way of figuring true number of buttons so let's
663 * assume that the device has 3.
665 __set_bit(BTN_MIDDLE
, psmouse
->dev
->keybit
);
672 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
673 * must be forced by sysfs protocol writing.
675 static int cortron_detect(struct psmouse
*psmouse
, bool set_properties
)
677 if (set_properties
) {
678 psmouse
->vendor
= "Cortron";
679 psmouse
->name
= "PS/2 Trackball";
681 __set_bit(BTN_MIDDLE
, psmouse
->dev
->keybit
);
682 __set_bit(BTN_SIDE
, psmouse
->dev
->keybit
);
688 static const struct psmouse_protocol psmouse_protocols
[] = {
694 .ignore_parity
= true,
695 .detect
= ps2bare_detect
,
696 .try_passthru
= true,
698 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
700 .type
= PSMOUSE_PS2PP
,
703 .detect
= ps2pp_detect
,
707 .type
= PSMOUSE_THINKPS
,
710 .detect
= thinking_detect
,
712 #ifdef CONFIG_MOUSE_PS2_CYPRESS
714 .type
= PSMOUSE_CYPRESS
,
717 .detect
= cypress_detect
,
718 .init
= cypress_init
,
722 .type
= PSMOUSE_GENPS
,
725 .detect
= genius_detect
,
728 .type
= PSMOUSE_IMPS
,
732 .ignore_parity
= true,
733 .detect
= intellimouse_detect
,
734 .try_passthru
= true,
737 .type
= PSMOUSE_IMEX
,
741 .ignore_parity
= true,
742 .detect
= im_explorer_detect
,
743 .try_passthru
= true,
745 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
747 .type
= PSMOUSE_SYNAPTICS
,
749 .alias
= "synaptics",
750 .detect
= synaptics_detect
,
751 .init
= synaptics_init
,
754 .type
= PSMOUSE_SYNAPTICS_RELATIVE
,
755 .name
= "SynRelPS/2",
756 .alias
= "synaptics-relative",
757 .detect
= synaptics_detect
,
758 .init
= synaptics_init_relative
,
761 #ifdef CONFIG_MOUSE_PS2_ALPS
763 .type
= PSMOUSE_ALPS
,
766 .detect
= alps_detect
,
770 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
772 .type
= PSMOUSE_LIFEBOOK
,
775 .detect
= lifebook_detect
,
776 .init
= lifebook_init
,
779 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
781 .type
= PSMOUSE_TRACKPOINT
,
783 .alias
= "trackpoint",
784 .detect
= trackpoint_detect
,
785 .try_passthru
= true,
788 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
790 .type
= PSMOUSE_TOUCHKIT_PS2
,
791 .name
= "touchkitPS/2",
793 .detect
= touchkit_ps2_detect
,
796 #ifdef CONFIG_MOUSE_PS2_OLPC
798 .type
= PSMOUSE_HGPK
,
801 .detect
= hgpk_detect
,
804 #ifdef CONFIG_MOUSE_PS2_ELANTECH
806 .type
= PSMOUSE_ELANTECH
,
809 .detect
= elantech_detect
,
810 .init
= elantech_init
,
813 #ifdef CONFIG_MOUSE_PS2_SENTELIC
818 .detect
= fsp_detect
,
823 .type
= PSMOUSE_CORTRON
,
824 .name
= "CortronPS/2",
826 .detect
= cortron_detect
,
828 #ifdef CONFIG_MOUSE_PS2_FOCALTECH
830 .type
= PSMOUSE_FOCALTECH
,
831 .name
= "FocalTechPS/2",
832 .alias
= "focaltech",
833 .detect
= focaltech_detect
,
834 .init
= focaltech_init
,
837 #ifdef CONFIG_MOUSE_PS2_VMMOUSE
839 .type
= PSMOUSE_VMMOUSE
,
840 .name
= VMMOUSE_PSNAME
,
842 .detect
= vmmouse_detect
,
843 .init
= vmmouse_init
,
846 #ifdef CONFIG_MOUSE_PS2_BYD
851 .detect
= byd_detect
,
856 .type
= PSMOUSE_AUTO
,
863 static const struct psmouse_protocol
*__psmouse_protocol_by_type(enum psmouse_type type
)
867 for (i
= 0; i
< ARRAY_SIZE(psmouse_protocols
); i
++)
868 if (psmouse_protocols
[i
].type
== type
)
869 return &psmouse_protocols
[i
];
874 static const struct psmouse_protocol
*psmouse_protocol_by_type(enum psmouse_type type
)
876 const struct psmouse_protocol
*proto
;
878 proto
= __psmouse_protocol_by_type(type
);
883 return &psmouse_protocols
[0];
886 static const struct psmouse_protocol
*psmouse_protocol_by_name(const char *name
, size_t len
)
888 const struct psmouse_protocol
*p
;
891 for (i
= 0; i
< ARRAY_SIZE(psmouse_protocols
); i
++) {
892 p
= &psmouse_protocols
[i
];
894 if ((strlen(p
->name
) == len
&& !strncmp(p
->name
, name
, len
)) ||
895 (strlen(p
->alias
) == len
&& !strncmp(p
->alias
, name
, len
)))
896 return &psmouse_protocols
[i
];
903 * Apply default settings to the psmouse structure. Most of them will
904 * be overridden by individual protocol initialization routines.
906 static void psmouse_apply_defaults(struct psmouse
*psmouse
)
908 struct input_dev
*input_dev
= psmouse
->dev
;
910 memset(input_dev
->evbit
, 0, sizeof(input_dev
->evbit
));
911 memset(input_dev
->keybit
, 0, sizeof(input_dev
->keybit
));
912 memset(input_dev
->relbit
, 0, sizeof(input_dev
->relbit
));
913 memset(input_dev
->absbit
, 0, sizeof(input_dev
->absbit
));
914 memset(input_dev
->mscbit
, 0, sizeof(input_dev
->mscbit
));
916 __set_bit(EV_KEY
, input_dev
->evbit
);
917 __set_bit(EV_REL
, input_dev
->evbit
);
919 __set_bit(BTN_LEFT
, input_dev
->keybit
);
920 __set_bit(BTN_RIGHT
, input_dev
->keybit
);
922 __set_bit(REL_X
, input_dev
->relbit
);
923 __set_bit(REL_Y
, input_dev
->relbit
);
925 __set_bit(INPUT_PROP_POINTER
, input_dev
->propbit
);
927 psmouse
->set_rate
= psmouse_set_rate
;
928 psmouse
->set_resolution
= psmouse_set_resolution
;
929 psmouse
->set_scale
= psmouse_set_scale
;
930 psmouse
->poll
= psmouse_poll
;
931 psmouse
->protocol_handler
= psmouse_process_byte
;
932 psmouse
->pktsize
= 3;
933 psmouse
->reconnect
= NULL
;
934 psmouse
->disconnect
= NULL
;
935 psmouse
->cleanup
= NULL
;
936 psmouse
->pt_activate
= NULL
;
937 psmouse
->pt_deactivate
= NULL
;
940 static bool psmouse_try_protocol(struct psmouse
*psmouse
,
941 enum psmouse_type type
,
942 unsigned int *max_proto
,
943 bool set_properties
, bool init_allowed
)
945 const struct psmouse_protocol
*proto
;
947 proto
= __psmouse_protocol_by_type(type
);
951 if (psmouse
->ps2dev
.serio
->id
.type
== SERIO_PS_PSTHRU
&&
952 !proto
->try_passthru
) {
957 psmouse_apply_defaults(psmouse
);
959 if (proto
->detect(psmouse
, set_properties
) != 0)
962 if (set_properties
&& proto
->init
&& init_allowed
) {
963 if (proto
->init(psmouse
) != 0) {
965 * We detected device, but init failed. Adjust
966 * max_proto so we only try standard protocols.
968 if (*max_proto
> PSMOUSE_IMEX
)
969 *max_proto
= PSMOUSE_IMEX
;
979 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
980 * the mouse may have.
982 static int psmouse_extensions(struct psmouse
*psmouse
,
983 unsigned int max_proto
, bool set_properties
)
985 bool synaptics_hardware
= false;
988 * Always check for focaltech, this is safe as it uses pnp-id
991 if (psmouse_try_protocol(psmouse
, PSMOUSE_FOCALTECH
,
992 &max_proto
, set_properties
, false)) {
993 if (max_proto
> PSMOUSE_IMEX
&&
994 IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH
) &&
995 (!set_properties
|| focaltech_init(psmouse
) == 0)) {
996 return PSMOUSE_FOCALTECH
;
999 * Restrict psmouse_max_proto so that psmouse_initialize()
1000 * does not try to reset rate and resolution, because even
1001 * that upsets the device.
1002 * This also causes us to basically fall through to basic
1003 * protocol detection, where we fully reset the mouse,
1004 * and set it up as bare PS/2 protocol device.
1006 psmouse_max_proto
= max_proto
= PSMOUSE_PS2
;
1010 * We always check for LifeBook because it does not disturb mouse
1011 * (it only checks DMI information).
1013 if (psmouse_try_protocol(psmouse
, PSMOUSE_LIFEBOOK
, &max_proto
,
1014 set_properties
, max_proto
> PSMOUSE_IMEX
))
1015 return PSMOUSE_LIFEBOOK
;
1017 if (psmouse_try_protocol(psmouse
, PSMOUSE_VMMOUSE
, &max_proto
,
1018 set_properties
, max_proto
> PSMOUSE_IMEX
))
1019 return PSMOUSE_VMMOUSE
;
1022 * Try Kensington ThinkingMouse (we try first, because Synaptics
1023 * probe upsets the ThinkingMouse).
1025 if (max_proto
> PSMOUSE_IMEX
&&
1026 psmouse_try_protocol(psmouse
, PSMOUSE_THINKPS
, &max_proto
,
1027 set_properties
, true)) {
1028 return PSMOUSE_THINKPS
;
1032 * Try Synaptics TouchPad. Note that probing is done even if
1033 * Synaptics protocol support is disabled in config - we need to
1034 * know if it is Synaptics so we can reset it properly after
1035 * probing for IntelliMouse.
1037 if (max_proto
> PSMOUSE_PS2
&&
1038 psmouse_try_protocol(psmouse
, PSMOUSE_SYNAPTICS
, &max_proto
,
1039 set_properties
, false)) {
1040 synaptics_hardware
= true;
1042 if (max_proto
> PSMOUSE_IMEX
) {
1044 * Try activating protocol, but check if support is
1045 * enabled first, since we try detecting Synaptics
1046 * even when protocol is disabled.
1048 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS
) &&
1049 (!set_properties
|| synaptics_init(psmouse
) == 0)) {
1050 return PSMOUSE_SYNAPTICS
;
1054 * Some Synaptics touchpads can emulate extended
1055 * protocols (like IMPS/2). Unfortunately
1056 * Logitech/Genius probes confuse some firmware
1057 * versions so we'll have to skip them.
1059 max_proto
= PSMOUSE_IMEX
;
1063 * Make sure that touchpad is in relative mode, gestures
1064 * (taps) are enabled.
1066 synaptics_reset(psmouse
);
1070 * Try Cypress Trackpad. We must try it before Finger Sensing Pad
1071 * because Finger Sensing Pad probe upsets some modules of Cypress
1074 if (max_proto
> PSMOUSE_IMEX
&&
1075 psmouse_try_protocol(psmouse
, PSMOUSE_CYPRESS
, &max_proto
,
1076 set_properties
, true)) {
1077 return PSMOUSE_CYPRESS
;
1080 /* Try ALPS TouchPad */
1081 if (max_proto
> PSMOUSE_IMEX
) {
1082 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
);
1083 if (psmouse_try_protocol(psmouse
, PSMOUSE_ALPS
,
1084 &max_proto
, set_properties
, true))
1085 return PSMOUSE_ALPS
;
1088 /* Try OLPC HGPK touchpad */
1089 if (max_proto
> PSMOUSE_IMEX
&&
1090 psmouse_try_protocol(psmouse
, PSMOUSE_HGPK
, &max_proto
,
1091 set_properties
, true)) {
1092 return PSMOUSE_HGPK
;
1095 /* Try Elantech touchpad */
1096 if (max_proto
> PSMOUSE_IMEX
&&
1097 psmouse_try_protocol(psmouse
, PSMOUSE_ELANTECH
,
1098 &max_proto
, set_properties
, true)) {
1099 return PSMOUSE_ELANTECH
;
1102 if (max_proto
> PSMOUSE_IMEX
) {
1103 if (psmouse_try_protocol(psmouse
, PSMOUSE_GENPS
,
1104 &max_proto
, set_properties
, true))
1105 return PSMOUSE_GENPS
;
1107 if (psmouse_try_protocol(psmouse
, PSMOUSE_PS2PP
,
1108 &max_proto
, set_properties
, true))
1109 return PSMOUSE_PS2PP
;
1111 if (psmouse_try_protocol(psmouse
, PSMOUSE_TRACKPOINT
,
1112 &max_proto
, set_properties
, true))
1113 return PSMOUSE_TRACKPOINT
;
1115 if (psmouse_try_protocol(psmouse
, PSMOUSE_TOUCHKIT_PS2
,
1116 &max_proto
, set_properties
, true))
1117 return PSMOUSE_TOUCHKIT_PS2
;
1119 if (psmouse_try_protocol(psmouse
, PSMOUSE_BYD
,
1120 &max_proto
, set_properties
, true))
1125 * Try Finger Sensing Pad. We do it here because its probe upsets
1126 * Trackpoint devices (causing TP_READ_ID command to time out).
1128 if (max_proto
> PSMOUSE_IMEX
&&
1129 psmouse_try_protocol(psmouse
, PSMOUSE_FSP
,
1130 &max_proto
, set_properties
, true)) {
1135 * Reset to defaults in case the device got confused by extended
1136 * protocol probes. Note that we follow up with full reset because
1137 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
1139 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
);
1140 psmouse_reset(psmouse
);
1142 if (max_proto
>= PSMOUSE_IMEX
&&
1143 psmouse_try_protocol(psmouse
, PSMOUSE_IMEX
,
1144 &max_proto
, set_properties
, true)) {
1145 return PSMOUSE_IMEX
;
1148 if (max_proto
>= PSMOUSE_IMPS
&&
1149 psmouse_try_protocol(psmouse
, PSMOUSE_IMPS
,
1150 &max_proto
, set_properties
, true)) {
1151 return PSMOUSE_IMPS
;
1155 * Okay, all failed, we have a standard mouse here. The number of
1156 * the buttons is still a question, though. We assume 3.
1158 psmouse_try_protocol(psmouse
, PSMOUSE_PS2
,
1159 &max_proto
, set_properties
, true);
1161 if (synaptics_hardware
) {
1163 * We detected Synaptics hardware but it did not respond to
1164 * IMPS/2 probes. We need to reset the touchpad because if
1165 * there is a track point on the pass through port it could
1166 * get disabled while probing for protocol extensions.
1168 psmouse_reset(psmouse
);
1175 * psmouse_probe() probes for a PS/2 mouse.
1177 static int psmouse_probe(struct psmouse
*psmouse
)
1179 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
1180 unsigned char param
[2];
1183 * First, we check if it's a mouse. It should send 0x00 or 0x03 in
1184 * case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1185 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and
1186 * subsequent ID queries, probably due to a firmware bug.
1189 if (ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETID
))
1192 if (param
[0] != 0x00 && param
[0] != 0x03 &&
1193 param
[0] != 0x04 && param
[0] != 0xff)
1197 * Then we reset and disable the mouse so that it doesn't generate
1200 if (ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
))
1201 psmouse_warn(psmouse
, "Failed to reset mouse on %s\n",
1202 ps2dev
->serio
->phys
);
1208 * psmouse_initialize() initializes the mouse to a sane state.
1210 static void psmouse_initialize(struct psmouse
*psmouse
)
1213 * We set the mouse report rate, resolution and scaling.
1215 if (psmouse_max_proto
!= PSMOUSE_PS2
) {
1216 psmouse
->set_rate(psmouse
, psmouse
->rate
);
1217 psmouse
->set_resolution(psmouse
, psmouse
->resolution
);
1218 psmouse
->set_scale(psmouse
, PSMOUSE_SCALE11
);
1223 * psmouse_activate() enables the mouse so that we get motion reports from it.
1225 int psmouse_activate(struct psmouse
*psmouse
)
1227 if (ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_ENABLE
)) {
1228 psmouse_warn(psmouse
, "Failed to enable mouse on %s\n",
1229 psmouse
->ps2dev
.serio
->phys
);
1233 psmouse_set_state(psmouse
, PSMOUSE_ACTIVATED
);
1238 * psmouse_deactivate() puts the mouse into poll mode so that we don't get
1239 * motion reports from it unless we explicitly request it.
1241 int psmouse_deactivate(struct psmouse
*psmouse
)
1243 if (ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_DISABLE
)) {
1244 psmouse_warn(psmouse
, "Failed to deactivate mouse on %s\n",
1245 psmouse
->ps2dev
.serio
->phys
);
1249 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1254 * psmouse_resync() attempts to re-validate current protocol.
1256 static void psmouse_resync(struct work_struct
*work
)
1258 struct psmouse
*parent
= NULL
, *psmouse
=
1259 container_of(work
, struct psmouse
, resync_work
.work
);
1260 struct serio
*serio
= psmouse
->ps2dev
.serio
;
1261 psmouse_ret_t rc
= PSMOUSE_GOOD_DATA
;
1262 bool failed
= false, enabled
= false;
1265 mutex_lock(&psmouse_mutex
);
1267 if (psmouse
->state
!= PSMOUSE_RESYNCING
)
1270 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1271 parent
= serio_get_drvdata(serio
->parent
);
1272 psmouse_deactivate(parent
);
1276 * Some mice don't ACK commands sent while they are in the middle of
1277 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1278 * instead of ps2_command() which would wait for 200ms for an ACK
1279 * that may never come.
1280 * As an additional quirk ALPS touchpads may not only forget to ACK
1281 * disable command but will stop reporting taps, so if we see that
1282 * mouse at least once ACKs disable we will do full reconnect if ACK
1285 psmouse
->num_resyncs
++;
1287 if (ps2_sendbyte(&psmouse
->ps2dev
, PSMOUSE_CMD_DISABLE
, 20)) {
1288 if (psmouse
->num_resyncs
< 3 || psmouse
->acks_disable_command
)
1291 psmouse
->acks_disable_command
= true;
1294 * Poll the mouse. If it was reset the packet will be shorter than
1295 * psmouse->pktsize and ps2_command will fail. We do not expect and
1296 * do not handle scenario when mouse "upgrades" its protocol while
1297 * disconnected since it would require additional delay. If we ever
1298 * see a mouse that does it we'll adjust the code.
1301 if (psmouse
->poll(psmouse
))
1304 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1305 for (i
= 0; i
< psmouse
->pktsize
; i
++) {
1307 rc
= psmouse
->protocol_handler(psmouse
);
1308 if (rc
!= PSMOUSE_GOOD_DATA
)
1311 if (rc
!= PSMOUSE_FULL_PACKET
)
1313 psmouse_set_state(psmouse
, PSMOUSE_RESYNCING
);
1318 * Now try to enable mouse. We try to do that even if poll failed
1319 * and also repeat our attempts 5 times, otherwise we may be left
1320 * out with disabled mouse.
1322 for (i
= 0; i
< 5; i
++) {
1323 if (!ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_ENABLE
)) {
1331 psmouse_warn(psmouse
, "failed to re-enable mouse on %s\n",
1332 psmouse
->ps2dev
.serio
->phys
);
1337 psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
1338 psmouse_info(psmouse
,
1339 "resync failed, issuing reconnect request\n");
1340 serio_reconnect(serio
);
1342 psmouse_set_state(psmouse
, PSMOUSE_ACTIVATED
);
1345 psmouse_activate(parent
);
1347 mutex_unlock(&psmouse_mutex
);
1351 * psmouse_cleanup() resets the mouse into power-on state.
1353 static void psmouse_cleanup(struct serio
*serio
)
1355 struct psmouse
*psmouse
= serio_get_drvdata(serio
);
1356 struct psmouse
*parent
= NULL
;
1358 mutex_lock(&psmouse_mutex
);
1360 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1361 parent
= serio_get_drvdata(serio
->parent
);
1362 psmouse_deactivate(parent
);
1365 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
1368 * Disable stream mode so cleanup routine can proceed undisturbed.
1370 if (ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_DISABLE
))
1371 psmouse_warn(psmouse
, "Failed to disable mouse on %s\n",
1372 psmouse
->ps2dev
.serio
->phys
);
1374 if (psmouse
->cleanup
)
1375 psmouse
->cleanup(psmouse
);
1378 * Reset the mouse to defaults (bare PS/2 protocol).
1380 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
);
1383 * Some boxes, such as HP nx7400, get terribly confused if mouse
1384 * is not fully enabled before suspending/shutting down.
1386 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_ENABLE
);
1389 if (parent
->pt_deactivate
)
1390 parent
->pt_deactivate(parent
);
1392 psmouse_activate(parent
);
1395 mutex_unlock(&psmouse_mutex
);
1399 * psmouse_disconnect() closes and frees.
1401 static void psmouse_disconnect(struct serio
*serio
)
1403 struct psmouse
*psmouse
, *parent
= NULL
;
1405 psmouse
= serio_get_drvdata(serio
);
1407 sysfs_remove_group(&serio
->dev
.kobj
, &psmouse_attribute_group
);
1409 mutex_lock(&psmouse_mutex
);
1411 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1413 /* make sure we don't have a resync in progress */
1414 mutex_unlock(&psmouse_mutex
);
1415 flush_workqueue(kpsmoused_wq
);
1416 mutex_lock(&psmouse_mutex
);
1418 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1419 parent
= serio_get_drvdata(serio
->parent
);
1420 psmouse_deactivate(parent
);
1423 if (psmouse
->disconnect
)
1424 psmouse
->disconnect(psmouse
);
1426 if (parent
&& parent
->pt_deactivate
)
1427 parent
->pt_deactivate(parent
);
1429 psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
1432 serio_set_drvdata(serio
, NULL
);
1433 input_unregister_device(psmouse
->dev
);
1437 psmouse_activate(parent
);
1439 mutex_unlock(&psmouse_mutex
);
1442 static int psmouse_switch_protocol(struct psmouse
*psmouse
,
1443 const struct psmouse_protocol
*proto
)
1445 const struct psmouse_protocol
*selected_proto
;
1446 struct input_dev
*input_dev
= psmouse
->dev
;
1448 input_dev
->dev
.parent
= &psmouse
->ps2dev
.serio
->dev
;
1450 if (proto
&& (proto
->detect
|| proto
->init
)) {
1451 psmouse_apply_defaults(psmouse
);
1453 if (proto
->detect
&& proto
->detect(psmouse
, true) < 0)
1456 if (proto
->init
&& proto
->init(psmouse
) < 0)
1459 psmouse
->type
= proto
->type
;
1460 selected_proto
= proto
;
1462 psmouse
->type
= psmouse_extensions(psmouse
,
1463 psmouse_max_proto
, true);
1464 selected_proto
= psmouse_protocol_by_type(psmouse
->type
);
1467 psmouse
->ignore_parity
= selected_proto
->ignore_parity
;
1470 * If mouse's packet size is 3 there is no point in polling the
1471 * device in hopes to detect protocol reset - we won't get less
1472 * than 3 bytes response anyhow.
1474 if (psmouse
->pktsize
== 3)
1475 psmouse
->resync_time
= 0;
1478 * Some smart KVMs fake response to POLL command returning just
1479 * 3 bytes and messing up our resync logic, so if initial poll
1480 * fails we won't try polling the device anymore. Hopefully
1481 * such KVM will maintain initially selected protocol.
1483 if (psmouse
->resync_time
&& psmouse
->poll(psmouse
))
1484 psmouse
->resync_time
= 0;
1486 snprintf(psmouse
->devname
, sizeof(psmouse
->devname
), "%s %s %s",
1487 selected_proto
->name
, psmouse
->vendor
, psmouse
->name
);
1489 input_dev
->name
= psmouse
->devname
;
1490 input_dev
->phys
= psmouse
->phys
;
1491 input_dev
->id
.bustype
= BUS_I8042
;
1492 input_dev
->id
.vendor
= 0x0002;
1493 input_dev
->id
.product
= psmouse
->type
;
1494 input_dev
->id
.version
= psmouse
->model
;
1500 * psmouse_connect() is a callback from the serio module when
1501 * an unhandled serio port is found.
1503 static int psmouse_connect(struct serio
*serio
, struct serio_driver
*drv
)
1505 struct psmouse
*psmouse
, *parent
= NULL
;
1506 struct input_dev
*input_dev
;
1507 int retval
= 0, error
= -ENOMEM
;
1509 mutex_lock(&psmouse_mutex
);
1512 * If this is a pass-through port deactivate parent so the device
1513 * connected to this port can be successfully identified
1515 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1516 parent
= serio_get_drvdata(serio
->parent
);
1517 psmouse_deactivate(parent
);
1520 psmouse
= kzalloc(sizeof(struct psmouse
), GFP_KERNEL
);
1521 input_dev
= input_allocate_device();
1522 if (!psmouse
|| !input_dev
)
1525 ps2_init(&psmouse
->ps2dev
, serio
);
1526 INIT_DELAYED_WORK(&psmouse
->resync_work
, psmouse_resync
);
1527 psmouse
->dev
= input_dev
;
1528 snprintf(psmouse
->phys
, sizeof(psmouse
->phys
), "%s/input0", serio
->phys
);
1530 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
1532 serio_set_drvdata(serio
, psmouse
);
1534 error
= serio_open(serio
, drv
);
1536 goto err_clear_drvdata
;
1538 /* give PT device some time to settle down before probing */
1539 if (serio
->id
.type
== SERIO_PS_PSTHRU
)
1540 usleep_range(10000, 15000);
1542 if (psmouse_probe(psmouse
) < 0) {
1544 goto err_close_serio
;
1547 psmouse
->rate
= psmouse_rate
;
1548 psmouse
->resolution
= psmouse_resolution
;
1549 psmouse
->resetafter
= psmouse_resetafter
;
1550 psmouse
->resync_time
= parent
? 0 : psmouse_resync_time
;
1551 psmouse
->smartscroll
= psmouse_smartscroll
;
1553 psmouse_switch_protocol(psmouse
, NULL
);
1555 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1556 psmouse_initialize(psmouse
);
1558 error
= input_register_device(psmouse
->dev
);
1560 goto err_protocol_disconnect
;
1562 if (parent
&& parent
->pt_activate
)
1563 parent
->pt_activate(parent
);
1565 error
= sysfs_create_group(&serio
->dev
.kobj
, &psmouse_attribute_group
);
1567 goto err_pt_deactivate
;
1569 psmouse_activate(psmouse
);
1572 /* If this is a pass-through port the parent needs to be re-activated */
1574 psmouse_activate(parent
);
1576 mutex_unlock(&psmouse_mutex
);
1580 if (parent
&& parent
->pt_deactivate
)
1581 parent
->pt_deactivate(parent
);
1582 input_unregister_device(psmouse
->dev
);
1583 input_dev
= NULL
; /* so we don't try to free it below */
1584 err_protocol_disconnect
:
1585 if (psmouse
->disconnect
)
1586 psmouse
->disconnect(psmouse
);
1587 psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
1591 serio_set_drvdata(serio
, NULL
);
1593 input_free_device(input_dev
);
1600 static int psmouse_reconnect(struct serio
*serio
)
1602 struct psmouse
*psmouse
= serio_get_drvdata(serio
);
1603 struct psmouse
*parent
= NULL
;
1607 mutex_lock(&psmouse_mutex
);
1609 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1610 parent
= serio_get_drvdata(serio
->parent
);
1611 psmouse_deactivate(parent
);
1614 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
1616 if (psmouse
->reconnect
) {
1617 if (psmouse
->reconnect(psmouse
))
1620 psmouse_reset(psmouse
);
1622 if (psmouse_probe(psmouse
) < 0)
1625 type
= psmouse_extensions(psmouse
, psmouse_max_proto
, false);
1626 if (psmouse
->type
!= type
)
1631 * OK, the device type (and capabilities) match the old one,
1632 * we can continue using it, complete initialization
1634 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1636 psmouse_initialize(psmouse
);
1638 if (parent
&& parent
->pt_activate
)
1639 parent
->pt_activate(parent
);
1641 psmouse_activate(psmouse
);
1645 /* If this is a pass-through port the parent waits to be activated */
1647 psmouse_activate(parent
);
1649 mutex_unlock(&psmouse_mutex
);
1653 static struct serio_device_id psmouse_serio_ids
[] = {
1661 .type
= SERIO_PS_PSTHRU
,
1669 MODULE_DEVICE_TABLE(serio
, psmouse_serio_ids
);
1671 static struct serio_driver psmouse_drv
= {
1675 .description
= DRIVER_DESC
,
1676 .id_table
= psmouse_serio_ids
,
1677 .interrupt
= psmouse_interrupt
,
1678 .connect
= psmouse_connect
,
1679 .reconnect
= psmouse_reconnect
,
1680 .disconnect
= psmouse_disconnect
,
1681 .cleanup
= psmouse_cleanup
,
1684 ssize_t
psmouse_attr_show_helper(struct device
*dev
, struct device_attribute
*devattr
,
1687 struct serio
*serio
= to_serio_port(dev
);
1688 struct psmouse_attribute
*attr
= to_psmouse_attr(devattr
);
1689 struct psmouse
*psmouse
;
1691 psmouse
= serio_get_drvdata(serio
);
1693 return attr
->show(psmouse
, attr
->data
, buf
);
1696 ssize_t
psmouse_attr_set_helper(struct device
*dev
, struct device_attribute
*devattr
,
1697 const char *buf
, size_t count
)
1699 struct serio
*serio
= to_serio_port(dev
);
1700 struct psmouse_attribute
*attr
= to_psmouse_attr(devattr
);
1701 struct psmouse
*psmouse
, *parent
= NULL
;
1704 retval
= mutex_lock_interruptible(&psmouse_mutex
);
1708 psmouse
= serio_get_drvdata(serio
);
1710 if (attr
->protect
) {
1711 if (psmouse
->state
== PSMOUSE_IGNORE
) {
1716 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1717 parent
= serio_get_drvdata(serio
->parent
);
1718 psmouse_deactivate(parent
);
1721 psmouse_deactivate(psmouse
);
1724 retval
= attr
->set(psmouse
, attr
->data
, buf
, count
);
1726 if (attr
->protect
) {
1727 if (retval
!= -ENODEV
)
1728 psmouse_activate(psmouse
);
1731 psmouse_activate(parent
);
1735 mutex_unlock(&psmouse_mutex
);
1740 static ssize_t
psmouse_show_int_attr(struct psmouse
*psmouse
, void *offset
, char *buf
)
1742 unsigned int *field
= (unsigned int *)((char *)psmouse
+ (size_t)offset
);
1744 return sprintf(buf
, "%u\n", *field
);
1747 static ssize_t
psmouse_set_int_attr(struct psmouse
*psmouse
, void *offset
, const char *buf
, size_t count
)
1749 unsigned int *field
= (unsigned int *)((char *)psmouse
+ (size_t)offset
);
1753 err
= kstrtouint(buf
, 10, &value
);
1762 static ssize_t
psmouse_attr_show_protocol(struct psmouse
*psmouse
, void *data
, char *buf
)
1764 return sprintf(buf
, "%s\n", psmouse_protocol_by_type(psmouse
->type
)->name
);
1767 static ssize_t
psmouse_attr_set_protocol(struct psmouse
*psmouse
, void *data
, const char *buf
, size_t count
)
1769 struct serio
*serio
= psmouse
->ps2dev
.serio
;
1770 struct psmouse
*parent
= NULL
;
1771 struct input_dev
*old_dev
, *new_dev
;
1772 const struct psmouse_protocol
*proto
, *old_proto
;
1776 proto
= psmouse_protocol_by_name(buf
, count
);
1780 if (psmouse
->type
== proto
->type
)
1783 new_dev
= input_allocate_device();
1787 while (!list_empty(&serio
->children
)) {
1789 psmouse_warn(psmouse
,
1790 "failed to destroy children ports, protocol change aborted.\n");
1791 input_free_device(new_dev
);
1795 mutex_unlock(&psmouse_mutex
);
1796 serio_unregister_child_port(serio
);
1797 mutex_lock(&psmouse_mutex
);
1799 if (serio
->drv
!= &psmouse_drv
) {
1800 input_free_device(new_dev
);
1804 if (psmouse
->type
== proto
->type
) {
1805 input_free_device(new_dev
);
1806 return count
; /* switched by other thread */
1810 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1811 parent
= serio_get_drvdata(serio
->parent
);
1812 if (parent
->pt_deactivate
)
1813 parent
->pt_deactivate(parent
);
1816 old_dev
= psmouse
->dev
;
1817 old_proto
= psmouse_protocol_by_type(psmouse
->type
);
1819 if (psmouse
->disconnect
)
1820 psmouse
->disconnect(psmouse
);
1822 psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
1824 psmouse
->dev
= new_dev
;
1825 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
1827 if (psmouse_switch_protocol(psmouse
, proto
) < 0) {
1828 psmouse_reset(psmouse
);
1829 /* default to PSMOUSE_PS2 */
1830 psmouse_switch_protocol(psmouse
, &psmouse_protocols
[0]);
1833 psmouse_initialize(psmouse
);
1834 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1836 error
= input_register_device(psmouse
->dev
);
1838 if (psmouse
->disconnect
)
1839 psmouse
->disconnect(psmouse
);
1841 psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
1842 input_free_device(new_dev
);
1843 psmouse
->dev
= old_dev
;
1844 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
1845 psmouse_switch_protocol(psmouse
, old_proto
);
1846 psmouse_initialize(psmouse
);
1847 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1852 input_unregister_device(old_dev
);
1854 if (parent
&& parent
->pt_activate
)
1855 parent
->pt_activate(parent
);
1860 static ssize_t
psmouse_attr_set_rate(struct psmouse
*psmouse
, void *data
, const char *buf
, size_t count
)
1865 err
= kstrtouint(buf
, 10, &value
);
1869 psmouse
->set_rate(psmouse
, value
);
1873 static ssize_t
psmouse_attr_set_resolution(struct psmouse
*psmouse
, void *data
, const char *buf
, size_t count
)
1878 err
= kstrtouint(buf
, 10, &value
);
1882 psmouse
->set_resolution(psmouse
, value
);
1887 static int psmouse_set_maxproto(const char *val
, const struct kernel_param
*kp
)
1889 const struct psmouse_protocol
*proto
;
1894 proto
= psmouse_protocol_by_name(val
, strlen(val
));
1896 if (!proto
|| !proto
->maxproto
)
1899 *((unsigned int *)kp
->arg
) = proto
->type
;
1904 static int psmouse_get_maxproto(char *buffer
, const struct kernel_param
*kp
)
1906 int type
= *((unsigned int *)kp
->arg
);
1908 return sprintf(buffer
, "%s", psmouse_protocol_by_type(type
)->name
);
1911 static int __init
psmouse_init(void)
1915 lifebook_module_init();
1916 synaptics_module_init();
1919 kpsmoused_wq
= create_singlethread_workqueue("kpsmoused");
1920 if (!kpsmoused_wq
) {
1921 pr_err("failed to create kpsmoused workqueue\n");
1925 err
= serio_register_driver(&psmouse_drv
);
1927 destroy_workqueue(kpsmoused_wq
);
1932 static void __exit
psmouse_exit(void)
1934 serio_unregister_driver(&psmouse_drv
);
1935 destroy_workqueue(kpsmoused_wq
);
1938 module_init(psmouse_init
);
1939 module_exit(psmouse_exit
);