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