i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / input / mouse / psmouse-base.c
blobe6c9931f02c73176483105b494feab56b19ddacd
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"
38 #define DRIVER_DESC "PS/2 mouse driver"
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
42 MODULE_LICENSE("GPL");
44 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
45 static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
46 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
47 static struct kernel_param_ops param_ops_proto_abbrev = {
48 .set = psmouse_set_maxproto,
49 .get = psmouse_get_maxproto,
51 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
52 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
53 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
55 static unsigned int psmouse_resolution = 200;
56 module_param_named(resolution, psmouse_resolution, uint, 0644);
57 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
59 static unsigned int psmouse_rate = 100;
60 module_param_named(rate, psmouse_rate, uint, 0644);
61 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
63 static bool psmouse_smartscroll = 1;
64 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
65 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
67 static unsigned int psmouse_resetafter = 5;
68 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
69 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
71 static unsigned int psmouse_resync_time;
72 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
73 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
75 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
76 NULL,
77 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
78 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
79 (void *) offsetof(struct psmouse, rate),
80 psmouse_show_int_attr, psmouse_attr_set_rate);
81 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, resolution),
83 psmouse_show_int_attr, psmouse_attr_set_resolution);
84 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
85 (void *) offsetof(struct psmouse, resetafter),
86 psmouse_show_int_attr, psmouse_set_int_attr);
87 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
88 (void *) offsetof(struct psmouse, resync_time),
89 psmouse_show_int_attr, psmouse_set_int_attr);
91 static struct attribute *psmouse_attributes[] = {
92 &psmouse_attr_protocol.dattr.attr,
93 &psmouse_attr_rate.dattr.attr,
94 &psmouse_attr_resolution.dattr.attr,
95 &psmouse_attr_resetafter.dattr.attr,
96 &psmouse_attr_resync_time.dattr.attr,
97 NULL
100 static struct attribute_group psmouse_attribute_group = {
101 .attrs = psmouse_attributes,
105 * psmouse_mutex protects all operations changing state of mouse
106 * (connecting, disconnecting, changing rate or resolution via
107 * sysfs). We could use a per-device semaphore but since there
108 * rarely more than one PS/2 mouse connected and since semaphore
109 * is taken in "slow" paths it is not worth it.
111 static DEFINE_MUTEX(psmouse_mutex);
113 static struct workqueue_struct *kpsmoused_wq;
115 struct psmouse_protocol {
116 enum psmouse_type type;
117 bool maxproto;
118 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
119 const char *name;
120 const char *alias;
121 int (*detect)(struct psmouse *, bool);
122 int (*init)(struct psmouse *);
126 * psmouse_process_byte() analyzes the PS/2 data stream and reports
127 * 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;
139 * Full packet accumulated, process it
143 * Scroll wheel on IntelliMice, scroll buttons on NetMice
146 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
147 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
150 * Scroll wheel and buttons on IntelliMouse Explorer
153 if (psmouse->type == PSMOUSE_IMEX) {
154 switch (packet[3] & 0xC0) {
155 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
156 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
157 break;
158 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
159 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
160 break;
161 case 0x00:
162 case 0xC0:
163 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
164 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
165 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
166 break;
171 * Extra buttons on Genius NewNet 3D
174 if (psmouse->type == PSMOUSE_GENPS) {
175 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
176 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
180 * Extra button on ThinkingMouse
182 if (psmouse->type == PSMOUSE_THINKPS) {
183 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
184 /* Without this bit of weirdness moving up gives wildly high Y changes. */
185 packet[1] |= (packet[0] & 0x40) << 1;
189 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
190 * byte.
192 if (psmouse->type == PSMOUSE_CORTRON) {
193 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
194 packet[0] |= 0x08;
198 * Generic PS/2 Mouse
201 input_report_key(dev, BTN_LEFT, packet[0] & 1);
202 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
203 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
205 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
206 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
208 input_sync(dev);
210 return PSMOUSE_FULL_PACKET;
213 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
214 unsigned long delay)
216 queue_delayed_work(kpsmoused_wq, work, delay);
220 * __psmouse_set_state() sets new psmouse state and resets all flags.
223 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
225 psmouse->state = new_state;
226 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
227 psmouse->ps2dev.flags = 0;
228 psmouse->last = jiffies;
233 * psmouse_set_state() sets new psmouse state and resets all flags and
234 * counters while holding serio lock so fighting with interrupt handler
235 * is not a concern.
238 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
240 serio_pause_rx(psmouse->ps2dev.serio);
241 __psmouse_set_state(psmouse, new_state);
242 serio_continue_rx(psmouse->ps2dev.serio);
246 * psmouse_handle_byte() processes one byte of the input data stream
247 * by calling corresponding protocol handler.
250 static int psmouse_handle_byte(struct psmouse *psmouse)
252 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
254 switch (rc) {
255 case PSMOUSE_BAD_DATA:
256 if (psmouse->state == PSMOUSE_ACTIVATED) {
257 psmouse_warn(psmouse,
258 "%s at %s lost sync at byte %d\n",
259 psmouse->name, psmouse->phys,
260 psmouse->pktcnt);
261 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
262 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
263 psmouse_notice(psmouse,
264 "issuing reconnect request\n");
265 serio_reconnect(psmouse->ps2dev.serio);
266 return -1;
269 psmouse->pktcnt = 0;
270 break;
272 case PSMOUSE_FULL_PACKET:
273 psmouse->pktcnt = 0;
274 if (psmouse->out_of_sync_cnt) {
275 psmouse->out_of_sync_cnt = 0;
276 psmouse_notice(psmouse,
277 "%s at %s - driver resynced.\n",
278 psmouse->name, psmouse->phys);
280 break;
282 case PSMOUSE_GOOD_DATA:
283 break;
285 return 0;
289 * psmouse_interrupt() handles incoming characters, either passing them
290 * for normal processing or gathering them as command response.
293 static irqreturn_t psmouse_interrupt(struct serio *serio,
294 unsigned char data, unsigned int flags)
296 struct psmouse *psmouse = serio_get_drvdata(serio);
298 if (psmouse->state == PSMOUSE_IGNORE)
299 goto out;
301 if (unlikely((flags & SERIO_TIMEOUT) ||
302 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
304 if (psmouse->state == PSMOUSE_ACTIVATED)
305 psmouse_warn(psmouse,
306 "bad data from KBC -%s%s\n",
307 flags & SERIO_TIMEOUT ? " timeout" : "",
308 flags & SERIO_PARITY ? " bad parity" : "");
309 ps2_cmd_aborted(&psmouse->ps2dev);
310 goto out;
313 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
314 if (ps2_handle_ack(&psmouse->ps2dev, data))
315 goto out;
317 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
318 if (ps2_handle_response(&psmouse->ps2dev, data))
319 goto out;
321 if (psmouse->state <= PSMOUSE_RESYNCING)
322 goto out;
324 if (psmouse->state == PSMOUSE_ACTIVATED &&
325 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
326 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
327 psmouse->name, psmouse->phys, psmouse->pktcnt);
328 psmouse->badbyte = psmouse->packet[0];
329 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
330 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
331 goto out;
334 psmouse->packet[psmouse->pktcnt++] = data;
336 * Check if this is a new device announcement (0xAA 0x00)
338 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
339 if (psmouse->pktcnt == 1) {
340 psmouse->last = jiffies;
341 goto out;
344 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
345 (psmouse->type == PSMOUSE_HGPK &&
346 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
347 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
348 serio_reconnect(serio);
349 goto out;
352 * Not a new device, try processing first byte normally
354 psmouse->pktcnt = 1;
355 if (psmouse_handle_byte(psmouse))
356 goto out;
358 psmouse->packet[psmouse->pktcnt++] = data;
362 * See if we need to force resync because mouse was idle for too long
364 if (psmouse->state == PSMOUSE_ACTIVATED &&
365 psmouse->pktcnt == 1 && psmouse->resync_time &&
366 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
367 psmouse->badbyte = psmouse->packet[0];
368 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
369 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
370 goto out;
373 psmouse->last = jiffies;
374 psmouse_handle_byte(psmouse);
376 out:
377 return IRQ_HANDLED;
382 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
383 * using sliced syntax, understood by advanced devices, such as Logitech
384 * or Synaptics touchpads. The command is encoded as:
385 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
386 * is the command.
388 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
390 int i;
392 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
393 return -1;
395 for (i = 6; i >= 0; i -= 2) {
396 unsigned char d = (command >> i) & 3;
397 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
398 return -1;
401 return 0;
406 * psmouse_reset() resets the mouse into power-on state.
408 int psmouse_reset(struct psmouse *psmouse)
410 unsigned char param[2];
412 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
413 return -1;
415 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
416 return -1;
418 return 0;
422 * Here we set the mouse resolution.
425 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
427 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
428 unsigned char p;
430 if (resolution == 0 || resolution > 200)
431 resolution = 200;
433 p = params[resolution / 50];
434 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
435 psmouse->resolution = 25 << p;
439 * Here we set the mouse report rate.
442 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
444 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
445 unsigned char r;
446 int i = 0;
448 while (rates[i] > rate) i++;
449 r = rates[i];
450 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
451 psmouse->rate = r;
455 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
458 static int psmouse_poll(struct psmouse *psmouse)
460 return ps2_command(&psmouse->ps2dev, psmouse->packet,
461 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
466 * Genius NetMouse magic init.
468 static int genius_detect(struct psmouse *psmouse, bool set_properties)
470 struct ps2dev *ps2dev = &psmouse->ps2dev;
471 unsigned char param[4];
473 param[0] = 3;
474 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
475 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
476 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
477 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
478 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
480 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
481 return -1;
483 if (set_properties) {
484 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
485 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
486 __set_bit(BTN_SIDE, psmouse->dev->keybit);
487 __set_bit(REL_WHEEL, psmouse->dev->relbit);
489 psmouse->vendor = "Genius";
490 psmouse->name = "Mouse";
491 psmouse->pktsize = 4;
494 return 0;
498 * IntelliMouse magic init.
500 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
502 struct ps2dev *ps2dev = &psmouse->ps2dev;
503 unsigned char param[2];
505 param[0] = 200;
506 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
507 param[0] = 100;
508 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
509 param[0] = 80;
510 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
511 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
513 if (param[0] != 3)
514 return -1;
516 if (set_properties) {
517 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
518 __set_bit(REL_WHEEL, psmouse->dev->relbit);
520 if (!psmouse->vendor)
521 psmouse->vendor = "Generic";
522 if (!psmouse->name)
523 psmouse->name = "Wheel Mouse";
524 psmouse->pktsize = 4;
527 return 0;
531 * Try IntelliMouse/Explorer magic init.
533 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
535 struct ps2dev *ps2dev = &psmouse->ps2dev;
536 unsigned char param[2];
538 intellimouse_detect(psmouse, 0);
540 param[0] = 200;
541 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
542 param[0] = 200;
543 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
544 param[0] = 80;
545 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
546 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
548 if (param[0] != 4)
549 return -1;
551 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
552 param[0] = 200;
553 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
554 param[0] = 80;
555 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
556 param[0] = 40;
557 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
559 if (set_properties) {
560 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
561 __set_bit(REL_WHEEL, psmouse->dev->relbit);
562 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
563 __set_bit(BTN_SIDE, psmouse->dev->keybit);
564 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
566 if (!psmouse->vendor)
567 psmouse->vendor = "Generic";
568 if (!psmouse->name)
569 psmouse->name = "Explorer Mouse";
570 psmouse->pktsize = 4;
573 return 0;
577 * Kensington ThinkingMouse / ExpertMouse magic init.
579 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
581 struct ps2dev *ps2dev = &psmouse->ps2dev;
582 unsigned char param[2];
583 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
584 int i;
586 param[0] = 10;
587 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
588 param[0] = 0;
589 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
590 for (i = 0; i < ARRAY_SIZE(seq); i++) {
591 param[0] = seq[i];
592 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
594 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
596 if (param[0] != 2)
597 return -1;
599 if (set_properties) {
600 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
601 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
603 psmouse->vendor = "Kensington";
604 psmouse->name = "ThinkingMouse";
607 return 0;
611 * Bare PS/2 protocol "detection". Always succeeds.
613 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
615 if (set_properties) {
616 if (!psmouse->vendor)
617 psmouse->vendor = "Generic";
618 if (!psmouse->name)
619 psmouse->name = "Mouse";
622 * We have no way of figuring true number of buttons so let's
623 * assume that the device has 3.
625 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
628 return 0;
632 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
633 * must be forced by sysfs protocol writing.
635 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
637 if (set_properties) {
638 psmouse->vendor = "Cortron";
639 psmouse->name = "PS/2 Trackball";
641 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
642 __set_bit(BTN_SIDE, psmouse->dev->keybit);
645 return 0;
649 * Apply default settings to the psmouse structure. Most of them will
650 * be overridden by individual protocol initialization routines.
653 static void psmouse_apply_defaults(struct psmouse *psmouse)
655 struct input_dev *input_dev = psmouse->dev;
657 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
658 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
659 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
660 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
661 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
663 __set_bit(EV_KEY, input_dev->evbit);
664 __set_bit(EV_REL, input_dev->evbit);
666 __set_bit(BTN_LEFT, input_dev->keybit);
667 __set_bit(BTN_RIGHT, input_dev->keybit);
669 __set_bit(REL_X, input_dev->relbit);
670 __set_bit(REL_Y, input_dev->relbit);
672 psmouse->set_rate = psmouse_set_rate;
673 psmouse->set_resolution = psmouse_set_resolution;
674 psmouse->poll = psmouse_poll;
675 psmouse->protocol_handler = psmouse_process_byte;
676 psmouse->pktsize = 3;
677 psmouse->reconnect = NULL;
678 psmouse->disconnect = NULL;
679 psmouse->cleanup = NULL;
680 psmouse->pt_activate = NULL;
681 psmouse->pt_deactivate = NULL;
685 * Apply default settings to the psmouse structure and call specified
686 * protocol detection or initialization routine.
688 static int psmouse_do_detect(int (*detect)(struct psmouse *psmouse,
689 bool set_properties),
690 struct psmouse *psmouse, bool set_properties)
692 if (set_properties)
693 psmouse_apply_defaults(psmouse);
695 return detect(psmouse, set_properties);
699 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
700 * the mouse may have.
703 static int psmouse_extensions(struct psmouse *psmouse,
704 unsigned int max_proto, bool set_properties)
706 bool synaptics_hardware = false;
709 * We always check for lifebook because it does not disturb mouse
710 * (it only checks DMI information).
712 if (psmouse_do_detect(lifebook_detect, psmouse, set_properties) == 0) {
713 if (max_proto > PSMOUSE_IMEX) {
714 if (!set_properties || lifebook_init(psmouse) == 0)
715 return PSMOUSE_LIFEBOOK;
720 * Try Kensington ThinkingMouse (we try first, because synaptics probe
721 * upsets the thinkingmouse).
724 if (max_proto > PSMOUSE_IMEX &&
725 psmouse_do_detect(thinking_detect, psmouse, set_properties) == 0) {
726 return PSMOUSE_THINKPS;
730 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
731 * support is disabled in config - we need to know if it is synaptics so we
732 * can reset it properly after probing for intellimouse.
734 if (max_proto > PSMOUSE_PS2 &&
735 psmouse_do_detect(synaptics_detect, psmouse, set_properties) == 0) {
736 synaptics_hardware = true;
738 if (max_proto > PSMOUSE_IMEX) {
740 * Try activating protocol, but check if support is enabled first, since
741 * we try detecting Synaptics even when protocol is disabled.
743 if (synaptics_supported() &&
744 (!set_properties || synaptics_init(psmouse) == 0)) {
745 return PSMOUSE_SYNAPTICS;
749 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
750 * Unfortunately Logitech/Genius probes confuse some firmware versions so
751 * we'll have to skip them.
753 max_proto = PSMOUSE_IMEX;
756 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
758 synaptics_reset(psmouse);
762 * Try ALPS TouchPad
764 if (max_proto > PSMOUSE_IMEX) {
765 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
766 if (psmouse_do_detect(alps_detect,
767 psmouse, set_properties) == 0) {
768 if (!set_properties || alps_init(psmouse) == 0)
769 return PSMOUSE_ALPS;
771 * Init failed, try basic relative protocols
773 max_proto = PSMOUSE_IMEX;
778 * Try OLPC HGPK touchpad.
780 if (max_proto > PSMOUSE_IMEX &&
781 psmouse_do_detect(hgpk_detect, psmouse, set_properties) == 0) {
782 if (!set_properties || hgpk_init(psmouse) == 0)
783 return PSMOUSE_HGPK;
785 * Init failed, try basic relative protocols
787 max_proto = PSMOUSE_IMEX;
791 * Try Elantech touchpad.
793 if (max_proto > PSMOUSE_IMEX &&
794 psmouse_do_detect(elantech_detect, psmouse, set_properties) == 0) {
795 if (!set_properties || elantech_init(psmouse) == 0)
796 return PSMOUSE_ELANTECH;
798 * Init failed, try basic relative protocols
800 max_proto = PSMOUSE_IMEX;
803 if (max_proto > PSMOUSE_IMEX) {
804 if (psmouse_do_detect(genius_detect,
805 psmouse, set_properties) == 0)
806 return PSMOUSE_GENPS;
808 if (psmouse_do_detect(ps2pp_init,
809 psmouse, set_properties) == 0)
810 return PSMOUSE_PS2PP;
812 if (psmouse_do_detect(trackpoint_detect,
813 psmouse, set_properties) == 0)
814 return PSMOUSE_TRACKPOINT;
816 if (psmouse_do_detect(touchkit_ps2_detect,
817 psmouse, set_properties) == 0)
818 return PSMOUSE_TOUCHKIT_PS2;
822 * Try Finger Sensing Pad. We do it here because its probe upsets
823 * Trackpoint devices (causing TP_READ_ID command to time out).
825 if (max_proto > PSMOUSE_IMEX) {
826 if (psmouse_do_detect(fsp_detect,
827 psmouse, set_properties) == 0) {
828 if (!set_properties || fsp_init(psmouse) == 0)
829 return PSMOUSE_FSP;
831 * Init failed, try basic relative protocols
833 max_proto = PSMOUSE_IMEX;
838 * Reset to defaults in case the device got confused by extended
839 * protocol probes. Note that we follow up with full reset because
840 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
842 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
843 psmouse_reset(psmouse);
845 if (max_proto >= PSMOUSE_IMEX &&
846 psmouse_do_detect(im_explorer_detect,
847 psmouse, set_properties) == 0) {
848 return PSMOUSE_IMEX;
851 if (max_proto >= PSMOUSE_IMPS &&
852 psmouse_do_detect(intellimouse_detect,
853 psmouse, set_properties) == 0) {
854 return PSMOUSE_IMPS;
858 * Okay, all failed, we have a standard mouse here. The number of the buttons
859 * is still a question, though. We assume 3.
861 psmouse_do_detect(ps2bare_detect, psmouse, set_properties);
863 if (synaptics_hardware) {
865 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
866 * We need to reset the touchpad because if there is a track point on the
867 * pass through port it could get disabled while probing for protocol
868 * extensions.
870 psmouse_reset(psmouse);
873 return PSMOUSE_PS2;
876 static const struct psmouse_protocol psmouse_protocols[] = {
878 .type = PSMOUSE_PS2,
879 .name = "PS/2",
880 .alias = "bare",
881 .maxproto = true,
882 .ignore_parity = true,
883 .detect = ps2bare_detect,
885 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
887 .type = PSMOUSE_PS2PP,
888 .name = "PS2++",
889 .alias = "logitech",
890 .detect = ps2pp_init,
892 #endif
894 .type = PSMOUSE_THINKPS,
895 .name = "ThinkPS/2",
896 .alias = "thinkps",
897 .detect = thinking_detect,
900 .type = PSMOUSE_GENPS,
901 .name = "GenPS/2",
902 .alias = "genius",
903 .detect = genius_detect,
906 .type = PSMOUSE_IMPS,
907 .name = "ImPS/2",
908 .alias = "imps",
909 .maxproto = true,
910 .ignore_parity = true,
911 .detect = intellimouse_detect,
914 .type = PSMOUSE_IMEX,
915 .name = "ImExPS/2",
916 .alias = "exps",
917 .maxproto = true,
918 .ignore_parity = true,
919 .detect = im_explorer_detect,
921 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
923 .type = PSMOUSE_SYNAPTICS,
924 .name = "SynPS/2",
925 .alias = "synaptics",
926 .detect = synaptics_detect,
927 .init = synaptics_init,
930 .type = PSMOUSE_SYNAPTICS_RELATIVE,
931 .name = "SynRelPS/2",
932 .alias = "synaptics-relative",
933 .detect = synaptics_detect,
934 .init = synaptics_init_relative,
936 #endif
937 #ifdef CONFIG_MOUSE_PS2_ALPS
939 .type = PSMOUSE_ALPS,
940 .name = "AlpsPS/2",
941 .alias = "alps",
942 .detect = alps_detect,
943 .init = alps_init,
945 #endif
946 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
948 .type = PSMOUSE_LIFEBOOK,
949 .name = "LBPS/2",
950 .alias = "lifebook",
951 .init = lifebook_init,
953 #endif
954 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
956 .type = PSMOUSE_TRACKPOINT,
957 .name = "TPPS/2",
958 .alias = "trackpoint",
959 .detect = trackpoint_detect,
961 #endif
962 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
964 .type = PSMOUSE_TOUCHKIT_PS2,
965 .name = "touchkitPS/2",
966 .alias = "touchkit",
967 .detect = touchkit_ps2_detect,
969 #endif
970 #ifdef CONFIG_MOUSE_PS2_OLPC
972 .type = PSMOUSE_HGPK,
973 .name = "OLPC HGPK",
974 .alias = "hgpk",
975 .detect = hgpk_detect,
977 #endif
978 #ifdef CONFIG_MOUSE_PS2_ELANTECH
980 .type = PSMOUSE_ELANTECH,
981 .name = "ETPS/2",
982 .alias = "elantech",
983 .detect = elantech_detect,
984 .init = elantech_init,
986 #endif
987 #ifdef CONFIG_MOUSE_PS2_SENTELIC
989 .type = PSMOUSE_FSP,
990 .name = "FSPPS/2",
991 .alias = "fsp",
992 .detect = fsp_detect,
993 .init = fsp_init,
995 #endif
997 .type = PSMOUSE_CORTRON,
998 .name = "CortronPS/2",
999 .alias = "cortps",
1000 .detect = cortron_detect,
1003 .type = PSMOUSE_AUTO,
1004 .name = "auto",
1005 .alias = "any",
1006 .maxproto = true,
1010 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
1012 int i;
1014 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
1015 if (psmouse_protocols[i].type == type)
1016 return &psmouse_protocols[i];
1018 WARN_ON(1);
1019 return &psmouse_protocols[0];
1022 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
1024 const struct psmouse_protocol *p;
1025 int i;
1027 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
1028 p = &psmouse_protocols[i];
1030 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
1031 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
1032 return &psmouse_protocols[i];
1035 return NULL;
1040 * psmouse_probe() probes for a PS/2 mouse.
1043 static int psmouse_probe(struct psmouse *psmouse)
1045 struct ps2dev *ps2dev = &psmouse->ps2dev;
1046 unsigned char param[2];
1049 * First, we check if it's a mouse. It should send 0x00 or 0x03
1050 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1051 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
1052 * ID queries, probably due to a firmware bug.
1055 param[0] = 0xa5;
1056 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1057 return -1;
1059 if (param[0] != 0x00 && param[0] != 0x03 &&
1060 param[0] != 0x04 && param[0] != 0xff)
1061 return -1;
1064 * Then we reset and disable the mouse so that it doesn't generate events.
1067 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
1068 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1069 ps2dev->serio->phys);
1071 return 0;
1075 * psmouse_initialize() initializes the mouse to a sane state.
1078 static void psmouse_initialize(struct psmouse *psmouse)
1081 * We set the mouse report rate, resolution and scaling.
1084 if (psmouse_max_proto != PSMOUSE_PS2) {
1085 psmouse->set_rate(psmouse, psmouse->rate);
1086 psmouse->set_resolution(psmouse, psmouse->resolution);
1087 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
1092 * psmouse_activate() enables the mouse so that we get motion reports from it.
1095 static void psmouse_activate(struct psmouse *psmouse)
1097 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1098 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1099 psmouse->ps2dev.serio->phys);
1101 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1106 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
1107 * reports from it unless we explicitly request it.
1110 static void psmouse_deactivate(struct psmouse *psmouse)
1112 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1113 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1114 psmouse->ps2dev.serio->phys);
1116 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1121 * psmouse_resync() attempts to re-validate current protocol.
1124 static void psmouse_resync(struct work_struct *work)
1126 struct psmouse *parent = NULL, *psmouse =
1127 container_of(work, struct psmouse, resync_work.work);
1128 struct serio *serio = psmouse->ps2dev.serio;
1129 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1130 bool failed = false, enabled = false;
1131 int i;
1133 mutex_lock(&psmouse_mutex);
1135 if (psmouse->state != PSMOUSE_RESYNCING)
1136 goto out;
1138 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1139 parent = serio_get_drvdata(serio->parent);
1140 psmouse_deactivate(parent);
1144 * Some mice don't ACK commands sent while they are in the middle of
1145 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1146 * instead of ps2_command() which would wait for 200ms for an ACK
1147 * that may never come.
1148 * As an additional quirk ALPS touchpads may not only forget to ACK
1149 * disable command but will stop reporting taps, so if we see that
1150 * mouse at least once ACKs disable we will do full reconnect if ACK
1151 * is missing.
1153 psmouse->num_resyncs++;
1155 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1156 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1157 failed = true;
1158 } else
1159 psmouse->acks_disable_command = true;
1162 * Poll the mouse. If it was reset the packet will be shorter than
1163 * psmouse->pktsize and ps2_command will fail. We do not expect and
1164 * do not handle scenario when mouse "upgrades" its protocol while
1165 * disconnected since it would require additional delay. If we ever
1166 * see a mouse that does it we'll adjust the code.
1168 if (!failed) {
1169 if (psmouse->poll(psmouse))
1170 failed = true;
1171 else {
1172 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1173 for (i = 0; i < psmouse->pktsize; i++) {
1174 psmouse->pktcnt++;
1175 rc = psmouse->protocol_handler(psmouse);
1176 if (rc != PSMOUSE_GOOD_DATA)
1177 break;
1179 if (rc != PSMOUSE_FULL_PACKET)
1180 failed = true;
1181 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1185 * Now try to enable mouse. We try to do that even if poll failed and also
1186 * repeat our attempts 5 times, otherwise we may be left out with disabled
1187 * mouse.
1189 for (i = 0; i < 5; i++) {
1190 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1191 enabled = true;
1192 break;
1194 msleep(200);
1197 if (!enabled) {
1198 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1199 psmouse->ps2dev.serio->phys);
1200 failed = true;
1203 if (failed) {
1204 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1205 psmouse_info(psmouse,
1206 "resync failed, issuing reconnect request\n");
1207 serio_reconnect(serio);
1208 } else
1209 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1211 if (parent)
1212 psmouse_activate(parent);
1213 out:
1214 mutex_unlock(&psmouse_mutex);
1218 * psmouse_cleanup() resets the mouse into power-on state.
1221 static void psmouse_cleanup(struct serio *serio)
1223 struct psmouse *psmouse = serio_get_drvdata(serio);
1224 struct psmouse *parent = NULL;
1226 mutex_lock(&psmouse_mutex);
1228 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1229 parent = serio_get_drvdata(serio->parent);
1230 psmouse_deactivate(parent);
1233 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1236 * Disable stream mode so cleanup routine can proceed undisturbed.
1238 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1239 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1240 psmouse->ps2dev.serio->phys);
1242 if (psmouse->cleanup)
1243 psmouse->cleanup(psmouse);
1246 * Reset the mouse to defaults (bare PS/2 protocol).
1248 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1251 * Some boxes, such as HP nx7400, get terribly confused if mouse
1252 * is not fully enabled before suspending/shutting down.
1254 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1256 if (parent) {
1257 if (parent->pt_deactivate)
1258 parent->pt_deactivate(parent);
1260 psmouse_activate(parent);
1263 mutex_unlock(&psmouse_mutex);
1267 * psmouse_disconnect() closes and frees.
1270 static void psmouse_disconnect(struct serio *serio)
1272 struct psmouse *psmouse, *parent = NULL;
1274 psmouse = serio_get_drvdata(serio);
1276 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1278 mutex_lock(&psmouse_mutex);
1280 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1282 /* make sure we don't have a resync in progress */
1283 mutex_unlock(&psmouse_mutex);
1284 flush_workqueue(kpsmoused_wq);
1285 mutex_lock(&psmouse_mutex);
1287 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1288 parent = serio_get_drvdata(serio->parent);
1289 psmouse_deactivate(parent);
1292 if (psmouse->disconnect)
1293 psmouse->disconnect(psmouse);
1295 if (parent && parent->pt_deactivate)
1296 parent->pt_deactivate(parent);
1298 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1300 serio_close(serio);
1301 serio_set_drvdata(serio, NULL);
1302 input_unregister_device(psmouse->dev);
1303 kfree(psmouse);
1305 if (parent)
1306 psmouse_activate(parent);
1308 mutex_unlock(&psmouse_mutex);
1311 static int psmouse_switch_protocol(struct psmouse *psmouse,
1312 const struct psmouse_protocol *proto)
1314 const struct psmouse_protocol *selected_proto;
1315 struct input_dev *input_dev = psmouse->dev;
1317 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1319 if (proto && (proto->detect || proto->init)) {
1320 psmouse_apply_defaults(psmouse);
1322 if (proto->detect && proto->detect(psmouse, true) < 0)
1323 return -1;
1325 if (proto->init && proto->init(psmouse) < 0)
1326 return -1;
1328 psmouse->type = proto->type;
1329 selected_proto = proto;
1330 } else {
1331 psmouse->type = psmouse_extensions(psmouse,
1332 psmouse_max_proto, true);
1333 selected_proto = psmouse_protocol_by_type(psmouse->type);
1336 psmouse->ignore_parity = selected_proto->ignore_parity;
1339 * If mouse's packet size is 3 there is no point in polling the
1340 * device in hopes to detect protocol reset - we won't get less
1341 * than 3 bytes response anyhow.
1343 if (psmouse->pktsize == 3)
1344 psmouse->resync_time = 0;
1347 * Some smart KVMs fake response to POLL command returning just
1348 * 3 bytes and messing up our resync logic, so if initial poll
1349 * fails we won't try polling the device anymore. Hopefully
1350 * such KVM will maintain initially selected protocol.
1352 if (psmouse->resync_time && psmouse->poll(psmouse))
1353 psmouse->resync_time = 0;
1355 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1356 selected_proto->name, psmouse->vendor, psmouse->name);
1358 input_dev->name = psmouse->devname;
1359 input_dev->phys = psmouse->phys;
1360 input_dev->id.bustype = BUS_I8042;
1361 input_dev->id.vendor = 0x0002;
1362 input_dev->id.product = psmouse->type;
1363 input_dev->id.version = psmouse->model;
1365 return 0;
1369 * psmouse_connect() is a callback from the serio module when
1370 * an unhandled serio port is found.
1372 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1374 struct psmouse *psmouse, *parent = NULL;
1375 struct input_dev *input_dev;
1376 int retval = 0, error = -ENOMEM;
1378 mutex_lock(&psmouse_mutex);
1381 * If this is a pass-through port deactivate parent so the device
1382 * connected to this port can be successfully identified
1384 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1385 parent = serio_get_drvdata(serio->parent);
1386 psmouse_deactivate(parent);
1389 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1390 input_dev = input_allocate_device();
1391 if (!psmouse || !input_dev)
1392 goto err_free;
1394 ps2_init(&psmouse->ps2dev, serio);
1395 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1396 psmouse->dev = input_dev;
1397 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1399 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1401 serio_set_drvdata(serio, psmouse);
1403 error = serio_open(serio, drv);
1404 if (error)
1405 goto err_clear_drvdata;
1407 if (psmouse_probe(psmouse) < 0) {
1408 error = -ENODEV;
1409 goto err_close_serio;
1412 psmouse->rate = psmouse_rate;
1413 psmouse->resolution = psmouse_resolution;
1414 psmouse->resetafter = psmouse_resetafter;
1415 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1416 psmouse->smartscroll = psmouse_smartscroll;
1418 psmouse_switch_protocol(psmouse, NULL);
1420 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1421 psmouse_initialize(psmouse);
1423 error = input_register_device(psmouse->dev);
1424 if (error)
1425 goto err_protocol_disconnect;
1427 if (parent && parent->pt_activate)
1428 parent->pt_activate(parent);
1430 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1431 if (error)
1432 goto err_pt_deactivate;
1434 psmouse_activate(psmouse);
1436 out:
1437 /* If this is a pass-through port the parent needs to be re-activated */
1438 if (parent)
1439 psmouse_activate(parent);
1441 mutex_unlock(&psmouse_mutex);
1442 return retval;
1444 err_pt_deactivate:
1445 if (parent && parent->pt_deactivate)
1446 parent->pt_deactivate(parent);
1447 input_unregister_device(psmouse->dev);
1448 input_dev = NULL; /* so we don't try to free it below */
1449 err_protocol_disconnect:
1450 if (psmouse->disconnect)
1451 psmouse->disconnect(psmouse);
1452 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1453 err_close_serio:
1454 serio_close(serio);
1455 err_clear_drvdata:
1456 serio_set_drvdata(serio, NULL);
1457 err_free:
1458 input_free_device(input_dev);
1459 kfree(psmouse);
1461 retval = error;
1462 goto out;
1466 static int psmouse_reconnect(struct serio *serio)
1468 struct psmouse *psmouse = serio_get_drvdata(serio);
1469 struct psmouse *parent = NULL;
1470 struct serio_driver *drv = serio->drv;
1471 unsigned char type;
1472 int rc = -1;
1474 if (!drv || !psmouse) {
1475 psmouse_dbg(psmouse,
1476 "reconnect request, but serio is disconnected, ignoring...\n");
1477 return -1;
1480 mutex_lock(&psmouse_mutex);
1482 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1483 parent = serio_get_drvdata(serio->parent);
1484 psmouse_deactivate(parent);
1487 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1489 if (psmouse->reconnect) {
1490 if (psmouse->reconnect(psmouse))
1491 goto out;
1492 } else {
1493 psmouse_reset(psmouse);
1495 if (psmouse_probe(psmouse) < 0)
1496 goto out;
1498 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1499 if (psmouse->type != type)
1500 goto out;
1504 * OK, the device type (and capabilities) match the old one,
1505 * we can continue using it, complete initialization
1507 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1509 psmouse_initialize(psmouse);
1511 if (parent && parent->pt_activate)
1512 parent->pt_activate(parent);
1514 psmouse_activate(psmouse);
1515 rc = 0;
1517 out:
1518 /* If this is a pass-through port the parent waits to be activated */
1519 if (parent)
1520 psmouse_activate(parent);
1522 mutex_unlock(&psmouse_mutex);
1523 return rc;
1526 static struct serio_device_id psmouse_serio_ids[] = {
1528 .type = SERIO_8042,
1529 .proto = SERIO_ANY,
1530 .id = SERIO_ANY,
1531 .extra = SERIO_ANY,
1534 .type = SERIO_PS_PSTHRU,
1535 .proto = SERIO_ANY,
1536 .id = SERIO_ANY,
1537 .extra = SERIO_ANY,
1539 { 0 }
1542 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1544 static struct serio_driver psmouse_drv = {
1545 .driver = {
1546 .name = "psmouse",
1548 .description = DRIVER_DESC,
1549 .id_table = psmouse_serio_ids,
1550 .interrupt = psmouse_interrupt,
1551 .connect = psmouse_connect,
1552 .reconnect = psmouse_reconnect,
1553 .disconnect = psmouse_disconnect,
1554 .cleanup = psmouse_cleanup,
1557 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1558 char *buf)
1560 struct serio *serio = to_serio_port(dev);
1561 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1562 struct psmouse *psmouse;
1564 psmouse = serio_get_drvdata(serio);
1566 return attr->show(psmouse, attr->data, buf);
1569 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1570 const char *buf, size_t count)
1572 struct serio *serio = to_serio_port(dev);
1573 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1574 struct psmouse *psmouse, *parent = NULL;
1575 int retval;
1577 retval = mutex_lock_interruptible(&psmouse_mutex);
1578 if (retval)
1579 goto out;
1581 psmouse = serio_get_drvdata(serio);
1583 if (attr->protect) {
1584 if (psmouse->state == PSMOUSE_IGNORE) {
1585 retval = -ENODEV;
1586 goto out_unlock;
1589 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1590 parent = serio_get_drvdata(serio->parent);
1591 psmouse_deactivate(parent);
1594 psmouse_deactivate(psmouse);
1597 retval = attr->set(psmouse, attr->data, buf, count);
1599 if (attr->protect) {
1600 if (retval != -ENODEV)
1601 psmouse_activate(psmouse);
1603 if (parent)
1604 psmouse_activate(parent);
1607 out_unlock:
1608 mutex_unlock(&psmouse_mutex);
1609 out:
1610 return retval;
1613 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1615 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1617 return sprintf(buf, "%u\n", *field);
1620 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1622 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1623 unsigned int value;
1624 int err;
1626 err = kstrtouint(buf, 10, &value);
1627 if (err)
1628 return err;
1630 *field = value;
1632 return count;
1635 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1637 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1640 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1642 struct serio *serio = psmouse->ps2dev.serio;
1643 struct psmouse *parent = NULL;
1644 struct input_dev *old_dev, *new_dev;
1645 const struct psmouse_protocol *proto, *old_proto;
1646 int error;
1647 int retry = 0;
1649 proto = psmouse_protocol_by_name(buf, count);
1650 if (!proto)
1651 return -EINVAL;
1653 if (psmouse->type == proto->type)
1654 return count;
1656 new_dev = input_allocate_device();
1657 if (!new_dev)
1658 return -ENOMEM;
1660 while (!list_empty(&serio->children)) {
1661 if (++retry > 3) {
1662 psmouse_warn(psmouse,
1663 "failed to destroy children ports, protocol change aborted.\n");
1664 input_free_device(new_dev);
1665 return -EIO;
1668 mutex_unlock(&psmouse_mutex);
1669 serio_unregister_child_port(serio);
1670 mutex_lock(&psmouse_mutex);
1672 if (serio->drv != &psmouse_drv) {
1673 input_free_device(new_dev);
1674 return -ENODEV;
1677 if (psmouse->type == proto->type) {
1678 input_free_device(new_dev);
1679 return count; /* switched by other thread */
1683 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1684 parent = serio_get_drvdata(serio->parent);
1685 if (parent->pt_deactivate)
1686 parent->pt_deactivate(parent);
1689 old_dev = psmouse->dev;
1690 old_proto = psmouse_protocol_by_type(psmouse->type);
1692 if (psmouse->disconnect)
1693 psmouse->disconnect(psmouse);
1695 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1697 psmouse->dev = new_dev;
1698 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1700 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1701 psmouse_reset(psmouse);
1702 /* default to PSMOUSE_PS2 */
1703 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1706 psmouse_initialize(psmouse);
1707 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1709 error = input_register_device(psmouse->dev);
1710 if (error) {
1711 if (psmouse->disconnect)
1712 psmouse->disconnect(psmouse);
1714 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1715 input_free_device(new_dev);
1716 psmouse->dev = old_dev;
1717 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1718 psmouse_switch_protocol(psmouse, old_proto);
1719 psmouse_initialize(psmouse);
1720 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1722 return error;
1725 input_unregister_device(old_dev);
1727 if (parent && parent->pt_activate)
1728 parent->pt_activate(parent);
1730 return count;
1733 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1735 unsigned int value;
1736 int err;
1738 err = kstrtouint(buf, 10, &value);
1739 if (err)
1740 return err;
1742 psmouse->set_rate(psmouse, value);
1743 return count;
1746 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1748 unsigned int value;
1749 int err;
1751 err = kstrtouint(buf, 10, &value);
1752 if (err)
1753 return err;
1755 psmouse->set_resolution(psmouse, value);
1756 return count;
1760 static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
1762 const struct psmouse_protocol *proto;
1764 if (!val)
1765 return -EINVAL;
1767 proto = psmouse_protocol_by_name(val, strlen(val));
1769 if (!proto || !proto->maxproto)
1770 return -EINVAL;
1772 *((unsigned int *)kp->arg) = proto->type;
1774 return 0;
1777 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
1779 int type = *((unsigned int *)kp->arg);
1781 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1784 static int __init psmouse_init(void)
1786 int err;
1788 lifebook_module_init();
1789 synaptics_module_init();
1790 hgpk_module_init();
1792 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1793 if (!kpsmoused_wq) {
1794 pr_err("failed to create kpsmoused workqueue\n");
1795 return -ENOMEM;
1798 err = serio_register_driver(&psmouse_drv);
1799 if (err)
1800 destroy_workqueue(kpsmoused_wq);
1802 return err;
1805 static void __exit psmouse_exit(void)
1807 serio_unregister_driver(&psmouse_drv);
1808 destroy_workqueue(kpsmoused_wq);
1811 module_init(psmouse_init);
1812 module_exit(psmouse_exit);