dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / input / mouse / psmouse-base.c
blobd3ff1fc09af712700507d05ac3548703e49173a1
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/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/input.h>
23 #include <linux/serio.h>
24 #include <linux/init.h>
25 #include <linux/libps2.h>
26 #include <linux/mutex.h>
27 #include <linux/types.h>
29 #include "psmouse.h"
30 #include "synaptics.h"
31 #include "logips2pp.h"
32 #include "alps.h"
33 #include "hgpk.h"
34 #include "lifebook.h"
35 #include "trackpoint.h"
36 #include "touchkit_ps2.h"
37 #include "elantech.h"
38 #include "sentelic.h"
39 #include "cypress_ps2.h"
40 #include "focaltech.h"
41 #include "vmmouse.h"
42 #include "byd.h"
44 #define DRIVER_DESC "PS/2 mouse driver"
46 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
47 MODULE_DESCRIPTION(DRIVER_DESC);
48 MODULE_LICENSE("GPL");
50 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
51 static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
52 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
53 static const struct kernel_param_ops param_ops_proto_abbrev = {
54 .set = psmouse_set_maxproto,
55 .get = psmouse_get_maxproto,
57 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
58 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
59 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
61 static unsigned int psmouse_resolution = 200;
62 module_param_named(resolution, psmouse_resolution, uint, 0644);
63 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
65 static unsigned int psmouse_rate = 100;
66 module_param_named(rate, psmouse_rate, uint, 0644);
67 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
69 static bool psmouse_smartscroll = true;
70 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
71 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
73 static bool psmouse_a4tech_2wheels;
74 module_param_named(a4tech_workaround, psmouse_a4tech_2wheels, bool, 0644);
75 MODULE_PARM_DESC(a4tech_workaround, "A4Tech second scroll wheel workaround, 1 = enabled, 0 = disabled (default).");
77 static unsigned int psmouse_resetafter = 5;
78 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
79 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
81 static unsigned int psmouse_resync_time;
82 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
83 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
85 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
86 NULL,
87 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
88 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
89 (void *) offsetof(struct psmouse, rate),
90 psmouse_show_int_attr, psmouse_attr_set_rate);
91 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
92 (void *) offsetof(struct psmouse, resolution),
93 psmouse_show_int_attr, psmouse_attr_set_resolution);
94 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
95 (void *) offsetof(struct psmouse, resetafter),
96 psmouse_show_int_attr, psmouse_set_int_attr);
97 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
98 (void *) offsetof(struct psmouse, resync_time),
99 psmouse_show_int_attr, psmouse_set_int_attr);
101 static struct attribute *psmouse_attributes[] = {
102 &psmouse_attr_protocol.dattr.attr,
103 &psmouse_attr_rate.dattr.attr,
104 &psmouse_attr_resolution.dattr.attr,
105 &psmouse_attr_resetafter.dattr.attr,
106 &psmouse_attr_resync_time.dattr.attr,
107 NULL
110 static const struct attribute_group psmouse_attribute_group = {
111 .attrs = psmouse_attributes,
115 * psmouse_mutex protects all operations changing state of mouse
116 * (connecting, disconnecting, changing rate or resolution via
117 * sysfs). We could use a per-device semaphore but since there
118 * rarely more than one PS/2 mouse connected and since semaphore
119 * is taken in "slow" paths it is not worth it.
121 static DEFINE_MUTEX(psmouse_mutex);
123 static struct workqueue_struct *kpsmoused_wq;
125 void psmouse_report_standard_buttons(struct input_dev *dev, u8 buttons)
127 input_report_key(dev, BTN_LEFT, buttons & BIT(0));
128 input_report_key(dev, BTN_MIDDLE, buttons & BIT(2));
129 input_report_key(dev, BTN_RIGHT, buttons & BIT(1));
132 void psmouse_report_standard_motion(struct input_dev *dev, u8 *packet)
134 int x, y;
136 x = packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0;
137 y = packet[2] ? packet[2] - ((packet[0] << 3) & 0x100) : 0;
139 input_report_rel(dev, REL_X, x);
140 input_report_rel(dev, REL_Y, -y);
143 void psmouse_report_standard_packet(struct input_dev *dev, u8 *packet)
145 psmouse_report_standard_buttons(dev, packet[0]);
146 psmouse_report_standard_motion(dev, packet);
150 * psmouse_process_byte() analyzes the PS/2 data stream and reports
151 * relevant events to the input module once full packet has arrived.
153 psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
155 struct input_dev *dev = psmouse->dev;
156 u8 *packet = psmouse->packet;
157 int wheel;
159 if (psmouse->pktcnt < psmouse->pktsize)
160 return PSMOUSE_GOOD_DATA;
162 /* Full packet accumulated, process it */
164 switch (psmouse->protocol->type) {
165 case PSMOUSE_IMPS:
166 /* IntelliMouse has scroll wheel */
167 input_report_rel(dev, REL_WHEEL, -(s8) packet[3]);
168 break;
170 case PSMOUSE_IMEX:
171 /* Scroll wheel and buttons on IntelliMouse Explorer */
172 switch (packet[3] & 0xC0) {
173 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
174 input_report_rel(dev, REL_WHEEL,
175 -sign_extend32(packet[3], 5));
176 break;
177 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
178 input_report_rel(dev, REL_HWHEEL,
179 -sign_extend32(packet[3], 5));
180 break;
181 case 0x00:
182 case 0xC0:
183 wheel = sign_extend32(packet[3], 3);
186 * Some A4Tech mice have two scroll wheels, with first
187 * one reporting +/-1 in the lower nibble, and second
188 * one reporting +/-2.
190 if (psmouse_a4tech_2wheels && abs(wheel) > 1)
191 input_report_rel(dev, REL_HWHEEL, wheel / 2);
192 else
193 input_report_rel(dev, REL_WHEEL, -wheel);
195 input_report_key(dev, BTN_SIDE, packet[3] & BIT(4));
196 input_report_key(dev, BTN_EXTRA, packet[3] & BIT(5));
197 break;
199 break;
201 case PSMOUSE_GENPS:
202 /* Report scroll buttons on NetMice */
203 input_report_rel(dev, REL_WHEEL, -(s8) packet[3]);
205 /* Extra buttons on Genius NewNet 3D */
206 input_report_key(dev, BTN_SIDE, packet[0] & BIT(6));
207 input_report_key(dev, BTN_EXTRA, packet[0] & BIT(7));
208 break;
210 case PSMOUSE_THINKPS:
211 /* Extra button on ThinkingMouse */
212 input_report_key(dev, BTN_EXTRA, packet[0] & BIT(3));
215 * Without this bit of weirdness moving up gives wildly
216 * high Y changes.
218 packet[1] |= (packet[0] & 0x40) << 1;
219 break;
221 case PSMOUSE_CORTRON:
223 * Cortron PS2 Trackball reports SIDE button in the
224 * 4th bit of the first byte.
226 input_report_key(dev, BTN_SIDE, packet[0] & BIT(3));
227 packet[0] |= BIT(3);
228 break;
230 default:
231 break;
234 /* Generic PS/2 Mouse */
235 packet[0] |= psmouse->extra_buttons;
236 psmouse_report_standard_packet(dev, packet);
238 input_sync(dev);
240 return PSMOUSE_FULL_PACKET;
243 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
244 unsigned long delay)
246 queue_delayed_work(kpsmoused_wq, work, delay);
250 * __psmouse_set_state() sets new psmouse state and resets all flags.
252 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
254 psmouse->state = new_state;
255 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
256 psmouse->ps2dev.flags = 0;
257 psmouse->last = jiffies;
261 * psmouse_set_state() sets new psmouse state and resets all flags and
262 * counters while holding serio lock so fighting with interrupt handler
263 * is not a concern.
265 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
267 serio_pause_rx(psmouse->ps2dev.serio);
268 __psmouse_set_state(psmouse, new_state);
269 serio_continue_rx(psmouse->ps2dev.serio);
273 * psmouse_handle_byte() processes one byte of the input data stream
274 * by calling corresponding protocol handler.
276 static int psmouse_handle_byte(struct psmouse *psmouse)
278 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
280 switch (rc) {
281 case PSMOUSE_BAD_DATA:
282 if (psmouse->state == PSMOUSE_ACTIVATED) {
283 psmouse_warn(psmouse,
284 "%s at %s lost sync at byte %d\n",
285 psmouse->name, psmouse->phys,
286 psmouse->pktcnt);
287 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
288 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
289 psmouse_notice(psmouse,
290 "issuing reconnect request\n");
291 serio_reconnect(psmouse->ps2dev.serio);
292 return -EIO;
295 psmouse->pktcnt = 0;
296 break;
298 case PSMOUSE_FULL_PACKET:
299 psmouse->pktcnt = 0;
300 if (psmouse->out_of_sync_cnt) {
301 psmouse->out_of_sync_cnt = 0;
302 psmouse_notice(psmouse,
303 "%s at %s - driver resynced.\n",
304 psmouse->name, psmouse->phys);
306 break;
308 case PSMOUSE_GOOD_DATA:
309 break;
311 return 0;
314 static void psmouse_handle_oob_data(struct psmouse *psmouse, u8 data)
316 switch (psmouse->oob_data_type) {
317 case PSMOUSE_OOB_NONE:
318 psmouse->oob_data_type = data;
319 break;
321 case PSMOUSE_OOB_EXTRA_BTNS:
322 psmouse_report_standard_buttons(psmouse->dev, data);
323 input_sync(psmouse->dev);
325 psmouse->extra_buttons = data;
326 psmouse->oob_data_type = PSMOUSE_OOB_NONE;
327 break;
329 default:
330 psmouse_warn(psmouse,
331 "unknown OOB_DATA type: 0x%02x\n",
332 psmouse->oob_data_type);
333 psmouse->oob_data_type = PSMOUSE_OOB_NONE;
334 break;
339 * psmouse_interrupt() handles incoming characters, either passing them
340 * for normal processing or gathering them as command response.
342 static irqreturn_t psmouse_interrupt(struct serio *serio,
343 u8 data, unsigned int flags)
345 struct psmouse *psmouse = serio_get_drvdata(serio);
347 if (psmouse->state == PSMOUSE_IGNORE)
348 goto out;
350 if (unlikely((flags & SERIO_TIMEOUT) ||
351 ((flags & SERIO_PARITY) &&
352 !psmouse->protocol->ignore_parity))) {
354 if (psmouse->state == PSMOUSE_ACTIVATED)
355 psmouse_warn(psmouse,
356 "bad data from KBC -%s%s\n",
357 flags & SERIO_TIMEOUT ? " timeout" : "",
358 flags & SERIO_PARITY ? " bad parity" : "");
359 ps2_cmd_aborted(&psmouse->ps2dev);
360 goto out;
363 if (flags & SERIO_OOB_DATA) {
364 psmouse_handle_oob_data(psmouse, data);
365 goto out;
368 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
369 if (ps2_handle_ack(&psmouse->ps2dev, data))
370 goto out;
372 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
373 if (ps2_handle_response(&psmouse->ps2dev, data))
374 goto out;
376 if (psmouse->state <= PSMOUSE_RESYNCING)
377 goto out;
379 if (psmouse->state == PSMOUSE_ACTIVATED &&
380 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
381 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
382 psmouse->name, psmouse->phys, psmouse->pktcnt);
383 psmouse->badbyte = psmouse->packet[0];
384 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
385 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
386 goto out;
389 psmouse->packet[psmouse->pktcnt++] = data;
391 /* Check if this is a new device announcement (0xAA 0x00) */
392 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
393 if (psmouse->pktcnt == 1) {
394 psmouse->last = jiffies;
395 goto out;
398 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
399 (psmouse->protocol->type == PSMOUSE_HGPK &&
400 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
401 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
402 serio_reconnect(serio);
403 goto out;
406 /* Not a new device, try processing first byte normally */
407 psmouse->pktcnt = 1;
408 if (psmouse_handle_byte(psmouse))
409 goto out;
411 psmouse->packet[psmouse->pktcnt++] = data;
415 * See if we need to force resync because mouse was idle for
416 * too long.
418 if (psmouse->state == PSMOUSE_ACTIVATED &&
419 psmouse->pktcnt == 1 && psmouse->resync_time &&
420 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
421 psmouse->badbyte = psmouse->packet[0];
422 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
423 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
424 goto out;
427 psmouse->last = jiffies;
428 psmouse_handle_byte(psmouse);
430 out:
431 return IRQ_HANDLED;
435 * psmouse_reset() resets the mouse into power-on state.
437 int psmouse_reset(struct psmouse *psmouse)
439 u8 param[2];
440 int error;
442 error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT);
443 if (error)
444 return error;
446 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
447 return -EIO;
449 return 0;
453 * Here we set the mouse resolution.
455 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
457 static const u8 params[] = { 0, 1, 2, 2, 3 };
458 u8 p;
460 if (resolution == 0 || resolution > 200)
461 resolution = 200;
463 p = params[resolution / 50];
464 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
465 psmouse->resolution = 25 << p;
469 * Here we set the mouse report rate.
471 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
473 static const u8 rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
474 u8 r;
475 int i = 0;
477 while (rates[i] > rate)
478 i++;
479 r = rates[i];
480 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
481 psmouse->rate = r;
485 * Here we set the mouse scaling.
487 static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
489 ps2_command(&psmouse->ps2dev, NULL,
490 scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
491 PSMOUSE_CMD_SETSCALE11);
495 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
497 static int psmouse_poll(struct psmouse *psmouse)
499 return ps2_command(&psmouse->ps2dev, psmouse->packet,
500 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
503 static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
505 int i;
507 for (i = 0; ids[i]; i++)
508 if (!strcasecmp(id, ids[i]))
509 return true;
511 return false;
515 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
517 bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
519 struct serio *serio = psmouse->ps2dev.serio;
520 char *p, *fw_id_copy, *save_ptr;
521 bool found = false;
523 if (strncmp(serio->firmware_id, "PNP: ", 5))
524 return false;
526 fw_id_copy = kstrndup(&serio->firmware_id[5],
527 sizeof(serio->firmware_id) - 5,
528 GFP_KERNEL);
529 if (!fw_id_copy)
530 return false;
532 save_ptr = fw_id_copy;
533 while ((p = strsep(&fw_id_copy, " ")) != NULL) {
534 if (psmouse_check_pnp_id(p, ids)) {
535 found = true;
536 break;
540 kfree(save_ptr);
541 return found;
545 * Genius NetMouse magic init.
547 static int genius_detect(struct psmouse *psmouse, bool set_properties)
549 struct ps2dev *ps2dev = &psmouse->ps2dev;
550 u8 param[4];
552 param[0] = 3;
553 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
554 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
555 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
556 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
557 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
559 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
560 return -ENODEV;
562 if (set_properties) {
563 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
564 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
565 __set_bit(BTN_SIDE, psmouse->dev->keybit);
566 __set_bit(REL_WHEEL, psmouse->dev->relbit);
568 psmouse->vendor = "Genius";
569 psmouse->name = "Mouse";
570 psmouse->pktsize = 4;
573 return 0;
577 * IntelliMouse magic init.
579 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
581 struct ps2dev *ps2dev = &psmouse->ps2dev;
582 u8 param[2];
584 param[0] = 200;
585 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
586 param[0] = 100;
587 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
588 param[0] = 80;
589 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
590 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
592 if (param[0] != 3)
593 return -ENODEV;
595 if (set_properties) {
596 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
597 __set_bit(REL_WHEEL, psmouse->dev->relbit);
599 if (!psmouse->vendor)
600 psmouse->vendor = "Generic";
601 if (!psmouse->name)
602 psmouse->name = "Wheel Mouse";
603 psmouse->pktsize = 4;
606 return 0;
610 * Try IntelliMouse/Explorer magic init.
612 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
614 struct ps2dev *ps2dev = &psmouse->ps2dev;
615 u8 param[2];
617 intellimouse_detect(psmouse, 0);
619 param[0] = 200;
620 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
621 param[0] = 200;
622 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
623 param[0] = 80;
624 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
625 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
627 if (param[0] != 4)
628 return -ENODEV;
630 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
631 param[0] = 200;
632 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
633 param[0] = 80;
634 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
635 param[0] = 40;
636 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
638 if (set_properties) {
639 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
640 __set_bit(REL_WHEEL, psmouse->dev->relbit);
641 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
642 __set_bit(BTN_SIDE, psmouse->dev->keybit);
643 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
645 if (!psmouse->vendor)
646 psmouse->vendor = "Generic";
647 if (!psmouse->name)
648 psmouse->name = "Explorer Mouse";
649 psmouse->pktsize = 4;
652 return 0;
656 * Kensington ThinkingMouse / ExpertMouse magic init.
658 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
660 struct ps2dev *ps2dev = &psmouse->ps2dev;
661 u8 param[2];
662 static const u8 seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
663 int i;
665 param[0] = 10;
666 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
667 param[0] = 0;
668 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
669 for (i = 0; i < ARRAY_SIZE(seq); i++) {
670 param[0] = seq[i];
671 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
673 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
675 if (param[0] != 2)
676 return -ENODEV;
678 if (set_properties) {
679 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
680 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
682 psmouse->vendor = "Kensington";
683 psmouse->name = "ThinkingMouse";
686 return 0;
690 * Bare PS/2 protocol "detection". Always succeeds.
692 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
694 if (set_properties) {
695 if (!psmouse->vendor)
696 psmouse->vendor = "Generic";
697 if (!psmouse->name)
698 psmouse->name = "Mouse";
701 * We have no way of figuring true number of buttons so let's
702 * assume that the device has 3.
704 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
707 return 0;
711 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
712 * must be forced by sysfs protocol writing.
714 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
716 if (set_properties) {
717 psmouse->vendor = "Cortron";
718 psmouse->name = "PS/2 Trackball";
720 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
721 __set_bit(BTN_SIDE, psmouse->dev->keybit);
724 return 0;
727 static const struct psmouse_protocol psmouse_protocols[] = {
729 .type = PSMOUSE_PS2,
730 .name = "PS/2",
731 .alias = "bare",
732 .maxproto = true,
733 .ignore_parity = true,
734 .detect = ps2bare_detect,
735 .try_passthru = true,
737 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
739 .type = PSMOUSE_PS2PP,
740 .name = "PS2++",
741 .alias = "logitech",
742 .detect = ps2pp_detect,
744 #endif
746 .type = PSMOUSE_THINKPS,
747 .name = "ThinkPS/2",
748 .alias = "thinkps",
749 .detect = thinking_detect,
751 #ifdef CONFIG_MOUSE_PS2_CYPRESS
753 .type = PSMOUSE_CYPRESS,
754 .name = "CyPS/2",
755 .alias = "cypress",
756 .detect = cypress_detect,
757 .init = cypress_init,
759 #endif
761 .type = PSMOUSE_GENPS,
762 .name = "GenPS/2",
763 .alias = "genius",
764 .detect = genius_detect,
767 .type = PSMOUSE_IMPS,
768 .name = "ImPS/2",
769 .alias = "imps",
770 .maxproto = true,
771 .ignore_parity = true,
772 .detect = intellimouse_detect,
773 .try_passthru = true,
776 .type = PSMOUSE_IMEX,
777 .name = "ImExPS/2",
778 .alias = "exps",
779 .maxproto = true,
780 .ignore_parity = true,
781 .detect = im_explorer_detect,
782 .try_passthru = true,
784 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
786 .type = PSMOUSE_SYNAPTICS,
787 .name = "SynPS/2",
788 .alias = "synaptics",
789 .detect = synaptics_detect,
790 .init = synaptics_init_absolute,
793 .type = PSMOUSE_SYNAPTICS_RELATIVE,
794 .name = "SynRelPS/2",
795 .alias = "synaptics-relative",
796 .detect = synaptics_detect,
797 .init = synaptics_init_relative,
799 #endif
800 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS
802 .type = PSMOUSE_SYNAPTICS_SMBUS,
803 .name = "SynSMBus",
804 .alias = "synaptics-smbus",
805 .detect = synaptics_detect,
806 .init = synaptics_init_smbus,
807 .smbus_companion = true,
809 #endif
810 #ifdef CONFIG_MOUSE_PS2_ALPS
812 .type = PSMOUSE_ALPS,
813 .name = "AlpsPS/2",
814 .alias = "alps",
815 .detect = alps_detect,
816 .init = alps_init,
818 #endif
819 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
821 .type = PSMOUSE_LIFEBOOK,
822 .name = "LBPS/2",
823 .alias = "lifebook",
824 .detect = lifebook_detect,
825 .init = lifebook_init,
827 #endif
828 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
830 .type = PSMOUSE_TRACKPOINT,
831 .name = "TPPS/2",
832 .alias = "trackpoint",
833 .detect = trackpoint_detect,
834 .try_passthru = true,
836 #endif
837 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
839 .type = PSMOUSE_TOUCHKIT_PS2,
840 .name = "touchkitPS/2",
841 .alias = "touchkit",
842 .detect = touchkit_ps2_detect,
844 #endif
845 #ifdef CONFIG_MOUSE_PS2_OLPC
847 .type = PSMOUSE_HGPK,
848 .name = "OLPC HGPK",
849 .alias = "hgpk",
850 .detect = hgpk_detect,
852 #endif
853 #ifdef CONFIG_MOUSE_PS2_ELANTECH
855 .type = PSMOUSE_ELANTECH,
856 .name = "ETPS/2",
857 .alias = "elantech",
858 .detect = elantech_detect,
859 .init = elantech_init_ps2,
861 #endif
862 #ifdef CONFIG_MOUSE_PS2_ELANTECH_SMBUS
864 .type = PSMOUSE_ELANTECH_SMBUS,
865 .name = "ETSMBus",
866 .alias = "elantech-smbus",
867 .detect = elantech_detect,
868 .init = elantech_init_smbus,
869 .smbus_companion = true,
871 #endif
872 #ifdef CONFIG_MOUSE_PS2_SENTELIC
874 .type = PSMOUSE_FSP,
875 .name = "FSPPS/2",
876 .alias = "fsp",
877 .detect = fsp_detect,
878 .init = fsp_init,
880 #endif
882 .type = PSMOUSE_CORTRON,
883 .name = "CortronPS/2",
884 .alias = "cortps",
885 .detect = cortron_detect,
887 #ifdef CONFIG_MOUSE_PS2_FOCALTECH
889 .type = PSMOUSE_FOCALTECH,
890 .name = "FocalTechPS/2",
891 .alias = "focaltech",
892 .detect = focaltech_detect,
893 .init = focaltech_init,
895 #endif
896 #ifdef CONFIG_MOUSE_PS2_VMMOUSE
898 .type = PSMOUSE_VMMOUSE,
899 .name = VMMOUSE_PSNAME,
900 .alias = "vmmouse",
901 .detect = vmmouse_detect,
902 .init = vmmouse_init,
904 #endif
905 #ifdef CONFIG_MOUSE_PS2_BYD
907 .type = PSMOUSE_BYD,
908 .name = "BYDPS/2",
909 .alias = "byd",
910 .detect = byd_detect,
911 .init = byd_init,
913 #endif
915 .type = PSMOUSE_AUTO,
916 .name = "auto",
917 .alias = "any",
918 .maxproto = true,
922 static const struct psmouse_protocol *__psmouse_protocol_by_type(enum psmouse_type type)
924 int i;
926 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
927 if (psmouse_protocols[i].type == type)
928 return &psmouse_protocols[i];
930 return NULL;
933 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
935 const struct psmouse_protocol *proto;
937 proto = __psmouse_protocol_by_type(type);
938 if (proto)
939 return proto;
941 WARN_ON(1);
942 return &psmouse_protocols[0];
945 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
947 const struct psmouse_protocol *p;
948 int i;
950 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
951 p = &psmouse_protocols[i];
953 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
954 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
955 return &psmouse_protocols[i];
958 return NULL;
962 * Apply default settings to the psmouse structure. Most of them will
963 * be overridden by individual protocol initialization routines.
965 static void psmouse_apply_defaults(struct psmouse *psmouse)
967 struct input_dev *input_dev = psmouse->dev;
969 bitmap_zero(input_dev->evbit, EV_CNT);
970 bitmap_zero(input_dev->keybit, KEY_CNT);
971 bitmap_zero(input_dev->relbit, REL_CNT);
972 bitmap_zero(input_dev->absbit, ABS_CNT);
973 bitmap_zero(input_dev->mscbit, MSC_CNT);
975 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
976 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
978 input_set_capability(input_dev, EV_REL, REL_X);
979 input_set_capability(input_dev, EV_REL, REL_Y);
981 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
983 psmouse->protocol = &psmouse_protocols[0];
985 psmouse->set_rate = psmouse_set_rate;
986 psmouse->set_resolution = psmouse_set_resolution;
987 psmouse->set_scale = psmouse_set_scale;
988 psmouse->poll = psmouse_poll;
989 psmouse->protocol_handler = psmouse_process_byte;
990 psmouse->pktsize = 3;
991 psmouse->reconnect = NULL;
992 psmouse->fast_reconnect = NULL;
993 psmouse->disconnect = NULL;
994 psmouse->cleanup = NULL;
995 psmouse->pt_activate = NULL;
996 psmouse->pt_deactivate = NULL;
999 static bool psmouse_do_detect(int (*detect)(struct psmouse *, bool),
1000 struct psmouse *psmouse, bool allow_passthrough,
1001 bool set_properties)
1003 if (psmouse->ps2dev.serio->id.type == SERIO_PS_PSTHRU &&
1004 !allow_passthrough) {
1005 return false;
1008 if (set_properties)
1009 psmouse_apply_defaults(psmouse);
1011 return detect(psmouse, set_properties) == 0;
1014 static bool psmouse_try_protocol(struct psmouse *psmouse,
1015 enum psmouse_type type,
1016 unsigned int *max_proto,
1017 bool set_properties, bool init_allowed)
1019 const struct psmouse_protocol *proto;
1021 proto = __psmouse_protocol_by_type(type);
1022 if (!proto)
1023 return false;
1025 if (!psmouse_do_detect(proto->detect, psmouse, proto->try_passthru,
1026 set_properties))
1027 return false;
1029 if (set_properties && proto->init && init_allowed) {
1030 if (proto->init(psmouse) != 0) {
1032 * We detected device, but init failed. Adjust
1033 * max_proto so we only try standard protocols.
1035 if (*max_proto > PSMOUSE_IMEX)
1036 *max_proto = PSMOUSE_IMEX;
1038 return false;
1042 return true;
1046 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
1047 * the mouse may have.
1049 static int psmouse_extensions(struct psmouse *psmouse,
1050 unsigned int max_proto, bool set_properties)
1052 bool synaptics_hardware = false;
1053 int ret;
1056 * Always check for focaltech, this is safe as it uses pnp-id
1057 * matching.
1059 if (psmouse_do_detect(focaltech_detect,
1060 psmouse, false, set_properties)) {
1061 if (max_proto > PSMOUSE_IMEX &&
1062 IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH) &&
1063 (!set_properties || focaltech_init(psmouse) == 0)) {
1064 return PSMOUSE_FOCALTECH;
1067 * Restrict psmouse_max_proto so that psmouse_initialize()
1068 * does not try to reset rate and resolution, because even
1069 * that upsets the device.
1070 * This also causes us to basically fall through to basic
1071 * protocol detection, where we fully reset the mouse,
1072 * and set it up as bare PS/2 protocol device.
1074 psmouse_max_proto = max_proto = PSMOUSE_PS2;
1078 * We always check for LifeBook because it does not disturb mouse
1079 * (it only checks DMI information).
1081 if (psmouse_try_protocol(psmouse, PSMOUSE_LIFEBOOK, &max_proto,
1082 set_properties, max_proto > PSMOUSE_IMEX))
1083 return PSMOUSE_LIFEBOOK;
1085 if (psmouse_try_protocol(psmouse, PSMOUSE_VMMOUSE, &max_proto,
1086 set_properties, max_proto > PSMOUSE_IMEX))
1087 return PSMOUSE_VMMOUSE;
1090 * Try Kensington ThinkingMouse (we try first, because Synaptics
1091 * probe upsets the ThinkingMouse).
1093 if (max_proto > PSMOUSE_IMEX &&
1094 psmouse_try_protocol(psmouse, PSMOUSE_THINKPS, &max_proto,
1095 set_properties, true)) {
1096 return PSMOUSE_THINKPS;
1100 * Try Synaptics TouchPad. Note that probing is done even if
1101 * Synaptics protocol support is disabled in config - we need to
1102 * know if it is Synaptics so we can reset it properly after
1103 * probing for IntelliMouse.
1105 if (max_proto > PSMOUSE_PS2 &&
1106 psmouse_do_detect(synaptics_detect,
1107 psmouse, false, set_properties)) {
1108 synaptics_hardware = true;
1110 if (max_proto > PSMOUSE_IMEX) {
1112 * Try activating protocol, but check if support is
1113 * enabled first, since we try detecting Synaptics
1114 * even when protocol is disabled.
1116 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) ||
1117 IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)) {
1118 if (!set_properties)
1119 return PSMOUSE_SYNAPTICS;
1121 ret = synaptics_init(psmouse);
1122 if (ret >= 0)
1123 return ret;
1127 * Some Synaptics touchpads can emulate extended
1128 * protocols (like IMPS/2). Unfortunately
1129 * Logitech/Genius probes confuse some firmware
1130 * versions so we'll have to skip them.
1132 max_proto = PSMOUSE_IMEX;
1136 * Make sure that touchpad is in relative mode, gestures
1137 * (taps) are enabled.
1139 synaptics_reset(psmouse);
1143 * Try Cypress Trackpad. We must try it before Finger Sensing Pad
1144 * because Finger Sensing Pad probe upsets some modules of Cypress
1145 * Trackpads.
1147 if (max_proto > PSMOUSE_IMEX &&
1148 psmouse_try_protocol(psmouse, PSMOUSE_CYPRESS, &max_proto,
1149 set_properties, true)) {
1150 return PSMOUSE_CYPRESS;
1153 /* Try ALPS TouchPad */
1154 if (max_proto > PSMOUSE_IMEX) {
1155 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1156 if (psmouse_try_protocol(psmouse, PSMOUSE_ALPS,
1157 &max_proto, set_properties, true))
1158 return PSMOUSE_ALPS;
1161 /* Try OLPC HGPK touchpad */
1162 if (max_proto > PSMOUSE_IMEX &&
1163 psmouse_try_protocol(psmouse, PSMOUSE_HGPK, &max_proto,
1164 set_properties, true)) {
1165 return PSMOUSE_HGPK;
1168 /* Try Elantech touchpad */
1169 if (max_proto > PSMOUSE_IMEX &&
1170 psmouse_try_protocol(psmouse, PSMOUSE_ELANTECH,
1171 &max_proto, set_properties, false)) {
1172 if (!set_properties)
1173 return PSMOUSE_ELANTECH;
1175 ret = elantech_init(psmouse);
1176 if (ret >= 0)
1177 return ret;
1180 if (max_proto > PSMOUSE_IMEX) {
1181 if (psmouse_try_protocol(psmouse, PSMOUSE_GENPS,
1182 &max_proto, set_properties, true))
1183 return PSMOUSE_GENPS;
1185 if (psmouse_try_protocol(psmouse, PSMOUSE_PS2PP,
1186 &max_proto, set_properties, true))
1187 return PSMOUSE_PS2PP;
1189 if (psmouse_try_protocol(psmouse, PSMOUSE_TRACKPOINT,
1190 &max_proto, set_properties, true))
1191 return PSMOUSE_TRACKPOINT;
1193 if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2,
1194 &max_proto, set_properties, true))
1195 return PSMOUSE_TOUCHKIT_PS2;
1199 * Try Finger Sensing Pad. We do it here because its probe upsets
1200 * Trackpoint devices (causing TP_READ_ID command to time out).
1202 if (max_proto > PSMOUSE_IMEX &&
1203 psmouse_try_protocol(psmouse, PSMOUSE_FSP,
1204 &max_proto, set_properties, true)) {
1205 return PSMOUSE_FSP;
1209 * Reset to defaults in case the device got confused by extended
1210 * protocol probes. Note that we follow up with full reset because
1211 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
1213 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1214 psmouse_reset(psmouse);
1216 if (max_proto >= PSMOUSE_IMEX &&
1217 psmouse_try_protocol(psmouse, PSMOUSE_IMEX,
1218 &max_proto, set_properties, true)) {
1219 return PSMOUSE_IMEX;
1222 if (max_proto >= PSMOUSE_IMPS &&
1223 psmouse_try_protocol(psmouse, PSMOUSE_IMPS,
1224 &max_proto, set_properties, true)) {
1225 return PSMOUSE_IMPS;
1229 * Okay, all failed, we have a standard mouse here. The number of
1230 * the buttons is still a question, though. We assume 3.
1232 psmouse_try_protocol(psmouse, PSMOUSE_PS2,
1233 &max_proto, set_properties, true);
1235 if (synaptics_hardware) {
1237 * We detected Synaptics hardware but it did not respond to
1238 * IMPS/2 probes. We need to reset the touchpad because if
1239 * there is a track point on the pass through port it could
1240 * get disabled while probing for protocol extensions.
1242 psmouse_reset(psmouse);
1245 return PSMOUSE_PS2;
1249 * psmouse_probe() probes for a PS/2 mouse.
1251 static int psmouse_probe(struct psmouse *psmouse)
1253 struct ps2dev *ps2dev = &psmouse->ps2dev;
1254 u8 param[2];
1255 int error;
1258 * First, we check if it's a mouse. It should send 0x00 or 0x03 in
1259 * case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1260 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and
1261 * subsequent ID queries, probably due to a firmware bug.
1263 param[0] = 0xa5;
1264 error = ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
1265 if (error)
1266 return error;
1268 if (param[0] != 0x00 && param[0] != 0x03 &&
1269 param[0] != 0x04 && param[0] != 0xff)
1270 return -ENODEV;
1273 * Then we reset and disable the mouse so that it doesn't generate
1274 * events.
1276 error = ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1277 if (error)
1278 psmouse_warn(psmouse, "Failed to reset mouse on %s: %d\n",
1279 ps2dev->serio->phys, error);
1281 return 0;
1285 * psmouse_initialize() initializes the mouse to a sane state.
1287 static void psmouse_initialize(struct psmouse *psmouse)
1290 * We set the mouse report rate, resolution and scaling.
1292 if (psmouse_max_proto != PSMOUSE_PS2) {
1293 psmouse->set_rate(psmouse, psmouse->rate);
1294 psmouse->set_resolution(psmouse, psmouse->resolution);
1295 psmouse->set_scale(psmouse, PSMOUSE_SCALE11);
1300 * psmouse_activate() enables the mouse so that we get motion reports from it.
1302 int psmouse_activate(struct psmouse *psmouse)
1304 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1305 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1306 psmouse->ps2dev.serio->phys);
1307 return -1;
1310 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1311 return 0;
1315 * psmouse_deactivate() puts the mouse into poll mode so that we don't get
1316 * motion reports from it unless we explicitly request it.
1318 int psmouse_deactivate(struct psmouse *psmouse)
1320 int error;
1322 error = ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE);
1323 if (error) {
1324 psmouse_warn(psmouse, "Failed to deactivate mouse on %s: %d\n",
1325 psmouse->ps2dev.serio->phys, error);
1326 return error;
1329 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1330 return 0;
1334 * psmouse_resync() attempts to re-validate current protocol.
1336 static void psmouse_resync(struct work_struct *work)
1338 struct psmouse *parent = NULL, *psmouse =
1339 container_of(work, struct psmouse, resync_work.work);
1340 struct serio *serio = psmouse->ps2dev.serio;
1341 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1342 bool failed = false, enabled = false;
1343 int i;
1345 mutex_lock(&psmouse_mutex);
1347 if (psmouse->state != PSMOUSE_RESYNCING)
1348 goto out;
1350 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1351 parent = serio_get_drvdata(serio->parent);
1352 psmouse_deactivate(parent);
1356 * Some mice don't ACK commands sent while they are in the middle of
1357 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1358 * instead of ps2_command() which would wait for 200ms for an ACK
1359 * that may never come.
1360 * As an additional quirk ALPS touchpads may not only forget to ACK
1361 * disable command but will stop reporting taps, so if we see that
1362 * mouse at least once ACKs disable we will do full reconnect if ACK
1363 * is missing.
1365 psmouse->num_resyncs++;
1367 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1368 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1369 failed = true;
1370 } else
1371 psmouse->acks_disable_command = true;
1374 * Poll the mouse. If it was reset the packet will be shorter than
1375 * psmouse->pktsize and ps2_command will fail. We do not expect and
1376 * do not handle scenario when mouse "upgrades" its protocol while
1377 * disconnected since it would require additional delay. If we ever
1378 * see a mouse that does it we'll adjust the code.
1380 if (!failed) {
1381 if (psmouse->poll(psmouse))
1382 failed = true;
1383 else {
1384 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1385 for (i = 0; i < psmouse->pktsize; i++) {
1386 psmouse->pktcnt++;
1387 rc = psmouse->protocol_handler(psmouse);
1388 if (rc != PSMOUSE_GOOD_DATA)
1389 break;
1391 if (rc != PSMOUSE_FULL_PACKET)
1392 failed = true;
1393 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1398 * Now try to enable mouse. We try to do that even if poll failed
1399 * and also repeat our attempts 5 times, otherwise we may be left
1400 * out with disabled mouse.
1402 for (i = 0; i < 5; i++) {
1403 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1404 enabled = true;
1405 break;
1407 msleep(200);
1410 if (!enabled) {
1411 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1412 psmouse->ps2dev.serio->phys);
1413 failed = true;
1416 if (failed) {
1417 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1418 psmouse_info(psmouse,
1419 "resync failed, issuing reconnect request\n");
1420 serio_reconnect(serio);
1421 } else
1422 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1424 if (parent)
1425 psmouse_activate(parent);
1426 out:
1427 mutex_unlock(&psmouse_mutex);
1431 * psmouse_cleanup() resets the mouse into power-on state.
1433 static void psmouse_cleanup(struct serio *serio)
1435 struct psmouse *psmouse = serio_get_drvdata(serio);
1436 struct psmouse *parent = NULL;
1438 mutex_lock(&psmouse_mutex);
1440 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1441 parent = serio_get_drvdata(serio->parent);
1442 psmouse_deactivate(parent);
1445 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1448 * Disable stream mode so cleanup routine can proceed undisturbed.
1450 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1451 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1452 psmouse->ps2dev.serio->phys);
1454 if (psmouse->cleanup)
1455 psmouse->cleanup(psmouse);
1458 * Reset the mouse to defaults (bare PS/2 protocol).
1460 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1463 * Some boxes, such as HP nx7400, get terribly confused if mouse
1464 * is not fully enabled before suspending/shutting down.
1466 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1468 if (parent) {
1469 if (parent->pt_deactivate)
1470 parent->pt_deactivate(parent);
1472 psmouse_activate(parent);
1475 mutex_unlock(&psmouse_mutex);
1479 * psmouse_disconnect() closes and frees.
1481 static void psmouse_disconnect(struct serio *serio)
1483 struct psmouse *psmouse = serio_get_drvdata(serio);
1484 struct psmouse *parent = NULL;
1486 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1488 mutex_lock(&psmouse_mutex);
1490 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1492 /* make sure we don't have a resync in progress */
1493 mutex_unlock(&psmouse_mutex);
1494 flush_workqueue(kpsmoused_wq);
1495 mutex_lock(&psmouse_mutex);
1497 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1498 parent = serio_get_drvdata(serio->parent);
1499 psmouse_deactivate(parent);
1502 if (psmouse->disconnect)
1503 psmouse->disconnect(psmouse);
1505 if (parent && parent->pt_deactivate)
1506 parent->pt_deactivate(parent);
1508 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1510 serio_close(serio);
1511 serio_set_drvdata(serio, NULL);
1513 if (psmouse->dev)
1514 input_unregister_device(psmouse->dev);
1516 kfree(psmouse);
1518 if (parent)
1519 psmouse_activate(parent);
1521 mutex_unlock(&psmouse_mutex);
1524 static int psmouse_switch_protocol(struct psmouse *psmouse,
1525 const struct psmouse_protocol *proto)
1527 const struct psmouse_protocol *selected_proto;
1528 struct input_dev *input_dev = psmouse->dev;
1529 enum psmouse_type type;
1531 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1533 if (proto && (proto->detect || proto->init)) {
1534 psmouse_apply_defaults(psmouse);
1536 if (proto->detect && proto->detect(psmouse, true) < 0)
1537 return -1;
1539 if (proto->init && proto->init(psmouse) < 0)
1540 return -1;
1542 selected_proto = proto;
1543 } else {
1544 type = psmouse_extensions(psmouse, psmouse_max_proto, true);
1545 selected_proto = psmouse_protocol_by_type(type);
1548 psmouse->protocol = selected_proto;
1551 * If mouse's packet size is 3 there is no point in polling the
1552 * device in hopes to detect protocol reset - we won't get less
1553 * than 3 bytes response anyhow.
1555 if (psmouse->pktsize == 3)
1556 psmouse->resync_time = 0;
1559 * Some smart KVMs fake response to POLL command returning just
1560 * 3 bytes and messing up our resync logic, so if initial poll
1561 * fails we won't try polling the device anymore. Hopefully
1562 * such KVM will maintain initially selected protocol.
1564 if (psmouse->resync_time && psmouse->poll(psmouse))
1565 psmouse->resync_time = 0;
1567 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1568 selected_proto->name, psmouse->vendor, psmouse->name);
1570 input_dev->name = psmouse->devname;
1571 input_dev->phys = psmouse->phys;
1572 input_dev->id.bustype = BUS_I8042;
1573 input_dev->id.vendor = 0x0002;
1574 input_dev->id.product = psmouse->protocol->type;
1575 input_dev->id.version = psmouse->model;
1577 return 0;
1581 * psmouse_connect() is a callback from the serio module when
1582 * an unhandled serio port is found.
1584 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1586 struct psmouse *psmouse, *parent = NULL;
1587 struct input_dev *input_dev;
1588 int retval = 0, error = -ENOMEM;
1590 mutex_lock(&psmouse_mutex);
1593 * If this is a pass-through port deactivate parent so the device
1594 * connected to this port can be successfully identified
1596 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1597 parent = serio_get_drvdata(serio->parent);
1598 psmouse_deactivate(parent);
1601 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1602 input_dev = input_allocate_device();
1603 if (!psmouse || !input_dev)
1604 goto err_free;
1606 ps2_init(&psmouse->ps2dev, serio);
1607 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1608 psmouse->dev = input_dev;
1609 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1611 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1613 serio_set_drvdata(serio, psmouse);
1615 error = serio_open(serio, drv);
1616 if (error)
1617 goto err_clear_drvdata;
1619 /* give PT device some time to settle down before probing */
1620 if (serio->id.type == SERIO_PS_PSTHRU)
1621 usleep_range(10000, 15000);
1623 if (psmouse_probe(psmouse) < 0) {
1624 error = -ENODEV;
1625 goto err_close_serio;
1628 psmouse->rate = psmouse_rate;
1629 psmouse->resolution = psmouse_resolution;
1630 psmouse->resetafter = psmouse_resetafter;
1631 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1632 psmouse->smartscroll = psmouse_smartscroll;
1634 psmouse_switch_protocol(psmouse, NULL);
1636 if (!psmouse->protocol->smbus_companion) {
1637 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1638 psmouse_initialize(psmouse);
1640 error = input_register_device(input_dev);
1641 if (error)
1642 goto err_protocol_disconnect;
1643 } else {
1644 /* Smbus companion will be reporting events, not us. */
1645 input_free_device(input_dev);
1646 psmouse->dev = input_dev = NULL;
1649 if (parent && parent->pt_activate)
1650 parent->pt_activate(parent);
1652 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1653 if (error)
1654 goto err_pt_deactivate;
1657 * PS/2 devices having SMBus companions should stay disabled
1658 * on PS/2 side, in order to have SMBus part operable.
1660 if (!psmouse->protocol->smbus_companion)
1661 psmouse_activate(psmouse);
1663 out:
1664 /* If this is a pass-through port the parent needs to be re-activated */
1665 if (parent)
1666 psmouse_activate(parent);
1668 mutex_unlock(&psmouse_mutex);
1669 return retval;
1671 err_pt_deactivate:
1672 if (parent && parent->pt_deactivate)
1673 parent->pt_deactivate(parent);
1674 if (input_dev) {
1675 input_unregister_device(input_dev);
1676 input_dev = NULL; /* so we don't try to free it below */
1678 err_protocol_disconnect:
1679 if (psmouse->disconnect)
1680 psmouse->disconnect(psmouse);
1681 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1682 err_close_serio:
1683 serio_close(serio);
1684 err_clear_drvdata:
1685 serio_set_drvdata(serio, NULL);
1686 err_free:
1687 input_free_device(input_dev);
1688 kfree(psmouse);
1690 retval = error;
1691 goto out;
1694 static int __psmouse_reconnect(struct serio *serio, bool fast_reconnect)
1696 struct psmouse *psmouse = serio_get_drvdata(serio);
1697 struct psmouse *parent = NULL;
1698 int (*reconnect_handler)(struct psmouse *);
1699 enum psmouse_type type;
1700 int rc = -1;
1702 mutex_lock(&psmouse_mutex);
1704 if (fast_reconnect) {
1705 reconnect_handler = psmouse->fast_reconnect;
1706 if (!reconnect_handler) {
1707 rc = -ENOENT;
1708 goto out_unlock;
1710 } else {
1711 reconnect_handler = psmouse->reconnect;
1714 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1715 parent = serio_get_drvdata(serio->parent);
1716 psmouse_deactivate(parent);
1719 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1721 if (reconnect_handler) {
1722 if (reconnect_handler(psmouse))
1723 goto out;
1724 } else {
1725 psmouse_reset(psmouse);
1727 if (psmouse_probe(psmouse) < 0)
1728 goto out;
1730 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1731 if (psmouse->protocol->type != type)
1732 goto out;
1736 * OK, the device type (and capabilities) match the old one,
1737 * we can continue using it, complete initialization
1739 if (!psmouse->protocol->smbus_companion) {
1740 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1741 psmouse_initialize(psmouse);
1744 if (parent && parent->pt_activate)
1745 parent->pt_activate(parent);
1748 * PS/2 devices having SMBus companions should stay disabled
1749 * on PS/2 side, in order to have SMBus part operable.
1751 if (!psmouse->protocol->smbus_companion)
1752 psmouse_activate(psmouse);
1754 rc = 0;
1756 out:
1757 /* If this is a pass-through port the parent waits to be activated */
1758 if (parent)
1759 psmouse_activate(parent);
1761 out_unlock:
1762 mutex_unlock(&psmouse_mutex);
1763 return rc;
1766 static int psmouse_reconnect(struct serio *serio)
1768 return __psmouse_reconnect(serio, false);
1771 static int psmouse_fast_reconnect(struct serio *serio)
1773 return __psmouse_reconnect(serio, true);
1776 static struct serio_device_id psmouse_serio_ids[] = {
1778 .type = SERIO_8042,
1779 .proto = SERIO_ANY,
1780 .id = SERIO_ANY,
1781 .extra = SERIO_ANY,
1784 .type = SERIO_PS_PSTHRU,
1785 .proto = SERIO_ANY,
1786 .id = SERIO_ANY,
1787 .extra = SERIO_ANY,
1789 { 0 }
1792 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1794 static struct serio_driver psmouse_drv = {
1795 .driver = {
1796 .name = "psmouse",
1798 .description = DRIVER_DESC,
1799 .id_table = psmouse_serio_ids,
1800 .interrupt = psmouse_interrupt,
1801 .connect = psmouse_connect,
1802 .reconnect = psmouse_reconnect,
1803 .fast_reconnect = psmouse_fast_reconnect,
1804 .disconnect = psmouse_disconnect,
1805 .cleanup = psmouse_cleanup,
1808 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1809 char *buf)
1811 struct serio *serio = to_serio_port(dev);
1812 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1813 struct psmouse *psmouse = serio_get_drvdata(serio);
1815 if (psmouse->protocol->smbus_companion &&
1816 devattr != &psmouse_attr_protocol.dattr)
1817 return -ENOENT;
1819 return attr->show(psmouse, attr->data, buf);
1822 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1823 const char *buf, size_t count)
1825 struct serio *serio = to_serio_port(dev);
1826 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1827 struct psmouse *psmouse, *parent = NULL;
1828 int retval;
1830 retval = mutex_lock_interruptible(&psmouse_mutex);
1831 if (retval)
1832 goto out;
1834 psmouse = serio_get_drvdata(serio);
1836 if (psmouse->protocol->smbus_companion &&
1837 devattr != &psmouse_attr_protocol.dattr) {
1838 retval = -ENOENT;
1839 goto out_unlock;
1842 if (attr->protect) {
1843 if (psmouse->state == PSMOUSE_IGNORE) {
1844 retval = -ENODEV;
1845 goto out_unlock;
1848 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1849 parent = serio_get_drvdata(serio->parent);
1850 psmouse_deactivate(parent);
1853 if (!psmouse->protocol->smbus_companion)
1854 psmouse_deactivate(psmouse);
1857 retval = attr->set(psmouse, attr->data, buf, count);
1859 if (attr->protect) {
1860 if (retval != -ENODEV && !psmouse->protocol->smbus_companion)
1861 psmouse_activate(psmouse);
1863 if (parent)
1864 psmouse_activate(parent);
1867 out_unlock:
1868 mutex_unlock(&psmouse_mutex);
1869 out:
1870 return retval;
1873 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1875 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1877 return sprintf(buf, "%u\n", *field);
1880 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1882 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1883 unsigned int value;
1884 int err;
1886 err = kstrtouint(buf, 10, &value);
1887 if (err)
1888 return err;
1890 *field = value;
1892 return count;
1895 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1897 return sprintf(buf, "%s\n", psmouse->protocol->name);
1900 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1902 struct serio *serio = psmouse->ps2dev.serio;
1903 struct psmouse *parent = NULL;
1904 struct input_dev *old_dev, *new_dev;
1905 const struct psmouse_protocol *proto, *old_proto;
1906 int error;
1907 int retry = 0;
1909 proto = psmouse_protocol_by_name(buf, count);
1910 if (!proto)
1911 return -EINVAL;
1913 if (psmouse->protocol == proto)
1914 return count;
1916 new_dev = input_allocate_device();
1917 if (!new_dev)
1918 return -ENOMEM;
1920 while (!list_empty(&serio->children)) {
1921 if (++retry > 3) {
1922 psmouse_warn(psmouse,
1923 "failed to destroy children ports, protocol change aborted.\n");
1924 input_free_device(new_dev);
1925 return -EIO;
1928 mutex_unlock(&psmouse_mutex);
1929 serio_unregister_child_port(serio);
1930 mutex_lock(&psmouse_mutex);
1932 if (serio->drv != &psmouse_drv) {
1933 input_free_device(new_dev);
1934 return -ENODEV;
1937 if (psmouse->protocol == proto) {
1938 input_free_device(new_dev);
1939 return count; /* switched by other thread */
1943 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1944 parent = serio_get_drvdata(serio->parent);
1945 if (parent->pt_deactivate)
1946 parent->pt_deactivate(parent);
1949 old_dev = psmouse->dev;
1950 old_proto = psmouse->protocol;
1952 if (psmouse->disconnect)
1953 psmouse->disconnect(psmouse);
1955 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1957 psmouse->dev = new_dev;
1958 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1960 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1961 psmouse_reset(psmouse);
1962 /* default to PSMOUSE_PS2 */
1963 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1966 psmouse_initialize(psmouse);
1967 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1969 if (psmouse->protocol->smbus_companion) {
1970 input_free_device(psmouse->dev);
1971 psmouse->dev = NULL;
1972 } else {
1973 error = input_register_device(psmouse->dev);
1974 if (error) {
1975 if (psmouse->disconnect)
1976 psmouse->disconnect(psmouse);
1978 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1979 input_free_device(new_dev);
1980 psmouse->dev = old_dev;
1981 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1982 psmouse_switch_protocol(psmouse, old_proto);
1983 psmouse_initialize(psmouse);
1984 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1986 return error;
1990 if (old_dev)
1991 input_unregister_device(old_dev);
1993 if (parent && parent->pt_activate)
1994 parent->pt_activate(parent);
1996 return count;
1999 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
2001 unsigned int value;
2002 int err;
2004 err = kstrtouint(buf, 10, &value);
2005 if (err)
2006 return err;
2008 psmouse->set_rate(psmouse, value);
2009 return count;
2012 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
2014 unsigned int value;
2015 int err;
2017 err = kstrtouint(buf, 10, &value);
2018 if (err)
2019 return err;
2021 psmouse->set_resolution(psmouse, value);
2022 return count;
2026 static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
2028 const struct psmouse_protocol *proto;
2030 if (!val)
2031 return -EINVAL;
2033 proto = psmouse_protocol_by_name(val, strlen(val));
2035 if (!proto || !proto->maxproto)
2036 return -EINVAL;
2038 *((unsigned int *)kp->arg) = proto->type;
2040 return 0;
2043 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
2045 int type = *((unsigned int *)kp->arg);
2047 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
2050 static int __init psmouse_init(void)
2052 int err;
2054 lifebook_module_init();
2055 synaptics_module_init();
2056 hgpk_module_init();
2058 err = psmouse_smbus_module_init();
2059 if (err)
2060 return err;
2062 kpsmoused_wq = alloc_ordered_workqueue("kpsmoused", 0);
2063 if (!kpsmoused_wq) {
2064 pr_err("failed to create kpsmoused workqueue\n");
2065 err = -ENOMEM;
2066 goto err_smbus_exit;
2069 err = serio_register_driver(&psmouse_drv);
2070 if (err)
2071 goto err_destroy_wq;
2073 return 0;
2075 err_destroy_wq:
2076 destroy_workqueue(kpsmoused_wq);
2077 err_smbus_exit:
2078 psmouse_smbus_module_exit();
2079 return err;
2082 static void __exit psmouse_exit(void)
2084 serio_unregister_driver(&psmouse_drv);
2085 destroy_workqueue(kpsmoused_wq);
2086 psmouse_smbus_module_exit();
2089 module_init(psmouse_init);
2090 module_exit(psmouse_exit);