14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #define psmouse_fmt(fmt) fmt
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/input.h>
22 #include <linux/serio.h>
38 #define DRIVER_DESC "PS/2 mouse driver"
45 static int psmouse_set_maxproto(
const char *
val,
const struct kernel_param *);
48 .set = psmouse_set_maxproto,
49 .get = psmouse_get_maxproto,
51 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
53 MODULE_PARM_DESC(
proto,
"Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
55 static unsigned int psmouse_resolution = 200;
59 static unsigned int psmouse_rate = 100;
63 static bool psmouse_smartscroll = 1;
65 MODULE_PARM_DESC(smartscroll,
"Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
67 static unsigned int psmouse_resetafter = 5;
69 MODULE_PARM_DESC(resetafter,
"Reset device after so many bad packets (0 = never).");
71 static unsigned int psmouse_resync_time;
73 MODULE_PARM_DESC(resync_time,
"How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
77 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
80 psmouse_show_int_attr, psmouse_attr_set_rate);
83 psmouse_show_int_attr, psmouse_attr_set_resolution);
86 psmouse_show_int_attr, psmouse_set_int_attr);
89 psmouse_show_int_attr, psmouse_set_int_attr);
91 static struct attribute *psmouse_attributes[] = {
92 &psmouse_attr_protocol.dattr.attr,
93 &psmouse_attr_rate.dattr.attr,
94 &psmouse_attr_resolution.dattr.attr,
95 &psmouse_attr_resetafter.dattr.attr,
96 &psmouse_attr_resync_time.dattr.attr,
101 .attrs = psmouse_attributes,
132 struct input_dev *
dev = psmouse->
dev;
147 input_report_rel(dev,
REL_WHEEL, -(
signed char) packet[3]);
154 switch (packet[3] & 0xC0) {
156 input_report_rel(dev,
REL_WHEEL, (
int) (packet[3] & 32) - (
int) (packet[3] & 31));
159 input_report_rel(dev,
REL_HWHEEL, (
int) (packet[3] & 32) - (
int) (packet[3] & 31));
163 input_report_rel(dev,
REL_WHEEL, (
int) (packet[3] & 8) - (
int) (packet[3] & 7));
164 input_report_key(dev,
BTN_SIDE, (packet[3] >> 4) & 1);
165 input_report_key(dev,
BTN_EXTRA, (packet[3] >> 5) & 1);
175 input_report_key(dev,
BTN_SIDE, (packet[0] >> 6) & 1);
176 input_report_key(dev,
BTN_EXTRA, (packet[0] >> 7) & 1);
183 input_report_key(dev,
BTN_EXTRA, (packet[0] >> 3) & 1);
185 packet[1] |= (packet[0] & 0x40) << 1;
193 input_report_key(dev,
BTN_SIDE, (packet[0] >> 3) & 1);
201 input_report_key(dev,
BTN_LEFT, packet[0] & 1);
202 input_report_key(dev,
BTN_MIDDLE, (packet[0] >> 2) & 1);
203 input_report_key(dev,
BTN_RIGHT, (packet[0] >> 1) & 1);
205 input_report_rel(dev,
REL_X, packet[1] ? (
int) packet[1] - (
int) ((packet[0] << 4) & 0x100) : 0);
206 input_report_rel(dev,
REL_Y, packet[2] ? (
int) ((packet[0] << 3) & 0x100) - (
int) packet[2] : 0);
227 psmouse->
ps2dev.flags = 0;
240 serio_pause_rx(psmouse->
ps2dev.serio);
241 __psmouse_set_state(psmouse, new_state);
242 serio_continue_rx(psmouse->
ps2dev.serio);
258 "%s at %s lost sync at byte %d\n",
264 "issuing reconnect request\n");
277 "%s at %s - driver resynced.\n",
296 struct psmouse *psmouse = serio_get_drvdata(serio);
306 "bad data from KBC -%s%s\n",
307 flags & SERIO_TIMEOUT ?
" timeout" :
"",
308 flags & SERIO_PARITY ?
" bad parity" :
"");
326 psmouse_info(psmouse,
"%s at %s lost synchronization, throwing %d bytes away.\n",
339 if (psmouse->
pktcnt == 1) {
355 if (psmouse_handle_byte(psmouse))
374 psmouse_handle_byte(psmouse);
395 for (i = 6; i >= 0; i -= 2) {
396 unsigned char d = (command >>
i) & 3;
410 unsigned char param[2];
427 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
430 if (resolution == 0 || resolution > 200)
433 p = params[resolution / 50];
442 static void psmouse_set_rate(
struct psmouse *psmouse,
unsigned int rate)
444 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
448 while (rates[i] > rate) i++;
458 static int psmouse_poll(
struct psmouse *psmouse)
468 static int genius_detect(
struct psmouse *psmouse,
bool set_properties)
471 unsigned char param[4];
480 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
483 if (set_properties) {
489 psmouse->
vendor =
"Genius";
490 psmouse->
name =
"Mouse";
500 static int intellimouse_detect(
struct psmouse *psmouse,
bool set_properties)
502 struct ps2dev *ps2dev = &psmouse->
ps2dev;
503 unsigned char param[2];
516 if (set_properties) {
521 psmouse->
vendor =
"Generic";
523 psmouse->
name =
"Wheel Mouse";
533 static int im_explorer_detect(
struct psmouse *psmouse,
bool set_properties)
535 struct ps2dev *ps2dev = &psmouse->
ps2dev;
536 unsigned char param[2];
538 intellimouse_detect(psmouse, 0);
559 if (set_properties) {
567 psmouse->
vendor =
"Generic";
569 psmouse->
name =
"Explorer Mouse";
579 static int thinking_detect(
struct psmouse *psmouse,
bool set_properties)
581 struct ps2dev *ps2dev = &psmouse->
ps2dev;
582 unsigned char param[2];
583 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
599 if (set_properties) {
603 psmouse->
vendor =
"Kensington";
604 psmouse->
name =
"ThinkingMouse";
613 static int ps2bare_detect(
struct psmouse *psmouse,
bool set_properties)
615 if (set_properties) {
617 psmouse->
vendor =
"Generic";
619 psmouse->
name =
"Mouse";
635 static int cortron_detect(
struct psmouse *psmouse,
bool set_properties)
637 if (set_properties) {
638 psmouse->
vendor =
"Cortron";
639 psmouse->
name =
"PS/2 Trackball";
653 static void psmouse_apply_defaults(
struct psmouse *psmouse)
655 struct input_dev *input_dev = psmouse->
dev;
657 memset(input_dev->evbit, 0,
sizeof(input_dev->evbit));
658 memset(input_dev->keybit, 0,
sizeof(input_dev->keybit));
659 memset(input_dev->relbit, 0,
sizeof(input_dev->relbit));
660 memset(input_dev->absbit, 0,
sizeof(input_dev->absbit));
661 memset(input_dev->mscbit, 0,
sizeof(input_dev->mscbit));
672 psmouse->
set_rate = psmouse_set_rate;
674 psmouse->
poll = psmouse_poll;
688 static int psmouse_do_detect(
int (*detect)(
struct psmouse *psmouse,
689 bool set_properties),
690 struct psmouse *psmouse,
bool set_properties)
693 psmouse_apply_defaults(psmouse);
695 return detect(psmouse, set_properties);
703 static int psmouse_extensions(
struct psmouse *psmouse,
704 unsigned int max_proto,
bool set_properties)
706 bool synaptics_hardware =
false;
712 if (psmouse_do_detect(
lifebook_detect, psmouse, set_properties) == 0) {
725 psmouse_do_detect(thinking_detect, psmouse, set_properties) == 0) {
736 synaptics_hardware =
true;
767 psmouse, set_properties) == 0) {
768 if (!set_properties ||
alps_init(psmouse) == 0)
781 psmouse_do_detect(
hgpk_detect, psmouse, set_properties) == 0) {
782 if (!set_properties ||
hgpk_init(psmouse) == 0)
804 if (psmouse_do_detect(genius_detect,
805 psmouse, set_properties) == 0)
809 psmouse, set_properties) == 0)
813 psmouse, set_properties) == 0)
817 psmouse, set_properties) == 0)
827 psmouse, set_properties) == 0) {
828 if (!set_properties ||
fsp_init(psmouse) == 0)
846 psmouse_do_detect(im_explorer_detect,
847 psmouse, set_properties) == 0) {
852 psmouse_do_detect(intellimouse_detect,
853 psmouse, set_properties) == 0) {
861 psmouse_do_detect(ps2bare_detect, psmouse, set_properties);
863 if (synaptics_hardware) {
882 .ignore_parity =
true,
883 .detect = ps2bare_detect,
885 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
897 .detect = thinking_detect,
903 .detect = genius_detect,
910 .ignore_parity =
true,
911 .detect = intellimouse_detect,
918 .ignore_parity =
true,
919 .detect = im_explorer_detect,
921 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
925 .alias =
"synaptics",
931 .name =
"SynRelPS/2",
932 .alias =
"synaptics-relative",
937 #ifdef CONFIG_MOUSE_PS2_ALPS
946 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
954 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
958 .alias =
"trackpoint",
962 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
965 .name =
"touchkitPS/2",
970 #ifdef CONFIG_MOUSE_PS2_OLPC
978 #ifdef CONFIG_MOUSE_PS2_ELANTECH
987 #ifdef CONFIG_MOUSE_PS2_SENTELIC
998 .name =
"CortronPS/2",
1000 .detect = cortron_detect,
1014 for (i = 0; i <
ARRAY_SIZE(psmouse_protocols); i++)
1015 if (psmouse_protocols[i].type == type)
1016 return &psmouse_protocols[
i];
1019 return &psmouse_protocols[0];
1027 for (i = 0; i <
ARRAY_SIZE(psmouse_protocols); i++) {
1028 p = &psmouse_protocols[
i];
1032 return &psmouse_protocols[
i];
1043 static int psmouse_probe(
struct psmouse *psmouse)
1045 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1046 unsigned char param[2];
1059 if (param[0] != 0x00 && param[0] != 0x03 &&
1060 param[0] != 0x04 && param[0] != 0xff)
1069 ps2dev->
serio->phys);
1078 static void psmouse_initialize(
struct psmouse *psmouse)
1098 psmouse_warn(psmouse,
"Failed to enable mouse on %s\n",
1099 psmouse->
ps2dev.serio->phys);
1115 psmouse_warn(psmouse,
"Failed to deactivate mouse on %s\n",
1116 psmouse->
ps2dev.serio->phys);
1133 struct serio *serio = psmouse->ps2dev.serio;
1135 bool failed =
false,
enabled =
false;
1144 parent = serio_get_drvdata(serio->
parent);
1158 psmouse->num_resyncs++;
1161 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1164 psmouse->acks_disable_command =
true;
1174 if (psmouse->poll(psmouse))
1178 for (i = 0; i < psmouse->pktsize; i++) {
1180 rc = psmouse->protocol_handler(psmouse);
1194 for (i = 0; i < 5; i++) {
1203 psmouse_warn(psmouse,
"failed to re-enable mouse on %s\n",
1204 psmouse->ps2dev.serio->phys);
1211 "resync failed, issuing reconnect request\n");
1226 static void psmouse_cleanup(
struct serio *serio)
1228 struct psmouse *psmouse = serio_get_drvdata(serio);
1229 struct psmouse *parent =
NULL;
1234 parent = serio_get_drvdata(serio->
parent);
1244 psmouse_warn(psmouse,
"Failed to disable mouse on %s\n",
1245 psmouse->
ps2dev.serio->phys);
1275 static void psmouse_disconnect(
struct serio *serio)
1277 struct psmouse *psmouse, *parent =
NULL;
1279 psmouse = serio_get_drvdata(serio);
1293 parent = serio_get_drvdata(serio->
parent);
1306 serio_set_drvdata(serio,
NULL);
1307 input_unregister_device(psmouse->
dev);
1316 static int psmouse_switch_protocol(
struct psmouse *psmouse,
1320 struct input_dev *input_dev = psmouse->
dev;
1322 input_dev->dev.parent = &psmouse->
ps2dev.serio->dev;
1324 if (proto && (proto->
detect || proto->
init)) {
1325 psmouse_apply_defaults(psmouse);
1330 if (proto->
init && proto->
init(psmouse) < 0)
1334 selected_proto =
proto;
1336 psmouse->
type = psmouse_extensions(psmouse,
1337 psmouse_max_proto,
true);
1338 selected_proto = psmouse_protocol_by_type(psmouse->
type);
1363 input_dev->name = psmouse->
devname;
1364 input_dev->phys = psmouse->
phys;
1366 input_dev->id.vendor = 0x0002;
1367 input_dev->id.product = psmouse->
type;
1368 input_dev->id.version = psmouse->
model;
1377 static int psmouse_connect(
struct serio *serio,
struct serio_driver *drv)
1379 struct psmouse *psmouse, *parent =
NULL;
1380 struct input_dev *input_dev;
1390 parent = serio_get_drvdata(serio->
parent);
1394 psmouse = kzalloc(
sizeof(
struct psmouse),
GFP_KERNEL);
1395 input_dev = input_allocate_device();
1396 if (!psmouse || !input_dev)
1401 psmouse->
dev = input_dev;
1406 serio_set_drvdata(serio, psmouse);
1410 goto err_clear_drvdata;
1412 if (psmouse_probe(psmouse) < 0) {
1414 goto err_close_serio;
1417 psmouse->
rate = psmouse_rate;
1420 psmouse->
resync_time = parent ? 0 : psmouse_resync_time;
1423 psmouse_switch_protocol(psmouse,
NULL);
1426 psmouse_initialize(psmouse);
1428 error = input_register_device(psmouse->
dev);
1430 goto err_protocol_disconnect;
1437 goto err_pt_deactivate;
1452 input_unregister_device(psmouse->
dev);
1454 err_protocol_disconnect:
1461 serio_set_drvdata(serio,
NULL);
1463 input_free_device(input_dev);
1471 static int psmouse_reconnect(
struct serio *serio)
1473 struct psmouse *psmouse = serio_get_drvdata(serio);
1474 struct psmouse *parent =
NULL;
1479 if (!drv || !psmouse) {
1481 "reconnect request, but serio is disconnected, ignoring...\n");
1488 parent = serio_get_drvdata(serio->
parent);
1500 if (psmouse_probe(psmouse) < 0)
1503 type = psmouse_extensions(psmouse, psmouse_max_proto,
false);
1504 if (psmouse->
type != type)
1514 psmouse_initialize(psmouse);
1554 .id_table = psmouse_serio_ids,
1555 .interrupt = psmouse_interrupt,
1556 .connect = psmouse_connect,
1557 .reconnect = psmouse_reconnect,
1558 .disconnect = psmouse_disconnect,
1559 .cleanup = psmouse_cleanup,
1567 struct psmouse *psmouse;
1569 psmouse = serio_get_drvdata(serio);
1571 return attr->
show(psmouse, attr->
data, buf);
1579 struct psmouse *psmouse, *parent =
NULL;
1586 psmouse = serio_get_drvdata(serio);
1595 parent = serio_get_drvdata(serio->
parent);
1602 retval = attr->
set(psmouse, attr->
data, buf, count);
1618 static ssize_t psmouse_show_int_attr(
struct psmouse *psmouse,
void *
offset,
char *
buf)
1620 unsigned int *
field = (
unsigned int *)((
char *)psmouse + (
size_t)offset);
1622 return sprintf(buf,
"%u\n", *field);
1625 static ssize_t psmouse_set_int_attr(
struct psmouse *psmouse,
void *offset,
const char *buf,
size_t count)
1627 unsigned int *field = (
unsigned int *)((
char *)psmouse + (
size_t)offset);
1640 static ssize_t psmouse_attr_show_protocol(
struct psmouse *psmouse,
void *data,
char *buf)
1642 return sprintf(buf,
"%s\n", psmouse_protocol_by_type(psmouse->
type)->name);
1645 static ssize_t psmouse_attr_set_protocol(
struct psmouse *psmouse,
void *data,
const char *buf,
size_t count)
1647 struct serio *serio = psmouse->
ps2dev.serio;
1648 struct psmouse *parent =
NULL;
1649 struct input_dev *old_dev, *new_dev;
1654 proto = psmouse_protocol_by_name(buf, count);
1661 new_dev = input_allocate_device();
1665 while (!list_empty(&serio->
children)) {
1668 "failed to destroy children ports, protocol change aborted.\n");
1669 input_free_device(new_dev);
1677 if (serio->
drv != &psmouse_drv) {
1678 input_free_device(new_dev);
1682 if (psmouse->
type == proto->
type) {
1683 input_free_device(new_dev);
1689 parent = serio_get_drvdata(serio->
parent);
1694 old_dev = psmouse->
dev;
1695 old_proto = psmouse_protocol_by_type(psmouse->
type);
1702 psmouse->
dev = new_dev;
1705 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1708 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1711 psmouse_initialize(psmouse);
1714 error = input_register_device(psmouse->
dev);
1720 input_free_device(new_dev);
1721 psmouse->
dev = old_dev;
1723 psmouse_switch_protocol(psmouse, old_proto);
1724 psmouse_initialize(psmouse);
1730 input_unregister_device(old_dev);
1738 static ssize_t psmouse_attr_set_rate(
struct psmouse *psmouse,
void *data,
const char *buf,
size_t count)
1751 static ssize_t psmouse_attr_set_resolution(
struct psmouse *psmouse,
void *data,
const char *buf,
size_t count)
1765 static int psmouse_set_maxproto(
const char *
val,
const struct kernel_param *kp)
1772 proto = psmouse_protocol_by_name(val,
strlen(val));
1777 *((
unsigned int *)kp->
arg) = proto->
type;
1784 int type = *((
unsigned int *)kp->
arg);
1786 return sprintf(buffer,
"%s", psmouse_protocol_by_type(type)->name);
1789 static int __init psmouse_init(
void)
1798 if (!kpsmoused_wq) {
1799 pr_err(
"failed to create kpsmoused workqueue\n");
1810 static void __exit psmouse_exit(
void)