blob: bf2fa51853e3d7532f032c819eb397ab0fe52591 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
23
24#include "psmouse.h"
25#include "alps.h"
26
Seth Forshee25bded72011-11-07 19:53:36 -080027/*
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
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080071#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 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080078#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
79 6-byte ALPS packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Helge Dellere38de672006-09-10 21:54:39 -040081static const struct alps_model_info alps_model_data[] = {
Seth Forshee25bded72011-11-07 19:53:36 -080082 { { 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 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800100 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Seth Forshee25bded72011-11-07 19:53:36 -0800101 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800102 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Seth Forshee25bded72011-11-07 19:53:36 -0800103 { { 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 */
Seth Forshee25bded72011-11-07 19:53:36 -0800106 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800109static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
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
Seth Forsheed4b347b2011-11-07 19:53:15 -0800120/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800122static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800123 unsigned char data)
124{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800125 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800126}
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{
Martin Buckc91ed052010-03-13 22:23:58 -0800132 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800133
Martin Buckc91ed052010-03-13 22:23:58 -0800134 /*
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);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800141
Martin Buckc91ed052010-03-13 22:23:58 -0800142 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
143 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800144
Martin Buckc91ed052010-03-13 22:23:58 -0800145 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
146 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800147
Martin Buckc91ed052010-03-13 22:23:58 -0800148 /*
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);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800153}
154
Seth Forshee25bded72011-11-07 19:53:36 -0800155static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct alps_data *priv = psmouse->private;
158 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500159 struct input_dev *dev = psmouse->dev;
160 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500162 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800164 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400165 left = packet[2] & 0x10;
166 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 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
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800180 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400181 back = packet[0] & 0x10;
182 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500183 }
184
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800185 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500186 back = packet[3] & 4;
187 forward = packet[2] & 4;
188 if ((middle = forward && back))
189 forward = back = 0;
190 }
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 ges = packet[2] & 1;
193 fin = packet[2] & 2;
194
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800195 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
197 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700198
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800199 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 input_sync(dev2);
202 return;
203 }
204
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800205 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800208 if (ges && !fin)
209 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
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
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800225 if (z > 30)
226 input_report_key(dev, BTN_TOUCH, 1);
227 if (z < 25)
228 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
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
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800238 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500239 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800241 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500242 input_report_key(dev, BTN_FORWARD, forward);
243 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800246 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800247 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 input_sync(dev);
254}
255
Seth Forshee01ce6612011-11-07 19:54:13 -0800256/*
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 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800266static int alps_process_bitmap(struct alps_data *priv,
267 unsigned int x_map, unsigned int y_map,
Seth Forshee01ce6612011-11-07 19:54:13 -0800268 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 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800309 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits);
Seth Forshee01ce6612011-11-07 19:54:13 -0800310 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
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800355 *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));
Seth Forshee01ce6612011-11-07 19:54:13 -0800359
360 if (fingers > 1) {
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800361 *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));
Seth Forshee01ce6612011-11-07 19:54:13 -0800367 }
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
Seth Forshee25bded72011-11-07 19:53:36 -0800390static 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
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800450static 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
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800482static 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
Seth Forshee25bded72011-11-07 19:53:36 -0800490static 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;
Seth Forshee01ce6612011-11-07 19:54:13 -0800496 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
497 int fingers = 0, bmap_fingers;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800498 struct alps_fields f;
499
500 priv->decode_fields(&f, packet);
Seth Forshee25bded72011-11-07 19:53:36 -0800501
502 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800503 * 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.
Seth Forshee25bded72011-11-07 19:53:36 -0800507 */
508 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800509 /*
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 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800515 if (f.is_mp) {
516 fingers = f.fingers;
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800517 bmap_fingers = alps_process_bitmap(priv,
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800518 f.x_map, f.y_map,
Seth Forshee01ce6612011-11-07 19:54:13 -0800519 &x1, &y1, &x2, &y2);
520
Seth Forshee25bded72011-11-07 19:53:36 -0800521 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800522 * We shouldn't report more than one finger if
523 * we don't have two coordinates.
Seth Forshee25bded72011-11-07 19:53:36 -0800524 */
Seth Forshee01ce6612011-11-07 19:54:13 -0800525 if (fingers > 1 && bmap_fingers < 2)
526 fingers = bmap_fingers;
527
528 /* Now process position packet */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800529 priv->decode_fields(&f, priv->multi_data);
Seth Forshee01ce6612011-11-07 19:54:13 -0800530 } else {
531 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800532 }
533 }
534
Seth Forshee01ce6612011-11-07 19:54:13 -0800535 /*
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 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800543 if (f.is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800544 return;
545
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800546 if (!priv->multi_packet && f.first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800547 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800548 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
549 return;
550 }
551
552 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800553
Seth Forshee25bded72011-11-07 19:53:36 -0800554 /*
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 */
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800560 if (f.x && f.y && !f.z)
Seth Forshee25bded72011-11-07 19:53:36 -0800561 return;
562
Seth Forshee01ce6612011-11-07 19:54:13 -0800563 /*
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) {
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800568 x1 = f.x;
569 y1 = f.y;
570 fingers = f.z > 0 ? 1 : 0;
Seth Forshee01ce6612011-11-07 19:54:13 -0800571 }
572
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800573 if (f.z >= 64)
Seth Forshee25bded72011-11-07 19:53:36 -0800574 input_report_key(dev, BTN_TOUCH, 1);
575 else
576 input_report_key(dev, BTN_TOUCH, 0);
577
Seth Forshee01ce6612011-11-07 19:54:13 -0800578 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
579
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700580 input_mt_report_finger_count(dev, fingers);
Seth Forshee01ce6612011-11-07 19:54:13 -0800581
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800582 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);
Seth Forshee01ce6612011-11-07 19:54:13 -0800585
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800586 if (f.z > 0) {
587 input_report_abs(dev, ABS_X, f.x);
588 input_report_abs(dev, ABS_Y, f.y);
Seth Forshee25bded72011-11-07 19:53:36 -0800589 }
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800590 input_report_abs(dev, ABS_PRESSURE, f.z);
Seth Forshee25bded72011-11-07 19:53:36 -0800591
Seth Forshee25bded72011-11-07 19:53:36 -0800592 input_sync(dev);
593
594 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800595 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);
Seth Forshee25bded72011-11-07 19:53:36 -0800598 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{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700624 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800625 unsigned char *packet = psmouse->packet;
626 struct input_dev *dev = psmouse->dev;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700627 int offset;
Seth Forshee25bded72011-11-07 19:53:36 -0800628 int x, y, z;
629 int left, right;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700630 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
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800662 fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
George Pantalos3b7e09f2012-05-10 22:31:59 -0700663 &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 }
Seth Forshee25bded72011-11-07 19:53:36 -0800672
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
George Pantalos3b7e09f2012-05-10 22:31:59 -0700681 /*
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
Seth Forshee25bded72011-11-07 19:53:36 -0800698 if (z >= 64)
699 input_report_key(dev, BTN_TOUCH, 1);
700 else
701 input_report_key(dev, BTN_TOUCH, 0);
702
George Pantalos3b7e09f2012-05-10 22:31:59 -0700703 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
704
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700705 input_mt_report_finger_count(dev, fingers);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700706
707 input_report_key(dev, BTN_LEFT, left);
708 input_report_key(dev, BTN_RIGHT, right);
709
Seth Forshee25bded72011-11-07 19:53:36 -0800710 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
Seth Forshee25bded72011-11-07 19:53:36 -0800716 input_sync(dev);
717}
718
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800719static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 struct alps_data *priv = psmouse->private;
741
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800742 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) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800769 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700770 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700771 "refusing packet %4ph (suspected interleaved ps/2)\n",
772 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800773 return PSMOUSE_BAD_DATA;
774 }
775
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800776 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800777
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
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700791 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800792 * 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
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700797 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800798 */
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;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800820 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800821
822 serio_pause_rx(psmouse->ps2dev.serio);
823
Seth Forsheeb46615f2011-11-07 19:53:30 -0800824 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800825
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) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700834 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700835 "refusing packet %3ph (suspected interleaved ps/2)\n",
836 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800837 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800838 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800839 }
840 psmouse->pktcnt = 0;
841 }
842
843 serio_continue_rx(psmouse->ps2dev.serio);
844}
845
846static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
847{
848 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
851 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800852 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
853 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return PSMOUSE_FULL_PACKET;
855 }
856 return PSMOUSE_GOOD_DATA;
857 }
858
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800859 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
860
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800861 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800862 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
863 return alps_handle_interleaved_ps2(psmouse);
864 }
865
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800866 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700867 psmouse_dbg(psmouse,
868 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800869 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Seth Forsheeb46615f2011-11-07 19:53:30 -0800873 /* Bytes 2 - pktsize should have 0 in the highest bit */
874 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800875 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700876 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
877 psmouse->pktcnt - 1,
878 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Seth Forsheeb46615f2011-11-07 19:53:30 -0800882 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800883 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return PSMOUSE_FULL_PACKET;
885 }
886
887 return PSMOUSE_GOOD_DATA;
888}
889
Seth Forshee25bded72011-11-07 19:53:36 -0800890static 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
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800971static 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
Seth Forshee25bded72011-11-07 19:53:36 -0800994static int alps_enter_command_mode(struct psmouse *psmouse,
995 unsigned char *resp)
996{
997 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -0800998
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800999 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001000 psmouse_err(psmouse, "failed to enter command mode\n");
1001 return -1;
1002 }
1003
Kevin Cernekee56fd3402013-02-13 22:24:22 -08001004 if (param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001005 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001006 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001007 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023/*
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 */
Seth Forshee25bded72011-11-07 19:53:36 -08001028static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 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 */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001040 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 return 0;
1043}
1044
Seth Forshee25bded72011-11-07 19:53:36 -08001045static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001067 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -1;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 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
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001101/*
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;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001108 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001109 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001110
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001111 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001112 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001113
1114 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1115 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1116
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001117 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001118 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001119
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001120 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001121 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
Seth Forshee25bded72011-11-07 19:53:36 -08001135static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001139 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001140 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001144 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001145 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Seth Forshee25bded72011-11-07 19:53:36 -08001149 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001150 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return -1;
1152 }
1153
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001154 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001155 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001159 /* ALPS needs stream mode, otherwise it won't report any data */
1160 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001161 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001162 return -1;
1163 }
1164
1165 return 0;
1166}
1167
Seth Forshee25bded72011-11-07 19:53:36 -08001168/*
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{
Seth Forshee25bded72011-11-07 19:53:36 -08001209 struct ps2dev *ps2dev = &psmouse->ps2dev;
1210 int reg_val;
1211 unsigned char param[4];
1212
Seth Forshee25bded72011-11-07 19:53:36 -08001213 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
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001340static 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
Seth Forshee25bded72011-11-07 19:53:36 -08001374/* 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{
Seth Forshee25bded72011-11-07 19:53:36 -08001392 struct ps2dev *ps2dev = &psmouse->ps2dev;
1393 unsigned char param[4];
1394
Seth Forshee25bded72011-11-07 19:53:36 -08001395 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
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001463static void alps_set_defaults(struct alps_data *priv)
Seth Forshee25bded72011-11-07 19:53:36 -08001464{
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001465 priv->byte0 = 0x8f;
1466 priv->mask0 = 0x8f;
1467 priv->flags = ALPS_DUALPOINT;
1468
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001469 priv->x_max = 2000;
1470 priv->y_max = 1400;
1471 priv->x_bits = 15;
1472 priv->y_bits = 11;
1473
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001474 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001475 case ALPS_PROTO_V1:
1476 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001477 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;
Seth Forshee25bded72011-11-07 19:53:36 -08001480 break;
1481 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001482 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;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08001485 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001486 priv->nibble_commands = alps_v3_nibble_commands;
1487 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08001488 break;
1489 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001490 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;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001493 priv->nibble_commands = alps_v4_nibble_commands;
1494 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08001495 break;
1496 }
Seth Forshee25bded72011-11-07 19:53:36 -08001497}
1498
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001499static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1500 unsigned char *e7, unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001501{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001502 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001503 int i;
1504
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001505 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;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001513 alps_set_defaults(priv);
1514
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001515 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
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001530 /*
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 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001536 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1537 PSMOUSE_CMD_SETSCALE11, e6))
1538 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001539
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001540 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1541 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001542
1543 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001544 * Now get the "E7" and "EC" reports. These will uniquely identify
1545 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001546 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001547 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;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001553
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001554 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001555 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001556 } 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
1565 return 0;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001566 } 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
1571 return 0;
1572 }
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001573
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001574 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]);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001577
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001578 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001579}
1580
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001581static int alps_reconnect(struct psmouse *psmouse)
1582{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001583 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001584
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001585 psmouse_reset(psmouse);
1586
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001587 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001588 return -1;
1589
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001590 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593static void alps_disconnect(struct psmouse *psmouse)
1594{
1595 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001598 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001599 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 kfree(priv);
1601}
1602
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001603static 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);
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001615 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);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001617
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
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001622 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);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001624}
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626int alps_init(struct psmouse *psmouse)
1627{
1628 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001629 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001631 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001632 dev2 = input_allocate_device();
1633 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001635
1636 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001637 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1638
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001639 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Seth Forshee25bded72011-11-07 19:53:36 -08001641 psmouse_reset(psmouse);
1642
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001643 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001644 goto init_fail;
1645
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001646 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 goto init_fail;
1648
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08001649 /*
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 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001660 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);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001663 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1664 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001666 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08001667
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001668 priv->set_abs_params(priv, dev1);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001669 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001671 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001672 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1673 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001676 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001677 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1678 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001681 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001682 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
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001690 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001691 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001692 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1693 "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001694 dev2->id.bustype = BUS_I8042;
1695 dev2->id.vendor = 0x0002;
1696 dev2->id.product = PSMOUSE_ALPS;
1697 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04001698 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001699
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001700 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001701 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001705 if (input_register_device(priv->dev2))
1706 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001709 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 psmouse->disconnect = alps_disconnect;
1711 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001712 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001714 /* We are having trouble resyncing ALPS touchpads so disable it for now */
1715 psmouse->resync_time = 0;
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718
1719init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001720 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001721 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001723 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return -1;
1725}
1726
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001727int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001729 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001731 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return -1;
1733
1734 if (set_properties) {
1735 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001736 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001737 "DualPoint TouchPad" : "GlidePoint";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001738 psmouse->model = dummy.proto_version << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740 return 0;
1741}
1742