blob: bfc193806fc1a3e83f64cb78cd60742680ca0060 [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 */
Seth Forshee25bded72011-11-07 19:53:36 -0800112 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800115static void alps_set_abs_params_st(struct alps_data *priv,
116 struct input_dev *dev1);
117static void alps_set_abs_params_mt(struct alps_data *priv,
118 struct input_dev *dev1);
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * XXX - this entry is suspicious. First byte has zero lower nibble,
122 * which is what a normal mouse would report. Also, the value 0x0e
123 * isn't valid per PS/2 spec.
124 */
125
Seth Forsheed4b347b2011-11-07 19:53:15 -0800126/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800128static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800129 unsigned char data)
130{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800131 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800132}
133
134static void alps_report_buttons(struct psmouse *psmouse,
135 struct input_dev *dev1, struct input_dev *dev2,
136 int left, int right, int middle)
137{
Martin Buckc91ed052010-03-13 22:23:58 -0800138 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800139
Martin Buckc91ed052010-03-13 22:23:58 -0800140 /*
141 * If shared button has already been reported on the
142 * other device (dev2) then this event should be also
143 * sent through that device.
144 */
145 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
146 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800147
Martin Buckc91ed052010-03-13 22:23:58 -0800148 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
149 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800150
Martin Buckc91ed052010-03-13 22:23:58 -0800151 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
152 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800153
Martin Buckc91ed052010-03-13 22:23:58 -0800154 /*
155 * Sync the _other_ device now, we'll do the first
156 * device later once we report the rest of the events.
157 */
158 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800159}
160
Seth Forshee25bded72011-11-07 19:53:36 -0800161static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct alps_data *priv = psmouse->private;
164 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500165 struct input_dev *dev = psmouse->dev;
166 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500168 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800170 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400171 left = packet[2] & 0x10;
172 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 middle = 0;
174 x = packet[1] | ((packet[0] & 0x07) << 7);
175 y = packet[4] | ((packet[3] & 0x07) << 7);
176 z = packet[5];
177 } else {
178 left = packet[3] & 1;
179 right = packet[3] & 2;
180 middle = packet[3] & 4;
181 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
182 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
183 z = packet[5];
184 }
185
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800186 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400187 back = packet[0] & 0x10;
188 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500189 }
190
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800191 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500192 back = packet[3] & 4;
193 forward = packet[2] & 4;
194 if ((middle = forward && back))
195 forward = back = 0;
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ges = packet[2] & 1;
199 fin = packet[2] & 2;
200
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800201 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
203 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700204
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800205 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 input_sync(dev2);
208 return;
209 }
210
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800211 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800214 if (ges && !fin)
215 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /*
218 * A "tap and drag" operation is reported by the hardware as a transition
219 * from (!fin && ges) to (fin && ges). This should be translated to the
220 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
221 */
222 if (ges && fin && !priv->prev_fin) {
223 input_report_abs(dev, ABS_X, x);
224 input_report_abs(dev, ABS_Y, y);
225 input_report_abs(dev, ABS_PRESSURE, 0);
226 input_report_key(dev, BTN_TOOL_FINGER, 0);
227 input_sync(dev);
228 }
229 priv->prev_fin = fin;
230
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800231 if (z > 30)
232 input_report_key(dev, BTN_TOUCH, 1);
233 if (z < 25)
234 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (z > 0) {
237 input_report_abs(dev, ABS_X, x);
238 input_report_abs(dev, ABS_Y, y);
239 }
240
241 input_report_abs(dev, ABS_PRESSURE, z);
242 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
243
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800244 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500245 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800247 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500248 input_report_key(dev, BTN_FORWARD, forward);
249 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800252 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800253 input_report_key(dev, BTN_0, packet[2] & 4);
254 input_report_key(dev, BTN_1, packet[0] & 0x10);
255 input_report_key(dev, BTN_2, packet[3] & 4);
256 input_report_key(dev, BTN_3, packet[0] & 0x20);
257 }
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 input_sync(dev);
260}
261
Seth Forshee01ce6612011-11-07 19:54:13 -0800262/*
263 * Process bitmap data from v3 and v4 protocols. Returns the number of
264 * fingers detected. A return value of 0 means at least one of the
265 * bitmaps was empty.
266 *
267 * The bitmaps don't have enough data to track fingers, so this function
268 * only generates points representing a bounding box of all contacts.
269 * These points are returned in x1, y1, x2, and y2 when the return value
270 * is greater than 0.
271 */
272static int alps_process_bitmap(unsigned int x_map, unsigned int y_map,
273 int *x1, int *y1, int *x2, int *y2)
274{
275 struct alps_bitmap_point {
276 int start_bit;
277 int num_bits;
278 };
279
280 int fingers_x = 0, fingers_y = 0, fingers;
281 int i, bit, prev_bit;
282 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
283 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
284 struct alps_bitmap_point *point;
285
286 if (!x_map || !y_map)
287 return 0;
288
289 *x1 = *y1 = *x2 = *y2 = 0;
290
291 prev_bit = 0;
292 point = &x_low;
293 for (i = 0; x_map != 0; i++, x_map >>= 1) {
294 bit = x_map & 1;
295 if (bit) {
296 if (!prev_bit) {
297 point->start_bit = i;
298 fingers_x++;
299 }
300 point->num_bits++;
301 } else {
302 if (prev_bit)
303 point = &x_high;
304 else
305 point->num_bits = 0;
306 }
307 prev_bit = bit;
308 }
309
310 /*
311 * y bitmap is reversed for what we need (lower positions are in
312 * higher bits), so we process from the top end.
313 */
314 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - ALPS_BITMAP_Y_BITS);
315 prev_bit = 0;
316 point = &y_low;
317 for (i = 0; y_map != 0; i++, y_map <<= 1) {
318 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
319 if (bit) {
320 if (!prev_bit) {
321 point->start_bit = i;
322 fingers_y++;
323 }
324 point->num_bits++;
325 } else {
326 if (prev_bit)
327 point = &y_high;
328 else
329 point->num_bits = 0;
330 }
331 prev_bit = bit;
332 }
333
334 /*
335 * Fingers can overlap, so we use the maximum count of fingers
336 * on either axis as the finger count.
337 */
338 fingers = max(fingers_x, fingers_y);
339
340 /*
341 * If total fingers is > 1 but either axis reports only a single
342 * contact, we have overlapping or adjacent fingers. For the
343 * purposes of creating a bounding box, divide the single contact
344 * (roughly) equally between the two points.
345 */
346 if (fingers > 1) {
347 if (fingers_x == 1) {
348 i = x_low.num_bits / 2;
349 x_low.num_bits = x_low.num_bits - i;
350 x_high.start_bit = x_low.start_bit + i;
351 x_high.num_bits = max(i, 1);
352 } else if (fingers_y == 1) {
353 i = y_low.num_bits / 2;
354 y_low.num_bits = y_low.num_bits - i;
355 y_high.start_bit = y_low.start_bit + i;
356 y_high.num_bits = max(i, 1);
357 }
358 }
359
360 *x1 = (ALPS_V3_X_MAX * (2 * x_low.start_bit + x_low.num_bits - 1)) /
361 (2 * (ALPS_BITMAP_X_BITS - 1));
362 *y1 = (ALPS_V3_Y_MAX * (2 * y_low.start_bit + y_low.num_bits - 1)) /
363 (2 * (ALPS_BITMAP_Y_BITS - 1));
364
365 if (fingers > 1) {
366 *x2 = (ALPS_V3_X_MAX * (2 * x_high.start_bit + x_high.num_bits - 1)) /
367 (2 * (ALPS_BITMAP_X_BITS - 1));
368 *y2 = (ALPS_V3_Y_MAX * (2 * y_high.start_bit + y_high.num_bits - 1)) /
369 (2 * (ALPS_BITMAP_Y_BITS - 1));
370 }
371
372 return fingers;
373}
374
375static void alps_set_slot(struct input_dev *dev, int slot, bool active,
376 int x, int y)
377{
378 input_mt_slot(dev, slot);
379 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
380 if (active) {
381 input_report_abs(dev, ABS_MT_POSITION_X, x);
382 input_report_abs(dev, ABS_MT_POSITION_Y, y);
383 }
384}
385
386static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
387 int x1, int y1, int x2, int y2)
388{
389 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
390 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
391}
392
Seth Forshee25bded72011-11-07 19:53:36 -0800393static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
394{
395 struct alps_data *priv = psmouse->private;
396 unsigned char *packet = psmouse->packet;
397 struct input_dev *dev = priv->dev2;
398 int x, y, z, left, right, middle;
399
400 /* Sanity check packet */
401 if (!(packet[0] & 0x40)) {
402 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
403 return;
404 }
405
406 /*
407 * There's a special packet that seems to indicate the end
408 * of a stream of trackstick data. Filter these out.
409 */
410 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
411 return;
412
413 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
414 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
415 z = (packet[4] & 0x7c) >> 2;
416
417 /*
418 * The x and y values tend to be quite large, and when used
419 * alone the trackstick is difficult to use. Scale them down
420 * to compensate.
421 */
422 x /= 8;
423 y /= 8;
424
425 input_report_rel(dev, REL_X, x);
426 input_report_rel(dev, REL_Y, -y);
427
428 /*
429 * Most ALPS models report the trackstick buttons in the touchpad
430 * packets, but a few report them here. No reliable way has been
431 * found to differentiate between the models upfront, so we enable
432 * the quirk in response to seeing a button press in the trackstick
433 * packet.
434 */
435 left = packet[3] & 0x01;
436 right = packet[3] & 0x02;
437 middle = packet[3] & 0x04;
438
439 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
440 (left || right || middle))
441 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
442
443 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
444 input_report_key(dev, BTN_LEFT, left);
445 input_report_key(dev, BTN_RIGHT, right);
446 input_report_key(dev, BTN_MIDDLE, middle);
447 }
448
449 input_sync(dev);
450 return;
451}
452
453static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
454{
455 struct alps_data *priv = psmouse->private;
456 unsigned char *packet = psmouse->packet;
457 struct input_dev *dev = psmouse->dev;
458 struct input_dev *dev2 = priv->dev2;
459 int x, y, z;
460 int left, right, middle;
Seth Forshee01ce6612011-11-07 19:54:13 -0800461 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
462 int fingers = 0, bmap_fingers;
463 unsigned int x_bitmap, y_bitmap;
Seth Forshee25bded72011-11-07 19:53:36 -0800464
465 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800466 * There's no single feature of touchpad position and bitmap packets
467 * that can be used to distinguish between them. We rely on the fact
468 * that a bitmap packet should always follow a position packet with
469 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800470 */
471 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800472 /*
473 * Sometimes a position packet will indicate a multi-packet
474 * sequence, but then what follows is another position
475 * packet. Check for this, and when it happens process the
476 * position packet as usual.
477 */
478 if (packet[0] & 0x40) {
Seth Forshee01ce6612011-11-07 19:54:13 -0800479 fingers = (packet[5] & 0x3) + 1;
480 x_bitmap = ((packet[4] & 0x7e) << 8) |
481 ((packet[1] & 0x7f) << 2) |
482 ((packet[0] & 0x30) >> 4);
483 y_bitmap = ((packet[3] & 0x70) << 4) |
484 ((packet[2] & 0x7f) << 1) |
485 (packet[4] & 0x01);
486
487 bmap_fingers = alps_process_bitmap(x_bitmap, y_bitmap,
488 &x1, &y1, &x2, &y2);
489
Seth Forshee25bded72011-11-07 19:53:36 -0800490 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800491 * We shouldn't report more than one finger if
492 * we don't have two coordinates.
Seth Forshee25bded72011-11-07 19:53:36 -0800493 */
Seth Forshee01ce6612011-11-07 19:54:13 -0800494 if (fingers > 1 && bmap_fingers < 2)
495 fingers = bmap_fingers;
496
497 /* Now process position packet */
498 packet = priv->multi_data;
499 } else {
500 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800501 }
502 }
503
Seth Forshee01ce6612011-11-07 19:54:13 -0800504 /*
505 * Bit 6 of byte 0 is not usually set in position packets. The only
506 * times it seems to be set is in situations where the data is
507 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
508 * this combined with the fact that this bit is useful for filtering
509 * out misidentified bitmap packets, we reject anything with this
510 * bit set.
511 */
512 if (packet[0] & 0x40)
513 return;
514
515 if (!priv->multi_packet && (packet[4] & 0x40)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800516 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800517 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
518 return;
519 }
520
521 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800522
523 left = packet[3] & 0x01;
524 right = packet[3] & 0x02;
525 middle = packet[3] & 0x04;
526
527 x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) |
528 ((packet[0] & 0x30) >> 4);
529 y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f);
530 z = packet[5] & 0x7f;
531
532 /*
533 * Sometimes the hardware sends a single packet with z = 0
534 * in the middle of a stream. Real releases generate packets
535 * with x, y, and z all zero, so these seem to be flukes.
536 * Ignore them.
537 */
538 if (x && y && !z)
539 return;
540
Seth Forshee01ce6612011-11-07 19:54:13 -0800541 /*
542 * If we don't have MT data or the bitmaps were empty, we have
543 * to rely on ST data.
544 */
545 if (!fingers) {
546 x1 = x;
547 y1 = y;
548 fingers = z > 0 ? 1 : 0;
549 }
550
Seth Forshee25bded72011-11-07 19:53:36 -0800551 if (z >= 64)
552 input_report_key(dev, BTN_TOUCH, 1);
553 else
554 input_report_key(dev, BTN_TOUCH, 0);
555
Seth Forshee01ce6612011-11-07 19:54:13 -0800556 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
557
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700558 input_mt_report_finger_count(dev, fingers);
Seth Forshee01ce6612011-11-07 19:54:13 -0800559
560 input_report_key(dev, BTN_LEFT, left);
561 input_report_key(dev, BTN_RIGHT, right);
562 input_report_key(dev, BTN_MIDDLE, middle);
563
Seth Forshee25bded72011-11-07 19:53:36 -0800564 if (z > 0) {
565 input_report_abs(dev, ABS_X, x);
566 input_report_abs(dev, ABS_Y, y);
567 }
568 input_report_abs(dev, ABS_PRESSURE, z);
569
Seth Forshee25bded72011-11-07 19:53:36 -0800570 input_sync(dev);
571
572 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
573 left = packet[3] & 0x10;
574 right = packet[3] & 0x20;
575 middle = packet[3] & 0x40;
576
577 input_report_key(dev2, BTN_LEFT, left);
578 input_report_key(dev2, BTN_RIGHT, right);
579 input_report_key(dev2, BTN_MIDDLE, middle);
580 input_sync(dev2);
581 }
582}
583
584static void alps_process_packet_v3(struct psmouse *psmouse)
585{
586 unsigned char *packet = psmouse->packet;
587
588 /*
589 * v3 protocol packets come in three types, two representing
590 * touchpad data and one representing trackstick data.
591 * Trackstick packets seem to be distinguished by always
592 * having 0x3f in the last byte. This value has never been
593 * observed in the last byte of either of the other types
594 * of packets.
595 */
596 if (packet[5] == 0x3f) {
597 alps_process_trackstick_packet_v3(psmouse);
598 return;
599 }
600
601 alps_process_touchpad_packet_v3(psmouse);
602}
603
604static void alps_process_packet_v4(struct psmouse *psmouse)
605{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700606 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800607 unsigned char *packet = psmouse->packet;
608 struct input_dev *dev = psmouse->dev;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700609 int offset;
Seth Forshee25bded72011-11-07 19:53:36 -0800610 int x, y, z;
611 int left, right;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700612 int x1, y1, x2, y2;
613 int fingers = 0;
614 unsigned int x_bitmap, y_bitmap;
615
616 /*
617 * v4 has a 6-byte encoding for bitmap data, but this data is
618 * broken up between 3 normal packets. Use priv->multi_packet to
619 * track our position in the bitmap packet.
620 */
621 if (packet[6] & 0x40) {
622 /* sync, reset position */
623 priv->multi_packet = 0;
624 }
625
626 if (WARN_ON_ONCE(priv->multi_packet > 2))
627 return;
628
629 offset = 2 * priv->multi_packet;
630 priv->multi_data[offset] = packet[6];
631 priv->multi_data[offset + 1] = packet[7];
632
633 if (++priv->multi_packet > 2) {
634 priv->multi_packet = 0;
635
636 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
637 ((priv->multi_data[3] & 0x60) << 3) |
638 ((priv->multi_data[0] & 0x3f) << 2) |
639 ((priv->multi_data[1] & 0x60) >> 5);
640 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
641 ((priv->multi_data[3] & 0x1f) << 5) |
642 (priv->multi_data[1] & 0x1f);
643
644 fingers = alps_process_bitmap(x_bitmap, y_bitmap,
645 &x1, &y1, &x2, &y2);
646
647 /* Store MT data.*/
648 priv->fingers = fingers;
649 priv->x1 = x1;
650 priv->x2 = x2;
651 priv->y1 = y1;
652 priv->y2 = y2;
653 }
Seth Forshee25bded72011-11-07 19:53:36 -0800654
655 left = packet[4] & 0x01;
656 right = packet[4] & 0x02;
657
658 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
659 ((packet[0] & 0x30) >> 4);
660 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
661 z = packet[5] & 0x7f;
662
George Pantalos3b7e09f2012-05-10 22:31:59 -0700663 /*
664 * If there were no contacts in the bitmap, use ST
665 * points in MT reports.
666 * If there were two contacts or more, report MT data.
667 */
668 if (priv->fingers < 2) {
669 x1 = x;
670 y1 = y;
671 fingers = z > 0 ? 1 : 0;
672 } else {
673 fingers = priv->fingers;
674 x1 = priv->x1;
675 x2 = priv->x2;
676 y1 = priv->y1;
677 y2 = priv->y2;
678 }
679
Seth Forshee25bded72011-11-07 19:53:36 -0800680 if (z >= 64)
681 input_report_key(dev, BTN_TOUCH, 1);
682 else
683 input_report_key(dev, BTN_TOUCH, 0);
684
George Pantalos3b7e09f2012-05-10 22:31:59 -0700685 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
686
Dmitry Torokhov616575c2012-05-10 22:31:59 -0700687 input_mt_report_finger_count(dev, fingers);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700688
689 input_report_key(dev, BTN_LEFT, left);
690 input_report_key(dev, BTN_RIGHT, right);
691
Seth Forshee25bded72011-11-07 19:53:36 -0800692 if (z > 0) {
693 input_report_abs(dev, ABS_X, x);
694 input_report_abs(dev, ABS_Y, y);
695 }
696 input_report_abs(dev, ABS_PRESSURE, z);
697
Seth Forshee25bded72011-11-07 19:53:36 -0800698 input_sync(dev);
699}
700
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800701static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
702 unsigned char packet[],
703 bool report_buttons)
704{
705 struct alps_data *priv = psmouse->private;
706 struct input_dev *dev2 = priv->dev2;
707
708 if (report_buttons)
709 alps_report_buttons(psmouse, dev2, psmouse->dev,
710 packet[0] & 1, packet[0] & 2, packet[0] & 4);
711
712 input_report_rel(dev2, REL_X,
713 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
714 input_report_rel(dev2, REL_Y,
715 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
716
717 input_sync(dev2);
718}
719
720static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct alps_data *priv = psmouse->private;
723
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800724 if (psmouse->pktcnt < 6)
725 return PSMOUSE_GOOD_DATA;
726
727 if (psmouse->pktcnt == 6) {
728 /*
729 * Start a timer to flush the packet if it ends up last
730 * 6-byte packet in the stream. Timer needs to fire
731 * psmouse core times out itself. 20 ms should be enough
732 * to decide if we are getting more data or not.
733 */
734 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
735 return PSMOUSE_GOOD_DATA;
736 }
737
738 del_timer(&priv->timer);
739
740 if (psmouse->packet[6] & 0x80) {
741
742 /*
743 * Highest bit is set - that means we either had
744 * complete ALPS packet and this is start of the
745 * next packet or we got garbage.
746 */
747
748 if (((psmouse->packet[3] |
749 psmouse->packet[4] |
750 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800751 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700752 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700753 "refusing packet %4ph (suspected interleaved ps/2)\n",
754 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800755 return PSMOUSE_BAD_DATA;
756 }
757
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800758 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800759
760 /* Continue with the next packet */
761 psmouse->packet[0] = psmouse->packet[6];
762 psmouse->pktcnt = 1;
763
764 } else {
765
766 /*
767 * High bit is 0 - that means that we indeed got a PS/2
768 * packet in the middle of ALPS packet.
769 *
770 * There is also possibility that we got 6-byte ALPS
771 * packet followed by 3-byte packet from trackpoint. We
772 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700773 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800774 * normal operation (user would need to press all
775 * buttons on the pad and start moving trackpoint
776 * without touching the pad surface) we assume former.
777 * Even if we are wrong the wost thing that would happen
778 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700779 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800780 */
781
782 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
783 false);
784
785 /*
786 * Continue with the standard ALPS protocol handling,
787 * but make sure we won't process it as an interleaved
788 * packet again, which may happen if all buttons are
789 * pressed. To avoid this let's reset the 4th bit which
790 * is normally 1.
791 */
792 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
793 psmouse->pktcnt = 4;
794 }
795
796 return PSMOUSE_GOOD_DATA;
797}
798
799static void alps_flush_packet(unsigned long data)
800{
801 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800802 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800803
804 serio_pause_rx(psmouse->ps2dev.serio);
805
Seth Forsheeb46615f2011-11-07 19:53:30 -0800806 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800807
808 /*
809 * We did not any more data in reasonable amount of time.
810 * Validate the last 3 bytes and process as a standard
811 * ALPS packet.
812 */
813 if ((psmouse->packet[3] |
814 psmouse->packet[4] |
815 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700816 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -0700817 "refusing packet %3ph (suspected interleaved ps/2)\n",
818 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800819 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800820 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800821 }
822 psmouse->pktcnt = 0;
823 }
824
825 serio_continue_rx(psmouse->ps2dev.serio);
826}
827
828static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
829{
830 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
833 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800834 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
835 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return PSMOUSE_FULL_PACKET;
837 }
838 return PSMOUSE_GOOD_DATA;
839 }
840
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800841 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
842
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800843 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800844 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
845 return alps_handle_interleaved_ps2(psmouse);
846 }
847
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800848 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700849 psmouse_dbg(psmouse,
850 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800851 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Seth Forsheeb46615f2011-11-07 19:53:30 -0800855 /* Bytes 2 - pktsize should have 0 in the highest bit */
856 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800857 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700858 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
859 psmouse->pktcnt - 1,
860 psmouse->packet[psmouse->pktcnt - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Seth Forsheeb46615f2011-11-07 19:53:30 -0800864 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800865 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return PSMOUSE_FULL_PACKET;
867 }
868
869 return PSMOUSE_GOOD_DATA;
870}
871
Seth Forshee25bded72011-11-07 19:53:36 -0800872static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
873{
874 struct ps2dev *ps2dev = &psmouse->ps2dev;
875 struct alps_data *priv = psmouse->private;
876 int command;
877 unsigned char *param;
878 unsigned char dummy[4];
879
880 BUG_ON(nibble > 0xf);
881
882 command = priv->nibble_commands[nibble].command;
883 param = (command & 0x0f00) ?
884 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
885
886 if (ps2_command(ps2dev, param, command))
887 return -1;
888
889 return 0;
890}
891
892static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
893{
894 struct ps2dev *ps2dev = &psmouse->ps2dev;
895 struct alps_data *priv = psmouse->private;
896 int i, nibble;
897
898 if (ps2_command(ps2dev, NULL, priv->addr_command))
899 return -1;
900
901 for (i = 12; i >= 0; i -= 4) {
902 nibble = (addr >> i) & 0xf;
903 if (alps_command_mode_send_nibble(psmouse, nibble))
904 return -1;
905 }
906
907 return 0;
908}
909
910static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
911{
912 struct ps2dev *ps2dev = &psmouse->ps2dev;
913 unsigned char param[4];
914
915 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
916 return -1;
917
918 /*
919 * The address being read is returned in the first two bytes
920 * of the result. Check that this address matches the expected
921 * address.
922 */
923 if (addr != ((param[0] << 8) | param[1]))
924 return -1;
925
926 return param[2];
927}
928
929static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
930{
931 if (alps_command_mode_set_addr(psmouse, addr))
932 return -1;
933 return __alps_command_mode_read_reg(psmouse, addr);
934}
935
936static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
937{
938 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
939 return -1;
940 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
941 return -1;
942 return 0;
943}
944
945static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
946 u8 value)
947{
948 if (alps_command_mode_set_addr(psmouse, addr))
949 return -1;
950 return __alps_command_mode_write_reg(psmouse, value);
951}
952
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800953static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
954 int repeated_command, unsigned char *param)
955{
956 struct ps2dev *ps2dev = &psmouse->ps2dev;
957
958 param[0] = 0;
959 if (init_command && ps2_command(ps2dev, param, init_command))
960 return -EIO;
961
962 if (ps2_command(ps2dev, NULL, repeated_command) ||
963 ps2_command(ps2dev, NULL, repeated_command) ||
964 ps2_command(ps2dev, NULL, repeated_command))
965 return -EIO;
966
967 param[0] = param[1] = param[2] = 0xff;
968 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
969 return -EIO;
970
971 psmouse_dbg(psmouse, "%2.2X report: %2.2x %2.2x %2.2x\n",
972 repeated_command, param[0], param[1], param[2]);
973 return 0;
974}
975
Seth Forshee25bded72011-11-07 19:53:36 -0800976static int alps_enter_command_mode(struct psmouse *psmouse,
977 unsigned char *resp)
978{
979 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -0800980
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800981 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800982 psmouse_err(psmouse, "failed to enter command mode\n");
983 return -1;
984 }
985
Kevin Cernekee56fd3402013-02-13 22:24:22 -0800986 if (param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) {
Seth Forshee25bded72011-11-07 19:53:36 -0800987 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -0800988 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -0800989 return -1;
990 }
991
992 if (resp)
993 *resp = param[2];
994 return 0;
995}
996
997static inline int alps_exit_command_mode(struct psmouse *psmouse)
998{
999 struct ps2dev *ps2dev = &psmouse->ps2dev;
1000 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1001 return -1;
1002 return 0;
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/*
1006 * For DualPoint devices select the device that should respond to
1007 * subsequent commands. It looks like glidepad is behind stickpointer,
1008 * I'd thought it would be other way around...
1009 */
Seth Forshee25bded72011-11-07 19:53:36 -08001010static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1014
1015 if (ps2_command(ps2dev, NULL, cmd) ||
1016 ps2_command(ps2dev, NULL, cmd) ||
1017 ps2_command(ps2dev, NULL, cmd) ||
1018 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1019 return -1;
1020
1021 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001022 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 return 0;
1025}
1026
Seth Forshee25bded72011-11-07 19:53:36 -08001027static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 struct ps2dev *ps2dev = &psmouse->ps2dev;
1030
1031 /* Try ALPS magic knock - 4 disable before enable */
1032 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1033 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1034 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1035 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1036 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1037 return -1;
1038
1039 /*
1040 * Switch mouse to poll (remote) mode so motion data will not
1041 * get in our way
1042 */
1043 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1044}
1045
1046static int alps_get_status(struct psmouse *psmouse, char *param)
1047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001049 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return -1;
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
1053}
1054
1055/*
1056 * Turn touchpad tapping on or off. The sequences are:
1057 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1058 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1059 * My guess that 0xE9 (GetInfo) is here as a sync point.
1060 * For models that also have stickpointer (DualPoints) its tapping
1061 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1062 * we don't fiddle with it.
1063 */
1064static int alps_tap_mode(struct psmouse *psmouse, int enable)
1065{
1066 struct ps2dev *ps2dev = &psmouse->ps2dev;
1067 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1068 unsigned char tap_arg = enable ? 0x0A : 0x00;
1069 unsigned char param[4];
1070
1071 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1072 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1073 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1074 ps2_command(ps2dev, &tap_arg, cmd))
1075 return -1;
1076
1077 if (alps_get_status(psmouse, param))
1078 return -1;
1079
1080 return 0;
1081}
1082
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001083/*
1084 * alps_poll() - poll the touchpad for current motion packet.
1085 * Used in resync.
1086 */
1087static int alps_poll(struct psmouse *psmouse)
1088{
1089 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001090 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001091 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001092
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001093 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001094 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001095
1096 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1097 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1098
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001099 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001100 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001101
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001102 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001103 return -1;
1104
1105 if ((psmouse->badbyte & 0xc8) == 0x08) {
1106/*
1107 * Poll the track stick ...
1108 */
1109 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1110 return -1;
1111 }
1112
1113 memcpy(psmouse->packet, buf, sizeof(buf));
1114 return 0;
1115}
1116
Seth Forshee25bded72011-11-07 19:53:36 -08001117static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001121 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001122 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001126 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001127 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Seth Forshee25bded72011-11-07 19:53:36 -08001131 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001132 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return -1;
1134 }
1135
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001136 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001137 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001141 /* ALPS needs stream mode, otherwise it won't report any data */
1142 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001143 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001144 return -1;
1145 }
1146
1147 return 0;
1148}
1149
Seth Forshee25bded72011-11-07 19:53:36 -08001150/*
1151 * Enable or disable passthrough mode to the trackstick. Must be in
1152 * command mode when calling this function.
1153 */
1154static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable)
1155{
1156 int reg_val;
1157
1158 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1159 if (reg_val == -1)
1160 return -1;
1161
1162 if (enable)
1163 reg_val |= 0x01;
1164 else
1165 reg_val &= ~0x01;
1166
1167 if (__alps_command_mode_write_reg(psmouse, reg_val))
1168 return -1;
1169
1170 return 0;
1171}
1172
1173/* Must be in command mode when calling this function */
1174static int alps_absolute_mode_v3(struct psmouse *psmouse)
1175{
1176 int reg_val;
1177
1178 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1179 if (reg_val == -1)
1180 return -1;
1181
1182 reg_val |= 0x06;
1183 if (__alps_command_mode_write_reg(psmouse, reg_val))
1184 return -1;
1185
1186 return 0;
1187}
1188
1189static int alps_hw_init_v3(struct psmouse *psmouse)
1190{
Seth Forshee25bded72011-11-07 19:53:36 -08001191 struct ps2dev *ps2dev = &psmouse->ps2dev;
1192 int reg_val;
1193 unsigned char param[4];
1194
Seth Forshee25bded72011-11-07 19:53:36 -08001195 if (alps_enter_command_mode(psmouse, NULL))
1196 goto error;
1197
1198 /* Check for trackstick */
1199 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1200 if (reg_val == -1)
1201 goto error;
1202 if (reg_val & 0x80) {
1203 if (alps_passthrough_mode_v3(psmouse, true))
1204 goto error;
1205 if (alps_exit_command_mode(psmouse))
1206 goto error;
1207
1208 /*
1209 * E7 report for the trackstick
1210 *
1211 * There have been reports of failures to seem to trace back
1212 * to the above trackstick check failing. When these occur
1213 * this E7 report fails, so when that happens we continue
1214 * with the assumption that there isn't a trackstick after
1215 * all.
1216 */
1217 param[0] = 0x64;
1218 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1219 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1220 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1221 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1222 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1223 } else {
1224 psmouse_dbg(psmouse,
1225 "trackstick E7 report: %2.2x %2.2x %2.2x\n",
1226 param[0], param[1], param[2]);
1227
1228 /*
1229 * Not sure what this does, but it is absolutely
1230 * essential. Without it, the touchpad does not
1231 * work at all and the trackstick just emits normal
1232 * PS/2 packets.
1233 */
1234 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1235 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1236 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1237 alps_command_mode_send_nibble(psmouse, 0x9) ||
1238 alps_command_mode_send_nibble(psmouse, 0x4)) {
1239 psmouse_err(psmouse,
1240 "Error sending magic E6 sequence\n");
1241 goto error_passthrough;
1242 }
1243 }
1244
1245 if (alps_enter_command_mode(psmouse, NULL))
1246 goto error_passthrough;
1247 if (alps_passthrough_mode_v3(psmouse, false))
1248 goto error;
1249 }
1250
1251 if (alps_absolute_mode_v3(psmouse)) {
1252 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1253 goto error;
1254 }
1255
1256 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1257 if (reg_val == -1)
1258 goto error;
1259 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1260 goto error;
1261
1262 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1263 if (reg_val == -1)
1264 goto error;
1265 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1266 goto error;
1267
1268 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1269 goto error;
1270 if (__alps_command_mode_write_reg(psmouse, 0x04))
1271 goto error;
1272
1273 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1274 goto error;
1275 if (__alps_command_mode_write_reg(psmouse, 0x03))
1276 goto error;
1277
1278 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1279 goto error;
1280 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1281 goto error;
1282
1283 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1284 goto error;
1285 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1286 goto error;
1287
1288 /*
1289 * This ensures the trackstick packets are in the format
1290 * supported by this driver. If bit 1 isn't set the packet
1291 * format is different.
1292 */
1293 if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82))
1294 goto error;
1295
1296 alps_exit_command_mode(psmouse);
1297
1298 /* Set rate and enable data reporting */
1299 param[0] = 0x64;
1300 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1301 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1302 psmouse_err(psmouse, "Failed to enable data reporting\n");
1303 return -1;
1304 }
1305
1306 return 0;
1307
1308error_passthrough:
1309 /* Something failed while in passthrough mode, so try to get out */
1310 if (!alps_enter_command_mode(psmouse, NULL))
1311 alps_passthrough_mode_v3(psmouse, false);
1312error:
1313 /*
1314 * Leaving the touchpad in command mode will essentially render
1315 * it unusable until the machine reboots, so exit it here just
1316 * to be safe
1317 */
1318 alps_exit_command_mode(psmouse);
1319 return -1;
1320}
1321
1322/* Must be in command mode when calling this function */
1323static int alps_absolute_mode_v4(struct psmouse *psmouse)
1324{
1325 int reg_val;
1326
1327 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1328 if (reg_val == -1)
1329 return -1;
1330
1331 reg_val |= 0x02;
1332 if (__alps_command_mode_write_reg(psmouse, reg_val))
1333 return -1;
1334
1335 return 0;
1336}
1337
1338static int alps_hw_init_v4(struct psmouse *psmouse)
1339{
Seth Forshee25bded72011-11-07 19:53:36 -08001340 struct ps2dev *ps2dev = &psmouse->ps2dev;
1341 unsigned char param[4];
1342
Seth Forshee25bded72011-11-07 19:53:36 -08001343 if (alps_enter_command_mode(psmouse, NULL))
1344 goto error;
1345
1346 if (alps_absolute_mode_v4(psmouse)) {
1347 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1348 goto error;
1349 }
1350
1351 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1352 goto error;
1353
1354 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1355 goto error;
1356
1357 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1358 goto error;
1359
1360 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1361 goto error;
1362
1363 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1364 goto error;
1365
1366 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1367 goto error;
1368
1369 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1370 goto error;
1371
1372 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1373 goto error;
1374
1375 alps_exit_command_mode(psmouse);
1376
1377 /*
1378 * This sequence changes the output from a 9-byte to an
1379 * 8-byte format. All the same data seems to be present,
1380 * just in a more compact format.
1381 */
1382 param[0] = 0xc8;
1383 param[1] = 0x64;
1384 param[2] = 0x50;
1385 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1386 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1387 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1388 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1389 return -1;
1390
1391 /* Set rate and enable data reporting */
1392 param[0] = 0x64;
1393 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1394 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1395 psmouse_err(psmouse, "Failed to enable data reporting\n");
1396 return -1;
1397 }
1398
1399 return 0;
1400
1401error:
1402 /*
1403 * Leaving the touchpad in command mode will essentially render
1404 * it unusable until the machine reboots, so exit it here just
1405 * to be safe
1406 */
1407 alps_exit_command_mode(psmouse);
1408 return -1;
1409}
1410
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001411static void alps_set_defaults(struct alps_data *priv)
Seth Forshee25bded72011-11-07 19:53:36 -08001412{
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001413 priv->byte0 = 0x8f;
1414 priv->mask0 = 0x8f;
1415 priv->flags = ALPS_DUALPOINT;
1416
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001417 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08001418 case ALPS_PROTO_V1:
1419 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001420 priv->hw_init = alps_hw_init_v1_v2;
1421 priv->process_packet = alps_process_packet_v1_v2;
1422 priv->set_abs_params = alps_set_abs_params_st;
Seth Forshee25bded72011-11-07 19:53:36 -08001423 break;
1424 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001425 priv->hw_init = alps_hw_init_v3;
1426 priv->process_packet = alps_process_packet_v3;
1427 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001428 priv->nibble_commands = alps_v3_nibble_commands;
1429 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08001430 break;
1431 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001432 priv->hw_init = alps_hw_init_v4;
1433 priv->process_packet = alps_process_packet_v4;
1434 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08001435 priv->nibble_commands = alps_v4_nibble_commands;
1436 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08001437 break;
1438 }
Seth Forshee25bded72011-11-07 19:53:36 -08001439}
1440
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001441static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1442 unsigned char *e7, unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001443{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001444 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001445 int i;
1446
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001447 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1448 model = &alps_model_data[i];
1449
1450 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1451 (!model->command_mode_resp ||
1452 model->command_mode_resp == ec[2])) {
1453
1454 priv->proto_version = model->proto_version;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001455 alps_set_defaults(priv);
1456
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001457 priv->flags = model->flags;
1458 priv->byte0 = model->byte0;
1459 priv->mask0 = model->mask0;
1460
1461 return 0;
1462 }
1463 }
1464
1465 return -EINVAL;
1466}
1467
1468static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1469{
1470 unsigned char e6[4], e7[4], ec[4];
1471
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001472 /*
1473 * First try "E6 report".
1474 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1475 * The bits 0-2 of the first byte will be 1s if some buttons are
1476 * pressed.
1477 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001478 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1479 PSMOUSE_CMD_SETSCALE11, e6))
1480 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001481
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001482 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1483 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001484
1485 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001486 * Now get the "E7" and "EC" reports. These will uniquely identify
1487 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001488 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001489 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1490 PSMOUSE_CMD_SETSCALE21, e7) ||
1491 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1492 PSMOUSE_CMD_RESET_WRAP, ec) ||
1493 alps_exit_command_mode(psmouse))
1494 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001495
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001496 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001497 return 0;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08001498 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1499 ec[2] >= 0x90 && ec[2] <= 0x9d) {
1500 priv->proto_version = ALPS_PROTO_V3;
1501 alps_set_defaults(priv);
1502
1503 return 0;
1504 }
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001505
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001506 psmouse_info(psmouse,
1507 "Unknown ALPS touchpad: E7=%2.2x %2.2x %2.2x, EC=%2.2x %2.2x %2.2x\n",
1508 e7[0], e7[1], e7[2], ec[0], ec[1], ec[2]);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001509
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001510 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08001511}
1512
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001513static int alps_reconnect(struct psmouse *psmouse)
1514{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001515 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001516
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001517 psmouse_reset(psmouse);
1518
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001519 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001520 return -1;
1521
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001522 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525static void alps_disconnect(struct psmouse *psmouse)
1526{
1527 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001530 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001531 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 kfree(priv);
1533}
1534
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001535static void alps_set_abs_params_st(struct alps_data *priv,
1536 struct input_dev *dev1)
1537{
1538 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1539 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1540}
1541
1542static void alps_set_abs_params_mt(struct alps_data *priv,
1543 struct input_dev *dev1)
1544{
1545 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
1546 input_mt_init_slots(dev1, 2, 0);
1547 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, ALPS_V3_X_MAX, 0, 0);
1548 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1549
1550 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1551 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1552 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
1553
1554 input_set_abs_params(dev1, ABS_X, 0, ALPS_V3_X_MAX, 0, 0);
1555 input_set_abs_params(dev1, ABS_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1556}
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558int alps_init(struct psmouse *psmouse)
1559{
1560 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001561 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001563 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001564 dev2 = input_allocate_device();
1565 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001567
1568 priv->dev2 = dev2;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001569 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1570
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001571 psmouse->private = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Seth Forshee25bded72011-11-07 19:53:36 -08001573 psmouse_reset(psmouse);
1574
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001575 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001576 goto init_fail;
1577
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001578 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 goto init_fail;
1580
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08001581 /*
1582 * Undo part of setup done for us by psmouse core since touchpad
1583 * is not a relative device.
1584 */
1585 __clear_bit(EV_REL, dev1->evbit);
1586 __clear_bit(REL_X, dev1->relbit);
1587 __clear_bit(REL_Y, dev1->relbit);
1588
1589 /*
1590 * Now set up our capabilities.
1591 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001592 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1593 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1594 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001595 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1596 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001598 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08001599
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001600 priv->set_abs_params(priv, dev1);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001601 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001603 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001604 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1605 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001608 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001609 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1610 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001613 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001614 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1615 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1616 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1617 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1618 } else {
1619 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1620 }
1621
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001622 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001623 dev2->phys = priv->phys;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001624 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1625 "DualPoint Stick" : "PS/2 Mouse";
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001626 dev2->id.bustype = BUS_I8042;
1627 dev2->id.vendor = 0x0002;
1628 dev2->id.product = PSMOUSE_ALPS;
1629 dev2->id.version = 0x0000;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04001630 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001631
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001632 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08001633 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1634 dev2->keybit[BIT_WORD(BTN_LEFT)] =
1635 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001637 if (input_register_device(priv->dev2))
1638 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001641 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 psmouse->disconnect = alps_disconnect;
1643 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001644 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001646 /* We are having trouble resyncing ALPS touchpads so disable it for now */
1647 psmouse->resync_time = 0;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
1650
1651init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04001652 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001653 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001655 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return -1;
1657}
1658
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001659int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001661 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001663 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return -1;
1665
1666 if (set_properties) {
1667 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001668 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05001669 "DualPoint TouchPad" : "GlidePoint";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08001670 psmouse->model = dummy.proto_version << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672 return 0;
1673}
1674