blob: 850b00e3ad8ecde13046449befa4fad95e308a60 [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 Torokhov626b9da2015-02-15 15:55:16 -0800109 /*
110 * XXX This entry is suspicious. First byte has zero lower nibble,
111 * which is what a normal mouse would report. Also, the value 0x0e
112 * isn't valid per PS/2 spec.
113 */
Pali Rohára3cbfd52017-03-07 09:43:35 -0800114 { { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800115
Pali Rohára3cbfd52017-03-07 09:43:35 -0800116 { { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
117 { { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
118 { { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
119 { { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
120 { { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff,
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800121 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
Pali Rohára3cbfd52017-03-07 09:43:35 -0800122 { { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
123 { { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
124 { { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
125 { { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf,
126 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
127 { { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
128 { { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
129 { { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
130 { { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
131 { { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
132 { { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
133 { { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
134 { { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
135 { { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
136 { { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
137 { { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
138 { { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800141static const struct alps_protocol_info alps_v3_protocol_data = {
142 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
143};
144
145static const struct alps_protocol_info alps_v3_rushmore_data = {
146 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
147};
148
Pali Rohár09c398b2017-03-07 09:41:34 -0800149static const struct alps_protocol_info alps_v4_protocol_data = {
150 ALPS_PROTO_V4, 0x8f, 0x8f, 0
151};
152
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800153static const struct alps_protocol_info alps_v5_protocol_data = {
154 ALPS_PROTO_V5, 0xc8, 0xd8, 0
155};
156
157static const struct alps_protocol_info alps_v7_protocol_data = {
158 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
159};
160
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700161static const struct alps_protocol_info alps_v8_protocol_data = {
162 ALPS_PROTO_V8, 0x18, 0x18, 0
163};
164
Pali Rohárc9815232017-03-07 09:42:46 -0800165static const struct alps_protocol_info alps_v9_protocol_data = {
166 ALPS_PROTO_V9, 0xc8, 0xc8, 0
167};
168
Hans de Goede19556212015-10-26 01:50:28 -0700169/*
170 * Some v2 models report the stick buttons in separate bits
171 */
172static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
173#if defined(CONFIG_DMI) && defined(CONFIG_X86)
174 {
175 /* Extrapolated from other entries */
176 .matches = {
177 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
178 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
179 },
180 },
181 {
182 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
183 .matches = {
184 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
185 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
186 },
187 },
188 {
189 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
190 .matches = {
191 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
192 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
193 },
194 },
195 {
196 /* Extrapolated from other entries */
197 .matches = {
198 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
199 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
200 },
201 },
202#endif
203 { }
204};
205
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800206static void alps_set_abs_params_st(struct alps_data *priv,
207 struct input_dev *dev1);
Hans de Goede688ea362015-05-20 14:42:50 -0700208static void alps_set_abs_params_semi_mt(struct alps_data *priv,
209 struct input_dev *dev1);
Masaki Ota8eccd392015-03-27 20:44:21 -0700210static void alps_set_abs_params_v7(struct alps_data *priv,
211 struct input_dev *dev1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700212static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
213 struct input_dev *dev1);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800214
Seth Forsheed4b347b2011-11-07 19:53:15 -0800215/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800217static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800218 unsigned char data)
219{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800220 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800221}
222
Pali Rohár04aae282015-01-14 13:18:30 -0800223static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800224 int left, int right, int middle)
225{
Martin Buckc91ed052010-03-13 22:23:58 -0800226 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800227
Martin Buckc91ed052010-03-13 22:23:58 -0800228 /*
229 * If shared button has already been reported on the
230 * other device (dev2) then this event should be also
231 * sent through that device.
232 */
Pali Rohár04aae282015-01-14 13:18:30 -0800233 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800234 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800235
Pali Rohár04aae282015-01-14 13:18:30 -0800236 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800237 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800238
Pali Rohár04aae282015-01-14 13:18:30 -0800239 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800240 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800241
Martin Buckc91ed052010-03-13 22:23:58 -0800242 /*
243 * Sync the _other_ device now, we'll do the first
244 * device later once we report the rest of the events.
245 */
Pali Rohár04aae282015-01-14 13:18:30 -0800246 if (dev2)
247 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800248}
249
Seth Forshee25bded72011-11-07 19:53:36 -0800250static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct alps_data *priv = psmouse->private;
253 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500254 struct input_dev *dev = psmouse->dev;
255 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500257 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800259 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400260 left = packet[2] & 0x10;
261 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 middle = 0;
263 x = packet[1] | ((packet[0] & 0x07) << 7);
264 y = packet[4] | ((packet[3] & 0x07) << 7);
265 z = packet[5];
266 } else {
267 left = packet[3] & 1;
268 right = packet[3] & 2;
269 middle = packet[3] & 4;
270 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
271 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
272 z = packet[5];
273 }
274
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800275 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400276 back = packet[0] & 0x10;
277 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500278 }
279
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800280 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500281 back = packet[3] & 4;
282 forward = packet[2] & 4;
283 if ((middle = forward && back))
284 forward = back = 0;
285 }
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 ges = packet[2] & 1;
288 fin = packet[2] & 2;
289
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800290 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
292 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700293
Pali Rohár04aae282015-01-14 13:18:30 -0800294 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 input_sync(dev2);
297 return;
298 }
299
Hans de Goede19556212015-10-26 01:50:28 -0700300 /* Some models have separate stick button bits */
301 if (priv->flags & ALPS_STICK_BITS) {
Hans de Goede92bac832015-04-12 15:42:35 -0700302 left |= packet[0] & 1;
303 right |= packet[0] & 2;
304 middle |= packet[0] & 4;
305 }
306
Pali Rohár04aae282015-01-14 13:18:30 -0800307 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800310 if (ges && !fin)
311 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /*
314 * A "tap and drag" operation is reported by the hardware as a transition
315 * from (!fin && ges) to (fin && ges). This should be translated to the
316 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
317 */
318 if (ges && fin && !priv->prev_fin) {
319 input_report_abs(dev, ABS_X, x);
320 input_report_abs(dev, ABS_Y, y);
321 input_report_abs(dev, ABS_PRESSURE, 0);
322 input_report_key(dev, BTN_TOOL_FINGER, 0);
323 input_sync(dev);
324 }
325 priv->prev_fin = fin;
326
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800327 if (z > 30)
328 input_report_key(dev, BTN_TOUCH, 1);
329 if (z < 25)
330 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (z > 0) {
333 input_report_abs(dev, ABS_X, x);
334 input_report_abs(dev, ABS_Y, y);
335 }
336
337 input_report_abs(dev, ABS_PRESSURE, z);
338 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
339
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800340 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500341 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800343 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500344 input_report_key(dev, BTN_FORWARD, forward);
345 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800348 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800349 input_report_key(dev, BTN_0, packet[2] & 4);
350 input_report_key(dev, BTN_1, packet[0] & 0x10);
351 input_report_key(dev, BTN_2, packet[3] & 4);
352 input_report_key(dev, BTN_3, packet[0] & 0x20);
353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 input_sync(dev);
356}
357
Hans de Goede036e6c72014-07-25 22:38:51 -0700358static void alps_get_bitmap_points(unsigned int map,
359 struct alps_bitmap_point *low,
360 struct alps_bitmap_point *high,
361 int *fingers)
362{
363 struct alps_bitmap_point *point;
364 int i, bit, prev_bit = 0;
365
366 point = low;
367 for (i = 0; map != 0; i++, map >>= 1) {
368 bit = map & 1;
369 if (bit) {
370 if (!prev_bit) {
371 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700372 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700373 (*fingers)++;
374 }
375 point->num_bits++;
376 } else {
377 if (prev_bit)
378 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700379 }
380 prev_bit = bit;
381 }
382}
383
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800384/*
Hans de Goededccf1dd2015-05-20 14:43:09 -0700385 * Process bitmap data from semi-mt protocols. Returns the number of
Seth Forshee01ce6612011-11-07 19:54:13 -0800386 * fingers detected. A return value of 0 means at least one of the
387 * bitmaps was empty.
388 *
389 * The bitmaps don't have enough data to track fingers, so this function
390 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700391 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800392 * is greater than 0.
393 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800394static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700395 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800396{
Hans de Goede4dd26572015-05-20 14:41:10 -0700397 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
Seth Forshee01ce6612011-11-07 19:54:13 -0800398 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
399 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Hans de Goede4dd26572015-05-20 14:41:10 -0700400 struct input_mt_pos corner[4];
Seth Forshee01ce6612011-11-07 19:54:13 -0800401
Hans de Goede02d04252014-07-25 22:43:35 -0700402 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800403 return 0;
404
Hans de Goede02d04252014-07-25 22:43:35 -0700405 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
406 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800407
408 /*
409 * Fingers can overlap, so we use the maximum count of fingers
410 * on either axis as the finger count.
411 */
412 fingers = max(fingers_x, fingers_y);
413
414 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700415 * If an axis reports only a single contact, we have overlapping or
416 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800417 */
Hans de Goede20bea682014-07-25 22:33:33 -0700418 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700419 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700420 x_low.num_bits = x_low.num_bits - i;
421 x_high.start_bit = x_low.start_bit + i;
422 x_high.num_bits = max(i, 1);
423 }
424 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700425 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700426 y_low.num_bits = y_low.num_bits - i;
427 y_high.start_bit = y_low.start_bit + i;
428 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800429 }
430
Hans de Goede4dd26572015-05-20 14:41:10 -0700431 /* top-left corner */
432 corner[0].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700433 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
434 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700435 corner[0].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700436 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
437 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800438
Hans de Goede4dd26572015-05-20 14:41:10 -0700439 /* top-right corner */
440 corner[1].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700441 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
442 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700443 corner[1].y =
444 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
445 (2 * (priv->y_bits - 1));
446
447 /* bottom-right corner */
448 corner[2].x =
449 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
450 (2 * (priv->x_bits - 1));
451 corner[2].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700452 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
453 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800454
Hans de Goede4dd26572015-05-20 14:41:10 -0700455 /* bottom-left corner */
456 corner[3].x =
457 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
458 (2 * (priv->x_bits - 1));
459 corner[3].y =
Seth Forshee01ce6612011-11-07 19:54:13 -0800460 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
461 (2 * (priv->y_bits - 1));
462
Hans de Goededccf1dd2015-05-20 14:43:09 -0700463 /* x-bitmap order is reversed on v5 touchpads */
464 if (priv->proto_version == ALPS_PROTO_V5) {
465 for (i = 0; i < 4; i++)
466 corner[i].x = priv->x_max - corner[i].x;
Hans de Goede40e8f532014-07-25 22:37:15 -0700467 }
468
Hans de Goededccf1dd2015-05-20 14:43:09 -0700469 /* y-bitmap order is reversed on v3 and v4 touchpads */
470 if (priv->proto_version == ALPS_PROTO_V3 ||
471 priv->proto_version == ALPS_PROTO_V4) {
Hans de Goede4dd26572015-05-20 14:41:10 -0700472 for (i = 0; i < 4; i++)
473 corner[i].y = priv->y_max - corner[i].y;
Seth Forshee01ce6612011-11-07 19:54:13 -0800474 }
475
Hans de Goede4dd26572015-05-20 14:41:10 -0700476 /*
477 * We only select a corner for the second touch once per 2 finger
478 * touch sequence to avoid the chosen corner (and thus the coordinates)
479 * jumping around when the first touch is in the middle.
480 */
481 if (priv->second_touch == -1) {
482 /* Find corner closest to our st coordinates */
483 closest = 0x7fffffff;
484 for (i = 0; i < 4; i++) {
485 int dx = fields->st.x - corner[i].x;
486 int dy = fields->st.y - corner[i].y;
487 int distance = dx * dx + dy * dy;
488
489 if (distance < closest) {
490 priv->second_touch = i;
491 closest = distance;
492 }
493 }
494 /* And select the opposite corner to use for the 2nd touch */
495 priv->second_touch = (priv->second_touch + 2) % 4;
496 }
497
498 fields->mt[0] = fields->st;
499 fields->mt[1] = corner[priv->second_touch];
500
Seth Forshee01ce6612011-11-07 19:54:13 -0800501 return fingers;
502}
503
Hans de Goedecdf333e2014-07-25 22:44:42 -0700504static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800505{
506 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700507 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
508 input_report_abs(dev, ABS_MT_POSITION_X, x);
509 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800510}
511
Hans de Goedecdf333e2014-07-25 22:44:42 -0700512static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800513{
Hans de Goede02d04252014-07-25 22:43:35 -0700514 struct alps_data *priv = psmouse->private;
515 struct input_dev *dev = psmouse->dev;
516 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700517 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700518
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800519 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700520 for (i = 0; i < n; i++)
521 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
522
523 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800524}
525
Hans de Goede68c21872014-07-25 22:47:25 -0700526static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
527{
528 struct alps_data *priv = psmouse->private;
529 struct input_dev *dev = psmouse->dev;
530 struct alps_fields *f = &priv->f;
531
532 /* Use st data when we don't have mt data */
533 if (fingers < 2) {
534 f->mt[0].x = f->st.x;
535 f->mt[0].y = f->st.y;
536 fingers = f->pressure > 0 ? 1 : 0;
Hans de Goede4dd26572015-05-20 14:41:10 -0700537 priv->second_touch = -1;
Hans de Goede68c21872014-07-25 22:47:25 -0700538 }
539
Hans de Goede1662c0332015-05-20 14:42:15 -0700540 if (fingers >= 1)
541 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
542 if (fingers >= 2)
543 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
544 input_mt_sync_frame(dev);
Hans de Goede68c21872014-07-25 22:47:25 -0700545
546 input_mt_report_finger_count(dev, fingers);
547
548 input_report_key(dev, BTN_LEFT, f->left);
549 input_report_key(dev, BTN_RIGHT, f->right);
550 input_report_key(dev, BTN_MIDDLE, f->middle);
551
552 input_report_abs(dev, ABS_PRESSURE, f->pressure);
553
554 input_sync(dev);
555}
556
Seth Forshee25bded72011-11-07 19:53:36 -0800557static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
558{
559 struct alps_data *priv = psmouse->private;
560 unsigned char *packet = psmouse->packet;
561 struct input_dev *dev = priv->dev2;
562 int x, y, z, left, right, middle;
563
Pali Rohár34412ba2015-01-09 12:48:58 -0800564 /* It should be a DualPoint when received trackstick packet */
565 if (!(priv->flags & ALPS_DUALPOINT)) {
566 psmouse_warn(psmouse,
567 "Rejected trackstick packet from non DualPoint device");
568 return;
569 }
570
Seth Forshee25bded72011-11-07 19:53:36 -0800571 /* Sanity check packet */
572 if (!(packet[0] & 0x40)) {
573 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
574 return;
575 }
576
577 /*
578 * There's a special packet that seems to indicate the end
579 * of a stream of trackstick data. Filter these out.
580 */
581 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
582 return;
583
584 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
585 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
586 z = (packet[4] & 0x7c) >> 2;
587
588 /*
589 * The x and y values tend to be quite large, and when used
590 * alone the trackstick is difficult to use. Scale them down
591 * to compensate.
592 */
593 x /= 8;
594 y /= 8;
595
596 input_report_rel(dev, REL_X, x);
597 input_report_rel(dev, REL_Y, -y);
598
599 /*
600 * Most ALPS models report the trackstick buttons in the touchpad
601 * packets, but a few report them here. No reliable way has been
602 * found to differentiate between the models upfront, so we enable
603 * the quirk in response to seeing a button press in the trackstick
604 * packet.
605 */
606 left = packet[3] & 0x01;
607 right = packet[3] & 0x02;
608 middle = packet[3] & 0x04;
609
610 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
611 (left || right || middle))
612 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
613
614 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
615 input_report_key(dev, BTN_LEFT, left);
616 input_report_key(dev, BTN_RIGHT, right);
617 input_report_key(dev, BTN_MIDDLE, middle);
618 }
619
620 input_sync(dev);
621 return;
622}
623
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800624static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
625{
626 f->left = !!(p[3] & 0x01);
627 f->right = !!(p[3] & 0x02);
628 f->middle = !!(p[3] & 0x04);
629
630 f->ts_left = !!(p[3] & 0x10);
631 f->ts_right = !!(p[3] & 0x20);
632 f->ts_middle = !!(p[3] & 0x40);
633}
634
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700635static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800636 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800637{
638 f->first_mp = !!(p[4] & 0x40);
639 f->is_mp = !!(p[0] & 0x40);
640
Hans de Goedea839cd52015-05-20 14:39:21 -0700641 if (f->is_mp) {
642 f->fingers = (p[5] & 0x3) + 1;
643 f->x_map = ((p[4] & 0x7e) << 8) |
644 ((p[1] & 0x7f) << 2) |
645 ((p[0] & 0x30) >> 4);
646 f->y_map = ((p[3] & 0x70) << 4) |
647 ((p[2] & 0x7f) << 1) |
648 (p[4] & 0x01);
649 } else {
650 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
651 ((p[0] & 0x30) >> 4);
652 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
653 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800654
Hans de Goedea839cd52015-05-20 14:39:21 -0700655 alps_decode_buttons_v3(f, p);
656 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700657
658 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800659}
660
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700661static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800662 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800663{
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700664 f->first_mp = !!(p[4] & 0x40);
Yunkang Tangf105e342014-07-25 22:29:24 -0700665 f->is_mp = !!(p[5] & 0x40);
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700666
Hans de Goedea839cd52015-05-20 14:39:21 -0700667 if (f->is_mp) {
668 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
669 f->x_map = ((p[5] & 0x10) << 11) |
670 ((p[4] & 0x7e) << 8) |
671 ((p[1] & 0x7f) << 2) |
672 ((p[0] & 0x30) >> 4);
673 f->y_map = ((p[5] & 0x20) << 6) |
674 ((p[3] & 0x70) << 4) |
675 ((p[2] & 0x7f) << 1) |
676 (p[4] & 0x01);
677 } else {
678 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
679 ((p[0] & 0x30) >> 4);
680 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
681 f->pressure = p[5] & 0x7f;
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700682
Hans de Goedea839cd52015-05-20 14:39:21 -0700683 alps_decode_buttons_v3(f, p);
684 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700685
686 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800687}
688
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700689static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800690 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800691{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800692 u64 palm_data = 0;
693 struct alps_data *priv = psmouse->private;
694
Dave Turvene75af9e52013-02-21 22:58:28 -0800695 f->first_mp = !!(p[0] & 0x02);
696 f->is_mp = !!(p[0] & 0x20);
697
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800698 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700699 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
700 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
701 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800702 alps_decode_buttons_v3(f, p);
703 } else {
704 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800705 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800706
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800707 palm_data = (p[1] & 0x7f) |
708 ((p[2] & 0x7f) << 7) |
709 ((p[4] & 0x7f) << 14) |
710 ((p[5] & 0x7f) << 21) |
711 ((p[3] & 0x07) << 28) |
712 (((u64)p[3] & 0x70) << 27) |
713 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800714
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800715 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
716 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
717
718 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
719 f->x_map = (palm_data >> priv->y_bits) &
720 (BIT(priv->x_bits) - 1);
721 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700722
723 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800724}
725
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800726static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800727{
728 struct alps_data *priv = psmouse->private;
729 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800730 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700731 struct alps_fields *f = &priv->f;
732 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800733
Hans de Goede02d04252014-07-25 22:43:35 -0700734 memset(f, 0, sizeof(*f));
735
736 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800737
738 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800739 * There's no single feature of touchpad position and bitmap packets
740 * that can be used to distinguish between them. We rely on the fact
741 * that a bitmap packet should always follow a position packet with
742 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800743 */
744 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800745 /*
746 * Sometimes a position packet will indicate a multi-packet
747 * sequence, but then what follows is another position
748 * packet. Check for this, and when it happens process the
749 * position packet as usual.
750 */
Hans de Goede02d04252014-07-25 22:43:35 -0700751 if (f->is_mp) {
752 fingers = f->fingers;
Hans de Goede44b77f32015-05-20 14:40:06 -0700753 /*
754 * Bitmap processing uses position packet's coordinate
755 * data, so we need to do decode it first.
756 */
757 priv->decode_fields(f, priv->multi_data, psmouse);
Hans de Goededccf1dd2015-05-20 14:43:09 -0700758 if (alps_process_bitmap(priv, f) == 0)
759 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800760 } else {
761 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800762 }
763 }
764
Seth Forshee01ce6612011-11-07 19:54:13 -0800765 /*
766 * Bit 6 of byte 0 is not usually set in position packets. The only
767 * times it seems to be set is in situations where the data is
768 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
769 * this combined with the fact that this bit is useful for filtering
770 * out misidentified bitmap packets, we reject anything with this
771 * bit set.
772 */
Hans de Goede02d04252014-07-25 22:43:35 -0700773 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800774 return;
775
Hans de Goede02d04252014-07-25 22:43:35 -0700776 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800777 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800778 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
779 return;
780 }
781
782 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800783
Seth Forshee25bded72011-11-07 19:53:36 -0800784 /*
785 * Sometimes the hardware sends a single packet with z = 0
786 * in the middle of a stream. Real releases generate packets
787 * with x, y, and z all zero, so these seem to be flukes.
788 * Ignore them.
789 */
Hans de Goede02d04252014-07-25 22:43:35 -0700790 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800791 return;
792
Hans de Goede68c21872014-07-25 22:47:25 -0700793 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800794
Pali Rohár34412ba2015-01-09 12:48:58 -0800795 if ((priv->flags & ALPS_DUALPOINT) &&
796 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700797 input_report_key(dev2, BTN_LEFT, f->ts_left);
798 input_report_key(dev2, BTN_RIGHT, f->ts_right);
799 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800800 input_sync(dev2);
801 }
802}
803
804static void alps_process_packet_v3(struct psmouse *psmouse)
805{
806 unsigned char *packet = psmouse->packet;
807
808 /*
809 * v3 protocol packets come in three types, two representing
810 * touchpad data and one representing trackstick data.
811 * Trackstick packets seem to be distinguished by always
812 * having 0x3f in the last byte. This value has never been
813 * observed in the last byte of either of the other types
814 * of packets.
815 */
816 if (packet[5] == 0x3f) {
817 alps_process_trackstick_packet_v3(psmouse);
818 return;
819 }
820
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800821 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800822}
823
Yunkang Tang95f75e92013-12-01 22:33:52 -0800824static void alps_process_packet_v6(struct psmouse *psmouse)
825{
826 struct alps_data *priv = psmouse->private;
827 unsigned char *packet = psmouse->packet;
828 struct input_dev *dev = psmouse->dev;
829 struct input_dev *dev2 = priv->dev2;
830 int x, y, z, left, right, middle;
831
832 /*
833 * We can use Byte5 to distinguish if the packet is from Touchpad
834 * or Trackpoint.
835 * Touchpad: 0 - 0x7E
836 * Trackpoint: 0x7F
837 */
838 if (packet[5] == 0x7F) {
839 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800840 if (!(priv->flags & ALPS_DUALPOINT)) {
841 psmouse_warn(psmouse,
842 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800843 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800844 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800845
846 /* Trackpoint packet */
847 x = packet[1] | ((packet[3] & 0x20) << 2);
848 y = packet[2] | ((packet[3] & 0x40) << 1);
849 z = packet[4];
850 left = packet[3] & 0x01;
851 right = packet[3] & 0x02;
852 middle = packet[3] & 0x04;
853
854 /* To prevent the cursor jump when finger lifted */
855 if (x == 0x7F && y == 0x7F && z == 0x7F)
856 x = y = z = 0;
857
858 /* Divide 4 since trackpoint's speed is too fast */
859 input_report_rel(dev2, REL_X, (char)x / 4);
860 input_report_rel(dev2, REL_Y, -((char)y / 4));
861
862 input_report_key(dev2, BTN_LEFT, left);
863 input_report_key(dev2, BTN_RIGHT, right);
864 input_report_key(dev2, BTN_MIDDLE, middle);
865
866 input_sync(dev2);
867 return;
868 }
869
870 /* Touchpad packet */
871 x = packet[1] | ((packet[3] & 0x78) << 4);
872 y = packet[2] | ((packet[4] & 0x78) << 4);
873 z = packet[5];
874 left = packet[3] & 0x01;
875 right = packet[3] & 0x02;
876
877 if (z > 30)
878 input_report_key(dev, BTN_TOUCH, 1);
879 if (z < 25)
880 input_report_key(dev, BTN_TOUCH, 0);
881
882 if (z > 0) {
883 input_report_abs(dev, ABS_X, x);
884 input_report_abs(dev, ABS_Y, y);
885 }
886
887 input_report_abs(dev, ABS_PRESSURE, z);
888 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
889
890 /* v6 touchpad does not have middle button */
891 input_report_key(dev, BTN_LEFT, left);
892 input_report_key(dev, BTN_RIGHT, right);
893
894 input_sync(dev);
895}
896
Seth Forshee25bded72011-11-07 19:53:36 -0800897static void alps_process_packet_v4(struct psmouse *psmouse)
898{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700899 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800900 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700901 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700902 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700903
904 /*
905 * v4 has a 6-byte encoding for bitmap data, but this data is
906 * broken up between 3 normal packets. Use priv->multi_packet to
907 * track our position in the bitmap packet.
908 */
909 if (packet[6] & 0x40) {
910 /* sync, reset position */
911 priv->multi_packet = 0;
912 }
913
914 if (WARN_ON_ONCE(priv->multi_packet > 2))
915 return;
916
917 offset = 2 * priv->multi_packet;
918 priv->multi_data[offset] = packet[6];
919 priv->multi_data[offset + 1] = packet[7];
920
Hans de Goede44b77f32015-05-20 14:40:06 -0700921 f->left = !!(packet[4] & 0x01);
922 f->right = !!(packet[4] & 0x02);
923
924 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
925 ((packet[0] & 0x30) >> 4);
926 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
927 f->pressure = packet[5] & 0x7f;
928
George Pantalos3b7e09f2012-05-10 22:31:59 -0700929 if (++priv->multi_packet > 2) {
930 priv->multi_packet = 0;
931
Hans de Goede02d04252014-07-25 22:43:35 -0700932 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700933 ((priv->multi_data[3] & 0x60) << 3) |
934 ((priv->multi_data[0] & 0x3f) << 2) |
935 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700936 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700937 ((priv->multi_data[3] & 0x1f) << 5) |
938 (priv->multi_data[1] & 0x1f);
939
Hans de Goede02d04252014-07-25 22:43:35 -0700940 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700941 }
Seth Forshee25bded72011-11-07 19:53:36 -0800942
Hans de Goede68c21872014-07-25 22:47:25 -0700943 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800944}
945
Yunkang Tang38088432014-07-26 13:51:41 -0700946static bool alps_is_valid_package_v7(struct psmouse *psmouse)
947{
948 switch (psmouse->pktcnt) {
949 case 3:
950 return (psmouse->packet[2] & 0x40) == 0x40;
951 case 4:
952 return (psmouse->packet[3] & 0x48) == 0x48;
953 case 6:
954 return (psmouse->packet[5] & 0x40) == 0x00;
955 }
956 return true;
957}
958
959static unsigned char alps_get_packet_id_v7(char *byte)
960{
961 unsigned char packet_id;
962
963 if (byte[4] & 0x40)
964 packet_id = V7_PACKET_ID_TWO;
965 else if (byte[4] & 0x01)
966 packet_id = V7_PACKET_ID_MULTI;
967 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
968 packet_id = V7_PACKET_ID_NEW;
969 else if (byte[1] == 0x00 && byte[4] == 0x00)
970 packet_id = V7_PACKET_ID_IDLE;
971 else
972 packet_id = V7_PACKET_ID_UNKNOWN;
973
974 return packet_id;
975}
976
977static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
978 unsigned char *pkt,
979 unsigned char pkt_id)
980{
981 mt[0].x = ((pkt[2] & 0x80) << 4);
982 mt[0].x |= ((pkt[2] & 0x3F) << 5);
983 mt[0].x |= ((pkt[3] & 0x30) >> 1);
984 mt[0].x |= (pkt[3] & 0x07);
985 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
986
987 mt[1].x = ((pkt[3] & 0x80) << 4);
988 mt[1].x |= ((pkt[4] & 0x80) << 3);
989 mt[1].x |= ((pkt[4] & 0x3F) << 4);
990 mt[1].y = ((pkt[5] & 0x80) << 3);
991 mt[1].y |= ((pkt[5] & 0x3F) << 4);
992
993 switch (pkt_id) {
994 case V7_PACKET_ID_TWO:
995 mt[1].x &= ~0x000F;
996 mt[1].y |= 0x000F;
Hans de Goede72eceab2015-05-20 14:37:41 -0700997 /* Detect false-postive touches where x & y report max value */
998 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
999 mt[1].x = 0;
1000 /* y gets set to 0 at the end of this function */
1001 }
Yunkang Tang38088432014-07-26 13:51:41 -07001002 break;
1003
1004 case V7_PACKET_ID_MULTI:
1005 mt[1].x &= ~0x003F;
1006 mt[1].y &= ~0x0020;
1007 mt[1].y |= ((pkt[4] & 0x02) << 4);
1008 mt[1].y |= 0x001F;
1009 break;
1010
1011 case V7_PACKET_ID_NEW:
1012 mt[1].x &= ~0x003F;
1013 mt[1].x |= (pkt[0] & 0x20);
1014 mt[1].y |= 0x000F;
1015 break;
1016 }
1017
1018 mt[0].y = 0x7FF - mt[0].y;
1019 mt[1].y = 0x7FF - mt[1].y;
1020}
1021
1022static int alps_get_mt_count(struct input_mt_pos *mt)
1023{
Hans de Goede7091c442014-12-18 09:53:34 -08001024 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -07001025
Hans de Goede7091c442014-12-18 09:53:34 -08001026 for (i = 0; i < MAX_TOUCHES; i++) {
1027 if (mt[i].x != 0 || mt[i].y != 0)
1028 fingers++;
1029 }
Yunkang Tang38088432014-07-26 13:51:41 -07001030
Hans de Goede7091c442014-12-18 09:53:34 -08001031 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -07001032}
1033
1034static int alps_decode_packet_v7(struct alps_fields *f,
1035 unsigned char *p,
1036 struct psmouse *psmouse)
1037{
Hans de Goeded27eb792014-12-18 09:55:14 -08001038 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -07001039 unsigned char pkt_id;
1040
1041 pkt_id = alps_get_packet_id_v7(p);
1042 if (pkt_id == V7_PACKET_ID_IDLE)
1043 return 0;
1044 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1045 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -08001046 /*
1047 * NEW packets are send to indicate a discontinuity in the finger
1048 * coordinate reporting. Specifically a finger may have moved from
1049 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1050 * us.
1051 *
1052 * NEW packets have 3 problems:
1053 * 1) They do not contain middle / right button info (on non clickpads)
1054 * this can be worked around by preserving the old button state
1055 * 2) They do not contain an accurate fingercount, and they are
1056 * typically send when the number of fingers changes. We cannot use
1057 * the old finger count as that may mismatch with the amount of
1058 * touch coordinates we've available in the NEW packet
1059 * 3) Their x data for the second touch is inaccurate leading to
1060 * a possible jump of the x coordinate by 16 units when the first
1061 * non NEW packet comes in
1062 * Since problems 2 & 3 cannot be worked around, just ignore them.
1063 */
1064 if (pkt_id == V7_PACKET_ID_NEW)
1065 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001066
1067 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1068
Yunkang Tang38088432014-07-26 13:51:41 -07001069 if (pkt_id == V7_PACKET_ID_TWO)
1070 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001071 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001072 f->fingers = 3 + (p[5] & 0x03);
1073
Hans de Goeded27eb792014-12-18 09:55:14 -08001074 f->left = (p[0] & 0x80) >> 7;
1075 if (priv->flags & ALPS_BUTTONPAD) {
1076 if (p[0] & 0x20)
1077 f->fingers++;
1078 if (p[0] & 0x10)
1079 f->fingers++;
1080 } else {
1081 f->right = (p[0] & 0x20) >> 5;
1082 f->middle = (p[0] & 0x10) >> 4;
1083 }
1084
Hans de Goede7091c442014-12-18 09:53:34 -08001085 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1086 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1087 f->mt[0].x = f->mt[1].x;
1088 f->mt[0].y = f->mt[1].y;
1089 f->mt[1].x = 0;
1090 f->mt[1].y = 0;
1091 }
1092
Yunkang Tang38088432014-07-26 13:51:41 -07001093 return 0;
1094}
1095
1096static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1097{
1098 struct alps_data *priv = psmouse->private;
1099 unsigned char *packet = psmouse->packet;
1100 struct input_dev *dev2 = priv->dev2;
1101 int x, y, z, left, right, middle;
1102
Pali Rohár34412ba2015-01-09 12:48:58 -08001103 /* It should be a DualPoint when received trackstick packet */
1104 if (!(priv->flags & ALPS_DUALPOINT)) {
1105 psmouse_warn(psmouse,
1106 "Rejected trackstick packet from non DualPoint device");
1107 return;
1108 }
1109
Yunkang Tang38088432014-07-26 13:51:41 -07001110 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1111 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1112 ((packet[3] & 0x20) << 1);
1113 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1114
1115 left = (packet[1] & 0x01);
1116 right = (packet[1] & 0x02) >> 1;
1117 middle = (packet[1] & 0x04) >> 2;
1118
Hans de Goede088df2c2015-06-04 22:31:43 -07001119 input_report_rel(dev2, REL_X, (char)x);
1120 input_report_rel(dev2, REL_Y, -((char)y));
Yunkang Tang38088432014-07-26 13:51:41 -07001121
1122 input_report_key(dev2, BTN_LEFT, left);
1123 input_report_key(dev2, BTN_RIGHT, right);
1124 input_report_key(dev2, BTN_MIDDLE, middle);
1125
1126 input_sync(dev2);
1127}
1128
1129static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1130{
1131 struct alps_data *priv = psmouse->private;
1132 struct input_dev *dev = psmouse->dev;
1133 struct alps_fields *f = &priv->f;
1134
1135 memset(f, 0, sizeof(*f));
1136
1137 if (priv->decode_fields(f, psmouse->packet, psmouse))
1138 return;
1139
1140 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1141
1142 input_mt_report_finger_count(dev, f->fingers);
1143
1144 input_report_key(dev, BTN_LEFT, f->left);
1145 input_report_key(dev, BTN_RIGHT, f->right);
1146 input_report_key(dev, BTN_MIDDLE, f->middle);
1147
1148 input_sync(dev);
1149}
1150
1151static void alps_process_packet_v7(struct psmouse *psmouse)
1152{
1153 unsigned char *packet = psmouse->packet;
1154
1155 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1156 alps_process_trackstick_packet_v7(psmouse);
1157 else
1158 alps_process_touchpad_packet_v7(psmouse);
1159}
1160
Paul Donohue23fce362016-11-28 20:16:21 -08001161static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001162{
Paul Donohue23fce362016-11-28 20:16:21 -08001163 enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001164
Ben Gamari4777ac22016-10-04 11:33:25 -07001165 switch (byte[3] & 0x30) {
1166 case 0x00:
Paul Donohue23fce362016-11-28 20:16:21 -08001167 if (SS4_IS_IDLE_V2(byte)) {
Ben Gamari4777ac22016-10-04 11:33:25 -07001168 pkt_id = SS4_PACKET_ID_IDLE;
1169 } else {
1170 pkt_id = SS4_PACKET_ID_ONE;
1171 }
1172 break;
1173 case 0x10:
1174 /* two-finger finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001175 pkt_id = SS4_PACKET_ID_TWO;
Ben Gamari4777ac22016-10-04 11:33:25 -07001176 break;
1177 case 0x20:
1178 /* stick pointer */
1179 pkt_id = SS4_PACKET_ID_STICK;
1180 break;
1181 case 0x30:
1182 /* third and fourth finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001183 pkt_id = SS4_PACKET_ID_MULTI;
Ben Gamari4777ac22016-10-04 11:33:25 -07001184 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001185 }
1186
1187 return pkt_id;
1188}
1189
1190static int alps_decode_ss4_v2(struct alps_fields *f,
1191 unsigned char *p, struct psmouse *psmouse)
1192{
1193 struct alps_data *priv = psmouse->private;
Paul Donohue23fce362016-11-28 20:16:21 -08001194 enum SS4_PACKET_ID pkt_id;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001195 unsigned int no_data_x, no_data_y;
1196
1197 pkt_id = alps_get_pkt_id_ss4_v2(p);
1198
1199 /* Current packet is 1Finger coordinate packet */
1200 switch (pkt_id) {
1201 case SS4_PACKET_ID_ONE:
1202 f->mt[0].x = SS4_1F_X_V2(p);
1203 f->mt[0].y = SS4_1F_Y_V2(p);
1204 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
Ben Gamaria8317762016-10-04 11:40:44 -07001205 /*
1206 * When a button is held the device will give us events
1207 * with x, y, and pressure of 0. This causes annoying jumps
1208 * if a touch is released while the button is held.
1209 * Handle this by claiming zero contacts.
1210 */
1211 f->fingers = f->pressure > 0 ? 1 : 0;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001212 f->first_mp = 0;
1213 f->is_mp = 0;
1214 break;
1215
1216 case SS4_PACKET_ID_TWO:
1217 if (priv->flags & ALPS_BUTTONPAD) {
Masaki Ota4a646582017-08-24 15:44:36 -07001218 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1219 f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1220 f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1221 } else {
1222 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1223 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1224 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001225 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001226 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1227 } else {
Masaki Ota4a646582017-08-24 15:44:36 -07001228 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1229 f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1230 f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1231 } else {
1232 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1233 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1234 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001235 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001236 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1237 }
1238 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1239
1240 if (SS4_IS_MF_CONTINUE(p)) {
1241 f->first_mp = 1;
1242 } else {
1243 f->fingers = 2;
1244 f->first_mp = 0;
1245 }
1246 f->is_mp = 0;
1247
1248 break;
1249
1250 case SS4_PACKET_ID_MULTI:
1251 if (priv->flags & ALPS_BUTTONPAD) {
Masaki Ota4a646582017-08-24 15:44:36 -07001252 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1253 f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1254 f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1255 } else {
1256 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1257 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1258 }
1259
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001260 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001261 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1262 no_data_x = SS4_MFPACKET_NO_AX_BL;
1263 no_data_y = SS4_MFPACKET_NO_AY_BL;
1264 } else {
Masaki Ota4a646582017-08-24 15:44:36 -07001265 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1266 f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1267 f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1268 } else {
1269 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1270 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1271 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001272 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001273 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1274 no_data_x = SS4_MFPACKET_NO_AX;
1275 no_data_y = SS4_MFPACKET_NO_AY;
1276 }
1277
1278 f->first_mp = 0;
1279 f->is_mp = 1;
1280
1281 if (SS4_IS_5F_DETECTED(p)) {
1282 f->fingers = 5;
1283 } else if (f->mt[3].x == no_data_x &&
1284 f->mt[3].y == no_data_y) {
1285 f->mt[3].x = 0;
1286 f->mt[3].y = 0;
1287 f->fingers = 3;
1288 } else {
1289 f->fingers = 4;
1290 }
1291 break;
1292
Ben Gamari4777ac22016-10-04 11:33:25 -07001293 case SS4_PACKET_ID_STICK:
Paul Donohue7229c582016-11-28 20:13:47 -08001294 /*
1295 * x, y, and pressure are decoded in
1296 * alps_process_packet_ss4_v2()
1297 */
1298 f->first_mp = 0;
1299 f->is_mp = 0;
Ben Gamari4777ac22016-10-04 11:33:25 -07001300 break;
1301
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001302 case SS4_PACKET_ID_IDLE:
1303 default:
1304 memset(f, 0, sizeof(struct alps_fields));
1305 break;
1306 }
1307
Ben Gamari4777ac22016-10-04 11:33:25 -07001308 /* handle buttons */
1309 if (pkt_id == SS4_PACKET_ID_STICK) {
1310 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
Masaki Ota47e6fb42017-03-17 14:19:40 -07001311 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1312 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
Ben Gamari4777ac22016-10-04 11:33:25 -07001313 } else {
1314 f->left = !!(SS4_BTN_V2(p) & 0x01);
1315 if (!(priv->flags & ALPS_BUTTONPAD)) {
1316 f->right = !!(SS4_BTN_V2(p) & 0x02);
1317 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1318 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001319 }
1320
1321 return 0;
1322}
1323
1324static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1325{
1326 struct alps_data *priv = psmouse->private;
1327 unsigned char *packet = psmouse->packet;
1328 struct input_dev *dev = psmouse->dev;
Ben Gamari4777ac22016-10-04 11:33:25 -07001329 struct input_dev *dev2 = priv->dev2;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001330 struct alps_fields *f = &priv->f;
1331
1332 memset(f, 0, sizeof(struct alps_fields));
1333 priv->decode_fields(f, packet, psmouse);
1334 if (priv->multi_packet) {
1335 /*
1336 * Sometimes the first packet will indicate a multi-packet
1337 * sequence, but sometimes the next multi-packet would not
1338 * come. Check for this, and when it happens process the
1339 * position packet as usual.
1340 */
1341 if (f->is_mp) {
1342 /* Now process the 1st packet */
1343 priv->decode_fields(f, priv->multi_data, psmouse);
1344 } else {
1345 priv->multi_packet = 0;
1346 }
1347 }
1348
1349 /*
1350 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1351 * When it is set, it means 2nd packet comes without 1st packet come.
1352 */
1353 if (f->is_mp)
1354 return;
1355
1356 /* Save the first packet */
1357 if (!priv->multi_packet && f->first_mp) {
1358 priv->multi_packet = 1;
1359 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1360 return;
1361 }
1362
1363 priv->multi_packet = 0;
1364
Paul Donohue864db922016-11-28 20:11:25 -08001365 /* Report trackstick */
1366 if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
Paul Donohue7229c582016-11-28 20:13:47 -08001367 if (!(priv->flags & ALPS_DUALPOINT)) {
1368 psmouse_warn(psmouse,
1369 "Rejected trackstick packet from non DualPoint device");
1370 return;
Paul Donohue864db922016-11-28 20:11:25 -08001371 }
Paul Donohue7229c582016-11-28 20:13:47 -08001372
Paul Donohue23fce362016-11-28 20:16:21 -08001373 input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
1374 input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
1375 input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
Paul Donohue7229c582016-11-28 20:13:47 -08001376
1377 input_report_key(dev2, BTN_LEFT, f->ts_left);
1378 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1379 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1380
1381 input_sync(dev2);
Paul Donohue864db922016-11-28 20:11:25 -08001382 return;
1383 }
1384
1385 /* Report touchpad */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001386 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1387
1388 input_mt_report_finger_count(dev, f->fingers);
1389
1390 input_report_key(dev, BTN_LEFT, f->left);
1391 input_report_key(dev, BTN_RIGHT, f->right);
1392 input_report_key(dev, BTN_MIDDLE, f->middle);
1393
1394 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1395 input_sync(dev);
1396}
1397
1398static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1399{
1400 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1401 return false;
1402 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1403 return false;
1404 return true;
1405}
1406
Pali Rohár04aae282015-01-14 13:18:30 -08001407static DEFINE_MUTEX(alps_mutex);
1408
1409static void alps_register_bare_ps2_mouse(struct work_struct *work)
1410{
1411 struct alps_data *priv =
1412 container_of(work, struct alps_data, dev3_register_work.work);
1413 struct psmouse *psmouse = priv->psmouse;
1414 struct input_dev *dev3;
1415 int error = 0;
1416
1417 mutex_lock(&alps_mutex);
1418
1419 if (priv->dev3)
1420 goto out;
1421
1422 dev3 = input_allocate_device();
1423 if (!dev3) {
1424 psmouse_err(psmouse, "failed to allocate secondary device\n");
1425 error = -ENOMEM;
1426 goto out;
1427 }
1428
1429 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1430 psmouse->ps2dev.serio->phys,
1431 (priv->dev2 ? "input2" : "input1"));
1432 dev3->phys = priv->phys3;
1433
1434 /*
1435 * format of input device name is: "protocol vendor name"
1436 * see function psmouse_switch_protocol() in psmouse-base.c
1437 */
1438 dev3->name = "PS/2 ALPS Mouse";
1439
1440 dev3->id.bustype = BUS_I8042;
1441 dev3->id.vendor = 0x0002;
1442 dev3->id.product = PSMOUSE_PS2;
1443 dev3->id.version = 0x0000;
1444 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1445
1446 input_set_capability(dev3, EV_REL, REL_X);
1447 input_set_capability(dev3, EV_REL, REL_Y);
1448 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1449 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1450 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1451
1452 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1453
1454 error = input_register_device(dev3);
1455 if (error) {
1456 psmouse_err(psmouse,
1457 "failed to register secondary device: %d\n",
1458 error);
1459 input_free_device(dev3);
1460 goto out;
1461 }
1462
1463 priv->dev3 = dev3;
1464
1465out:
1466 /*
1467 * Save the error code so that we can detect that we
1468 * already tried to create the device.
1469 */
1470 if (error)
1471 priv->dev3 = ERR_PTR(error);
1472
1473 mutex_unlock(&alps_mutex);
1474}
1475
Hans de Goede59c30af2015-04-03 17:14:40 -07001476static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001477 unsigned char packet[],
1478 bool report_buttons)
1479{
Hans de Goede59c30af2015-04-03 17:14:40 -07001480 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001481 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001482
Hans de Goedee3a79212015-04-03 17:20:05 -07001483 /* Figure out which device to use to report the bare packet */
1484 if (priv->proto_version == ALPS_PROTO_V2 &&
1485 (priv->flags & ALPS_DUALPOINT)) {
1486 /* On V2 devices the DualPoint Stick reports bare packets */
1487 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001488 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001489 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001490 /* Register dev3 mouse if we received PS/2 packet first time */
1491 if (!IS_ERR(priv->dev3))
1492 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1493 0);
1494 return;
1495 } else {
1496 dev = priv->dev3;
1497 }
1498
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001499 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001500 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001501 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1502
Pali Rohár04aae282015-01-14 13:18:30 -08001503 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001504 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001505 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001506 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1507
Pali Rohár04aae282015-01-14 13:18:30 -08001508 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001509}
1510
1511static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 struct alps_data *priv = psmouse->private;
1514
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001515 if (psmouse->pktcnt < 6)
1516 return PSMOUSE_GOOD_DATA;
1517
1518 if (psmouse->pktcnt == 6) {
1519 /*
1520 * Start a timer to flush the packet if it ends up last
1521 * 6-byte packet in the stream. Timer needs to fire
1522 * psmouse core times out itself. 20 ms should be enough
1523 * to decide if we are getting more data or not.
1524 */
1525 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1526 return PSMOUSE_GOOD_DATA;
1527 }
1528
1529 del_timer(&priv->timer);
1530
1531 if (psmouse->packet[6] & 0x80) {
1532
1533 /*
1534 * Highest bit is set - that means we either had
1535 * complete ALPS packet and this is start of the
1536 * next packet or we got garbage.
1537 */
1538
1539 if (((psmouse->packet[3] |
1540 psmouse->packet[4] |
1541 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001542 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001543 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001544 "refusing packet %4ph (suspected interleaved ps/2)\n",
1545 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001546 return PSMOUSE_BAD_DATA;
1547 }
1548
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001549 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001550
1551 /* Continue with the next packet */
1552 psmouse->packet[0] = psmouse->packet[6];
1553 psmouse->pktcnt = 1;
1554
1555 } else {
1556
1557 /*
1558 * High bit is 0 - that means that we indeed got a PS/2
1559 * packet in the middle of ALPS packet.
1560 *
1561 * There is also possibility that we got 6-byte ALPS
1562 * packet followed by 3-byte packet from trackpoint. We
1563 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001564 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001565 * normal operation (user would need to press all
1566 * buttons on the pad and start moving trackpoint
1567 * without touching the pad surface) we assume former.
1568 * Even if we are wrong the wost thing that would happen
1569 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001570 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001571 */
1572
Hans de Goede59c30af2015-04-03 17:14:40 -07001573 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1574 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001575
1576 /*
1577 * Continue with the standard ALPS protocol handling,
1578 * but make sure we won't process it as an interleaved
1579 * packet again, which may happen if all buttons are
1580 * pressed. To avoid this let's reset the 4th bit which
1581 * is normally 1.
1582 */
1583 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1584 psmouse->pktcnt = 4;
1585 }
1586
1587 return PSMOUSE_GOOD_DATA;
1588}
1589
1590static void alps_flush_packet(unsigned long data)
1591{
1592 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001593 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001594
1595 serio_pause_rx(psmouse->ps2dev.serio);
1596
Seth Forsheeb46615f2011-11-07 19:53:30 -08001597 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001598
1599 /*
1600 * We did not any more data in reasonable amount of time.
1601 * Validate the last 3 bytes and process as a standard
1602 * ALPS packet.
1603 */
1604 if ((psmouse->packet[3] |
1605 psmouse->packet[4] |
1606 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001607 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001608 "refusing packet %3ph (suspected interleaved ps/2)\n",
1609 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001610 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001611 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001612 }
1613 psmouse->pktcnt = 0;
1614 }
1615
1616 serio_continue_rx(psmouse->ps2dev.serio);
1617}
1618
1619static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1620{
1621 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001622
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001623 /*
1624 * Check if we are dealing with a bare PS/2 packet, presumably from
1625 * a device connected to the external PS/2 port. Because bare PS/2
1626 * protocol does not have enough constant bits to self-synchronize
1627 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001628 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001629 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001630 if (priv->proto_version != ALPS_PROTO_V8 &&
1631 !psmouse->out_of_sync_cnt &&
1632 (psmouse->packet[0] & 0xc8) == 0x08) {
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001635 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1636 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return PSMOUSE_FULL_PACKET;
1638 }
1639 return PSMOUSE_GOOD_DATA;
1640 }
1641
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001642 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1643
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001644 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001645 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1646 return alps_handle_interleaved_ps2(psmouse);
1647 }
1648
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001649 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001650 psmouse_dbg(psmouse,
1651 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001652 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Seth Forsheeb46615f2011-11-07 19:53:30 -08001656 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001657 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001658 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001659 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001660 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1661 psmouse->pktcnt - 1,
1662 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001663
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001664 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001665 psmouse->pktcnt == psmouse->pktsize) {
1666 /*
1667 * Some Dell boxes, such as Latitude E6440 or E7440
1668 * with closed lid, quite often smash last byte of
1669 * otherwise valid packet with 0xff. Given that the
1670 * next packet is very likely to be valid let's
1671 * report PSMOUSE_FULL_PACKET but not process data,
1672 * rather than reporting PSMOUSE_BAD_DATA and
1673 * filling the logs.
1674 */
1675 return PSMOUSE_FULL_PACKET;
1676 }
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001681 if ((priv->proto_version == ALPS_PROTO_V7 &&
1682 !alps_is_valid_package_v7(psmouse)) ||
1683 (priv->proto_version == ALPS_PROTO_V8 &&
1684 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001685 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1686 psmouse->pktcnt - 1,
1687 psmouse->packet[psmouse->pktcnt - 1]);
1688 return PSMOUSE_BAD_DATA;
1689 }
1690
Seth Forsheeb46615f2011-11-07 19:53:30 -08001691 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001692 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return PSMOUSE_FULL_PACKET;
1694 }
1695
1696 return PSMOUSE_GOOD_DATA;
1697}
1698
Seth Forshee25bded72011-11-07 19:53:36 -08001699static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1700{
1701 struct ps2dev *ps2dev = &psmouse->ps2dev;
1702 struct alps_data *priv = psmouse->private;
1703 int command;
1704 unsigned char *param;
1705 unsigned char dummy[4];
1706
1707 BUG_ON(nibble > 0xf);
1708
1709 command = priv->nibble_commands[nibble].command;
1710 param = (command & 0x0f00) ?
1711 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1712
1713 if (ps2_command(ps2dev, param, command))
1714 return -1;
1715
1716 return 0;
1717}
1718
1719static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1720{
1721 struct ps2dev *ps2dev = &psmouse->ps2dev;
1722 struct alps_data *priv = psmouse->private;
1723 int i, nibble;
1724
1725 if (ps2_command(ps2dev, NULL, priv->addr_command))
1726 return -1;
1727
1728 for (i = 12; i >= 0; i -= 4) {
1729 nibble = (addr >> i) & 0xf;
1730 if (alps_command_mode_send_nibble(psmouse, nibble))
1731 return -1;
1732 }
1733
1734 return 0;
1735}
1736
1737static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1738{
1739 struct ps2dev *ps2dev = &psmouse->ps2dev;
1740 unsigned char param[4];
1741
1742 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1743 return -1;
1744
1745 /*
1746 * The address being read is returned in the first two bytes
1747 * of the result. Check that this address matches the expected
1748 * address.
1749 */
1750 if (addr != ((param[0] << 8) | param[1]))
1751 return -1;
1752
1753 return param[2];
1754}
1755
1756static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1757{
1758 if (alps_command_mode_set_addr(psmouse, addr))
1759 return -1;
1760 return __alps_command_mode_read_reg(psmouse, addr);
1761}
1762
1763static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1764{
1765 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1766 return -1;
1767 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1768 return -1;
1769 return 0;
1770}
1771
1772static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1773 u8 value)
1774{
1775 if (alps_command_mode_set_addr(psmouse, addr))
1776 return -1;
1777 return __alps_command_mode_write_reg(psmouse, value);
1778}
1779
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001780static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1781 int repeated_command, unsigned char *param)
1782{
1783 struct ps2dev *ps2dev = &psmouse->ps2dev;
1784
1785 param[0] = 0;
1786 if (init_command && ps2_command(ps2dev, param, init_command))
1787 return -EIO;
1788
1789 if (ps2_command(ps2dev, NULL, repeated_command) ||
1790 ps2_command(ps2dev, NULL, repeated_command) ||
1791 ps2_command(ps2dev, NULL, repeated_command))
1792 return -EIO;
1793
1794 param[0] = param[1] = param[2] = 0xff;
1795 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1796 return -EIO;
1797
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001798 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1799 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001800 return 0;
1801}
1802
Yunkang Tang38088432014-07-26 13:51:41 -07001803static bool alps_check_valid_firmware_id(unsigned char id[])
1804{
1805 if (id[0] == 0x73)
1806 return true;
1807
1808 if (id[0] == 0x88 &&
1809 (id[1] == 0x07 ||
1810 id[1] == 0x08 ||
1811 (id[1] & 0xf0) == 0xb0 ||
1812 (id[1] & 0xf0) == 0xc0)) {
1813 return true;
1814 }
1815
1816 return false;
1817}
1818
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001819static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001820{
1821 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001822
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001823 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001824 psmouse_err(psmouse, "failed to enter command mode\n");
1825 return -1;
1826 }
1827
Yunkang Tang38088432014-07-26 13:51:41 -07001828 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001829 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001830 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001831 return -1;
1832 }
Seth Forshee25bded72011-11-07 19:53:36 -08001833 return 0;
1834}
1835
1836static inline int alps_exit_command_mode(struct psmouse *psmouse)
1837{
1838 struct ps2dev *ps2dev = &psmouse->ps2dev;
1839 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1840 return -1;
1841 return 0;
1842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844/*
1845 * For DualPoint devices select the device that should respond to
1846 * subsequent commands. It looks like glidepad is behind stickpointer,
1847 * I'd thought it would be other way around...
1848 */
Seth Forshee25bded72011-11-07 19:53:36 -08001849static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1853
1854 if (ps2_command(ps2dev, NULL, cmd) ||
1855 ps2_command(ps2dev, NULL, cmd) ||
1856 ps2_command(ps2dev, NULL, cmd) ||
1857 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1858 return -1;
1859
1860 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001861 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 return 0;
1864}
1865
Seth Forshee25bded72011-11-07 19:53:36 -08001866static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
1868 struct ps2dev *ps2dev = &psmouse->ps2dev;
1869
1870 /* Try ALPS magic knock - 4 disable before enable */
1871 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1872 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1873 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1874 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1875 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1876 return -1;
1877
1878 /*
1879 * Switch mouse to poll (remote) mode so motion data will not
1880 * get in our way
1881 */
Guenter Roeckad568142017-01-21 23:44:46 -08001882 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Yunkang Tang95f75e92013-12-01 22:33:52 -08001885static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1886{
1887 int i, nibble;
1888
1889 /*
1890 * b0-b11 are valid bits, send sequence is inverse.
1891 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1892 */
1893 for (i = 0; i <= 8; i += 4) {
1894 nibble = (word >> i) & 0xf;
1895 if (alps_command_mode_send_nibble(psmouse, nibble))
1896 return -1;
1897 }
1898
1899 return 0;
1900}
1901
1902static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1903 u16 addr, u16 value)
1904{
1905 struct ps2dev *ps2dev = &psmouse->ps2dev;
1906
1907 /* 0x0A0 is the command to write the word */
1908 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1909 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1910 alps_monitor_mode_send_word(psmouse, addr) ||
1911 alps_monitor_mode_send_word(psmouse, value) ||
1912 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1913 return -1;
1914
1915 return 0;
1916}
1917
1918static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1919{
1920 struct ps2dev *ps2dev = &psmouse->ps2dev;
1921
1922 if (enable) {
1923 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1924 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1925 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1926 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1927 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1928 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1929 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1930 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1931 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1932 return -1;
1933 } else {
1934 /* EC to exit monitor mode */
1935 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1936 return -1;
1937 }
1938
1939 return 0;
1940}
1941
1942static int alps_absolute_mode_v6(struct psmouse *psmouse)
1943{
1944 u16 reg_val = 0x181;
1945 int ret = -1;
1946
1947 /* enter monitor mode, to write the register */
1948 if (alps_monitor_mode(psmouse, true))
1949 return -1;
1950
1951 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1952
1953 if (alps_monitor_mode(psmouse, false))
1954 ret = -1;
1955
1956 return ret;
1957}
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959static int alps_get_status(struct psmouse *psmouse, char *param)
1960{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001962 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return -1;
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return 0;
1966}
1967
1968/*
1969 * Turn touchpad tapping on or off. The sequences are:
1970 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1971 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1972 * My guess that 0xE9 (GetInfo) is here as a sync point.
1973 * For models that also have stickpointer (DualPoints) its tapping
1974 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1975 * we don't fiddle with it.
1976 */
1977static int alps_tap_mode(struct psmouse *psmouse, int enable)
1978{
1979 struct ps2dev *ps2dev = &psmouse->ps2dev;
1980 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1981 unsigned char tap_arg = enable ? 0x0A : 0x00;
1982 unsigned char param[4];
1983
1984 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1985 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1986 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1987 ps2_command(ps2dev, &tap_arg, cmd))
1988 return -1;
1989
1990 if (alps_get_status(psmouse, param))
1991 return -1;
1992
1993 return 0;
1994}
1995
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001996/*
1997 * alps_poll() - poll the touchpad for current motion packet.
1998 * Used in resync.
1999 */
2000static int alps_poll(struct psmouse *psmouse)
2001{
2002 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08002003 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002004 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002005
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002006 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08002007 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002008
2009 poll_failed = ps2_command(&psmouse->ps2dev, buf,
2010 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
2011
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002012 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08002013 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002014
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002015 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002016 return -1;
2017
2018 if ((psmouse->badbyte & 0xc8) == 0x08) {
2019/*
2020 * Poll the track stick ...
2021 */
2022 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2023 return -1;
2024 }
2025
2026 memcpy(psmouse->packet, buf, sizeof(buf));
2027 return 0;
2028}
2029
Seth Forshee25bded72011-11-07 19:53:36 -08002030static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002034 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002035 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002039 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002040 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Seth Forshee25bded72011-11-07 19:53:36 -08002044 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002045 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return -1;
2047 }
2048
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002049 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002050 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002054 /* ALPS needs stream mode, otherwise it won't report any data */
2055 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002056 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002057 return -1;
2058 }
2059
2060 return 0;
2061}
2062
Yunkang Tang95f75e92013-12-01 22:33:52 -08002063static int alps_hw_init_v6(struct psmouse *psmouse)
2064{
2065 unsigned char param[2] = {0xC8, 0x14};
2066
2067 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2068 if (alps_passthrough_mode_v2(psmouse, true))
2069 return -1;
2070
2071 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2072 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2073 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2074 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2075 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2076 return -1;
2077
2078 if (alps_passthrough_mode_v2(psmouse, false))
2079 return -1;
2080
2081 if (alps_absolute_mode_v6(psmouse)) {
2082 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2083 return -1;
2084 }
2085
2086 return 0;
2087}
2088
Seth Forshee25bded72011-11-07 19:53:36 -08002089/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08002090 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08002091 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08002092static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2093 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08002094{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002095 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08002096
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002097 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002098 return -1;
2099
Kevin Cernekeecd401202013-02-13 22:28:07 -08002100 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2101 if (reg_val == -1)
2102 goto error;
2103
Seth Forshee25bded72011-11-07 19:53:36 -08002104 if (enable)
2105 reg_val |= 0x01;
2106 else
2107 reg_val &= ~0x01;
2108
Kevin Cernekeecd401202013-02-13 22:28:07 -08002109 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08002110
Kevin Cernekeecd401202013-02-13 22:28:07 -08002111error:
2112 if (alps_exit_command_mode(psmouse))
2113 ret = -1;
2114 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08002115}
2116
2117/* Must be in command mode when calling this function */
2118static int alps_absolute_mode_v3(struct psmouse *psmouse)
2119{
2120 int reg_val;
2121
2122 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2123 if (reg_val == -1)
2124 return -1;
2125
2126 reg_val |= 0x06;
2127 if (__alps_command_mode_write_reg(psmouse, reg_val))
2128 return -1;
2129
2130 return 0;
2131}
2132
Pali Rohárdae928e2016-01-06 15:50:08 -08002133static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
Kevin Cernekeecd401202013-02-13 22:28:07 -08002134{
2135 int ret = -EIO, reg_val;
2136
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002137 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002138 goto error;
2139
2140 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2141 if (reg_val == -1)
2142 goto error;
2143
2144 /* bit 7: trackstick is present */
2145 ret = reg_val & 0x80 ? 0 : -ENODEV;
2146
2147error:
2148 alps_exit_command_mode(psmouse);
2149 return ret;
2150}
2151
2152static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2153{
2154 struct ps2dev *ps2dev = &psmouse->ps2dev;
2155 int ret = 0;
2156 unsigned char param[4];
2157
2158 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2159 return -EIO;
2160
2161 /*
2162 * E7 report for the trackstick
2163 *
2164 * There have been reports of failures to seem to trace back
2165 * to the above trackstick check failing. When these occur
2166 * this E7 report fails, so when that happens we continue
2167 * with the assumption that there isn't a trackstick after
2168 * all.
2169 */
2170 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002171 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002172 ret = -ENODEV;
2173 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002174 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002175
2176 /*
2177 * Not sure what this does, but it is absolutely
2178 * essential. Without it, the touchpad does not
2179 * work at all and the trackstick just emits normal
2180 * PS/2 packets.
2181 */
2182 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2183 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2184 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2185 alps_command_mode_send_nibble(psmouse, 0x9) ||
2186 alps_command_mode_send_nibble(psmouse, 0x4)) {
2187 psmouse_err(psmouse,
2188 "Error sending magic E6 sequence\n");
2189 ret = -EIO;
2190 goto error;
2191 }
2192
2193 /*
2194 * This ensures the trackstick packets are in the format
2195 * supported by this driver. If bit 1 isn't set the packet
2196 * format is different.
2197 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002198 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002199 alps_command_mode_write_reg(psmouse,
2200 reg_base + 0x08, 0x82) ||
2201 alps_exit_command_mode(psmouse))
2202 ret = -EIO;
2203 }
2204
2205error:
2206 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2207 ret = -EIO;
2208
2209 return ret;
2210}
2211
Seth Forshee25bded72011-11-07 19:53:36 -08002212static int alps_hw_init_v3(struct psmouse *psmouse)
2213{
Pali Rohár4b1af852016-01-06 15:58:56 -08002214 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -08002215 struct ps2dev *ps2dev = &psmouse->ps2dev;
2216 int reg_val;
2217 unsigned char param[4];
2218
Pali Rohár4b1af852016-01-06 15:58:56 -08002219 if ((priv->flags & ALPS_DUALPOINT) &&
Kevin Cernekeecd401202013-02-13 22:28:07 -08002220 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002221 goto error;
2222
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002223 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002224 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002225 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2226 goto error;
2227 }
2228
2229 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2230 if (reg_val == -1)
2231 goto error;
2232 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2233 goto error;
2234
2235 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2236 if (reg_val == -1)
2237 goto error;
2238 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2239 goto error;
2240
2241 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2242 goto error;
2243 if (__alps_command_mode_write_reg(psmouse, 0x04))
2244 goto error;
2245
2246 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2247 goto error;
2248 if (__alps_command_mode_write_reg(psmouse, 0x03))
2249 goto error;
2250
2251 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2252 goto error;
2253 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2254 goto error;
2255
2256 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2257 goto error;
2258 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2259 goto error;
2260
Seth Forshee25bded72011-11-07 19:53:36 -08002261 alps_exit_command_mode(psmouse);
2262
2263 /* Set rate and enable data reporting */
2264 param[0] = 0x64;
2265 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2266 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2267 psmouse_err(psmouse, "Failed to enable data reporting\n");
2268 return -1;
2269 }
2270
2271 return 0;
2272
Seth Forshee25bded72011-11-07 19:53:36 -08002273error:
2274 /*
2275 * Leaving the touchpad in command mode will essentially render
2276 * it unusable until the machine reboots, so exit it here just
2277 * to be safe
2278 */
2279 alps_exit_command_mode(psmouse);
2280 return -1;
2281}
2282
Hans de Goedef3f33c62014-07-29 11:22:07 -07002283static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2284{
2285 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2286 struct alps_data *priv = psmouse->private;
2287
2288 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2289 if (reg < 0)
2290 return reg;
2291
2292 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2293 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2294
2295 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2296 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2297
2298 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2299 if (reg < 0)
2300 return reg;
2301
2302 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2303 x_electrode = 17 + x_electrode;
2304
2305 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2306 y_electrode = 13 + y_electrode;
2307
2308 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2309 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2310
2311 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2312 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2313
2314 psmouse_dbg(psmouse,
2315 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2316 x_pitch, y_pitch, x_electrode, y_electrode,
2317 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2318
2319 return 0;
2320}
2321
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002322static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2323{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002324 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002325 struct ps2dev *ps2dev = &psmouse->ps2dev;
2326 int reg_val, ret = -1;
2327
Kevin Cernekeecd401202013-02-13 22:28:07 -08002328 if (priv->flags & ALPS_DUALPOINT) {
2329 reg_val = alps_setup_trackstick_v3(psmouse,
2330 ALPS_REG_BASE_RUSHMORE);
2331 if (reg_val == -EIO)
2332 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002333 }
2334
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002335 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002336 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2337 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2338 goto error;
2339
Hans de Goedef3f33c62014-07-29 11:22:07 -07002340 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2341 goto error;
2342
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002343 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2344 if (reg_val == -1)
2345 goto error;
2346 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2347 goto error;
2348
2349 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2350 goto error;
2351
2352 /* enter absolute mode */
2353 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2354 if (reg_val == -1)
2355 goto error;
2356 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2357 goto error;
2358
2359 alps_exit_command_mode(psmouse);
2360 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2361
2362error:
2363 alps_exit_command_mode(psmouse);
2364 return ret;
2365}
2366
Seth Forshee25bded72011-11-07 19:53:36 -08002367/* Must be in command mode when calling this function */
2368static int alps_absolute_mode_v4(struct psmouse *psmouse)
2369{
2370 int reg_val;
2371
2372 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2373 if (reg_val == -1)
2374 return -1;
2375
2376 reg_val |= 0x02;
2377 if (__alps_command_mode_write_reg(psmouse, reg_val))
2378 return -1;
2379
2380 return 0;
2381}
2382
2383static int alps_hw_init_v4(struct psmouse *psmouse)
2384{
Seth Forshee25bded72011-11-07 19:53:36 -08002385 struct ps2dev *ps2dev = &psmouse->ps2dev;
2386 unsigned char param[4];
2387
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002388 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002389 goto error;
2390
2391 if (alps_absolute_mode_v4(psmouse)) {
2392 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2393 goto error;
2394 }
2395
2396 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2397 goto error;
2398
2399 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2400 goto error;
2401
2402 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2403 goto error;
2404
2405 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2406 goto error;
2407
2408 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2409 goto error;
2410
2411 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2412 goto error;
2413
2414 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2415 goto error;
2416
2417 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2418 goto error;
2419
2420 alps_exit_command_mode(psmouse);
2421
2422 /*
2423 * This sequence changes the output from a 9-byte to an
2424 * 8-byte format. All the same data seems to be present,
2425 * just in a more compact format.
2426 */
2427 param[0] = 0xc8;
2428 param[1] = 0x64;
2429 param[2] = 0x50;
2430 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2431 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2432 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2433 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2434 return -1;
2435
2436 /* Set rate and enable data reporting */
2437 param[0] = 0x64;
2438 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2439 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2440 psmouse_err(psmouse, "Failed to enable data reporting\n");
2441 return -1;
2442 }
2443
2444 return 0;
2445
2446error:
2447 /*
2448 * Leaving the touchpad in command mode will essentially render
2449 * it unusable until the machine reboots, so exit it here just
2450 * to be safe
2451 */
2452 alps_exit_command_mode(psmouse);
2453 return -1;
2454}
2455
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002456static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2457 unsigned char index, unsigned char otp[])
2458{
2459 struct ps2dev *ps2dev = &psmouse->ps2dev;
2460
2461 switch (index) {
2462 case 0:
2463 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2464 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2465 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2466 return -1;
2467
2468 break;
2469
2470 case 1:
2471 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2472 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2473 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2474 return -1;
2475
2476 break;
2477 }
2478
2479 return 0;
2480}
2481
Fengguang Wu07f19e32015-04-10 23:54:31 -07002482static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2483 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002484{
2485 int num_x_electrode;
2486 int num_y_electrode;
2487 int x_pitch, y_pitch, x_phys, y_phys;
2488
Masaki Otae7348392017-03-17 14:10:57 -07002489 if (IS_SS4PLUS_DEV(priv->dev_id)) {
2490 num_x_electrode =
2491 SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2492 num_y_electrode =
2493 SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002494
Masaki Otae7348392017-03-17 14:10:57 -07002495 priv->x_max =
2496 (num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2497 priv->y_max =
2498 (num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002499
Masaki Otae7348392017-03-17 14:10:57 -07002500 x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2501 y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2502
2503 } else {
2504 num_x_electrode =
2505 SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2506 num_y_electrode =
2507 SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2508
2509 priv->x_max =
2510 (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2511 priv->y_max =
2512 (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2513
2514 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2515 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2516 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002517
2518 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2519 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2520
2521 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2522 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2523
2524 return 0;
2525}
2526
Fengguang Wu07f19e32015-04-10 23:54:31 -07002527static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2528 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002529{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002530 unsigned char is_btnless;
2531
Masaki Otae7348392017-03-17 14:10:57 -07002532 if (IS_SS4PLUS_DEV(priv->dev_id))
2533 is_btnless = (otp[1][0] >> 1) & 0x01;
2534 else
2535 is_btnless = (otp[1][1] >> 3) & 0x01;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002536
2537 if (is_btnless)
2538 priv->flags |= ALPS_BUTTONPAD;
2539
2540 return 0;
2541}
2542
Masaki Otae7348392017-03-17 14:10:57 -07002543static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2544 struct alps_data *priv)
2545{
2546 bool is_dual = false;
2547
2548 if (IS_SS4PLUS_DEV(priv->dev_id))
2549 is_dual = (otp[0][0] >> 4) & 0x01;
2550
2551 if (is_dual)
2552 priv->flags |= ALPS_DUALPOINT |
2553 ALPS_DUALPOINT_WITH_PRESSURE;
2554
2555 return 0;
2556}
2557
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002558static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2559 struct alps_data *priv)
2560{
2561 unsigned char otp[2][4];
2562
2563 memset(otp, 0, sizeof(otp));
2564
Masaki Ota4a646582017-08-24 15:44:36 -07002565 if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
2566 alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002567 return -1;
2568
2569 alps_update_device_area_ss4_v2(otp, priv);
2570
2571 alps_update_btn_info_ss4_v2(otp, priv);
2572
Masaki Otae7348392017-03-17 14:10:57 -07002573 alps_update_dual_info_ss4_v2(otp, priv);
2574
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002575 return 0;
2576}
2577
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002578static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2579 struct alps_data *priv)
2580{
2581 struct ps2dev *ps2dev = &psmouse->ps2dev;
2582 unsigned char param[4] = {0};
2583 int num_x_electrode, num_y_electrode;
2584
2585 if (alps_enter_command_mode(psmouse))
2586 return -1;
2587
2588 param[0] = 0x0a;
2589 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2590 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2591 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2592 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2593 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2594 return -1;
2595
2596 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2597 return -1;
2598
2599 /*
2600 * Dolphin's sensor line number is not fixed. It can be calculated
2601 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2602 * Further more, we can get device's x_max and y_max by multiplying
2603 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2604 *
2605 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2606 * real sensor line number X = 11 + 8 = 19, and
2607 * real sensor line number Y = 8 + 1 = 9.
2608 * So, x_max = (19 - 1) * 64 = 1152, and
2609 * y_max = (9 - 1) * 64 = 512.
2610 */
2611 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2612 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2613 priv->x_bits = num_x_electrode;
2614 priv->y_bits = num_y_electrode;
2615 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2616 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2617
2618 if (alps_exit_command_mode(psmouse))
2619 return -1;
2620
2621 return 0;
2622}
2623
Dave Turvene75af9e52013-02-21 22:58:28 -08002624static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2625{
2626 struct ps2dev *ps2dev = &psmouse->ps2dev;
2627 unsigned char param[2];
2628
2629 /* This is dolphin "v1" as empirically defined by florin9doi */
2630 param[0] = 0x64;
2631 param[1] = 0x28;
2632
2633 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2634 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2635 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2636 return -1;
2637
2638 return 0;
2639}
2640
Yunkang Tang38088432014-07-26 13:51:41 -07002641static int alps_hw_init_v7(struct psmouse *psmouse)
2642{
2643 struct ps2dev *ps2dev = &psmouse->ps2dev;
2644 int reg_val, ret = -1;
2645
2646 if (alps_enter_command_mode(psmouse) ||
2647 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2648 goto error;
2649
Hans de Goedef3f33c62014-07-29 11:22:07 -07002650 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2651 goto error;
2652
Yunkang Tang38088432014-07-26 13:51:41 -07002653 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2654 goto error;
2655
2656 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2657 if (reg_val == -1)
2658 goto error;
2659 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2660 goto error;
2661
2662 alps_exit_command_mode(psmouse);
2663 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2664
2665error:
2666 alps_exit_command_mode(psmouse);
2667 return ret;
2668}
2669
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002670static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2671{
2672 struct ps2dev *ps2dev = &psmouse->ps2dev;
2673 char param[2] = {0x64, 0x28};
2674 int ret = -1;
2675
2676 /* enter absolute mode */
2677 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2678 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2679 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2680 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2681 goto error;
2682 }
2683
2684 /* T.B.D. Decread noise packet number, delete in the future */
2685 if (alps_exit_command_mode(psmouse) ||
2686 alps_enter_command_mode(psmouse) ||
2687 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2688 goto error;
2689 }
2690 alps_exit_command_mode(psmouse);
2691
2692 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2693
2694error:
2695 alps_exit_command_mode(psmouse);
2696 return ret;
2697}
2698
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002699static int alps_set_protocol(struct psmouse *psmouse,
2700 struct alps_data *priv,
2701 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002702{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002703 psmouse->private = priv;
2704
2705 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2706
2707 priv->proto_version = protocol->version;
2708 priv->byte0 = protocol->byte0;
2709 priv->mask0 = protocol->mask0;
2710 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002711
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002712 priv->x_max = 2000;
2713 priv->y_max = 1400;
2714 priv->x_bits = 15;
2715 priv->y_bits = 11;
2716
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002717 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002718 case ALPS_PROTO_V1:
2719 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002720 priv->hw_init = alps_hw_init_v1_v2;
2721 priv->process_packet = alps_process_packet_v1_v2;
2722 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002723 priv->x_max = 1023;
2724 priv->y_max = 767;
Hans de Goede19556212015-10-26 01:50:28 -07002725 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2726 priv->flags |= ALPS_STICK_BITS;
Seth Forshee25bded72011-11-07 19:53:36 -08002727 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002728
Seth Forshee25bded72011-11-07 19:53:36 -08002729 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002730 priv->hw_init = alps_hw_init_v3;
2731 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002732 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002733 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002734 priv->nibble_commands = alps_v3_nibble_commands;
2735 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Pali Rohár4b1af852016-01-06 15:58:56 -08002736
2737 if (alps_probe_trackstick_v3_v7(psmouse,
2738 ALPS_REG_BASE_PINNACLE) < 0)
2739 priv->flags &= ~ALPS_DUALPOINT;
2740
Seth Forshee25bded72011-11-07 19:53:36 -08002741 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002742
2743 case ALPS_PROTO_V3_RUSHMORE:
2744 priv->hw_init = alps_hw_init_rushmore_v3;
2745 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002746 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002747 priv->decode_fields = alps_decode_rushmore;
2748 priv->nibble_commands = alps_v3_nibble_commands;
2749 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2750 priv->x_bits = 16;
2751 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002752
Pali Rohárdae928e2016-01-06 15:50:08 -08002753 if (alps_probe_trackstick_v3_v7(psmouse,
2754 ALPS_REG_BASE_RUSHMORE) < 0)
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002755 priv->flags &= ~ALPS_DUALPOINT;
2756
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002757 break;
2758
Seth Forshee25bded72011-11-07 19:53:36 -08002759 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002760 priv->hw_init = alps_hw_init_v4;
2761 priv->process_packet = alps_process_packet_v4;
Hans de Goede688ea362015-05-20 14:42:50 -07002762 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002763 priv->nibble_commands = alps_v4_nibble_commands;
2764 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002765 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002766
Dave Turvene75af9e52013-02-21 22:58:28 -08002767 case ALPS_PROTO_V5:
2768 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002769 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002770 priv->decode_fields = alps_decode_dolphin;
Hans de Goede688ea362015-05-20 14:42:50 -07002771 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dave Turvene75af9e52013-02-21 22:58:28 -08002772 priv->nibble_commands = alps_v3_nibble_commands;
2773 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002774 priv->x_bits = 23;
2775 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002776
2777 if (alps_dolphin_get_device_area(psmouse, priv))
2778 return -EIO;
2779
Dave Turvene75af9e52013-02-21 22:58:28 -08002780 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002781
Yunkang Tang95f75e92013-12-01 22:33:52 -08002782 case ALPS_PROTO_V6:
2783 priv->hw_init = alps_hw_init_v6;
2784 priv->process_packet = alps_process_packet_v6;
2785 priv->set_abs_params = alps_set_abs_params_st;
2786 priv->nibble_commands = alps_v6_nibble_commands;
2787 priv->x_max = 2047;
2788 priv->y_max = 1535;
2789 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002790
Yunkang Tang38088432014-07-26 13:51:41 -07002791 case ALPS_PROTO_V7:
2792 priv->hw_init = alps_hw_init_v7;
2793 priv->process_packet = alps_process_packet_v7;
2794 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002795 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002796 priv->nibble_commands = alps_v3_nibble_commands;
2797 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002798 priv->x_max = 0xfff;
2799 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002800
2801 if (priv->fw_ver[1] != 0xba)
2802 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002803
Pali Rohárdae928e2016-01-06 15:50:08 -08002804 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2805 priv->flags &= ~ALPS_DUALPOINT;
2806
Yunkang Tang38088432014-07-26 13:51:41 -07002807 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002808
2809 case ALPS_PROTO_V8:
2810 priv->hw_init = alps_hw_init_ss4_v2;
2811 priv->process_packet = alps_process_packet_ss4_v2;
2812 priv->decode_fields = alps_decode_ss4_v2;
2813 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2814 priv->nibble_commands = alps_v3_nibble_commands;
2815 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2816
2817 if (alps_set_defaults_ss4_v2(psmouse, priv))
2818 return -EIO;
2819
2820 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002821 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002822
2823 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002824}
2825
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002826static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2827 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002828{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002829 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002830 int i;
2831
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002832 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2833 model = &alps_model_data[i];
2834
Pali Rohára3cbfd52017-03-07 09:43:35 -08002835 if (!memcmp(e7, model->signature, sizeof(model->signature)))
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002836 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002837 }
2838
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002839 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002840}
2841
2842static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2843{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002844 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002845 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002846 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002847
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002848 /*
2849 * First try "E6 report".
2850 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2851 * The bits 0-2 of the first byte will be 1s if some buttons are
2852 * pressed.
2853 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002854 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2855 PSMOUSE_CMD_SETSCALE11, e6))
2856 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002857
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002858 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2859 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002860
2861 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002862 * Now get the "E7" and "EC" reports. These will uniquely identify
2863 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002864 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002865 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2866 PSMOUSE_CMD_SETSCALE21, e7) ||
2867 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2868 PSMOUSE_CMD_RESET_WRAP, ec) ||
2869 alps_exit_command_mode(psmouse))
2870 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002871
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002872 protocol = alps_match_table(e7, ec);
2873 if (!protocol) {
Pali Rohár09c398b2017-03-07 09:41:34 -08002874 if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
2875 ec[2] == 0x8a) {
2876 protocol = &alps_v4_protocol_data;
2877 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002878 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2879 protocol = &alps_v5_protocol_data;
2880 } else if (ec[0] == 0x88 &&
2881 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2882 protocol = &alps_v7_protocol_data;
2883 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2884 protocol = &alps_v3_rushmore_data;
2885 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2886 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2887 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002888 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
Masaki Otae7348392017-03-17 14:10:57 -07002889 (e7[2] == 0x14 || e7[2] == 0x28)) {
Ben Gamariaeaa8812016-10-04 11:43:30 -07002890 protocol = &alps_v8_protocol_data;
Pali Rohárc9815232017-03-07 09:42:46 -08002891 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2892 protocol = &alps_v9_protocol_data;
2893 psmouse_warn(psmouse,
2894 "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2895 e7, ec);
2896 return -EINVAL;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002897 } else {
2898 psmouse_dbg(psmouse,
2899 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2900 return -EINVAL;
2901 }
2902 }
2903
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002904 if (priv) {
Masaki Otae7348392017-03-17 14:10:57 -07002905 /* Save Device ID and Firmware version */
2906 memcpy(priv->dev_id, e7, 3);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002907 memcpy(priv->fw_ver, ec, 3);
2908 error = alps_set_protocol(psmouse, priv, protocol);
2909 if (error)
2910 return error;
2911 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002912
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002913 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002914}
2915
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002916static int alps_reconnect(struct psmouse *psmouse)
2917{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002918 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002919
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002920 psmouse_reset(psmouse);
2921
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002922 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002923 return -1;
2924
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002925 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
2928static void alps_disconnect(struct psmouse *psmouse)
2929{
2930 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002933 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002934 if (priv->dev2)
2935 input_unregister_device(priv->dev2);
2936 if (!IS_ERR_OR_NULL(priv->dev3))
2937 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 kfree(priv);
2939}
2940
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002941static void alps_set_abs_params_st(struct alps_data *priv,
2942 struct input_dev *dev1)
2943{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002944 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2945 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002946 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002947}
2948
Masaki Ota8eccd392015-03-27 20:44:21 -07002949static void alps_set_abs_params_mt_common(struct alps_data *priv,
2950 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002951{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002952 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2953 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002954
Hans de Goedef3f33c62014-07-29 11:22:07 -07002955 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2956 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2957
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002958 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2959 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002960}
Yunkang Tang38088432014-07-26 13:51:41 -07002961
Hans de Goede688ea362015-05-20 14:42:50 -07002962static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2963 struct input_dev *dev1)
Masaki Ota8eccd392015-03-27 20:44:21 -07002964{
2965 alps_set_abs_params_mt_common(priv, dev1);
2966 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2967
2968 input_mt_init_slots(dev1, MAX_TOUCHES,
2969 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
Hans de Goede1662c0332015-05-20 14:42:15 -07002970 INPUT_MT_SEMI_MT);
Masaki Ota8eccd392015-03-27 20:44:21 -07002971}
2972
2973static void alps_set_abs_params_v7(struct alps_data *priv,
2974 struct input_dev *dev1)
2975{
2976 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002977 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002978
2979 input_mt_init_slots(dev1, MAX_TOUCHES,
2980 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2981 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002982
2983 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2984}
2985
2986static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2987 struct input_dev *dev1)
2988{
2989 alps_set_abs_params_mt_common(priv, dev1);
2990 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2991 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2992
2993 input_mt_init_slots(dev1, MAX_TOUCHES,
2994 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2995 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002996}
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998int alps_init(struct psmouse *psmouse)
2999{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003000 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08003001 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003002 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003004 error = priv->hw_init(psmouse);
3005 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 goto init_fail;
3007
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08003008 /*
3009 * Undo part of setup done for us by psmouse core since touchpad
3010 * is not a relative device.
3011 */
3012 __clear_bit(EV_REL, dev1->evbit);
3013 __clear_bit(REL_X, dev1->relbit);
3014 __clear_bit(REL_Y, dev1->relbit);
3015
3016 /*
3017 * Now set up our capabilities.
3018 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07003019 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
3020 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
3021 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08003022 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
3023 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Jiri Slaby7b19ada2007-10-18 23:40:32 -07003025 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08003026
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08003027 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003029 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07003030 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
3031 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003034 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07003035 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
3036 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
3038
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003039 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08003040 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
3041 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
3042 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
3043 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07003044 } else if (priv->flags & ALPS_BUTTONPAD) {
3045 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
3046 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08003047 } else {
3048 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
3049 }
3050
Pali Rohárdfba8602015-01-09 12:48:44 -08003051 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08003052 struct input_dev *dev2;
3053
3054 dev2 = input_allocate_device();
3055 if (!dev2) {
3056 psmouse_err(psmouse,
3057 "failed to allocate trackstick device\n");
3058 error = -ENOMEM;
3059 goto init_fail;
3060 }
3061
3062 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3063 psmouse->ps2dev.serio->phys);
3064 dev2->phys = priv->phys2;
3065
Pali Rohárdfba8602015-01-09 12:48:44 -08003066 /*
3067 * format of input device name is: "protocol vendor name"
3068 * see function psmouse_switch_protocol() in psmouse-base.c
3069 */
3070 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08003071
3072 dev2->id.bustype = BUS_I8042;
3073 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08003074 dev2->id.product = PSMOUSE_ALPS;
3075 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08003076 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08003077
Pali Rohár04aae282015-01-14 13:18:30 -08003078 input_set_capability(dev2, EV_REL, REL_X);
3079 input_set_capability(dev2, EV_REL, REL_Y);
Ben Gamari7ad8a102016-10-04 11:41:58 -07003080 if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3081 input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3082 input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3083 }
Pali Rohár04aae282015-01-14 13:18:30 -08003084 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3085 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3086 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003087
Pali Rohár04aae282015-01-14 13:18:30 -08003088 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07003089 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3090
Pali Rohár04aae282015-01-14 13:18:30 -08003091 error = input_register_device(dev2);
3092 if (error) {
3093 psmouse_err(psmouse,
3094 "failed to register trackstick device: %d\n",
3095 error);
3096 input_free_device(dev2);
3097 goto init_fail;
3098 }
3099
3100 priv->dev2 = dev2;
3101 }
3102
3103 priv->psmouse = psmouse;
3104
3105 INIT_DELAYED_WORK(&priv->dev3_register_work,
3106 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003109 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 psmouse->disconnect = alps_disconnect;
3111 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003112 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003114 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3115 psmouse->resync_time = 0;
3116
Pali Rohár9d720b32014-11-08 12:58:57 -08003117 /* Allow 2 invalid packets without resetting device */
3118 psmouse->resetafter = psmouse->pktsize * 2;
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return 0;
3121
3122init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04003123 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003124 /*
3125 * Even though we did not allocate psmouse->private we do free
3126 * it here.
3127 */
3128 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04003129 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003130 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07003133int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003135 struct alps_data *priv;
3136 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003138 error = alps_identify(psmouse, NULL);
3139 if (error)
3140 return error;
3141
3142 /*
3143 * Reset the device to make sure it is fully operational:
3144 * on some laptops, like certain Dell Latitudes, we may
3145 * fail to properly detect presence of trackstick if device
3146 * has not been reset.
3147 */
3148 psmouse_reset(psmouse);
3149
3150 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3151 if (!priv)
3152 return -ENOMEM;
3153
3154 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003155 if (error) {
3156 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003157 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 if (set_properties) {
3161 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003162 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003163 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003164 psmouse->model = priv->proto_version;
3165 } else {
3166 /*
3167 * Destroy alps_data structure we allocated earlier since
3168 * this was just a "trial run". Otherwise we'll keep it
3169 * to be used by alps_init() which has to be called if
3170 * we succeed and set_properties is true.
3171 */
3172 kfree(priv);
3173 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 return 0;
3177}
3178