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