4 * Copyright (c) 1999-2002 Vojtech Pavlik
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.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>
22 #include "synaptics.h"
23 #include "logips2pp.h"
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
26 MODULE_DESCRIPTION("PS/2 mouse driver");
27 MODULE_LICENSE("GPL");
29 static char *psmouse_proto;
30 static unsigned int psmouse_max_proto = -1U;
31 module_param_named(proto, psmouse_proto, charp, 0);
32 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps). Useful for KVM switches.");
34 int psmouse_resolution = 200;
35 module_param_named(resolution, psmouse_resolution, uint, 0);
36 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
38 unsigned int psmouse_rate = 100;
39 module_param_named(rate, psmouse_rate, uint, 0);
40 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
42 int psmouse_smartscroll = 1;
43 module_param_named(smartscroll, psmouse_smartscroll, bool, 0);
44 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
46 unsigned int psmouse_resetafter;
47 module_param_named(resetafter, psmouse_resetafter, uint, 0);
48 MODULE_PARM_DESC(resetafter, "Reset Synaptics Touchpad after so many bad packets (0 = never).");
50 __obsolete_setup("psmouse_noext");
51 __obsolete_setup("psmouse_resolution=");
52 __obsolete_setup("psmouse_smartscroll=");
53 __obsolete_setup("psmouse_resetafter=");
54 __obsolete_setup("psmouse_rate=");
56 static char *psmouse_protocols[] = { "None", "PS/2", "PS2++", "PS2T++", "GenPS/2", "ImPS/2", "ImExPS/2", "SynPS/2"};
59 * psmouse_process_packet() analyzes the PS/2 mouse packet contents and
60 * reports relevant events to the input module.
63 static void psmouse_process_packet(struct psmouse *psmouse, struct pt_regs *regs)
65 struct input_dev *dev = &psmouse->dev;
66 unsigned char *packet = psmouse->packet;
68 input_regs(dev, regs);
71 * The PS2++ protocol is a little bit complex
74 if (psmouse->type == PSMOUSE_PS2PP || psmouse->type == PSMOUSE_PS2TPP)
75 ps2pp_process_packet(psmouse);
78 * Scroll wheel on IntelliMice, scroll buttons on NetMice
81 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
82 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
85 * Scroll wheel and buttons on IntelliMouse Explorer
88 if (psmouse->type == PSMOUSE_IMEX) {
89 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
90 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
91 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
95 * Extra buttons on Genius NewNet 3D
98 if (psmouse->type == PSMOUSE_GENPS) {
99 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
100 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
107 input_report_key(dev, BTN_LEFT, packet[0] & 1);
108 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
109 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
111 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
112 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
118 * psmouse_interrupt() handles incoming characters, either gathering them into
119 * packets or passing them to the command routine as command output.
122 static irqreturn_t psmouse_interrupt(struct serio *serio,
123 unsigned char data, unsigned int flags, struct pt_regs *regs)
125 struct psmouse *psmouse = serio->private;
127 if (psmouse->state == PSMOUSE_IGNORE)
130 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
131 if (psmouse->state == PSMOUSE_ACTIVATED)
132 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
133 flags & SERIO_TIMEOUT ? " timeout" : "",
134 flags & SERIO_PARITY ? " bad parity" : "");
135 if (psmouse->acking) {
143 if (psmouse->acking) {
145 case PSMOUSE_RET_ACK:
148 case PSMOUSE_RET_NAK:
152 psmouse->ack = 1; /* Workaround for mice which don't ACK the Get ID command */
154 psmouse->cmdbuf[--psmouse->cmdcnt] = data;
161 if (psmouse->cmdcnt) {
162 psmouse->cmdbuf[--psmouse->cmdcnt] = data;
166 if (psmouse->state == PSMOUSE_ACTIVATED &&
167 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
168 printk(KERN_WARNING "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
169 psmouse->name, psmouse->phys, psmouse->pktcnt);
173 psmouse->last = jiffies;
174 psmouse->packet[psmouse->pktcnt++] = data;
176 if (psmouse->packet[0] == PSMOUSE_RET_BAT) {
177 if (psmouse->pktcnt == 1)
180 if (psmouse->pktcnt == 2) {
181 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
182 psmouse->state = PSMOUSE_IGNORE;
186 if (psmouse->type == PSMOUSE_SYNAPTICS) {
187 /* neither 0xAA nor 0x00 are valid first bytes
188 * for a packet in absolute mode
196 if (psmouse->type == PSMOUSE_SYNAPTICS) {
198 * The synaptics driver has its own resync logic,
199 * so it needs to receive all bytes one at a time.
201 synaptics_process_byte(psmouse, regs);
205 if (psmouse->pktcnt == 3 + (psmouse->type >= PSMOUSE_GENPS)) {
206 psmouse_process_packet(psmouse, regs);
215 * psmouse_sendbyte() sends a byte to the mouse, and waits for acknowledge.
216 * It doesn't handle retransmission, though it could - because when there would
217 * be need for retransmissions, the mouse has to be replaced anyway.
220 static int psmouse_sendbyte(struct psmouse *psmouse, unsigned char byte)
222 int timeout = 10000; /* 100 msec */
226 if (serio_write(psmouse->serio, byte)) {
231 while (!psmouse->ack && timeout--) udelay(10);
233 return -(psmouse->ack <= 0);
237 * psmouse_command() sends a command and its parameters to the mouse,
238 * then waits for the response and puts it in the param array.
241 int psmouse_command(struct psmouse *psmouse, unsigned char *param, int command)
243 int timeout = 500000; /* 500 msec */
244 int send = (command >> 12) & 0xf;
245 int receive = (command >> 8) & 0xf;
248 psmouse->cmdcnt = receive;
250 if (command == PSMOUSE_CMD_RESET_BAT)
251 timeout = 4000000; /* 4 sec */
253 /* initialize cmdbuf with preset values from param */
255 for (i = 0; i < receive; i++)
256 psmouse->cmdbuf[(receive - 1) - i] = param[i];
259 if (psmouse_sendbyte(psmouse, command & 0xff))
260 return (psmouse->cmdcnt = 0) - 1;
262 for (i = 0; i < send; i++)
263 if (psmouse_sendbyte(psmouse, param[i]))
264 return (psmouse->cmdcnt = 0) - 1;
266 while (psmouse->cmdcnt && timeout--) {
268 if (psmouse->cmdcnt == 1 && command == PSMOUSE_CMD_RESET_BAT &&
269 timeout > 100000) /* do not run in a endless loop */
270 timeout = 100000; /* 1 sec */
272 if (psmouse->cmdcnt == 1 && command == PSMOUSE_CMD_GETID &&
273 psmouse->cmdbuf[1] != 0xab && psmouse->cmdbuf[1] != 0xac) {
281 for (i = 0; i < receive; i++)
282 param[i] = psmouse->cmdbuf[(receive - 1) - i];
285 return (psmouse->cmdcnt = 0) - 1;
292 * psmouse_reset() resets the mouse into power-on state.
294 int psmouse_reset(struct psmouse *psmouse)
296 unsigned char param[2];
298 if (psmouse_command(psmouse, param, PSMOUSE_CMD_RESET_BAT))
301 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
309 * Genius NetMouse magic init.
311 static int genius_detect(struct psmouse *psmouse)
313 unsigned char param[4];
316 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRES);
317 psmouse_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11);
318 psmouse_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11);
319 psmouse_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11);
320 psmouse_command(psmouse, param, PSMOUSE_CMD_GETINFO);
322 return param[0] == 0x00 && param[1] == 0x33 && param[2] == 0x55;
326 * IntelliMouse magic init.
328 static int intellimouse_detect(struct psmouse *psmouse)
330 unsigned char param[2];
333 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRATE);
335 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRATE);
337 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRATE);
338 psmouse_command(psmouse, param, PSMOUSE_CMD_GETID);
340 return param[0] == 3;
344 * Try IntelliMouse/Explorer magic init.
346 static int im_explorer_detect(struct psmouse *psmouse)
348 unsigned char param[2];
351 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRATE);
353 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRATE);
355 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRATE);
356 psmouse_command(psmouse, param, PSMOUSE_CMD_GETID);
358 return param[0] == 4;
362 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
363 * the mouse may have.
366 static int psmouse_extensions(struct psmouse *psmouse)
368 int synaptics_hardware = 0;
370 psmouse->vendor = "Generic";
371 psmouse->name = "Mouse";
375 * Try Synaptics TouchPad
377 if (psmouse_max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse)) {
378 synaptics_hardware = 1;
379 psmouse->vendor = "Synaptics";
380 psmouse->name = "TouchPad";
382 if (psmouse_max_proto > PSMOUSE_IMEX) {
383 if (synaptics_init(psmouse) == 0)
384 return PSMOUSE_SYNAPTICS;
386 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
387 * Unfortunately Logitech/Genius probes confuse some firmware versions so
388 * we'll have to skip them.
390 psmouse_max_proto = PSMOUSE_IMEX;
393 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
395 synaptics_reset(psmouse);
398 if (psmouse_max_proto > PSMOUSE_IMEX && genius_detect(psmouse)) {
399 set_bit(BTN_EXTRA, psmouse->dev.keybit);
400 set_bit(BTN_SIDE, psmouse->dev.keybit);
401 set_bit(REL_WHEEL, psmouse->dev.relbit);
403 psmouse->vendor = "Genius";
404 psmouse->name = "Wheel Mouse";
405 return PSMOUSE_GENPS;
408 if (psmouse_max_proto > PSMOUSE_IMEX) {
409 int type = ps2pp_detect(psmouse);
414 if (psmouse_max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse)) {
415 set_bit(REL_WHEEL, psmouse->dev.relbit);
417 if (psmouse_max_proto >= PSMOUSE_IMEX &&
418 im_explorer_detect(psmouse)) {
419 set_bit(BTN_SIDE, psmouse->dev.keybit);
420 set_bit(BTN_EXTRA, psmouse->dev.keybit);
422 psmouse->name = "Explorer Mouse";
426 psmouse->name = "Wheel Mouse";
431 * Okay, all failed, we have a standard mouse here. The number of the buttons
432 * is still a question, though. We assume 3.
434 if (synaptics_hardware) {
436 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
437 * We need to reset the touchpad because if there is a track point on the
438 * pass through port it could get disabled while probing for protocol
441 psmouse_reset(psmouse);
442 psmouse_command(psmouse, NULL, PSMOUSE_CMD_RESET_DIS);
449 * psmouse_probe() probes for a PS/2 mouse.
452 static int psmouse_probe(struct psmouse *psmouse)
454 unsigned char param[2];
457 * First, we check if it's a mouse. It should send 0x00 or 0x03
458 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
463 if (psmouse_command(psmouse, param, PSMOUSE_CMD_GETID))
466 if (param[0] != 0x00 && param[0] != 0x03 && param[0] != 0x04)
470 * Then we reset and disable the mouse so that it doesn't generate events.
473 if (psmouse_command(psmouse, NULL, PSMOUSE_CMD_RESET_DIS))
474 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", psmouse->serio->phys);
477 * And here we try to determine if it has any extensions over the
478 * basic PS/2 3-button mouse.
481 return psmouse->type = psmouse_extensions(psmouse);
485 * Here we set the mouse resolution.
488 static void psmouse_set_resolution(struct psmouse *psmouse)
490 unsigned char param[1];
492 if (psmouse->type == PSMOUSE_PS2PP && psmouse_resolution > 400) {
493 ps2pp_set_800dpi(psmouse);
497 if (!psmouse_resolution || psmouse_resolution >= 200)
499 else if (psmouse_resolution >= 100)
501 else if (psmouse_resolution >= 50)
503 else if (psmouse_resolution)
506 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRES);
510 * Here we set the mouse report rate.
513 static void psmouse_set_rate(struct psmouse *psmouse)
515 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
518 while (rates[i] > psmouse_rate) i++;
519 psmouse_command(psmouse, rates + i, PSMOUSE_CMD_SETRATE);
523 * psmouse_initialize() initializes the mouse to a sane state.
526 static void psmouse_initialize(struct psmouse *psmouse)
528 unsigned char param[2];
531 * We set the mouse report rate, resolution and scaling.
534 if (psmouse_max_proto != PSMOUSE_PS2) {
535 psmouse_set_rate(psmouse);
536 psmouse_set_resolution(psmouse);
537 psmouse_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11);
541 * We set the mouse into streaming mode.
544 psmouse_command(psmouse, param, PSMOUSE_CMD_SETSTREAM);
548 * psmouse_activate() enables the mouse so that we get motion reports from it.
551 static void psmouse_activate(struct psmouse *psmouse)
553 if (psmouse_command(psmouse, NULL, PSMOUSE_CMD_ENABLE))
554 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n", psmouse->serio->phys);
556 psmouse->state = PSMOUSE_ACTIVATED;
560 * psmouse_cleanup() resets the mouse into power-on state.
563 static void psmouse_cleanup(struct serio *serio)
565 struct psmouse *psmouse = serio->private;
567 psmouse_reset(psmouse);
571 * psmouse_disconnect() closes and frees.
574 static void psmouse_disconnect(struct serio *serio)
576 struct psmouse *psmouse = serio->private;
578 psmouse->state = PSMOUSE_CMD_MODE;
580 if (psmouse->ptport) {
581 if (psmouse->ptport->deactivate)
582 psmouse->ptport->deactivate(psmouse);
583 __serio_unregister_port(&psmouse->ptport->serio); /* we have serio_sem */
584 kfree(psmouse->ptport);
585 psmouse->ptport = NULL;
588 if (psmouse->disconnect)
589 psmouse->disconnect(psmouse);
591 psmouse->state = PSMOUSE_IGNORE;
593 input_unregister_device(&psmouse->dev);
599 * psmouse_connect() is a callback from the serio module when
600 * an unhandled serio port is found.
602 static void psmouse_connect(struct serio *serio, struct serio_dev *dev)
604 struct psmouse *psmouse;
606 if ((serio->type & SERIO_TYPE) != SERIO_8042 &&
607 (serio->type & SERIO_TYPE) != SERIO_PS_PSTHRU)
610 if (!(psmouse = kmalloc(sizeof(struct psmouse), GFP_KERNEL)))
613 memset(psmouse, 0, sizeof(struct psmouse));
615 init_input_dev(&psmouse->dev);
616 psmouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
617 psmouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
618 psmouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
620 psmouse->state = PSMOUSE_CMD_MODE;
621 psmouse->serio = serio;
622 psmouse->dev.private = psmouse;
624 serio->private = psmouse;
625 if (serio_open(serio, dev)) {
627 serio->private = NULL;
631 if (psmouse_probe(psmouse) <= 0) {
634 serio->private = NULL;
638 sprintf(psmouse->devname, "%s %s %s",
639 psmouse_protocols[psmouse->type], psmouse->vendor, psmouse->name);
640 sprintf(psmouse->phys, "%s/input0",
643 psmouse->dev.name = psmouse->devname;
644 psmouse->dev.phys = psmouse->phys;
645 psmouse->dev.id.bustype = BUS_I8042;
646 psmouse->dev.id.vendor = 0x0002;
647 psmouse->dev.id.product = psmouse->type;
648 psmouse->dev.id.version = psmouse->model;
650 input_register_device(&psmouse->dev);
652 printk(KERN_INFO "input: %s on %s\n", psmouse->devname, serio->phys);
654 psmouse_initialize(psmouse);
656 if (psmouse->ptport) {
657 printk(KERN_INFO "serio: %s port at %s\n", psmouse->ptport->serio.name, psmouse->phys);
658 __serio_register_port(&psmouse->ptport->serio); /* we have serio_sem */
659 if (psmouse->ptport->activate)
660 psmouse->ptport->activate(psmouse);
663 psmouse_activate(psmouse);
667 static int psmouse_reconnect(struct serio *serio)
669 struct psmouse *psmouse = serio->private;
670 struct serio_dev *dev = serio->dev;
673 if (!dev || !psmouse) {
674 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
678 old_type = psmouse->type;
680 psmouse->state = PSMOUSE_CMD_MODE;
681 psmouse->type = psmouse->acking = psmouse->cmdcnt = psmouse->pktcnt = 0;
682 if (psmouse->reconnect) {
683 if (psmouse->reconnect(psmouse))
685 } else if (psmouse_probe(psmouse) != old_type)
688 /* ok, the device type (and capabilities) match the old one,
689 * we can continue using it, complete intialization
691 psmouse->type = old_type;
692 psmouse_initialize(psmouse);
694 if (psmouse->ptport) {
695 if (psmouse_reconnect(&psmouse->ptport->serio)) {
696 __serio_unregister_port(&psmouse->ptport->serio);
697 __serio_register_port(&psmouse->ptport->serio);
698 if (psmouse->ptport->activate)
699 psmouse->ptport->activate(psmouse);
703 psmouse_activate(psmouse);
708 static struct serio_dev psmouse_dev = {
709 .interrupt = psmouse_interrupt,
710 .connect = psmouse_connect,
711 .reconnect = psmouse_reconnect,
712 .disconnect = psmouse_disconnect,
713 .cleanup = psmouse_cleanup,
716 static inline void psmouse_parse_proto(void)
719 if (!strcmp(psmouse_proto, "bare"))
720 psmouse_max_proto = PSMOUSE_PS2;
721 else if (!strcmp(psmouse_proto, "imps"))
722 psmouse_max_proto = PSMOUSE_IMPS;
723 else if (!strcmp(psmouse_proto, "exps"))
724 psmouse_max_proto = PSMOUSE_IMEX;
726 printk(KERN_ERR "psmouse: unknown protocol type '%s'\n", psmouse_proto);
730 int __init psmouse_init(void)
732 psmouse_parse_proto();
733 serio_register_device(&psmouse_dev);
737 void __exit psmouse_exit(void)
739 serio_unregister_device(&psmouse_dev);
742 module_init(psmouse_init);
743 module_exit(psmouse_exit);