blob: a76e5c6fce54fbb0bdf30e7f9a69dc261da12e37 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Helge Dellere38de672006-09-10 21:54:39 -0400107static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800108 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
109 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
110 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
113 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
114 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
115 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
116 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
117 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
118 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
120 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
121 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
122 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800123
124 /*
125 * XXX This entry is suspicious. First byte has zero lower nibble,
126 * which is what a normal mouse would report. Also, the value 0x0e
127 * isn't valid per PS/2 spec.
128 */
129 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
130
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800131 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
132 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800133 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800134 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
135 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
136 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
137 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
138 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
139 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
140 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
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
151static const struct alps_protocol_info alps_v5_protocol_data = {
152 ALPS_PROTO_V5, 0xc8, 0xd8, 0
153};
154
155static const struct alps_protocol_info alps_v7_protocol_data = {
156 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
157};
158
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700159static const struct alps_protocol_info alps_v8_protocol_data = {
160 ALPS_PROTO_V8, 0x18, 0x18, 0
161};
162
Hans de Goede19556212015-10-26 01:50:28 -0700163/*
164 * Some v2 models report the stick buttons in separate bits
165 */
166static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
167#if defined(CONFIG_DMI) && defined(CONFIG_X86)
168 {
169 /* Extrapolated from other entries */
170 .matches = {
171 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
172 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
173 },
174 },
175 {
176 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
177 .matches = {
178 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
179 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
180 },
181 },
182 {
183 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
184 .matches = {
185 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
186 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
187 },
188 },
189 {
190 /* Extrapolated from other entries */
191 .matches = {
192 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
193 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
194 },
195 },
196#endif
197 { }
198};
199
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800200static void alps_set_abs_params_st(struct alps_data *priv,
201 struct input_dev *dev1);
Hans de Goede688ea362015-05-20 14:42:50 -0700202static void alps_set_abs_params_semi_mt(struct alps_data *priv,
203 struct input_dev *dev1);
Masaki Ota8eccd392015-03-27 20:44:21 -0700204static void alps_set_abs_params_v7(struct alps_data *priv,
205 struct input_dev *dev1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700206static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
207 struct input_dev *dev1);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800208
Seth Forsheed4b347b2011-11-07 19:53:15 -0800209/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800211static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800212 unsigned char data)
213{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800214 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800215}
216
Pali Rohár04aae282015-01-14 13:18:30 -0800217static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800218 int left, int right, int middle)
219{
Martin Buckc91ed052010-03-13 22:23:58 -0800220 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800221
Martin Buckc91ed052010-03-13 22:23:58 -0800222 /*
223 * If shared button has already been reported on the
224 * other device (dev2) then this event should be also
225 * sent through that device.
226 */
Pali Rohár04aae282015-01-14 13:18:30 -0800227 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800228 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800229
Pali Rohár04aae282015-01-14 13:18:30 -0800230 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800231 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800232
Pali Rohár04aae282015-01-14 13:18:30 -0800233 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800234 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800235
Martin Buckc91ed052010-03-13 22:23:58 -0800236 /*
237 * Sync the _other_ device now, we'll do the first
238 * device later once we report the rest of the events.
239 */
Pali Rohár04aae282015-01-14 13:18:30 -0800240 if (dev2)
241 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800242}
243
Seth Forshee25bded72011-11-07 19:53:36 -0800244static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct alps_data *priv = psmouse->private;
247 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500248 struct input_dev *dev = psmouse->dev;
249 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500251 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800253 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400254 left = packet[2] & 0x10;
255 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 middle = 0;
257 x = packet[1] | ((packet[0] & 0x07) << 7);
258 y = packet[4] | ((packet[3] & 0x07) << 7);
259 z = packet[5];
260 } else {
261 left = packet[3] & 1;
262 right = packet[3] & 2;
263 middle = packet[3] & 4;
264 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
265 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
266 z = packet[5];
267 }
268
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800269 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400270 back = packet[0] & 0x10;
271 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500272 }
273
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800274 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500275 back = packet[3] & 4;
276 forward = packet[2] & 4;
277 if ((middle = forward && back))
278 forward = back = 0;
279 }
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 ges = packet[2] & 1;
282 fin = packet[2] & 2;
283
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800284 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
286 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700287
Pali Rohár04aae282015-01-14 13:18:30 -0800288 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 input_sync(dev2);
291 return;
292 }
293
Hans de Goede19556212015-10-26 01:50:28 -0700294 /* Some models have separate stick button bits */
295 if (priv->flags & ALPS_STICK_BITS) {
Hans de Goede92bac832015-04-12 15:42:35 -0700296 left |= packet[0] & 1;
297 right |= packet[0] & 2;
298 middle |= packet[0] & 4;
299 }
300
Pali Rohár04aae282015-01-14 13:18:30 -0800301 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800304 if (ges && !fin)
305 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 /*
308 * A "tap and drag" operation is reported by the hardware as a transition
309 * from (!fin && ges) to (fin && ges). This should be translated to the
310 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
311 */
312 if (ges && fin && !priv->prev_fin) {
313 input_report_abs(dev, ABS_X, x);
314 input_report_abs(dev, ABS_Y, y);
315 input_report_abs(dev, ABS_PRESSURE, 0);
316 input_report_key(dev, BTN_TOOL_FINGER, 0);
317 input_sync(dev);
318 }
319 priv->prev_fin = fin;
320
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800321 if (z > 30)
322 input_report_key(dev, BTN_TOUCH, 1);
323 if (z < 25)
324 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (z > 0) {
327 input_report_abs(dev, ABS_X, x);
328 input_report_abs(dev, ABS_Y, y);
329 }
330
331 input_report_abs(dev, ABS_PRESSURE, z);
332 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
333
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800334 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500335 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800337 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500338 input_report_key(dev, BTN_FORWARD, forward);
339 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800342 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800343 input_report_key(dev, BTN_0, packet[2] & 4);
344 input_report_key(dev, BTN_1, packet[0] & 0x10);
345 input_report_key(dev, BTN_2, packet[3] & 4);
346 input_report_key(dev, BTN_3, packet[0] & 0x20);
347 }
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 input_sync(dev);
350}
351
Hans de Goede036e6c72014-07-25 22:38:51 -0700352static void alps_get_bitmap_points(unsigned int map,
353 struct alps_bitmap_point *low,
354 struct alps_bitmap_point *high,
355 int *fingers)
356{
357 struct alps_bitmap_point *point;
358 int i, bit, prev_bit = 0;
359
360 point = low;
361 for (i = 0; map != 0; i++, map >>= 1) {
362 bit = map & 1;
363 if (bit) {
364 if (!prev_bit) {
365 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700366 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700367 (*fingers)++;
368 }
369 point->num_bits++;
370 } else {
371 if (prev_bit)
372 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700373 }
374 prev_bit = bit;
375 }
376}
377
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800378/*
Hans de Goededccf1dd2015-05-20 14:43:09 -0700379 * Process bitmap data from semi-mt protocols. Returns the number of
Seth Forshee01ce6612011-11-07 19:54:13 -0800380 * fingers detected. A return value of 0 means at least one of the
381 * bitmaps was empty.
382 *
383 * The bitmaps don't have enough data to track fingers, so this function
384 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700385 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800386 * is greater than 0.
387 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800388static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700389 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800390{
Hans de Goede4dd26572015-05-20 14:41:10 -0700391 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
Seth Forshee01ce6612011-11-07 19:54:13 -0800392 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
393 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Hans de Goede4dd26572015-05-20 14:41:10 -0700394 struct input_mt_pos corner[4];
Seth Forshee01ce6612011-11-07 19:54:13 -0800395
Hans de Goede02d04252014-07-25 22:43:35 -0700396 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800397 return 0;
398
Hans de Goede02d04252014-07-25 22:43:35 -0700399 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
400 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800401
402 /*
403 * Fingers can overlap, so we use the maximum count of fingers
404 * on either axis as the finger count.
405 */
406 fingers = max(fingers_x, fingers_y);
407
408 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700409 * If an axis reports only a single contact, we have overlapping or
410 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800411 */
Hans de Goede20bea682014-07-25 22:33:33 -0700412 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700413 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700414 x_low.num_bits = x_low.num_bits - i;
415 x_high.start_bit = x_low.start_bit + i;
416 x_high.num_bits = max(i, 1);
417 }
418 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700419 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700420 y_low.num_bits = y_low.num_bits - i;
421 y_high.start_bit = y_low.start_bit + i;
422 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800423 }
424
Hans de Goede4dd26572015-05-20 14:41:10 -0700425 /* top-left corner */
426 corner[0].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700427 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
428 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700429 corner[0].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700430 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
431 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800432
Hans de Goede4dd26572015-05-20 14:41:10 -0700433 /* top-right corner */
434 corner[1].x =
Hans de Goede02d04252014-07-25 22:43:35 -0700435 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
436 (2 * (priv->x_bits - 1));
Hans de Goede4dd26572015-05-20 14:41:10 -0700437 corner[1].y =
438 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
439 (2 * (priv->y_bits - 1));
440
441 /* bottom-right corner */
442 corner[2].x =
443 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
444 (2 * (priv->x_bits - 1));
445 corner[2].y =
Hans de Goede02d04252014-07-25 22:43:35 -0700446 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
447 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800448
Hans de Goede4dd26572015-05-20 14:41:10 -0700449 /* bottom-left corner */
450 corner[3].x =
451 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
452 (2 * (priv->x_bits - 1));
453 corner[3].y =
Seth Forshee01ce6612011-11-07 19:54:13 -0800454 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
455 (2 * (priv->y_bits - 1));
456
Hans de Goededccf1dd2015-05-20 14:43:09 -0700457 /* x-bitmap order is reversed on v5 touchpads */
458 if (priv->proto_version == ALPS_PROTO_V5) {
459 for (i = 0; i < 4; i++)
460 corner[i].x = priv->x_max - corner[i].x;
Hans de Goede40e8f532014-07-25 22:37:15 -0700461 }
462
Hans de Goededccf1dd2015-05-20 14:43:09 -0700463 /* y-bitmap order is reversed on v3 and v4 touchpads */
464 if (priv->proto_version == ALPS_PROTO_V3 ||
465 priv->proto_version == ALPS_PROTO_V4) {
Hans de Goede4dd26572015-05-20 14:41:10 -0700466 for (i = 0; i < 4; i++)
467 corner[i].y = priv->y_max - corner[i].y;
Seth Forshee01ce6612011-11-07 19:54:13 -0800468 }
469
Hans de Goede4dd26572015-05-20 14:41:10 -0700470 /*
471 * We only select a corner for the second touch once per 2 finger
472 * touch sequence to avoid the chosen corner (and thus the coordinates)
473 * jumping around when the first touch is in the middle.
474 */
475 if (priv->second_touch == -1) {
476 /* Find corner closest to our st coordinates */
477 closest = 0x7fffffff;
478 for (i = 0; i < 4; i++) {
479 int dx = fields->st.x - corner[i].x;
480 int dy = fields->st.y - corner[i].y;
481 int distance = dx * dx + dy * dy;
482
483 if (distance < closest) {
484 priv->second_touch = i;
485 closest = distance;
486 }
487 }
488 /* And select the opposite corner to use for the 2nd touch */
489 priv->second_touch = (priv->second_touch + 2) % 4;
490 }
491
492 fields->mt[0] = fields->st;
493 fields->mt[1] = corner[priv->second_touch];
494
Seth Forshee01ce6612011-11-07 19:54:13 -0800495 return fingers;
496}
497
Hans de Goedecdf333e2014-07-25 22:44:42 -0700498static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800499{
500 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700501 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
502 input_report_abs(dev, ABS_MT_POSITION_X, x);
503 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800504}
505
Hans de Goedecdf333e2014-07-25 22:44:42 -0700506static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800507{
Hans de Goede02d04252014-07-25 22:43:35 -0700508 struct alps_data *priv = psmouse->private;
509 struct input_dev *dev = psmouse->dev;
510 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700511 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700512
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800513 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700514 for (i = 0; i < n; i++)
515 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
516
517 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800518}
519
Hans de Goede68c21872014-07-25 22:47:25 -0700520static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
521{
522 struct alps_data *priv = psmouse->private;
523 struct input_dev *dev = psmouse->dev;
524 struct alps_fields *f = &priv->f;
525
526 /* Use st data when we don't have mt data */
527 if (fingers < 2) {
528 f->mt[0].x = f->st.x;
529 f->mt[0].y = f->st.y;
530 fingers = f->pressure > 0 ? 1 : 0;
Hans de Goede4dd26572015-05-20 14:41:10 -0700531 priv->second_touch = -1;
Hans de Goede68c21872014-07-25 22:47:25 -0700532 }
533
Hans de Goede1662c0332015-05-20 14:42:15 -0700534 if (fingers >= 1)
535 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
536 if (fingers >= 2)
537 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
538 input_mt_sync_frame(dev);
Hans de Goede68c21872014-07-25 22:47:25 -0700539
540 input_mt_report_finger_count(dev, fingers);
541
542 input_report_key(dev, BTN_LEFT, f->left);
543 input_report_key(dev, BTN_RIGHT, f->right);
544 input_report_key(dev, BTN_MIDDLE, f->middle);
545
546 input_report_abs(dev, ABS_PRESSURE, f->pressure);
547
548 input_sync(dev);
549}
550
Seth Forshee25bded72011-11-07 19:53:36 -0800551static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
552{
553 struct alps_data *priv = psmouse->private;
554 unsigned char *packet = psmouse->packet;
555 struct input_dev *dev = priv->dev2;
556 int x, y, z, left, right, middle;
557
Pali Rohár34412ba2015-01-09 12:48:58 -0800558 /* It should be a DualPoint when received trackstick packet */
559 if (!(priv->flags & ALPS_DUALPOINT)) {
560 psmouse_warn(psmouse,
561 "Rejected trackstick packet from non DualPoint device");
562 return;
563 }
564
Seth Forshee25bded72011-11-07 19:53:36 -0800565 /* Sanity check packet */
566 if (!(packet[0] & 0x40)) {
567 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
568 return;
569 }
570
571 /*
572 * There's a special packet that seems to indicate the end
573 * of a stream of trackstick data. Filter these out.
574 */
575 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
576 return;
577
578 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
579 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
580 z = (packet[4] & 0x7c) >> 2;
581
582 /*
583 * The x and y values tend to be quite large, and when used
584 * alone the trackstick is difficult to use. Scale them down
585 * to compensate.
586 */
587 x /= 8;
588 y /= 8;
589
590 input_report_rel(dev, REL_X, x);
591 input_report_rel(dev, REL_Y, -y);
592
593 /*
594 * Most ALPS models report the trackstick buttons in the touchpad
595 * packets, but a few report them here. No reliable way has been
596 * found to differentiate between the models upfront, so we enable
597 * the quirk in response to seeing a button press in the trackstick
598 * packet.
599 */
600 left = packet[3] & 0x01;
601 right = packet[3] & 0x02;
602 middle = packet[3] & 0x04;
603
604 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
605 (left || right || middle))
606 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
607
608 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
609 input_report_key(dev, BTN_LEFT, left);
610 input_report_key(dev, BTN_RIGHT, right);
611 input_report_key(dev, BTN_MIDDLE, middle);
612 }
613
614 input_sync(dev);
615 return;
616}
617
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800618static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
619{
620 f->left = !!(p[3] & 0x01);
621 f->right = !!(p[3] & 0x02);
622 f->middle = !!(p[3] & 0x04);
623
624 f->ts_left = !!(p[3] & 0x10);
625 f->ts_right = !!(p[3] & 0x20);
626 f->ts_middle = !!(p[3] & 0x40);
627}
628
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700629static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800630 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800631{
632 f->first_mp = !!(p[4] & 0x40);
633 f->is_mp = !!(p[0] & 0x40);
634
Hans de Goedea839cd52015-05-20 14:39:21 -0700635 if (f->is_mp) {
636 f->fingers = (p[5] & 0x3) + 1;
637 f->x_map = ((p[4] & 0x7e) << 8) |
638 ((p[1] & 0x7f) << 2) |
639 ((p[0] & 0x30) >> 4);
640 f->y_map = ((p[3] & 0x70) << 4) |
641 ((p[2] & 0x7f) << 1) |
642 (p[4] & 0x01);
643 } else {
644 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
645 ((p[0] & 0x30) >> 4);
646 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
647 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800648
Hans de Goedea839cd52015-05-20 14:39:21 -0700649 alps_decode_buttons_v3(f, p);
650 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700651
652 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800653}
654
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700655static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800656 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800657{
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700658 f->first_mp = !!(p[4] & 0x40);
Yunkang Tangf105e342014-07-25 22:29:24 -0700659 f->is_mp = !!(p[5] & 0x40);
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700660
Hans de Goedea839cd52015-05-20 14:39:21 -0700661 if (f->is_mp) {
662 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
663 f->x_map = ((p[5] & 0x10) << 11) |
664 ((p[4] & 0x7e) << 8) |
665 ((p[1] & 0x7f) << 2) |
666 ((p[0] & 0x30) >> 4);
667 f->y_map = ((p[5] & 0x20) << 6) |
668 ((p[3] & 0x70) << 4) |
669 ((p[2] & 0x7f) << 1) |
670 (p[4] & 0x01);
671 } else {
672 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
673 ((p[0] & 0x30) >> 4);
674 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
675 f->pressure = p[5] & 0x7f;
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700676
Hans de Goedea839cd52015-05-20 14:39:21 -0700677 alps_decode_buttons_v3(f, p);
678 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700679
680 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800681}
682
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700683static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800684 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800685{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800686 u64 palm_data = 0;
687 struct alps_data *priv = psmouse->private;
688
Dave Turvene75af9e52013-02-21 22:58:28 -0800689 f->first_mp = !!(p[0] & 0x02);
690 f->is_mp = !!(p[0] & 0x20);
691
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800692 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700693 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
694 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
695 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800696 alps_decode_buttons_v3(f, p);
697 } else {
698 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800699 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800700
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800701 palm_data = (p[1] & 0x7f) |
702 ((p[2] & 0x7f) << 7) |
703 ((p[4] & 0x7f) << 14) |
704 ((p[5] & 0x7f) << 21) |
705 ((p[3] & 0x07) << 28) |
706 (((u64)p[3] & 0x70) << 27) |
707 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800708
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800709 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
710 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
711
712 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
713 f->x_map = (palm_data >> priv->y_bits) &
714 (BIT(priv->x_bits) - 1);
715 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700716
717 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800718}
719
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800720static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800721{
722 struct alps_data *priv = psmouse->private;
723 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800724 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700725 struct alps_fields *f = &priv->f;
726 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800727
Hans de Goede02d04252014-07-25 22:43:35 -0700728 memset(f, 0, sizeof(*f));
729
730 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800731
732 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800733 * There's no single feature of touchpad position and bitmap packets
734 * that can be used to distinguish between them. We rely on the fact
735 * that a bitmap packet should always follow a position packet with
736 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800737 */
738 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800739 /*
740 * Sometimes a position packet will indicate a multi-packet
741 * sequence, but then what follows is another position
742 * packet. Check for this, and when it happens process the
743 * position packet as usual.
744 */
Hans de Goede02d04252014-07-25 22:43:35 -0700745 if (f->is_mp) {
746 fingers = f->fingers;
Hans de Goede44b77f32015-05-20 14:40:06 -0700747 /*
748 * Bitmap processing uses position packet's coordinate
749 * data, so we need to do decode it first.
750 */
751 priv->decode_fields(f, priv->multi_data, psmouse);
Hans de Goededccf1dd2015-05-20 14:43:09 -0700752 if (alps_process_bitmap(priv, f) == 0)
753 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800754 } else {
755 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800756 }
757 }
758
Seth Forshee01ce6612011-11-07 19:54:13 -0800759 /*
760 * Bit 6 of byte 0 is not usually set in position packets. The only
761 * times it seems to be set is in situations where the data is
762 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
763 * this combined with the fact that this bit is useful for filtering
764 * out misidentified bitmap packets, we reject anything with this
765 * bit set.
766 */
Hans de Goede02d04252014-07-25 22:43:35 -0700767 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800768 return;
769
Hans de Goede02d04252014-07-25 22:43:35 -0700770 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800771 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800772 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
773 return;
774 }
775
776 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800777
Seth Forshee25bded72011-11-07 19:53:36 -0800778 /*
779 * Sometimes the hardware sends a single packet with z = 0
780 * in the middle of a stream. Real releases generate packets
781 * with x, y, and z all zero, so these seem to be flukes.
782 * Ignore them.
783 */
Hans de Goede02d04252014-07-25 22:43:35 -0700784 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800785 return;
786
Hans de Goede68c21872014-07-25 22:47:25 -0700787 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800788
Pali Rohár34412ba2015-01-09 12:48:58 -0800789 if ((priv->flags & ALPS_DUALPOINT) &&
790 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700791 input_report_key(dev2, BTN_LEFT, f->ts_left);
792 input_report_key(dev2, BTN_RIGHT, f->ts_right);
793 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800794 input_sync(dev2);
795 }
796}
797
798static void alps_process_packet_v3(struct psmouse *psmouse)
799{
800 unsigned char *packet = psmouse->packet;
801
802 /*
803 * v3 protocol packets come in three types, two representing
804 * touchpad data and one representing trackstick data.
805 * Trackstick packets seem to be distinguished by always
806 * having 0x3f in the last byte. This value has never been
807 * observed in the last byte of either of the other types
808 * of packets.
809 */
810 if (packet[5] == 0x3f) {
811 alps_process_trackstick_packet_v3(psmouse);
812 return;
813 }
814
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800815 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800816}
817
Yunkang Tang95f75e92013-12-01 22:33:52 -0800818static void alps_process_packet_v6(struct psmouse *psmouse)
819{
820 struct alps_data *priv = psmouse->private;
821 unsigned char *packet = psmouse->packet;
822 struct input_dev *dev = psmouse->dev;
823 struct input_dev *dev2 = priv->dev2;
824 int x, y, z, left, right, middle;
825
826 /*
827 * We can use Byte5 to distinguish if the packet is from Touchpad
828 * or Trackpoint.
829 * Touchpad: 0 - 0x7E
830 * Trackpoint: 0x7F
831 */
832 if (packet[5] == 0x7F) {
833 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800834 if (!(priv->flags & ALPS_DUALPOINT)) {
835 psmouse_warn(psmouse,
836 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800837 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800838 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800839
840 /* Trackpoint packet */
841 x = packet[1] | ((packet[3] & 0x20) << 2);
842 y = packet[2] | ((packet[3] & 0x40) << 1);
843 z = packet[4];
844 left = packet[3] & 0x01;
845 right = packet[3] & 0x02;
846 middle = packet[3] & 0x04;
847
848 /* To prevent the cursor jump when finger lifted */
849 if (x == 0x7F && y == 0x7F && z == 0x7F)
850 x = y = z = 0;
851
852 /* Divide 4 since trackpoint's speed is too fast */
853 input_report_rel(dev2, REL_X, (char)x / 4);
854 input_report_rel(dev2, REL_Y, -((char)y / 4));
855
856 input_report_key(dev2, BTN_LEFT, left);
857 input_report_key(dev2, BTN_RIGHT, right);
858 input_report_key(dev2, BTN_MIDDLE, middle);
859
860 input_sync(dev2);
861 return;
862 }
863
864 /* Touchpad packet */
865 x = packet[1] | ((packet[3] & 0x78) << 4);
866 y = packet[2] | ((packet[4] & 0x78) << 4);
867 z = packet[5];
868 left = packet[3] & 0x01;
869 right = packet[3] & 0x02;
870
871 if (z > 30)
872 input_report_key(dev, BTN_TOUCH, 1);
873 if (z < 25)
874 input_report_key(dev, BTN_TOUCH, 0);
875
876 if (z > 0) {
877 input_report_abs(dev, ABS_X, x);
878 input_report_abs(dev, ABS_Y, y);
879 }
880
881 input_report_abs(dev, ABS_PRESSURE, z);
882 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
883
884 /* v6 touchpad does not have middle button */
885 input_report_key(dev, BTN_LEFT, left);
886 input_report_key(dev, BTN_RIGHT, right);
887
888 input_sync(dev);
889}
890
Seth Forshee25bded72011-11-07 19:53:36 -0800891static void alps_process_packet_v4(struct psmouse *psmouse)
892{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700893 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800894 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700895 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700896 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700897
898 /*
899 * v4 has a 6-byte encoding for bitmap data, but this data is
900 * broken up between 3 normal packets. Use priv->multi_packet to
901 * track our position in the bitmap packet.
902 */
903 if (packet[6] & 0x40) {
904 /* sync, reset position */
905 priv->multi_packet = 0;
906 }
907
908 if (WARN_ON_ONCE(priv->multi_packet > 2))
909 return;
910
911 offset = 2 * priv->multi_packet;
912 priv->multi_data[offset] = packet[6];
913 priv->multi_data[offset + 1] = packet[7];
914
Hans de Goede44b77f32015-05-20 14:40:06 -0700915 f->left = !!(packet[4] & 0x01);
916 f->right = !!(packet[4] & 0x02);
917
918 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
919 ((packet[0] & 0x30) >> 4);
920 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
921 f->pressure = packet[5] & 0x7f;
922
George Pantalos3b7e09f2012-05-10 22:31:59 -0700923 if (++priv->multi_packet > 2) {
924 priv->multi_packet = 0;
925
Hans de Goede02d04252014-07-25 22:43:35 -0700926 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700927 ((priv->multi_data[3] & 0x60) << 3) |
928 ((priv->multi_data[0] & 0x3f) << 2) |
929 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700930 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700931 ((priv->multi_data[3] & 0x1f) << 5) |
932 (priv->multi_data[1] & 0x1f);
933
Hans de Goede02d04252014-07-25 22:43:35 -0700934 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700935 }
Seth Forshee25bded72011-11-07 19:53:36 -0800936
Hans de Goede68c21872014-07-25 22:47:25 -0700937 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800938}
939
Yunkang Tang38088432014-07-26 13:51:41 -0700940static bool alps_is_valid_package_v7(struct psmouse *psmouse)
941{
942 switch (psmouse->pktcnt) {
943 case 3:
944 return (psmouse->packet[2] & 0x40) == 0x40;
945 case 4:
946 return (psmouse->packet[3] & 0x48) == 0x48;
947 case 6:
948 return (psmouse->packet[5] & 0x40) == 0x00;
949 }
950 return true;
951}
952
953static unsigned char alps_get_packet_id_v7(char *byte)
954{
955 unsigned char packet_id;
956
957 if (byte[4] & 0x40)
958 packet_id = V7_PACKET_ID_TWO;
959 else if (byte[4] & 0x01)
960 packet_id = V7_PACKET_ID_MULTI;
961 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
962 packet_id = V7_PACKET_ID_NEW;
963 else if (byte[1] == 0x00 && byte[4] == 0x00)
964 packet_id = V7_PACKET_ID_IDLE;
965 else
966 packet_id = V7_PACKET_ID_UNKNOWN;
967
968 return packet_id;
969}
970
971static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
972 unsigned char *pkt,
973 unsigned char pkt_id)
974{
975 mt[0].x = ((pkt[2] & 0x80) << 4);
976 mt[0].x |= ((pkt[2] & 0x3F) << 5);
977 mt[0].x |= ((pkt[3] & 0x30) >> 1);
978 mt[0].x |= (pkt[3] & 0x07);
979 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
980
981 mt[1].x = ((pkt[3] & 0x80) << 4);
982 mt[1].x |= ((pkt[4] & 0x80) << 3);
983 mt[1].x |= ((pkt[4] & 0x3F) << 4);
984 mt[1].y = ((pkt[5] & 0x80) << 3);
985 mt[1].y |= ((pkt[5] & 0x3F) << 4);
986
987 switch (pkt_id) {
988 case V7_PACKET_ID_TWO:
989 mt[1].x &= ~0x000F;
990 mt[1].y |= 0x000F;
Hans de Goede72eceab2015-05-20 14:37:41 -0700991 /* Detect false-postive touches where x & y report max value */
992 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
993 mt[1].x = 0;
994 /* y gets set to 0 at the end of this function */
995 }
Yunkang Tang38088432014-07-26 13:51:41 -0700996 break;
997
998 case V7_PACKET_ID_MULTI:
999 mt[1].x &= ~0x003F;
1000 mt[1].y &= ~0x0020;
1001 mt[1].y |= ((pkt[4] & 0x02) << 4);
1002 mt[1].y |= 0x001F;
1003 break;
1004
1005 case V7_PACKET_ID_NEW:
1006 mt[1].x &= ~0x003F;
1007 mt[1].x |= (pkt[0] & 0x20);
1008 mt[1].y |= 0x000F;
1009 break;
1010 }
1011
1012 mt[0].y = 0x7FF - mt[0].y;
1013 mt[1].y = 0x7FF - mt[1].y;
1014}
1015
1016static int alps_get_mt_count(struct input_mt_pos *mt)
1017{
Hans de Goede7091c442014-12-18 09:53:34 -08001018 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -07001019
Hans de Goede7091c442014-12-18 09:53:34 -08001020 for (i = 0; i < MAX_TOUCHES; i++) {
1021 if (mt[i].x != 0 || mt[i].y != 0)
1022 fingers++;
1023 }
Yunkang Tang38088432014-07-26 13:51:41 -07001024
Hans de Goede7091c442014-12-18 09:53:34 -08001025 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -07001026}
1027
1028static int alps_decode_packet_v7(struct alps_fields *f,
1029 unsigned char *p,
1030 struct psmouse *psmouse)
1031{
Hans de Goeded27eb792014-12-18 09:55:14 -08001032 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -07001033 unsigned char pkt_id;
1034
1035 pkt_id = alps_get_packet_id_v7(p);
1036 if (pkt_id == V7_PACKET_ID_IDLE)
1037 return 0;
1038 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1039 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -08001040 /*
1041 * NEW packets are send to indicate a discontinuity in the finger
1042 * coordinate reporting. Specifically a finger may have moved from
1043 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1044 * us.
1045 *
1046 * NEW packets have 3 problems:
1047 * 1) They do not contain middle / right button info (on non clickpads)
1048 * this can be worked around by preserving the old button state
1049 * 2) They do not contain an accurate fingercount, and they are
1050 * typically send when the number of fingers changes. We cannot use
1051 * the old finger count as that may mismatch with the amount of
1052 * touch coordinates we've available in the NEW packet
1053 * 3) Their x data for the second touch is inaccurate leading to
1054 * a possible jump of the x coordinate by 16 units when the first
1055 * non NEW packet comes in
1056 * Since problems 2 & 3 cannot be worked around, just ignore them.
1057 */
1058 if (pkt_id == V7_PACKET_ID_NEW)
1059 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001060
1061 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1062
Yunkang Tang38088432014-07-26 13:51:41 -07001063 if (pkt_id == V7_PACKET_ID_TWO)
1064 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001065 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001066 f->fingers = 3 + (p[5] & 0x03);
1067
Hans de Goeded27eb792014-12-18 09:55:14 -08001068 f->left = (p[0] & 0x80) >> 7;
1069 if (priv->flags & ALPS_BUTTONPAD) {
1070 if (p[0] & 0x20)
1071 f->fingers++;
1072 if (p[0] & 0x10)
1073 f->fingers++;
1074 } else {
1075 f->right = (p[0] & 0x20) >> 5;
1076 f->middle = (p[0] & 0x10) >> 4;
1077 }
1078
Hans de Goede7091c442014-12-18 09:53:34 -08001079 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1080 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1081 f->mt[0].x = f->mt[1].x;
1082 f->mt[0].y = f->mt[1].y;
1083 f->mt[1].x = 0;
1084 f->mt[1].y = 0;
1085 }
1086
Yunkang Tang38088432014-07-26 13:51:41 -07001087 return 0;
1088}
1089
1090static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1091{
1092 struct alps_data *priv = psmouse->private;
1093 unsigned char *packet = psmouse->packet;
1094 struct input_dev *dev2 = priv->dev2;
1095 int x, y, z, left, right, middle;
1096
Pali Rohár34412ba2015-01-09 12:48:58 -08001097 /* It should be a DualPoint when received trackstick packet */
1098 if (!(priv->flags & ALPS_DUALPOINT)) {
1099 psmouse_warn(psmouse,
1100 "Rejected trackstick packet from non DualPoint device");
1101 return;
1102 }
1103
Yunkang Tang38088432014-07-26 13:51:41 -07001104 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1105 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1106 ((packet[3] & 0x20) << 1);
1107 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1108
1109 left = (packet[1] & 0x01);
1110 right = (packet[1] & 0x02) >> 1;
1111 middle = (packet[1] & 0x04) >> 2;
1112
Hans de Goede088df2c2015-06-04 22:31:43 -07001113 input_report_rel(dev2, REL_X, (char)x);
1114 input_report_rel(dev2, REL_Y, -((char)y));
Yunkang Tang38088432014-07-26 13:51:41 -07001115
1116 input_report_key(dev2, BTN_LEFT, left);
1117 input_report_key(dev2, BTN_RIGHT, right);
1118 input_report_key(dev2, BTN_MIDDLE, middle);
1119
1120 input_sync(dev2);
1121}
1122
1123static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1124{
1125 struct alps_data *priv = psmouse->private;
1126 struct input_dev *dev = psmouse->dev;
1127 struct alps_fields *f = &priv->f;
1128
1129 memset(f, 0, sizeof(*f));
1130
1131 if (priv->decode_fields(f, psmouse->packet, psmouse))
1132 return;
1133
1134 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1135
1136 input_mt_report_finger_count(dev, f->fingers);
1137
1138 input_report_key(dev, BTN_LEFT, f->left);
1139 input_report_key(dev, BTN_RIGHT, f->right);
1140 input_report_key(dev, BTN_MIDDLE, f->middle);
1141
1142 input_sync(dev);
1143}
1144
1145static void alps_process_packet_v7(struct psmouse *psmouse)
1146{
1147 unsigned char *packet = psmouse->packet;
1148
1149 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1150 alps_process_trackstick_packet_v7(psmouse);
1151 else
1152 alps_process_touchpad_packet_v7(psmouse);
1153}
1154
Fengguang Wu07f19e32015-04-10 23:54:31 -07001155static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001156{
1157 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1158
Ben Gamari4777ac22016-10-04 11:33:25 -07001159 switch (byte[3] & 0x30) {
1160 case 0x00:
1161 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1162 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 &&
1163 byte[5] == 0x00) {
1164 pkt_id = SS4_PACKET_ID_IDLE;
1165 } else {
1166 pkt_id = SS4_PACKET_ID_ONE;
1167 }
1168 break;
1169 case 0x10:
1170 /* two-finger finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001171 pkt_id = SS4_PACKET_ID_TWO;
Ben Gamari4777ac22016-10-04 11:33:25 -07001172 break;
1173 case 0x20:
1174 /* stick pointer */
1175 pkt_id = SS4_PACKET_ID_STICK;
1176 break;
1177 case 0x30:
1178 /* third and fourth finger positions */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001179 pkt_id = SS4_PACKET_ID_MULTI;
Ben Gamari4777ac22016-10-04 11:33:25 -07001180 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001181 }
1182
1183 return pkt_id;
1184}
1185
1186static int alps_decode_ss4_v2(struct alps_fields *f,
1187 unsigned char *p, struct psmouse *psmouse)
1188{
1189 struct alps_data *priv = psmouse->private;
1190 unsigned char pkt_id;
1191 unsigned int no_data_x, no_data_y;
1192
1193 pkt_id = alps_get_pkt_id_ss4_v2(p);
1194
1195 /* Current packet is 1Finger coordinate packet */
1196 switch (pkt_id) {
1197 case SS4_PACKET_ID_ONE:
1198 f->mt[0].x = SS4_1F_X_V2(p);
1199 f->mt[0].y = SS4_1F_Y_V2(p);
1200 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
Ben Gamaria8317762016-10-04 11:40:44 -07001201 /*
1202 * When a button is held the device will give us events
1203 * with x, y, and pressure of 0. This causes annoying jumps
1204 * if a touch is released while the button is held.
1205 * Handle this by claiming zero contacts.
1206 */
1207 f->fingers = f->pressure > 0 ? 1 : 0;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001208 f->first_mp = 0;
1209 f->is_mp = 0;
1210 break;
1211
1212 case SS4_PACKET_ID_TWO:
1213 if (priv->flags & ALPS_BUTTONPAD) {
1214 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1215 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1216 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1217 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1218 } else {
1219 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1220 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1221 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1222 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1223 }
1224 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1225
1226 if (SS4_IS_MF_CONTINUE(p)) {
1227 f->first_mp = 1;
1228 } else {
1229 f->fingers = 2;
1230 f->first_mp = 0;
1231 }
1232 f->is_mp = 0;
1233
1234 break;
1235
1236 case SS4_PACKET_ID_MULTI:
1237 if (priv->flags & ALPS_BUTTONPAD) {
1238 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1239 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1240 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1241 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1242 no_data_x = SS4_MFPACKET_NO_AX_BL;
1243 no_data_y = SS4_MFPACKET_NO_AY_BL;
1244 } else {
1245 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1246 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1247 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1248 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1249 no_data_x = SS4_MFPACKET_NO_AX;
1250 no_data_y = SS4_MFPACKET_NO_AY;
1251 }
1252
1253 f->first_mp = 0;
1254 f->is_mp = 1;
1255
1256 if (SS4_IS_5F_DETECTED(p)) {
1257 f->fingers = 5;
1258 } else if (f->mt[3].x == no_data_x &&
1259 f->mt[3].y == no_data_y) {
1260 f->mt[3].x = 0;
1261 f->mt[3].y = 0;
1262 f->fingers = 3;
1263 } else {
1264 f->fingers = 4;
1265 }
1266 break;
1267
Ben Gamari4777ac22016-10-04 11:33:25 -07001268 case SS4_PACKET_ID_STICK:
1269 if (!(priv->flags & ALPS_DUALPOINT)) {
1270 psmouse_warn(psmouse,
1271 "Rejected trackstick packet from non DualPoint device");
1272 } else {
1273 int x = (s8)(((p[0] & 1) << 7) | (p[1] & 0x7f));
1274 int y = (s8)(((p[3] & 1) << 7) | (p[2] & 0x7f));
1275
1276 input_report_rel(priv->dev2, REL_X, x);
1277 input_report_rel(priv->dev2, REL_Y, -y);
1278 }
1279 break;
1280
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001281 case SS4_PACKET_ID_IDLE:
1282 default:
1283 memset(f, 0, sizeof(struct alps_fields));
1284 break;
1285 }
1286
Ben Gamari4777ac22016-10-04 11:33:25 -07001287 /* handle buttons */
1288 if (pkt_id == SS4_PACKET_ID_STICK) {
1289 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1290 if (!(priv->flags & ALPS_BUTTONPAD)) {
1291 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1292 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1293 }
1294 } else {
1295 f->left = !!(SS4_BTN_V2(p) & 0x01);
1296 if (!(priv->flags & ALPS_BUTTONPAD)) {
1297 f->right = !!(SS4_BTN_V2(p) & 0x02);
1298 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1299 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001300 }
1301
1302 return 0;
1303}
1304
1305static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1306{
1307 struct alps_data *priv = psmouse->private;
1308 unsigned char *packet = psmouse->packet;
1309 struct input_dev *dev = psmouse->dev;
Ben Gamari4777ac22016-10-04 11:33:25 -07001310 struct input_dev *dev2 = priv->dev2;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001311 struct alps_fields *f = &priv->f;
1312
1313 memset(f, 0, sizeof(struct alps_fields));
1314 priv->decode_fields(f, packet, psmouse);
1315 if (priv->multi_packet) {
1316 /*
1317 * Sometimes the first packet will indicate a multi-packet
1318 * sequence, but sometimes the next multi-packet would not
1319 * come. Check for this, and when it happens process the
1320 * position packet as usual.
1321 */
1322 if (f->is_mp) {
1323 /* Now process the 1st packet */
1324 priv->decode_fields(f, priv->multi_data, psmouse);
1325 } else {
1326 priv->multi_packet = 0;
1327 }
1328 }
1329
1330 /*
1331 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1332 * When it is set, it means 2nd packet comes without 1st packet come.
1333 */
1334 if (f->is_mp)
1335 return;
1336
1337 /* Save the first packet */
1338 if (!priv->multi_packet && f->first_mp) {
1339 priv->multi_packet = 1;
1340 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1341 return;
1342 }
1343
1344 priv->multi_packet = 0;
1345
1346 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1347
1348 input_mt_report_finger_count(dev, f->fingers);
1349
1350 input_report_key(dev, BTN_LEFT, f->left);
1351 input_report_key(dev, BTN_RIGHT, f->right);
1352 input_report_key(dev, BTN_MIDDLE, f->middle);
1353
1354 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1355 input_sync(dev);
Ben Gamari4777ac22016-10-04 11:33:25 -07001356
1357 if (priv->flags & ALPS_DUALPOINT) {
1358 input_report_key(dev2, BTN_LEFT, f->ts_left);
1359 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1360 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1361 input_sync(dev2);
1362 }
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001363}
1364
1365static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1366{
1367 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1368 return false;
1369 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1370 return false;
1371 return true;
1372}
1373
Pali Rohár04aae282015-01-14 13:18:30 -08001374static DEFINE_MUTEX(alps_mutex);
1375
1376static void alps_register_bare_ps2_mouse(struct work_struct *work)
1377{
1378 struct alps_data *priv =
1379 container_of(work, struct alps_data, dev3_register_work.work);
1380 struct psmouse *psmouse = priv->psmouse;
1381 struct input_dev *dev3;
1382 int error = 0;
1383
1384 mutex_lock(&alps_mutex);
1385
1386 if (priv->dev3)
1387 goto out;
1388
1389 dev3 = input_allocate_device();
1390 if (!dev3) {
1391 psmouse_err(psmouse, "failed to allocate secondary device\n");
1392 error = -ENOMEM;
1393 goto out;
1394 }
1395
1396 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1397 psmouse->ps2dev.serio->phys,
1398 (priv->dev2 ? "input2" : "input1"));
1399 dev3->phys = priv->phys3;
1400
1401 /*
1402 * format of input device name is: "protocol vendor name"
1403 * see function psmouse_switch_protocol() in psmouse-base.c
1404 */
1405 dev3->name = "PS/2 ALPS Mouse";
1406
1407 dev3->id.bustype = BUS_I8042;
1408 dev3->id.vendor = 0x0002;
1409 dev3->id.product = PSMOUSE_PS2;
1410 dev3->id.version = 0x0000;
1411 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1412
1413 input_set_capability(dev3, EV_REL, REL_X);
1414 input_set_capability(dev3, EV_REL, REL_Y);
1415 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1416 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1417 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1418
1419 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1420
1421 error = input_register_device(dev3);
1422 if (error) {
1423 psmouse_err(psmouse,
1424 "failed to register secondary device: %d\n",
1425 error);
1426 input_free_device(dev3);
1427 goto out;
1428 }
1429
1430 priv->dev3 = dev3;
1431
1432out:
1433 /*
1434 * Save the error code so that we can detect that we
1435 * already tried to create the device.
1436 */
1437 if (error)
1438 priv->dev3 = ERR_PTR(error);
1439
1440 mutex_unlock(&alps_mutex);
1441}
1442
Hans de Goede59c30af2015-04-03 17:14:40 -07001443static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001444 unsigned char packet[],
1445 bool report_buttons)
1446{
Hans de Goede59c30af2015-04-03 17:14:40 -07001447 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001448 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001449
Hans de Goedee3a79212015-04-03 17:20:05 -07001450 /* Figure out which device to use to report the bare packet */
1451 if (priv->proto_version == ALPS_PROTO_V2 &&
1452 (priv->flags & ALPS_DUALPOINT)) {
1453 /* On V2 devices the DualPoint Stick reports bare packets */
1454 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001455 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001456 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001457 /* Register dev3 mouse if we received PS/2 packet first time */
1458 if (!IS_ERR(priv->dev3))
1459 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1460 0);
1461 return;
1462 } else {
1463 dev = priv->dev3;
1464 }
1465
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001466 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001467 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001468 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1469
Pali Rohár04aae282015-01-14 13:18:30 -08001470 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001471 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001472 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001473 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1474
Pali Rohár04aae282015-01-14 13:18:30 -08001475 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001476}
1477
1478static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 struct alps_data *priv = psmouse->private;
1481
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001482 if (psmouse->pktcnt < 6)
1483 return PSMOUSE_GOOD_DATA;
1484
1485 if (psmouse->pktcnt == 6) {
1486 /*
1487 * Start a timer to flush the packet if it ends up last
1488 * 6-byte packet in the stream. Timer needs to fire
1489 * psmouse core times out itself. 20 ms should be enough
1490 * to decide if we are getting more data or not.
1491 */
1492 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1493 return PSMOUSE_GOOD_DATA;
1494 }
1495
1496 del_timer(&priv->timer);
1497
1498 if (psmouse->packet[6] & 0x80) {
1499
1500 /*
1501 * Highest bit is set - that means we either had
1502 * complete ALPS packet and this is start of the
1503 * next packet or we got garbage.
1504 */
1505
1506 if (((psmouse->packet[3] |
1507 psmouse->packet[4] |
1508 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001509 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001510 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001511 "refusing packet %4ph (suspected interleaved ps/2)\n",
1512 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001513 return PSMOUSE_BAD_DATA;
1514 }
1515
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001516 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001517
1518 /* Continue with the next packet */
1519 psmouse->packet[0] = psmouse->packet[6];
1520 psmouse->pktcnt = 1;
1521
1522 } else {
1523
1524 /*
1525 * High bit is 0 - that means that we indeed got a PS/2
1526 * packet in the middle of ALPS packet.
1527 *
1528 * There is also possibility that we got 6-byte ALPS
1529 * packet followed by 3-byte packet from trackpoint. We
1530 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001531 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001532 * normal operation (user would need to press all
1533 * buttons on the pad and start moving trackpoint
1534 * without touching the pad surface) we assume former.
1535 * Even if we are wrong the wost thing that would happen
1536 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001537 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001538 */
1539
Hans de Goede59c30af2015-04-03 17:14:40 -07001540 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1541 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001542
1543 /*
1544 * Continue with the standard ALPS protocol handling,
1545 * but make sure we won't process it as an interleaved
1546 * packet again, which may happen if all buttons are
1547 * pressed. To avoid this let's reset the 4th bit which
1548 * is normally 1.
1549 */
1550 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1551 psmouse->pktcnt = 4;
1552 }
1553
1554 return PSMOUSE_GOOD_DATA;
1555}
1556
1557static void alps_flush_packet(unsigned long data)
1558{
1559 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001560 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001561
1562 serio_pause_rx(psmouse->ps2dev.serio);
1563
Seth Forsheeb46615f2011-11-07 19:53:30 -08001564 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001565
1566 /*
1567 * We did not any more data in reasonable amount of time.
1568 * Validate the last 3 bytes and process as a standard
1569 * ALPS packet.
1570 */
1571 if ((psmouse->packet[3] |
1572 psmouse->packet[4] |
1573 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001574 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001575 "refusing packet %3ph (suspected interleaved ps/2)\n",
1576 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001577 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001578 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001579 }
1580 psmouse->pktcnt = 0;
1581 }
1582
1583 serio_continue_rx(psmouse->ps2dev.serio);
1584}
1585
1586static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1587{
1588 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001589
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001590 /*
1591 * Check if we are dealing with a bare PS/2 packet, presumably from
1592 * a device connected to the external PS/2 port. Because bare PS/2
1593 * protocol does not have enough constant bits to self-synchronize
1594 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001595 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001596 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001597 if (priv->proto_version != ALPS_PROTO_V8 &&
1598 !psmouse->out_of_sync_cnt &&
1599 (psmouse->packet[0] & 0xc8) == 0x08) {
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001602 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1603 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return PSMOUSE_FULL_PACKET;
1605 }
1606 return PSMOUSE_GOOD_DATA;
1607 }
1608
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001609 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1610
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001611 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001612 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1613 return alps_handle_interleaved_ps2(psmouse);
1614 }
1615
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001616 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001617 psmouse_dbg(psmouse,
1618 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001619 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Seth Forsheeb46615f2011-11-07 19:53:30 -08001623 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001624 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001625 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001626 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001627 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1628 psmouse->pktcnt - 1,
1629 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001630
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001631 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001632 psmouse->pktcnt == psmouse->pktsize) {
1633 /*
1634 * Some Dell boxes, such as Latitude E6440 or E7440
1635 * with closed lid, quite often smash last byte of
1636 * otherwise valid packet with 0xff. Given that the
1637 * next packet is very likely to be valid let's
1638 * report PSMOUSE_FULL_PACKET but not process data,
1639 * rather than reporting PSMOUSE_BAD_DATA and
1640 * filling the logs.
1641 */
1642 return PSMOUSE_FULL_PACKET;
1643 }
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001648 if ((priv->proto_version == ALPS_PROTO_V7 &&
1649 !alps_is_valid_package_v7(psmouse)) ||
1650 (priv->proto_version == ALPS_PROTO_V8 &&
1651 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001652 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1653 psmouse->pktcnt - 1,
1654 psmouse->packet[psmouse->pktcnt - 1]);
1655 return PSMOUSE_BAD_DATA;
1656 }
1657
Seth Forsheeb46615f2011-11-07 19:53:30 -08001658 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001659 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return PSMOUSE_FULL_PACKET;
1661 }
1662
1663 return PSMOUSE_GOOD_DATA;
1664}
1665
Seth Forshee25bded72011-11-07 19:53:36 -08001666static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1667{
1668 struct ps2dev *ps2dev = &psmouse->ps2dev;
1669 struct alps_data *priv = psmouse->private;
1670 int command;
1671 unsigned char *param;
1672 unsigned char dummy[4];
1673
1674 BUG_ON(nibble > 0xf);
1675
1676 command = priv->nibble_commands[nibble].command;
1677 param = (command & 0x0f00) ?
1678 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1679
1680 if (ps2_command(ps2dev, param, command))
1681 return -1;
1682
1683 return 0;
1684}
1685
1686static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1687{
1688 struct ps2dev *ps2dev = &psmouse->ps2dev;
1689 struct alps_data *priv = psmouse->private;
1690 int i, nibble;
1691
1692 if (ps2_command(ps2dev, NULL, priv->addr_command))
1693 return -1;
1694
1695 for (i = 12; i >= 0; i -= 4) {
1696 nibble = (addr >> i) & 0xf;
1697 if (alps_command_mode_send_nibble(psmouse, nibble))
1698 return -1;
1699 }
1700
1701 return 0;
1702}
1703
1704static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1705{
1706 struct ps2dev *ps2dev = &psmouse->ps2dev;
1707 unsigned char param[4];
1708
1709 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1710 return -1;
1711
1712 /*
1713 * The address being read is returned in the first two bytes
1714 * of the result. Check that this address matches the expected
1715 * address.
1716 */
1717 if (addr != ((param[0] << 8) | param[1]))
1718 return -1;
1719
1720 return param[2];
1721}
1722
1723static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1724{
1725 if (alps_command_mode_set_addr(psmouse, addr))
1726 return -1;
1727 return __alps_command_mode_read_reg(psmouse, addr);
1728}
1729
1730static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1731{
1732 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1733 return -1;
1734 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1735 return -1;
1736 return 0;
1737}
1738
1739static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1740 u8 value)
1741{
1742 if (alps_command_mode_set_addr(psmouse, addr))
1743 return -1;
1744 return __alps_command_mode_write_reg(psmouse, value);
1745}
1746
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001747static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1748 int repeated_command, unsigned char *param)
1749{
1750 struct ps2dev *ps2dev = &psmouse->ps2dev;
1751
1752 param[0] = 0;
1753 if (init_command && ps2_command(ps2dev, param, init_command))
1754 return -EIO;
1755
1756 if (ps2_command(ps2dev, NULL, repeated_command) ||
1757 ps2_command(ps2dev, NULL, repeated_command) ||
1758 ps2_command(ps2dev, NULL, repeated_command))
1759 return -EIO;
1760
1761 param[0] = param[1] = param[2] = 0xff;
1762 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1763 return -EIO;
1764
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001765 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1766 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001767 return 0;
1768}
1769
Yunkang Tang38088432014-07-26 13:51:41 -07001770static bool alps_check_valid_firmware_id(unsigned char id[])
1771{
1772 if (id[0] == 0x73)
1773 return true;
1774
1775 if (id[0] == 0x88 &&
1776 (id[1] == 0x07 ||
1777 id[1] == 0x08 ||
1778 (id[1] & 0xf0) == 0xb0 ||
1779 (id[1] & 0xf0) == 0xc0)) {
1780 return true;
1781 }
1782
1783 return false;
1784}
1785
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001786static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001787{
1788 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001789
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001790 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001791 psmouse_err(psmouse, "failed to enter command mode\n");
1792 return -1;
1793 }
1794
Yunkang Tang38088432014-07-26 13:51:41 -07001795 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001796 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001797 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001798 return -1;
1799 }
Seth Forshee25bded72011-11-07 19:53:36 -08001800 return 0;
1801}
1802
1803static inline int alps_exit_command_mode(struct psmouse *psmouse)
1804{
1805 struct ps2dev *ps2dev = &psmouse->ps2dev;
1806 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1807 return -1;
1808 return 0;
1809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/*
1812 * For DualPoint devices select the device that should respond to
1813 * subsequent commands. It looks like glidepad is behind stickpointer,
1814 * I'd thought it would be other way around...
1815 */
Seth Forshee25bded72011-11-07 19:53:36 -08001816static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1820
1821 if (ps2_command(ps2dev, NULL, cmd) ||
1822 ps2_command(ps2dev, NULL, cmd) ||
1823 ps2_command(ps2dev, NULL, cmd) ||
1824 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1825 return -1;
1826
1827 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001828 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 return 0;
1831}
1832
Seth Forshee25bded72011-11-07 19:53:36 -08001833static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct ps2dev *ps2dev = &psmouse->ps2dev;
1836
1837 /* Try ALPS magic knock - 4 disable before enable */
1838 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1839 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1840 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1841 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1842 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1843 return -1;
1844
1845 /*
1846 * Switch mouse to poll (remote) mode so motion data will not
1847 * get in our way
1848 */
1849 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1850}
1851
Yunkang Tang95f75e92013-12-01 22:33:52 -08001852static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1853{
1854 int i, nibble;
1855
1856 /*
1857 * b0-b11 are valid bits, send sequence is inverse.
1858 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1859 */
1860 for (i = 0; i <= 8; i += 4) {
1861 nibble = (word >> i) & 0xf;
1862 if (alps_command_mode_send_nibble(psmouse, nibble))
1863 return -1;
1864 }
1865
1866 return 0;
1867}
1868
1869static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1870 u16 addr, u16 value)
1871{
1872 struct ps2dev *ps2dev = &psmouse->ps2dev;
1873
1874 /* 0x0A0 is the command to write the word */
1875 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1876 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1877 alps_monitor_mode_send_word(psmouse, addr) ||
1878 alps_monitor_mode_send_word(psmouse, value) ||
1879 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1880 return -1;
1881
1882 return 0;
1883}
1884
1885static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1886{
1887 struct ps2dev *ps2dev = &psmouse->ps2dev;
1888
1889 if (enable) {
1890 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1891 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1892 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1893 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1894 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1895 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1896 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1897 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1898 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1899 return -1;
1900 } else {
1901 /* EC to exit monitor mode */
1902 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1903 return -1;
1904 }
1905
1906 return 0;
1907}
1908
1909static int alps_absolute_mode_v6(struct psmouse *psmouse)
1910{
1911 u16 reg_val = 0x181;
1912 int ret = -1;
1913
1914 /* enter monitor mode, to write the register */
1915 if (alps_monitor_mode(psmouse, true))
1916 return -1;
1917
1918 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1919
1920 if (alps_monitor_mode(psmouse, false))
1921 ret = -1;
1922
1923 return ret;
1924}
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926static int alps_get_status(struct psmouse *psmouse, char *param)
1927{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001929 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return -1;
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
1933}
1934
1935/*
1936 * Turn touchpad tapping on or off. The sequences are:
1937 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1938 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1939 * My guess that 0xE9 (GetInfo) is here as a sync point.
1940 * For models that also have stickpointer (DualPoints) its tapping
1941 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1942 * we don't fiddle with it.
1943 */
1944static int alps_tap_mode(struct psmouse *psmouse, int enable)
1945{
1946 struct ps2dev *ps2dev = &psmouse->ps2dev;
1947 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1948 unsigned char tap_arg = enable ? 0x0A : 0x00;
1949 unsigned char param[4];
1950
1951 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1952 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1953 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1954 ps2_command(ps2dev, &tap_arg, cmd))
1955 return -1;
1956
1957 if (alps_get_status(psmouse, param))
1958 return -1;
1959
1960 return 0;
1961}
1962
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001963/*
1964 * alps_poll() - poll the touchpad for current motion packet.
1965 * Used in resync.
1966 */
1967static int alps_poll(struct psmouse *psmouse)
1968{
1969 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001970 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001971 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001972
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001973 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001974 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001975
1976 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1977 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1978
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001979 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001980 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001981
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001982 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001983 return -1;
1984
1985 if ((psmouse->badbyte & 0xc8) == 0x08) {
1986/*
1987 * Poll the track stick ...
1988 */
1989 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1990 return -1;
1991 }
1992
1993 memcpy(psmouse->packet, buf, sizeof(buf));
1994 return 0;
1995}
1996
Seth Forshee25bded72011-11-07 19:53:36 -08001997static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002001 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002002 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002006 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002007 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05002009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Seth Forshee25bded72011-11-07 19:53:36 -08002011 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002012 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return -1;
2014 }
2015
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002016 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08002017 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002021 /* ALPS needs stream mode, otherwise it won't report any data */
2022 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002023 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002024 return -1;
2025 }
2026
2027 return 0;
2028}
2029
Yunkang Tang95f75e92013-12-01 22:33:52 -08002030static int alps_hw_init_v6(struct psmouse *psmouse)
2031{
2032 unsigned char param[2] = {0xC8, 0x14};
2033
2034 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2035 if (alps_passthrough_mode_v2(psmouse, true))
2036 return -1;
2037
2038 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2039 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2040 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2041 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2042 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2043 return -1;
2044
2045 if (alps_passthrough_mode_v2(psmouse, false))
2046 return -1;
2047
2048 if (alps_absolute_mode_v6(psmouse)) {
2049 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2050 return -1;
2051 }
2052
2053 return 0;
2054}
2055
Seth Forshee25bded72011-11-07 19:53:36 -08002056/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08002057 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08002058 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08002059static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2060 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08002061{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002062 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08002063
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002064 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002065 return -1;
2066
Kevin Cernekeecd401202013-02-13 22:28:07 -08002067 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2068 if (reg_val == -1)
2069 goto error;
2070
Seth Forshee25bded72011-11-07 19:53:36 -08002071 if (enable)
2072 reg_val |= 0x01;
2073 else
2074 reg_val &= ~0x01;
2075
Kevin Cernekeecd401202013-02-13 22:28:07 -08002076 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08002077
Kevin Cernekeecd401202013-02-13 22:28:07 -08002078error:
2079 if (alps_exit_command_mode(psmouse))
2080 ret = -1;
2081 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08002082}
2083
2084/* Must be in command mode when calling this function */
2085static int alps_absolute_mode_v3(struct psmouse *psmouse)
2086{
2087 int reg_val;
2088
2089 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2090 if (reg_val == -1)
2091 return -1;
2092
2093 reg_val |= 0x06;
2094 if (__alps_command_mode_write_reg(psmouse, reg_val))
2095 return -1;
2096
2097 return 0;
2098}
2099
Pali Rohárdae928e2016-01-06 15:50:08 -08002100static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
Kevin Cernekeecd401202013-02-13 22:28:07 -08002101{
2102 int ret = -EIO, reg_val;
2103
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002104 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002105 goto error;
2106
2107 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2108 if (reg_val == -1)
2109 goto error;
2110
2111 /* bit 7: trackstick is present */
2112 ret = reg_val & 0x80 ? 0 : -ENODEV;
2113
2114error:
2115 alps_exit_command_mode(psmouse);
2116 return ret;
2117}
2118
2119static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2120{
2121 struct ps2dev *ps2dev = &psmouse->ps2dev;
2122 int ret = 0;
2123 unsigned char param[4];
2124
2125 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2126 return -EIO;
2127
2128 /*
2129 * E7 report for the trackstick
2130 *
2131 * There have been reports of failures to seem to trace back
2132 * to the above trackstick check failing. When these occur
2133 * this E7 report fails, so when that happens we continue
2134 * with the assumption that there isn't a trackstick after
2135 * all.
2136 */
2137 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002138 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002139 ret = -ENODEV;
2140 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002141 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002142
2143 /*
2144 * Not sure what this does, but it is absolutely
2145 * essential. Without it, the touchpad does not
2146 * work at all and the trackstick just emits normal
2147 * PS/2 packets.
2148 */
2149 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2150 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2151 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2152 alps_command_mode_send_nibble(psmouse, 0x9) ||
2153 alps_command_mode_send_nibble(psmouse, 0x4)) {
2154 psmouse_err(psmouse,
2155 "Error sending magic E6 sequence\n");
2156 ret = -EIO;
2157 goto error;
2158 }
2159
2160 /*
2161 * This ensures the trackstick packets are in the format
2162 * supported by this driver. If bit 1 isn't set the packet
2163 * format is different.
2164 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002165 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002166 alps_command_mode_write_reg(psmouse,
2167 reg_base + 0x08, 0x82) ||
2168 alps_exit_command_mode(psmouse))
2169 ret = -EIO;
2170 }
2171
2172error:
2173 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2174 ret = -EIO;
2175
2176 return ret;
2177}
2178
Seth Forshee25bded72011-11-07 19:53:36 -08002179static int alps_hw_init_v3(struct psmouse *psmouse)
2180{
Pali Rohár4b1af852016-01-06 15:58:56 -08002181 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -08002182 struct ps2dev *ps2dev = &psmouse->ps2dev;
2183 int reg_val;
2184 unsigned char param[4];
2185
Pali Rohár4b1af852016-01-06 15:58:56 -08002186 if ((priv->flags & ALPS_DUALPOINT) &&
Kevin Cernekeecd401202013-02-13 22:28:07 -08002187 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002188 goto error;
2189
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002190 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002191 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002192 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2193 goto error;
2194 }
2195
2196 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2197 if (reg_val == -1)
2198 goto error;
2199 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2200 goto error;
2201
2202 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2203 if (reg_val == -1)
2204 goto error;
2205 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2206 goto error;
2207
2208 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2209 goto error;
2210 if (__alps_command_mode_write_reg(psmouse, 0x04))
2211 goto error;
2212
2213 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2214 goto error;
2215 if (__alps_command_mode_write_reg(psmouse, 0x03))
2216 goto error;
2217
2218 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2219 goto error;
2220 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2221 goto error;
2222
2223 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2224 goto error;
2225 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2226 goto error;
2227
Seth Forshee25bded72011-11-07 19:53:36 -08002228 alps_exit_command_mode(psmouse);
2229
2230 /* Set rate and enable data reporting */
2231 param[0] = 0x64;
2232 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2233 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2234 psmouse_err(psmouse, "Failed to enable data reporting\n");
2235 return -1;
2236 }
2237
2238 return 0;
2239
Seth Forshee25bded72011-11-07 19:53:36 -08002240error:
2241 /*
2242 * Leaving the touchpad in command mode will essentially render
2243 * it unusable until the machine reboots, so exit it here just
2244 * to be safe
2245 */
2246 alps_exit_command_mode(psmouse);
2247 return -1;
2248}
2249
Hans de Goedef3f33c62014-07-29 11:22:07 -07002250static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2251{
2252 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2253 struct alps_data *priv = psmouse->private;
2254
2255 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2256 if (reg < 0)
2257 return reg;
2258
2259 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2260 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2261
2262 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2263 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2264
2265 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2266 if (reg < 0)
2267 return reg;
2268
2269 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2270 x_electrode = 17 + x_electrode;
2271
2272 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2273 y_electrode = 13 + y_electrode;
2274
2275 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2276 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2277
2278 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2279 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2280
2281 psmouse_dbg(psmouse,
2282 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2283 x_pitch, y_pitch, x_electrode, y_electrode,
2284 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2285
2286 return 0;
2287}
2288
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002289static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2290{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002291 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002292 struct ps2dev *ps2dev = &psmouse->ps2dev;
2293 int reg_val, ret = -1;
2294
Kevin Cernekeecd401202013-02-13 22:28:07 -08002295 if (priv->flags & ALPS_DUALPOINT) {
2296 reg_val = alps_setup_trackstick_v3(psmouse,
2297 ALPS_REG_BASE_RUSHMORE);
2298 if (reg_val == -EIO)
2299 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002300 }
2301
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002302 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002303 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2304 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2305 goto error;
2306
Hans de Goedef3f33c62014-07-29 11:22:07 -07002307 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2308 goto error;
2309
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002310 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2311 if (reg_val == -1)
2312 goto error;
2313 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2314 goto error;
2315
2316 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2317 goto error;
2318
2319 /* enter absolute mode */
2320 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2321 if (reg_val == -1)
2322 goto error;
2323 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2324 goto error;
2325
2326 alps_exit_command_mode(psmouse);
2327 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2328
2329error:
2330 alps_exit_command_mode(psmouse);
2331 return ret;
2332}
2333
Seth Forshee25bded72011-11-07 19:53:36 -08002334/* Must be in command mode when calling this function */
2335static int alps_absolute_mode_v4(struct psmouse *psmouse)
2336{
2337 int reg_val;
2338
2339 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2340 if (reg_val == -1)
2341 return -1;
2342
2343 reg_val |= 0x02;
2344 if (__alps_command_mode_write_reg(psmouse, reg_val))
2345 return -1;
2346
2347 return 0;
2348}
2349
2350static int alps_hw_init_v4(struct psmouse *psmouse)
2351{
Seth Forshee25bded72011-11-07 19:53:36 -08002352 struct ps2dev *ps2dev = &psmouse->ps2dev;
2353 unsigned char param[4];
2354
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002355 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002356 goto error;
2357
2358 if (alps_absolute_mode_v4(psmouse)) {
2359 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2360 goto error;
2361 }
2362
2363 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2364 goto error;
2365
2366 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2367 goto error;
2368
2369 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2370 goto error;
2371
2372 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2373 goto error;
2374
2375 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2376 goto error;
2377
2378 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2379 goto error;
2380
2381 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2382 goto error;
2383
2384 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2385 goto error;
2386
2387 alps_exit_command_mode(psmouse);
2388
2389 /*
2390 * This sequence changes the output from a 9-byte to an
2391 * 8-byte format. All the same data seems to be present,
2392 * just in a more compact format.
2393 */
2394 param[0] = 0xc8;
2395 param[1] = 0x64;
2396 param[2] = 0x50;
2397 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2398 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2399 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2400 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2401 return -1;
2402
2403 /* Set rate and enable data reporting */
2404 param[0] = 0x64;
2405 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2406 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2407 psmouse_err(psmouse, "Failed to enable data reporting\n");
2408 return -1;
2409 }
2410
2411 return 0;
2412
2413error:
2414 /*
2415 * Leaving the touchpad in command mode will essentially render
2416 * it unusable until the machine reboots, so exit it here just
2417 * to be safe
2418 */
2419 alps_exit_command_mode(psmouse);
2420 return -1;
2421}
2422
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002423static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2424 unsigned char index, unsigned char otp[])
2425{
2426 struct ps2dev *ps2dev = &psmouse->ps2dev;
2427
2428 switch (index) {
2429 case 0:
2430 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2431 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2432 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2433 return -1;
2434
2435 break;
2436
2437 case 1:
2438 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2439 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2440 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2441 return -1;
2442
2443 break;
2444 }
2445
2446 return 0;
2447}
2448
Fengguang Wu07f19e32015-04-10 23:54:31 -07002449static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2450 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002451{
2452 int num_x_electrode;
2453 int num_y_electrode;
2454 int x_pitch, y_pitch, x_phys, y_phys;
2455
2456 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2457 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2458
2459 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2460 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2461
2462 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2463 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2464
2465 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2466 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2467
2468 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2469 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2470
2471 return 0;
2472}
2473
Fengguang Wu07f19e32015-04-10 23:54:31 -07002474static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2475 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002476{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002477 unsigned char is_btnless;
2478
2479 is_btnless = (otp[1][1] >> 3) & 0x01;
2480
2481 if (is_btnless)
2482 priv->flags |= ALPS_BUTTONPAD;
2483
2484 return 0;
2485}
2486
2487static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2488 struct alps_data *priv)
2489{
2490 unsigned char otp[2][4];
2491
2492 memset(otp, 0, sizeof(otp));
2493
2494 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2495 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2496 return -1;
2497
2498 alps_update_device_area_ss4_v2(otp, priv);
2499
2500 alps_update_btn_info_ss4_v2(otp, priv);
2501
2502 return 0;
2503}
2504
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002505static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2506 struct alps_data *priv)
2507{
2508 struct ps2dev *ps2dev = &psmouse->ps2dev;
2509 unsigned char param[4] = {0};
2510 int num_x_electrode, num_y_electrode;
2511
2512 if (alps_enter_command_mode(psmouse))
2513 return -1;
2514
2515 param[0] = 0x0a;
2516 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2517 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2518 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2519 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2520 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2521 return -1;
2522
2523 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2524 return -1;
2525
2526 /*
2527 * Dolphin's sensor line number is not fixed. It can be calculated
2528 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2529 * Further more, we can get device's x_max and y_max by multiplying
2530 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2531 *
2532 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2533 * real sensor line number X = 11 + 8 = 19, and
2534 * real sensor line number Y = 8 + 1 = 9.
2535 * So, x_max = (19 - 1) * 64 = 1152, and
2536 * y_max = (9 - 1) * 64 = 512.
2537 */
2538 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2539 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2540 priv->x_bits = num_x_electrode;
2541 priv->y_bits = num_y_electrode;
2542 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2543 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2544
2545 if (alps_exit_command_mode(psmouse))
2546 return -1;
2547
2548 return 0;
2549}
2550
Dave Turvene75af9e52013-02-21 22:58:28 -08002551static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2552{
2553 struct ps2dev *ps2dev = &psmouse->ps2dev;
2554 unsigned char param[2];
2555
2556 /* This is dolphin "v1" as empirically defined by florin9doi */
2557 param[0] = 0x64;
2558 param[1] = 0x28;
2559
2560 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2561 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2562 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2563 return -1;
2564
2565 return 0;
2566}
2567
Yunkang Tang38088432014-07-26 13:51:41 -07002568static int alps_hw_init_v7(struct psmouse *psmouse)
2569{
2570 struct ps2dev *ps2dev = &psmouse->ps2dev;
2571 int reg_val, ret = -1;
2572
2573 if (alps_enter_command_mode(psmouse) ||
2574 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2575 goto error;
2576
Hans de Goedef3f33c62014-07-29 11:22:07 -07002577 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2578 goto error;
2579
Yunkang Tang38088432014-07-26 13:51:41 -07002580 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2581 goto error;
2582
2583 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2584 if (reg_val == -1)
2585 goto error;
2586 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2587 goto error;
2588
2589 alps_exit_command_mode(psmouse);
2590 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2591
2592error:
2593 alps_exit_command_mode(psmouse);
2594 return ret;
2595}
2596
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002597static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2598{
2599 struct ps2dev *ps2dev = &psmouse->ps2dev;
2600 char param[2] = {0x64, 0x28};
2601 int ret = -1;
2602
2603 /* enter absolute mode */
2604 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2605 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2606 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2607 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2608 goto error;
2609 }
2610
2611 /* T.B.D. Decread noise packet number, delete in the future */
2612 if (alps_exit_command_mode(psmouse) ||
2613 alps_enter_command_mode(psmouse) ||
2614 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2615 goto error;
2616 }
2617 alps_exit_command_mode(psmouse);
2618
2619 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2620
2621error:
2622 alps_exit_command_mode(psmouse);
2623 return ret;
2624}
2625
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002626static int alps_set_protocol(struct psmouse *psmouse,
2627 struct alps_data *priv,
2628 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002629{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002630 psmouse->private = priv;
2631
2632 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2633
2634 priv->proto_version = protocol->version;
2635 priv->byte0 = protocol->byte0;
2636 priv->mask0 = protocol->mask0;
2637 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002638
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002639 priv->x_max = 2000;
2640 priv->y_max = 1400;
2641 priv->x_bits = 15;
2642 priv->y_bits = 11;
2643
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002644 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002645 case ALPS_PROTO_V1:
2646 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002647 priv->hw_init = alps_hw_init_v1_v2;
2648 priv->process_packet = alps_process_packet_v1_v2;
2649 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002650 priv->x_max = 1023;
2651 priv->y_max = 767;
Hans de Goede19556212015-10-26 01:50:28 -07002652 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2653 priv->flags |= ALPS_STICK_BITS;
Seth Forshee25bded72011-11-07 19:53:36 -08002654 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002655
Seth Forshee25bded72011-11-07 19:53:36 -08002656 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002657 priv->hw_init = alps_hw_init_v3;
2658 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002659 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002660 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002661 priv->nibble_commands = alps_v3_nibble_commands;
2662 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Pali Rohár4b1af852016-01-06 15:58:56 -08002663
2664 if (alps_probe_trackstick_v3_v7(psmouse,
2665 ALPS_REG_BASE_PINNACLE) < 0)
2666 priv->flags &= ~ALPS_DUALPOINT;
2667
Seth Forshee25bded72011-11-07 19:53:36 -08002668 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002669
2670 case ALPS_PROTO_V3_RUSHMORE:
2671 priv->hw_init = alps_hw_init_rushmore_v3;
2672 priv->process_packet = alps_process_packet_v3;
Hans de Goede688ea362015-05-20 14:42:50 -07002673 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002674 priv->decode_fields = alps_decode_rushmore;
2675 priv->nibble_commands = alps_v3_nibble_commands;
2676 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2677 priv->x_bits = 16;
2678 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002679
Pali Rohárdae928e2016-01-06 15:50:08 -08002680 if (alps_probe_trackstick_v3_v7(psmouse,
2681 ALPS_REG_BASE_RUSHMORE) < 0)
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002682 priv->flags &= ~ALPS_DUALPOINT;
2683
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002684 break;
2685
Seth Forshee25bded72011-11-07 19:53:36 -08002686 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002687 priv->hw_init = alps_hw_init_v4;
2688 priv->process_packet = alps_process_packet_v4;
Hans de Goede688ea362015-05-20 14:42:50 -07002689 priv->set_abs_params = alps_set_abs_params_semi_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002690 priv->nibble_commands = alps_v4_nibble_commands;
2691 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002692 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002693
Dave Turvene75af9e52013-02-21 22:58:28 -08002694 case ALPS_PROTO_V5:
2695 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002696 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002697 priv->decode_fields = alps_decode_dolphin;
Hans de Goede688ea362015-05-20 14:42:50 -07002698 priv->set_abs_params = alps_set_abs_params_semi_mt;
Dave Turvene75af9e52013-02-21 22:58:28 -08002699 priv->nibble_commands = alps_v3_nibble_commands;
2700 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002701 priv->x_bits = 23;
2702 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002703
2704 if (alps_dolphin_get_device_area(psmouse, priv))
2705 return -EIO;
2706
Dave Turvene75af9e52013-02-21 22:58:28 -08002707 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002708
Yunkang Tang95f75e92013-12-01 22:33:52 -08002709 case ALPS_PROTO_V6:
2710 priv->hw_init = alps_hw_init_v6;
2711 priv->process_packet = alps_process_packet_v6;
2712 priv->set_abs_params = alps_set_abs_params_st;
2713 priv->nibble_commands = alps_v6_nibble_commands;
2714 priv->x_max = 2047;
2715 priv->y_max = 1535;
2716 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002717
Yunkang Tang38088432014-07-26 13:51:41 -07002718 case ALPS_PROTO_V7:
2719 priv->hw_init = alps_hw_init_v7;
2720 priv->process_packet = alps_process_packet_v7;
2721 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002722 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002723 priv->nibble_commands = alps_v3_nibble_commands;
2724 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002725 priv->x_max = 0xfff;
2726 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002727
2728 if (priv->fw_ver[1] != 0xba)
2729 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002730
Pali Rohárdae928e2016-01-06 15:50:08 -08002731 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2732 priv->flags &= ~ALPS_DUALPOINT;
2733
Yunkang Tang38088432014-07-26 13:51:41 -07002734 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002735
2736 case ALPS_PROTO_V8:
2737 priv->hw_init = alps_hw_init_ss4_v2;
2738 priv->process_packet = alps_process_packet_ss4_v2;
2739 priv->decode_fields = alps_decode_ss4_v2;
2740 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2741 priv->nibble_commands = alps_v3_nibble_commands;
2742 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2743
2744 if (alps_set_defaults_ss4_v2(psmouse, priv))
2745 return -EIO;
2746
2747 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002748 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002749
2750 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002751}
2752
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002753static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2754 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002755{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002756 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002757 int i;
2758
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002759 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2760 model = &alps_model_data[i];
2761
2762 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2763 (!model->command_mode_resp ||
2764 model->command_mode_resp == ec[2])) {
2765
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002766 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002767 }
2768 }
2769
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002770 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002771}
2772
2773static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2774{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002775 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002776 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002777 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002778
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002779 /*
2780 * First try "E6 report".
2781 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2782 * The bits 0-2 of the first byte will be 1s if some buttons are
2783 * pressed.
2784 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002785 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2786 PSMOUSE_CMD_SETSCALE11, e6))
2787 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002788
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002789 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2790 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002791
2792 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002793 * Now get the "E7" and "EC" reports. These will uniquely identify
2794 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002795 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002796 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2797 PSMOUSE_CMD_SETSCALE21, e7) ||
2798 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2799 PSMOUSE_CMD_RESET_WRAP, ec) ||
2800 alps_exit_command_mode(psmouse))
2801 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002802
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002803 protocol = alps_match_table(e7, ec);
2804 if (!protocol) {
2805 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2806 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2807 protocol = &alps_v5_protocol_data;
2808 } else if (ec[0] == 0x88 &&
2809 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2810 protocol = &alps_v7_protocol_data;
2811 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2812 protocol = &alps_v3_rushmore_data;
2813 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2814 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2815 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002816 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2817 e7[2] == 0x14 && ec[1] == 0x02) {
2818 protocol = &alps_v8_protocol_data;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002819 } else {
2820 psmouse_dbg(psmouse,
2821 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2822 return -EINVAL;
2823 }
2824 }
2825
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002826 if (priv) {
2827 /* Save the Firmware version */
2828 memcpy(priv->fw_ver, ec, 3);
2829 error = alps_set_protocol(psmouse, priv, protocol);
2830 if (error)
2831 return error;
2832 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002833
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002834 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002835}
2836
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002837static int alps_reconnect(struct psmouse *psmouse)
2838{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002839 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002840
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002841 psmouse_reset(psmouse);
2842
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002843 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002844 return -1;
2845
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002846 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
2849static void alps_disconnect(struct psmouse *psmouse)
2850{
2851 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002854 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002855 if (priv->dev2)
2856 input_unregister_device(priv->dev2);
2857 if (!IS_ERR_OR_NULL(priv->dev3))
2858 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 kfree(priv);
2860}
2861
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002862static void alps_set_abs_params_st(struct alps_data *priv,
2863 struct input_dev *dev1)
2864{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002865 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2866 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002867 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002868}
2869
Masaki Ota8eccd392015-03-27 20:44:21 -07002870static void alps_set_abs_params_mt_common(struct alps_data *priv,
2871 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002872{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002873 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2874 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002875
Hans de Goedef3f33c62014-07-29 11:22:07 -07002876 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2877 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2878
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002879 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2880 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002881}
Yunkang Tang38088432014-07-26 13:51:41 -07002882
Hans de Goede688ea362015-05-20 14:42:50 -07002883static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2884 struct input_dev *dev1)
Masaki Ota8eccd392015-03-27 20:44:21 -07002885{
2886 alps_set_abs_params_mt_common(priv, dev1);
2887 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2888
2889 input_mt_init_slots(dev1, MAX_TOUCHES,
2890 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
Hans de Goede1662c0332015-05-20 14:42:15 -07002891 INPUT_MT_SEMI_MT);
Masaki Ota8eccd392015-03-27 20:44:21 -07002892}
2893
2894static void alps_set_abs_params_v7(struct alps_data *priv,
2895 struct input_dev *dev1)
2896{
2897 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002898 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002899
2900 input_mt_init_slots(dev1, MAX_TOUCHES,
2901 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2902 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002903
2904 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2905}
2906
2907static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2908 struct input_dev *dev1)
2909{
2910 alps_set_abs_params_mt_common(priv, dev1);
2911 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2912 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2913
2914 input_mt_init_slots(dev1, MAX_TOUCHES,
2915 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2916 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002917}
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919int alps_init(struct psmouse *psmouse)
2920{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002921 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002922 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002923 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002925 error = priv->hw_init(psmouse);
2926 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 goto init_fail;
2928
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002929 /*
2930 * Undo part of setup done for us by psmouse core since touchpad
2931 * is not a relative device.
2932 */
2933 __clear_bit(EV_REL, dev1->evbit);
2934 __clear_bit(REL_X, dev1->relbit);
2935 __clear_bit(REL_Y, dev1->relbit);
2936
2937 /*
2938 * Now set up our capabilities.
2939 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002940 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2941 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2942 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002943 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2944 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002946 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002947
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002948 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002950 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002951 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2952 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
2954
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002955 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002956 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2957 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
2959
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002960 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002961 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2962 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2963 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2964 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002965 } else if (priv->flags & ALPS_BUTTONPAD) {
2966 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2967 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002968 } else {
2969 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2970 }
2971
Pali Rohárdfba8602015-01-09 12:48:44 -08002972 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002973 struct input_dev *dev2;
2974
2975 dev2 = input_allocate_device();
2976 if (!dev2) {
2977 psmouse_err(psmouse,
2978 "failed to allocate trackstick device\n");
2979 error = -ENOMEM;
2980 goto init_fail;
2981 }
2982
2983 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2984 psmouse->ps2dev.serio->phys);
2985 dev2->phys = priv->phys2;
2986
Pali Rohárdfba8602015-01-09 12:48:44 -08002987 /*
2988 * format of input device name is: "protocol vendor name"
2989 * see function psmouse_switch_protocol() in psmouse-base.c
2990 */
2991 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002992
2993 dev2->id.bustype = BUS_I8042;
2994 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002995 dev2->id.product = PSMOUSE_ALPS;
2996 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002997 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002998
Pali Rohár04aae282015-01-14 13:18:30 -08002999 input_set_capability(dev2, EV_REL, REL_X);
3000 input_set_capability(dev2, EV_REL, REL_Y);
3001 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3002 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3003 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003004
Pali Rohár04aae282015-01-14 13:18:30 -08003005 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07003006 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3007
Pali Rohár04aae282015-01-14 13:18:30 -08003008 error = input_register_device(dev2);
3009 if (error) {
3010 psmouse_err(psmouse,
3011 "failed to register trackstick device: %d\n",
3012 error);
3013 input_free_device(dev2);
3014 goto init_fail;
3015 }
3016
3017 priv->dev2 = dev2;
3018 }
3019
3020 priv->psmouse = psmouse;
3021
3022 INIT_DELAYED_WORK(&priv->dev3_register_work,
3023 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003026 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 psmouse->disconnect = alps_disconnect;
3028 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08003029 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05003031 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3032 psmouse->resync_time = 0;
3033
Pali Rohár9d720b32014-11-08 12:58:57 -08003034 /* Allow 2 invalid packets without resetting device */
3035 psmouse->resetafter = psmouse->pktsize * 2;
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return 0;
3038
3039init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04003040 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003041 /*
3042 * Even though we did not allocate psmouse->private we do free
3043 * it here.
3044 */
3045 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04003046 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003047 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07003050int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003052 struct alps_data *priv;
3053 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003055 error = alps_identify(psmouse, NULL);
3056 if (error)
3057 return error;
3058
3059 /*
3060 * Reset the device to make sure it is fully operational:
3061 * on some laptops, like certain Dell Latitudes, we may
3062 * fail to properly detect presence of trackstick if device
3063 * has not been reset.
3064 */
3065 psmouse_reset(psmouse);
3066
3067 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3068 if (!priv)
3069 return -ENOMEM;
3070
3071 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003072 if (error) {
3073 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003074 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 if (set_properties) {
3078 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003079 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003080 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003081 psmouse->model = priv->proto_version;
3082 } else {
3083 /*
3084 * Destroy alps_data structure we allocated earlier since
3085 * this was just a "trial run". Otherwise we'll keep it
3086 * to be used by alps_init() which has to be called if
3087 * we succeed and set_properties is true.
3088 */
3089 kfree(priv);
3090 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return 0;
3094}
3095