Linux 4.2.1
[linux/fpc-iii.git] / drivers / input / mouse / psmouse-base.c
blobec34770361501254e8e2c417315f74e3e00e5a97
1 /*
2 * PS/2 mouse driver
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
6 */
8 /*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
14 #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>
27 #include "psmouse.h"
28 #include "synaptics.h"
29 #include "logips2pp.h"
30 #include "alps.h"
31 #include "hgpk.h"
32 #include "lifebook.h"
33 #include "trackpoint.h"
34 #include "touchkit_ps2.h"
35 #include "elantech.h"
36 #include "sentelic.h"
37 #include "cypress_ps2.h"
38 #include "focaltech.h"
39 #include "vmmouse.h"
41 #define DRIVER_DESC "PS/2 mouse driver"
43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
44 MODULE_DESCRIPTION(DRIVER_DESC);
45 MODULE_LICENSE("GPL");
47 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
48 static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
49 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
50 static const struct kernel_param_ops param_ops_proto_abbrev = {
51 .set = psmouse_set_maxproto,
52 .get = psmouse_get_maxproto,
54 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
55 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
56 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
58 static unsigned int psmouse_resolution = 200;
59 module_param_named(resolution, psmouse_resolution, uint, 0644);
60 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
62 static unsigned int psmouse_rate = 100;
63 module_param_named(rate, psmouse_rate, uint, 0644);
64 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
66 static bool psmouse_smartscroll = true;
67 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
68 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
70 static unsigned int psmouse_resetafter = 5;
71 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
72 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
74 static unsigned int psmouse_resync_time;
75 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
76 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
78 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
79 NULL,
80 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
81 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, rate),
83 psmouse_show_int_attr, psmouse_attr_set_rate);
84 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
85 (void *) offsetof(struct psmouse, resolution),
86 psmouse_show_int_attr, psmouse_attr_set_resolution);
87 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
88 (void *) offsetof(struct psmouse, resetafter),
89 psmouse_show_int_attr, psmouse_set_int_attr);
90 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
91 (void *) offsetof(struct psmouse, resync_time),
92 psmouse_show_int_attr, psmouse_set_int_attr);
94 static struct attribute *psmouse_attributes[] = {
95 &psmouse_attr_protocol.dattr.attr,
96 &psmouse_attr_rate.dattr.attr,
97 &psmouse_attr_resolution.dattr.attr,
98 &psmouse_attr_resetafter.dattr.attr,
99 &psmouse_attr_resync_time.dattr.attr,
100 NULL
103 static struct attribute_group psmouse_attribute_group = {
104 .attrs = psmouse_attributes,
108 * psmouse_mutex protects all operations changing state of mouse
109 * (connecting, disconnecting, changing rate or resolution via
110 * sysfs). We could use a per-device semaphore but since there
111 * rarely more than one PS/2 mouse connected and since semaphore
112 * is taken in "slow" paths it is not worth it.
114 static DEFINE_MUTEX(psmouse_mutex);
116 static struct workqueue_struct *kpsmoused_wq;
118 struct psmouse_protocol {
119 enum psmouse_type type;
120 bool maxproto;
121 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
122 const char *name;
123 const char *alias;
124 int (*detect)(struct psmouse *, bool);
125 int (*init)(struct psmouse *);
129 * psmouse_process_byte() analyzes the PS/2 data stream and reports
130 * relevant events to the input module once full packet has arrived.
133 psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
135 struct input_dev *dev = psmouse->dev;
136 unsigned char *packet = psmouse->packet;
138 if (psmouse->pktcnt < psmouse->pktsize)
139 return PSMOUSE_GOOD_DATA;
142 * Full packet accumulated, process it
146 * Scroll wheel on IntelliMice, scroll buttons on NetMice
149 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
150 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
153 * Scroll wheel and buttons on IntelliMouse Explorer
156 if (psmouse->type == PSMOUSE_IMEX) {
157 switch (packet[3] & 0xC0) {
158 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
159 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
160 break;
161 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
162 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
163 break;
164 case 0x00:
165 case 0xC0:
166 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
167 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
168 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
169 break;
174 * Extra buttons on Genius NewNet 3D
177 if (psmouse->type == PSMOUSE_GENPS) {
178 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
179 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
183 * Extra button on ThinkingMouse
185 if (psmouse->type == PSMOUSE_THINKPS) {
186 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
187 /* Without this bit of weirdness moving up gives wildly high Y changes. */
188 packet[1] |= (packet[0] & 0x40) << 1;
192 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
193 * byte.
195 if (psmouse->type == PSMOUSE_CORTRON) {
196 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
197 packet[0] |= 0x08;
201 * Generic PS/2 Mouse
204 input_report_key(dev, BTN_LEFT, packet[0] & 1);
205 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
206 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
208 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
209 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
211 input_sync(dev);
213 return PSMOUSE_FULL_PACKET;
216 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
217 unsigned long delay)
219 queue_delayed_work(kpsmoused_wq, work, delay);
223 * __psmouse_set_state() sets new psmouse state and resets all flags.
226 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
228 psmouse->state = new_state;
229 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
230 psmouse->ps2dev.flags = 0;
231 psmouse->last = jiffies;
236 * psmouse_set_state() sets new psmouse state and resets all flags and
237 * counters while holding serio lock so fighting with interrupt handler
238 * is not a concern.
241 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
243 serio_pause_rx(psmouse->ps2dev.serio);
244 __psmouse_set_state(psmouse, new_state);
245 serio_continue_rx(psmouse->ps2dev.serio);
249 * psmouse_handle_byte() processes one byte of the input data stream
250 * by calling corresponding protocol handler.
253 static int psmouse_handle_byte(struct psmouse *psmouse)
255 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
257 switch (rc) {
258 case PSMOUSE_BAD_DATA:
259 if (psmouse->state == PSMOUSE_ACTIVATED) {
260 psmouse_warn(psmouse,
261 "%s at %s lost sync at byte %d\n",
262 psmouse->name, psmouse->phys,
263 psmouse->pktcnt);
264 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
265 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
266 psmouse_notice(psmouse,
267 "issuing reconnect request\n");
268 serio_reconnect(psmouse->ps2dev.serio);
269 return -1;
272 psmouse->pktcnt = 0;
273 break;
275 case PSMOUSE_FULL_PACKET:
276 psmouse->pktcnt = 0;
277 if (psmouse->out_of_sync_cnt) {
278 psmouse->out_of_sync_cnt = 0;
279 psmouse_notice(psmouse,
280 "%s at %s - driver resynced.\n",
281 psmouse->name, psmouse->phys);
283 break;
285 case PSMOUSE_GOOD_DATA:
286 break;
288 return 0;
292 * psmouse_interrupt() handles incoming characters, either passing them
293 * for normal processing or gathering them as command response.
296 static irqreturn_t psmouse_interrupt(struct serio *serio,
297 unsigned char data, unsigned int flags)
299 struct psmouse *psmouse = serio_get_drvdata(serio);
301 if (psmouse->state == PSMOUSE_IGNORE)
302 goto out;
304 if (unlikely((flags & SERIO_TIMEOUT) ||
305 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
307 if (psmouse->state == PSMOUSE_ACTIVATED)
308 psmouse_warn(psmouse,
309 "bad data from KBC -%s%s\n",
310 flags & SERIO_TIMEOUT ? " timeout" : "",
311 flags & SERIO_PARITY ? " bad parity" : "");
312 ps2_cmd_aborted(&psmouse->ps2dev);
313 goto out;
316 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
317 if (ps2_handle_ack(&psmouse->ps2dev, data))
318 goto out;
320 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
321 if (ps2_handle_response(&psmouse->ps2dev, data))
322 goto out;
324 if (psmouse->state <= PSMOUSE_RESYNCING)
325 goto out;
327 if (psmouse->state == PSMOUSE_ACTIVATED &&
328 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
329 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
330 psmouse->name, psmouse->phys, psmouse->pktcnt);
331 psmouse->badbyte = psmouse->packet[0];
332 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
333 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
334 goto out;
337 psmouse->packet[psmouse->pktcnt++] = data;
339 * Check if this is a new device announcement (0xAA 0x00)
341 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
342 if (psmouse->pktcnt == 1) {
343 psmouse->last = jiffies;
344 goto out;
347 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
348 (psmouse->type == PSMOUSE_HGPK &&
349 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
350 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
351 serio_reconnect(serio);
352 goto out;
355 * Not a new device, try processing first byte normally
357 psmouse->pktcnt = 1;
358 if (psmouse_handle_byte(psmouse))
359 goto out;
361 psmouse->packet[psmouse->pktcnt++] = data;
365 * See if we need to force resync because mouse was idle for too long
367 if (psmouse->state == PSMOUSE_ACTIVATED &&
368 psmouse->pktcnt == 1 && psmouse->resync_time &&
369 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
370 psmouse->badbyte = psmouse->packet[0];
371 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
372 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
373 goto out;
376 psmouse->last = jiffies;
377 psmouse_handle_byte(psmouse);
379 out:
380 return IRQ_HANDLED;
385 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
386 * using sliced syntax, understood by advanced devices, such as Logitech
387 * or Synaptics touchpads. The command is encoded as:
388 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
389 * is the command.
391 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
393 int i;
395 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
396 return -1;
398 for (i = 6; i >= 0; i -= 2) {
399 unsigned char d = (command >> i) & 3;
400 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
401 return -1;
404 return 0;
409 * psmouse_reset() resets the mouse into power-on state.
411 int psmouse_reset(struct psmouse *psmouse)
413 unsigned char param[2];
415 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
416 return -1;
418 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
419 return -1;
421 return 0;
425 * Here we set the mouse resolution.
428 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
430 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
431 unsigned char p;
433 if (resolution == 0 || resolution > 200)
434 resolution = 200;
436 p = params[resolution / 50];
437 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
438 psmouse->resolution = 25 << p;
442 * Here we set the mouse report rate.
445 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
447 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
448 unsigned char r;
449 int i = 0;
451 while (rates[i] > rate) i++;
452 r = rates[i];
453 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
454 psmouse->rate = r;
458 * Here we set the mouse scaling.
461 static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
463 ps2_command(&psmouse->ps2dev, NULL,
464 scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
465 PSMOUSE_CMD_SETSCALE11);
469 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
472 static int psmouse_poll(struct psmouse *psmouse)
474 return ps2_command(&psmouse->ps2dev, psmouse->packet,
475 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
478 static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
480 int i;
482 for (i = 0; ids[i]; i++)
483 if (!strcasecmp(id, ids[i]))
484 return true;
486 return false;
490 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
492 bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
494 struct serio *serio = psmouse->ps2dev.serio;
495 char *p, *fw_id_copy, *save_ptr;
496 bool found = false;
498 if (strncmp(serio->firmware_id, "PNP: ", 5))
499 return false;
501 fw_id_copy = kstrndup(&serio->firmware_id[5],
502 sizeof(serio->firmware_id) - 5,
503 GFP_KERNEL);
504 if (!fw_id_copy)
505 return false;
507 save_ptr = fw_id_copy;
508 while ((p = strsep(&fw_id_copy, " ")) != NULL) {
509 if (psmouse_check_pnp_id(p, ids)) {
510 found = true;
511 break;
515 kfree(save_ptr);
516 return found;
520 * Genius NetMouse magic init.
522 static int genius_detect(struct psmouse *psmouse, bool set_properties)
524 struct ps2dev *ps2dev = &psmouse->ps2dev;
525 unsigned char param[4];
527 param[0] = 3;
528 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
529 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
530 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
531 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
532 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
534 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
535 return -1;
537 if (set_properties) {
538 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
539 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
540 __set_bit(BTN_SIDE, psmouse->dev->keybit);
541 __set_bit(REL_WHEEL, psmouse->dev->relbit);
543 psmouse->vendor = "Genius";
544 psmouse->name = "Mouse";
545 psmouse->pktsize = 4;
548 return 0;
552 * IntelliMouse magic init.
554 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
556 struct ps2dev *ps2dev = &psmouse->ps2dev;
557 unsigned char param[2];
559 param[0] = 200;
560 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
561 param[0] = 100;
562 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
563 param[0] = 80;
564 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
565 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
567 if (param[0] != 3)
568 return -1;
570 if (set_properties) {
571 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
572 __set_bit(REL_WHEEL, psmouse->dev->relbit);
574 if (!psmouse->vendor)
575 psmouse->vendor = "Generic";
576 if (!psmouse->name)
577 psmouse->name = "Wheel Mouse";
578 psmouse->pktsize = 4;
581 return 0;
585 * Try IntelliMouse/Explorer magic init.
587 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
589 struct ps2dev *ps2dev = &psmouse->ps2dev;
590 unsigned char param[2];
592 intellimouse_detect(psmouse, 0);
594 param[0] = 200;
595 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
596 param[0] = 200;
597 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
598 param[0] = 80;
599 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
600 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
602 if (param[0] != 4)
603 return -1;
605 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
606 param[0] = 200;
607 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
608 param[0] = 80;
609 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
610 param[0] = 40;
611 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
613 if (set_properties) {
614 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
615 __set_bit(REL_WHEEL, psmouse->dev->relbit);
616 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
617 __set_bit(BTN_SIDE, psmouse->dev->keybit);
618 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
620 if (!psmouse->vendor)
621 psmouse->vendor = "Generic";
622 if (!psmouse->name)
623 psmouse->name = "Explorer Mouse";
624 psmouse->pktsize = 4;
627 return 0;
631 * Kensington ThinkingMouse / ExpertMouse magic init.
633 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
635 struct ps2dev *ps2dev = &psmouse->ps2dev;
636 unsigned char param[2];
637 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
638 int i;
640 param[0] = 10;
641 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
642 param[0] = 0;
643 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
644 for (i = 0; i < ARRAY_SIZE(seq); i++) {
645 param[0] = seq[i];
646 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
648 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
650 if (param[0] != 2)
651 return -1;
653 if (set_properties) {
654 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
655 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
657 psmouse->vendor = "Kensington";
658 psmouse->name = "ThinkingMouse";
661 return 0;
665 * Bare PS/2 protocol "detection". Always succeeds.
667 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
669 if (set_properties) {
670 if (!psmouse->vendor)
671 psmouse->vendor = "Generic";
672 if (!psmouse->name)
673 psmouse->name = "Mouse";
676 * We have no way of figuring true number of buttons so let's
677 * assume that the device has 3.
679 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
682 return 0;
686 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
687 * must be forced by sysfs protocol writing.
689 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
691 if (set_properties) {
692 psmouse->vendor = "Cortron";
693 psmouse->name = "PS/2 Trackball";
695 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
696 __set_bit(BTN_SIDE, psmouse->dev->keybit);
699 return 0;
703 * Apply default settings to the psmouse structure. Most of them will
704 * be overridden by individual protocol initialization routines.
707 static void psmouse_apply_defaults(struct psmouse *psmouse)
709 struct input_dev *input_dev = psmouse->dev;
711 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
712 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
713 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
714 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
715 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
717 __set_bit(EV_KEY, input_dev->evbit);
718 __set_bit(EV_REL, input_dev->evbit);
720 __set_bit(BTN_LEFT, input_dev->keybit);
721 __set_bit(BTN_RIGHT, input_dev->keybit);
723 __set_bit(REL_X, input_dev->relbit);
724 __set_bit(REL_Y, input_dev->relbit);
726 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
728 psmouse->set_rate = psmouse_set_rate;
729 psmouse->set_resolution = psmouse_set_resolution;
730 psmouse->set_scale = psmouse_set_scale;
731 psmouse->poll = psmouse_poll;
732 psmouse->protocol_handler = psmouse_process_byte;
733 psmouse->pktsize = 3;
734 psmouse->reconnect = NULL;
735 psmouse->disconnect = NULL;
736 psmouse->cleanup = NULL;
737 psmouse->pt_activate = NULL;
738 psmouse->pt_deactivate = NULL;
742 * Apply default settings to the psmouse structure and call specified
743 * protocol detection or initialization routine.
745 static int psmouse_do_detect(int (*detect)(struct psmouse *psmouse,
746 bool set_properties),
747 struct psmouse *psmouse, bool set_properties)
749 if (set_properties)
750 psmouse_apply_defaults(psmouse);
752 return detect(psmouse, set_properties);
756 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
757 * the mouse may have.
760 static int psmouse_extensions(struct psmouse *psmouse,
761 unsigned int max_proto, bool set_properties)
763 bool synaptics_hardware = false;
765 /* Always check for focaltech, this is safe as it uses pnp-id matching */
766 if (psmouse_do_detect(focaltech_detect, psmouse, set_properties) == 0) {
767 if (max_proto > PSMOUSE_IMEX) {
768 if (!set_properties || focaltech_init(psmouse) == 0) {
769 if (IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH))
770 return PSMOUSE_FOCALTECH;
772 * Note that we need to also restrict
773 * psmouse_max_proto so that psmouse_initialize()
774 * does not try to reset rate and resolution,
775 * because even that upsets the device.
777 psmouse_max_proto = PSMOUSE_PS2;
778 return PSMOUSE_PS2;
784 * We always check for lifebook because it does not disturb mouse
785 * (it only checks DMI information).
787 if (psmouse_do_detect(lifebook_detect, psmouse, set_properties) == 0) {
788 if (max_proto > PSMOUSE_IMEX) {
789 if (!set_properties || lifebook_init(psmouse) == 0)
790 return PSMOUSE_LIFEBOOK;
794 if (psmouse_do_detect(vmmouse_detect, psmouse, set_properties) == 0) {
795 if (max_proto > PSMOUSE_IMEX) {
796 if (!set_properties || vmmouse_init(psmouse) == 0)
797 return PSMOUSE_VMMOUSE;
802 * Try Kensington ThinkingMouse (we try first, because synaptics probe
803 * upsets the thinkingmouse).
806 if (max_proto > PSMOUSE_IMEX &&
807 psmouse_do_detect(thinking_detect, psmouse, set_properties) == 0) {
808 return PSMOUSE_THINKPS;
812 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
813 * support is disabled in config - we need to know if it is synaptics so we
814 * can reset it properly after probing for intellimouse.
816 if (max_proto > PSMOUSE_PS2 &&
817 psmouse_do_detect(synaptics_detect, psmouse, set_properties) == 0) {
818 synaptics_hardware = true;
820 if (max_proto > PSMOUSE_IMEX) {
822 * Try activating protocol, but check if support is enabled first, since
823 * we try detecting Synaptics even when protocol is disabled.
825 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) &&
826 (!set_properties || synaptics_init(psmouse) == 0)) {
827 return PSMOUSE_SYNAPTICS;
831 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
832 * Unfortunately Logitech/Genius probes confuse some firmware versions so
833 * we'll have to skip them.
835 max_proto = PSMOUSE_IMEX;
838 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
840 synaptics_reset(psmouse);
844 * Try Cypress Trackpad.
845 * Must try it before Finger Sensing Pad because Finger Sensing Pad probe
846 * upsets some modules of Cypress Trackpads.
848 if (max_proto > PSMOUSE_IMEX &&
849 cypress_detect(psmouse, set_properties) == 0) {
850 if (IS_ENABLED(CONFIG_MOUSE_PS2_CYPRESS)) {
851 if (cypress_init(psmouse) == 0)
852 return PSMOUSE_CYPRESS;
855 * Finger Sensing Pad probe upsets some modules of
856 * Cypress Trackpad, must avoid Finger Sensing Pad
857 * probe if Cypress Trackpad device detected.
859 return PSMOUSE_PS2;
862 max_proto = PSMOUSE_IMEX;
866 * Try ALPS TouchPad
868 if (max_proto > PSMOUSE_IMEX) {
869 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
870 if (psmouse_do_detect(alps_detect,
871 psmouse, set_properties) == 0) {
872 if (!set_properties || alps_init(psmouse) == 0)
873 return PSMOUSE_ALPS;
875 * Init failed, try basic relative protocols
877 max_proto = PSMOUSE_IMEX;
882 * Try OLPC HGPK touchpad.
884 if (max_proto > PSMOUSE_IMEX &&
885 psmouse_do_detect(hgpk_detect, psmouse, set_properties) == 0) {
886 if (!set_properties || hgpk_init(psmouse) == 0)
887 return PSMOUSE_HGPK;
889 * Init failed, try basic relative protocols
891 max_proto = PSMOUSE_IMEX;
895 * Try Elantech touchpad.
897 if (max_proto > PSMOUSE_IMEX &&
898 psmouse_do_detect(elantech_detect, psmouse, set_properties) == 0) {
899 if (!set_properties || elantech_init(psmouse) == 0)
900 return PSMOUSE_ELANTECH;
902 * Init failed, try basic relative protocols
904 max_proto = PSMOUSE_IMEX;
907 if (max_proto > PSMOUSE_IMEX) {
908 if (psmouse_do_detect(genius_detect,
909 psmouse, set_properties) == 0)
910 return PSMOUSE_GENPS;
912 if (psmouse_do_detect(ps2pp_init,
913 psmouse, set_properties) == 0)
914 return PSMOUSE_PS2PP;
916 if (psmouse_do_detect(trackpoint_detect,
917 psmouse, set_properties) == 0)
918 return PSMOUSE_TRACKPOINT;
920 if (psmouse_do_detect(touchkit_ps2_detect,
921 psmouse, set_properties) == 0)
922 return PSMOUSE_TOUCHKIT_PS2;
926 * Try Finger Sensing Pad. We do it here because its probe upsets
927 * Trackpoint devices (causing TP_READ_ID command to time out).
929 if (max_proto > PSMOUSE_IMEX) {
930 if (psmouse_do_detect(fsp_detect,
931 psmouse, set_properties) == 0) {
932 if (!set_properties || fsp_init(psmouse) == 0)
933 return PSMOUSE_FSP;
935 * Init failed, try basic relative protocols
937 max_proto = PSMOUSE_IMEX;
942 * Reset to defaults in case the device got confused by extended
943 * protocol probes. Note that we follow up with full reset because
944 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
946 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
947 psmouse_reset(psmouse);
949 if (max_proto >= PSMOUSE_IMEX &&
950 psmouse_do_detect(im_explorer_detect,
951 psmouse, set_properties) == 0) {
952 return PSMOUSE_IMEX;
955 if (max_proto >= PSMOUSE_IMPS &&
956 psmouse_do_detect(intellimouse_detect,
957 psmouse, set_properties) == 0) {
958 return PSMOUSE_IMPS;
962 * Okay, all failed, we have a standard mouse here. The number of the buttons
963 * is still a question, though. We assume 3.
965 psmouse_do_detect(ps2bare_detect, psmouse, set_properties);
967 if (synaptics_hardware) {
969 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
970 * We need to reset the touchpad because if there is a track point on the
971 * pass through port it could get disabled while probing for protocol
972 * extensions.
974 psmouse_reset(psmouse);
977 return PSMOUSE_PS2;
980 static const struct psmouse_protocol psmouse_protocols[] = {
982 .type = PSMOUSE_PS2,
983 .name = "PS/2",
984 .alias = "bare",
985 .maxproto = true,
986 .ignore_parity = true,
987 .detect = ps2bare_detect,
989 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
991 .type = PSMOUSE_PS2PP,
992 .name = "PS2++",
993 .alias = "logitech",
994 .detect = ps2pp_init,
996 #endif
998 .type = PSMOUSE_THINKPS,
999 .name = "ThinkPS/2",
1000 .alias = "thinkps",
1001 .detect = thinking_detect,
1003 #ifdef CONFIG_MOUSE_PS2_CYPRESS
1005 .type = PSMOUSE_CYPRESS,
1006 .name = "CyPS/2",
1007 .alias = "cypress",
1008 .detect = cypress_detect,
1009 .init = cypress_init,
1011 #endif
1013 .type = PSMOUSE_GENPS,
1014 .name = "GenPS/2",
1015 .alias = "genius",
1016 .detect = genius_detect,
1019 .type = PSMOUSE_IMPS,
1020 .name = "ImPS/2",
1021 .alias = "imps",
1022 .maxproto = true,
1023 .ignore_parity = true,
1024 .detect = intellimouse_detect,
1027 .type = PSMOUSE_IMEX,
1028 .name = "ImExPS/2",
1029 .alias = "exps",
1030 .maxproto = true,
1031 .ignore_parity = true,
1032 .detect = im_explorer_detect,
1034 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
1036 .type = PSMOUSE_SYNAPTICS,
1037 .name = "SynPS/2",
1038 .alias = "synaptics",
1039 .detect = synaptics_detect,
1040 .init = synaptics_init,
1043 .type = PSMOUSE_SYNAPTICS_RELATIVE,
1044 .name = "SynRelPS/2",
1045 .alias = "synaptics-relative",
1046 .detect = synaptics_detect,
1047 .init = synaptics_init_relative,
1049 #endif
1050 #ifdef CONFIG_MOUSE_PS2_ALPS
1052 .type = PSMOUSE_ALPS,
1053 .name = "AlpsPS/2",
1054 .alias = "alps",
1055 .detect = alps_detect,
1056 .init = alps_init,
1058 #endif
1059 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
1061 .type = PSMOUSE_LIFEBOOK,
1062 .name = "LBPS/2",
1063 .alias = "lifebook",
1064 .init = lifebook_init,
1066 #endif
1067 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
1069 .type = PSMOUSE_TRACKPOINT,
1070 .name = "TPPS/2",
1071 .alias = "trackpoint",
1072 .detect = trackpoint_detect,
1074 #endif
1075 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
1077 .type = PSMOUSE_TOUCHKIT_PS2,
1078 .name = "touchkitPS/2",
1079 .alias = "touchkit",
1080 .detect = touchkit_ps2_detect,
1082 #endif
1083 #ifdef CONFIG_MOUSE_PS2_OLPC
1085 .type = PSMOUSE_HGPK,
1086 .name = "OLPC HGPK",
1087 .alias = "hgpk",
1088 .detect = hgpk_detect,
1090 #endif
1091 #ifdef CONFIG_MOUSE_PS2_ELANTECH
1093 .type = PSMOUSE_ELANTECH,
1094 .name = "ETPS/2",
1095 .alias = "elantech",
1096 .detect = elantech_detect,
1097 .init = elantech_init,
1099 #endif
1100 #ifdef CONFIG_MOUSE_PS2_SENTELIC
1102 .type = PSMOUSE_FSP,
1103 .name = "FSPPS/2",
1104 .alias = "fsp",
1105 .detect = fsp_detect,
1106 .init = fsp_init,
1108 #endif
1110 .type = PSMOUSE_CORTRON,
1111 .name = "CortronPS/2",
1112 .alias = "cortps",
1113 .detect = cortron_detect,
1115 #ifdef CONFIG_MOUSE_PS2_FOCALTECH
1117 .type = PSMOUSE_FOCALTECH,
1118 .name = "FocalTechPS/2",
1119 .alias = "focaltech",
1120 .detect = focaltech_detect,
1121 .init = focaltech_init,
1123 #endif
1124 #ifdef CONFIG_MOUSE_PS2_VMMOUSE
1126 .type = PSMOUSE_VMMOUSE,
1127 .name = VMMOUSE_PSNAME,
1128 .alias = "vmmouse",
1129 .detect = vmmouse_detect,
1130 .init = vmmouse_init,
1132 #endif
1134 .type = PSMOUSE_AUTO,
1135 .name = "auto",
1136 .alias = "any",
1137 .maxproto = true,
1141 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
1143 int i;
1145 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
1146 if (psmouse_protocols[i].type == type)
1147 return &psmouse_protocols[i];
1149 WARN_ON(1);
1150 return &psmouse_protocols[0];
1153 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
1155 const struct psmouse_protocol *p;
1156 int i;
1158 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
1159 p = &psmouse_protocols[i];
1161 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
1162 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
1163 return &psmouse_protocols[i];
1166 return NULL;
1171 * psmouse_probe() probes for a PS/2 mouse.
1174 static int psmouse_probe(struct psmouse *psmouse)
1176 struct ps2dev *ps2dev = &psmouse->ps2dev;
1177 unsigned char param[2];
1180 * First, we check if it's a mouse. It should send 0x00 or 0x03
1181 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1182 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
1183 * ID queries, probably due to a firmware bug.
1186 param[0] = 0xa5;
1187 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1188 return -1;
1190 if (param[0] != 0x00 && param[0] != 0x03 &&
1191 param[0] != 0x04 && param[0] != 0xff)
1192 return -1;
1195 * Then we reset and disable the mouse so that it doesn't generate events.
1198 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
1199 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1200 ps2dev->serio->phys);
1202 return 0;
1206 * psmouse_initialize() initializes the mouse to a sane state.
1209 static void psmouse_initialize(struct psmouse *psmouse)
1212 * 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.
1226 int psmouse_activate(struct psmouse *psmouse)
1228 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1229 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1230 psmouse->ps2dev.serio->phys);
1231 return -1;
1234 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1235 return 0;
1239 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
1240 * reports from it unless we explicitly request it.
1243 int psmouse_deactivate(struct psmouse *psmouse)
1245 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
1246 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1247 psmouse->ps2dev.serio->phys);
1248 return -1;
1251 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1252 return 0;
1257 * psmouse_resync() attempts to re-validate current protocol.
1260 static void psmouse_resync(struct work_struct *work)
1262 struct psmouse *parent = NULL, *psmouse =
1263 container_of(work, struct psmouse, resync_work.work);
1264 struct serio *serio = psmouse->ps2dev.serio;
1265 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1266 bool failed = false, enabled = false;
1267 int i;
1269 mutex_lock(&psmouse_mutex);
1271 if (psmouse->state != PSMOUSE_RESYNCING)
1272 goto out;
1274 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1275 parent = serio_get_drvdata(serio->parent);
1276 psmouse_deactivate(parent);
1280 * Some mice don't ACK commands sent while they are in the middle of
1281 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1282 * instead of ps2_command() which would wait for 200ms for an ACK
1283 * that may never come.
1284 * As an additional quirk ALPS touchpads may not only forget to ACK
1285 * disable command but will stop reporting taps, so if we see that
1286 * mouse at least once ACKs disable we will do full reconnect if ACK
1287 * is missing.
1289 psmouse->num_resyncs++;
1291 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1292 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1293 failed = true;
1294 } else
1295 psmouse->acks_disable_command = true;
1298 * Poll the mouse. If it was reset the packet will be shorter than
1299 * psmouse->pktsize and ps2_command will fail. We do not expect and
1300 * do not handle scenario when mouse "upgrades" its protocol while
1301 * disconnected since it would require additional delay. If we ever
1302 * see a mouse that does it we'll adjust the code.
1304 if (!failed) {
1305 if (psmouse->poll(psmouse))
1306 failed = true;
1307 else {
1308 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1309 for (i = 0; i < psmouse->pktsize; i++) {
1310 psmouse->pktcnt++;
1311 rc = psmouse->protocol_handler(psmouse);
1312 if (rc != PSMOUSE_GOOD_DATA)
1313 break;
1315 if (rc != PSMOUSE_FULL_PACKET)
1316 failed = true;
1317 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1321 * Now try to enable mouse. We try to do that even if poll failed and also
1322 * repeat our attempts 5 times, otherwise we may be left out with disabled
1323 * mouse.
1325 for (i = 0; i < 5; i++) {
1326 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1327 enabled = true;
1328 break;
1330 msleep(200);
1333 if (!enabled) {
1334 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1335 psmouse->ps2dev.serio->phys);
1336 failed = true;
1339 if (failed) {
1340 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1341 psmouse_info(psmouse,
1342 "resync failed, issuing reconnect request\n");
1343 serio_reconnect(serio);
1344 } else
1345 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1347 if (parent)
1348 psmouse_activate(parent);
1349 out:
1350 mutex_unlock(&psmouse_mutex);
1354 * psmouse_cleanup() resets the mouse into power-on state.
1357 static void psmouse_cleanup(struct serio *serio)
1359 struct psmouse *psmouse = serio_get_drvdata(serio);
1360 struct psmouse *parent = NULL;
1362 mutex_lock(&psmouse_mutex);
1364 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1365 parent = serio_get_drvdata(serio->parent);
1366 psmouse_deactivate(parent);
1369 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1372 * Disable stream mode so cleanup routine can proceed undisturbed.
1374 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1375 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1376 psmouse->ps2dev.serio->phys);
1378 if (psmouse->cleanup)
1379 psmouse->cleanup(psmouse);
1382 * Reset the mouse to defaults (bare PS/2 protocol).
1384 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1387 * Some boxes, such as HP nx7400, get terribly confused if mouse
1388 * is not fully enabled before suspending/shutting down.
1390 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1392 if (parent) {
1393 if (parent->pt_deactivate)
1394 parent->pt_deactivate(parent);
1396 psmouse_activate(parent);
1399 mutex_unlock(&psmouse_mutex);
1403 * psmouse_disconnect() closes and frees.
1406 static void psmouse_disconnect(struct serio *serio)
1408 struct psmouse *psmouse, *parent = NULL;
1410 psmouse = serio_get_drvdata(serio);
1412 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1414 mutex_lock(&psmouse_mutex);
1416 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1418 /* make sure we don't have a resync in progress */
1419 mutex_unlock(&psmouse_mutex);
1420 flush_workqueue(kpsmoused_wq);
1421 mutex_lock(&psmouse_mutex);
1423 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1424 parent = serio_get_drvdata(serio->parent);
1425 psmouse_deactivate(parent);
1428 if (psmouse->disconnect)
1429 psmouse->disconnect(psmouse);
1431 if (parent && parent->pt_deactivate)
1432 parent->pt_deactivate(parent);
1434 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1436 serio_close(serio);
1437 serio_set_drvdata(serio, NULL);
1438 input_unregister_device(psmouse->dev);
1439 kfree(psmouse);
1441 if (parent)
1442 psmouse_activate(parent);
1444 mutex_unlock(&psmouse_mutex);
1447 static int psmouse_switch_protocol(struct psmouse *psmouse,
1448 const struct psmouse_protocol *proto)
1450 const struct psmouse_protocol *selected_proto;
1451 struct input_dev *input_dev = psmouse->dev;
1453 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1455 if (proto && (proto->detect || proto->init)) {
1456 psmouse_apply_defaults(psmouse);
1458 if (proto->detect && proto->detect(psmouse, true) < 0)
1459 return -1;
1461 if (proto->init && proto->init(psmouse) < 0)
1462 return -1;
1464 psmouse->type = proto->type;
1465 selected_proto = proto;
1466 } else {
1467 psmouse->type = psmouse_extensions(psmouse,
1468 psmouse_max_proto, true);
1469 selected_proto = psmouse_protocol_by_type(psmouse->type);
1472 psmouse->ignore_parity = selected_proto->ignore_parity;
1475 * If mouse's packet size is 3 there is no point in polling the
1476 * device in hopes to detect protocol reset - we won't get less
1477 * than 3 bytes response anyhow.
1479 if (psmouse->pktsize == 3)
1480 psmouse->resync_time = 0;
1483 * Some smart KVMs fake response to POLL command returning just
1484 * 3 bytes and messing up our resync logic, so if initial poll
1485 * fails we won't try polling the device anymore. Hopefully
1486 * such KVM will maintain initially selected protocol.
1488 if (psmouse->resync_time && psmouse->poll(psmouse))
1489 psmouse->resync_time = 0;
1491 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1492 selected_proto->name, psmouse->vendor, psmouse->name);
1494 input_dev->name = psmouse->devname;
1495 input_dev->phys = psmouse->phys;
1496 input_dev->id.bustype = BUS_I8042;
1497 input_dev->id.vendor = 0x0002;
1498 input_dev->id.product = psmouse->type;
1499 input_dev->id.version = psmouse->model;
1501 return 0;
1505 * psmouse_connect() is a callback from the serio module when
1506 * an unhandled serio port is found.
1508 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1510 struct psmouse *psmouse, *parent = NULL;
1511 struct input_dev *input_dev;
1512 int retval = 0, error = -ENOMEM;
1514 mutex_lock(&psmouse_mutex);
1517 * If this is a pass-through port deactivate parent so the device
1518 * connected to this port can be successfully identified
1520 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1521 parent = serio_get_drvdata(serio->parent);
1522 psmouse_deactivate(parent);
1525 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1526 input_dev = input_allocate_device();
1527 if (!psmouse || !input_dev)
1528 goto err_free;
1530 ps2_init(&psmouse->ps2dev, serio);
1531 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1532 psmouse->dev = input_dev;
1533 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1535 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1537 serio_set_drvdata(serio, psmouse);
1539 error = serio_open(serio, drv);
1540 if (error)
1541 goto err_clear_drvdata;
1543 if (psmouse_probe(psmouse) < 0) {
1544 error = -ENODEV;
1545 goto err_close_serio;
1548 psmouse->rate = psmouse_rate;
1549 psmouse->resolution = psmouse_resolution;
1550 psmouse->resetafter = psmouse_resetafter;
1551 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1552 psmouse->smartscroll = psmouse_smartscroll;
1554 psmouse_switch_protocol(psmouse, NULL);
1556 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1557 psmouse_initialize(psmouse);
1559 error = input_register_device(psmouse->dev);
1560 if (error)
1561 goto err_protocol_disconnect;
1563 if (parent && parent->pt_activate)
1564 parent->pt_activate(parent);
1566 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1567 if (error)
1568 goto err_pt_deactivate;
1570 psmouse_activate(psmouse);
1572 out:
1573 /* If this is a pass-through port the parent needs to be re-activated */
1574 if (parent)
1575 psmouse_activate(parent);
1577 mutex_unlock(&psmouse_mutex);
1578 return retval;
1580 err_pt_deactivate:
1581 if (parent && parent->pt_deactivate)
1582 parent->pt_deactivate(parent);
1583 input_unregister_device(psmouse->dev);
1584 input_dev = NULL; /* so we don't try to free it below */
1585 err_protocol_disconnect:
1586 if (psmouse->disconnect)
1587 psmouse->disconnect(psmouse);
1588 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1589 err_close_serio:
1590 serio_close(serio);
1591 err_clear_drvdata:
1592 serio_set_drvdata(serio, NULL);
1593 err_free:
1594 input_free_device(input_dev);
1595 kfree(psmouse);
1597 retval = error;
1598 goto out;
1602 static int psmouse_reconnect(struct serio *serio)
1604 struct psmouse *psmouse = serio_get_drvdata(serio);
1605 struct psmouse *parent = NULL;
1606 unsigned char type;
1607 int rc = -1;
1609 mutex_lock(&psmouse_mutex);
1611 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1612 parent = serio_get_drvdata(serio->parent);
1613 psmouse_deactivate(parent);
1616 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1618 if (psmouse->reconnect) {
1619 if (psmouse->reconnect(psmouse))
1620 goto out;
1621 } else {
1622 psmouse_reset(psmouse);
1624 if (psmouse_probe(psmouse) < 0)
1625 goto out;
1627 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1628 if (psmouse->type != type)
1629 goto out;
1633 * OK, the device type (and capabilities) match the old one,
1634 * we can continue using it, complete initialization
1636 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1638 psmouse_initialize(psmouse);
1640 if (parent && parent->pt_activate)
1641 parent->pt_activate(parent);
1643 psmouse_activate(psmouse);
1644 rc = 0;
1646 out:
1647 /* If this is a pass-through port the parent waits to be activated */
1648 if (parent)
1649 psmouse_activate(parent);
1651 mutex_unlock(&psmouse_mutex);
1652 return rc;
1655 static struct serio_device_id psmouse_serio_ids[] = {
1657 .type = SERIO_8042,
1658 .proto = SERIO_ANY,
1659 .id = SERIO_ANY,
1660 .extra = SERIO_ANY,
1663 .type = SERIO_PS_PSTHRU,
1664 .proto = SERIO_ANY,
1665 .id = SERIO_ANY,
1666 .extra = SERIO_ANY,
1668 { 0 }
1671 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1673 static struct serio_driver psmouse_drv = {
1674 .driver = {
1675 .name = "psmouse",
1677 .description = DRIVER_DESC,
1678 .id_table = psmouse_serio_ids,
1679 .interrupt = psmouse_interrupt,
1680 .connect = psmouse_connect,
1681 .reconnect = psmouse_reconnect,
1682 .disconnect = psmouse_disconnect,
1683 .cleanup = psmouse_cleanup,
1686 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1687 char *buf)
1689 struct serio *serio = to_serio_port(dev);
1690 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1691 struct psmouse *psmouse;
1693 psmouse = serio_get_drvdata(serio);
1695 return attr->show(psmouse, attr->data, buf);
1698 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1699 const char *buf, size_t count)
1701 struct serio *serio = to_serio_port(dev);
1702 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1703 struct psmouse *psmouse, *parent = NULL;
1704 int retval;
1706 retval = mutex_lock_interruptible(&psmouse_mutex);
1707 if (retval)
1708 goto out;
1710 psmouse = serio_get_drvdata(serio);
1712 if (attr->protect) {
1713 if (psmouse->state == PSMOUSE_IGNORE) {
1714 retval = -ENODEV;
1715 goto out_unlock;
1718 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1719 parent = serio_get_drvdata(serio->parent);
1720 psmouse_deactivate(parent);
1723 psmouse_deactivate(psmouse);
1726 retval = attr->set(psmouse, attr->data, buf, count);
1728 if (attr->protect) {
1729 if (retval != -ENODEV)
1730 psmouse_activate(psmouse);
1732 if (parent)
1733 psmouse_activate(parent);
1736 out_unlock:
1737 mutex_unlock(&psmouse_mutex);
1738 out:
1739 return retval;
1742 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1744 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1746 return sprintf(buf, "%u\n", *field);
1749 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1751 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1752 unsigned int value;
1753 int err;
1755 err = kstrtouint(buf, 10, &value);
1756 if (err)
1757 return err;
1759 *field = value;
1761 return count;
1764 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1766 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1769 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1771 struct serio *serio = psmouse->ps2dev.serio;
1772 struct psmouse *parent = NULL;
1773 struct input_dev *old_dev, *new_dev;
1774 const struct psmouse_protocol *proto, *old_proto;
1775 int error;
1776 int retry = 0;
1778 proto = psmouse_protocol_by_name(buf, count);
1779 if (!proto)
1780 return -EINVAL;
1782 if (psmouse->type == proto->type)
1783 return count;
1785 new_dev = input_allocate_device();
1786 if (!new_dev)
1787 return -ENOMEM;
1789 while (!list_empty(&serio->children)) {
1790 if (++retry > 3) {
1791 psmouse_warn(psmouse,
1792 "failed to destroy children ports, protocol change aborted.\n");
1793 input_free_device(new_dev);
1794 return -EIO;
1797 mutex_unlock(&psmouse_mutex);
1798 serio_unregister_child_port(serio);
1799 mutex_lock(&psmouse_mutex);
1801 if (serio->drv != &psmouse_drv) {
1802 input_free_device(new_dev);
1803 return -ENODEV;
1806 if (psmouse->type == proto->type) {
1807 input_free_device(new_dev);
1808 return count; /* switched by other thread */
1812 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1813 parent = serio_get_drvdata(serio->parent);
1814 if (parent->pt_deactivate)
1815 parent->pt_deactivate(parent);
1818 old_dev = psmouse->dev;
1819 old_proto = psmouse_protocol_by_type(psmouse->type);
1821 if (psmouse->disconnect)
1822 psmouse->disconnect(psmouse);
1824 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1826 psmouse->dev = new_dev;
1827 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1829 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1830 psmouse_reset(psmouse);
1831 /* default to PSMOUSE_PS2 */
1832 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1835 psmouse_initialize(psmouse);
1836 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1838 error = input_register_device(psmouse->dev);
1839 if (error) {
1840 if (psmouse->disconnect)
1841 psmouse->disconnect(psmouse);
1843 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1844 input_free_device(new_dev);
1845 psmouse->dev = old_dev;
1846 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1847 psmouse_switch_protocol(psmouse, old_proto);
1848 psmouse_initialize(psmouse);
1849 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1851 return error;
1854 input_unregister_device(old_dev);
1856 if (parent && parent->pt_activate)
1857 parent->pt_activate(parent);
1859 return count;
1862 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1864 unsigned int value;
1865 int err;
1867 err = kstrtouint(buf, 10, &value);
1868 if (err)
1869 return err;
1871 psmouse->set_rate(psmouse, value);
1872 return count;
1875 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1877 unsigned int value;
1878 int err;
1880 err = kstrtouint(buf, 10, &value);
1881 if (err)
1882 return err;
1884 psmouse->set_resolution(psmouse, value);
1885 return count;
1889 static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
1891 const struct psmouse_protocol *proto;
1893 if (!val)
1894 return -EINVAL;
1896 proto = psmouse_protocol_by_name(val, strlen(val));
1898 if (!proto || !proto->maxproto)
1899 return -EINVAL;
1901 *((unsigned int *)kp->arg) = proto->type;
1903 return 0;
1906 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
1908 int type = *((unsigned int *)kp->arg);
1910 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1913 static int __init psmouse_init(void)
1915 int err;
1917 lifebook_module_init();
1918 synaptics_module_init();
1919 hgpk_module_init();
1921 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1922 if (!kpsmoused_wq) {
1923 pr_err("failed to create kpsmoused workqueue\n");
1924 return -ENOMEM;
1927 err = serio_register_driver(&psmouse_drv);
1928 if (err)
1929 destroy_workqueue(kpsmoused_wq);
1931 return err;
1934 static void __exit psmouse_exit(void)
1936 serio_unregister_driver(&psmouse_drv);
1937 destroy_workqueue(kpsmoused_wq);
1940 module_init(psmouse_init);
1941 module_exit(psmouse_exit);