blob: a8a96def0ba21addc2fe9c115c8f3cd496c895ad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
Hans de Goede073e5702015-08-03 14:06:24 -070023#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "psmouse.h"
26#include "alps.h"
27
Seth Forshee25bded72011-11-07 19:53:36 -080028/*
29 * Definitions for ALPS version 3 and 4 command mode protocol
30 */
31#define ALPS_CMD_NIBBLE_10 0x01f2
32
Kevin Cernekeecd401202013-02-13 22:28:07 -080033#define ALPS_REG_BASE_RUSHMORE 0xc2c0
Pali Rohárdae928e2016-01-06 15:50:08 -080034#define ALPS_REG_BASE_V7 0xc2c0
Kevin Cernekeecd401202013-02-13 22:28:07 -080035#define ALPS_REG_BASE_PINNACLE 0x0000
36
Seth Forshee25bded72011-11-07 19:53:36 -080037static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
38 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
39 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
40 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
41 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
42 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
43 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
44 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
45 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
46 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
47 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
48 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
49 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
50 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
51 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
52 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
53 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
54};
55
56static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
57 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
58 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
59 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
60 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
61 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
62 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
63 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
64 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
65 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
66 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
67 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
68 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
69 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
70 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
71 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
72 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
73};
74
Yunkang Tang95f75e92013-12-01 22:33:52 -080075static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
76 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
77 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
78 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
79 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
80 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
81 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
82 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
83 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
84 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
85 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
86 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
87 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
88 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
89 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
90 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
91 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
92};
93
Seth Forshee25bded72011-11-07 19:53:36 -080094
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080095#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
96#define ALPS_PASS 0x04 /* device has a pass-through port */
97
98#define ALPS_WHEEL 0x08 /* hardware wheel present */
99#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
100#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
101#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800102#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
103 6-byte ALPS packet */
Hans de Goede19556212015-10-26 01:50:28 -0700104#define ALPS_STICK_BITS 0x100 /* separate stick button bits */
Yunkang Tang38088432014-07-26 13:51:41 -0700105#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Ben Gamari7ad8a102016-10-04 11:41:58 -0700106#define ALPS_DUALPOINT_WITH_PRESSURE 0x400 /* device can report trackpoint pressure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Helge Dellere38de672006-09-10 21:54:39 -0400108static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800109 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
110 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
111 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
113 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
114 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
115 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
117 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
118 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
119 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
120 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
121 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
122 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
123 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800124
125 /*
126 * XXX This entry is suspicious. First byte has zero lower nibble,
127 * which is what a normal mouse would report. Also, the value 0x0e
128 * isn't valid per PS/2 spec.
129 */
130 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
131
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800132 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
133 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800134 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800135 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
137 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
138 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
139 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
140 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
141 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800144static const struct alps_protocol_info alps_v3_protocol_data = {
145 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
146};
147
148static const struct alps_protocol_info alps_v3_rushmore_data = {
149 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
150};
151
152static const struct alps_protocol_info alps_v5_protocol_data = {
153 ALPS_PROTO_V5, 0xc8, 0xd8, 0
154};
155
156static const struct alps_protocol_info alps_v7_protocol_data = {
157 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
158};
159
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700160static const struct alps_protocol_info alps_v8_protocol_data = {
161 ALPS_PROTO_V8, 0x18, 0x18, 0
162};
163
Hans de Goede19556212015-10-26 01:50:28 -0700164/*
165 * Some v2 models report the stick buttons in separate bits
166 */
167static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
168#if defined(CONFIG_DMI) && defined(CONFIG_X86)
169 {
170 /* Extrapolated from other entries */
171 .matches = {
172 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
174 },
175 },
176 {
177 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
178 .matches = {
179 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
180 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
181 },
182 },
183 {
184 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
185 .matches = {
186 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
187 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
188 },
189 },
190 {
191 /* Extrapolated from other entries */
192 .matches = {
193 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
194 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
195 },
196 },
197#endif
198 { }
199};
200
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800201static void alps_set_abs_params_st(struct alps_data *priv,
202 struct input_dev *dev1);
Hans de Goede688ea362015-05-20 14:42:50 -0700203static void alps_set_abs_params_semi_mt(struct alps_data *priv,
204 struct input_dev *dev1);
Masaki Ota8eccd392015-03-27 20:44:21 -0700205static void alps_set_abs_params_v7(struct alps_data *priv,
206 struct input_dev *dev1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700207static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
208 struct input_dev *dev1);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800209
Seth Forsheed4b347b2011-11-07 19:53:15 -0800210/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800212static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800213 unsigned char data)
214{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800215 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800216}
217
Pali Rohár04aae282015-01-14 13:18:30 -0800218static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800219 int left, int right, int middle)
220{
Martin Buckc91ed052010-03-13 22:23:58 -0800221 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800222
Martin Buckc91ed052010-03-13 22:23:58 -0800223 /*
224 * If shared button has already been reported on the
225 * other device (dev2) then this event should be also
226 * sent through that device.
227 */
Pali Rohár04aae282015-01-14 13:18:30 -0800228 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800229 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800230
Pali Rohár04aae282015-01-14 13:18:30 -0800231 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800232 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800233
Pali Rohár04aae282015-01-14 13:18:30 -0800234 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800235 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800236
Martin Buckc91ed052010-03-13 22:23:58 -0800237 /*
238 * Sync the _other_ device now, we'll do the first
239 * device later once we report the rest of the events.
240 */
Pali Rohár04aae282015-01-14 13:18:30 -0800241 if (dev2)
242 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800243}
244
Seth Forshee25bded72011-11-07 19:53:36 -0800245static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct alps_data *priv = psmouse->private;
248 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500249 struct input_dev *dev = psmouse->dev;
250 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500252 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800254 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400255 left = packet[2] & 0x10;
256 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 middle = 0;
258 x = packet[1] | ((packet[0] & 0x07) << 7);
259 y = packet[4] | ((packet[3] & 0x07) << 7);
260 z = packet[5];
261 } else {
262 left = packet[3] & 1;
263 right = packet[3] & 2;
264 middle = packet[3] & 4;
265 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
266 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
267 z = packet[5];
268 }
269
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800270 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400271 back = packet[0] & 0x10;
272 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500273 }
274
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800275 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500276 back = packet[3] & 4;
277 forward = packet[2] & 4;
278 if ((middle = forward && back))
279 forward = back = 0;
280 }
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 ges = packet[2] & 1;
283 fin = packet[2] & 2;
284
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800285 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
287 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700288
Pali Rohár04aae282015-01-14 13:18:30 -0800289 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 input_sync(dev2);
292 return;
293 }
294
Hans de Goede19556212015-10-26 01:50:28 -0700295 /* Some models have separate stick button bits */
296 if (priv->flags & ALPS_STICK_BITS) {
Hans de Goede92bac832015-04-12 15:42:35 -0700297 left |= packet[0] & 1;
298 right |= packet[0] & 2;
299 middle |= packet[0] & 4;
300 }
301
Pali Rohár04aae282015-01-14 13:18:30 -0800302 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800305 if (ges && !fin)
306 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /*
309 * A "tap and drag" operation is reported by the hardware as a transition
310 * from (!fin && ges) to (fin && ges). This should be translated to the
311 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
312 */
313 if (ges && fin && !priv->prev_fin) {
314 input_report_abs(dev, ABS_X, x);
315 input_report_abs(dev, ABS_Y, y);
316 input_report_abs(dev, ABS_PRESSURE, 0);
317 input_report_key(dev, BTN_TOOL_FINGER, 0);
318 input_sync(dev);
319 }
320 priv->prev_fin = fin;
321
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800322 if (z > 30)
323 input_report_key(dev, BTN_TOUCH, 1);
324 if (z < 25)
325 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (z > 0) {
328 input_report_abs(dev, ABS_X, x);
329 input_report_abs(dev, ABS_Y, y);
330 }
331
332 input_report_abs(dev, ABS_PRESSURE, z);
333 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
334
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800335 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500336 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800338 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500339 input_report_key(dev, BTN_FORWARD, forward);
340 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800343 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800344 input_report_key(dev, BTN_0, packet[2] & 4);
345 input_report_key(dev, BTN_1, packet[0] & 0x10);
346 input_report_key(dev, BTN_2, packet[3] & 4);
347 input_report_key(dev, BTN_3, packet[0] & 0x20);
348 }
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 input_sync(dev);
351}
352
Hans de Goede036e6c72014-07-25 22:38:51 -0700353static void alps_get_bitmap_points(unsigned int map,
354 struct alps_bitmap_point *low,
355 struct alps_bitmap_point *high,
356 int *fingers)
357{
358 struct alps_bitmap_point *point;
359 int i, bit, prev_bit = 0;
360
361 point = low;
362 for (i = 0; map != 0; i++, map >>= 1) {
363 bit = map & 1;
364 if (bit) {
365 if (!prev_bit) {
366 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700367 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700368 (*fingers)++;
369 }
370 point->num_bits++;
371 } else {
372 if (prev_bit)
373 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700374 }
375 prev_bit = bit;
376 }
377}
378
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800379/*
Hans de Goededccf1dd2015-05-20 14:43:09 -0700380 * Process bitmap data from semi-mt protocols. Returns the number of
Seth Forshee01ce6612011-11-07 19:54:13 -0800381 * fingers detected. A return value of 0 means at least one of the
382 * bitmaps was empty.
383 *
384 * The bitmaps don't have enough data to track fingers, so this function
385 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700386 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800387 * is greater than 0.
388 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800389static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700390 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800391{
Hans de Goede4dd26572015-05-20 14:41:10 -0700392 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
Seth Forshee01ce6612011-11-07 19:54:13 -0800393 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
394 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Hans de Goede4dd26572015-05-20 14:41:10 -0700395 struct input_mt_pos corner[4];
Seth Forshee01ce6612011-11-07 19:54:13 -0800396
Hans de Goede02d04252014-07-25 22:43:35 -0700397 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800398 return 0;
399
Hans de Goede02d04252014-07-25 22:43:35 -0700400 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
401 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800402
403 /*
404 * Fingers can overlap, so we use the maximum count of fingers
405 * on either axis as the finger count.
406 */
407 fingers = max(fingers_x, fingers_y);
408
409 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700410 * If an axis reports only a single contact, we have overlapping or
411 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800412 */
Hans de Goede20bea682014-07-25 22:33:33 -0700413 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700414 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700415 x_low.num_bits = x_low.num_bits - i;
416 x_high.start_bit = x_low.start_bit + i;
417 x_high.num_bits = max(i, 1);
418 }
419 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700420 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700421 y_low.num_bits = y_low.num_bits - i;
422 y_high.start_bit = y_low.start_bit + i;
423 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800424 }
425
Hans de Goede4dd26572015-05-20 14:41:10 -0700426 /* top-left corner */
427 corner[0].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700428 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
429 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700430 corner[0].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700431 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
432 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800433
Hans de Goede4dd26572015-05-20 14:41:10 -0700434 /* top-right corner */
435 corner[1].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700436 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
437 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700438 corner[1].y =
439 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
440 (2 * (priv->y_bits - 1));
441
442 /* bottom-right corner */
443 corner[2].x =
444 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
445 (2 * (priv->x_bits - 1));
446 corner[2].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700447 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
448 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800449
Hans de Goede4dd26572015-05-20 14:41:10 -0700450 /* bottom-left corner */
451 corner[3].x =
452 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
453 (2 * (priv->x_bits - 1));
454 corner[3].y =
Seth Forshee01ce6612011-11-07 19:54:13 -0800455 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
456 (2 * (priv->y_bits - 1));
457
Hans de Goededccf1dd2015-05-20 14:43:09 -0700458 /* x-bitmap order is reversed on v5 touchpads */
459 if (priv->proto_version == ALPS_PROTO_V5) {
460 for (i = 0; i < 4; i++)
461 corner[i].x = priv->x_max - corner[i].x;
Hans de Goede40e8f532014-07-25 22:37:15 -0700462 }
463
Hans de Goededccf1dd2015-05-20 14:43:09 -0700464 /* y-bitmap order is reversed on v3 and v4 touchpads */
465 if (priv->proto_version == ALPS_PROTO_V3 ||
466 priv->proto_version == ALPS_PROTO_V4) {
Hans de Goede4dd26572015-05-20 14:41:10 -0700467 for (i = 0; i < 4; i++)
468 corner[i].y = priv->y_max - corner[i].y;
Seth Forshee01ce6612011-11-07 19:54:13 -0800469 }
470
Hans de Goede4dd26572015-05-20 14:41:10 -0700471 /*
472 * We only select a corner for the second touch once per 2 finger
473 * touch sequence to avoid the chosen corner (and thus the coordinates)
474 * jumping around when the first touch is in the middle.
475 */
476 if (priv->second_touch == -1) {
477 /* Find corner closest to our st coordinates */
478 closest = 0x7fffffff;
479 for (i = 0; i < 4; i++) {
480 int dx = fields->st.x - corner[i].x;
481 int dy = fields->st.y - corner[i].y;
482 int distance = dx * dx + dy * dy;
483
484 if (distance < closest) {
485 priv->second_touch = i;
486 closest = distance;
487 }
488 }
489 /* And select the opposite corner to use for the 2nd touch */
490 priv->second_touch = (priv->second_touch + 2) % 4;
491 }
492
493 fields->mt[0] = fields->st;
494 fields->mt[1] = corner[priv->second_touch];
495
Seth Forshee01ce6612011-11-07 19:54:13 -0800496 return fingers;
497}
498
Hans de Goedecdf333e2014-07-25 22:44:42 -0700499static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800500{
501 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700502 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
503 input_report_abs(dev, ABS_MT_POSITION_X, x);
504 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800505}
506
Hans de Goedecdf333e2014-07-25 22:44:42 -0700507static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800508{
Hans de Goede02d04252014-07-25 22:43:35 -0700509 struct alps_data *priv = psmouse->private;
510 struct input_dev *dev = psmouse->dev;
511 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700512 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700513
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800514 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700515 for (i = 0; i < n; i++)
516 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
517
518 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800519}
520
Hans de Goede68c21872014-07-25 22:47:25 -0700521static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
522{
523 struct alps_data *priv = psmouse->private;
524 struct input_dev *dev = psmouse->dev;
525 struct alps_fields *f = &priv->f;
526
527 /* Use st data when we don't have mt data */
528 if (fingers < 2) {
529 f->mt[0].x = f->st.x;
530 f->mt[0].y = f->st.y;
531 fingers = f->pressure > 0 ? 1 : 0;
Hans de Goede4dd26572015-05-20 14:41:10 -0700532 priv->second_touch = -1;
Hans de Goede68c21872014-07-25 22:47:25 -0700533 }
534
Hans de Goede1662c0332015-05-20 14:42:15 -0700535 if (fingers >= 1)
536 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
537 if (fingers >= 2)
538 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
539 input_mt_sync_frame(dev);
Hans de Goede68c21872014-07-25 22:47:25 -0700540
541 input_mt_report_finger_count(dev, fingers);
542
543 input_report_key(dev, BTN_LEFT, f->left);
544 input_report_key(dev, BTN_RIGHT, f->right);
545 input_report_key(dev, BTN_MIDDLE, f->middle);
546
547 input_report_abs(dev, ABS_PRESSURE, f->pressure);
548
549 input_sync(dev);
550}
551
Seth Forshee25bded72011-11-07 19:53:36 -0800552static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
553{
554 struct alps_data *priv = psmouse->private;
555 unsigned char *packet = psmouse->packet;
556 struct input_dev *dev = priv->dev2;
557 int x, y, z, left, right, middle;
558
Pali Rohár34412ba2015-01-09 12:48:58 -0800559 /* It should be a DualPoint when received trackstick packet */
560 if (!(priv->flags & ALPS_DUALPOINT)) {
561 psmouse_warn(psmouse,
562 "Rejected trackstick packet from non DualPoint device");
563 return;
564 }
565
Seth Forshee25bded72011-11-07 19:53:36 -0800566 /* Sanity check packet */
567 if (!(packet[0] & 0x40)) {
568 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
569 return;
570 }
571
572 /*
573 * There's a special packet that seems to indicate the end
574 * of a stream of trackstick data. Filter these out.
575 */
576 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
577 return;
578
579 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
580 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
581 z = (packet[4] & 0x7c) >> 2;
582
583 /*
584 * The x and y values tend to be quite large, and when used
585 * alone the trackstick is difficult to use. Scale them down
586 * to compensate.
587 */
588 x /= 8;
589 y /= 8;
590
591 input_report_rel(dev, REL_X, x);
592 input_report_rel(dev, REL_Y, -y);
593
594 /*
595 * Most ALPS models report the trackstick buttons in the touchpad
596 * packets, but a few report them here. No reliable way has been
597 * found to differentiate between the models upfront, so we enable
598 * the quirk in response to seeing a button press in the trackstick
599 * packet.
600 */
601 left = packet[3] & 0x01;
602 right = packet[3] & 0x02;
603 middle = packet[3] & 0x04;
604
605 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
606 (left || right || middle))
607 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
608
609 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
610 input_report_key(dev, BTN_LEFT, left);
611 input_report_key(dev, BTN_RIGHT, right);
612 input_report_key(dev, BTN_MIDDLE, middle);
613 }
614
615 input_sync(dev);
616 return;
617}
618
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800619static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
620{
621 f->left = !!(p[3] & 0x01);
622 f->right = !!(p[3] & 0x02);
623 f->middle = !!(p[3] & 0x04);
624
625 f->ts_left = !!(p[3] & 0x10);
626 f->ts_right = !!(p[3] & 0x20);
627 f->ts_middle = !!(p[3] & 0x40);
628}
629
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700630static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800631 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800632{
633 f->first_mp = !!(p[4] & 0x40);
634 f->is_mp = !!(p[0] & 0x40);
635
Hans de Goedea839cd52015-05-20 14:39:21 -0700636 if (f->is_mp) {
637 f->fingers = (p[5] & 0x3) + 1;
638 f->x_map = ((p[4] & 0x7e) << 8) |
639 ((p[1] & 0x7f) << 2) |
640 ((p[0] & 0x30) >> 4);
641 f->y_map = ((p[3] & 0x70) << 4) |
642 ((p[2] & 0x7f) << 1) |
643 (p[4] & 0x01);
644 } else {
645 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
646 ((p[0] & 0x30) >> 4);
647 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
648 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800649
Hans de Goedea839cd52015-05-20 14:39:21 -0700650 alps_decode_buttons_v3(f, p);
651 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700652
653 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800654}
655
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700656static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800657 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800658{
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700659 f->first_mp = !!(p[4] & 0x40);
Yunkang Tangf105e342014-07-25 22:29:24 -0700660 f->is_mp = !!(p[5] & 0x40);
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700661
Hans de Goedea839cd52015-05-20 14:39:21 -0700662 if (f->is_mp) {
663 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
664 f->x_map = ((p[5] & 0x10) << 11) |
665 ((p[4] & 0x7e) << 8) |
666 ((p[1] & 0x7f) << 2) |
667 ((p[0] & 0x30) >> 4);
668 f->y_map = ((p[5] & 0x20) << 6) |
669 ((p[3] & 0x70) << 4) |
670 ((p[2] & 0x7f) << 1) |
671 (p[4] & 0x01);
672 } else {
673 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
674 ((p[0] & 0x30) >> 4);
675 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
676 f->pressure = p[5] & 0x7f;
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700677
Hans de Goedea839cd52015-05-20 14:39:21 -0700678 alps_decode_buttons_v3(f, p);
679 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700680
681 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800682}
683
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700684static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800685 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800686{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800687 u64 palm_data = 0;
688 struct alps_data *priv = psmouse->private;
689
Dave Turvene75af9e52013-02-21 22:58:28 -0800690 f->first_mp = !!(p[0] & 0x02);
691 f->is_mp = !!(p[0] & 0x20);
692
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800693 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700694 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
695 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
696 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800697 alps_decode_buttons_v3(f, p);
698 } else {
699 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800700 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800701
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800702 palm_data = (p[1] & 0x7f) |
703 ((p[2] & 0x7f) << 7) |
704 ((p[4] & 0x7f) << 14) |
705 ((p[5] & 0x7f) << 21) |
706 ((p[3] & 0x07) << 28) |
707 (((u64)p[3] & 0x70) << 27) |
708 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800709
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800710 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
711 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
712
713 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
714 f->x_map = (palm_data >> priv->y_bits) &
715 (BIT(priv->x_bits) - 1);
716 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700717
718 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800719}
720
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800721static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800722{
723 struct alps_data *priv = psmouse->private;
724 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800725 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700726 struct alps_fields *f = &priv->f;
727 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800728
Hans de Goede02d04252014-07-25 22:43:35 -0700729 memset(f, 0, sizeof(*f));
730
731 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800732
733 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800734 * There's no single feature of touchpad position and bitmap packets
735 * that can be used to distinguish between them. We rely on the fact
736 * that a bitmap packet should always follow a position packet with
737 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800738 */
739 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800740 /*
741 * Sometimes a position packet will indicate a multi-packet
742 * sequence, but then what follows is another position
743 * packet. Check for this, and when it happens process the
744 * position packet as usual.
745 */
Hans de Goede02d04252014-07-25 22:43:35 -0700746 if (f->is_mp) {
747 fingers = f->fingers;
Hans de Goede44b77f32015-05-20 14:40:06 -0700748 /*
749 * Bitmap processing uses position packet's coordinate
750 * data, so we need to do decode it first.
751 */
752 priv->decode_fields(f, priv->multi_data, psmouse);
Hans de Goededccf1dd2015-05-20 14:43:09 -0700753 if (alps_process_bitmap(priv, f) == 0)
754 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800755 } else {
756 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800757 }
758 }
759
Seth Forshee01ce6612011-11-07 19:54:13 -0800760 /*
761 * Bit 6 of byte 0 is not usually set in position packets. The only
762 * times it seems to be set is in situations where the data is
763 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
764 * this combined with the fact that this bit is useful for filtering
765 * out misidentified bitmap packets, we reject anything with this
766 * bit set.
767 */
Hans de Goede02d04252014-07-25 22:43:35 -0700768 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800769 return;
770
Hans de Goede02d04252014-07-25 22:43:35 -0700771 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800772 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800773 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
774 return;
775 }
776
777 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800778
Seth Forshee25bded72011-11-07 19:53:36 -0800779 /*
780 * Sometimes the hardware sends a single packet with z = 0
781 * in the middle of a stream. Real releases generate packets
782 * with x, y, and z all zero, so these seem to be flukes.
783 * Ignore them.
784 */
Hans de Goede02d04252014-07-25 22:43:35 -0700785 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800786 return;
787
Hans de Goede68c21872014-07-25 22:47:25 -0700788 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800789
Pali Rohár34412ba2015-01-09 12:48:58 -0800790 if ((priv->flags & ALPS_DUALPOINT) &&
791 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700792 input_report_key(dev2, BTN_LEFT, f->ts_left);
793 input_report_key(dev2, BTN_RIGHT, f->ts_right);
794 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800795 input_sync(dev2);
796 }
797}
798
799static void alps_process_packet_v3(struct psmouse *psmouse)
800{
801 unsigned char *packet = psmouse->packet;
802
803 /*
804 * v3 protocol packets come in three types, two representing
805 * touchpad data and one representing trackstick data.
806 * Trackstick packets seem to be distinguished by always
807 * having 0x3f in the last byte. This value has never been
808 * observed in the last byte of either of the other types
809 * of packets.
810 */
811 if (packet[5] == 0x3f) {
812 alps_process_trackstick_packet_v3(psmouse);
813 return;
814 }
815
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800816 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800817}
818
Yunkang Tang95f75e92013-12-01 22:33:52 -0800819static void alps_process_packet_v6(struct psmouse *psmouse)
820{
821 struct alps_data *priv = psmouse->private;
822 unsigned char *packet = psmouse->packet;
823 struct input_dev *dev = psmouse->dev;
824 struct input_dev *dev2 = priv->dev2;
825 int x, y, z, left, right, middle;
826
827 /*
828 * We can use Byte5 to distinguish if the packet is from Touchpad
829 * or Trackpoint.
830 * Touchpad: 0 - 0x7E
831 * Trackpoint: 0x7F
832 */
833 if (packet[5] == 0x7F) {
834 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800835 if (!(priv->flags & ALPS_DUALPOINT)) {
836 psmouse_warn(psmouse,
837 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800838 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800839 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800840
841 /* Trackpoint packet */
842 x = packet[1] | ((packet[3] & 0x20) << 2);
843 y = packet[2] | ((packet[3] & 0x40) << 1);
844 z = packet[4];
845 left = packet[3] & 0x01;
846 right = packet[3] & 0x02;
847 middle = packet[3] & 0x04;
848
849 /* To prevent the cursor jump when finger lifted */
850 if (x == 0x7F && y == 0x7F && z == 0x7F)
851 x = y = z = 0;
852
853 /* Divide 4 since trackpoint's speed is too fast */
854 input_report_rel(dev2, REL_X, (char)x / 4);
855 input_report_rel(dev2, REL_Y, -((char)y / 4));
856
857 input_report_key(dev2, BTN_LEFT, left);
858 input_report_key(dev2, BTN_RIGHT, right);
859 input_report_key(dev2, BTN_MIDDLE, middle);
860
861 input_sync(dev2);
862 return;
863 }
864
865 /* Touchpad packet */
866 x = packet[1] | ((packet[3] & 0x78) << 4);
867 y = packet[2] | ((packet[4] & 0x78) << 4);
868 z = packet[5];
869 left = packet[3] & 0x01;
870 right = packet[3] & 0x02;
871
872 if (z > 30)
873 input_report_key(dev, BTN_TOUCH, 1);
874 if (z < 25)
875 input_report_key(dev, BTN_TOUCH, 0);
876
877 if (z > 0) {
878 input_report_abs(dev, ABS_X, x);
879 input_report_abs(dev, ABS_Y, y);
880 }
881
882 input_report_abs(dev, ABS_PRESSURE, z);
883 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
884
885 /* v6 touchpad does not have middle button */
886 input_report_key(dev, BTN_LEFT, left);
887 input_report_key(dev, BTN_RIGHT, right);
888
889 input_sync(dev);
890}
891
Seth Forshee25bded72011-11-07 19:53:36 -0800892static void alps_process_packet_v4(struct psmouse *psmouse)
893{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700894 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800895 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700896 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700897 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700898
899 /*
900 * v4 has a 6-byte encoding for bitmap data, but this data is
901 * broken up between 3 normal packets. Use priv->multi_packet to
902 * track our position in the bitmap packet.
903 */
904 if (packet[6] & 0x40) {
905 /* sync, reset position */
906 priv->multi_packet = 0;
907 }
908
909 if (WARN_ON_ONCE(priv->multi_packet > 2))
910 return;
911
912 offset = 2 * priv->multi_packet;
913 priv->multi_data[offset] = packet[6];
914 priv->multi_data[offset + 1] = packet[7];
915
Hans de Goede44b77f32015-05-20 14:40:06 -0700916 f->left = !!(packet[4] & 0x01);
917 f->right = !!(packet[4] & 0x02);
918
919 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
920 ((packet[0] & 0x30) >> 4);
921 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
922 f->pressure = packet[5] & 0x7f;
923
George Pantalos3b7e09f2012-05-10 22:31:59 -0700924 if (++priv->multi_packet > 2) {
925 priv->multi_packet = 0;
926
Hans de Goede02d04252014-07-25 22:43:35 -0700927 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700928 ((priv->multi_data[3] & 0x60) << 3) |
929 ((priv->multi_data[0] & 0x3f) << 2) |
930 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700931 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700932 ((priv->multi_data[3] & 0x1f) << 5) |
933 (priv->multi_data[1] & 0x1f);
934
Hans de Goede02d04252014-07-25 22:43:35 -0700935 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700936 }
Seth Forshee25bded72011-11-07 19:53:36 -0800937
Hans de Goede68c21872014-07-25 22:47:25 -0700938 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800939}
940
Yunkang Tang38088432014-07-26 13:51:41 -0700941static bool alps_is_valid_package_v7(struct psmouse *psmouse)
942{
943 switch (psmouse->pktcnt) {
944 case 3:
945 return (psmouse->packet[2] & 0x40) == 0x40;
946 case 4:
947 return (psmouse->packet[3] & 0x48) == 0x48;
948 case 6:
949 return (psmouse->packet[5] & 0x40) == 0x00;
950 }
951 return true;
952}
953
954static unsigned char alps_get_packet_id_v7(char *byte)
955{
956 unsigned char packet_id;
957
958 if (byte[4] & 0x40)
959 packet_id = V7_PACKET_ID_TWO;
960 else if (byte[4] & 0x01)
961 packet_id = V7_PACKET_ID_MULTI;
962 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
963 packet_id = V7_PACKET_ID_NEW;
964 else if (byte[1] == 0x00 && byte[4] == 0x00)
965 packet_id = V7_PACKET_ID_IDLE;
966 else
967 packet_id = V7_PACKET_ID_UNKNOWN;
968
969 return packet_id;
970}
971
972static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
973 unsigned char *pkt,
974 unsigned char pkt_id)
975{
976 mt[0].x = ((pkt[2] & 0x80) << 4);
977 mt[0].x |= ((pkt[2] & 0x3F) << 5);
978 mt[0].x |= ((pkt[3] & 0x30) >> 1);
979 mt[0].x |= (pkt[3] & 0x07);
980 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
981
982 mt[1].x = ((pkt[3] & 0x80) << 4);
983 mt[1].x |= ((pkt[4] & 0x80) << 3);
984 mt[1].x |= ((pkt[4] & 0x3F) << 4);
985 mt[1].y = ((pkt[5] & 0x80) << 3);
986 mt[1].y |= ((pkt[5] & 0x3F) << 4);
987
988 switch (pkt_id) {
989 case V7_PACKET_ID_TWO:
990 mt[1].x &= ~0x000F;
991 mt[1].y |= 0x000F;
Hans de Goede72eceab2015-05-20 14:37:41 -0700992 /* Detect false-postive touches where x & y report max value */
993 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
994 mt[1].x = 0;
995 /* y gets set to 0 at the end of this function */
996 }
Yunkang Tang38088432014-07-26 13:51:41 -0700997 break;
998
999 case V7_PACKET_ID_MULTI:
1000 mt[1].x &= ~0x003F;
1001 mt[1].y &= ~0x0020;
1002 mt[1].y |= ((pkt[4] & 0x02) << 4);
1003 mt[1].y |= 0x001F;
1004 break;
1005
1006 case V7_PACKET_ID_NEW:
1007 mt[1].x &= ~0x003F;
1008 mt[1].x |= (pkt[0] & 0x20);
1009 mt[1].y |= 0x000F;
1010 break;
1011 }
1012
1013 mt[0].y = 0x7FF - mt[0].y;
1014 mt[1].y = 0x7FF - mt[1].y;
1015}
1016
1017static int alps_get_mt_count(struct input_mt_pos *mt)
1018{
Hans de Goede7091c442014-12-18 09:53:34 -08001019 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -07001020
Hans de Goede7091c442014-12-18 09:53:34 -08001021 for (i = 0; i < MAX_TOUCHES; i++) {
1022 if (mt[i].x != 0 || mt[i].y != 0)
1023 fingers++;
1024 }
Yunkang Tang38088432014-07-26 13:51:41 -07001025
Hans de Goede7091c442014-12-18 09:53:34 -08001026 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -07001027}
1028
1029static int alps_decode_packet_v7(struct alps_fields *f,
1030 unsigned char *p,
1031 struct psmouse *psmouse)
1032{
Hans de Goeded27eb792014-12-18 09:55:14 -08001033 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -07001034 unsigned char pkt_id;
1035
1036 pkt_id = alps_get_packet_id_v7(p);
1037 if (pkt_id == V7_PACKET_ID_IDLE)
1038 return 0;
1039 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1040 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -08001041 /*
1042 * NEW packets are send to indicate a discontinuity in the finger
1043 * coordinate reporting. Specifically a finger may have moved from
1044 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1045 * us.
1046 *
1047 * NEW packets have 3 problems:
1048 * 1) They do not contain middle / right button info (on non clickpads)
1049 * this can be worked around by preserving the old button state
1050 * 2) They do not contain an accurate fingercount, and they are
1051 * typically send when the number of fingers changes. We cannot use
1052 * the old finger count as that may mismatch with the amount of
1053 * touch coordinates we've available in the NEW packet
1054 * 3) Their x data for the second touch is inaccurate leading to
1055 * a possible jump of the x coordinate by 16 units when the first
1056 * non NEW packet comes in
1057 * Since problems 2 & 3 cannot be worked around, just ignore them.
1058 */
1059 if (pkt_id == V7_PACKET_ID_NEW)
1060 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001061
1062 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1063
Yunkang Tang38088432014-07-26 13:51:41 -07001064 if (pkt_id == V7_PACKET_ID_TWO)
1065 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001066 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001067 f->fingers = 3 + (p[5] & 0x03);
1068
Hans de Goeded27eb792014-12-18 09:55:14 -08001069 f->left = (p[0] & 0x80) >> 7;
1070 if (priv->flags & ALPS_BUTTONPAD) {
1071 if (p[0] & 0x20)
1072 f->fingers++;
1073 if (p[0] & 0x10)
1074 f->fingers++;
1075 } else {
1076 f->right = (p[0] & 0x20) >> 5;
1077 f->middle = (p[0] & 0x10) >> 4;
1078 }
1079
Hans de Goede7091c442014-12-18 09:53:34 -08001080 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1081 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1082 f->mt[0].x = f->mt[1].x;
1083 f->mt[0].y = f->mt[1].y;
1084 f->mt[1].x = 0;
1085 f->mt[1].y = 0;
1086 }
1087
Yunkang Tang38088432014-07-26 13:51:41 -07001088 return 0;
1089}
1090
1091static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1092{
1093 struct alps_data *priv = psmouse->private;
1094 unsigned char *packet = psmouse->packet;
1095 struct input_dev *dev2 = priv->dev2;
1096 int x, y, z, left, right, middle;
1097
Pali Rohár34412ba2015-01-09 12:48:58 -08001098 /* It should be a DualPoint when received trackstick packet */
1099 if (!(priv->flags & ALPS_DUALPOINT)) {
1100 psmouse_warn(psmouse,
1101 "Rejected trackstick packet from non DualPoint device");
1102 return;
1103 }
1104
Yunkang Tang38088432014-07-26 13:51:41 -07001105 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1106 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1107 ((packet[3] & 0x20) << 1);
1108 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1109
1110 left = (packet[1] & 0x01);
1111 right = (packet[1] & 0x02) >> 1;
1112 middle = (packet[1] & 0x04) >> 2;
1113
Hans de Goede088df2c2015-06-04 22:31:43 -07001114 input_report_rel(dev2, REL_X, (char)x);
1115 input_report_rel(dev2, REL_Y, -((char)y));
Yunkang Tang38088432014-07-26 13:51:41 -07001116
1117 input_report_key(dev2, BTN_LEFT, left);
1118 input_report_key(dev2, BTN_RIGHT, right);
1119 input_report_key(dev2, BTN_MIDDLE, middle);
1120
1121 input_sync(dev2);
1122}
1123
1124static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1125{
1126 struct alps_data *priv = psmouse->private;
1127 struct input_dev *dev = psmouse->dev;
1128 struct alps_fields *f = &priv->f;
1129
1130 memset(f, 0, sizeof(*f));
1131
1132 if (priv->decode_fields(f, psmouse->packet, psmouse))
1133 return;
1134
1135 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1136
1137 input_mt_report_finger_count(dev, f->fingers);
1138
1139 input_report_key(dev, BTN_LEFT, f->left);
1140 input_report_key(dev, BTN_RIGHT, f->right);
1141 input_report_key(dev, BTN_MIDDLE, f->middle);
1142
1143 input_sync(dev);
1144}
1145
1146static void alps_process_packet_v7(struct psmouse *psmouse)
1147{
1148 unsigned char *packet = psmouse->packet;
1149
1150 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1151 alps_process_trackstick_packet_v7(psmouse);
1152 else
1153 alps_process_touchpad_packet_v7(psmouse);
1154}
1155
Fengguang Wu07f19e32015-04-10 23:54:31 -07001156static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001157{
1158 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1159
Ben Gamari4777ac22016-10-04 11:33:25 -07001160 switch (byte[3] & 0x30) {
1161 case 0x00:
1162 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1163 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 &&
1164 byte[5] == 0x00) {
1165 pkt_id = SS4_PACKET_ID_IDLE;
1166 } else {
1167 pkt_id = SS4_PACKET_ID_ONE;
1168 }
1169 break;
1170 case 0x10:
1171 /* two-finger finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001172 pkt_id = SS4_PACKET_ID_TWO;
Ben Gamari4777ac22016-10-04 11:33:25 -07001173 break;
1174 case 0x20:
1175 /* stick pointer */
1176 pkt_id = SS4_PACKET_ID_STICK;
1177 break;
1178 case 0x30:
1179 /* third and fourth finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001180 pkt_id = SS4_PACKET_ID_MULTI;
Ben Gamari4777ac22016-10-04 11:33:25 -07001181 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001182 }
1183
1184 return pkt_id;
1185}
1186
1187static int alps_decode_ss4_v2(struct alps_fields *f,
1188 unsigned char *p, struct psmouse *psmouse)
1189{
1190 struct alps_data *priv = psmouse->private;
1191 unsigned char pkt_id;
1192 unsigned int no_data_x, no_data_y;
1193
1194 pkt_id = alps_get_pkt_id_ss4_v2(p);
1195
1196 /* Current packet is 1Finger coordinate packet */
1197 switch (pkt_id) {
1198 case SS4_PACKET_ID_ONE:
1199 f->mt[0].x = SS4_1F_X_V2(p);
1200 f->mt[0].y = SS4_1F_Y_V2(p);
1201 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
Ben Gamaria8317762016-10-04 11:40:44 -07001202 /*
1203 * When a button is held the device will give us events
1204 * with x, y, and pressure of 0. This causes annoying jumps
1205 * if a touch is released while the button is held.
1206 * Handle this by claiming zero contacts.
1207 */
1208 f->fingers = f->pressure > 0 ? 1 : 0;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001209 f->first_mp = 0;
1210 f->is_mp = 0;
1211 break;
1212
1213 case SS4_PACKET_ID_TWO:
1214 if (priv->flags & ALPS_BUTTONPAD) {
Masaki Ota963068b2017-08-24 15:44:36 -07001215 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1216 f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1217 f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1218 } else {
1219 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1220 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1221 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001222 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001223 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1224 } else {
Masaki Ota963068b2017-08-24 15:44:36 -07001225 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1226 f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1227 f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1228 } else {
1229 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1230 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1231 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001232 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001233 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1234 }
1235 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1236
1237 if (SS4_IS_MF_CONTINUE(p)) {
1238 f->first_mp = 1;
1239 } else {
1240 f->fingers = 2;
1241 f->first_mp = 0;
1242 }
1243 f->is_mp = 0;
1244
1245 break;
1246
1247 case SS4_PACKET_ID_MULTI:
1248 if (priv->flags & ALPS_BUTTONPAD) {
Masaki Ota963068b2017-08-24 15:44:36 -07001249 if (IS_SS4PLUS_DEV(priv->dev_id)) {
Nir Perry607b86e2018-01-11 23:43:26 -08001250 f->mt[2].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1251 f->mt[3].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1252 no_data_x = SS4_PLUS_MFPACKET_NO_AX_BL;
Masaki Ota963068b2017-08-24 15:44:36 -07001253 } else {
1254 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1255 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
Nir Perry607b86e2018-01-11 23:43:26 -08001256 no_data_x = SS4_MFPACKET_NO_AX_BL;
Masaki Ota963068b2017-08-24 15:44:36 -07001257 }
Nir Perry607b86e2018-01-11 23:43:26 -08001258 no_data_y = SS4_MFPACKET_NO_AY_BL;
Masaki Ota963068b2017-08-24 15:44:36 -07001259
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);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001262 } else {
Masaki Ota963068b2017-08-24 15:44:36 -07001263 if (IS_SS4PLUS_DEV(priv->dev_id)) {
Nir Perry607b86e2018-01-11 23:43:26 -08001264 f->mt[2].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1265 f->mt[3].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1266 no_data_x = SS4_PLUS_MFPACKET_NO_AX;
Masaki Ota963068b2017-08-24 15:44:36 -07001267 } else {
Nir Perry607b86e2018-01-11 23:43:26 -08001268 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1269 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1270 no_data_x = SS4_MFPACKET_NO_AX;
Masaki Ota963068b2017-08-24 15:44:36 -07001271 }
Nir Perry607b86e2018-01-11 23:43:26 -08001272 no_data_y = SS4_MFPACKET_NO_AY;
1273
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001274 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001275 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001276 }
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:
1294 if (!(priv->flags & ALPS_DUALPOINT)) {
1295 psmouse_warn(psmouse,
1296 "Rejected trackstick packet from non DualPoint device");
1297 } else {
1298 int x = (s8)(((p[0] & 1) << 7) | (p[1] & 0x7f));
1299 int y = (s8)(((p[3] & 1) << 7) | (p[2] & 0x7f));
Ben Gamari7ad8a102016-10-04 11:41:58 -07001300 int pressure = (s8)(p[4] & 0x7f);
Ben Gamari4777ac22016-10-04 11:33:25 -07001301
1302 input_report_rel(priv->dev2, REL_X, x);
1303 input_report_rel(priv->dev2, REL_Y, -y);
Ben Gamari7ad8a102016-10-04 11:41:58 -07001304 input_report_abs(priv->dev2, ABS_PRESSURE, pressure);
Ben Gamari4777ac22016-10-04 11:33:25 -07001305 }
1306 break;
1307
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001308 case SS4_PACKET_ID_IDLE:
1309 default:
1310 memset(f, 0, sizeof(struct alps_fields));
1311 break;
1312 }
1313
Ben Gamari4777ac22016-10-04 11:33:25 -07001314 /* handle buttons */
1315 if (pkt_id == SS4_PACKET_ID_STICK) {
1316 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
Masaki Ota4ccef6b2017-03-17 14:19:40 -07001317 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1318 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
Ben Gamari4777ac22016-10-04 11:33:25 -07001319 } else {
1320 f->left = !!(SS4_BTN_V2(p) & 0x01);
1321 if (!(priv->flags & ALPS_BUTTONPAD)) {
1322 f->right = !!(SS4_BTN_V2(p) & 0x02);
1323 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1324 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001325 }
1326
1327 return 0;
1328}
1329
1330static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1331{
1332 struct alps_data *priv = psmouse->private;
1333 unsigned char *packet = psmouse->packet;
1334 struct input_dev *dev = psmouse->dev;
Ben Gamari4777ac22016-10-04 11:33:25 -07001335 struct input_dev *dev2 = priv->dev2;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001336 struct alps_fields *f = &priv->f;
1337
1338 memset(f, 0, sizeof(struct alps_fields));
1339 priv->decode_fields(f, packet, psmouse);
1340 if (priv->multi_packet) {
1341 /*
1342 * Sometimes the first packet will indicate a multi-packet
1343 * sequence, but sometimes the next multi-packet would not
1344 * come. Check for this, and when it happens process the
1345 * position packet as usual.
1346 */
1347 if (f->is_mp) {
1348 /* Now process the 1st packet */
1349 priv->decode_fields(f, priv->multi_data, psmouse);
1350 } else {
1351 priv->multi_packet = 0;
1352 }
1353 }
1354
1355 /*
1356 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1357 * When it is set, it means 2nd packet comes without 1st packet come.
1358 */
1359 if (f->is_mp)
1360 return;
1361
1362 /* Save the first packet */
1363 if (!priv->multi_packet && f->first_mp) {
1364 priv->multi_packet = 1;
1365 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1366 return;
1367 }
1368
1369 priv->multi_packet = 0;
1370
Paul Donohue6d9b544d2016-11-28 20:11:25 -08001371 /* Report trackstick */
1372 if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
1373 if (priv->flags & ALPS_DUALPOINT) {
1374 input_report_key(dev2, BTN_LEFT, f->ts_left);
1375 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1376 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1377 input_sync(dev2);
1378 }
1379 return;
1380 }
1381
1382 /* Report touchpad */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001383 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1384
1385 input_mt_report_finger_count(dev, f->fingers);
1386
1387 input_report_key(dev, BTN_LEFT, f->left);
1388 input_report_key(dev, BTN_RIGHT, f->right);
1389 input_report_key(dev, BTN_MIDDLE, f->middle);
1390
1391 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1392 input_sync(dev);
1393}
1394
1395static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1396{
1397 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1398 return false;
1399 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1400 return false;
1401 return true;
1402}
1403
Pali Rohár04aae282015-01-14 13:18:30 -08001404static DEFINE_MUTEX(alps_mutex);
1405
1406static void alps_register_bare_ps2_mouse(struct work_struct *work)
1407{
1408 struct alps_data *priv =
1409 container_of(work, struct alps_data, dev3_register_work.work);
1410 struct psmouse *psmouse = priv->psmouse;
1411 struct input_dev *dev3;
1412 int error = 0;
1413
1414 mutex_lock(&alps_mutex);
1415
1416 if (priv->dev3)
1417 goto out;
1418
1419 dev3 = input_allocate_device();
1420 if (!dev3) {
1421 psmouse_err(psmouse, "failed to allocate secondary device\n");
1422 error = -ENOMEM;
1423 goto out;
1424 }
1425
1426 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1427 psmouse->ps2dev.serio->phys,
1428 (priv->dev2 ? "input2" : "input1"));
1429 dev3->phys = priv->phys3;
1430
1431 /*
1432 * format of input device name is: "protocol vendor name"
1433 * see function psmouse_switch_protocol() in psmouse-base.c
1434 */
1435 dev3->name = "PS/2 ALPS Mouse";
1436
1437 dev3->id.bustype = BUS_I8042;
1438 dev3->id.vendor = 0x0002;
1439 dev3->id.product = PSMOUSE_PS2;
1440 dev3->id.version = 0x0000;
1441 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1442
1443 input_set_capability(dev3, EV_REL, REL_X);
1444 input_set_capability(dev3, EV_REL, REL_Y);
1445 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1446 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1447 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1448
1449 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1450
1451 error = input_register_device(dev3);
1452 if (error) {
1453 psmouse_err(psmouse,
1454 "failed to register secondary device: %d\n",
1455 error);
1456 input_free_device(dev3);
1457 goto out;
1458 }
1459
1460 priv->dev3 = dev3;
1461
1462out:
1463 /*
1464 * Save the error code so that we can detect that we
1465 * already tried to create the device.
1466 */
1467 if (error)
1468 priv->dev3 = ERR_PTR(error);
1469
1470 mutex_unlock(&alps_mutex);
1471}
1472
Hans de Goede59c30af2015-04-03 17:14:40 -07001473static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001474 unsigned char packet[],
1475 bool report_buttons)
1476{
Hans de Goede59c30af2015-04-03 17:14:40 -07001477 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001478 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001479
Hans de Goedee3a79212015-04-03 17:20:05 -07001480 /* Figure out which device to use to report the bare packet */
1481 if (priv->proto_version == ALPS_PROTO_V2 &&
1482 (priv->flags & ALPS_DUALPOINT)) {
1483 /* On V2 devices the DualPoint Stick reports bare packets */
1484 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001485 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001486 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001487 /* Register dev3 mouse if we received PS/2 packet first time */
1488 if (!IS_ERR(priv->dev3))
1489 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1490 0);
1491 return;
1492 } else {
1493 dev = priv->dev3;
1494 }
1495
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001496 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001497 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001498 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1499
Pali Rohár04aae282015-01-14 13:18:30 -08001500 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001501 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001502 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001503 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1504
Pali Rohár04aae282015-01-14 13:18:30 -08001505 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001506}
1507
1508static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct alps_data *priv = psmouse->private;
1511
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001512 if (psmouse->pktcnt < 6)
1513 return PSMOUSE_GOOD_DATA;
1514
1515 if (psmouse->pktcnt == 6) {
1516 /*
1517 * Start a timer to flush the packet if it ends up last
1518 * 6-byte packet in the stream. Timer needs to fire
1519 * psmouse core times out itself. 20 ms should be enough
1520 * to decide if we are getting more data or not.
1521 */
1522 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1523 return PSMOUSE_GOOD_DATA;
1524 }
1525
1526 del_timer(&priv->timer);
1527
1528 if (psmouse->packet[6] & 0x80) {
1529
1530 /*
1531 * Highest bit is set - that means we either had
1532 * complete ALPS packet and this is start of the
1533 * next packet or we got garbage.
1534 */
1535
1536 if (((psmouse->packet[3] |
1537 psmouse->packet[4] |
1538 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001539 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001540 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001541 "refusing packet %4ph (suspected interleaved ps/2)\n",
1542 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001543 return PSMOUSE_BAD_DATA;
1544 }
1545
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001546 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001547
1548 /* Continue with the next packet */
1549 psmouse->packet[0] = psmouse->packet[6];
1550 psmouse->pktcnt = 1;
1551
1552 } else {
1553
1554 /*
1555 * High bit is 0 - that means that we indeed got a PS/2
1556 * packet in the middle of ALPS packet.
1557 *
1558 * There is also possibility that we got 6-byte ALPS
1559 * packet followed by 3-byte packet from trackpoint. We
1560 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001561 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001562 * normal operation (user would need to press all
1563 * buttons on the pad and start moving trackpoint
1564 * without touching the pad surface) we assume former.
1565 * Even if we are wrong the wost thing that would happen
1566 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001567 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001568 */
1569
Hans de Goede59c30af2015-04-03 17:14:40 -07001570 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1571 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001572
1573 /*
1574 * Continue with the standard ALPS protocol handling,
1575 * but make sure we won't process it as an interleaved
1576 * packet again, which may happen if all buttons are
1577 * pressed. To avoid this let's reset the 4th bit which
1578 * is normally 1.
1579 */
1580 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1581 psmouse->pktcnt = 4;
1582 }
1583
1584 return PSMOUSE_GOOD_DATA;
1585}
1586
1587static void alps_flush_packet(unsigned long data)
1588{
1589 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001590 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001591
1592 serio_pause_rx(psmouse->ps2dev.serio);
1593
Seth Forsheeb46615f2011-11-07 19:53:30 -08001594 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001595
1596 /*
1597 * We did not any more data in reasonable amount of time.
1598 * Validate the last 3 bytes and process as a standard
1599 * ALPS packet.
1600 */
1601 if ((psmouse->packet[3] |
1602 psmouse->packet[4] |
1603 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001604 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001605 "refusing packet %3ph (suspected interleaved ps/2)\n",
1606 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001607 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001608 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001609 }
1610 psmouse->pktcnt = 0;
1611 }
1612
1613 serio_continue_rx(psmouse->ps2dev.serio);
1614}
1615
1616static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1617{
1618 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001619
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001620 /*
1621 * Check if we are dealing with a bare PS/2 packet, presumably from
1622 * a device connected to the external PS/2 port. Because bare PS/2
1623 * protocol does not have enough constant bits to self-synchronize
1624 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001625 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001626 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001627 if (priv->proto_version != ALPS_PROTO_V8 &&
1628 !psmouse->out_of_sync_cnt &&
1629 (psmouse->packet[0] & 0xc8) == 0x08) {
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001632 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1633 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return PSMOUSE_FULL_PACKET;
1635 }
1636 return PSMOUSE_GOOD_DATA;
1637 }
1638
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001639 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1640
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001641 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001642 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1643 return alps_handle_interleaved_ps2(psmouse);
1644 }
1645
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001646 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001647 psmouse_dbg(psmouse,
1648 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001649 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Seth Forsheeb46615f2011-11-07 19:53:30 -08001653 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001654 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001655 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001656 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001657 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1658 psmouse->pktcnt - 1,
1659 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001660
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001661 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001662 psmouse->pktcnt == psmouse->pktsize) {
1663 /*
1664 * Some Dell boxes, such as Latitude E6440 or E7440
1665 * with closed lid, quite often smash last byte of
1666 * otherwise valid packet with 0xff. Given that the
1667 * next packet is very likely to be valid let's
1668 * report PSMOUSE_FULL_PACKET but not process data,
1669 * rather than reporting PSMOUSE_BAD_DATA and
1670 * filling the logs.
1671 */
1672 return PSMOUSE_FULL_PACKET;
1673 }
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001678 if ((priv->proto_version == ALPS_PROTO_V7 &&
1679 !alps_is_valid_package_v7(psmouse)) ||
1680 (priv->proto_version == ALPS_PROTO_V8 &&
1681 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001682 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1683 psmouse->pktcnt - 1,
1684 psmouse->packet[psmouse->pktcnt - 1]);
1685 return PSMOUSE_BAD_DATA;
1686 }
1687
Seth Forsheeb46615f2011-11-07 19:53:30 -08001688 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001689 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return PSMOUSE_FULL_PACKET;
1691 }
1692
1693 return PSMOUSE_GOOD_DATA;
1694}
1695
Seth Forshee25bded72011-11-07 19:53:36 -08001696static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1697{
1698 struct ps2dev *ps2dev = &psmouse->ps2dev;
1699 struct alps_data *priv = psmouse->private;
1700 int command;
1701 unsigned char *param;
1702 unsigned char dummy[4];
1703
1704 BUG_ON(nibble > 0xf);
1705
1706 command = priv->nibble_commands[nibble].command;
1707 param = (command & 0x0f00) ?
1708 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1709
1710 if (ps2_command(ps2dev, param, command))
1711 return -1;
1712
1713 return 0;
1714}
1715
1716static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1717{
1718 struct ps2dev *ps2dev = &psmouse->ps2dev;
1719 struct alps_data *priv = psmouse->private;
1720 int i, nibble;
1721
1722 if (ps2_command(ps2dev, NULL, priv->addr_command))
1723 return -1;
1724
1725 for (i = 12; i >= 0; i -= 4) {
1726 nibble = (addr >> i) & 0xf;
1727 if (alps_command_mode_send_nibble(psmouse, nibble))
1728 return -1;
1729 }
1730
1731 return 0;
1732}
1733
1734static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1735{
1736 struct ps2dev *ps2dev = &psmouse->ps2dev;
1737 unsigned char param[4];
1738
1739 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1740 return -1;
1741
1742 /*
1743 * The address being read is returned in the first two bytes
1744 * of the result. Check that this address matches the expected
1745 * address.
1746 */
1747 if (addr != ((param[0] << 8) | param[1]))
1748 return -1;
1749
1750 return param[2];
1751}
1752
1753static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1754{
1755 if (alps_command_mode_set_addr(psmouse, addr))
1756 return -1;
1757 return __alps_command_mode_read_reg(psmouse, addr);
1758}
1759
1760static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1761{
1762 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1763 return -1;
1764 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1765 return -1;
1766 return 0;
1767}
1768
1769static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1770 u8 value)
1771{
1772 if (alps_command_mode_set_addr(psmouse, addr))
1773 return -1;
1774 return __alps_command_mode_write_reg(psmouse, value);
1775}
1776
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001777static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1778 int repeated_command, unsigned char *param)
1779{
1780 struct ps2dev *ps2dev = &psmouse->ps2dev;
1781
1782 param[0] = 0;
1783 if (init_command && ps2_command(ps2dev, param, init_command))
1784 return -EIO;
1785
1786 if (ps2_command(ps2dev, NULL, repeated_command) ||
1787 ps2_command(ps2dev, NULL, repeated_command) ||
1788 ps2_command(ps2dev, NULL, repeated_command))
1789 return -EIO;
1790
1791 param[0] = param[1] = param[2] = 0xff;
1792 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1793 return -EIO;
1794
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001795 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1796 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001797 return 0;
1798}
1799
Yunkang Tang38088432014-07-26 13:51:41 -07001800static bool alps_check_valid_firmware_id(unsigned char id[])
1801{
1802 if (id[0] == 0x73)
1803 return true;
1804
1805 if (id[0] == 0x88 &&
1806 (id[1] == 0x07 ||
1807 id[1] == 0x08 ||
1808 (id[1] & 0xf0) == 0xb0 ||
1809 (id[1] & 0xf0) == 0xc0)) {
1810 return true;
1811 }
1812
1813 return false;
1814}
1815
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001816static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001817{
1818 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001819
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001820 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001821 psmouse_err(psmouse, "failed to enter command mode\n");
1822 return -1;
1823 }
1824
Yunkang Tang38088432014-07-26 13:51:41 -07001825 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001826 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001827 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001828 return -1;
1829 }
Seth Forshee25bded72011-11-07 19:53:36 -08001830 return 0;
1831}
1832
1833static inline int alps_exit_command_mode(struct psmouse *psmouse)
1834{
1835 struct ps2dev *ps2dev = &psmouse->ps2dev;
1836 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1837 return -1;
1838 return 0;
1839}
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841/*
1842 * For DualPoint devices select the device that should respond to
1843 * subsequent commands. It looks like glidepad is behind stickpointer,
1844 * I'd thought it would be other way around...
1845 */
Seth Forshee25bded72011-11-07 19:53:36 -08001846static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1850
1851 if (ps2_command(ps2dev, NULL, cmd) ||
1852 ps2_command(ps2dev, NULL, cmd) ||
1853 ps2_command(ps2dev, NULL, cmd) ||
1854 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1855 return -1;
1856
1857 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001858 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 return 0;
1861}
1862
Seth Forshee25bded72011-11-07 19:53:36 -08001863static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 struct ps2dev *ps2dev = &psmouse->ps2dev;
1866
1867 /* Try ALPS magic knock - 4 disable before enable */
1868 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1869 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1870 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1871 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1872 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1873 return -1;
1874
1875 /*
1876 * Switch mouse to poll (remote) mode so motion data will not
1877 * get in our way
1878 */
1879 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1880}
1881
Yunkang Tang95f75e92013-12-01 22:33:52 -08001882static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1883{
1884 int i, nibble;
1885
1886 /*
1887 * b0-b11 are valid bits, send sequence is inverse.
1888 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1889 */
1890 for (i = 0; i <= 8; i += 4) {
1891 nibble = (word >> i) & 0xf;
1892 if (alps_command_mode_send_nibble(psmouse, nibble))
1893 return -1;
1894 }
1895
1896 return 0;
1897}
1898
1899static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1900 u16 addr, u16 value)
1901{
1902 struct ps2dev *ps2dev = &psmouse->ps2dev;
1903
1904 /* 0x0A0 is the command to write the word */
1905 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1906 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1907 alps_monitor_mode_send_word(psmouse, addr) ||
1908 alps_monitor_mode_send_word(psmouse, value) ||
1909 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1910 return -1;
1911
1912 return 0;
1913}
1914
1915static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1916{
1917 struct ps2dev *ps2dev = &psmouse->ps2dev;
1918
1919 if (enable) {
1920 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1921 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1922 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1923 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1924 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1925 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1926 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1927 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1928 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1929 return -1;
1930 } else {
1931 /* EC to exit monitor mode */
1932 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1933 return -1;
1934 }
1935
1936 return 0;
1937}
1938
1939static int alps_absolute_mode_v6(struct psmouse *psmouse)
1940{
1941 u16 reg_val = 0x181;
1942 int ret = -1;
1943
1944 /* enter monitor mode, to write the register */
1945 if (alps_monitor_mode(psmouse, true))
1946 return -1;
1947
1948 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1949
1950 if (alps_monitor_mode(psmouse, false))
1951 ret = -1;
1952
1953 return ret;
1954}
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956static int alps_get_status(struct psmouse *psmouse, char *param)
1957{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001959 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return -1;
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return 0;
1963}
1964
1965/*
1966 * Turn touchpad tapping on or off. The sequences are:
1967 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1968 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1969 * My guess that 0xE9 (GetInfo) is here as a sync point.
1970 * For models that also have stickpointer (DualPoints) its tapping
1971 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1972 * we don't fiddle with it.
1973 */
1974static int alps_tap_mode(struct psmouse *psmouse, int enable)
1975{
1976 struct ps2dev *ps2dev = &psmouse->ps2dev;
1977 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1978 unsigned char tap_arg = enable ? 0x0A : 0x00;
1979 unsigned char param[4];
1980
1981 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1982 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1983 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1984 ps2_command(ps2dev, &tap_arg, cmd))
1985 return -1;
1986
1987 if (alps_get_status(psmouse, param))
1988 return -1;
1989
1990 return 0;
1991}
1992
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001993/*
1994 * alps_poll() - poll the touchpad for current motion packet.
1995 * Used in resync.
1996 */
1997static int alps_poll(struct psmouse *psmouse)
1998{
1999 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08002000 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002001 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002002
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002003 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08002004 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002005
2006 poll_failed = ps2_command(&psmouse->ps2dev, buf,
2007 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
2008
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002009 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08002010 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002011
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002012 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002013 return -1;
2014
2015 if ((psmouse->badbyte & 0xc8) == 0x08) {
2016/*
2017 * Poll the track stick ...
2018 */
2019 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2020 return -1;
2021 }
2022
2023 memcpy(psmouse->packet, buf, sizeof(buf));
2024 return 0;
2025}
2026
Seth Forshee25bded72011-11-07 19:53:36 -08002027static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002031 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002032 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002036 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002037 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05002039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Seth Forshee25bded72011-11-07 19:53:36 -08002041 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002042 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return -1;
2044 }
2045
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002046 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002047 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002051 /* ALPS needs stream mode, otherwise it won't report any data */
2052 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002053 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002054 return -1;
2055 }
2056
2057 return 0;
2058}
2059
Yunkang Tang95f75e92013-12-01 22:33:52 -08002060static int alps_hw_init_v6(struct psmouse *psmouse)
2061{
2062 unsigned char param[2] = {0xC8, 0x14};
2063
2064 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2065 if (alps_passthrough_mode_v2(psmouse, true))
2066 return -1;
2067
2068 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2069 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2070 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2071 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2072 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2073 return -1;
2074
2075 if (alps_passthrough_mode_v2(psmouse, false))
2076 return -1;
2077
2078 if (alps_absolute_mode_v6(psmouse)) {
2079 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2080 return -1;
2081 }
2082
2083 return 0;
2084}
2085
Seth Forshee25bded72011-11-07 19:53:36 -08002086/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08002087 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08002088 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08002089static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2090 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08002091{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002092 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08002093
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002094 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002095 return -1;
2096
Kevin Cernekeecd401202013-02-13 22:28:07 -08002097 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2098 if (reg_val == -1)
2099 goto error;
2100
Seth Forshee25bded72011-11-07 19:53:36 -08002101 if (enable)
2102 reg_val |= 0x01;
2103 else
2104 reg_val &= ~0x01;
2105
Kevin Cernekeecd401202013-02-13 22:28:07 -08002106 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08002107
Kevin Cernekeecd401202013-02-13 22:28:07 -08002108error:
2109 if (alps_exit_command_mode(psmouse))
2110 ret = -1;
2111 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08002112}
2113
2114/* Must be in command mode when calling this function */
2115static int alps_absolute_mode_v3(struct psmouse *psmouse)
2116{
2117 int reg_val;
2118
2119 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2120 if (reg_val == -1)
2121 return -1;
2122
2123 reg_val |= 0x06;
2124 if (__alps_command_mode_write_reg(psmouse, reg_val))
2125 return -1;
2126
2127 return 0;
2128}
2129
Pali Rohárdae928e2016-01-06 15:50:08 -08002130static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
Kevin Cernekeecd401202013-02-13 22:28:07 -08002131{
2132 int ret = -EIO, reg_val;
2133
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002134 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002135 goto error;
2136
2137 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2138 if (reg_val == -1)
2139 goto error;
2140
2141 /* bit 7: trackstick is present */
2142 ret = reg_val & 0x80 ? 0 : -ENODEV;
2143
2144error:
2145 alps_exit_command_mode(psmouse);
2146 return ret;
2147}
2148
2149static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2150{
2151 struct ps2dev *ps2dev = &psmouse->ps2dev;
2152 int ret = 0;
2153 unsigned char param[4];
2154
2155 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2156 return -EIO;
2157
2158 /*
2159 * E7 report for the trackstick
2160 *
2161 * There have been reports of failures to seem to trace back
2162 * to the above trackstick check failing. When these occur
2163 * this E7 report fails, so when that happens we continue
2164 * with the assumption that there isn't a trackstick after
2165 * all.
2166 */
2167 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002168 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002169 ret = -ENODEV;
2170 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002171 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002172
2173 /*
2174 * Not sure what this does, but it is absolutely
2175 * essential. Without it, the touchpad does not
2176 * work at all and the trackstick just emits normal
2177 * PS/2 packets.
2178 */
2179 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2180 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2181 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2182 alps_command_mode_send_nibble(psmouse, 0x9) ||
2183 alps_command_mode_send_nibble(psmouse, 0x4)) {
2184 psmouse_err(psmouse,
2185 "Error sending magic E6 sequence\n");
2186 ret = -EIO;
2187 goto error;
2188 }
2189
2190 /*
2191 * This ensures the trackstick packets are in the format
2192 * supported by this driver. If bit 1 isn't set the packet
2193 * format is different.
2194 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002195 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002196 alps_command_mode_write_reg(psmouse,
2197 reg_base + 0x08, 0x82) ||
2198 alps_exit_command_mode(psmouse))
2199 ret = -EIO;
2200 }
2201
2202error:
2203 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2204 ret = -EIO;
2205
2206 return ret;
2207}
2208
Seth Forshee25bded72011-11-07 19:53:36 -08002209static int alps_hw_init_v3(struct psmouse *psmouse)
2210{
Pali Rohár4b1af852016-01-06 15:58:56 -08002211 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -08002212 struct ps2dev *ps2dev = &psmouse->ps2dev;
2213 int reg_val;
2214 unsigned char param[4];
2215
Pali Rohár4b1af852016-01-06 15:58:56 -08002216 if ((priv->flags & ALPS_DUALPOINT) &&
Kevin Cernekeecd401202013-02-13 22:28:07 -08002217 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002218 goto error;
2219
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002220 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002221 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002222 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2223 goto error;
2224 }
2225
2226 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2227 if (reg_val == -1)
2228 goto error;
2229 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2230 goto error;
2231
2232 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2233 if (reg_val == -1)
2234 goto error;
2235 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2236 goto error;
2237
2238 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2239 goto error;
2240 if (__alps_command_mode_write_reg(psmouse, 0x04))
2241 goto error;
2242
2243 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2244 goto error;
2245 if (__alps_command_mode_write_reg(psmouse, 0x03))
2246 goto error;
2247
2248 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2249 goto error;
2250 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2251 goto error;
2252
2253 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2254 goto error;
2255 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2256 goto error;
2257
Seth Forshee25bded72011-11-07 19:53:36 -08002258 alps_exit_command_mode(psmouse);
2259
2260 /* Set rate and enable data reporting */
2261 param[0] = 0x64;
2262 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2263 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2264 psmouse_err(psmouse, "Failed to enable data reporting\n");
2265 return -1;
2266 }
2267
2268 return 0;
2269
Seth Forshee25bded72011-11-07 19:53:36 -08002270error:
2271 /*
2272 * Leaving the touchpad in command mode will essentially render
2273 * it unusable until the machine reboots, so exit it here just
2274 * to be safe
2275 */
2276 alps_exit_command_mode(psmouse);
2277 return -1;
2278}
2279
Hans de Goedef3f33c62014-07-29 11:22:07 -07002280static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2281{
2282 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2283 struct alps_data *priv = psmouse->private;
2284
2285 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2286 if (reg < 0)
2287 return reg;
2288
2289 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2290 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2291
2292 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2293 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2294
2295 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2296 if (reg < 0)
2297 return reg;
2298
2299 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2300 x_electrode = 17 + x_electrode;
2301
2302 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2303 y_electrode = 13 + y_electrode;
2304
2305 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2306 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2307
2308 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2309 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2310
2311 psmouse_dbg(psmouse,
2312 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2313 x_pitch, y_pitch, x_electrode, y_electrode,
2314 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2315
2316 return 0;
2317}
2318
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002319static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2320{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002321 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002322 struct ps2dev *ps2dev = &psmouse->ps2dev;
2323 int reg_val, ret = -1;
2324
Kevin Cernekeecd401202013-02-13 22:28:07 -08002325 if (priv->flags & ALPS_DUALPOINT) {
2326 reg_val = alps_setup_trackstick_v3(psmouse,
2327 ALPS_REG_BASE_RUSHMORE);
2328 if (reg_val == -EIO)
2329 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002330 }
2331
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002332 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002333 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2334 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2335 goto error;
2336
Hans de Goedef3f33c62014-07-29 11:22:07 -07002337 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2338 goto error;
2339
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002340 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2341 if (reg_val == -1)
2342 goto error;
2343 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2344 goto error;
2345
2346 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2347 goto error;
2348
2349 /* enter absolute mode */
2350 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2351 if (reg_val == -1)
2352 goto error;
2353 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2354 goto error;
2355
2356 alps_exit_command_mode(psmouse);
2357 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2358
2359error:
2360 alps_exit_command_mode(psmouse);
2361 return ret;
2362}
2363
Seth Forshee25bded72011-11-07 19:53:36 -08002364/* Must be in command mode when calling this function */
2365static int alps_absolute_mode_v4(struct psmouse *psmouse)
2366{
2367 int reg_val;
2368
2369 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2370 if (reg_val == -1)
2371 return -1;
2372
2373 reg_val |= 0x02;
2374 if (__alps_command_mode_write_reg(psmouse, reg_val))
2375 return -1;
2376
2377 return 0;
2378}
2379
2380static int alps_hw_init_v4(struct psmouse *psmouse)
2381{
Seth Forshee25bded72011-11-07 19:53:36 -08002382 struct ps2dev *ps2dev = &psmouse->ps2dev;
2383 unsigned char param[4];
2384
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002385 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002386 goto error;
2387
2388 if (alps_absolute_mode_v4(psmouse)) {
2389 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2390 goto error;
2391 }
2392
2393 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2394 goto error;
2395
2396 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2397 goto error;
2398
2399 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2400 goto error;
2401
2402 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2403 goto error;
2404
2405 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2406 goto error;
2407
2408 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2409 goto error;
2410
2411 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2412 goto error;
2413
2414 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2415 goto error;
2416
2417 alps_exit_command_mode(psmouse);
2418
2419 /*
2420 * This sequence changes the output from a 9-byte to an
2421 * 8-byte format. All the same data seems to be present,
2422 * just in a more compact format.
2423 */
2424 param[0] = 0xc8;
2425 param[1] = 0x64;
2426 param[2] = 0x50;
2427 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2428 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2429 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2430 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2431 return -1;
2432
2433 /* Set rate and enable data reporting */
2434 param[0] = 0x64;
2435 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2436 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2437 psmouse_err(psmouse, "Failed to enable data reporting\n");
2438 return -1;
2439 }
2440
2441 return 0;
2442
2443error:
2444 /*
2445 * Leaving the touchpad in command mode will essentially render
2446 * it unusable until the machine reboots, so exit it here just
2447 * to be safe
2448 */
2449 alps_exit_command_mode(psmouse);
2450 return -1;
2451}
2452
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002453static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2454 unsigned char index, unsigned char otp[])
2455{
2456 struct ps2dev *ps2dev = &psmouse->ps2dev;
2457
2458 switch (index) {
2459 case 0:
2460 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2461 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2462 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2463 return -1;
2464
2465 break;
2466
2467 case 1:
2468 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2469 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2470 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2471 return -1;
2472
2473 break;
2474 }
2475
2476 return 0;
2477}
2478
Fengguang Wu07f19e32015-04-10 23:54:31 -07002479static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2480 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002481{
2482 int num_x_electrode;
2483 int num_y_electrode;
2484 int x_pitch, y_pitch, x_phys, y_phys;
2485
Masaki Ota81643d02017-03-17 14:10:57 -07002486 if (IS_SS4PLUS_DEV(priv->dev_id)) {
2487 num_x_electrode =
2488 SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2489 num_y_electrode =
2490 SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002491
Masaki Ota81643d02017-03-17 14:10:57 -07002492 priv->x_max =
2493 (num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2494 priv->y_max =
2495 (num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002496
Masaki Ota81643d02017-03-17 14:10:57 -07002497 x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2498 y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2499
2500 } else {
2501 num_x_electrode =
2502 SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2503 num_y_electrode =
2504 SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2505
2506 priv->x_max =
2507 (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2508 priv->y_max =
2509 (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2510
2511 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2512 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2513 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002514
2515 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2516 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2517
2518 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2519 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2520
2521 return 0;
2522}
2523
Fengguang Wu07f19e32015-04-10 23:54:31 -07002524static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2525 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002526{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002527 unsigned char is_btnless;
2528
Masaki Ota81643d02017-03-17 14:10:57 -07002529 if (IS_SS4PLUS_DEV(priv->dev_id))
2530 is_btnless = (otp[1][0] >> 1) & 0x01;
2531 else
2532 is_btnless = (otp[1][1] >> 3) & 0x01;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002533
2534 if (is_btnless)
2535 priv->flags |= ALPS_BUTTONPAD;
2536
2537 return 0;
2538}
2539
Masaki Ota81643d02017-03-17 14:10:57 -07002540static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
Masaki Ota61981aa2018-01-29 14:36:54 -08002541 struct alps_data *priv,
2542 struct psmouse *psmouse)
Masaki Ota81643d02017-03-17 14:10:57 -07002543{
2544 bool is_dual = false;
Masaki Ota61981aa2018-01-29 14:36:54 -08002545 int reg_val = 0;
2546 struct ps2dev *ps2dev = &psmouse->ps2dev;
Masaki Ota81643d02017-03-17 14:10:57 -07002547
Masaki Ota61981aa2018-01-29 14:36:54 -08002548 if (IS_SS4PLUS_DEV(priv->dev_id)) {
Masaki Ota81643d02017-03-17 14:10:57 -07002549 is_dual = (otp[0][0] >> 4) & 0x01;
2550
Masaki Ota61981aa2018-01-29 14:36:54 -08002551 if (!is_dual) {
2552 /* For support TrackStick of Thinkpad L/E series */
2553 if (alps_exit_command_mode(psmouse) == 0 &&
2554 alps_enter_command_mode(psmouse) == 0) {
2555 reg_val = alps_command_mode_read_reg(psmouse,
2556 0xD7);
2557 }
2558 alps_exit_command_mode(psmouse);
2559 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2560
2561 if (reg_val == 0x0C || reg_val == 0x1D)
2562 is_dual = true;
2563 }
2564 }
2565
Masaki Ota81643d02017-03-17 14:10:57 -07002566 if (is_dual)
2567 priv->flags |= ALPS_DUALPOINT |
2568 ALPS_DUALPOINT_WITH_PRESSURE;
2569
2570 return 0;
2571}
2572
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002573static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2574 struct alps_data *priv)
2575{
2576 unsigned char otp[2][4];
2577
2578 memset(otp, 0, sizeof(otp));
2579
Masaki Ota963068b2017-08-24 15:44:36 -07002580 if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
2581 alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002582 return -1;
2583
2584 alps_update_device_area_ss4_v2(otp, priv);
2585
2586 alps_update_btn_info_ss4_v2(otp, priv);
2587
Masaki Ota61981aa2018-01-29 14:36:54 -08002588 alps_update_dual_info_ss4_v2(otp, priv, psmouse);
Masaki Ota81643d02017-03-17 14:10:57 -07002589
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002590 return 0;
2591}
2592
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002593static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2594 struct alps_data *priv)
2595{
2596 struct ps2dev *ps2dev = &psmouse->ps2dev;
2597 unsigned char param[4] = {0};
2598 int num_x_electrode, num_y_electrode;
2599
2600 if (alps_enter_command_mode(psmouse))
2601 return -1;
2602
2603 param[0] = 0x0a;
2604 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2605 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2606 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2607 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2608 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2609 return -1;
2610
2611 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2612 return -1;
2613
2614 /*
2615 * Dolphin's sensor line number is not fixed. It can be calculated
2616 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2617 * Further more, we can get device's x_max and y_max by multiplying
2618 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2619 *
2620 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2621 * real sensor line number X = 11 + 8 = 19, and
2622 * real sensor line number Y = 8 + 1 = 9.
2623 * So, x_max = (19 - 1) * 64 = 1152, and
2624 * y_max = (9 - 1) * 64 = 512.
2625 */
2626 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2627 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2628 priv->x_bits = num_x_electrode;
2629 priv->y_bits = num_y_electrode;
2630 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2631 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2632
2633 if (alps_exit_command_mode(psmouse))
2634 return -1;
2635
2636 return 0;
2637}
2638
Dave Turvene75af9e52013-02-21 22:58:28 -08002639static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2640{
2641 struct ps2dev *ps2dev = &psmouse->ps2dev;
2642 unsigned char param[2];
2643
2644 /* This is dolphin "v1" as empirically defined by florin9doi */
2645 param[0] = 0x64;
2646 param[1] = 0x28;
2647
2648 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2649 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2650 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2651 return -1;
2652
2653 return 0;
2654}
2655
Yunkang Tang38088432014-07-26 13:51:41 -07002656static int alps_hw_init_v7(struct psmouse *psmouse)
2657{
2658 struct ps2dev *ps2dev = &psmouse->ps2dev;
2659 int reg_val, ret = -1;
2660
2661 if (alps_enter_command_mode(psmouse) ||
2662 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2663 goto error;
2664
Hans de Goedef3f33c62014-07-29 11:22:07 -07002665 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2666 goto error;
2667
Yunkang Tang38088432014-07-26 13:51:41 -07002668 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2669 goto error;
2670
2671 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2672 if (reg_val == -1)
2673 goto error;
2674 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2675 goto error;
2676
2677 alps_exit_command_mode(psmouse);
2678 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2679
2680error:
2681 alps_exit_command_mode(psmouse);
2682 return ret;
2683}
2684
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002685static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2686{
2687 struct ps2dev *ps2dev = &psmouse->ps2dev;
2688 char param[2] = {0x64, 0x28};
2689 int ret = -1;
2690
2691 /* enter absolute mode */
2692 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2693 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2694 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2695 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2696 goto error;
2697 }
2698
2699 /* T.B.D. Decread noise packet number, delete in the future */
2700 if (alps_exit_command_mode(psmouse) ||
2701 alps_enter_command_mode(psmouse) ||
2702 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2703 goto error;
2704 }
2705 alps_exit_command_mode(psmouse);
2706
2707 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2708
2709error:
2710 alps_exit_command_mode(psmouse);
2711 return ret;
2712}
2713
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002714static int alps_set_protocol(struct psmouse *psmouse,
2715 struct alps_data *priv,
2716 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002717{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002718 psmouse->private = priv;
2719
2720 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2721
2722 priv->proto_version = protocol->version;
2723 priv->byte0 = protocol->byte0;
2724 priv->mask0 = protocol->mask0;
2725 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002726
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002727 priv->x_max = 2000;
2728 priv->y_max = 1400;
2729 priv->x_bits = 15;
2730 priv->y_bits = 11;
2731
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002732 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002733 case ALPS_PROTO_V1:
2734 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002735 priv->hw_init = alps_hw_init_v1_v2;
2736 priv->process_packet = alps_process_packet_v1_v2;
2737 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002738 priv->x_max = 1023;
2739 priv->y_max = 767;
Hans de Goede19556212015-10-26 01:50:28 -07002740 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2741 priv->flags |= ALPS_STICK_BITS;
Seth Forshee25bded72011-11-07 19:53:36 -08002742 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002743
Seth Forshee25bded72011-11-07 19:53:36 -08002744 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002745 priv->hw_init = alps_hw_init_v3;
2746 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002747 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002748 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002749 priv->nibble_commands = alps_v3_nibble_commands;
2750 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Pali Rohár4b1af852016-01-06 15:58:56 -08002751
2752 if (alps_probe_trackstick_v3_v7(psmouse,
2753 ALPS_REG_BASE_PINNACLE) < 0)
2754 priv->flags &= ~ALPS_DUALPOINT;
2755
Seth Forshee25bded72011-11-07 19:53:36 -08002756 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002757
2758 case ALPS_PROTO_V3_RUSHMORE:
2759 priv->hw_init = alps_hw_init_rushmore_v3;
2760 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002761 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002762 priv->decode_fields = alps_decode_rushmore;
2763 priv->nibble_commands = alps_v3_nibble_commands;
2764 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2765 priv->x_bits = 16;
2766 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002767
Pali Rohárdae928e2016-01-06 15:50:08 -08002768 if (alps_probe_trackstick_v3_v7(psmouse,
2769 ALPS_REG_BASE_RUSHMORE) < 0)
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002770 priv->flags &= ~ALPS_DUALPOINT;
2771
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002772 break;
2773
Seth Forshee25bded72011-11-07 19:53:36 -08002774 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002775 priv->hw_init = alps_hw_init_v4;
2776 priv->process_packet = alps_process_packet_v4;
Hans de Goede688ea362015-05-20 14:42:50 -07002777 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002778 priv->nibble_commands = alps_v4_nibble_commands;
2779 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002780 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002781
Dave Turvene75af9e52013-02-21 22:58:28 -08002782 case ALPS_PROTO_V5:
2783 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002784 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002785 priv->decode_fields = alps_decode_dolphin;
Hans de Goede688ea362015-05-20 14:42:50 -07002786 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dave Turvene75af9e52013-02-21 22:58:28 -08002787 priv->nibble_commands = alps_v3_nibble_commands;
2788 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002789 priv->x_bits = 23;
2790 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002791
2792 if (alps_dolphin_get_device_area(psmouse, priv))
2793 return -EIO;
2794
Dave Turvene75af9e52013-02-21 22:58:28 -08002795 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002796
Yunkang Tang95f75e92013-12-01 22:33:52 -08002797 case ALPS_PROTO_V6:
2798 priv->hw_init = alps_hw_init_v6;
2799 priv->process_packet = alps_process_packet_v6;
2800 priv->set_abs_params = alps_set_abs_params_st;
2801 priv->nibble_commands = alps_v6_nibble_commands;
2802 priv->x_max = 2047;
2803 priv->y_max = 1535;
2804 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002805
Yunkang Tang38088432014-07-26 13:51:41 -07002806 case ALPS_PROTO_V7:
2807 priv->hw_init = alps_hw_init_v7;
2808 priv->process_packet = alps_process_packet_v7;
2809 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002810 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002811 priv->nibble_commands = alps_v3_nibble_commands;
2812 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002813 priv->x_max = 0xfff;
2814 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002815
2816 if (priv->fw_ver[1] != 0xba)
2817 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002818
Pali Rohárdae928e2016-01-06 15:50:08 -08002819 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2820 priv->flags &= ~ALPS_DUALPOINT;
2821
Yunkang Tang38088432014-07-26 13:51:41 -07002822 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002823
2824 case ALPS_PROTO_V8:
2825 priv->hw_init = alps_hw_init_ss4_v2;
2826 priv->process_packet = alps_process_packet_ss4_v2;
2827 priv->decode_fields = alps_decode_ss4_v2;
2828 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2829 priv->nibble_commands = alps_v3_nibble_commands;
2830 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2831
2832 if (alps_set_defaults_ss4_v2(psmouse, priv))
2833 return -EIO;
2834
2835 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002836 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002837
2838 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002839}
2840
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002841static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2842 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002843{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002844 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002845 int i;
2846
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002847 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2848 model = &alps_model_data[i];
2849
2850 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2851 (!model->command_mode_resp ||
2852 model->command_mode_resp == ec[2])) {
2853
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002854 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002855 }
2856 }
2857
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002858 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002859}
2860
2861static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2862{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002863 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002864 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002865 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002866
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002867 /*
2868 * First try "E6 report".
2869 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2870 * The bits 0-2 of the first byte will be 1s if some buttons are
2871 * pressed.
2872 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002873 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2874 PSMOUSE_CMD_SETSCALE11, e6))
2875 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002876
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002877 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2878 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002879
2880 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002881 * Now get the "E7" and "EC" reports. These will uniquely identify
2882 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002883 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002884 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2885 PSMOUSE_CMD_SETSCALE21, e7) ||
2886 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2887 PSMOUSE_CMD_RESET_WRAP, ec) ||
2888 alps_exit_command_mode(psmouse))
2889 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002890
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002891 protocol = alps_match_table(e7, ec);
2892 if (!protocol) {
2893 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2894 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2895 protocol = &alps_v5_protocol_data;
2896 } else if (ec[0] == 0x88 &&
2897 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2898 protocol = &alps_v7_protocol_data;
2899 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2900 protocol = &alps_v3_rushmore_data;
2901 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2902 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2903 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002904 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
Masaki Ota81643d02017-03-17 14:10:57 -07002905 (e7[2] == 0x14 || e7[2] == 0x28)) {
Ben Gamariaeaa8812016-10-04 11:43:30 -07002906 protocol = &alps_v8_protocol_data;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002907 } else {
2908 psmouse_dbg(psmouse,
2909 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2910 return -EINVAL;
2911 }
2912 }
2913
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002914 if (priv) {
Masaki Ota81643d02017-03-17 14:10:57 -07002915 /* Save Device ID and Firmware version */
2916 memcpy(priv->dev_id, e7, 3);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002917 memcpy(priv->fw_ver, ec, 3);
2918 error = alps_set_protocol(psmouse, priv, protocol);
2919 if (error)
2920 return error;
2921 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002922
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002923 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002924}
2925
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002926static int alps_reconnect(struct psmouse *psmouse)
2927{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002928 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002929
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002930 psmouse_reset(psmouse);
2931
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002932 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002933 return -1;
2934
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002935 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937
2938static void alps_disconnect(struct psmouse *psmouse)
2939{
2940 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002943 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002944 if (priv->dev2)
2945 input_unregister_device(priv->dev2);
2946 if (!IS_ERR_OR_NULL(priv->dev3))
2947 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 kfree(priv);
2949}
2950
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002951static void alps_set_abs_params_st(struct alps_data *priv,
2952 struct input_dev *dev1)
2953{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002954 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2955 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002956 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002957}
2958
Masaki Ota8eccd392015-03-27 20:44:21 -07002959static void alps_set_abs_params_mt_common(struct alps_data *priv,
2960 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002961{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002962 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2963 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002964
Hans de Goedef3f33c62014-07-29 11:22:07 -07002965 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2966 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2967
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002968 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2969 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002970}
Yunkang Tang38088432014-07-26 13:51:41 -07002971
Hans de Goede688ea362015-05-20 14:42:50 -07002972static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2973 struct input_dev *dev1)
Masaki Ota8eccd392015-03-27 20:44:21 -07002974{
2975 alps_set_abs_params_mt_common(priv, dev1);
2976 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2977
2978 input_mt_init_slots(dev1, MAX_TOUCHES,
2979 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
Hans de Goede1662c0332015-05-20 14:42:15 -07002980 INPUT_MT_SEMI_MT);
Masaki Ota8eccd392015-03-27 20:44:21 -07002981}
2982
2983static void alps_set_abs_params_v7(struct alps_data *priv,
2984 struct input_dev *dev1)
2985{
2986 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002987 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002988
2989 input_mt_init_slots(dev1, MAX_TOUCHES,
2990 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2991 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002992
2993 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2994}
2995
2996static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2997 struct input_dev *dev1)
2998{
2999 alps_set_abs_params_mt_common(priv, dev1);
3000 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
3001 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3002
3003 input_mt_init_slots(dev1, MAX_TOUCHES,
3004 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3005 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08003006}
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008int alps_init(struct psmouse *psmouse)
3009{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003010 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08003011 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003012 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003014 error = priv->hw_init(psmouse);
3015 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 goto init_fail;
3017
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08003018 /*
3019 * Undo part of setup done for us by psmouse core since touchpad
3020 * is not a relative device.
3021 */
3022 __clear_bit(EV_REL, dev1->evbit);
3023 __clear_bit(REL_X, dev1->relbit);
3024 __clear_bit(REL_Y, dev1->relbit);
3025
3026 /*
3027 * Now set up our capabilities.
3028 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07003029 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
3030 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
3031 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08003032 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
3033 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Jiri Slaby7b19ada2007-10-18 23:40:32 -07003035 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08003036
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08003037 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003039 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07003040 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
3041 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003044 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07003045 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
3046 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003049 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08003050 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
3051 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
3052 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
3053 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07003054 } else if (priv->flags & ALPS_BUTTONPAD) {
3055 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
3056 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08003057 } else {
3058 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
3059 }
3060
Pali Rohárdfba8602015-01-09 12:48:44 -08003061 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08003062 struct input_dev *dev2;
3063
3064 dev2 = input_allocate_device();
3065 if (!dev2) {
3066 psmouse_err(psmouse,
3067 "failed to allocate trackstick device\n");
3068 error = -ENOMEM;
3069 goto init_fail;
3070 }
3071
3072 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3073 psmouse->ps2dev.serio->phys);
3074 dev2->phys = priv->phys2;
3075
Pali Rohárdfba8602015-01-09 12:48:44 -08003076 /*
3077 * format of input device name is: "protocol vendor name"
3078 * see function psmouse_switch_protocol() in psmouse-base.c
3079 */
3080 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08003081
3082 dev2->id.bustype = BUS_I8042;
3083 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08003084 dev2->id.product = PSMOUSE_ALPS;
3085 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08003086 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08003087
Pali Rohár04aae282015-01-14 13:18:30 -08003088 input_set_capability(dev2, EV_REL, REL_X);
3089 input_set_capability(dev2, EV_REL, REL_Y);
Ben Gamari7ad8a102016-10-04 11:41:58 -07003090 if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3091 input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3092 input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3093 }
Pali Rohár04aae282015-01-14 13:18:30 -08003094 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3095 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3096 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003097
Pali Rohár04aae282015-01-14 13:18:30 -08003098 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07003099 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3100
Pali Rohár04aae282015-01-14 13:18:30 -08003101 error = input_register_device(dev2);
3102 if (error) {
3103 psmouse_err(psmouse,
3104 "failed to register trackstick device: %d\n",
3105 error);
3106 input_free_device(dev2);
3107 goto init_fail;
3108 }
3109
3110 priv->dev2 = dev2;
3111 }
3112
3113 priv->psmouse = psmouse;
3114
3115 INIT_DELAYED_WORK(&priv->dev3_register_work,
3116 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003119 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 psmouse->disconnect = alps_disconnect;
3121 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003122 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003124 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3125 psmouse->resync_time = 0;
3126
Pali Rohár9d720b32014-11-08 12:58:57 -08003127 /* Allow 2 invalid packets without resetting device */
3128 psmouse->resetafter = psmouse->pktsize * 2;
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return 0;
3131
3132init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04003133 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003134 /*
3135 * Even though we did not allocate psmouse->private we do free
3136 * it here.
3137 */
3138 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04003139 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003140 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07003143int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003145 struct alps_data *priv;
3146 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003148 error = alps_identify(psmouse, NULL);
3149 if (error)
3150 return error;
3151
3152 /*
3153 * Reset the device to make sure it is fully operational:
3154 * on some laptops, like certain Dell Latitudes, we may
3155 * fail to properly detect presence of trackstick if device
3156 * has not been reset.
3157 */
3158 psmouse_reset(psmouse);
3159
3160 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3161 if (!priv)
3162 return -ENOMEM;
3163
3164 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003165 if (error) {
3166 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003167 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 if (set_properties) {
3171 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003172 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003173 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003174 psmouse->model = priv->proto_version;
3175 } else {
3176 /*
3177 * Destroy alps_data structure we allocated earlier since
3178 * this was just a "trial run". Otherwise we'll keep it
3179 * to be used by alps_init() which has to be called if
3180 * we succeed and set_properties is true.
3181 */
3182 kfree(priv);
3183 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return 0;
3187}
3188