Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ALPS touchpad PS/2 mouse driver
4 *
5 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
6 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
7 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
8 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
9 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
10 *
11 * ALPS detection, tap switching and status querying info is taken from
12 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
13 */
14
15#include <linux/slab.h>
16#include <linux/input.h>
17#include <linux/input/mt.h>
18#include <linux/serio.h>
19#include <linux/libps2.h>
20#include <linux/dmi.h>
21
22#include "psmouse.h"
23#include "alps.h"
24
25/*
26 * Definitions for ALPS version 3 and 4 command mode protocol
27 */
28#define ALPS_CMD_NIBBLE_10 0x01f2
29
30#define ALPS_REG_BASE_RUSHMORE 0xc2c0
31#define ALPS_REG_BASE_V7 0xc2c0
32#define ALPS_REG_BASE_PINNACLE 0x0000
33
34static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
35 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
36 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
37 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
38 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
39 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
40 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
41 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
42 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
43 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
44 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
45 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
46 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
47 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
48 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
49 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
50 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
51};
52
53static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
54 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
55 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
56 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
57 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
58 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
59 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
60 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
61 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
62 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
63 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
64 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
65 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
66 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
67 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
68 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
69 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
70};
71
72static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
73 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
74 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
75 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
76 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
77 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
78 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
79 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
80 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
81 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
82 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
83 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
84 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
85 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
86 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
87 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
88 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
89};
90
91
92#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
93#define ALPS_PASS 0x04 /* device has a pass-through port */
94
95#define ALPS_WHEEL 0x08 /* hardware wheel present */
96#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
97#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
98#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
99#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
100 6-byte ALPS packet */
101#define ALPS_STICK_BITS 0x100 /* separate stick button bits */
102#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
103#define ALPS_DUALPOINT_WITH_PRESSURE 0x400 /* device can report trackpoint pressure */
104
105static const struct alps_model_info alps_model_data[] = {
106 /*
107 * XXX This entry is suspicious. First byte has zero lower nibble,
108 * which is what a normal mouse would report. Also, the value 0x0e
109 * isn't valid per PS/2 spec.
110 */
111 { { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
112
113 { { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
114 { { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
115 { { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
116 { { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
117 { { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff,
118 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
119 { { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
120 { { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
121 { { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
122 { { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf,
123 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
124 { { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
125 { { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
126 { { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
127 { { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
128 { { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
129 { { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
130 { { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
131 { { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
132 { { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
133 { { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
134 { { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
135 { { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
136};
137
138static const struct alps_protocol_info alps_v3_protocol_data = {
139 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
140};
141
142static const struct alps_protocol_info alps_v3_rushmore_data = {
143 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
144};
145
146static const struct alps_protocol_info alps_v4_protocol_data = {
147 ALPS_PROTO_V4, 0x8f, 0x8f, 0
148};
149
150static const struct alps_protocol_info alps_v5_protocol_data = {
151 ALPS_PROTO_V5, 0xc8, 0xd8, 0
152};
153
154static const struct alps_protocol_info alps_v7_protocol_data = {
155 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
156};
157
158static const struct alps_protocol_info alps_v8_protocol_data = {
159 ALPS_PROTO_V8, 0x18, 0x18, 0
160};
161
162static const struct alps_protocol_info alps_v9_protocol_data = {
163 ALPS_PROTO_V9, 0xc8, 0xc8, 0
164};
165
166/*
167 * Some v2 models report the stick buttons in separate bits
168 */
169static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
170#if defined(CONFIG_DMI) && defined(CONFIG_X86)
171 {
172 /* Extrapolated from other entries */
173 .matches = {
174 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
175 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
176 },
177 },
178 {
179 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
180 .matches = {
181 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
182 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
183 },
184 },
185 {
186 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
187 .matches = {
188 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
189 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
190 },
191 },
192 {
193 /* Extrapolated from other entries */
194 .matches = {
195 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
196 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
197 },
198 },
199#endif
200 { }
201};
202
203static void alps_set_abs_params_st(struct alps_data *priv,
204 struct input_dev *dev1);
205static void alps_set_abs_params_semi_mt(struct alps_data *priv,
206 struct input_dev *dev1);
207static void alps_set_abs_params_v7(struct alps_data *priv,
208 struct input_dev *dev1);
209static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
210 struct input_dev *dev1);
211
212/* Packet formats are described in Documentation/input/devices/alps.rst */
213
214static bool alps_is_valid_first_byte(struct alps_data *priv,
215 unsigned char data)
216{
217 return (data & priv->mask0) == priv->byte0;
218}
219
220static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
221 int left, int right, int middle)
222{
223 struct input_dev *dev;
224
225 /*
226 * If shared button has already been reported on the
227 * other device (dev2) then this event should be also
228 * sent through that device.
229 */
230 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
231 input_report_key(dev, BTN_LEFT, left);
232
233 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
234 input_report_key(dev, BTN_RIGHT, right);
235
236 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
237 input_report_key(dev, BTN_MIDDLE, middle);
238
239 /*
240 * Sync the _other_ device now, we'll do the first
241 * device later once we report the rest of the events.
242 */
243 if (dev2)
244 input_sync(dev2);
245}
246
247static void alps_process_packet_v1_v2(struct psmouse *psmouse)
248{
249 struct alps_data *priv = psmouse->private;
250 unsigned char *packet = psmouse->packet;
251 struct input_dev *dev = psmouse->dev;
252 struct input_dev *dev2 = priv->dev2;
253 int x, y, z, ges, fin, left, right, middle;
254 int back = 0, forward = 0;
255
256 if (priv->proto_version == ALPS_PROTO_V1) {
257 left = packet[2] & 0x10;
258 right = packet[2] & 0x08;
259 middle = 0;
260 x = packet[1] | ((packet[0] & 0x07) << 7);
261 y = packet[4] | ((packet[3] & 0x07) << 7);
262 z = packet[5];
263 } else {
264 left = packet[3] & 1;
265 right = packet[3] & 2;
266 middle = packet[3] & 4;
267 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
268 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
269 z = packet[5];
270 }
271
272 if (priv->flags & ALPS_FW_BK_1) {
273 back = packet[0] & 0x10;
274 forward = packet[2] & 4;
275 }
276
277 if (priv->flags & ALPS_FW_BK_2) {
278 back = packet[3] & 4;
279 forward = packet[2] & 4;
280 if ((middle = forward && back))
281 forward = back = 0;
282 }
283
284 ges = packet[2] & 1;
285 fin = packet[2] & 2;
286
287 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
288 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
289 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
290
291 alps_report_buttons(dev2, dev, left, right, middle);
292
293 input_sync(dev2);
294 return;
295 }
296
297 /* Some models have separate stick button bits */
298 if (priv->flags & ALPS_STICK_BITS) {
299 left |= packet[0] & 1;
300 right |= packet[0] & 2;
301 middle |= packet[0] & 4;
302 }
303
304 alps_report_buttons(dev, dev2, left, right, middle);
305
306 /* Convert hardware tap to a reasonable Z value */
307 if (ges && !fin)
308 z = 40;
309
310 /*
311 * A "tap and drag" operation is reported by the hardware as a transition
312 * from (!fin && ges) to (fin && ges). This should be translated to the
313 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
314 */
315 if (ges && fin && !priv->prev_fin) {
316 input_report_abs(dev, ABS_X, x);
317 input_report_abs(dev, ABS_Y, y);
318 input_report_abs(dev, ABS_PRESSURE, 0);
319 input_report_key(dev, BTN_TOOL_FINGER, 0);
320 input_sync(dev);
321 }
322 priv->prev_fin = fin;
323
324 if (z > 30)
325 input_report_key(dev, BTN_TOUCH, 1);
326 if (z < 25)
327 input_report_key(dev, BTN_TOUCH, 0);
328
329 if (z > 0) {
330 input_report_abs(dev, ABS_X, x);
331 input_report_abs(dev, ABS_Y, y);
332 }
333
334 input_report_abs(dev, ABS_PRESSURE, z);
335 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
336
337 if (priv->flags & ALPS_WHEEL)
338 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
339
340 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
341 input_report_key(dev, BTN_FORWARD, forward);
342 input_report_key(dev, BTN_BACK, back);
343 }
344
345 if (priv->flags & ALPS_FOUR_BUTTONS) {
346 input_report_key(dev, BTN_0, packet[2] & 4);
347 input_report_key(dev, BTN_1, packet[0] & 0x10);
348 input_report_key(dev, BTN_2, packet[3] & 4);
349 input_report_key(dev, BTN_3, packet[0] & 0x20);
350 }
351
352 input_sync(dev);
353}
354
355static void alps_get_bitmap_points(unsigned int map,
356 struct alps_bitmap_point *low,
357 struct alps_bitmap_point *high,
358 int *fingers)
359{
360 struct alps_bitmap_point *point;
361 int i, bit, prev_bit = 0;
362
363 point = low;
364 for (i = 0; map != 0; i++, map >>= 1) {
365 bit = map & 1;
366 if (bit) {
367 if (!prev_bit) {
368 point->start_bit = i;
369 point->num_bits = 0;
370 (*fingers)++;
371 }
372 point->num_bits++;
373 } else {
374 if (prev_bit)
375 point = high;
376 }
377 prev_bit = bit;
378 }
379}
380
381/*
382 * Process bitmap data from semi-mt protocols. Returns the number of
383 * fingers detected. A return value of 0 means at least one of the
384 * bitmaps was empty.
385 *
386 * The bitmaps don't have enough data to track fingers, so this function
387 * only generates points representing a bounding box of all contacts.
388 * These points are returned in fields->mt when the return value
389 * is greater than 0.
390 */
391static int alps_process_bitmap(struct alps_data *priv,
392 struct alps_fields *fields)
393{
394 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
395 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
396 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
397 struct input_mt_pos corner[4];
398
399 if (!fields->x_map || !fields->y_map)
400 return 0;
401
402 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
403 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
404
405 /*
406 * Fingers can overlap, so we use the maximum count of fingers
407 * on either axis as the finger count.
408 */
409 fingers = max(fingers_x, fingers_y);
410
411 /*
412 * If an axis reports only a single contact, we have overlapping or
413 * adjacent fingers. Divide the single contact between the two points.
414 */
415 if (fingers_x == 1) {
416 i = (x_low.num_bits - 1) / 2;
417 x_low.num_bits = x_low.num_bits - i;
418 x_high.start_bit = x_low.start_bit + i;
419 x_high.num_bits = max(i, 1);
420 }
421 if (fingers_y == 1) {
422 i = (y_low.num_bits - 1) / 2;
423 y_low.num_bits = y_low.num_bits - i;
424 y_high.start_bit = y_low.start_bit + i;
425 y_high.num_bits = max(i, 1);
426 }
427
428 /* top-left corner */
429 corner[0].x =
430 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
431 (2 * (priv->x_bits - 1));
432 corner[0].y =
433 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
434 (2 * (priv->y_bits - 1));
435
436 /* top-right corner */
437 corner[1].x =
438 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
439 (2 * (priv->x_bits - 1));
440 corner[1].y =
441 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
442 (2 * (priv->y_bits - 1));
443
444 /* bottom-right corner */
445 corner[2].x =
446 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
447 (2 * (priv->x_bits - 1));
448 corner[2].y =
449 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
450 (2 * (priv->y_bits - 1));
451
452 /* bottom-left corner */
453 corner[3].x =
454 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
455 (2 * (priv->x_bits - 1));
456 corner[3].y =
457 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
458 (2 * (priv->y_bits - 1));
459
460 /* x-bitmap order is reversed on v5 touchpads */
461 if (priv->proto_version == ALPS_PROTO_V5) {
462 for (i = 0; i < 4; i++)
463 corner[i].x = priv->x_max - corner[i].x;
464 }
465
466 /* y-bitmap order is reversed on v3 and v4 touchpads */
467 if (priv->proto_version == ALPS_PROTO_V3 ||
468 priv->proto_version == ALPS_PROTO_V4) {
469 for (i = 0; i < 4; i++)
470 corner[i].y = priv->y_max - corner[i].y;
471 }
472
473 /*
474 * We only select a corner for the second touch once per 2 finger
475 * touch sequence to avoid the chosen corner (and thus the coordinates)
476 * jumping around when the first touch is in the middle.
477 */
478 if (priv->second_touch == -1) {
479 /* Find corner closest to our st coordinates */
480 closest = 0x7fffffff;
481 for (i = 0; i < 4; i++) {
482 int dx = fields->st.x - corner[i].x;
483 int dy = fields->st.y - corner[i].y;
484 int distance = dx * dx + dy * dy;
485
486 if (distance < closest) {
487 priv->second_touch = i;
488 closest = distance;
489 }
490 }
491 /* And select the opposite corner to use for the 2nd touch */
492 priv->second_touch = (priv->second_touch + 2) % 4;
493 }
494
495 fields->mt[0] = fields->st;
496 fields->mt[1] = corner[priv->second_touch];
497
498 return fingers;
499}
500
501static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
502{
503 input_mt_slot(dev, slot);
504 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
505 input_report_abs(dev, ABS_MT_POSITION_X, x);
506 input_report_abs(dev, ABS_MT_POSITION_Y, y);
507}
508
509static void alps_report_mt_data(struct psmouse *psmouse, int n)
510{
511 struct alps_data *priv = psmouse->private;
512 struct input_dev *dev = psmouse->dev;
513 struct alps_fields *f = &priv->f;
514 int i, slot[MAX_TOUCHES];
515
516 input_mt_assign_slots(dev, slot, f->mt, n, 0);
517 for (i = 0; i < n; i++)
518 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
519
520 input_mt_sync_frame(dev);
521}
522
523static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
524{
525 struct alps_data *priv = psmouse->private;
526 struct input_dev *dev = psmouse->dev;
527 struct alps_fields *f = &priv->f;
528
529 /* Use st data when we don't have mt data */
530 if (fingers < 2) {
531 f->mt[0].x = f->st.x;
532 f->mt[0].y = f->st.y;
533 fingers = f->pressure > 0 ? 1 : 0;
534 priv->second_touch = -1;
535 }
536
537 if (fingers >= 1)
538 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
539 if (fingers >= 2)
540 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
541 input_mt_sync_frame(dev);
542
543 input_mt_report_finger_count(dev, fingers);
544
545 input_report_key(dev, BTN_LEFT, f->left);
546 input_report_key(dev, BTN_RIGHT, f->right);
547 input_report_key(dev, BTN_MIDDLE, f->middle);
548
549 input_report_abs(dev, ABS_PRESSURE, f->pressure);
550
551 input_sync(dev);
552}
553
554static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
555{
556 struct alps_data *priv = psmouse->private;
557 unsigned char *packet = psmouse->packet;
558 struct input_dev *dev = priv->dev2;
559 int x, y, z, left, right, middle;
560
561 /* It should be a DualPoint when received trackstick packet */
562 if (!(priv->flags & ALPS_DUALPOINT)) {
563 psmouse_warn(psmouse,
564 "Rejected trackstick packet from non DualPoint device");
565 return;
566 }
567
568 /* Sanity check packet */
569 if (!(packet[0] & 0x40)) {
570 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
571 return;
572 }
573
574 /*
575 * There's a special packet that seems to indicate the end
576 * of a stream of trackstick data. Filter these out.
577 */
578 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
579 return;
580
581 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
582 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
583 z = packet[4] & 0x7f;
584
585 /*
586 * The x and y values tend to be quite large, and when used
587 * alone the trackstick is difficult to use. Scale them down
588 * to compensate.
589 */
590 x /= 8;
591 y /= 8;
592
593 input_report_rel(dev, REL_X, x);
594 input_report_rel(dev, REL_Y, -y);
595 input_report_abs(dev, ABS_PRESSURE, z);
596
597 /*
598 * Most ALPS models report the trackstick buttons in the touchpad
599 * packets, but a few report them here. No reliable way has been
600 * found to differentiate between the models upfront, so we enable
601 * the quirk in response to seeing a button press in the trackstick
602 * packet.
603 */
604 left = packet[3] & 0x01;
605 right = packet[3] & 0x02;
606 middle = packet[3] & 0x04;
607
608 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
609 (left || right || middle))
610 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
611
612 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
613 input_report_key(dev, BTN_LEFT, left);
614 input_report_key(dev, BTN_RIGHT, right);
615 input_report_key(dev, BTN_MIDDLE, middle);
616 }
617
618 input_sync(dev);
619 return;
620}
621
622static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
623{
624 f->left = !!(p[3] & 0x01);
625 f->right = !!(p[3] & 0x02);
626 f->middle = !!(p[3] & 0x04);
627
628 f->ts_left = !!(p[3] & 0x10);
629 f->ts_right = !!(p[3] & 0x20);
630 f->ts_middle = !!(p[3] & 0x40);
631}
632
633static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
634 struct psmouse *psmouse)
635{
636 f->first_mp = !!(p[4] & 0x40);
637 f->is_mp = !!(p[0] & 0x40);
638
639 if (f->is_mp) {
640 f->fingers = (p[5] & 0x3) + 1;
641 f->x_map = ((p[4] & 0x7e) << 8) |
642 ((p[1] & 0x7f) << 2) |
643 ((p[0] & 0x30) >> 4);
644 f->y_map = ((p[3] & 0x70) << 4) |
645 ((p[2] & 0x7f) << 1) |
646 (p[4] & 0x01);
647 } else {
648 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
649 ((p[0] & 0x30) >> 4);
650 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
651 f->pressure = p[5] & 0x7f;
652
653 alps_decode_buttons_v3(f, p);
654 }
655
656 return 0;
657}
658
659static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
660 struct psmouse *psmouse)
661{
662 f->first_mp = !!(p[4] & 0x40);
663 f->is_mp = !!(p[5] & 0x40);
664
665 if (f->is_mp) {
666 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
667 f->x_map = ((p[5] & 0x10) << 11) |
668 ((p[4] & 0x7e) << 8) |
669 ((p[1] & 0x7f) << 2) |
670 ((p[0] & 0x30) >> 4);
671 f->y_map = ((p[5] & 0x20) << 6) |
672 ((p[3] & 0x70) << 4) |
673 ((p[2] & 0x7f) << 1) |
674 (p[4] & 0x01);
675 } else {
676 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
677 ((p[0] & 0x30) >> 4);
678 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
679 f->pressure = p[5] & 0x7f;
680
681 alps_decode_buttons_v3(f, p);
682 }
683
684 return 0;
685}
686
687static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
688 struct psmouse *psmouse)
689{
690 u64 palm_data = 0;
691 struct alps_data *priv = psmouse->private;
692
693 f->first_mp = !!(p[0] & 0x02);
694 f->is_mp = !!(p[0] & 0x20);
695
696 if (!f->is_mp) {
697 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
698 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
699 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
700 alps_decode_buttons_v3(f, p);
701 } else {
702 f->fingers = ((p[0] & 0x6) >> 1 |
703 (p[0] & 0x10) >> 2);
704
705 palm_data = (p[1] & 0x7f) |
706 ((p[2] & 0x7f) << 7) |
707 ((p[4] & 0x7f) << 14) |
708 ((p[5] & 0x7f) << 21) |
709 ((p[3] & 0x07) << 28) |
710 (((u64)p[3] & 0x70) << 27) |
711 (((u64)p[0] & 0x01) << 34);
712
713 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
714 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
715
716 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
717 f->x_map = (palm_data >> priv->y_bits) &
718 (BIT(priv->x_bits) - 1);
719 }
720
721 return 0;
722}
723
724static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
725{
726 struct alps_data *priv = psmouse->private;
727 unsigned char *packet = psmouse->packet;
728 struct input_dev *dev2 = priv->dev2;
729 struct alps_fields *f = &priv->f;
730 int fingers = 0;
731
732 memset(f, 0, sizeof(*f));
733
734 priv->decode_fields(f, packet, psmouse);
735
736 /*
737 * There's no single feature of touchpad position and bitmap packets
738 * that can be used to distinguish between them. We rely on the fact
739 * that a bitmap packet should always follow a position packet with
740 * bit 6 of packet[4] set.
741 */
742 if (priv->multi_packet) {
743 /*
744 * Sometimes a position packet will indicate a multi-packet
745 * sequence, but then what follows is another position
746 * packet. Check for this, and when it happens process the
747 * position packet as usual.
748 */
749 if (f->is_mp) {
750 fingers = f->fingers;
751 /*
752 * Bitmap processing uses position packet's coordinate
753 * data, so we need to do decode it first.
754 */
755 priv->decode_fields(f, priv->multi_data, psmouse);
756 if (alps_process_bitmap(priv, f) == 0)
757 fingers = 0; /* Use st data */
758 } else {
759 priv->multi_packet = 0;
760 }
761 }
762
763 /*
764 * Bit 6 of byte 0 is not usually set in position packets. The only
765 * times it seems to be set is in situations where the data is
766 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
767 * this combined with the fact that this bit is useful for filtering
768 * out misidentified bitmap packets, we reject anything with this
769 * bit set.
770 */
771 if (f->is_mp)
772 return;
773
774 if (!priv->multi_packet && f->first_mp) {
775 priv->multi_packet = 1;
776 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
777 return;
778 }
779
780 priv->multi_packet = 0;
781
782 /*
783 * Sometimes the hardware sends a single packet with z = 0
784 * in the middle of a stream. Real releases generate packets
785 * with x, y, and z all zero, so these seem to be flukes.
786 * Ignore them.
787 */
788 if (f->st.x && f->st.y && !f->pressure)
789 return;
790
791 alps_report_semi_mt_data(psmouse, fingers);
792
793 if ((priv->flags & ALPS_DUALPOINT) &&
794 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
795 input_report_key(dev2, BTN_LEFT, f->ts_left);
796 input_report_key(dev2, BTN_RIGHT, f->ts_right);
797 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
798 input_sync(dev2);
799 }
800}
801
802static void alps_process_packet_v3(struct psmouse *psmouse)
803{
804 unsigned char *packet = psmouse->packet;
805
806 /*
807 * v3 protocol packets come in three types, two representing
808 * touchpad data and one representing trackstick data.
809 * Trackstick packets seem to be distinguished by always
810 * having 0x3f in the last byte. This value has never been
811 * observed in the last byte of either of the other types
812 * of packets.
813 */
814 if (packet[5] == 0x3f) {
815 alps_process_trackstick_packet_v3(psmouse);
816 return;
817 }
818
819 alps_process_touchpad_packet_v3_v5(psmouse);
820}
821
822static void alps_process_packet_v6(struct psmouse *psmouse)
823{
824 struct alps_data *priv = psmouse->private;
825 unsigned char *packet = psmouse->packet;
826 struct input_dev *dev = psmouse->dev;
827 struct input_dev *dev2 = priv->dev2;
828 int x, y, z;
829
830 /*
831 * We can use Byte5 to distinguish if the packet is from Touchpad
832 * or Trackpoint.
833 * Touchpad: 0 - 0x7E
834 * Trackpoint: 0x7F
835 */
836 if (packet[5] == 0x7F) {
837 /* It should be a DualPoint when received Trackpoint packet */
838 if (!(priv->flags & ALPS_DUALPOINT)) {
839 psmouse_warn(psmouse,
840 "Rejected trackstick packet from non DualPoint device");
841 return;
842 }
843
844 /* Trackpoint packet */
845 x = packet[1] | ((packet[3] & 0x20) << 2);
846 y = packet[2] | ((packet[3] & 0x40) << 1);
847 z = packet[4];
848
849 /* To prevent the cursor jump when finger lifted */
850 if (x == 0x7F && y == 0x7F && z == 0x7F)
851 x = y = z = 0;
852
853 /* Divide 4 since trackpoint's speed is too fast */
854 input_report_rel(dev2, REL_X, (char)x / 4);
855 input_report_rel(dev2, REL_Y, -((char)y / 4));
856
857 psmouse_report_standard_buttons(dev2, packet[3]);
858
859 input_sync(dev2);
860 return;
861 }
862
863 /* Touchpad packet */
864 x = packet[1] | ((packet[3] & 0x78) << 4);
865 y = packet[2] | ((packet[4] & 0x78) << 4);
866 z = packet[5];
867
868 if (z > 30)
869 input_report_key(dev, BTN_TOUCH, 1);
870 if (z < 25)
871 input_report_key(dev, BTN_TOUCH, 0);
872
873 if (z > 0) {
874 input_report_abs(dev, ABS_X, x);
875 input_report_abs(dev, ABS_Y, y);
876 }
877
878 input_report_abs(dev, ABS_PRESSURE, z);
879 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
880
881 /* v6 touchpad does not have middle button */
882 packet[3] &= ~BIT(2);
883 psmouse_report_standard_buttons(dev2, packet[3]);
884
885 input_sync(dev);
886}
887
888static void alps_process_packet_v4(struct psmouse *psmouse)
889{
890 struct alps_data *priv = psmouse->private;
891 unsigned char *packet = psmouse->packet;
892 struct alps_fields *f = &priv->f;
893 int offset;
894
895 /*
896 * v4 has a 6-byte encoding for bitmap data, but this data is
897 * broken up between 3 normal packets. Use priv->multi_packet to
898 * track our position in the bitmap packet.
899 */
900 if (packet[6] & 0x40) {
901 /* sync, reset position */
902 priv->multi_packet = 0;
903 }
904
905 if (WARN_ON_ONCE(priv->multi_packet > 2))
906 return;
907
908 offset = 2 * priv->multi_packet;
909 priv->multi_data[offset] = packet[6];
910 priv->multi_data[offset + 1] = packet[7];
911
912 f->left = !!(packet[4] & 0x01);
913 f->right = !!(packet[4] & 0x02);
914
915 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
916 ((packet[0] & 0x30) >> 4);
917 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
918 f->pressure = packet[5] & 0x7f;
919
920 if (++priv->multi_packet > 2) {
921 priv->multi_packet = 0;
922
923 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
924 ((priv->multi_data[3] & 0x60) << 3) |
925 ((priv->multi_data[0] & 0x3f) << 2) |
926 ((priv->multi_data[1] & 0x60) >> 5);
927 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
928 ((priv->multi_data[3] & 0x1f) << 5) |
929 (priv->multi_data[1] & 0x1f);
930
931 f->fingers = alps_process_bitmap(priv, f);
932 }
933
934 alps_report_semi_mt_data(psmouse, f->fingers);
935}
936
937static bool alps_is_valid_package_v7(struct psmouse *psmouse)
938{
939 switch (psmouse->pktcnt) {
940 case 3:
941 return (psmouse->packet[2] & 0x40) == 0x40;
942 case 4:
943 return (psmouse->packet[3] & 0x48) == 0x48;
944 case 6:
945 return (psmouse->packet[5] & 0x40) == 0x00;
946 }
947 return true;
948}
949
950static unsigned char alps_get_packet_id_v7(char *byte)
951{
952 unsigned char packet_id;
953
954 if (byte[4] & 0x40)
955 packet_id = V7_PACKET_ID_TWO;
956 else if (byte[4] & 0x01)
957 packet_id = V7_PACKET_ID_MULTI;
958 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
959 packet_id = V7_PACKET_ID_NEW;
960 else if (byte[1] == 0x00 && byte[4] == 0x00)
961 packet_id = V7_PACKET_ID_IDLE;
962 else
963 packet_id = V7_PACKET_ID_UNKNOWN;
964
965 return packet_id;
966}
967
968static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
969 unsigned char *pkt,
970 unsigned char pkt_id)
971{
972 mt[0].x = ((pkt[2] & 0x80) << 4);
973 mt[0].x |= ((pkt[2] & 0x3F) << 5);
974 mt[0].x |= ((pkt[3] & 0x30) >> 1);
975 mt[0].x |= (pkt[3] & 0x07);
976 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
977
978 mt[1].x = ((pkt[3] & 0x80) << 4);
979 mt[1].x |= ((pkt[4] & 0x80) << 3);
980 mt[1].x |= ((pkt[4] & 0x3F) << 4);
981 mt[1].y = ((pkt[5] & 0x80) << 3);
982 mt[1].y |= ((pkt[5] & 0x3F) << 4);
983
984 switch (pkt_id) {
985 case V7_PACKET_ID_TWO:
986 mt[1].x &= ~0x000F;
987 mt[1].y |= 0x000F;
988 /* Detect false-postive touches where x & y report max value */
989 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
990 mt[1].x = 0;
991 /* y gets set to 0 at the end of this function */
992 }
993 break;
994
995 case V7_PACKET_ID_MULTI:
996 mt[1].x &= ~0x003F;
997 mt[1].y &= ~0x0020;
998 mt[1].y |= ((pkt[4] & 0x02) << 4);
999 mt[1].y |= 0x001F;
1000 break;
1001
1002 case V7_PACKET_ID_NEW:
1003 mt[1].x &= ~0x003F;
1004 mt[1].x |= (pkt[0] & 0x20);
1005 mt[1].y |= 0x000F;
1006 break;
1007 }
1008
1009 mt[0].y = 0x7FF - mt[0].y;
1010 mt[1].y = 0x7FF - mt[1].y;
1011}
1012
1013static int alps_get_mt_count(struct input_mt_pos *mt)
1014{
1015 int i, fingers = 0;
1016
1017 for (i = 0; i < MAX_TOUCHES; i++) {
1018 if (mt[i].x != 0 || mt[i].y != 0)
1019 fingers++;
1020 }
1021
1022 return fingers;
1023}
1024
1025static int alps_decode_packet_v7(struct alps_fields *f,
1026 unsigned char *p,
1027 struct psmouse *psmouse)
1028{
1029 struct alps_data *priv = psmouse->private;
1030 unsigned char pkt_id;
1031
1032 pkt_id = alps_get_packet_id_v7(p);
1033 if (pkt_id == V7_PACKET_ID_IDLE)
1034 return 0;
1035 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1036 return -1;
1037 /*
1038 * NEW packets are send to indicate a discontinuity in the finger
1039 * coordinate reporting. Specifically a finger may have moved from
1040 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1041 * us.
1042 *
1043 * NEW packets have 3 problems:
1044 * 1) They do not contain middle / right button info (on non clickpads)
1045 * this can be worked around by preserving the old button state
1046 * 2) They do not contain an accurate fingercount, and they are
1047 * typically send when the number of fingers changes. We cannot use
1048 * the old finger count as that may mismatch with the amount of
1049 * touch coordinates we've available in the NEW packet
1050 * 3) Their x data for the second touch is inaccurate leading to
1051 * a possible jump of the x coordinate by 16 units when the first
1052 * non NEW packet comes in
1053 * Since problems 2 & 3 cannot be worked around, just ignore them.
1054 */
1055 if (pkt_id == V7_PACKET_ID_NEW)
1056 return 1;
1057
1058 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1059
1060 if (pkt_id == V7_PACKET_ID_TWO)
1061 f->fingers = alps_get_mt_count(f->mt);
1062 else /* pkt_id == V7_PACKET_ID_MULTI */
1063 f->fingers = 3 + (p[5] & 0x03);
1064
1065 f->left = (p[0] & 0x80) >> 7;
1066 if (priv->flags & ALPS_BUTTONPAD) {
1067 if (p[0] & 0x20)
1068 f->fingers++;
1069 if (p[0] & 0x10)
1070 f->fingers++;
1071 } else {
1072 f->right = (p[0] & 0x20) >> 5;
1073 f->middle = (p[0] & 0x10) >> 4;
1074 }
1075
1076 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1077 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1078 f->mt[0].x = f->mt[1].x;
1079 f->mt[0].y = f->mt[1].y;
1080 f->mt[1].x = 0;
1081 f->mt[1].y = 0;
1082 }
1083
1084 return 0;
1085}
1086
1087static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1088{
1089 struct alps_data *priv = psmouse->private;
1090 unsigned char *packet = psmouse->packet;
1091 struct input_dev *dev2 = priv->dev2;
1092 int x, y, z;
1093
1094 /* It should be a DualPoint when received trackstick packet */
1095 if (!(priv->flags & ALPS_DUALPOINT)) {
1096 psmouse_warn(psmouse,
1097 "Rejected trackstick packet from non DualPoint device");
1098 return;
1099 }
1100
1101 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1102 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1103 ((packet[3] & 0x20) << 1);
1104 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1105
1106 input_report_rel(dev2, REL_X, (char)x);
1107 input_report_rel(dev2, REL_Y, -((char)y));
1108 input_report_abs(dev2, ABS_PRESSURE, z);
1109
1110 psmouse_report_standard_buttons(dev2, packet[1]);
1111
1112 input_sync(dev2);
1113}
1114
1115static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1116{
1117 struct alps_data *priv = psmouse->private;
1118 struct input_dev *dev = psmouse->dev;
1119 struct alps_fields *f = &priv->f;
1120
1121 memset(f, 0, sizeof(*f));
1122
1123 if (priv->decode_fields(f, psmouse->packet, psmouse))
1124 return;
1125
1126 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1127
1128 input_mt_report_finger_count(dev, f->fingers);
1129
1130 input_report_key(dev, BTN_LEFT, f->left);
1131 input_report_key(dev, BTN_RIGHT, f->right);
1132 input_report_key(dev, BTN_MIDDLE, f->middle);
1133
1134 input_sync(dev);
1135}
1136
1137static void alps_process_packet_v7(struct psmouse *psmouse)
1138{
1139 unsigned char *packet = psmouse->packet;
1140
1141 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1142 alps_process_trackstick_packet_v7(psmouse);
1143 else
1144 alps_process_touchpad_packet_v7(psmouse);
1145}
1146
1147static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
1148{
1149 enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
1150
1151 switch (byte[3] & 0x30) {
1152 case 0x00:
1153 if (SS4_IS_IDLE_V2(byte)) {
1154 pkt_id = SS4_PACKET_ID_IDLE;
1155 } else {
1156 pkt_id = SS4_PACKET_ID_ONE;
1157 }
1158 break;
1159 case 0x10:
1160 /* two-finger finger positions */
1161 pkt_id = SS4_PACKET_ID_TWO;
1162 break;
1163 case 0x20:
1164 /* stick pointer */
1165 pkt_id = SS4_PACKET_ID_STICK;
1166 break;
1167 case 0x30:
1168 /* third and fourth finger positions */
1169 pkt_id = SS4_PACKET_ID_MULTI;
1170 break;
1171 }
1172
1173 return pkt_id;
1174}
1175
1176static int alps_decode_ss4_v2(struct alps_fields *f,
1177 unsigned char *p, struct psmouse *psmouse)
1178{
1179 struct alps_data *priv = psmouse->private;
1180 enum SS4_PACKET_ID pkt_id;
1181 unsigned int no_data_x, no_data_y;
1182
1183 pkt_id = alps_get_pkt_id_ss4_v2(p);
1184
1185 /* Current packet is 1Finger coordinate packet */
1186 switch (pkt_id) {
1187 case SS4_PACKET_ID_ONE:
1188 f->mt[0].x = SS4_1F_X_V2(p);
1189 f->mt[0].y = SS4_1F_Y_V2(p);
1190 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1191 /*
1192 * When a button is held the device will give us events
1193 * with x, y, and pressure of 0. This causes annoying jumps
1194 * if a touch is released while the button is held.
1195 * Handle this by claiming zero contacts.
1196 */
1197 f->fingers = f->pressure > 0 ? 1 : 0;
1198 f->first_mp = 0;
1199 f->is_mp = 0;
1200 break;
1201
1202 case SS4_PACKET_ID_TWO:
1203 if (priv->flags & ALPS_BUTTONPAD) {
1204 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1205 f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1206 f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1207 } else {
1208 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1209 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1210 }
1211 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1212 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1213 } else {
1214 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1215 f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1216 f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1217 } else {
1218 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1219 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1220 }
1221 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1222 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1223 }
1224 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1225
1226 if (SS4_IS_MF_CONTINUE(p)) {
1227 f->first_mp = 1;
1228 } else {
1229 f->fingers = 2;
1230 f->first_mp = 0;
1231 }
1232 f->is_mp = 0;
1233
1234 break;
1235
1236 case SS4_PACKET_ID_MULTI:
1237 if (priv->flags & ALPS_BUTTONPAD) {
1238 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1239 f->mt[2].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1240 f->mt[3].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1241 no_data_x = SS4_PLUS_MFPACKET_NO_AX_BL;
1242 } else {
1243 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1244 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1245 no_data_x = SS4_MFPACKET_NO_AX_BL;
1246 }
1247 no_data_y = SS4_MFPACKET_NO_AY_BL;
1248
1249 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1250 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1251 } else {
1252 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1253 f->mt[2].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1254 f->mt[3].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1255 no_data_x = SS4_PLUS_MFPACKET_NO_AX;
1256 } else {
1257 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1258 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1259 no_data_x = SS4_MFPACKET_NO_AX;
1260 }
1261 no_data_y = SS4_MFPACKET_NO_AY;
1262
1263 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1264 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1265 }
1266
1267 f->first_mp = 0;
1268 f->is_mp = 1;
1269
1270 if (SS4_IS_5F_DETECTED(p)) {
1271 f->fingers = 5;
1272 } else if (f->mt[3].x == no_data_x &&
1273 f->mt[3].y == no_data_y) {
1274 f->mt[3].x = 0;
1275 f->mt[3].y = 0;
1276 f->fingers = 3;
1277 } else {
1278 f->fingers = 4;
1279 }
1280 break;
1281
1282 case SS4_PACKET_ID_STICK:
1283 /*
1284 * x, y, and pressure are decoded in
1285 * alps_process_packet_ss4_v2()
1286 */
1287 f->first_mp = 0;
1288 f->is_mp = 0;
1289 break;
1290
1291 case SS4_PACKET_ID_IDLE:
1292 default:
1293 memset(f, 0, sizeof(struct alps_fields));
1294 break;
1295 }
1296
1297 /* handle buttons */
1298 if (pkt_id == SS4_PACKET_ID_STICK) {
1299 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1300 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1301 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1302 } else {
1303 f->left = !!(SS4_BTN_V2(p) & 0x01);
1304 if (!(priv->flags & ALPS_BUTTONPAD)) {
1305 f->right = !!(SS4_BTN_V2(p) & 0x02);
1306 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1307 }
1308 }
1309
1310 return 0;
1311}
1312
1313static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1314{
1315 struct alps_data *priv = psmouse->private;
1316 unsigned char *packet = psmouse->packet;
1317 struct input_dev *dev = psmouse->dev;
1318 struct input_dev *dev2 = priv->dev2;
1319 struct alps_fields *f = &priv->f;
1320
1321 memset(f, 0, sizeof(struct alps_fields));
1322 priv->decode_fields(f, packet, psmouse);
1323 if (priv->multi_packet) {
1324 /*
1325 * Sometimes the first packet will indicate a multi-packet
1326 * sequence, but sometimes the next multi-packet would not
1327 * come. Check for this, and when it happens process the
1328 * position packet as usual.
1329 */
1330 if (f->is_mp) {
1331 /* Now process the 1st packet */
1332 priv->decode_fields(f, priv->multi_data, psmouse);
1333 } else {
1334 priv->multi_packet = 0;
1335 }
1336 }
1337
1338 /*
1339 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1340 * When it is set, it means 2nd packet comes without 1st packet come.
1341 */
1342 if (f->is_mp)
1343 return;
1344
1345 /* Save the first packet */
1346 if (!priv->multi_packet && f->first_mp) {
1347 priv->multi_packet = 1;
1348 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1349 return;
1350 }
1351
1352 priv->multi_packet = 0;
1353
1354 /* Report trackstick */
1355 if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
1356 if (!(priv->flags & ALPS_DUALPOINT)) {
1357 psmouse_warn(psmouse,
1358 "Rejected trackstick packet from non DualPoint device");
1359 return;
1360 }
1361
1362 input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
1363 input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
1364 input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
1365
1366 input_report_key(dev2, BTN_LEFT, f->ts_left);
1367 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1368 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1369
1370 input_sync(dev2);
1371 return;
1372 }
1373
1374 /* Report touchpad */
1375 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1376
1377 input_mt_report_finger_count(dev, f->fingers);
1378
1379 input_report_key(dev, BTN_LEFT, f->left);
1380 input_report_key(dev, BTN_RIGHT, f->right);
1381 input_report_key(dev, BTN_MIDDLE, f->middle);
1382
1383 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1384 input_sync(dev);
1385}
1386
1387static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1388{
1389 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1390 return false;
1391 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1392 return false;
1393 return true;
1394}
1395
1396static DEFINE_MUTEX(alps_mutex);
1397
1398static void alps_register_bare_ps2_mouse(struct work_struct *work)
1399{
1400 struct alps_data *priv =
1401 container_of(work, struct alps_data, dev3_register_work.work);
1402 struct psmouse *psmouse = priv->psmouse;
1403 struct input_dev *dev3;
1404 int error = 0;
1405
1406 mutex_lock(&alps_mutex);
1407
1408 if (priv->dev3)
1409 goto out;
1410
1411 dev3 = input_allocate_device();
1412 if (!dev3) {
1413 psmouse_err(psmouse, "failed to allocate secondary device\n");
1414 error = -ENOMEM;
1415 goto out;
1416 }
1417
1418 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1419 psmouse->ps2dev.serio->phys,
1420 (priv->dev2 ? "input2" : "input1"));
1421 dev3->phys = priv->phys3;
1422
1423 /*
1424 * format of input device name is: "protocol vendor name"
1425 * see function psmouse_switch_protocol() in psmouse-base.c
1426 */
1427 dev3->name = "PS/2 ALPS Mouse";
1428
1429 dev3->id.bustype = BUS_I8042;
1430 dev3->id.vendor = 0x0002;
1431 dev3->id.product = PSMOUSE_PS2;
1432 dev3->id.version = 0x0000;
1433 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1434
1435 input_set_capability(dev3, EV_REL, REL_X);
1436 input_set_capability(dev3, EV_REL, REL_Y);
1437 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1438 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1439 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1440
1441 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1442
1443 error = input_register_device(dev3);
1444 if (error) {
1445 psmouse_err(psmouse,
1446 "failed to register secondary device: %d\n",
1447 error);
1448 input_free_device(dev3);
1449 goto out;
1450 }
1451
1452 priv->dev3 = dev3;
1453
1454out:
1455 /*
1456 * Save the error code so that we can detect that we
1457 * already tried to create the device.
1458 */
1459 if (error)
1460 priv->dev3 = ERR_PTR(error);
1461
1462 mutex_unlock(&alps_mutex);
1463}
1464
1465static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1466 unsigned char packet[],
1467 bool report_buttons)
1468{
1469 struct alps_data *priv = psmouse->private;
1470 struct input_dev *dev, *dev2 = NULL;
1471
1472 /* Figure out which device to use to report the bare packet */
1473 if (priv->proto_version == ALPS_PROTO_V2 &&
1474 (priv->flags & ALPS_DUALPOINT)) {
1475 /* On V2 devices the DualPoint Stick reports bare packets */
1476 dev = priv->dev2;
1477 dev2 = psmouse->dev;
1478 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1479 /* Register dev3 mouse if we received PS/2 packet first time */
1480 if (!IS_ERR(priv->dev3))
1481 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1482 0);
1483 return;
1484 } else {
1485 dev = priv->dev3;
1486 }
1487
1488 if (report_buttons)
1489 alps_report_buttons(dev, dev2,
1490 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1491
1492 psmouse_report_standard_motion(dev, packet);
1493
1494 input_sync(dev);
1495}
1496
1497static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1498{
1499 struct alps_data *priv = psmouse->private;
1500
1501 if (psmouse->pktcnt < 6)
1502 return PSMOUSE_GOOD_DATA;
1503
1504 if (psmouse->pktcnt == 6) {
1505 /*
1506 * Start a timer to flush the packet if it ends up last
1507 * 6-byte packet in the stream. Timer needs to fire
1508 * psmouse core times out itself. 20 ms should be enough
1509 * to decide if we are getting more data or not.
1510 */
1511 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1512 return PSMOUSE_GOOD_DATA;
1513 }
1514
1515 del_timer(&priv->timer);
1516
1517 if (psmouse->packet[6] & 0x80) {
1518
1519 /*
1520 * Highest bit is set - that means we either had
1521 * complete ALPS packet and this is start of the
1522 * next packet or we got garbage.
1523 */
1524
1525 if (((psmouse->packet[3] |
1526 psmouse->packet[4] |
1527 psmouse->packet[5]) & 0x80) ||
1528 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1529 psmouse_dbg(psmouse,
1530 "refusing packet %4ph (suspected interleaved ps/2)\n",
1531 psmouse->packet + 3);
1532 return PSMOUSE_BAD_DATA;
1533 }
1534
1535 priv->process_packet(psmouse);
1536
1537 /* Continue with the next packet */
1538 psmouse->packet[0] = psmouse->packet[6];
1539 psmouse->pktcnt = 1;
1540
1541 } else {
1542
1543 /*
1544 * High bit is 0 - that means that we indeed got a PS/2
1545 * packet in the middle of ALPS packet.
1546 *
1547 * There is also possibility that we got 6-byte ALPS
1548 * packet followed by 3-byte packet from trackpoint. We
1549 * can not distinguish between these 2 scenarios but
1550 * because the latter is unlikely to happen in course of
1551 * normal operation (user would need to press all
1552 * buttons on the pad and start moving trackpoint
1553 * without touching the pad surface) we assume former.
1554 * Even if we are wrong the wost thing that would happen
1555 * the cursor would jump but we should not get protocol
1556 * de-synchronization.
1557 */
1558
1559 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1560 false);
1561
1562 /*
1563 * Continue with the standard ALPS protocol handling,
1564 * but make sure we won't process it as an interleaved
1565 * packet again, which may happen if all buttons are
1566 * pressed. To avoid this let's reset the 4th bit which
1567 * is normally 1.
1568 */
1569 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1570 psmouse->pktcnt = 4;
1571 }
1572
1573 return PSMOUSE_GOOD_DATA;
1574}
1575
1576static void alps_flush_packet(struct timer_list *t)
1577{
1578 struct alps_data *priv = from_timer(priv, t, timer);
1579 struct psmouse *psmouse = priv->psmouse;
1580
1581 serio_pause_rx(psmouse->ps2dev.serio);
1582
1583 if (psmouse->pktcnt == psmouse->pktsize) {
1584
1585 /*
1586 * We did not any more data in reasonable amount of time.
1587 * Validate the last 3 bytes and process as a standard
1588 * ALPS packet.
1589 */
1590 if ((psmouse->packet[3] |
1591 psmouse->packet[4] |
1592 psmouse->packet[5]) & 0x80) {
1593 psmouse_dbg(psmouse,
1594 "refusing packet %3ph (suspected interleaved ps/2)\n",
1595 psmouse->packet + 3);
1596 } else {
1597 priv->process_packet(psmouse);
1598 }
1599 psmouse->pktcnt = 0;
1600 }
1601
1602 serio_continue_rx(psmouse->ps2dev.serio);
1603}
1604
1605static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1606{
1607 struct alps_data *priv = psmouse->private;
1608
1609 /*
1610 * Check if we are dealing with a bare PS/2 packet, presumably from
1611 * a device connected to the external PS/2 port. Because bare PS/2
1612 * protocol does not have enough constant bits to self-synchronize
1613 * properly we only do this if the device is fully synchronized.
1614 * Can not distinguish V8's first byte from PS/2 packet's
1615 */
1616 if (priv->proto_version != ALPS_PROTO_V8 &&
1617 !psmouse->out_of_sync_cnt &&
1618 (psmouse->packet[0] & 0xc8) == 0x08) {
1619
1620 if (psmouse->pktcnt == 3) {
1621 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1622 true);
1623 return PSMOUSE_FULL_PACKET;
1624 }
1625 return PSMOUSE_GOOD_DATA;
1626 }
1627
1628 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1629
1630 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1631 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1632 return alps_handle_interleaved_ps2(psmouse);
1633 }
1634
1635 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1636 psmouse_dbg(psmouse,
1637 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1638 psmouse->packet[0], priv->mask0, priv->byte0);
1639 return PSMOUSE_BAD_DATA;
1640 }
1641
1642 /* Bytes 2 - pktsize should have 0 in the highest bit */
1643 if (priv->proto_version < ALPS_PROTO_V5 &&
1644 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1645 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1646 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1647 psmouse->pktcnt - 1,
1648 psmouse->packet[psmouse->pktcnt - 1]);
1649
1650 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1651 psmouse->pktcnt == psmouse->pktsize) {
1652 /*
1653 * Some Dell boxes, such as Latitude E6440 or E7440
1654 * with closed lid, quite often smash last byte of
1655 * otherwise valid packet with 0xff. Given that the
1656 * next packet is very likely to be valid let's
1657 * report PSMOUSE_FULL_PACKET but not process data,
1658 * rather than reporting PSMOUSE_BAD_DATA and
1659 * filling the logs.
1660 */
1661 return PSMOUSE_FULL_PACKET;
1662 }
1663
1664 return PSMOUSE_BAD_DATA;
1665 }
1666
1667 if ((priv->proto_version == ALPS_PROTO_V7 &&
1668 !alps_is_valid_package_v7(psmouse)) ||
1669 (priv->proto_version == ALPS_PROTO_V8 &&
1670 !alps_is_valid_package_ss4_v2(psmouse))) {
1671 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1672 psmouse->pktcnt - 1,
1673 psmouse->packet[psmouse->pktcnt - 1]);
1674 return PSMOUSE_BAD_DATA;
1675 }
1676
1677 if (psmouse->pktcnt == psmouse->pktsize) {
1678 priv->process_packet(psmouse);
1679 return PSMOUSE_FULL_PACKET;
1680 }
1681
1682 return PSMOUSE_GOOD_DATA;
1683}
1684
1685static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1686{
1687 struct ps2dev *ps2dev = &psmouse->ps2dev;
1688 struct alps_data *priv = psmouse->private;
1689 int command;
1690 unsigned char *param;
1691 unsigned char dummy[4];
1692
1693 BUG_ON(nibble > 0xf);
1694
1695 command = priv->nibble_commands[nibble].command;
1696 param = (command & 0x0f00) ?
1697 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1698
1699 if (ps2_command(ps2dev, param, command))
1700 return -1;
1701
1702 return 0;
1703}
1704
1705static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1706{
1707 struct ps2dev *ps2dev = &psmouse->ps2dev;
1708 struct alps_data *priv = psmouse->private;
1709 int i, nibble;
1710
1711 if (ps2_command(ps2dev, NULL, priv->addr_command))
1712 return -1;
1713
1714 for (i = 12; i >= 0; i -= 4) {
1715 nibble = (addr >> i) & 0xf;
1716 if (alps_command_mode_send_nibble(psmouse, nibble))
1717 return -1;
1718 }
1719
1720 return 0;
1721}
1722
1723static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1724{
1725 struct ps2dev *ps2dev = &psmouse->ps2dev;
1726 unsigned char param[4];
1727
1728 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1729 return -1;
1730
1731 /*
1732 * The address being read is returned in the first two bytes
1733 * of the result. Check that this address matches the expected
1734 * address.
1735 */
1736 if (addr != ((param[0] << 8) | param[1]))
1737 return -1;
1738
1739 return param[2];
1740}
1741
1742static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1743{
1744 if (alps_command_mode_set_addr(psmouse, addr))
1745 return -1;
1746 return __alps_command_mode_read_reg(psmouse, addr);
1747}
1748
1749static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1750{
1751 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1752 return -1;
1753 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1754 return -1;
1755 return 0;
1756}
1757
1758static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1759 u8 value)
1760{
1761 if (alps_command_mode_set_addr(psmouse, addr))
1762 return -1;
1763 return __alps_command_mode_write_reg(psmouse, value);
1764}
1765
1766static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1767 int repeated_command, unsigned char *param)
1768{
1769 struct ps2dev *ps2dev = &psmouse->ps2dev;
1770
1771 param[0] = 0;
1772 if (init_command && ps2_command(ps2dev, param, init_command))
1773 return -EIO;
1774
1775 if (ps2_command(ps2dev, NULL, repeated_command) ||
1776 ps2_command(ps2dev, NULL, repeated_command) ||
1777 ps2_command(ps2dev, NULL, repeated_command))
1778 return -EIO;
1779
1780 param[0] = param[1] = param[2] = 0xff;
1781 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1782 return -EIO;
1783
1784 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1785 repeated_command, param);
1786 return 0;
1787}
1788
1789static bool alps_check_valid_firmware_id(unsigned char id[])
1790{
1791 if (id[0] == 0x73)
1792 return true;
1793
1794 if (id[0] == 0x88 &&
1795 (id[1] == 0x07 ||
1796 id[1] == 0x08 ||
1797 (id[1] & 0xf0) == 0xb0 ||
1798 (id[1] & 0xf0) == 0xc0)) {
1799 return true;
1800 }
1801
1802 return false;
1803}
1804
1805static int alps_enter_command_mode(struct psmouse *psmouse)
1806{
1807 unsigned char param[4];
1808
1809 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1810 psmouse_err(psmouse, "failed to enter command mode\n");
1811 return -1;
1812 }
1813
1814 if (!alps_check_valid_firmware_id(param)) {
1815 psmouse_dbg(psmouse,
1816 "unknown response while entering command mode\n");
1817 return -1;
1818 }
1819 return 0;
1820}
1821
1822static inline int alps_exit_command_mode(struct psmouse *psmouse)
1823{
1824 struct ps2dev *ps2dev = &psmouse->ps2dev;
1825 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1826 return -1;
1827 return 0;
1828}
1829
1830/*
1831 * For DualPoint devices select the device that should respond to
1832 * subsequent commands. It looks like glidepad is behind stickpointer,
1833 * I'd thought it would be other way around...
1834 */
1835static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1836{
1837 struct ps2dev *ps2dev = &psmouse->ps2dev;
1838 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1839
1840 if (ps2_command(ps2dev, NULL, cmd) ||
1841 ps2_command(ps2dev, NULL, cmd) ||
1842 ps2_command(ps2dev, NULL, cmd) ||
1843 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1844 return -1;
1845
1846 /* we may get 3 more bytes, just ignore them */
1847 ps2_drain(ps2dev, 3, 100);
1848
1849 return 0;
1850}
1851
1852static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1853{
1854 struct ps2dev *ps2dev = &psmouse->ps2dev;
1855
1856 /* Try ALPS magic knock - 4 disable before enable */
1857 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1858 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1859 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1860 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1861 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1862 return -1;
1863
1864 /*
1865 * Switch mouse to poll (remote) mode so motion data will not
1866 * get in our way
1867 */
1868 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1869}
1870
1871static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1872{
1873 int i, nibble;
1874
1875 /*
1876 * b0-b11 are valid bits, send sequence is inverse.
1877 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1878 */
1879 for (i = 0; i <= 8; i += 4) {
1880 nibble = (word >> i) & 0xf;
1881 if (alps_command_mode_send_nibble(psmouse, nibble))
1882 return -1;
1883 }
1884
1885 return 0;
1886}
1887
1888static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1889 u16 addr, u16 value)
1890{
1891 struct ps2dev *ps2dev = &psmouse->ps2dev;
1892
1893 /* 0x0A0 is the command to write the word */
1894 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1895 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1896 alps_monitor_mode_send_word(psmouse, addr) ||
1897 alps_monitor_mode_send_word(psmouse, value) ||
1898 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1899 return -1;
1900
1901 return 0;
1902}
1903
1904static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1905{
1906 struct ps2dev *ps2dev = &psmouse->ps2dev;
1907
1908 if (enable) {
1909 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1910 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1911 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1912 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1913 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1914 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1915 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1916 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1917 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1918 return -1;
1919 } else {
1920 /* EC to exit monitor mode */
1921 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1922 return -1;
1923 }
1924
1925 return 0;
1926}
1927
1928static int alps_absolute_mode_v6(struct psmouse *psmouse)
1929{
1930 u16 reg_val = 0x181;
1931 int ret = -1;
1932
1933 /* enter monitor mode, to write the register */
1934 if (alps_monitor_mode(psmouse, true))
1935 return -1;
1936
1937 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1938
1939 if (alps_monitor_mode(psmouse, false))
1940 ret = -1;
1941
1942 return ret;
1943}
1944
1945static int alps_get_status(struct psmouse *psmouse, char *param)
1946{
1947 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1948 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1949 return -1;
1950
1951 return 0;
1952}
1953
1954/*
1955 * Turn touchpad tapping on or off. The sequences are:
1956 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1957 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1958 * My guess that 0xE9 (GetInfo) is here as a sync point.
1959 * For models that also have stickpointer (DualPoints) its tapping
1960 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1961 * we don't fiddle with it.
1962 */
1963static int alps_tap_mode(struct psmouse *psmouse, int enable)
1964{
1965 struct ps2dev *ps2dev = &psmouse->ps2dev;
1966 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1967 unsigned char tap_arg = enable ? 0x0A : 0x00;
1968 unsigned char param[4];
1969
1970 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1971 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1972 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1973 ps2_command(ps2dev, &tap_arg, cmd))
1974 return -1;
1975
1976 if (alps_get_status(psmouse, param))
1977 return -1;
1978
1979 return 0;
1980}
1981
1982/*
1983 * alps_poll() - poll the touchpad for current motion packet.
1984 * Used in resync.
1985 */
1986static int alps_poll(struct psmouse *psmouse)
1987{
1988 struct alps_data *priv = psmouse->private;
1989 unsigned char buf[sizeof(psmouse->packet)];
1990 bool poll_failed;
1991
1992 if (priv->flags & ALPS_PASS)
1993 alps_passthrough_mode_v2(psmouse, true);
1994
1995 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1996 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1997
1998 if (priv->flags & ALPS_PASS)
1999 alps_passthrough_mode_v2(psmouse, false);
2000
2001 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
2002 return -1;
2003
2004 if ((psmouse->badbyte & 0xc8) == 0x08) {
2005/*
2006 * Poll the track stick ...
2007 */
2008 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2009 return -1;
2010 }
2011
2012 memcpy(psmouse->packet, buf, sizeof(buf));
2013 return 0;
2014}
2015
2016static int alps_hw_init_v1_v2(struct psmouse *psmouse)
2017{
2018 struct alps_data *priv = psmouse->private;
2019
2020 if ((priv->flags & ALPS_PASS) &&
2021 alps_passthrough_mode_v2(psmouse, true)) {
2022 return -1;
2023 }
2024
2025 if (alps_tap_mode(psmouse, true)) {
2026 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
2027 return -1;
2028 }
2029
2030 if (alps_absolute_mode_v1_v2(psmouse)) {
2031 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2032 return -1;
2033 }
2034
2035 if ((priv->flags & ALPS_PASS) &&
2036 alps_passthrough_mode_v2(psmouse, false)) {
2037 return -1;
2038 }
2039
2040 /* ALPS needs stream mode, otherwise it won't report any data */
2041 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2042 psmouse_err(psmouse, "Failed to enable stream mode\n");
2043 return -1;
2044 }
2045
2046 return 0;
2047}
2048
2049/* Must be in passthrough mode when calling this function */
2050static int alps_trackstick_enter_extended_mode_v3_v6(struct psmouse *psmouse)
2051{
2052 unsigned char param[2] = {0xC8, 0x14};
2053
2054 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2055 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2056 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2057 ps2_command(&psmouse->ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2058 ps2_command(&psmouse->ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
2059 return -1;
2060
2061 return 0;
2062}
2063
2064static int alps_hw_init_v6(struct psmouse *psmouse)
2065{
2066 int ret;
2067
2068 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2069 if (alps_passthrough_mode_v2(psmouse, true))
2070 return -1;
2071
2072 ret = alps_trackstick_enter_extended_mode_v3_v6(psmouse);
2073
2074 if (alps_passthrough_mode_v2(psmouse, false))
2075 return -1;
2076
2077 if (ret)
2078 return ret;
2079
2080 if (alps_absolute_mode_v6(psmouse)) {
2081 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2082 return -1;
2083 }
2084
2085 return 0;
2086}
2087
2088/*
2089 * Enable or disable passthrough mode to the trackstick.
2090 */
2091static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2092 int reg_base, bool enable)
2093{
2094 int reg_val, ret = -1;
2095
2096 if (alps_enter_command_mode(psmouse))
2097 return -1;
2098
2099 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2100 if (reg_val == -1)
2101 goto error;
2102
2103 if (enable)
2104 reg_val |= 0x01;
2105 else
2106 reg_val &= ~0x01;
2107
2108 ret = __alps_command_mode_write_reg(psmouse, reg_val);
2109
2110error:
2111 if (alps_exit_command_mode(psmouse))
2112 ret = -1;
2113 return ret;
2114}
2115
2116/* Must be in command mode when calling this function */
2117static int alps_absolute_mode_v3(struct psmouse *psmouse)
2118{
2119 int reg_val;
2120
2121 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2122 if (reg_val == -1)
2123 return -1;
2124
2125 reg_val |= 0x06;
2126 if (__alps_command_mode_write_reg(psmouse, reg_val))
2127 return -1;
2128
2129 return 0;
2130}
2131
2132static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
2133{
2134 int ret = -EIO, reg_val;
2135
2136 if (alps_enter_command_mode(psmouse))
2137 goto error;
2138
2139 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2140 if (reg_val == -1)
2141 goto error;
2142
2143 /* bit 7: trackstick is present */
2144 ret = reg_val & 0x80 ? 0 : -ENODEV;
2145
2146error:
2147 alps_exit_command_mode(psmouse);
2148 return ret;
2149}
2150
2151static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2152{
2153 int ret = 0;
2154 int reg_val;
2155 unsigned char param[4];
2156
2157 /*
2158 * We need to configure trackstick to report data for touchpad in
2159 * extended format. And also we need to tell touchpad to expect data
2160 * from trackstick in extended format. Without this configuration
2161 * trackstick packets sent from touchpad are in basic format which is
2162 * different from what we expect.
2163 */
2164
2165 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2166 return -EIO;
2167
2168 /*
2169 * E7 report for the trackstick
2170 *
2171 * There have been reports of failures to seem to trace back
2172 * to the above trackstick check failing. When these occur
2173 * this E7 report fails, so when that happens we continue
2174 * with the assumption that there isn't a trackstick after
2175 * all.
2176 */
2177 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2178 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2179 ret = -ENODEV;
2180 } else {
2181 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2182 if (alps_trackstick_enter_extended_mode_v3_v6(psmouse)) {
2183 psmouse_err(psmouse, "Failed to enter into trackstick extended mode\n");
2184 ret = -EIO;
2185 }
2186 }
2187
2188 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2189 return -EIO;
2190
2191 if (ret)
2192 return ret;
2193
2194 if (alps_enter_command_mode(psmouse))
2195 return -EIO;
2196
2197 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2198 if (reg_val == -1) {
2199 ret = -EIO;
2200 } else {
2201 /*
2202 * Tell touchpad that trackstick is now in extended mode.
2203 * If bit 1 isn't set the packet format is different.
2204 */
2205 reg_val |= BIT(1);
2206 if (__alps_command_mode_write_reg(psmouse, reg_val))
2207 ret = -EIO;
2208 }
2209
2210 if (alps_exit_command_mode(psmouse))
2211 return -EIO;
2212
2213 return ret;
2214}
2215
2216static int alps_hw_init_v3(struct psmouse *psmouse)
2217{
2218 struct alps_data *priv = psmouse->private;
2219 struct ps2dev *ps2dev = &psmouse->ps2dev;
2220 int reg_val;
2221 unsigned char param[4];
2222
2223 if ((priv->flags & ALPS_DUALPOINT) &&
2224 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2225 goto error;
2226
2227 if (alps_enter_command_mode(psmouse) ||
2228 alps_absolute_mode_v3(psmouse)) {
2229 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2230 goto error;
2231 }
2232
2233 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2234 if (reg_val == -1)
2235 goto error;
2236 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2237 goto error;
2238
2239 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2240 if (reg_val == -1)
2241 goto error;
2242 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2243 goto error;
2244
2245 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2246 goto error;
2247 if (__alps_command_mode_write_reg(psmouse, 0x04))
2248 goto error;
2249
2250 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2251 goto error;
2252 if (__alps_command_mode_write_reg(psmouse, 0x03))
2253 goto error;
2254
2255 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2256 goto error;
2257 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2258 goto error;
2259
2260 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2261 goto error;
2262 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2263 goto error;
2264
2265 alps_exit_command_mode(psmouse);
2266
2267 /* Set rate and enable data reporting */
2268 param[0] = 0x64;
2269 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2270 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2271 psmouse_err(psmouse, "Failed to enable data reporting\n");
2272 return -1;
2273 }
2274
2275 return 0;
2276
2277error:
2278 /*
2279 * Leaving the touchpad in command mode will essentially render
2280 * it unusable until the machine reboots, so exit it here just
2281 * to be safe
2282 */
2283 alps_exit_command_mode(psmouse);
2284 return -1;
2285}
2286
2287static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2288{
2289 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2290 struct alps_data *priv = psmouse->private;
2291
2292 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2293 if (reg < 0)
2294 return reg;
2295
2296 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2297 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2298
2299 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2300 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2301
2302 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2303 if (reg < 0)
2304 return reg;
2305
2306 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2307 x_electrode = 17 + x_electrode;
2308
2309 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2310 y_electrode = 13 + y_electrode;
2311
2312 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2313 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2314
2315 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2316 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2317
2318 psmouse_dbg(psmouse,
2319 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2320 x_pitch, y_pitch, x_electrode, y_electrode,
2321 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2322
2323 return 0;
2324}
2325
2326static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2327{
2328 struct alps_data *priv = psmouse->private;
2329 struct ps2dev *ps2dev = &psmouse->ps2dev;
2330 int reg_val, ret = -1;
2331
2332 if (priv->flags & ALPS_DUALPOINT) {
2333 reg_val = alps_setup_trackstick_v3(psmouse,
2334 ALPS_REG_BASE_RUSHMORE);
2335 if (reg_val == -EIO)
2336 goto error;
2337 }
2338
2339 if (alps_enter_command_mode(psmouse) ||
2340 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2341 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2342 goto error;
2343
2344 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2345 goto error;
2346
2347 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2348 if (reg_val == -1)
2349 goto error;
2350 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2351 goto error;
2352
2353 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2354 goto error;
2355
2356 /* enter absolute mode */
2357 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2358 if (reg_val == -1)
2359 goto error;
2360 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2361 goto error;
2362
2363 alps_exit_command_mode(psmouse);
2364 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2365
2366error:
2367 alps_exit_command_mode(psmouse);
2368 return ret;
2369}
2370
2371/* Must be in command mode when calling this function */
2372static int alps_absolute_mode_v4(struct psmouse *psmouse)
2373{
2374 int reg_val;
2375
2376 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2377 if (reg_val == -1)
2378 return -1;
2379
2380 reg_val |= 0x02;
2381 if (__alps_command_mode_write_reg(psmouse, reg_val))
2382 return -1;
2383
2384 return 0;
2385}
2386
2387static int alps_hw_init_v4(struct psmouse *psmouse)
2388{
2389 struct ps2dev *ps2dev = &psmouse->ps2dev;
2390 unsigned char param[4];
2391
2392 if (alps_enter_command_mode(psmouse))
2393 goto error;
2394
2395 if (alps_absolute_mode_v4(psmouse)) {
2396 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2397 goto error;
2398 }
2399
2400 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2401 goto error;
2402
2403 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2404 goto error;
2405
2406 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2407 goto error;
2408
2409 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2410 goto error;
2411
2412 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2413 goto error;
2414
2415 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2416 goto error;
2417
2418 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2419 goto error;
2420
2421 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2422 goto error;
2423
2424 alps_exit_command_mode(psmouse);
2425
2426 /*
2427 * This sequence changes the output from a 9-byte to an
2428 * 8-byte format. All the same data seems to be present,
2429 * just in a more compact format.
2430 */
2431 param[0] = 0xc8;
2432 param[1] = 0x64;
2433 param[2] = 0x50;
2434 if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2435 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) ||
2436 ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) ||
2437 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2438 return -1;
2439
2440 /* Set rate and enable data reporting */
2441 param[0] = 0x64;
2442 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2443 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2444 psmouse_err(psmouse, "Failed to enable data reporting\n");
2445 return -1;
2446 }
2447
2448 return 0;
2449
2450error:
2451 /*
2452 * Leaving the touchpad in command mode will essentially render
2453 * it unusable until the machine reboots, so exit it here just
2454 * to be safe
2455 */
2456 alps_exit_command_mode(psmouse);
2457 return -1;
2458}
2459
2460static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2461 unsigned char index, unsigned char otp[])
2462{
2463 struct ps2dev *ps2dev = &psmouse->ps2dev;
2464
2465 switch (index) {
2466 case 0:
2467 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2468 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2469 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2470 return -1;
2471
2472 break;
2473
2474 case 1:
2475 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2476 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2477 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2478 return -1;
2479
2480 break;
2481 }
2482
2483 return 0;
2484}
2485
2486static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2487 struct alps_data *priv)
2488{
2489 int num_x_electrode;
2490 int num_y_electrode;
2491 int x_pitch, y_pitch, x_phys, y_phys;
2492
2493 if (IS_SS4PLUS_DEV(priv->dev_id)) {
2494 num_x_electrode =
2495 SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2496 num_y_electrode =
2497 SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
2498
2499 priv->x_max =
2500 (num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2501 priv->y_max =
2502 (num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2503
2504 x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2505 y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2506
2507 } else {
2508 num_x_electrode =
2509 SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2510 num_y_electrode =
2511 SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2512
2513 priv->x_max =
2514 (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2515 priv->y_max =
2516 (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2517
2518 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2519 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2520 }
2521
2522 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2523 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2524
2525 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2526 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2527
2528 return 0;
2529}
2530
2531static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2532 struct alps_data *priv)
2533{
2534 unsigned char is_btnless;
2535
2536 if (IS_SS4PLUS_DEV(priv->dev_id))
2537 is_btnless = (otp[1][0] >> 1) & 0x01;
2538 else
2539 is_btnless = (otp[1][1] >> 3) & 0x01;
2540
2541 if (is_btnless)
2542 priv->flags |= ALPS_BUTTONPAD;
2543
2544 return 0;
2545}
2546
2547static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2548 struct alps_data *priv,
2549 struct psmouse *psmouse)
2550{
2551 bool is_dual = false;
2552 int reg_val = 0;
2553 struct ps2dev *ps2dev = &psmouse->ps2dev;
2554
2555 if (IS_SS4PLUS_DEV(priv->dev_id)) {
2556 is_dual = (otp[0][0] >> 4) & 0x01;
2557
2558 if (!is_dual) {
2559 /* For support TrackStick of Thinkpad L/E series */
2560 if (alps_exit_command_mode(psmouse) == 0 &&
2561 alps_enter_command_mode(psmouse) == 0) {
2562 reg_val = alps_command_mode_read_reg(psmouse,
2563 0xD7);
2564 }
2565 alps_exit_command_mode(psmouse);
2566 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2567
2568 if (reg_val == 0x0C || reg_val == 0x1D)
2569 is_dual = true;
2570 }
2571 }
2572
2573 if (is_dual)
2574 priv->flags |= ALPS_DUALPOINT |
2575 ALPS_DUALPOINT_WITH_PRESSURE;
2576
2577 return 0;
2578}
2579
2580static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2581 struct alps_data *priv)
2582{
2583 unsigned char otp[2][4];
2584
2585 memset(otp, 0, sizeof(otp));
2586
2587 if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
2588 alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
2589 return -1;
2590
2591 alps_update_device_area_ss4_v2(otp, priv);
2592
2593 alps_update_btn_info_ss4_v2(otp, priv);
2594
2595 alps_update_dual_info_ss4_v2(otp, priv, psmouse);
2596
2597 return 0;
2598}
2599
2600static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2601 struct alps_data *priv)
2602{
2603 struct ps2dev *ps2dev = &psmouse->ps2dev;
2604 unsigned char param[4] = {0};
2605 int num_x_electrode, num_y_electrode;
2606
2607 if (alps_enter_command_mode(psmouse))
2608 return -1;
2609
2610 param[0] = 0x0a;
2611 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2612 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2613 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2614 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2615 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE))
2616 return -1;
2617
2618 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2619 return -1;
2620
2621 /*
2622 * Dolphin's sensor line number is not fixed. It can be calculated
2623 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2624 * Further more, we can get device's x_max and y_max by multiplying
2625 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2626 *
2627 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2628 * real sensor line number X = 11 + 8 = 19, and
2629 * real sensor line number Y = 8 + 1 = 9.
2630 * So, x_max = (19 - 1) * 64 = 1152, and
2631 * y_max = (9 - 1) * 64 = 512.
2632 */
2633 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2634 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2635 priv->x_bits = num_x_electrode;
2636 priv->y_bits = num_y_electrode;
2637 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2638 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2639
2640 if (alps_exit_command_mode(psmouse))
2641 return -1;
2642
2643 return 0;
2644}
2645
2646static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2647{
2648 struct ps2dev *ps2dev = &psmouse->ps2dev;
2649 unsigned char param[2];
2650
2651 /* This is dolphin "v1" as empirically defined by florin9doi */
2652 param[0] = 0x64;
2653 param[1] = 0x28;
2654
2655 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2656 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2657 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
2658 return -1;
2659
2660 return 0;
2661}
2662
2663static int alps_hw_init_v7(struct psmouse *psmouse)
2664{
2665 struct ps2dev *ps2dev = &psmouse->ps2dev;
2666 int reg_val, ret = -1;
2667
2668 if (alps_enter_command_mode(psmouse) ||
2669 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2670 goto error;
2671
2672 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2673 goto error;
2674
2675 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2676 goto error;
2677
2678 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2679 if (reg_val == -1)
2680 goto error;
2681 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2682 goto error;
2683
2684 alps_exit_command_mode(psmouse);
2685 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2686
2687error:
2688 alps_exit_command_mode(psmouse);
2689 return ret;
2690}
2691
2692static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2693{
2694 struct ps2dev *ps2dev = &psmouse->ps2dev;
2695 char param[2] = {0x64, 0x28};
2696 int ret = -1;
2697
2698 /* enter absolute mode */
2699 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2700 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2701 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2702 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE)) {
2703 goto error;
2704 }
2705
2706 /* T.B.D. Decread noise packet number, delete in the future */
2707 if (alps_exit_command_mode(psmouse) ||
2708 alps_enter_command_mode(psmouse) ||
2709 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2710 goto error;
2711 }
2712 alps_exit_command_mode(psmouse);
2713
2714 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2715
2716error:
2717 alps_exit_command_mode(psmouse);
2718 return ret;
2719}
2720
2721static int alps_set_protocol(struct psmouse *psmouse,
2722 struct alps_data *priv,
2723 const struct alps_protocol_info *protocol)
2724{
2725 psmouse->private = priv;
2726
2727 timer_setup(&priv->timer, alps_flush_packet, 0);
2728
2729 priv->proto_version = protocol->version;
2730 priv->byte0 = protocol->byte0;
2731 priv->mask0 = protocol->mask0;
2732 priv->flags = protocol->flags;
2733
2734 priv->x_max = 2000;
2735 priv->y_max = 1400;
2736 priv->x_bits = 15;
2737 priv->y_bits = 11;
2738
2739 switch (priv->proto_version) {
2740 case ALPS_PROTO_V1:
2741 case ALPS_PROTO_V2:
2742 priv->hw_init = alps_hw_init_v1_v2;
2743 priv->process_packet = alps_process_packet_v1_v2;
2744 priv->set_abs_params = alps_set_abs_params_st;
2745 priv->x_max = 1023;
2746 priv->y_max = 767;
2747 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2748 priv->flags |= ALPS_STICK_BITS;
2749 break;
2750
2751 case ALPS_PROTO_V3:
2752 priv->hw_init = alps_hw_init_v3;
2753 priv->process_packet = alps_process_packet_v3;
2754 priv->set_abs_params = alps_set_abs_params_semi_mt;
2755 priv->decode_fields = alps_decode_pinnacle;
2756 priv->nibble_commands = alps_v3_nibble_commands;
2757 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2758
2759 if (alps_probe_trackstick_v3_v7(psmouse,
2760 ALPS_REG_BASE_PINNACLE) < 0)
2761 priv->flags &= ~ALPS_DUALPOINT;
2762
2763 break;
2764
2765 case ALPS_PROTO_V3_RUSHMORE:
2766 priv->hw_init = alps_hw_init_rushmore_v3;
2767 priv->process_packet = alps_process_packet_v3;
2768 priv->set_abs_params = alps_set_abs_params_semi_mt;
2769 priv->decode_fields = alps_decode_rushmore;
2770 priv->nibble_commands = alps_v3_nibble_commands;
2771 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2772 priv->x_bits = 16;
2773 priv->y_bits = 12;
2774
2775 if (alps_probe_trackstick_v3_v7(psmouse,
2776 ALPS_REG_BASE_RUSHMORE) < 0)
2777 priv->flags &= ~ALPS_DUALPOINT;
2778
2779 break;
2780
2781 case ALPS_PROTO_V4:
2782 priv->hw_init = alps_hw_init_v4;
2783 priv->process_packet = alps_process_packet_v4;
2784 priv->set_abs_params = alps_set_abs_params_semi_mt;
2785 priv->nibble_commands = alps_v4_nibble_commands;
2786 priv->addr_command = PSMOUSE_CMD_DISABLE;
2787 break;
2788
2789 case ALPS_PROTO_V5:
2790 priv->hw_init = alps_hw_init_dolphin_v1;
2791 priv->process_packet = alps_process_touchpad_packet_v3_v5;
2792 priv->decode_fields = alps_decode_dolphin;
2793 priv->set_abs_params = alps_set_abs_params_semi_mt;
2794 priv->nibble_commands = alps_v3_nibble_commands;
2795 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2796 priv->x_bits = 23;
2797 priv->y_bits = 12;
2798
2799 if (alps_dolphin_get_device_area(psmouse, priv))
2800 return -EIO;
2801
2802 break;
2803
2804 case ALPS_PROTO_V6:
2805 priv->hw_init = alps_hw_init_v6;
2806 priv->process_packet = alps_process_packet_v6;
2807 priv->set_abs_params = alps_set_abs_params_st;
2808 priv->nibble_commands = alps_v6_nibble_commands;
2809 priv->x_max = 2047;
2810 priv->y_max = 1535;
2811 break;
2812
2813 case ALPS_PROTO_V7:
2814 priv->hw_init = alps_hw_init_v7;
2815 priv->process_packet = alps_process_packet_v7;
2816 priv->decode_fields = alps_decode_packet_v7;
2817 priv->set_abs_params = alps_set_abs_params_v7;
2818 priv->nibble_commands = alps_v3_nibble_commands;
2819 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2820 priv->x_max = 0xfff;
2821 priv->y_max = 0x7ff;
2822
2823 if (priv->fw_ver[1] != 0xba)
2824 priv->flags |= ALPS_BUTTONPAD;
2825
2826 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2827 priv->flags &= ~ALPS_DUALPOINT;
2828
2829 break;
2830
2831 case ALPS_PROTO_V8:
2832 priv->hw_init = alps_hw_init_ss4_v2;
2833 priv->process_packet = alps_process_packet_ss4_v2;
2834 priv->decode_fields = alps_decode_ss4_v2;
2835 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2836 priv->nibble_commands = alps_v3_nibble_commands;
2837 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2838
2839 if (alps_set_defaults_ss4_v2(psmouse, priv))
2840 return -EIO;
2841
2842 break;
2843 }
2844
2845 return 0;
2846}
2847
2848static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2849 unsigned char *ec)
2850{
2851 const struct alps_model_info *model;
2852 int i;
2853
2854 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2855 model = &alps_model_data[i];
2856
2857 if (!memcmp(e7, model->signature, sizeof(model->signature)))
2858 return &model->protocol_info;
2859 }
2860
2861 return NULL;
2862}
2863
2864static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2865{
2866 const struct alps_protocol_info *protocol;
2867 unsigned char e6[4], e7[4], ec[4];
2868 int error;
2869
2870 /*
2871 * First try "E6 report".
2872 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2873 * The bits 0-2 of the first byte will be 1s if some buttons are
2874 * pressed.
2875 */
2876 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2877 PSMOUSE_CMD_SETSCALE11, e6))
2878 return -EIO;
2879
2880 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2881 return -EINVAL;
2882
2883 /*
2884 * Now get the "E7" and "EC" reports. These will uniquely identify
2885 * most ALPS touchpads.
2886 */
2887 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2888 PSMOUSE_CMD_SETSCALE21, e7) ||
2889 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2890 PSMOUSE_CMD_RESET_WRAP, ec) ||
2891 alps_exit_command_mode(psmouse))
2892 return -EIO;
2893
2894 protocol = alps_match_table(e7, ec);
2895 if (!protocol) {
2896 if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
2897 ec[2] == 0x8a) {
2898 protocol = &alps_v4_protocol_data;
2899 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2900 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2901 protocol = &alps_v5_protocol_data;
2902 } else if (ec[0] == 0x88 &&
2903 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2904 protocol = &alps_v7_protocol_data;
2905 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2906 protocol = &alps_v3_rushmore_data;
2907 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2908 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2909 protocol = &alps_v3_protocol_data;
2910 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2911 (e7[2] == 0x14 || e7[2] == 0x28)) {
2912 protocol = &alps_v8_protocol_data;
2913 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2914 protocol = &alps_v9_protocol_data;
2915 psmouse_warn(psmouse,
2916 "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2917 e7, ec);
2918 return -EINVAL;
2919 } else {
2920 psmouse_dbg(psmouse,
2921 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2922 return -EINVAL;
2923 }
2924 }
2925
2926 if (priv) {
2927 /* Save Device ID and Firmware version */
2928 memcpy(priv->dev_id, e7, 3);
2929 memcpy(priv->fw_ver, ec, 3);
2930 error = alps_set_protocol(psmouse, priv, protocol);
2931 if (error)
2932 return error;
2933 }
2934
2935 return 0;
2936}
2937
2938static int alps_reconnect(struct psmouse *psmouse)
2939{
2940 struct alps_data *priv = psmouse->private;
2941
2942 psmouse_reset(psmouse);
2943
2944 if (alps_identify(psmouse, priv) < 0)
2945 return -1;
2946
2947 return priv->hw_init(psmouse);
2948}
2949
2950static void alps_disconnect(struct psmouse *psmouse)
2951{
2952 struct alps_data *priv = psmouse->private;
2953
2954 psmouse_reset(psmouse);
2955 del_timer_sync(&priv->timer);
2956 if (priv->dev2)
2957 input_unregister_device(priv->dev2);
2958 if (!IS_ERR_OR_NULL(priv->dev3))
2959 input_unregister_device(priv->dev3);
2960 kfree(priv);
2961}
2962
2963static void alps_set_abs_params_st(struct alps_data *priv,
2964 struct input_dev *dev1)
2965{
2966 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2967 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2968 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2969}
2970
2971static void alps_set_abs_params_mt_common(struct alps_data *priv,
2972 struct input_dev *dev1)
2973{
2974 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2975 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2976
2977 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2978 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2979
2980 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2981 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2982}
2983
2984static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2985 struct input_dev *dev1)
2986{
2987 alps_set_abs_params_mt_common(priv, dev1);
2988 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2989
2990 input_mt_init_slots(dev1, MAX_TOUCHES,
2991 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2992 INPUT_MT_SEMI_MT);
2993}
2994
2995static void alps_set_abs_params_v7(struct alps_data *priv,
2996 struct input_dev *dev1)
2997{
2998 alps_set_abs_params_mt_common(priv, dev1);
2999 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3000
3001 input_mt_init_slots(dev1, MAX_TOUCHES,
3002 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3003 INPUT_MT_TRACK);
3004
3005 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3006}
3007
3008static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
3009 struct input_dev *dev1)
3010{
3011 alps_set_abs_params_mt_common(priv, dev1);
3012 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
3013 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3014
3015 input_mt_init_slots(dev1, MAX_TOUCHES,
3016 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3017 INPUT_MT_TRACK);
3018}
3019
3020int alps_init(struct psmouse *psmouse)
3021{
3022 struct alps_data *priv = psmouse->private;
3023 struct input_dev *dev1 = psmouse->dev;
3024 int error;
3025
3026 error = priv->hw_init(psmouse);
3027 if (error)
3028 goto init_fail;
3029
3030 /*
3031 * Undo part of setup done for us by psmouse core since touchpad
3032 * is not a relative device.
3033 */
3034 __clear_bit(EV_REL, dev1->evbit);
3035 __clear_bit(REL_X, dev1->relbit);
3036 __clear_bit(REL_Y, dev1->relbit);
3037
3038 /*
3039 * Now set up our capabilities.
3040 */
3041 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
3042 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
3043 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
3044 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
3045 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
3046
3047 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
3048
3049 priv->set_abs_params(priv, dev1);
3050
3051 if (priv->flags & ALPS_WHEEL) {
3052 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
3053 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
3054 }
3055
3056 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
3057 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
3058 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
3059 }
3060
3061 if (priv->flags & ALPS_FOUR_BUTTONS) {
3062 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
3063 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
3064 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
3065 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
3066 } else if (priv->flags & ALPS_BUTTONPAD) {
3067 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
3068 clear_bit(BTN_RIGHT, dev1->keybit);
3069 } else {
3070 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
3071 }
3072
3073 if (priv->flags & ALPS_DUALPOINT) {
3074 struct input_dev *dev2;
3075
3076 dev2 = input_allocate_device();
3077 if (!dev2) {
3078 psmouse_err(psmouse,
3079 "failed to allocate trackstick device\n");
3080 error = -ENOMEM;
3081 goto init_fail;
3082 }
3083
3084 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3085 psmouse->ps2dev.serio->phys);
3086 dev2->phys = priv->phys2;
3087
3088 /*
3089 * format of input device name is: "protocol vendor name"
3090 * see function psmouse_switch_protocol() in psmouse-base.c
3091 */
3092 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3093
3094 dev2->id.bustype = BUS_I8042;
3095 dev2->id.vendor = 0x0002;
3096 dev2->id.product = PSMOUSE_ALPS;
3097 dev2->id.version = priv->proto_version;
3098 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
3099
3100 input_set_capability(dev2, EV_REL, REL_X);
3101 input_set_capability(dev2, EV_REL, REL_Y);
3102 if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3103 input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3104 input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3105 }
3106 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3107 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3108 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
3109
3110 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
3111 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3112
3113 error = input_register_device(dev2);
3114 if (error) {
3115 psmouse_err(psmouse,
3116 "failed to register trackstick device: %d\n",
3117 error);
3118 input_free_device(dev2);
3119 goto init_fail;
3120 }
3121
3122 priv->dev2 = dev2;
3123 }
3124
3125 priv->psmouse = psmouse;
3126
3127 INIT_DELAYED_WORK(&priv->dev3_register_work,
3128 alps_register_bare_ps2_mouse);
3129
3130 psmouse->protocol_handler = alps_process_byte;
3131 psmouse->poll = alps_poll;
3132 psmouse->disconnect = alps_disconnect;
3133 psmouse->reconnect = alps_reconnect;
3134 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
3135
3136 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3137 psmouse->resync_time = 0;
3138
3139 /* Allow 2 invalid packets without resetting device */
3140 psmouse->resetafter = psmouse->pktsize * 2;
3141
3142 return 0;
3143
3144init_fail:
3145 psmouse_reset(psmouse);
3146 /*
3147 * Even though we did not allocate psmouse->private we do free
3148 * it here.
3149 */
3150 kfree(psmouse->private);
3151 psmouse->private = NULL;
3152 return error;
3153}
3154
3155int alps_detect(struct psmouse *psmouse, bool set_properties)
3156{
3157 struct alps_data *priv;
3158 int error;
3159
3160 error = alps_identify(psmouse, NULL);
3161 if (error)
3162 return error;
3163
3164 /*
3165 * Reset the device to make sure it is fully operational:
3166 * on some laptops, like certain Dell Latitudes, we may
3167 * fail to properly detect presence of trackstick if device
3168 * has not been reset.
3169 */
3170 psmouse_reset(psmouse);
3171
3172 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3173 if (!priv)
3174 return -ENOMEM;
3175
3176 error = alps_identify(psmouse, priv);
3177 if (error) {
3178 kfree(priv);
3179 return error;
3180 }
3181
3182 if (set_properties) {
3183 psmouse->vendor = "ALPS";
3184 psmouse->name = priv->flags & ALPS_DUALPOINT ?
3185 "DualPoint TouchPad" : "GlidePoint";
3186 psmouse->model = priv->proto_version;
3187 } else {
3188 /*
3189 * Destroy alps_data structure we allocated earlier since
3190 * this was just a "trial run". Otherwise we'll keep it
3191 * to be used by alps_init() which has to be called if
3192 * we succeed and set_properties is true.
3193 */
3194 kfree(priv);
3195 psmouse->private = NULL;
3196 }
3197
3198 return 0;
3199}
3200