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