blob: 2cd8be774f91ed41c89d63d021cf2fd68ee5a40d [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
450static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
451{
452 struct alps_data *priv = psmouse->private;
453 unsigned char *packet = psmouse->packet;
454 struct input_dev *dev = psmouse->dev;
455 struct input_dev *dev2 = priv->dev2;
456 int x, y, z;
457 int left, right, middle;
Seth Forshee01ce6612011-11-07 19:54:13 -0800458 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
459 int fingers = 0, bmap_fingers;
460 unsigned int x_bitmap, y_bitmap;
Seth Forshee25bded72011-11-07 19:53:36 -0800461
462 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800463 * There's no single feature of touchpad position and bitmap packets
464 * that can be used to distinguish between them. We rely on the fact
465 * that a bitmap packet should always follow a position packet with
466 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800467 */
468 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800469 /*
470 * Sometimes a position packet will indicate a multi-packet
471 * sequence, but then what follows is another position
472 * packet. Check for this, and when it happens process the
473 * position packet as usual.
474 */
475 if (packet[0] & 0x40) {
Seth Forshee01ce6612011-11-07 19:54:13 -0800476 fingers = (packet[5] & 0x3) + 1;
477 x_bitmap = ((packet[4] & 0x7e) << 8) |
478 ((packet[1] & 0x7f) << 2) |
479 ((packet[0] & 0x30) >> 4);
480 y_bitmap = ((packet[3] & 0x70) << 4) |
481 ((packet[2] & 0x7f) << 1) |
482 (packet[4] & 0x01);
483
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800484 bmap_fingers = alps_process_bitmap(priv,
485 x_bitmap, y_bitmap,
Seth Forshee01ce6612011-11-07 19:54:13 -0800486 &x1, &y1, &x2, &y2);
487
Seth Forshee25bded72011-11-07 19:53:36 -0800488 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800489 * We shouldn't report more than one finger if
490 * we don't have two coordinates.
Seth Forshee25bded72011-11-07 19:53:36 -0800491 */
Seth Forshee01ce6612011-11-07 19:54:13 -0800492 if (fingers > 1 && bmap_fingers < 2)
493 fingers = bmap_fingers;
494
495 /* Now process position packet */
496 packet = priv->multi_data;
497 } else {
498 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800499 }
500 }
501
Seth Forshee01ce6612011-11-07 19:54:13 -0800502 /*
503 * Bit 6 of byte 0 is not usually set in position packets. The only
504 * times it seems to be set is in situations where the data is
505 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
506 * this combined with the fact that this bit is useful for filtering
507 * out misidentified bitmap packets, we reject anything with this
508 * bit set.
509 */
510 if (packet[0] & 0x40)
511 return;
512
513 if (!priv->multi_packet && (packet[4] & 0x40)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800514 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800515 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
516 return;
517 }
518
519 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800520
521 left = packet[3] & 0x01;
522 right = packet[3] & 0x02;
523 middle = packet[3] & 0x04;
524
525 x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) |
526 ((packet[0] & 0x30) >> 4);
527 y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f);
528 z = packet[5] & 0x7f;
529
530 /*
531 * Sometimes the hardware sends a single packet with z = 0
532 * in the middle of a stream. Real releases generate packets
533 * with x, y, and z all zero, so these seem to be flukes.
534 * Ignore them.
535 */
536 if (x && y && !z)
537 return;
538
Seth Forshee01ce6612011-11-07 19:54:13 -0800539 /*
540 * If we don't have MT data or the bitmaps were empty, we have
541 * to rely on ST data.
542 */
543 if (!fingers) {
544 x1 = x;
545 y1 = y;
546 fingers = z > 0 ? 1 : 0;
547 }
548
Seth Forshee25bded72011-11-07 19:53:36 -0800549 if (z >= 64)
550 input_report_key(dev, BTN_TOUCH, 1);
551 else
552 input_report_key(dev, BTN_TOUCH, 0);
553
Seth Forshee01ce6612011-11-07 19:54:13 -0800554 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
555
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700556 input_mt_report_finger_count(dev, fingers);
Seth Forshee01ce6612011-11-07 19:54:13 -0800557
558 input_report_key(dev, BTN_LEFT, left);
559 input_report_key(dev, BTN_RIGHT, right);
560 input_report_key(dev, BTN_MIDDLE, middle);
561
Seth Forshee25bded72011-11-07 19:53:36 -0800562 if (z > 0) {
563 input_report_abs(dev, ABS_X, x);
564 input_report_abs(dev, ABS_Y, y);
565 }
566 input_report_abs(dev, ABS_PRESSURE, z);
567
Seth Forshee25bded72011-11-07 19:53:36 -0800568 input_sync(dev);
569
570 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
571 left = packet[3] & 0x10;
572 right = packet[3] & 0x20;
573 middle = packet[3] & 0x40;
574
575 input_report_key(dev2, BTN_LEFT, left);
576 input_report_key(dev2, BTN_RIGHT, right);
577 input_report_key(dev2, BTN_MIDDLE, middle);
578 input_sync(dev2);
579 }
580}
581
582static void alps_process_packet_v3(struct psmouse *psmouse)
583{
584 unsigned char *packet = psmouse->packet;
585
586 /*
587 * v3 protocol packets come in three types, two representing
588 * touchpad data and one representing trackstick data.
589 * Trackstick packets seem to be distinguished by always
590 * having 0x3f in the last byte. This value has never been
591 * observed in the last byte of either of the other types
592 * of packets.
593 */
594 if (packet[5] == 0x3f) {
595 alps_process_trackstick_packet_v3(psmouse);
596 return;
597 }
598
599 alps_process_touchpad_packet_v3(psmouse);
600}
601
602static void alps_process_packet_v4(struct psmouse *psmouse)
603{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700604 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800605 unsigned char *packet = psmouse->packet;
606 struct input_dev *dev = psmouse->dev;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700607 int offset;
Seth Forshee25bded72011-11-07 19:53:36 -0800608 int x, y, z;
609 int left, right;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700610 int x1, y1, x2, y2;
611 int fingers = 0;
612 unsigned int x_bitmap, y_bitmap;
613
614 /*
615 * v4 has a 6-byte encoding for bitmap data, but this data is
616 * broken up between 3 normal packets. Use priv->multi_packet to
617 * track our position in the bitmap packet.
618 */
619 if (packet[6] & 0x40) {
620 /* sync, reset position */
621 priv->multi_packet = 0;
622 }
623
624 if (WARN_ON_ONCE(priv->multi_packet > 2))
625 return;
626
627 offset = 2 * priv->multi_packet;
628 priv->multi_data[offset] = packet[6];
629 priv->multi_data[offset + 1] = packet[7];
630
631 if (++priv->multi_packet > 2) {
632 priv->multi_packet = 0;
633
634 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
635 ((priv->multi_data[3] & 0x60) << 3) |
636 ((priv->multi_data[0] & 0x3f) << 2) |
637 ((priv->multi_data[1] & 0x60) >> 5);
638 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
639 ((priv->multi_data[3] & 0x1f) << 5) |
640 (priv->multi_data[1] & 0x1f);
641
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800642 fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
George Pantalos3b7e09f2012-05-10 22:31:59 -0700643 &x1, &y1, &x2, &y2);
644
645 /* Store MT data.*/
646 priv->fingers = fingers;
647 priv->x1 = x1;
648 priv->x2 = x2;
649 priv->y1 = y1;
650 priv->y2 = y2;
651 }
Seth Forshee25bded72011-11-07 19:53:36 -0800652
653 left = packet[4] & 0x01;
654 right = packet[4] & 0x02;
655
656 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
657 ((packet[0] & 0x30) >> 4);
658 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
659 z = packet[5] & 0x7f;
660
George Pantalos3b7e09f2012-05-10 22:31:59 -0700661 /*
662 * If there were no contacts in the bitmap, use ST
663 * points in MT reports.
664 * If there were two contacts or more, report MT data.
665 */
666 if (priv->fingers < 2) {
667 x1 = x;
668 y1 = y;
669 fingers = z > 0 ? 1 : 0;
670 } else {
671 fingers = priv->fingers;
672 x1 = priv->x1;
673 x2 = priv->x2;
674 y1 = priv->y1;
675 y2 = priv->y2;
676 }
677
Seth Forshee25bded72011-11-07 19:53:36 -0800678 if (z >= 64)
679 input_report_key(dev, BTN_TOUCH, 1);
680 else
681 input_report_key(dev, BTN_TOUCH, 0);
682
George Pantalos3b7e09f2012-05-10 22:31:59 -0700683 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
684
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700685 input_mt_report_finger_count(dev, fingers);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700686
687 input_report_key(dev, BTN_LEFT, left);
688 input_report_key(dev, BTN_RIGHT, right);
689
Seth Forshee25bded72011-11-07 19:53:36 -0800690 if (z > 0) {
691 input_report_abs(dev, ABS_X, x);
692 input_report_abs(dev, ABS_Y, y);
693 }
694 input_report_abs(dev, ABS_PRESSURE, z);
695
Seth Forshee25bded72011-11-07 19:53:36 -0800696 input_sync(dev);
697}
698
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800699static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
700 unsigned char packet[],
701 bool report_buttons)
702{
703 struct alps_data *priv = psmouse->private;
704 struct input_dev *dev2 = priv->dev2;
705
706 if (report_buttons)
707 alps_report_buttons(psmouse, dev2, psmouse->dev,
708 packet[0] & 1, packet[0] & 2, packet[0] & 4);
709
710 input_report_rel(dev2, REL_X,
711 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
712 input_report_rel(dev2, REL_Y,
713 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
714
715 input_sync(dev2);
716}
717
718static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct alps_data *priv = psmouse->private;
721
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800722 if (psmouse->pktcnt < 6)
723 return PSMOUSE_GOOD_DATA;
724
725 if (psmouse->pktcnt == 6) {
726 /*
727 * Start a timer to flush the packet if it ends up last
728 * 6-byte packet in the stream. Timer needs to fire
729 * psmouse core times out itself. 20 ms should be enough
730 * to decide if we are getting more data or not.
731 */
732 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
733 return PSMOUSE_GOOD_DATA;
734 }
735
736 del_timer(&priv->timer);
737
738 if (psmouse->packet[6] & 0x80) {
739
740 /*
741 * Highest bit is set - that means we either had
742 * complete ALPS packet and this is start of the
743 * next packet or we got garbage.
744 */
745
746 if (((psmouse->packet[3] |
747 psmouse->packet[4] |
748 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800749 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700750 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700751 "refusing packet %4ph (suspected interleaved ps/2)\n",
752 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800753 return PSMOUSE_BAD_DATA;
754 }
755
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800756 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800757
758 /* Continue with the next packet */
759 psmouse->packet[0] = psmouse->packet[6];
760 psmouse->pktcnt = 1;
761
762 } else {
763
764 /*
765 * High bit is 0 - that means that we indeed got a PS/2
766 * packet in the middle of ALPS packet.
767 *
768 * There is also possibility that we got 6-byte ALPS
769 * packet followed by 3-byte packet from trackpoint. We
770 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700771 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800772 * normal operation (user would need to press all
773 * buttons on the pad and start moving trackpoint
774 * without touching the pad surface) we assume former.
775 * Even if we are wrong the wost thing that would happen
776 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700777 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800778 */
779
780 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
781 false);
782
783 /*
784 * Continue with the standard ALPS protocol handling,
785 * but make sure we won't process it as an interleaved
786 * packet again, which may happen if all buttons are
787 * pressed. To avoid this let's reset the 4th bit which
788 * is normally 1.
789 */
790 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
791 psmouse->pktcnt = 4;
792 }
793
794 return PSMOUSE_GOOD_DATA;
795}
796
797static void alps_flush_packet(unsigned long data)
798{
799 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800800 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800801
802 serio_pause_rx(psmouse->ps2dev.serio);
803
Seth Forsheeb46615f2011-11-07 19:53:30 -0800804 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800805
806 /*
807 * We did not any more data in reasonable amount of time.
808 * Validate the last 3 bytes and process as a standard
809 * ALPS packet.
810 */
811 if ((psmouse->packet[3] |
812 psmouse->packet[4] |
813 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700814 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700815 "refusing packet %3ph (suspected interleaved ps/2)\n",
816 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800817 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800818 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800819 }
820 psmouse->pktcnt = 0;
821 }
822
823 serio_continue_rx(psmouse->ps2dev.serio);
824}
825
826static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
827{
828 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
831 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800832 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
833 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return PSMOUSE_FULL_PACKET;
835 }
836 return PSMOUSE_GOOD_DATA;
837 }
838
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800839 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
840
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800841 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800842 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
843 return alps_handle_interleaved_ps2(psmouse);
844 }
845
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800846 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700847 psmouse_dbg(psmouse,
848 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800849 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Seth Forsheeb46615f2011-11-07 19:53:30 -0800853 /* Bytes 2 - pktsize should have 0 in the highest bit */
854 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800855 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700856 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
857 psmouse->pktcnt - 1,
858 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Seth Forsheeb46615f2011-11-07 19:53:30 -0800862 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800863 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return PSMOUSE_FULL_PACKET;
865 }
866
867 return PSMOUSE_GOOD_DATA;
868}
869
Seth Forshee25bded72011-11-07 19:53:36 -0800870static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
871{
872 struct ps2dev *ps2dev = &psmouse->ps2dev;
873 struct alps_data *priv = psmouse->private;
874 int command;
875 unsigned char *param;
876 unsigned char dummy[4];
877
878 BUG_ON(nibble > 0xf);
879
880 command = priv->nibble_commands[nibble].command;
881 param = (command & 0x0f00) ?
882 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
883
884 if (ps2_command(ps2dev, param, command))
885 return -1;
886
887 return 0;
888}
889
890static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
891{
892 struct ps2dev *ps2dev = &psmouse->ps2dev;
893 struct alps_data *priv = psmouse->private;
894 int i, nibble;
895
896 if (ps2_command(ps2dev, NULL, priv->addr_command))
897 return -1;
898
899 for (i = 12; i >= 0; i -= 4) {
900 nibble = (addr >> i) & 0xf;
901 if (alps_command_mode_send_nibble(psmouse, nibble))
902 return -1;
903 }
904
905 return 0;
906}
907
908static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
909{
910 struct ps2dev *ps2dev = &psmouse->ps2dev;
911 unsigned char param[4];
912
913 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
914 return -1;
915
916 /*
917 * The address being read is returned in the first two bytes
918 * of the result. Check that this address matches the expected
919 * address.
920 */
921 if (addr != ((param[0] << 8) | param[1]))
922 return -1;
923
924 return param[2];
925}
926
927static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
928{
929 if (alps_command_mode_set_addr(psmouse, addr))
930 return -1;
931 return __alps_command_mode_read_reg(psmouse, addr);
932}
933
934static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
935{
936 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
937 return -1;
938 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
939 return -1;
940 return 0;
941}
942
943static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
944 u8 value)
945{
946 if (alps_command_mode_set_addr(psmouse, addr))
947 return -1;
948 return __alps_command_mode_write_reg(psmouse, value);
949}
950
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800951static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
952 int repeated_command, unsigned char *param)
953{
954 struct ps2dev *ps2dev = &psmouse->ps2dev;
955
956 param[0] = 0;
957 if (init_command && ps2_command(ps2dev, param, init_command))
958 return -EIO;
959
960 if (ps2_command(ps2dev, NULL, repeated_command) ||
961 ps2_command(ps2dev, NULL, repeated_command) ||
962 ps2_command(ps2dev, NULL, repeated_command))
963 return -EIO;
964
965 param[0] = param[1] = param[2] = 0xff;
966 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
967 return -EIO;
968
969 psmouse_dbg(psmouse, "%2.2X report: %2.2x %2.2x %2.2x\n",
970 repeated_command, param[0], param[1], param[2]);
971 return 0;
972}
973
Seth Forshee25bded72011-11-07 19:53:36 -0800974static int alps_enter_command_mode(struct psmouse *psmouse,
975 unsigned char *resp)
976{
977 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -0800978
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800979 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800980 psmouse_err(psmouse, "failed to enter command mode\n");
981 return -1;
982 }
983
Kevin Cernekee56fd3402013-02-13 22:24:22 -0800984 if (param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800985 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800986 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -0800987 return -1;
988 }
989
990 if (resp)
991 *resp = param[2];
992 return 0;
993}
994
995static inline int alps_exit_command_mode(struct psmouse *psmouse)
996{
997 struct ps2dev *ps2dev = &psmouse->ps2dev;
998 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
999 return -1;
1000 return 0;
1001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/*
1004 * For DualPoint devices select the device that should respond to
1005 * subsequent commands. It looks like glidepad is behind stickpointer,
1006 * I'd thought it would be other way around...
1007 */
Seth Forshee25bded72011-11-07 19:53:36 -08001008static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1012
1013 if (ps2_command(ps2dev, NULL, cmd) ||
1014 ps2_command(ps2dev, NULL, cmd) ||
1015 ps2_command(ps2dev, NULL, cmd) ||
1016 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1017 return -1;
1018
1019 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001020 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 return 0;
1023}
1024
Seth Forshee25bded72011-11-07 19:53:36 -08001025static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct ps2dev *ps2dev = &psmouse->ps2dev;
1028
1029 /* Try ALPS magic knock - 4 disable before enable */
1030 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1031 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1032 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1033 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1034 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1035 return -1;
1036
1037 /*
1038 * Switch mouse to poll (remote) mode so motion data will not
1039 * get in our way
1040 */
1041 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1042}
1043
1044static int alps_get_status(struct psmouse *psmouse, char *param)
1045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001047 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -1;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return 0;
1051}
1052
1053/*
1054 * Turn touchpad tapping on or off. The sequences are:
1055 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1056 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1057 * My guess that 0xE9 (GetInfo) is here as a sync point.
1058 * For models that also have stickpointer (DualPoints) its tapping
1059 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1060 * we don't fiddle with it.
1061 */
1062static int alps_tap_mode(struct psmouse *psmouse, int enable)
1063{
1064 struct ps2dev *ps2dev = &psmouse->ps2dev;
1065 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1066 unsigned char tap_arg = enable ? 0x0A : 0x00;
1067 unsigned char param[4];
1068
1069 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1070 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1071 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1072 ps2_command(ps2dev, &tap_arg, cmd))
1073 return -1;
1074
1075 if (alps_get_status(psmouse, param))
1076 return -1;
1077
1078 return 0;
1079}
1080
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001081/*
1082 * alps_poll() - poll the touchpad for current motion packet.
1083 * Used in resync.
1084 */
1085static int alps_poll(struct psmouse *psmouse)
1086{
1087 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001088 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001089 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001090
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001091 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001092 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001093
1094 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1095 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1096
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001097 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001098 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001099
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001100 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001101 return -1;
1102
1103 if ((psmouse->badbyte & 0xc8) == 0x08) {
1104/*
1105 * Poll the track stick ...
1106 */
1107 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1108 return -1;
1109 }
1110
1111 memcpy(psmouse->packet, buf, sizeof(buf));
1112 return 0;
1113}
1114
Seth Forshee25bded72011-11-07 19:53:36 -08001115static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001119 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001120 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001124 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001125 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Seth Forshee25bded72011-11-07 19:53:36 -08001129 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001130 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -1;
1132 }
1133
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001134 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001135 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001139 /* ALPS needs stream mode, otherwise it won't report any data */
1140 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001141 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001142 return -1;
1143 }
1144
1145 return 0;
1146}
1147
Seth Forshee25bded72011-11-07 19:53:36 -08001148/*
1149 * Enable or disable passthrough mode to the trackstick. Must be in
1150 * command mode when calling this function.
1151 */
1152static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable)
1153{
1154 int reg_val;
1155
1156 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1157 if (reg_val == -1)
1158 return -1;
1159
1160 if (enable)
1161 reg_val |= 0x01;
1162 else
1163 reg_val &= ~0x01;
1164
1165 if (__alps_command_mode_write_reg(psmouse, reg_val))
1166 return -1;
1167
1168 return 0;
1169}
1170
1171/* Must be in command mode when calling this function */
1172static int alps_absolute_mode_v3(struct psmouse *psmouse)
1173{
1174 int reg_val;
1175
1176 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1177 if (reg_val == -1)
1178 return -1;
1179
1180 reg_val |= 0x06;
1181 if (__alps_command_mode_write_reg(psmouse, reg_val))
1182 return -1;
1183
1184 return 0;
1185}
1186
1187static int alps_hw_init_v3(struct psmouse *psmouse)
1188{
Seth Forshee25bded72011-11-07 19:53:36 -08001189 struct ps2dev *ps2dev = &psmouse->ps2dev;
1190 int reg_val;
1191 unsigned char param[4];
1192
Seth Forshee25bded72011-11-07 19:53:36 -08001193 if (alps_enter_command_mode(psmouse, NULL))
1194 goto error;
1195
1196 /* Check for trackstick */
1197 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1198 if (reg_val == -1)
1199 goto error;
1200 if (reg_val & 0x80) {
1201 if (alps_passthrough_mode_v3(psmouse, true))
1202 goto error;
1203 if (alps_exit_command_mode(psmouse))
1204 goto error;
1205
1206 /*
1207 * E7 report for the trackstick
1208 *
1209 * There have been reports of failures to seem to trace back
1210 * to the above trackstick check failing. When these occur
1211 * this E7 report fails, so when that happens we continue
1212 * with the assumption that there isn't a trackstick after
1213 * all.
1214 */
1215 param[0] = 0x64;
1216 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1217 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1218 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1219 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1220 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1221 } else {
1222 psmouse_dbg(psmouse,
1223 "trackstick E7 report: %2.2x %2.2x %2.2x\n",
1224 param[0], param[1], param[2]);
1225
1226 /*
1227 * Not sure what this does, but it is absolutely
1228 * essential. Without it, the touchpad does not
1229 * work at all and the trackstick just emits normal
1230 * PS/2 packets.
1231 */
1232 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1233 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1234 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1235 alps_command_mode_send_nibble(psmouse, 0x9) ||
1236 alps_command_mode_send_nibble(psmouse, 0x4)) {
1237 psmouse_err(psmouse,
1238 "Error sending magic E6 sequence\n");
1239 goto error_passthrough;
1240 }
1241 }
1242
1243 if (alps_enter_command_mode(psmouse, NULL))
1244 goto error_passthrough;
1245 if (alps_passthrough_mode_v3(psmouse, false))
1246 goto error;
1247 }
1248
1249 if (alps_absolute_mode_v3(psmouse)) {
1250 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1251 goto error;
1252 }
1253
1254 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1255 if (reg_val == -1)
1256 goto error;
1257 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1258 goto error;
1259
1260 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1261 if (reg_val == -1)
1262 goto error;
1263 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1264 goto error;
1265
1266 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1267 goto error;
1268 if (__alps_command_mode_write_reg(psmouse, 0x04))
1269 goto error;
1270
1271 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1272 goto error;
1273 if (__alps_command_mode_write_reg(psmouse, 0x03))
1274 goto error;
1275
1276 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1277 goto error;
1278 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1279 goto error;
1280
1281 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1282 goto error;
1283 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1284 goto error;
1285
1286 /*
1287 * This ensures the trackstick packets are in the format
1288 * supported by this driver. If bit 1 isn't set the packet
1289 * format is different.
1290 */
1291 if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82))
1292 goto error;
1293
1294 alps_exit_command_mode(psmouse);
1295
1296 /* Set rate and enable data reporting */
1297 param[0] = 0x64;
1298 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1299 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1300 psmouse_err(psmouse, "Failed to enable data reporting\n");
1301 return -1;
1302 }
1303
1304 return 0;
1305
1306error_passthrough:
1307 /* Something failed while in passthrough mode, so try to get out */
1308 if (!alps_enter_command_mode(psmouse, NULL))
1309 alps_passthrough_mode_v3(psmouse, false);
1310error:
1311 /*
1312 * Leaving the touchpad in command mode will essentially render
1313 * it unusable until the machine reboots, so exit it here just
1314 * to be safe
1315 */
1316 alps_exit_command_mode(psmouse);
1317 return -1;
1318}
1319
1320/* Must be in command mode when calling this function */
1321static int alps_absolute_mode_v4(struct psmouse *psmouse)
1322{
1323 int reg_val;
1324
1325 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1326 if (reg_val == -1)
1327 return -1;
1328
1329 reg_val |= 0x02;
1330 if (__alps_command_mode_write_reg(psmouse, reg_val))
1331 return -1;
1332
1333 return 0;
1334}
1335
1336static int alps_hw_init_v4(struct psmouse *psmouse)
1337{
Seth Forshee25bded72011-11-07 19:53:36 -08001338 struct ps2dev *ps2dev = &psmouse->ps2dev;
1339 unsigned char param[4];
1340
Seth Forshee25bded72011-11-07 19:53:36 -08001341 if (alps_enter_command_mode(psmouse, NULL))
1342 goto error;
1343
1344 if (alps_absolute_mode_v4(psmouse)) {
1345 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1346 goto error;
1347 }
1348
1349 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1350 goto error;
1351
1352 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1353 goto error;
1354
1355 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1356 goto error;
1357
1358 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1359 goto error;
1360
1361 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1362 goto error;
1363
1364 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1365 goto error;
1366
1367 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1368 goto error;
1369
1370 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1371 goto error;
1372
1373 alps_exit_command_mode(psmouse);
1374
1375 /*
1376 * This sequence changes the output from a 9-byte to an
1377 * 8-byte format. All the same data seems to be present,
1378 * just in a more compact format.
1379 */
1380 param[0] = 0xc8;
1381 param[1] = 0x64;
1382 param[2] = 0x50;
1383 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1384 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1385 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1386 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1387 return -1;
1388
1389 /* Set rate and enable data reporting */
1390 param[0] = 0x64;
1391 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1392 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1393 psmouse_err(psmouse, "Failed to enable data reporting\n");
1394 return -1;
1395 }
1396
1397 return 0;
1398
1399error:
1400 /*
1401 * Leaving the touchpad in command mode will essentially render
1402 * it unusable until the machine reboots, so exit it here just
1403 * to be safe
1404 */
1405 alps_exit_command_mode(psmouse);
1406 return -1;
1407}
1408
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001409static void alps_set_defaults(struct alps_data *priv)
Seth Forshee25bded72011-11-07 19:53:36 -08001410{
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001411 priv->byte0 = 0x8f;
1412 priv->mask0 = 0x8f;
1413 priv->flags = ALPS_DUALPOINT;
1414
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001415 priv->x_max = 2000;
1416 priv->y_max = 1400;
1417 priv->x_bits = 15;
1418 priv->y_bits = 11;
1419
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001420 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001421 case ALPS_PROTO_V1:
1422 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001423 priv->hw_init = alps_hw_init_v1_v2;
1424 priv->process_packet = alps_process_packet_v1_v2;
1425 priv->set_abs_params = alps_set_abs_params_st;
Seth Forshee25bded72011-11-07 19:53:36 -08001426 break;
1427 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001428 priv->hw_init = alps_hw_init_v3;
1429 priv->process_packet = alps_process_packet_v3;
1430 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001431 priv->nibble_commands = alps_v3_nibble_commands;
1432 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08001433 break;
1434 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001435 priv->hw_init = alps_hw_init_v4;
1436 priv->process_packet = alps_process_packet_v4;
1437 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001438 priv->nibble_commands = alps_v4_nibble_commands;
1439 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08001440 break;
1441 }
Seth Forshee25bded72011-11-07 19:53:36 -08001442}
1443
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001444static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1445 unsigned char *e7, unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001446{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001447 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001448 int i;
1449
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001450 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1451 model = &alps_model_data[i];
1452
1453 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1454 (!model->command_mode_resp ||
1455 model->command_mode_resp == ec[2])) {
1456
1457 priv->proto_version = model->proto_version;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001458 alps_set_defaults(priv);
1459
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001460 priv->flags = model->flags;
1461 priv->byte0 = model->byte0;
1462 priv->mask0 = model->mask0;
1463
1464 return 0;
1465 }
1466 }
1467
1468 return -EINVAL;
1469}
1470
1471static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1472{
1473 unsigned char e6[4], e7[4], ec[4];
1474
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001475 /*
1476 * First try "E6 report".
1477 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1478 * The bits 0-2 of the first byte will be 1s if some buttons are
1479 * pressed.
1480 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001481 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1482 PSMOUSE_CMD_SETSCALE11, e6))
1483 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001484
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001485 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1486 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001487
1488 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001489 * Now get the "E7" and "EC" reports. These will uniquely identify
1490 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001491 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001492 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1493 PSMOUSE_CMD_SETSCALE21, e7) ||
1494 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1495 PSMOUSE_CMD_RESET_WRAP, ec) ||
1496 alps_exit_command_mode(psmouse))
1497 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001498
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001499 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001500 return 0;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001501 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1502 ec[2] >= 0x90 && ec[2] <= 0x9d) {
1503 priv->proto_version = ALPS_PROTO_V3;
1504 alps_set_defaults(priv);
1505
1506 return 0;
1507 }
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001508
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001509 psmouse_info(psmouse,
1510 "Unknown ALPS touchpad: E7=%2.2x %2.2x %2.2x, EC=%2.2x %2.2x %2.2x\n",
1511 e7[0], e7[1], e7[2], ec[0], ec[1], ec[2]);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001512
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001513 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001514}
1515
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001516static int alps_reconnect(struct psmouse *psmouse)
1517{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001518 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001519
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001520 psmouse_reset(psmouse);
1521
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001522 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001523 return -1;
1524
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001525 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528static void alps_disconnect(struct psmouse *psmouse)
1529{
1530 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001533 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001534 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 kfree(priv);
1536}
1537
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001538static void alps_set_abs_params_st(struct alps_data *priv,
1539 struct input_dev *dev1)
1540{
1541 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1542 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1543}
1544
1545static void alps_set_abs_params_mt(struct alps_data *priv,
1546 struct input_dev *dev1)
1547{
1548 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
1549 input_mt_init_slots(dev1, 2, 0);
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001550 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
1551 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001552
1553 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1554 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1555 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
1556
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08001557 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
1558 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001559}
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561int alps_init(struct psmouse *psmouse)
1562{
1563 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001564 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001566 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001567 dev2 = input_allocate_device();
1568 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001570
1571 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001572 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1573
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001574 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Seth Forshee25bded72011-11-07 19:53:36 -08001576 psmouse_reset(psmouse);
1577
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001578 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001579 goto init_fail;
1580
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001581 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 goto init_fail;
1583
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08001584 /*
1585 * Undo part of setup done for us by psmouse core since touchpad
1586 * is not a relative device.
1587 */
1588 __clear_bit(EV_REL, dev1->evbit);
1589 __clear_bit(REL_X, dev1->relbit);
1590 __clear_bit(REL_Y, dev1->relbit);
1591
1592 /*
1593 * Now set up our capabilities.
1594 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001595 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1596 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1597 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001598 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1599 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001601 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08001602
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001603 priv->set_abs_params(priv, dev1);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001604 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001606 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001607 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1608 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001611 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001612 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1613 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001616 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001617 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1618 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1619 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1620 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1621 } else {
1622 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1623 }
1624
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001625 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001626 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001627 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1628 "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001629 dev2->id.bustype = BUS_I8042;
1630 dev2->id.vendor = 0x0002;
1631 dev2->id.product = PSMOUSE_ALPS;
1632 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04001633 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001634
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001635 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001636 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1637 dev2->keybit[BIT_WORD(BTN_LEFT)] =
1638 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001640 if (input_register_device(priv->dev2))
1641 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001644 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 psmouse->disconnect = alps_disconnect;
1646 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001647 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001649 /* We are having trouble resyncing ALPS touchpads so disable it for now */
1650 psmouse->resync_time = 0;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
1653
1654init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001655 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001656 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001658 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return -1;
1660}
1661
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001662int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001664 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001666 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return -1;
1668
1669 if (set_properties) {
1670 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001671 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001672 "DualPoint TouchPad" : "GlidePoint";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001673 psmouse->model = dummy.proto_version << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675 return 0;
1676}
1677