Linux v2.6.13
[linux-2.6/next.git] / drivers / input / mouse / psmouse-base.c
blob2bb2fe78bdca988f34ccae1cdebf0ba0faff79c5
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/moduleparam.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/input.h>
20 #include <linux/serio.h>
21 #include <linux/init.h>
22 #include <linux/libps2.h>
23 #include "psmouse.h"
24 #include "synaptics.h"
25 #include "logips2pp.h"
26 #include "alps.h"
27 #include "lifebook.h"
29 #define DRIVER_DESC "PS/2 mouse driver"
31 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
32 MODULE_DESCRIPTION(DRIVER_DESC);
33 MODULE_LICENSE("GPL");
35 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
36 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
37 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
38 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
39 #define param_set_proto_abbrev psmouse_set_maxproto
40 #define param_get_proto_abbrev psmouse_get_maxproto
41 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
42 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
44 static unsigned int psmouse_resolution = 200;
45 module_param_named(resolution, psmouse_resolution, uint, 0644);
46 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
48 static unsigned int psmouse_rate = 100;
49 module_param_named(rate, psmouse_rate, uint, 0644);
50 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
52 static unsigned int psmouse_smartscroll = 1;
53 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
54 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
56 static unsigned int psmouse_resetafter;
57 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
58 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
60 PSMOUSE_DEFINE_ATTR(protocol);
61 PSMOUSE_DEFINE_ATTR(rate);
62 PSMOUSE_DEFINE_ATTR(resolution);
63 PSMOUSE_DEFINE_ATTR(resetafter);
65 __obsolete_setup("psmouse_noext");
66 __obsolete_setup("psmouse_resolution=");
67 __obsolete_setup("psmouse_smartscroll=");
68 __obsolete_setup("psmouse_resetafter=");
69 __obsolete_setup("psmouse_rate=");
72 * psmouse_sem protects all operations changing state of mouse
73 * (connecting, disconnecting, changing rate or resolution via
74 * sysfs). We could use a per-device semaphore but since there
75 * rarely more than one PS/2 mouse connected and since semaphore
76 * is taken in "slow" paths it is not worth it.
78 static DECLARE_MUTEX(psmouse_sem);
80 struct psmouse_protocol {
81 enum psmouse_type type;
82 char *name;
83 char *alias;
84 int maxproto;
85 int (*detect)(struct psmouse *, int);
86 int (*init)(struct psmouse *);
90 * psmouse_process_byte() analyzes the PS/2 data stream and reports
91 * relevant events to the input module once full packet has arrived.
94 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
96 struct input_dev *dev = &psmouse->dev;
97 unsigned char *packet = psmouse->packet;
99 if (psmouse->pktcnt < psmouse->pktsize)
100 return PSMOUSE_GOOD_DATA;
103 * Full packet accumulated, process it
106 input_regs(dev, regs);
109 * Scroll wheel on IntelliMice, scroll buttons on NetMice
112 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
113 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
116 * Scroll wheel and buttons on IntelliMouse Explorer
119 if (psmouse->type == PSMOUSE_IMEX) {
120 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
121 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
122 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
126 * Extra buttons on Genius NewNet 3D
129 if (psmouse->type == PSMOUSE_GENPS) {
130 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
131 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
135 * Extra button on ThinkingMouse
137 if (psmouse->type == PSMOUSE_THINKPS) {
138 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
139 /* Without this bit of weirdness moving up gives wildly high Y changes. */
140 packet[1] |= (packet[0] & 0x40) << 1;
144 * Generic PS/2 Mouse
147 input_report_key(dev, BTN_LEFT, packet[0] & 1);
148 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
149 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
151 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
152 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
154 input_sync(dev);
156 return PSMOUSE_FULL_PACKET;
160 * psmouse_interrupt() handles incoming characters, either gathering them into
161 * packets or passing them to the command routine as command output.
164 static irqreturn_t psmouse_interrupt(struct serio *serio,
165 unsigned char data, unsigned int flags, struct pt_regs *regs)
167 struct psmouse *psmouse = serio_get_drvdata(serio);
168 psmouse_ret_t rc;
170 if (psmouse->state == PSMOUSE_IGNORE)
171 goto out;
173 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
174 if (psmouse->state == PSMOUSE_ACTIVATED)
175 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
176 flags & SERIO_TIMEOUT ? " timeout" : "",
177 flags & SERIO_PARITY ? " bad parity" : "");
178 ps2_cmd_aborted(&psmouse->ps2dev);
179 goto out;
182 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
183 if (ps2_handle_ack(&psmouse->ps2dev, data))
184 goto out;
186 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
187 if (ps2_handle_response(&psmouse->ps2dev, data))
188 goto out;
190 if (psmouse->state == PSMOUSE_INITIALIZING)
191 goto out;
193 if (psmouse->state == PSMOUSE_ACTIVATED &&
194 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
195 printk(KERN_WARNING "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
196 psmouse->name, psmouse->phys, psmouse->pktcnt);
197 psmouse->pktcnt = 0;
200 psmouse->last = jiffies;
201 psmouse->packet[psmouse->pktcnt++] = data;
203 if (psmouse->packet[0] == PSMOUSE_RET_BAT) {
204 if (psmouse->pktcnt == 1)
205 goto out;
207 if (psmouse->pktcnt == 2) {
208 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
209 psmouse->state = PSMOUSE_IGNORE;
210 serio_reconnect(serio);
211 goto out;
213 if (psmouse->type == PSMOUSE_SYNAPTICS) {
214 /* neither 0xAA nor 0x00 are valid first bytes
215 * for a packet in absolute mode
217 psmouse->pktcnt = 0;
218 goto out;
223 rc = psmouse->protocol_handler(psmouse, regs);
225 switch (rc) {
226 case PSMOUSE_BAD_DATA:
227 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
228 psmouse->name, psmouse->phys, psmouse->pktcnt);
229 psmouse->pktcnt = 0;
231 if (++psmouse->out_of_sync == psmouse->resetafter) {
232 psmouse->state = PSMOUSE_IGNORE;
233 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
234 serio_reconnect(psmouse->ps2dev.serio);
236 break;
238 case PSMOUSE_FULL_PACKET:
239 psmouse->pktcnt = 0;
240 if (psmouse->out_of_sync) {
241 psmouse->out_of_sync = 0;
242 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
243 psmouse->name, psmouse->phys);
245 break;
247 case PSMOUSE_GOOD_DATA:
248 break;
250 out:
251 return IRQ_HANDLED;
256 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
257 * using sliced syntax, understood by advanced devices, such as Logitech
258 * or Synaptics touchpads. The command is encoded as:
259 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
260 * is the command.
262 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
264 int i;
266 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
267 return -1;
269 for (i = 6; i >= 0; i -= 2) {
270 unsigned char d = (command >> i) & 3;
271 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
272 return -1;
275 return 0;
280 * psmouse_reset() resets the mouse into power-on state.
282 int psmouse_reset(struct psmouse *psmouse)
284 unsigned char param[2];
286 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
287 return -1;
289 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
290 return -1;
292 return 0;
297 * Genius NetMouse magic init.
299 static int genius_detect(struct psmouse *psmouse, int set_properties)
301 struct ps2dev *ps2dev = &psmouse->ps2dev;
302 unsigned char param[4];
304 param[0] = 3;
305 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
306 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
307 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
308 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
309 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
311 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
312 return -1;
314 if (set_properties) {
315 set_bit(BTN_EXTRA, psmouse->dev.keybit);
316 set_bit(BTN_SIDE, psmouse->dev.keybit);
317 set_bit(REL_WHEEL, psmouse->dev.relbit);
319 psmouse->vendor = "Genius";
320 psmouse->name = "Wheel Mouse";
321 psmouse->pktsize = 4;
324 return 0;
328 * IntelliMouse magic init.
330 static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
332 struct ps2dev *ps2dev = &psmouse->ps2dev;
333 unsigned char param[2];
335 param[0] = 200;
336 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
337 param[0] = 100;
338 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
339 param[0] = 80;
340 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
341 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
343 if (param[0] != 3)
344 return -1;
346 if (set_properties) {
347 set_bit(BTN_MIDDLE, psmouse->dev.keybit);
348 set_bit(REL_WHEEL, psmouse->dev.relbit);
350 if (!psmouse->vendor) psmouse->vendor = "Generic";
351 if (!psmouse->name) psmouse->name = "Wheel Mouse";
352 psmouse->pktsize = 4;
355 return 0;
359 * Try IntelliMouse/Explorer magic init.
361 static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
363 struct ps2dev *ps2dev = &psmouse->ps2dev;
364 unsigned char param[2];
366 intellimouse_detect(psmouse, 0);
368 param[0] = 200;
369 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
370 param[0] = 200;
371 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
372 param[0] = 80;
373 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
374 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
376 if (param[0] != 4)
377 return -1;
379 if (set_properties) {
380 set_bit(BTN_MIDDLE, psmouse->dev.keybit);
381 set_bit(REL_WHEEL, psmouse->dev.relbit);
382 set_bit(BTN_SIDE, psmouse->dev.keybit);
383 set_bit(BTN_EXTRA, psmouse->dev.keybit);
385 if (!psmouse->vendor) psmouse->vendor = "Generic";
386 if (!psmouse->name) psmouse->name = "Explorer Mouse";
387 psmouse->pktsize = 4;
390 return 0;
394 * Kensington ThinkingMouse / ExpertMouse magic init.
396 static int thinking_detect(struct psmouse *psmouse, int set_properties)
398 struct ps2dev *ps2dev = &psmouse->ps2dev;
399 unsigned char param[2];
400 unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
401 int i;
403 param[0] = 10;
404 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
405 param[0] = 0;
406 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
407 for (i = 0; seq[i]; i++)
408 ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE);
409 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
411 if (param[0] != 2)
412 return -1;
414 if (set_properties) {
415 set_bit(BTN_EXTRA, psmouse->dev.keybit);
417 psmouse->vendor = "Kensington";
418 psmouse->name = "ThinkingMouse";
421 return 0;
425 * Bare PS/2 protocol "detection". Always succeeds.
427 static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
429 if (set_properties) {
430 if (!psmouse->vendor) psmouse->vendor = "Generic";
431 if (!psmouse->name) psmouse->name = "Mouse";
434 return 0;
439 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
440 * the mouse may have.
443 static int psmouse_extensions(struct psmouse *psmouse,
444 unsigned int max_proto, int set_properties)
446 int synaptics_hardware = 0;
449 * We always check for lifebook because it does not disturb mouse
450 * (it only checks DMI information).
452 if (lifebook_detect(psmouse, set_properties) == 0) {
453 if (max_proto > PSMOUSE_IMEX) {
454 if (!set_properties || lifebook_init(psmouse) == 0)
455 return PSMOUSE_LIFEBOOK;
460 * Try Kensington ThinkingMouse (we try first, because synaptics probe
461 * upsets the thinkingmouse).
464 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
465 return PSMOUSE_THINKPS;
468 * Try Synaptics TouchPad
470 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
471 synaptics_hardware = 1;
473 if (max_proto > PSMOUSE_IMEX) {
474 if (!set_properties || synaptics_init(psmouse) == 0)
475 return PSMOUSE_SYNAPTICS;
477 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
478 * Unfortunately Logitech/Genius probes confuse some firmware versions so
479 * we'll have to skip them.
481 max_proto = PSMOUSE_IMEX;
484 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
486 synaptics_reset(psmouse);
490 * Try ALPS TouchPad
492 if (max_proto > PSMOUSE_IMEX) {
493 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
494 if (alps_detect(psmouse, set_properties) == 0) {
495 if (!set_properties || alps_init(psmouse) == 0)
496 return PSMOUSE_ALPS;
498 * Init failed, try basic relative protocols
500 max_proto = PSMOUSE_IMEX;
504 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
505 return PSMOUSE_GENPS;
507 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
508 return PSMOUSE_PS2PP;
511 * Reset to defaults in case the device got confused by extended
512 * protocol probes.
514 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
516 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
517 return PSMOUSE_IMEX;
519 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
520 return PSMOUSE_IMPS;
523 * Okay, all failed, we have a standard mouse here. The number of the buttons
524 * is still a question, though. We assume 3.
526 ps2bare_detect(psmouse, set_properties);
528 if (synaptics_hardware) {
530 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
531 * We need to reset the touchpad because if there is a track point on the
532 * pass through port it could get disabled while probing for protocol
533 * extensions.
535 psmouse_reset(psmouse);
536 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
539 return PSMOUSE_PS2;
542 static struct psmouse_protocol psmouse_protocols[] = {
544 .type = PSMOUSE_PS2,
545 .name = "PS/2",
546 .alias = "bare",
547 .maxproto = 1,
548 .detect = ps2bare_detect,
551 .type = PSMOUSE_PS2PP,
552 .name = "PS2++",
553 .alias = "logitech",
554 .detect = ps2pp_init,
557 .type = PSMOUSE_THINKPS,
558 .name = "ThinkPS/2",
559 .alias = "thinkps",
560 .detect = thinking_detect,
563 .type = PSMOUSE_GENPS,
564 .name = "GenPS/2",
565 .alias = "genius",
566 .detect = genius_detect,
569 .type = PSMOUSE_IMPS,
570 .name = "ImPS/2",
571 .alias = "imps",
572 .maxproto = 1,
573 .detect = intellimouse_detect,
576 .type = PSMOUSE_IMEX,
577 .name = "ImExPS/2",
578 .alias = "exps",
579 .maxproto = 1,
580 .detect = im_explorer_detect,
583 .type = PSMOUSE_SYNAPTICS,
584 .name = "SynPS/2",
585 .alias = "synaptics",
586 .detect = synaptics_detect,
587 .init = synaptics_init,
590 .type = PSMOUSE_ALPS,
591 .name = "AlpsPS/2",
592 .alias = "alps",
593 .detect = alps_detect,
594 .init = alps_init,
597 .type = PSMOUSE_LIFEBOOK,
598 .name = "LBPS/2",
599 .alias = "lifebook",
600 .init = lifebook_init,
603 .type = PSMOUSE_AUTO,
604 .name = "auto",
605 .alias = "any",
606 .maxproto = 1,
610 static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
612 int i;
614 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
615 if (psmouse_protocols[i].type == type)
616 return &psmouse_protocols[i];
618 WARN_ON(1);
619 return &psmouse_protocols[0];
622 static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
624 struct psmouse_protocol *p;
625 int i;
627 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
628 p = &psmouse_protocols[i];
630 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
631 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
632 return &psmouse_protocols[i];
635 return NULL;
640 * psmouse_probe() probes for a PS/2 mouse.
643 static int psmouse_probe(struct psmouse *psmouse)
645 struct ps2dev *ps2dev = &psmouse->ps2dev;
646 unsigned char param[2];
649 * First, we check if it's a mouse. It should send 0x00 or 0x03
650 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
651 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
652 * ID queries, probably due to a firmware bug.
655 param[0] = 0xa5;
656 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
657 return -1;
659 if (param[0] != 0x00 && param[0] != 0x03 &&
660 param[0] != 0x04 && param[0] != 0xff)
661 return -1;
664 * Then we reset and disable the mouse so that it doesn't generate events.
667 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
668 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
670 return 0;
674 * Here we set the mouse resolution.
677 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
679 unsigned char params[] = { 0, 1, 2, 2, 3 };
681 if (resolution == 0 || resolution > 200)
682 resolution = 200;
684 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
685 psmouse->resolution = 25 << params[resolution / 50];
689 * Here we set the mouse report rate.
692 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
694 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
695 int i = 0;
697 while (rates[i] > rate) i++;
698 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
699 psmouse->rate = rates[i];
703 * psmouse_initialize() initializes the mouse to a sane state.
706 static void psmouse_initialize(struct psmouse *psmouse)
709 * We set the mouse into streaming mode.
712 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
715 * We set the mouse report rate, resolution and scaling.
718 if (psmouse_max_proto != PSMOUSE_PS2) {
719 psmouse->set_rate(psmouse, psmouse->rate);
720 psmouse->set_resolution(psmouse, psmouse->resolution);
721 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
726 * psmouse_set_state() sets new psmouse state and resets all flags and
727 * counters while holding serio lock so fighting with interrupt handler
728 * is not a concern.
731 static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
733 serio_pause_rx(psmouse->ps2dev.serio);
734 psmouse->state = new_state;
735 psmouse->pktcnt = psmouse->out_of_sync = 0;
736 psmouse->ps2dev.flags = 0;
737 serio_continue_rx(psmouse->ps2dev.serio);
741 * psmouse_activate() enables the mouse so that we get motion reports from it.
744 static void psmouse_activate(struct psmouse *psmouse)
746 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
747 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
748 psmouse->ps2dev.serio->phys);
750 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
755 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
756 * reports from it unless we explicitely request it.
759 static void psmouse_deactivate(struct psmouse *psmouse)
761 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
762 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
763 psmouse->ps2dev.serio->phys);
765 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
770 * psmouse_cleanup() resets the mouse into power-on state.
773 static void psmouse_cleanup(struct serio *serio)
775 struct psmouse *psmouse = serio_get_drvdata(serio);
777 psmouse_reset(psmouse);
781 * psmouse_disconnect() closes and frees.
784 static void psmouse_disconnect(struct serio *serio)
786 struct psmouse *psmouse, *parent = NULL;
788 psmouse = serio_get_drvdata(serio);
790 device_remove_file(&serio->dev, &psmouse_attr_protocol);
791 device_remove_file(&serio->dev, &psmouse_attr_rate);
792 device_remove_file(&serio->dev, &psmouse_attr_resolution);
793 device_remove_file(&serio->dev, &psmouse_attr_resetafter);
795 down(&psmouse_sem);
797 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
799 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
800 parent = serio_get_drvdata(serio->parent);
801 psmouse_deactivate(parent);
804 if (psmouse->disconnect)
805 psmouse->disconnect(psmouse);
807 if (parent && parent->pt_deactivate)
808 parent->pt_deactivate(parent);
810 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
812 input_unregister_device(&psmouse->dev);
813 serio_close(serio);
814 serio_set_drvdata(serio, NULL);
815 kfree(psmouse);
817 if (parent)
818 psmouse_activate(parent);
820 up(&psmouse_sem);
823 static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto)
825 memset(&psmouse->dev, 0, sizeof(struct input_dev));
827 init_input_dev(&psmouse->dev);
829 psmouse->dev.private = psmouse;
830 psmouse->dev.dev = &psmouse->ps2dev.serio->dev;
832 psmouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
833 psmouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
834 psmouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
836 psmouse->set_rate = psmouse_set_rate;
837 psmouse->set_resolution = psmouse_set_resolution;
838 psmouse->protocol_handler = psmouse_process_byte;
839 psmouse->pktsize = 3;
841 if (proto && (proto->detect || proto->init)) {
842 if (proto->detect && proto->detect(psmouse, 1) < 0)
843 return -1;
845 if (proto->init && proto->init(psmouse) < 0)
846 return -1;
848 psmouse->type = proto->type;
850 else
851 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
853 sprintf(psmouse->devname, "%s %s %s",
854 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
856 psmouse->dev.name = psmouse->devname;
857 psmouse->dev.phys = psmouse->phys;
858 psmouse->dev.id.bustype = BUS_I8042;
859 psmouse->dev.id.vendor = 0x0002;
860 psmouse->dev.id.product = psmouse->type;
861 psmouse->dev.id.version = psmouse->model;
863 return 0;
867 * psmouse_connect() is a callback from the serio module when
868 * an unhandled serio port is found.
870 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
872 struct psmouse *psmouse, *parent = NULL;
873 int retval;
875 down(&psmouse_sem);
878 * If this is a pass-through port deactivate parent so the device
879 * connected to this port can be successfully identified
881 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
882 parent = serio_get_drvdata(serio->parent);
883 psmouse_deactivate(parent);
886 if (!(psmouse = kcalloc(1, sizeof(struct psmouse), GFP_KERNEL))) {
887 retval = -ENOMEM;
888 goto out;
891 ps2_init(&psmouse->ps2dev, serio);
892 sprintf(psmouse->phys, "%s/input0", serio->phys);
894 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
896 serio_set_drvdata(serio, psmouse);
898 retval = serio_open(serio, drv);
899 if (retval) {
900 serio_set_drvdata(serio, NULL);
901 kfree(psmouse);
902 goto out;
905 if (psmouse_probe(psmouse) < 0) {
906 serio_close(serio);
907 serio_set_drvdata(serio, NULL);
908 kfree(psmouse);
909 retval = -ENODEV;
910 goto out;
913 psmouse->rate = psmouse_rate;
914 psmouse->resolution = psmouse_resolution;
915 psmouse->resetafter = psmouse_resetafter;
916 psmouse->smartscroll = psmouse_smartscroll;
918 psmouse_switch_protocol(psmouse, NULL);
920 input_register_device(&psmouse->dev);
921 printk(KERN_INFO "input: %s on %s\n", psmouse->devname, serio->phys);
923 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
925 psmouse_initialize(psmouse);
927 if (parent && parent->pt_activate)
928 parent->pt_activate(parent);
930 device_create_file(&serio->dev, &psmouse_attr_protocol);
931 device_create_file(&serio->dev, &psmouse_attr_rate);
932 device_create_file(&serio->dev, &psmouse_attr_resolution);
933 device_create_file(&serio->dev, &psmouse_attr_resetafter);
935 psmouse_activate(psmouse);
937 retval = 0;
939 out:
940 /* If this is a pass-through port the parent needs to be re-activated */
941 if (parent)
942 psmouse_activate(parent);
944 up(&psmouse_sem);
945 return retval;
949 static int psmouse_reconnect(struct serio *serio)
951 struct psmouse *psmouse = serio_get_drvdata(serio);
952 struct psmouse *parent = NULL;
953 struct serio_driver *drv = serio->drv;
954 int rc = -1;
956 if (!drv || !psmouse) {
957 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
958 return -1;
961 down(&psmouse_sem);
963 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
964 parent = serio_get_drvdata(serio->parent);
965 psmouse_deactivate(parent);
968 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
970 if (psmouse->reconnect) {
971 if (psmouse->reconnect(psmouse))
972 goto out;
973 } else if (psmouse_probe(psmouse) < 0 ||
974 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
975 goto out;
977 /* ok, the device type (and capabilities) match the old one,
978 * we can continue using it, complete intialization
980 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
982 psmouse_initialize(psmouse);
984 if (parent && parent->pt_activate)
985 parent->pt_activate(parent);
987 psmouse_activate(psmouse);
988 rc = 0;
990 out:
991 /* If this is a pass-through port the parent waits to be activated */
992 if (parent)
993 psmouse_activate(parent);
995 up(&psmouse_sem);
996 return rc;
999 static struct serio_device_id psmouse_serio_ids[] = {
1001 .type = SERIO_8042,
1002 .proto = SERIO_ANY,
1003 .id = SERIO_ANY,
1004 .extra = SERIO_ANY,
1007 .type = SERIO_PS_PSTHRU,
1008 .proto = SERIO_ANY,
1009 .id = SERIO_ANY,
1010 .extra = SERIO_ANY,
1012 { 0 }
1015 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1017 static struct serio_driver psmouse_drv = {
1018 .driver = {
1019 .name = "psmouse",
1021 .description = DRIVER_DESC,
1022 .id_table = psmouse_serio_ids,
1023 .interrupt = psmouse_interrupt,
1024 .connect = psmouse_connect,
1025 .reconnect = psmouse_reconnect,
1026 .disconnect = psmouse_disconnect,
1027 .cleanup = psmouse_cleanup,
1030 ssize_t psmouse_attr_show_helper(struct device *dev, char *buf,
1031 ssize_t (*handler)(struct psmouse *, char *))
1033 struct serio *serio = to_serio_port(dev);
1034 int retval;
1036 retval = serio_pin_driver(serio);
1037 if (retval)
1038 return retval;
1040 if (serio->drv != &psmouse_drv) {
1041 retval = -ENODEV;
1042 goto out;
1045 retval = handler(serio_get_drvdata(serio), buf);
1047 out:
1048 serio_unpin_driver(serio);
1049 return retval;
1052 ssize_t psmouse_attr_set_helper(struct device *dev, const char *buf, size_t count,
1053 ssize_t (*handler)(struct psmouse *, const char *, size_t))
1055 struct serio *serio = to_serio_port(dev);
1056 struct psmouse *psmouse = serio_get_drvdata(serio);
1057 struct psmouse *parent = NULL;
1058 int retval;
1060 retval = serio_pin_driver(serio);
1061 if (retval)
1062 return retval;
1064 if (serio->drv != &psmouse_drv) {
1065 retval = -ENODEV;
1066 goto out_unpin;
1069 retval = down_interruptible(&psmouse_sem);
1070 if (retval)
1071 goto out_unpin;
1073 if (psmouse->state == PSMOUSE_IGNORE) {
1074 retval = -ENODEV;
1075 goto out_up;
1078 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1079 parent = serio_get_drvdata(serio->parent);
1080 psmouse_deactivate(parent);
1083 psmouse_deactivate(psmouse);
1085 retval = handler(psmouse, buf, count);
1087 if (retval != -ENODEV)
1088 psmouse_activate(psmouse);
1090 if (parent)
1091 psmouse_activate(parent);
1093 out_up:
1094 up(&psmouse_sem);
1095 out_unpin:
1096 serio_unpin_driver(serio);
1097 return retval;
1100 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, char *buf)
1102 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1105 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, const char *buf, size_t count)
1107 struct serio *serio = psmouse->ps2dev.serio;
1108 struct psmouse *parent = NULL;
1109 struct psmouse_protocol *proto;
1110 int retry = 0;
1112 if (!(proto = psmouse_protocol_by_name(buf, count)))
1113 return -EINVAL;
1115 if (psmouse->type == proto->type)
1116 return count;
1118 while (serio->child) {
1119 if (++retry > 3) {
1120 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
1121 return -EIO;
1124 up(&psmouse_sem);
1125 serio_unpin_driver(serio);
1126 serio_unregister_child_port(serio);
1127 serio_pin_driver_uninterruptible(serio);
1128 down(&psmouse_sem);
1130 if (serio->drv != &psmouse_drv)
1131 return -ENODEV;
1133 if (psmouse->type == proto->type)
1134 return count; /* switched by other thread */
1137 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1138 parent = serio_get_drvdata(serio->parent);
1139 if (parent->pt_deactivate)
1140 parent->pt_deactivate(parent);
1143 if (psmouse->disconnect)
1144 psmouse->disconnect(psmouse);
1146 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1147 input_unregister_device(&psmouse->dev);
1149 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1151 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1152 psmouse_reset(psmouse);
1153 /* default to PSMOUSE_PS2 */
1154 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1157 psmouse_initialize(psmouse);
1158 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1160 input_register_device(&psmouse->dev);
1161 printk(KERN_INFO "input: %s on %s\n", psmouse->devname, serio->phys);
1163 if (parent && parent->pt_activate)
1164 parent->pt_activate(parent);
1166 return count;
1169 static ssize_t psmouse_attr_show_rate(struct psmouse *psmouse, char *buf)
1171 return sprintf(buf, "%d\n", psmouse->rate);
1174 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, const char *buf, size_t count)
1176 unsigned long value;
1177 char *rest;
1179 value = simple_strtoul(buf, &rest, 10);
1180 if (*rest)
1181 return -EINVAL;
1183 psmouse->set_rate(psmouse, value);
1184 return count;
1187 static ssize_t psmouse_attr_show_resolution(struct psmouse *psmouse, char *buf)
1189 return sprintf(buf, "%d\n", psmouse->resolution);
1192 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, const char *buf, size_t count)
1194 unsigned long value;
1195 char *rest;
1197 value = simple_strtoul(buf, &rest, 10);
1198 if (*rest)
1199 return -EINVAL;
1201 psmouse->set_resolution(psmouse, value);
1202 return count;
1205 static ssize_t psmouse_attr_show_resetafter(struct psmouse *psmouse, char *buf)
1207 return sprintf(buf, "%d\n", psmouse->resetafter);
1210 static ssize_t psmouse_attr_set_resetafter(struct psmouse *psmouse, const char *buf, size_t count)
1212 unsigned long value;
1213 char *rest;
1215 value = simple_strtoul(buf, &rest, 10);
1216 if (*rest)
1217 return -EINVAL;
1219 psmouse->resetafter = value;
1220 return count;
1223 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1225 struct psmouse_protocol *proto;
1227 if (!val)
1228 return -EINVAL;
1230 proto = psmouse_protocol_by_name(val, strlen(val));
1232 if (!proto || !proto->maxproto)
1233 return -EINVAL;
1235 *((unsigned int *)kp->arg) = proto->type;
1237 return 0; \
1240 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1242 int type = *((unsigned int *)kp->arg);
1244 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
1247 static int __init psmouse_init(void)
1249 serio_register_driver(&psmouse_drv);
1250 return 0;
1253 static void __exit psmouse_exit(void)
1255 serio_unregister_driver(&psmouse_drv);
1258 module_init(psmouse_init);
1259 module_exit(psmouse_exit);