Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / input / mouse / psmouse-base.c
blob8ac9e03c05b457523b7480bfca1154144ac64da7
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"
40 #include "byd.h"
42 #define DRIVER_DESC "PS/2 mouse driver"
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
45 MODULE_DESCRIPTION(DRIVER_DESC);
46 MODULE_LICENSE("GPL");
48 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
49 static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
50 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
51 static const struct kernel_param_ops param_ops_proto_abbrev = {
52 .set = psmouse_set_maxproto,
53 .get = psmouse_get_maxproto,
55 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
56 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
57 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
59 static unsigned int psmouse_resolution = 200;
60 module_param_named(resolution, psmouse_resolution, uint, 0644);
61 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
63 static unsigned int psmouse_rate = 100;
64 module_param_named(rate, psmouse_rate, uint, 0644);
65 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
67 static bool psmouse_smartscroll = true;
68 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
69 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
71 static unsigned int psmouse_resetafter = 5;
72 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
73 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
75 static unsigned int psmouse_resync_time;
76 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
77 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
79 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
80 NULL,
81 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
82 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
83 (void *) offsetof(struct psmouse, rate),
84 psmouse_show_int_attr, psmouse_attr_set_rate);
85 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
86 (void *) offsetof(struct psmouse, resolution),
87 psmouse_show_int_attr, psmouse_attr_set_resolution);
88 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
89 (void *) offsetof(struct psmouse, resetafter),
90 psmouse_show_int_attr, psmouse_set_int_attr);
91 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
92 (void *) offsetof(struct psmouse, resync_time),
93 psmouse_show_int_attr, psmouse_set_int_attr);
95 static struct attribute *psmouse_attributes[] = {
96 &psmouse_attr_protocol.dattr.attr,
97 &psmouse_attr_rate.dattr.attr,
98 &psmouse_attr_resolution.dattr.attr,
99 &psmouse_attr_resetafter.dattr.attr,
100 &psmouse_attr_resync_time.dattr.attr,
101 NULL
104 static const struct attribute_group psmouse_attribute_group = {
105 .attrs = psmouse_attributes,
109 * psmouse_mutex protects all operations changing state of mouse
110 * (connecting, disconnecting, changing rate or resolution via
111 * sysfs). We could use a per-device semaphore but since there
112 * rarely more than one PS/2 mouse connected and since semaphore
113 * is taken in "slow" paths it is not worth it.
115 static DEFINE_MUTEX(psmouse_mutex);
117 static struct workqueue_struct *kpsmoused_wq;
119 static void psmouse_report_standard_buttons(struct input_dev *dev, u8 buttons)
121 input_report_key(dev, BTN_LEFT, buttons & BIT(0));
122 input_report_key(dev, BTN_MIDDLE, buttons & BIT(2));
123 input_report_key(dev, BTN_RIGHT, buttons & BIT(1));
127 * psmouse_process_byte() analyzes the PS/2 data stream and reports
128 * relevant events to the input module once full packet has arrived.
130 psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
132 struct input_dev *dev = psmouse->dev;
133 unsigned char *packet = psmouse->packet;
135 if (psmouse->pktcnt < psmouse->pktsize)
136 return PSMOUSE_GOOD_DATA;
138 /* Full packet accumulated, process it */
140 switch (psmouse->protocol->type) {
141 case PSMOUSE_IMPS:
142 /* IntelliMouse has scroll wheel */
143 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
144 break;
146 case PSMOUSE_IMEX:
147 /* Scroll wheel and buttons on IntelliMouse Explorer */
148 switch (packet[3] & 0xC0) {
149 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
150 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
151 break;
152 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
153 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154 break;
155 case 0x00:
156 case 0xC0:
157 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
158 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
159 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
160 break;
162 break;
164 case PSMOUSE_GENPS:
165 /* Report scroll buttons on NetMice */
166 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
168 /* Extra buttons on Genius NewNet 3D */
169 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
170 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
171 break;
173 case PSMOUSE_THINKPS:
174 /* Extra button on ThinkingMouse */
175 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
178 * Without this bit of weirdness moving up gives wildly
179 * high Y changes.
181 packet[1] |= (packet[0] & 0x40) << 1;
182 break;
184 case PSMOUSE_CORTRON:
186 * Cortron PS2 Trackball reports SIDE button in the
187 * 4th bit of the first byte.
189 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
190 packet[0] |= 0x08;
191 break;
193 default:
194 break;
197 /* Generic PS/2 Mouse */
198 psmouse_report_standard_buttons(dev,
199 packet[0] | psmouse->extra_buttons);
201 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
202 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
204 input_sync(dev);
206 return PSMOUSE_FULL_PACKET;
209 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
210 unsigned long delay)
212 queue_delayed_work(kpsmoused_wq, work, delay);
216 * __psmouse_set_state() sets new psmouse state and resets all flags.
218 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
220 psmouse->state = new_state;
221 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
222 psmouse->ps2dev.flags = 0;
223 psmouse->last = jiffies;
227 * psmouse_set_state() sets new psmouse state and resets all flags and
228 * counters while holding serio lock so fighting with interrupt handler
229 * is not a concern.
231 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
233 serio_pause_rx(psmouse->ps2dev.serio);
234 __psmouse_set_state(psmouse, new_state);
235 serio_continue_rx(psmouse->ps2dev.serio);
239 * psmouse_handle_byte() processes one byte of the input data stream
240 * by calling corresponding protocol handler.
242 static int psmouse_handle_byte(struct psmouse *psmouse)
244 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
246 switch (rc) {
247 case PSMOUSE_BAD_DATA:
248 if (psmouse->state == PSMOUSE_ACTIVATED) {
249 psmouse_warn(psmouse,
250 "%s at %s lost sync at byte %d\n",
251 psmouse->name, psmouse->phys,
252 psmouse->pktcnt);
253 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
254 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
255 psmouse_notice(psmouse,
256 "issuing reconnect request\n");
257 serio_reconnect(psmouse->ps2dev.serio);
258 return -1;
261 psmouse->pktcnt = 0;
262 break;
264 case PSMOUSE_FULL_PACKET:
265 psmouse->pktcnt = 0;
266 if (psmouse->out_of_sync_cnt) {
267 psmouse->out_of_sync_cnt = 0;
268 psmouse_notice(psmouse,
269 "%s at %s - driver resynced.\n",
270 psmouse->name, psmouse->phys);
272 break;
274 case PSMOUSE_GOOD_DATA:
275 break;
277 return 0;
280 static void psmouse_handle_oob_data(struct psmouse *psmouse, u8 data)
282 switch (psmouse->oob_data_type) {
283 case PSMOUSE_OOB_NONE:
284 psmouse->oob_data_type = data;
285 break;
287 case PSMOUSE_OOB_EXTRA_BTNS:
288 psmouse_report_standard_buttons(psmouse->dev, data);
289 input_sync(psmouse->dev);
291 psmouse->extra_buttons = data;
292 psmouse->oob_data_type = PSMOUSE_OOB_NONE;
293 break;
295 default:
296 psmouse_warn(psmouse,
297 "unknown OOB_DATA type: 0x%02x\n",
298 psmouse->oob_data_type);
299 psmouse->oob_data_type = PSMOUSE_OOB_NONE;
300 break;
305 * psmouse_interrupt() handles incoming characters, either passing them
306 * for normal processing or gathering them as command response.
308 static irqreturn_t psmouse_interrupt(struct serio *serio,
309 unsigned char data, unsigned int flags)
311 struct psmouse *psmouse = serio_get_drvdata(serio);
313 if (psmouse->state == PSMOUSE_IGNORE)
314 goto out;
316 if (unlikely((flags & SERIO_TIMEOUT) ||
317 ((flags & SERIO_PARITY) &&
318 !psmouse->protocol->ignore_parity))) {
320 if (psmouse->state == PSMOUSE_ACTIVATED)
321 psmouse_warn(psmouse,
322 "bad data from KBC -%s%s\n",
323 flags & SERIO_TIMEOUT ? " timeout" : "",
324 flags & SERIO_PARITY ? " bad parity" : "");
325 ps2_cmd_aborted(&psmouse->ps2dev);
326 goto out;
329 if (flags & SERIO_OOB_DATA) {
330 psmouse_handle_oob_data(psmouse, data);
331 goto out;
334 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
335 if (ps2_handle_ack(&psmouse->ps2dev, data))
336 goto out;
338 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
339 if (ps2_handle_response(&psmouse->ps2dev, data))
340 goto out;
342 if (psmouse->state <= PSMOUSE_RESYNCING)
343 goto out;
345 if (psmouse->state == PSMOUSE_ACTIVATED &&
346 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
347 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
348 psmouse->name, psmouse->phys, psmouse->pktcnt);
349 psmouse->badbyte = psmouse->packet[0];
350 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
351 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
352 goto out;
355 psmouse->packet[psmouse->pktcnt++] = data;
357 /* Check if this is a new device announcement (0xAA 0x00) */
358 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
359 if (psmouse->pktcnt == 1) {
360 psmouse->last = jiffies;
361 goto out;
364 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
365 (psmouse->protocol->type == PSMOUSE_HGPK &&
366 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
367 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
368 serio_reconnect(serio);
369 goto out;
372 /* Not a new device, try processing first byte normally */
373 psmouse->pktcnt = 1;
374 if (psmouse_handle_byte(psmouse))
375 goto out;
377 psmouse->packet[psmouse->pktcnt++] = data;
381 * See if we need to force resync because mouse was idle for
382 * too long.
384 if (psmouse->state == PSMOUSE_ACTIVATED &&
385 psmouse->pktcnt == 1 && psmouse->resync_time &&
386 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
387 psmouse->badbyte = psmouse->packet[0];
388 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
389 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
390 goto out;
393 psmouse->last = jiffies;
394 psmouse_handle_byte(psmouse);
396 out:
397 return IRQ_HANDLED;
401 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
402 * using sliced syntax, understood by advanced devices, such as Logitech
403 * or Synaptics touchpads. The command is encoded as:
404 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
405 * is the command.
407 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
409 int i;
411 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
412 return -1;
414 for (i = 6; i >= 0; i -= 2) {
415 unsigned char d = (command >> i) & 3;
416 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
417 return -1;
420 return 0;
424 * psmouse_reset() resets the mouse into power-on state.
426 int psmouse_reset(struct psmouse *psmouse)
428 unsigned char param[2];
430 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
431 return -1;
433 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
434 return -1;
436 return 0;
440 * Here we set the mouse resolution.
442 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
444 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
445 unsigned char p;
447 if (resolution == 0 || resolution > 200)
448 resolution = 200;
450 p = params[resolution / 50];
451 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
452 psmouse->resolution = 25 << p;
456 * Here we set the mouse report rate.
458 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
460 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
461 unsigned char r;
462 int i = 0;
464 while (rates[i] > rate) i++;
465 r = rates[i];
466 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
467 psmouse->rate = r;
471 * Here we set the mouse scaling.
473 static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
475 ps2_command(&psmouse->ps2dev, NULL,
476 scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
477 PSMOUSE_CMD_SETSCALE11);
481 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
483 static int psmouse_poll(struct psmouse *psmouse)
485 return ps2_command(&psmouse->ps2dev, psmouse->packet,
486 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
489 static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
491 int i;
493 for (i = 0; ids[i]; i++)
494 if (!strcasecmp(id, ids[i]))
495 return true;
497 return false;
501 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
503 bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
505 struct serio *serio = psmouse->ps2dev.serio;
506 char *p, *fw_id_copy, *save_ptr;
507 bool found = false;
509 if (strncmp(serio->firmware_id, "PNP: ", 5))
510 return false;
512 fw_id_copy = kstrndup(&serio->firmware_id[5],
513 sizeof(serio->firmware_id) - 5,
514 GFP_KERNEL);
515 if (!fw_id_copy)
516 return false;
518 save_ptr = fw_id_copy;
519 while ((p = strsep(&fw_id_copy, " ")) != NULL) {
520 if (psmouse_check_pnp_id(p, ids)) {
521 found = true;
522 break;
526 kfree(save_ptr);
527 return found;
531 * Genius NetMouse magic init.
533 static int genius_detect(struct psmouse *psmouse, bool set_properties)
535 struct ps2dev *ps2dev = &psmouse->ps2dev;
536 unsigned char param[4];
538 param[0] = 3;
539 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
540 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
541 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
542 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
543 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
545 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
546 return -1;
548 if (set_properties) {
549 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
550 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
551 __set_bit(BTN_SIDE, psmouse->dev->keybit);
552 __set_bit(REL_WHEEL, psmouse->dev->relbit);
554 psmouse->vendor = "Genius";
555 psmouse->name = "Mouse";
556 psmouse->pktsize = 4;
559 return 0;
563 * IntelliMouse magic init.
565 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
567 struct ps2dev *ps2dev = &psmouse->ps2dev;
568 unsigned char param[2];
570 param[0] = 200;
571 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
572 param[0] = 100;
573 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
574 param[0] = 80;
575 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
576 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
578 if (param[0] != 3)
579 return -1;
581 if (set_properties) {
582 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
583 __set_bit(REL_WHEEL, psmouse->dev->relbit);
585 if (!psmouse->vendor)
586 psmouse->vendor = "Generic";
587 if (!psmouse->name)
588 psmouse->name = "Wheel Mouse";
589 psmouse->pktsize = 4;
592 return 0;
596 * Try IntelliMouse/Explorer magic init.
598 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
600 struct ps2dev *ps2dev = &psmouse->ps2dev;
601 unsigned char param[2];
603 intellimouse_detect(psmouse, 0);
605 param[0] = 200;
606 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
607 param[0] = 200;
608 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
609 param[0] = 80;
610 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
611 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
613 if (param[0] != 4)
614 return -1;
616 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
617 param[0] = 200;
618 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
619 param[0] = 80;
620 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
621 param[0] = 40;
622 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
624 if (set_properties) {
625 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
626 __set_bit(REL_WHEEL, psmouse->dev->relbit);
627 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
628 __set_bit(BTN_SIDE, psmouse->dev->keybit);
629 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
631 if (!psmouse->vendor)
632 psmouse->vendor = "Generic";
633 if (!psmouse->name)
634 psmouse->name = "Explorer Mouse";
635 psmouse->pktsize = 4;
638 return 0;
642 * Kensington ThinkingMouse / ExpertMouse magic init.
644 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
646 struct ps2dev *ps2dev = &psmouse->ps2dev;
647 unsigned char param[2];
648 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
649 int i;
651 param[0] = 10;
652 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
653 param[0] = 0;
654 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
655 for (i = 0; i < ARRAY_SIZE(seq); i++) {
656 param[0] = seq[i];
657 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
659 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
661 if (param[0] != 2)
662 return -1;
664 if (set_properties) {
665 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
666 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
668 psmouse->vendor = "Kensington";
669 psmouse->name = "ThinkingMouse";
672 return 0;
676 * Bare PS/2 protocol "detection". Always succeeds.
678 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
680 if (set_properties) {
681 if (!psmouse->vendor)
682 psmouse->vendor = "Generic";
683 if (!psmouse->name)
684 psmouse->name = "Mouse";
687 * We have no way of figuring true number of buttons so let's
688 * assume that the device has 3.
690 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
693 return 0;
697 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
698 * must be forced by sysfs protocol writing.
700 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
702 if (set_properties) {
703 psmouse->vendor = "Cortron";
704 psmouse->name = "PS/2 Trackball";
706 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
707 __set_bit(BTN_SIDE, psmouse->dev->keybit);
710 return 0;
713 static const struct psmouse_protocol psmouse_protocols[] = {
715 .type = PSMOUSE_PS2,
716 .name = "PS/2",
717 .alias = "bare",
718 .maxproto = true,
719 .ignore_parity = true,
720 .detect = ps2bare_detect,
721 .try_passthru = true,
723 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
725 .type = PSMOUSE_PS2PP,
726 .name = "PS2++",
727 .alias = "logitech",
728 .detect = ps2pp_detect,
730 #endif
732 .type = PSMOUSE_THINKPS,
733 .name = "ThinkPS/2",
734 .alias = "thinkps",
735 .detect = thinking_detect,
737 #ifdef CONFIG_MOUSE_PS2_CYPRESS
739 .type = PSMOUSE_CYPRESS,
740 .name = "CyPS/2",
741 .alias = "cypress",
742 .detect = cypress_detect,
743 .init = cypress_init,
745 #endif
747 .type = PSMOUSE_GENPS,
748 .name = "GenPS/2",
749 .alias = "genius",
750 .detect = genius_detect,
753 .type = PSMOUSE_IMPS,
754 .name = "ImPS/2",
755 .alias = "imps",
756 .maxproto = true,
757 .ignore_parity = true,
758 .detect = intellimouse_detect,
759 .try_passthru = true,
762 .type = PSMOUSE_IMEX,
763 .name = "ImExPS/2",
764 .alias = "exps",
765 .maxproto = true,
766 .ignore_parity = true,
767 .detect = im_explorer_detect,
768 .try_passthru = true,
770 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
772 .type = PSMOUSE_SYNAPTICS,
773 .name = "SynPS/2",
774 .alias = "synaptics",
775 .detect = synaptics_detect,
776 .init = synaptics_init_absolute,
779 .type = PSMOUSE_SYNAPTICS_RELATIVE,
780 .name = "SynRelPS/2",
781 .alias = "synaptics-relative",
782 .detect = synaptics_detect,
783 .init = synaptics_init_relative,
785 #endif
786 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS
788 .type = PSMOUSE_SYNAPTICS_SMBUS,
789 .name = "SynSMBus",
790 .alias = "synaptics-smbus",
791 .detect = synaptics_detect,
792 .init = synaptics_init_smbus,
793 .smbus_companion = true,
795 #endif
796 #ifdef CONFIG_MOUSE_PS2_ALPS
798 .type = PSMOUSE_ALPS,
799 .name = "AlpsPS/2",
800 .alias = "alps",
801 .detect = alps_detect,
802 .init = alps_init,
804 #endif
805 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
807 .type = PSMOUSE_LIFEBOOK,
808 .name = "LBPS/2",
809 .alias = "lifebook",
810 .detect = lifebook_detect,
811 .init = lifebook_init,
813 #endif
814 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
816 .type = PSMOUSE_TRACKPOINT,
817 .name = "TPPS/2",
818 .alias = "trackpoint",
819 .detect = trackpoint_detect,
820 .try_passthru = true,
822 #endif
823 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
825 .type = PSMOUSE_TOUCHKIT_PS2,
826 .name = "touchkitPS/2",
827 .alias = "touchkit",
828 .detect = touchkit_ps2_detect,
830 #endif
831 #ifdef CONFIG_MOUSE_PS2_OLPC
833 .type = PSMOUSE_HGPK,
834 .name = "OLPC HGPK",
835 .alias = "hgpk",
836 .detect = hgpk_detect,
838 #endif
839 #ifdef CONFIG_MOUSE_PS2_ELANTECH
841 .type = PSMOUSE_ELANTECH,
842 .name = "ETPS/2",
843 .alias = "elantech",
844 .detect = elantech_detect,
845 .init = elantech_init,
847 #endif
848 #ifdef CONFIG_MOUSE_PS2_SENTELIC
850 .type = PSMOUSE_FSP,
851 .name = "FSPPS/2",
852 .alias = "fsp",
853 .detect = fsp_detect,
854 .init = fsp_init,
856 #endif
858 .type = PSMOUSE_CORTRON,
859 .name = "CortronPS/2",
860 .alias = "cortps",
861 .detect = cortron_detect,
863 #ifdef CONFIG_MOUSE_PS2_FOCALTECH
865 .type = PSMOUSE_FOCALTECH,
866 .name = "FocalTechPS/2",
867 .alias = "focaltech",
868 .detect = focaltech_detect,
869 .init = focaltech_init,
871 #endif
872 #ifdef CONFIG_MOUSE_PS2_VMMOUSE
874 .type = PSMOUSE_VMMOUSE,
875 .name = VMMOUSE_PSNAME,
876 .alias = "vmmouse",
877 .detect = vmmouse_detect,
878 .init = vmmouse_init,
880 #endif
881 #ifdef CONFIG_MOUSE_PS2_BYD
883 .type = PSMOUSE_BYD,
884 .name = "BYDPS/2",
885 .alias = "byd",
886 .detect = byd_detect,
887 .init = byd_init,
889 #endif
891 .type = PSMOUSE_AUTO,
892 .name = "auto",
893 .alias = "any",
894 .maxproto = true,
898 static const struct psmouse_protocol *__psmouse_protocol_by_type(enum psmouse_type type)
900 int i;
902 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
903 if (psmouse_protocols[i].type == type)
904 return &psmouse_protocols[i];
906 return NULL;
909 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
911 const struct psmouse_protocol *proto;
913 proto = __psmouse_protocol_by_type(type);
914 if (proto)
915 return proto;
917 WARN_ON(1);
918 return &psmouse_protocols[0];
921 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
923 const struct psmouse_protocol *p;
924 int i;
926 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
927 p = &psmouse_protocols[i];
929 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
930 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
931 return &psmouse_protocols[i];
934 return NULL;
938 * Apply default settings to the psmouse structure. Most of them will
939 * be overridden by individual protocol initialization routines.
941 static void psmouse_apply_defaults(struct psmouse *psmouse)
943 struct input_dev *input_dev = psmouse->dev;
945 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
946 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
947 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
948 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
949 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
951 __set_bit(EV_KEY, input_dev->evbit);
952 __set_bit(EV_REL, input_dev->evbit);
954 __set_bit(BTN_LEFT, input_dev->keybit);
955 __set_bit(BTN_RIGHT, input_dev->keybit);
957 __set_bit(REL_X, input_dev->relbit);
958 __set_bit(REL_Y, input_dev->relbit);
960 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
962 psmouse->protocol = &psmouse_protocols[0];
964 psmouse->set_rate = psmouse_set_rate;
965 psmouse->set_resolution = psmouse_set_resolution;
966 psmouse->set_scale = psmouse_set_scale;
967 psmouse->poll = psmouse_poll;
968 psmouse->protocol_handler = psmouse_process_byte;
969 psmouse->pktsize = 3;
970 psmouse->reconnect = NULL;
971 psmouse->fast_reconnect = NULL;
972 psmouse->disconnect = NULL;
973 psmouse->cleanup = NULL;
974 psmouse->pt_activate = NULL;
975 psmouse->pt_deactivate = NULL;
978 static bool psmouse_do_detect(int (*detect)(struct psmouse *, bool),
979 struct psmouse *psmouse, bool allow_passthrough,
980 bool set_properties)
982 if (psmouse->ps2dev.serio->id.type == SERIO_PS_PSTHRU &&
983 !allow_passthrough) {
984 return false;
987 if (set_properties)
988 psmouse_apply_defaults(psmouse);
990 return detect(psmouse, set_properties) == 0;
993 static bool psmouse_try_protocol(struct psmouse *psmouse,
994 enum psmouse_type type,
995 unsigned int *max_proto,
996 bool set_properties, bool init_allowed)
998 const struct psmouse_protocol *proto;
1000 proto = __psmouse_protocol_by_type(type);
1001 if (!proto)
1002 return false;
1004 if (!psmouse_do_detect(proto->detect, psmouse, proto->try_passthru,
1005 set_properties))
1006 return false;
1008 if (set_properties && proto->init && init_allowed) {
1009 if (proto->init(psmouse) != 0) {
1011 * We detected device, but init failed. Adjust
1012 * max_proto so we only try standard protocols.
1014 if (*max_proto > PSMOUSE_IMEX)
1015 *max_proto = PSMOUSE_IMEX;
1017 return false;
1021 return true;
1025 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
1026 * the mouse may have.
1028 static int psmouse_extensions(struct psmouse *psmouse,
1029 unsigned int max_proto, bool set_properties)
1031 bool synaptics_hardware = false;
1032 int ret;
1035 * Always check for focaltech, this is safe as it uses pnp-id
1036 * matching.
1038 if (psmouse_do_detect(focaltech_detect,
1039 psmouse, false, set_properties)) {
1040 if (max_proto > PSMOUSE_IMEX &&
1041 IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH) &&
1042 (!set_properties || focaltech_init(psmouse) == 0)) {
1043 return PSMOUSE_FOCALTECH;
1046 * Restrict psmouse_max_proto so that psmouse_initialize()
1047 * does not try to reset rate and resolution, because even
1048 * that upsets the device.
1049 * This also causes us to basically fall through to basic
1050 * protocol detection, where we fully reset the mouse,
1051 * and set it up as bare PS/2 protocol device.
1053 psmouse_max_proto = max_proto = PSMOUSE_PS2;
1057 * We always check for LifeBook because it does not disturb mouse
1058 * (it only checks DMI information).
1060 if (psmouse_try_protocol(psmouse, PSMOUSE_LIFEBOOK, &max_proto,
1061 set_properties, max_proto > PSMOUSE_IMEX))
1062 return PSMOUSE_LIFEBOOK;
1064 if (psmouse_try_protocol(psmouse, PSMOUSE_VMMOUSE, &max_proto,
1065 set_properties, max_proto > PSMOUSE_IMEX))
1066 return PSMOUSE_VMMOUSE;
1069 * Try Kensington ThinkingMouse (we try first, because Synaptics
1070 * probe upsets the ThinkingMouse).
1072 if (max_proto > PSMOUSE_IMEX &&
1073 psmouse_try_protocol(psmouse, PSMOUSE_THINKPS, &max_proto,
1074 set_properties, true)) {
1075 return PSMOUSE_THINKPS;
1079 * Try Synaptics TouchPad. Note that probing is done even if
1080 * Synaptics protocol support is disabled in config - we need to
1081 * know if it is Synaptics so we can reset it properly after
1082 * probing for IntelliMouse.
1084 if (max_proto > PSMOUSE_PS2 &&
1085 psmouse_do_detect(synaptics_detect,
1086 psmouse, false, set_properties)) {
1087 synaptics_hardware = true;
1089 if (max_proto > PSMOUSE_IMEX) {
1091 * Try activating protocol, but check if support is
1092 * enabled first, since we try detecting Synaptics
1093 * even when protocol is disabled.
1095 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) ||
1096 IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)) {
1097 if (!set_properties)
1098 return PSMOUSE_SYNAPTICS;
1100 ret = synaptics_init(psmouse);
1101 if (ret >= 0)
1102 return ret;
1106 * Some Synaptics touchpads can emulate extended
1107 * protocols (like IMPS/2). Unfortunately
1108 * Logitech/Genius probes confuse some firmware
1109 * versions so we'll have to skip them.
1111 max_proto = PSMOUSE_IMEX;
1115 * Make sure that touchpad is in relative mode, gestures
1116 * (taps) are enabled.
1118 synaptics_reset(psmouse);
1122 * Try Cypress Trackpad. We must try it before Finger Sensing Pad
1123 * because Finger Sensing Pad probe upsets some modules of Cypress
1124 * Trackpads.
1126 if (max_proto > PSMOUSE_IMEX &&
1127 psmouse_try_protocol(psmouse, PSMOUSE_CYPRESS, &max_proto,
1128 set_properties, true)) {
1129 return PSMOUSE_CYPRESS;
1132 /* Try ALPS TouchPad */
1133 if (max_proto > PSMOUSE_IMEX) {
1134 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1135 if (psmouse_try_protocol(psmouse, PSMOUSE_ALPS,
1136 &max_proto, set_properties, true))
1137 return PSMOUSE_ALPS;
1140 /* Try OLPC HGPK touchpad */
1141 if (max_proto > PSMOUSE_IMEX &&
1142 psmouse_try_protocol(psmouse, PSMOUSE_HGPK, &max_proto,
1143 set_properties, true)) {
1144 return PSMOUSE_HGPK;
1147 /* Try Elantech touchpad */
1148 if (max_proto > PSMOUSE_IMEX &&
1149 psmouse_try_protocol(psmouse, PSMOUSE_ELANTECH,
1150 &max_proto, set_properties, true)) {
1151 return PSMOUSE_ELANTECH;
1154 if (max_proto > PSMOUSE_IMEX) {
1155 if (psmouse_try_protocol(psmouse, PSMOUSE_GENPS,
1156 &max_proto, set_properties, true))
1157 return PSMOUSE_GENPS;
1159 if (psmouse_try_protocol(psmouse, PSMOUSE_PS2PP,
1160 &max_proto, set_properties, true))
1161 return PSMOUSE_PS2PP;
1163 if (psmouse_try_protocol(psmouse, PSMOUSE_TRACKPOINT,
1164 &max_proto, set_properties, true))
1165 return PSMOUSE_TRACKPOINT;
1167 if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2,
1168 &max_proto, set_properties, true))
1169 return PSMOUSE_TOUCHKIT_PS2;
1173 * Try Finger Sensing Pad. We do it here because its probe upsets
1174 * Trackpoint devices (causing TP_READ_ID command to time out).
1176 if (max_proto > PSMOUSE_IMEX &&
1177 psmouse_try_protocol(psmouse, PSMOUSE_FSP,
1178 &max_proto, set_properties, true)) {
1179 return PSMOUSE_FSP;
1183 * Reset to defaults in case the device got confused by extended
1184 * protocol probes. Note that we follow up with full reset because
1185 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
1187 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1188 psmouse_reset(psmouse);
1190 if (max_proto >= PSMOUSE_IMEX &&
1191 psmouse_try_protocol(psmouse, PSMOUSE_IMEX,
1192 &max_proto, set_properties, true)) {
1193 return PSMOUSE_IMEX;
1196 if (max_proto >= PSMOUSE_IMPS &&
1197 psmouse_try_protocol(psmouse, PSMOUSE_IMPS,
1198 &max_proto, set_properties, true)) {
1199 return PSMOUSE_IMPS;
1203 * Okay, all failed, we have a standard mouse here. The number of
1204 * the buttons is still a question, though. We assume 3.
1206 psmouse_try_protocol(psmouse, PSMOUSE_PS2,
1207 &max_proto, set_properties, true);
1209 if (synaptics_hardware) {
1211 * We detected Synaptics hardware but it did not respond to
1212 * IMPS/2 probes. We need to reset the touchpad because if
1213 * there is a track point on the pass through port it could
1214 * get disabled while probing for protocol extensions.
1216 psmouse_reset(psmouse);
1219 return PSMOUSE_PS2;
1223 * psmouse_probe() probes for a PS/2 mouse.
1225 static int psmouse_probe(struct psmouse *psmouse)
1227 struct ps2dev *ps2dev = &psmouse->ps2dev;
1228 unsigned char param[2];
1231 * First, we check if it's a mouse. It should send 0x00 or 0x03 in
1232 * case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1233 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and
1234 * subsequent ID queries, probably due to a firmware bug.
1236 param[0] = 0xa5;
1237 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1238 return -1;
1240 if (param[0] != 0x00 && param[0] != 0x03 &&
1241 param[0] != 0x04 && param[0] != 0xff)
1242 return -1;
1245 * Then we reset and disable the mouse so that it doesn't generate
1246 * events.
1248 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
1249 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1250 ps2dev->serio->phys);
1252 return 0;
1256 * psmouse_initialize() initializes the mouse to a sane state.
1258 static void psmouse_initialize(struct psmouse *psmouse)
1261 * We set the mouse report rate, resolution and scaling.
1263 if (psmouse_max_proto != PSMOUSE_PS2) {
1264 psmouse->set_rate(psmouse, psmouse->rate);
1265 psmouse->set_resolution(psmouse, psmouse->resolution);
1266 psmouse->set_scale(psmouse, PSMOUSE_SCALE11);
1271 * psmouse_activate() enables the mouse so that we get motion reports from it.
1273 int psmouse_activate(struct psmouse *psmouse)
1275 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1276 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1277 psmouse->ps2dev.serio->phys);
1278 return -1;
1281 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1282 return 0;
1286 * psmouse_deactivate() puts the mouse into poll mode so that we don't get
1287 * motion reports from it unless we explicitly request it.
1289 int psmouse_deactivate(struct psmouse *psmouse)
1291 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
1292 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1293 psmouse->ps2dev.serio->phys);
1294 return -1;
1297 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1298 return 0;
1302 * psmouse_resync() attempts to re-validate current protocol.
1304 static void psmouse_resync(struct work_struct *work)
1306 struct psmouse *parent = NULL, *psmouse =
1307 container_of(work, struct psmouse, resync_work.work);
1308 struct serio *serio = psmouse->ps2dev.serio;
1309 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1310 bool failed = false, enabled = false;
1311 int i;
1313 mutex_lock(&psmouse_mutex);
1315 if (psmouse->state != PSMOUSE_RESYNCING)
1316 goto out;
1318 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1319 parent = serio_get_drvdata(serio->parent);
1320 psmouse_deactivate(parent);
1324 * Some mice don't ACK commands sent while they are in the middle of
1325 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1326 * instead of ps2_command() which would wait for 200ms for an ACK
1327 * that may never come.
1328 * As an additional quirk ALPS touchpads may not only forget to ACK
1329 * disable command but will stop reporting taps, so if we see that
1330 * mouse at least once ACKs disable we will do full reconnect if ACK
1331 * is missing.
1333 psmouse->num_resyncs++;
1335 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1336 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1337 failed = true;
1338 } else
1339 psmouse->acks_disable_command = true;
1342 * Poll the mouse. If it was reset the packet will be shorter than
1343 * psmouse->pktsize and ps2_command will fail. We do not expect and
1344 * do not handle scenario when mouse "upgrades" its protocol while
1345 * disconnected since it would require additional delay. If we ever
1346 * see a mouse that does it we'll adjust the code.
1348 if (!failed) {
1349 if (psmouse->poll(psmouse))
1350 failed = true;
1351 else {
1352 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1353 for (i = 0; i < psmouse->pktsize; i++) {
1354 psmouse->pktcnt++;
1355 rc = psmouse->protocol_handler(psmouse);
1356 if (rc != PSMOUSE_GOOD_DATA)
1357 break;
1359 if (rc != PSMOUSE_FULL_PACKET)
1360 failed = true;
1361 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1366 * Now try to enable mouse. We try to do that even if poll failed
1367 * and also repeat our attempts 5 times, otherwise we may be left
1368 * out with disabled mouse.
1370 for (i = 0; i < 5; i++) {
1371 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1372 enabled = true;
1373 break;
1375 msleep(200);
1378 if (!enabled) {
1379 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1380 psmouse->ps2dev.serio->phys);
1381 failed = true;
1384 if (failed) {
1385 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1386 psmouse_info(psmouse,
1387 "resync failed, issuing reconnect request\n");
1388 serio_reconnect(serio);
1389 } else
1390 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1392 if (parent)
1393 psmouse_activate(parent);
1394 out:
1395 mutex_unlock(&psmouse_mutex);
1399 * psmouse_cleanup() resets the mouse into power-on state.
1401 static void psmouse_cleanup(struct serio *serio)
1403 struct psmouse *psmouse = serio_get_drvdata(serio);
1404 struct psmouse *parent = NULL;
1406 mutex_lock(&psmouse_mutex);
1408 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1409 parent = serio_get_drvdata(serio->parent);
1410 psmouse_deactivate(parent);
1413 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1416 * Disable stream mode so cleanup routine can proceed undisturbed.
1418 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1419 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1420 psmouse->ps2dev.serio->phys);
1422 if (psmouse->cleanup)
1423 psmouse->cleanup(psmouse);
1426 * Reset the mouse to defaults (bare PS/2 protocol).
1428 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1431 * Some boxes, such as HP nx7400, get terribly confused if mouse
1432 * is not fully enabled before suspending/shutting down.
1434 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1436 if (parent) {
1437 if (parent->pt_deactivate)
1438 parent->pt_deactivate(parent);
1440 psmouse_activate(parent);
1443 mutex_unlock(&psmouse_mutex);
1447 * psmouse_disconnect() closes and frees.
1449 static void psmouse_disconnect(struct serio *serio)
1451 struct psmouse *psmouse = serio_get_drvdata(serio);
1452 struct psmouse *parent = NULL;
1454 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1456 mutex_lock(&psmouse_mutex);
1458 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1460 /* make sure we don't have a resync in progress */
1461 mutex_unlock(&psmouse_mutex);
1462 flush_workqueue(kpsmoused_wq);
1463 mutex_lock(&psmouse_mutex);
1465 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1466 parent = serio_get_drvdata(serio->parent);
1467 psmouse_deactivate(parent);
1470 if (psmouse->disconnect)
1471 psmouse->disconnect(psmouse);
1473 if (parent && parent->pt_deactivate)
1474 parent->pt_deactivate(parent);
1476 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1478 serio_close(serio);
1479 serio_set_drvdata(serio, NULL);
1481 if (psmouse->dev)
1482 input_unregister_device(psmouse->dev);
1484 kfree(psmouse);
1486 if (parent)
1487 psmouse_activate(parent);
1489 mutex_unlock(&psmouse_mutex);
1492 static int psmouse_switch_protocol(struct psmouse *psmouse,
1493 const struct psmouse_protocol *proto)
1495 const struct psmouse_protocol *selected_proto;
1496 struct input_dev *input_dev = psmouse->dev;
1497 enum psmouse_type type;
1499 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1501 if (proto && (proto->detect || proto->init)) {
1502 psmouse_apply_defaults(psmouse);
1504 if (proto->detect && proto->detect(psmouse, true) < 0)
1505 return -1;
1507 if (proto->init && proto->init(psmouse) < 0)
1508 return -1;
1510 selected_proto = proto;
1511 } else {
1512 type = psmouse_extensions(psmouse, psmouse_max_proto, true);
1513 selected_proto = psmouse_protocol_by_type(type);
1516 psmouse->protocol = selected_proto;
1519 * If mouse's packet size is 3 there is no point in polling the
1520 * device in hopes to detect protocol reset - we won't get less
1521 * than 3 bytes response anyhow.
1523 if (psmouse->pktsize == 3)
1524 psmouse->resync_time = 0;
1527 * Some smart KVMs fake response to POLL command returning just
1528 * 3 bytes and messing up our resync logic, so if initial poll
1529 * fails we won't try polling the device anymore. Hopefully
1530 * such KVM will maintain initially selected protocol.
1532 if (psmouse->resync_time && psmouse->poll(psmouse))
1533 psmouse->resync_time = 0;
1535 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1536 selected_proto->name, psmouse->vendor, psmouse->name);
1538 input_dev->name = psmouse->devname;
1539 input_dev->phys = psmouse->phys;
1540 input_dev->id.bustype = BUS_I8042;
1541 input_dev->id.vendor = 0x0002;
1542 input_dev->id.product = psmouse->protocol->type;
1543 input_dev->id.version = psmouse->model;
1545 return 0;
1549 * psmouse_connect() is a callback from the serio module when
1550 * an unhandled serio port is found.
1552 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1554 struct psmouse *psmouse, *parent = NULL;
1555 struct input_dev *input_dev;
1556 int retval = 0, error = -ENOMEM;
1558 mutex_lock(&psmouse_mutex);
1561 * If this is a pass-through port deactivate parent so the device
1562 * connected to this port can be successfully identified
1564 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1565 parent = serio_get_drvdata(serio->parent);
1566 psmouse_deactivate(parent);
1569 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1570 input_dev = input_allocate_device();
1571 if (!psmouse || !input_dev)
1572 goto err_free;
1574 ps2_init(&psmouse->ps2dev, serio);
1575 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1576 psmouse->dev = input_dev;
1577 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1579 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1581 serio_set_drvdata(serio, psmouse);
1583 error = serio_open(serio, drv);
1584 if (error)
1585 goto err_clear_drvdata;
1587 /* give PT device some time to settle down before probing */
1588 if (serio->id.type == SERIO_PS_PSTHRU)
1589 usleep_range(10000, 15000);
1591 if (psmouse_probe(psmouse) < 0) {
1592 error = -ENODEV;
1593 goto err_close_serio;
1596 psmouse->rate = psmouse_rate;
1597 psmouse->resolution = psmouse_resolution;
1598 psmouse->resetafter = psmouse_resetafter;
1599 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1600 psmouse->smartscroll = psmouse_smartscroll;
1602 psmouse_switch_protocol(psmouse, NULL);
1604 if (!psmouse->protocol->smbus_companion) {
1605 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1606 psmouse_initialize(psmouse);
1608 error = input_register_device(input_dev);
1609 if (error)
1610 goto err_protocol_disconnect;
1611 } else {
1612 /* Smbus companion will be reporting events, not us. */
1613 input_free_device(input_dev);
1614 psmouse->dev = input_dev = NULL;
1617 if (parent && parent->pt_activate)
1618 parent->pt_activate(parent);
1620 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1621 if (error)
1622 goto err_pt_deactivate;
1625 * PS/2 devices having SMBus companions should stay disabled
1626 * on PS/2 side, in order to have SMBus part operable.
1628 if (!psmouse->protocol->smbus_companion)
1629 psmouse_activate(psmouse);
1631 out:
1632 /* If this is a pass-through port the parent needs to be re-activated */
1633 if (parent)
1634 psmouse_activate(parent);
1636 mutex_unlock(&psmouse_mutex);
1637 return retval;
1639 err_pt_deactivate:
1640 if (parent && parent->pt_deactivate)
1641 parent->pt_deactivate(parent);
1642 if (input_dev) {
1643 input_unregister_device(input_dev);
1644 input_dev = NULL; /* so we don't try to free it below */
1646 err_protocol_disconnect:
1647 if (psmouse->disconnect)
1648 psmouse->disconnect(psmouse);
1649 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1650 err_close_serio:
1651 serio_close(serio);
1652 err_clear_drvdata:
1653 serio_set_drvdata(serio, NULL);
1654 err_free:
1655 input_free_device(input_dev);
1656 kfree(psmouse);
1658 retval = error;
1659 goto out;
1662 static int __psmouse_reconnect(struct serio *serio, bool fast_reconnect)
1664 struct psmouse *psmouse = serio_get_drvdata(serio);
1665 struct psmouse *parent = NULL;
1666 int (*reconnect_handler)(struct psmouse *);
1667 enum psmouse_type type;
1668 int rc = -1;
1670 mutex_lock(&psmouse_mutex);
1672 if (fast_reconnect) {
1673 reconnect_handler = psmouse->fast_reconnect;
1674 if (!reconnect_handler) {
1675 rc = -ENOENT;
1676 goto out_unlock;
1678 } else {
1679 reconnect_handler = psmouse->reconnect;
1682 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1683 parent = serio_get_drvdata(serio->parent);
1684 psmouse_deactivate(parent);
1687 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1689 if (reconnect_handler) {
1690 if (reconnect_handler(psmouse))
1691 goto out;
1692 } else {
1693 psmouse_reset(psmouse);
1695 if (psmouse_probe(psmouse) < 0)
1696 goto out;
1698 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1699 if (psmouse->protocol->type != type)
1700 goto out;
1704 * OK, the device type (and capabilities) match the old one,
1705 * we can continue using it, complete initialization
1707 if (!psmouse->protocol->smbus_companion) {
1708 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1709 psmouse_initialize(psmouse);
1712 if (parent && parent->pt_activate)
1713 parent->pt_activate(parent);
1716 * PS/2 devices having SMBus companions should stay disabled
1717 * on PS/2 side, in order to have SMBus part operable.
1719 if (!psmouse->protocol->smbus_companion)
1720 psmouse_activate(psmouse);
1722 rc = 0;
1724 out:
1725 /* If this is a pass-through port the parent waits to be activated */
1726 if (parent)
1727 psmouse_activate(parent);
1729 out_unlock:
1730 mutex_unlock(&psmouse_mutex);
1731 return rc;
1734 static int psmouse_reconnect(struct serio *serio)
1736 return __psmouse_reconnect(serio, false);
1739 static int psmouse_fast_reconnect(struct serio *serio)
1741 return __psmouse_reconnect(serio, true);
1744 static struct serio_device_id psmouse_serio_ids[] = {
1746 .type = SERIO_8042,
1747 .proto = SERIO_ANY,
1748 .id = SERIO_ANY,
1749 .extra = SERIO_ANY,
1752 .type = SERIO_PS_PSTHRU,
1753 .proto = SERIO_ANY,
1754 .id = SERIO_ANY,
1755 .extra = SERIO_ANY,
1757 { 0 }
1760 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1762 static struct serio_driver psmouse_drv = {
1763 .driver = {
1764 .name = "psmouse",
1766 .description = DRIVER_DESC,
1767 .id_table = psmouse_serio_ids,
1768 .interrupt = psmouse_interrupt,
1769 .connect = psmouse_connect,
1770 .reconnect = psmouse_reconnect,
1771 .fast_reconnect = psmouse_fast_reconnect,
1772 .disconnect = psmouse_disconnect,
1773 .cleanup = psmouse_cleanup,
1776 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1777 char *buf)
1779 struct serio *serio = to_serio_port(dev);
1780 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1781 struct psmouse *psmouse = serio_get_drvdata(serio);
1783 if (psmouse->protocol->smbus_companion &&
1784 devattr != &psmouse_attr_protocol.dattr)
1785 return -ENOENT;
1787 return attr->show(psmouse, attr->data, buf);
1790 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1791 const char *buf, size_t count)
1793 struct serio *serio = to_serio_port(dev);
1794 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1795 struct psmouse *psmouse, *parent = NULL;
1796 int retval;
1798 retval = mutex_lock_interruptible(&psmouse_mutex);
1799 if (retval)
1800 goto out;
1802 psmouse = serio_get_drvdata(serio);
1804 if (psmouse->protocol->smbus_companion &&
1805 devattr != &psmouse_attr_protocol.dattr) {
1806 retval = -ENOENT;
1807 goto out_unlock;
1810 if (attr->protect) {
1811 if (psmouse->state == PSMOUSE_IGNORE) {
1812 retval = -ENODEV;
1813 goto out_unlock;
1816 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1817 parent = serio_get_drvdata(serio->parent);
1818 psmouse_deactivate(parent);
1821 if (!psmouse->protocol->smbus_companion)
1822 psmouse_deactivate(psmouse);
1825 retval = attr->set(psmouse, attr->data, buf, count);
1827 if (attr->protect) {
1828 if (retval != -ENODEV && !psmouse->protocol->smbus_companion)
1829 psmouse_activate(psmouse);
1831 if (parent)
1832 psmouse_activate(parent);
1835 out_unlock:
1836 mutex_unlock(&psmouse_mutex);
1837 out:
1838 return retval;
1841 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1843 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1845 return sprintf(buf, "%u\n", *field);
1848 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1850 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1851 unsigned int value;
1852 int err;
1854 err = kstrtouint(buf, 10, &value);
1855 if (err)
1856 return err;
1858 *field = value;
1860 return count;
1863 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1865 return sprintf(buf, "%s\n", psmouse->protocol->name);
1868 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1870 struct serio *serio = psmouse->ps2dev.serio;
1871 struct psmouse *parent = NULL;
1872 struct input_dev *old_dev, *new_dev;
1873 const struct psmouse_protocol *proto, *old_proto;
1874 int error;
1875 int retry = 0;
1877 proto = psmouse_protocol_by_name(buf, count);
1878 if (!proto)
1879 return -EINVAL;
1881 if (psmouse->protocol == proto)
1882 return count;
1884 new_dev = input_allocate_device();
1885 if (!new_dev)
1886 return -ENOMEM;
1888 while (!list_empty(&serio->children)) {
1889 if (++retry > 3) {
1890 psmouse_warn(psmouse,
1891 "failed to destroy children ports, protocol change aborted.\n");
1892 input_free_device(new_dev);
1893 return -EIO;
1896 mutex_unlock(&psmouse_mutex);
1897 serio_unregister_child_port(serio);
1898 mutex_lock(&psmouse_mutex);
1900 if (serio->drv != &psmouse_drv) {
1901 input_free_device(new_dev);
1902 return -ENODEV;
1905 if (psmouse->protocol == proto) {
1906 input_free_device(new_dev);
1907 return count; /* switched by other thread */
1911 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1912 parent = serio_get_drvdata(serio->parent);
1913 if (parent->pt_deactivate)
1914 parent->pt_deactivate(parent);
1917 old_dev = psmouse->dev;
1918 old_proto = psmouse->protocol;
1920 if (psmouse->disconnect)
1921 psmouse->disconnect(psmouse);
1923 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1925 psmouse->dev = new_dev;
1926 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1928 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1929 psmouse_reset(psmouse);
1930 /* default to PSMOUSE_PS2 */
1931 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1934 psmouse_initialize(psmouse);
1935 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1937 if (psmouse->protocol->smbus_companion) {
1938 input_free_device(psmouse->dev);
1939 psmouse->dev = NULL;
1940 } else {
1941 error = input_register_device(psmouse->dev);
1942 if (error) {
1943 if (psmouse->disconnect)
1944 psmouse->disconnect(psmouse);
1946 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1947 input_free_device(new_dev);
1948 psmouse->dev = old_dev;
1949 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1950 psmouse_switch_protocol(psmouse, old_proto);
1951 psmouse_initialize(psmouse);
1952 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1954 return error;
1958 if (old_dev)
1959 input_unregister_device(old_dev);
1961 if (parent && parent->pt_activate)
1962 parent->pt_activate(parent);
1964 return count;
1967 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1969 unsigned int value;
1970 int err;
1972 err = kstrtouint(buf, 10, &value);
1973 if (err)
1974 return err;
1976 psmouse->set_rate(psmouse, value);
1977 return count;
1980 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1982 unsigned int value;
1983 int err;
1985 err = kstrtouint(buf, 10, &value);
1986 if (err)
1987 return err;
1989 psmouse->set_resolution(psmouse, value);
1990 return count;
1994 static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
1996 const struct psmouse_protocol *proto;
1998 if (!val)
1999 return -EINVAL;
2001 proto = psmouse_protocol_by_name(val, strlen(val));
2003 if (!proto || !proto->maxproto)
2004 return -EINVAL;
2006 *((unsigned int *)kp->arg) = proto->type;
2008 return 0;
2011 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
2013 int type = *((unsigned int *)kp->arg);
2015 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
2018 static int __init psmouse_init(void)
2020 int err;
2022 lifebook_module_init();
2023 synaptics_module_init();
2024 hgpk_module_init();
2026 err = psmouse_smbus_module_init();
2027 if (err)
2028 return err;
2030 kpsmoused_wq = alloc_ordered_workqueue("kpsmoused", 0);
2031 if (!kpsmoused_wq) {
2032 pr_err("failed to create kpsmoused workqueue\n");
2033 err = -ENOMEM;
2034 goto err_smbus_exit;
2037 err = serio_register_driver(&psmouse_drv);
2038 if (err)
2039 goto err_destroy_wq;
2041 return 0;
2043 err_destroy_wq:
2044 destroy_workqueue(kpsmoused_wq);
2045 err_smbus_exit:
2046 psmouse_smbus_module_exit();
2047 return err;
2050 static void __exit psmouse_exit(void)
2052 serio_unregister_driver(&psmouse_drv);
2053 destroy_workqueue(kpsmoused_wq);
2054 psmouse_smbus_module_exit();
2057 module_init(psmouse_init);
2058 module_exit(psmouse_exit);