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