blob: c893af6a33b03e85262485d2972d17e17bba5f45 [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
Pali Rohárdae928e2016-01-06 15:50:08 -080034#define ALPS_REG_BASE_V7 0xc2c0
Kevin Cernekeecd401202013-02-13 22:28:07 -080035#define ALPS_REG_BASE_PINNACLE 0x0000
36
Seth Forshee25bded72011-11-07 19:53:36 -080037static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
38 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
39 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
40 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
41 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
42 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
43 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
44 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
45 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
46 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
47 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
48 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
49 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
50 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
51 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
52 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
53 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
54};
55
56static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
57 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
58 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
59 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
60 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
61 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
62 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
63 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
64 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
65 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
66 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
67 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
68 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
69 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
70 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
71 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
72 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
73};
74
Yunkang Tang95f75e92013-12-01 22:33:52 -080075static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
76 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
77 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
78 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
79 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
80 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
81 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
82 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
83 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
84 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
85 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
86 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
87 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
88 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
89 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
90 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
91 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
92};
93
Seth Forshee25bded72011-11-07 19:53:36 -080094
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080095#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
96#define ALPS_PASS 0x04 /* device has a pass-through port */
97
98#define ALPS_WHEEL 0x08 /* hardware wheel present */
99#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
100#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
101#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800102#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
103 6-byte ALPS packet */
Hans de Goede19556212015-10-26 01:50:28 -0700104#define ALPS_STICK_BITS 0x100 /* separate stick button bits */
Yunkang Tang38088432014-07-26 13:51:41 -0700105#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Ben Gamari7ad8a102016-10-04 11:41:58 -0700106#define ALPS_DUALPOINT_WITH_PRESSURE 0x400 /* device can report trackpoint pressure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Helge Dellere38de672006-09-10 21:54:39 -0400108static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800109 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
110 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
111 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
113 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
114 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
115 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
117 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
118 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
119 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
120 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
121 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
122 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
123 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800124
125 /*
126 * XXX This entry is suspicious. First byte has zero lower nibble,
127 * which is what a normal mouse would report. Also, the value 0x0e
128 * isn't valid per PS/2 spec.
129 */
130 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
131
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800132 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
133 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800134 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800135 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
137 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
138 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
139 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
140 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800143static const struct alps_protocol_info alps_v3_protocol_data = {
144 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
145};
146
147static const struct alps_protocol_info alps_v3_rushmore_data = {
148 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
149};
150
Pali Rohár09c398b2017-03-07 09:41:34 -0800151static const struct alps_protocol_info alps_v4_protocol_data = {
152 ALPS_PROTO_V4, 0x8f, 0x8f, 0
153};
154
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800155static const struct alps_protocol_info alps_v5_protocol_data = {
156 ALPS_PROTO_V5, 0xc8, 0xd8, 0
157};
158
159static const struct alps_protocol_info alps_v7_protocol_data = {
160 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
161};
162
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700163static const struct alps_protocol_info alps_v8_protocol_data = {
164 ALPS_PROTO_V8, 0x18, 0x18, 0
165};
166
Pali Rohárc9815232017-03-07 09:42:46 -0800167static const struct alps_protocol_info alps_v9_protocol_data = {
168 ALPS_PROTO_V9, 0xc8, 0xc8, 0
169};
170
Hans de Goede19556212015-10-26 01:50:28 -0700171/*
172 * Some v2 models report the stick buttons in separate bits
173 */
174static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
175#if defined(CONFIG_DMI) && defined(CONFIG_X86)
176 {
177 /* Extrapolated from other entries */
178 .matches = {
179 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
180 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
181 },
182 },
183 {
184 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
185 .matches = {
186 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
187 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
188 },
189 },
190 {
191 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
192 .matches = {
193 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
194 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
195 },
196 },
197 {
198 /* Extrapolated from other entries */
199 .matches = {
200 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
201 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
202 },
203 },
204#endif
205 { }
206};
207
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800208static void alps_set_abs_params_st(struct alps_data *priv,
209 struct input_dev *dev1);
Hans de Goede688ea362015-05-20 14:42:50 -0700210static void alps_set_abs_params_semi_mt(struct alps_data *priv,
211 struct input_dev *dev1);
Masaki Ota8eccd392015-03-27 20:44:21 -0700212static void alps_set_abs_params_v7(struct alps_data *priv,
213 struct input_dev *dev1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700214static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
215 struct input_dev *dev1);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800216
Seth Forsheed4b347b2011-11-07 19:53:15 -0800217/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800219static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800220 unsigned char data)
221{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800222 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800223}
224
Pali Rohár04aae282015-01-14 13:18:30 -0800225static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800226 int left, int right, int middle)
227{
Martin Buckc91ed052010-03-13 22:23:58 -0800228 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800229
Martin Buckc91ed052010-03-13 22:23:58 -0800230 /*
231 * If shared button has already been reported on the
232 * other device (dev2) then this event should be also
233 * sent through that device.
234 */
Pali Rohár04aae282015-01-14 13:18:30 -0800235 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800236 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800237
Pali Rohár04aae282015-01-14 13:18:30 -0800238 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800239 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800240
Pali Rohár04aae282015-01-14 13:18:30 -0800241 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800242 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800243
Martin Buckc91ed052010-03-13 22:23:58 -0800244 /*
245 * Sync the _other_ device now, we'll do the first
246 * device later once we report the rest of the events.
247 */
Pali Rohár04aae282015-01-14 13:18:30 -0800248 if (dev2)
249 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800250}
251
Seth Forshee25bded72011-11-07 19:53:36 -0800252static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct alps_data *priv = psmouse->private;
255 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500256 struct input_dev *dev = psmouse->dev;
257 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500259 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800261 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400262 left = packet[2] & 0x10;
263 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 middle = 0;
265 x = packet[1] | ((packet[0] & 0x07) << 7);
266 y = packet[4] | ((packet[3] & 0x07) << 7);
267 z = packet[5];
268 } else {
269 left = packet[3] & 1;
270 right = packet[3] & 2;
271 middle = packet[3] & 4;
272 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
273 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
274 z = packet[5];
275 }
276
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800277 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400278 back = packet[0] & 0x10;
279 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500280 }
281
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800282 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500283 back = packet[3] & 4;
284 forward = packet[2] & 4;
285 if ((middle = forward && back))
286 forward = back = 0;
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 ges = packet[2] & 1;
290 fin = packet[2] & 2;
291
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800292 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
294 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700295
Pali Rohár04aae282015-01-14 13:18:30 -0800296 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 input_sync(dev2);
299 return;
300 }
301
Hans de Goede19556212015-10-26 01:50:28 -0700302 /* Some models have separate stick button bits */
303 if (priv->flags & ALPS_STICK_BITS) {
Hans de Goede92bac832015-04-12 15:42:35 -0700304 left |= packet[0] & 1;
305 right |= packet[0] & 2;
306 middle |= packet[0] & 4;
307 }
308
Pali Rohár04aae282015-01-14 13:18:30 -0800309 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800312 if (ges && !fin)
313 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /*
316 * A "tap and drag" operation is reported by the hardware as a transition
317 * from (!fin && ges) to (fin && ges). This should be translated to the
318 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
319 */
320 if (ges && fin && !priv->prev_fin) {
321 input_report_abs(dev, ABS_X, x);
322 input_report_abs(dev, ABS_Y, y);
323 input_report_abs(dev, ABS_PRESSURE, 0);
324 input_report_key(dev, BTN_TOOL_FINGER, 0);
325 input_sync(dev);
326 }
327 priv->prev_fin = fin;
328
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800329 if (z > 30)
330 input_report_key(dev, BTN_TOUCH, 1);
331 if (z < 25)
332 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (z > 0) {
335 input_report_abs(dev, ABS_X, x);
336 input_report_abs(dev, ABS_Y, y);
337 }
338
339 input_report_abs(dev, ABS_PRESSURE, z);
340 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
341
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800342 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500343 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800345 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500346 input_report_key(dev, BTN_FORWARD, forward);
347 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800350 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800351 input_report_key(dev, BTN_0, packet[2] & 4);
352 input_report_key(dev, BTN_1, packet[0] & 0x10);
353 input_report_key(dev, BTN_2, packet[3] & 4);
354 input_report_key(dev, BTN_3, packet[0] & 0x20);
355 }
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 input_sync(dev);
358}
359
Hans de Goede036e6c72014-07-25 22:38:51 -0700360static void alps_get_bitmap_points(unsigned int map,
361 struct alps_bitmap_point *low,
362 struct alps_bitmap_point *high,
363 int *fingers)
364{
365 struct alps_bitmap_point *point;
366 int i, bit, prev_bit = 0;
367
368 point = low;
369 for (i = 0; map != 0; i++, map >>= 1) {
370 bit = map & 1;
371 if (bit) {
372 if (!prev_bit) {
373 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700374 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700375 (*fingers)++;
376 }
377 point->num_bits++;
378 } else {
379 if (prev_bit)
380 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700381 }
382 prev_bit = bit;
383 }
384}
385
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800386/*
Hans de Goededccf1dd2015-05-20 14:43:09 -0700387 * Process bitmap data from semi-mt protocols. Returns the number of
Seth Forshee01ce6612011-11-07 19:54:13 -0800388 * fingers detected. A return value of 0 means at least one of the
389 * bitmaps was empty.
390 *
391 * The bitmaps don't have enough data to track fingers, so this function
392 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700393 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800394 * is greater than 0.
395 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800396static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700397 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800398{
Hans de Goede4dd26572015-05-20 14:41:10 -0700399 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
Seth Forshee01ce6612011-11-07 19:54:13 -0800400 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
401 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Hans de Goede4dd26572015-05-20 14:41:10 -0700402 struct input_mt_pos corner[4];
Seth Forshee01ce6612011-11-07 19:54:13 -0800403
Hans de Goede02d04252014-07-25 22:43:35 -0700404 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800405 return 0;
406
Hans de Goede02d04252014-07-25 22:43:35 -0700407 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
408 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800409
410 /*
411 * Fingers can overlap, so we use the maximum count of fingers
412 * on either axis as the finger count.
413 */
414 fingers = max(fingers_x, fingers_y);
415
416 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700417 * If an axis reports only a single contact, we have overlapping or
418 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800419 */
Hans de Goede20bea682014-07-25 22:33:33 -0700420 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700421 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700422 x_low.num_bits = x_low.num_bits - i;
423 x_high.start_bit = x_low.start_bit + i;
424 x_high.num_bits = max(i, 1);
425 }
426 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700427 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700428 y_low.num_bits = y_low.num_bits - i;
429 y_high.start_bit = y_low.start_bit + i;
430 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800431 }
432
Hans de Goede4dd26572015-05-20 14:41:10 -0700433 /* top-left corner */
434 corner[0].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700435 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
436 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700437 corner[0].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700438 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
439 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800440
Hans de Goede4dd26572015-05-20 14:41:10 -0700441 /* top-right corner */
442 corner[1].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700443 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
444 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700445 corner[1].y =
446 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
447 (2 * (priv->y_bits - 1));
448
449 /* bottom-right corner */
450 corner[2].x =
451 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
452 (2 * (priv->x_bits - 1));
453 corner[2].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700454 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
455 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800456
Hans de Goede4dd26572015-05-20 14:41:10 -0700457 /* bottom-left corner */
458 corner[3].x =
459 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
460 (2 * (priv->x_bits - 1));
461 corner[3].y =
Seth Forshee01ce6612011-11-07 19:54:13 -0800462 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
463 (2 * (priv->y_bits - 1));
464
Hans de Goededccf1dd2015-05-20 14:43:09 -0700465 /* x-bitmap order is reversed on v5 touchpads */
466 if (priv->proto_version == ALPS_PROTO_V5) {
467 for (i = 0; i < 4; i++)
468 corner[i].x = priv->x_max - corner[i].x;
Hans de Goede40e8f532014-07-25 22:37:15 -0700469 }
470
Hans de Goededccf1dd2015-05-20 14:43:09 -0700471 /* y-bitmap order is reversed on v3 and v4 touchpads */
472 if (priv->proto_version == ALPS_PROTO_V3 ||
473 priv->proto_version == ALPS_PROTO_V4) {
Hans de Goede4dd26572015-05-20 14:41:10 -0700474 for (i = 0; i < 4; i++)
475 corner[i].y = priv->y_max - corner[i].y;
Seth Forshee01ce6612011-11-07 19:54:13 -0800476 }
477
Hans de Goede4dd26572015-05-20 14:41:10 -0700478 /*
479 * We only select a corner for the second touch once per 2 finger
480 * touch sequence to avoid the chosen corner (and thus the coordinates)
481 * jumping around when the first touch is in the middle.
482 */
483 if (priv->second_touch == -1) {
484 /* Find corner closest to our st coordinates */
485 closest = 0x7fffffff;
486 for (i = 0; i < 4; i++) {
487 int dx = fields->st.x - corner[i].x;
488 int dy = fields->st.y - corner[i].y;
489 int distance = dx * dx + dy * dy;
490
491 if (distance < closest) {
492 priv->second_touch = i;
493 closest = distance;
494 }
495 }
496 /* And select the opposite corner to use for the 2nd touch */
497 priv->second_touch = (priv->second_touch + 2) % 4;
498 }
499
500 fields->mt[0] = fields->st;
501 fields->mt[1] = corner[priv->second_touch];
502
Seth Forshee01ce6612011-11-07 19:54:13 -0800503 return fingers;
504}
505
Hans de Goedecdf333e2014-07-25 22:44:42 -0700506static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800507{
508 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700509 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
510 input_report_abs(dev, ABS_MT_POSITION_X, x);
511 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800512}
513
Hans de Goedecdf333e2014-07-25 22:44:42 -0700514static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800515{
Hans de Goede02d04252014-07-25 22:43:35 -0700516 struct alps_data *priv = psmouse->private;
517 struct input_dev *dev = psmouse->dev;
518 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700519 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700520
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800521 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700522 for (i = 0; i < n; i++)
523 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
524
525 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800526}
527
Hans de Goede68c21872014-07-25 22:47:25 -0700528static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
529{
530 struct alps_data *priv = psmouse->private;
531 struct input_dev *dev = psmouse->dev;
532 struct alps_fields *f = &priv->f;
533
534 /* Use st data when we don't have mt data */
535 if (fingers < 2) {
536 f->mt[0].x = f->st.x;
537 f->mt[0].y = f->st.y;
538 fingers = f->pressure > 0 ? 1 : 0;
Hans de Goede4dd26572015-05-20 14:41:10 -0700539 priv->second_touch = -1;
Hans de Goede68c21872014-07-25 22:47:25 -0700540 }
541
Hans de Goede1662c0332015-05-20 14:42:15 -0700542 if (fingers >= 1)
543 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
544 if (fingers >= 2)
545 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
546 input_mt_sync_frame(dev);
Hans de Goede68c21872014-07-25 22:47:25 -0700547
548 input_mt_report_finger_count(dev, fingers);
549
550 input_report_key(dev, BTN_LEFT, f->left);
551 input_report_key(dev, BTN_RIGHT, f->right);
552 input_report_key(dev, BTN_MIDDLE, f->middle);
553
554 input_report_abs(dev, ABS_PRESSURE, f->pressure);
555
556 input_sync(dev);
557}
558
Seth Forshee25bded72011-11-07 19:53:36 -0800559static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
560{
561 struct alps_data *priv = psmouse->private;
562 unsigned char *packet = psmouse->packet;
563 struct input_dev *dev = priv->dev2;
564 int x, y, z, left, right, middle;
565
Pali Rohár34412ba2015-01-09 12:48:58 -0800566 /* It should be a DualPoint when received trackstick packet */
567 if (!(priv->flags & ALPS_DUALPOINT)) {
568 psmouse_warn(psmouse,
569 "Rejected trackstick packet from non DualPoint device");
570 return;
571 }
572
Seth Forshee25bded72011-11-07 19:53:36 -0800573 /* Sanity check packet */
574 if (!(packet[0] & 0x40)) {
575 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
576 return;
577 }
578
579 /*
580 * There's a special packet that seems to indicate the end
581 * of a stream of trackstick data. Filter these out.
582 */
583 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
584 return;
585
586 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
587 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
588 z = (packet[4] & 0x7c) >> 2;
589
590 /*
591 * The x and y values tend to be quite large, and when used
592 * alone the trackstick is difficult to use. Scale them down
593 * to compensate.
594 */
595 x /= 8;
596 y /= 8;
597
598 input_report_rel(dev, REL_X, x);
599 input_report_rel(dev, REL_Y, -y);
600
601 /*
602 * Most ALPS models report the trackstick buttons in the touchpad
603 * packets, but a few report them here. No reliable way has been
604 * found to differentiate between the models upfront, so we enable
605 * the quirk in response to seeing a button press in the trackstick
606 * packet.
607 */
608 left = packet[3] & 0x01;
609 right = packet[3] & 0x02;
610 middle = packet[3] & 0x04;
611
612 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
613 (left || right || middle))
614 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
615
616 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
617 input_report_key(dev, BTN_LEFT, left);
618 input_report_key(dev, BTN_RIGHT, right);
619 input_report_key(dev, BTN_MIDDLE, middle);
620 }
621
622 input_sync(dev);
623 return;
624}
625
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800626static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
627{
628 f->left = !!(p[3] & 0x01);
629 f->right = !!(p[3] & 0x02);
630 f->middle = !!(p[3] & 0x04);
631
632 f->ts_left = !!(p[3] & 0x10);
633 f->ts_right = !!(p[3] & 0x20);
634 f->ts_middle = !!(p[3] & 0x40);
635}
636
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700637static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800638 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800639{
640 f->first_mp = !!(p[4] & 0x40);
641 f->is_mp = !!(p[0] & 0x40);
642
Hans de Goedea839cd52015-05-20 14:39:21 -0700643 if (f->is_mp) {
644 f->fingers = (p[5] & 0x3) + 1;
645 f->x_map = ((p[4] & 0x7e) << 8) |
646 ((p[1] & 0x7f) << 2) |
647 ((p[0] & 0x30) >> 4);
648 f->y_map = ((p[3] & 0x70) << 4) |
649 ((p[2] & 0x7f) << 1) |
650 (p[4] & 0x01);
651 } else {
652 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
653 ((p[0] & 0x30) >> 4);
654 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
655 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800656
Hans de Goedea839cd52015-05-20 14:39:21 -0700657 alps_decode_buttons_v3(f, p);
658 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700659
660 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800661}
662
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700663static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800664 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800665{
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700666 f->first_mp = !!(p[4] & 0x40);
Yunkang Tangf105e342014-07-25 22:29:24 -0700667 f->is_mp = !!(p[5] & 0x40);
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700668
Hans de Goedea839cd52015-05-20 14:39:21 -0700669 if (f->is_mp) {
670 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
671 f->x_map = ((p[5] & 0x10) << 11) |
672 ((p[4] & 0x7e) << 8) |
673 ((p[1] & 0x7f) << 2) |
674 ((p[0] & 0x30) >> 4);
675 f->y_map = ((p[5] & 0x20) << 6) |
676 ((p[3] & 0x70) << 4) |
677 ((p[2] & 0x7f) << 1) |
678 (p[4] & 0x01);
679 } else {
680 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
681 ((p[0] & 0x30) >> 4);
682 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
683 f->pressure = p[5] & 0x7f;
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700684
Hans de Goedea839cd52015-05-20 14:39:21 -0700685 alps_decode_buttons_v3(f, p);
686 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700687
688 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800689}
690
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700691static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800692 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800693{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800694 u64 palm_data = 0;
695 struct alps_data *priv = psmouse->private;
696
Dave Turvene75af9e52013-02-21 22:58:28 -0800697 f->first_mp = !!(p[0] & 0x02);
698 f->is_mp = !!(p[0] & 0x20);
699
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800700 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700701 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
702 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
703 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800704 alps_decode_buttons_v3(f, p);
705 } else {
706 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800707 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800708
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800709 palm_data = (p[1] & 0x7f) |
710 ((p[2] & 0x7f) << 7) |
711 ((p[4] & 0x7f) << 14) |
712 ((p[5] & 0x7f) << 21) |
713 ((p[3] & 0x07) << 28) |
714 (((u64)p[3] & 0x70) << 27) |
715 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800716
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800717 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
718 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
719
720 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
721 f->x_map = (palm_data >> priv->y_bits) &
722 (BIT(priv->x_bits) - 1);
723 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700724
725 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800726}
727
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800728static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800729{
730 struct alps_data *priv = psmouse->private;
731 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800732 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700733 struct alps_fields *f = &priv->f;
734 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800735
Hans de Goede02d04252014-07-25 22:43:35 -0700736 memset(f, 0, sizeof(*f));
737
738 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800739
740 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800741 * There's no single feature of touchpad position and bitmap packets
742 * that can be used to distinguish between them. We rely on the fact
743 * that a bitmap packet should always follow a position packet with
744 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800745 */
746 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800747 /*
748 * Sometimes a position packet will indicate a multi-packet
749 * sequence, but then what follows is another position
750 * packet. Check for this, and when it happens process the
751 * position packet as usual.
752 */
Hans de Goede02d04252014-07-25 22:43:35 -0700753 if (f->is_mp) {
754 fingers = f->fingers;
Hans de Goede44b77f32015-05-20 14:40:06 -0700755 /*
756 * Bitmap processing uses position packet's coordinate
757 * data, so we need to do decode it first.
758 */
759 priv->decode_fields(f, priv->multi_data, psmouse);
Hans de Goededccf1dd2015-05-20 14:43:09 -0700760 if (alps_process_bitmap(priv, f) == 0)
761 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800762 } else {
763 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800764 }
765 }
766
Seth Forshee01ce6612011-11-07 19:54:13 -0800767 /*
768 * Bit 6 of byte 0 is not usually set in position packets. The only
769 * times it seems to be set is in situations where the data is
770 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
771 * this combined with the fact that this bit is useful for filtering
772 * out misidentified bitmap packets, we reject anything with this
773 * bit set.
774 */
Hans de Goede02d04252014-07-25 22:43:35 -0700775 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800776 return;
777
Hans de Goede02d04252014-07-25 22:43:35 -0700778 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800779 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800780 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
781 return;
782 }
783
784 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800785
Seth Forshee25bded72011-11-07 19:53:36 -0800786 /*
787 * Sometimes the hardware sends a single packet with z = 0
788 * in the middle of a stream. Real releases generate packets
789 * with x, y, and z all zero, so these seem to be flukes.
790 * Ignore them.
791 */
Hans de Goede02d04252014-07-25 22:43:35 -0700792 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800793 return;
794
Hans de Goede68c21872014-07-25 22:47:25 -0700795 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800796
Pali Rohár34412ba2015-01-09 12:48:58 -0800797 if ((priv->flags & ALPS_DUALPOINT) &&
798 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700799 input_report_key(dev2, BTN_LEFT, f->ts_left);
800 input_report_key(dev2, BTN_RIGHT, f->ts_right);
801 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800802 input_sync(dev2);
803 }
804}
805
806static void alps_process_packet_v3(struct psmouse *psmouse)
807{
808 unsigned char *packet = psmouse->packet;
809
810 /*
811 * v3 protocol packets come in three types, two representing
812 * touchpad data and one representing trackstick data.
813 * Trackstick packets seem to be distinguished by always
814 * having 0x3f in the last byte. This value has never been
815 * observed in the last byte of either of the other types
816 * of packets.
817 */
818 if (packet[5] == 0x3f) {
819 alps_process_trackstick_packet_v3(psmouse);
820 return;
821 }
822
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800823 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800824}
825
Yunkang Tang95f75e92013-12-01 22:33:52 -0800826static void alps_process_packet_v6(struct psmouse *psmouse)
827{
828 struct alps_data *priv = psmouse->private;
829 unsigned char *packet = psmouse->packet;
830 struct input_dev *dev = psmouse->dev;
831 struct input_dev *dev2 = priv->dev2;
832 int x, y, z, left, right, middle;
833
834 /*
835 * We can use Byte5 to distinguish if the packet is from Touchpad
836 * or Trackpoint.
837 * Touchpad: 0 - 0x7E
838 * Trackpoint: 0x7F
839 */
840 if (packet[5] == 0x7F) {
841 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800842 if (!(priv->flags & ALPS_DUALPOINT)) {
843 psmouse_warn(psmouse,
844 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800845 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800846 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800847
848 /* Trackpoint packet */
849 x = packet[1] | ((packet[3] & 0x20) << 2);
850 y = packet[2] | ((packet[3] & 0x40) << 1);
851 z = packet[4];
852 left = packet[3] & 0x01;
853 right = packet[3] & 0x02;
854 middle = packet[3] & 0x04;
855
856 /* To prevent the cursor jump when finger lifted */
857 if (x == 0x7F && y == 0x7F && z == 0x7F)
858 x = y = z = 0;
859
860 /* Divide 4 since trackpoint's speed is too fast */
861 input_report_rel(dev2, REL_X, (char)x / 4);
862 input_report_rel(dev2, REL_Y, -((char)y / 4));
863
864 input_report_key(dev2, BTN_LEFT, left);
865 input_report_key(dev2, BTN_RIGHT, right);
866 input_report_key(dev2, BTN_MIDDLE, middle);
867
868 input_sync(dev2);
869 return;
870 }
871
872 /* Touchpad packet */
873 x = packet[1] | ((packet[3] & 0x78) << 4);
874 y = packet[2] | ((packet[4] & 0x78) << 4);
875 z = packet[5];
876 left = packet[3] & 0x01;
877 right = packet[3] & 0x02;
878
879 if (z > 30)
880 input_report_key(dev, BTN_TOUCH, 1);
881 if (z < 25)
882 input_report_key(dev, BTN_TOUCH, 0);
883
884 if (z > 0) {
885 input_report_abs(dev, ABS_X, x);
886 input_report_abs(dev, ABS_Y, y);
887 }
888
889 input_report_abs(dev, ABS_PRESSURE, z);
890 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
891
892 /* v6 touchpad does not have middle button */
893 input_report_key(dev, BTN_LEFT, left);
894 input_report_key(dev, BTN_RIGHT, right);
895
896 input_sync(dev);
897}
898
Seth Forshee25bded72011-11-07 19:53:36 -0800899static void alps_process_packet_v4(struct psmouse *psmouse)
900{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700901 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800902 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700903 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700904 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700905
906 /*
907 * v4 has a 6-byte encoding for bitmap data, but this data is
908 * broken up between 3 normal packets. Use priv->multi_packet to
909 * track our position in the bitmap packet.
910 */
911 if (packet[6] & 0x40) {
912 /* sync, reset position */
913 priv->multi_packet = 0;
914 }
915
916 if (WARN_ON_ONCE(priv->multi_packet > 2))
917 return;
918
919 offset = 2 * priv->multi_packet;
920 priv->multi_data[offset] = packet[6];
921 priv->multi_data[offset + 1] = packet[7];
922
Hans de Goede44b77f32015-05-20 14:40:06 -0700923 f->left = !!(packet[4] & 0x01);
924 f->right = !!(packet[4] & 0x02);
925
926 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
927 ((packet[0] & 0x30) >> 4);
928 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
929 f->pressure = packet[5] & 0x7f;
930
George Pantalos3b7e09f2012-05-10 22:31:59 -0700931 if (++priv->multi_packet > 2) {
932 priv->multi_packet = 0;
933
Hans de Goede02d04252014-07-25 22:43:35 -0700934 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700935 ((priv->multi_data[3] & 0x60) << 3) |
936 ((priv->multi_data[0] & 0x3f) << 2) |
937 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700938 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700939 ((priv->multi_data[3] & 0x1f) << 5) |
940 (priv->multi_data[1] & 0x1f);
941
Hans de Goede02d04252014-07-25 22:43:35 -0700942 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700943 }
Seth Forshee25bded72011-11-07 19:53:36 -0800944
Hans de Goede68c21872014-07-25 22:47:25 -0700945 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800946}
947
Yunkang Tang38088432014-07-26 13:51:41 -0700948static bool alps_is_valid_package_v7(struct psmouse *psmouse)
949{
950 switch (psmouse->pktcnt) {
951 case 3:
952 return (psmouse->packet[2] & 0x40) == 0x40;
953 case 4:
954 return (psmouse->packet[3] & 0x48) == 0x48;
955 case 6:
956 return (psmouse->packet[5] & 0x40) == 0x00;
957 }
958 return true;
959}
960
961static unsigned char alps_get_packet_id_v7(char *byte)
962{
963 unsigned char packet_id;
964
965 if (byte[4] & 0x40)
966 packet_id = V7_PACKET_ID_TWO;
967 else if (byte[4] & 0x01)
968 packet_id = V7_PACKET_ID_MULTI;
969 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
970 packet_id = V7_PACKET_ID_NEW;
971 else if (byte[1] == 0x00 && byte[4] == 0x00)
972 packet_id = V7_PACKET_ID_IDLE;
973 else
974 packet_id = V7_PACKET_ID_UNKNOWN;
975
976 return packet_id;
977}
978
979static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
980 unsigned char *pkt,
981 unsigned char pkt_id)
982{
983 mt[0].x = ((pkt[2] & 0x80) << 4);
984 mt[0].x |= ((pkt[2] & 0x3F) << 5);
985 mt[0].x |= ((pkt[3] & 0x30) >> 1);
986 mt[0].x |= (pkt[3] & 0x07);
987 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
988
989 mt[1].x = ((pkt[3] & 0x80) << 4);
990 mt[1].x |= ((pkt[4] & 0x80) << 3);
991 mt[1].x |= ((pkt[4] & 0x3F) << 4);
992 mt[1].y = ((pkt[5] & 0x80) << 3);
993 mt[1].y |= ((pkt[5] & 0x3F) << 4);
994
995 switch (pkt_id) {
996 case V7_PACKET_ID_TWO:
997 mt[1].x &= ~0x000F;
998 mt[1].y |= 0x000F;
Hans de Goede72eceab2015-05-20 14:37:41 -0700999 /* Detect false-postive touches where x & y report max value */
1000 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
1001 mt[1].x = 0;
1002 /* y gets set to 0 at the end of this function */
1003 }
Yunkang Tang38088432014-07-26 13:51:41 -07001004 break;
1005
1006 case V7_PACKET_ID_MULTI:
1007 mt[1].x &= ~0x003F;
1008 mt[1].y &= ~0x0020;
1009 mt[1].y |= ((pkt[4] & 0x02) << 4);
1010 mt[1].y |= 0x001F;
1011 break;
1012
1013 case V7_PACKET_ID_NEW:
1014 mt[1].x &= ~0x003F;
1015 mt[1].x |= (pkt[0] & 0x20);
1016 mt[1].y |= 0x000F;
1017 break;
1018 }
1019
1020 mt[0].y = 0x7FF - mt[0].y;
1021 mt[1].y = 0x7FF - mt[1].y;
1022}
1023
1024static int alps_get_mt_count(struct input_mt_pos *mt)
1025{
Hans de Goede7091c442014-12-18 09:53:34 -08001026 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -07001027
Hans de Goede7091c442014-12-18 09:53:34 -08001028 for (i = 0; i < MAX_TOUCHES; i++) {
1029 if (mt[i].x != 0 || mt[i].y != 0)
1030 fingers++;
1031 }
Yunkang Tang38088432014-07-26 13:51:41 -07001032
Hans de Goede7091c442014-12-18 09:53:34 -08001033 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -07001034}
1035
1036static int alps_decode_packet_v7(struct alps_fields *f,
1037 unsigned char *p,
1038 struct psmouse *psmouse)
1039{
Hans de Goeded27eb792014-12-18 09:55:14 -08001040 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -07001041 unsigned char pkt_id;
1042
1043 pkt_id = alps_get_packet_id_v7(p);
1044 if (pkt_id == V7_PACKET_ID_IDLE)
1045 return 0;
1046 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1047 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -08001048 /*
1049 * NEW packets are send to indicate a discontinuity in the finger
1050 * coordinate reporting. Specifically a finger may have moved from
1051 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1052 * us.
1053 *
1054 * NEW packets have 3 problems:
1055 * 1) They do not contain middle / right button info (on non clickpads)
1056 * this can be worked around by preserving the old button state
1057 * 2) They do not contain an accurate fingercount, and they are
1058 * typically send when the number of fingers changes. We cannot use
1059 * the old finger count as that may mismatch with the amount of
1060 * touch coordinates we've available in the NEW packet
1061 * 3) Their x data for the second touch is inaccurate leading to
1062 * a possible jump of the x coordinate by 16 units when the first
1063 * non NEW packet comes in
1064 * Since problems 2 & 3 cannot be worked around, just ignore them.
1065 */
1066 if (pkt_id == V7_PACKET_ID_NEW)
1067 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001068
1069 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1070
Yunkang Tang38088432014-07-26 13:51:41 -07001071 if (pkt_id == V7_PACKET_ID_TWO)
1072 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001073 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001074 f->fingers = 3 + (p[5] & 0x03);
1075
Hans de Goeded27eb792014-12-18 09:55:14 -08001076 f->left = (p[0] & 0x80) >> 7;
1077 if (priv->flags & ALPS_BUTTONPAD) {
1078 if (p[0] & 0x20)
1079 f->fingers++;
1080 if (p[0] & 0x10)
1081 f->fingers++;
1082 } else {
1083 f->right = (p[0] & 0x20) >> 5;
1084 f->middle = (p[0] & 0x10) >> 4;
1085 }
1086
Hans de Goede7091c442014-12-18 09:53:34 -08001087 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1088 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1089 f->mt[0].x = f->mt[1].x;
1090 f->mt[0].y = f->mt[1].y;
1091 f->mt[1].x = 0;
1092 f->mt[1].y = 0;
1093 }
1094
Yunkang Tang38088432014-07-26 13:51:41 -07001095 return 0;
1096}
1097
1098static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1099{
1100 struct alps_data *priv = psmouse->private;
1101 unsigned char *packet = psmouse->packet;
1102 struct input_dev *dev2 = priv->dev2;
1103 int x, y, z, left, right, middle;
1104
Pali Rohár34412ba2015-01-09 12:48:58 -08001105 /* It should be a DualPoint when received trackstick packet */
1106 if (!(priv->flags & ALPS_DUALPOINT)) {
1107 psmouse_warn(psmouse,
1108 "Rejected trackstick packet from non DualPoint device");
1109 return;
1110 }
1111
Yunkang Tang38088432014-07-26 13:51:41 -07001112 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1113 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1114 ((packet[3] & 0x20) << 1);
1115 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1116
1117 left = (packet[1] & 0x01);
1118 right = (packet[1] & 0x02) >> 1;
1119 middle = (packet[1] & 0x04) >> 2;
1120
Hans de Goede088df2c2015-06-04 22:31:43 -07001121 input_report_rel(dev2, REL_X, (char)x);
1122 input_report_rel(dev2, REL_Y, -((char)y));
Yunkang Tang38088432014-07-26 13:51:41 -07001123
1124 input_report_key(dev2, BTN_LEFT, left);
1125 input_report_key(dev2, BTN_RIGHT, right);
1126 input_report_key(dev2, BTN_MIDDLE, middle);
1127
1128 input_sync(dev2);
1129}
1130
1131static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1132{
1133 struct alps_data *priv = psmouse->private;
1134 struct input_dev *dev = psmouse->dev;
1135 struct alps_fields *f = &priv->f;
1136
1137 memset(f, 0, sizeof(*f));
1138
1139 if (priv->decode_fields(f, psmouse->packet, psmouse))
1140 return;
1141
1142 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1143
1144 input_mt_report_finger_count(dev, f->fingers);
1145
1146 input_report_key(dev, BTN_LEFT, f->left);
1147 input_report_key(dev, BTN_RIGHT, f->right);
1148 input_report_key(dev, BTN_MIDDLE, f->middle);
1149
1150 input_sync(dev);
1151}
1152
1153static void alps_process_packet_v7(struct psmouse *psmouse)
1154{
1155 unsigned char *packet = psmouse->packet;
1156
1157 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1158 alps_process_trackstick_packet_v7(psmouse);
1159 else
1160 alps_process_touchpad_packet_v7(psmouse);
1161}
1162
Paul Donohue23fce362016-11-28 20:16:21 -08001163static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001164{
Paul Donohue23fce362016-11-28 20:16:21 -08001165 enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001166
Ben Gamari4777ac22016-10-04 11:33:25 -07001167 switch (byte[3] & 0x30) {
1168 case 0x00:
Paul Donohue23fce362016-11-28 20:16:21 -08001169 if (SS4_IS_IDLE_V2(byte)) {
Ben Gamari4777ac22016-10-04 11:33:25 -07001170 pkt_id = SS4_PACKET_ID_IDLE;
1171 } else {
1172 pkt_id = SS4_PACKET_ID_ONE;
1173 }
1174 break;
1175 case 0x10:
1176 /* two-finger finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001177 pkt_id = SS4_PACKET_ID_TWO;
Ben Gamari4777ac22016-10-04 11:33:25 -07001178 break;
1179 case 0x20:
1180 /* stick pointer */
1181 pkt_id = SS4_PACKET_ID_STICK;
1182 break;
1183 case 0x30:
1184 /* third and fourth finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001185 pkt_id = SS4_PACKET_ID_MULTI;
Ben Gamari4777ac22016-10-04 11:33:25 -07001186 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001187 }
1188
1189 return pkt_id;
1190}
1191
1192static int alps_decode_ss4_v2(struct alps_fields *f,
1193 unsigned char *p, struct psmouse *psmouse)
1194{
1195 struct alps_data *priv = psmouse->private;
Paul Donohue23fce362016-11-28 20:16:21 -08001196 enum SS4_PACKET_ID pkt_id;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001197 unsigned int no_data_x, no_data_y;
1198
1199 pkt_id = alps_get_pkt_id_ss4_v2(p);
1200
1201 /* Current packet is 1Finger coordinate packet */
1202 switch (pkt_id) {
1203 case SS4_PACKET_ID_ONE:
1204 f->mt[0].x = SS4_1F_X_V2(p);
1205 f->mt[0].y = SS4_1F_Y_V2(p);
1206 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
Ben Gamaria8317762016-10-04 11:40:44 -07001207 /*
1208 * When a button is held the device will give us events
1209 * with x, y, and pressure of 0. This causes annoying jumps
1210 * if a touch is released while the button is held.
1211 * Handle this by claiming zero contacts.
1212 */
1213 f->fingers = f->pressure > 0 ? 1 : 0;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001214 f->first_mp = 0;
1215 f->is_mp = 0;
1216 break;
1217
1218 case SS4_PACKET_ID_TWO:
1219 if (priv->flags & ALPS_BUTTONPAD) {
1220 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1221 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1222 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1223 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1224 } else {
1225 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1226 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1227 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1228 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1229 }
1230 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1231
1232 if (SS4_IS_MF_CONTINUE(p)) {
1233 f->first_mp = 1;
1234 } else {
1235 f->fingers = 2;
1236 f->first_mp = 0;
1237 }
1238 f->is_mp = 0;
1239
1240 break;
1241
1242 case SS4_PACKET_ID_MULTI:
1243 if (priv->flags & ALPS_BUTTONPAD) {
1244 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1245 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1246 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1247 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1248 no_data_x = SS4_MFPACKET_NO_AX_BL;
1249 no_data_y = SS4_MFPACKET_NO_AY_BL;
1250 } else {
1251 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1252 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1253 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1254 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1255 no_data_x = SS4_MFPACKET_NO_AX;
1256 no_data_y = SS4_MFPACKET_NO_AY;
1257 }
1258
1259 f->first_mp = 0;
1260 f->is_mp = 1;
1261
1262 if (SS4_IS_5F_DETECTED(p)) {
1263 f->fingers = 5;
1264 } else if (f->mt[3].x == no_data_x &&
1265 f->mt[3].y == no_data_y) {
1266 f->mt[3].x = 0;
1267 f->mt[3].y = 0;
1268 f->fingers = 3;
1269 } else {
1270 f->fingers = 4;
1271 }
1272 break;
1273
Ben Gamari4777ac22016-10-04 11:33:25 -07001274 case SS4_PACKET_ID_STICK:
Paul Donohue7229c582016-11-28 20:13:47 -08001275 /*
1276 * x, y, and pressure are decoded in
1277 * alps_process_packet_ss4_v2()
1278 */
1279 f->first_mp = 0;
1280 f->is_mp = 0;
Ben Gamari4777ac22016-10-04 11:33:25 -07001281 break;
1282
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001283 case SS4_PACKET_ID_IDLE:
1284 default:
1285 memset(f, 0, sizeof(struct alps_fields));
1286 break;
1287 }
1288
Ben Gamari4777ac22016-10-04 11:33:25 -07001289 /* handle buttons */
1290 if (pkt_id == SS4_PACKET_ID_STICK) {
1291 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1292 if (!(priv->flags & ALPS_BUTTONPAD)) {
1293 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1294 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1295 }
1296 } else {
1297 f->left = !!(SS4_BTN_V2(p) & 0x01);
1298 if (!(priv->flags & ALPS_BUTTONPAD)) {
1299 f->right = !!(SS4_BTN_V2(p) & 0x02);
1300 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1301 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001302 }
1303
1304 return 0;
1305}
1306
1307static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1308{
1309 struct alps_data *priv = psmouse->private;
1310 unsigned char *packet = psmouse->packet;
1311 struct input_dev *dev = psmouse->dev;
Ben Gamari4777ac22016-10-04 11:33:25 -07001312 struct input_dev *dev2 = priv->dev2;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001313 struct alps_fields *f = &priv->f;
1314
1315 memset(f, 0, sizeof(struct alps_fields));
1316 priv->decode_fields(f, packet, psmouse);
1317 if (priv->multi_packet) {
1318 /*
1319 * Sometimes the first packet will indicate a multi-packet
1320 * sequence, but sometimes the next multi-packet would not
1321 * come. Check for this, and when it happens process the
1322 * position packet as usual.
1323 */
1324 if (f->is_mp) {
1325 /* Now process the 1st packet */
1326 priv->decode_fields(f, priv->multi_data, psmouse);
1327 } else {
1328 priv->multi_packet = 0;
1329 }
1330 }
1331
1332 /*
1333 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1334 * When it is set, it means 2nd packet comes without 1st packet come.
1335 */
1336 if (f->is_mp)
1337 return;
1338
1339 /* Save the first packet */
1340 if (!priv->multi_packet && f->first_mp) {
1341 priv->multi_packet = 1;
1342 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1343 return;
1344 }
1345
1346 priv->multi_packet = 0;
1347
Paul Donohue864db922016-11-28 20:11:25 -08001348 /* Report trackstick */
1349 if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
Paul Donohue7229c582016-11-28 20:13:47 -08001350 if (!(priv->flags & ALPS_DUALPOINT)) {
1351 psmouse_warn(psmouse,
1352 "Rejected trackstick packet from non DualPoint device");
1353 return;
Paul Donohue864db922016-11-28 20:11:25 -08001354 }
Paul Donohue7229c582016-11-28 20:13:47 -08001355
Paul Donohue23fce362016-11-28 20:16:21 -08001356 input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
1357 input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
1358 input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
Paul Donohue7229c582016-11-28 20:13:47 -08001359
1360 input_report_key(dev2, BTN_LEFT, f->ts_left);
1361 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1362 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1363
1364 input_sync(dev2);
Paul Donohue864db922016-11-28 20:11:25 -08001365 return;
1366 }
1367
1368 /* Report touchpad */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001369 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1370
1371 input_mt_report_finger_count(dev, f->fingers);
1372
1373 input_report_key(dev, BTN_LEFT, f->left);
1374 input_report_key(dev, BTN_RIGHT, f->right);
1375 input_report_key(dev, BTN_MIDDLE, f->middle);
1376
1377 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1378 input_sync(dev);
1379}
1380
1381static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1382{
1383 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1384 return false;
1385 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1386 return false;
1387 return true;
1388}
1389
Pali Rohár04aae282015-01-14 13:18:30 -08001390static DEFINE_MUTEX(alps_mutex);
1391
1392static void alps_register_bare_ps2_mouse(struct work_struct *work)
1393{
1394 struct alps_data *priv =
1395 container_of(work, struct alps_data, dev3_register_work.work);
1396 struct psmouse *psmouse = priv->psmouse;
1397 struct input_dev *dev3;
1398 int error = 0;
1399
1400 mutex_lock(&alps_mutex);
1401
1402 if (priv->dev3)
1403 goto out;
1404
1405 dev3 = input_allocate_device();
1406 if (!dev3) {
1407 psmouse_err(psmouse, "failed to allocate secondary device\n");
1408 error = -ENOMEM;
1409 goto out;
1410 }
1411
1412 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1413 psmouse->ps2dev.serio->phys,
1414 (priv->dev2 ? "input2" : "input1"));
1415 dev3->phys = priv->phys3;
1416
1417 /*
1418 * format of input device name is: "protocol vendor name"
1419 * see function psmouse_switch_protocol() in psmouse-base.c
1420 */
1421 dev3->name = "PS/2 ALPS Mouse";
1422
1423 dev3->id.bustype = BUS_I8042;
1424 dev3->id.vendor = 0x0002;
1425 dev3->id.product = PSMOUSE_PS2;
1426 dev3->id.version = 0x0000;
1427 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1428
1429 input_set_capability(dev3, EV_REL, REL_X);
1430 input_set_capability(dev3, EV_REL, REL_Y);
1431 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1432 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1433 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1434
1435 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1436
1437 error = input_register_device(dev3);
1438 if (error) {
1439 psmouse_err(psmouse,
1440 "failed to register secondary device: %d\n",
1441 error);
1442 input_free_device(dev3);
1443 goto out;
1444 }
1445
1446 priv->dev3 = dev3;
1447
1448out:
1449 /*
1450 * Save the error code so that we can detect that we
1451 * already tried to create the device.
1452 */
1453 if (error)
1454 priv->dev3 = ERR_PTR(error);
1455
1456 mutex_unlock(&alps_mutex);
1457}
1458
Hans de Goede59c30af2015-04-03 17:14:40 -07001459static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001460 unsigned char packet[],
1461 bool report_buttons)
1462{
Hans de Goede59c30af2015-04-03 17:14:40 -07001463 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001464 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001465
Hans de Goedee3a79212015-04-03 17:20:05 -07001466 /* Figure out which device to use to report the bare packet */
1467 if (priv->proto_version == ALPS_PROTO_V2 &&
1468 (priv->flags & ALPS_DUALPOINT)) {
1469 /* On V2 devices the DualPoint Stick reports bare packets */
1470 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001471 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001472 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001473 /* Register dev3 mouse if we received PS/2 packet first time */
1474 if (!IS_ERR(priv->dev3))
1475 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1476 0);
1477 return;
1478 } else {
1479 dev = priv->dev3;
1480 }
1481
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001482 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001483 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001484 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1485
Pali Rohár04aae282015-01-14 13:18:30 -08001486 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001487 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001488 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001489 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1490
Pali Rohár04aae282015-01-14 13:18:30 -08001491 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001492}
1493
1494static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 struct alps_data *priv = psmouse->private;
1497
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001498 if (psmouse->pktcnt < 6)
1499 return PSMOUSE_GOOD_DATA;
1500
1501 if (psmouse->pktcnt == 6) {
1502 /*
1503 * Start a timer to flush the packet if it ends up last
1504 * 6-byte packet in the stream. Timer needs to fire
1505 * psmouse core times out itself. 20 ms should be enough
1506 * to decide if we are getting more data or not.
1507 */
1508 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1509 return PSMOUSE_GOOD_DATA;
1510 }
1511
1512 del_timer(&priv->timer);
1513
1514 if (psmouse->packet[6] & 0x80) {
1515
1516 /*
1517 * Highest bit is set - that means we either had
1518 * complete ALPS packet and this is start of the
1519 * next packet or we got garbage.
1520 */
1521
1522 if (((psmouse->packet[3] |
1523 psmouse->packet[4] |
1524 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001525 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001526 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001527 "refusing packet %4ph (suspected interleaved ps/2)\n",
1528 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001529 return PSMOUSE_BAD_DATA;
1530 }
1531
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001532 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001533
1534 /* Continue with the next packet */
1535 psmouse->packet[0] = psmouse->packet[6];
1536 psmouse->pktcnt = 1;
1537
1538 } else {
1539
1540 /*
1541 * High bit is 0 - that means that we indeed got a PS/2
1542 * packet in the middle of ALPS packet.
1543 *
1544 * There is also possibility that we got 6-byte ALPS
1545 * packet followed by 3-byte packet from trackpoint. We
1546 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001547 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001548 * normal operation (user would need to press all
1549 * buttons on the pad and start moving trackpoint
1550 * without touching the pad surface) we assume former.
1551 * Even if we are wrong the wost thing that would happen
1552 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001553 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001554 */
1555
Hans de Goede59c30af2015-04-03 17:14:40 -07001556 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1557 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001558
1559 /*
1560 * Continue with the standard ALPS protocol handling,
1561 * but make sure we won't process it as an interleaved
1562 * packet again, which may happen if all buttons are
1563 * pressed. To avoid this let's reset the 4th bit which
1564 * is normally 1.
1565 */
1566 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1567 psmouse->pktcnt = 4;
1568 }
1569
1570 return PSMOUSE_GOOD_DATA;
1571}
1572
1573static void alps_flush_packet(unsigned long data)
1574{
1575 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001576 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001577
1578 serio_pause_rx(psmouse->ps2dev.serio);
1579
Seth Forsheeb46615f2011-11-07 19:53:30 -08001580 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001581
1582 /*
1583 * We did not any more data in reasonable amount of time.
1584 * Validate the last 3 bytes and process as a standard
1585 * ALPS packet.
1586 */
1587 if ((psmouse->packet[3] |
1588 psmouse->packet[4] |
1589 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001590 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001591 "refusing packet %3ph (suspected interleaved ps/2)\n",
1592 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001593 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001594 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001595 }
1596 psmouse->pktcnt = 0;
1597 }
1598
1599 serio_continue_rx(psmouse->ps2dev.serio);
1600}
1601
1602static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1603{
1604 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001605
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001606 /*
1607 * Check if we are dealing with a bare PS/2 packet, presumably from
1608 * a device connected to the external PS/2 port. Because bare PS/2
1609 * protocol does not have enough constant bits to self-synchronize
1610 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001611 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001612 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001613 if (priv->proto_version != ALPS_PROTO_V8 &&
1614 !psmouse->out_of_sync_cnt &&
1615 (psmouse->packet[0] & 0xc8) == 0x08) {
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001618 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1619 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return PSMOUSE_FULL_PACKET;
1621 }
1622 return PSMOUSE_GOOD_DATA;
1623 }
1624
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001625 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1626
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001627 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001628 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1629 return alps_handle_interleaved_ps2(psmouse);
1630 }
1631
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001632 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001633 psmouse_dbg(psmouse,
1634 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001635 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Seth Forsheeb46615f2011-11-07 19:53:30 -08001639 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001640 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001641 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001642 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001643 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1644 psmouse->pktcnt - 1,
1645 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001646
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001647 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001648 psmouse->pktcnt == psmouse->pktsize) {
1649 /*
1650 * Some Dell boxes, such as Latitude E6440 or E7440
1651 * with closed lid, quite often smash last byte of
1652 * otherwise valid packet with 0xff. Given that the
1653 * next packet is very likely to be valid let's
1654 * report PSMOUSE_FULL_PACKET but not process data,
1655 * rather than reporting PSMOUSE_BAD_DATA and
1656 * filling the logs.
1657 */
1658 return PSMOUSE_FULL_PACKET;
1659 }
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001664 if ((priv->proto_version == ALPS_PROTO_V7 &&
1665 !alps_is_valid_package_v7(psmouse)) ||
1666 (priv->proto_version == ALPS_PROTO_V8 &&
1667 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001668 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1669 psmouse->pktcnt - 1,
1670 psmouse->packet[psmouse->pktcnt - 1]);
1671 return PSMOUSE_BAD_DATA;
1672 }
1673
Seth Forsheeb46615f2011-11-07 19:53:30 -08001674 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001675 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return PSMOUSE_FULL_PACKET;
1677 }
1678
1679 return PSMOUSE_GOOD_DATA;
1680}
1681
Seth Forshee25bded72011-11-07 19:53:36 -08001682static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1683{
1684 struct ps2dev *ps2dev = &psmouse->ps2dev;
1685 struct alps_data *priv = psmouse->private;
1686 int command;
1687 unsigned char *param;
1688 unsigned char dummy[4];
1689
1690 BUG_ON(nibble > 0xf);
1691
1692 command = priv->nibble_commands[nibble].command;
1693 param = (command & 0x0f00) ?
1694 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1695
1696 if (ps2_command(ps2dev, param, command))
1697 return -1;
1698
1699 return 0;
1700}
1701
1702static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1703{
1704 struct ps2dev *ps2dev = &psmouse->ps2dev;
1705 struct alps_data *priv = psmouse->private;
1706 int i, nibble;
1707
1708 if (ps2_command(ps2dev, NULL, priv->addr_command))
1709 return -1;
1710
1711 for (i = 12; i >= 0; i -= 4) {
1712 nibble = (addr >> i) & 0xf;
1713 if (alps_command_mode_send_nibble(psmouse, nibble))
1714 return -1;
1715 }
1716
1717 return 0;
1718}
1719
1720static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1721{
1722 struct ps2dev *ps2dev = &psmouse->ps2dev;
1723 unsigned char param[4];
1724
1725 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1726 return -1;
1727
1728 /*
1729 * The address being read is returned in the first two bytes
1730 * of the result. Check that this address matches the expected
1731 * address.
1732 */
1733 if (addr != ((param[0] << 8) | param[1]))
1734 return -1;
1735
1736 return param[2];
1737}
1738
1739static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1740{
1741 if (alps_command_mode_set_addr(psmouse, addr))
1742 return -1;
1743 return __alps_command_mode_read_reg(psmouse, addr);
1744}
1745
1746static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1747{
1748 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1749 return -1;
1750 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1751 return -1;
1752 return 0;
1753}
1754
1755static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1756 u8 value)
1757{
1758 if (alps_command_mode_set_addr(psmouse, addr))
1759 return -1;
1760 return __alps_command_mode_write_reg(psmouse, value);
1761}
1762
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001763static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1764 int repeated_command, unsigned char *param)
1765{
1766 struct ps2dev *ps2dev = &psmouse->ps2dev;
1767
1768 param[0] = 0;
1769 if (init_command && ps2_command(ps2dev, param, init_command))
1770 return -EIO;
1771
1772 if (ps2_command(ps2dev, NULL, repeated_command) ||
1773 ps2_command(ps2dev, NULL, repeated_command) ||
1774 ps2_command(ps2dev, NULL, repeated_command))
1775 return -EIO;
1776
1777 param[0] = param[1] = param[2] = 0xff;
1778 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1779 return -EIO;
1780
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001781 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1782 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001783 return 0;
1784}
1785
Yunkang Tang38088432014-07-26 13:51:41 -07001786static bool alps_check_valid_firmware_id(unsigned char id[])
1787{
1788 if (id[0] == 0x73)
1789 return true;
1790
1791 if (id[0] == 0x88 &&
1792 (id[1] == 0x07 ||
1793 id[1] == 0x08 ||
1794 (id[1] & 0xf0) == 0xb0 ||
1795 (id[1] & 0xf0) == 0xc0)) {
1796 return true;
1797 }
1798
1799 return false;
1800}
1801
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001802static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001803{
1804 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001805
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001806 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001807 psmouse_err(psmouse, "failed to enter command mode\n");
1808 return -1;
1809 }
1810
Yunkang Tang38088432014-07-26 13:51:41 -07001811 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001812 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001813 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001814 return -1;
1815 }
Seth Forshee25bded72011-11-07 19:53:36 -08001816 return 0;
1817}
1818
1819static inline int alps_exit_command_mode(struct psmouse *psmouse)
1820{
1821 struct ps2dev *ps2dev = &psmouse->ps2dev;
1822 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1823 return -1;
1824 return 0;
1825}
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/*
1828 * For DualPoint devices select the device that should respond to
1829 * subsequent commands. It looks like glidepad is behind stickpointer,
1830 * I'd thought it would be other way around...
1831 */
Seth Forshee25bded72011-11-07 19:53:36 -08001832static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1836
1837 if (ps2_command(ps2dev, NULL, cmd) ||
1838 ps2_command(ps2dev, NULL, cmd) ||
1839 ps2_command(ps2dev, NULL, cmd) ||
1840 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1841 return -1;
1842
1843 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001844 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 return 0;
1847}
1848
Seth Forshee25bded72011-11-07 19:53:36 -08001849static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 struct ps2dev *ps2dev = &psmouse->ps2dev;
1852
1853 /* Try ALPS magic knock - 4 disable before enable */
1854 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1855 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1856 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1857 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1858 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1859 return -1;
1860
1861 /*
1862 * Switch mouse to poll (remote) mode so motion data will not
1863 * get in our way
1864 */
Guenter Roeckad568142017-01-21 23:44:46 -08001865 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Yunkang Tang95f75e92013-12-01 22:33:52 -08001868static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1869{
1870 int i, nibble;
1871
1872 /*
1873 * b0-b11 are valid bits, send sequence is inverse.
1874 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1875 */
1876 for (i = 0; i <= 8; i += 4) {
1877 nibble = (word >> i) & 0xf;
1878 if (alps_command_mode_send_nibble(psmouse, nibble))
1879 return -1;
1880 }
1881
1882 return 0;
1883}
1884
1885static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1886 u16 addr, u16 value)
1887{
1888 struct ps2dev *ps2dev = &psmouse->ps2dev;
1889
1890 /* 0x0A0 is the command to write the word */
1891 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1892 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1893 alps_monitor_mode_send_word(psmouse, addr) ||
1894 alps_monitor_mode_send_word(psmouse, value) ||
1895 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1896 return -1;
1897
1898 return 0;
1899}
1900
1901static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1902{
1903 struct ps2dev *ps2dev = &psmouse->ps2dev;
1904
1905 if (enable) {
1906 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1907 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1908 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1909 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1910 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1911 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1912 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1913 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1914 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1915 return -1;
1916 } else {
1917 /* EC to exit monitor mode */
1918 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1919 return -1;
1920 }
1921
1922 return 0;
1923}
1924
1925static int alps_absolute_mode_v6(struct psmouse *psmouse)
1926{
1927 u16 reg_val = 0x181;
1928 int ret = -1;
1929
1930 /* enter monitor mode, to write the register */
1931 if (alps_monitor_mode(psmouse, true))
1932 return -1;
1933
1934 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1935
1936 if (alps_monitor_mode(psmouse, false))
1937 ret = -1;
1938
1939 return ret;
1940}
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942static int alps_get_status(struct psmouse *psmouse, char *param)
1943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001945 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return -1;
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return 0;
1949}
1950
1951/*
1952 * Turn touchpad tapping on or off. The sequences are:
1953 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1954 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1955 * My guess that 0xE9 (GetInfo) is here as a sync point.
1956 * For models that also have stickpointer (DualPoints) its tapping
1957 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1958 * we don't fiddle with it.
1959 */
1960static int alps_tap_mode(struct psmouse *psmouse, int enable)
1961{
1962 struct ps2dev *ps2dev = &psmouse->ps2dev;
1963 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1964 unsigned char tap_arg = enable ? 0x0A : 0x00;
1965 unsigned char param[4];
1966
1967 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1968 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1969 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1970 ps2_command(ps2dev, &tap_arg, cmd))
1971 return -1;
1972
1973 if (alps_get_status(psmouse, param))
1974 return -1;
1975
1976 return 0;
1977}
1978
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001979/*
1980 * alps_poll() - poll the touchpad for current motion packet.
1981 * Used in resync.
1982 */
1983static int alps_poll(struct psmouse *psmouse)
1984{
1985 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001986 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001987 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001988
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001989 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001990 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001991
1992 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1993 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1994
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001995 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001996 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001997
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001998 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001999 return -1;
2000
2001 if ((psmouse->badbyte & 0xc8) == 0x08) {
2002/*
2003 * Poll the track stick ...
2004 */
2005 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2006 return -1;
2007 }
2008
2009 memcpy(psmouse->packet, buf, sizeof(buf));
2010 return 0;
2011}
2012
Seth Forshee25bded72011-11-07 19:53:36 -08002013static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002017 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002018 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002022 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002023 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05002025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Seth Forshee25bded72011-11-07 19:53:36 -08002027 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002028 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return -1;
2030 }
2031
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002032 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002033 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002037 /* ALPS needs stream mode, otherwise it won't report any data */
2038 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002039 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002040 return -1;
2041 }
2042
2043 return 0;
2044}
2045
Yunkang Tang95f75e92013-12-01 22:33:52 -08002046static int alps_hw_init_v6(struct psmouse *psmouse)
2047{
2048 unsigned char param[2] = {0xC8, 0x14};
2049
2050 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2051 if (alps_passthrough_mode_v2(psmouse, true))
2052 return -1;
2053
2054 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2055 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2056 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2057 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2058 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2059 return -1;
2060
2061 if (alps_passthrough_mode_v2(psmouse, false))
2062 return -1;
2063
2064 if (alps_absolute_mode_v6(psmouse)) {
2065 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2066 return -1;
2067 }
2068
2069 return 0;
2070}
2071
Seth Forshee25bded72011-11-07 19:53:36 -08002072/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08002073 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08002074 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08002075static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2076 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08002077{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002078 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08002079
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002080 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002081 return -1;
2082
Kevin Cernekeecd401202013-02-13 22:28:07 -08002083 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2084 if (reg_val == -1)
2085 goto error;
2086
Seth Forshee25bded72011-11-07 19:53:36 -08002087 if (enable)
2088 reg_val |= 0x01;
2089 else
2090 reg_val &= ~0x01;
2091
Kevin Cernekeecd401202013-02-13 22:28:07 -08002092 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08002093
Kevin Cernekeecd401202013-02-13 22:28:07 -08002094error:
2095 if (alps_exit_command_mode(psmouse))
2096 ret = -1;
2097 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08002098}
2099
2100/* Must be in command mode when calling this function */
2101static int alps_absolute_mode_v3(struct psmouse *psmouse)
2102{
2103 int reg_val;
2104
2105 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2106 if (reg_val == -1)
2107 return -1;
2108
2109 reg_val |= 0x06;
2110 if (__alps_command_mode_write_reg(psmouse, reg_val))
2111 return -1;
2112
2113 return 0;
2114}
2115
Pali Rohárdae928e2016-01-06 15:50:08 -08002116static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
Kevin Cernekeecd401202013-02-13 22:28:07 -08002117{
2118 int ret = -EIO, reg_val;
2119
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002120 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002121 goto error;
2122
2123 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2124 if (reg_val == -1)
2125 goto error;
2126
2127 /* bit 7: trackstick is present */
2128 ret = reg_val & 0x80 ? 0 : -ENODEV;
2129
2130error:
2131 alps_exit_command_mode(psmouse);
2132 return ret;
2133}
2134
2135static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2136{
2137 struct ps2dev *ps2dev = &psmouse->ps2dev;
2138 int ret = 0;
2139 unsigned char param[4];
2140
2141 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2142 return -EIO;
2143
2144 /*
2145 * E7 report for the trackstick
2146 *
2147 * There have been reports of failures to seem to trace back
2148 * to the above trackstick check failing. When these occur
2149 * this E7 report fails, so when that happens we continue
2150 * with the assumption that there isn't a trackstick after
2151 * all.
2152 */
2153 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002154 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002155 ret = -ENODEV;
2156 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002157 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002158
2159 /*
2160 * Not sure what this does, but it is absolutely
2161 * essential. Without it, the touchpad does not
2162 * work at all and the trackstick just emits normal
2163 * PS/2 packets.
2164 */
2165 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2166 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2167 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2168 alps_command_mode_send_nibble(psmouse, 0x9) ||
2169 alps_command_mode_send_nibble(psmouse, 0x4)) {
2170 psmouse_err(psmouse,
2171 "Error sending magic E6 sequence\n");
2172 ret = -EIO;
2173 goto error;
2174 }
2175
2176 /*
2177 * This ensures the trackstick packets are in the format
2178 * supported by this driver. If bit 1 isn't set the packet
2179 * format is different.
2180 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002181 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002182 alps_command_mode_write_reg(psmouse,
2183 reg_base + 0x08, 0x82) ||
2184 alps_exit_command_mode(psmouse))
2185 ret = -EIO;
2186 }
2187
2188error:
2189 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2190 ret = -EIO;
2191
2192 return ret;
2193}
2194
Seth Forshee25bded72011-11-07 19:53:36 -08002195static int alps_hw_init_v3(struct psmouse *psmouse)
2196{
Pali Rohár4b1af852016-01-06 15:58:56 -08002197 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -08002198 struct ps2dev *ps2dev = &psmouse->ps2dev;
2199 int reg_val;
2200 unsigned char param[4];
2201
Pali Rohár4b1af852016-01-06 15:58:56 -08002202 if ((priv->flags & ALPS_DUALPOINT) &&
Kevin Cernekeecd401202013-02-13 22:28:07 -08002203 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002204 goto error;
2205
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002206 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002207 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002208 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2209 goto error;
2210 }
2211
2212 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2213 if (reg_val == -1)
2214 goto error;
2215 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2216 goto error;
2217
2218 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2219 if (reg_val == -1)
2220 goto error;
2221 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2222 goto error;
2223
2224 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2225 goto error;
2226 if (__alps_command_mode_write_reg(psmouse, 0x04))
2227 goto error;
2228
2229 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2230 goto error;
2231 if (__alps_command_mode_write_reg(psmouse, 0x03))
2232 goto error;
2233
2234 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2235 goto error;
2236 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2237 goto error;
2238
2239 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2240 goto error;
2241 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2242 goto error;
2243
Seth Forshee25bded72011-11-07 19:53:36 -08002244 alps_exit_command_mode(psmouse);
2245
2246 /* Set rate and enable data reporting */
2247 param[0] = 0x64;
2248 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2249 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2250 psmouse_err(psmouse, "Failed to enable data reporting\n");
2251 return -1;
2252 }
2253
2254 return 0;
2255
Seth Forshee25bded72011-11-07 19:53:36 -08002256error:
2257 /*
2258 * Leaving the touchpad in command mode will essentially render
2259 * it unusable until the machine reboots, so exit it here just
2260 * to be safe
2261 */
2262 alps_exit_command_mode(psmouse);
2263 return -1;
2264}
2265
Hans de Goedef3f33c62014-07-29 11:22:07 -07002266static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2267{
2268 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2269 struct alps_data *priv = psmouse->private;
2270
2271 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2272 if (reg < 0)
2273 return reg;
2274
2275 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2276 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2277
2278 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2279 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2280
2281 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2282 if (reg < 0)
2283 return reg;
2284
2285 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2286 x_electrode = 17 + x_electrode;
2287
2288 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2289 y_electrode = 13 + y_electrode;
2290
2291 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2292 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2293
2294 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2295 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2296
2297 psmouse_dbg(psmouse,
2298 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2299 x_pitch, y_pitch, x_electrode, y_electrode,
2300 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2301
2302 return 0;
2303}
2304
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002305static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2306{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002307 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002308 struct ps2dev *ps2dev = &psmouse->ps2dev;
2309 int reg_val, ret = -1;
2310
Kevin Cernekeecd401202013-02-13 22:28:07 -08002311 if (priv->flags & ALPS_DUALPOINT) {
2312 reg_val = alps_setup_trackstick_v3(psmouse,
2313 ALPS_REG_BASE_RUSHMORE);
2314 if (reg_val == -EIO)
2315 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002316 }
2317
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002318 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002319 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2320 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2321 goto error;
2322
Hans de Goedef3f33c62014-07-29 11:22:07 -07002323 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2324 goto error;
2325
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002326 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2327 if (reg_val == -1)
2328 goto error;
2329 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2330 goto error;
2331
2332 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2333 goto error;
2334
2335 /* enter absolute mode */
2336 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2337 if (reg_val == -1)
2338 goto error;
2339 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2340 goto error;
2341
2342 alps_exit_command_mode(psmouse);
2343 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2344
2345error:
2346 alps_exit_command_mode(psmouse);
2347 return ret;
2348}
2349
Seth Forshee25bded72011-11-07 19:53:36 -08002350/* Must be in command mode when calling this function */
2351static int alps_absolute_mode_v4(struct psmouse *psmouse)
2352{
2353 int reg_val;
2354
2355 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2356 if (reg_val == -1)
2357 return -1;
2358
2359 reg_val |= 0x02;
2360 if (__alps_command_mode_write_reg(psmouse, reg_val))
2361 return -1;
2362
2363 return 0;
2364}
2365
2366static int alps_hw_init_v4(struct psmouse *psmouse)
2367{
Seth Forshee25bded72011-11-07 19:53:36 -08002368 struct ps2dev *ps2dev = &psmouse->ps2dev;
2369 unsigned char param[4];
2370
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002371 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002372 goto error;
2373
2374 if (alps_absolute_mode_v4(psmouse)) {
2375 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2376 goto error;
2377 }
2378
2379 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2380 goto error;
2381
2382 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2383 goto error;
2384
2385 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2386 goto error;
2387
2388 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2389 goto error;
2390
2391 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2392 goto error;
2393
2394 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2395 goto error;
2396
2397 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2398 goto error;
2399
2400 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2401 goto error;
2402
2403 alps_exit_command_mode(psmouse);
2404
2405 /*
2406 * This sequence changes the output from a 9-byte to an
2407 * 8-byte format. All the same data seems to be present,
2408 * just in a more compact format.
2409 */
2410 param[0] = 0xc8;
2411 param[1] = 0x64;
2412 param[2] = 0x50;
2413 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2414 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2415 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2416 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2417 return -1;
2418
2419 /* Set rate and enable data reporting */
2420 param[0] = 0x64;
2421 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2422 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2423 psmouse_err(psmouse, "Failed to enable data reporting\n");
2424 return -1;
2425 }
2426
2427 return 0;
2428
2429error:
2430 /*
2431 * Leaving the touchpad in command mode will essentially render
2432 * it unusable until the machine reboots, so exit it here just
2433 * to be safe
2434 */
2435 alps_exit_command_mode(psmouse);
2436 return -1;
2437}
2438
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002439static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2440 unsigned char index, unsigned char otp[])
2441{
2442 struct ps2dev *ps2dev = &psmouse->ps2dev;
2443
2444 switch (index) {
2445 case 0:
2446 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2447 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2448 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2449 return -1;
2450
2451 break;
2452
2453 case 1:
2454 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2455 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2456 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2457 return -1;
2458
2459 break;
2460 }
2461
2462 return 0;
2463}
2464
Fengguang Wu07f19e32015-04-10 23:54:31 -07002465static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2466 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002467{
2468 int num_x_electrode;
2469 int num_y_electrode;
2470 int x_pitch, y_pitch, x_phys, y_phys;
2471
2472 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2473 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2474
2475 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2476 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2477
2478 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2479 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2480
2481 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2482 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2483
2484 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2485 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2486
2487 return 0;
2488}
2489
Fengguang Wu07f19e32015-04-10 23:54:31 -07002490static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2491 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002492{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002493 unsigned char is_btnless;
2494
2495 is_btnless = (otp[1][1] >> 3) & 0x01;
2496
2497 if (is_btnless)
2498 priv->flags |= ALPS_BUTTONPAD;
2499
2500 return 0;
2501}
2502
2503static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2504 struct alps_data *priv)
2505{
2506 unsigned char otp[2][4];
2507
2508 memset(otp, 0, sizeof(otp));
2509
2510 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2511 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2512 return -1;
2513
2514 alps_update_device_area_ss4_v2(otp, priv);
2515
2516 alps_update_btn_info_ss4_v2(otp, priv);
2517
2518 return 0;
2519}
2520
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002521static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2522 struct alps_data *priv)
2523{
2524 struct ps2dev *ps2dev = &psmouse->ps2dev;
2525 unsigned char param[4] = {0};
2526 int num_x_electrode, num_y_electrode;
2527
2528 if (alps_enter_command_mode(psmouse))
2529 return -1;
2530
2531 param[0] = 0x0a;
2532 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2533 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2534 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2535 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2536 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2537 return -1;
2538
2539 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2540 return -1;
2541
2542 /*
2543 * Dolphin's sensor line number is not fixed. It can be calculated
2544 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2545 * Further more, we can get device's x_max and y_max by multiplying
2546 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2547 *
2548 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2549 * real sensor line number X = 11 + 8 = 19, and
2550 * real sensor line number Y = 8 + 1 = 9.
2551 * So, x_max = (19 - 1) * 64 = 1152, and
2552 * y_max = (9 - 1) * 64 = 512.
2553 */
2554 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2555 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2556 priv->x_bits = num_x_electrode;
2557 priv->y_bits = num_y_electrode;
2558 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2559 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2560
2561 if (alps_exit_command_mode(psmouse))
2562 return -1;
2563
2564 return 0;
2565}
2566
Dave Turvene75af9e52013-02-21 22:58:28 -08002567static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2568{
2569 struct ps2dev *ps2dev = &psmouse->ps2dev;
2570 unsigned char param[2];
2571
2572 /* This is dolphin "v1" as empirically defined by florin9doi */
2573 param[0] = 0x64;
2574 param[1] = 0x28;
2575
2576 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2577 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2578 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2579 return -1;
2580
2581 return 0;
2582}
2583
Yunkang Tang38088432014-07-26 13:51:41 -07002584static int alps_hw_init_v7(struct psmouse *psmouse)
2585{
2586 struct ps2dev *ps2dev = &psmouse->ps2dev;
2587 int reg_val, ret = -1;
2588
2589 if (alps_enter_command_mode(psmouse) ||
2590 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2591 goto error;
2592
Hans de Goedef3f33c62014-07-29 11:22:07 -07002593 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2594 goto error;
2595
Yunkang Tang38088432014-07-26 13:51:41 -07002596 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2597 goto error;
2598
2599 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2600 if (reg_val == -1)
2601 goto error;
2602 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2603 goto error;
2604
2605 alps_exit_command_mode(psmouse);
2606 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2607
2608error:
2609 alps_exit_command_mode(psmouse);
2610 return ret;
2611}
2612
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002613static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2614{
2615 struct ps2dev *ps2dev = &psmouse->ps2dev;
2616 char param[2] = {0x64, 0x28};
2617 int ret = -1;
2618
2619 /* enter absolute mode */
2620 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2621 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2622 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2623 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2624 goto error;
2625 }
2626
2627 /* T.B.D. Decread noise packet number, delete in the future */
2628 if (alps_exit_command_mode(psmouse) ||
2629 alps_enter_command_mode(psmouse) ||
2630 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2631 goto error;
2632 }
2633 alps_exit_command_mode(psmouse);
2634
2635 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2636
2637error:
2638 alps_exit_command_mode(psmouse);
2639 return ret;
2640}
2641
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002642static int alps_set_protocol(struct psmouse *psmouse,
2643 struct alps_data *priv,
2644 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002645{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002646 psmouse->private = priv;
2647
2648 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2649
2650 priv->proto_version = protocol->version;
2651 priv->byte0 = protocol->byte0;
2652 priv->mask0 = protocol->mask0;
2653 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002654
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002655 priv->x_max = 2000;
2656 priv->y_max = 1400;
2657 priv->x_bits = 15;
2658 priv->y_bits = 11;
2659
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002660 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002661 case ALPS_PROTO_V1:
2662 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002663 priv->hw_init = alps_hw_init_v1_v2;
2664 priv->process_packet = alps_process_packet_v1_v2;
2665 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002666 priv->x_max = 1023;
2667 priv->y_max = 767;
Hans de Goede19556212015-10-26 01:50:28 -07002668 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2669 priv->flags |= ALPS_STICK_BITS;
Seth Forshee25bded72011-11-07 19:53:36 -08002670 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002671
Seth Forshee25bded72011-11-07 19:53:36 -08002672 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002673 priv->hw_init = alps_hw_init_v3;
2674 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002675 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002676 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002677 priv->nibble_commands = alps_v3_nibble_commands;
2678 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Pali Rohár4b1af852016-01-06 15:58:56 -08002679
2680 if (alps_probe_trackstick_v3_v7(psmouse,
2681 ALPS_REG_BASE_PINNACLE) < 0)
2682 priv->flags &= ~ALPS_DUALPOINT;
2683
Seth Forshee25bded72011-11-07 19:53:36 -08002684 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002685
2686 case ALPS_PROTO_V3_RUSHMORE:
2687 priv->hw_init = alps_hw_init_rushmore_v3;
2688 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002689 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002690 priv->decode_fields = alps_decode_rushmore;
2691 priv->nibble_commands = alps_v3_nibble_commands;
2692 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2693 priv->x_bits = 16;
2694 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002695
Pali Rohárdae928e2016-01-06 15:50:08 -08002696 if (alps_probe_trackstick_v3_v7(psmouse,
2697 ALPS_REG_BASE_RUSHMORE) < 0)
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002698 priv->flags &= ~ALPS_DUALPOINT;
2699
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002700 break;
2701
Seth Forshee25bded72011-11-07 19:53:36 -08002702 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002703 priv->hw_init = alps_hw_init_v4;
2704 priv->process_packet = alps_process_packet_v4;
Hans de Goede688ea362015-05-20 14:42:50 -07002705 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002706 priv->nibble_commands = alps_v4_nibble_commands;
2707 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002708 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002709
Dave Turvene75af9e52013-02-21 22:58:28 -08002710 case ALPS_PROTO_V5:
2711 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002712 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002713 priv->decode_fields = alps_decode_dolphin;
Hans de Goede688ea362015-05-20 14:42:50 -07002714 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dave Turvene75af9e52013-02-21 22:58:28 -08002715 priv->nibble_commands = alps_v3_nibble_commands;
2716 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002717 priv->x_bits = 23;
2718 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002719
2720 if (alps_dolphin_get_device_area(psmouse, priv))
2721 return -EIO;
2722
Dave Turvene75af9e52013-02-21 22:58:28 -08002723 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002724
Yunkang Tang95f75e92013-12-01 22:33:52 -08002725 case ALPS_PROTO_V6:
2726 priv->hw_init = alps_hw_init_v6;
2727 priv->process_packet = alps_process_packet_v6;
2728 priv->set_abs_params = alps_set_abs_params_st;
2729 priv->nibble_commands = alps_v6_nibble_commands;
2730 priv->x_max = 2047;
2731 priv->y_max = 1535;
2732 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002733
Yunkang Tang38088432014-07-26 13:51:41 -07002734 case ALPS_PROTO_V7:
2735 priv->hw_init = alps_hw_init_v7;
2736 priv->process_packet = alps_process_packet_v7;
2737 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002738 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002739 priv->nibble_commands = alps_v3_nibble_commands;
2740 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002741 priv->x_max = 0xfff;
2742 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002743
2744 if (priv->fw_ver[1] != 0xba)
2745 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002746
Pali Rohárdae928e2016-01-06 15:50:08 -08002747 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2748 priv->flags &= ~ALPS_DUALPOINT;
2749
Yunkang Tang38088432014-07-26 13:51:41 -07002750 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002751
2752 case ALPS_PROTO_V8:
2753 priv->hw_init = alps_hw_init_ss4_v2;
2754 priv->process_packet = alps_process_packet_ss4_v2;
2755 priv->decode_fields = alps_decode_ss4_v2;
2756 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2757 priv->nibble_commands = alps_v3_nibble_commands;
2758 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2759
2760 if (alps_set_defaults_ss4_v2(psmouse, priv))
2761 return -EIO;
2762
Ben Gamariaeaa8812016-10-04 11:43:30 -07002763 if (priv->fw_ver[1] == 0x1)
2764 priv->flags |= ALPS_DUALPOINT |
2765 ALPS_DUALPOINT_WITH_PRESSURE;
2766
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002767 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002768 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002769
2770 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002771}
2772
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002773static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2774 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002775{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002776 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002777 int i;
2778
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002779 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2780 model = &alps_model_data[i];
2781
2782 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2783 (!model->command_mode_resp ||
2784 model->command_mode_resp == ec[2])) {
2785
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002786 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002787 }
2788 }
2789
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002790 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002791}
2792
2793static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2794{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002795 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002796 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002797 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002798
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002799 /*
2800 * First try "E6 report".
2801 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2802 * The bits 0-2 of the first byte will be 1s if some buttons are
2803 * pressed.
2804 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002805 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2806 PSMOUSE_CMD_SETSCALE11, e6))
2807 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002808
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002809 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2810 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002811
2812 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002813 * Now get the "E7" and "EC" reports. These will uniquely identify
2814 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002815 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002816 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2817 PSMOUSE_CMD_SETSCALE21, e7) ||
2818 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2819 PSMOUSE_CMD_RESET_WRAP, ec) ||
2820 alps_exit_command_mode(psmouse))
2821 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002822
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002823 protocol = alps_match_table(e7, ec);
2824 if (!protocol) {
Pali Rohár09c398b2017-03-07 09:41:34 -08002825 if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
2826 ec[2] == 0x8a) {
2827 protocol = &alps_v4_protocol_data;
2828 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002829 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2830 protocol = &alps_v5_protocol_data;
2831 } else if (ec[0] == 0x88 &&
2832 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2833 protocol = &alps_v7_protocol_data;
2834 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2835 protocol = &alps_v3_rushmore_data;
2836 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2837 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2838 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002839 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2840 e7[2] == 0x14 && ec[1] == 0x02) {
2841 protocol = &alps_v8_protocol_data;
Ben Gamariaeaa8812016-10-04 11:43:30 -07002842 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2843 e7[2] == 0x28 && ec[1] == 0x01) {
2844 protocol = &alps_v8_protocol_data;
Pali Rohárc9815232017-03-07 09:42:46 -08002845 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2846 protocol = &alps_v9_protocol_data;
2847 psmouse_warn(psmouse,
2848 "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2849 e7, ec);
2850 return -EINVAL;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002851 } else {
2852 psmouse_dbg(psmouse,
2853 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2854 return -EINVAL;
2855 }
2856 }
2857
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002858 if (priv) {
2859 /* Save the Firmware version */
2860 memcpy(priv->fw_ver, ec, 3);
2861 error = alps_set_protocol(psmouse, priv, protocol);
2862 if (error)
2863 return error;
2864 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002865
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002866 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002867}
2868
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002869static int alps_reconnect(struct psmouse *psmouse)
2870{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002871 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002872
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002873 psmouse_reset(psmouse);
2874
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002875 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002876 return -1;
2877
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002878 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
2881static void alps_disconnect(struct psmouse *psmouse)
2882{
2883 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002886 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002887 if (priv->dev2)
2888 input_unregister_device(priv->dev2);
2889 if (!IS_ERR_OR_NULL(priv->dev3))
2890 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 kfree(priv);
2892}
2893
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002894static void alps_set_abs_params_st(struct alps_data *priv,
2895 struct input_dev *dev1)
2896{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002897 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2898 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002899 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002900}
2901
Masaki Ota8eccd392015-03-27 20:44:21 -07002902static void alps_set_abs_params_mt_common(struct alps_data *priv,
2903 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002904{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002905 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2906 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002907
Hans de Goedef3f33c62014-07-29 11:22:07 -07002908 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2909 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2910
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002911 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2912 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002913}
Yunkang Tang38088432014-07-26 13:51:41 -07002914
Hans de Goede688ea362015-05-20 14:42:50 -07002915static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2916 struct input_dev *dev1)
Masaki Ota8eccd392015-03-27 20:44:21 -07002917{
2918 alps_set_abs_params_mt_common(priv, dev1);
2919 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2920
2921 input_mt_init_slots(dev1, MAX_TOUCHES,
2922 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
Hans de Goede1662c0332015-05-20 14:42:15 -07002923 INPUT_MT_SEMI_MT);
Masaki Ota8eccd392015-03-27 20:44:21 -07002924}
2925
2926static void alps_set_abs_params_v7(struct alps_data *priv,
2927 struct input_dev *dev1)
2928{
2929 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002930 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002931
2932 input_mt_init_slots(dev1, MAX_TOUCHES,
2933 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2934 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002935
2936 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2937}
2938
2939static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2940 struct input_dev *dev1)
2941{
2942 alps_set_abs_params_mt_common(priv, dev1);
2943 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2944 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2945
2946 input_mt_init_slots(dev1, MAX_TOUCHES,
2947 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2948 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002949}
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951int alps_init(struct psmouse *psmouse)
2952{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002953 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002954 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002955 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002957 error = priv->hw_init(psmouse);
2958 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 goto init_fail;
2960
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002961 /*
2962 * Undo part of setup done for us by psmouse core since touchpad
2963 * is not a relative device.
2964 */
2965 __clear_bit(EV_REL, dev1->evbit);
2966 __clear_bit(REL_X, dev1->relbit);
2967 __clear_bit(REL_Y, dev1->relbit);
2968
2969 /*
2970 * Now set up our capabilities.
2971 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002972 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2973 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2974 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002975 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2976 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002978 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002979
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002980 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002982 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002983 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2984 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
2986
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002987 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002988 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2989 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
2991
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002992 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002993 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2994 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2995 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2996 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002997 } else if (priv->flags & ALPS_BUTTONPAD) {
2998 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2999 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08003000 } else {
3001 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
3002 }
3003
Pali Rohárdfba8602015-01-09 12:48:44 -08003004 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08003005 struct input_dev *dev2;
3006
3007 dev2 = input_allocate_device();
3008 if (!dev2) {
3009 psmouse_err(psmouse,
3010 "failed to allocate trackstick device\n");
3011 error = -ENOMEM;
3012 goto init_fail;
3013 }
3014
3015 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3016 psmouse->ps2dev.serio->phys);
3017 dev2->phys = priv->phys2;
3018
Pali Rohárdfba8602015-01-09 12:48:44 -08003019 /*
3020 * format of input device name is: "protocol vendor name"
3021 * see function psmouse_switch_protocol() in psmouse-base.c
3022 */
3023 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08003024
3025 dev2->id.bustype = BUS_I8042;
3026 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08003027 dev2->id.product = PSMOUSE_ALPS;
3028 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08003029 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08003030
Pali Rohár04aae282015-01-14 13:18:30 -08003031 input_set_capability(dev2, EV_REL, REL_X);
3032 input_set_capability(dev2, EV_REL, REL_Y);
Ben Gamari7ad8a102016-10-04 11:41:58 -07003033 if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3034 input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3035 input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3036 }
Pali Rohár04aae282015-01-14 13:18:30 -08003037 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3038 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3039 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003040
Pali Rohár04aae282015-01-14 13:18:30 -08003041 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07003042 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3043
Pali Rohár04aae282015-01-14 13:18:30 -08003044 error = input_register_device(dev2);
3045 if (error) {
3046 psmouse_err(psmouse,
3047 "failed to register trackstick device: %d\n",
3048 error);
3049 input_free_device(dev2);
3050 goto init_fail;
3051 }
3052
3053 priv->dev2 = dev2;
3054 }
3055
3056 priv->psmouse = psmouse;
3057
3058 INIT_DELAYED_WORK(&priv->dev3_register_work,
3059 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003062 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 psmouse->disconnect = alps_disconnect;
3064 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003065 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003067 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3068 psmouse->resync_time = 0;
3069
Pali Rohár9d720b32014-11-08 12:58:57 -08003070 /* Allow 2 invalid packets without resetting device */
3071 psmouse->resetafter = psmouse->pktsize * 2;
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return 0;
3074
3075init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04003076 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003077 /*
3078 * Even though we did not allocate psmouse->private we do free
3079 * it here.
3080 */
3081 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04003082 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003083 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07003086int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003088 struct alps_data *priv;
3089 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003091 error = alps_identify(psmouse, NULL);
3092 if (error)
3093 return error;
3094
3095 /*
3096 * Reset the device to make sure it is fully operational:
3097 * on some laptops, like certain Dell Latitudes, we may
3098 * fail to properly detect presence of trackstick if device
3099 * has not been reset.
3100 */
3101 psmouse_reset(psmouse);
3102
3103 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3104 if (!priv)
3105 return -ENOMEM;
3106
3107 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003108 if (error) {
3109 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003110 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 if (set_properties) {
3114 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003115 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003116 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003117 psmouse->model = priv->proto_version;
3118 } else {
3119 /*
3120 * Destroy alps_data structure we allocated earlier since
3121 * this was just a "trial run". Otherwise we'll keep it
3122 * to be used by alps_init() which has to be called if
3123 * we succeed and set_properties is true.
3124 */
3125 kfree(priv);
3126 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return 0;
3130}
3131