blob: 7752bd59d4b7d529218dad186c155a91a9922ad1 [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
Hans de Goede92bac832015-04-12 15:42:35 -0700254 /* Non interleaved V2 dualpoint has separate stick button bits */
255 if (priv->proto_version == ALPS_PROTO_V2 &&
256 priv->flags == (ALPS_PASS | ALPS_DUALPOINT)) {
257 left |= packet[0] & 1;
258 right |= packet[0] & 2;
259 middle |= packet[0] & 4;
260 }
261
Pali Rohár04aae282015-01-14 13:18:30 -0800262 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800265 if (ges && !fin)
266 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /*
269 * A "tap and drag" operation is reported by the hardware as a transition
270 * from (!fin && ges) to (fin && ges). This should be translated to the
271 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
272 */
273 if (ges && fin && !priv->prev_fin) {
274 input_report_abs(dev, ABS_X, x);
275 input_report_abs(dev, ABS_Y, y);
276 input_report_abs(dev, ABS_PRESSURE, 0);
277 input_report_key(dev, BTN_TOOL_FINGER, 0);
278 input_sync(dev);
279 }
280 priv->prev_fin = fin;
281
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800282 if (z > 30)
283 input_report_key(dev, BTN_TOUCH, 1);
284 if (z < 25)
285 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (z > 0) {
288 input_report_abs(dev, ABS_X, x);
289 input_report_abs(dev, ABS_Y, y);
290 }
291
292 input_report_abs(dev, ABS_PRESSURE, z);
293 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
294
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800295 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500296 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800298 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500299 input_report_key(dev, BTN_FORWARD, forward);
300 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800303 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800304 input_report_key(dev, BTN_0, packet[2] & 4);
305 input_report_key(dev, BTN_1, packet[0] & 0x10);
306 input_report_key(dev, BTN_2, packet[3] & 4);
307 input_report_key(dev, BTN_3, packet[0] & 0x20);
308 }
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 input_sync(dev);
311}
312
Seth Forshee01ce6612011-11-07 19:54:13 -0800313/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800314 * Process bitmap data for V5 protocols. Return value is null.
315 *
316 * The bitmaps don't have enough data to track fingers, so this function
317 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700318 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800319 */
320static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700321 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800322{
323 int box_middle_x, box_middle_y;
324 unsigned int x_map, y_map;
325 unsigned char start_bit, end_bit;
326 unsigned char x_msb, x_lsb, y_msb, y_lsb;
327
328 x_map = fields->x_map;
329 y_map = fields->y_map;
330
331 if (!x_map || !y_map)
332 return;
333
334 /* Get Most-significant and Least-significant bit */
335 x_msb = fls(x_map);
336 x_lsb = ffs(x_map);
337 y_msb = fls(y_map);
338 y_lsb = ffs(y_map);
339
340 /* Most-significant bit should never exceed max sensor line number */
341 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
342 return;
343
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800344 if (fields->fingers > 1) {
345 start_bit = priv->x_bits - x_msb;
346 end_bit = priv->x_bits - x_lsb;
347 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
348 (2 * (priv->x_bits - 1));
349
350 start_bit = y_lsb - 1;
351 end_bit = y_msb - 1;
352 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
353 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700354 fields->mt[0] = fields->st;
355 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
356 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800357 }
358}
359
Hans de Goede036e6c72014-07-25 22:38:51 -0700360static void alps_get_bitmap_points(unsigned int map,
361 struct alps_bitmap_point *low,
362 struct alps_bitmap_point *high,
363 int *fingers)
364{
365 struct alps_bitmap_point *point;
366 int i, bit, prev_bit = 0;
367
368 point = low;
369 for (i = 0; map != 0; i++, map >>= 1) {
370 bit = map & 1;
371 if (bit) {
372 if (!prev_bit) {
373 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700374 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700375 (*fingers)++;
376 }
377 point->num_bits++;
378 } else {
379 if (prev_bit)
380 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700381 }
382 prev_bit = bit;
383 }
384}
385
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800386/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800387 * Process bitmap data from v3 and v4 protocols. Returns the number of
388 * fingers detected. A return value of 0 means at least one of the
389 * bitmaps was empty.
390 *
391 * The bitmaps don't have enough data to track fingers, so this function
392 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700393 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800394 * is greater than 0.
395 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800396static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700397 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800398{
Hans de Goede036e6c72014-07-25 22:38:51 -0700399 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800400 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
401 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800402
Hans de Goede02d04252014-07-25 22:43:35 -0700403 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800404 return 0;
405
Hans de Goede02d04252014-07-25 22:43:35 -0700406 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
407 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800408
409 /*
410 * Fingers can overlap, so we use the maximum count of fingers
411 * on either axis as the finger count.
412 */
413 fingers = max(fingers_x, fingers_y);
414
415 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700416 * If an axis reports only a single contact, we have overlapping or
417 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800418 */
Hans de Goede20bea682014-07-25 22:33:33 -0700419 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700420 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700421 x_low.num_bits = x_low.num_bits - i;
422 x_high.start_bit = x_low.start_bit + i;
423 x_high.num_bits = max(i, 1);
424 }
425 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700426 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700427 y_low.num_bits = y_low.num_bits - i;
428 y_high.start_bit = y_low.start_bit + i;
429 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800430 }
431
Hans de Goede02d04252014-07-25 22:43:35 -0700432 fields->mt[0].x =
433 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
434 (2 * (priv->x_bits - 1));
435 fields->mt[0].y =
436 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
437 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800438
Hans de Goede02d04252014-07-25 22:43:35 -0700439 fields->mt[1].x =
440 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
441 (2 * (priv->x_bits - 1));
442 fields->mt[1].y =
443 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
444 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800445
Hans de Goede40e8f532014-07-25 22:37:15 -0700446 /* y-bitmap order is reversed, except on rushmore */
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800447 if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700448 fields->mt[0].y = priv->y_max - fields->mt[0].y;
449 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700450 }
451
Seth Forshee01ce6612011-11-07 19:54:13 -0800452 return fingers;
453}
454
Hans de Goedecdf333e2014-07-25 22:44:42 -0700455static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800456{
457 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700458 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
459 input_report_abs(dev, ABS_MT_POSITION_X, x);
460 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800461}
462
Hans de Goedecdf333e2014-07-25 22:44:42 -0700463static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800464{
Hans de Goede02d04252014-07-25 22:43:35 -0700465 struct alps_data *priv = psmouse->private;
466 struct input_dev *dev = psmouse->dev;
467 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700468 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700469
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800470 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700471 for (i = 0; i < n; i++)
472 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
473
474 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800475}
476
Hans de Goede68c21872014-07-25 22:47:25 -0700477static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
478{
479 struct alps_data *priv = psmouse->private;
480 struct input_dev *dev = psmouse->dev;
481 struct alps_fields *f = &priv->f;
482
483 /* Use st data when we don't have mt data */
484 if (fingers < 2) {
485 f->mt[0].x = f->st.x;
486 f->mt[0].y = f->st.y;
487 fingers = f->pressure > 0 ? 1 : 0;
488 }
489
Hans de Goede99d99962014-07-25 22:48:02 -0700490 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
Hans de Goede68c21872014-07-25 22:47:25 -0700491
492 input_mt_report_finger_count(dev, fingers);
493
494 input_report_key(dev, BTN_LEFT, f->left);
495 input_report_key(dev, BTN_RIGHT, f->right);
496 input_report_key(dev, BTN_MIDDLE, f->middle);
497
498 input_report_abs(dev, ABS_PRESSURE, f->pressure);
499
500 input_sync(dev);
501}
502
Seth Forshee25bded72011-11-07 19:53:36 -0800503static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
504{
505 struct alps_data *priv = psmouse->private;
506 unsigned char *packet = psmouse->packet;
507 struct input_dev *dev = priv->dev2;
508 int x, y, z, left, right, middle;
509
Pali Rohár34412ba2015-01-09 12:48:58 -0800510 /* It should be a DualPoint when received trackstick packet */
511 if (!(priv->flags & ALPS_DUALPOINT)) {
512 psmouse_warn(psmouse,
513 "Rejected trackstick packet from non DualPoint device");
514 return;
515 }
516
Seth Forshee25bded72011-11-07 19:53:36 -0800517 /* Sanity check packet */
518 if (!(packet[0] & 0x40)) {
519 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
520 return;
521 }
522
523 /*
524 * There's a special packet that seems to indicate the end
525 * of a stream of trackstick data. Filter these out.
526 */
527 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
528 return;
529
530 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
531 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
532 z = (packet[4] & 0x7c) >> 2;
533
534 /*
535 * The x and y values tend to be quite large, and when used
536 * alone the trackstick is difficult to use. Scale them down
537 * to compensate.
538 */
539 x /= 8;
540 y /= 8;
541
542 input_report_rel(dev, REL_X, x);
543 input_report_rel(dev, REL_Y, -y);
544
545 /*
546 * Most ALPS models report the trackstick buttons in the touchpad
547 * packets, but a few report them here. No reliable way has been
548 * found to differentiate between the models upfront, so we enable
549 * the quirk in response to seeing a button press in the trackstick
550 * packet.
551 */
552 left = packet[3] & 0x01;
553 right = packet[3] & 0x02;
554 middle = packet[3] & 0x04;
555
556 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
557 (left || right || middle))
558 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
559
560 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
561 input_report_key(dev, BTN_LEFT, left);
562 input_report_key(dev, BTN_RIGHT, right);
563 input_report_key(dev, BTN_MIDDLE, middle);
564 }
565
566 input_sync(dev);
567 return;
568}
569
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800570static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
571{
572 f->left = !!(p[3] & 0x01);
573 f->right = !!(p[3] & 0x02);
574 f->middle = !!(p[3] & 0x04);
575
576 f->ts_left = !!(p[3] & 0x10);
577 f->ts_right = !!(p[3] & 0x20);
578 f->ts_middle = !!(p[3] & 0x40);
579}
580
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700581static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800582 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800583{
584 f->first_mp = !!(p[4] & 0x40);
585 f->is_mp = !!(p[0] & 0x40);
586
587 f->fingers = (p[5] & 0x3) + 1;
588 f->x_map = ((p[4] & 0x7e) << 8) |
589 ((p[1] & 0x7f) << 2) |
590 ((p[0] & 0x30) >> 4);
591 f->y_map = ((p[3] & 0x70) << 4) |
592 ((p[2] & 0x7f) << 1) |
593 (p[4] & 0x01);
594
Hans de Goede02d04252014-07-25 22:43:35 -0700595 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800596 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700597 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
598 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800599
600 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700601
602 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800603}
604
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700605static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800606 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800607{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800608 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800609
Yunkang Tangf105e342014-07-25 22:29:24 -0700610 /* Rushmore's packet decode has a bit difference with Pinnacle's */
611 f->is_mp = !!(p[5] & 0x40);
612 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800613 f->x_map |= (p[5] & 0x10) << 11;
614 f->y_map |= (p[5] & 0x20) << 6;
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700615
616 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800617}
618
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700619static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800620 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800621{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800622 u64 palm_data = 0;
623 struct alps_data *priv = psmouse->private;
624
Dave Turvene75af9e52013-02-21 22:58:28 -0800625 f->first_mp = !!(p[0] & 0x02);
626 f->is_mp = !!(p[0] & 0x20);
627
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800628 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700629 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
630 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
631 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800632 alps_decode_buttons_v3(f, p);
633 } else {
634 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800635 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800636
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800637 palm_data = (p[1] & 0x7f) |
638 ((p[2] & 0x7f) << 7) |
639 ((p[4] & 0x7f) << 14) |
640 ((p[5] & 0x7f) << 21) |
641 ((p[3] & 0x07) << 28) |
642 (((u64)p[3] & 0x70) << 27) |
643 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800644
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800645 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
646 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
647
648 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
649 f->x_map = (palm_data >> priv->y_bits) &
650 (BIT(priv->x_bits) - 1);
651 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700652
653 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800654}
655
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800656static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800657{
658 struct alps_data *priv = psmouse->private;
659 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800660 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700661 struct alps_fields *f = &priv->f;
662 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800663
Hans de Goede02d04252014-07-25 22:43:35 -0700664 memset(f, 0, sizeof(*f));
665
666 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800667
668 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800669 * There's no single feature of touchpad position and bitmap packets
670 * that can be used to distinguish between them. We rely on the fact
671 * that a bitmap packet should always follow a position packet with
672 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800673 */
674 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800675 /*
676 * Sometimes a position packet will indicate a multi-packet
677 * sequence, but then what follows is another position
678 * packet. Check for this, and when it happens process the
679 * position packet as usual.
680 */
Hans de Goede02d04252014-07-25 22:43:35 -0700681 if (f->is_mp) {
682 fingers = f->fingers;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800683 if (priv->proto_version == ALPS_PROTO_V3 ||
684 priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700685 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700686 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800687
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800688 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700689 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800690 psmouse);
691 } else {
692 /*
693 * Because Dolphin uses position packet's
694 * coordinate data as Pt1 and uses it to
695 * calculate Pt2, so we need to do position
696 * packet decode first.
697 */
Hans de Goede02d04252014-07-25 22:43:35 -0700698 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800699 psmouse);
700
701 /*
702 * Since Dolphin's finger number is reliable,
703 * there is no need to compare with bmap_fn.
704 */
Hans de Goede02d04252014-07-25 22:43:35 -0700705 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800706 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800707 } else {
708 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800709 }
710 }
711
Seth Forshee01ce6612011-11-07 19:54:13 -0800712 /*
713 * Bit 6 of byte 0 is not usually set in position packets. The only
714 * times it seems to be set is in situations where the data is
715 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
716 * this combined with the fact that this bit is useful for filtering
717 * out misidentified bitmap packets, we reject anything with this
718 * bit set.
719 */
Hans de Goede02d04252014-07-25 22:43:35 -0700720 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800721 return;
722
Hans de Goede02d04252014-07-25 22:43:35 -0700723 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800724 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800725 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
726 return;
727 }
728
729 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800730
Seth Forshee25bded72011-11-07 19:53:36 -0800731 /*
732 * Sometimes the hardware sends a single packet with z = 0
733 * in the middle of a stream. Real releases generate packets
734 * with x, y, and z all zero, so these seem to be flukes.
735 * Ignore them.
736 */
Hans de Goede02d04252014-07-25 22:43:35 -0700737 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800738 return;
739
Hans de Goede68c21872014-07-25 22:47:25 -0700740 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800741
Pali Rohár34412ba2015-01-09 12:48:58 -0800742 if ((priv->flags & ALPS_DUALPOINT) &&
743 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700744 input_report_key(dev2, BTN_LEFT, f->ts_left);
745 input_report_key(dev2, BTN_RIGHT, f->ts_right);
746 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800747 input_sync(dev2);
748 }
749}
750
751static void alps_process_packet_v3(struct psmouse *psmouse)
752{
753 unsigned char *packet = psmouse->packet;
754
755 /*
756 * v3 protocol packets come in three types, two representing
757 * touchpad data and one representing trackstick data.
758 * Trackstick packets seem to be distinguished by always
759 * having 0x3f in the last byte. This value has never been
760 * observed in the last byte of either of the other types
761 * of packets.
762 */
763 if (packet[5] == 0x3f) {
764 alps_process_trackstick_packet_v3(psmouse);
765 return;
766 }
767
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800768 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800769}
770
Yunkang Tang95f75e92013-12-01 22:33:52 -0800771static void alps_process_packet_v6(struct psmouse *psmouse)
772{
773 struct alps_data *priv = psmouse->private;
774 unsigned char *packet = psmouse->packet;
775 struct input_dev *dev = psmouse->dev;
776 struct input_dev *dev2 = priv->dev2;
777 int x, y, z, left, right, middle;
778
779 /*
780 * We can use Byte5 to distinguish if the packet is from Touchpad
781 * or Trackpoint.
782 * Touchpad: 0 - 0x7E
783 * Trackpoint: 0x7F
784 */
785 if (packet[5] == 0x7F) {
786 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800787 if (!(priv->flags & ALPS_DUALPOINT)) {
788 psmouse_warn(psmouse,
789 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800790 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800791 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800792
793 /* Trackpoint packet */
794 x = packet[1] | ((packet[3] & 0x20) << 2);
795 y = packet[2] | ((packet[3] & 0x40) << 1);
796 z = packet[4];
797 left = packet[3] & 0x01;
798 right = packet[3] & 0x02;
799 middle = packet[3] & 0x04;
800
801 /* To prevent the cursor jump when finger lifted */
802 if (x == 0x7F && y == 0x7F && z == 0x7F)
803 x = y = z = 0;
804
805 /* Divide 4 since trackpoint's speed is too fast */
806 input_report_rel(dev2, REL_X, (char)x / 4);
807 input_report_rel(dev2, REL_Y, -((char)y / 4));
808
809 input_report_key(dev2, BTN_LEFT, left);
810 input_report_key(dev2, BTN_RIGHT, right);
811 input_report_key(dev2, BTN_MIDDLE, middle);
812
813 input_sync(dev2);
814 return;
815 }
816
817 /* Touchpad packet */
818 x = packet[1] | ((packet[3] & 0x78) << 4);
819 y = packet[2] | ((packet[4] & 0x78) << 4);
820 z = packet[5];
821 left = packet[3] & 0x01;
822 right = packet[3] & 0x02;
823
824 if (z > 30)
825 input_report_key(dev, BTN_TOUCH, 1);
826 if (z < 25)
827 input_report_key(dev, BTN_TOUCH, 0);
828
829 if (z > 0) {
830 input_report_abs(dev, ABS_X, x);
831 input_report_abs(dev, ABS_Y, y);
832 }
833
834 input_report_abs(dev, ABS_PRESSURE, z);
835 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
836
837 /* v6 touchpad does not have middle button */
838 input_report_key(dev, BTN_LEFT, left);
839 input_report_key(dev, BTN_RIGHT, right);
840
841 input_sync(dev);
842}
843
Seth Forshee25bded72011-11-07 19:53:36 -0800844static void alps_process_packet_v4(struct psmouse *psmouse)
845{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700846 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800847 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700848 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700849 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700850
851 /*
852 * v4 has a 6-byte encoding for bitmap data, but this data is
853 * broken up between 3 normal packets. Use priv->multi_packet to
854 * track our position in the bitmap packet.
855 */
856 if (packet[6] & 0x40) {
857 /* sync, reset position */
858 priv->multi_packet = 0;
859 }
860
861 if (WARN_ON_ONCE(priv->multi_packet > 2))
862 return;
863
864 offset = 2 * priv->multi_packet;
865 priv->multi_data[offset] = packet[6];
866 priv->multi_data[offset + 1] = packet[7];
867
868 if (++priv->multi_packet > 2) {
869 priv->multi_packet = 0;
870
Hans de Goede02d04252014-07-25 22:43:35 -0700871 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700872 ((priv->multi_data[3] & 0x60) << 3) |
873 ((priv->multi_data[0] & 0x3f) << 2) |
874 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700875 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700876 ((priv->multi_data[3] & 0x1f) << 5) |
877 (priv->multi_data[1] & 0x1f);
878
Hans de Goede02d04252014-07-25 22:43:35 -0700879 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700880 }
Seth Forshee25bded72011-11-07 19:53:36 -0800881
Andreas Boschb0cfb792014-10-15 10:44:50 -0700882 f->left = !!(packet[4] & 0x01);
883 f->right = !!(packet[4] & 0x02);
Seth Forshee25bded72011-11-07 19:53:36 -0800884
Hans de Goede02d04252014-07-25 22:43:35 -0700885 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
886 ((packet[0] & 0x30) >> 4);
887 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
888 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800889
Hans de Goede68c21872014-07-25 22:47:25 -0700890 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800891}
892
Yunkang Tang38088432014-07-26 13:51:41 -0700893static bool alps_is_valid_package_v7(struct psmouse *psmouse)
894{
895 switch (psmouse->pktcnt) {
896 case 3:
897 return (psmouse->packet[2] & 0x40) == 0x40;
898 case 4:
899 return (psmouse->packet[3] & 0x48) == 0x48;
900 case 6:
901 return (psmouse->packet[5] & 0x40) == 0x00;
902 }
903 return true;
904}
905
906static unsigned char alps_get_packet_id_v7(char *byte)
907{
908 unsigned char packet_id;
909
910 if (byte[4] & 0x40)
911 packet_id = V7_PACKET_ID_TWO;
912 else if (byte[4] & 0x01)
913 packet_id = V7_PACKET_ID_MULTI;
914 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
915 packet_id = V7_PACKET_ID_NEW;
916 else if (byte[1] == 0x00 && byte[4] == 0x00)
917 packet_id = V7_PACKET_ID_IDLE;
918 else
919 packet_id = V7_PACKET_ID_UNKNOWN;
920
921 return packet_id;
922}
923
924static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
925 unsigned char *pkt,
926 unsigned char pkt_id)
927{
928 mt[0].x = ((pkt[2] & 0x80) << 4);
929 mt[0].x |= ((pkt[2] & 0x3F) << 5);
930 mt[0].x |= ((pkt[3] & 0x30) >> 1);
931 mt[0].x |= (pkt[3] & 0x07);
932 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
933
934 mt[1].x = ((pkt[3] & 0x80) << 4);
935 mt[1].x |= ((pkt[4] & 0x80) << 3);
936 mt[1].x |= ((pkt[4] & 0x3F) << 4);
937 mt[1].y = ((pkt[5] & 0x80) << 3);
938 mt[1].y |= ((pkt[5] & 0x3F) << 4);
939
940 switch (pkt_id) {
941 case V7_PACKET_ID_TWO:
942 mt[1].x &= ~0x000F;
943 mt[1].y |= 0x000F;
Hans de Goede72eceab2015-05-20 14:37:41 -0700944 /* Detect false-postive touches where x & y report max value */
945 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
946 mt[1].x = 0;
947 /* y gets set to 0 at the end of this function */
948 }
Yunkang Tang38088432014-07-26 13:51:41 -0700949 break;
950
951 case V7_PACKET_ID_MULTI:
952 mt[1].x &= ~0x003F;
953 mt[1].y &= ~0x0020;
954 mt[1].y |= ((pkt[4] & 0x02) << 4);
955 mt[1].y |= 0x001F;
956 break;
957
958 case V7_PACKET_ID_NEW:
959 mt[1].x &= ~0x003F;
960 mt[1].x |= (pkt[0] & 0x20);
961 mt[1].y |= 0x000F;
962 break;
963 }
964
965 mt[0].y = 0x7FF - mt[0].y;
966 mt[1].y = 0x7FF - mt[1].y;
967}
968
969static int alps_get_mt_count(struct input_mt_pos *mt)
970{
Hans de Goede7091c442014-12-18 09:53:34 -0800971 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700972
Hans de Goede7091c442014-12-18 09:53:34 -0800973 for (i = 0; i < MAX_TOUCHES; i++) {
974 if (mt[i].x != 0 || mt[i].y != 0)
975 fingers++;
976 }
Yunkang Tang38088432014-07-26 13:51:41 -0700977
Hans de Goede7091c442014-12-18 09:53:34 -0800978 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700979}
980
981static int alps_decode_packet_v7(struct alps_fields *f,
982 unsigned char *p,
983 struct psmouse *psmouse)
984{
Hans de Goeded27eb792014-12-18 09:55:14 -0800985 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700986 unsigned char pkt_id;
987
988 pkt_id = alps_get_packet_id_v7(p);
989 if (pkt_id == V7_PACKET_ID_IDLE)
990 return 0;
991 if (pkt_id == V7_PACKET_ID_UNKNOWN)
992 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -0800993 /*
994 * NEW packets are send to indicate a discontinuity in the finger
995 * coordinate reporting. Specifically a finger may have moved from
996 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
997 * us.
998 *
999 * NEW packets have 3 problems:
1000 * 1) They do not contain middle / right button info (on non clickpads)
1001 * this can be worked around by preserving the old button state
1002 * 2) They do not contain an accurate fingercount, and they are
1003 * typically send when the number of fingers changes. We cannot use
1004 * the old finger count as that may mismatch with the amount of
1005 * touch coordinates we've available in the NEW packet
1006 * 3) Their x data for the second touch is inaccurate leading to
1007 * a possible jump of the x coordinate by 16 units when the first
1008 * non NEW packet comes in
1009 * Since problems 2 & 3 cannot be worked around, just ignore them.
1010 */
1011 if (pkt_id == V7_PACKET_ID_NEW)
1012 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001013
1014 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1015
Yunkang Tang38088432014-07-26 13:51:41 -07001016 if (pkt_id == V7_PACKET_ID_TWO)
1017 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001018 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001019 f->fingers = 3 + (p[5] & 0x03);
1020
Hans de Goeded27eb792014-12-18 09:55:14 -08001021 f->left = (p[0] & 0x80) >> 7;
1022 if (priv->flags & ALPS_BUTTONPAD) {
1023 if (p[0] & 0x20)
1024 f->fingers++;
1025 if (p[0] & 0x10)
1026 f->fingers++;
1027 } else {
1028 f->right = (p[0] & 0x20) >> 5;
1029 f->middle = (p[0] & 0x10) >> 4;
1030 }
1031
Hans de Goede7091c442014-12-18 09:53:34 -08001032 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1033 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1034 f->mt[0].x = f->mt[1].x;
1035 f->mt[0].y = f->mt[1].y;
1036 f->mt[1].x = 0;
1037 f->mt[1].y = 0;
1038 }
1039
Yunkang Tang38088432014-07-26 13:51:41 -07001040 return 0;
1041}
1042
1043static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1044{
1045 struct alps_data *priv = psmouse->private;
1046 unsigned char *packet = psmouse->packet;
1047 struct input_dev *dev2 = priv->dev2;
1048 int x, y, z, left, right, middle;
1049
Pali Rohár34412ba2015-01-09 12:48:58 -08001050 /* It should be a DualPoint when received trackstick packet */
1051 if (!(priv->flags & ALPS_DUALPOINT)) {
1052 psmouse_warn(psmouse,
1053 "Rejected trackstick packet from non DualPoint device");
1054 return;
1055 }
1056
Yunkang Tang38088432014-07-26 13:51:41 -07001057 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1058 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1059 ((packet[3] & 0x20) << 1);
1060 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1061
1062 left = (packet[1] & 0x01);
1063 right = (packet[1] & 0x02) >> 1;
1064 middle = (packet[1] & 0x04) >> 2;
1065
1066 /* Divide 2 since trackpoint's speed is too fast */
1067 input_report_rel(dev2, REL_X, (char)x / 2);
1068 input_report_rel(dev2, REL_Y, -((char)y / 2));
1069
1070 input_report_key(dev2, BTN_LEFT, left);
1071 input_report_key(dev2, BTN_RIGHT, right);
1072 input_report_key(dev2, BTN_MIDDLE, middle);
1073
1074 input_sync(dev2);
1075}
1076
1077static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1078{
1079 struct alps_data *priv = psmouse->private;
1080 struct input_dev *dev = psmouse->dev;
1081 struct alps_fields *f = &priv->f;
1082
1083 memset(f, 0, sizeof(*f));
1084
1085 if (priv->decode_fields(f, psmouse->packet, psmouse))
1086 return;
1087
1088 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1089
1090 input_mt_report_finger_count(dev, f->fingers);
1091
1092 input_report_key(dev, BTN_LEFT, f->left);
1093 input_report_key(dev, BTN_RIGHT, f->right);
1094 input_report_key(dev, BTN_MIDDLE, f->middle);
1095
1096 input_sync(dev);
1097}
1098
1099static void alps_process_packet_v7(struct psmouse *psmouse)
1100{
1101 unsigned char *packet = psmouse->packet;
1102
1103 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1104 alps_process_trackstick_packet_v7(psmouse);
1105 else
1106 alps_process_touchpad_packet_v7(psmouse);
1107}
1108
Fengguang Wu07f19e32015-04-10 23:54:31 -07001109static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001110{
1111 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1112
1113 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1114 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
1115 pkt_id = SS4_PACKET_ID_IDLE;
1116 } else if (!(byte[3] & 0x10)) {
1117 pkt_id = SS4_PACKET_ID_ONE;
1118 } else if (!(byte[3] & 0x20)) {
1119 pkt_id = SS4_PACKET_ID_TWO;
1120 } else {
1121 pkt_id = SS4_PACKET_ID_MULTI;
1122 }
1123
1124 return pkt_id;
1125}
1126
1127static int alps_decode_ss4_v2(struct alps_fields *f,
1128 unsigned char *p, struct psmouse *psmouse)
1129{
1130 struct alps_data *priv = psmouse->private;
1131 unsigned char pkt_id;
1132 unsigned int no_data_x, no_data_y;
1133
1134 pkt_id = alps_get_pkt_id_ss4_v2(p);
1135
1136 /* Current packet is 1Finger coordinate packet */
1137 switch (pkt_id) {
1138 case SS4_PACKET_ID_ONE:
1139 f->mt[0].x = SS4_1F_X_V2(p);
1140 f->mt[0].y = SS4_1F_Y_V2(p);
1141 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1142 f->fingers = 1;
1143 f->first_mp = 0;
1144 f->is_mp = 0;
1145 break;
1146
1147 case SS4_PACKET_ID_TWO:
1148 if (priv->flags & ALPS_BUTTONPAD) {
1149 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1150 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1151 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1152 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1153 } else {
1154 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1155 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1156 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1157 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1158 }
1159 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1160
1161 if (SS4_IS_MF_CONTINUE(p)) {
1162 f->first_mp = 1;
1163 } else {
1164 f->fingers = 2;
1165 f->first_mp = 0;
1166 }
1167 f->is_mp = 0;
1168
1169 break;
1170
1171 case SS4_PACKET_ID_MULTI:
1172 if (priv->flags & ALPS_BUTTONPAD) {
1173 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1174 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1175 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1176 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1177 no_data_x = SS4_MFPACKET_NO_AX_BL;
1178 no_data_y = SS4_MFPACKET_NO_AY_BL;
1179 } else {
1180 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1181 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1182 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1183 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1184 no_data_x = SS4_MFPACKET_NO_AX;
1185 no_data_y = SS4_MFPACKET_NO_AY;
1186 }
1187
1188 f->first_mp = 0;
1189 f->is_mp = 1;
1190
1191 if (SS4_IS_5F_DETECTED(p)) {
1192 f->fingers = 5;
1193 } else if (f->mt[3].x == no_data_x &&
1194 f->mt[3].y == no_data_y) {
1195 f->mt[3].x = 0;
1196 f->mt[3].y = 0;
1197 f->fingers = 3;
1198 } else {
1199 f->fingers = 4;
1200 }
1201 break;
1202
1203 case SS4_PACKET_ID_IDLE:
1204 default:
1205 memset(f, 0, sizeof(struct alps_fields));
1206 break;
1207 }
1208
1209 f->left = !!(SS4_BTN_V2(p) & 0x01);
1210 if (!(priv->flags & ALPS_BUTTONPAD)) {
1211 f->right = !!(SS4_BTN_V2(p) & 0x02);
1212 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1213 }
1214
1215 return 0;
1216}
1217
1218static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1219{
1220 struct alps_data *priv = psmouse->private;
1221 unsigned char *packet = psmouse->packet;
1222 struct input_dev *dev = psmouse->dev;
1223 struct alps_fields *f = &priv->f;
1224
1225 memset(f, 0, sizeof(struct alps_fields));
1226 priv->decode_fields(f, packet, psmouse);
1227 if (priv->multi_packet) {
1228 /*
1229 * Sometimes the first packet will indicate a multi-packet
1230 * sequence, but sometimes the next multi-packet would not
1231 * come. Check for this, and when it happens process the
1232 * position packet as usual.
1233 */
1234 if (f->is_mp) {
1235 /* Now process the 1st packet */
1236 priv->decode_fields(f, priv->multi_data, psmouse);
1237 } else {
1238 priv->multi_packet = 0;
1239 }
1240 }
1241
1242 /*
1243 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1244 * When it is set, it means 2nd packet comes without 1st packet come.
1245 */
1246 if (f->is_mp)
1247 return;
1248
1249 /* Save the first packet */
1250 if (!priv->multi_packet && f->first_mp) {
1251 priv->multi_packet = 1;
1252 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1253 return;
1254 }
1255
1256 priv->multi_packet = 0;
1257
1258 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1259
1260 input_mt_report_finger_count(dev, f->fingers);
1261
1262 input_report_key(dev, BTN_LEFT, f->left);
1263 input_report_key(dev, BTN_RIGHT, f->right);
1264 input_report_key(dev, BTN_MIDDLE, f->middle);
1265
1266 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1267 input_sync(dev);
1268}
1269
1270static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1271{
1272 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1273 return false;
1274 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1275 return false;
1276 return true;
1277}
1278
Pali Rohár04aae282015-01-14 13:18:30 -08001279static DEFINE_MUTEX(alps_mutex);
1280
1281static void alps_register_bare_ps2_mouse(struct work_struct *work)
1282{
1283 struct alps_data *priv =
1284 container_of(work, struct alps_data, dev3_register_work.work);
1285 struct psmouse *psmouse = priv->psmouse;
1286 struct input_dev *dev3;
1287 int error = 0;
1288
1289 mutex_lock(&alps_mutex);
1290
1291 if (priv->dev3)
1292 goto out;
1293
1294 dev3 = input_allocate_device();
1295 if (!dev3) {
1296 psmouse_err(psmouse, "failed to allocate secondary device\n");
1297 error = -ENOMEM;
1298 goto out;
1299 }
1300
1301 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1302 psmouse->ps2dev.serio->phys,
1303 (priv->dev2 ? "input2" : "input1"));
1304 dev3->phys = priv->phys3;
1305
1306 /*
1307 * format of input device name is: "protocol vendor name"
1308 * see function psmouse_switch_protocol() in psmouse-base.c
1309 */
1310 dev3->name = "PS/2 ALPS Mouse";
1311
1312 dev3->id.bustype = BUS_I8042;
1313 dev3->id.vendor = 0x0002;
1314 dev3->id.product = PSMOUSE_PS2;
1315 dev3->id.version = 0x0000;
1316 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1317
1318 input_set_capability(dev3, EV_REL, REL_X);
1319 input_set_capability(dev3, EV_REL, REL_Y);
1320 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1321 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1322 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1323
1324 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1325
1326 error = input_register_device(dev3);
1327 if (error) {
1328 psmouse_err(psmouse,
1329 "failed to register secondary device: %d\n",
1330 error);
1331 input_free_device(dev3);
1332 goto out;
1333 }
1334
1335 priv->dev3 = dev3;
1336
1337out:
1338 /*
1339 * Save the error code so that we can detect that we
1340 * already tried to create the device.
1341 */
1342 if (error)
1343 priv->dev3 = ERR_PTR(error);
1344
1345 mutex_unlock(&alps_mutex);
1346}
1347
Hans de Goede59c30af2015-04-03 17:14:40 -07001348static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001349 unsigned char packet[],
1350 bool report_buttons)
1351{
Hans de Goede59c30af2015-04-03 17:14:40 -07001352 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001353 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001354
Hans de Goedee3a79212015-04-03 17:20:05 -07001355 /* Figure out which device to use to report the bare packet */
1356 if (priv->proto_version == ALPS_PROTO_V2 &&
1357 (priv->flags & ALPS_DUALPOINT)) {
1358 /* On V2 devices the DualPoint Stick reports bare packets */
1359 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001360 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001361 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001362 /* Register dev3 mouse if we received PS/2 packet first time */
1363 if (!IS_ERR(priv->dev3))
1364 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1365 0);
1366 return;
1367 } else {
1368 dev = priv->dev3;
1369 }
1370
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001371 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001372 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001373 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1374
Pali Rohár04aae282015-01-14 13:18:30 -08001375 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001376 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001377 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001378 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1379
Pali Rohár04aae282015-01-14 13:18:30 -08001380 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001381}
1382
1383static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 struct alps_data *priv = psmouse->private;
1386
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001387 if (psmouse->pktcnt < 6)
1388 return PSMOUSE_GOOD_DATA;
1389
1390 if (psmouse->pktcnt == 6) {
1391 /*
1392 * Start a timer to flush the packet if it ends up last
1393 * 6-byte packet in the stream. Timer needs to fire
1394 * psmouse core times out itself. 20 ms should be enough
1395 * to decide if we are getting more data or not.
1396 */
1397 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1398 return PSMOUSE_GOOD_DATA;
1399 }
1400
1401 del_timer(&priv->timer);
1402
1403 if (psmouse->packet[6] & 0x80) {
1404
1405 /*
1406 * Highest bit is set - that means we either had
1407 * complete ALPS packet and this is start of the
1408 * next packet or we got garbage.
1409 */
1410
1411 if (((psmouse->packet[3] |
1412 psmouse->packet[4] |
1413 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001414 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001415 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001416 "refusing packet %4ph (suspected interleaved ps/2)\n",
1417 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001418 return PSMOUSE_BAD_DATA;
1419 }
1420
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001421 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001422
1423 /* Continue with the next packet */
1424 psmouse->packet[0] = psmouse->packet[6];
1425 psmouse->pktcnt = 1;
1426
1427 } else {
1428
1429 /*
1430 * High bit is 0 - that means that we indeed got a PS/2
1431 * packet in the middle of ALPS packet.
1432 *
1433 * There is also possibility that we got 6-byte ALPS
1434 * packet followed by 3-byte packet from trackpoint. We
1435 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001436 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001437 * normal operation (user would need to press all
1438 * buttons on the pad and start moving trackpoint
1439 * without touching the pad surface) we assume former.
1440 * Even if we are wrong the wost thing that would happen
1441 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001442 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001443 */
1444
Hans de Goede59c30af2015-04-03 17:14:40 -07001445 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1446 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001447
1448 /*
1449 * Continue with the standard ALPS protocol handling,
1450 * but make sure we won't process it as an interleaved
1451 * packet again, which may happen if all buttons are
1452 * pressed. To avoid this let's reset the 4th bit which
1453 * is normally 1.
1454 */
1455 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1456 psmouse->pktcnt = 4;
1457 }
1458
1459 return PSMOUSE_GOOD_DATA;
1460}
1461
1462static void alps_flush_packet(unsigned long data)
1463{
1464 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001465 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001466
1467 serio_pause_rx(psmouse->ps2dev.serio);
1468
Seth Forsheeb46615f2011-11-07 19:53:30 -08001469 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001470
1471 /*
1472 * We did not any more data in reasonable amount of time.
1473 * Validate the last 3 bytes and process as a standard
1474 * ALPS packet.
1475 */
1476 if ((psmouse->packet[3] |
1477 psmouse->packet[4] |
1478 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001479 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001480 "refusing packet %3ph (suspected interleaved ps/2)\n",
1481 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001482 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001483 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001484 }
1485 psmouse->pktcnt = 0;
1486 }
1487
1488 serio_continue_rx(psmouse->ps2dev.serio);
1489}
1490
1491static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1492{
1493 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001494
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001495 /*
1496 * Check if we are dealing with a bare PS/2 packet, presumably from
1497 * a device connected to the external PS/2 port. Because bare PS/2
1498 * protocol does not have enough constant bits to self-synchronize
1499 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001500 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001501 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001502 if (priv->proto_version != ALPS_PROTO_V8 &&
1503 !psmouse->out_of_sync_cnt &&
1504 (psmouse->packet[0] & 0xc8) == 0x08) {
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001507 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1508 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return PSMOUSE_FULL_PACKET;
1510 }
1511 return PSMOUSE_GOOD_DATA;
1512 }
1513
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001514 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1515
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001516 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001517 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1518 return alps_handle_interleaved_ps2(psmouse);
1519 }
1520
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001521 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001522 psmouse_dbg(psmouse,
1523 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001524 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Seth Forsheeb46615f2011-11-07 19:53:30 -08001528 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001529 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001530 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001531 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001532 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1533 psmouse->pktcnt - 1,
1534 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001535
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001536 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001537 psmouse->pktcnt == psmouse->pktsize) {
1538 /*
1539 * Some Dell boxes, such as Latitude E6440 or E7440
1540 * with closed lid, quite often smash last byte of
1541 * otherwise valid packet with 0xff. Given that the
1542 * next packet is very likely to be valid let's
1543 * report PSMOUSE_FULL_PACKET but not process data,
1544 * rather than reporting PSMOUSE_BAD_DATA and
1545 * filling the logs.
1546 */
1547 return PSMOUSE_FULL_PACKET;
1548 }
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001553 if ((priv->proto_version == ALPS_PROTO_V7 &&
1554 !alps_is_valid_package_v7(psmouse)) ||
1555 (priv->proto_version == ALPS_PROTO_V8 &&
1556 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001557 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1558 psmouse->pktcnt - 1,
1559 psmouse->packet[psmouse->pktcnt - 1]);
1560 return PSMOUSE_BAD_DATA;
1561 }
1562
Seth Forsheeb46615f2011-11-07 19:53:30 -08001563 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001564 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return PSMOUSE_FULL_PACKET;
1566 }
1567
1568 return PSMOUSE_GOOD_DATA;
1569}
1570
Seth Forshee25bded72011-11-07 19:53:36 -08001571static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1572{
1573 struct ps2dev *ps2dev = &psmouse->ps2dev;
1574 struct alps_data *priv = psmouse->private;
1575 int command;
1576 unsigned char *param;
1577 unsigned char dummy[4];
1578
1579 BUG_ON(nibble > 0xf);
1580
1581 command = priv->nibble_commands[nibble].command;
1582 param = (command & 0x0f00) ?
1583 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1584
1585 if (ps2_command(ps2dev, param, command))
1586 return -1;
1587
1588 return 0;
1589}
1590
1591static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1592{
1593 struct ps2dev *ps2dev = &psmouse->ps2dev;
1594 struct alps_data *priv = psmouse->private;
1595 int i, nibble;
1596
1597 if (ps2_command(ps2dev, NULL, priv->addr_command))
1598 return -1;
1599
1600 for (i = 12; i >= 0; i -= 4) {
1601 nibble = (addr >> i) & 0xf;
1602 if (alps_command_mode_send_nibble(psmouse, nibble))
1603 return -1;
1604 }
1605
1606 return 0;
1607}
1608
1609static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1610{
1611 struct ps2dev *ps2dev = &psmouse->ps2dev;
1612 unsigned char param[4];
1613
1614 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1615 return -1;
1616
1617 /*
1618 * The address being read is returned in the first two bytes
1619 * of the result. Check that this address matches the expected
1620 * address.
1621 */
1622 if (addr != ((param[0] << 8) | param[1]))
1623 return -1;
1624
1625 return param[2];
1626}
1627
1628static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1629{
1630 if (alps_command_mode_set_addr(psmouse, addr))
1631 return -1;
1632 return __alps_command_mode_read_reg(psmouse, addr);
1633}
1634
1635static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1636{
1637 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1638 return -1;
1639 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1640 return -1;
1641 return 0;
1642}
1643
1644static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1645 u8 value)
1646{
1647 if (alps_command_mode_set_addr(psmouse, addr))
1648 return -1;
1649 return __alps_command_mode_write_reg(psmouse, value);
1650}
1651
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001652static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1653 int repeated_command, unsigned char *param)
1654{
1655 struct ps2dev *ps2dev = &psmouse->ps2dev;
1656
1657 param[0] = 0;
1658 if (init_command && ps2_command(ps2dev, param, init_command))
1659 return -EIO;
1660
1661 if (ps2_command(ps2dev, NULL, repeated_command) ||
1662 ps2_command(ps2dev, NULL, repeated_command) ||
1663 ps2_command(ps2dev, NULL, repeated_command))
1664 return -EIO;
1665
1666 param[0] = param[1] = param[2] = 0xff;
1667 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1668 return -EIO;
1669
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001670 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1671 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001672 return 0;
1673}
1674
Yunkang Tang38088432014-07-26 13:51:41 -07001675static bool alps_check_valid_firmware_id(unsigned char id[])
1676{
1677 if (id[0] == 0x73)
1678 return true;
1679
1680 if (id[0] == 0x88 &&
1681 (id[1] == 0x07 ||
1682 id[1] == 0x08 ||
1683 (id[1] & 0xf0) == 0xb0 ||
1684 (id[1] & 0xf0) == 0xc0)) {
1685 return true;
1686 }
1687
1688 return false;
1689}
1690
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001691static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001692{
1693 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001694
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001695 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001696 psmouse_err(psmouse, "failed to enter command mode\n");
1697 return -1;
1698 }
1699
Yunkang Tang38088432014-07-26 13:51:41 -07001700 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001701 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001702 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001703 return -1;
1704 }
Seth Forshee25bded72011-11-07 19:53:36 -08001705 return 0;
1706}
1707
1708static inline int alps_exit_command_mode(struct psmouse *psmouse)
1709{
1710 struct ps2dev *ps2dev = &psmouse->ps2dev;
1711 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1712 return -1;
1713 return 0;
1714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716/*
1717 * For DualPoint devices select the device that should respond to
1718 * subsequent commands. It looks like glidepad is behind stickpointer,
1719 * I'd thought it would be other way around...
1720 */
Seth Forshee25bded72011-11-07 19:53:36 -08001721static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1725
1726 if (ps2_command(ps2dev, NULL, cmd) ||
1727 ps2_command(ps2dev, NULL, cmd) ||
1728 ps2_command(ps2dev, NULL, cmd) ||
1729 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1730 return -1;
1731
1732 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001733 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 return 0;
1736}
1737
Seth Forshee25bded72011-11-07 19:53:36 -08001738static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 struct ps2dev *ps2dev = &psmouse->ps2dev;
1741
1742 /* Try ALPS magic knock - 4 disable before enable */
1743 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1744 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1745 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1746 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1747 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1748 return -1;
1749
1750 /*
1751 * Switch mouse to poll (remote) mode so motion data will not
1752 * get in our way
1753 */
1754 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1755}
1756
Yunkang Tang95f75e92013-12-01 22:33:52 -08001757static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1758{
1759 int i, nibble;
1760
1761 /*
1762 * b0-b11 are valid bits, send sequence is inverse.
1763 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1764 */
1765 for (i = 0; i <= 8; i += 4) {
1766 nibble = (word >> i) & 0xf;
1767 if (alps_command_mode_send_nibble(psmouse, nibble))
1768 return -1;
1769 }
1770
1771 return 0;
1772}
1773
1774static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1775 u16 addr, u16 value)
1776{
1777 struct ps2dev *ps2dev = &psmouse->ps2dev;
1778
1779 /* 0x0A0 is the command to write the word */
1780 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1781 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1782 alps_monitor_mode_send_word(psmouse, addr) ||
1783 alps_monitor_mode_send_word(psmouse, value) ||
1784 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1785 return -1;
1786
1787 return 0;
1788}
1789
1790static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1791{
1792 struct ps2dev *ps2dev = &psmouse->ps2dev;
1793
1794 if (enable) {
1795 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1796 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1797 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1798 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1799 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1800 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1801 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1802 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1803 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1804 return -1;
1805 } else {
1806 /* EC to exit monitor mode */
1807 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1808 return -1;
1809 }
1810
1811 return 0;
1812}
1813
1814static int alps_absolute_mode_v6(struct psmouse *psmouse)
1815{
1816 u16 reg_val = 0x181;
1817 int ret = -1;
1818
1819 /* enter monitor mode, to write the register */
1820 if (alps_monitor_mode(psmouse, true))
1821 return -1;
1822
1823 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1824
1825 if (alps_monitor_mode(psmouse, false))
1826 ret = -1;
1827
1828 return ret;
1829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831static int alps_get_status(struct psmouse *psmouse, char *param)
1832{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001834 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return -1;
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return 0;
1838}
1839
1840/*
1841 * Turn touchpad tapping on or off. The sequences are:
1842 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1843 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1844 * My guess that 0xE9 (GetInfo) is here as a sync point.
1845 * For models that also have stickpointer (DualPoints) its tapping
1846 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1847 * we don't fiddle with it.
1848 */
1849static int alps_tap_mode(struct psmouse *psmouse, int enable)
1850{
1851 struct ps2dev *ps2dev = &psmouse->ps2dev;
1852 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1853 unsigned char tap_arg = enable ? 0x0A : 0x00;
1854 unsigned char param[4];
1855
1856 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1857 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1858 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1859 ps2_command(ps2dev, &tap_arg, cmd))
1860 return -1;
1861
1862 if (alps_get_status(psmouse, param))
1863 return -1;
1864
1865 return 0;
1866}
1867
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001868/*
1869 * alps_poll() - poll the touchpad for current motion packet.
1870 * Used in resync.
1871 */
1872static int alps_poll(struct psmouse *psmouse)
1873{
1874 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001875 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001876 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001877
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001878 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001879 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001880
1881 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1882 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1883
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001884 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001885 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001886
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001887 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001888 return -1;
1889
1890 if ((psmouse->badbyte & 0xc8) == 0x08) {
1891/*
1892 * Poll the track stick ...
1893 */
1894 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1895 return -1;
1896 }
1897
1898 memcpy(psmouse->packet, buf, sizeof(buf));
1899 return 0;
1900}
1901
Seth Forshee25bded72011-11-07 19:53:36 -08001902static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001906 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001907 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001911 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001912 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Seth Forshee25bded72011-11-07 19:53:36 -08001916 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001917 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return -1;
1919 }
1920
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001921 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001922 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001926 /* ALPS needs stream mode, otherwise it won't report any data */
1927 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001928 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001929 return -1;
1930 }
1931
1932 return 0;
1933}
1934
Yunkang Tang95f75e92013-12-01 22:33:52 -08001935static int alps_hw_init_v6(struct psmouse *psmouse)
1936{
1937 unsigned char param[2] = {0xC8, 0x14};
1938
1939 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1940 if (alps_passthrough_mode_v2(psmouse, true))
1941 return -1;
1942
1943 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1944 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1945 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1946 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1947 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1948 return -1;
1949
1950 if (alps_passthrough_mode_v2(psmouse, false))
1951 return -1;
1952
1953 if (alps_absolute_mode_v6(psmouse)) {
1954 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1955 return -1;
1956 }
1957
1958 return 0;
1959}
1960
Seth Forshee25bded72011-11-07 19:53:36 -08001961/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001962 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001963 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001964static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1965 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001966{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001967 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001968
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001969 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001970 return -1;
1971
Kevin Cernekeecd401202013-02-13 22:28:07 -08001972 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1973 if (reg_val == -1)
1974 goto error;
1975
Seth Forshee25bded72011-11-07 19:53:36 -08001976 if (enable)
1977 reg_val |= 0x01;
1978 else
1979 reg_val &= ~0x01;
1980
Kevin Cernekeecd401202013-02-13 22:28:07 -08001981 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001982
Kevin Cernekeecd401202013-02-13 22:28:07 -08001983error:
1984 if (alps_exit_command_mode(psmouse))
1985 ret = -1;
1986 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001987}
1988
1989/* Must be in command mode when calling this function */
1990static int alps_absolute_mode_v3(struct psmouse *psmouse)
1991{
1992 int reg_val;
1993
1994 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1995 if (reg_val == -1)
1996 return -1;
1997
1998 reg_val |= 0x06;
1999 if (__alps_command_mode_write_reg(psmouse, reg_val))
2000 return -1;
2001
2002 return 0;
2003}
2004
Kevin Cernekeecd401202013-02-13 22:28:07 -08002005static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
2006{
2007 int ret = -EIO, reg_val;
2008
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002009 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002010 goto error;
2011
2012 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2013 if (reg_val == -1)
2014 goto error;
2015
2016 /* bit 7: trackstick is present */
2017 ret = reg_val & 0x80 ? 0 : -ENODEV;
2018
2019error:
2020 alps_exit_command_mode(psmouse);
2021 return ret;
2022}
2023
2024static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2025{
2026 struct ps2dev *ps2dev = &psmouse->ps2dev;
2027 int ret = 0;
2028 unsigned char param[4];
2029
2030 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2031 return -EIO;
2032
2033 /*
2034 * E7 report for the trackstick
2035 *
2036 * There have been reports of failures to seem to trace back
2037 * to the above trackstick check failing. When these occur
2038 * this E7 report fails, so when that happens we continue
2039 * with the assumption that there isn't a trackstick after
2040 * all.
2041 */
2042 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002043 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002044 ret = -ENODEV;
2045 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002046 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002047
2048 /*
2049 * Not sure what this does, but it is absolutely
2050 * essential. Without it, the touchpad does not
2051 * work at all and the trackstick just emits normal
2052 * PS/2 packets.
2053 */
2054 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2055 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2056 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2057 alps_command_mode_send_nibble(psmouse, 0x9) ||
2058 alps_command_mode_send_nibble(psmouse, 0x4)) {
2059 psmouse_err(psmouse,
2060 "Error sending magic E6 sequence\n");
2061 ret = -EIO;
2062 goto error;
2063 }
2064
2065 /*
2066 * This ensures the trackstick packets are in the format
2067 * supported by this driver. If bit 1 isn't set the packet
2068 * format is different.
2069 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002070 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002071 alps_command_mode_write_reg(psmouse,
2072 reg_base + 0x08, 0x82) ||
2073 alps_exit_command_mode(psmouse))
2074 ret = -EIO;
2075 }
2076
2077error:
2078 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2079 ret = -EIO;
2080
2081 return ret;
2082}
2083
Seth Forshee25bded72011-11-07 19:53:36 -08002084static int alps_hw_init_v3(struct psmouse *psmouse)
2085{
Seth Forshee25bded72011-11-07 19:53:36 -08002086 struct ps2dev *ps2dev = &psmouse->ps2dev;
2087 int reg_val;
2088 unsigned char param[4];
2089
Kevin Cernekeecd401202013-02-13 22:28:07 -08002090 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2091 if (reg_val == -EIO)
2092 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002093
Kevin Cernekeecd401202013-02-13 22:28:07 -08002094 if (reg_val == 0 &&
2095 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002096 goto error;
2097
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002098 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002099 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002100 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2101 goto error;
2102 }
2103
2104 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2105 if (reg_val == -1)
2106 goto error;
2107 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2108 goto error;
2109
2110 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2111 if (reg_val == -1)
2112 goto error;
2113 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2114 goto error;
2115
2116 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2117 goto error;
2118 if (__alps_command_mode_write_reg(psmouse, 0x04))
2119 goto error;
2120
2121 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2122 goto error;
2123 if (__alps_command_mode_write_reg(psmouse, 0x03))
2124 goto error;
2125
2126 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2127 goto error;
2128 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2129 goto error;
2130
2131 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2132 goto error;
2133 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2134 goto error;
2135
Seth Forshee25bded72011-11-07 19:53:36 -08002136 alps_exit_command_mode(psmouse);
2137
2138 /* Set rate and enable data reporting */
2139 param[0] = 0x64;
2140 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2141 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2142 psmouse_err(psmouse, "Failed to enable data reporting\n");
2143 return -1;
2144 }
2145
2146 return 0;
2147
Seth Forshee25bded72011-11-07 19:53:36 -08002148error:
2149 /*
2150 * Leaving the touchpad in command mode will essentially render
2151 * it unusable until the machine reboots, so exit it here just
2152 * to be safe
2153 */
2154 alps_exit_command_mode(psmouse);
2155 return -1;
2156}
2157
Hans de Goedef3f33c62014-07-29 11:22:07 -07002158static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2159{
2160 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2161 struct alps_data *priv = psmouse->private;
2162
2163 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2164 if (reg < 0)
2165 return reg;
2166
2167 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2168 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2169
2170 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2171 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2172
2173 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2174 if (reg < 0)
2175 return reg;
2176
2177 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2178 x_electrode = 17 + x_electrode;
2179
2180 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2181 y_electrode = 13 + y_electrode;
2182
2183 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2184 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2185
2186 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2187 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2188
2189 psmouse_dbg(psmouse,
2190 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2191 x_pitch, y_pitch, x_electrode, y_electrode,
2192 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2193
2194 return 0;
2195}
2196
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002197static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2198{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002199 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002200 struct ps2dev *ps2dev = &psmouse->ps2dev;
2201 int reg_val, ret = -1;
2202
Kevin Cernekeecd401202013-02-13 22:28:07 -08002203 if (priv->flags & ALPS_DUALPOINT) {
2204 reg_val = alps_setup_trackstick_v3(psmouse,
2205 ALPS_REG_BASE_RUSHMORE);
2206 if (reg_val == -EIO)
2207 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002208 }
2209
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002210 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002211 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2212 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2213 goto error;
2214
Hans de Goedef3f33c62014-07-29 11:22:07 -07002215 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2216 goto error;
2217
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002218 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2219 if (reg_val == -1)
2220 goto error;
2221 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2222 goto error;
2223
2224 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2225 goto error;
2226
2227 /* enter absolute mode */
2228 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2229 if (reg_val == -1)
2230 goto error;
2231 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2232 goto error;
2233
2234 alps_exit_command_mode(psmouse);
2235 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2236
2237error:
2238 alps_exit_command_mode(psmouse);
2239 return ret;
2240}
2241
Seth Forshee25bded72011-11-07 19:53:36 -08002242/* Must be in command mode when calling this function */
2243static int alps_absolute_mode_v4(struct psmouse *psmouse)
2244{
2245 int reg_val;
2246
2247 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2248 if (reg_val == -1)
2249 return -1;
2250
2251 reg_val |= 0x02;
2252 if (__alps_command_mode_write_reg(psmouse, reg_val))
2253 return -1;
2254
2255 return 0;
2256}
2257
2258static int alps_hw_init_v4(struct psmouse *psmouse)
2259{
Seth Forshee25bded72011-11-07 19:53:36 -08002260 struct ps2dev *ps2dev = &psmouse->ps2dev;
2261 unsigned char param[4];
2262
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002263 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002264 goto error;
2265
2266 if (alps_absolute_mode_v4(psmouse)) {
2267 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2268 goto error;
2269 }
2270
2271 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2272 goto error;
2273
2274 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2275 goto error;
2276
2277 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2278 goto error;
2279
2280 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2281 goto error;
2282
2283 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2284 goto error;
2285
2286 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2287 goto error;
2288
2289 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2290 goto error;
2291
2292 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2293 goto error;
2294
2295 alps_exit_command_mode(psmouse);
2296
2297 /*
2298 * This sequence changes the output from a 9-byte to an
2299 * 8-byte format. All the same data seems to be present,
2300 * just in a more compact format.
2301 */
2302 param[0] = 0xc8;
2303 param[1] = 0x64;
2304 param[2] = 0x50;
2305 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2306 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2307 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2308 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2309 return -1;
2310
2311 /* Set rate and enable data reporting */
2312 param[0] = 0x64;
2313 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2314 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2315 psmouse_err(psmouse, "Failed to enable data reporting\n");
2316 return -1;
2317 }
2318
2319 return 0;
2320
2321error:
2322 /*
2323 * Leaving the touchpad in command mode will essentially render
2324 * it unusable until the machine reboots, so exit it here just
2325 * to be safe
2326 */
2327 alps_exit_command_mode(psmouse);
2328 return -1;
2329}
2330
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002331static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2332 unsigned char index, unsigned char otp[])
2333{
2334 struct ps2dev *ps2dev = &psmouse->ps2dev;
2335
2336 switch (index) {
2337 case 0:
2338 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2339 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2340 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2341 return -1;
2342
2343 break;
2344
2345 case 1:
2346 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2347 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2348 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2349 return -1;
2350
2351 break;
2352 }
2353
2354 return 0;
2355}
2356
Fengguang Wu07f19e32015-04-10 23:54:31 -07002357static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2358 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002359{
2360 int num_x_electrode;
2361 int num_y_electrode;
2362 int x_pitch, y_pitch, x_phys, y_phys;
2363
2364 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2365 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2366
2367 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2368 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2369
2370 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2371 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2372
2373 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2374 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2375
2376 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2377 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2378
2379 return 0;
2380}
2381
Fengguang Wu07f19e32015-04-10 23:54:31 -07002382static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2383 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002384{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002385 unsigned char is_btnless;
2386
2387 is_btnless = (otp[1][1] >> 3) & 0x01;
2388
2389 if (is_btnless)
2390 priv->flags |= ALPS_BUTTONPAD;
2391
2392 return 0;
2393}
2394
2395static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2396 struct alps_data *priv)
2397{
2398 unsigned char otp[2][4];
2399
2400 memset(otp, 0, sizeof(otp));
2401
2402 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2403 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2404 return -1;
2405
2406 alps_update_device_area_ss4_v2(otp, priv);
2407
2408 alps_update_btn_info_ss4_v2(otp, priv);
2409
2410 return 0;
2411}
2412
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002413static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2414 struct alps_data *priv)
2415{
2416 struct ps2dev *ps2dev = &psmouse->ps2dev;
2417 unsigned char param[4] = {0};
2418 int num_x_electrode, num_y_electrode;
2419
2420 if (alps_enter_command_mode(psmouse))
2421 return -1;
2422
2423 param[0] = 0x0a;
2424 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2425 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2426 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2427 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2428 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2429 return -1;
2430
2431 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2432 return -1;
2433
2434 /*
2435 * Dolphin's sensor line number is not fixed. It can be calculated
2436 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2437 * Further more, we can get device's x_max and y_max by multiplying
2438 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2439 *
2440 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2441 * real sensor line number X = 11 + 8 = 19, and
2442 * real sensor line number Y = 8 + 1 = 9.
2443 * So, x_max = (19 - 1) * 64 = 1152, and
2444 * y_max = (9 - 1) * 64 = 512.
2445 */
2446 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2447 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2448 priv->x_bits = num_x_electrode;
2449 priv->y_bits = num_y_electrode;
2450 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2451 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2452
2453 if (alps_exit_command_mode(psmouse))
2454 return -1;
2455
2456 return 0;
2457}
2458
Dave Turvene75af9e52013-02-21 22:58:28 -08002459static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2460{
2461 struct ps2dev *ps2dev = &psmouse->ps2dev;
2462 unsigned char param[2];
2463
2464 /* This is dolphin "v1" as empirically defined by florin9doi */
2465 param[0] = 0x64;
2466 param[1] = 0x28;
2467
2468 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2469 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2470 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2471 return -1;
2472
2473 return 0;
2474}
2475
Yunkang Tang38088432014-07-26 13:51:41 -07002476static int alps_hw_init_v7(struct psmouse *psmouse)
2477{
2478 struct ps2dev *ps2dev = &psmouse->ps2dev;
2479 int reg_val, ret = -1;
2480
2481 if (alps_enter_command_mode(psmouse) ||
2482 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2483 goto error;
2484
Hans de Goedef3f33c62014-07-29 11:22:07 -07002485 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2486 goto error;
2487
Yunkang Tang38088432014-07-26 13:51:41 -07002488 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2489 goto error;
2490
2491 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2492 if (reg_val == -1)
2493 goto error;
2494 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2495 goto error;
2496
2497 alps_exit_command_mode(psmouse);
2498 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2499
2500error:
2501 alps_exit_command_mode(psmouse);
2502 return ret;
2503}
2504
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002505static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2506{
2507 struct ps2dev *ps2dev = &psmouse->ps2dev;
2508 char param[2] = {0x64, 0x28};
2509 int ret = -1;
2510
2511 /* enter absolute mode */
2512 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2513 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2514 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2515 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2516 goto error;
2517 }
2518
2519 /* T.B.D. Decread noise packet number, delete in the future */
2520 if (alps_exit_command_mode(psmouse) ||
2521 alps_enter_command_mode(psmouse) ||
2522 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2523 goto error;
2524 }
2525 alps_exit_command_mode(psmouse);
2526
2527 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2528
2529error:
2530 alps_exit_command_mode(psmouse);
2531 return ret;
2532}
2533
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002534static int alps_set_protocol(struct psmouse *psmouse,
2535 struct alps_data *priv,
2536 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002537{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002538 psmouse->private = priv;
2539
2540 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2541
2542 priv->proto_version = protocol->version;
2543 priv->byte0 = protocol->byte0;
2544 priv->mask0 = protocol->mask0;
2545 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002546
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002547 priv->x_max = 2000;
2548 priv->y_max = 1400;
2549 priv->x_bits = 15;
2550 priv->y_bits = 11;
2551
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002552 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002553 case ALPS_PROTO_V1:
2554 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002555 priv->hw_init = alps_hw_init_v1_v2;
2556 priv->process_packet = alps_process_packet_v1_v2;
2557 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002558 priv->x_max = 1023;
2559 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002560 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002561
Seth Forshee25bded72011-11-07 19:53:36 -08002562 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002563 priv->hw_init = alps_hw_init_v3;
2564 priv->process_packet = alps_process_packet_v3;
2565 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002566 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002567 priv->nibble_commands = alps_v3_nibble_commands;
2568 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002569 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002570
2571 case ALPS_PROTO_V3_RUSHMORE:
2572 priv->hw_init = alps_hw_init_rushmore_v3;
2573 priv->process_packet = alps_process_packet_v3;
2574 priv->set_abs_params = alps_set_abs_params_mt;
2575 priv->decode_fields = alps_decode_rushmore;
2576 priv->nibble_commands = alps_v3_nibble_commands;
2577 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2578 priv->x_bits = 16;
2579 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002580
2581 if (alps_probe_trackstick_v3(psmouse,
2582 ALPS_REG_BASE_RUSHMORE) < 0)
2583 priv->flags &= ~ALPS_DUALPOINT;
2584
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002585 break;
2586
Seth Forshee25bded72011-11-07 19:53:36 -08002587 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002588 priv->hw_init = alps_hw_init_v4;
2589 priv->process_packet = alps_process_packet_v4;
2590 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002591 priv->nibble_commands = alps_v4_nibble_commands;
2592 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002593 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002594
Dave Turvene75af9e52013-02-21 22:58:28 -08002595 case ALPS_PROTO_V5:
2596 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002597 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002598 priv->decode_fields = alps_decode_dolphin;
2599 priv->set_abs_params = alps_set_abs_params_mt;
2600 priv->nibble_commands = alps_v3_nibble_commands;
2601 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002602 priv->x_bits = 23;
2603 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002604
2605 if (alps_dolphin_get_device_area(psmouse, priv))
2606 return -EIO;
2607
Dave Turvene75af9e52013-02-21 22:58:28 -08002608 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002609
Yunkang Tang95f75e92013-12-01 22:33:52 -08002610 case ALPS_PROTO_V6:
2611 priv->hw_init = alps_hw_init_v6;
2612 priv->process_packet = alps_process_packet_v6;
2613 priv->set_abs_params = alps_set_abs_params_st;
2614 priv->nibble_commands = alps_v6_nibble_commands;
2615 priv->x_max = 2047;
2616 priv->y_max = 1535;
2617 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002618
Yunkang Tang38088432014-07-26 13:51:41 -07002619 case ALPS_PROTO_V7:
2620 priv->hw_init = alps_hw_init_v7;
2621 priv->process_packet = alps_process_packet_v7;
2622 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002623 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002624 priv->nibble_commands = alps_v3_nibble_commands;
2625 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002626 priv->x_max = 0xfff;
2627 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002628
2629 if (priv->fw_ver[1] != 0xba)
2630 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002631
Yunkang Tang38088432014-07-26 13:51:41 -07002632 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002633
2634 case ALPS_PROTO_V8:
2635 priv->hw_init = alps_hw_init_ss4_v2;
2636 priv->process_packet = alps_process_packet_ss4_v2;
2637 priv->decode_fields = alps_decode_ss4_v2;
2638 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2639 priv->nibble_commands = alps_v3_nibble_commands;
2640 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2641
2642 if (alps_set_defaults_ss4_v2(psmouse, priv))
2643 return -EIO;
2644
2645 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002646 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002647
2648 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002649}
2650
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002651static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2652 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002653{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002654 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002655 int i;
2656
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002657 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2658 model = &alps_model_data[i];
2659
2660 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2661 (!model->command_mode_resp ||
2662 model->command_mode_resp == ec[2])) {
2663
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002664 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002665 }
2666 }
2667
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002668 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002669}
2670
2671static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2672{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002673 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002674 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002675 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002676
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002677 /*
2678 * First try "E6 report".
2679 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2680 * The bits 0-2 of the first byte will be 1s if some buttons are
2681 * pressed.
2682 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002683 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2684 PSMOUSE_CMD_SETSCALE11, e6))
2685 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002686
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002687 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2688 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002689
2690 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002691 * Now get the "E7" and "EC" reports. These will uniquely identify
2692 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002693 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002694 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2695 PSMOUSE_CMD_SETSCALE21, e7) ||
2696 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2697 PSMOUSE_CMD_RESET_WRAP, ec) ||
2698 alps_exit_command_mode(psmouse))
2699 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002700
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002701 protocol = alps_match_table(e7, ec);
2702 if (!protocol) {
2703 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2704 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2705 protocol = &alps_v5_protocol_data;
2706 } else if (ec[0] == 0x88 &&
2707 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2708 protocol = &alps_v7_protocol_data;
2709 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2710 protocol = &alps_v3_rushmore_data;
2711 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2712 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2713 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002714 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2715 e7[2] == 0x14 && ec[1] == 0x02) {
2716 protocol = &alps_v8_protocol_data;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002717 } else {
2718 psmouse_dbg(psmouse,
2719 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2720 return -EINVAL;
2721 }
2722 }
2723
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002724 if (priv) {
2725 /* Save the Firmware version */
2726 memcpy(priv->fw_ver, ec, 3);
2727 error = alps_set_protocol(psmouse, priv, protocol);
2728 if (error)
2729 return error;
2730 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002731
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002732 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002733}
2734
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002735static int alps_reconnect(struct psmouse *psmouse)
2736{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002737 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002738
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002739 psmouse_reset(psmouse);
2740
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002741 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002742 return -1;
2743
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002744 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
2747static void alps_disconnect(struct psmouse *psmouse)
2748{
2749 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002752 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002753 if (priv->dev2)
2754 input_unregister_device(priv->dev2);
2755 if (!IS_ERR_OR_NULL(priv->dev3))
2756 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 kfree(priv);
2758}
2759
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002760static void alps_set_abs_params_st(struct alps_data *priv,
2761 struct input_dev *dev1)
2762{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002763 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2764 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002765 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002766}
2767
Masaki Ota8eccd392015-03-27 20:44:21 -07002768static void alps_set_abs_params_mt_common(struct alps_data *priv,
2769 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002770{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002771 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2772 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002773
Hans de Goedef3f33c62014-07-29 11:22:07 -07002774 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2775 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2776
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002777 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2778 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002779}
Yunkang Tang38088432014-07-26 13:51:41 -07002780
Masaki Ota8eccd392015-03-27 20:44:21 -07002781static void alps_set_abs_params_mt(struct alps_data *priv,
2782 struct input_dev *dev1)
2783{
2784 alps_set_abs_params_mt_common(priv, dev1);
2785 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2786
2787 input_mt_init_slots(dev1, MAX_TOUCHES,
2788 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2789 INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2790}
2791
2792static void alps_set_abs_params_v7(struct alps_data *priv,
2793 struct input_dev *dev1)
2794{
2795 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002796 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002797
2798 input_mt_init_slots(dev1, MAX_TOUCHES,
2799 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2800 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002801
2802 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2803}
2804
2805static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2806 struct input_dev *dev1)
2807{
2808 alps_set_abs_params_mt_common(priv, dev1);
2809 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2810 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2811
2812 input_mt_init_slots(dev1, MAX_TOUCHES,
2813 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2814 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002815}
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817int alps_init(struct psmouse *psmouse)
2818{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002819 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002820 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002821 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002823 error = priv->hw_init(psmouse);
2824 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 goto init_fail;
2826
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002827 /*
2828 * Undo part of setup done for us by psmouse core since touchpad
2829 * is not a relative device.
2830 */
2831 __clear_bit(EV_REL, dev1->evbit);
2832 __clear_bit(REL_X, dev1->relbit);
2833 __clear_bit(REL_Y, dev1->relbit);
2834
2835 /*
2836 * Now set up our capabilities.
2837 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002838 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2839 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2840 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002841 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2842 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002844 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002845
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002846 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002848 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002849 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2850 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 }
2852
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002853 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002854 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2855 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
2857
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002858 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002859 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2860 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2861 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2862 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002863 } else if (priv->flags & ALPS_BUTTONPAD) {
2864 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2865 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002866 } else {
2867 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2868 }
2869
Pali Rohárdfba8602015-01-09 12:48:44 -08002870 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002871 struct input_dev *dev2;
2872
2873 dev2 = input_allocate_device();
2874 if (!dev2) {
2875 psmouse_err(psmouse,
2876 "failed to allocate trackstick device\n");
2877 error = -ENOMEM;
2878 goto init_fail;
2879 }
2880
2881 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2882 psmouse->ps2dev.serio->phys);
2883 dev2->phys = priv->phys2;
2884
Pali Rohárdfba8602015-01-09 12:48:44 -08002885 /*
2886 * format of input device name is: "protocol vendor name"
2887 * see function psmouse_switch_protocol() in psmouse-base.c
2888 */
2889 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002890
2891 dev2->id.bustype = BUS_I8042;
2892 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002893 dev2->id.product = PSMOUSE_ALPS;
2894 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002895 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002896
Pali Rohár04aae282015-01-14 13:18:30 -08002897 input_set_capability(dev2, EV_REL, REL_X);
2898 input_set_capability(dev2, EV_REL, REL_Y);
2899 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2900 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2901 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002902
Pali Rohár04aae282015-01-14 13:18:30 -08002903 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002904 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2905
Pali Rohár04aae282015-01-14 13:18:30 -08002906 error = input_register_device(dev2);
2907 if (error) {
2908 psmouse_err(psmouse,
2909 "failed to register trackstick device: %d\n",
2910 error);
2911 input_free_device(dev2);
2912 goto init_fail;
2913 }
2914
2915 priv->dev2 = dev2;
2916 }
2917
2918 priv->psmouse = psmouse;
2919
2920 INIT_DELAYED_WORK(&priv->dev3_register_work,
2921 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002924 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 psmouse->disconnect = alps_disconnect;
2926 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002927 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002929 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2930 psmouse->resync_time = 0;
2931
Pali Rohár9d720b32014-11-08 12:58:57 -08002932 /* Allow 2 invalid packets without resetting device */
2933 psmouse->resetafter = psmouse->pktsize * 2;
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return 0;
2936
2937init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002938 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002939 /*
2940 * Even though we did not allocate psmouse->private we do free
2941 * it here.
2942 */
2943 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002944 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002945 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002948int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002950 struct alps_data *priv;
2951 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002953 error = alps_identify(psmouse, NULL);
2954 if (error)
2955 return error;
2956
2957 /*
2958 * Reset the device to make sure it is fully operational:
2959 * on some laptops, like certain Dell Latitudes, we may
2960 * fail to properly detect presence of trackstick if device
2961 * has not been reset.
2962 */
2963 psmouse_reset(psmouse);
2964
2965 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2966 if (!priv)
2967 return -ENOMEM;
2968
2969 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08002970 if (error) {
2971 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002972 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08002973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 if (set_properties) {
2976 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002977 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002978 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002979 psmouse->model = priv->proto_version;
2980 } else {
2981 /*
2982 * Destroy alps_data structure we allocated earlier since
2983 * this was just a "trial run". Otherwise we'll keep it
2984 * to be used by alps_init() which has to be called if
2985 * we succeed and set_properties is true.
2986 */
2987 kfree(priv);
2988 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return 0;
2992}
2993