Staging: hv: mousevsc: Cleanup and properly implement reportdesc_callback()
[zen-stable.git] / drivers / input / mouse / psmouse-base.c
blob3f74baee102bbd706e3541f073cc1105931e8165
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 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/input.h>
19 #include <linux/serio.h>
20 #include <linux/init.h>
21 #include <linux/libps2.h>
22 #include <linux/mutex.h>
24 #include "psmouse.h"
25 #include "synaptics.h"
26 #include "logips2pp.h"
27 #include "alps.h"
28 #include "hgpk.h"
29 #include "lifebook.h"
30 #include "trackpoint.h"
31 #include "touchkit_ps2.h"
32 #include "elantech.h"
33 #include "sentelic.h"
35 #define DRIVER_DESC "PS/2 mouse driver"
37 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
38 MODULE_DESCRIPTION(DRIVER_DESC);
39 MODULE_LICENSE("GPL");
41 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
42 static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
43 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
44 static struct kernel_param_ops param_ops_proto_abbrev = {
45 .set = psmouse_set_maxproto,
46 .get = psmouse_get_maxproto,
48 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
49 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
50 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
52 static unsigned int psmouse_resolution = 200;
53 module_param_named(resolution, psmouse_resolution, uint, 0644);
54 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
56 static unsigned int psmouse_rate = 100;
57 module_param_named(rate, psmouse_rate, uint, 0644);
58 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
60 static unsigned int psmouse_smartscroll = 1;
61 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
62 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
64 static unsigned int psmouse_resetafter = 5;
65 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
66 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
68 static unsigned int psmouse_resync_time;
69 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
70 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
72 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
73 NULL,
74 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
75 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
76 (void *) offsetof(struct psmouse, rate),
77 psmouse_show_int_attr, psmouse_attr_set_rate);
78 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
79 (void *) offsetof(struct psmouse, resolution),
80 psmouse_show_int_attr, psmouse_attr_set_resolution);
81 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, resetafter),
83 psmouse_show_int_attr, psmouse_set_int_attr);
84 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
85 (void *) offsetof(struct psmouse, resync_time),
86 psmouse_show_int_attr, psmouse_set_int_attr);
88 static struct attribute *psmouse_attributes[] = {
89 &psmouse_attr_protocol.dattr.attr,
90 &psmouse_attr_rate.dattr.attr,
91 &psmouse_attr_resolution.dattr.attr,
92 &psmouse_attr_resetafter.dattr.attr,
93 &psmouse_attr_resync_time.dattr.attr,
94 NULL
97 static struct attribute_group psmouse_attribute_group = {
98 .attrs = psmouse_attributes,
102 * psmouse_mutex protects all operations changing state of mouse
103 * (connecting, disconnecting, changing rate or resolution via
104 * sysfs). We could use a per-device semaphore but since there
105 * rarely more than one PS/2 mouse connected and since semaphore
106 * is taken in "slow" paths it is not worth it.
108 static DEFINE_MUTEX(psmouse_mutex);
110 static struct workqueue_struct *kpsmoused_wq;
112 struct psmouse_protocol {
113 enum psmouse_type type;
114 bool maxproto;
115 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
116 const char *name;
117 const char *alias;
118 int (*detect)(struct psmouse *, bool);
119 int (*init)(struct psmouse *);
123 * psmouse_process_byte() analyzes the PS/2 data stream and reports
124 * relevant events to the input module once full packet has arrived.
127 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
129 struct input_dev *dev = psmouse->dev;
130 unsigned char *packet = psmouse->packet;
132 if (psmouse->pktcnt < psmouse->pktsize)
133 return PSMOUSE_GOOD_DATA;
136 * Full packet accumulated, process it
140 * Scroll wheel on IntelliMice, scroll buttons on NetMice
143 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
144 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
147 * Scroll wheel and buttons on IntelliMouse Explorer
150 if (psmouse->type == PSMOUSE_IMEX) {
151 switch (packet[3] & 0xC0) {
152 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
153 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154 break;
155 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
156 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
157 break;
158 case 0x00:
159 case 0xC0:
160 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
161 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
162 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
163 break;
168 * Extra buttons on Genius NewNet 3D
171 if (psmouse->type == PSMOUSE_GENPS) {
172 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
173 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
177 * Extra button on ThinkingMouse
179 if (psmouse->type == PSMOUSE_THINKPS) {
180 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
181 /* Without this bit of weirdness moving up gives wildly high Y changes. */
182 packet[1] |= (packet[0] & 0x40) << 1;
186 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
187 * byte.
189 if (psmouse->type == PSMOUSE_CORTRON) {
190 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
191 packet[0] |= 0x08;
195 * Generic PS/2 Mouse
198 input_report_key(dev, BTN_LEFT, packet[0] & 1);
199 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
200 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
202 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
203 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
205 input_sync(dev);
207 return PSMOUSE_FULL_PACKET;
210 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
211 unsigned long delay)
213 queue_delayed_work(kpsmoused_wq, work, delay);
217 * __psmouse_set_state() sets new psmouse state and resets all flags.
220 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
222 psmouse->state = new_state;
223 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
224 psmouse->ps2dev.flags = 0;
225 psmouse->last = jiffies;
230 * psmouse_set_state() sets new psmouse state and resets all flags and
231 * counters while holding serio lock so fighting with interrupt handler
232 * is not a concern.
235 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
237 serio_pause_rx(psmouse->ps2dev.serio);
238 __psmouse_set_state(psmouse, new_state);
239 serio_continue_rx(psmouse->ps2dev.serio);
243 * psmouse_handle_byte() processes one byte of the input data stream
244 * by calling corresponding protocol handler.
247 static int psmouse_handle_byte(struct psmouse *psmouse)
249 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
251 switch (rc) {
252 case PSMOUSE_BAD_DATA:
253 if (psmouse->state == PSMOUSE_ACTIVATED) {
254 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
255 psmouse->name, psmouse->phys, psmouse->pktcnt);
256 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
257 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
258 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
259 serio_reconnect(psmouse->ps2dev.serio);
260 return -1;
263 psmouse->pktcnt = 0;
264 break;
266 case PSMOUSE_FULL_PACKET:
267 psmouse->pktcnt = 0;
268 if (psmouse->out_of_sync_cnt) {
269 psmouse->out_of_sync_cnt = 0;
270 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
271 psmouse->name, psmouse->phys);
273 break;
275 case PSMOUSE_GOOD_DATA:
276 break;
278 return 0;
282 * psmouse_interrupt() handles incoming characters, either passing them
283 * for normal processing or gathering them as command response.
286 static irqreturn_t psmouse_interrupt(struct serio *serio,
287 unsigned char data, unsigned int flags)
289 struct psmouse *psmouse = serio_get_drvdata(serio);
291 if (psmouse->state == PSMOUSE_IGNORE)
292 goto out;
294 if (unlikely((flags & SERIO_TIMEOUT) ||
295 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
297 if (psmouse->state == PSMOUSE_ACTIVATED)
298 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
299 flags & SERIO_TIMEOUT ? " timeout" : "",
300 flags & SERIO_PARITY ? " bad parity" : "");
301 ps2_cmd_aborted(&psmouse->ps2dev);
302 goto out;
305 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
306 if (ps2_handle_ack(&psmouse->ps2dev, data))
307 goto out;
309 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
310 if (ps2_handle_response(&psmouse->ps2dev, data))
311 goto out;
313 if (psmouse->state <= PSMOUSE_RESYNCING)
314 goto out;
316 if (psmouse->state == PSMOUSE_ACTIVATED &&
317 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
318 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
319 psmouse->name, psmouse->phys, psmouse->pktcnt);
320 psmouse->badbyte = psmouse->packet[0];
321 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
322 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
323 goto out;
326 psmouse->packet[psmouse->pktcnt++] = data;
328 * Check if this is a new device announcement (0xAA 0x00)
330 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
331 if (psmouse->pktcnt == 1) {
332 psmouse->last = jiffies;
333 goto out;
336 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
337 (psmouse->type == PSMOUSE_HGPK &&
338 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
339 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
340 serio_reconnect(serio);
341 goto out;
344 * Not a new device, try processing first byte normally
346 psmouse->pktcnt = 1;
347 if (psmouse_handle_byte(psmouse))
348 goto out;
350 psmouse->packet[psmouse->pktcnt++] = data;
354 * See if we need to force resync because mouse was idle for too long
356 if (psmouse->state == PSMOUSE_ACTIVATED &&
357 psmouse->pktcnt == 1 && psmouse->resync_time &&
358 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
359 psmouse->badbyte = psmouse->packet[0];
360 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
361 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
362 goto out;
365 psmouse->last = jiffies;
366 psmouse_handle_byte(psmouse);
368 out:
369 return IRQ_HANDLED;
374 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
375 * using sliced syntax, understood by advanced devices, such as Logitech
376 * or Synaptics touchpads. The command is encoded as:
377 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
378 * is the command.
380 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
382 int i;
384 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
385 return -1;
387 for (i = 6; i >= 0; i -= 2) {
388 unsigned char d = (command >> i) & 3;
389 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
390 return -1;
393 return 0;
398 * psmouse_reset() resets the mouse into power-on state.
400 int psmouse_reset(struct psmouse *psmouse)
402 unsigned char param[2];
404 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
405 return -1;
407 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
408 return -1;
410 return 0;
415 * Genius NetMouse magic init.
417 static int genius_detect(struct psmouse *psmouse, bool set_properties)
419 struct ps2dev *ps2dev = &psmouse->ps2dev;
420 unsigned char param[4];
422 param[0] = 3;
423 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
424 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
425 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
426 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
427 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
429 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
430 return -1;
432 if (set_properties) {
433 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
434 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
435 __set_bit(BTN_SIDE, psmouse->dev->keybit);
436 __set_bit(REL_WHEEL, psmouse->dev->relbit);
438 psmouse->vendor = "Genius";
439 psmouse->name = "Mouse";
440 psmouse->pktsize = 4;
443 return 0;
447 * IntelliMouse magic init.
449 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
451 struct ps2dev *ps2dev = &psmouse->ps2dev;
452 unsigned char param[2];
454 param[0] = 200;
455 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
456 param[0] = 100;
457 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
458 param[0] = 80;
459 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
460 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
462 if (param[0] != 3)
463 return -1;
465 if (set_properties) {
466 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
467 __set_bit(REL_WHEEL, psmouse->dev->relbit);
469 if (!psmouse->vendor)
470 psmouse->vendor = "Generic";
471 if (!psmouse->name)
472 psmouse->name = "Wheel Mouse";
473 psmouse->pktsize = 4;
476 return 0;
480 * Try IntelliMouse/Explorer magic init.
482 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
484 struct ps2dev *ps2dev = &psmouse->ps2dev;
485 unsigned char param[2];
487 intellimouse_detect(psmouse, 0);
489 param[0] = 200;
490 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
491 param[0] = 200;
492 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
493 param[0] = 80;
494 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
495 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
497 if (param[0] != 4)
498 return -1;
500 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
501 param[0] = 200;
502 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
503 param[0] = 80;
504 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
505 param[0] = 40;
506 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
508 if (set_properties) {
509 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
510 __set_bit(REL_WHEEL, psmouse->dev->relbit);
511 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
512 __set_bit(BTN_SIDE, psmouse->dev->keybit);
513 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
515 if (!psmouse->vendor)
516 psmouse->vendor = "Generic";
517 if (!psmouse->name)
518 psmouse->name = "Explorer Mouse";
519 psmouse->pktsize = 4;
522 return 0;
526 * Kensington ThinkingMouse / ExpertMouse magic init.
528 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
530 struct ps2dev *ps2dev = &psmouse->ps2dev;
531 unsigned char param[2];
532 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
533 int i;
535 param[0] = 10;
536 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
537 param[0] = 0;
538 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
539 for (i = 0; i < ARRAY_SIZE(seq); i++) {
540 param[0] = seq[i];
541 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
543 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
545 if (param[0] != 2)
546 return -1;
548 if (set_properties) {
549 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
550 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
552 psmouse->vendor = "Kensington";
553 psmouse->name = "ThinkingMouse";
556 return 0;
560 * Bare PS/2 protocol "detection". Always succeeds.
562 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
564 if (set_properties) {
565 if (!psmouse->vendor)
566 psmouse->vendor = "Generic";
567 if (!psmouse->name)
568 psmouse->name = "Mouse";
571 * We have no way of figuring true number of buttons so let's
572 * assume that the device has 3.
574 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
577 return 0;
581 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
582 * must be forced by sysfs protocol writing.
584 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
586 if (set_properties) {
587 psmouse->vendor = "Cortron";
588 psmouse->name = "PS/2 Trackball";
590 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
591 __set_bit(BTN_SIDE, psmouse->dev->keybit);
594 return 0;
598 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
599 * the mouse may have.
602 static int psmouse_extensions(struct psmouse *psmouse,
603 unsigned int max_proto, bool set_properties)
605 bool synaptics_hardware = false;
608 * We always check for lifebook because it does not disturb mouse
609 * (it only checks DMI information).
611 if (lifebook_detect(psmouse, set_properties) == 0) {
612 if (max_proto > PSMOUSE_IMEX) {
613 if (!set_properties || lifebook_init(psmouse) == 0)
614 return PSMOUSE_LIFEBOOK;
619 * Try Kensington ThinkingMouse (we try first, because synaptics probe
620 * upsets the thinkingmouse).
623 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
624 return PSMOUSE_THINKPS;
627 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
628 * support is disabled in config - we need to know if it is synaptics so we
629 * can reset it properly after probing for intellimouse.
631 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
632 synaptics_hardware = true;
634 if (max_proto > PSMOUSE_IMEX) {
636 * Try activating protocol, but check if support is enabled first, since
637 * we try detecting Synaptics even when protocol is disabled.
639 if (synaptics_supported() &&
640 (!set_properties || synaptics_init(psmouse) == 0)) {
641 return PSMOUSE_SYNAPTICS;
645 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
646 * Unfortunately Logitech/Genius probes confuse some firmware versions so
647 * we'll have to skip them.
649 max_proto = PSMOUSE_IMEX;
652 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
654 synaptics_reset(psmouse);
658 * Try ALPS TouchPad
660 if (max_proto > PSMOUSE_IMEX) {
661 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
662 if (alps_detect(psmouse, set_properties) == 0) {
663 if (!set_properties || alps_init(psmouse) == 0)
664 return PSMOUSE_ALPS;
666 * Init failed, try basic relative protocols
668 max_proto = PSMOUSE_IMEX;
673 * Try OLPC HGPK touchpad.
675 if (max_proto > PSMOUSE_IMEX &&
676 hgpk_detect(psmouse, set_properties) == 0) {
677 if (!set_properties || hgpk_init(psmouse) == 0)
678 return PSMOUSE_HGPK;
680 * Init failed, try basic relative protocols
682 max_proto = PSMOUSE_IMEX;
686 * Try Elantech touchpad.
688 if (max_proto > PSMOUSE_IMEX &&
689 elantech_detect(psmouse, set_properties) == 0) {
690 if (!set_properties || elantech_init(psmouse) == 0)
691 return PSMOUSE_ELANTECH;
693 * Init failed, try basic relative protocols
695 max_proto = PSMOUSE_IMEX;
699 if (max_proto > PSMOUSE_IMEX) {
700 if (genius_detect(psmouse, set_properties) == 0)
701 return PSMOUSE_GENPS;
703 if (ps2pp_init(psmouse, set_properties) == 0)
704 return PSMOUSE_PS2PP;
706 if (trackpoint_detect(psmouse, set_properties) == 0)
707 return PSMOUSE_TRACKPOINT;
709 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
710 return PSMOUSE_TOUCHKIT_PS2;
714 * Try Finger Sensing Pad. We do it here because its probe upsets
715 * Trackpoint devices (causing TP_READ_ID command to time out).
717 if (max_proto > PSMOUSE_IMEX) {
718 if (fsp_detect(psmouse, set_properties) == 0) {
719 if (!set_properties || fsp_init(psmouse) == 0)
720 return PSMOUSE_FSP;
722 * Init failed, try basic relative protocols
724 max_proto = PSMOUSE_IMEX;
729 * Reset to defaults in case the device got confused by extended
730 * protocol probes. Note that we follow up with full reset because
731 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
733 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
734 psmouse_reset(psmouse);
736 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
737 return PSMOUSE_IMEX;
739 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
740 return PSMOUSE_IMPS;
743 * Okay, all failed, we have a standard mouse here. The number of the buttons
744 * is still a question, though. We assume 3.
746 ps2bare_detect(psmouse, set_properties);
748 if (synaptics_hardware) {
750 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
751 * We need to reset the touchpad because if there is a track point on the
752 * pass through port it could get disabled while probing for protocol
753 * extensions.
755 psmouse_reset(psmouse);
758 return PSMOUSE_PS2;
761 static const struct psmouse_protocol psmouse_protocols[] = {
763 .type = PSMOUSE_PS2,
764 .name = "PS/2",
765 .alias = "bare",
766 .maxproto = true,
767 .ignore_parity = true,
768 .detect = ps2bare_detect,
770 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
772 .type = PSMOUSE_PS2PP,
773 .name = "PS2++",
774 .alias = "logitech",
775 .detect = ps2pp_init,
777 #endif
779 .type = PSMOUSE_THINKPS,
780 .name = "ThinkPS/2",
781 .alias = "thinkps",
782 .detect = thinking_detect,
785 .type = PSMOUSE_GENPS,
786 .name = "GenPS/2",
787 .alias = "genius",
788 .detect = genius_detect,
791 .type = PSMOUSE_IMPS,
792 .name = "ImPS/2",
793 .alias = "imps",
794 .maxproto = true,
795 .ignore_parity = true,
796 .detect = intellimouse_detect,
799 .type = PSMOUSE_IMEX,
800 .name = "ImExPS/2",
801 .alias = "exps",
802 .maxproto = true,
803 .ignore_parity = true,
804 .detect = im_explorer_detect,
806 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
808 .type = PSMOUSE_SYNAPTICS,
809 .name = "SynPS/2",
810 .alias = "synaptics",
811 .detect = synaptics_detect,
812 .init = synaptics_init,
814 #endif
815 #ifdef CONFIG_MOUSE_PS2_ALPS
817 .type = PSMOUSE_ALPS,
818 .name = "AlpsPS/2",
819 .alias = "alps",
820 .detect = alps_detect,
821 .init = alps_init,
823 #endif
824 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
826 .type = PSMOUSE_LIFEBOOK,
827 .name = "LBPS/2",
828 .alias = "lifebook",
829 .init = lifebook_init,
831 #endif
832 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
834 .type = PSMOUSE_TRACKPOINT,
835 .name = "TPPS/2",
836 .alias = "trackpoint",
837 .detect = trackpoint_detect,
839 #endif
840 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
842 .type = PSMOUSE_TOUCHKIT_PS2,
843 .name = "touchkitPS/2",
844 .alias = "touchkit",
845 .detect = touchkit_ps2_detect,
847 #endif
848 #ifdef CONFIG_MOUSE_PS2_OLPC
850 .type = PSMOUSE_HGPK,
851 .name = "OLPC HGPK",
852 .alias = "hgpk",
853 .detect = hgpk_detect,
855 #endif
856 #ifdef CONFIG_MOUSE_PS2_ELANTECH
858 .type = PSMOUSE_ELANTECH,
859 .name = "ETPS/2",
860 .alias = "elantech",
861 .detect = elantech_detect,
862 .init = elantech_init,
864 #endif
865 #ifdef CONFIG_MOUSE_PS2_SENTELIC
867 .type = PSMOUSE_FSP,
868 .name = "FSPPS/2",
869 .alias = "fsp",
870 .detect = fsp_detect,
871 .init = fsp_init,
873 #endif
875 .type = PSMOUSE_CORTRON,
876 .name = "CortronPS/2",
877 .alias = "cortps",
878 .detect = cortron_detect,
881 .type = PSMOUSE_AUTO,
882 .name = "auto",
883 .alias = "any",
884 .maxproto = true,
888 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
890 int i;
892 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
893 if (psmouse_protocols[i].type == type)
894 return &psmouse_protocols[i];
896 WARN_ON(1);
897 return &psmouse_protocols[0];
900 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
902 const struct psmouse_protocol *p;
903 int i;
905 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
906 p = &psmouse_protocols[i];
908 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
909 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
910 return &psmouse_protocols[i];
913 return NULL;
918 * psmouse_probe() probes for a PS/2 mouse.
921 static int psmouse_probe(struct psmouse *psmouse)
923 struct ps2dev *ps2dev = &psmouse->ps2dev;
924 unsigned char param[2];
927 * First, we check if it's a mouse. It should send 0x00 or 0x03
928 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
929 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
930 * ID queries, probably due to a firmware bug.
933 param[0] = 0xa5;
934 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
935 return -1;
937 if (param[0] != 0x00 && param[0] != 0x03 &&
938 param[0] != 0x04 && param[0] != 0xff)
939 return -1;
942 * Then we reset and disable the mouse so that it doesn't generate events.
945 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
946 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
948 return 0;
952 * Here we set the mouse resolution.
955 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
957 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
958 unsigned char p;
960 if (resolution == 0 || resolution > 200)
961 resolution = 200;
963 p = params[resolution / 50];
964 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
965 psmouse->resolution = 25 << p;
969 * Here we set the mouse report rate.
972 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
974 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
975 unsigned char r;
976 int i = 0;
978 while (rates[i] > rate) i++;
979 r = rates[i];
980 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
981 psmouse->rate = r;
985 * psmouse_initialize() initializes the mouse to a sane state.
988 static void psmouse_initialize(struct psmouse *psmouse)
991 * We set the mouse report rate, resolution and scaling.
994 if (psmouse_max_proto != PSMOUSE_PS2) {
995 psmouse->set_rate(psmouse, psmouse->rate);
996 psmouse->set_resolution(psmouse, psmouse->resolution);
997 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
1002 * psmouse_activate() enables the mouse so that we get motion reports from it.
1005 static void psmouse_activate(struct psmouse *psmouse)
1007 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1008 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
1009 psmouse->ps2dev.serio->phys);
1011 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1016 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
1017 * reports from it unless we explicitly request it.
1020 static void psmouse_deactivate(struct psmouse *psmouse)
1022 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1023 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
1024 psmouse->ps2dev.serio->phys);
1026 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1030 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
1033 static int psmouse_poll(struct psmouse *psmouse)
1035 return ps2_command(&psmouse->ps2dev, psmouse->packet,
1036 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1041 * psmouse_resync() attempts to re-validate current protocol.
1044 static void psmouse_resync(struct work_struct *work)
1046 struct psmouse *parent = NULL, *psmouse =
1047 container_of(work, struct psmouse, resync_work.work);
1048 struct serio *serio = psmouse->ps2dev.serio;
1049 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1050 bool failed = false, enabled = false;
1051 int i;
1053 mutex_lock(&psmouse_mutex);
1055 if (psmouse->state != PSMOUSE_RESYNCING)
1056 goto out;
1058 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1059 parent = serio_get_drvdata(serio->parent);
1060 psmouse_deactivate(parent);
1064 * Some mice don't ACK commands sent while they are in the middle of
1065 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1066 * instead of ps2_command() which would wait for 200ms for an ACK
1067 * that may never come.
1068 * As an additional quirk ALPS touchpads may not only forget to ACK
1069 * disable command but will stop reporting taps, so if we see that
1070 * mouse at least once ACKs disable we will do full reconnect if ACK
1071 * is missing.
1073 psmouse->num_resyncs++;
1075 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1076 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1077 failed = true;
1078 } else
1079 psmouse->acks_disable_command = true;
1082 * Poll the mouse. If it was reset the packet will be shorter than
1083 * psmouse->pktsize and ps2_command will fail. We do not expect and
1084 * do not handle scenario when mouse "upgrades" its protocol while
1085 * disconnected since it would require additional delay. If we ever
1086 * see a mouse that does it we'll adjust the code.
1088 if (!failed) {
1089 if (psmouse->poll(psmouse))
1090 failed = true;
1091 else {
1092 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1093 for (i = 0; i < psmouse->pktsize; i++) {
1094 psmouse->pktcnt++;
1095 rc = psmouse->protocol_handler(psmouse);
1096 if (rc != PSMOUSE_GOOD_DATA)
1097 break;
1099 if (rc != PSMOUSE_FULL_PACKET)
1100 failed = true;
1101 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1105 * Now try to enable mouse. We try to do that even if poll failed and also
1106 * repeat our attempts 5 times, otherwise we may be left out with disabled
1107 * mouse.
1109 for (i = 0; i < 5; i++) {
1110 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1111 enabled = true;
1112 break;
1114 msleep(200);
1117 if (!enabled) {
1118 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1119 psmouse->ps2dev.serio->phys);
1120 failed = true;
1123 if (failed) {
1124 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1125 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1126 serio_reconnect(serio);
1127 } else
1128 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1130 if (parent)
1131 psmouse_activate(parent);
1132 out:
1133 mutex_unlock(&psmouse_mutex);
1137 * psmouse_cleanup() resets the mouse into power-on state.
1140 static void psmouse_cleanup(struct serio *serio)
1142 struct psmouse *psmouse = serio_get_drvdata(serio);
1143 struct psmouse *parent = NULL;
1145 mutex_lock(&psmouse_mutex);
1147 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1148 parent = serio_get_drvdata(serio->parent);
1149 psmouse_deactivate(parent);
1152 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1155 * Disable stream mode so cleanup routine can proceed undisturbed.
1157 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1158 printk(KERN_WARNING "psmouse.c: Failed to disable mouse on %s\n",
1159 psmouse->ps2dev.serio->phys);
1161 if (psmouse->cleanup)
1162 psmouse->cleanup(psmouse);
1165 * Reset the mouse to defaults (bare PS/2 protocol).
1167 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1170 * Some boxes, such as HP nx7400, get terribly confused if mouse
1171 * is not fully enabled before suspending/shutting down.
1173 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1175 if (parent) {
1176 if (parent->pt_deactivate)
1177 parent->pt_deactivate(parent);
1179 psmouse_activate(parent);
1182 mutex_unlock(&psmouse_mutex);
1186 * psmouse_disconnect() closes and frees.
1189 static void psmouse_disconnect(struct serio *serio)
1191 struct psmouse *psmouse, *parent = NULL;
1193 psmouse = serio_get_drvdata(serio);
1195 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1197 mutex_lock(&psmouse_mutex);
1199 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1201 /* make sure we don't have a resync in progress */
1202 mutex_unlock(&psmouse_mutex);
1203 flush_workqueue(kpsmoused_wq);
1204 mutex_lock(&psmouse_mutex);
1206 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1207 parent = serio_get_drvdata(serio->parent);
1208 psmouse_deactivate(parent);
1211 if (psmouse->disconnect)
1212 psmouse->disconnect(psmouse);
1214 if (parent && parent->pt_deactivate)
1215 parent->pt_deactivate(parent);
1217 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1219 serio_close(serio);
1220 serio_set_drvdata(serio, NULL);
1221 input_unregister_device(psmouse->dev);
1222 kfree(psmouse);
1224 if (parent)
1225 psmouse_activate(parent);
1227 mutex_unlock(&psmouse_mutex);
1230 static int psmouse_switch_protocol(struct psmouse *psmouse,
1231 const struct psmouse_protocol *proto)
1233 const struct psmouse_protocol *selected_proto;
1234 struct input_dev *input_dev = psmouse->dev;
1236 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1238 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1239 input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1240 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1241 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1243 psmouse->set_rate = psmouse_set_rate;
1244 psmouse->set_resolution = psmouse_set_resolution;
1245 psmouse->poll = psmouse_poll;
1246 psmouse->protocol_handler = psmouse_process_byte;
1247 psmouse->pktsize = 3;
1249 if (proto && (proto->detect || proto->init)) {
1250 if (proto->detect && proto->detect(psmouse, true) < 0)
1251 return -1;
1253 if (proto->init && proto->init(psmouse) < 0)
1254 return -1;
1256 psmouse->type = proto->type;
1257 selected_proto = proto;
1258 } else {
1259 psmouse->type = psmouse_extensions(psmouse,
1260 psmouse_max_proto, true);
1261 selected_proto = psmouse_protocol_by_type(psmouse->type);
1264 psmouse->ignore_parity = selected_proto->ignore_parity;
1267 * If mouse's packet size is 3 there is no point in polling the
1268 * device in hopes to detect protocol reset - we won't get less
1269 * than 3 bytes response anyhow.
1271 if (psmouse->pktsize == 3)
1272 psmouse->resync_time = 0;
1275 * Some smart KVMs fake response to POLL command returning just
1276 * 3 bytes and messing up our resync logic, so if initial poll
1277 * fails we won't try polling the device anymore. Hopefully
1278 * such KVM will maintain initially selected protocol.
1280 if (psmouse->resync_time && psmouse->poll(psmouse))
1281 psmouse->resync_time = 0;
1283 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1284 selected_proto->name, psmouse->vendor, psmouse->name);
1286 input_dev->name = psmouse->devname;
1287 input_dev->phys = psmouse->phys;
1288 input_dev->id.bustype = BUS_I8042;
1289 input_dev->id.vendor = 0x0002;
1290 input_dev->id.product = psmouse->type;
1291 input_dev->id.version = psmouse->model;
1293 return 0;
1297 * psmouse_connect() is a callback from the serio module when
1298 * an unhandled serio port is found.
1300 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1302 struct psmouse *psmouse, *parent = NULL;
1303 struct input_dev *input_dev;
1304 int retval = 0, error = -ENOMEM;
1306 mutex_lock(&psmouse_mutex);
1309 * If this is a pass-through port deactivate parent so the device
1310 * connected to this port can be successfully identified
1312 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1313 parent = serio_get_drvdata(serio->parent);
1314 psmouse_deactivate(parent);
1317 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1318 input_dev = input_allocate_device();
1319 if (!psmouse || !input_dev)
1320 goto err_free;
1322 ps2_init(&psmouse->ps2dev, serio);
1323 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1324 psmouse->dev = input_dev;
1325 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1327 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1329 serio_set_drvdata(serio, psmouse);
1331 error = serio_open(serio, drv);
1332 if (error)
1333 goto err_clear_drvdata;
1335 if (psmouse_probe(psmouse) < 0) {
1336 error = -ENODEV;
1337 goto err_close_serio;
1340 psmouse->rate = psmouse_rate;
1341 psmouse->resolution = psmouse_resolution;
1342 psmouse->resetafter = psmouse_resetafter;
1343 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1344 psmouse->smartscroll = psmouse_smartscroll;
1346 psmouse_switch_protocol(psmouse, NULL);
1348 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1349 psmouse_initialize(psmouse);
1351 error = input_register_device(psmouse->dev);
1352 if (error)
1353 goto err_protocol_disconnect;
1355 if (parent && parent->pt_activate)
1356 parent->pt_activate(parent);
1358 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1359 if (error)
1360 goto err_pt_deactivate;
1362 psmouse_activate(psmouse);
1364 out:
1365 /* If this is a pass-through port the parent needs to be re-activated */
1366 if (parent)
1367 psmouse_activate(parent);
1369 mutex_unlock(&psmouse_mutex);
1370 return retval;
1372 err_pt_deactivate:
1373 if (parent && parent->pt_deactivate)
1374 parent->pt_deactivate(parent);
1375 input_unregister_device(psmouse->dev);
1376 input_dev = NULL; /* so we don't try to free it below */
1377 err_protocol_disconnect:
1378 if (psmouse->disconnect)
1379 psmouse->disconnect(psmouse);
1380 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1381 err_close_serio:
1382 serio_close(serio);
1383 err_clear_drvdata:
1384 serio_set_drvdata(serio, NULL);
1385 err_free:
1386 input_free_device(input_dev);
1387 kfree(psmouse);
1389 retval = error;
1390 goto out;
1394 static int psmouse_reconnect(struct serio *serio)
1396 struct psmouse *psmouse = serio_get_drvdata(serio);
1397 struct psmouse *parent = NULL;
1398 struct serio_driver *drv = serio->drv;
1399 unsigned char type;
1400 int rc = -1;
1402 if (!drv || !psmouse) {
1403 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1404 return -1;
1407 mutex_lock(&psmouse_mutex);
1409 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1410 parent = serio_get_drvdata(serio->parent);
1411 psmouse_deactivate(parent);
1414 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1416 if (psmouse->reconnect) {
1417 if (psmouse->reconnect(psmouse))
1418 goto out;
1419 } else {
1420 psmouse_reset(psmouse);
1422 if (psmouse_probe(psmouse) < 0)
1423 goto out;
1425 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1426 if (psmouse->type != type)
1427 goto out;
1430 /* ok, the device type (and capabilities) match the old one,
1431 * we can continue using it, complete intialization
1433 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1435 psmouse_initialize(psmouse);
1437 if (parent && parent->pt_activate)
1438 parent->pt_activate(parent);
1440 psmouse_activate(psmouse);
1441 rc = 0;
1443 out:
1444 /* If this is a pass-through port the parent waits to be activated */
1445 if (parent)
1446 psmouse_activate(parent);
1448 mutex_unlock(&psmouse_mutex);
1449 return rc;
1452 static struct serio_device_id psmouse_serio_ids[] = {
1454 .type = SERIO_8042,
1455 .proto = SERIO_ANY,
1456 .id = SERIO_ANY,
1457 .extra = SERIO_ANY,
1460 .type = SERIO_PS_PSTHRU,
1461 .proto = SERIO_ANY,
1462 .id = SERIO_ANY,
1463 .extra = SERIO_ANY,
1465 { 0 }
1468 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1470 static struct serio_driver psmouse_drv = {
1471 .driver = {
1472 .name = "psmouse",
1474 .description = DRIVER_DESC,
1475 .id_table = psmouse_serio_ids,
1476 .interrupt = psmouse_interrupt,
1477 .connect = psmouse_connect,
1478 .reconnect = psmouse_reconnect,
1479 .disconnect = psmouse_disconnect,
1480 .cleanup = psmouse_cleanup,
1483 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1484 char *buf)
1486 struct serio *serio = to_serio_port(dev);
1487 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1488 struct psmouse *psmouse;
1490 psmouse = serio_get_drvdata(serio);
1492 return attr->show(psmouse, attr->data, buf);
1495 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1496 const char *buf, size_t count)
1498 struct serio *serio = to_serio_port(dev);
1499 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1500 struct psmouse *psmouse, *parent = NULL;
1501 int retval;
1503 retval = mutex_lock_interruptible(&psmouse_mutex);
1504 if (retval)
1505 goto out;
1507 psmouse = serio_get_drvdata(serio);
1509 if (attr->protect) {
1510 if (psmouse->state == PSMOUSE_IGNORE) {
1511 retval = -ENODEV;
1512 goto out_unlock;
1515 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1516 parent = serio_get_drvdata(serio->parent);
1517 psmouse_deactivate(parent);
1520 psmouse_deactivate(psmouse);
1523 retval = attr->set(psmouse, attr->data, buf, count);
1525 if (attr->protect) {
1526 if (retval != -ENODEV)
1527 psmouse_activate(psmouse);
1529 if (parent)
1530 psmouse_activate(parent);
1533 out_unlock:
1534 mutex_unlock(&psmouse_mutex);
1535 out:
1536 return retval;
1539 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1541 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1543 return sprintf(buf, "%u\n", *field);
1546 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1548 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1549 unsigned long value;
1551 if (strict_strtoul(buf, 10, &value))
1552 return -EINVAL;
1554 if ((unsigned int)value != value)
1555 return -EINVAL;
1557 *field = value;
1559 return count;
1562 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1564 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1567 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1569 struct serio *serio = psmouse->ps2dev.serio;
1570 struct psmouse *parent = NULL;
1571 struct input_dev *old_dev, *new_dev;
1572 const struct psmouse_protocol *proto, *old_proto;
1573 int error;
1574 int retry = 0;
1576 proto = psmouse_protocol_by_name(buf, count);
1577 if (!proto)
1578 return -EINVAL;
1580 if (psmouse->type == proto->type)
1581 return count;
1583 new_dev = input_allocate_device();
1584 if (!new_dev)
1585 return -ENOMEM;
1587 while (!list_empty(&serio->children)) {
1588 if (++retry > 3) {
1589 printk(KERN_WARNING
1590 "psmouse: failed to destroy children ports, "
1591 "protocol change aborted.\n");
1592 input_free_device(new_dev);
1593 return -EIO;
1596 mutex_unlock(&psmouse_mutex);
1597 serio_unregister_child_port(serio);
1598 mutex_lock(&psmouse_mutex);
1600 if (serio->drv != &psmouse_drv) {
1601 input_free_device(new_dev);
1602 return -ENODEV;
1605 if (psmouse->type == proto->type) {
1606 input_free_device(new_dev);
1607 return count; /* switched by other thread */
1611 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1612 parent = serio_get_drvdata(serio->parent);
1613 if (parent->pt_deactivate)
1614 parent->pt_deactivate(parent);
1617 old_dev = psmouse->dev;
1618 old_proto = psmouse_protocol_by_type(psmouse->type);
1620 if (psmouse->disconnect)
1621 psmouse->disconnect(psmouse);
1623 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1625 psmouse->dev = new_dev;
1626 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1628 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1629 psmouse_reset(psmouse);
1630 /* default to PSMOUSE_PS2 */
1631 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1634 psmouse_initialize(psmouse);
1635 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1637 error = input_register_device(psmouse->dev);
1638 if (error) {
1639 if (psmouse->disconnect)
1640 psmouse->disconnect(psmouse);
1642 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1643 input_free_device(new_dev);
1644 psmouse->dev = old_dev;
1645 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1646 psmouse_switch_protocol(psmouse, old_proto);
1647 psmouse_initialize(psmouse);
1648 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1650 return error;
1653 input_unregister_device(old_dev);
1655 if (parent && parent->pt_activate)
1656 parent->pt_activate(parent);
1658 return count;
1661 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1663 unsigned long value;
1665 if (strict_strtoul(buf, 10, &value))
1666 return -EINVAL;
1668 psmouse->set_rate(psmouse, value);
1669 return count;
1672 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1674 unsigned long value;
1676 if (strict_strtoul(buf, 10, &value))
1677 return -EINVAL;
1679 psmouse->set_resolution(psmouse, value);
1680 return count;
1684 static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
1686 const struct psmouse_protocol *proto;
1688 if (!val)
1689 return -EINVAL;
1691 proto = psmouse_protocol_by_name(val, strlen(val));
1693 if (!proto || !proto->maxproto)
1694 return -EINVAL;
1696 *((unsigned int *)kp->arg) = proto->type;
1698 return 0;
1701 static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
1703 int type = *((unsigned int *)kp->arg);
1705 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1708 static int __init psmouse_init(void)
1710 int err;
1712 lifebook_module_init();
1713 synaptics_module_init();
1714 hgpk_module_init();
1716 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1717 if (!kpsmoused_wq) {
1718 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1719 return -ENOMEM;
1722 err = serio_register_driver(&psmouse_drv);
1723 if (err)
1724 destroy_workqueue(kpsmoused_wq);
1726 return err;
1729 static void __exit psmouse_exit(void)
1731 serio_unregister_driver(&psmouse_drv);
1732 destroy_workqueue(kpsmoused_wq);
1735 module_init(psmouse_init);
1736 module_exit(psmouse_exit);