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