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"
40 #define DRIVER_DESC "PS/2 mouse driver"
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
43 MODULE_DESCRIPTION(DRIVER_DESC);
44 MODULE_LICENSE("GPL");
46 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
47 static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
48 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
49 static struct kernel_param_ops param_ops_proto_abbrev = {
50 .set = psmouse_set_maxproto,
51 .get = psmouse_get_maxproto,
53 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
54 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
55 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
57 static unsigned int psmouse_resolution = 200;
58 module_param_named(resolution, psmouse_resolution, uint, 0644);
59 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
61 static unsigned int psmouse_rate = 100;
62 module_param_named(rate, psmouse_rate, uint, 0644);
63 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
65 static bool psmouse_smartscroll = 1;
66 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
67 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
69 static unsigned int psmouse_resetafter = 5;
70 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
71 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
73 static unsigned int psmouse_resync_time;
74 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
75 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
77 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
79 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
80 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
81 (void *) offsetof(struct psmouse, rate),
82 psmouse_show_int_attr, psmouse_attr_set_rate);
83 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
84 (void *) offsetof(struct psmouse, resolution),
85 psmouse_show_int_attr, psmouse_attr_set_resolution);
86 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
87 (void *) offsetof(struct psmouse, resetafter),
88 psmouse_show_int_attr, psmouse_set_int_attr);
89 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
90 (void *) offsetof(struct psmouse, resync_time),
91 psmouse_show_int_attr, psmouse_set_int_attr);
93 static struct attribute *psmouse_attributes[] = {
94 &psmouse_attr_protocol.dattr.attr,
95 &psmouse_attr_rate.dattr.attr,
96 &psmouse_attr_resolution.dattr.attr,
97 &psmouse_attr_resetafter.dattr.attr,
98 &psmouse_attr_resync_time.dattr.attr,
102 static struct attribute_group psmouse_attribute_group = {
103 .attrs = psmouse_attributes,
107 * psmouse_mutex protects all operations changing state of mouse
108 * (connecting, disconnecting, changing rate or resolution via
109 * sysfs). We could use a per-device semaphore but since there
110 * rarely more than one PS/2 mouse connected and since semaphore
111 * is taken in "slow" paths it is not worth it.
113 static DEFINE_MUTEX(psmouse_mutex);
115 static struct workqueue_struct *kpsmoused_wq;
117 struct psmouse_protocol {
118 enum psmouse_type type;
120 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
123 int (*detect)(struct psmouse *, bool);
124 int (*init)(struct psmouse *);
128 * psmouse_process_byte() analyzes the PS/2 data stream and reports
129 * relevant events to the input module once full packet has arrived.
132 psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
134 struct input_dev *dev = psmouse->dev;
135 unsigned char *packet = psmouse->packet;
137 if (psmouse->pktcnt < psmouse->pktsize)
138 return PSMOUSE_GOOD_DATA;
141 * Full packet accumulated, process it
145 * Scroll wheel on IntelliMice, scroll buttons on NetMice
148 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
149 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
152 * Scroll wheel and buttons on IntelliMouse Explorer
155 if (psmouse->type == PSMOUSE_IMEX) {
156 switch (packet[3] & 0xC0) {
157 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
158 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
160 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
161 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
165 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
166 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
167 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
173 * Extra buttons on Genius NewNet 3D
176 if (psmouse->type == PSMOUSE_GENPS) {
177 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
178 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
182 * Extra button on ThinkingMouse
184 if (psmouse->type == PSMOUSE_THINKPS) {
185 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
186 /* Without this bit of weirdness moving up gives wildly high Y changes. */
187 packet[1] |= (packet[0] & 0x40) << 1;
191 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
194 if (psmouse->type == PSMOUSE_CORTRON) {
195 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
203 input_report_key(dev, BTN_LEFT, packet[0] & 1);
204 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
205 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
207 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
208 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
212 return PSMOUSE_FULL_PACKET;
215 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
218 queue_delayed_work(kpsmoused_wq, work, delay);
222 * __psmouse_set_state() sets new psmouse state and resets all flags.
225 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
227 psmouse->state = new_state;
228 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
229 psmouse->ps2dev.flags = 0;
230 psmouse->last = jiffies;
235 * psmouse_set_state() sets new psmouse state and resets all flags and
236 * counters while holding serio lock so fighting with interrupt handler
240 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
242 serio_pause_rx(psmouse->ps2dev.serio);
243 __psmouse_set_state(psmouse, new_state);
244 serio_continue_rx(psmouse->ps2dev.serio);
248 * psmouse_handle_byte() processes one byte of the input data stream
249 * by calling corresponding protocol handler.
252 static int psmouse_handle_byte(struct psmouse *psmouse)
254 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
257 case PSMOUSE_BAD_DATA:
258 if (psmouse->state == PSMOUSE_ACTIVATED) {
259 psmouse_warn(psmouse,
260 "%s at %s lost sync at byte %d\n",
261 psmouse->name, psmouse->phys,
263 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
264 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
265 psmouse_notice(psmouse,
266 "issuing reconnect request\n");
267 serio_reconnect(psmouse->ps2dev.serio);
274 case PSMOUSE_FULL_PACKET:
276 if (psmouse->out_of_sync_cnt) {
277 psmouse->out_of_sync_cnt = 0;
278 psmouse_notice(psmouse,
279 "%s at %s - driver resynced.\n",
280 psmouse->name, psmouse->phys);
284 case PSMOUSE_GOOD_DATA:
291 * psmouse_interrupt() handles incoming characters, either passing them
292 * for normal processing or gathering them as command response.
295 static irqreturn_t psmouse_interrupt(struct serio *serio,
296 unsigned char data, unsigned int flags)
298 struct psmouse *psmouse = serio_get_drvdata(serio);
300 if (psmouse->state == PSMOUSE_IGNORE)
303 if (unlikely((flags & SERIO_TIMEOUT) ||
304 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
306 if (psmouse->state == PSMOUSE_ACTIVATED)
307 psmouse_warn(psmouse,
308 "bad data from KBC -%s%s\n",
309 flags & SERIO_TIMEOUT ? " timeout" : "",
310 flags & SERIO_PARITY ? " bad parity" : "");
311 ps2_cmd_aborted(&psmouse->ps2dev);
315 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
316 if (ps2_handle_ack(&psmouse->ps2dev, data))
319 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
320 if (ps2_handle_response(&psmouse->ps2dev, data))
323 if (psmouse->state <= PSMOUSE_RESYNCING)
326 if (psmouse->state == PSMOUSE_ACTIVATED &&
327 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
328 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
329 psmouse->name, psmouse->phys, psmouse->pktcnt);
330 psmouse->badbyte = psmouse->packet[0];
331 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
332 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
336 psmouse->packet[psmouse->pktcnt++] = data;
338 * Check if this is a new device announcement (0xAA 0x00)
340 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
341 if (psmouse->pktcnt == 1) {
342 psmouse->last = jiffies;
346 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
347 (psmouse->type == PSMOUSE_HGPK &&
348 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
349 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
350 serio_reconnect(serio);
354 * Not a new device, try processing first byte normally
357 if (psmouse_handle_byte(psmouse))
360 psmouse->packet[psmouse->pktcnt++] = data;
364 * See if we need to force resync because mouse was idle for too long
366 if (psmouse->state == PSMOUSE_ACTIVATED &&
367 psmouse->pktcnt == 1 && psmouse->resync_time &&
368 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
369 psmouse->badbyte = psmouse->packet[0];
370 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
371 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
375 psmouse->last = jiffies;
376 psmouse_handle_byte(psmouse);
384 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
385 * using sliced syntax, understood by advanced devices, such as Logitech
386 * or Synaptics touchpads. The command is encoded as:
387 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
390 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
394 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
397 for (i = 6; i >= 0; i -= 2) {
398 unsigned char d = (command >> i) & 3;
399 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
408 * psmouse_reset() resets the mouse into power-on state.
410 int psmouse_reset(struct psmouse *psmouse)
412 unsigned char param[2];
414 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
417 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
424 * Here we set the mouse resolution.
427 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
429 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
432 if (resolution == 0 || resolution > 200)
435 p = params[resolution / 50];
436 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
437 psmouse->resolution = 25 << p;
441 * Here we set the mouse report rate.
444 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
446 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
450 while (rates[i] > rate) i++;
452 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
457 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
460 static int psmouse_poll(struct psmouse *psmouse)
462 return ps2_command(&psmouse->ps2dev, psmouse->packet,
463 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
467 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
469 bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
473 if (!strncmp(psmouse->ps2dev.serio->firmware_id, "PNP:", 4))
474 for (i = 0; ids[i]; i++)
475 if (strstr(psmouse->ps2dev.serio->firmware_id, ids[i]))
482 * Genius NetMouse magic init.
484 static int genius_detect(struct psmouse *psmouse, bool set_properties)
486 struct ps2dev *ps2dev = &psmouse->ps2dev;
487 unsigned char param[4];
490 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
491 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
492 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
493 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
494 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
496 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
499 if (set_properties) {
500 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
501 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
502 __set_bit(BTN_SIDE, psmouse->dev->keybit);
503 __set_bit(REL_WHEEL, psmouse->dev->relbit);
505 psmouse->vendor = "Genius";
506 psmouse->name = "Mouse";
507 psmouse->pktsize = 4;
514 * IntelliMouse magic init.
516 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
518 struct ps2dev *ps2dev = &psmouse->ps2dev;
519 unsigned char param[2];
522 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
524 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
526 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
527 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
532 if (set_properties) {
533 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
534 __set_bit(REL_WHEEL, psmouse->dev->relbit);
536 if (!psmouse->vendor)
537 psmouse->vendor = "Generic";
539 psmouse->name = "Wheel Mouse";
540 psmouse->pktsize = 4;
547 * Try IntelliMouse/Explorer magic init.
549 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
551 struct ps2dev *ps2dev = &psmouse->ps2dev;
552 unsigned char param[2];
554 intellimouse_detect(psmouse, 0);
557 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
559 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
561 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
562 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
567 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
569 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
571 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
573 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
575 if (set_properties) {
576 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
577 __set_bit(REL_WHEEL, psmouse->dev->relbit);
578 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
579 __set_bit(BTN_SIDE, psmouse->dev->keybit);
580 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
582 if (!psmouse->vendor)
583 psmouse->vendor = "Generic";
585 psmouse->name = "Explorer Mouse";
586 psmouse->pktsize = 4;
593 * Kensington ThinkingMouse / ExpertMouse magic init.
595 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
597 struct ps2dev *ps2dev = &psmouse->ps2dev;
598 unsigned char param[2];
599 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
603 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
605 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
606 for (i = 0; i < ARRAY_SIZE(seq); i++) {
608 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
610 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
615 if (set_properties) {
616 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
617 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
619 psmouse->vendor = "Kensington";
620 psmouse->name = "ThinkingMouse";
627 * Bare PS/2 protocol "detection". Always succeeds.
629 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
631 if (set_properties) {
632 if (!psmouse->vendor)
633 psmouse->vendor = "Generic";
635 psmouse->name = "Mouse";
638 * We have no way of figuring true number of buttons so let's
639 * assume that the device has 3.
641 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
648 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
649 * must be forced by sysfs protocol writing.
651 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
653 if (set_properties) {
654 psmouse->vendor = "Cortron";
655 psmouse->name = "PS/2 Trackball";
657 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
658 __set_bit(BTN_SIDE, psmouse->dev->keybit);
665 * Apply default settings to the psmouse structure. Most of them will
666 * be overridden by individual protocol initialization routines.
669 static void psmouse_apply_defaults(struct psmouse *psmouse)
671 struct input_dev *input_dev = psmouse->dev;
673 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
674 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
675 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
676 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
677 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
679 __set_bit(EV_KEY, input_dev->evbit);
680 __set_bit(EV_REL, input_dev->evbit);
682 __set_bit(BTN_LEFT, input_dev->keybit);
683 __set_bit(BTN_RIGHT, input_dev->keybit);
685 __set_bit(REL_X, input_dev->relbit);
686 __set_bit(REL_Y, input_dev->relbit);
688 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
690 psmouse->set_rate = psmouse_set_rate;
691 psmouse->set_resolution = psmouse_set_resolution;
692 psmouse->poll = psmouse_poll;
693 psmouse->protocol_handler = psmouse_process_byte;
694 psmouse->pktsize = 3;
695 psmouse->reconnect = NULL;
696 psmouse->disconnect = NULL;
697 psmouse->cleanup = NULL;
698 psmouse->pt_activate = NULL;
699 psmouse->pt_deactivate = NULL;
703 * Apply default settings to the psmouse structure and call specified
704 * protocol detection or initialization routine.
706 static int psmouse_do_detect(int (*detect)(struct psmouse *psmouse,
707 bool set_properties),
708 struct psmouse *psmouse, bool set_properties)
711 psmouse_apply_defaults(psmouse);
713 return detect(psmouse, set_properties);
717 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
718 * the mouse may have.
721 static int psmouse_extensions(struct psmouse *psmouse,
722 unsigned int max_proto, bool set_properties)
724 bool synaptics_hardware = false;
726 /* Always check for focaltech, this is safe as it uses pnp-id matching */
727 if (psmouse_do_detect(focaltech_detect, psmouse, set_properties) == 0) {
728 if (max_proto > PSMOUSE_IMEX) {
729 if (!set_properties || focaltech_init(psmouse) == 0) {
730 if (IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH))
731 return PSMOUSE_FOCALTECH;
733 * Note that we need to also restrict
734 * psmouse_max_proto so that psmouse_initialize()
735 * does not try to reset rate and resolution,
736 * because even that upsets the device.
738 psmouse_max_proto = PSMOUSE_PS2;
745 * We always check for lifebook because it does not disturb mouse
746 * (it only checks DMI information).
748 if (psmouse_do_detect(lifebook_detect, psmouse, set_properties) == 0) {
749 if (max_proto > PSMOUSE_IMEX) {
750 if (!set_properties || lifebook_init(psmouse) == 0)
751 return PSMOUSE_LIFEBOOK;
756 * Try Kensington ThinkingMouse (we try first, because synaptics probe
757 * upsets the thinkingmouse).
760 if (max_proto > PSMOUSE_IMEX &&
761 psmouse_do_detect(thinking_detect, psmouse, set_properties) == 0) {
762 return PSMOUSE_THINKPS;
766 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
767 * support is disabled in config - we need to know if it is synaptics so we
768 * can reset it properly after probing for intellimouse.
770 if (max_proto > PSMOUSE_PS2 &&
771 psmouse_do_detect(synaptics_detect, psmouse, set_properties) == 0) {
772 synaptics_hardware = true;
774 if (max_proto > PSMOUSE_IMEX) {
776 * Try activating protocol, but check if support is enabled first, since
777 * we try detecting Synaptics even when protocol is disabled.
779 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) &&
780 (!set_properties || synaptics_init(psmouse) == 0)) {
781 return PSMOUSE_SYNAPTICS;
785 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
786 * Unfortunately Logitech/Genius probes confuse some firmware versions so
787 * we'll have to skip them.
789 max_proto = PSMOUSE_IMEX;
792 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
794 synaptics_reset(psmouse);
798 * Try Cypress Trackpad.
799 * Must try it before Finger Sensing Pad because Finger Sensing Pad probe
800 * upsets some modules of Cypress Trackpads.
802 if (max_proto > PSMOUSE_IMEX &&
803 cypress_detect(psmouse, set_properties) == 0) {
804 if (IS_ENABLED(CONFIG_MOUSE_PS2_CYPRESS)) {
805 if (cypress_init(psmouse) == 0)
806 return PSMOUSE_CYPRESS;
809 * Finger Sensing Pad probe upsets some modules of
810 * Cypress Trackpad, must avoid Finger Sensing Pad
811 * probe if Cypress Trackpad device detected.
816 max_proto = PSMOUSE_IMEX;
822 if (max_proto > PSMOUSE_IMEX) {
823 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
824 if (psmouse_do_detect(alps_detect,
825 psmouse, set_properties) == 0) {
826 if (!set_properties || alps_init(psmouse) == 0)
829 * Init failed, try basic relative protocols
831 max_proto = PSMOUSE_IMEX;
836 * Try OLPC HGPK touchpad.
838 if (max_proto > PSMOUSE_IMEX &&
839 psmouse_do_detect(hgpk_detect, psmouse, set_properties) == 0) {
840 if (!set_properties || hgpk_init(psmouse) == 0)
843 * Init failed, try basic relative protocols
845 max_proto = PSMOUSE_IMEX;
849 * Try Elantech touchpad.
851 if (max_proto > PSMOUSE_IMEX &&
852 psmouse_do_detect(elantech_detect, psmouse, set_properties) == 0) {
853 if (!set_properties || elantech_init(psmouse) == 0)
854 return PSMOUSE_ELANTECH;
856 * Init failed, try basic relative protocols
858 max_proto = PSMOUSE_IMEX;
861 if (max_proto > PSMOUSE_IMEX) {
862 if (psmouse_do_detect(genius_detect,
863 psmouse, set_properties) == 0)
864 return PSMOUSE_GENPS;
866 if (psmouse_do_detect(ps2pp_init,
867 psmouse, set_properties) == 0)
868 return PSMOUSE_PS2PP;
870 if (psmouse_do_detect(trackpoint_detect,
871 psmouse, set_properties) == 0)
872 return PSMOUSE_TRACKPOINT;
874 if (psmouse_do_detect(touchkit_ps2_detect,
875 psmouse, set_properties) == 0)
876 return PSMOUSE_TOUCHKIT_PS2;
880 * Try Finger Sensing Pad. We do it here because its probe upsets
881 * Trackpoint devices (causing TP_READ_ID command to time out).
883 if (max_proto > PSMOUSE_IMEX) {
884 if (psmouse_do_detect(fsp_detect,
885 psmouse, set_properties) == 0) {
886 if (!set_properties || fsp_init(psmouse) == 0)
889 * Init failed, try basic relative protocols
891 max_proto = PSMOUSE_IMEX;
896 * Reset to defaults in case the device got confused by extended
897 * protocol probes. Note that we follow up with full reset because
898 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
900 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
901 psmouse_reset(psmouse);
903 if (max_proto >= PSMOUSE_IMEX &&
904 psmouse_do_detect(im_explorer_detect,
905 psmouse, set_properties) == 0) {
909 if (max_proto >= PSMOUSE_IMPS &&
910 psmouse_do_detect(intellimouse_detect,
911 psmouse, set_properties) == 0) {
916 * Okay, all failed, we have a standard mouse here. The number of the buttons
917 * is still a question, though. We assume 3.
919 psmouse_do_detect(ps2bare_detect, psmouse, set_properties);
921 if (synaptics_hardware) {
923 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
924 * We need to reset the touchpad because if there is a track point on the
925 * pass through port it could get disabled while probing for protocol
928 psmouse_reset(psmouse);
934 static const struct psmouse_protocol psmouse_protocols[] = {
940 .ignore_parity = true,
941 .detect = ps2bare_detect,
943 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
945 .type = PSMOUSE_PS2PP,
948 .detect = ps2pp_init,
952 .type = PSMOUSE_THINKPS,
955 .detect = thinking_detect,
957 #ifdef CONFIG_MOUSE_PS2_CYPRESS
959 .type = PSMOUSE_CYPRESS,
962 .detect = cypress_detect,
963 .init = cypress_init,
967 .type = PSMOUSE_GENPS,
970 .detect = genius_detect,
973 .type = PSMOUSE_IMPS,
977 .ignore_parity = true,
978 .detect = intellimouse_detect,
981 .type = PSMOUSE_IMEX,
985 .ignore_parity = true,
986 .detect = im_explorer_detect,
988 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
990 .type = PSMOUSE_SYNAPTICS,
992 .alias = "synaptics",
993 .detect = synaptics_detect,
994 .init = synaptics_init,
997 .type = PSMOUSE_SYNAPTICS_RELATIVE,
998 .name = "SynRelPS/2",
999 .alias = "synaptics-relative",
1000 .detect = synaptics_detect,
1001 .init = synaptics_init_relative,
1004 #ifdef CONFIG_MOUSE_PS2_ALPS
1006 .type = PSMOUSE_ALPS,
1009 .detect = alps_detect,
1013 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
1015 .type = PSMOUSE_LIFEBOOK,
1017 .alias = "lifebook",
1018 .init = lifebook_init,
1021 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
1023 .type = PSMOUSE_TRACKPOINT,
1025 .alias = "trackpoint",
1026 .detect = trackpoint_detect,
1029 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
1031 .type = PSMOUSE_TOUCHKIT_PS2,
1032 .name = "touchkitPS/2",
1033 .alias = "touchkit",
1034 .detect = touchkit_ps2_detect,
1037 #ifdef CONFIG_MOUSE_PS2_OLPC
1039 .type = PSMOUSE_HGPK,
1040 .name = "OLPC HGPK",
1042 .detect = hgpk_detect,
1045 #ifdef CONFIG_MOUSE_PS2_ELANTECH
1047 .type = PSMOUSE_ELANTECH,
1049 .alias = "elantech",
1050 .detect = elantech_detect,
1051 .init = elantech_init,
1054 #ifdef CONFIG_MOUSE_PS2_SENTELIC
1056 .type = PSMOUSE_FSP,
1059 .detect = fsp_detect,
1064 .type = PSMOUSE_CORTRON,
1065 .name = "CortronPS/2",
1067 .detect = cortron_detect,
1069 #ifdef CONFIG_MOUSE_PS2_FOCALTECH
1071 .type = PSMOUSE_FOCALTECH,
1072 .name = "FocalTechPS/2",
1073 .alias = "focaltech",
1074 .detect = focaltech_detect,
1075 .init = focaltech_init,
1079 .type = PSMOUSE_AUTO,
1086 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
1090 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
1091 if (psmouse_protocols[i].type == type)
1092 return &psmouse_protocols[i];
1095 return &psmouse_protocols[0];
1098 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
1100 const struct psmouse_protocol *p;
1103 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
1104 p = &psmouse_protocols[i];
1106 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
1107 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
1108 return &psmouse_protocols[i];
1116 * psmouse_probe() probes for a PS/2 mouse.
1119 static int psmouse_probe(struct psmouse *psmouse)
1121 struct ps2dev *ps2dev = &psmouse->ps2dev;
1122 unsigned char param[2];
1125 * First, we check if it's a mouse. It should send 0x00 or 0x03
1126 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1127 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
1128 * ID queries, probably due to a firmware bug.
1132 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1135 if (param[0] != 0x00 && param[0] != 0x03 &&
1136 param[0] != 0x04 && param[0] != 0xff)
1140 * Then we reset and disable the mouse so that it doesn't generate events.
1143 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
1144 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1145 ps2dev->serio->phys);
1151 * psmouse_initialize() initializes the mouse to a sane state.
1154 static void psmouse_initialize(struct psmouse *psmouse)
1157 * We set the mouse report rate, resolution and scaling.
1160 if (psmouse_max_proto != PSMOUSE_PS2) {
1161 psmouse->set_rate(psmouse, psmouse->rate);
1162 psmouse->set_resolution(psmouse, psmouse->resolution);
1163 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
1168 * psmouse_activate() enables the mouse so that we get motion reports from it.
1171 int psmouse_activate(struct psmouse *psmouse)
1173 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1174 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1175 psmouse->ps2dev.serio->phys);
1179 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1184 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
1185 * reports from it unless we explicitly request it.
1188 int psmouse_deactivate(struct psmouse *psmouse)
1190 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
1191 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1192 psmouse->ps2dev.serio->phys);
1196 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1202 * psmouse_resync() attempts to re-validate current protocol.
1205 static void psmouse_resync(struct work_struct *work)
1207 struct psmouse *parent = NULL, *psmouse =
1208 container_of(work, struct psmouse, resync_work.work);
1209 struct serio *serio = psmouse->ps2dev.serio;
1210 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1211 bool failed = false, enabled = false;
1214 mutex_lock(&psmouse_mutex);
1216 if (psmouse->state != PSMOUSE_RESYNCING)
1219 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1220 parent = serio_get_drvdata(serio->parent);
1221 psmouse_deactivate(parent);
1225 * Some mice don't ACK commands sent while they are in the middle of
1226 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1227 * instead of ps2_command() which would wait for 200ms for an ACK
1228 * that may never come.
1229 * As an additional quirk ALPS touchpads may not only forget to ACK
1230 * disable command but will stop reporting taps, so if we see that
1231 * mouse at least once ACKs disable we will do full reconnect if ACK
1234 psmouse->num_resyncs++;
1236 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1237 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1240 psmouse->acks_disable_command = true;
1243 * Poll the mouse. If it was reset the packet will be shorter than
1244 * psmouse->pktsize and ps2_command will fail. We do not expect and
1245 * do not handle scenario when mouse "upgrades" its protocol while
1246 * disconnected since it would require additional delay. If we ever
1247 * see a mouse that does it we'll adjust the code.
1250 if (psmouse->poll(psmouse))
1253 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1254 for (i = 0; i < psmouse->pktsize; i++) {
1256 rc = psmouse->protocol_handler(psmouse);
1257 if (rc != PSMOUSE_GOOD_DATA)
1260 if (rc != PSMOUSE_FULL_PACKET)
1262 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1266 * Now try to enable mouse. We try to do that even if poll failed and also
1267 * repeat our attempts 5 times, otherwise we may be left out with disabled
1270 for (i = 0; i < 5; i++) {
1271 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1279 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1280 psmouse->ps2dev.serio->phys);
1285 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1286 psmouse_info(psmouse,
1287 "resync failed, issuing reconnect request\n");
1288 serio_reconnect(serio);
1290 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1293 psmouse_activate(parent);
1295 mutex_unlock(&psmouse_mutex);
1299 * psmouse_cleanup() resets the mouse into power-on state.
1302 static void psmouse_cleanup(struct serio *serio)
1304 struct psmouse *psmouse = serio_get_drvdata(serio);
1305 struct psmouse *parent = NULL;
1307 mutex_lock(&psmouse_mutex);
1309 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1310 parent = serio_get_drvdata(serio->parent);
1311 psmouse_deactivate(parent);
1314 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1317 * Disable stream mode so cleanup routine can proceed undisturbed.
1319 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1320 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1321 psmouse->ps2dev.serio->phys);
1323 if (psmouse->cleanup)
1324 psmouse->cleanup(psmouse);
1327 * Reset the mouse to defaults (bare PS/2 protocol).
1329 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1332 * Some boxes, such as HP nx7400, get terribly confused if mouse
1333 * is not fully enabled before suspending/shutting down.
1335 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1338 if (parent->pt_deactivate)
1339 parent->pt_deactivate(parent);
1341 psmouse_activate(parent);
1344 mutex_unlock(&psmouse_mutex);
1348 * psmouse_disconnect() closes and frees.
1351 static void psmouse_disconnect(struct serio *serio)
1353 struct psmouse *psmouse, *parent = NULL;
1355 psmouse = serio_get_drvdata(serio);
1357 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1359 mutex_lock(&psmouse_mutex);
1361 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1363 /* make sure we don't have a resync in progress */
1364 mutex_unlock(&psmouse_mutex);
1365 flush_workqueue(kpsmoused_wq);
1366 mutex_lock(&psmouse_mutex);
1368 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1369 parent = serio_get_drvdata(serio->parent);
1370 psmouse_deactivate(parent);
1373 if (psmouse->disconnect)
1374 psmouse->disconnect(psmouse);
1376 if (parent && parent->pt_deactivate)
1377 parent->pt_deactivate(parent);
1379 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1382 serio_set_drvdata(serio, NULL);
1383 input_unregister_device(psmouse->dev);
1387 psmouse_activate(parent);
1389 mutex_unlock(&psmouse_mutex);
1392 static int psmouse_switch_protocol(struct psmouse *psmouse,
1393 const struct psmouse_protocol *proto)
1395 const struct psmouse_protocol *selected_proto;
1396 struct input_dev *input_dev = psmouse->dev;
1398 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1400 if (proto && (proto->detect || proto->init)) {
1401 psmouse_apply_defaults(psmouse);
1403 if (proto->detect && proto->detect(psmouse, true) < 0)
1406 if (proto->init && proto->init(psmouse) < 0)
1409 psmouse->type = proto->type;
1410 selected_proto = proto;
1412 psmouse->type = psmouse_extensions(psmouse,
1413 psmouse_max_proto, true);
1414 selected_proto = psmouse_protocol_by_type(psmouse->type);
1417 psmouse->ignore_parity = selected_proto->ignore_parity;
1420 * If mouse's packet size is 3 there is no point in polling the
1421 * device in hopes to detect protocol reset - we won't get less
1422 * than 3 bytes response anyhow.
1424 if (psmouse->pktsize == 3)
1425 psmouse->resync_time = 0;
1428 * Some smart KVMs fake response to POLL command returning just
1429 * 3 bytes and messing up our resync logic, so if initial poll
1430 * fails we won't try polling the device anymore. Hopefully
1431 * such KVM will maintain initially selected protocol.
1433 if (psmouse->resync_time && psmouse->poll(psmouse))
1434 psmouse->resync_time = 0;
1436 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1437 selected_proto->name, psmouse->vendor, psmouse->name);
1439 input_dev->name = psmouse->devname;
1440 input_dev->phys = psmouse->phys;
1441 input_dev->id.bustype = BUS_I8042;
1442 input_dev->id.vendor = 0x0002;
1443 input_dev->id.product = psmouse->type;
1444 input_dev->id.version = psmouse->model;
1450 * psmouse_connect() is a callback from the serio module when
1451 * an unhandled serio port is found.
1453 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1455 struct psmouse *psmouse, *parent = NULL;
1456 struct input_dev *input_dev;
1457 int retval = 0, error = -ENOMEM;
1459 mutex_lock(&psmouse_mutex);
1462 * If this is a pass-through port deactivate parent so the device
1463 * connected to this port can be successfully identified
1465 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1466 parent = serio_get_drvdata(serio->parent);
1467 psmouse_deactivate(parent);
1470 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1471 input_dev = input_allocate_device();
1472 if (!psmouse || !input_dev)
1475 ps2_init(&psmouse->ps2dev, serio);
1476 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1477 psmouse->dev = input_dev;
1478 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1480 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1482 serio_set_drvdata(serio, psmouse);
1484 error = serio_open(serio, drv);
1486 goto err_clear_drvdata;
1488 if (psmouse_probe(psmouse) < 0) {
1490 goto err_close_serio;
1493 psmouse->rate = psmouse_rate;
1494 psmouse->resolution = psmouse_resolution;
1495 psmouse->resetafter = psmouse_resetafter;
1496 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1497 psmouse->smartscroll = psmouse_smartscroll;
1499 psmouse_switch_protocol(psmouse, NULL);
1501 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1502 psmouse_initialize(psmouse);
1504 error = input_register_device(psmouse->dev);
1506 goto err_protocol_disconnect;
1508 if (parent && parent->pt_activate)
1509 parent->pt_activate(parent);
1511 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1513 goto err_pt_deactivate;
1515 psmouse_activate(psmouse);
1518 /* If this is a pass-through port the parent needs to be re-activated */
1520 psmouse_activate(parent);
1522 mutex_unlock(&psmouse_mutex);
1526 if (parent && parent->pt_deactivate)
1527 parent->pt_deactivate(parent);
1528 input_unregister_device(psmouse->dev);
1529 input_dev = NULL; /* so we don't try to free it below */
1530 err_protocol_disconnect:
1531 if (psmouse->disconnect)
1532 psmouse->disconnect(psmouse);
1533 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1537 serio_set_drvdata(serio, NULL);
1539 input_free_device(input_dev);
1547 static int psmouse_reconnect(struct serio *serio)
1549 struct psmouse *psmouse = serio_get_drvdata(serio);
1550 struct psmouse *parent = NULL;
1554 mutex_lock(&psmouse_mutex);
1556 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1557 parent = serio_get_drvdata(serio->parent);
1558 psmouse_deactivate(parent);
1561 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1563 if (psmouse->reconnect) {
1564 if (psmouse->reconnect(psmouse))
1567 psmouse_reset(psmouse);
1569 if (psmouse_probe(psmouse) < 0)
1572 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1573 if (psmouse->type != type)
1578 * OK, the device type (and capabilities) match the old one,
1579 * we can continue using it, complete initialization
1581 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1583 psmouse_initialize(psmouse);
1585 if (parent && parent->pt_activate)
1586 parent->pt_activate(parent);
1588 psmouse_activate(psmouse);
1592 /* If this is a pass-through port the parent waits to be activated */
1594 psmouse_activate(parent);
1596 mutex_unlock(&psmouse_mutex);
1600 static struct serio_device_id psmouse_serio_ids[] = {
1608 .type = SERIO_PS_PSTHRU,
1616 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1618 static struct serio_driver psmouse_drv = {
1622 .description = DRIVER_DESC,
1623 .id_table = psmouse_serio_ids,
1624 .interrupt = psmouse_interrupt,
1625 .connect = psmouse_connect,
1626 .reconnect = psmouse_reconnect,
1627 .disconnect = psmouse_disconnect,
1628 .cleanup = psmouse_cleanup,
1631 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1634 struct serio *serio = to_serio_port(dev);
1635 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1636 struct psmouse *psmouse;
1638 psmouse = serio_get_drvdata(serio);
1640 return attr->show(psmouse, attr->data, buf);
1643 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1644 const char *buf, size_t count)
1646 struct serio *serio = to_serio_port(dev);
1647 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1648 struct psmouse *psmouse, *parent = NULL;
1651 retval = mutex_lock_interruptible(&psmouse_mutex);
1655 psmouse = serio_get_drvdata(serio);
1657 if (attr->protect) {
1658 if (psmouse->state == PSMOUSE_IGNORE) {
1663 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1664 parent = serio_get_drvdata(serio->parent);
1665 psmouse_deactivate(parent);
1668 psmouse_deactivate(psmouse);
1671 retval = attr->set(psmouse, attr->data, buf, count);
1673 if (attr->protect) {
1674 if (retval != -ENODEV)
1675 psmouse_activate(psmouse);
1678 psmouse_activate(parent);
1682 mutex_unlock(&psmouse_mutex);
1687 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1689 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1691 return sprintf(buf, "%u\n", *field);
1694 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1696 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1700 err = kstrtouint(buf, 10, &value);
1709 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1711 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1714 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1716 struct serio *serio = psmouse->ps2dev.serio;
1717 struct psmouse *parent = NULL;
1718 struct input_dev *old_dev, *new_dev;
1719 const struct psmouse_protocol *proto, *old_proto;
1723 proto = psmouse_protocol_by_name(buf, count);
1727 if (psmouse->type == proto->type)
1730 new_dev = input_allocate_device();
1734 while (!list_empty(&serio->children)) {
1736 psmouse_warn(psmouse,
1737 "failed to destroy children ports, protocol change aborted.\n");
1738 input_free_device(new_dev);
1742 mutex_unlock(&psmouse_mutex);
1743 serio_unregister_child_port(serio);
1744 mutex_lock(&psmouse_mutex);
1746 if (serio->drv != &psmouse_drv) {
1747 input_free_device(new_dev);
1751 if (psmouse->type == proto->type) {
1752 input_free_device(new_dev);
1753 return count; /* switched by other thread */
1757 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1758 parent = serio_get_drvdata(serio->parent);
1759 if (parent->pt_deactivate)
1760 parent->pt_deactivate(parent);
1763 old_dev = psmouse->dev;
1764 old_proto = psmouse_protocol_by_type(psmouse->type);
1766 if (psmouse->disconnect)
1767 psmouse->disconnect(psmouse);
1769 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1771 psmouse->dev = new_dev;
1772 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1774 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1775 psmouse_reset(psmouse);
1776 /* default to PSMOUSE_PS2 */
1777 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1780 psmouse_initialize(psmouse);
1781 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1783 error = input_register_device(psmouse->dev);
1785 if (psmouse->disconnect)
1786 psmouse->disconnect(psmouse);
1788 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1789 input_free_device(new_dev);
1790 psmouse->dev = old_dev;
1791 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1792 psmouse_switch_protocol(psmouse, old_proto);
1793 psmouse_initialize(psmouse);
1794 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1799 input_unregister_device(old_dev);
1801 if (parent && parent->pt_activate)
1802 parent->pt_activate(parent);
1807 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1812 err = kstrtouint(buf, 10, &value);
1816 psmouse->set_rate(psmouse, value);
1820 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1825 err = kstrtouint(buf, 10, &value);
1829 psmouse->set_resolution(psmouse, value);
1834 static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
1836 const struct psmouse_protocol *proto;
1841 proto = psmouse_protocol_by_name(val, strlen(val));
1843 if (!proto || !proto->maxproto)
1846 *((unsigned int *)kp->arg) = proto->type;
1851 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
1853 int type = *((unsigned int *)kp->arg);
1855 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1858 static int __init psmouse_init(void)
1862 lifebook_module_init();
1863 synaptics_module_init();
1866 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1867 if (!kpsmoused_wq) {
1868 pr_err("failed to create kpsmoused workqueue\n");
1872 err = serio_register_driver(&psmouse_drv);
1874 destroy_workqueue(kpsmoused_wq);
1879 static void __exit psmouse_exit(void)
1881 serio_unregister_driver(&psmouse_drv);
1882 destroy_workqueue(kpsmoused_wq);
1885 module_init(psmouse_init);
1886 module_exit(psmouse_exit);