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