18 #include <linux/slab.h>
19 #include <linux/input.h>
21 #include <linux/serio.h>
30 #define ALPS_V3_X_MAX 2000
31 #define ALPS_V3_Y_MAX 1400
33 #define ALPS_BITMAP_X_BITS 15
34 #define ALPS_BITMAP_Y_BITS 11
36 #define ALPS_CMD_NIBBLE_10 0x01f2
77 #define ALPS_DUALPOINT 0x02
78 #define ALPS_PASS 0x04
80 #define ALPS_WHEEL 0x08
81 #define ALPS_FW_BK_1 0x10
82 #define ALPS_FW_BK_2 0x20
83 #define ALPS_FOUR_BUTTONS 0x40
84 #define ALPS_PS2_INTERLEAVED 0x80
101 { { 0x73, 0x02, 0x0a }, 0x00,
ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
114 { { 0x73, 0x02, 0x64 }, 0x8a,
ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
125 static bool alps_is_valid_first_byte(
const struct alps_model_info *model,
132 struct input_dev *dev1,
struct input_dev *dev2,
135 struct input_dev *
dev;
143 input_report_key(dev,
BTN_LEFT, left);
163 struct input_dev *
dev = psmouse->
dev;
164 struct input_dev *dev2 = priv->
dev2;
166 int back = 0, forward = 0;
169 left = packet[2] & 0x10;
170 right = packet[2] & 0x08;
172 x = packet[1] | ((packet[0] & 0x07) << 7);
173 y = packet[4] | ((packet[3] & 0x07) << 7);
176 left = packet[3] & 1;
177 right = packet[3] & 2;
178 middle = packet[3] & 4;
179 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
180 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
185 back = packet[0] & 0x10;
186 forward = packet[2] & 4;
190 back = packet[3] & 4;
191 forward = packet[2] & 4;
192 if ((middle = forward && back))
200 input_report_rel(dev2,
REL_X, (x > 383 ? (x - 768) : x));
201 input_report_rel(dev2,
REL_Y, -(y > 255 ? (y - 512) : y));
203 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
209 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
220 if (ges && fin && !priv->
prev_fin) {
221 input_report_abs(dev,
ABS_X, x);
222 input_report_abs(dev,
ABS_Y, y);
235 input_report_abs(dev,
ABS_X, x);
236 input_report_abs(dev,
ABS_Y, y);
243 input_report_rel(dev,
REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
247 input_report_key(dev,
BTN_BACK, back);
251 input_report_key(dev,
BTN_0, packet[2] & 4);
252 input_report_key(dev,
BTN_1, packet[0] & 0x10);
253 input_report_key(dev,
BTN_2, packet[3] & 4);
254 input_report_key(dev,
BTN_3, packet[0] & 0x20);
270 static int alps_process_bitmap(
unsigned int x_map,
unsigned int y_map,
271 int *x1,
int *y1,
int *x2,
int *y2)
273 struct alps_bitmap_point {
278 int fingers_x = 0, fingers_y = 0, fingers;
279 int i,
bit, prev_bit;
280 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
281 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
282 struct alps_bitmap_point *
point;
284 if (!x_map || !y_map)
287 *x1 = *y1 = *x2 = *y2 = 0;
291 for (i = 0; x_map != 0; i++, x_map >>= 1) {
295 point->start_bit =
i;
315 for (i = 0; y_map != 0; i++, y_map <<= 1) {
319 point->start_bit =
i;
336 fingers =
max(fingers_x, fingers_y);
345 if (fingers_x == 1) {
346 i = x_low.num_bits / 2;
347 x_low.num_bits = x_low.num_bits -
i;
348 x_high.start_bit = x_low.start_bit +
i;
349 x_high.num_bits =
max(i, 1);
350 }
else if (fingers_y == 1) {
351 i = y_low.num_bits / 2;
352 y_low.num_bits = y_low.num_bits -
i;
353 y_high.start_bit = y_low.start_bit +
i;
354 y_high.num_bits =
max(i, 1);
358 *x1 = (
ALPS_V3_X_MAX * (2 * x_low.start_bit + x_low.num_bits - 1)) /
360 *y1 = (
ALPS_V3_Y_MAX * (2 * y_low.start_bit + y_low.num_bits - 1)) /
364 *x2 = (
ALPS_V3_X_MAX * (2 * x_high.start_bit + x_high.num_bits - 1)) /
366 *y2 = (
ALPS_V3_Y_MAX * (2 * y_high.start_bit + y_high.num_bits - 1)) /
373 static void alps_set_slot(
struct input_dev *dev,
int slot,
bool active,
384 static void alps_report_semi_mt_data(
struct input_dev *dev,
int num_fingers,
385 int x1,
int y1,
int x2,
int y2)
387 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
388 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
391 static void alps_process_trackstick_packet_v3(
struct psmouse *psmouse)
394 unsigned char *packet = psmouse->
packet;
395 struct input_dev *dev = priv->
dev2;
399 if (!(packet[0] & 0x40)) {
400 psmouse_dbg(psmouse,
"Bad trackstick packet, discarding\n");
408 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
411 x = (
s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
412 y = (
s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
413 z = (packet[4] & 0x7c) >> 2;
423 input_report_rel(dev,
REL_X, x);
424 input_report_rel(dev,
REL_Y, -y);
433 left = packet[3] & 0x01;
434 right = packet[3] & 0x02;
435 middle = packet[3] & 0x04;
438 (left || right || middle))
442 input_report_key(dev,
BTN_LEFT, left);
451 static void alps_process_touchpad_packet_v3(
struct psmouse *psmouse)
454 unsigned char *packet = psmouse->
packet;
455 struct input_dev *dev = psmouse->
dev;
456 struct input_dev *dev2 = priv->
dev2;
459 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
460 int fingers = 0, bmap_fingers;
461 unsigned int x_bitmap, y_bitmap;
476 if (packet[0] & 0x40) {
477 fingers = (packet[5] & 0x3) + 1;
478 x_bitmap = ((packet[4] & 0x7e) << 8) |
479 ((packet[1] & 0x7f) << 2) |
480 ((packet[0] & 0x30) >> 4);
481 y_bitmap = ((packet[3] & 0x70) << 4) |
482 ((packet[2] & 0x7f) << 1) |
485 bmap_fingers = alps_process_bitmap(x_bitmap, y_bitmap,
492 if (fingers > 1 && bmap_fingers < 2)
493 fingers = bmap_fingers;
510 if (packet[0] & 0x40)
521 left = packet[3] & 0x01;
522 right = packet[3] & 0x02;
523 middle = packet[3] & 0x04;
525 x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) |
526 ((packet[0] & 0x30) >> 4);
527 y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f);
528 z = packet[5] & 0x7f;
546 fingers = z > 0 ? 1 : 0;
554 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
558 input_report_key(dev,
BTN_LEFT, left);
563 input_report_abs(dev,
ABS_X, x);
564 input_report_abs(dev,
ABS_Y, y);
571 left = packet[3] & 0x10;
572 right = packet[3] & 0x20;
573 middle = packet[3] & 0x40;
575 input_report_key(dev2,
BTN_LEFT, left);
576 input_report_key(dev2,
BTN_RIGHT, right);
582 static void alps_process_packet_v3(
struct psmouse *psmouse)
584 unsigned char *packet = psmouse->
packet;
594 if (packet[5] == 0x3f) {
595 alps_process_trackstick_packet_v3(psmouse);
599 alps_process_touchpad_packet_v3(psmouse);
602 static void alps_process_packet_v4(
struct psmouse *psmouse)
605 unsigned char *packet = psmouse->
packet;
606 struct input_dev *dev = psmouse->
dev;
612 unsigned int x_bitmap, y_bitmap;
619 if (packet[6] & 0x40) {
634 x_bitmap = ((priv->
multi_data[2] & 0x1f) << 10) |
638 y_bitmap = ((priv->
multi_data[5] & 0x01) << 10) |
642 fingers = alps_process_bitmap(x_bitmap, y_bitmap,
653 left = packet[4] & 0x01;
654 right = packet[4] & 0x02;
656 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
657 ((packet[0] & 0x30) >> 4);
658 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
659 z = packet[5] & 0x7f;
669 fingers = z > 0 ? 1 : 0;
683 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
687 input_report_key(dev,
BTN_LEFT, left);
691 input_report_abs(dev,
ABS_X, x);
692 input_report_abs(dev,
ABS_Y, y);
699 static void alps_process_packet(
struct psmouse *psmouse)
707 alps_process_packet_v1_v2(psmouse);
710 alps_process_packet_v3(psmouse);
713 alps_process_packet_v4(psmouse);
718 static void alps_report_bare_ps2_packet(
struct psmouse *psmouse,
719 unsigned char packet[],
723 struct input_dev *dev2 = priv->
dev2;
726 alps_report_buttons(psmouse, dev2, psmouse->
dev,
727 packet[0] & 1, packet[0] & 2, packet[0] & 4);
729 input_report_rel(dev2,
REL_X,
730 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
731 input_report_rel(dev2,
REL_Y,
732 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
737 static psmouse_ret_t alps_handle_interleaved_ps2(
struct psmouse *psmouse)
744 if (psmouse->
pktcnt == 6) {
757 if (psmouse->
packet[6] & 0x80) {
765 if (((psmouse->
packet[3] |
767 psmouse->
packet[5]) & 0x80) ||
768 (!alps_is_valid_first_byte(priv->
i, psmouse->
packet[6]))) {
770 "refusing packet %x %x %x %x (suspected interleaved ps/2)\n",
776 alps_process_packet(psmouse);
800 alps_report_bare_ps2_packet(psmouse, &psmouse->
packet[3],
817 static void alps_flush_packet(
unsigned long data)
819 struct psmouse *psmouse = (
struct psmouse *)data;
821 serio_pause_rx(psmouse->
ps2dev.serio);
832 psmouse->
packet[5]) & 0x80) {
834 "refusing packet %x %x %x (suspected interleaved ps/2)\n",
838 alps_process_packet(psmouse);
843 serio_continue_rx(psmouse->
ps2dev.serio);
846 static psmouse_ret_t alps_process_byte(
struct psmouse *psmouse)
851 if ((psmouse->
packet[0] & 0xc8) == 0x08) {
852 if (psmouse->
pktcnt == 3) {
853 alps_report_bare_ps2_packet(psmouse, psmouse->
packet,
863 psmouse->
pktcnt >= 4 && (psmouse->
packet[3] & 0x0f) == 0x0f) {
864 return alps_handle_interleaved_ps2(psmouse);
867 if (!alps_is_valid_first_byte(model, psmouse->
packet[0])) {
869 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
884 alps_process_packet(psmouse);
891 static int alps_command_mode_send_nibble(
struct psmouse *psmouse,
int nibble)
896 unsigned char *
param;
897 unsigned char dummy[4];
902 param = (command & 0x0f00) ?
911 static int alps_command_mode_set_addr(
struct psmouse *psmouse,
int addr)
913 struct ps2dev *ps2dev = &psmouse->
ps2dev;
920 for (i = 12; i >= 0; i -= 4) {
921 nibble = (addr >>
i) & 0xf;
922 if (alps_command_mode_send_nibble(psmouse, nibble))
929 static int __alps_command_mode_read_reg(
struct psmouse *psmouse,
int addr)
931 struct ps2dev *ps2dev = &psmouse->
ps2dev;
932 unsigned char param[4];
942 if (addr != ((param[0] << 8) | param[1]))
948 static int alps_command_mode_read_reg(
struct psmouse *psmouse,
int addr)
950 if (alps_command_mode_set_addr(psmouse, addr))
952 return __alps_command_mode_read_reg(psmouse, addr);
955 static int __alps_command_mode_write_reg(
struct psmouse *psmouse,
u8 value)
957 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
959 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
964 static int alps_command_mode_write_reg(
struct psmouse *psmouse,
int addr,
967 if (alps_command_mode_set_addr(psmouse, addr))
969 return __alps_command_mode_write_reg(psmouse, value);
972 static int alps_enter_command_mode(
struct psmouse *psmouse,
975 unsigned char param[4];
976 struct ps2dev *ps2dev = &psmouse->
ps2dev;
982 psmouse_err(psmouse,
"failed to enter command mode\n");
986 if (param[0] != 0x88 && param[1] != 0x07) {
988 "unknown response while entering command mode: %2.2x %2.2x %2.2x\n",
989 param[0], param[1], param[2]);
998 static inline int alps_exit_command_mode(
struct psmouse *psmouse)
1000 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1008 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1009 static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
1010 unsigned char param[4];
1027 param[0] = param[1] = param[2] = 0xff;
1031 psmouse_dbg(psmouse,
"E6 report: %2.2x %2.2x %2.2x",
1032 param[0], param[1], param[2]);
1034 if ((param[0] & 0xf8) != 0 || param[1] != 0 ||
1035 (param[2] != 10 && param[2] != 100))
1049 param[0] = param[1] = param[2] = 0xff;
1053 psmouse_dbg(psmouse,
"E7 report: %2.2x %2.2x %2.2x",
1054 param[0], param[1], param[2]);
1057 for (i = 0; i <
ARRAY_SIZE(rates) && param[2] != rates[
i]; i++)
1059 *version = (param[0] << 8) | (param[1] << 4) |
i;
1062 for (i = 0; i <
ARRAY_SIZE(alps_model_data); i++) {
1064 sizeof(alps_model_data[i].signature))) {
1065 model = alps_model_data +
i;
1076 if (alps_enter_command_mode(psmouse, param)) {
1078 "touchpad failed to enter command mode\n");
1080 for (i = 0; i <
ARRAY_SIZE(alps_model_data); i++) {
1083 model = alps_model_data +
i;
1087 alps_exit_command_mode(psmouse);
1091 "Unknown command mode response %2.2x\n",
1104 static int alps_passthrough_mode_v2(
struct psmouse *psmouse,
bool enable)
1106 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1121 static int alps_absolute_mode_v1_v2(
struct psmouse *psmouse)
1123 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1140 static int alps_get_status(
struct psmouse *psmouse,
char *param)
1142 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1152 param[0], param[1], param[2]);
1166 static int alps_tap_mode(
struct psmouse *psmouse,
int enable)
1168 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1170 unsigned char tap_arg = enable ? 0x0A : 0x00;
1171 unsigned char param[4];
1179 if (alps_get_status(psmouse, param))
1189 static int alps_poll(
struct psmouse *psmouse)
1192 unsigned char buf[
sizeof(psmouse->
packet)];
1196 alps_passthrough_mode_v2(psmouse,
true);
1202 alps_passthrough_mode_v2(psmouse,
false);
1204 if (poll_failed || (
buf[0] & priv->
i->mask0) != priv->
i->byte0)
1207 if ((psmouse->
badbyte & 0xc8) == 0x08) {
1219 static int alps_hw_init_v1_v2(
struct psmouse *psmouse)
1225 alps_passthrough_mode_v2(psmouse,
true)) {
1229 if (alps_tap_mode(psmouse,
true)) {
1230 psmouse_warn(psmouse,
"Failed to enable hardware tapping\n");
1234 if (alps_absolute_mode_v1_v2(psmouse)) {
1235 psmouse_err(psmouse,
"Failed to enable absolute mode\n");
1240 alps_passthrough_mode_v2(psmouse,
false)) {
1246 psmouse_err(psmouse,
"Failed to enable stream mode\n");
1257 static int alps_passthrough_mode_v3(
struct psmouse *psmouse,
bool enable)
1261 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1270 if (__alps_command_mode_write_reg(psmouse, reg_val))
1277 static int alps_absolute_mode_v3(
struct psmouse *psmouse)
1281 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1286 if (__alps_command_mode_write_reg(psmouse, reg_val))
1292 static int alps_hw_init_v3(
struct psmouse *psmouse)
1295 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1297 unsigned char param[4];
1302 if (alps_enter_command_mode(psmouse,
NULL))
1306 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1309 if (reg_val & 0x80) {
1310 if (alps_passthrough_mode_v3(psmouse,
true))
1312 if (alps_exit_command_mode(psmouse))
1329 psmouse_warn(psmouse,
"trackstick E7 report failed\n");
1332 "trackstick E7 report: %2.2x %2.2x %2.2x\n",
1333 param[0], param[1], param[2]);
1344 alps_command_mode_send_nibble(psmouse, 0x9) ||
1345 alps_command_mode_send_nibble(psmouse, 0x4)) {
1347 "Error sending magic E6 sequence\n");
1348 goto error_passthrough;
1352 if (alps_enter_command_mode(psmouse,
NULL))
1353 goto error_passthrough;
1354 if (alps_passthrough_mode_v3(psmouse,
false))
1358 if (alps_absolute_mode_v3(psmouse)) {
1359 psmouse_err(psmouse,
"Failed to enter absolute mode\n");
1363 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1366 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1369 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1372 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1375 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1377 if (__alps_command_mode_write_reg(psmouse, 0x04))
1380 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1382 if (__alps_command_mode_write_reg(psmouse, 0x03))
1385 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1387 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1390 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1392 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1400 if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82))
1403 alps_exit_command_mode(psmouse);
1409 psmouse_err(psmouse,
"Failed to enable data reporting\n");
1417 if (!alps_enter_command_mode(psmouse,
NULL))
1418 alps_passthrough_mode_v3(psmouse,
false);
1425 alps_exit_command_mode(psmouse);
1430 static int alps_absolute_mode_v4(
struct psmouse *psmouse)
1434 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1439 if (__alps_command_mode_write_reg(psmouse, reg_val))
1445 static int alps_hw_init_v4(
struct psmouse *psmouse)
1448 struct ps2dev *ps2dev = &psmouse->
ps2dev;
1449 unsigned char param[4];
1454 if (alps_enter_command_mode(psmouse,
NULL))
1457 if (alps_absolute_mode_v4(psmouse)) {
1458 psmouse_err(psmouse,
"Failed to enter absolute mode\n");
1462 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1465 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1468 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1471 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1474 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1477 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1480 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1483 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1486 alps_exit_command_mode(psmouse);
1506 psmouse_err(psmouse,
"Failed to enable data reporting\n");
1518 alps_exit_command_mode(psmouse);
1522 static int alps_hw_init(
struct psmouse *psmouse)
1531 ret = alps_hw_init_v1_v2(psmouse);
1534 ret = alps_hw_init_v3(psmouse);
1537 ret = alps_hw_init_v4(psmouse);
1544 static int alps_reconnect(
struct psmouse *psmouse)
1550 model = alps_get_model(psmouse,
NULL);
1554 return alps_hw_init(psmouse);
1557 static void alps_disconnect(
struct psmouse *psmouse)
1563 input_unregister_device(priv->
dev2);
1571 struct input_dev *dev1 = psmouse->
dev, *dev2;
1575 dev2 = input_allocate_device();
1582 psmouse->private =
priv;
1586 model = alps_get_model(psmouse, &version);
1592 if (alps_hw_init(psmouse))
1617 input_set_abs_params(dev1,
ABS_X, 0, 1023, 0, 0);
1618 input_set_abs_params(dev1,
ABS_Y, 0, 767, 0, 0);
1636 input_set_abs_params(dev1,
ABS_PRESSURE, 0, 127, 0, 0);
1657 snprintf(priv->
phys,
sizeof(priv->
phys),
"%s/input1", psmouse->ps2dev.serio->phys);
1658 dev2->phys = priv->
phys;
1661 dev2->id.vendor = 0x0002;
1663 dev2->id.version = 0x0000;
1664 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
1671 if (input_register_device(priv->
dev2))
1674 psmouse->protocol_handler = alps_process_byte;
1675 psmouse->poll = alps_poll;
1676 psmouse->disconnect = alps_disconnect;
1677 psmouse->reconnect = alps_reconnect;
1681 psmouse->resync_time = 0;
1687 input_free_device(dev2);
1689 psmouse->private =
NULL;
1693 int alps_detect(
struct psmouse *psmouse,
bool set_properties)
1698 model = alps_get_model(psmouse, &version);
1702 if (set_properties) {
1703 psmouse->
vendor =
"ALPS";
1705 "DualPoint TouchPad" :
"GlidePoint";