blob: f066761e898523636d53224cb3eeaae1948d1681 [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>
23
24#include "psmouse.h"
25#include "alps.h"
26
Seth Forshee25bded72011-11-07 19:53:36 -080027/*
28 * Definitions for ALPS version 3 and 4 command mode protocol
29 */
30#define ALPS_CMD_NIBBLE_10 0x01f2
31
Kevin Cernekeecd401202013-02-13 22:28:07 -080032#define ALPS_REG_BASE_RUSHMORE 0xc2c0
33#define ALPS_REG_BASE_PINNACLE 0x0000
34
Seth Forshee25bded72011-11-07 19:53:36 -080035static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
37 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
38 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
39 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
40 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
41 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
42 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
43 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
44 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
45 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
46 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
47 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
48 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
49 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
50 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
51 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
52};
53
54static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
56 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
57 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
58 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
59 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
60 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
61 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
62 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
63 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
64 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
65 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
66 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
67 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
68 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
69 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
70 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
71};
72
Yunkang Tang95f75e92013-12-01 22:33:52 -080073static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
75 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
76 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
77 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
78 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
79 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
80 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
81 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
82 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
83 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
84 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
85 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
86 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
87 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
88 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
89 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
90};
91
Seth Forshee25bded72011-11-07 19:53:36 -080092
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080093#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
94#define ALPS_PASS 0x04 /* device has a pass-through port */
95
96#define ALPS_WHEEL 0x08 /* hardware wheel present */
97#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
98#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
99#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800100#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
Yunkang Tang38088432014-07-26 13:51:41 -0700102#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Helge Dellere38de672006-09-10 21:54:39 -0400104static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800105 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
106 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
107 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
108 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
109 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
110 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
113 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
114 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
115 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
117 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
118 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800120
121 /*
122 * XXX This entry is suspicious. First byte has zero lower nibble,
123 * which is what a normal mouse would report. Also, the value 0x0e
124 * isn't valid per PS/2 spec.
125 */
126 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
127
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800128 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
129 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800130 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800131 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
132 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
133 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
134 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
135 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
137 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800140static const struct alps_protocol_info alps_v3_protocol_data = {
141 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
142};
143
144static const struct alps_protocol_info alps_v3_rushmore_data = {
145 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
146};
147
148static const struct alps_protocol_info alps_v5_protocol_data = {
149 ALPS_PROTO_V5, 0xc8, 0xd8, 0
150};
151
152static const struct alps_protocol_info alps_v7_protocol_data = {
153 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
154};
155
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700156static const struct alps_protocol_info alps_v8_protocol_data = {
157 ALPS_PROTO_V8, 0x18, 0x18, 0
158};
159
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800160static void alps_set_abs_params_st(struct alps_data *priv,
161 struct input_dev *dev1);
162static void alps_set_abs_params_mt(struct alps_data *priv,
163 struct input_dev *dev1);
Masaki Ota8eccd392015-03-27 20:44:21 -0700164static void alps_set_abs_params_v7(struct alps_data *priv,
165 struct input_dev *dev1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700166static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
167 struct input_dev *dev1);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800168
Seth Forsheed4b347b2011-11-07 19:53:15 -0800169/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800171static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800172 unsigned char data)
173{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800174 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800175}
176
Pali Rohár04aae282015-01-14 13:18:30 -0800177static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800178 int left, int right, int middle)
179{
Martin Buckc91ed052010-03-13 22:23:58 -0800180 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800181
Martin Buckc91ed052010-03-13 22:23:58 -0800182 /*
183 * If shared button has already been reported on the
184 * other device (dev2) then this event should be also
185 * sent through that device.
186 */
Pali Rohár04aae282015-01-14 13:18:30 -0800187 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800188 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800189
Pali Rohár04aae282015-01-14 13:18:30 -0800190 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800191 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800192
Pali Rohár04aae282015-01-14 13:18:30 -0800193 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800194 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800195
Martin Buckc91ed052010-03-13 22:23:58 -0800196 /*
197 * Sync the _other_ device now, we'll do the first
198 * device later once we report the rest of the events.
199 */
Pali Rohár04aae282015-01-14 13:18:30 -0800200 if (dev2)
201 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800202}
203
Seth Forshee25bded72011-11-07 19:53:36 -0800204static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct alps_data *priv = psmouse->private;
207 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500208 struct input_dev *dev = psmouse->dev;
209 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500211 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800213 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400214 left = packet[2] & 0x10;
215 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 middle = 0;
217 x = packet[1] | ((packet[0] & 0x07) << 7);
218 y = packet[4] | ((packet[3] & 0x07) << 7);
219 z = packet[5];
220 } else {
221 left = packet[3] & 1;
222 right = packet[3] & 2;
223 middle = packet[3] & 4;
224 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
225 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
226 z = packet[5];
227 }
228
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800229 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400230 back = packet[0] & 0x10;
231 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500232 }
233
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800234 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500235 back = packet[3] & 4;
236 forward = packet[2] & 4;
237 if ((middle = forward && back))
238 forward = back = 0;
239 }
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 ges = packet[2] & 1;
242 fin = packet[2] & 2;
243
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800244 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
246 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700247
Pali Rohár04aae282015-01-14 13:18:30 -0800248 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 input_sync(dev2);
251 return;
252 }
253
Pali Rohár04aae282015-01-14 13:18:30 -0800254 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800257 if (ges && !fin)
258 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * A "tap and drag" operation is reported by the hardware as a transition
262 * from (!fin && ges) to (fin && ges). This should be translated to the
263 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
264 */
265 if (ges && fin && !priv->prev_fin) {
266 input_report_abs(dev, ABS_X, x);
267 input_report_abs(dev, ABS_Y, y);
268 input_report_abs(dev, ABS_PRESSURE, 0);
269 input_report_key(dev, BTN_TOOL_FINGER, 0);
270 input_sync(dev);
271 }
272 priv->prev_fin = fin;
273
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800274 if (z > 30)
275 input_report_key(dev, BTN_TOUCH, 1);
276 if (z < 25)
277 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (z > 0) {
280 input_report_abs(dev, ABS_X, x);
281 input_report_abs(dev, ABS_Y, y);
282 }
283
284 input_report_abs(dev, ABS_PRESSURE, z);
285 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
286
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800287 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500288 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800290 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500291 input_report_key(dev, BTN_FORWARD, forward);
292 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800295 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800296 input_report_key(dev, BTN_0, packet[2] & 4);
297 input_report_key(dev, BTN_1, packet[0] & 0x10);
298 input_report_key(dev, BTN_2, packet[3] & 4);
299 input_report_key(dev, BTN_3, packet[0] & 0x20);
300 }
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 input_sync(dev);
303}
304
Seth Forshee01ce6612011-11-07 19:54:13 -0800305/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800306 * Process bitmap data for V5 protocols. Return value is null.
307 *
308 * The bitmaps don't have enough data to track fingers, so this function
309 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700310 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800311 */
312static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700313 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800314{
315 int box_middle_x, box_middle_y;
316 unsigned int x_map, y_map;
317 unsigned char start_bit, end_bit;
318 unsigned char x_msb, x_lsb, y_msb, y_lsb;
319
320 x_map = fields->x_map;
321 y_map = fields->y_map;
322
323 if (!x_map || !y_map)
324 return;
325
326 /* Get Most-significant and Least-significant bit */
327 x_msb = fls(x_map);
328 x_lsb = ffs(x_map);
329 y_msb = fls(y_map);
330 y_lsb = ffs(y_map);
331
332 /* Most-significant bit should never exceed max sensor line number */
333 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
334 return;
335
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800336 if (fields->fingers > 1) {
337 start_bit = priv->x_bits - x_msb;
338 end_bit = priv->x_bits - x_lsb;
339 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
340 (2 * (priv->x_bits - 1));
341
342 start_bit = y_lsb - 1;
343 end_bit = y_msb - 1;
344 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
345 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700346 fields->mt[0] = fields->st;
347 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
348 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800349 }
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/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800379 * Process bitmap data from v3 and v4 protocols. Returns the number of
380 * 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 =
446 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
447 (2 * (priv->y_bits - 1));
448
449 /* 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 =
Hans de Goede02d04252014-07-25 22:43:35 -0700454 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
455 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800456
Hans de Goede40e8f532014-07-25 22:37:15 -0700457 /* y-bitmap order is reversed, except on rushmore */
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800458 if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede4dd26572015-05-20 14:41:10 -0700459 for (i = 0; i < 4; i++)
460 corner[i].y = priv->y_max - corner[i].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700461 }
462
Hans de Goede4dd26572015-05-20 14:41:10 -0700463 /*
464 * We only select a corner for the second touch once per 2 finger
465 * touch sequence to avoid the chosen corner (and thus the coordinates)
466 * jumping around when the first touch is in the middle.
467 */
468 if (priv->second_touch == -1) {
469 /* Find corner closest to our st coordinates */
470 closest = 0x7fffffff;
471 for (i = 0; i < 4; i++) {
472 int dx = fields->st.x - corner[i].x;
473 int dy = fields->st.y - corner[i].y;
474 int distance = dx * dx + dy * dy;
475
476 if (distance < closest) {
477 priv->second_touch = i;
478 closest = distance;
479 }
480 }
481 /* And select the opposite corner to use for the 2nd touch */
482 priv->second_touch = (priv->second_touch + 2) % 4;
483 }
484
485 fields->mt[0] = fields->st;
486 fields->mt[1] = corner[priv->second_touch];
487
Seth Forshee01ce6612011-11-07 19:54:13 -0800488 return fingers;
489}
490
Hans de Goedecdf333e2014-07-25 22:44:42 -0700491static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800492{
493 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700494 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
495 input_report_abs(dev, ABS_MT_POSITION_X, x);
496 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800497}
498
Hans de Goedecdf333e2014-07-25 22:44:42 -0700499static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800500{
Hans de Goede02d04252014-07-25 22:43:35 -0700501 struct alps_data *priv = psmouse->private;
502 struct input_dev *dev = psmouse->dev;
503 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700504 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700505
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800506 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700507 for (i = 0; i < n; i++)
508 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
509
510 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800511}
512
Hans de Goede68c21872014-07-25 22:47:25 -0700513static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
514{
515 struct alps_data *priv = psmouse->private;
516 struct input_dev *dev = psmouse->dev;
517 struct alps_fields *f = &priv->f;
518
519 /* Use st data when we don't have mt data */
520 if (fingers < 2) {
521 f->mt[0].x = f->st.x;
522 f->mt[0].y = f->st.y;
523 fingers = f->pressure > 0 ? 1 : 0;
Hans de Goede4dd26572015-05-20 14:41:10 -0700524 priv->second_touch = -1;
Hans de Goede68c21872014-07-25 22:47:25 -0700525 }
526
Hans de Goede1662c0332015-05-20 14:42:15 -0700527 if (fingers >= 1)
528 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
529 if (fingers >= 2)
530 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
531 input_mt_sync_frame(dev);
Hans de Goede68c21872014-07-25 22:47:25 -0700532
533 input_mt_report_finger_count(dev, fingers);
534
535 input_report_key(dev, BTN_LEFT, f->left);
536 input_report_key(dev, BTN_RIGHT, f->right);
537 input_report_key(dev, BTN_MIDDLE, f->middle);
538
539 input_report_abs(dev, ABS_PRESSURE, f->pressure);
540
541 input_sync(dev);
542}
543
Seth Forshee25bded72011-11-07 19:53:36 -0800544static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
545{
546 struct alps_data *priv = psmouse->private;
547 unsigned char *packet = psmouse->packet;
548 struct input_dev *dev = priv->dev2;
549 int x, y, z, left, right, middle;
550
Pali Rohár34412ba2015-01-09 12:48:58 -0800551 /* It should be a DualPoint when received trackstick packet */
552 if (!(priv->flags & ALPS_DUALPOINT)) {
553 psmouse_warn(psmouse,
554 "Rejected trackstick packet from non DualPoint device");
555 return;
556 }
557
Seth Forshee25bded72011-11-07 19:53:36 -0800558 /* Sanity check packet */
559 if (!(packet[0] & 0x40)) {
560 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
561 return;
562 }
563
564 /*
565 * There's a special packet that seems to indicate the end
566 * of a stream of trackstick data. Filter these out.
567 */
568 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
569 return;
570
571 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
572 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
573 z = (packet[4] & 0x7c) >> 2;
574
575 /*
576 * The x and y values tend to be quite large, and when used
577 * alone the trackstick is difficult to use. Scale them down
578 * to compensate.
579 */
580 x /= 8;
581 y /= 8;
582
583 input_report_rel(dev, REL_X, x);
584 input_report_rel(dev, REL_Y, -y);
585
586 /*
587 * Most ALPS models report the trackstick buttons in the touchpad
588 * packets, but a few report them here. No reliable way has been
589 * found to differentiate between the models upfront, so we enable
590 * the quirk in response to seeing a button press in the trackstick
591 * packet.
592 */
593 left = packet[3] & 0x01;
594 right = packet[3] & 0x02;
595 middle = packet[3] & 0x04;
596
597 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
598 (left || right || middle))
599 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
600
601 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
602 input_report_key(dev, BTN_LEFT, left);
603 input_report_key(dev, BTN_RIGHT, right);
604 input_report_key(dev, BTN_MIDDLE, middle);
605 }
606
607 input_sync(dev);
608 return;
609}
610
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800611static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
612{
613 f->left = !!(p[3] & 0x01);
614 f->right = !!(p[3] & 0x02);
615 f->middle = !!(p[3] & 0x04);
616
617 f->ts_left = !!(p[3] & 0x10);
618 f->ts_right = !!(p[3] & 0x20);
619 f->ts_middle = !!(p[3] & 0x40);
620}
621
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700622static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800623 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800624{
625 f->first_mp = !!(p[4] & 0x40);
626 f->is_mp = !!(p[0] & 0x40);
627
Hans de Goedea839cd52015-05-20 14:39:21 -0700628 if (f->is_mp) {
629 f->fingers = (p[5] & 0x3) + 1;
630 f->x_map = ((p[4] & 0x7e) << 8) |
631 ((p[1] & 0x7f) << 2) |
632 ((p[0] & 0x30) >> 4);
633 f->y_map = ((p[3] & 0x70) << 4) |
634 ((p[2] & 0x7f) << 1) |
635 (p[4] & 0x01);
636 } else {
637 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
638 ((p[0] & 0x30) >> 4);
639 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
640 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800641
Hans de Goedea839cd52015-05-20 14:39:21 -0700642 alps_decode_buttons_v3(f, p);
643 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700644
645 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800646}
647
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700648static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800649 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800650{
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700651 f->first_mp = !!(p[4] & 0x40);
Yunkang Tangf105e342014-07-25 22:29:24 -0700652 f->is_mp = !!(p[5] & 0x40);
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700653
Hans de Goedea839cd52015-05-20 14:39:21 -0700654 if (f->is_mp) {
655 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
656 f->x_map = ((p[5] & 0x10) << 11) |
657 ((p[4] & 0x7e) << 8) |
658 ((p[1] & 0x7f) << 2) |
659 ((p[0] & 0x30) >> 4);
660 f->y_map = ((p[5] & 0x20) << 6) |
661 ((p[3] & 0x70) << 4) |
662 ((p[2] & 0x7f) << 1) |
663 (p[4] & 0x01);
664 } else {
665 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
666 ((p[0] & 0x30) >> 4);
667 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
668 f->pressure = p[5] & 0x7f;
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700669
Hans de Goedea839cd52015-05-20 14:39:21 -0700670 alps_decode_buttons_v3(f, p);
671 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700672
673 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800674}
675
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700676static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800677 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800678{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800679 u64 palm_data = 0;
680 struct alps_data *priv = psmouse->private;
681
Dave Turvene75af9e52013-02-21 22:58:28 -0800682 f->first_mp = !!(p[0] & 0x02);
683 f->is_mp = !!(p[0] & 0x20);
684
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800685 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700686 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
687 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
688 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800689 alps_decode_buttons_v3(f, p);
690 } else {
691 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800692 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800693
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800694 palm_data = (p[1] & 0x7f) |
695 ((p[2] & 0x7f) << 7) |
696 ((p[4] & 0x7f) << 14) |
697 ((p[5] & 0x7f) << 21) |
698 ((p[3] & 0x07) << 28) |
699 (((u64)p[3] & 0x70) << 27) |
700 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800701
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800702 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
703 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
704
705 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
706 f->x_map = (palm_data >> priv->y_bits) &
707 (BIT(priv->x_bits) - 1);
708 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700709
710 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800711}
712
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800713static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800714{
715 struct alps_data *priv = psmouse->private;
716 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800717 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700718 struct alps_fields *f = &priv->f;
719 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800720
Hans de Goede02d04252014-07-25 22:43:35 -0700721 memset(f, 0, sizeof(*f));
722
723 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800724
725 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800726 * There's no single feature of touchpad position and bitmap packets
727 * that can be used to distinguish between them. We rely on the fact
728 * that a bitmap packet should always follow a position packet with
729 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800730 */
731 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800732 /*
733 * Sometimes a position packet will indicate a multi-packet
734 * sequence, but then what follows is another position
735 * packet. Check for this, and when it happens process the
736 * position packet as usual.
737 */
Hans de Goede02d04252014-07-25 22:43:35 -0700738 if (f->is_mp) {
739 fingers = f->fingers;
Hans de Goede44b77f32015-05-20 14:40:06 -0700740 /*
741 * Bitmap processing uses position packet's coordinate
742 * data, so we need to do decode it first.
743 */
744 priv->decode_fields(f, priv->multi_data, psmouse);
745
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800746 if (priv->proto_version == ALPS_PROTO_V3 ||
747 priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700748 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700749 fingers = 0; /* Use st data */
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800750 } else {
751 /*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800752 * Since Dolphin's finger number is reliable,
753 * there is no need to compare with bmap_fn.
754 */
Hans de Goede02d04252014-07-25 22:43:35 -0700755 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800756 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800757 } else {
758 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800759 }
760 }
761
Seth Forshee01ce6612011-11-07 19:54:13 -0800762 /*
763 * Bit 6 of byte 0 is not usually set in position packets. The only
764 * times it seems to be set is in situations where the data is
765 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
766 * this combined with the fact that this bit is useful for filtering
767 * out misidentified bitmap packets, we reject anything with this
768 * bit set.
769 */
Hans de Goede02d04252014-07-25 22:43:35 -0700770 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800771 return;
772
Hans de Goede02d04252014-07-25 22:43:35 -0700773 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800774 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800775 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
776 return;
777 }
778
779 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800780
Seth Forshee25bded72011-11-07 19:53:36 -0800781 /*
782 * Sometimes the hardware sends a single packet with z = 0
783 * in the middle of a stream. Real releases generate packets
784 * with x, y, and z all zero, so these seem to be flukes.
785 * Ignore them.
786 */
Hans de Goede02d04252014-07-25 22:43:35 -0700787 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800788 return;
789
Hans de Goede68c21872014-07-25 22:47:25 -0700790 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800791
Pali Rohár34412ba2015-01-09 12:48:58 -0800792 if ((priv->flags & ALPS_DUALPOINT) &&
793 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700794 input_report_key(dev2, BTN_LEFT, f->ts_left);
795 input_report_key(dev2, BTN_RIGHT, f->ts_right);
796 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800797 input_sync(dev2);
798 }
799}
800
801static void alps_process_packet_v3(struct psmouse *psmouse)
802{
803 unsigned char *packet = psmouse->packet;
804
805 /*
806 * v3 protocol packets come in three types, two representing
807 * touchpad data and one representing trackstick data.
808 * Trackstick packets seem to be distinguished by always
809 * having 0x3f in the last byte. This value has never been
810 * observed in the last byte of either of the other types
811 * of packets.
812 */
813 if (packet[5] == 0x3f) {
814 alps_process_trackstick_packet_v3(psmouse);
815 return;
816 }
817
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800818 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800819}
820
Yunkang Tang95f75e92013-12-01 22:33:52 -0800821static void alps_process_packet_v6(struct psmouse *psmouse)
822{
823 struct alps_data *priv = psmouse->private;
824 unsigned char *packet = psmouse->packet;
825 struct input_dev *dev = psmouse->dev;
826 struct input_dev *dev2 = priv->dev2;
827 int x, y, z, left, right, middle;
828
829 /*
830 * We can use Byte5 to distinguish if the packet is from Touchpad
831 * or Trackpoint.
832 * Touchpad: 0 - 0x7E
833 * Trackpoint: 0x7F
834 */
835 if (packet[5] == 0x7F) {
836 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800837 if (!(priv->flags & ALPS_DUALPOINT)) {
838 psmouse_warn(psmouse,
839 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800840 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800841 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800842
843 /* Trackpoint packet */
844 x = packet[1] | ((packet[3] & 0x20) << 2);
845 y = packet[2] | ((packet[3] & 0x40) << 1);
846 z = packet[4];
847 left = packet[3] & 0x01;
848 right = packet[3] & 0x02;
849 middle = packet[3] & 0x04;
850
851 /* To prevent the cursor jump when finger lifted */
852 if (x == 0x7F && y == 0x7F && z == 0x7F)
853 x = y = z = 0;
854
855 /* Divide 4 since trackpoint's speed is too fast */
856 input_report_rel(dev2, REL_X, (char)x / 4);
857 input_report_rel(dev2, REL_Y, -((char)y / 4));
858
859 input_report_key(dev2, BTN_LEFT, left);
860 input_report_key(dev2, BTN_RIGHT, right);
861 input_report_key(dev2, BTN_MIDDLE, middle);
862
863 input_sync(dev2);
864 return;
865 }
866
867 /* Touchpad packet */
868 x = packet[1] | ((packet[3] & 0x78) << 4);
869 y = packet[2] | ((packet[4] & 0x78) << 4);
870 z = packet[5];
871 left = packet[3] & 0x01;
872 right = packet[3] & 0x02;
873
874 if (z > 30)
875 input_report_key(dev, BTN_TOUCH, 1);
876 if (z < 25)
877 input_report_key(dev, BTN_TOUCH, 0);
878
879 if (z > 0) {
880 input_report_abs(dev, ABS_X, x);
881 input_report_abs(dev, ABS_Y, y);
882 }
883
884 input_report_abs(dev, ABS_PRESSURE, z);
885 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
886
887 /* v6 touchpad does not have middle button */
888 input_report_key(dev, BTN_LEFT, left);
889 input_report_key(dev, BTN_RIGHT, right);
890
891 input_sync(dev);
892}
893
Seth Forshee25bded72011-11-07 19:53:36 -0800894static void alps_process_packet_v4(struct psmouse *psmouse)
895{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700896 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800897 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700898 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700899 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700900
901 /*
902 * v4 has a 6-byte encoding for bitmap data, but this data is
903 * broken up between 3 normal packets. Use priv->multi_packet to
904 * track our position in the bitmap packet.
905 */
906 if (packet[6] & 0x40) {
907 /* sync, reset position */
908 priv->multi_packet = 0;
909 }
910
911 if (WARN_ON_ONCE(priv->multi_packet > 2))
912 return;
913
914 offset = 2 * priv->multi_packet;
915 priv->multi_data[offset] = packet[6];
916 priv->multi_data[offset + 1] = packet[7];
917
Hans de Goede44b77f32015-05-20 14:40:06 -0700918 f->left = !!(packet[4] & 0x01);
919 f->right = !!(packet[4] & 0x02);
920
921 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
922 ((packet[0] & 0x30) >> 4);
923 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
924 f->pressure = packet[5] & 0x7f;
925
George Pantalos3b7e09f2012-05-10 22:31:59 -0700926 if (++priv->multi_packet > 2) {
927 priv->multi_packet = 0;
928
Hans de Goede02d04252014-07-25 22:43:35 -0700929 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700930 ((priv->multi_data[3] & 0x60) << 3) |
931 ((priv->multi_data[0] & 0x3f) << 2) |
932 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700933 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700934 ((priv->multi_data[3] & 0x1f) << 5) |
935 (priv->multi_data[1] & 0x1f);
936
Hans de Goede02d04252014-07-25 22:43:35 -0700937 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700938 }
Seth Forshee25bded72011-11-07 19:53:36 -0800939
Hans de Goede68c21872014-07-25 22:47:25 -0700940 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800941}
942
Yunkang Tang38088432014-07-26 13:51:41 -0700943static bool alps_is_valid_package_v7(struct psmouse *psmouse)
944{
945 switch (psmouse->pktcnt) {
946 case 3:
947 return (psmouse->packet[2] & 0x40) == 0x40;
948 case 4:
949 return (psmouse->packet[3] & 0x48) == 0x48;
950 case 6:
951 return (psmouse->packet[5] & 0x40) == 0x00;
952 }
953 return true;
954}
955
956static unsigned char alps_get_packet_id_v7(char *byte)
957{
958 unsigned char packet_id;
959
960 if (byte[4] & 0x40)
961 packet_id = V7_PACKET_ID_TWO;
962 else if (byte[4] & 0x01)
963 packet_id = V7_PACKET_ID_MULTI;
964 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
965 packet_id = V7_PACKET_ID_NEW;
966 else if (byte[1] == 0x00 && byte[4] == 0x00)
967 packet_id = V7_PACKET_ID_IDLE;
968 else
969 packet_id = V7_PACKET_ID_UNKNOWN;
970
971 return packet_id;
972}
973
974static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
975 unsigned char *pkt,
976 unsigned char pkt_id)
977{
978 mt[0].x = ((pkt[2] & 0x80) << 4);
979 mt[0].x |= ((pkt[2] & 0x3F) << 5);
980 mt[0].x |= ((pkt[3] & 0x30) >> 1);
981 mt[0].x |= (pkt[3] & 0x07);
982 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
983
984 mt[1].x = ((pkt[3] & 0x80) << 4);
985 mt[1].x |= ((pkt[4] & 0x80) << 3);
986 mt[1].x |= ((pkt[4] & 0x3F) << 4);
987 mt[1].y = ((pkt[5] & 0x80) << 3);
988 mt[1].y |= ((pkt[5] & 0x3F) << 4);
989
990 switch (pkt_id) {
991 case V7_PACKET_ID_TWO:
992 mt[1].x &= ~0x000F;
993 mt[1].y |= 0x000F;
994 break;
995
996 case V7_PACKET_ID_MULTI:
997 mt[1].x &= ~0x003F;
998 mt[1].y &= ~0x0020;
999 mt[1].y |= ((pkt[4] & 0x02) << 4);
1000 mt[1].y |= 0x001F;
1001 break;
1002
1003 case V7_PACKET_ID_NEW:
1004 mt[1].x &= ~0x003F;
1005 mt[1].x |= (pkt[0] & 0x20);
1006 mt[1].y |= 0x000F;
1007 break;
1008 }
1009
1010 mt[0].y = 0x7FF - mt[0].y;
1011 mt[1].y = 0x7FF - mt[1].y;
1012}
1013
1014static int alps_get_mt_count(struct input_mt_pos *mt)
1015{
Hans de Goede7091c442014-12-18 09:53:34 -08001016 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -07001017
Hans de Goede7091c442014-12-18 09:53:34 -08001018 for (i = 0; i < MAX_TOUCHES; i++) {
1019 if (mt[i].x != 0 || mt[i].y != 0)
1020 fingers++;
1021 }
Yunkang Tang38088432014-07-26 13:51:41 -07001022
Hans de Goede7091c442014-12-18 09:53:34 -08001023 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -07001024}
1025
1026static int alps_decode_packet_v7(struct alps_fields *f,
1027 unsigned char *p,
1028 struct psmouse *psmouse)
1029{
Hans de Goeded27eb792014-12-18 09:55:14 -08001030 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -07001031 unsigned char pkt_id;
1032
1033 pkt_id = alps_get_packet_id_v7(p);
1034 if (pkt_id == V7_PACKET_ID_IDLE)
1035 return 0;
1036 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1037 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -08001038 /*
1039 * NEW packets are send to indicate a discontinuity in the finger
1040 * coordinate reporting. Specifically a finger may have moved from
1041 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1042 * us.
1043 *
1044 * NEW packets have 3 problems:
1045 * 1) They do not contain middle / right button info (on non clickpads)
1046 * this can be worked around by preserving the old button state
1047 * 2) They do not contain an accurate fingercount, and they are
1048 * typically send when the number of fingers changes. We cannot use
1049 * the old finger count as that may mismatch with the amount of
1050 * touch coordinates we've available in the NEW packet
1051 * 3) Their x data for the second touch is inaccurate leading to
1052 * a possible jump of the x coordinate by 16 units when the first
1053 * non NEW packet comes in
1054 * Since problems 2 & 3 cannot be worked around, just ignore them.
1055 */
1056 if (pkt_id == V7_PACKET_ID_NEW)
1057 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001058
1059 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1060
Yunkang Tang38088432014-07-26 13:51:41 -07001061 if (pkt_id == V7_PACKET_ID_TWO)
1062 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001063 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001064 f->fingers = 3 + (p[5] & 0x03);
1065
Hans de Goeded27eb792014-12-18 09:55:14 -08001066 f->left = (p[0] & 0x80) >> 7;
1067 if (priv->flags & ALPS_BUTTONPAD) {
1068 if (p[0] & 0x20)
1069 f->fingers++;
1070 if (p[0] & 0x10)
1071 f->fingers++;
1072 } else {
1073 f->right = (p[0] & 0x20) >> 5;
1074 f->middle = (p[0] & 0x10) >> 4;
1075 }
1076
Hans de Goede7091c442014-12-18 09:53:34 -08001077 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1078 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1079 f->mt[0].x = f->mt[1].x;
1080 f->mt[0].y = f->mt[1].y;
1081 f->mt[1].x = 0;
1082 f->mt[1].y = 0;
1083 }
1084
Yunkang Tang38088432014-07-26 13:51:41 -07001085 return 0;
1086}
1087
1088static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1089{
1090 struct alps_data *priv = psmouse->private;
1091 unsigned char *packet = psmouse->packet;
1092 struct input_dev *dev2 = priv->dev2;
1093 int x, y, z, left, right, middle;
1094
Pali Rohár34412ba2015-01-09 12:48:58 -08001095 /* It should be a DualPoint when received trackstick packet */
1096 if (!(priv->flags & ALPS_DUALPOINT)) {
1097 psmouse_warn(psmouse,
1098 "Rejected trackstick packet from non DualPoint device");
1099 return;
1100 }
1101
Yunkang Tang38088432014-07-26 13:51:41 -07001102 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1103 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1104 ((packet[3] & 0x20) << 1);
1105 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1106
1107 left = (packet[1] & 0x01);
1108 right = (packet[1] & 0x02) >> 1;
1109 middle = (packet[1] & 0x04) >> 2;
1110
1111 /* Divide 2 since trackpoint's speed is too fast */
1112 input_report_rel(dev2, REL_X, (char)x / 2);
1113 input_report_rel(dev2, REL_Y, -((char)y / 2));
1114
1115 input_report_key(dev2, BTN_LEFT, left);
1116 input_report_key(dev2, BTN_RIGHT, right);
1117 input_report_key(dev2, BTN_MIDDLE, middle);
1118
1119 input_sync(dev2);
1120}
1121
1122static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1123{
1124 struct alps_data *priv = psmouse->private;
1125 struct input_dev *dev = psmouse->dev;
1126 struct alps_fields *f = &priv->f;
1127
1128 memset(f, 0, sizeof(*f));
1129
1130 if (priv->decode_fields(f, psmouse->packet, psmouse))
1131 return;
1132
1133 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1134
1135 input_mt_report_finger_count(dev, f->fingers);
1136
1137 input_report_key(dev, BTN_LEFT, f->left);
1138 input_report_key(dev, BTN_RIGHT, f->right);
1139 input_report_key(dev, BTN_MIDDLE, f->middle);
1140
1141 input_sync(dev);
1142}
1143
1144static void alps_process_packet_v7(struct psmouse *psmouse)
1145{
1146 unsigned char *packet = psmouse->packet;
1147
1148 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1149 alps_process_trackstick_packet_v7(psmouse);
1150 else
1151 alps_process_touchpad_packet_v7(psmouse);
1152}
1153
Fengguang Wu07f19e32015-04-10 23:54:31 -07001154static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001155{
1156 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1157
1158 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1159 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
1160 pkt_id = SS4_PACKET_ID_IDLE;
1161 } else if (!(byte[3] & 0x10)) {
1162 pkt_id = SS4_PACKET_ID_ONE;
1163 } else if (!(byte[3] & 0x20)) {
1164 pkt_id = SS4_PACKET_ID_TWO;
1165 } else {
1166 pkt_id = SS4_PACKET_ID_MULTI;
1167 }
1168
1169 return pkt_id;
1170}
1171
1172static int alps_decode_ss4_v2(struct alps_fields *f,
1173 unsigned char *p, struct psmouse *psmouse)
1174{
1175 struct alps_data *priv = psmouse->private;
1176 unsigned char pkt_id;
1177 unsigned int no_data_x, no_data_y;
1178
1179 pkt_id = alps_get_pkt_id_ss4_v2(p);
1180
1181 /* Current packet is 1Finger coordinate packet */
1182 switch (pkt_id) {
1183 case SS4_PACKET_ID_ONE:
1184 f->mt[0].x = SS4_1F_X_V2(p);
1185 f->mt[0].y = SS4_1F_Y_V2(p);
1186 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1187 f->fingers = 1;
1188 f->first_mp = 0;
1189 f->is_mp = 0;
1190 break;
1191
1192 case SS4_PACKET_ID_TWO:
1193 if (priv->flags & ALPS_BUTTONPAD) {
1194 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1195 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1196 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1197 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1198 } else {
1199 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1200 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1201 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1202 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1203 }
1204 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1205
1206 if (SS4_IS_MF_CONTINUE(p)) {
1207 f->first_mp = 1;
1208 } else {
1209 f->fingers = 2;
1210 f->first_mp = 0;
1211 }
1212 f->is_mp = 0;
1213
1214 break;
1215
1216 case SS4_PACKET_ID_MULTI:
1217 if (priv->flags & ALPS_BUTTONPAD) {
1218 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1219 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1220 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1221 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1222 no_data_x = SS4_MFPACKET_NO_AX_BL;
1223 no_data_y = SS4_MFPACKET_NO_AY_BL;
1224 } else {
1225 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1226 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1227 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1228 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1229 no_data_x = SS4_MFPACKET_NO_AX;
1230 no_data_y = SS4_MFPACKET_NO_AY;
1231 }
1232
1233 f->first_mp = 0;
1234 f->is_mp = 1;
1235
1236 if (SS4_IS_5F_DETECTED(p)) {
1237 f->fingers = 5;
1238 } else if (f->mt[3].x == no_data_x &&
1239 f->mt[3].y == no_data_y) {
1240 f->mt[3].x = 0;
1241 f->mt[3].y = 0;
1242 f->fingers = 3;
1243 } else {
1244 f->fingers = 4;
1245 }
1246 break;
1247
1248 case SS4_PACKET_ID_IDLE:
1249 default:
1250 memset(f, 0, sizeof(struct alps_fields));
1251 break;
1252 }
1253
1254 f->left = !!(SS4_BTN_V2(p) & 0x01);
1255 if (!(priv->flags & ALPS_BUTTONPAD)) {
1256 f->right = !!(SS4_BTN_V2(p) & 0x02);
1257 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1258 }
1259
1260 return 0;
1261}
1262
1263static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1264{
1265 struct alps_data *priv = psmouse->private;
1266 unsigned char *packet = psmouse->packet;
1267 struct input_dev *dev = psmouse->dev;
1268 struct alps_fields *f = &priv->f;
1269
1270 memset(f, 0, sizeof(struct alps_fields));
1271 priv->decode_fields(f, packet, psmouse);
1272 if (priv->multi_packet) {
1273 /*
1274 * Sometimes the first packet will indicate a multi-packet
1275 * sequence, but sometimes the next multi-packet would not
1276 * come. Check for this, and when it happens process the
1277 * position packet as usual.
1278 */
1279 if (f->is_mp) {
1280 /* Now process the 1st packet */
1281 priv->decode_fields(f, priv->multi_data, psmouse);
1282 } else {
1283 priv->multi_packet = 0;
1284 }
1285 }
1286
1287 /*
1288 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1289 * When it is set, it means 2nd packet comes without 1st packet come.
1290 */
1291 if (f->is_mp)
1292 return;
1293
1294 /* Save the first packet */
1295 if (!priv->multi_packet && f->first_mp) {
1296 priv->multi_packet = 1;
1297 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1298 return;
1299 }
1300
1301 priv->multi_packet = 0;
1302
1303 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1304
1305 input_mt_report_finger_count(dev, f->fingers);
1306
1307 input_report_key(dev, BTN_LEFT, f->left);
1308 input_report_key(dev, BTN_RIGHT, f->right);
1309 input_report_key(dev, BTN_MIDDLE, f->middle);
1310
1311 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1312 input_sync(dev);
1313}
1314
1315static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1316{
1317 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1318 return false;
1319 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1320 return false;
1321 return true;
1322}
1323
Pali Rohár04aae282015-01-14 13:18:30 -08001324static DEFINE_MUTEX(alps_mutex);
1325
1326static void alps_register_bare_ps2_mouse(struct work_struct *work)
1327{
1328 struct alps_data *priv =
1329 container_of(work, struct alps_data, dev3_register_work.work);
1330 struct psmouse *psmouse = priv->psmouse;
1331 struct input_dev *dev3;
1332 int error = 0;
1333
1334 mutex_lock(&alps_mutex);
1335
1336 if (priv->dev3)
1337 goto out;
1338
1339 dev3 = input_allocate_device();
1340 if (!dev3) {
1341 psmouse_err(psmouse, "failed to allocate secondary device\n");
1342 error = -ENOMEM;
1343 goto out;
1344 }
1345
1346 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1347 psmouse->ps2dev.serio->phys,
1348 (priv->dev2 ? "input2" : "input1"));
1349 dev3->phys = priv->phys3;
1350
1351 /*
1352 * format of input device name is: "protocol vendor name"
1353 * see function psmouse_switch_protocol() in psmouse-base.c
1354 */
1355 dev3->name = "PS/2 ALPS Mouse";
1356
1357 dev3->id.bustype = BUS_I8042;
1358 dev3->id.vendor = 0x0002;
1359 dev3->id.product = PSMOUSE_PS2;
1360 dev3->id.version = 0x0000;
1361 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1362
1363 input_set_capability(dev3, EV_REL, REL_X);
1364 input_set_capability(dev3, EV_REL, REL_Y);
1365 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1366 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1367 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1368
1369 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1370
1371 error = input_register_device(dev3);
1372 if (error) {
1373 psmouse_err(psmouse,
1374 "failed to register secondary device: %d\n",
1375 error);
1376 input_free_device(dev3);
1377 goto out;
1378 }
1379
1380 priv->dev3 = dev3;
1381
1382out:
1383 /*
1384 * Save the error code so that we can detect that we
1385 * already tried to create the device.
1386 */
1387 if (error)
1388 priv->dev3 = ERR_PTR(error);
1389
1390 mutex_unlock(&alps_mutex);
1391}
1392
Hans de Goede59c30af2015-04-03 17:14:40 -07001393static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001394 unsigned char packet[],
1395 bool report_buttons)
1396{
Hans de Goede59c30af2015-04-03 17:14:40 -07001397 struct alps_data *priv = psmouse->private;
1398 struct input_dev *dev;
1399
Hans de Goedee3a79212015-04-03 17:20:05 -07001400 /* Figure out which device to use to report the bare packet */
1401 if (priv->proto_version == ALPS_PROTO_V2 &&
1402 (priv->flags & ALPS_DUALPOINT)) {
1403 /* On V2 devices the DualPoint Stick reports bare packets */
1404 dev = priv->dev2;
1405 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001406 /* Register dev3 mouse if we received PS/2 packet first time */
1407 if (!IS_ERR(priv->dev3))
1408 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1409 0);
1410 return;
1411 } else {
1412 dev = priv->dev3;
1413 }
1414
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001415 if (report_buttons)
Pali Rohár04aae282015-01-14 13:18:30 -08001416 alps_report_buttons(dev, NULL,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001417 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1418
Pali Rohár04aae282015-01-14 13:18:30 -08001419 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001420 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001421 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001422 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1423
Pali Rohár04aae282015-01-14 13:18:30 -08001424 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001425}
1426
1427static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 struct alps_data *priv = psmouse->private;
1430
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001431 if (psmouse->pktcnt < 6)
1432 return PSMOUSE_GOOD_DATA;
1433
1434 if (psmouse->pktcnt == 6) {
1435 /*
1436 * Start a timer to flush the packet if it ends up last
1437 * 6-byte packet in the stream. Timer needs to fire
1438 * psmouse core times out itself. 20 ms should be enough
1439 * to decide if we are getting more data or not.
1440 */
1441 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1442 return PSMOUSE_GOOD_DATA;
1443 }
1444
1445 del_timer(&priv->timer);
1446
1447 if (psmouse->packet[6] & 0x80) {
1448
1449 /*
1450 * Highest bit is set - that means we either had
1451 * complete ALPS packet and this is start of the
1452 * next packet or we got garbage.
1453 */
1454
1455 if (((psmouse->packet[3] |
1456 psmouse->packet[4] |
1457 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001458 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001459 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001460 "refusing packet %4ph (suspected interleaved ps/2)\n",
1461 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001462 return PSMOUSE_BAD_DATA;
1463 }
1464
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001465 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001466
1467 /* Continue with the next packet */
1468 psmouse->packet[0] = psmouse->packet[6];
1469 psmouse->pktcnt = 1;
1470
1471 } else {
1472
1473 /*
1474 * High bit is 0 - that means that we indeed got a PS/2
1475 * packet in the middle of ALPS packet.
1476 *
1477 * There is also possibility that we got 6-byte ALPS
1478 * packet followed by 3-byte packet from trackpoint. We
1479 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001480 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001481 * normal operation (user would need to press all
1482 * buttons on the pad and start moving trackpoint
1483 * without touching the pad surface) we assume former.
1484 * Even if we are wrong the wost thing that would happen
1485 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001486 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001487 */
1488
Hans de Goede59c30af2015-04-03 17:14:40 -07001489 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1490 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001491
1492 /*
1493 * Continue with the standard ALPS protocol handling,
1494 * but make sure we won't process it as an interleaved
1495 * packet again, which may happen if all buttons are
1496 * pressed. To avoid this let's reset the 4th bit which
1497 * is normally 1.
1498 */
1499 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1500 psmouse->pktcnt = 4;
1501 }
1502
1503 return PSMOUSE_GOOD_DATA;
1504}
1505
1506static void alps_flush_packet(unsigned long data)
1507{
1508 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001509 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001510
1511 serio_pause_rx(psmouse->ps2dev.serio);
1512
Seth Forsheeb46615f2011-11-07 19:53:30 -08001513 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001514
1515 /*
1516 * We did not any more data in reasonable amount of time.
1517 * Validate the last 3 bytes and process as a standard
1518 * ALPS packet.
1519 */
1520 if ((psmouse->packet[3] |
1521 psmouse->packet[4] |
1522 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001523 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001524 "refusing packet %3ph (suspected interleaved ps/2)\n",
1525 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001526 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001527 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001528 }
1529 psmouse->pktcnt = 0;
1530 }
1531
1532 serio_continue_rx(psmouse->ps2dev.serio);
1533}
1534
1535static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1536{
1537 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001538
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001539 /*
1540 * Check if we are dealing with a bare PS/2 packet, presumably from
1541 * a device connected to the external PS/2 port. Because bare PS/2
1542 * protocol does not have enough constant bits to self-synchronize
1543 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001544 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001545 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001546 if (priv->proto_version != ALPS_PROTO_V8 &&
1547 !psmouse->out_of_sync_cnt &&
1548 (psmouse->packet[0] & 0xc8) == 0x08) {
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001551 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1552 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return PSMOUSE_FULL_PACKET;
1554 }
1555 return PSMOUSE_GOOD_DATA;
1556 }
1557
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001558 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1559
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001560 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001561 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1562 return alps_handle_interleaved_ps2(psmouse);
1563 }
1564
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001565 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001566 psmouse_dbg(psmouse,
1567 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001568 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Seth Forsheeb46615f2011-11-07 19:53:30 -08001572 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001573 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001574 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001575 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001576 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1577 psmouse->pktcnt - 1,
1578 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001579
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001580 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001581 psmouse->pktcnt == psmouse->pktsize) {
1582 /*
1583 * Some Dell boxes, such as Latitude E6440 or E7440
1584 * with closed lid, quite often smash last byte of
1585 * otherwise valid packet with 0xff. Given that the
1586 * next packet is very likely to be valid let's
1587 * report PSMOUSE_FULL_PACKET but not process data,
1588 * rather than reporting PSMOUSE_BAD_DATA and
1589 * filling the logs.
1590 */
1591 return PSMOUSE_FULL_PACKET;
1592 }
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001597 if ((priv->proto_version == ALPS_PROTO_V7 &&
1598 !alps_is_valid_package_v7(psmouse)) ||
1599 (priv->proto_version == ALPS_PROTO_V8 &&
1600 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001601 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1602 psmouse->pktcnt - 1,
1603 psmouse->packet[psmouse->pktcnt - 1]);
1604 return PSMOUSE_BAD_DATA;
1605 }
1606
Seth Forsheeb46615f2011-11-07 19:53:30 -08001607 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001608 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return PSMOUSE_FULL_PACKET;
1610 }
1611
1612 return PSMOUSE_GOOD_DATA;
1613}
1614
Seth Forshee25bded72011-11-07 19:53:36 -08001615static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1616{
1617 struct ps2dev *ps2dev = &psmouse->ps2dev;
1618 struct alps_data *priv = psmouse->private;
1619 int command;
1620 unsigned char *param;
1621 unsigned char dummy[4];
1622
1623 BUG_ON(nibble > 0xf);
1624
1625 command = priv->nibble_commands[nibble].command;
1626 param = (command & 0x0f00) ?
1627 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1628
1629 if (ps2_command(ps2dev, param, command))
1630 return -1;
1631
1632 return 0;
1633}
1634
1635static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1636{
1637 struct ps2dev *ps2dev = &psmouse->ps2dev;
1638 struct alps_data *priv = psmouse->private;
1639 int i, nibble;
1640
1641 if (ps2_command(ps2dev, NULL, priv->addr_command))
1642 return -1;
1643
1644 for (i = 12; i >= 0; i -= 4) {
1645 nibble = (addr >> i) & 0xf;
1646 if (alps_command_mode_send_nibble(psmouse, nibble))
1647 return -1;
1648 }
1649
1650 return 0;
1651}
1652
1653static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1654{
1655 struct ps2dev *ps2dev = &psmouse->ps2dev;
1656 unsigned char param[4];
1657
1658 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1659 return -1;
1660
1661 /*
1662 * The address being read is returned in the first two bytes
1663 * of the result. Check that this address matches the expected
1664 * address.
1665 */
1666 if (addr != ((param[0] << 8) | param[1]))
1667 return -1;
1668
1669 return param[2];
1670}
1671
1672static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1673{
1674 if (alps_command_mode_set_addr(psmouse, addr))
1675 return -1;
1676 return __alps_command_mode_read_reg(psmouse, addr);
1677}
1678
1679static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1680{
1681 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1682 return -1;
1683 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1684 return -1;
1685 return 0;
1686}
1687
1688static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1689 u8 value)
1690{
1691 if (alps_command_mode_set_addr(psmouse, addr))
1692 return -1;
1693 return __alps_command_mode_write_reg(psmouse, value);
1694}
1695
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001696static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1697 int repeated_command, unsigned char *param)
1698{
1699 struct ps2dev *ps2dev = &psmouse->ps2dev;
1700
1701 param[0] = 0;
1702 if (init_command && ps2_command(ps2dev, param, init_command))
1703 return -EIO;
1704
1705 if (ps2_command(ps2dev, NULL, repeated_command) ||
1706 ps2_command(ps2dev, NULL, repeated_command) ||
1707 ps2_command(ps2dev, NULL, repeated_command))
1708 return -EIO;
1709
1710 param[0] = param[1] = param[2] = 0xff;
1711 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1712 return -EIO;
1713
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001714 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1715 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001716 return 0;
1717}
1718
Yunkang Tang38088432014-07-26 13:51:41 -07001719static bool alps_check_valid_firmware_id(unsigned char id[])
1720{
1721 if (id[0] == 0x73)
1722 return true;
1723
1724 if (id[0] == 0x88 &&
1725 (id[1] == 0x07 ||
1726 id[1] == 0x08 ||
1727 (id[1] & 0xf0) == 0xb0 ||
1728 (id[1] & 0xf0) == 0xc0)) {
1729 return true;
1730 }
1731
1732 return false;
1733}
1734
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001735static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001736{
1737 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001738
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001739 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001740 psmouse_err(psmouse, "failed to enter command mode\n");
1741 return -1;
1742 }
1743
Yunkang Tang38088432014-07-26 13:51:41 -07001744 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001745 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001746 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001747 return -1;
1748 }
Seth Forshee25bded72011-11-07 19:53:36 -08001749 return 0;
1750}
1751
1752static inline int alps_exit_command_mode(struct psmouse *psmouse)
1753{
1754 struct ps2dev *ps2dev = &psmouse->ps2dev;
1755 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1756 return -1;
1757 return 0;
1758}
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760/*
1761 * For DualPoint devices select the device that should respond to
1762 * subsequent commands. It looks like glidepad is behind stickpointer,
1763 * I'd thought it would be other way around...
1764 */
Seth Forshee25bded72011-11-07 19:53:36 -08001765static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1769
1770 if (ps2_command(ps2dev, NULL, cmd) ||
1771 ps2_command(ps2dev, NULL, cmd) ||
1772 ps2_command(ps2dev, NULL, cmd) ||
1773 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1774 return -1;
1775
1776 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001777 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 return 0;
1780}
1781
Seth Forshee25bded72011-11-07 19:53:36 -08001782static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct ps2dev *ps2dev = &psmouse->ps2dev;
1785
1786 /* Try ALPS magic knock - 4 disable before enable */
1787 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1788 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1789 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1790 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1791 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1792 return -1;
1793
1794 /*
1795 * Switch mouse to poll (remote) mode so motion data will not
1796 * get in our way
1797 */
1798 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1799}
1800
Yunkang Tang95f75e92013-12-01 22:33:52 -08001801static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1802{
1803 int i, nibble;
1804
1805 /*
1806 * b0-b11 are valid bits, send sequence is inverse.
1807 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1808 */
1809 for (i = 0; i <= 8; i += 4) {
1810 nibble = (word >> i) & 0xf;
1811 if (alps_command_mode_send_nibble(psmouse, nibble))
1812 return -1;
1813 }
1814
1815 return 0;
1816}
1817
1818static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1819 u16 addr, u16 value)
1820{
1821 struct ps2dev *ps2dev = &psmouse->ps2dev;
1822
1823 /* 0x0A0 is the command to write the word */
1824 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1825 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1826 alps_monitor_mode_send_word(psmouse, addr) ||
1827 alps_monitor_mode_send_word(psmouse, value) ||
1828 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1829 return -1;
1830
1831 return 0;
1832}
1833
1834static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1835{
1836 struct ps2dev *ps2dev = &psmouse->ps2dev;
1837
1838 if (enable) {
1839 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1840 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1841 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1842 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1843 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1844 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1845 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1846 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1847 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1848 return -1;
1849 } else {
1850 /* EC to exit monitor mode */
1851 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1852 return -1;
1853 }
1854
1855 return 0;
1856}
1857
1858static int alps_absolute_mode_v6(struct psmouse *psmouse)
1859{
1860 u16 reg_val = 0x181;
1861 int ret = -1;
1862
1863 /* enter monitor mode, to write the register */
1864 if (alps_monitor_mode(psmouse, true))
1865 return -1;
1866
1867 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1868
1869 if (alps_monitor_mode(psmouse, false))
1870 ret = -1;
1871
1872 return ret;
1873}
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875static int alps_get_status(struct psmouse *psmouse, char *param)
1876{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001878 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return -1;
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return 0;
1882}
1883
1884/*
1885 * Turn touchpad tapping on or off. The sequences are:
1886 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1887 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1888 * My guess that 0xE9 (GetInfo) is here as a sync point.
1889 * For models that also have stickpointer (DualPoints) its tapping
1890 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1891 * we don't fiddle with it.
1892 */
1893static int alps_tap_mode(struct psmouse *psmouse, int enable)
1894{
1895 struct ps2dev *ps2dev = &psmouse->ps2dev;
1896 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1897 unsigned char tap_arg = enable ? 0x0A : 0x00;
1898 unsigned char param[4];
1899
1900 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1901 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1902 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1903 ps2_command(ps2dev, &tap_arg, cmd))
1904 return -1;
1905
1906 if (alps_get_status(psmouse, param))
1907 return -1;
1908
1909 return 0;
1910}
1911
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001912/*
1913 * alps_poll() - poll the touchpad for current motion packet.
1914 * Used in resync.
1915 */
1916static int alps_poll(struct psmouse *psmouse)
1917{
1918 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001919 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001920 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001921
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001922 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001923 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001924
1925 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1926 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1927
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001928 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001929 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001930
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001931 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001932 return -1;
1933
1934 if ((psmouse->badbyte & 0xc8) == 0x08) {
1935/*
1936 * Poll the track stick ...
1937 */
1938 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1939 return -1;
1940 }
1941
1942 memcpy(psmouse->packet, buf, sizeof(buf));
1943 return 0;
1944}
1945
Seth Forshee25bded72011-11-07 19:53:36 -08001946static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001950 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001951 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001955 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001956 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Seth Forshee25bded72011-11-07 19:53:36 -08001960 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001961 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return -1;
1963 }
1964
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001965 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001966 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001970 /* ALPS needs stream mode, otherwise it won't report any data */
1971 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001972 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001973 return -1;
1974 }
1975
1976 return 0;
1977}
1978
Yunkang Tang95f75e92013-12-01 22:33:52 -08001979static int alps_hw_init_v6(struct psmouse *psmouse)
1980{
1981 unsigned char param[2] = {0xC8, 0x14};
1982
1983 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1984 if (alps_passthrough_mode_v2(psmouse, true))
1985 return -1;
1986
1987 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1988 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1989 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1990 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1991 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1992 return -1;
1993
1994 if (alps_passthrough_mode_v2(psmouse, false))
1995 return -1;
1996
1997 if (alps_absolute_mode_v6(psmouse)) {
1998 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1999 return -1;
2000 }
2001
2002 return 0;
2003}
2004
Seth Forshee25bded72011-11-07 19:53:36 -08002005/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08002006 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08002007 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08002008static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2009 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08002010{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002011 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08002012
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002013 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002014 return -1;
2015
Kevin Cernekeecd401202013-02-13 22:28:07 -08002016 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2017 if (reg_val == -1)
2018 goto error;
2019
Seth Forshee25bded72011-11-07 19:53:36 -08002020 if (enable)
2021 reg_val |= 0x01;
2022 else
2023 reg_val &= ~0x01;
2024
Kevin Cernekeecd401202013-02-13 22:28:07 -08002025 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08002026
Kevin Cernekeecd401202013-02-13 22:28:07 -08002027error:
2028 if (alps_exit_command_mode(psmouse))
2029 ret = -1;
2030 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08002031}
2032
2033/* Must be in command mode when calling this function */
2034static int alps_absolute_mode_v3(struct psmouse *psmouse)
2035{
2036 int reg_val;
2037
2038 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2039 if (reg_val == -1)
2040 return -1;
2041
2042 reg_val |= 0x06;
2043 if (__alps_command_mode_write_reg(psmouse, reg_val))
2044 return -1;
2045
2046 return 0;
2047}
2048
Kevin Cernekeecd401202013-02-13 22:28:07 -08002049static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
2050{
2051 int ret = -EIO, reg_val;
2052
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002053 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002054 goto error;
2055
2056 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2057 if (reg_val == -1)
2058 goto error;
2059
2060 /* bit 7: trackstick is present */
2061 ret = reg_val & 0x80 ? 0 : -ENODEV;
2062
2063error:
2064 alps_exit_command_mode(psmouse);
2065 return ret;
2066}
2067
2068static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2069{
2070 struct ps2dev *ps2dev = &psmouse->ps2dev;
2071 int ret = 0;
2072 unsigned char param[4];
2073
2074 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2075 return -EIO;
2076
2077 /*
2078 * E7 report for the trackstick
2079 *
2080 * There have been reports of failures to seem to trace back
2081 * to the above trackstick check failing. When these occur
2082 * this E7 report fails, so when that happens we continue
2083 * with the assumption that there isn't a trackstick after
2084 * all.
2085 */
2086 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002087 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002088 ret = -ENODEV;
2089 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002090 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002091
2092 /*
2093 * Not sure what this does, but it is absolutely
2094 * essential. Without it, the touchpad does not
2095 * work at all and the trackstick just emits normal
2096 * PS/2 packets.
2097 */
2098 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2099 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2100 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2101 alps_command_mode_send_nibble(psmouse, 0x9) ||
2102 alps_command_mode_send_nibble(psmouse, 0x4)) {
2103 psmouse_err(psmouse,
2104 "Error sending magic E6 sequence\n");
2105 ret = -EIO;
2106 goto error;
2107 }
2108
2109 /*
2110 * This ensures the trackstick packets are in the format
2111 * supported by this driver. If bit 1 isn't set the packet
2112 * format is different.
2113 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002114 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002115 alps_command_mode_write_reg(psmouse,
2116 reg_base + 0x08, 0x82) ||
2117 alps_exit_command_mode(psmouse))
2118 ret = -EIO;
2119 }
2120
2121error:
2122 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2123 ret = -EIO;
2124
2125 return ret;
2126}
2127
Seth Forshee25bded72011-11-07 19:53:36 -08002128static int alps_hw_init_v3(struct psmouse *psmouse)
2129{
Seth Forshee25bded72011-11-07 19:53:36 -08002130 struct ps2dev *ps2dev = &psmouse->ps2dev;
2131 int reg_val;
2132 unsigned char param[4];
2133
Kevin Cernekeecd401202013-02-13 22:28:07 -08002134 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2135 if (reg_val == -EIO)
2136 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002137
Kevin Cernekeecd401202013-02-13 22:28:07 -08002138 if (reg_val == 0 &&
2139 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002140 goto error;
2141
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002142 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002143 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002144 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2145 goto error;
2146 }
2147
2148 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2149 if (reg_val == -1)
2150 goto error;
2151 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2152 goto error;
2153
2154 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2155 if (reg_val == -1)
2156 goto error;
2157 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2158 goto error;
2159
2160 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2161 goto error;
2162 if (__alps_command_mode_write_reg(psmouse, 0x04))
2163 goto error;
2164
2165 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2166 goto error;
2167 if (__alps_command_mode_write_reg(psmouse, 0x03))
2168 goto error;
2169
2170 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2171 goto error;
2172 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2173 goto error;
2174
2175 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2176 goto error;
2177 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2178 goto error;
2179
Seth Forshee25bded72011-11-07 19:53:36 -08002180 alps_exit_command_mode(psmouse);
2181
2182 /* Set rate and enable data reporting */
2183 param[0] = 0x64;
2184 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2185 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2186 psmouse_err(psmouse, "Failed to enable data reporting\n");
2187 return -1;
2188 }
2189
2190 return 0;
2191
Seth Forshee25bded72011-11-07 19:53:36 -08002192error:
2193 /*
2194 * Leaving the touchpad in command mode will essentially render
2195 * it unusable until the machine reboots, so exit it here just
2196 * to be safe
2197 */
2198 alps_exit_command_mode(psmouse);
2199 return -1;
2200}
2201
Hans de Goedef3f33c62014-07-29 11:22:07 -07002202static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2203{
2204 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2205 struct alps_data *priv = psmouse->private;
2206
2207 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2208 if (reg < 0)
2209 return reg;
2210
2211 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2212 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2213
2214 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2215 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2216
2217 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2218 if (reg < 0)
2219 return reg;
2220
2221 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2222 x_electrode = 17 + x_electrode;
2223
2224 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2225 y_electrode = 13 + y_electrode;
2226
2227 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2228 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2229
2230 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2231 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2232
2233 psmouse_dbg(psmouse,
2234 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2235 x_pitch, y_pitch, x_electrode, y_electrode,
2236 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2237
2238 return 0;
2239}
2240
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002241static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2242{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002243 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002244 struct ps2dev *ps2dev = &psmouse->ps2dev;
2245 int reg_val, ret = -1;
2246
Kevin Cernekeecd401202013-02-13 22:28:07 -08002247 if (priv->flags & ALPS_DUALPOINT) {
2248 reg_val = alps_setup_trackstick_v3(psmouse,
2249 ALPS_REG_BASE_RUSHMORE);
2250 if (reg_val == -EIO)
2251 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002252 }
2253
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002254 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002255 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2256 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2257 goto error;
2258
Hans de Goedef3f33c62014-07-29 11:22:07 -07002259 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2260 goto error;
2261
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002262 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2263 if (reg_val == -1)
2264 goto error;
2265 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2266 goto error;
2267
2268 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2269 goto error;
2270
2271 /* enter absolute mode */
2272 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2273 if (reg_val == -1)
2274 goto error;
2275 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2276 goto error;
2277
2278 alps_exit_command_mode(psmouse);
2279 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2280
2281error:
2282 alps_exit_command_mode(psmouse);
2283 return ret;
2284}
2285
Seth Forshee25bded72011-11-07 19:53:36 -08002286/* Must be in command mode when calling this function */
2287static int alps_absolute_mode_v4(struct psmouse *psmouse)
2288{
2289 int reg_val;
2290
2291 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2292 if (reg_val == -1)
2293 return -1;
2294
2295 reg_val |= 0x02;
2296 if (__alps_command_mode_write_reg(psmouse, reg_val))
2297 return -1;
2298
2299 return 0;
2300}
2301
2302static int alps_hw_init_v4(struct psmouse *psmouse)
2303{
Seth Forshee25bded72011-11-07 19:53:36 -08002304 struct ps2dev *ps2dev = &psmouse->ps2dev;
2305 unsigned char param[4];
2306
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002307 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002308 goto error;
2309
2310 if (alps_absolute_mode_v4(psmouse)) {
2311 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2312 goto error;
2313 }
2314
2315 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2316 goto error;
2317
2318 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2319 goto error;
2320
2321 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2322 goto error;
2323
2324 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2325 goto error;
2326
2327 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2328 goto error;
2329
2330 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2331 goto error;
2332
2333 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2334 goto error;
2335
2336 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2337 goto error;
2338
2339 alps_exit_command_mode(psmouse);
2340
2341 /*
2342 * This sequence changes the output from a 9-byte to an
2343 * 8-byte format. All the same data seems to be present,
2344 * just in a more compact format.
2345 */
2346 param[0] = 0xc8;
2347 param[1] = 0x64;
2348 param[2] = 0x50;
2349 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2350 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2351 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2352 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2353 return -1;
2354
2355 /* Set rate and enable data reporting */
2356 param[0] = 0x64;
2357 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2358 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2359 psmouse_err(psmouse, "Failed to enable data reporting\n");
2360 return -1;
2361 }
2362
2363 return 0;
2364
2365error:
2366 /*
2367 * Leaving the touchpad in command mode will essentially render
2368 * it unusable until the machine reboots, so exit it here just
2369 * to be safe
2370 */
2371 alps_exit_command_mode(psmouse);
2372 return -1;
2373}
2374
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002375static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2376 unsigned char index, unsigned char otp[])
2377{
2378 struct ps2dev *ps2dev = &psmouse->ps2dev;
2379
2380 switch (index) {
2381 case 0:
2382 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2383 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2384 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2385 return -1;
2386
2387 break;
2388
2389 case 1:
2390 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2391 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2392 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2393 return -1;
2394
2395 break;
2396 }
2397
2398 return 0;
2399}
2400
Fengguang Wu07f19e32015-04-10 23:54:31 -07002401static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2402 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002403{
2404 int num_x_electrode;
2405 int num_y_electrode;
2406 int x_pitch, y_pitch, x_phys, y_phys;
2407
2408 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2409 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2410
2411 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2412 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2413
2414 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2415 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2416
2417 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2418 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2419
2420 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2421 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2422
2423 return 0;
2424}
2425
Fengguang Wu07f19e32015-04-10 23:54:31 -07002426static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2427 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002428{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002429 unsigned char is_btnless;
2430
2431 is_btnless = (otp[1][1] >> 3) & 0x01;
2432
2433 if (is_btnless)
2434 priv->flags |= ALPS_BUTTONPAD;
2435
2436 return 0;
2437}
2438
2439static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2440 struct alps_data *priv)
2441{
2442 unsigned char otp[2][4];
2443
2444 memset(otp, 0, sizeof(otp));
2445
2446 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2447 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2448 return -1;
2449
2450 alps_update_device_area_ss4_v2(otp, priv);
2451
2452 alps_update_btn_info_ss4_v2(otp, priv);
2453
2454 return 0;
2455}
2456
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002457static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2458 struct alps_data *priv)
2459{
2460 struct ps2dev *ps2dev = &psmouse->ps2dev;
2461 unsigned char param[4] = {0};
2462 int num_x_electrode, num_y_electrode;
2463
2464 if (alps_enter_command_mode(psmouse))
2465 return -1;
2466
2467 param[0] = 0x0a;
2468 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2469 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2470 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2471 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2472 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2473 return -1;
2474
2475 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2476 return -1;
2477
2478 /*
2479 * Dolphin's sensor line number is not fixed. It can be calculated
2480 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2481 * Further more, we can get device's x_max and y_max by multiplying
2482 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2483 *
2484 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2485 * real sensor line number X = 11 + 8 = 19, and
2486 * real sensor line number Y = 8 + 1 = 9.
2487 * So, x_max = (19 - 1) * 64 = 1152, and
2488 * y_max = (9 - 1) * 64 = 512.
2489 */
2490 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2491 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2492 priv->x_bits = num_x_electrode;
2493 priv->y_bits = num_y_electrode;
2494 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2495 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2496
2497 if (alps_exit_command_mode(psmouse))
2498 return -1;
2499
2500 return 0;
2501}
2502
Dave Turvene75af9e52013-02-21 22:58:28 -08002503static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2504{
2505 struct ps2dev *ps2dev = &psmouse->ps2dev;
2506 unsigned char param[2];
2507
2508 /* This is dolphin "v1" as empirically defined by florin9doi */
2509 param[0] = 0x64;
2510 param[1] = 0x28;
2511
2512 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2513 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2514 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2515 return -1;
2516
2517 return 0;
2518}
2519
Yunkang Tang38088432014-07-26 13:51:41 -07002520static int alps_hw_init_v7(struct psmouse *psmouse)
2521{
2522 struct ps2dev *ps2dev = &psmouse->ps2dev;
2523 int reg_val, ret = -1;
2524
2525 if (alps_enter_command_mode(psmouse) ||
2526 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2527 goto error;
2528
Hans de Goedef3f33c62014-07-29 11:22:07 -07002529 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2530 goto error;
2531
Yunkang Tang38088432014-07-26 13:51:41 -07002532 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2533 goto error;
2534
2535 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2536 if (reg_val == -1)
2537 goto error;
2538 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2539 goto error;
2540
2541 alps_exit_command_mode(psmouse);
2542 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2543
2544error:
2545 alps_exit_command_mode(psmouse);
2546 return ret;
2547}
2548
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002549static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2550{
2551 struct ps2dev *ps2dev = &psmouse->ps2dev;
2552 char param[2] = {0x64, 0x28};
2553 int ret = -1;
2554
2555 /* enter absolute mode */
2556 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2557 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2558 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2559 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2560 goto error;
2561 }
2562
2563 /* T.B.D. Decread noise packet number, delete in the future */
2564 if (alps_exit_command_mode(psmouse) ||
2565 alps_enter_command_mode(psmouse) ||
2566 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2567 goto error;
2568 }
2569 alps_exit_command_mode(psmouse);
2570
2571 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2572
2573error:
2574 alps_exit_command_mode(psmouse);
2575 return ret;
2576}
2577
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002578static int alps_set_protocol(struct psmouse *psmouse,
2579 struct alps_data *priv,
2580 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002581{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002582 psmouse->private = priv;
2583
2584 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2585
2586 priv->proto_version = protocol->version;
2587 priv->byte0 = protocol->byte0;
2588 priv->mask0 = protocol->mask0;
2589 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002590
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002591 priv->x_max = 2000;
2592 priv->y_max = 1400;
2593 priv->x_bits = 15;
2594 priv->y_bits = 11;
2595
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002596 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002597 case ALPS_PROTO_V1:
2598 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002599 priv->hw_init = alps_hw_init_v1_v2;
2600 priv->process_packet = alps_process_packet_v1_v2;
2601 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002602 priv->x_max = 1023;
2603 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002604 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002605
Seth Forshee25bded72011-11-07 19:53:36 -08002606 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002607 priv->hw_init = alps_hw_init_v3;
2608 priv->process_packet = alps_process_packet_v3;
2609 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002610 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002611 priv->nibble_commands = alps_v3_nibble_commands;
2612 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002613 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002614
2615 case ALPS_PROTO_V3_RUSHMORE:
2616 priv->hw_init = alps_hw_init_rushmore_v3;
2617 priv->process_packet = alps_process_packet_v3;
2618 priv->set_abs_params = alps_set_abs_params_mt;
2619 priv->decode_fields = alps_decode_rushmore;
2620 priv->nibble_commands = alps_v3_nibble_commands;
2621 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2622 priv->x_bits = 16;
2623 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002624
2625 if (alps_probe_trackstick_v3(psmouse,
2626 ALPS_REG_BASE_RUSHMORE) < 0)
2627 priv->flags &= ~ALPS_DUALPOINT;
2628
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002629 break;
2630
Seth Forshee25bded72011-11-07 19:53:36 -08002631 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002632 priv->hw_init = alps_hw_init_v4;
2633 priv->process_packet = alps_process_packet_v4;
2634 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002635 priv->nibble_commands = alps_v4_nibble_commands;
2636 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002637 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002638
Dave Turvene75af9e52013-02-21 22:58:28 -08002639 case ALPS_PROTO_V5:
2640 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002641 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002642 priv->decode_fields = alps_decode_dolphin;
2643 priv->set_abs_params = alps_set_abs_params_mt;
2644 priv->nibble_commands = alps_v3_nibble_commands;
2645 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002646 priv->x_bits = 23;
2647 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002648
2649 if (alps_dolphin_get_device_area(psmouse, priv))
2650 return -EIO;
2651
Dave Turvene75af9e52013-02-21 22:58:28 -08002652 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002653
Yunkang Tang95f75e92013-12-01 22:33:52 -08002654 case ALPS_PROTO_V6:
2655 priv->hw_init = alps_hw_init_v6;
2656 priv->process_packet = alps_process_packet_v6;
2657 priv->set_abs_params = alps_set_abs_params_st;
2658 priv->nibble_commands = alps_v6_nibble_commands;
2659 priv->x_max = 2047;
2660 priv->y_max = 1535;
2661 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002662
Yunkang Tang38088432014-07-26 13:51:41 -07002663 case ALPS_PROTO_V7:
2664 priv->hw_init = alps_hw_init_v7;
2665 priv->process_packet = alps_process_packet_v7;
2666 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002667 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002668 priv->nibble_commands = alps_v3_nibble_commands;
2669 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002670 priv->x_max = 0xfff;
2671 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002672
2673 if (priv->fw_ver[1] != 0xba)
2674 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002675
Yunkang Tang38088432014-07-26 13:51:41 -07002676 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002677
2678 case ALPS_PROTO_V8:
2679 priv->hw_init = alps_hw_init_ss4_v2;
2680 priv->process_packet = alps_process_packet_ss4_v2;
2681 priv->decode_fields = alps_decode_ss4_v2;
2682 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2683 priv->nibble_commands = alps_v3_nibble_commands;
2684 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2685
2686 if (alps_set_defaults_ss4_v2(psmouse, priv))
2687 return -EIO;
2688
2689 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002690 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002691
2692 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002693}
2694
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002695static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2696 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002697{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002698 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002699 int i;
2700
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002701 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2702 model = &alps_model_data[i];
2703
2704 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2705 (!model->command_mode_resp ||
2706 model->command_mode_resp == ec[2])) {
2707
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002708 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002709 }
2710 }
2711
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002712 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002713}
2714
2715static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2716{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002717 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002718 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002719 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002720
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002721 /*
2722 * First try "E6 report".
2723 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2724 * The bits 0-2 of the first byte will be 1s if some buttons are
2725 * pressed.
2726 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002727 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2728 PSMOUSE_CMD_SETSCALE11, e6))
2729 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002730
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002731 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2732 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002733
2734 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002735 * Now get the "E7" and "EC" reports. These will uniquely identify
2736 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002737 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002738 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2739 PSMOUSE_CMD_SETSCALE21, e7) ||
2740 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2741 PSMOUSE_CMD_RESET_WRAP, ec) ||
2742 alps_exit_command_mode(psmouse))
2743 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002744
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002745 protocol = alps_match_table(e7, ec);
2746 if (!protocol) {
2747 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2748 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2749 protocol = &alps_v5_protocol_data;
2750 } else if (ec[0] == 0x88 &&
2751 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2752 protocol = &alps_v7_protocol_data;
2753 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2754 protocol = &alps_v3_rushmore_data;
2755 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2756 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2757 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002758 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2759 e7[2] == 0x14 && ec[1] == 0x02) {
2760 protocol = &alps_v8_protocol_data;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002761 } else {
2762 psmouse_dbg(psmouse,
2763 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2764 return -EINVAL;
2765 }
2766 }
2767
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002768 if (priv) {
2769 /* Save the Firmware version */
2770 memcpy(priv->fw_ver, ec, 3);
2771 error = alps_set_protocol(psmouse, priv, protocol);
2772 if (error)
2773 return error;
2774 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002775
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002776 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002777}
2778
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002779static int alps_reconnect(struct psmouse *psmouse)
2780{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002781 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002782
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002783 psmouse_reset(psmouse);
2784
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002785 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002786 return -1;
2787
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002788 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
2791static void alps_disconnect(struct psmouse *psmouse)
2792{
2793 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002796 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002797 if (priv->dev2)
2798 input_unregister_device(priv->dev2);
2799 if (!IS_ERR_OR_NULL(priv->dev3))
2800 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 kfree(priv);
2802}
2803
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002804static void alps_set_abs_params_st(struct alps_data *priv,
2805 struct input_dev *dev1)
2806{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002807 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2808 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002809 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002810}
2811
Masaki Ota8eccd392015-03-27 20:44:21 -07002812static void alps_set_abs_params_mt_common(struct alps_data *priv,
2813 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002814{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002815 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2816 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002817
Hans de Goedef3f33c62014-07-29 11:22:07 -07002818 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2819 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2820
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002821 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2822 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002823}
Yunkang Tang38088432014-07-26 13:51:41 -07002824
Masaki Ota8eccd392015-03-27 20:44:21 -07002825static void alps_set_abs_params_mt(struct alps_data *priv,
2826 struct input_dev *dev1)
2827{
2828 alps_set_abs_params_mt_common(priv, dev1);
2829 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2830
2831 input_mt_init_slots(dev1, MAX_TOUCHES,
2832 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
Hans de Goede1662c0332015-05-20 14:42:15 -07002833 INPUT_MT_SEMI_MT);
Masaki Ota8eccd392015-03-27 20:44:21 -07002834}
2835
2836static void alps_set_abs_params_v7(struct alps_data *priv,
2837 struct input_dev *dev1)
2838{
2839 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002840 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002841
2842 input_mt_init_slots(dev1, MAX_TOUCHES,
2843 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2844 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002845
2846 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2847}
2848
2849static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2850 struct input_dev *dev1)
2851{
2852 alps_set_abs_params_mt_common(priv, dev1);
2853 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2854 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2855
2856 input_mt_init_slots(dev1, MAX_TOUCHES,
2857 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2858 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002859}
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861int alps_init(struct psmouse *psmouse)
2862{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002863 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002864 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002865 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002867 error = priv->hw_init(psmouse);
2868 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 goto init_fail;
2870
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002871 /*
2872 * Undo part of setup done for us by psmouse core since touchpad
2873 * is not a relative device.
2874 */
2875 __clear_bit(EV_REL, dev1->evbit);
2876 __clear_bit(REL_X, dev1->relbit);
2877 __clear_bit(REL_Y, dev1->relbit);
2878
2879 /*
2880 * Now set up our capabilities.
2881 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002882 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2883 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2884 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002885 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2886 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002888 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002889
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002890 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002892 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002893 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2894 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002897 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002898 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2899 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002902 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002903 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2904 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2905 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2906 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002907 } else if (priv->flags & ALPS_BUTTONPAD) {
2908 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2909 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002910 } else {
2911 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2912 }
2913
Pali Rohárdfba8602015-01-09 12:48:44 -08002914 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002915 struct input_dev *dev2;
2916
2917 dev2 = input_allocate_device();
2918 if (!dev2) {
2919 psmouse_err(psmouse,
2920 "failed to allocate trackstick device\n");
2921 error = -ENOMEM;
2922 goto init_fail;
2923 }
2924
2925 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2926 psmouse->ps2dev.serio->phys);
2927 dev2->phys = priv->phys2;
2928
Pali Rohárdfba8602015-01-09 12:48:44 -08002929 /*
2930 * format of input device name is: "protocol vendor name"
2931 * see function psmouse_switch_protocol() in psmouse-base.c
2932 */
2933 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002934
2935 dev2->id.bustype = BUS_I8042;
2936 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002937 dev2->id.product = PSMOUSE_ALPS;
2938 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002939 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002940
Pali Rohár04aae282015-01-14 13:18:30 -08002941 input_set_capability(dev2, EV_REL, REL_X);
2942 input_set_capability(dev2, EV_REL, REL_Y);
2943 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2944 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2945 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002946
Pali Rohár04aae282015-01-14 13:18:30 -08002947 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002948 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2949
Pali Rohár04aae282015-01-14 13:18:30 -08002950 error = input_register_device(dev2);
2951 if (error) {
2952 psmouse_err(psmouse,
2953 "failed to register trackstick device: %d\n",
2954 error);
2955 input_free_device(dev2);
2956 goto init_fail;
2957 }
2958
2959 priv->dev2 = dev2;
2960 }
2961
2962 priv->psmouse = psmouse;
2963
2964 INIT_DELAYED_WORK(&priv->dev3_register_work,
2965 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002968 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 psmouse->disconnect = alps_disconnect;
2970 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002971 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002973 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2974 psmouse->resync_time = 0;
2975
Pali Rohár9d720b32014-11-08 12:58:57 -08002976 /* Allow 2 invalid packets without resetting device */
2977 psmouse->resetafter = psmouse->pktsize * 2;
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return 0;
2980
2981init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002982 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002983 /*
2984 * Even though we did not allocate psmouse->private we do free
2985 * it here.
2986 */
2987 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002988 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002989 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002992int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002994 struct alps_data *priv;
2995 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002997 error = alps_identify(psmouse, NULL);
2998 if (error)
2999 return error;
3000
3001 /*
3002 * Reset the device to make sure it is fully operational:
3003 * on some laptops, like certain Dell Latitudes, we may
3004 * fail to properly detect presence of trackstick if device
3005 * has not been reset.
3006 */
3007 psmouse_reset(psmouse);
3008
3009 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3010 if (!priv)
3011 return -ENOMEM;
3012
3013 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003014 if (error) {
3015 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003016 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08003017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019 if (set_properties) {
3020 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003021 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05003022 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003023 psmouse->model = priv->proto_version;
3024 } else {
3025 /*
3026 * Destroy alps_data structure we allocated earlier since
3027 * this was just a "trial run". Otherwise we'll keep it
3028 * to be used by alps_init() which has to be called if
3029 * we succeed and set_properties is true.
3030 */
3031 kfree(priv);
3032 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08003034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return 0;
3036}
3037