blob: f4ba73b81d31a04d7a325c7ca39819be379e70be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
23
24#include "psmouse.h"
25#include "alps.h"
26
Seth Forshee25bded72011-11-07 19:53:36 -080027/*
28 * Definitions for ALPS version 3 and 4 command mode protocol
29 */
30#define ALPS_CMD_NIBBLE_10 0x01f2
31
Kevin Cernekeecd401202013-02-13 22:28:07 -080032#define ALPS_REG_BASE_RUSHMORE 0xc2c0
33#define ALPS_REG_BASE_PINNACLE 0x0000
34
Seth Forshee25bded72011-11-07 19:53:36 -080035static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
37 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
38 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
39 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
40 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
41 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
42 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
43 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
44 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
45 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
46 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
47 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
48 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
49 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
50 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
51 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
52};
53
54static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
56 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
57 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
58 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
59 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
60 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
61 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
62 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
63 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
64 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
65 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
66 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
67 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
68 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
69 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
70 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
71};
72
Yunkang Tang95f75e92013-12-01 22:33:52 -080073static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
75 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
76 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
77 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
78 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
79 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
80 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
81 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
82 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
83 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
84 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
85 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
86 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
87 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
88 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
89 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
90};
91
Seth Forshee25bded72011-11-07 19:53:36 -080092
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080093#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
94#define ALPS_PASS 0x04 /* device has a pass-through port */
95
96#define ALPS_WHEEL 0x08 /* hardware wheel present */
97#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
98#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
99#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800100#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
Yunkang Tang38088432014-07-26 13:51:41 -0700102#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Helge Dellere38de672006-09-10 21:54:39 -0400104static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800105 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
106 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
107 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
108 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
109 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
110 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
113 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
114 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
115 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
117 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
118 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800120
121 /*
122 * XXX This entry is suspicious. First byte has zero lower nibble,
123 * which is what a normal mouse would report. Also, the value 0x0e
124 * isn't valid per PS/2 spec.
125 */
126 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
127
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800128 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
129 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800130 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800131 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
132 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
133 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
134 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
135 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
137 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800140static const struct alps_protocol_info alps_v3_protocol_data = {
141 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
142};
143
144static const struct alps_protocol_info alps_v3_rushmore_data = {
145 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
146};
147
148static const struct alps_protocol_info alps_v5_protocol_data = {
149 ALPS_PROTO_V5, 0xc8, 0xd8, 0
150};
151
152static const struct alps_protocol_info alps_v7_protocol_data = {
153 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
154};
155
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700156static const struct alps_protocol_info alps_v8_protocol_data = {
157 ALPS_PROTO_V8, 0x18, 0x18, 0
158};
159
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800160static void alps_set_abs_params_st(struct alps_data *priv,
161 struct input_dev *dev1);
162static void alps_set_abs_params_mt(struct alps_data *priv,
163 struct input_dev *dev1);
Masaki Ota8eccd392015-03-27 20:44:21 -0700164static void alps_set_abs_params_v7(struct alps_data *priv,
165 struct input_dev *dev1);
Masaki Ota3db5b9f2015-03-27 20:57:52 -0700166static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
167 struct input_dev *dev1);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800168
Seth Forsheed4b347b2011-11-07 19:53:15 -0800169/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800171static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800172 unsigned char data)
173{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800174 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800175}
176
Pali Rohár04aae282015-01-14 13:18:30 -0800177static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800178 int left, int right, int middle)
179{
Martin Buckc91ed052010-03-13 22:23:58 -0800180 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800181
Martin Buckc91ed052010-03-13 22:23:58 -0800182 /*
183 * If shared button has already been reported on the
184 * other device (dev2) then this event should be also
185 * sent through that device.
186 */
Pali Rohár04aae282015-01-14 13:18:30 -0800187 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800188 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800189
Pali Rohár04aae282015-01-14 13:18:30 -0800190 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800191 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800192
Pali Rohár04aae282015-01-14 13:18:30 -0800193 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800194 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800195
Martin Buckc91ed052010-03-13 22:23:58 -0800196 /*
197 * Sync the _other_ device now, we'll do the first
198 * device later once we report the rest of the events.
199 */
Pali Rohár04aae282015-01-14 13:18:30 -0800200 if (dev2)
201 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800202}
203
Seth Forshee25bded72011-11-07 19:53:36 -0800204static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct alps_data *priv = psmouse->private;
207 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500208 struct input_dev *dev = psmouse->dev;
209 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500211 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800213 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400214 left = packet[2] & 0x10;
215 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 middle = 0;
217 x = packet[1] | ((packet[0] & 0x07) << 7);
218 y = packet[4] | ((packet[3] & 0x07) << 7);
219 z = packet[5];
220 } else {
221 left = packet[3] & 1;
222 right = packet[3] & 2;
223 middle = packet[3] & 4;
224 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
225 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
226 z = packet[5];
227 }
228
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800229 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400230 back = packet[0] & 0x10;
231 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500232 }
233
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800234 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500235 back = packet[3] & 4;
236 forward = packet[2] & 4;
237 if ((middle = forward && back))
238 forward = back = 0;
239 }
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 ges = packet[2] & 1;
242 fin = packet[2] & 2;
243
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800244 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
246 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700247
Pali Rohár04aae282015-01-14 13:18:30 -0800248 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 input_sync(dev2);
251 return;
252 }
253
Pali Rohár04aae282015-01-14 13:18:30 -0800254 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800257 if (ges && !fin)
258 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * A "tap and drag" operation is reported by the hardware as a transition
262 * from (!fin && ges) to (fin && ges). This should be translated to the
263 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
264 */
265 if (ges && fin && !priv->prev_fin) {
266 input_report_abs(dev, ABS_X, x);
267 input_report_abs(dev, ABS_Y, y);
268 input_report_abs(dev, ABS_PRESSURE, 0);
269 input_report_key(dev, BTN_TOOL_FINGER, 0);
270 input_sync(dev);
271 }
272 priv->prev_fin = fin;
273
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800274 if (z > 30)
275 input_report_key(dev, BTN_TOUCH, 1);
276 if (z < 25)
277 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (z > 0) {
280 input_report_abs(dev, ABS_X, x);
281 input_report_abs(dev, ABS_Y, y);
282 }
283
284 input_report_abs(dev, ABS_PRESSURE, z);
285 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
286
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800287 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500288 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800290 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500291 input_report_key(dev, BTN_FORWARD, forward);
292 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800295 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800296 input_report_key(dev, BTN_0, packet[2] & 4);
297 input_report_key(dev, BTN_1, packet[0] & 0x10);
298 input_report_key(dev, BTN_2, packet[3] & 4);
299 input_report_key(dev, BTN_3, packet[0] & 0x20);
300 }
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 input_sync(dev);
303}
304
Seth Forshee01ce6612011-11-07 19:54:13 -0800305/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800306 * Process bitmap data for V5 protocols. Return value is null.
307 *
308 * The bitmaps don't have enough data to track fingers, so this function
309 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700310 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800311 */
312static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700313 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800314{
315 int box_middle_x, box_middle_y;
316 unsigned int x_map, y_map;
317 unsigned char start_bit, end_bit;
318 unsigned char x_msb, x_lsb, y_msb, y_lsb;
319
320 x_map = fields->x_map;
321 y_map = fields->y_map;
322
323 if (!x_map || !y_map)
324 return;
325
326 /* Get Most-significant and Least-significant bit */
327 x_msb = fls(x_map);
328 x_lsb = ffs(x_map);
329 y_msb = fls(y_map);
330 y_lsb = ffs(y_map);
331
332 /* Most-significant bit should never exceed max sensor line number */
333 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
334 return;
335
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800336 if (fields->fingers > 1) {
337 start_bit = priv->x_bits - x_msb;
338 end_bit = priv->x_bits - x_lsb;
339 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
340 (2 * (priv->x_bits - 1));
341
342 start_bit = y_lsb - 1;
343 end_bit = y_msb - 1;
344 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
345 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700346 fields->mt[0] = fields->st;
347 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
348 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800349 }
350}
351
Hans de Goede036e6c72014-07-25 22:38:51 -0700352static void alps_get_bitmap_points(unsigned int map,
353 struct alps_bitmap_point *low,
354 struct alps_bitmap_point *high,
355 int *fingers)
356{
357 struct alps_bitmap_point *point;
358 int i, bit, prev_bit = 0;
359
360 point = low;
361 for (i = 0; map != 0; i++, map >>= 1) {
362 bit = map & 1;
363 if (bit) {
364 if (!prev_bit) {
365 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700366 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700367 (*fingers)++;
368 }
369 point->num_bits++;
370 } else {
371 if (prev_bit)
372 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700373 }
374 prev_bit = bit;
375 }
376}
377
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800378/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800379 * Process bitmap data from v3 and v4 protocols. Returns the number of
380 * fingers detected. A return value of 0 means at least one of the
381 * bitmaps was empty.
382 *
383 * The bitmaps don't have enough data to track fingers, so this function
384 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700385 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800386 * is greater than 0.
387 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800388static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700389 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800390{
Hans de Goede036e6c72014-07-25 22:38:51 -0700391 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800392 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
393 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800394
Hans de Goede02d04252014-07-25 22:43:35 -0700395 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800396 return 0;
397
Hans de Goede02d04252014-07-25 22:43:35 -0700398 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
399 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800400
401 /*
402 * Fingers can overlap, so we use the maximum count of fingers
403 * on either axis as the finger count.
404 */
405 fingers = max(fingers_x, fingers_y);
406
407 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700408 * If an axis reports only a single contact, we have overlapping or
409 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800410 */
Hans de Goede20bea682014-07-25 22:33:33 -0700411 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700412 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700413 x_low.num_bits = x_low.num_bits - i;
414 x_high.start_bit = x_low.start_bit + i;
415 x_high.num_bits = max(i, 1);
416 }
417 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700418 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700419 y_low.num_bits = y_low.num_bits - i;
420 y_high.start_bit = y_low.start_bit + i;
421 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800422 }
423
Hans de Goede02d04252014-07-25 22:43:35 -0700424 fields->mt[0].x =
425 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
426 (2 * (priv->x_bits - 1));
427 fields->mt[0].y =
428 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
429 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800430
Hans de Goede02d04252014-07-25 22:43:35 -0700431 fields->mt[1].x =
432 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
433 (2 * (priv->x_bits - 1));
434 fields->mt[1].y =
435 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
436 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800437
Hans de Goede40e8f532014-07-25 22:37:15 -0700438 /* y-bitmap order is reversed, except on rushmore */
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800439 if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700440 fields->mt[0].y = priv->y_max - fields->mt[0].y;
441 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700442 }
443
Seth Forshee01ce6612011-11-07 19:54:13 -0800444 return fingers;
445}
446
Hans de Goedecdf333e2014-07-25 22:44:42 -0700447static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800448{
449 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700450 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
451 input_report_abs(dev, ABS_MT_POSITION_X, x);
452 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800453}
454
Hans de Goedecdf333e2014-07-25 22:44:42 -0700455static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800456{
Hans de Goede02d04252014-07-25 22:43:35 -0700457 struct alps_data *priv = psmouse->private;
458 struct input_dev *dev = psmouse->dev;
459 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700460 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700461
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800462 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700463 for (i = 0; i < n; i++)
464 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
465
466 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800467}
468
Hans de Goede68c21872014-07-25 22:47:25 -0700469static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
470{
471 struct alps_data *priv = psmouse->private;
472 struct input_dev *dev = psmouse->dev;
473 struct alps_fields *f = &priv->f;
474
475 /* Use st data when we don't have mt data */
476 if (fingers < 2) {
477 f->mt[0].x = f->st.x;
478 f->mt[0].y = f->st.y;
479 fingers = f->pressure > 0 ? 1 : 0;
480 }
481
Hans de Goede99d99962014-07-25 22:48:02 -0700482 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
Hans de Goede68c21872014-07-25 22:47:25 -0700483
484 input_mt_report_finger_count(dev, fingers);
485
486 input_report_key(dev, BTN_LEFT, f->left);
487 input_report_key(dev, BTN_RIGHT, f->right);
488 input_report_key(dev, BTN_MIDDLE, f->middle);
489
490 input_report_abs(dev, ABS_PRESSURE, f->pressure);
491
492 input_sync(dev);
493}
494
Seth Forshee25bded72011-11-07 19:53:36 -0800495static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
496{
497 struct alps_data *priv = psmouse->private;
498 unsigned char *packet = psmouse->packet;
499 struct input_dev *dev = priv->dev2;
500 int x, y, z, left, right, middle;
501
Pali Rohár34412ba2015-01-09 12:48:58 -0800502 /* It should be a DualPoint when received trackstick packet */
503 if (!(priv->flags & ALPS_DUALPOINT)) {
504 psmouse_warn(psmouse,
505 "Rejected trackstick packet from non DualPoint device");
506 return;
507 }
508
Seth Forshee25bded72011-11-07 19:53:36 -0800509 /* Sanity check packet */
510 if (!(packet[0] & 0x40)) {
511 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
512 return;
513 }
514
515 /*
516 * There's a special packet that seems to indicate the end
517 * of a stream of trackstick data. Filter these out.
518 */
519 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
520 return;
521
522 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
523 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
524 z = (packet[4] & 0x7c) >> 2;
525
526 /*
527 * The x and y values tend to be quite large, and when used
528 * alone the trackstick is difficult to use. Scale them down
529 * to compensate.
530 */
531 x /= 8;
532 y /= 8;
533
534 input_report_rel(dev, REL_X, x);
535 input_report_rel(dev, REL_Y, -y);
536
537 /*
538 * Most ALPS models report the trackstick buttons in the touchpad
539 * packets, but a few report them here. No reliable way has been
540 * found to differentiate between the models upfront, so we enable
541 * the quirk in response to seeing a button press in the trackstick
542 * packet.
543 */
544 left = packet[3] & 0x01;
545 right = packet[3] & 0x02;
546 middle = packet[3] & 0x04;
547
548 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
549 (left || right || middle))
550 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
551
552 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
553 input_report_key(dev, BTN_LEFT, left);
554 input_report_key(dev, BTN_RIGHT, right);
555 input_report_key(dev, BTN_MIDDLE, middle);
556 }
557
558 input_sync(dev);
559 return;
560}
561
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800562static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
563{
564 f->left = !!(p[3] & 0x01);
565 f->right = !!(p[3] & 0x02);
566 f->middle = !!(p[3] & 0x04);
567
568 f->ts_left = !!(p[3] & 0x10);
569 f->ts_right = !!(p[3] & 0x20);
570 f->ts_middle = !!(p[3] & 0x40);
571}
572
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700573static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800574 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800575{
576 f->first_mp = !!(p[4] & 0x40);
577 f->is_mp = !!(p[0] & 0x40);
578
579 f->fingers = (p[5] & 0x3) + 1;
580 f->x_map = ((p[4] & 0x7e) << 8) |
581 ((p[1] & 0x7f) << 2) |
582 ((p[0] & 0x30) >> 4);
583 f->y_map = ((p[3] & 0x70) << 4) |
584 ((p[2] & 0x7f) << 1) |
585 (p[4] & 0x01);
586
Hans de Goede02d04252014-07-25 22:43:35 -0700587 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800588 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700589 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
590 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800591
592 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700593
594 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800595}
596
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700597static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800598 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800599{
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700600 f->first_mp = !!(p[4] & 0x40);
Yunkang Tangf105e342014-07-25 22:29:24 -0700601 f->is_mp = !!(p[5] & 0x40);
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700602
Yunkang Tangf105e342014-07-25 22:29:24 -0700603 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Hans de Goedeaab9cf72015-05-20 14:38:33 -0700604 f->x_map = ((p[5] & 0x10) << 11) |
605 ((p[4] & 0x7e) << 8) |
606 ((p[1] & 0x7f) << 2) |
607 ((p[0] & 0x30) >> 4);
608 f->y_map = ((p[5] & 0x20) << 6) |
609 ((p[3] & 0x70) << 4) |
610 ((p[2] & 0x7f) << 1) |
611 (p[4] & 0x01);
612
613 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
614 ((p[0] & 0x30) >> 4);
615 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
616 f->pressure = p[5] & 0x7f;
617
618 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700619
620 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800621}
622
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700623static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800624 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800625{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800626 u64 palm_data = 0;
627 struct alps_data *priv = psmouse->private;
628
Dave Turvene75af9e52013-02-21 22:58:28 -0800629 f->first_mp = !!(p[0] & 0x02);
630 f->is_mp = !!(p[0] & 0x20);
631
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800632 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700633 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
634 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
635 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800636 alps_decode_buttons_v3(f, p);
637 } else {
638 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800639 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800640
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800641 palm_data = (p[1] & 0x7f) |
642 ((p[2] & 0x7f) << 7) |
643 ((p[4] & 0x7f) << 14) |
644 ((p[5] & 0x7f) << 21) |
645 ((p[3] & 0x07) << 28) |
646 (((u64)p[3] & 0x70) << 27) |
647 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800648
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800649 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
650 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
651
652 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
653 f->x_map = (palm_data >> priv->y_bits) &
654 (BIT(priv->x_bits) - 1);
655 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700656
657 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800658}
659
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800660static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800661{
662 struct alps_data *priv = psmouse->private;
663 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800664 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700665 struct alps_fields *f = &priv->f;
666 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800667
Hans de Goede02d04252014-07-25 22:43:35 -0700668 memset(f, 0, sizeof(*f));
669
670 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800671
672 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800673 * There's no single feature of touchpad position and bitmap packets
674 * that can be used to distinguish between them. We rely on the fact
675 * that a bitmap packet should always follow a position packet with
676 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800677 */
678 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800679 /*
680 * Sometimes a position packet will indicate a multi-packet
681 * sequence, but then what follows is another position
682 * packet. Check for this, and when it happens process the
683 * position packet as usual.
684 */
Hans de Goede02d04252014-07-25 22:43:35 -0700685 if (f->is_mp) {
686 fingers = f->fingers;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800687 if (priv->proto_version == ALPS_PROTO_V3 ||
688 priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700689 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700690 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800691
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800692 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700693 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800694 psmouse);
695 } else {
696 /*
697 * Because Dolphin uses position packet's
698 * coordinate data as Pt1 and uses it to
699 * calculate Pt2, so we need to do position
700 * packet decode first.
701 */
Hans de Goede02d04252014-07-25 22:43:35 -0700702 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800703 psmouse);
704
705 /*
706 * Since Dolphin's finger number is reliable,
707 * there is no need to compare with bmap_fn.
708 */
Hans de Goede02d04252014-07-25 22:43:35 -0700709 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800710 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800711 } else {
712 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800713 }
714 }
715
Seth Forshee01ce6612011-11-07 19:54:13 -0800716 /*
717 * Bit 6 of byte 0 is not usually set in position packets. The only
718 * times it seems to be set is in situations where the data is
719 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
720 * this combined with the fact that this bit is useful for filtering
721 * out misidentified bitmap packets, we reject anything with this
722 * bit set.
723 */
Hans de Goede02d04252014-07-25 22:43:35 -0700724 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800725 return;
726
Hans de Goede02d04252014-07-25 22:43:35 -0700727 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800728 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800729 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
730 return;
731 }
732
733 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800734
Seth Forshee25bded72011-11-07 19:53:36 -0800735 /*
736 * Sometimes the hardware sends a single packet with z = 0
737 * in the middle of a stream. Real releases generate packets
738 * with x, y, and z all zero, so these seem to be flukes.
739 * Ignore them.
740 */
Hans de Goede02d04252014-07-25 22:43:35 -0700741 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800742 return;
743
Hans de Goede68c21872014-07-25 22:47:25 -0700744 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800745
Pali Rohár34412ba2015-01-09 12:48:58 -0800746 if ((priv->flags & ALPS_DUALPOINT) &&
747 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700748 input_report_key(dev2, BTN_LEFT, f->ts_left);
749 input_report_key(dev2, BTN_RIGHT, f->ts_right);
750 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800751 input_sync(dev2);
752 }
753}
754
755static void alps_process_packet_v3(struct psmouse *psmouse)
756{
757 unsigned char *packet = psmouse->packet;
758
759 /*
760 * v3 protocol packets come in three types, two representing
761 * touchpad data and one representing trackstick data.
762 * Trackstick packets seem to be distinguished by always
763 * having 0x3f in the last byte. This value has never been
764 * observed in the last byte of either of the other types
765 * of packets.
766 */
767 if (packet[5] == 0x3f) {
768 alps_process_trackstick_packet_v3(psmouse);
769 return;
770 }
771
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800772 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800773}
774
Yunkang Tang95f75e92013-12-01 22:33:52 -0800775static void alps_process_packet_v6(struct psmouse *psmouse)
776{
777 struct alps_data *priv = psmouse->private;
778 unsigned char *packet = psmouse->packet;
779 struct input_dev *dev = psmouse->dev;
780 struct input_dev *dev2 = priv->dev2;
781 int x, y, z, left, right, middle;
782
783 /*
784 * We can use Byte5 to distinguish if the packet is from Touchpad
785 * or Trackpoint.
786 * Touchpad: 0 - 0x7E
787 * Trackpoint: 0x7F
788 */
789 if (packet[5] == 0x7F) {
790 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800791 if (!(priv->flags & ALPS_DUALPOINT)) {
792 psmouse_warn(psmouse,
793 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800794 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800795 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800796
797 /* Trackpoint packet */
798 x = packet[1] | ((packet[3] & 0x20) << 2);
799 y = packet[2] | ((packet[3] & 0x40) << 1);
800 z = packet[4];
801 left = packet[3] & 0x01;
802 right = packet[3] & 0x02;
803 middle = packet[3] & 0x04;
804
805 /* To prevent the cursor jump when finger lifted */
806 if (x == 0x7F && y == 0x7F && z == 0x7F)
807 x = y = z = 0;
808
809 /* Divide 4 since trackpoint's speed is too fast */
810 input_report_rel(dev2, REL_X, (char)x / 4);
811 input_report_rel(dev2, REL_Y, -((char)y / 4));
812
813 input_report_key(dev2, BTN_LEFT, left);
814 input_report_key(dev2, BTN_RIGHT, right);
815 input_report_key(dev2, BTN_MIDDLE, middle);
816
817 input_sync(dev2);
818 return;
819 }
820
821 /* Touchpad packet */
822 x = packet[1] | ((packet[3] & 0x78) << 4);
823 y = packet[2] | ((packet[4] & 0x78) << 4);
824 z = packet[5];
825 left = packet[3] & 0x01;
826 right = packet[3] & 0x02;
827
828 if (z > 30)
829 input_report_key(dev, BTN_TOUCH, 1);
830 if (z < 25)
831 input_report_key(dev, BTN_TOUCH, 0);
832
833 if (z > 0) {
834 input_report_abs(dev, ABS_X, x);
835 input_report_abs(dev, ABS_Y, y);
836 }
837
838 input_report_abs(dev, ABS_PRESSURE, z);
839 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
840
841 /* v6 touchpad does not have middle button */
842 input_report_key(dev, BTN_LEFT, left);
843 input_report_key(dev, BTN_RIGHT, right);
844
845 input_sync(dev);
846}
847
Seth Forshee25bded72011-11-07 19:53:36 -0800848static void alps_process_packet_v4(struct psmouse *psmouse)
849{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700850 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800851 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700852 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700853 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700854
855 /*
856 * v4 has a 6-byte encoding for bitmap data, but this data is
857 * broken up between 3 normal packets. Use priv->multi_packet to
858 * track our position in the bitmap packet.
859 */
860 if (packet[6] & 0x40) {
861 /* sync, reset position */
862 priv->multi_packet = 0;
863 }
864
865 if (WARN_ON_ONCE(priv->multi_packet > 2))
866 return;
867
868 offset = 2 * priv->multi_packet;
869 priv->multi_data[offset] = packet[6];
870 priv->multi_data[offset + 1] = packet[7];
871
872 if (++priv->multi_packet > 2) {
873 priv->multi_packet = 0;
874
Hans de Goede02d04252014-07-25 22:43:35 -0700875 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700876 ((priv->multi_data[3] & 0x60) << 3) |
877 ((priv->multi_data[0] & 0x3f) << 2) |
878 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700879 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700880 ((priv->multi_data[3] & 0x1f) << 5) |
881 (priv->multi_data[1] & 0x1f);
882
Hans de Goede02d04252014-07-25 22:43:35 -0700883 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700884 }
Seth Forshee25bded72011-11-07 19:53:36 -0800885
Andreas Boschb0cfb792014-10-15 10:44:50 -0700886 f->left = !!(packet[4] & 0x01);
887 f->right = !!(packet[4] & 0x02);
Seth Forshee25bded72011-11-07 19:53:36 -0800888
Hans de Goede02d04252014-07-25 22:43:35 -0700889 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
890 ((packet[0] & 0x30) >> 4);
891 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
892 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800893
Hans de Goede68c21872014-07-25 22:47:25 -0700894 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800895}
896
Yunkang Tang38088432014-07-26 13:51:41 -0700897static bool alps_is_valid_package_v7(struct psmouse *psmouse)
898{
899 switch (psmouse->pktcnt) {
900 case 3:
901 return (psmouse->packet[2] & 0x40) == 0x40;
902 case 4:
903 return (psmouse->packet[3] & 0x48) == 0x48;
904 case 6:
905 return (psmouse->packet[5] & 0x40) == 0x00;
906 }
907 return true;
908}
909
910static unsigned char alps_get_packet_id_v7(char *byte)
911{
912 unsigned char packet_id;
913
914 if (byte[4] & 0x40)
915 packet_id = V7_PACKET_ID_TWO;
916 else if (byte[4] & 0x01)
917 packet_id = V7_PACKET_ID_MULTI;
918 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
919 packet_id = V7_PACKET_ID_NEW;
920 else if (byte[1] == 0x00 && byte[4] == 0x00)
921 packet_id = V7_PACKET_ID_IDLE;
922 else
923 packet_id = V7_PACKET_ID_UNKNOWN;
924
925 return packet_id;
926}
927
928static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
929 unsigned char *pkt,
930 unsigned char pkt_id)
931{
932 mt[0].x = ((pkt[2] & 0x80) << 4);
933 mt[0].x |= ((pkt[2] & 0x3F) << 5);
934 mt[0].x |= ((pkt[3] & 0x30) >> 1);
935 mt[0].x |= (pkt[3] & 0x07);
936 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
937
938 mt[1].x = ((pkt[3] & 0x80) << 4);
939 mt[1].x |= ((pkt[4] & 0x80) << 3);
940 mt[1].x |= ((pkt[4] & 0x3F) << 4);
941 mt[1].y = ((pkt[5] & 0x80) << 3);
942 mt[1].y |= ((pkt[5] & 0x3F) << 4);
943
944 switch (pkt_id) {
945 case V7_PACKET_ID_TWO:
946 mt[1].x &= ~0x000F;
947 mt[1].y |= 0x000F;
948 break;
949
950 case V7_PACKET_ID_MULTI:
951 mt[1].x &= ~0x003F;
952 mt[1].y &= ~0x0020;
953 mt[1].y |= ((pkt[4] & 0x02) << 4);
954 mt[1].y |= 0x001F;
955 break;
956
957 case V7_PACKET_ID_NEW:
958 mt[1].x &= ~0x003F;
959 mt[1].x |= (pkt[0] & 0x20);
960 mt[1].y |= 0x000F;
961 break;
962 }
963
964 mt[0].y = 0x7FF - mt[0].y;
965 mt[1].y = 0x7FF - mt[1].y;
966}
967
968static int alps_get_mt_count(struct input_mt_pos *mt)
969{
Hans de Goede7091c442014-12-18 09:53:34 -0800970 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700971
Hans de Goede7091c442014-12-18 09:53:34 -0800972 for (i = 0; i < MAX_TOUCHES; i++) {
973 if (mt[i].x != 0 || mt[i].y != 0)
974 fingers++;
975 }
Yunkang Tang38088432014-07-26 13:51:41 -0700976
Hans de Goede7091c442014-12-18 09:53:34 -0800977 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700978}
979
980static int alps_decode_packet_v7(struct alps_fields *f,
981 unsigned char *p,
982 struct psmouse *psmouse)
983{
Hans de Goeded27eb792014-12-18 09:55:14 -0800984 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700985 unsigned char pkt_id;
986
987 pkt_id = alps_get_packet_id_v7(p);
988 if (pkt_id == V7_PACKET_ID_IDLE)
989 return 0;
990 if (pkt_id == V7_PACKET_ID_UNKNOWN)
991 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -0800992 /*
993 * NEW packets are send to indicate a discontinuity in the finger
994 * coordinate reporting. Specifically a finger may have moved from
995 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
996 * us.
997 *
998 * NEW packets have 3 problems:
999 * 1) They do not contain middle / right button info (on non clickpads)
1000 * this can be worked around by preserving the old button state
1001 * 2) They do not contain an accurate fingercount, and they are
1002 * typically send when the number of fingers changes. We cannot use
1003 * the old finger count as that may mismatch with the amount of
1004 * touch coordinates we've available in the NEW packet
1005 * 3) Their x data for the second touch is inaccurate leading to
1006 * a possible jump of the x coordinate by 16 units when the first
1007 * non NEW packet comes in
1008 * Since problems 2 & 3 cannot be worked around, just ignore them.
1009 */
1010 if (pkt_id == V7_PACKET_ID_NEW)
1011 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001012
1013 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1014
Yunkang Tang38088432014-07-26 13:51:41 -07001015 if (pkt_id == V7_PACKET_ID_TWO)
1016 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001017 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001018 f->fingers = 3 + (p[5] & 0x03);
1019
Hans de Goeded27eb792014-12-18 09:55:14 -08001020 f->left = (p[0] & 0x80) >> 7;
1021 if (priv->flags & ALPS_BUTTONPAD) {
1022 if (p[0] & 0x20)
1023 f->fingers++;
1024 if (p[0] & 0x10)
1025 f->fingers++;
1026 } else {
1027 f->right = (p[0] & 0x20) >> 5;
1028 f->middle = (p[0] & 0x10) >> 4;
1029 }
1030
Hans de Goede7091c442014-12-18 09:53:34 -08001031 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1032 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1033 f->mt[0].x = f->mt[1].x;
1034 f->mt[0].y = f->mt[1].y;
1035 f->mt[1].x = 0;
1036 f->mt[1].y = 0;
1037 }
1038
Yunkang Tang38088432014-07-26 13:51:41 -07001039 return 0;
1040}
1041
1042static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1043{
1044 struct alps_data *priv = psmouse->private;
1045 unsigned char *packet = psmouse->packet;
1046 struct input_dev *dev2 = priv->dev2;
1047 int x, y, z, left, right, middle;
1048
Pali Rohár34412ba2015-01-09 12:48:58 -08001049 /* It should be a DualPoint when received trackstick packet */
1050 if (!(priv->flags & ALPS_DUALPOINT)) {
1051 psmouse_warn(psmouse,
1052 "Rejected trackstick packet from non DualPoint device");
1053 return;
1054 }
1055
Yunkang Tang38088432014-07-26 13:51:41 -07001056 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1057 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1058 ((packet[3] & 0x20) << 1);
1059 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1060
1061 left = (packet[1] & 0x01);
1062 right = (packet[1] & 0x02) >> 1;
1063 middle = (packet[1] & 0x04) >> 2;
1064
1065 /* Divide 2 since trackpoint's speed is too fast */
1066 input_report_rel(dev2, REL_X, (char)x / 2);
1067 input_report_rel(dev2, REL_Y, -((char)y / 2));
1068
1069 input_report_key(dev2, BTN_LEFT, left);
1070 input_report_key(dev2, BTN_RIGHT, right);
1071 input_report_key(dev2, BTN_MIDDLE, middle);
1072
1073 input_sync(dev2);
1074}
1075
1076static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1077{
1078 struct alps_data *priv = psmouse->private;
1079 struct input_dev *dev = psmouse->dev;
1080 struct alps_fields *f = &priv->f;
1081
1082 memset(f, 0, sizeof(*f));
1083
1084 if (priv->decode_fields(f, psmouse->packet, psmouse))
1085 return;
1086
1087 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1088
1089 input_mt_report_finger_count(dev, f->fingers);
1090
1091 input_report_key(dev, BTN_LEFT, f->left);
1092 input_report_key(dev, BTN_RIGHT, f->right);
1093 input_report_key(dev, BTN_MIDDLE, f->middle);
1094
1095 input_sync(dev);
1096}
1097
1098static void alps_process_packet_v7(struct psmouse *psmouse)
1099{
1100 unsigned char *packet = psmouse->packet;
1101
1102 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1103 alps_process_trackstick_packet_v7(psmouse);
1104 else
1105 alps_process_touchpad_packet_v7(psmouse);
1106}
1107
Fengguang Wu07f19e32015-04-10 23:54:31 -07001108static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001109{
1110 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1111
1112 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1113 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
1114 pkt_id = SS4_PACKET_ID_IDLE;
1115 } else if (!(byte[3] & 0x10)) {
1116 pkt_id = SS4_PACKET_ID_ONE;
1117 } else if (!(byte[3] & 0x20)) {
1118 pkt_id = SS4_PACKET_ID_TWO;
1119 } else {
1120 pkt_id = SS4_PACKET_ID_MULTI;
1121 }
1122
1123 return pkt_id;
1124}
1125
1126static int alps_decode_ss4_v2(struct alps_fields *f,
1127 unsigned char *p, struct psmouse *psmouse)
1128{
1129 struct alps_data *priv = psmouse->private;
1130 unsigned char pkt_id;
1131 unsigned int no_data_x, no_data_y;
1132
1133 pkt_id = alps_get_pkt_id_ss4_v2(p);
1134
1135 /* Current packet is 1Finger coordinate packet */
1136 switch (pkt_id) {
1137 case SS4_PACKET_ID_ONE:
1138 f->mt[0].x = SS4_1F_X_V2(p);
1139 f->mt[0].y = SS4_1F_Y_V2(p);
1140 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1141 f->fingers = 1;
1142 f->first_mp = 0;
1143 f->is_mp = 0;
1144 break;
1145
1146 case SS4_PACKET_ID_TWO:
1147 if (priv->flags & ALPS_BUTTONPAD) {
1148 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1149 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1150 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1151 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1152 } else {
1153 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1154 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1155 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1156 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1157 }
1158 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1159
1160 if (SS4_IS_MF_CONTINUE(p)) {
1161 f->first_mp = 1;
1162 } else {
1163 f->fingers = 2;
1164 f->first_mp = 0;
1165 }
1166 f->is_mp = 0;
1167
1168 break;
1169
1170 case SS4_PACKET_ID_MULTI:
1171 if (priv->flags & ALPS_BUTTONPAD) {
1172 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1173 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1174 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1175 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1176 no_data_x = SS4_MFPACKET_NO_AX_BL;
1177 no_data_y = SS4_MFPACKET_NO_AY_BL;
1178 } else {
1179 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1180 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1181 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1182 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1183 no_data_x = SS4_MFPACKET_NO_AX;
1184 no_data_y = SS4_MFPACKET_NO_AY;
1185 }
1186
1187 f->first_mp = 0;
1188 f->is_mp = 1;
1189
1190 if (SS4_IS_5F_DETECTED(p)) {
1191 f->fingers = 5;
1192 } else if (f->mt[3].x == no_data_x &&
1193 f->mt[3].y == no_data_y) {
1194 f->mt[3].x = 0;
1195 f->mt[3].y = 0;
1196 f->fingers = 3;
1197 } else {
1198 f->fingers = 4;
1199 }
1200 break;
1201
1202 case SS4_PACKET_ID_IDLE:
1203 default:
1204 memset(f, 0, sizeof(struct alps_fields));
1205 break;
1206 }
1207
1208 f->left = !!(SS4_BTN_V2(p) & 0x01);
1209 if (!(priv->flags & ALPS_BUTTONPAD)) {
1210 f->right = !!(SS4_BTN_V2(p) & 0x02);
1211 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1212 }
1213
1214 return 0;
1215}
1216
1217static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1218{
1219 struct alps_data *priv = psmouse->private;
1220 unsigned char *packet = psmouse->packet;
1221 struct input_dev *dev = psmouse->dev;
1222 struct alps_fields *f = &priv->f;
1223
1224 memset(f, 0, sizeof(struct alps_fields));
1225 priv->decode_fields(f, packet, psmouse);
1226 if (priv->multi_packet) {
1227 /*
1228 * Sometimes the first packet will indicate a multi-packet
1229 * sequence, but sometimes the next multi-packet would not
1230 * come. Check for this, and when it happens process the
1231 * position packet as usual.
1232 */
1233 if (f->is_mp) {
1234 /* Now process the 1st packet */
1235 priv->decode_fields(f, priv->multi_data, psmouse);
1236 } else {
1237 priv->multi_packet = 0;
1238 }
1239 }
1240
1241 /*
1242 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1243 * When it is set, it means 2nd packet comes without 1st packet come.
1244 */
1245 if (f->is_mp)
1246 return;
1247
1248 /* Save the first packet */
1249 if (!priv->multi_packet && f->first_mp) {
1250 priv->multi_packet = 1;
1251 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1252 return;
1253 }
1254
1255 priv->multi_packet = 0;
1256
1257 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1258
1259 input_mt_report_finger_count(dev, f->fingers);
1260
1261 input_report_key(dev, BTN_LEFT, f->left);
1262 input_report_key(dev, BTN_RIGHT, f->right);
1263 input_report_key(dev, BTN_MIDDLE, f->middle);
1264
1265 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1266 input_sync(dev);
1267}
1268
1269static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1270{
1271 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1272 return false;
1273 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1274 return false;
1275 return true;
1276}
1277
Pali Rohár04aae282015-01-14 13:18:30 -08001278static DEFINE_MUTEX(alps_mutex);
1279
1280static void alps_register_bare_ps2_mouse(struct work_struct *work)
1281{
1282 struct alps_data *priv =
1283 container_of(work, struct alps_data, dev3_register_work.work);
1284 struct psmouse *psmouse = priv->psmouse;
1285 struct input_dev *dev3;
1286 int error = 0;
1287
1288 mutex_lock(&alps_mutex);
1289
1290 if (priv->dev3)
1291 goto out;
1292
1293 dev3 = input_allocate_device();
1294 if (!dev3) {
1295 psmouse_err(psmouse, "failed to allocate secondary device\n");
1296 error = -ENOMEM;
1297 goto out;
1298 }
1299
1300 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1301 psmouse->ps2dev.serio->phys,
1302 (priv->dev2 ? "input2" : "input1"));
1303 dev3->phys = priv->phys3;
1304
1305 /*
1306 * format of input device name is: "protocol vendor name"
1307 * see function psmouse_switch_protocol() in psmouse-base.c
1308 */
1309 dev3->name = "PS/2 ALPS Mouse";
1310
1311 dev3->id.bustype = BUS_I8042;
1312 dev3->id.vendor = 0x0002;
1313 dev3->id.product = PSMOUSE_PS2;
1314 dev3->id.version = 0x0000;
1315 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1316
1317 input_set_capability(dev3, EV_REL, REL_X);
1318 input_set_capability(dev3, EV_REL, REL_Y);
1319 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1320 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1321 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1322
1323 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1324
1325 error = input_register_device(dev3);
1326 if (error) {
1327 psmouse_err(psmouse,
1328 "failed to register secondary device: %d\n",
1329 error);
1330 input_free_device(dev3);
1331 goto out;
1332 }
1333
1334 priv->dev3 = dev3;
1335
1336out:
1337 /*
1338 * Save the error code so that we can detect that we
1339 * already tried to create the device.
1340 */
1341 if (error)
1342 priv->dev3 = ERR_PTR(error);
1343
1344 mutex_unlock(&alps_mutex);
1345}
1346
Hans de Goede59c30af2015-04-03 17:14:40 -07001347static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001348 unsigned char packet[],
1349 bool report_buttons)
1350{
Hans de Goede59c30af2015-04-03 17:14:40 -07001351 struct alps_data *priv = psmouse->private;
1352 struct input_dev *dev;
1353
Hans de Goedee3a79212015-04-03 17:20:05 -07001354 /* Figure out which device to use to report the bare packet */
1355 if (priv->proto_version == ALPS_PROTO_V2 &&
1356 (priv->flags & ALPS_DUALPOINT)) {
1357 /* On V2 devices the DualPoint Stick reports bare packets */
1358 dev = priv->dev2;
1359 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001360 /* Register dev3 mouse if we received PS/2 packet first time */
1361 if (!IS_ERR(priv->dev3))
1362 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1363 0);
1364 return;
1365 } else {
1366 dev = priv->dev3;
1367 }
1368
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001369 if (report_buttons)
Pali Rohár04aae282015-01-14 13:18:30 -08001370 alps_report_buttons(dev, NULL,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001371 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1372
Pali Rohár04aae282015-01-14 13:18:30 -08001373 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001374 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001375 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001376 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1377
Pali Rohár04aae282015-01-14 13:18:30 -08001378 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001379}
1380
1381static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct alps_data *priv = psmouse->private;
1384
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001385 if (psmouse->pktcnt < 6)
1386 return PSMOUSE_GOOD_DATA;
1387
1388 if (psmouse->pktcnt == 6) {
1389 /*
1390 * Start a timer to flush the packet if it ends up last
1391 * 6-byte packet in the stream. Timer needs to fire
1392 * psmouse core times out itself. 20 ms should be enough
1393 * to decide if we are getting more data or not.
1394 */
1395 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1396 return PSMOUSE_GOOD_DATA;
1397 }
1398
1399 del_timer(&priv->timer);
1400
1401 if (psmouse->packet[6] & 0x80) {
1402
1403 /*
1404 * Highest bit is set - that means we either had
1405 * complete ALPS packet and this is start of the
1406 * next packet or we got garbage.
1407 */
1408
1409 if (((psmouse->packet[3] |
1410 psmouse->packet[4] |
1411 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001412 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001413 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001414 "refusing packet %4ph (suspected interleaved ps/2)\n",
1415 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001416 return PSMOUSE_BAD_DATA;
1417 }
1418
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001419 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001420
1421 /* Continue with the next packet */
1422 psmouse->packet[0] = psmouse->packet[6];
1423 psmouse->pktcnt = 1;
1424
1425 } else {
1426
1427 /*
1428 * High bit is 0 - that means that we indeed got a PS/2
1429 * packet in the middle of ALPS packet.
1430 *
1431 * There is also possibility that we got 6-byte ALPS
1432 * packet followed by 3-byte packet from trackpoint. We
1433 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001434 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001435 * normal operation (user would need to press all
1436 * buttons on the pad and start moving trackpoint
1437 * without touching the pad surface) we assume former.
1438 * Even if we are wrong the wost thing that would happen
1439 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001440 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001441 */
1442
Hans de Goede59c30af2015-04-03 17:14:40 -07001443 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1444 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001445
1446 /*
1447 * Continue with the standard ALPS protocol handling,
1448 * but make sure we won't process it as an interleaved
1449 * packet again, which may happen if all buttons are
1450 * pressed. To avoid this let's reset the 4th bit which
1451 * is normally 1.
1452 */
1453 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1454 psmouse->pktcnt = 4;
1455 }
1456
1457 return PSMOUSE_GOOD_DATA;
1458}
1459
1460static void alps_flush_packet(unsigned long data)
1461{
1462 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001463 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001464
1465 serio_pause_rx(psmouse->ps2dev.serio);
1466
Seth Forsheeb46615f2011-11-07 19:53:30 -08001467 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001468
1469 /*
1470 * We did not any more data in reasonable amount of time.
1471 * Validate the last 3 bytes and process as a standard
1472 * ALPS packet.
1473 */
1474 if ((psmouse->packet[3] |
1475 psmouse->packet[4] |
1476 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001477 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001478 "refusing packet %3ph (suspected interleaved ps/2)\n",
1479 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001480 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001481 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001482 }
1483 psmouse->pktcnt = 0;
1484 }
1485
1486 serio_continue_rx(psmouse->ps2dev.serio);
1487}
1488
1489static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1490{
1491 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001492
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001493 /*
1494 * Check if we are dealing with a bare PS/2 packet, presumably from
1495 * a device connected to the external PS/2 port. Because bare PS/2
1496 * protocol does not have enough constant bits to self-synchronize
1497 * properly we only do this if the device is fully synchronized.
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001498 * Can not distinguish V8's first byte from PS/2 packet's
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001499 */
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001500 if (priv->proto_version != ALPS_PROTO_V8 &&
1501 !psmouse->out_of_sync_cnt &&
1502 (psmouse->packet[0] & 0xc8) == 0x08) {
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001505 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1506 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return PSMOUSE_FULL_PACKET;
1508 }
1509 return PSMOUSE_GOOD_DATA;
1510 }
1511
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001512 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1513
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001514 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001515 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1516 return alps_handle_interleaved_ps2(psmouse);
1517 }
1518
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001519 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001520 psmouse_dbg(psmouse,
1521 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001522 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Seth Forsheeb46615f2011-11-07 19:53:30 -08001526 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001527 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001528 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001529 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001530 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1531 psmouse->pktcnt - 1,
1532 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001533
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001534 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001535 psmouse->pktcnt == psmouse->pktsize) {
1536 /*
1537 * Some Dell boxes, such as Latitude E6440 or E7440
1538 * with closed lid, quite often smash last byte of
1539 * otherwise valid packet with 0xff. Given that the
1540 * next packet is very likely to be valid let's
1541 * report PSMOUSE_FULL_PACKET but not process data,
1542 * rather than reporting PSMOUSE_BAD_DATA and
1543 * filling the logs.
1544 */
1545 return PSMOUSE_FULL_PACKET;
1546 }
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Masaki Ota3db5b9f2015-03-27 20:57:52 -07001551 if ((priv->proto_version == ALPS_PROTO_V7 &&
1552 !alps_is_valid_package_v7(psmouse)) ||
1553 (priv->proto_version == ALPS_PROTO_V8 &&
1554 !alps_is_valid_package_ss4_v2(psmouse))) {
Yunkang Tang38088432014-07-26 13:51:41 -07001555 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1556 psmouse->pktcnt - 1,
1557 psmouse->packet[psmouse->pktcnt - 1]);
1558 return PSMOUSE_BAD_DATA;
1559 }
1560
Seth Forsheeb46615f2011-11-07 19:53:30 -08001561 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001562 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return PSMOUSE_FULL_PACKET;
1564 }
1565
1566 return PSMOUSE_GOOD_DATA;
1567}
1568
Seth Forshee25bded72011-11-07 19:53:36 -08001569static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1570{
1571 struct ps2dev *ps2dev = &psmouse->ps2dev;
1572 struct alps_data *priv = psmouse->private;
1573 int command;
1574 unsigned char *param;
1575 unsigned char dummy[4];
1576
1577 BUG_ON(nibble > 0xf);
1578
1579 command = priv->nibble_commands[nibble].command;
1580 param = (command & 0x0f00) ?
1581 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1582
1583 if (ps2_command(ps2dev, param, command))
1584 return -1;
1585
1586 return 0;
1587}
1588
1589static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1590{
1591 struct ps2dev *ps2dev = &psmouse->ps2dev;
1592 struct alps_data *priv = psmouse->private;
1593 int i, nibble;
1594
1595 if (ps2_command(ps2dev, NULL, priv->addr_command))
1596 return -1;
1597
1598 for (i = 12; i >= 0; i -= 4) {
1599 nibble = (addr >> i) & 0xf;
1600 if (alps_command_mode_send_nibble(psmouse, nibble))
1601 return -1;
1602 }
1603
1604 return 0;
1605}
1606
1607static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1608{
1609 struct ps2dev *ps2dev = &psmouse->ps2dev;
1610 unsigned char param[4];
1611
1612 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1613 return -1;
1614
1615 /*
1616 * The address being read is returned in the first two bytes
1617 * of the result. Check that this address matches the expected
1618 * address.
1619 */
1620 if (addr != ((param[0] << 8) | param[1]))
1621 return -1;
1622
1623 return param[2];
1624}
1625
1626static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1627{
1628 if (alps_command_mode_set_addr(psmouse, addr))
1629 return -1;
1630 return __alps_command_mode_read_reg(psmouse, addr);
1631}
1632
1633static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1634{
1635 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1636 return -1;
1637 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1638 return -1;
1639 return 0;
1640}
1641
1642static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1643 u8 value)
1644{
1645 if (alps_command_mode_set_addr(psmouse, addr))
1646 return -1;
1647 return __alps_command_mode_write_reg(psmouse, value);
1648}
1649
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001650static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1651 int repeated_command, unsigned char *param)
1652{
1653 struct ps2dev *ps2dev = &psmouse->ps2dev;
1654
1655 param[0] = 0;
1656 if (init_command && ps2_command(ps2dev, param, init_command))
1657 return -EIO;
1658
1659 if (ps2_command(ps2dev, NULL, repeated_command) ||
1660 ps2_command(ps2dev, NULL, repeated_command) ||
1661 ps2_command(ps2dev, NULL, repeated_command))
1662 return -EIO;
1663
1664 param[0] = param[1] = param[2] = 0xff;
1665 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1666 return -EIO;
1667
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001668 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1669 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001670 return 0;
1671}
1672
Yunkang Tang38088432014-07-26 13:51:41 -07001673static bool alps_check_valid_firmware_id(unsigned char id[])
1674{
1675 if (id[0] == 0x73)
1676 return true;
1677
1678 if (id[0] == 0x88 &&
1679 (id[1] == 0x07 ||
1680 id[1] == 0x08 ||
1681 (id[1] & 0xf0) == 0xb0 ||
1682 (id[1] & 0xf0) == 0xc0)) {
1683 return true;
1684 }
1685
1686 return false;
1687}
1688
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001689static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001690{
1691 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001692
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001693 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001694 psmouse_err(psmouse, "failed to enter command mode\n");
1695 return -1;
1696 }
1697
Yunkang Tang38088432014-07-26 13:51:41 -07001698 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001699 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001700 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001701 return -1;
1702 }
Seth Forshee25bded72011-11-07 19:53:36 -08001703 return 0;
1704}
1705
1706static inline int alps_exit_command_mode(struct psmouse *psmouse)
1707{
1708 struct ps2dev *ps2dev = &psmouse->ps2dev;
1709 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1710 return -1;
1711 return 0;
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*
1715 * For DualPoint devices select the device that should respond to
1716 * subsequent commands. It looks like glidepad is behind stickpointer,
1717 * I'd thought it would be other way around...
1718 */
Seth Forshee25bded72011-11-07 19:53:36 -08001719static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1723
1724 if (ps2_command(ps2dev, NULL, cmd) ||
1725 ps2_command(ps2dev, NULL, cmd) ||
1726 ps2_command(ps2dev, NULL, cmd) ||
1727 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1728 return -1;
1729
1730 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001731 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 return 0;
1734}
1735
Seth Forshee25bded72011-11-07 19:53:36 -08001736static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct ps2dev *ps2dev = &psmouse->ps2dev;
1739
1740 /* Try ALPS magic knock - 4 disable before enable */
1741 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1742 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1743 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1744 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1745 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1746 return -1;
1747
1748 /*
1749 * Switch mouse to poll (remote) mode so motion data will not
1750 * get in our way
1751 */
1752 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1753}
1754
Yunkang Tang95f75e92013-12-01 22:33:52 -08001755static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1756{
1757 int i, nibble;
1758
1759 /*
1760 * b0-b11 are valid bits, send sequence is inverse.
1761 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1762 */
1763 for (i = 0; i <= 8; i += 4) {
1764 nibble = (word >> i) & 0xf;
1765 if (alps_command_mode_send_nibble(psmouse, nibble))
1766 return -1;
1767 }
1768
1769 return 0;
1770}
1771
1772static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1773 u16 addr, u16 value)
1774{
1775 struct ps2dev *ps2dev = &psmouse->ps2dev;
1776
1777 /* 0x0A0 is the command to write the word */
1778 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1779 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1780 alps_monitor_mode_send_word(psmouse, addr) ||
1781 alps_monitor_mode_send_word(psmouse, value) ||
1782 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1783 return -1;
1784
1785 return 0;
1786}
1787
1788static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1789{
1790 struct ps2dev *ps2dev = &psmouse->ps2dev;
1791
1792 if (enable) {
1793 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1794 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1795 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1796 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1797 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1798 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1799 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1800 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1801 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1802 return -1;
1803 } else {
1804 /* EC to exit monitor mode */
1805 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1806 return -1;
1807 }
1808
1809 return 0;
1810}
1811
1812static int alps_absolute_mode_v6(struct psmouse *psmouse)
1813{
1814 u16 reg_val = 0x181;
1815 int ret = -1;
1816
1817 /* enter monitor mode, to write the register */
1818 if (alps_monitor_mode(psmouse, true))
1819 return -1;
1820
1821 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1822
1823 if (alps_monitor_mode(psmouse, false))
1824 ret = -1;
1825
1826 return ret;
1827}
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829static int alps_get_status(struct psmouse *psmouse, char *param)
1830{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001832 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return -1;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 0;
1836}
1837
1838/*
1839 * Turn touchpad tapping on or off. The sequences are:
1840 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1841 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1842 * My guess that 0xE9 (GetInfo) is here as a sync point.
1843 * For models that also have stickpointer (DualPoints) its tapping
1844 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1845 * we don't fiddle with it.
1846 */
1847static int alps_tap_mode(struct psmouse *psmouse, int enable)
1848{
1849 struct ps2dev *ps2dev = &psmouse->ps2dev;
1850 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1851 unsigned char tap_arg = enable ? 0x0A : 0x00;
1852 unsigned char param[4];
1853
1854 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1855 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1856 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1857 ps2_command(ps2dev, &tap_arg, cmd))
1858 return -1;
1859
1860 if (alps_get_status(psmouse, param))
1861 return -1;
1862
1863 return 0;
1864}
1865
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001866/*
1867 * alps_poll() - poll the touchpad for current motion packet.
1868 * Used in resync.
1869 */
1870static int alps_poll(struct psmouse *psmouse)
1871{
1872 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001873 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001874 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001875
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001876 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001877 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001878
1879 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1880 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1881
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001882 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001883 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001884
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001885 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001886 return -1;
1887
1888 if ((psmouse->badbyte & 0xc8) == 0x08) {
1889/*
1890 * Poll the track stick ...
1891 */
1892 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1893 return -1;
1894 }
1895
1896 memcpy(psmouse->packet, buf, sizeof(buf));
1897 return 0;
1898}
1899
Seth Forshee25bded72011-11-07 19:53:36 -08001900static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001904 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001905 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001909 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001910 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Seth Forshee25bded72011-11-07 19:53:36 -08001914 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001915 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return -1;
1917 }
1918
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001919 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001920 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001924 /* ALPS needs stream mode, otherwise it won't report any data */
1925 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001926 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001927 return -1;
1928 }
1929
1930 return 0;
1931}
1932
Yunkang Tang95f75e92013-12-01 22:33:52 -08001933static int alps_hw_init_v6(struct psmouse *psmouse)
1934{
1935 unsigned char param[2] = {0xC8, 0x14};
1936
1937 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1938 if (alps_passthrough_mode_v2(psmouse, true))
1939 return -1;
1940
1941 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1942 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1943 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1944 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1945 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1946 return -1;
1947
1948 if (alps_passthrough_mode_v2(psmouse, false))
1949 return -1;
1950
1951 if (alps_absolute_mode_v6(psmouse)) {
1952 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1953 return -1;
1954 }
1955
1956 return 0;
1957}
1958
Seth Forshee25bded72011-11-07 19:53:36 -08001959/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001960 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001961 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001962static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1963 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001964{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001965 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001966
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001967 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001968 return -1;
1969
Kevin Cernekeecd401202013-02-13 22:28:07 -08001970 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1971 if (reg_val == -1)
1972 goto error;
1973
Seth Forshee25bded72011-11-07 19:53:36 -08001974 if (enable)
1975 reg_val |= 0x01;
1976 else
1977 reg_val &= ~0x01;
1978
Kevin Cernekeecd401202013-02-13 22:28:07 -08001979 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001980
Kevin Cernekeecd401202013-02-13 22:28:07 -08001981error:
1982 if (alps_exit_command_mode(psmouse))
1983 ret = -1;
1984 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001985}
1986
1987/* Must be in command mode when calling this function */
1988static int alps_absolute_mode_v3(struct psmouse *psmouse)
1989{
1990 int reg_val;
1991
1992 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1993 if (reg_val == -1)
1994 return -1;
1995
1996 reg_val |= 0x06;
1997 if (__alps_command_mode_write_reg(psmouse, reg_val))
1998 return -1;
1999
2000 return 0;
2001}
2002
Kevin Cernekeecd401202013-02-13 22:28:07 -08002003static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
2004{
2005 int ret = -EIO, reg_val;
2006
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002007 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08002008 goto error;
2009
2010 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2011 if (reg_val == -1)
2012 goto error;
2013
2014 /* bit 7: trackstick is present */
2015 ret = reg_val & 0x80 ? 0 : -ENODEV;
2016
2017error:
2018 alps_exit_command_mode(psmouse);
2019 return ret;
2020}
2021
2022static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2023{
2024 struct ps2dev *ps2dev = &psmouse->ps2dev;
2025 int ret = 0;
2026 unsigned char param[4];
2027
2028 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2029 return -EIO;
2030
2031 /*
2032 * E7 report for the trackstick
2033 *
2034 * There have been reports of failures to seem to trace back
2035 * to the above trackstick check failing. When these occur
2036 * this E7 report fails, so when that happens we continue
2037 * with the assumption that there isn't a trackstick after
2038 * all.
2039 */
2040 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002041 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08002042 ret = -ENODEV;
2043 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002044 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08002045
2046 /*
2047 * Not sure what this does, but it is absolutely
2048 * essential. Without it, the touchpad does not
2049 * work at all and the trackstick just emits normal
2050 * PS/2 packets.
2051 */
2052 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2053 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2054 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2055 alps_command_mode_send_nibble(psmouse, 0x9) ||
2056 alps_command_mode_send_nibble(psmouse, 0x4)) {
2057 psmouse_err(psmouse,
2058 "Error sending magic E6 sequence\n");
2059 ret = -EIO;
2060 goto error;
2061 }
2062
2063 /*
2064 * This ensures the trackstick packets are in the format
2065 * supported by this driver. If bit 1 isn't set the packet
2066 * format is different.
2067 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002068 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002069 alps_command_mode_write_reg(psmouse,
2070 reg_base + 0x08, 0x82) ||
2071 alps_exit_command_mode(psmouse))
2072 ret = -EIO;
2073 }
2074
2075error:
2076 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2077 ret = -EIO;
2078
2079 return ret;
2080}
2081
Seth Forshee25bded72011-11-07 19:53:36 -08002082static int alps_hw_init_v3(struct psmouse *psmouse)
2083{
Seth Forshee25bded72011-11-07 19:53:36 -08002084 struct ps2dev *ps2dev = &psmouse->ps2dev;
2085 int reg_val;
2086 unsigned char param[4];
2087
Kevin Cernekeecd401202013-02-13 22:28:07 -08002088 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2089 if (reg_val == -EIO)
2090 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08002091
Kevin Cernekeecd401202013-02-13 22:28:07 -08002092 if (reg_val == 0 &&
2093 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08002094 goto error;
2095
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002096 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08002097 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08002098 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2099 goto error;
2100 }
2101
2102 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2103 if (reg_val == -1)
2104 goto error;
2105 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2106 goto error;
2107
2108 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2109 if (reg_val == -1)
2110 goto error;
2111 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2112 goto error;
2113
2114 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2115 goto error;
2116 if (__alps_command_mode_write_reg(psmouse, 0x04))
2117 goto error;
2118
2119 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2120 goto error;
2121 if (__alps_command_mode_write_reg(psmouse, 0x03))
2122 goto error;
2123
2124 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2125 goto error;
2126 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2127 goto error;
2128
2129 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2130 goto error;
2131 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2132 goto error;
2133
Seth Forshee25bded72011-11-07 19:53:36 -08002134 alps_exit_command_mode(psmouse);
2135
2136 /* Set rate and enable data reporting */
2137 param[0] = 0x64;
2138 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2139 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2140 psmouse_err(psmouse, "Failed to enable data reporting\n");
2141 return -1;
2142 }
2143
2144 return 0;
2145
Seth Forshee25bded72011-11-07 19:53:36 -08002146error:
2147 /*
2148 * Leaving the touchpad in command mode will essentially render
2149 * it unusable until the machine reboots, so exit it here just
2150 * to be safe
2151 */
2152 alps_exit_command_mode(psmouse);
2153 return -1;
2154}
2155
Hans de Goedef3f33c62014-07-29 11:22:07 -07002156static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2157{
2158 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2159 struct alps_data *priv = psmouse->private;
2160
2161 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2162 if (reg < 0)
2163 return reg;
2164
2165 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2166 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2167
2168 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2169 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2170
2171 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2172 if (reg < 0)
2173 return reg;
2174
2175 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2176 x_electrode = 17 + x_electrode;
2177
2178 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2179 y_electrode = 13 + y_electrode;
2180
2181 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2182 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2183
2184 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2185 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2186
2187 psmouse_dbg(psmouse,
2188 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2189 x_pitch, y_pitch, x_electrode, y_electrode,
2190 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2191
2192 return 0;
2193}
2194
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002195static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2196{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002197 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002198 struct ps2dev *ps2dev = &psmouse->ps2dev;
2199 int reg_val, ret = -1;
2200
Kevin Cernekeecd401202013-02-13 22:28:07 -08002201 if (priv->flags & ALPS_DUALPOINT) {
2202 reg_val = alps_setup_trackstick_v3(psmouse,
2203 ALPS_REG_BASE_RUSHMORE);
2204 if (reg_val == -EIO)
2205 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002206 }
2207
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002208 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002209 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2210 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2211 goto error;
2212
Hans de Goedef3f33c62014-07-29 11:22:07 -07002213 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2214 goto error;
2215
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002216 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2217 if (reg_val == -1)
2218 goto error;
2219 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2220 goto error;
2221
2222 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2223 goto error;
2224
2225 /* enter absolute mode */
2226 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2227 if (reg_val == -1)
2228 goto error;
2229 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2230 goto error;
2231
2232 alps_exit_command_mode(psmouse);
2233 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2234
2235error:
2236 alps_exit_command_mode(psmouse);
2237 return ret;
2238}
2239
Seth Forshee25bded72011-11-07 19:53:36 -08002240/* Must be in command mode when calling this function */
2241static int alps_absolute_mode_v4(struct psmouse *psmouse)
2242{
2243 int reg_val;
2244
2245 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2246 if (reg_val == -1)
2247 return -1;
2248
2249 reg_val |= 0x02;
2250 if (__alps_command_mode_write_reg(psmouse, reg_val))
2251 return -1;
2252
2253 return 0;
2254}
2255
2256static int alps_hw_init_v4(struct psmouse *psmouse)
2257{
Seth Forshee25bded72011-11-07 19:53:36 -08002258 struct ps2dev *ps2dev = &psmouse->ps2dev;
2259 unsigned char param[4];
2260
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002261 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002262 goto error;
2263
2264 if (alps_absolute_mode_v4(psmouse)) {
2265 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2266 goto error;
2267 }
2268
2269 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2270 goto error;
2271
2272 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2273 goto error;
2274
2275 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2276 goto error;
2277
2278 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2279 goto error;
2280
2281 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2282 goto error;
2283
2284 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2285 goto error;
2286
2287 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2288 goto error;
2289
2290 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2291 goto error;
2292
2293 alps_exit_command_mode(psmouse);
2294
2295 /*
2296 * This sequence changes the output from a 9-byte to an
2297 * 8-byte format. All the same data seems to be present,
2298 * just in a more compact format.
2299 */
2300 param[0] = 0xc8;
2301 param[1] = 0x64;
2302 param[2] = 0x50;
2303 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2304 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2305 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2306 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2307 return -1;
2308
2309 /* Set rate and enable data reporting */
2310 param[0] = 0x64;
2311 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2312 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2313 psmouse_err(psmouse, "Failed to enable data reporting\n");
2314 return -1;
2315 }
2316
2317 return 0;
2318
2319error:
2320 /*
2321 * Leaving the touchpad in command mode will essentially render
2322 * it unusable until the machine reboots, so exit it here just
2323 * to be safe
2324 */
2325 alps_exit_command_mode(psmouse);
2326 return -1;
2327}
2328
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002329static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2330 unsigned char index, unsigned char otp[])
2331{
2332 struct ps2dev *ps2dev = &psmouse->ps2dev;
2333
2334 switch (index) {
2335 case 0:
2336 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2337 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2338 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2339 return -1;
2340
2341 break;
2342
2343 case 1:
2344 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2345 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2346 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2347 return -1;
2348
2349 break;
2350 }
2351
2352 return 0;
2353}
2354
Fengguang Wu07f19e32015-04-10 23:54:31 -07002355static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2356 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002357{
2358 int num_x_electrode;
2359 int num_y_electrode;
2360 int x_pitch, y_pitch, x_phys, y_phys;
2361
2362 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2363 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2364
2365 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2366 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2367
2368 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2369 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2370
2371 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2372 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2373
2374 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2375 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2376
2377 return 0;
2378}
2379
Fengguang Wu07f19e32015-04-10 23:54:31 -07002380static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2381 struct alps_data *priv)
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002382{
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002383 unsigned char is_btnless;
2384
2385 is_btnless = (otp[1][1] >> 3) & 0x01;
2386
2387 if (is_btnless)
2388 priv->flags |= ALPS_BUTTONPAD;
2389
2390 return 0;
2391}
2392
2393static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2394 struct alps_data *priv)
2395{
2396 unsigned char otp[2][4];
2397
2398 memset(otp, 0, sizeof(otp));
2399
2400 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2401 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2402 return -1;
2403
2404 alps_update_device_area_ss4_v2(otp, priv);
2405
2406 alps_update_btn_info_ss4_v2(otp, priv);
2407
2408 return 0;
2409}
2410
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002411static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2412 struct alps_data *priv)
2413{
2414 struct ps2dev *ps2dev = &psmouse->ps2dev;
2415 unsigned char param[4] = {0};
2416 int num_x_electrode, num_y_electrode;
2417
2418 if (alps_enter_command_mode(psmouse))
2419 return -1;
2420
2421 param[0] = 0x0a;
2422 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2423 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2424 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2425 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2426 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2427 return -1;
2428
2429 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2430 return -1;
2431
2432 /*
2433 * Dolphin's sensor line number is not fixed. It can be calculated
2434 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2435 * Further more, we can get device's x_max and y_max by multiplying
2436 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2437 *
2438 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2439 * real sensor line number X = 11 + 8 = 19, and
2440 * real sensor line number Y = 8 + 1 = 9.
2441 * So, x_max = (19 - 1) * 64 = 1152, and
2442 * y_max = (9 - 1) * 64 = 512.
2443 */
2444 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2445 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2446 priv->x_bits = num_x_electrode;
2447 priv->y_bits = num_y_electrode;
2448 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2449 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2450
2451 if (alps_exit_command_mode(psmouse))
2452 return -1;
2453
2454 return 0;
2455}
2456
Dave Turvene75af9e52013-02-21 22:58:28 -08002457static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2458{
2459 struct ps2dev *ps2dev = &psmouse->ps2dev;
2460 unsigned char param[2];
2461
2462 /* This is dolphin "v1" as empirically defined by florin9doi */
2463 param[0] = 0x64;
2464 param[1] = 0x28;
2465
2466 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2467 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2468 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2469 return -1;
2470
2471 return 0;
2472}
2473
Yunkang Tang38088432014-07-26 13:51:41 -07002474static int alps_hw_init_v7(struct psmouse *psmouse)
2475{
2476 struct ps2dev *ps2dev = &psmouse->ps2dev;
2477 int reg_val, ret = -1;
2478
2479 if (alps_enter_command_mode(psmouse) ||
2480 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2481 goto error;
2482
Hans de Goedef3f33c62014-07-29 11:22:07 -07002483 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2484 goto error;
2485
Yunkang Tang38088432014-07-26 13:51:41 -07002486 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2487 goto error;
2488
2489 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2490 if (reg_val == -1)
2491 goto error;
2492 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2493 goto error;
2494
2495 alps_exit_command_mode(psmouse);
2496 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2497
2498error:
2499 alps_exit_command_mode(psmouse);
2500 return ret;
2501}
2502
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002503static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2504{
2505 struct ps2dev *ps2dev = &psmouse->ps2dev;
2506 char param[2] = {0x64, 0x28};
2507 int ret = -1;
2508
2509 /* enter absolute mode */
2510 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2511 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2512 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2513 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2514 goto error;
2515 }
2516
2517 /* T.B.D. Decread noise packet number, delete in the future */
2518 if (alps_exit_command_mode(psmouse) ||
2519 alps_enter_command_mode(psmouse) ||
2520 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2521 goto error;
2522 }
2523 alps_exit_command_mode(psmouse);
2524
2525 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2526
2527error:
2528 alps_exit_command_mode(psmouse);
2529 return ret;
2530}
2531
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002532static int alps_set_protocol(struct psmouse *psmouse,
2533 struct alps_data *priv,
2534 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002535{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002536 psmouse->private = priv;
2537
2538 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2539
2540 priv->proto_version = protocol->version;
2541 priv->byte0 = protocol->byte0;
2542 priv->mask0 = protocol->mask0;
2543 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002544
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002545 priv->x_max = 2000;
2546 priv->y_max = 1400;
2547 priv->x_bits = 15;
2548 priv->y_bits = 11;
2549
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002550 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002551 case ALPS_PROTO_V1:
2552 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002553 priv->hw_init = alps_hw_init_v1_v2;
2554 priv->process_packet = alps_process_packet_v1_v2;
2555 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002556 priv->x_max = 1023;
2557 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002558 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002559
Seth Forshee25bded72011-11-07 19:53:36 -08002560 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002561 priv->hw_init = alps_hw_init_v3;
2562 priv->process_packet = alps_process_packet_v3;
2563 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002564 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002565 priv->nibble_commands = alps_v3_nibble_commands;
2566 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002567 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002568
2569 case ALPS_PROTO_V3_RUSHMORE:
2570 priv->hw_init = alps_hw_init_rushmore_v3;
2571 priv->process_packet = alps_process_packet_v3;
2572 priv->set_abs_params = alps_set_abs_params_mt;
2573 priv->decode_fields = alps_decode_rushmore;
2574 priv->nibble_commands = alps_v3_nibble_commands;
2575 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2576 priv->x_bits = 16;
2577 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002578
2579 if (alps_probe_trackstick_v3(psmouse,
2580 ALPS_REG_BASE_RUSHMORE) < 0)
2581 priv->flags &= ~ALPS_DUALPOINT;
2582
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002583 break;
2584
Seth Forshee25bded72011-11-07 19:53:36 -08002585 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002586 priv->hw_init = alps_hw_init_v4;
2587 priv->process_packet = alps_process_packet_v4;
2588 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002589 priv->nibble_commands = alps_v4_nibble_commands;
2590 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002591 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002592
Dave Turvene75af9e52013-02-21 22:58:28 -08002593 case ALPS_PROTO_V5:
2594 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002595 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002596 priv->decode_fields = alps_decode_dolphin;
2597 priv->set_abs_params = alps_set_abs_params_mt;
2598 priv->nibble_commands = alps_v3_nibble_commands;
2599 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002600 priv->x_bits = 23;
2601 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002602
2603 if (alps_dolphin_get_device_area(psmouse, priv))
2604 return -EIO;
2605
Dave Turvene75af9e52013-02-21 22:58:28 -08002606 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002607
Yunkang Tang95f75e92013-12-01 22:33:52 -08002608 case ALPS_PROTO_V6:
2609 priv->hw_init = alps_hw_init_v6;
2610 priv->process_packet = alps_process_packet_v6;
2611 priv->set_abs_params = alps_set_abs_params_st;
2612 priv->nibble_commands = alps_v6_nibble_commands;
2613 priv->x_max = 2047;
2614 priv->y_max = 1535;
2615 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002616
Yunkang Tang38088432014-07-26 13:51:41 -07002617 case ALPS_PROTO_V7:
2618 priv->hw_init = alps_hw_init_v7;
2619 priv->process_packet = alps_process_packet_v7;
2620 priv->decode_fields = alps_decode_packet_v7;
Masaki Ota8eccd392015-03-27 20:44:21 -07002621 priv->set_abs_params = alps_set_abs_params_v7;
Yunkang Tang38088432014-07-26 13:51:41 -07002622 priv->nibble_commands = alps_v3_nibble_commands;
2623 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002624 priv->x_max = 0xfff;
2625 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002626
2627 if (priv->fw_ver[1] != 0xba)
2628 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002629
Yunkang Tang38088432014-07-26 13:51:41 -07002630 break;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002631
2632 case ALPS_PROTO_V8:
2633 priv->hw_init = alps_hw_init_ss4_v2;
2634 priv->process_packet = alps_process_packet_ss4_v2;
2635 priv->decode_fields = alps_decode_ss4_v2;
2636 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2637 priv->nibble_commands = alps_v3_nibble_commands;
2638 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2639
2640 if (alps_set_defaults_ss4_v2(psmouse, priv))
2641 return -EIO;
2642
2643 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002644 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002645
2646 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002647}
2648
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002649static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2650 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002651{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002652 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002653 int i;
2654
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002655 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2656 model = &alps_model_data[i];
2657
2658 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2659 (!model->command_mode_resp ||
2660 model->command_mode_resp == ec[2])) {
2661
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002662 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002663 }
2664 }
2665
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002666 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002667}
2668
2669static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2670{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002671 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002672 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002673 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002674
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002675 /*
2676 * First try "E6 report".
2677 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2678 * The bits 0-2 of the first byte will be 1s if some buttons are
2679 * pressed.
2680 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002681 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2682 PSMOUSE_CMD_SETSCALE11, e6))
2683 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002684
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002685 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2686 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002687
2688 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002689 * Now get the "E7" and "EC" reports. These will uniquely identify
2690 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002691 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002692 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2693 PSMOUSE_CMD_SETSCALE21, e7) ||
2694 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2695 PSMOUSE_CMD_RESET_WRAP, ec) ||
2696 alps_exit_command_mode(psmouse))
2697 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002698
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002699 protocol = alps_match_table(e7, ec);
2700 if (!protocol) {
2701 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2702 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2703 protocol = &alps_v5_protocol_data;
2704 } else if (ec[0] == 0x88 &&
2705 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2706 protocol = &alps_v7_protocol_data;
2707 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2708 protocol = &alps_v3_rushmore_data;
2709 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2710 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2711 protocol = &alps_v3_protocol_data;
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002712 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2713 e7[2] == 0x14 && ec[1] == 0x02) {
2714 protocol = &alps_v8_protocol_data;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002715 } else {
2716 psmouse_dbg(psmouse,
2717 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2718 return -EINVAL;
2719 }
2720 }
2721
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002722 if (priv) {
2723 /* Save the Firmware version */
2724 memcpy(priv->fw_ver, ec, 3);
2725 error = alps_set_protocol(psmouse, priv, protocol);
2726 if (error)
2727 return error;
2728 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002729
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002730 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002731}
2732
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002733static int alps_reconnect(struct psmouse *psmouse)
2734{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002735 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002736
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002737 psmouse_reset(psmouse);
2738
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002739 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002740 return -1;
2741
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002742 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
2745static void alps_disconnect(struct psmouse *psmouse)
2746{
2747 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002750 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002751 if (priv->dev2)
2752 input_unregister_device(priv->dev2);
2753 if (!IS_ERR_OR_NULL(priv->dev3))
2754 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 kfree(priv);
2756}
2757
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002758static void alps_set_abs_params_st(struct alps_data *priv,
2759 struct input_dev *dev1)
2760{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002761 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2762 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Masaki Ota8eccd392015-03-27 20:44:21 -07002763 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002764}
2765
Masaki Ota8eccd392015-03-27 20:44:21 -07002766static void alps_set_abs_params_mt_common(struct alps_data *priv,
2767 struct input_dev *dev1)
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002768{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002769 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2770 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002771
Hans de Goedef3f33c62014-07-29 11:22:07 -07002772 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2773 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2774
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002775 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2776 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002777}
Yunkang Tang38088432014-07-26 13:51:41 -07002778
Masaki Ota8eccd392015-03-27 20:44:21 -07002779static void alps_set_abs_params_mt(struct alps_data *priv,
2780 struct input_dev *dev1)
2781{
2782 alps_set_abs_params_mt_common(priv, dev1);
2783 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2784
2785 input_mt_init_slots(dev1, MAX_TOUCHES,
2786 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2787 INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2788}
2789
2790static void alps_set_abs_params_v7(struct alps_data *priv,
2791 struct input_dev *dev1)
2792{
2793 alps_set_abs_params_mt_common(priv, dev1);
Masaki Ota8d289842015-03-27 21:30:46 -07002794 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
Masaki Ota8eccd392015-03-27 20:44:21 -07002795
2796 input_mt_init_slots(dev1, MAX_TOUCHES,
2797 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2798 INPUT_MT_TRACK);
Masaki Ota3db5b9f2015-03-27 20:57:52 -07002799
2800 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2801}
2802
2803static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2804 struct input_dev *dev1)
2805{
2806 alps_set_abs_params_mt_common(priv, dev1);
2807 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2808 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2809
2810 input_mt_init_slots(dev1, MAX_TOUCHES,
2811 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2812 INPUT_MT_TRACK);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002813}
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815int alps_init(struct psmouse *psmouse)
2816{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002817 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002818 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002819 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002821 error = priv->hw_init(psmouse);
2822 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 goto init_fail;
2824
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002825 /*
2826 * Undo part of setup done for us by psmouse core since touchpad
2827 * is not a relative device.
2828 */
2829 __clear_bit(EV_REL, dev1->evbit);
2830 __clear_bit(REL_X, dev1->relbit);
2831 __clear_bit(REL_Y, dev1->relbit);
2832
2833 /*
2834 * Now set up our capabilities.
2835 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002836 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2837 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2838 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002839 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2840 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002842 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002843
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002844 priv->set_abs_params(priv, dev1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002846 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002847 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2848 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002851 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002852 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2853 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
2855
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002856 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002857 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2858 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2859 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2860 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002861 } else if (priv->flags & ALPS_BUTTONPAD) {
2862 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2863 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002864 } else {
2865 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2866 }
2867
Pali Rohárdfba8602015-01-09 12:48:44 -08002868 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002869 struct input_dev *dev2;
2870
2871 dev2 = input_allocate_device();
2872 if (!dev2) {
2873 psmouse_err(psmouse,
2874 "failed to allocate trackstick device\n");
2875 error = -ENOMEM;
2876 goto init_fail;
2877 }
2878
2879 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2880 psmouse->ps2dev.serio->phys);
2881 dev2->phys = priv->phys2;
2882
Pali Rohárdfba8602015-01-09 12:48:44 -08002883 /*
2884 * format of input device name is: "protocol vendor name"
2885 * see function psmouse_switch_protocol() in psmouse-base.c
2886 */
2887 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002888
2889 dev2->id.bustype = BUS_I8042;
2890 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002891 dev2->id.product = PSMOUSE_ALPS;
2892 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002893 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002894
Pali Rohár04aae282015-01-14 13:18:30 -08002895 input_set_capability(dev2, EV_REL, REL_X);
2896 input_set_capability(dev2, EV_REL, REL_Y);
2897 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2898 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2899 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002900
Pali Rohár04aae282015-01-14 13:18:30 -08002901 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002902 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2903
Pali Rohár04aae282015-01-14 13:18:30 -08002904 error = input_register_device(dev2);
2905 if (error) {
2906 psmouse_err(psmouse,
2907 "failed to register trackstick device: %d\n",
2908 error);
2909 input_free_device(dev2);
2910 goto init_fail;
2911 }
2912
2913 priv->dev2 = dev2;
2914 }
2915
2916 priv->psmouse = psmouse;
2917
2918 INIT_DELAYED_WORK(&priv->dev3_register_work,
2919 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002922 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 psmouse->disconnect = alps_disconnect;
2924 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002925 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002927 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2928 psmouse->resync_time = 0;
2929
Pali Rohár9d720b32014-11-08 12:58:57 -08002930 /* Allow 2 invalid packets without resetting device */
2931 psmouse->resetafter = psmouse->pktsize * 2;
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return 0;
2934
2935init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002936 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002937 /*
2938 * Even though we did not allocate psmouse->private we do free
2939 * it here.
2940 */
2941 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002942 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002946int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002948 struct alps_data *priv;
2949 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002951 error = alps_identify(psmouse, NULL);
2952 if (error)
2953 return error;
2954
2955 /*
2956 * Reset the device to make sure it is fully operational:
2957 * on some laptops, like certain Dell Latitudes, we may
2958 * fail to properly detect presence of trackstick if device
2959 * has not been reset.
2960 */
2961 psmouse_reset(psmouse);
2962
2963 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2964 if (!priv)
2965 return -ENOMEM;
2966
2967 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08002968 if (error) {
2969 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002970 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08002971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 if (set_properties) {
2974 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002975 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002976 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002977 psmouse->model = priv->proto_version;
2978 } else {
2979 /*
2980 * Destroy alps_data structure we allocated earlier since
2981 * this was just a "trial run". Otherwise we'll keep it
2982 * to be used by alps_init() which has to be called if
2983 * we succeed and set_properties is true.
2984 */
2985 kfree(priv);
2986 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return 0;
2990}
2991