blob: c473549c33bd0dd6315bec62df7dfb88eab0114a [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 */
Seth Forshee01ce6612011-11-07 19:54:13 -080030#define ALPS_V3_X_MAX 2000
31#define ALPS_V3_Y_MAX 1400
32
33#define ALPS_BITMAP_X_BITS 15
34#define ALPS_BITMAP_Y_BITS 11
35
Seth Forshee25bded72011-11-07 19:53:36 -080036#define ALPS_CMD_NIBBLE_10 0x01f2
37
38static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
39 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
40 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
41 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
42 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
43 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
44 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
45 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
46 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
47 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
48 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
49 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
50 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
51 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
52 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
53 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
54 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
55};
56
57static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
58 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
59 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
60 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
61 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
62 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
63 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
64 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
65 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
66 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
67 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
68 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
69 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
70 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
71 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
72 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
73 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
74};
75
76
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080077#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
78#define ALPS_PASS 0x04 /* device has a pass-through port */
79
80#define ALPS_WHEEL 0x08 /* hardware wheel present */
81#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
82#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
83#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -080084#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
85 6-byte ALPS packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Helge Dellere38de672006-09-10 21:54:39 -040087static const struct alps_model_info alps_model_data[] = {
Seth Forshee25bded72011-11-07 19:53:36 -080088 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
89 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
90 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
91 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
92 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
93 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
94 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
95 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
96 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
97 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
98 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
99 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
100 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
101 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
102 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
103 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
104 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
105 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800106 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Seth Forshee25bded72011-11-07 19:53:36 -0800107 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800108 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
Seth Forshee25bded72011-11-07 19:53:36 -0800109 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
110 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
111 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
112 { { 0x73, 0x02, 0x64 }, 0x9b, ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT },
113 { { 0x73, 0x02, 0x64 }, 0x9d, ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT },
114 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117/*
118 * XXX - this entry is suspicious. First byte has zero lower nibble,
119 * which is what a normal mouse would report. Also, the value 0x0e
120 * isn't valid per PS/2 spec.
121 */
122
Seth Forsheed4b347b2011-11-07 19:53:15 -0800123/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800125static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800126 unsigned char data)
127{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800128 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800129}
130
131static void alps_report_buttons(struct psmouse *psmouse,
132 struct input_dev *dev1, struct input_dev *dev2,
133 int left, int right, int middle)
134{
Martin Buckc91ed052010-03-13 22:23:58 -0800135 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800136
Martin Buckc91ed052010-03-13 22:23:58 -0800137 /*
138 * If shared button has already been reported on the
139 * other device (dev2) then this event should be also
140 * sent through that device.
141 */
142 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
143 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800144
Martin Buckc91ed052010-03-13 22:23:58 -0800145 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
146 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800147
Martin Buckc91ed052010-03-13 22:23:58 -0800148 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
149 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800150
Martin Buckc91ed052010-03-13 22:23:58 -0800151 /*
152 * Sync the _other_ device now, we'll do the first
153 * device later once we report the rest of the events.
154 */
155 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800156}
157
Seth Forshee25bded72011-11-07 19:53:36 -0800158static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct alps_data *priv = psmouse->private;
161 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500162 struct input_dev *dev = psmouse->dev;
163 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500165 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800167 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400168 left = packet[2] & 0x10;
169 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 middle = 0;
171 x = packet[1] | ((packet[0] & 0x07) << 7);
172 y = packet[4] | ((packet[3] & 0x07) << 7);
173 z = packet[5];
174 } else {
175 left = packet[3] & 1;
176 right = packet[3] & 2;
177 middle = packet[3] & 4;
178 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
179 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
180 z = packet[5];
181 }
182
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800183 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400184 back = packet[0] & 0x10;
185 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500186 }
187
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800188 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500189 back = packet[3] & 4;
190 forward = packet[2] & 4;
191 if ((middle = forward && back))
192 forward = back = 0;
193 }
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ges = packet[2] & 1;
196 fin = packet[2] & 2;
197
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800198 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
200 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700201
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800202 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 input_sync(dev2);
205 return;
206 }
207
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800208 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800211 if (ges && !fin)
212 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /*
215 * A "tap and drag" operation is reported by the hardware as a transition
216 * from (!fin && ges) to (fin && ges). This should be translated to the
217 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
218 */
219 if (ges && fin && !priv->prev_fin) {
220 input_report_abs(dev, ABS_X, x);
221 input_report_abs(dev, ABS_Y, y);
222 input_report_abs(dev, ABS_PRESSURE, 0);
223 input_report_key(dev, BTN_TOOL_FINGER, 0);
224 input_sync(dev);
225 }
226 priv->prev_fin = fin;
227
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800228 if (z > 30)
229 input_report_key(dev, BTN_TOUCH, 1);
230 if (z < 25)
231 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (z > 0) {
234 input_report_abs(dev, ABS_X, x);
235 input_report_abs(dev, ABS_Y, y);
236 }
237
238 input_report_abs(dev, ABS_PRESSURE, z);
239 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
240
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800241 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500242 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800244 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500245 input_report_key(dev, BTN_FORWARD, forward);
246 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800249 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800250 input_report_key(dev, BTN_0, packet[2] & 4);
251 input_report_key(dev, BTN_1, packet[0] & 0x10);
252 input_report_key(dev, BTN_2, packet[3] & 4);
253 input_report_key(dev, BTN_3, packet[0] & 0x20);
254 }
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 input_sync(dev);
257}
258
Seth Forshee01ce6612011-11-07 19:54:13 -0800259/*
260 * Process bitmap data from v3 and v4 protocols. Returns the number of
261 * fingers detected. A return value of 0 means at least one of the
262 * bitmaps was empty.
263 *
264 * The bitmaps don't have enough data to track fingers, so this function
265 * only generates points representing a bounding box of all contacts.
266 * These points are returned in x1, y1, x2, and y2 when the return value
267 * is greater than 0.
268 */
269static int alps_process_bitmap(unsigned int x_map, unsigned int y_map,
270 int *x1, int *y1, int *x2, int *y2)
271{
272 struct alps_bitmap_point {
273 int start_bit;
274 int num_bits;
275 };
276
277 int fingers_x = 0, fingers_y = 0, fingers;
278 int i, bit, prev_bit;
279 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
280 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
281 struct alps_bitmap_point *point;
282
283 if (!x_map || !y_map)
284 return 0;
285
286 *x1 = *y1 = *x2 = *y2 = 0;
287
288 prev_bit = 0;
289 point = &x_low;
290 for (i = 0; x_map != 0; i++, x_map >>= 1) {
291 bit = x_map & 1;
292 if (bit) {
293 if (!prev_bit) {
294 point->start_bit = i;
295 fingers_x++;
296 }
297 point->num_bits++;
298 } else {
299 if (prev_bit)
300 point = &x_high;
301 else
302 point->num_bits = 0;
303 }
304 prev_bit = bit;
305 }
306
307 /*
308 * y bitmap is reversed for what we need (lower positions are in
309 * higher bits), so we process from the top end.
310 */
311 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - ALPS_BITMAP_Y_BITS);
312 prev_bit = 0;
313 point = &y_low;
314 for (i = 0; y_map != 0; i++, y_map <<= 1) {
315 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
316 if (bit) {
317 if (!prev_bit) {
318 point->start_bit = i;
319 fingers_y++;
320 }
321 point->num_bits++;
322 } else {
323 if (prev_bit)
324 point = &y_high;
325 else
326 point->num_bits = 0;
327 }
328 prev_bit = bit;
329 }
330
331 /*
332 * Fingers can overlap, so we use the maximum count of fingers
333 * on either axis as the finger count.
334 */
335 fingers = max(fingers_x, fingers_y);
336
337 /*
338 * If total fingers is > 1 but either axis reports only a single
339 * contact, we have overlapping or adjacent fingers. For the
340 * purposes of creating a bounding box, divide the single contact
341 * (roughly) equally between the two points.
342 */
343 if (fingers > 1) {
344 if (fingers_x == 1) {
345 i = x_low.num_bits / 2;
346 x_low.num_bits = x_low.num_bits - i;
347 x_high.start_bit = x_low.start_bit + i;
348 x_high.num_bits = max(i, 1);
349 } else if (fingers_y == 1) {
350 i = y_low.num_bits / 2;
351 y_low.num_bits = y_low.num_bits - i;
352 y_high.start_bit = y_low.start_bit + i;
353 y_high.num_bits = max(i, 1);
354 }
355 }
356
357 *x1 = (ALPS_V3_X_MAX * (2 * x_low.start_bit + x_low.num_bits - 1)) /
358 (2 * (ALPS_BITMAP_X_BITS - 1));
359 *y1 = (ALPS_V3_Y_MAX * (2 * y_low.start_bit + y_low.num_bits - 1)) /
360 (2 * (ALPS_BITMAP_Y_BITS - 1));
361
362 if (fingers > 1) {
363 *x2 = (ALPS_V3_X_MAX * (2 * x_high.start_bit + x_high.num_bits - 1)) /
364 (2 * (ALPS_BITMAP_X_BITS - 1));
365 *y2 = (ALPS_V3_Y_MAX * (2 * y_high.start_bit + y_high.num_bits - 1)) /
366 (2 * (ALPS_BITMAP_Y_BITS - 1));
367 }
368
369 return fingers;
370}
371
372static void alps_set_slot(struct input_dev *dev, int slot, bool active,
373 int x, int y)
374{
375 input_mt_slot(dev, slot);
376 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
377 if (active) {
378 input_report_abs(dev, ABS_MT_POSITION_X, x);
379 input_report_abs(dev, ABS_MT_POSITION_Y, y);
380 }
381}
382
383static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
384 int x1, int y1, int x2, int y2)
385{
386 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
387 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
388}
389
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
484 bmap_fingers = alps_process_bitmap(x_bitmap, y_bitmap,
485 &x1, &y1, &x2, &y2);
486
Seth Forshee25bded72011-11-07 19:53:36 -0800487 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800488 * We shouldn't report more than one finger if
489 * we don't have two coordinates.
Seth Forshee25bded72011-11-07 19:53:36 -0800490 */
Seth Forshee01ce6612011-11-07 19:54:13 -0800491 if (fingers > 1 && bmap_fingers < 2)
492 fingers = bmap_fingers;
493
494 /* Now process position packet */
495 packet = priv->multi_data;
496 } else {
497 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800498 }
499 }
500
Seth Forshee01ce6612011-11-07 19:54:13 -0800501 /*
502 * Bit 6 of byte 0 is not usually set in position packets. The only
503 * times it seems to be set is in situations where the data is
504 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
505 * this combined with the fact that this bit is useful for filtering
506 * out misidentified bitmap packets, we reject anything with this
507 * bit set.
508 */
509 if (packet[0] & 0x40)
510 return;
511
512 if (!priv->multi_packet && (packet[4] & 0x40)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800513 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800514 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
515 return;
516 }
517
518 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800519
520 left = packet[3] & 0x01;
521 right = packet[3] & 0x02;
522 middle = packet[3] & 0x04;
523
524 x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) |
525 ((packet[0] & 0x30) >> 4);
526 y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f);
527 z = packet[5] & 0x7f;
528
529 /*
530 * Sometimes the hardware sends a single packet with z = 0
531 * in the middle of a stream. Real releases generate packets
532 * with x, y, and z all zero, so these seem to be flukes.
533 * Ignore them.
534 */
535 if (x && y && !z)
536 return;
537
Seth Forshee01ce6612011-11-07 19:54:13 -0800538 /*
539 * If we don't have MT data or the bitmaps were empty, we have
540 * to rely on ST data.
541 */
542 if (!fingers) {
543 x1 = x;
544 y1 = y;
545 fingers = z > 0 ? 1 : 0;
546 }
547
Seth Forshee25bded72011-11-07 19:53:36 -0800548 if (z >= 64)
549 input_report_key(dev, BTN_TOUCH, 1);
550 else
551 input_report_key(dev, BTN_TOUCH, 0);
552
Seth Forshee01ce6612011-11-07 19:54:13 -0800553 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
554
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700555 input_mt_report_finger_count(dev, fingers);
Seth Forshee01ce6612011-11-07 19:54:13 -0800556
557 input_report_key(dev, BTN_LEFT, left);
558 input_report_key(dev, BTN_RIGHT, right);
559 input_report_key(dev, BTN_MIDDLE, middle);
560
Seth Forshee25bded72011-11-07 19:53:36 -0800561 if (z > 0) {
562 input_report_abs(dev, ABS_X, x);
563 input_report_abs(dev, ABS_Y, y);
564 }
565 input_report_abs(dev, ABS_PRESSURE, z);
566
Seth Forshee25bded72011-11-07 19:53:36 -0800567 input_sync(dev);
568
569 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
570 left = packet[3] & 0x10;
571 right = packet[3] & 0x20;
572 middle = packet[3] & 0x40;
573
574 input_report_key(dev2, BTN_LEFT, left);
575 input_report_key(dev2, BTN_RIGHT, right);
576 input_report_key(dev2, BTN_MIDDLE, middle);
577 input_sync(dev2);
578 }
579}
580
581static void alps_process_packet_v3(struct psmouse *psmouse)
582{
583 unsigned char *packet = psmouse->packet;
584
585 /*
586 * v3 protocol packets come in three types, two representing
587 * touchpad data and one representing trackstick data.
588 * Trackstick packets seem to be distinguished by always
589 * having 0x3f in the last byte. This value has never been
590 * observed in the last byte of either of the other types
591 * of packets.
592 */
593 if (packet[5] == 0x3f) {
594 alps_process_trackstick_packet_v3(psmouse);
595 return;
596 }
597
598 alps_process_touchpad_packet_v3(psmouse);
599}
600
601static void alps_process_packet_v4(struct psmouse *psmouse)
602{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700603 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800604 unsigned char *packet = psmouse->packet;
605 struct input_dev *dev = psmouse->dev;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700606 int offset;
Seth Forshee25bded72011-11-07 19:53:36 -0800607 int x, y, z;
608 int left, right;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700609 int x1, y1, x2, y2;
610 int fingers = 0;
611 unsigned int x_bitmap, y_bitmap;
612
613 /*
614 * v4 has a 6-byte encoding for bitmap data, but this data is
615 * broken up between 3 normal packets. Use priv->multi_packet to
616 * track our position in the bitmap packet.
617 */
618 if (packet[6] & 0x40) {
619 /* sync, reset position */
620 priv->multi_packet = 0;
621 }
622
623 if (WARN_ON_ONCE(priv->multi_packet > 2))
624 return;
625
626 offset = 2 * priv->multi_packet;
627 priv->multi_data[offset] = packet[6];
628 priv->multi_data[offset + 1] = packet[7];
629
630 if (++priv->multi_packet > 2) {
631 priv->multi_packet = 0;
632
633 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
634 ((priv->multi_data[3] & 0x60) << 3) |
635 ((priv->multi_data[0] & 0x3f) << 2) |
636 ((priv->multi_data[1] & 0x60) >> 5);
637 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
638 ((priv->multi_data[3] & 0x1f) << 5) |
639 (priv->multi_data[1] & 0x1f);
640
641 fingers = alps_process_bitmap(x_bitmap, y_bitmap,
642 &x1, &y1, &x2, &y2);
643
644 /* Store MT data.*/
645 priv->fingers = fingers;
646 priv->x1 = x1;
647 priv->x2 = x2;
648 priv->y1 = y1;
649 priv->y2 = y2;
650 }
Seth Forshee25bded72011-11-07 19:53:36 -0800651
652 left = packet[4] & 0x01;
653 right = packet[4] & 0x02;
654
655 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
656 ((packet[0] & 0x30) >> 4);
657 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
658 z = packet[5] & 0x7f;
659
George Pantalos3b7e09f2012-05-10 22:31:59 -0700660 /*
661 * If there were no contacts in the bitmap, use ST
662 * points in MT reports.
663 * If there were two contacts or more, report MT data.
664 */
665 if (priv->fingers < 2) {
666 x1 = x;
667 y1 = y;
668 fingers = z > 0 ? 1 : 0;
669 } else {
670 fingers = priv->fingers;
671 x1 = priv->x1;
672 x2 = priv->x2;
673 y1 = priv->y1;
674 y2 = priv->y2;
675 }
676
Seth Forshee25bded72011-11-07 19:53:36 -0800677 if (z >= 64)
678 input_report_key(dev, BTN_TOUCH, 1);
679 else
680 input_report_key(dev, BTN_TOUCH, 0);
681
George Pantalos3b7e09f2012-05-10 22:31:59 -0700682 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
683
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700684 input_mt_report_finger_count(dev, fingers);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700685
686 input_report_key(dev, BTN_LEFT, left);
687 input_report_key(dev, BTN_RIGHT, right);
688
Seth Forshee25bded72011-11-07 19:53:36 -0800689 if (z > 0) {
690 input_report_abs(dev, ABS_X, x);
691 input_report_abs(dev, ABS_Y, y);
692 }
693 input_report_abs(dev, ABS_PRESSURE, z);
694
Seth Forshee25bded72011-11-07 19:53:36 -0800695 input_sync(dev);
696}
697
698static void alps_process_packet(struct psmouse *psmouse)
699{
700 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800701
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800702 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -0800703 case ALPS_PROTO_V1:
704 case ALPS_PROTO_V2:
705 alps_process_packet_v1_v2(psmouse);
706 break;
707 case ALPS_PROTO_V3:
708 alps_process_packet_v3(psmouse);
709 break;
710 case ALPS_PROTO_V4:
711 alps_process_packet_v4(psmouse);
712 break;
713 }
714}
715
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800716static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
717 unsigned char packet[],
718 bool report_buttons)
719{
720 struct alps_data *priv = psmouse->private;
721 struct input_dev *dev2 = priv->dev2;
722
723 if (report_buttons)
724 alps_report_buttons(psmouse, dev2, psmouse->dev,
725 packet[0] & 1, packet[0] & 2, packet[0] & 4);
726
727 input_report_rel(dev2, REL_X,
728 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
729 input_report_rel(dev2, REL_Y,
730 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
731
732 input_sync(dev2);
733}
734
735static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct alps_data *priv = psmouse->private;
738
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800739 if (psmouse->pktcnt < 6)
740 return PSMOUSE_GOOD_DATA;
741
742 if (psmouse->pktcnt == 6) {
743 /*
744 * Start a timer to flush the packet if it ends up last
745 * 6-byte packet in the stream. Timer needs to fire
746 * psmouse core times out itself. 20 ms should be enough
747 * to decide if we are getting more data or not.
748 */
749 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
750 return PSMOUSE_GOOD_DATA;
751 }
752
753 del_timer(&priv->timer);
754
755 if (psmouse->packet[6] & 0x80) {
756
757 /*
758 * Highest bit is set - that means we either had
759 * complete ALPS packet and this is start of the
760 * next packet or we got garbage.
761 */
762
763 if (((psmouse->packet[3] |
764 psmouse->packet[4] |
765 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800766 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700767 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700768 "refusing packet %4ph (suspected interleaved ps/2)\n",
769 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800770 return PSMOUSE_BAD_DATA;
771 }
772
773 alps_process_packet(psmouse);
774
775 /* Continue with the next packet */
776 psmouse->packet[0] = psmouse->packet[6];
777 psmouse->pktcnt = 1;
778
779 } else {
780
781 /*
782 * High bit is 0 - that means that we indeed got a PS/2
783 * packet in the middle of ALPS packet.
784 *
785 * There is also possibility that we got 6-byte ALPS
786 * packet followed by 3-byte packet from trackpoint. We
787 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700788 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800789 * normal operation (user would need to press all
790 * buttons on the pad and start moving trackpoint
791 * without touching the pad surface) we assume former.
792 * Even if we are wrong the wost thing that would happen
793 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700794 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800795 */
796
797 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
798 false);
799
800 /*
801 * Continue with the standard ALPS protocol handling,
802 * but make sure we won't process it as an interleaved
803 * packet again, which may happen if all buttons are
804 * pressed. To avoid this let's reset the 4th bit which
805 * is normally 1.
806 */
807 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
808 psmouse->pktcnt = 4;
809 }
810
811 return PSMOUSE_GOOD_DATA;
812}
813
814static void alps_flush_packet(unsigned long data)
815{
816 struct psmouse *psmouse = (struct psmouse *)data;
817
818 serio_pause_rx(psmouse->ps2dev.serio);
819
Seth Forsheeb46615f2011-11-07 19:53:30 -0800820 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800821
822 /*
823 * We did not any more data in reasonable amount of time.
824 * Validate the last 3 bytes and process as a standard
825 * ALPS packet.
826 */
827 if ((psmouse->packet[3] |
828 psmouse->packet[4] |
829 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700830 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700831 "refusing packet %3ph (suspected interleaved ps/2)\n",
832 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800833 } else {
834 alps_process_packet(psmouse);
835 }
836 psmouse->pktcnt = 0;
837 }
838
839 serio_continue_rx(psmouse->ps2dev.serio);
840}
841
842static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
843{
844 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
847 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800848 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
849 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return PSMOUSE_FULL_PACKET;
851 }
852 return PSMOUSE_GOOD_DATA;
853 }
854
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800855 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
856
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800857 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800858 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
859 return alps_handle_interleaved_ps2(psmouse);
860 }
861
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800862 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700863 psmouse_dbg(psmouse,
864 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800865 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Seth Forsheeb46615f2011-11-07 19:53:30 -0800869 /* Bytes 2 - pktsize should have 0 in the highest bit */
870 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800871 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700872 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
873 psmouse->pktcnt - 1,
874 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Seth Forsheeb46615f2011-11-07 19:53:30 -0800878 if (psmouse->pktcnt == psmouse->pktsize) {
David Howells7d12e782006-10-05 14:55:46 +0100879 alps_process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return PSMOUSE_FULL_PACKET;
881 }
882
883 return PSMOUSE_GOOD_DATA;
884}
885
Seth Forshee25bded72011-11-07 19:53:36 -0800886static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
887{
888 struct ps2dev *ps2dev = &psmouse->ps2dev;
889 struct alps_data *priv = psmouse->private;
890 int command;
891 unsigned char *param;
892 unsigned char dummy[4];
893
894 BUG_ON(nibble > 0xf);
895
896 command = priv->nibble_commands[nibble].command;
897 param = (command & 0x0f00) ?
898 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
899
900 if (ps2_command(ps2dev, param, command))
901 return -1;
902
903 return 0;
904}
905
906static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
907{
908 struct ps2dev *ps2dev = &psmouse->ps2dev;
909 struct alps_data *priv = psmouse->private;
910 int i, nibble;
911
912 if (ps2_command(ps2dev, NULL, priv->addr_command))
913 return -1;
914
915 for (i = 12; i >= 0; i -= 4) {
916 nibble = (addr >> i) & 0xf;
917 if (alps_command_mode_send_nibble(psmouse, nibble))
918 return -1;
919 }
920
921 return 0;
922}
923
924static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
925{
926 struct ps2dev *ps2dev = &psmouse->ps2dev;
927 unsigned char param[4];
928
929 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
930 return -1;
931
932 /*
933 * The address being read is returned in the first two bytes
934 * of the result. Check that this address matches the expected
935 * address.
936 */
937 if (addr != ((param[0] << 8) | param[1]))
938 return -1;
939
940 return param[2];
941}
942
943static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
944{
945 if (alps_command_mode_set_addr(psmouse, addr))
946 return -1;
947 return __alps_command_mode_read_reg(psmouse, addr);
948}
949
950static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
951{
952 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
953 return -1;
954 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
955 return -1;
956 return 0;
957}
958
959static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
960 u8 value)
961{
962 if (alps_command_mode_set_addr(psmouse, addr))
963 return -1;
964 return __alps_command_mode_write_reg(psmouse, value);
965}
966
967static int alps_enter_command_mode(struct psmouse *psmouse,
968 unsigned char *resp)
969{
970 unsigned char param[4];
971 struct ps2dev *ps2dev = &psmouse->ps2dev;
972
973 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
974 ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
975 ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
976 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
977 psmouse_err(psmouse, "failed to enter command mode\n");
978 return -1;
979 }
980
981 if (param[0] != 0x88 && param[1] != 0x07) {
982 psmouse_dbg(psmouse,
983 "unknown response while entering command mode: %2.2x %2.2x %2.2x\n",
984 param[0], param[1], param[2]);
985 return -1;
986 }
987
988 if (resp)
989 *resp = param[2];
990 return 0;
991}
992
993static inline int alps_exit_command_mode(struct psmouse *psmouse)
994{
995 struct ps2dev *ps2dev = &psmouse->ps2dev;
996 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
997 return -1;
998 return 0;
999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/*
1002 * For DualPoint devices select the device that should respond to
1003 * subsequent commands. It looks like glidepad is behind stickpointer,
1004 * I'd thought it would be other way around...
1005 */
Seth Forshee25bded72011-11-07 19:53:36 -08001006static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1010
1011 if (ps2_command(ps2dev, NULL, cmd) ||
1012 ps2_command(ps2dev, NULL, cmd) ||
1013 ps2_command(ps2dev, NULL, cmd) ||
1014 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1015 return -1;
1016
1017 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001018 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 return 0;
1021}
1022
Seth Forshee25bded72011-11-07 19:53:36 -08001023static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct ps2dev *ps2dev = &psmouse->ps2dev;
1026
1027 /* Try ALPS magic knock - 4 disable before enable */
1028 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1029 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1030 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1031 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1032 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1033 return -1;
1034
1035 /*
1036 * Switch mouse to poll (remote) mode so motion data will not
1037 * get in our way
1038 */
1039 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1040}
1041
1042static int alps_get_status(struct psmouse *psmouse, char *param)
1043{
1044 struct ps2dev *ps2dev = &psmouse->ps2dev;
1045
1046 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1047 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1048 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1049 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1050 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1051 return -1;
1052
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001053 psmouse_dbg(psmouse, "Status: %2.2x %2.2x %2.2x",
1054 param[0], param[1], param[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 return 0;
1057}
1058
1059/*
1060 * Turn touchpad tapping on or off. The sequences are:
1061 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1062 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1063 * My guess that 0xE9 (GetInfo) is here as a sync point.
1064 * For models that also have stickpointer (DualPoints) its tapping
1065 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1066 * we don't fiddle with it.
1067 */
1068static int alps_tap_mode(struct psmouse *psmouse, int enable)
1069{
1070 struct ps2dev *ps2dev = &psmouse->ps2dev;
1071 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1072 unsigned char tap_arg = enable ? 0x0A : 0x00;
1073 unsigned char param[4];
1074
1075 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1076 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1077 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1078 ps2_command(ps2dev, &tap_arg, cmd))
1079 return -1;
1080
1081 if (alps_get_status(psmouse, param))
1082 return -1;
1083
1084 return 0;
1085}
1086
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001087/*
1088 * alps_poll() - poll the touchpad for current motion packet.
1089 * Used in resync.
1090 */
1091static int alps_poll(struct psmouse *psmouse)
1092{
1093 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001094 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001095 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001096
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, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001099
1100 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1101 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1102
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001103 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001104 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001105
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001106 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001107 return -1;
1108
1109 if ((psmouse->badbyte & 0xc8) == 0x08) {
1110/*
1111 * Poll the track stick ...
1112 */
1113 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1114 return -1;
1115 }
1116
1117 memcpy(psmouse->packet, buf, sizeof(buf));
1118 return 0;
1119}
1120
Seth Forshee25bded72011-11-07 19:53:36 -08001121static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001125 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001126 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001130 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001131 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Seth Forshee25bded72011-11-07 19:53:36 -08001135 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001136 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return -1;
1138 }
1139
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001140 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001141 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001145 /* ALPS needs stream mode, otherwise it won't report any data */
1146 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001147 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001148 return -1;
1149 }
1150
1151 return 0;
1152}
1153
Seth Forshee25bded72011-11-07 19:53:36 -08001154/*
1155 * Enable or disable passthrough mode to the trackstick. Must be in
1156 * command mode when calling this function.
1157 */
1158static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable)
1159{
1160 int reg_val;
1161
1162 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1163 if (reg_val == -1)
1164 return -1;
1165
1166 if (enable)
1167 reg_val |= 0x01;
1168 else
1169 reg_val &= ~0x01;
1170
1171 if (__alps_command_mode_write_reg(psmouse, reg_val))
1172 return -1;
1173
1174 return 0;
1175}
1176
1177/* Must be in command mode when calling this function */
1178static int alps_absolute_mode_v3(struct psmouse *psmouse)
1179{
1180 int reg_val;
1181
1182 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1183 if (reg_val == -1)
1184 return -1;
1185
1186 reg_val |= 0x06;
1187 if (__alps_command_mode_write_reg(psmouse, reg_val))
1188 return -1;
1189
1190 return 0;
1191}
1192
1193static int alps_hw_init_v3(struct psmouse *psmouse)
1194{
1195 struct alps_data *priv = psmouse->private;
1196 struct ps2dev *ps2dev = &psmouse->ps2dev;
1197 int reg_val;
1198 unsigned char param[4];
1199
1200 priv->nibble_commands = alps_v3_nibble_commands;
1201 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1202
1203 if (alps_enter_command_mode(psmouse, NULL))
1204 goto error;
1205
1206 /* Check for trackstick */
1207 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1208 if (reg_val == -1)
1209 goto error;
1210 if (reg_val & 0x80) {
1211 if (alps_passthrough_mode_v3(psmouse, true))
1212 goto error;
1213 if (alps_exit_command_mode(psmouse))
1214 goto error;
1215
1216 /*
1217 * E7 report for the trackstick
1218 *
1219 * There have been reports of failures to seem to trace back
1220 * to the above trackstick check failing. When these occur
1221 * this E7 report fails, so when that happens we continue
1222 * with the assumption that there isn't a trackstick after
1223 * all.
1224 */
1225 param[0] = 0x64;
1226 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1227 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1228 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1229 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1230 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1231 } else {
1232 psmouse_dbg(psmouse,
1233 "trackstick E7 report: %2.2x %2.2x %2.2x\n",
1234 param[0], param[1], param[2]);
1235
1236 /*
1237 * Not sure what this does, but it is absolutely
1238 * essential. Without it, the touchpad does not
1239 * work at all and the trackstick just emits normal
1240 * PS/2 packets.
1241 */
1242 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1243 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1244 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1245 alps_command_mode_send_nibble(psmouse, 0x9) ||
1246 alps_command_mode_send_nibble(psmouse, 0x4)) {
1247 psmouse_err(psmouse,
1248 "Error sending magic E6 sequence\n");
1249 goto error_passthrough;
1250 }
1251 }
1252
1253 if (alps_enter_command_mode(psmouse, NULL))
1254 goto error_passthrough;
1255 if (alps_passthrough_mode_v3(psmouse, false))
1256 goto error;
1257 }
1258
1259 if (alps_absolute_mode_v3(psmouse)) {
1260 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1261 goto error;
1262 }
1263
1264 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1265 if (reg_val == -1)
1266 goto error;
1267 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1268 goto error;
1269
1270 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1271 if (reg_val == -1)
1272 goto error;
1273 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1274 goto error;
1275
1276 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1277 goto error;
1278 if (__alps_command_mode_write_reg(psmouse, 0x04))
1279 goto error;
1280
1281 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1282 goto error;
1283 if (__alps_command_mode_write_reg(psmouse, 0x03))
1284 goto error;
1285
1286 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1287 goto error;
1288 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1289 goto error;
1290
1291 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1292 goto error;
1293 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1294 goto error;
1295
1296 /*
1297 * This ensures the trackstick packets are in the format
1298 * supported by this driver. If bit 1 isn't set the packet
1299 * format is different.
1300 */
1301 if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82))
1302 goto error;
1303
1304 alps_exit_command_mode(psmouse);
1305
1306 /* Set rate and enable data reporting */
1307 param[0] = 0x64;
1308 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1309 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1310 psmouse_err(psmouse, "Failed to enable data reporting\n");
1311 return -1;
1312 }
1313
1314 return 0;
1315
1316error_passthrough:
1317 /* Something failed while in passthrough mode, so try to get out */
1318 if (!alps_enter_command_mode(psmouse, NULL))
1319 alps_passthrough_mode_v3(psmouse, false);
1320error:
1321 /*
1322 * Leaving the touchpad in command mode will essentially render
1323 * it unusable until the machine reboots, so exit it here just
1324 * to be safe
1325 */
1326 alps_exit_command_mode(psmouse);
1327 return -1;
1328}
1329
1330/* Must be in command mode when calling this function */
1331static int alps_absolute_mode_v4(struct psmouse *psmouse)
1332{
1333 int reg_val;
1334
1335 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1336 if (reg_val == -1)
1337 return -1;
1338
1339 reg_val |= 0x02;
1340 if (__alps_command_mode_write_reg(psmouse, reg_val))
1341 return -1;
1342
1343 return 0;
1344}
1345
1346static int alps_hw_init_v4(struct psmouse *psmouse)
1347{
1348 struct alps_data *priv = psmouse->private;
1349 struct ps2dev *ps2dev = &psmouse->ps2dev;
1350 unsigned char param[4];
1351
1352 priv->nibble_commands = alps_v4_nibble_commands;
1353 priv->addr_command = PSMOUSE_CMD_DISABLE;
1354
1355 if (alps_enter_command_mode(psmouse, NULL))
1356 goto error;
1357
1358 if (alps_absolute_mode_v4(psmouse)) {
1359 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1360 goto error;
1361 }
1362
1363 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1364 goto error;
1365
1366 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1367 goto error;
1368
1369 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1370 goto error;
1371
1372 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1373 goto error;
1374
1375 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1376 goto error;
1377
1378 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1379 goto error;
1380
1381 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1382 goto error;
1383
1384 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1385 goto error;
1386
1387 alps_exit_command_mode(psmouse);
1388
1389 /*
1390 * This sequence changes the output from a 9-byte to an
1391 * 8-byte format. All the same data seems to be present,
1392 * just in a more compact format.
1393 */
1394 param[0] = 0xc8;
1395 param[1] = 0x64;
1396 param[2] = 0x50;
1397 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1398 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1399 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1400 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1401 return -1;
1402
1403 /* Set rate and enable data reporting */
1404 param[0] = 0x64;
1405 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1406 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1407 psmouse_err(psmouse, "Failed to enable data reporting\n");
1408 return -1;
1409 }
1410
1411 return 0;
1412
1413error:
1414 /*
1415 * Leaving the touchpad in command mode will essentially render
1416 * it unusable until the machine reboots, so exit it here just
1417 * to be safe
1418 */
1419 alps_exit_command_mode(psmouse);
1420 return -1;
1421}
1422
1423static int alps_hw_init(struct psmouse *psmouse)
1424{
1425 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -08001426 int ret = -1;
1427
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001428 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001429 case ALPS_PROTO_V1:
1430 case ALPS_PROTO_V2:
1431 ret = alps_hw_init_v1_v2(psmouse);
1432 break;
1433 case ALPS_PROTO_V3:
1434 ret = alps_hw_init_v3(psmouse);
1435 break;
1436 case ALPS_PROTO_V4:
1437 ret = alps_hw_init_v4(psmouse);
1438 break;
1439 }
1440
1441 return ret;
1442}
1443
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001444static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
1445{
1446 struct ps2dev *ps2dev = &psmouse->ps2dev;
1447 static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
1448 unsigned char param[4];
1449 const struct alps_model_info *model = NULL;
1450 int i;
1451
1452 /*
1453 * First try "E6 report".
1454 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1455 * The bits 0-2 of the first byte will be 1s if some buttons are
1456 * pressed.
1457 */
1458 param[0] = 0;
1459 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
1460 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1461 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1462 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
1463 return NULL;
1464
1465 param[0] = param[1] = param[2] = 0xff;
1466 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1467 return NULL;
1468
1469 psmouse_dbg(psmouse, "E6 report: %2.2x %2.2x %2.2x",
1470 param[0], param[1], param[2]);
1471
1472 if ((param[0] & 0xf8) != 0 || param[1] != 0 ||
1473 (param[2] != 10 && param[2] != 100))
1474 return NULL;
1475
1476 /*
1477 * Now try "E7 report". Allowed responses are in
1478 * alps_model_data[].signature
1479 */
1480 param[0] = 0;
1481 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
1482 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1483 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1484 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
1485 return NULL;
1486
1487 param[0] = param[1] = param[2] = 0xff;
1488 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1489 return NULL;
1490
1491 psmouse_dbg(psmouse, "E7 report: %2.2x %2.2x %2.2x",
1492 param[0], param[1], param[2]);
1493
1494 if (version) {
1495 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
1496 /* empty */;
1497 *version = (param[0] << 8) | (param[1] << 4) | i;
1498 }
1499
1500 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1501 if (!memcmp(param, alps_model_data[i].signature,
1502 sizeof(alps_model_data[i].signature))) {
1503 model = alps_model_data + i;
1504 break;
1505 }
1506 }
1507
1508 if (model && model->proto_version > ALPS_PROTO_V2) {
1509 /*
1510 * Need to check command mode response to identify
1511 * model
1512 */
1513 model = NULL;
1514 if (alps_enter_command_mode(psmouse, param)) {
1515 psmouse_warn(psmouse,
1516 "touchpad failed to enter command mode\n");
1517 } else {
1518 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1519 if (alps_model_data[i].proto_version > ALPS_PROTO_V2 &&
1520 alps_model_data[i].command_mode_resp == param[0]) {
1521 model = alps_model_data + i;
1522 break;
1523 }
1524 }
1525 alps_exit_command_mode(psmouse);
1526
1527 if (!model)
1528 psmouse_dbg(psmouse,
1529 "Unknown command mode response %2.2x\n",
1530 param[0]);
1531 }
1532 }
1533
1534 return model;
1535}
1536
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001537static int alps_reconnect(struct psmouse *psmouse)
1538{
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001539 const struct alps_model_info *model;
1540
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001541 psmouse_reset(psmouse);
1542
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001543 model = alps_get_model(psmouse, NULL);
1544 if (!model)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001545 return -1;
1546
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001547 return alps_hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550static void alps_disconnect(struct psmouse *psmouse)
1551{
1552 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001555 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001556 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 kfree(priv);
1558}
1559
1560int alps_init(struct psmouse *psmouse)
1561{
1562 struct alps_data *priv;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001563 const struct alps_model_info *model;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001564 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int version;
1566
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001567 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001568 dev2 = input_allocate_device();
1569 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001571
1572 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001573 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1574
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001575 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Seth Forshee25bded72011-11-07 19:53:36 -08001577 psmouse_reset(psmouse);
1578
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001579 model = alps_get_model(psmouse, &version);
1580 if (!model)
1581 goto init_fail;
1582
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001583 priv->proto_version = model->proto_version;
1584 priv->byte0 = model->byte0;
1585 priv->mask0 = model->mask0;
1586 priv->flags = model->flags;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001587
1588 if (alps_hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 goto init_fail;
1590
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08001591 /*
1592 * Undo part of setup done for us by psmouse core since touchpad
1593 * is not a relative device.
1594 */
1595 __clear_bit(EV_REL, dev1->evbit);
1596 __clear_bit(REL_X, dev1->relbit);
1597 __clear_bit(REL_Y, dev1->relbit);
1598
1599 /*
1600 * Now set up our capabilities.
1601 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001602 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1603 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1604 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001605 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1606 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001608 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08001609
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001610 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001611 case ALPS_PROTO_V1:
1612 case ALPS_PROTO_V2:
1613 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1614 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1615 break;
1616 case ALPS_PROTO_V3:
George Pantalos3b7e09f2012-05-10 22:31:59 -07001617 case ALPS_PROTO_V4:
Seth Forshee01ce6612011-11-07 19:54:13 -08001618 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Henrik Rydbergb4adbbe2012-08-11 22:07:55 +02001619 input_mt_init_slots(dev1, 2, 0);
Seth Forshee01ce6612011-11-07 19:54:13 -08001620 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, ALPS_V3_X_MAX, 0, 0);
1621 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1622
1623 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1624 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1625 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
George Pantalos3b7e09f2012-05-10 22:31:59 -07001626
Seth Forshee01ce6612011-11-07 19:54:13 -08001627 input_set_abs_params(dev1, ABS_X, 0, ALPS_V3_X_MAX, 0, 0);
1628 input_set_abs_params(dev1, ABS_Y, 0, ALPS_V3_Y_MAX, 0, 0);
Seth Forshee25bded72011-11-07 19:53:36 -08001629 break;
1630 }
1631
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001632 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001634 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001635 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1636 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001639 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001640 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1641 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001644 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001645 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1646 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1647 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1648 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1649 } else {
1650 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1651 }
1652
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001653 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001654 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001655 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1656 "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001657 dev2->id.bustype = BUS_I8042;
1658 dev2->id.vendor = 0x0002;
1659 dev2->id.product = PSMOUSE_ALPS;
1660 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04001661 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001662
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001663 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001664 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1665 dev2->keybit[BIT_WORD(BTN_LEFT)] =
1666 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001668 if (input_register_device(priv->dev2))
1669 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001672 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 psmouse->disconnect = alps_disconnect;
1674 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001675 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001677 /* We are having trouble resyncing ALPS touchpads so disable it for now */
1678 psmouse->resync_time = 0;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return 0;
1681
1682init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001683 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001684 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001686 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return -1;
1688}
1689
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001690int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 int version;
Helge Dellere38de672006-09-10 21:54:39 -04001693 const struct alps_model_info *model;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001695 model = alps_get_model(psmouse, &version);
1696 if (!model)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -1;
1698
1699 if (set_properties) {
1700 psmouse->vendor = "ALPS";
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001701 psmouse->name = model->flags & ALPS_DUALPOINT ?
1702 "DualPoint TouchPad" : "GlidePoint";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 psmouse->model = version;
1704 }
1705 return 0;
1706}
1707