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