blob: f36de829390be9da1aea1aeea3d439b2db87d40e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
Hans de Goede073e5702015-08-03 14:06:24 -070023#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "psmouse.h"
26#include "alps.h"
27
Seth Forshee25bded72011-11-07 19:53:36 -080028/*
29 * Definitions for ALPS version 3 and 4 command mode protocol
30 */
31#define ALPS_CMD_NIBBLE_10 0x01f2
32
Kevin Cernekeecd401202013-02-13 22:28:07 -080033#define ALPS_REG_BASE_RUSHMORE 0xc2c0
Pali Rohárdae928e2016-01-06 15:50:08 -080034#define ALPS_REG_BASE_V7 0xc2c0
Kevin Cernekeecd401202013-02-13 22:28:07 -080035#define ALPS_REG_BASE_PINNACLE 0x0000
36
Seth Forshee25bded72011-11-07 19:53:36 -080037static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
38 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
39 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
40 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
41 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
42 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
43 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
44 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
45 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
46 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
47 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
48 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
49 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
50 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
51 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
52 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
53 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
54};
55
56static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
57 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
58 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
59 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
60 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
61 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
62 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
63 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
64 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
65 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
66 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
67 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
68 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
69 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
70 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
71 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
72 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
73};
74
Yunkang Tang95f75e92013-12-01 22:33:52 -080075static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
76 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
77 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
78 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
79 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
80 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
81 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
82 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
83 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
84 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
85 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
86 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
87 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
88 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
89 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
90 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
91 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
92};
93
Seth Forshee25bded72011-11-07 19:53:36 -080094
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080095#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
96#define ALPS_PASS 0x04 /* device has a pass-through port */
97
98#define ALPS_WHEEL 0x08 /* hardware wheel present */
99#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
100#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
101#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800102#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
103 6-byte ALPS packet */
Hans de Goede19556212015-10-26 01:50:28 -0700104#define ALPS_STICK_BITS 0x100 /* separate stick button bits */
Yunkang Tang38088432014-07-26 13:51:41 -0700105#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Ben Gamari7ad8a102016-10-04 11:41:58 -0700106#define ALPS_DUALPOINT_WITH_PRESSURE 0x400 /* device can report trackpoint pressure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Helge Dellere38de672006-09-10 21:54:39 -0400108static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800109 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
110 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
111 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
113 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
114 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
115 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
117 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
118 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
119 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
120 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
121 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
122 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
123 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800124
125 /*
126 * XXX This entry is suspicious. First byte has zero lower nibble,
127 * which is what a normal mouse would report. Also, the value 0x0e
128 * isn't valid per PS/2 spec.
129 */
130 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
131
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800132 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
133 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800134 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800135 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
137 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
138 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
139 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
140 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800143static const struct alps_protocol_info alps_v3_protocol_data = {
144 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
145};
146
147static const struct alps_protocol_info alps_v3_rushmore_data = {
148 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
149};
150
Pali Rohár09c398b2017-03-07 09:41:34 -0800151static const struct alps_protocol_info alps_v4_protocol_data = {
152 ALPS_PROTO_V4, 0x8f, 0x8f, 0
153};
154
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800155static const struct alps_protocol_info alps_v5_protocol_data = {
156 ALPS_PROTO_V5, 0xc8, 0xd8, 0
157};
158
159static const struct alps_protocol_info alps_v7_protocol_data = {
160 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
161};
162
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700163static const struct alps_protocol_info alps_v8_protocol_data = {
164 ALPS_PROTO_V8, 0x18, 0x18, 0
165};
166
Hans de Goede19556212015-10-26 01:50:28 -0700167/*
168 * Some v2 models report the stick buttons in separate bits
169 */
170static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
171#if defined(CONFIG_DMI) && defined(CONFIG_X86)
172 {
173 /* Extrapolated from other entries */
174 .matches = {
175 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
176 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
177 },
178 },
179 {
180 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
181 .matches = {
182 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
183 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
184 },
185 },
186 {
187 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
188 .matches = {
189 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
190 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
191 },
192 },
193 {
194 /* Extrapolated from other entries */
195 .matches = {
196 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
197 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
198 },
199 },
200#endif
201 { }
202};
203
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800204static void alps_set_abs_params_st(struct alps_data *priv,
205 struct input_dev *dev1);
Hans de Goede688ea362015-05-20 14:42:50 -0700206static void alps_set_abs_params_semi_mt(struct alps_data *priv,
207 struct input_dev *dev1);
Masaki Ota8eccd392015-03-27 20:44:21 -0700208static void alps_set_abs_params_v7(struct alps_data *priv,
209 struct input_dev *dev1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700210static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
211 struct input_dev *dev1);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800212
Seth Forsheed4b347b2011-11-07 19:53:15 -0800213/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800215static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800216 unsigned char data)
217{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800218 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800219}
220
Pali Rohár04aae282015-01-14 13:18:30 -0800221static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800222 int left, int right, int middle)
223{
Martin Buckc91ed052010-03-13 22:23:58 -0800224 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800225
Martin Buckc91ed052010-03-13 22:23:58 -0800226 /*
227 * If shared button has already been reported on the
228 * other device (dev2) then this event should be also
229 * sent through that device.
230 */
Pali Rohár04aae282015-01-14 13:18:30 -0800231 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800232 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800233
Pali Rohár04aae282015-01-14 13:18:30 -0800234 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800235 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800236
Pali Rohár04aae282015-01-14 13:18:30 -0800237 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800238 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800239
Martin Buckc91ed052010-03-13 22:23:58 -0800240 /*
241 * Sync the _other_ device now, we'll do the first
242 * device later once we report the rest of the events.
243 */
Pali Rohár04aae282015-01-14 13:18:30 -0800244 if (dev2)
245 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800246}
247
Seth Forshee25bded72011-11-07 19:53:36 -0800248static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct alps_data *priv = psmouse->private;
251 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500252 struct input_dev *dev = psmouse->dev;
253 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500255 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800257 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400258 left = packet[2] & 0x10;
259 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 middle = 0;
261 x = packet[1] | ((packet[0] & 0x07) << 7);
262 y = packet[4] | ((packet[3] & 0x07) << 7);
263 z = packet[5];
264 } else {
265 left = packet[3] & 1;
266 right = packet[3] & 2;
267 middle = packet[3] & 4;
268 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
269 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
270 z = packet[5];
271 }
272
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800273 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400274 back = packet[0] & 0x10;
275 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500276 }
277
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800278 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500279 back = packet[3] & 4;
280 forward = packet[2] & 4;
281 if ((middle = forward && back))
282 forward = back = 0;
283 }
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 ges = packet[2] & 1;
286 fin = packet[2] & 2;
287
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800288 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
290 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700291
Pali Rohár04aae282015-01-14 13:18:30 -0800292 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 input_sync(dev2);
295 return;
296 }
297
Hans de Goede19556212015-10-26 01:50:28 -0700298 /* Some models have separate stick button bits */
299 if (priv->flags & ALPS_STICK_BITS) {
Hans de Goede92bac832015-04-12 15:42:35 -0700300 left |= packet[0] & 1;
301 right |= packet[0] & 2;
302 middle |= packet[0] & 4;
303 }
304
Pali Rohár04aae282015-01-14 13:18:30 -0800305 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800308 if (ges && !fin)
309 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /*
312 * A "tap and drag" operation is reported by the hardware as a transition
313 * from (!fin && ges) to (fin && ges). This should be translated to the
314 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
315 */
316 if (ges && fin && !priv->prev_fin) {
317 input_report_abs(dev, ABS_X, x);
318 input_report_abs(dev, ABS_Y, y);
319 input_report_abs(dev, ABS_PRESSURE, 0);
320 input_report_key(dev, BTN_TOOL_FINGER, 0);
321 input_sync(dev);
322 }
323 priv->prev_fin = fin;
324
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800325 if (z > 30)
326 input_report_key(dev, BTN_TOUCH, 1);
327 if (z < 25)
328 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (z > 0) {
331 input_report_abs(dev, ABS_X, x);
332 input_report_abs(dev, ABS_Y, y);
333 }
334
335 input_report_abs(dev, ABS_PRESSURE, z);
336 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
337
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800338 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500339 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800341 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500342 input_report_key(dev, BTN_FORWARD, forward);
343 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800346 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800347 input_report_key(dev, BTN_0, packet[2] & 4);
348 input_report_key(dev, BTN_1, packet[0] & 0x10);
349 input_report_key(dev, BTN_2, packet[3] & 4);
350 input_report_key(dev, BTN_3, packet[0] & 0x20);
351 }
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 input_sync(dev);
354}
355
Hans de Goede036e6c72014-07-25 22:38:51 -0700356static void alps_get_bitmap_points(unsigned int map,
357 struct alps_bitmap_point *low,
358 struct alps_bitmap_point *high,
359 int *fingers)
360{
361 struct alps_bitmap_point *point;
362 int i, bit, prev_bit = 0;
363
364 point = low;
365 for (i = 0; map != 0; i++, map >>= 1) {
366 bit = map & 1;
367 if (bit) {
368 if (!prev_bit) {
369 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700370 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700371 (*fingers)++;
372 }
373 point->num_bits++;
374 } else {
375 if (prev_bit)
376 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700377 }
378 prev_bit = bit;
379 }
380}
381
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800382/*
Hans de Goededccf1dd2015-05-20 14:43:09 -0700383 * Process bitmap data from semi-mt protocols. Returns the number of
Seth Forshee01ce6612011-11-07 19:54:13 -0800384 * fingers detected. A return value of 0 means at least one of the
385 * bitmaps was empty.
386 *
387 * The bitmaps don't have enough data to track fingers, so this function
388 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700389 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800390 * is greater than 0.
391 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800392static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700393 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800394{
Hans de Goede4dd26572015-05-20 14:41:10 -0700395 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
Seth Forshee01ce6612011-11-07 19:54:13 -0800396 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
397 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Hans de Goede4dd26572015-05-20 14:41:10 -0700398 struct input_mt_pos corner[4];
Seth Forshee01ce6612011-11-07 19:54:13 -0800399
Hans de Goede02d04252014-07-25 22:43:35 -0700400 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800401 return 0;
402
Hans de Goede02d04252014-07-25 22:43:35 -0700403 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
404 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800405
406 /*
407 * Fingers can overlap, so we use the maximum count of fingers
408 * on either axis as the finger count.
409 */
410 fingers = max(fingers_x, fingers_y);
411
412 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700413 * If an axis reports only a single contact, we have overlapping or
414 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800415 */
Hans de Goede20bea682014-07-25 22:33:33 -0700416 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700417 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700418 x_low.num_bits = x_low.num_bits - i;
419 x_high.start_bit = x_low.start_bit + i;
420 x_high.num_bits = max(i, 1);
421 }
422 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700423 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700424 y_low.num_bits = y_low.num_bits - i;
425 y_high.start_bit = y_low.start_bit + i;
426 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800427 }
428
Hans de Goede4dd26572015-05-20 14:41:10 -0700429 /* top-left corner */
430 corner[0].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700431 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
432 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700433 corner[0].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700434 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
435 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800436
Hans de Goede4dd26572015-05-20 14:41:10 -0700437 /* top-right corner */
438 corner[1].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700439 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
440 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700441 corner[1].y =
442 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
443 (2 * (priv->y_bits - 1));
444
445 /* bottom-right corner */
446 corner[2].x =
447 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
448 (2 * (priv->x_bits - 1));
449 corner[2].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700450 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
451 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800452
Hans de Goede4dd26572015-05-20 14:41:10 -0700453 /* bottom-left corner */
454 corner[3].x =
455 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
456 (2 * (priv->x_bits - 1));
457 corner[3].y =
Seth Forshee01ce6612011-11-07 19:54:13 -0800458 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
459 (2 * (priv->y_bits - 1));
460
Hans de Goededccf1dd2015-05-20 14:43:09 -0700461 /* x-bitmap order is reversed on v5 touchpads */
462 if (priv->proto_version == ALPS_PROTO_V5) {
463 for (i = 0; i < 4; i++)
464 corner[i].x = priv->x_max - corner[i].x;
Hans de Goede40e8f532014-07-25 22:37:15 -0700465 }
466
Hans de Goededccf1dd2015-05-20 14:43:09 -0700467 /* y-bitmap order is reversed on v3 and v4 touchpads */
468 if (priv->proto_version == ALPS_PROTO_V3 ||
469 priv->proto_version == ALPS_PROTO_V4) {
Hans de Goede4dd26572015-05-20 14:41:10 -0700470 for (i = 0; i < 4; i++)
471 corner[i].y = priv->y_max - corner[i].y;
Seth Forshee01ce6612011-11-07 19:54:13 -0800472 }
473
Hans de Goede4dd26572015-05-20 14:41:10 -0700474 /*
475 * We only select a corner for the second touch once per 2 finger
476 * touch sequence to avoid the chosen corner (and thus the coordinates)
477 * jumping around when the first touch is in the middle.
478 */
479 if (priv->second_touch == -1) {
480 /* Find corner closest to our st coordinates */
481 closest = 0x7fffffff;
482 for (i = 0; i < 4; i++) {
483 int dx = fields->st.x - corner[i].x;
484 int dy = fields->st.y - corner[i].y;
485 int distance = dx * dx + dy * dy;
486
487 if (distance < closest) {
488 priv->second_touch = i;
489 closest = distance;
490 }
491 }
492 /* And select the opposite corner to use for the 2nd touch */
493 priv->second_touch = (priv->second_touch + 2) % 4;
494 }
495
496 fields->mt[0] = fields->st;
497 fields->mt[1] = corner[priv->second_touch];
498
Seth Forshee01ce6612011-11-07 19:54:13 -0800499 return fingers;
500}
501
Hans de Goedecdf333e2014-07-25 22:44:42 -0700502static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800503{
504 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700505 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
506 input_report_abs(dev, ABS_MT_POSITION_X, x);
507 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800508}
509
Hans de Goedecdf333e2014-07-25 22:44:42 -0700510static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800511{
Hans de Goede02d04252014-07-25 22:43:35 -0700512 struct alps_data *priv = psmouse->private;
513 struct input_dev *dev = psmouse->dev;
514 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700515 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700516
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800517 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700518 for (i = 0; i < n; i++)
519 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
520
521 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800522}
523
Hans de Goede68c21872014-07-25 22:47:25 -0700524static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
525{
526 struct alps_data *priv = psmouse->private;
527 struct input_dev *dev = psmouse->dev;
528 struct alps_fields *f = &priv->f;
529
530 /* Use st data when we don't have mt data */
531 if (fingers < 2) {
532 f->mt[0].x = f->st.x;
533 f->mt[0].y = f->st.y;
534 fingers = f->pressure > 0 ? 1 : 0;
Hans de Goede4dd26572015-05-20 14:41:10 -0700535 priv->second_touch = -1;
Hans de Goede68c21872014-07-25 22:47:25 -0700536 }
537
Hans de Goede1662c0332015-05-20 14:42:15 -0700538 if (fingers >= 1)
539 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
540 if (fingers >= 2)
541 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
542 input_mt_sync_frame(dev);
Hans de Goede68c21872014-07-25 22:47:25 -0700543
544 input_mt_report_finger_count(dev, fingers);
545
546 input_report_key(dev, BTN_LEFT, f->left);
547 input_report_key(dev, BTN_RIGHT, f->right);
548 input_report_key(dev, BTN_MIDDLE, f->middle);
549
550 input_report_abs(dev, ABS_PRESSURE, f->pressure);
551
552 input_sync(dev);
553}
554
Seth Forshee25bded72011-11-07 19:53:36 -0800555static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
556{
557 struct alps_data *priv = psmouse->private;
558 unsigned char *packet = psmouse->packet;
559 struct input_dev *dev = priv->dev2;
560 int x, y, z, left, right, middle;
561
Pali Rohár34412ba2015-01-09 12:48:58 -0800562 /* It should be a DualPoint when received trackstick packet */
563 if (!(priv->flags & ALPS_DUALPOINT)) {
564 psmouse_warn(psmouse,
565 "Rejected trackstick packet from non DualPoint device");
566 return;
567 }
568
Seth Forshee25bded72011-11-07 19:53:36 -0800569 /* Sanity check packet */
570 if (!(packet[0] & 0x40)) {
571 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
572 return;
573 }
574
575 /*
576 * There's a special packet that seems to indicate the end
577 * of a stream of trackstick data. Filter these out.
578 */
579 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
580 return;
581
582 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
583 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
584 z = (packet[4] & 0x7c) >> 2;
585
586 /*
587 * The x and y values tend to be quite large, and when used
588 * alone the trackstick is difficult to use. Scale them down
589 * to compensate.
590 */
591 x /= 8;
592 y /= 8;
593
594 input_report_rel(dev, REL_X, x);
595 input_report_rel(dev, REL_Y, -y);
596
597 /*
598 * Most ALPS models report the trackstick buttons in the touchpad
599 * packets, but a few report them here. No reliable way has been
600 * found to differentiate between the models upfront, so we enable
601 * the quirk in response to seeing a button press in the trackstick
602 * packet.
603 */
604 left = packet[3] & 0x01;
605 right = packet[3] & 0x02;
606 middle = packet[3] & 0x04;
607
608 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
609 (left || right || middle))
610 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
611
612 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
613 input_report_key(dev, BTN_LEFT, left);
614 input_report_key(dev, BTN_RIGHT, right);
615 input_report_key(dev, BTN_MIDDLE, middle);
616 }
617
618 input_sync(dev);
619 return;
620}
621
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800622static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
623{
624 f->left = !!(p[3] & 0x01);
625 f->right = !!(p[3] & 0x02);
626 f->middle = !!(p[3] & 0x04);
627
628 f->ts_left = !!(p[3] & 0x10);
629 f->ts_right = !!(p[3] & 0x20);
630 f->ts_middle = !!(p[3] & 0x40);
631}
632
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700633static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800634 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800635{
636 f->first_mp = !!(p[4] & 0x40);
637 f->is_mp = !!(p[0] & 0x40);
638
Hans de Goedea839cd52015-05-20 14:39:21 -0700639 if (f->is_mp) {
640 f->fingers = (p[5] & 0x3) + 1;
641 f->x_map = ((p[4] & 0x7e) << 8) |
642 ((p[1] & 0x7f) << 2) |
643 ((p[0] & 0x30) >> 4);
644 f->y_map = ((p[3] & 0x70) << 4) |
645 ((p[2] & 0x7f) << 1) |
646 (p[4] & 0x01);
647 } else {
648 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
649 ((p[0] & 0x30) >> 4);
650 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
651 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800652
Hans de Goedea839cd52015-05-20 14:39:21 -0700653 alps_decode_buttons_v3(f, p);
654 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700655
656 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800657}
658
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700659static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800660 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800661{
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700662 f->first_mp = !!(p[4] & 0x40);
Yunkang Tangf105e342014-07-25 22:29:24 -0700663 f->is_mp = !!(p[5] & 0x40);
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700664
Hans de Goedea839cd52015-05-20 14:39:21 -0700665 if (f->is_mp) {
666 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
667 f->x_map = ((p[5] & 0x10) << 11) |
668 ((p[4] & 0x7e) << 8) |
669 ((p[1] & 0x7f) << 2) |
670 ((p[0] & 0x30) >> 4);
671 f->y_map = ((p[5] & 0x20) << 6) |
672 ((p[3] & 0x70) << 4) |
673 ((p[2] & 0x7f) << 1) |
674 (p[4] & 0x01);
675 } else {
676 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
677 ((p[0] & 0x30) >> 4);
678 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
679 f->pressure = p[5] & 0x7f;
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700680
Hans de Goedea839cd52015-05-20 14:39:21 -0700681 alps_decode_buttons_v3(f, p);
682 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700683
684 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800685}
686
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700687static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800688 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800689{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800690 u64 palm_data = 0;
691 struct alps_data *priv = psmouse->private;
692
Dave Turvene75af9e52013-02-21 22:58:28 -0800693 f->first_mp = !!(p[0] & 0x02);
694 f->is_mp = !!(p[0] & 0x20);
695
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800696 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700697 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
698 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
699 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800700 alps_decode_buttons_v3(f, p);
701 } else {
702 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800703 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800704
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800705 palm_data = (p[1] & 0x7f) |
706 ((p[2] & 0x7f) << 7) |
707 ((p[4] & 0x7f) << 14) |
708 ((p[5] & 0x7f) << 21) |
709 ((p[3] & 0x07) << 28) |
710 (((u64)p[3] & 0x70) << 27) |
711 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800712
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800713 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
714 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
715
716 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
717 f->x_map = (palm_data >> priv->y_bits) &
718 (BIT(priv->x_bits) - 1);
719 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700720
721 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800722}
723
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800724static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800725{
726 struct alps_data *priv = psmouse->private;
727 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800728 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700729 struct alps_fields *f = &priv->f;
730 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800731
Hans de Goede02d04252014-07-25 22:43:35 -0700732 memset(f, 0, sizeof(*f));
733
734 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800735
736 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800737 * There's no single feature of touchpad position and bitmap packets
738 * that can be used to distinguish between them. We rely on the fact
739 * that a bitmap packet should always follow a position packet with
740 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800741 */
742 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800743 /*
744 * Sometimes a position packet will indicate a multi-packet
745 * sequence, but then what follows is another position
746 * packet. Check for this, and when it happens process the
747 * position packet as usual.
748 */
Hans de Goede02d04252014-07-25 22:43:35 -0700749 if (f->is_mp) {
750 fingers = f->fingers;
Hans de Goede44b77f32015-05-20 14:40:06 -0700751 /*
752 * Bitmap processing uses position packet's coordinate
753 * data, so we need to do decode it first.
754 */
755 priv->decode_fields(f, priv->multi_data, psmouse);
Hans de Goededccf1dd2015-05-20 14:43:09 -0700756 if (alps_process_bitmap(priv, f) == 0)
757 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800758 } else {
759 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800760 }
761 }
762
Seth Forshee01ce6612011-11-07 19:54:13 -0800763 /*
764 * Bit 6 of byte 0 is not usually set in position packets. The only
765 * times it seems to be set is in situations where the data is
766 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
767 * this combined with the fact that this bit is useful for filtering
768 * out misidentified bitmap packets, we reject anything with this
769 * bit set.
770 */
Hans de Goede02d04252014-07-25 22:43:35 -0700771 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800772 return;
773
Hans de Goede02d04252014-07-25 22:43:35 -0700774 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800775 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800776 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
777 return;
778 }
779
780 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800781
Seth Forshee25bded72011-11-07 19:53:36 -0800782 /*
783 * Sometimes the hardware sends a single packet with z = 0
784 * in the middle of a stream. Real releases generate packets
785 * with x, y, and z all zero, so these seem to be flukes.
786 * Ignore them.
787 */
Hans de Goede02d04252014-07-25 22:43:35 -0700788 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800789 return;
790
Hans de Goede68c21872014-07-25 22:47:25 -0700791 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800792
Pali Rohár34412ba2015-01-09 12:48:58 -0800793 if ((priv->flags & ALPS_DUALPOINT) &&
794 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700795 input_report_key(dev2, BTN_LEFT, f->ts_left);
796 input_report_key(dev2, BTN_RIGHT, f->ts_right);
797 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800798 input_sync(dev2);
799 }
800}
801
802static void alps_process_packet_v3(struct psmouse *psmouse)
803{
804 unsigned char *packet = psmouse->packet;
805
806 /*
807 * v3 protocol packets come in three types, two representing
808 * touchpad data and one representing trackstick data.
809 * Trackstick packets seem to be distinguished by always
810 * having 0x3f in the last byte. This value has never been
811 * observed in the last byte of either of the other types
812 * of packets.
813 */
814 if (packet[5] == 0x3f) {
815 alps_process_trackstick_packet_v3(psmouse);
816 return;
817 }
818
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800819 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800820}
821
Yunkang Tang95f75e92013-12-01 22:33:52 -0800822static void alps_process_packet_v6(struct psmouse *psmouse)
823{
824 struct alps_data *priv = psmouse->private;
825 unsigned char *packet = psmouse->packet;
826 struct input_dev *dev = psmouse->dev;
827 struct input_dev *dev2 = priv->dev2;
828 int x, y, z, left, right, middle;
829
830 /*
831 * We can use Byte5 to distinguish if the packet is from Touchpad
832 * or Trackpoint.
833 * Touchpad: 0 - 0x7E
834 * Trackpoint: 0x7F
835 */
836 if (packet[5] == 0x7F) {
837 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800838 if (!(priv->flags & ALPS_DUALPOINT)) {
839 psmouse_warn(psmouse,
840 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800841 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800842 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800843
844 /* Trackpoint packet */
845 x = packet[1] | ((packet[3] & 0x20) << 2);
846 y = packet[2] | ((packet[3] & 0x40) << 1);
847 z = packet[4];
848 left = packet[3] & 0x01;
849 right = packet[3] & 0x02;
850 middle = packet[3] & 0x04;
851
852 /* To prevent the cursor jump when finger lifted */
853 if (x == 0x7F && y == 0x7F && z == 0x7F)
854 x = y = z = 0;
855
856 /* Divide 4 since trackpoint's speed is too fast */
857 input_report_rel(dev2, REL_X, (char)x / 4);
858 input_report_rel(dev2, REL_Y, -((char)y / 4));
859
860 input_report_key(dev2, BTN_LEFT, left);
861 input_report_key(dev2, BTN_RIGHT, right);
862 input_report_key(dev2, BTN_MIDDLE, middle);
863
864 input_sync(dev2);
865 return;
866 }
867
868 /* Touchpad packet */
869 x = packet[1] | ((packet[3] & 0x78) << 4);
870 y = packet[2] | ((packet[4] & 0x78) << 4);
871 z = packet[5];
872 left = packet[3] & 0x01;
873 right = packet[3] & 0x02;
874
875 if (z > 30)
876 input_report_key(dev, BTN_TOUCH, 1);
877 if (z < 25)
878 input_report_key(dev, BTN_TOUCH, 0);
879
880 if (z > 0) {
881 input_report_abs(dev, ABS_X, x);
882 input_report_abs(dev, ABS_Y, y);
883 }
884
885 input_report_abs(dev, ABS_PRESSURE, z);
886 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
887
888 /* v6 touchpad does not have middle button */
889 input_report_key(dev, BTN_LEFT, left);
890 input_report_key(dev, BTN_RIGHT, right);
891
892 input_sync(dev);
893}
894
Seth Forshee25bded72011-11-07 19:53:36 -0800895static void alps_process_packet_v4(struct psmouse *psmouse)
896{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700897 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800898 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700899 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700900 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700901
902 /*
903 * v4 has a 6-byte encoding for bitmap data, but this data is
904 * broken up between 3 normal packets. Use priv->multi_packet to
905 * track our position in the bitmap packet.
906 */
907 if (packet[6] & 0x40) {
908 /* sync, reset position */
909 priv->multi_packet = 0;
910 }
911
912 if (WARN_ON_ONCE(priv->multi_packet > 2))
913 return;
914
915 offset = 2 * priv->multi_packet;
916 priv->multi_data[offset] = packet[6];
917 priv->multi_data[offset + 1] = packet[7];
918
Hans de Goede44b77f32015-05-20 14:40:06 -0700919 f->left = !!(packet[4] & 0x01);
920 f->right = !!(packet[4] & 0x02);
921
922 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
923 ((packet[0] & 0x30) >> 4);
924 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
925 f->pressure = packet[5] & 0x7f;
926
George Pantalos3b7e09f2012-05-10 22:31:59 -0700927 if (++priv->multi_packet > 2) {
928 priv->multi_packet = 0;
929
Hans de Goede02d04252014-07-25 22:43:35 -0700930 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700931 ((priv->multi_data[3] & 0x60) << 3) |
932 ((priv->multi_data[0] & 0x3f) << 2) |
933 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700934 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700935 ((priv->multi_data[3] & 0x1f) << 5) |
936 (priv->multi_data[1] & 0x1f);
937
Hans de Goede02d04252014-07-25 22:43:35 -0700938 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700939 }
Seth Forshee25bded72011-11-07 19:53:36 -0800940
Hans de Goede68c21872014-07-25 22:47:25 -0700941 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800942}
943
Yunkang Tang38088432014-07-26 13:51:41 -0700944static bool alps_is_valid_package_v7(struct psmouse *psmouse)
945{
946 switch (psmouse->pktcnt) {
947 case 3:
948 return (psmouse->packet[2] & 0x40) == 0x40;
949 case 4:
950 return (psmouse->packet[3] & 0x48) == 0x48;
951 case 6:
952 return (psmouse->packet[5] & 0x40) == 0x00;
953 }
954 return true;
955}
956
957static unsigned char alps_get_packet_id_v7(char *byte)
958{
959 unsigned char packet_id;
960
961 if (byte[4] & 0x40)
962 packet_id = V7_PACKET_ID_TWO;
963 else if (byte[4] & 0x01)
964 packet_id = V7_PACKET_ID_MULTI;
965 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
966 packet_id = V7_PACKET_ID_NEW;
967 else if (byte[1] == 0x00 && byte[4] == 0x00)
968 packet_id = V7_PACKET_ID_IDLE;
969 else
970 packet_id = V7_PACKET_ID_UNKNOWN;
971
972 return packet_id;
973}
974
975static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
976 unsigned char *pkt,
977 unsigned char pkt_id)
978{
979 mt[0].x = ((pkt[2] & 0x80) << 4);
980 mt[0].x |= ((pkt[2] & 0x3F) << 5);
981 mt[0].x |= ((pkt[3] & 0x30) >> 1);
982 mt[0].x |= (pkt[3] & 0x07);
983 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
984
985 mt[1].x = ((pkt[3] & 0x80) << 4);
986 mt[1].x |= ((pkt[4] & 0x80) << 3);
987 mt[1].x |= ((pkt[4] & 0x3F) << 4);
988 mt[1].y = ((pkt[5] & 0x80) << 3);
989 mt[1].y |= ((pkt[5] & 0x3F) << 4);
990
991 switch (pkt_id) {
992 case V7_PACKET_ID_TWO:
993 mt[1].x &= ~0x000F;
994 mt[1].y |= 0x000F;
Hans de Goede72eceab2015-05-20 14:37:41 -0700995 /* Detect false-postive touches where x & y report max value */
996 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
997 mt[1].x = 0;
998 /* y gets set to 0 at the end of this function */
999 }
Yunkang Tang38088432014-07-26 13:51:41 -07001000 break;
1001
1002 case V7_PACKET_ID_MULTI:
1003 mt[1].x &= ~0x003F;
1004 mt[1].y &= ~0x0020;
1005 mt[1].y |= ((pkt[4] & 0x02) << 4);
1006 mt[1].y |= 0x001F;
1007 break;
1008
1009 case V7_PACKET_ID_NEW:
1010 mt[1].x &= ~0x003F;
1011 mt[1].x |= (pkt[0] & 0x20);
1012 mt[1].y |= 0x000F;
1013 break;
1014 }
1015
1016 mt[0].y = 0x7FF - mt[0].y;
1017 mt[1].y = 0x7FF - mt[1].y;
1018}
1019
1020static int alps_get_mt_count(struct input_mt_pos *mt)
1021{
Hans de Goede7091c442014-12-18 09:53:34 -08001022 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -07001023
Hans de Goede7091c442014-12-18 09:53:34 -08001024 for (i = 0; i < MAX_TOUCHES; i++) {
1025 if (mt[i].x != 0 || mt[i].y != 0)
1026 fingers++;
1027 }
Yunkang Tang38088432014-07-26 13:51:41 -07001028
Hans de Goede7091c442014-12-18 09:53:34 -08001029 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -07001030}
1031
1032static int alps_decode_packet_v7(struct alps_fields *f,
1033 unsigned char *p,
1034 struct psmouse *psmouse)
1035{
Hans de Goeded27eb792014-12-18 09:55:14 -08001036 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -07001037 unsigned char pkt_id;
1038
1039 pkt_id = alps_get_packet_id_v7(p);
1040 if (pkt_id == V7_PACKET_ID_IDLE)
1041 return 0;
1042 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1043 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -08001044 /*
1045 * NEW packets are send to indicate a discontinuity in the finger
1046 * coordinate reporting. Specifically a finger may have moved from
1047 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1048 * us.
1049 *
1050 * NEW packets have 3 problems:
1051 * 1) They do not contain middle / right button info (on non clickpads)
1052 * this can be worked around by preserving the old button state
1053 * 2) They do not contain an accurate fingercount, and they are
1054 * typically send when the number of fingers changes. We cannot use
1055 * the old finger count as that may mismatch with the amount of
1056 * touch coordinates we've available in the NEW packet
1057 * 3) Their x data for the second touch is inaccurate leading to
1058 * a possible jump of the x coordinate by 16 units when the first
1059 * non NEW packet comes in
1060 * Since problems 2 & 3 cannot be worked around, just ignore them.
1061 */
1062 if (pkt_id == V7_PACKET_ID_NEW)
1063 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001064
1065 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1066
Yunkang Tang38088432014-07-26 13:51:41 -07001067 if (pkt_id == V7_PACKET_ID_TWO)
1068 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001069 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001070 f->fingers = 3 + (p[5] & 0x03);
1071
Hans de Goeded27eb792014-12-18 09:55:14 -08001072 f->left = (p[0] & 0x80) >> 7;
1073 if (priv->flags & ALPS_BUTTONPAD) {
1074 if (p[0] & 0x20)
1075 f->fingers++;
1076 if (p[0] & 0x10)
1077 f->fingers++;
1078 } else {
1079 f->right = (p[0] & 0x20) >> 5;
1080 f->middle = (p[0] & 0x10) >> 4;
1081 }
1082
Hans de Goede7091c442014-12-18 09:53:34 -08001083 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1084 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1085 f->mt[0].x = f->mt[1].x;
1086 f->mt[0].y = f->mt[1].y;
1087 f->mt[1].x = 0;
1088 f->mt[1].y = 0;
1089 }
1090
Yunkang Tang38088432014-07-26 13:51:41 -07001091 return 0;
1092}
1093
1094static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1095{
1096 struct alps_data *priv = psmouse->private;
1097 unsigned char *packet = psmouse->packet;
1098 struct input_dev *dev2 = priv->dev2;
1099 int x, y, z, left, right, middle;
1100
Pali Rohár34412ba2015-01-09 12:48:58 -08001101 /* It should be a DualPoint when received trackstick packet */
1102 if (!(priv->flags & ALPS_DUALPOINT)) {
1103 psmouse_warn(psmouse,
1104 "Rejected trackstick packet from non DualPoint device");
1105 return;
1106 }
1107
Yunkang Tang38088432014-07-26 13:51:41 -07001108 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1109 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1110 ((packet[3] & 0x20) << 1);
1111 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1112
1113 left = (packet[1] & 0x01);
1114 right = (packet[1] & 0x02) >> 1;
1115 middle = (packet[1] & 0x04) >> 2;
1116
Hans de Goede088df2c2015-06-04 22:31:43 -07001117 input_report_rel(dev2, REL_X, (char)x);
1118 input_report_rel(dev2, REL_Y, -((char)y));
Yunkang Tang38088432014-07-26 13:51:41 -07001119
1120 input_report_key(dev2, BTN_LEFT, left);
1121 input_report_key(dev2, BTN_RIGHT, right);
1122 input_report_key(dev2, BTN_MIDDLE, middle);
1123
1124 input_sync(dev2);
1125}
1126
1127static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1128{
1129 struct alps_data *priv = psmouse->private;
1130 struct input_dev *dev = psmouse->dev;
1131 struct alps_fields *f = &priv->f;
1132
1133 memset(f, 0, sizeof(*f));
1134
1135 if (priv->decode_fields(f, psmouse->packet, psmouse))
1136 return;
1137
1138 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1139
1140 input_mt_report_finger_count(dev, f->fingers);
1141
1142 input_report_key(dev, BTN_LEFT, f->left);
1143 input_report_key(dev, BTN_RIGHT, f->right);
1144 input_report_key(dev, BTN_MIDDLE, f->middle);
1145
1146 input_sync(dev);
1147}
1148
1149static void alps_process_packet_v7(struct psmouse *psmouse)
1150{
1151 unsigned char *packet = psmouse->packet;
1152
1153 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1154 alps_process_trackstick_packet_v7(psmouse);
1155 else
1156 alps_process_touchpad_packet_v7(psmouse);
1157}
1158
Paul Donohue23fce362016-11-28 20:16:21 -08001159static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001160{
Paul Donohue23fce362016-11-28 20:16:21 -08001161 enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001162
Ben Gamari4777ac22016-10-04 11:33:25 -07001163 switch (byte[3] & 0x30) {
1164 case 0x00:
Paul Donohue23fce362016-11-28 20:16:21 -08001165 if (SS4_IS_IDLE_V2(byte)) {
Ben Gamari4777ac22016-10-04 11:33:25 -07001166 pkt_id = SS4_PACKET_ID_IDLE;
1167 } else {
1168 pkt_id = SS4_PACKET_ID_ONE;
1169 }
1170 break;
1171 case 0x10:
1172 /* two-finger finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001173 pkt_id = SS4_PACKET_ID_TWO;
Ben Gamari4777ac22016-10-04 11:33:25 -07001174 break;
1175 case 0x20:
1176 /* stick pointer */
1177 pkt_id = SS4_PACKET_ID_STICK;
1178 break;
1179 case 0x30:
1180 /* third and fourth finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001181 pkt_id = SS4_PACKET_ID_MULTI;
Ben Gamari4777ac22016-10-04 11:33:25 -07001182 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001183 }
1184
1185 return pkt_id;
1186}
1187
1188static int alps_decode_ss4_v2(struct alps_fields *f,
1189 unsigned char *p, struct psmouse *psmouse)
1190{
1191 struct alps_data *priv = psmouse->private;
Paul Donohue23fce362016-11-28 20:16:21 -08001192 enum SS4_PACKET_ID pkt_id;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001193 unsigned int no_data_x, no_data_y;
1194
1195 pkt_id = alps_get_pkt_id_ss4_v2(p);
1196
1197 /* Current packet is 1Finger coordinate packet */
1198 switch (pkt_id) {
1199 case SS4_PACKET_ID_ONE:
1200 f->mt[0].x = SS4_1F_X_V2(p);
1201 f->mt[0].y = SS4_1F_Y_V2(p);
1202 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
Ben Gamaria8317762016-10-04 11:40:44 -07001203 /*
1204 * When a button is held the device will give us events
1205 * with x, y, and pressure of 0. This causes annoying jumps
1206 * if a touch is released while the button is held.
1207 * Handle this by claiming zero contacts.
1208 */
1209 f->fingers = f->pressure > 0 ? 1 : 0;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001210 f->first_mp = 0;
1211 f->is_mp = 0;
1212 break;
1213
1214 case SS4_PACKET_ID_TWO:
1215 if (priv->flags & ALPS_BUTTONPAD) {
1216 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1217 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1218 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1219 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1220 } else {
1221 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1222 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1223 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1224 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1225 }
1226 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1227
1228 if (SS4_IS_MF_CONTINUE(p)) {
1229 f->first_mp = 1;
1230 } else {
1231 f->fingers = 2;
1232 f->first_mp = 0;
1233 }
1234 f->is_mp = 0;
1235
1236 break;
1237
1238 case SS4_PACKET_ID_MULTI:
1239 if (priv->flags & ALPS_BUTTONPAD) {
1240 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1241 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1242 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1243 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1244 no_data_x = SS4_MFPACKET_NO_AX_BL;
1245 no_data_y = SS4_MFPACKET_NO_AY_BL;
1246 } else {
1247 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1248 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1249 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1250 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1251 no_data_x = SS4_MFPACKET_NO_AX;
1252 no_data_y = SS4_MFPACKET_NO_AY;
1253 }
1254
1255 f->first_mp = 0;
1256 f->is_mp = 1;
1257
1258 if (SS4_IS_5F_DETECTED(p)) {
1259 f->fingers = 5;
1260 } else if (f->mt[3].x == no_data_x &&
1261 f->mt[3].y == no_data_y) {
1262 f->mt[3].x = 0;
1263 f->mt[3].y = 0;
1264 f->fingers = 3;
1265 } else {
1266 f->fingers = 4;
1267 }
1268 break;
1269
Ben Gamari4777ac22016-10-04 11:33:25 -07001270 case SS4_PACKET_ID_STICK:
Paul Donohue7229c582016-11-28 20:13:47 -08001271 /*
1272 * x, y, and pressure are decoded in
1273 * alps_process_packet_ss4_v2()
1274 */
1275 f->first_mp = 0;
1276 f->is_mp = 0;
Ben Gamari4777ac22016-10-04 11:33:25 -07001277 break;
1278
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001279 case SS4_PACKET_ID_IDLE:
1280 default:
1281 memset(f, 0, sizeof(struct alps_fields));
1282 break;
1283 }
1284
Ben Gamari4777ac22016-10-04 11:33:25 -07001285 /* handle buttons */
1286 if (pkt_id == SS4_PACKET_ID_STICK) {
1287 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1288 if (!(priv->flags & ALPS_BUTTONPAD)) {
1289 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1290 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1291 }
1292 } else {
1293 f->left = !!(SS4_BTN_V2(p) & 0x01);
1294 if (!(priv->flags & ALPS_BUTTONPAD)) {
1295 f->right = !!(SS4_BTN_V2(p) & 0x02);
1296 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1297 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001298 }
1299
1300 return 0;
1301}
1302
1303static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1304{
1305 struct alps_data *priv = psmouse->private;
1306 unsigned char *packet = psmouse->packet;
1307 struct input_dev *dev = psmouse->dev;
Ben Gamari4777ac22016-10-04 11:33:25 -07001308 struct input_dev *dev2 = priv->dev2;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001309 struct alps_fields *f = &priv->f;
1310
1311 memset(f, 0, sizeof(struct alps_fields));
1312 priv->decode_fields(f, packet, psmouse);
1313 if (priv->multi_packet) {
1314 /*
1315 * Sometimes the first packet will indicate a multi-packet
1316 * sequence, but sometimes the next multi-packet would not
1317 * come. Check for this, and when it happens process the
1318 * position packet as usual.
1319 */
1320 if (f->is_mp) {
1321 /* Now process the 1st packet */
1322 priv->decode_fields(f, priv->multi_data, psmouse);
1323 } else {
1324 priv->multi_packet = 0;
1325 }
1326 }
1327
1328 /*
1329 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1330 * When it is set, it means 2nd packet comes without 1st packet come.
1331 */
1332 if (f->is_mp)
1333 return;
1334
1335 /* Save the first packet */
1336 if (!priv->multi_packet && f->first_mp) {
1337 priv->multi_packet = 1;
1338 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1339 return;
1340 }
1341
1342 priv->multi_packet = 0;
1343
Paul Donohue864db922016-11-28 20:11:25 -08001344 /* Report trackstick */
1345 if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
Paul Donohue7229c582016-11-28 20:13:47 -08001346 if (!(priv->flags & ALPS_DUALPOINT)) {
1347 psmouse_warn(psmouse,
1348 "Rejected trackstick packet from non DualPoint device");
1349 return;
Paul Donohue864db922016-11-28 20:11:25 -08001350 }
Paul Donohue7229c582016-11-28 20:13:47 -08001351
Paul Donohue23fce362016-11-28 20:16:21 -08001352 input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
1353 input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
1354 input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
Paul Donohue7229c582016-11-28 20:13:47 -08001355
1356 input_report_key(dev2, BTN_LEFT, f->ts_left);
1357 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1358 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1359
1360 input_sync(dev2);
Paul Donohue864db922016-11-28 20:11:25 -08001361 return;
1362 }
1363
1364 /* Report touchpad */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001365 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1366
1367 input_mt_report_finger_count(dev, f->fingers);
1368
1369 input_report_key(dev, BTN_LEFT, f->left);
1370 input_report_key(dev, BTN_RIGHT, f->right);
1371 input_report_key(dev, BTN_MIDDLE, f->middle);
1372
1373 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1374 input_sync(dev);
1375}
1376
1377static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1378{
1379 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1380 return false;
1381 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1382 return false;
1383 return true;
1384}
1385
Pali Rohár04aae282015-01-14 13:18:30 -08001386static DEFINE_MUTEX(alps_mutex);
1387
1388static void alps_register_bare_ps2_mouse(struct work_struct *work)
1389{
1390 struct alps_data *priv =
1391 container_of(work, struct alps_data, dev3_register_work.work);
1392 struct psmouse *psmouse = priv->psmouse;
1393 struct input_dev *dev3;
1394 int error = 0;
1395
1396 mutex_lock(&alps_mutex);
1397
1398 if (priv->dev3)
1399 goto out;
1400
1401 dev3 = input_allocate_device();
1402 if (!dev3) {
1403 psmouse_err(psmouse, "failed to allocate secondary device\n");
1404 error = -ENOMEM;
1405 goto out;
1406 }
1407
1408 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1409 psmouse->ps2dev.serio->phys,
1410 (priv->dev2 ? "input2" : "input1"));
1411 dev3->phys = priv->phys3;
1412
1413 /*
1414 * format of input device name is: "protocol vendor name"
1415 * see function psmouse_switch_protocol() in psmouse-base.c
1416 */
1417 dev3->name = "PS/2 ALPS Mouse";
1418
1419 dev3->id.bustype = BUS_I8042;
1420 dev3->id.vendor = 0x0002;
1421 dev3->id.product = PSMOUSE_PS2;
1422 dev3->id.version = 0x0000;
1423 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1424
1425 input_set_capability(dev3, EV_REL, REL_X);
1426 input_set_capability(dev3, EV_REL, REL_Y);
1427 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1428 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1429 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1430
1431 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1432
1433 error = input_register_device(dev3);
1434 if (error) {
1435 psmouse_err(psmouse,
1436 "failed to register secondary device: %d\n",
1437 error);
1438 input_free_device(dev3);
1439 goto out;
1440 }
1441
1442 priv->dev3 = dev3;
1443
1444out:
1445 /*
1446 * Save the error code so that we can detect that we
1447 * already tried to create the device.
1448 */
1449 if (error)
1450 priv->dev3 = ERR_PTR(error);
1451
1452 mutex_unlock(&alps_mutex);
1453}
1454
Hans de Goede59c30af2015-04-03 17:14:40 -07001455static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001456 unsigned char packet[],
1457 bool report_buttons)
1458{
Hans de Goede59c30af2015-04-03 17:14:40 -07001459 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001460 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001461
Hans de Goedee3a79212015-04-03 17:20:05 -07001462 /* Figure out which device to use to report the bare packet */
1463 if (priv->proto_version == ALPS_PROTO_V2 &&
1464 (priv->flags & ALPS_DUALPOINT)) {
1465 /* On V2 devices the DualPoint Stick reports bare packets */
1466 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001467 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001468 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001469 /* Register dev3 mouse if we received PS/2 packet first time */
1470 if (!IS_ERR(priv->dev3))
1471 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1472 0);
1473 return;
1474 } else {
1475 dev = priv->dev3;
1476 }
1477
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001478 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001479 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001480 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1481
Pali Rohár04aae282015-01-14 13:18:30 -08001482 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001483 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001484 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001485 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1486
Pali Rohár04aae282015-01-14 13:18:30 -08001487 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001488}
1489
1490static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct alps_data *priv = psmouse->private;
1493
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001494 if (psmouse->pktcnt < 6)
1495 return PSMOUSE_GOOD_DATA;
1496
1497 if (psmouse->pktcnt == 6) {
1498 /*
1499 * Start a timer to flush the packet if it ends up last
1500 * 6-byte packet in the stream. Timer needs to fire
1501 * psmouse core times out itself. 20 ms should be enough
1502 * to decide if we are getting more data or not.
1503 */
1504 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1505 return PSMOUSE_GOOD_DATA;
1506 }
1507
1508 del_timer(&priv->timer);
1509
1510 if (psmouse->packet[6] & 0x80) {
1511
1512 /*
1513 * Highest bit is set - that means we either had
1514 * complete ALPS packet and this is start of the
1515 * next packet or we got garbage.
1516 */
1517
1518 if (((psmouse->packet[3] |
1519 psmouse->packet[4] |
1520 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001521 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001522 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001523 "refusing packet %4ph (suspected interleaved ps/2)\n",
1524 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001525 return PSMOUSE_BAD_DATA;
1526 }
1527
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001528 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001529
1530 /* Continue with the next packet */
1531 psmouse->packet[0] = psmouse->packet[6];
1532 psmouse->pktcnt = 1;
1533
1534 } else {
1535
1536 /*
1537 * High bit is 0 - that means that we indeed got a PS/2
1538 * packet in the middle of ALPS packet.
1539 *
1540 * There is also possibility that we got 6-byte ALPS
1541 * packet followed by 3-byte packet from trackpoint. We
1542 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001543 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001544 * normal operation (user would need to press all
1545 * buttons on the pad and start moving trackpoint
1546 * without touching the pad surface) we assume former.
1547 * Even if we are wrong the wost thing that would happen
1548 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001549 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001550 */
1551
Hans de Goede59c30af2015-04-03 17:14:40 -07001552 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1553 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001554
1555 /*
1556 * Continue with the standard ALPS protocol handling,
1557 * but make sure we won't process it as an interleaved
1558 * packet again, which may happen if all buttons are
1559 * pressed. To avoid this let's reset the 4th bit which
1560 * is normally 1.
1561 */
1562 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1563 psmouse->pktcnt = 4;
1564 }
1565
1566 return PSMOUSE_GOOD_DATA;
1567}
1568
1569static void alps_flush_packet(unsigned long data)
1570{
1571 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001572 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001573
1574 serio_pause_rx(psmouse->ps2dev.serio);
1575
Seth Forsheeb46615f2011-11-07 19:53:30 -08001576 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001577
1578 /*
1579 * We did not any more data in reasonable amount of time.
1580 * Validate the last 3 bytes and process as a standard
1581 * ALPS packet.
1582 */
1583 if ((psmouse->packet[3] |
1584 psmouse->packet[4] |
1585 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001586 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001587 "refusing packet %3ph (suspected interleaved ps/2)\n",
1588 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001589 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001590 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001591 }
1592 psmouse->pktcnt = 0;
1593 }
1594
1595 serio_continue_rx(psmouse->ps2dev.serio);
1596}
1597
1598static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1599{
1600 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001601
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001602 /*
1603 * Check if we are dealing with a bare PS/2 packet, presumably from
1604 * a device connected to the external PS/2 port. Because bare PS/2
1605 * protocol does not have enough constant bits to self-synchronize
1606 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001607 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001608 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001609 if (priv->proto_version != ALPS_PROTO_V8 &&
1610 !psmouse->out_of_sync_cnt &&
1611 (psmouse->packet[0] & 0xc8) == 0x08) {
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001614 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1615 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return PSMOUSE_FULL_PACKET;
1617 }
1618 return PSMOUSE_GOOD_DATA;
1619 }
1620
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001621 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1622
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001623 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001624 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1625 return alps_handle_interleaved_ps2(psmouse);
1626 }
1627
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001628 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001629 psmouse_dbg(psmouse,
1630 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001631 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Seth Forsheeb46615f2011-11-07 19:53:30 -08001635 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001636 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001637 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001638 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001639 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1640 psmouse->pktcnt - 1,
1641 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001642
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001643 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001644 psmouse->pktcnt == psmouse->pktsize) {
1645 /*
1646 * Some Dell boxes, such as Latitude E6440 or E7440
1647 * with closed lid, quite often smash last byte of
1648 * otherwise valid packet with 0xff. Given that the
1649 * next packet is very likely to be valid let's
1650 * report PSMOUSE_FULL_PACKET but not process data,
1651 * rather than reporting PSMOUSE_BAD_DATA and
1652 * filling the logs.
1653 */
1654 return PSMOUSE_FULL_PACKET;
1655 }
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001660 if ((priv->proto_version == ALPS_PROTO_V7 &&
1661 !alps_is_valid_package_v7(psmouse)) ||
1662 (priv->proto_version == ALPS_PROTO_V8 &&
1663 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001664 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1665 psmouse->pktcnt - 1,
1666 psmouse->packet[psmouse->pktcnt - 1]);
1667 return PSMOUSE_BAD_DATA;
1668 }
1669
Seth Forsheeb46615f2011-11-07 19:53:30 -08001670 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001671 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return PSMOUSE_FULL_PACKET;
1673 }
1674
1675 return PSMOUSE_GOOD_DATA;
1676}
1677
Seth Forshee25bded72011-11-07 19:53:36 -08001678static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1679{
1680 struct ps2dev *ps2dev = &psmouse->ps2dev;
1681 struct alps_data *priv = psmouse->private;
1682 int command;
1683 unsigned char *param;
1684 unsigned char dummy[4];
1685
1686 BUG_ON(nibble > 0xf);
1687
1688 command = priv->nibble_commands[nibble].command;
1689 param = (command & 0x0f00) ?
1690 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1691
1692 if (ps2_command(ps2dev, param, command))
1693 return -1;
1694
1695 return 0;
1696}
1697
1698static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1699{
1700 struct ps2dev *ps2dev = &psmouse->ps2dev;
1701 struct alps_data *priv = psmouse->private;
1702 int i, nibble;
1703
1704 if (ps2_command(ps2dev, NULL, priv->addr_command))
1705 return -1;
1706
1707 for (i = 12; i >= 0; i -= 4) {
1708 nibble = (addr >> i) & 0xf;
1709 if (alps_command_mode_send_nibble(psmouse, nibble))
1710 return -1;
1711 }
1712
1713 return 0;
1714}
1715
1716static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1717{
1718 struct ps2dev *ps2dev = &psmouse->ps2dev;
1719 unsigned char param[4];
1720
1721 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1722 return -1;
1723
1724 /*
1725 * The address being read is returned in the first two bytes
1726 * of the result. Check that this address matches the expected
1727 * address.
1728 */
1729 if (addr != ((param[0] << 8) | param[1]))
1730 return -1;
1731
1732 return param[2];
1733}
1734
1735static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1736{
1737 if (alps_command_mode_set_addr(psmouse, addr))
1738 return -1;
1739 return __alps_command_mode_read_reg(psmouse, addr);
1740}
1741
1742static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1743{
1744 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1745 return -1;
1746 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1747 return -1;
1748 return 0;
1749}
1750
1751static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1752 u8 value)
1753{
1754 if (alps_command_mode_set_addr(psmouse, addr))
1755 return -1;
1756 return __alps_command_mode_write_reg(psmouse, value);
1757}
1758
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001759static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1760 int repeated_command, unsigned char *param)
1761{
1762 struct ps2dev *ps2dev = &psmouse->ps2dev;
1763
1764 param[0] = 0;
1765 if (init_command && ps2_command(ps2dev, param, init_command))
1766 return -EIO;
1767
1768 if (ps2_command(ps2dev, NULL, repeated_command) ||
1769 ps2_command(ps2dev, NULL, repeated_command) ||
1770 ps2_command(ps2dev, NULL, repeated_command))
1771 return -EIO;
1772
1773 param[0] = param[1] = param[2] = 0xff;
1774 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1775 return -EIO;
1776
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001777 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1778 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001779 return 0;
1780}
1781
Yunkang Tang38088432014-07-26 13:51:41 -07001782static bool alps_check_valid_firmware_id(unsigned char id[])
1783{
1784 if (id[0] == 0x73)
1785 return true;
1786
1787 if (id[0] == 0x88 &&
1788 (id[1] == 0x07 ||
1789 id[1] == 0x08 ||
1790 (id[1] & 0xf0) == 0xb0 ||
1791 (id[1] & 0xf0) == 0xc0)) {
1792 return true;
1793 }
1794
1795 return false;
1796}
1797
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001798static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001799{
1800 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001801
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001802 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001803 psmouse_err(psmouse, "failed to enter command mode\n");
1804 return -1;
1805 }
1806
Yunkang Tang38088432014-07-26 13:51:41 -07001807 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001808 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001809 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001810 return -1;
1811 }
Seth Forshee25bded72011-11-07 19:53:36 -08001812 return 0;
1813}
1814
1815static inline int alps_exit_command_mode(struct psmouse *psmouse)
1816{
1817 struct ps2dev *ps2dev = &psmouse->ps2dev;
1818 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1819 return -1;
1820 return 0;
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/*
1824 * For DualPoint devices select the device that should respond to
1825 * subsequent commands. It looks like glidepad is behind stickpointer,
1826 * I'd thought it would be other way around...
1827 */
Seth Forshee25bded72011-11-07 19:53:36 -08001828static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1832
1833 if (ps2_command(ps2dev, NULL, cmd) ||
1834 ps2_command(ps2dev, NULL, cmd) ||
1835 ps2_command(ps2dev, NULL, cmd) ||
1836 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1837 return -1;
1838
1839 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001840 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 return 0;
1843}
1844
Seth Forshee25bded72011-11-07 19:53:36 -08001845static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct ps2dev *ps2dev = &psmouse->ps2dev;
1848
1849 /* Try ALPS magic knock - 4 disable before enable */
1850 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1851 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1852 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1853 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1854 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1855 return -1;
1856
1857 /*
1858 * Switch mouse to poll (remote) mode so motion data will not
1859 * get in our way
1860 */
Guenter Roeckad568142017-01-21 23:44:46 -08001861 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Yunkang Tang95f75e92013-12-01 22:33:52 -08001864static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1865{
1866 int i, nibble;
1867
1868 /*
1869 * b0-b11 are valid bits, send sequence is inverse.
1870 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1871 */
1872 for (i = 0; i <= 8; i += 4) {
1873 nibble = (word >> i) & 0xf;
1874 if (alps_command_mode_send_nibble(psmouse, nibble))
1875 return -1;
1876 }
1877
1878 return 0;
1879}
1880
1881static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1882 u16 addr, u16 value)
1883{
1884 struct ps2dev *ps2dev = &psmouse->ps2dev;
1885
1886 /* 0x0A0 is the command to write the word */
1887 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1888 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1889 alps_monitor_mode_send_word(psmouse, addr) ||
1890 alps_monitor_mode_send_word(psmouse, value) ||
1891 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1892 return -1;
1893
1894 return 0;
1895}
1896
1897static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1898{
1899 struct ps2dev *ps2dev = &psmouse->ps2dev;
1900
1901 if (enable) {
1902 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1903 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1904 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1905 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1906 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1907 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1908 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1909 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1910 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1911 return -1;
1912 } else {
1913 /* EC to exit monitor mode */
1914 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1915 return -1;
1916 }
1917
1918 return 0;
1919}
1920
1921static int alps_absolute_mode_v6(struct psmouse *psmouse)
1922{
1923 u16 reg_val = 0x181;
1924 int ret = -1;
1925
1926 /* enter monitor mode, to write the register */
1927 if (alps_monitor_mode(psmouse, true))
1928 return -1;
1929
1930 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1931
1932 if (alps_monitor_mode(psmouse, false))
1933 ret = -1;
1934
1935 return ret;
1936}
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938static int alps_get_status(struct psmouse *psmouse, char *param)
1939{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001941 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return -1;
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return 0;
1945}
1946
1947/*
1948 * Turn touchpad tapping on or off. The sequences are:
1949 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1950 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1951 * My guess that 0xE9 (GetInfo) is here as a sync point.
1952 * For models that also have stickpointer (DualPoints) its tapping
1953 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1954 * we don't fiddle with it.
1955 */
1956static int alps_tap_mode(struct psmouse *psmouse, int enable)
1957{
1958 struct ps2dev *ps2dev = &psmouse->ps2dev;
1959 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1960 unsigned char tap_arg = enable ? 0x0A : 0x00;
1961 unsigned char param[4];
1962
1963 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1964 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1965 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1966 ps2_command(ps2dev, &tap_arg, cmd))
1967 return -1;
1968
1969 if (alps_get_status(psmouse, param))
1970 return -1;
1971
1972 return 0;
1973}
1974
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001975/*
1976 * alps_poll() - poll the touchpad for current motion packet.
1977 * Used in resync.
1978 */
1979static int alps_poll(struct psmouse *psmouse)
1980{
1981 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001982 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001983 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001984
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001985 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001986 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001987
1988 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1989 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1990
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001991 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001992 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001993
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001994 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001995 return -1;
1996
1997 if ((psmouse->badbyte & 0xc8) == 0x08) {
1998/*
1999 * Poll the track stick ...
2000 */
2001 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2002 return -1;
2003 }
2004
2005 memcpy(psmouse->packet, buf, sizeof(buf));
2006 return 0;
2007}
2008
Seth Forshee25bded72011-11-07 19:53:36 -08002009static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002013 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002014 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002018 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002019 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05002021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Seth Forshee25bded72011-11-07 19:53:36 -08002023 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002024 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return -1;
2026 }
2027
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002028 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002029 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002033 /* ALPS needs stream mode, otherwise it won't report any data */
2034 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002035 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002036 return -1;
2037 }
2038
2039 return 0;
2040}
2041
Yunkang Tang95f75e92013-12-01 22:33:52 -08002042static int alps_hw_init_v6(struct psmouse *psmouse)
2043{
2044 unsigned char param[2] = {0xC8, 0x14};
2045
2046 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2047 if (alps_passthrough_mode_v2(psmouse, true))
2048 return -1;
2049
2050 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2051 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2052 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2053 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2054 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2055 return -1;
2056
2057 if (alps_passthrough_mode_v2(psmouse, false))
2058 return -1;
2059
2060 if (alps_absolute_mode_v6(psmouse)) {
2061 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2062 return -1;
2063 }
2064
2065 return 0;
2066}
2067
Seth Forshee25bded72011-11-07 19:53:36 -08002068/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08002069 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08002070 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08002071static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2072 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08002073{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002074 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08002075
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002076 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002077 return -1;
2078
Kevin Cernekeecd401202013-02-13 22:28:07 -08002079 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2080 if (reg_val == -1)
2081 goto error;
2082
Seth Forshee25bded72011-11-07 19:53:36 -08002083 if (enable)
2084 reg_val |= 0x01;
2085 else
2086 reg_val &= ~0x01;
2087
Kevin Cernekeecd401202013-02-13 22:28:07 -08002088 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08002089
Kevin Cernekeecd401202013-02-13 22:28:07 -08002090error:
2091 if (alps_exit_command_mode(psmouse))
2092 ret = -1;
2093 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08002094}
2095
2096/* Must be in command mode when calling this function */
2097static int alps_absolute_mode_v3(struct psmouse *psmouse)
2098{
2099 int reg_val;
2100
2101 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2102 if (reg_val == -1)
2103 return -1;
2104
2105 reg_val |= 0x06;
2106 if (__alps_command_mode_write_reg(psmouse, reg_val))
2107 return -1;
2108
2109 return 0;
2110}
2111
Pali Rohárdae928e2016-01-06 15:50:08 -08002112static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
Kevin Cernekeecd401202013-02-13 22:28:07 -08002113{
2114 int ret = -EIO, reg_val;
2115
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002116 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002117 goto error;
2118
2119 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2120 if (reg_val == -1)
2121 goto error;
2122
2123 /* bit 7: trackstick is present */
2124 ret = reg_val & 0x80 ? 0 : -ENODEV;
2125
2126error:
2127 alps_exit_command_mode(psmouse);
2128 return ret;
2129}
2130
2131static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2132{
2133 struct ps2dev *ps2dev = &psmouse->ps2dev;
2134 int ret = 0;
2135 unsigned char param[4];
2136
2137 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2138 return -EIO;
2139
2140 /*
2141 * E7 report for the trackstick
2142 *
2143 * There have been reports of failures to seem to trace back
2144 * to the above trackstick check failing. When these occur
2145 * this E7 report fails, so when that happens we continue
2146 * with the assumption that there isn't a trackstick after
2147 * all.
2148 */
2149 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002150 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002151 ret = -ENODEV;
2152 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002153 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002154
2155 /*
2156 * Not sure what this does, but it is absolutely
2157 * essential. Without it, the touchpad does not
2158 * work at all and the trackstick just emits normal
2159 * PS/2 packets.
2160 */
2161 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2162 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2163 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2164 alps_command_mode_send_nibble(psmouse, 0x9) ||
2165 alps_command_mode_send_nibble(psmouse, 0x4)) {
2166 psmouse_err(psmouse,
2167 "Error sending magic E6 sequence\n");
2168 ret = -EIO;
2169 goto error;
2170 }
2171
2172 /*
2173 * This ensures the trackstick packets are in the format
2174 * supported by this driver. If bit 1 isn't set the packet
2175 * format is different.
2176 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002177 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002178 alps_command_mode_write_reg(psmouse,
2179 reg_base + 0x08, 0x82) ||
2180 alps_exit_command_mode(psmouse))
2181 ret = -EIO;
2182 }
2183
2184error:
2185 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2186 ret = -EIO;
2187
2188 return ret;
2189}
2190
Seth Forshee25bded72011-11-07 19:53:36 -08002191static int alps_hw_init_v3(struct psmouse *psmouse)
2192{
Pali Rohár4b1af852016-01-06 15:58:56 -08002193 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -08002194 struct ps2dev *ps2dev = &psmouse->ps2dev;
2195 int reg_val;
2196 unsigned char param[4];
2197
Pali Rohár4b1af852016-01-06 15:58:56 -08002198 if ((priv->flags & ALPS_DUALPOINT) &&
Kevin Cernekeecd401202013-02-13 22:28:07 -08002199 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002200 goto error;
2201
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002202 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002203 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002204 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2205 goto error;
2206 }
2207
2208 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2209 if (reg_val == -1)
2210 goto error;
2211 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2212 goto error;
2213
2214 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2215 if (reg_val == -1)
2216 goto error;
2217 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2218 goto error;
2219
2220 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2221 goto error;
2222 if (__alps_command_mode_write_reg(psmouse, 0x04))
2223 goto error;
2224
2225 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2226 goto error;
2227 if (__alps_command_mode_write_reg(psmouse, 0x03))
2228 goto error;
2229
2230 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2231 goto error;
2232 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2233 goto error;
2234
2235 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2236 goto error;
2237 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2238 goto error;
2239
Seth Forshee25bded72011-11-07 19:53:36 -08002240 alps_exit_command_mode(psmouse);
2241
2242 /* Set rate and enable data reporting */
2243 param[0] = 0x64;
2244 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2245 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2246 psmouse_err(psmouse, "Failed to enable data reporting\n");
2247 return -1;
2248 }
2249
2250 return 0;
2251
Seth Forshee25bded72011-11-07 19:53:36 -08002252error:
2253 /*
2254 * Leaving the touchpad in command mode will essentially render
2255 * it unusable until the machine reboots, so exit it here just
2256 * to be safe
2257 */
2258 alps_exit_command_mode(psmouse);
2259 return -1;
2260}
2261
Hans de Goedef3f33c62014-07-29 11:22:07 -07002262static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2263{
2264 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2265 struct alps_data *priv = psmouse->private;
2266
2267 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2268 if (reg < 0)
2269 return reg;
2270
2271 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2272 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2273
2274 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2275 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2276
2277 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2278 if (reg < 0)
2279 return reg;
2280
2281 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2282 x_electrode = 17 + x_electrode;
2283
2284 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2285 y_electrode = 13 + y_electrode;
2286
2287 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2288 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2289
2290 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2291 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2292
2293 psmouse_dbg(psmouse,
2294 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2295 x_pitch, y_pitch, x_electrode, y_electrode,
2296 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2297
2298 return 0;
2299}
2300
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002301static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2302{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002303 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002304 struct ps2dev *ps2dev = &psmouse->ps2dev;
2305 int reg_val, ret = -1;
2306
Kevin Cernekeecd401202013-02-13 22:28:07 -08002307 if (priv->flags & ALPS_DUALPOINT) {
2308 reg_val = alps_setup_trackstick_v3(psmouse,
2309 ALPS_REG_BASE_RUSHMORE);
2310 if (reg_val == -EIO)
2311 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002312 }
2313
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002314 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002315 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2316 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2317 goto error;
2318
Hans de Goedef3f33c62014-07-29 11:22:07 -07002319 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2320 goto error;
2321
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002322 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2323 if (reg_val == -1)
2324 goto error;
2325 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2326 goto error;
2327
2328 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2329 goto error;
2330
2331 /* enter absolute mode */
2332 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2333 if (reg_val == -1)
2334 goto error;
2335 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2336 goto error;
2337
2338 alps_exit_command_mode(psmouse);
2339 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2340
2341error:
2342 alps_exit_command_mode(psmouse);
2343 return ret;
2344}
2345
Seth Forshee25bded72011-11-07 19:53:36 -08002346/* Must be in command mode when calling this function */
2347static int alps_absolute_mode_v4(struct psmouse *psmouse)
2348{
2349 int reg_val;
2350
2351 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2352 if (reg_val == -1)
2353 return -1;
2354
2355 reg_val |= 0x02;
2356 if (__alps_command_mode_write_reg(psmouse, reg_val))
2357 return -1;
2358
2359 return 0;
2360}
2361
2362static int alps_hw_init_v4(struct psmouse *psmouse)
2363{
Seth Forshee25bded72011-11-07 19:53:36 -08002364 struct ps2dev *ps2dev = &psmouse->ps2dev;
2365 unsigned char param[4];
2366
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002367 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002368 goto error;
2369
2370 if (alps_absolute_mode_v4(psmouse)) {
2371 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2372 goto error;
2373 }
2374
2375 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2376 goto error;
2377
2378 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2379 goto error;
2380
2381 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2382 goto error;
2383
2384 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2385 goto error;
2386
2387 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2388 goto error;
2389
2390 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2391 goto error;
2392
2393 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2394 goto error;
2395
2396 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2397 goto error;
2398
2399 alps_exit_command_mode(psmouse);
2400
2401 /*
2402 * This sequence changes the output from a 9-byte to an
2403 * 8-byte format. All the same data seems to be present,
2404 * just in a more compact format.
2405 */
2406 param[0] = 0xc8;
2407 param[1] = 0x64;
2408 param[2] = 0x50;
2409 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2410 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2411 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2412 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2413 return -1;
2414
2415 /* Set rate and enable data reporting */
2416 param[0] = 0x64;
2417 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2418 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2419 psmouse_err(psmouse, "Failed to enable data reporting\n");
2420 return -1;
2421 }
2422
2423 return 0;
2424
2425error:
2426 /*
2427 * Leaving the touchpad in command mode will essentially render
2428 * it unusable until the machine reboots, so exit it here just
2429 * to be safe
2430 */
2431 alps_exit_command_mode(psmouse);
2432 return -1;
2433}
2434
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002435static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2436 unsigned char index, unsigned char otp[])
2437{
2438 struct ps2dev *ps2dev = &psmouse->ps2dev;
2439
2440 switch (index) {
2441 case 0:
2442 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2443 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2444 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2445 return -1;
2446
2447 break;
2448
2449 case 1:
2450 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2451 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2452 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2453 return -1;
2454
2455 break;
2456 }
2457
2458 return 0;
2459}
2460
Fengguang Wu07f19e32015-04-10 23:54:31 -07002461static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2462 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002463{
2464 int num_x_electrode;
2465 int num_y_electrode;
2466 int x_pitch, y_pitch, x_phys, y_phys;
2467
2468 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2469 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2470
2471 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2472 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2473
2474 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2475 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2476
2477 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2478 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2479
2480 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2481 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2482
2483 return 0;
2484}
2485
Fengguang Wu07f19e32015-04-10 23:54:31 -07002486static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2487 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002488{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002489 unsigned char is_btnless;
2490
2491 is_btnless = (otp[1][1] >> 3) & 0x01;
2492
2493 if (is_btnless)
2494 priv->flags |= ALPS_BUTTONPAD;
2495
2496 return 0;
2497}
2498
2499static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2500 struct alps_data *priv)
2501{
2502 unsigned char otp[2][4];
2503
2504 memset(otp, 0, sizeof(otp));
2505
2506 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2507 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2508 return -1;
2509
2510 alps_update_device_area_ss4_v2(otp, priv);
2511
2512 alps_update_btn_info_ss4_v2(otp, priv);
2513
2514 return 0;
2515}
2516
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002517static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2518 struct alps_data *priv)
2519{
2520 struct ps2dev *ps2dev = &psmouse->ps2dev;
2521 unsigned char param[4] = {0};
2522 int num_x_electrode, num_y_electrode;
2523
2524 if (alps_enter_command_mode(psmouse))
2525 return -1;
2526
2527 param[0] = 0x0a;
2528 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2529 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2530 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2531 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2532 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2533 return -1;
2534
2535 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2536 return -1;
2537
2538 /*
2539 * Dolphin's sensor line number is not fixed. It can be calculated
2540 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2541 * Further more, we can get device's x_max and y_max by multiplying
2542 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2543 *
2544 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2545 * real sensor line number X = 11 + 8 = 19, and
2546 * real sensor line number Y = 8 + 1 = 9.
2547 * So, x_max = (19 - 1) * 64 = 1152, and
2548 * y_max = (9 - 1) * 64 = 512.
2549 */
2550 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2551 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2552 priv->x_bits = num_x_electrode;
2553 priv->y_bits = num_y_electrode;
2554 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2555 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2556
2557 if (alps_exit_command_mode(psmouse))
2558 return -1;
2559
2560 return 0;
2561}
2562
Dave Turvene75af9e52013-02-21 22:58:28 -08002563static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2564{
2565 struct ps2dev *ps2dev = &psmouse->ps2dev;
2566 unsigned char param[2];
2567
2568 /* This is dolphin "v1" as empirically defined by florin9doi */
2569 param[0] = 0x64;
2570 param[1] = 0x28;
2571
2572 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2573 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2574 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2575 return -1;
2576
2577 return 0;
2578}
2579
Yunkang Tang38088432014-07-26 13:51:41 -07002580static int alps_hw_init_v7(struct psmouse *psmouse)
2581{
2582 struct ps2dev *ps2dev = &psmouse->ps2dev;
2583 int reg_val, ret = -1;
2584
2585 if (alps_enter_command_mode(psmouse) ||
2586 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2587 goto error;
2588
Hans de Goedef3f33c62014-07-29 11:22:07 -07002589 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2590 goto error;
2591
Yunkang Tang38088432014-07-26 13:51:41 -07002592 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2593 goto error;
2594
2595 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2596 if (reg_val == -1)
2597 goto error;
2598 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2599 goto error;
2600
2601 alps_exit_command_mode(psmouse);
2602 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2603
2604error:
2605 alps_exit_command_mode(psmouse);
2606 return ret;
2607}
2608
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002609static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2610{
2611 struct ps2dev *ps2dev = &psmouse->ps2dev;
2612 char param[2] = {0x64, 0x28};
2613 int ret = -1;
2614
2615 /* enter absolute mode */
2616 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2617 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2618 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2619 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2620 goto error;
2621 }
2622
2623 /* T.B.D. Decread noise packet number, delete in the future */
2624 if (alps_exit_command_mode(psmouse) ||
2625 alps_enter_command_mode(psmouse) ||
2626 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2627 goto error;
2628 }
2629 alps_exit_command_mode(psmouse);
2630
2631 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2632
2633error:
2634 alps_exit_command_mode(psmouse);
2635 return ret;
2636}
2637
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002638static int alps_set_protocol(struct psmouse *psmouse,
2639 struct alps_data *priv,
2640 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002641{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002642 psmouse->private = priv;
2643
2644 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2645
2646 priv->proto_version = protocol->version;
2647 priv->byte0 = protocol->byte0;
2648 priv->mask0 = protocol->mask0;
2649 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002650
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002651 priv->x_max = 2000;
2652 priv->y_max = 1400;
2653 priv->x_bits = 15;
2654 priv->y_bits = 11;
2655
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002656 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002657 case ALPS_PROTO_V1:
2658 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002659 priv->hw_init = alps_hw_init_v1_v2;
2660 priv->process_packet = alps_process_packet_v1_v2;
2661 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002662 priv->x_max = 1023;
2663 priv->y_max = 767;
Hans de Goede19556212015-10-26 01:50:28 -07002664 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2665 priv->flags |= ALPS_STICK_BITS;
Seth Forshee25bded72011-11-07 19:53:36 -08002666 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002667
Seth Forshee25bded72011-11-07 19:53:36 -08002668 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002669 priv->hw_init = alps_hw_init_v3;
2670 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002671 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002672 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002673 priv->nibble_commands = alps_v3_nibble_commands;
2674 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Pali Rohár4b1af852016-01-06 15:58:56 -08002675
2676 if (alps_probe_trackstick_v3_v7(psmouse,
2677 ALPS_REG_BASE_PINNACLE) < 0)
2678 priv->flags &= ~ALPS_DUALPOINT;
2679
Seth Forshee25bded72011-11-07 19:53:36 -08002680 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002681
2682 case ALPS_PROTO_V3_RUSHMORE:
2683 priv->hw_init = alps_hw_init_rushmore_v3;
2684 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002685 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002686 priv->decode_fields = alps_decode_rushmore;
2687 priv->nibble_commands = alps_v3_nibble_commands;
2688 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2689 priv->x_bits = 16;
2690 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002691
Pali Rohárdae928e2016-01-06 15:50:08 -08002692 if (alps_probe_trackstick_v3_v7(psmouse,
2693 ALPS_REG_BASE_RUSHMORE) < 0)
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002694 priv->flags &= ~ALPS_DUALPOINT;
2695
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002696 break;
2697
Seth Forshee25bded72011-11-07 19:53:36 -08002698 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002699 priv->hw_init = alps_hw_init_v4;
2700 priv->process_packet = alps_process_packet_v4;
Hans de Goede688ea362015-05-20 14:42:50 -07002701 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002702 priv->nibble_commands = alps_v4_nibble_commands;
2703 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002704 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002705
Dave Turvene75af9e52013-02-21 22:58:28 -08002706 case ALPS_PROTO_V5:
2707 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002708 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002709 priv->decode_fields = alps_decode_dolphin;
Hans de Goede688ea362015-05-20 14:42:50 -07002710 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dave Turvene75af9e52013-02-21 22:58:28 -08002711 priv->nibble_commands = alps_v3_nibble_commands;
2712 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002713 priv->x_bits = 23;
2714 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002715
2716 if (alps_dolphin_get_device_area(psmouse, priv))
2717 return -EIO;
2718
Dave Turvene75af9e52013-02-21 22:58:28 -08002719 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002720
Yunkang Tang95f75e92013-12-01 22:33:52 -08002721 case ALPS_PROTO_V6:
2722 priv->hw_init = alps_hw_init_v6;
2723 priv->process_packet = alps_process_packet_v6;
2724 priv->set_abs_params = alps_set_abs_params_st;
2725 priv->nibble_commands = alps_v6_nibble_commands;
2726 priv->x_max = 2047;
2727 priv->y_max = 1535;
2728 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002729
Yunkang Tang38088432014-07-26 13:51:41 -07002730 case ALPS_PROTO_V7:
2731 priv->hw_init = alps_hw_init_v7;
2732 priv->process_packet = alps_process_packet_v7;
2733 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002734 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002735 priv->nibble_commands = alps_v3_nibble_commands;
2736 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002737 priv->x_max = 0xfff;
2738 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002739
2740 if (priv->fw_ver[1] != 0xba)
2741 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002742
Pali Rohárdae928e2016-01-06 15:50:08 -08002743 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2744 priv->flags &= ~ALPS_DUALPOINT;
2745
Yunkang Tang38088432014-07-26 13:51:41 -07002746 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002747
2748 case ALPS_PROTO_V8:
2749 priv->hw_init = alps_hw_init_ss4_v2;
2750 priv->process_packet = alps_process_packet_ss4_v2;
2751 priv->decode_fields = alps_decode_ss4_v2;
2752 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2753 priv->nibble_commands = alps_v3_nibble_commands;
2754 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2755
2756 if (alps_set_defaults_ss4_v2(psmouse, priv))
2757 return -EIO;
2758
Ben Gamariaeaa8812016-10-04 11:43:30 -07002759 if (priv->fw_ver[1] == 0x1)
2760 priv->flags |= ALPS_DUALPOINT |
2761 ALPS_DUALPOINT_WITH_PRESSURE;
2762
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002763 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002764 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002765
2766 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002767}
2768
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002769static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2770 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002771{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002772 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002773 int i;
2774
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002775 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2776 model = &alps_model_data[i];
2777
2778 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2779 (!model->command_mode_resp ||
2780 model->command_mode_resp == ec[2])) {
2781
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002782 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002783 }
2784 }
2785
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002786 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002787}
2788
2789static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2790{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002791 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002792 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002793 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002794
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002795 /*
2796 * First try "E6 report".
2797 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2798 * The bits 0-2 of the first byte will be 1s if some buttons are
2799 * pressed.
2800 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002801 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2802 PSMOUSE_CMD_SETSCALE11, e6))
2803 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002804
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002805 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2806 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002807
2808 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002809 * Now get the "E7" and "EC" reports. These will uniquely identify
2810 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002811 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002812 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2813 PSMOUSE_CMD_SETSCALE21, e7) ||
2814 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2815 PSMOUSE_CMD_RESET_WRAP, ec) ||
2816 alps_exit_command_mode(psmouse))
2817 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002818
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002819 protocol = alps_match_table(e7, ec);
2820 if (!protocol) {
Pali Rohár09c398b2017-03-07 09:41:34 -08002821 if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
2822 ec[2] == 0x8a) {
2823 protocol = &alps_v4_protocol_data;
2824 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002825 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2826 protocol = &alps_v5_protocol_data;
2827 } else if (ec[0] == 0x88 &&
2828 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2829 protocol = &alps_v7_protocol_data;
2830 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2831 protocol = &alps_v3_rushmore_data;
2832 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2833 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2834 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002835 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2836 e7[2] == 0x14 && ec[1] == 0x02) {
2837 protocol = &alps_v8_protocol_data;
Ben Gamariaeaa8812016-10-04 11:43:30 -07002838 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2839 e7[2] == 0x28 && ec[1] == 0x01) {
2840 protocol = &alps_v8_protocol_data;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002841 } else {
2842 psmouse_dbg(psmouse,
2843 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2844 return -EINVAL;
2845 }
2846 }
2847
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002848 if (priv) {
2849 /* Save the Firmware version */
2850 memcpy(priv->fw_ver, ec, 3);
2851 error = alps_set_protocol(psmouse, priv, protocol);
2852 if (error)
2853 return error;
2854 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002855
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002856 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002857}
2858
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002859static int alps_reconnect(struct psmouse *psmouse)
2860{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002861 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002862
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002863 psmouse_reset(psmouse);
2864
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002865 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002866 return -1;
2867
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002868 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
2871static void alps_disconnect(struct psmouse *psmouse)
2872{
2873 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002876 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002877 if (priv->dev2)
2878 input_unregister_device(priv->dev2);
2879 if (!IS_ERR_OR_NULL(priv->dev3))
2880 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 kfree(priv);
2882}
2883
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002884static void alps_set_abs_params_st(struct alps_data *priv,
2885 struct input_dev *dev1)
2886{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002887 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2888 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002889 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002890}
2891
Masaki Ota8eccd392015-03-27 20:44:21 -07002892static void alps_set_abs_params_mt_common(struct alps_data *priv,
2893 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002894{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002895 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2896 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002897
Hans de Goedef3f33c62014-07-29 11:22:07 -07002898 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2899 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2900
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002901 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2902 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002903}
Yunkang Tang38088432014-07-26 13:51:41 -07002904
Hans de Goede688ea362015-05-20 14:42:50 -07002905static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2906 struct input_dev *dev1)
Masaki Ota8eccd392015-03-27 20:44:21 -07002907{
2908 alps_set_abs_params_mt_common(priv, dev1);
2909 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2910
2911 input_mt_init_slots(dev1, MAX_TOUCHES,
2912 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
Hans de Goede1662c0332015-05-20 14:42:15 -07002913 INPUT_MT_SEMI_MT);
Masaki Ota8eccd392015-03-27 20:44:21 -07002914}
2915
2916static void alps_set_abs_params_v7(struct alps_data *priv,
2917 struct input_dev *dev1)
2918{
2919 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002920 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002921
2922 input_mt_init_slots(dev1, MAX_TOUCHES,
2923 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2924 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002925
2926 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2927}
2928
2929static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2930 struct input_dev *dev1)
2931{
2932 alps_set_abs_params_mt_common(priv, dev1);
2933 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2934 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2935
2936 input_mt_init_slots(dev1, MAX_TOUCHES,
2937 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2938 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002939}
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941int alps_init(struct psmouse *psmouse)
2942{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002943 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002944 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002945 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002947 error = priv->hw_init(psmouse);
2948 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 goto init_fail;
2950
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002951 /*
2952 * Undo part of setup done for us by psmouse core since touchpad
2953 * is not a relative device.
2954 */
2955 __clear_bit(EV_REL, dev1->evbit);
2956 __clear_bit(REL_X, dev1->relbit);
2957 __clear_bit(REL_Y, dev1->relbit);
2958
2959 /*
2960 * Now set up our capabilities.
2961 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002962 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2963 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2964 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002965 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2966 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002968 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002969
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002970 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002972 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002973 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2974 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002977 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002978 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2979 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002982 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002983 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2984 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2985 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2986 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002987 } else if (priv->flags & ALPS_BUTTONPAD) {
2988 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2989 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002990 } else {
2991 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2992 }
2993
Pali Rohárdfba8602015-01-09 12:48:44 -08002994 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002995 struct input_dev *dev2;
2996
2997 dev2 = input_allocate_device();
2998 if (!dev2) {
2999 psmouse_err(psmouse,
3000 "failed to allocate trackstick device\n");
3001 error = -ENOMEM;
3002 goto init_fail;
3003 }
3004
3005 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3006 psmouse->ps2dev.serio->phys);
3007 dev2->phys = priv->phys2;
3008
Pali Rohárdfba8602015-01-09 12:48:44 -08003009 /*
3010 * format of input device name is: "protocol vendor name"
3011 * see function psmouse_switch_protocol() in psmouse-base.c
3012 */
3013 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08003014
3015 dev2->id.bustype = BUS_I8042;
3016 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08003017 dev2->id.product = PSMOUSE_ALPS;
3018 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08003019 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08003020
Pali Rohár04aae282015-01-14 13:18:30 -08003021 input_set_capability(dev2, EV_REL, REL_X);
3022 input_set_capability(dev2, EV_REL, REL_Y);
Ben Gamari7ad8a102016-10-04 11:41:58 -07003023 if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3024 input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3025 input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3026 }
Pali Rohár04aae282015-01-14 13:18:30 -08003027 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3028 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3029 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003030
Pali Rohár04aae282015-01-14 13:18:30 -08003031 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07003032 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3033
Pali Rohár04aae282015-01-14 13:18:30 -08003034 error = input_register_device(dev2);
3035 if (error) {
3036 psmouse_err(psmouse,
3037 "failed to register trackstick device: %d\n",
3038 error);
3039 input_free_device(dev2);
3040 goto init_fail;
3041 }
3042
3043 priv->dev2 = dev2;
3044 }
3045
3046 priv->psmouse = psmouse;
3047
3048 INIT_DELAYED_WORK(&priv->dev3_register_work,
3049 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003052 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 psmouse->disconnect = alps_disconnect;
3054 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003055 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003057 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3058 psmouse->resync_time = 0;
3059
Pali Rohár9d720b32014-11-08 12:58:57 -08003060 /* Allow 2 invalid packets without resetting device */
3061 psmouse->resetafter = psmouse->pktsize * 2;
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return 0;
3064
3065init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04003066 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003067 /*
3068 * Even though we did not allocate psmouse->private we do free
3069 * it here.
3070 */
3071 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04003072 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003073 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074}
3075
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07003076int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003078 struct alps_data *priv;
3079 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003081 error = alps_identify(psmouse, NULL);
3082 if (error)
3083 return error;
3084
3085 /*
3086 * Reset the device to make sure it is fully operational:
3087 * on some laptops, like certain Dell Latitudes, we may
3088 * fail to properly detect presence of trackstick if device
3089 * has not been reset.
3090 */
3091 psmouse_reset(psmouse);
3092
3093 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3094 if (!priv)
3095 return -ENOMEM;
3096
3097 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003098 if (error) {
3099 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003100 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103 if (set_properties) {
3104 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003105 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003106 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003107 psmouse->model = priv->proto_version;
3108 } else {
3109 /*
3110 * Destroy alps_data structure we allocated earlier since
3111 * this was just a "trial run". Otherwise we'll keep it
3112 * to be used by alps_init() which has to be called if
3113 * we succeed and set_properties is true.
3114 */
3115 kfree(priv);
3116 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return 0;
3120}
3121