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