blob: 41e6cb501e6a1d5b07801463460fe4ebbaace887 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
Hans de Goede073e5702015-08-03 14:06:24 -070023#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "psmouse.h"
26#include "alps.h"
27
Seth Forshee25bded72011-11-07 19:53:36 -080028/*
29 * Definitions for ALPS version 3 and 4 command mode protocol
30 */
31#define ALPS_CMD_NIBBLE_10 0x01f2
32
Kevin Cernekeecd401202013-02-13 22:28:07 -080033#define ALPS_REG_BASE_RUSHMORE 0xc2c0
34#define ALPS_REG_BASE_PINNACLE 0x0000
35
Seth Forshee25bded72011-11-07 19:53:36 -080036static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
37 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
38 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
39 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
40 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
41 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
42 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
43 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
44 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
45 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
46 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
47 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
48 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
49 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
50 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
51 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
52 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
53};
54
55static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
56 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
57 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
58 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
59 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
60 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
61 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
62 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
63 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
64 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
65 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
66 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
67 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
68 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
69 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
70 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
71 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
72};
73
Yunkang Tang95f75e92013-12-01 22:33:52 -080074static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
75 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
76 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
77 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
78 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
79 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
80 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
81 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
82 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
83 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
84 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
85 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
86 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
87 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
88 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
89 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
90 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
91};
92
Seth Forshee25bded72011-11-07 19:53:36 -080093
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080094#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
95#define ALPS_PASS 0x04 /* device has a pass-through port */
96
97#define ALPS_WHEEL 0x08 /* hardware wheel present */
98#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
99#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
100#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800101#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
102 6-byte ALPS packet */
Hans de Goede19556212015-10-26 01:50:28 -0700103#define ALPS_STICK_BITS 0x100 /* separate stick button bits */
Yunkang Tang38088432014-07-26 13:51:41 -0700104#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Helge Dellere38de672006-09-10 21:54:39 -0400106static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800107 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
108 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
109 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
110 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
112 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
113 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
114 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
115 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
116 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
117 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
118 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
119 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
120 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
121 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800122
123 /*
124 * XXX This entry is suspicious. First byte has zero lower nibble,
125 * which is what a normal mouse would report. Also, the value 0x0e
126 * isn't valid per PS/2 spec.
127 */
128 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
129
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800130 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
131 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800132 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800133 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
134 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
135 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
136 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
137 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
138 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
139 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800142static const struct alps_protocol_info alps_v3_protocol_data = {
143 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
144};
145
146static const struct alps_protocol_info alps_v3_rushmore_data = {
147 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
148};
149
150static const struct alps_protocol_info alps_v5_protocol_data = {
151 ALPS_PROTO_V5, 0xc8, 0xd8, 0
152};
153
154static const struct alps_protocol_info alps_v7_protocol_data = {
155 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
156};
157
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700158static const struct alps_protocol_info alps_v8_protocol_data = {
159 ALPS_PROTO_V8, 0x18, 0x18, 0
160};
161
Hans de Goede19556212015-10-26 01:50:28 -0700162/*
163 * Some v2 models report the stick buttons in separate bits
164 */
165static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
166#if defined(CONFIG_DMI) && defined(CONFIG_X86)
167 {
168 /* Extrapolated from other entries */
169 .matches = {
170 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
171 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
172 },
173 },
174 {
175 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
176 .matches = {
177 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
178 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
179 },
180 },
181 {
182 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
183 .matches = {
184 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
185 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
186 },
187 },
188 {
189 /* Extrapolated from other entries */
190 .matches = {
191 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
192 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
193 },
194 },
195#endif
196 { }
197};
198
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800199static void alps_set_abs_params_st(struct alps_data *priv,
200 struct input_dev *dev1);
Hans de Goede688ea362015-05-20 14:42:50 -0700201static void alps_set_abs_params_semi_mt(struct alps_data *priv,
202 struct input_dev *dev1);
Masaki Ota8eccd392015-03-27 20:44:21 -0700203static void alps_set_abs_params_v7(struct alps_data *priv,
204 struct input_dev *dev1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700205static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
206 struct input_dev *dev1);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800207
Seth Forsheed4b347b2011-11-07 19:53:15 -0800208/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800210static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800211 unsigned char data)
212{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800213 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800214}
215
Pali Rohár04aae282015-01-14 13:18:30 -0800216static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800217 int left, int right, int middle)
218{
Martin Buckc91ed052010-03-13 22:23:58 -0800219 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800220
Martin Buckc91ed052010-03-13 22:23:58 -0800221 /*
222 * If shared button has already been reported on the
223 * other device (dev2) then this event should be also
224 * sent through that device.
225 */
Pali Rohár04aae282015-01-14 13:18:30 -0800226 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800227 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800228
Pali Rohár04aae282015-01-14 13:18:30 -0800229 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800230 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800231
Pali Rohár04aae282015-01-14 13:18:30 -0800232 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800233 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800234
Martin Buckc91ed052010-03-13 22:23:58 -0800235 /*
236 * Sync the _other_ device now, we'll do the first
237 * device later once we report the rest of the events.
238 */
Pali Rohár04aae282015-01-14 13:18:30 -0800239 if (dev2)
240 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800241}
242
Seth Forshee25bded72011-11-07 19:53:36 -0800243static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct alps_data *priv = psmouse->private;
246 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500247 struct input_dev *dev = psmouse->dev;
248 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500250 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800252 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400253 left = packet[2] & 0x10;
254 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 middle = 0;
256 x = packet[1] | ((packet[0] & 0x07) << 7);
257 y = packet[4] | ((packet[3] & 0x07) << 7);
258 z = packet[5];
259 } else {
260 left = packet[3] & 1;
261 right = packet[3] & 2;
262 middle = packet[3] & 4;
263 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
264 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
265 z = packet[5];
266 }
267
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800268 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400269 back = packet[0] & 0x10;
270 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500271 }
272
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800273 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500274 back = packet[3] & 4;
275 forward = packet[2] & 4;
276 if ((middle = forward && back))
277 forward = back = 0;
278 }
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ges = packet[2] & 1;
281 fin = packet[2] & 2;
282
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800283 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
285 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700286
Pali Rohár04aae282015-01-14 13:18:30 -0800287 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 input_sync(dev2);
290 return;
291 }
292
Hans de Goede19556212015-10-26 01:50:28 -0700293 /* Some models have separate stick button bits */
294 if (priv->flags & ALPS_STICK_BITS) {
Hans de Goede92bac832015-04-12 15:42:35 -0700295 left |= packet[0] & 1;
296 right |= packet[0] & 2;
297 middle |= packet[0] & 4;
298 }
299
Pali Rohár04aae282015-01-14 13:18:30 -0800300 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800303 if (ges && !fin)
304 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /*
307 * A "tap and drag" operation is reported by the hardware as a transition
308 * from (!fin && ges) to (fin && ges). This should be translated to the
309 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
310 */
311 if (ges && fin && !priv->prev_fin) {
312 input_report_abs(dev, ABS_X, x);
313 input_report_abs(dev, ABS_Y, y);
314 input_report_abs(dev, ABS_PRESSURE, 0);
315 input_report_key(dev, BTN_TOOL_FINGER, 0);
316 input_sync(dev);
317 }
318 priv->prev_fin = fin;
319
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800320 if (z > 30)
321 input_report_key(dev, BTN_TOUCH, 1);
322 if (z < 25)
323 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (z > 0) {
326 input_report_abs(dev, ABS_X, x);
327 input_report_abs(dev, ABS_Y, y);
328 }
329
330 input_report_abs(dev, ABS_PRESSURE, z);
331 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
332
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800333 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500334 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800336 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500337 input_report_key(dev, BTN_FORWARD, forward);
338 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800341 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800342 input_report_key(dev, BTN_0, packet[2] & 4);
343 input_report_key(dev, BTN_1, packet[0] & 0x10);
344 input_report_key(dev, BTN_2, packet[3] & 4);
345 input_report_key(dev, BTN_3, packet[0] & 0x20);
346 }
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 input_sync(dev);
349}
350
Hans de Goede036e6c72014-07-25 22:38:51 -0700351static void alps_get_bitmap_points(unsigned int map,
352 struct alps_bitmap_point *low,
353 struct alps_bitmap_point *high,
354 int *fingers)
355{
356 struct alps_bitmap_point *point;
357 int i, bit, prev_bit = 0;
358
359 point = low;
360 for (i = 0; map != 0; i++, map >>= 1) {
361 bit = map & 1;
362 if (bit) {
363 if (!prev_bit) {
364 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700365 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700366 (*fingers)++;
367 }
368 point->num_bits++;
369 } else {
370 if (prev_bit)
371 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700372 }
373 prev_bit = bit;
374 }
375}
376
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800377/*
Hans de Goededccf1dd2015-05-20 14:43:09 -0700378 * Process bitmap data from semi-mt protocols. Returns the number of
Seth Forshee01ce6612011-11-07 19:54:13 -0800379 * fingers detected. A return value of 0 means at least one of the
380 * bitmaps was empty.
381 *
382 * The bitmaps don't have enough data to track fingers, so this function
383 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700384 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800385 * is greater than 0.
386 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800387static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700388 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800389{
Hans de Goede4dd26572015-05-20 14:41:10 -0700390 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
Seth Forshee01ce6612011-11-07 19:54:13 -0800391 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
392 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Hans de Goede4dd26572015-05-20 14:41:10 -0700393 struct input_mt_pos corner[4];
Seth Forshee01ce6612011-11-07 19:54:13 -0800394
Hans de Goede02d04252014-07-25 22:43:35 -0700395 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800396 return 0;
397
Hans de Goede02d04252014-07-25 22:43:35 -0700398 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
399 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800400
401 /*
402 * Fingers can overlap, so we use the maximum count of fingers
403 * on either axis as the finger count.
404 */
405 fingers = max(fingers_x, fingers_y);
406
407 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700408 * If an axis reports only a single contact, we have overlapping or
409 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800410 */
Hans de Goede20bea682014-07-25 22:33:33 -0700411 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700412 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700413 x_low.num_bits = x_low.num_bits - i;
414 x_high.start_bit = x_low.start_bit + i;
415 x_high.num_bits = max(i, 1);
416 }
417 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700418 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700419 y_low.num_bits = y_low.num_bits - i;
420 y_high.start_bit = y_low.start_bit + i;
421 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800422 }
423
Hans de Goede4dd26572015-05-20 14:41:10 -0700424 /* top-left corner */
425 corner[0].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700426 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
427 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700428 corner[0].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700429 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
430 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800431
Hans de Goede4dd26572015-05-20 14:41:10 -0700432 /* top-right corner */
433 corner[1].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700434 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
435 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700436 corner[1].y =
437 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
438 (2 * (priv->y_bits - 1));
439
440 /* bottom-right corner */
441 corner[2].x =
442 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
443 (2 * (priv->x_bits - 1));
444 corner[2].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700445 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
446 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800447
Hans de Goede4dd26572015-05-20 14:41:10 -0700448 /* bottom-left corner */
449 corner[3].x =
450 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
451 (2 * (priv->x_bits - 1));
452 corner[3].y =
Seth Forshee01ce6612011-11-07 19:54:13 -0800453 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
454 (2 * (priv->y_bits - 1));
455
Hans de Goededccf1dd2015-05-20 14:43:09 -0700456 /* x-bitmap order is reversed on v5 touchpads */
457 if (priv->proto_version == ALPS_PROTO_V5) {
458 for (i = 0; i < 4; i++)
459 corner[i].x = priv->x_max - corner[i].x;
Hans de Goede40e8f532014-07-25 22:37:15 -0700460 }
461
Hans de Goededccf1dd2015-05-20 14:43:09 -0700462 /* y-bitmap order is reversed on v3 and v4 touchpads */
463 if (priv->proto_version == ALPS_PROTO_V3 ||
464 priv->proto_version == ALPS_PROTO_V4) {
Hans de Goede4dd26572015-05-20 14:41:10 -0700465 for (i = 0; i < 4; i++)
466 corner[i].y = priv->y_max - corner[i].y;
Seth Forshee01ce6612011-11-07 19:54:13 -0800467 }
468
Hans de Goede4dd26572015-05-20 14:41:10 -0700469 /*
470 * We only select a corner for the second touch once per 2 finger
471 * touch sequence to avoid the chosen corner (and thus the coordinates)
472 * jumping around when the first touch is in the middle.
473 */
474 if (priv->second_touch == -1) {
475 /* Find corner closest to our st coordinates */
476 closest = 0x7fffffff;
477 for (i = 0; i < 4; i++) {
478 int dx = fields->st.x - corner[i].x;
479 int dy = fields->st.y - corner[i].y;
480 int distance = dx * dx + dy * dy;
481
482 if (distance < closest) {
483 priv->second_touch = i;
484 closest = distance;
485 }
486 }
487 /* And select the opposite corner to use for the 2nd touch */
488 priv->second_touch = (priv->second_touch + 2) % 4;
489 }
490
491 fields->mt[0] = fields->st;
492 fields->mt[1] = corner[priv->second_touch];
493
Seth Forshee01ce6612011-11-07 19:54:13 -0800494 return fingers;
495}
496
Hans de Goedecdf333e2014-07-25 22:44:42 -0700497static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800498{
499 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700500 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
501 input_report_abs(dev, ABS_MT_POSITION_X, x);
502 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800503}
504
Hans de Goedecdf333e2014-07-25 22:44:42 -0700505static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800506{
Hans de Goede02d04252014-07-25 22:43:35 -0700507 struct alps_data *priv = psmouse->private;
508 struct input_dev *dev = psmouse->dev;
509 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700510 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700511
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800512 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700513 for (i = 0; i < n; i++)
514 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
515
516 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800517}
518
Hans de Goede68c21872014-07-25 22:47:25 -0700519static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
520{
521 struct alps_data *priv = psmouse->private;
522 struct input_dev *dev = psmouse->dev;
523 struct alps_fields *f = &priv->f;
524
525 /* Use st data when we don't have mt data */
526 if (fingers < 2) {
527 f->mt[0].x = f->st.x;
528 f->mt[0].y = f->st.y;
529 fingers = f->pressure > 0 ? 1 : 0;
Hans de Goede4dd26572015-05-20 14:41:10 -0700530 priv->second_touch = -1;
Hans de Goede68c21872014-07-25 22:47:25 -0700531 }
532
Hans de Goede1662c0332015-05-20 14:42:15 -0700533 if (fingers >= 1)
534 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
535 if (fingers >= 2)
536 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
537 input_mt_sync_frame(dev);
Hans de Goede68c21872014-07-25 22:47:25 -0700538
539 input_mt_report_finger_count(dev, fingers);
540
541 input_report_key(dev, BTN_LEFT, f->left);
542 input_report_key(dev, BTN_RIGHT, f->right);
543 input_report_key(dev, BTN_MIDDLE, f->middle);
544
545 input_report_abs(dev, ABS_PRESSURE, f->pressure);
546
547 input_sync(dev);
548}
549
Seth Forshee25bded72011-11-07 19:53:36 -0800550static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
551{
552 struct alps_data *priv = psmouse->private;
553 unsigned char *packet = psmouse->packet;
554 struct input_dev *dev = priv->dev2;
555 int x, y, z, left, right, middle;
556
Pali Rohár34412ba2015-01-09 12:48:58 -0800557 /* It should be a DualPoint when received trackstick packet */
558 if (!(priv->flags & ALPS_DUALPOINT)) {
559 psmouse_warn(psmouse,
560 "Rejected trackstick packet from non DualPoint device");
561 return;
562 }
563
Seth Forshee25bded72011-11-07 19:53:36 -0800564 /* Sanity check packet */
565 if (!(packet[0] & 0x40)) {
566 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
567 return;
568 }
569
570 /*
571 * There's a special packet that seems to indicate the end
572 * of a stream of trackstick data. Filter these out.
573 */
574 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
575 return;
576
577 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
578 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
579 z = (packet[4] & 0x7c) >> 2;
580
581 /*
582 * The x and y values tend to be quite large, and when used
583 * alone the trackstick is difficult to use. Scale them down
584 * to compensate.
585 */
586 x /= 8;
587 y /= 8;
588
589 input_report_rel(dev, REL_X, x);
590 input_report_rel(dev, REL_Y, -y);
591
592 /*
593 * Most ALPS models report the trackstick buttons in the touchpad
594 * packets, but a few report them here. No reliable way has been
595 * found to differentiate between the models upfront, so we enable
596 * the quirk in response to seeing a button press in the trackstick
597 * packet.
598 */
599 left = packet[3] & 0x01;
600 right = packet[3] & 0x02;
601 middle = packet[3] & 0x04;
602
603 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
604 (left || right || middle))
605 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
606
607 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
608 input_report_key(dev, BTN_LEFT, left);
609 input_report_key(dev, BTN_RIGHT, right);
610 input_report_key(dev, BTN_MIDDLE, middle);
611 }
612
613 input_sync(dev);
614 return;
615}
616
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800617static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
618{
619 f->left = !!(p[3] & 0x01);
620 f->right = !!(p[3] & 0x02);
621 f->middle = !!(p[3] & 0x04);
622
623 f->ts_left = !!(p[3] & 0x10);
624 f->ts_right = !!(p[3] & 0x20);
625 f->ts_middle = !!(p[3] & 0x40);
626}
627
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700628static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800629 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800630{
631 f->first_mp = !!(p[4] & 0x40);
632 f->is_mp = !!(p[0] & 0x40);
633
Hans de Goedea839cd52015-05-20 14:39:21 -0700634 if (f->is_mp) {
635 f->fingers = (p[5] & 0x3) + 1;
636 f->x_map = ((p[4] & 0x7e) << 8) |
637 ((p[1] & 0x7f) << 2) |
638 ((p[0] & 0x30) >> 4);
639 f->y_map = ((p[3] & 0x70) << 4) |
640 ((p[2] & 0x7f) << 1) |
641 (p[4] & 0x01);
642 } else {
643 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
644 ((p[0] & 0x30) >> 4);
645 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
646 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800647
Hans de Goedea839cd52015-05-20 14:39:21 -0700648 alps_decode_buttons_v3(f, p);
649 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700650
651 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800652}
653
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700654static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800655 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800656{
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700657 f->first_mp = !!(p[4] & 0x40);
Yunkang Tangf105e342014-07-25 22:29:24 -0700658 f->is_mp = !!(p[5] & 0x40);
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700659
Hans de Goedea839cd52015-05-20 14:39:21 -0700660 if (f->is_mp) {
661 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
662 f->x_map = ((p[5] & 0x10) << 11) |
663 ((p[4] & 0x7e) << 8) |
664 ((p[1] & 0x7f) << 2) |
665 ((p[0] & 0x30) >> 4);
666 f->y_map = ((p[5] & 0x20) << 6) |
667 ((p[3] & 0x70) << 4) |
668 ((p[2] & 0x7f) << 1) |
669 (p[4] & 0x01);
670 } else {
671 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
672 ((p[0] & 0x30) >> 4);
673 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
674 f->pressure = p[5] & 0x7f;
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700675
Hans de Goedea839cd52015-05-20 14:39:21 -0700676 alps_decode_buttons_v3(f, p);
677 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700678
679 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800680}
681
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700682static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800683 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800684{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800685 u64 palm_data = 0;
686 struct alps_data *priv = psmouse->private;
687
Dave Turvene75af9e52013-02-21 22:58:28 -0800688 f->first_mp = !!(p[0] & 0x02);
689 f->is_mp = !!(p[0] & 0x20);
690
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800691 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700692 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
693 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
694 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800695 alps_decode_buttons_v3(f, p);
696 } else {
697 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800698 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800699
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800700 palm_data = (p[1] & 0x7f) |
701 ((p[2] & 0x7f) << 7) |
702 ((p[4] & 0x7f) << 14) |
703 ((p[5] & 0x7f) << 21) |
704 ((p[3] & 0x07) << 28) |
705 (((u64)p[3] & 0x70) << 27) |
706 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800707
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800708 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
709 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
710
711 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
712 f->x_map = (palm_data >> priv->y_bits) &
713 (BIT(priv->x_bits) - 1);
714 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700715
716 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800717}
718
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800719static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800720{
721 struct alps_data *priv = psmouse->private;
722 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800723 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700724 struct alps_fields *f = &priv->f;
725 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800726
Hans de Goede02d04252014-07-25 22:43:35 -0700727 memset(f, 0, sizeof(*f));
728
729 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800730
731 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800732 * There's no single feature of touchpad position and bitmap packets
733 * that can be used to distinguish between them. We rely on the fact
734 * that a bitmap packet should always follow a position packet with
735 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800736 */
737 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800738 /*
739 * Sometimes a position packet will indicate a multi-packet
740 * sequence, but then what follows is another position
741 * packet. Check for this, and when it happens process the
742 * position packet as usual.
743 */
Hans de Goede02d04252014-07-25 22:43:35 -0700744 if (f->is_mp) {
745 fingers = f->fingers;
Hans de Goede44b77f32015-05-20 14:40:06 -0700746 /*
747 * Bitmap processing uses position packet's coordinate
748 * data, so we need to do decode it first.
749 */
750 priv->decode_fields(f, priv->multi_data, psmouse);
Hans de Goededccf1dd2015-05-20 14:43:09 -0700751 if (alps_process_bitmap(priv, f) == 0)
752 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800753 } else {
754 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800755 }
756 }
757
Seth Forshee01ce6612011-11-07 19:54:13 -0800758 /*
759 * Bit 6 of byte 0 is not usually set in position packets. The only
760 * times it seems to be set is in situations where the data is
761 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
762 * this combined with the fact that this bit is useful for filtering
763 * out misidentified bitmap packets, we reject anything with this
764 * bit set.
765 */
Hans de Goede02d04252014-07-25 22:43:35 -0700766 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800767 return;
768
Hans de Goede02d04252014-07-25 22:43:35 -0700769 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800770 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800771 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
772 return;
773 }
774
775 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800776
Seth Forshee25bded72011-11-07 19:53:36 -0800777 /*
778 * Sometimes the hardware sends a single packet with z = 0
779 * in the middle of a stream. Real releases generate packets
780 * with x, y, and z all zero, so these seem to be flukes.
781 * Ignore them.
782 */
Hans de Goede02d04252014-07-25 22:43:35 -0700783 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800784 return;
785
Hans de Goede68c21872014-07-25 22:47:25 -0700786 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800787
Pali Rohár34412ba2015-01-09 12:48:58 -0800788 if ((priv->flags & ALPS_DUALPOINT) &&
789 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700790 input_report_key(dev2, BTN_LEFT, f->ts_left);
791 input_report_key(dev2, BTN_RIGHT, f->ts_right);
792 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800793 input_sync(dev2);
794 }
795}
796
797static void alps_process_packet_v3(struct psmouse *psmouse)
798{
799 unsigned char *packet = psmouse->packet;
800
801 /*
802 * v3 protocol packets come in three types, two representing
803 * touchpad data and one representing trackstick data.
804 * Trackstick packets seem to be distinguished by always
805 * having 0x3f in the last byte. This value has never been
806 * observed in the last byte of either of the other types
807 * of packets.
808 */
809 if (packet[5] == 0x3f) {
810 alps_process_trackstick_packet_v3(psmouse);
811 return;
812 }
813
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800814 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800815}
816
Yunkang Tang95f75e92013-12-01 22:33:52 -0800817static void alps_process_packet_v6(struct psmouse *psmouse)
818{
819 struct alps_data *priv = psmouse->private;
820 unsigned char *packet = psmouse->packet;
821 struct input_dev *dev = psmouse->dev;
822 struct input_dev *dev2 = priv->dev2;
823 int x, y, z, left, right, middle;
824
825 /*
826 * We can use Byte5 to distinguish if the packet is from Touchpad
827 * or Trackpoint.
828 * Touchpad: 0 - 0x7E
829 * Trackpoint: 0x7F
830 */
831 if (packet[5] == 0x7F) {
832 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800833 if (!(priv->flags & ALPS_DUALPOINT)) {
834 psmouse_warn(psmouse,
835 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800836 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800837 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800838
839 /* Trackpoint packet */
840 x = packet[1] | ((packet[3] & 0x20) << 2);
841 y = packet[2] | ((packet[3] & 0x40) << 1);
842 z = packet[4];
843 left = packet[3] & 0x01;
844 right = packet[3] & 0x02;
845 middle = packet[3] & 0x04;
846
847 /* To prevent the cursor jump when finger lifted */
848 if (x == 0x7F && y == 0x7F && z == 0x7F)
849 x = y = z = 0;
850
851 /* Divide 4 since trackpoint's speed is too fast */
852 input_report_rel(dev2, REL_X, (char)x / 4);
853 input_report_rel(dev2, REL_Y, -((char)y / 4));
854
855 input_report_key(dev2, BTN_LEFT, left);
856 input_report_key(dev2, BTN_RIGHT, right);
857 input_report_key(dev2, BTN_MIDDLE, middle);
858
859 input_sync(dev2);
860 return;
861 }
862
863 /* Touchpad packet */
864 x = packet[1] | ((packet[3] & 0x78) << 4);
865 y = packet[2] | ((packet[4] & 0x78) << 4);
866 z = packet[5];
867 left = packet[3] & 0x01;
868 right = packet[3] & 0x02;
869
870 if (z > 30)
871 input_report_key(dev, BTN_TOUCH, 1);
872 if (z < 25)
873 input_report_key(dev, BTN_TOUCH, 0);
874
875 if (z > 0) {
876 input_report_abs(dev, ABS_X, x);
877 input_report_abs(dev, ABS_Y, y);
878 }
879
880 input_report_abs(dev, ABS_PRESSURE, z);
881 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
882
883 /* v6 touchpad does not have middle button */
884 input_report_key(dev, BTN_LEFT, left);
885 input_report_key(dev, BTN_RIGHT, right);
886
887 input_sync(dev);
888}
889
Seth Forshee25bded72011-11-07 19:53:36 -0800890static void alps_process_packet_v4(struct psmouse *psmouse)
891{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700892 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800893 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700894 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700895 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700896
897 /*
898 * v4 has a 6-byte encoding for bitmap data, but this data is
899 * broken up between 3 normal packets. Use priv->multi_packet to
900 * track our position in the bitmap packet.
901 */
902 if (packet[6] & 0x40) {
903 /* sync, reset position */
904 priv->multi_packet = 0;
905 }
906
907 if (WARN_ON_ONCE(priv->multi_packet > 2))
908 return;
909
910 offset = 2 * priv->multi_packet;
911 priv->multi_data[offset] = packet[6];
912 priv->multi_data[offset + 1] = packet[7];
913
Hans de Goede44b77f32015-05-20 14:40:06 -0700914 f->left = !!(packet[4] & 0x01);
915 f->right = !!(packet[4] & 0x02);
916
917 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
918 ((packet[0] & 0x30) >> 4);
919 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
920 f->pressure = packet[5] & 0x7f;
921
George Pantalos3b7e09f2012-05-10 22:31:59 -0700922 if (++priv->multi_packet > 2) {
923 priv->multi_packet = 0;
924
Hans de Goede02d04252014-07-25 22:43:35 -0700925 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700926 ((priv->multi_data[3] & 0x60) << 3) |
927 ((priv->multi_data[0] & 0x3f) << 2) |
928 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700929 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700930 ((priv->multi_data[3] & 0x1f) << 5) |
931 (priv->multi_data[1] & 0x1f);
932
Hans de Goede02d04252014-07-25 22:43:35 -0700933 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700934 }
Seth Forshee25bded72011-11-07 19:53:36 -0800935
Hans de Goede68c21872014-07-25 22:47:25 -0700936 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800937}
938
Yunkang Tang38088432014-07-26 13:51:41 -0700939static bool alps_is_valid_package_v7(struct psmouse *psmouse)
940{
941 switch (psmouse->pktcnt) {
942 case 3:
943 return (psmouse->packet[2] & 0x40) == 0x40;
944 case 4:
945 return (psmouse->packet[3] & 0x48) == 0x48;
946 case 6:
947 return (psmouse->packet[5] & 0x40) == 0x00;
948 }
949 return true;
950}
951
952static unsigned char alps_get_packet_id_v7(char *byte)
953{
954 unsigned char packet_id;
955
956 if (byte[4] & 0x40)
957 packet_id = V7_PACKET_ID_TWO;
958 else if (byte[4] & 0x01)
959 packet_id = V7_PACKET_ID_MULTI;
960 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
961 packet_id = V7_PACKET_ID_NEW;
962 else if (byte[1] == 0x00 && byte[4] == 0x00)
963 packet_id = V7_PACKET_ID_IDLE;
964 else
965 packet_id = V7_PACKET_ID_UNKNOWN;
966
967 return packet_id;
968}
969
970static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
971 unsigned char *pkt,
972 unsigned char pkt_id)
973{
974 mt[0].x = ((pkt[2] & 0x80) << 4);
975 mt[0].x |= ((pkt[2] & 0x3F) << 5);
976 mt[0].x |= ((pkt[3] & 0x30) >> 1);
977 mt[0].x |= (pkt[3] & 0x07);
978 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
979
980 mt[1].x = ((pkt[3] & 0x80) << 4);
981 mt[1].x |= ((pkt[4] & 0x80) << 3);
982 mt[1].x |= ((pkt[4] & 0x3F) << 4);
983 mt[1].y = ((pkt[5] & 0x80) << 3);
984 mt[1].y |= ((pkt[5] & 0x3F) << 4);
985
986 switch (pkt_id) {
987 case V7_PACKET_ID_TWO:
988 mt[1].x &= ~0x000F;
989 mt[1].y |= 0x000F;
Hans de Goede72eceab2015-05-20 14:37:41 -0700990 /* Detect false-postive touches where x & y report max value */
991 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
992 mt[1].x = 0;
993 /* y gets set to 0 at the end of this function */
994 }
Yunkang Tang38088432014-07-26 13:51:41 -0700995 break;
996
997 case V7_PACKET_ID_MULTI:
998 mt[1].x &= ~0x003F;
999 mt[1].y &= ~0x0020;
1000 mt[1].y |= ((pkt[4] & 0x02) << 4);
1001 mt[1].y |= 0x001F;
1002 break;
1003
1004 case V7_PACKET_ID_NEW:
1005 mt[1].x &= ~0x003F;
1006 mt[1].x |= (pkt[0] & 0x20);
1007 mt[1].y |= 0x000F;
1008 break;
1009 }
1010
1011 mt[0].y = 0x7FF - mt[0].y;
1012 mt[1].y = 0x7FF - mt[1].y;
1013}
1014
1015static int alps_get_mt_count(struct input_mt_pos *mt)
1016{
Hans de Goede7091c442014-12-18 09:53:34 -08001017 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -07001018
Hans de Goede7091c442014-12-18 09:53:34 -08001019 for (i = 0; i < MAX_TOUCHES; i++) {
1020 if (mt[i].x != 0 || mt[i].y != 0)
1021 fingers++;
1022 }
Yunkang Tang38088432014-07-26 13:51:41 -07001023
Hans de Goede7091c442014-12-18 09:53:34 -08001024 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -07001025}
1026
1027static int alps_decode_packet_v7(struct alps_fields *f,
1028 unsigned char *p,
1029 struct psmouse *psmouse)
1030{
Hans de Goeded27eb792014-12-18 09:55:14 -08001031 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -07001032 unsigned char pkt_id;
1033
1034 pkt_id = alps_get_packet_id_v7(p);
1035 if (pkt_id == V7_PACKET_ID_IDLE)
1036 return 0;
1037 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1038 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -08001039 /*
1040 * NEW packets are send to indicate a discontinuity in the finger
1041 * coordinate reporting. Specifically a finger may have moved from
1042 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1043 * us.
1044 *
1045 * NEW packets have 3 problems:
1046 * 1) They do not contain middle / right button info (on non clickpads)
1047 * this can be worked around by preserving the old button state
1048 * 2) They do not contain an accurate fingercount, and they are
1049 * typically send when the number of fingers changes. We cannot use
1050 * the old finger count as that may mismatch with the amount of
1051 * touch coordinates we've available in the NEW packet
1052 * 3) Their x data for the second touch is inaccurate leading to
1053 * a possible jump of the x coordinate by 16 units when the first
1054 * non NEW packet comes in
1055 * Since problems 2 & 3 cannot be worked around, just ignore them.
1056 */
1057 if (pkt_id == V7_PACKET_ID_NEW)
1058 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001059
1060 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1061
Yunkang Tang38088432014-07-26 13:51:41 -07001062 if (pkt_id == V7_PACKET_ID_TWO)
1063 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001064 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001065 f->fingers = 3 + (p[5] & 0x03);
1066
Hans de Goeded27eb792014-12-18 09:55:14 -08001067 f->left = (p[0] & 0x80) >> 7;
1068 if (priv->flags & ALPS_BUTTONPAD) {
1069 if (p[0] & 0x20)
1070 f->fingers++;
1071 if (p[0] & 0x10)
1072 f->fingers++;
1073 } else {
1074 f->right = (p[0] & 0x20) >> 5;
1075 f->middle = (p[0] & 0x10) >> 4;
1076 }
1077
Hans de Goede7091c442014-12-18 09:53:34 -08001078 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1079 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1080 f->mt[0].x = f->mt[1].x;
1081 f->mt[0].y = f->mt[1].y;
1082 f->mt[1].x = 0;
1083 f->mt[1].y = 0;
1084 }
1085
Yunkang Tang38088432014-07-26 13:51:41 -07001086 return 0;
1087}
1088
1089static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1090{
1091 struct alps_data *priv = psmouse->private;
1092 unsigned char *packet = psmouse->packet;
1093 struct input_dev *dev2 = priv->dev2;
1094 int x, y, z, left, right, middle;
1095
Pali Rohár34412ba2015-01-09 12:48:58 -08001096 /* It should be a DualPoint when received trackstick packet */
1097 if (!(priv->flags & ALPS_DUALPOINT)) {
1098 psmouse_warn(psmouse,
1099 "Rejected trackstick packet from non DualPoint device");
1100 return;
1101 }
1102
Yunkang Tang38088432014-07-26 13:51:41 -07001103 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1104 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1105 ((packet[3] & 0x20) << 1);
1106 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1107
1108 left = (packet[1] & 0x01);
1109 right = (packet[1] & 0x02) >> 1;
1110 middle = (packet[1] & 0x04) >> 2;
1111
Hans de Goede088df2c2015-06-04 22:31:43 -07001112 input_report_rel(dev2, REL_X, (char)x);
1113 input_report_rel(dev2, REL_Y, -((char)y));
Yunkang Tang38088432014-07-26 13:51:41 -07001114
1115 input_report_key(dev2, BTN_LEFT, left);
1116 input_report_key(dev2, BTN_RIGHT, right);
1117 input_report_key(dev2, BTN_MIDDLE, middle);
1118
1119 input_sync(dev2);
1120}
1121
1122static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1123{
1124 struct alps_data *priv = psmouse->private;
1125 struct input_dev *dev = psmouse->dev;
1126 struct alps_fields *f = &priv->f;
1127
1128 memset(f, 0, sizeof(*f));
1129
1130 if (priv->decode_fields(f, psmouse->packet, psmouse))
1131 return;
1132
1133 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1134
1135 input_mt_report_finger_count(dev, f->fingers);
1136
1137 input_report_key(dev, BTN_LEFT, f->left);
1138 input_report_key(dev, BTN_RIGHT, f->right);
1139 input_report_key(dev, BTN_MIDDLE, f->middle);
1140
1141 input_sync(dev);
1142}
1143
1144static void alps_process_packet_v7(struct psmouse *psmouse)
1145{
1146 unsigned char *packet = psmouse->packet;
1147
1148 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1149 alps_process_trackstick_packet_v7(psmouse);
1150 else
1151 alps_process_touchpad_packet_v7(psmouse);
1152}
1153
Fengguang Wu07f19e32015-04-10 23:54:31 -07001154static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001155{
1156 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1157
1158 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1159 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
1160 pkt_id = SS4_PACKET_ID_IDLE;
1161 } else if (!(byte[3] & 0x10)) {
1162 pkt_id = SS4_PACKET_ID_ONE;
1163 } else if (!(byte[3] & 0x20)) {
1164 pkt_id = SS4_PACKET_ID_TWO;
1165 } else {
1166 pkt_id = SS4_PACKET_ID_MULTI;
1167 }
1168
1169 return pkt_id;
1170}
1171
1172static int alps_decode_ss4_v2(struct alps_fields *f,
1173 unsigned char *p, struct psmouse *psmouse)
1174{
1175 struct alps_data *priv = psmouse->private;
1176 unsigned char pkt_id;
1177 unsigned int no_data_x, no_data_y;
1178
1179 pkt_id = alps_get_pkt_id_ss4_v2(p);
1180
1181 /* Current packet is 1Finger coordinate packet */
1182 switch (pkt_id) {
1183 case SS4_PACKET_ID_ONE:
1184 f->mt[0].x = SS4_1F_X_V2(p);
1185 f->mt[0].y = SS4_1F_Y_V2(p);
1186 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1187 f->fingers = 1;
1188 f->first_mp = 0;
1189 f->is_mp = 0;
1190 break;
1191
1192 case SS4_PACKET_ID_TWO:
1193 if (priv->flags & ALPS_BUTTONPAD) {
1194 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1195 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1196 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1197 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1198 } else {
1199 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1200 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1201 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1202 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1203 }
1204 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1205
1206 if (SS4_IS_MF_CONTINUE(p)) {
1207 f->first_mp = 1;
1208 } else {
1209 f->fingers = 2;
1210 f->first_mp = 0;
1211 }
1212 f->is_mp = 0;
1213
1214 break;
1215
1216 case SS4_PACKET_ID_MULTI:
1217 if (priv->flags & ALPS_BUTTONPAD) {
1218 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1219 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1220 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1221 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1222 no_data_x = SS4_MFPACKET_NO_AX_BL;
1223 no_data_y = SS4_MFPACKET_NO_AY_BL;
1224 } else {
1225 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1226 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1227 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1228 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1229 no_data_x = SS4_MFPACKET_NO_AX;
1230 no_data_y = SS4_MFPACKET_NO_AY;
1231 }
1232
1233 f->first_mp = 0;
1234 f->is_mp = 1;
1235
1236 if (SS4_IS_5F_DETECTED(p)) {
1237 f->fingers = 5;
1238 } else if (f->mt[3].x == no_data_x &&
1239 f->mt[3].y == no_data_y) {
1240 f->mt[3].x = 0;
1241 f->mt[3].y = 0;
1242 f->fingers = 3;
1243 } else {
1244 f->fingers = 4;
1245 }
1246 break;
1247
1248 case SS4_PACKET_ID_IDLE:
1249 default:
1250 memset(f, 0, sizeof(struct alps_fields));
1251 break;
1252 }
1253
1254 f->left = !!(SS4_BTN_V2(p) & 0x01);
1255 if (!(priv->flags & ALPS_BUTTONPAD)) {
1256 f->right = !!(SS4_BTN_V2(p) & 0x02);
1257 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1258 }
1259
1260 return 0;
1261}
1262
1263static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1264{
1265 struct alps_data *priv = psmouse->private;
1266 unsigned char *packet = psmouse->packet;
1267 struct input_dev *dev = psmouse->dev;
1268 struct alps_fields *f = &priv->f;
1269
1270 memset(f, 0, sizeof(struct alps_fields));
1271 priv->decode_fields(f, packet, psmouse);
1272 if (priv->multi_packet) {
1273 /*
1274 * Sometimes the first packet will indicate a multi-packet
1275 * sequence, but sometimes the next multi-packet would not
1276 * come. Check for this, and when it happens process the
1277 * position packet as usual.
1278 */
1279 if (f->is_mp) {
1280 /* Now process the 1st packet */
1281 priv->decode_fields(f, priv->multi_data, psmouse);
1282 } else {
1283 priv->multi_packet = 0;
1284 }
1285 }
1286
1287 /*
1288 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1289 * When it is set, it means 2nd packet comes without 1st packet come.
1290 */
1291 if (f->is_mp)
1292 return;
1293
1294 /* Save the first packet */
1295 if (!priv->multi_packet && f->first_mp) {
1296 priv->multi_packet = 1;
1297 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1298 return;
1299 }
1300
1301 priv->multi_packet = 0;
1302
1303 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1304
1305 input_mt_report_finger_count(dev, f->fingers);
1306
1307 input_report_key(dev, BTN_LEFT, f->left);
1308 input_report_key(dev, BTN_RIGHT, f->right);
1309 input_report_key(dev, BTN_MIDDLE, f->middle);
1310
1311 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1312 input_sync(dev);
1313}
1314
1315static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1316{
1317 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1318 return false;
1319 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1320 return false;
1321 return true;
1322}
1323
Pali Rohár04aae282015-01-14 13:18:30 -08001324static DEFINE_MUTEX(alps_mutex);
1325
1326static void alps_register_bare_ps2_mouse(struct work_struct *work)
1327{
1328 struct alps_data *priv =
1329 container_of(work, struct alps_data, dev3_register_work.work);
1330 struct psmouse *psmouse = priv->psmouse;
1331 struct input_dev *dev3;
1332 int error = 0;
1333
1334 mutex_lock(&alps_mutex);
1335
1336 if (priv->dev3)
1337 goto out;
1338
1339 dev3 = input_allocate_device();
1340 if (!dev3) {
1341 psmouse_err(psmouse, "failed to allocate secondary device\n");
1342 error = -ENOMEM;
1343 goto out;
1344 }
1345
1346 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1347 psmouse->ps2dev.serio->phys,
1348 (priv->dev2 ? "input2" : "input1"));
1349 dev3->phys = priv->phys3;
1350
1351 /*
1352 * format of input device name is: "protocol vendor name"
1353 * see function psmouse_switch_protocol() in psmouse-base.c
1354 */
1355 dev3->name = "PS/2 ALPS Mouse";
1356
1357 dev3->id.bustype = BUS_I8042;
1358 dev3->id.vendor = 0x0002;
1359 dev3->id.product = PSMOUSE_PS2;
1360 dev3->id.version = 0x0000;
1361 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1362
1363 input_set_capability(dev3, EV_REL, REL_X);
1364 input_set_capability(dev3, EV_REL, REL_Y);
1365 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1366 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1367 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1368
1369 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1370
1371 error = input_register_device(dev3);
1372 if (error) {
1373 psmouse_err(psmouse,
1374 "failed to register secondary device: %d\n",
1375 error);
1376 input_free_device(dev3);
1377 goto out;
1378 }
1379
1380 priv->dev3 = dev3;
1381
1382out:
1383 /*
1384 * Save the error code so that we can detect that we
1385 * already tried to create the device.
1386 */
1387 if (error)
1388 priv->dev3 = ERR_PTR(error);
1389
1390 mutex_unlock(&alps_mutex);
1391}
1392
Hans de Goede59c30af2015-04-03 17:14:40 -07001393static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001394 unsigned char packet[],
1395 bool report_buttons)
1396{
Hans de Goede59c30af2015-04-03 17:14:40 -07001397 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001398 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001399
Hans de Goedee3a79212015-04-03 17:20:05 -07001400 /* Figure out which device to use to report the bare packet */
1401 if (priv->proto_version == ALPS_PROTO_V2 &&
1402 (priv->flags & ALPS_DUALPOINT)) {
1403 /* On V2 devices the DualPoint Stick reports bare packets */
1404 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001405 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001406 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001407 /* Register dev3 mouse if we received PS/2 packet first time */
1408 if (!IS_ERR(priv->dev3))
1409 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1410 0);
1411 return;
1412 } else {
1413 dev = priv->dev3;
1414 }
1415
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001416 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001417 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001418 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1419
Pali Rohár04aae282015-01-14 13:18:30 -08001420 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001421 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001422 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001423 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1424
Pali Rohár04aae282015-01-14 13:18:30 -08001425 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001426}
1427
1428static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 struct alps_data *priv = psmouse->private;
1431
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001432 if (psmouse->pktcnt < 6)
1433 return PSMOUSE_GOOD_DATA;
1434
1435 if (psmouse->pktcnt == 6) {
1436 /*
1437 * Start a timer to flush the packet if it ends up last
1438 * 6-byte packet in the stream. Timer needs to fire
1439 * psmouse core times out itself. 20 ms should be enough
1440 * to decide if we are getting more data or not.
1441 */
1442 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1443 return PSMOUSE_GOOD_DATA;
1444 }
1445
1446 del_timer(&priv->timer);
1447
1448 if (psmouse->packet[6] & 0x80) {
1449
1450 /*
1451 * Highest bit is set - that means we either had
1452 * complete ALPS packet and this is start of the
1453 * next packet or we got garbage.
1454 */
1455
1456 if (((psmouse->packet[3] |
1457 psmouse->packet[4] |
1458 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001459 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001460 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001461 "refusing packet %4ph (suspected interleaved ps/2)\n",
1462 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001463 return PSMOUSE_BAD_DATA;
1464 }
1465
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001466 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001467
1468 /* Continue with the next packet */
1469 psmouse->packet[0] = psmouse->packet[6];
1470 psmouse->pktcnt = 1;
1471
1472 } else {
1473
1474 /*
1475 * High bit is 0 - that means that we indeed got a PS/2
1476 * packet in the middle of ALPS packet.
1477 *
1478 * There is also possibility that we got 6-byte ALPS
1479 * packet followed by 3-byte packet from trackpoint. We
1480 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001481 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001482 * normal operation (user would need to press all
1483 * buttons on the pad and start moving trackpoint
1484 * without touching the pad surface) we assume former.
1485 * Even if we are wrong the wost thing that would happen
1486 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001487 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001488 */
1489
Hans de Goede59c30af2015-04-03 17:14:40 -07001490 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1491 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001492
1493 /*
1494 * Continue with the standard ALPS protocol handling,
1495 * but make sure we won't process it as an interleaved
1496 * packet again, which may happen if all buttons are
1497 * pressed. To avoid this let's reset the 4th bit which
1498 * is normally 1.
1499 */
1500 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1501 psmouse->pktcnt = 4;
1502 }
1503
1504 return PSMOUSE_GOOD_DATA;
1505}
1506
1507static void alps_flush_packet(unsigned long data)
1508{
1509 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001510 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001511
1512 serio_pause_rx(psmouse->ps2dev.serio);
1513
Seth Forsheeb46615f2011-11-07 19:53:30 -08001514 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001515
1516 /*
1517 * We did not any more data in reasonable amount of time.
1518 * Validate the last 3 bytes and process as a standard
1519 * ALPS packet.
1520 */
1521 if ((psmouse->packet[3] |
1522 psmouse->packet[4] |
1523 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001524 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001525 "refusing packet %3ph (suspected interleaved ps/2)\n",
1526 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001527 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001528 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001529 }
1530 psmouse->pktcnt = 0;
1531 }
1532
1533 serio_continue_rx(psmouse->ps2dev.serio);
1534}
1535
1536static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1537{
1538 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001539
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001540 /*
1541 * Check if we are dealing with a bare PS/2 packet, presumably from
1542 * a device connected to the external PS/2 port. Because bare PS/2
1543 * protocol does not have enough constant bits to self-synchronize
1544 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001545 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001546 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001547 if (priv->proto_version != ALPS_PROTO_V8 &&
1548 !psmouse->out_of_sync_cnt &&
1549 (psmouse->packet[0] & 0xc8) == 0x08) {
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001552 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1553 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return PSMOUSE_FULL_PACKET;
1555 }
1556 return PSMOUSE_GOOD_DATA;
1557 }
1558
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001559 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1560
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001561 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001562 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1563 return alps_handle_interleaved_ps2(psmouse);
1564 }
1565
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001566 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001567 psmouse_dbg(psmouse,
1568 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001569 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Seth Forsheeb46615f2011-11-07 19:53:30 -08001573 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001574 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001575 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001576 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001577 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1578 psmouse->pktcnt - 1,
1579 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001580
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001581 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001582 psmouse->pktcnt == psmouse->pktsize) {
1583 /*
1584 * Some Dell boxes, such as Latitude E6440 or E7440
1585 * with closed lid, quite often smash last byte of
1586 * otherwise valid packet with 0xff. Given that the
1587 * next packet is very likely to be valid let's
1588 * report PSMOUSE_FULL_PACKET but not process data,
1589 * rather than reporting PSMOUSE_BAD_DATA and
1590 * filling the logs.
1591 */
1592 return PSMOUSE_FULL_PACKET;
1593 }
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001598 if ((priv->proto_version == ALPS_PROTO_V7 &&
1599 !alps_is_valid_package_v7(psmouse)) ||
1600 (priv->proto_version == ALPS_PROTO_V8 &&
1601 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001602 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1603 psmouse->pktcnt - 1,
1604 psmouse->packet[psmouse->pktcnt - 1]);
1605 return PSMOUSE_BAD_DATA;
1606 }
1607
Seth Forsheeb46615f2011-11-07 19:53:30 -08001608 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001609 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return PSMOUSE_FULL_PACKET;
1611 }
1612
1613 return PSMOUSE_GOOD_DATA;
1614}
1615
Seth Forshee25bded72011-11-07 19:53:36 -08001616static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1617{
1618 struct ps2dev *ps2dev = &psmouse->ps2dev;
1619 struct alps_data *priv = psmouse->private;
1620 int command;
1621 unsigned char *param;
1622 unsigned char dummy[4];
1623
1624 BUG_ON(nibble > 0xf);
1625
1626 command = priv->nibble_commands[nibble].command;
1627 param = (command & 0x0f00) ?
1628 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1629
1630 if (ps2_command(ps2dev, param, command))
1631 return -1;
1632
1633 return 0;
1634}
1635
1636static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1637{
1638 struct ps2dev *ps2dev = &psmouse->ps2dev;
1639 struct alps_data *priv = psmouse->private;
1640 int i, nibble;
1641
1642 if (ps2_command(ps2dev, NULL, priv->addr_command))
1643 return -1;
1644
1645 for (i = 12; i >= 0; i -= 4) {
1646 nibble = (addr >> i) & 0xf;
1647 if (alps_command_mode_send_nibble(psmouse, nibble))
1648 return -1;
1649 }
1650
1651 return 0;
1652}
1653
1654static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1655{
1656 struct ps2dev *ps2dev = &psmouse->ps2dev;
1657 unsigned char param[4];
1658
1659 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1660 return -1;
1661
1662 /*
1663 * The address being read is returned in the first two bytes
1664 * of the result. Check that this address matches the expected
1665 * address.
1666 */
1667 if (addr != ((param[0] << 8) | param[1]))
1668 return -1;
1669
1670 return param[2];
1671}
1672
1673static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1674{
1675 if (alps_command_mode_set_addr(psmouse, addr))
1676 return -1;
1677 return __alps_command_mode_read_reg(psmouse, addr);
1678}
1679
1680static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1681{
1682 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1683 return -1;
1684 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1685 return -1;
1686 return 0;
1687}
1688
1689static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1690 u8 value)
1691{
1692 if (alps_command_mode_set_addr(psmouse, addr))
1693 return -1;
1694 return __alps_command_mode_write_reg(psmouse, value);
1695}
1696
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001697static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1698 int repeated_command, unsigned char *param)
1699{
1700 struct ps2dev *ps2dev = &psmouse->ps2dev;
1701
1702 param[0] = 0;
1703 if (init_command && ps2_command(ps2dev, param, init_command))
1704 return -EIO;
1705
1706 if (ps2_command(ps2dev, NULL, repeated_command) ||
1707 ps2_command(ps2dev, NULL, repeated_command) ||
1708 ps2_command(ps2dev, NULL, repeated_command))
1709 return -EIO;
1710
1711 param[0] = param[1] = param[2] = 0xff;
1712 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1713 return -EIO;
1714
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001715 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1716 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001717 return 0;
1718}
1719
Yunkang Tang38088432014-07-26 13:51:41 -07001720static bool alps_check_valid_firmware_id(unsigned char id[])
1721{
1722 if (id[0] == 0x73)
1723 return true;
1724
1725 if (id[0] == 0x88 &&
1726 (id[1] == 0x07 ||
1727 id[1] == 0x08 ||
1728 (id[1] & 0xf0) == 0xb0 ||
1729 (id[1] & 0xf0) == 0xc0)) {
1730 return true;
1731 }
1732
1733 return false;
1734}
1735
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001736static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001737{
1738 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001739
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001740 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001741 psmouse_err(psmouse, "failed to enter command mode\n");
1742 return -1;
1743 }
1744
Yunkang Tang38088432014-07-26 13:51:41 -07001745 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001746 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001747 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001748 return -1;
1749 }
Seth Forshee25bded72011-11-07 19:53:36 -08001750 return 0;
1751}
1752
1753static inline int alps_exit_command_mode(struct psmouse *psmouse)
1754{
1755 struct ps2dev *ps2dev = &psmouse->ps2dev;
1756 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1757 return -1;
1758 return 0;
1759}
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761/*
1762 * For DualPoint devices select the device that should respond to
1763 * subsequent commands. It looks like glidepad is behind stickpointer,
1764 * I'd thought it would be other way around...
1765 */
Seth Forshee25bded72011-11-07 19:53:36 -08001766static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1770
1771 if (ps2_command(ps2dev, NULL, cmd) ||
1772 ps2_command(ps2dev, NULL, cmd) ||
1773 ps2_command(ps2dev, NULL, cmd) ||
1774 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1775 return -1;
1776
1777 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001778 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 return 0;
1781}
1782
Seth Forshee25bded72011-11-07 19:53:36 -08001783static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct ps2dev *ps2dev = &psmouse->ps2dev;
1786
1787 /* Try ALPS magic knock - 4 disable before enable */
1788 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1789 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1790 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1791 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1792 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1793 return -1;
1794
1795 /*
1796 * Switch mouse to poll (remote) mode so motion data will not
1797 * get in our way
1798 */
1799 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1800}
1801
Yunkang Tang95f75e92013-12-01 22:33:52 -08001802static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1803{
1804 int i, nibble;
1805
1806 /*
1807 * b0-b11 are valid bits, send sequence is inverse.
1808 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1809 */
1810 for (i = 0; i <= 8; i += 4) {
1811 nibble = (word >> i) & 0xf;
1812 if (alps_command_mode_send_nibble(psmouse, nibble))
1813 return -1;
1814 }
1815
1816 return 0;
1817}
1818
1819static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1820 u16 addr, u16 value)
1821{
1822 struct ps2dev *ps2dev = &psmouse->ps2dev;
1823
1824 /* 0x0A0 is the command to write the word */
1825 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1826 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1827 alps_monitor_mode_send_word(psmouse, addr) ||
1828 alps_monitor_mode_send_word(psmouse, value) ||
1829 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1830 return -1;
1831
1832 return 0;
1833}
1834
1835static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1836{
1837 struct ps2dev *ps2dev = &psmouse->ps2dev;
1838
1839 if (enable) {
1840 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1841 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1842 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1843 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1844 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1845 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1846 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1847 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1848 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1849 return -1;
1850 } else {
1851 /* EC to exit monitor mode */
1852 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1853 return -1;
1854 }
1855
1856 return 0;
1857}
1858
1859static int alps_absolute_mode_v6(struct psmouse *psmouse)
1860{
1861 u16 reg_val = 0x181;
1862 int ret = -1;
1863
1864 /* enter monitor mode, to write the register */
1865 if (alps_monitor_mode(psmouse, true))
1866 return -1;
1867
1868 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1869
1870 if (alps_monitor_mode(psmouse, false))
1871 ret = -1;
1872
1873 return ret;
1874}
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876static int alps_get_status(struct psmouse *psmouse, char *param)
1877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001879 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return -1;
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 0;
1883}
1884
1885/*
1886 * Turn touchpad tapping on or off. The sequences are:
1887 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1888 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1889 * My guess that 0xE9 (GetInfo) is here as a sync point.
1890 * For models that also have stickpointer (DualPoints) its tapping
1891 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1892 * we don't fiddle with it.
1893 */
1894static int alps_tap_mode(struct psmouse *psmouse, int enable)
1895{
1896 struct ps2dev *ps2dev = &psmouse->ps2dev;
1897 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1898 unsigned char tap_arg = enable ? 0x0A : 0x00;
1899 unsigned char param[4];
1900
1901 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1902 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1903 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1904 ps2_command(ps2dev, &tap_arg, cmd))
1905 return -1;
1906
1907 if (alps_get_status(psmouse, param))
1908 return -1;
1909
1910 return 0;
1911}
1912
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001913/*
1914 * alps_poll() - poll the touchpad for current motion packet.
1915 * Used in resync.
1916 */
1917static int alps_poll(struct psmouse *psmouse)
1918{
1919 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001920 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001921 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001922
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001923 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001924 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001925
1926 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1927 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1928
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001929 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001930 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001931
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001932 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001933 return -1;
1934
1935 if ((psmouse->badbyte & 0xc8) == 0x08) {
1936/*
1937 * Poll the track stick ...
1938 */
1939 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1940 return -1;
1941 }
1942
1943 memcpy(psmouse->packet, buf, sizeof(buf));
1944 return 0;
1945}
1946
Seth Forshee25bded72011-11-07 19:53:36 -08001947static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001951 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001952 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001956 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001957 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Seth Forshee25bded72011-11-07 19:53:36 -08001961 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001962 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return -1;
1964 }
1965
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001966 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001967 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001971 /* ALPS needs stream mode, otherwise it won't report any data */
1972 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001973 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001974 return -1;
1975 }
1976
1977 return 0;
1978}
1979
Yunkang Tang95f75e92013-12-01 22:33:52 -08001980static int alps_hw_init_v6(struct psmouse *psmouse)
1981{
1982 unsigned char param[2] = {0xC8, 0x14};
1983
1984 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1985 if (alps_passthrough_mode_v2(psmouse, true))
1986 return -1;
1987
1988 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1989 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1990 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1991 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1992 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1993 return -1;
1994
1995 if (alps_passthrough_mode_v2(psmouse, false))
1996 return -1;
1997
1998 if (alps_absolute_mode_v6(psmouse)) {
1999 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2000 return -1;
2001 }
2002
2003 return 0;
2004}
2005
Seth Forshee25bded72011-11-07 19:53:36 -08002006/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08002007 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08002008 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08002009static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2010 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08002011{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002012 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08002013
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002014 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002015 return -1;
2016
Kevin Cernekeecd401202013-02-13 22:28:07 -08002017 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2018 if (reg_val == -1)
2019 goto error;
2020
Seth Forshee25bded72011-11-07 19:53:36 -08002021 if (enable)
2022 reg_val |= 0x01;
2023 else
2024 reg_val &= ~0x01;
2025
Kevin Cernekeecd401202013-02-13 22:28:07 -08002026 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08002027
Kevin Cernekeecd401202013-02-13 22:28:07 -08002028error:
2029 if (alps_exit_command_mode(psmouse))
2030 ret = -1;
2031 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08002032}
2033
2034/* Must be in command mode when calling this function */
2035static int alps_absolute_mode_v3(struct psmouse *psmouse)
2036{
2037 int reg_val;
2038
2039 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2040 if (reg_val == -1)
2041 return -1;
2042
2043 reg_val |= 0x06;
2044 if (__alps_command_mode_write_reg(psmouse, reg_val))
2045 return -1;
2046
2047 return 0;
2048}
2049
Kevin Cernekeecd401202013-02-13 22:28:07 -08002050static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
2051{
2052 int ret = -EIO, reg_val;
2053
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002054 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002055 goto error;
2056
2057 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2058 if (reg_val == -1)
2059 goto error;
2060
2061 /* bit 7: trackstick is present */
2062 ret = reg_val & 0x80 ? 0 : -ENODEV;
2063
2064error:
2065 alps_exit_command_mode(psmouse);
2066 return ret;
2067}
2068
2069static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2070{
2071 struct ps2dev *ps2dev = &psmouse->ps2dev;
2072 int ret = 0;
2073 unsigned char param[4];
2074
2075 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2076 return -EIO;
2077
2078 /*
2079 * E7 report for the trackstick
2080 *
2081 * There have been reports of failures to seem to trace back
2082 * to the above trackstick check failing. When these occur
2083 * this E7 report fails, so when that happens we continue
2084 * with the assumption that there isn't a trackstick after
2085 * all.
2086 */
2087 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002088 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002089 ret = -ENODEV;
2090 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002091 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002092
2093 /*
2094 * Not sure what this does, but it is absolutely
2095 * essential. Without it, the touchpad does not
2096 * work at all and the trackstick just emits normal
2097 * PS/2 packets.
2098 */
2099 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2100 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2101 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2102 alps_command_mode_send_nibble(psmouse, 0x9) ||
2103 alps_command_mode_send_nibble(psmouse, 0x4)) {
2104 psmouse_err(psmouse,
2105 "Error sending magic E6 sequence\n");
2106 ret = -EIO;
2107 goto error;
2108 }
2109
2110 /*
2111 * This ensures the trackstick packets are in the format
2112 * supported by this driver. If bit 1 isn't set the packet
2113 * format is different.
2114 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002115 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002116 alps_command_mode_write_reg(psmouse,
2117 reg_base + 0x08, 0x82) ||
2118 alps_exit_command_mode(psmouse))
2119 ret = -EIO;
2120 }
2121
2122error:
2123 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2124 ret = -EIO;
2125
2126 return ret;
2127}
2128
Seth Forshee25bded72011-11-07 19:53:36 -08002129static int alps_hw_init_v3(struct psmouse *psmouse)
2130{
Seth Forshee25bded72011-11-07 19:53:36 -08002131 struct ps2dev *ps2dev = &psmouse->ps2dev;
2132 int reg_val;
2133 unsigned char param[4];
2134
Kevin Cernekeecd401202013-02-13 22:28:07 -08002135 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2136 if (reg_val == -EIO)
2137 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002138
Kevin Cernekeecd401202013-02-13 22:28:07 -08002139 if (reg_val == 0 &&
2140 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002141 goto error;
2142
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002143 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002144 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002145 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2146 goto error;
2147 }
2148
2149 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2150 if (reg_val == -1)
2151 goto error;
2152 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2153 goto error;
2154
2155 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2156 if (reg_val == -1)
2157 goto error;
2158 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2159 goto error;
2160
2161 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2162 goto error;
2163 if (__alps_command_mode_write_reg(psmouse, 0x04))
2164 goto error;
2165
2166 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2167 goto error;
2168 if (__alps_command_mode_write_reg(psmouse, 0x03))
2169 goto error;
2170
2171 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2172 goto error;
2173 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2174 goto error;
2175
2176 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2177 goto error;
2178 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2179 goto error;
2180
Seth Forshee25bded72011-11-07 19:53:36 -08002181 alps_exit_command_mode(psmouse);
2182
2183 /* Set rate and enable data reporting */
2184 param[0] = 0x64;
2185 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2186 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2187 psmouse_err(psmouse, "Failed to enable data reporting\n");
2188 return -1;
2189 }
2190
2191 return 0;
2192
Seth Forshee25bded72011-11-07 19:53:36 -08002193error:
2194 /*
2195 * Leaving the touchpad in command mode will essentially render
2196 * it unusable until the machine reboots, so exit it here just
2197 * to be safe
2198 */
2199 alps_exit_command_mode(psmouse);
2200 return -1;
2201}
2202
Hans de Goedef3f33c62014-07-29 11:22:07 -07002203static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2204{
2205 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2206 struct alps_data *priv = psmouse->private;
2207
2208 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2209 if (reg < 0)
2210 return reg;
2211
2212 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2213 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2214
2215 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2216 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2217
2218 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2219 if (reg < 0)
2220 return reg;
2221
2222 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2223 x_electrode = 17 + x_electrode;
2224
2225 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2226 y_electrode = 13 + y_electrode;
2227
2228 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2229 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2230
2231 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2232 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2233
2234 psmouse_dbg(psmouse,
2235 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2236 x_pitch, y_pitch, x_electrode, y_electrode,
2237 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2238
2239 return 0;
2240}
2241
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002242static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2243{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002244 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002245 struct ps2dev *ps2dev = &psmouse->ps2dev;
2246 int reg_val, ret = -1;
2247
Kevin Cernekeecd401202013-02-13 22:28:07 -08002248 if (priv->flags & ALPS_DUALPOINT) {
2249 reg_val = alps_setup_trackstick_v3(psmouse,
2250 ALPS_REG_BASE_RUSHMORE);
2251 if (reg_val == -EIO)
2252 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002253 }
2254
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002255 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002256 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2257 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2258 goto error;
2259
Hans de Goedef3f33c62014-07-29 11:22:07 -07002260 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2261 goto error;
2262
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002263 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2264 if (reg_val == -1)
2265 goto error;
2266 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2267 goto error;
2268
2269 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2270 goto error;
2271
2272 /* enter absolute mode */
2273 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2274 if (reg_val == -1)
2275 goto error;
2276 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2277 goto error;
2278
2279 alps_exit_command_mode(psmouse);
2280 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2281
2282error:
2283 alps_exit_command_mode(psmouse);
2284 return ret;
2285}
2286
Seth Forshee25bded72011-11-07 19:53:36 -08002287/* Must be in command mode when calling this function */
2288static int alps_absolute_mode_v4(struct psmouse *psmouse)
2289{
2290 int reg_val;
2291
2292 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2293 if (reg_val == -1)
2294 return -1;
2295
2296 reg_val |= 0x02;
2297 if (__alps_command_mode_write_reg(psmouse, reg_val))
2298 return -1;
2299
2300 return 0;
2301}
2302
2303static int alps_hw_init_v4(struct psmouse *psmouse)
2304{
Seth Forshee25bded72011-11-07 19:53:36 -08002305 struct ps2dev *ps2dev = &psmouse->ps2dev;
2306 unsigned char param[4];
2307
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002308 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002309 goto error;
2310
2311 if (alps_absolute_mode_v4(psmouse)) {
2312 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2313 goto error;
2314 }
2315
2316 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2317 goto error;
2318
2319 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2320 goto error;
2321
2322 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2323 goto error;
2324
2325 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2326 goto error;
2327
2328 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2329 goto error;
2330
2331 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2332 goto error;
2333
2334 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2335 goto error;
2336
2337 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2338 goto error;
2339
2340 alps_exit_command_mode(psmouse);
2341
2342 /*
2343 * This sequence changes the output from a 9-byte to an
2344 * 8-byte format. All the same data seems to be present,
2345 * just in a more compact format.
2346 */
2347 param[0] = 0xc8;
2348 param[1] = 0x64;
2349 param[2] = 0x50;
2350 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2351 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2352 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2353 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2354 return -1;
2355
2356 /* Set rate and enable data reporting */
2357 param[0] = 0x64;
2358 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2359 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2360 psmouse_err(psmouse, "Failed to enable data reporting\n");
2361 return -1;
2362 }
2363
2364 return 0;
2365
2366error:
2367 /*
2368 * Leaving the touchpad in command mode will essentially render
2369 * it unusable until the machine reboots, so exit it here just
2370 * to be safe
2371 */
2372 alps_exit_command_mode(psmouse);
2373 return -1;
2374}
2375
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002376static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2377 unsigned char index, unsigned char otp[])
2378{
2379 struct ps2dev *ps2dev = &psmouse->ps2dev;
2380
2381 switch (index) {
2382 case 0:
2383 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2384 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2385 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2386 return -1;
2387
2388 break;
2389
2390 case 1:
2391 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2392 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2393 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2394 return -1;
2395
2396 break;
2397 }
2398
2399 return 0;
2400}
2401
Fengguang Wu07f19e32015-04-10 23:54:31 -07002402static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2403 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002404{
2405 int num_x_electrode;
2406 int num_y_electrode;
2407 int x_pitch, y_pitch, x_phys, y_phys;
2408
2409 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2410 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2411
2412 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2413 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2414
2415 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2416 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2417
2418 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2419 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2420
2421 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2422 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2423
2424 return 0;
2425}
2426
Fengguang Wu07f19e32015-04-10 23:54:31 -07002427static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2428 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002429{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002430 unsigned char is_btnless;
2431
2432 is_btnless = (otp[1][1] >> 3) & 0x01;
2433
2434 if (is_btnless)
2435 priv->flags |= ALPS_BUTTONPAD;
2436
2437 return 0;
2438}
2439
2440static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2441 struct alps_data *priv)
2442{
2443 unsigned char otp[2][4];
2444
2445 memset(otp, 0, sizeof(otp));
2446
2447 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2448 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2449 return -1;
2450
2451 alps_update_device_area_ss4_v2(otp, priv);
2452
2453 alps_update_btn_info_ss4_v2(otp, priv);
2454
2455 return 0;
2456}
2457
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002458static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2459 struct alps_data *priv)
2460{
2461 struct ps2dev *ps2dev = &psmouse->ps2dev;
2462 unsigned char param[4] = {0};
2463 int num_x_electrode, num_y_electrode;
2464
2465 if (alps_enter_command_mode(psmouse))
2466 return -1;
2467
2468 param[0] = 0x0a;
2469 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2470 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2471 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2472 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2473 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2474 return -1;
2475
2476 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2477 return -1;
2478
2479 /*
2480 * Dolphin's sensor line number is not fixed. It can be calculated
2481 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2482 * Further more, we can get device's x_max and y_max by multiplying
2483 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2484 *
2485 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2486 * real sensor line number X = 11 + 8 = 19, and
2487 * real sensor line number Y = 8 + 1 = 9.
2488 * So, x_max = (19 - 1) * 64 = 1152, and
2489 * y_max = (9 - 1) * 64 = 512.
2490 */
2491 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2492 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2493 priv->x_bits = num_x_electrode;
2494 priv->y_bits = num_y_electrode;
2495 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2496 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2497
2498 if (alps_exit_command_mode(psmouse))
2499 return -1;
2500
2501 return 0;
2502}
2503
Dave Turvene75af9e52013-02-21 22:58:28 -08002504static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2505{
2506 struct ps2dev *ps2dev = &psmouse->ps2dev;
2507 unsigned char param[2];
2508
2509 /* This is dolphin "v1" as empirically defined by florin9doi */
2510 param[0] = 0x64;
2511 param[1] = 0x28;
2512
2513 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2514 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2515 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2516 return -1;
2517
2518 return 0;
2519}
2520
Yunkang Tang38088432014-07-26 13:51:41 -07002521static int alps_hw_init_v7(struct psmouse *psmouse)
2522{
2523 struct ps2dev *ps2dev = &psmouse->ps2dev;
2524 int reg_val, ret = -1;
2525
2526 if (alps_enter_command_mode(psmouse) ||
2527 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2528 goto error;
2529
Hans de Goedef3f33c62014-07-29 11:22:07 -07002530 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2531 goto error;
2532
Yunkang Tang38088432014-07-26 13:51:41 -07002533 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2534 goto error;
2535
2536 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2537 if (reg_val == -1)
2538 goto error;
2539 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2540 goto error;
2541
2542 alps_exit_command_mode(psmouse);
2543 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2544
2545error:
2546 alps_exit_command_mode(psmouse);
2547 return ret;
2548}
2549
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002550static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2551{
2552 struct ps2dev *ps2dev = &psmouse->ps2dev;
2553 char param[2] = {0x64, 0x28};
2554 int ret = -1;
2555
2556 /* enter absolute mode */
2557 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2558 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2559 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2560 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2561 goto error;
2562 }
2563
2564 /* T.B.D. Decread noise packet number, delete in the future */
2565 if (alps_exit_command_mode(psmouse) ||
2566 alps_enter_command_mode(psmouse) ||
2567 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2568 goto error;
2569 }
2570 alps_exit_command_mode(psmouse);
2571
2572 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2573
2574error:
2575 alps_exit_command_mode(psmouse);
2576 return ret;
2577}
2578
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002579static int alps_set_protocol(struct psmouse *psmouse,
2580 struct alps_data *priv,
2581 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002582{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002583 psmouse->private = priv;
2584
2585 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2586
2587 priv->proto_version = protocol->version;
2588 priv->byte0 = protocol->byte0;
2589 priv->mask0 = protocol->mask0;
2590 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002591
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002592 priv->x_max = 2000;
2593 priv->y_max = 1400;
2594 priv->x_bits = 15;
2595 priv->y_bits = 11;
2596
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002597 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002598 case ALPS_PROTO_V1:
2599 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002600 priv->hw_init = alps_hw_init_v1_v2;
2601 priv->process_packet = alps_process_packet_v1_v2;
2602 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002603 priv->x_max = 1023;
2604 priv->y_max = 767;
Hans de Goede19556212015-10-26 01:50:28 -07002605 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2606 priv->flags |= ALPS_STICK_BITS;
Seth Forshee25bded72011-11-07 19:53:36 -08002607 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002608
Seth Forshee25bded72011-11-07 19:53:36 -08002609 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002610 priv->hw_init = alps_hw_init_v3;
2611 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002612 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002613 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002614 priv->nibble_commands = alps_v3_nibble_commands;
2615 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002616 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002617
2618 case ALPS_PROTO_V3_RUSHMORE:
2619 priv->hw_init = alps_hw_init_rushmore_v3;
2620 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002621 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002622 priv->decode_fields = alps_decode_rushmore;
2623 priv->nibble_commands = alps_v3_nibble_commands;
2624 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2625 priv->x_bits = 16;
2626 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002627
2628 if (alps_probe_trackstick_v3(psmouse,
2629 ALPS_REG_BASE_RUSHMORE) < 0)
2630 priv->flags &= ~ALPS_DUALPOINT;
2631
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002632 break;
2633
Seth Forshee25bded72011-11-07 19:53:36 -08002634 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002635 priv->hw_init = alps_hw_init_v4;
2636 priv->process_packet = alps_process_packet_v4;
Hans de Goede688ea362015-05-20 14:42:50 -07002637 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002638 priv->nibble_commands = alps_v4_nibble_commands;
2639 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002640 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002641
Dave Turvene75af9e52013-02-21 22:58:28 -08002642 case ALPS_PROTO_V5:
2643 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002644 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002645 priv->decode_fields = alps_decode_dolphin;
Hans de Goede688ea362015-05-20 14:42:50 -07002646 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dave Turvene75af9e52013-02-21 22:58:28 -08002647 priv->nibble_commands = alps_v3_nibble_commands;
2648 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002649 priv->x_bits = 23;
2650 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002651
2652 if (alps_dolphin_get_device_area(psmouse, priv))
2653 return -EIO;
2654
Dave Turvene75af9e52013-02-21 22:58:28 -08002655 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002656
Yunkang Tang95f75e92013-12-01 22:33:52 -08002657 case ALPS_PROTO_V6:
2658 priv->hw_init = alps_hw_init_v6;
2659 priv->process_packet = alps_process_packet_v6;
2660 priv->set_abs_params = alps_set_abs_params_st;
2661 priv->nibble_commands = alps_v6_nibble_commands;
2662 priv->x_max = 2047;
2663 priv->y_max = 1535;
2664 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002665
Yunkang Tang38088432014-07-26 13:51:41 -07002666 case ALPS_PROTO_V7:
2667 priv->hw_init = alps_hw_init_v7;
2668 priv->process_packet = alps_process_packet_v7;
2669 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002670 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002671 priv->nibble_commands = alps_v3_nibble_commands;
2672 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002673 priv->x_max = 0xfff;
2674 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002675
2676 if (priv->fw_ver[1] != 0xba)
2677 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002678
Yunkang Tang38088432014-07-26 13:51:41 -07002679 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002680
2681 case ALPS_PROTO_V8:
2682 priv->hw_init = alps_hw_init_ss4_v2;
2683 priv->process_packet = alps_process_packet_ss4_v2;
2684 priv->decode_fields = alps_decode_ss4_v2;
2685 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2686 priv->nibble_commands = alps_v3_nibble_commands;
2687 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2688
2689 if (alps_set_defaults_ss4_v2(psmouse, priv))
2690 return -EIO;
2691
2692 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002693 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002694
2695 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002696}
2697
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002698static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2699 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002700{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002701 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002702 int i;
2703
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002704 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2705 model = &alps_model_data[i];
2706
2707 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2708 (!model->command_mode_resp ||
2709 model->command_mode_resp == ec[2])) {
2710
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002711 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002712 }
2713 }
2714
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002715 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002716}
2717
2718static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2719{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002720 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002721 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002722 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002723
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002724 /*
2725 * First try "E6 report".
2726 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2727 * The bits 0-2 of the first byte will be 1s if some buttons are
2728 * pressed.
2729 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002730 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2731 PSMOUSE_CMD_SETSCALE11, e6))
2732 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002733
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002734 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2735 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002736
2737 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002738 * Now get the "E7" and "EC" reports. These will uniquely identify
2739 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002740 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002741 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2742 PSMOUSE_CMD_SETSCALE21, e7) ||
2743 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2744 PSMOUSE_CMD_RESET_WRAP, ec) ||
2745 alps_exit_command_mode(psmouse))
2746 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002747
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002748 protocol = alps_match_table(e7, ec);
2749 if (!protocol) {
2750 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2751 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2752 protocol = &alps_v5_protocol_data;
2753 } else if (ec[0] == 0x88 &&
2754 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2755 protocol = &alps_v7_protocol_data;
2756 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2757 protocol = &alps_v3_rushmore_data;
2758 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2759 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2760 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002761 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2762 e7[2] == 0x14 && ec[1] == 0x02) {
2763 protocol = &alps_v8_protocol_data;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002764 } else {
2765 psmouse_dbg(psmouse,
2766 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2767 return -EINVAL;
2768 }
2769 }
2770
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002771 if (priv) {
2772 /* Save the Firmware version */
2773 memcpy(priv->fw_ver, ec, 3);
2774 error = alps_set_protocol(psmouse, priv, protocol);
2775 if (error)
2776 return error;
2777 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002778
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002779 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002780}
2781
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002782static int alps_reconnect(struct psmouse *psmouse)
2783{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002784 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002785
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002786 psmouse_reset(psmouse);
2787
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002788 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002789 return -1;
2790
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002791 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
2794static void alps_disconnect(struct psmouse *psmouse)
2795{
2796 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002799 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002800 if (priv->dev2)
2801 input_unregister_device(priv->dev2);
2802 if (!IS_ERR_OR_NULL(priv->dev3))
2803 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 kfree(priv);
2805}
2806
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002807static void alps_set_abs_params_st(struct alps_data *priv,
2808 struct input_dev *dev1)
2809{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002810 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2811 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002812 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002813}
2814
Masaki Ota8eccd392015-03-27 20:44:21 -07002815static void alps_set_abs_params_mt_common(struct alps_data *priv,
2816 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002817{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002818 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2819 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002820
Hans de Goedef3f33c62014-07-29 11:22:07 -07002821 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2822 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2823
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002824 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2825 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002826}
Yunkang Tang38088432014-07-26 13:51:41 -07002827
Hans de Goede688ea362015-05-20 14:42:50 -07002828static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2829 struct input_dev *dev1)
Masaki Ota8eccd392015-03-27 20:44:21 -07002830{
2831 alps_set_abs_params_mt_common(priv, dev1);
2832 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2833
2834 input_mt_init_slots(dev1, MAX_TOUCHES,
2835 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
Hans de Goede1662c0332015-05-20 14:42:15 -07002836 INPUT_MT_SEMI_MT);
Masaki Ota8eccd392015-03-27 20:44:21 -07002837}
2838
2839static void alps_set_abs_params_v7(struct alps_data *priv,
2840 struct input_dev *dev1)
2841{
2842 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002843 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002844
2845 input_mt_init_slots(dev1, MAX_TOUCHES,
2846 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2847 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002848
2849 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2850}
2851
2852static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2853 struct input_dev *dev1)
2854{
2855 alps_set_abs_params_mt_common(priv, dev1);
2856 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2857 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2858
2859 input_mt_init_slots(dev1, MAX_TOUCHES,
2860 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2861 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002862}
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864int alps_init(struct psmouse *psmouse)
2865{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002866 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002867 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002868 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002870 error = priv->hw_init(psmouse);
2871 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 goto init_fail;
2873
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002874 /*
2875 * Undo part of setup done for us by psmouse core since touchpad
2876 * is not a relative device.
2877 */
2878 __clear_bit(EV_REL, dev1->evbit);
2879 __clear_bit(REL_X, dev1->relbit);
2880 __clear_bit(REL_Y, dev1->relbit);
2881
2882 /*
2883 * Now set up our capabilities.
2884 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002885 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2886 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2887 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002888 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2889 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002891 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002892
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002893 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002895 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002896 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2897 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
2899
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002900 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002901 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2902 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
2904
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002905 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002906 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2907 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2908 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2909 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002910 } else if (priv->flags & ALPS_BUTTONPAD) {
2911 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2912 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002913 } else {
2914 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2915 }
2916
Pali Rohárdfba8602015-01-09 12:48:44 -08002917 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002918 struct input_dev *dev2;
2919
2920 dev2 = input_allocate_device();
2921 if (!dev2) {
2922 psmouse_err(psmouse,
2923 "failed to allocate trackstick device\n");
2924 error = -ENOMEM;
2925 goto init_fail;
2926 }
2927
2928 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2929 psmouse->ps2dev.serio->phys);
2930 dev2->phys = priv->phys2;
2931
Pali Rohárdfba8602015-01-09 12:48:44 -08002932 /*
2933 * format of input device name is: "protocol vendor name"
2934 * see function psmouse_switch_protocol() in psmouse-base.c
2935 */
2936 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002937
2938 dev2->id.bustype = BUS_I8042;
2939 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002940 dev2->id.product = PSMOUSE_ALPS;
2941 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002942 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002943
Pali Rohár04aae282015-01-14 13:18:30 -08002944 input_set_capability(dev2, EV_REL, REL_X);
2945 input_set_capability(dev2, EV_REL, REL_Y);
2946 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2947 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2948 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002949
Pali Rohár04aae282015-01-14 13:18:30 -08002950 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002951 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2952
Pali Rohár04aae282015-01-14 13:18:30 -08002953 error = input_register_device(dev2);
2954 if (error) {
2955 psmouse_err(psmouse,
2956 "failed to register trackstick device: %d\n",
2957 error);
2958 input_free_device(dev2);
2959 goto init_fail;
2960 }
2961
2962 priv->dev2 = dev2;
2963 }
2964
2965 priv->psmouse = psmouse;
2966
2967 INIT_DELAYED_WORK(&priv->dev3_register_work,
2968 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002971 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 psmouse->disconnect = alps_disconnect;
2973 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002974 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002976 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2977 psmouse->resync_time = 0;
2978
Pali Rohár9d720b32014-11-08 12:58:57 -08002979 /* Allow 2 invalid packets without resetting device */
2980 psmouse->resetafter = psmouse->pktsize * 2;
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return 0;
2983
2984init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002985 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002986 /*
2987 * Even though we did not allocate psmouse->private we do free
2988 * it here.
2989 */
2990 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002991 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002992 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002995int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002997 struct alps_data *priv;
2998 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003000 error = alps_identify(psmouse, NULL);
3001 if (error)
3002 return error;
3003
3004 /*
3005 * Reset the device to make sure it is fully operational:
3006 * on some laptops, like certain Dell Latitudes, we may
3007 * fail to properly detect presence of trackstick if device
3008 * has not been reset.
3009 */
3010 psmouse_reset(psmouse);
3011
3012 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3013 if (!priv)
3014 return -ENOMEM;
3015
3016 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003017 if (error) {
3018 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003019 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 if (set_properties) {
3023 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003024 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003025 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003026 psmouse->model = priv->proto_version;
3027 } else {
3028 /*
3029 * Destroy alps_data structure we allocated earlier since
3030 * this was just a "trial run". Otherwise we'll keep it
3031 * to be used by alps_init() which has to be called if
3032 * we succeed and set_properties is true.
3033 */
3034 kfree(priv);
3035 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return 0;
3039}
3040