blob: b97832b622cbcac21dc5cebef44ce05149bf2a62 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
23
24#include "psmouse.h"
25#include "alps.h"
26
Seth Forshee25bded72011-11-07 19:53:36 -080027/*
28 * Definitions for ALPS version 3 and 4 command mode protocol
29 */
30#define ALPS_CMD_NIBBLE_10 0x01f2
31
Kevin Cernekeecd401202013-02-13 22:28:07 -080032#define ALPS_REG_BASE_RUSHMORE 0xc2c0
33#define ALPS_REG_BASE_PINNACLE 0x0000
34
Seth Forshee25bded72011-11-07 19:53:36 -080035static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
37 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
38 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
39 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
40 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
41 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
42 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
43 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
44 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
45 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
46 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
47 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
48 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
49 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
50 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
51 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
52};
53
54static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
56 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
57 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
58 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
59 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
60 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
61 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
62 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
63 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
64 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
65 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
66 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
67 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
68 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
69 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
70 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
71};
72
Yunkang Tang95f75e92013-12-01 22:33:52 -080073static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
75 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
76 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
77 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
78 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
79 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
80 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
81 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
82 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
83 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
84 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
85 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
86 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
87 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
88 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
89 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
90};
91
Seth Forshee25bded72011-11-07 19:53:36 -080092
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080093#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
94#define ALPS_PASS 0x04 /* device has a pass-through port */
95
96#define ALPS_WHEEL 0x08 /* hardware wheel present */
97#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
98#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
99#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800100#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
Yunkang Tang38088432014-07-26 13:51:41 -0700102#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Helge Dellere38de672006-09-10 21:54:39 -0400104static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800105 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
106 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
107 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
108 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
109 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
110 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
113 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
114 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
115 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
117 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
118 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
120 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* XXX */
121 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
122 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800123 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800124 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
125 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
126 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
127 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
128 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
129 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
130 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800133static const struct alps_protocol_info alps_v3_protocol_data = {
134 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
135};
136
137static const struct alps_protocol_info alps_v3_rushmore_data = {
138 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
139};
140
141static const struct alps_protocol_info alps_v5_protocol_data = {
142 ALPS_PROTO_V5, 0xc8, 0xd8, 0
143};
144
145static const struct alps_protocol_info alps_v7_protocol_data = {
146 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
147};
148
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800149static void alps_set_abs_params_st(struct alps_data *priv,
150 struct input_dev *dev1);
151static void alps_set_abs_params_mt(struct alps_data *priv,
152 struct input_dev *dev1);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
155 * XXX - this entry is suspicious. First byte has zero lower nibble,
156 * which is what a normal mouse would report. Also, the value 0x0e
157 * isn't valid per PS/2 spec.
158 */
159
Seth Forsheed4b347b2011-11-07 19:53:15 -0800160/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800162static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800163 unsigned char data)
164{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800165 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800166}
167
168static void alps_report_buttons(struct psmouse *psmouse,
169 struct input_dev *dev1, struct input_dev *dev2,
170 int left, int right, int middle)
171{
Martin Buckc91ed052010-03-13 22:23:58 -0800172 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800173
Martin Buckc91ed052010-03-13 22:23:58 -0800174 /*
175 * If shared button has already been reported on the
176 * other device (dev2) then this event should be also
177 * sent through that device.
178 */
179 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
180 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800181
Martin Buckc91ed052010-03-13 22:23:58 -0800182 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
183 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800184
Martin Buckc91ed052010-03-13 22:23:58 -0800185 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
186 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800187
Martin Buckc91ed052010-03-13 22:23:58 -0800188 /*
189 * Sync the _other_ device now, we'll do the first
190 * device later once we report the rest of the events.
191 */
192 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800193}
194
Seth Forshee25bded72011-11-07 19:53:36 -0800195static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct alps_data *priv = psmouse->private;
198 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500199 struct input_dev *dev = psmouse->dev;
200 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500202 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800204 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400205 left = packet[2] & 0x10;
206 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 middle = 0;
208 x = packet[1] | ((packet[0] & 0x07) << 7);
209 y = packet[4] | ((packet[3] & 0x07) << 7);
210 z = packet[5];
211 } else {
212 left = packet[3] & 1;
213 right = packet[3] & 2;
214 middle = packet[3] & 4;
215 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
216 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
217 z = packet[5];
218 }
219
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800220 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400221 back = packet[0] & 0x10;
222 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500223 }
224
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800225 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500226 back = packet[3] & 4;
227 forward = packet[2] & 4;
228 if ((middle = forward && back))
229 forward = back = 0;
230 }
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 ges = packet[2] & 1;
233 fin = packet[2] & 2;
234
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800235 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
237 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700238
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800239 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 input_sync(dev2);
242 return;
243 }
244
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800245 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800248 if (ges && !fin)
249 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /*
252 * A "tap and drag" operation is reported by the hardware as a transition
253 * from (!fin && ges) to (fin && ges). This should be translated to the
254 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
255 */
256 if (ges && fin && !priv->prev_fin) {
257 input_report_abs(dev, ABS_X, x);
258 input_report_abs(dev, ABS_Y, y);
259 input_report_abs(dev, ABS_PRESSURE, 0);
260 input_report_key(dev, BTN_TOOL_FINGER, 0);
261 input_sync(dev);
262 }
263 priv->prev_fin = fin;
264
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800265 if (z > 30)
266 input_report_key(dev, BTN_TOUCH, 1);
267 if (z < 25)
268 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (z > 0) {
271 input_report_abs(dev, ABS_X, x);
272 input_report_abs(dev, ABS_Y, y);
273 }
274
275 input_report_abs(dev, ABS_PRESSURE, z);
276 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
277
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800278 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500279 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800281 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500282 input_report_key(dev, BTN_FORWARD, forward);
283 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800286 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800287 input_report_key(dev, BTN_0, packet[2] & 4);
288 input_report_key(dev, BTN_1, packet[0] & 0x10);
289 input_report_key(dev, BTN_2, packet[3] & 4);
290 input_report_key(dev, BTN_3, packet[0] & 0x20);
291 }
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 input_sync(dev);
294}
295
Seth Forshee01ce6612011-11-07 19:54:13 -0800296/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800297 * Process bitmap data for V5 protocols. Return value is null.
298 *
299 * The bitmaps don't have enough data to track fingers, so this function
300 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700301 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800302 */
303static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700304 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800305{
306 int box_middle_x, box_middle_y;
307 unsigned int x_map, y_map;
308 unsigned char start_bit, end_bit;
309 unsigned char x_msb, x_lsb, y_msb, y_lsb;
310
311 x_map = fields->x_map;
312 y_map = fields->y_map;
313
314 if (!x_map || !y_map)
315 return;
316
317 /* Get Most-significant and Least-significant bit */
318 x_msb = fls(x_map);
319 x_lsb = ffs(x_map);
320 y_msb = fls(y_map);
321 y_lsb = ffs(y_map);
322
323 /* Most-significant bit should never exceed max sensor line number */
324 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
325 return;
326
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800327 if (fields->fingers > 1) {
328 start_bit = priv->x_bits - x_msb;
329 end_bit = priv->x_bits - x_lsb;
330 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
331 (2 * (priv->x_bits - 1));
332
333 start_bit = y_lsb - 1;
334 end_bit = y_msb - 1;
335 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
336 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700337 fields->mt[0] = fields->st;
338 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
339 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800340 }
341}
342
Hans de Goede036e6c72014-07-25 22:38:51 -0700343static void alps_get_bitmap_points(unsigned int map,
344 struct alps_bitmap_point *low,
345 struct alps_bitmap_point *high,
346 int *fingers)
347{
348 struct alps_bitmap_point *point;
349 int i, bit, prev_bit = 0;
350
351 point = low;
352 for (i = 0; map != 0; i++, map >>= 1) {
353 bit = map & 1;
354 if (bit) {
355 if (!prev_bit) {
356 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700357 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700358 (*fingers)++;
359 }
360 point->num_bits++;
361 } else {
362 if (prev_bit)
363 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700364 }
365 prev_bit = bit;
366 }
367}
368
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800369/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800370 * Process bitmap data from v3 and v4 protocols. Returns the number of
371 * fingers detected. A return value of 0 means at least one of the
372 * bitmaps was empty.
373 *
374 * The bitmaps don't have enough data to track fingers, so this function
375 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700376 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800377 * is greater than 0.
378 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800379static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700380 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800381{
Hans de Goede036e6c72014-07-25 22:38:51 -0700382 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800383 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
384 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800385
Hans de Goede02d04252014-07-25 22:43:35 -0700386 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800387 return 0;
388
Hans de Goede02d04252014-07-25 22:43:35 -0700389 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
390 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800391
392 /*
393 * Fingers can overlap, so we use the maximum count of fingers
394 * on either axis as the finger count.
395 */
396 fingers = max(fingers_x, fingers_y);
397
398 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700399 * If an axis reports only a single contact, we have overlapping or
400 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800401 */
Hans de Goede20bea682014-07-25 22:33:33 -0700402 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700403 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700404 x_low.num_bits = x_low.num_bits - i;
405 x_high.start_bit = x_low.start_bit + i;
406 x_high.num_bits = max(i, 1);
407 }
408 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700409 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700410 y_low.num_bits = y_low.num_bits - i;
411 y_high.start_bit = y_low.start_bit + i;
412 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800413 }
414
Hans de Goede02d04252014-07-25 22:43:35 -0700415 fields->mt[0].x =
416 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
417 (2 * (priv->x_bits - 1));
418 fields->mt[0].y =
419 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
420 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800421
Hans de Goede02d04252014-07-25 22:43:35 -0700422 fields->mt[1].x =
423 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
424 (2 * (priv->x_bits - 1));
425 fields->mt[1].y =
426 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
427 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800428
Hans de Goede40e8f532014-07-25 22:37:15 -0700429 /* y-bitmap order is reversed, except on rushmore */
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800430 if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700431 fields->mt[0].y = priv->y_max - fields->mt[0].y;
432 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700433 }
434
Seth Forshee01ce6612011-11-07 19:54:13 -0800435 return fingers;
436}
437
Hans de Goedecdf333e2014-07-25 22:44:42 -0700438static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800439{
440 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700441 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
442 input_report_abs(dev, ABS_MT_POSITION_X, x);
443 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800444}
445
Hans de Goedecdf333e2014-07-25 22:44:42 -0700446static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800447{
Hans de Goede02d04252014-07-25 22:43:35 -0700448 struct alps_data *priv = psmouse->private;
449 struct input_dev *dev = psmouse->dev;
450 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700451 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700452
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800453 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700454 for (i = 0; i < n; i++)
455 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
456
457 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800458}
459
Hans de Goede68c21872014-07-25 22:47:25 -0700460static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
461{
462 struct alps_data *priv = psmouse->private;
463 struct input_dev *dev = psmouse->dev;
464 struct alps_fields *f = &priv->f;
465
466 /* Use st data when we don't have mt data */
467 if (fingers < 2) {
468 f->mt[0].x = f->st.x;
469 f->mt[0].y = f->st.y;
470 fingers = f->pressure > 0 ? 1 : 0;
471 }
472
Hans de Goede99d99962014-07-25 22:48:02 -0700473 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
Hans de Goede68c21872014-07-25 22:47:25 -0700474
475 input_mt_report_finger_count(dev, fingers);
476
477 input_report_key(dev, BTN_LEFT, f->left);
478 input_report_key(dev, BTN_RIGHT, f->right);
479 input_report_key(dev, BTN_MIDDLE, f->middle);
480
481 input_report_abs(dev, ABS_PRESSURE, f->pressure);
482
483 input_sync(dev);
484}
485
Seth Forshee25bded72011-11-07 19:53:36 -0800486static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
487{
488 struct alps_data *priv = psmouse->private;
489 unsigned char *packet = psmouse->packet;
490 struct input_dev *dev = priv->dev2;
491 int x, y, z, left, right, middle;
492
Pali Rohár34412ba2015-01-09 12:48:58 -0800493 /* It should be a DualPoint when received trackstick packet */
494 if (!(priv->flags & ALPS_DUALPOINT)) {
495 psmouse_warn(psmouse,
496 "Rejected trackstick packet from non DualPoint device");
497 return;
498 }
499
Seth Forshee25bded72011-11-07 19:53:36 -0800500 /* Sanity check packet */
501 if (!(packet[0] & 0x40)) {
502 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
503 return;
504 }
505
506 /*
507 * There's a special packet that seems to indicate the end
508 * of a stream of trackstick data. Filter these out.
509 */
510 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
511 return;
512
513 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
514 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
515 z = (packet[4] & 0x7c) >> 2;
516
517 /*
518 * The x and y values tend to be quite large, and when used
519 * alone the trackstick is difficult to use. Scale them down
520 * to compensate.
521 */
522 x /= 8;
523 y /= 8;
524
525 input_report_rel(dev, REL_X, x);
526 input_report_rel(dev, REL_Y, -y);
527
528 /*
529 * Most ALPS models report the trackstick buttons in the touchpad
530 * packets, but a few report them here. No reliable way has been
531 * found to differentiate between the models upfront, so we enable
532 * the quirk in response to seeing a button press in the trackstick
533 * packet.
534 */
535 left = packet[3] & 0x01;
536 right = packet[3] & 0x02;
537 middle = packet[3] & 0x04;
538
539 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
540 (left || right || middle))
541 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
542
543 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
544 input_report_key(dev, BTN_LEFT, left);
545 input_report_key(dev, BTN_RIGHT, right);
546 input_report_key(dev, BTN_MIDDLE, middle);
547 }
548
549 input_sync(dev);
550 return;
551}
552
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800553static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
554{
555 f->left = !!(p[3] & 0x01);
556 f->right = !!(p[3] & 0x02);
557 f->middle = !!(p[3] & 0x04);
558
559 f->ts_left = !!(p[3] & 0x10);
560 f->ts_right = !!(p[3] & 0x20);
561 f->ts_middle = !!(p[3] & 0x40);
562}
563
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700564static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800565 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800566{
567 f->first_mp = !!(p[4] & 0x40);
568 f->is_mp = !!(p[0] & 0x40);
569
570 f->fingers = (p[5] & 0x3) + 1;
571 f->x_map = ((p[4] & 0x7e) << 8) |
572 ((p[1] & 0x7f) << 2) |
573 ((p[0] & 0x30) >> 4);
574 f->y_map = ((p[3] & 0x70) << 4) |
575 ((p[2] & 0x7f) << 1) |
576 (p[4] & 0x01);
577
Hans de Goede02d04252014-07-25 22:43:35 -0700578 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800579 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700580 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
581 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800582
583 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700584
585 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800586}
587
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700588static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800589 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800590{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800591 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800592
Yunkang Tangf105e342014-07-25 22:29:24 -0700593 /* Rushmore's packet decode has a bit difference with Pinnacle's */
594 f->is_mp = !!(p[5] & 0x40);
595 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800596 f->x_map |= (p[5] & 0x10) << 11;
597 f->y_map |= (p[5] & 0x20) << 6;
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700598
599 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800600}
601
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700602static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800603 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800604{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800605 u64 palm_data = 0;
606 struct alps_data *priv = psmouse->private;
607
Dave Turvene75af9e52013-02-21 22:58:28 -0800608 f->first_mp = !!(p[0] & 0x02);
609 f->is_mp = !!(p[0] & 0x20);
610
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800611 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700612 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
613 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
614 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800615 alps_decode_buttons_v3(f, p);
616 } else {
617 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800618 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800619
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800620 palm_data = (p[1] & 0x7f) |
621 ((p[2] & 0x7f) << 7) |
622 ((p[4] & 0x7f) << 14) |
623 ((p[5] & 0x7f) << 21) |
624 ((p[3] & 0x07) << 28) |
625 (((u64)p[3] & 0x70) << 27) |
626 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800627
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800628 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
629 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
630
631 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
632 f->x_map = (palm_data >> priv->y_bits) &
633 (BIT(priv->x_bits) - 1);
634 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700635
636 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800637}
638
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800639static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800640{
641 struct alps_data *priv = psmouse->private;
642 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800643 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700644 struct alps_fields *f = &priv->f;
645 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800646
Hans de Goede02d04252014-07-25 22:43:35 -0700647 memset(f, 0, sizeof(*f));
648
649 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800650
651 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800652 * There's no single feature of touchpad position and bitmap packets
653 * that can be used to distinguish between them. We rely on the fact
654 * that a bitmap packet should always follow a position packet with
655 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800656 */
657 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800658 /*
659 * Sometimes a position packet will indicate a multi-packet
660 * sequence, but then what follows is another position
661 * packet. Check for this, and when it happens process the
662 * position packet as usual.
663 */
Hans de Goede02d04252014-07-25 22:43:35 -0700664 if (f->is_mp) {
665 fingers = f->fingers;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800666 if (priv->proto_version == ALPS_PROTO_V3 ||
667 priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700668 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700669 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800670
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800671 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700672 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800673 psmouse);
674 } else {
675 /*
676 * Because Dolphin uses position packet's
677 * coordinate data as Pt1 and uses it to
678 * calculate Pt2, so we need to do position
679 * packet decode first.
680 */
Hans de Goede02d04252014-07-25 22:43:35 -0700681 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800682 psmouse);
683
684 /*
685 * Since Dolphin's finger number is reliable,
686 * there is no need to compare with bmap_fn.
687 */
Hans de Goede02d04252014-07-25 22:43:35 -0700688 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800689 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800690 } else {
691 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800692 }
693 }
694
Seth Forshee01ce6612011-11-07 19:54:13 -0800695 /*
696 * Bit 6 of byte 0 is not usually set in position packets. The only
697 * times it seems to be set is in situations where the data is
698 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
699 * this combined with the fact that this bit is useful for filtering
700 * out misidentified bitmap packets, we reject anything with this
701 * bit set.
702 */
Hans de Goede02d04252014-07-25 22:43:35 -0700703 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800704 return;
705
Hans de Goede02d04252014-07-25 22:43:35 -0700706 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800707 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800708 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
709 return;
710 }
711
712 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800713
Seth Forshee25bded72011-11-07 19:53:36 -0800714 /*
715 * Sometimes the hardware sends a single packet with z = 0
716 * in the middle of a stream. Real releases generate packets
717 * with x, y, and z all zero, so these seem to be flukes.
718 * Ignore them.
719 */
Hans de Goede02d04252014-07-25 22:43:35 -0700720 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800721 return;
722
Hans de Goede68c21872014-07-25 22:47:25 -0700723 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800724
Pali Rohár34412ba2015-01-09 12:48:58 -0800725 if ((priv->flags & ALPS_DUALPOINT) &&
726 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700727 input_report_key(dev2, BTN_LEFT, f->ts_left);
728 input_report_key(dev2, BTN_RIGHT, f->ts_right);
729 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800730 input_sync(dev2);
731 }
732}
733
734static void alps_process_packet_v3(struct psmouse *psmouse)
735{
736 unsigned char *packet = psmouse->packet;
737
738 /*
739 * v3 protocol packets come in three types, two representing
740 * touchpad data and one representing trackstick data.
741 * Trackstick packets seem to be distinguished by always
742 * having 0x3f in the last byte. This value has never been
743 * observed in the last byte of either of the other types
744 * of packets.
745 */
746 if (packet[5] == 0x3f) {
747 alps_process_trackstick_packet_v3(psmouse);
748 return;
749 }
750
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800751 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800752}
753
Yunkang Tang95f75e92013-12-01 22:33:52 -0800754static void alps_process_packet_v6(struct psmouse *psmouse)
755{
756 struct alps_data *priv = psmouse->private;
757 unsigned char *packet = psmouse->packet;
758 struct input_dev *dev = psmouse->dev;
759 struct input_dev *dev2 = priv->dev2;
760 int x, y, z, left, right, middle;
761
762 /*
763 * We can use Byte5 to distinguish if the packet is from Touchpad
764 * or Trackpoint.
765 * Touchpad: 0 - 0x7E
766 * Trackpoint: 0x7F
767 */
768 if (packet[5] == 0x7F) {
769 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800770 if (!(priv->flags & ALPS_DUALPOINT)) {
771 psmouse_warn(psmouse,
772 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800773 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800774 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800775
776 /* Trackpoint packet */
777 x = packet[1] | ((packet[3] & 0x20) << 2);
778 y = packet[2] | ((packet[3] & 0x40) << 1);
779 z = packet[4];
780 left = packet[3] & 0x01;
781 right = packet[3] & 0x02;
782 middle = packet[3] & 0x04;
783
784 /* To prevent the cursor jump when finger lifted */
785 if (x == 0x7F && y == 0x7F && z == 0x7F)
786 x = y = z = 0;
787
788 /* Divide 4 since trackpoint's speed is too fast */
789 input_report_rel(dev2, REL_X, (char)x / 4);
790 input_report_rel(dev2, REL_Y, -((char)y / 4));
791
792 input_report_key(dev2, BTN_LEFT, left);
793 input_report_key(dev2, BTN_RIGHT, right);
794 input_report_key(dev2, BTN_MIDDLE, middle);
795
796 input_sync(dev2);
797 return;
798 }
799
800 /* Touchpad packet */
801 x = packet[1] | ((packet[3] & 0x78) << 4);
802 y = packet[2] | ((packet[4] & 0x78) << 4);
803 z = packet[5];
804 left = packet[3] & 0x01;
805 right = packet[3] & 0x02;
806
807 if (z > 30)
808 input_report_key(dev, BTN_TOUCH, 1);
809 if (z < 25)
810 input_report_key(dev, BTN_TOUCH, 0);
811
812 if (z > 0) {
813 input_report_abs(dev, ABS_X, x);
814 input_report_abs(dev, ABS_Y, y);
815 }
816
817 input_report_abs(dev, ABS_PRESSURE, z);
818 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
819
820 /* v6 touchpad does not have middle button */
821 input_report_key(dev, BTN_LEFT, left);
822 input_report_key(dev, BTN_RIGHT, right);
823
824 input_sync(dev);
825}
826
Seth Forshee25bded72011-11-07 19:53:36 -0800827static void alps_process_packet_v4(struct psmouse *psmouse)
828{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700829 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800830 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700831 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700832 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700833
834 /*
835 * v4 has a 6-byte encoding for bitmap data, but this data is
836 * broken up between 3 normal packets. Use priv->multi_packet to
837 * track our position in the bitmap packet.
838 */
839 if (packet[6] & 0x40) {
840 /* sync, reset position */
841 priv->multi_packet = 0;
842 }
843
844 if (WARN_ON_ONCE(priv->multi_packet > 2))
845 return;
846
847 offset = 2 * priv->multi_packet;
848 priv->multi_data[offset] = packet[6];
849 priv->multi_data[offset + 1] = packet[7];
850
851 if (++priv->multi_packet > 2) {
852 priv->multi_packet = 0;
853
Hans de Goede02d04252014-07-25 22:43:35 -0700854 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700855 ((priv->multi_data[3] & 0x60) << 3) |
856 ((priv->multi_data[0] & 0x3f) << 2) |
857 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700858 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700859 ((priv->multi_data[3] & 0x1f) << 5) |
860 (priv->multi_data[1] & 0x1f);
861
Hans de Goede02d04252014-07-25 22:43:35 -0700862 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700863 }
Seth Forshee25bded72011-11-07 19:53:36 -0800864
Andreas Boschb0cfb792014-10-15 10:44:50 -0700865 f->left = !!(packet[4] & 0x01);
866 f->right = !!(packet[4] & 0x02);
Seth Forshee25bded72011-11-07 19:53:36 -0800867
Hans de Goede02d04252014-07-25 22:43:35 -0700868 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
869 ((packet[0] & 0x30) >> 4);
870 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
871 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800872
Hans de Goede68c21872014-07-25 22:47:25 -0700873 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800874}
875
Yunkang Tang38088432014-07-26 13:51:41 -0700876static bool alps_is_valid_package_v7(struct psmouse *psmouse)
877{
878 switch (psmouse->pktcnt) {
879 case 3:
880 return (psmouse->packet[2] & 0x40) == 0x40;
881 case 4:
882 return (psmouse->packet[3] & 0x48) == 0x48;
883 case 6:
884 return (psmouse->packet[5] & 0x40) == 0x00;
885 }
886 return true;
887}
888
889static unsigned char alps_get_packet_id_v7(char *byte)
890{
891 unsigned char packet_id;
892
893 if (byte[4] & 0x40)
894 packet_id = V7_PACKET_ID_TWO;
895 else if (byte[4] & 0x01)
896 packet_id = V7_PACKET_ID_MULTI;
897 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
898 packet_id = V7_PACKET_ID_NEW;
899 else if (byte[1] == 0x00 && byte[4] == 0x00)
900 packet_id = V7_PACKET_ID_IDLE;
901 else
902 packet_id = V7_PACKET_ID_UNKNOWN;
903
904 return packet_id;
905}
906
907static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
908 unsigned char *pkt,
909 unsigned char pkt_id)
910{
Hans de Goede27a560b2014-12-18 09:54:50 -0800911 /*
912 * packet-fmt b7 b6 b5 b4 b3 b2 b1 b0
913 * Byte0 TWO & MULTI L 1 R M 1 Y0-2 Y0-1 Y0-0
914 * Byte0 NEW L 1 X1-5 1 1 Y0-2 Y0-1 Y0-0
915 * Byte1 Y0-10 Y0-9 Y0-8 Y0-7 Y0-6 Y0-5 Y0-4 Y0-3
916 * Byte2 X0-11 1 X0-10 X0-9 X0-8 X0-7 X0-6 X0-5
917 * Byte3 X1-11 1 X0-4 X0-3 1 X0-2 X0-1 X0-0
918 * Byte4 TWO X1-10 TWO X1-9 X1-8 X1-7 X1-6 X1-5 X1-4
919 * Byte4 MULTI X1-10 TWO X1-9 X1-8 X1-7 X1-6 Y1-5 1
920 * Byte4 NEW X1-10 TWO X1-9 X1-8 X1-7 X1-6 0 0
921 * Byte5 TWO & NEW Y1-10 0 Y1-9 Y1-8 Y1-7 Y1-6 Y1-5 Y1-4
922 * Byte5 MULTI Y1-10 0 Y1-9 Y1-8 Y1-7 Y1-6 F-1 F-0
923 * L: Left button
924 * R / M: Non-clickpads: Right / Middle button
925 * Clickpads: When > 2 fingers are down, and some fingers
926 * are in the button area, then the 2 coordinates reported
927 * are for fingers outside the button area and these report
928 * extra fingers being present in the right / left button
929 * area. Note these fingers are not added to the F field!
930 * so if a TWO packet is received and R = 1 then there are
931 * 3 fingers down, etc.
932 * TWO: 1: Two touches present, byte 0/4/5 are in TWO fmt
933 * 0: If byte 4 bit 0 is 1, then byte 0/4/5 are in MULTI fmt
934 * otherwise byte 0 bit 4 must be set and byte 0/4/5 are
935 * in NEW fmt
936 * F: Number of fingers - 3, 0 means 3 fingers, 1 means 4 ...
937 */
938
Yunkang Tang38088432014-07-26 13:51:41 -0700939 mt[0].x = ((pkt[2] & 0x80) << 4);
940 mt[0].x |= ((pkt[2] & 0x3F) << 5);
941 mt[0].x |= ((pkt[3] & 0x30) >> 1);
942 mt[0].x |= (pkt[3] & 0x07);
943 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
944
945 mt[1].x = ((pkt[3] & 0x80) << 4);
946 mt[1].x |= ((pkt[4] & 0x80) << 3);
947 mt[1].x |= ((pkt[4] & 0x3F) << 4);
948 mt[1].y = ((pkt[5] & 0x80) << 3);
949 mt[1].y |= ((pkt[5] & 0x3F) << 4);
950
951 switch (pkt_id) {
952 case V7_PACKET_ID_TWO:
953 mt[1].x &= ~0x000F;
954 mt[1].y |= 0x000F;
955 break;
956
957 case V7_PACKET_ID_MULTI:
958 mt[1].x &= ~0x003F;
959 mt[1].y &= ~0x0020;
960 mt[1].y |= ((pkt[4] & 0x02) << 4);
961 mt[1].y |= 0x001F;
962 break;
963
964 case V7_PACKET_ID_NEW:
965 mt[1].x &= ~0x003F;
966 mt[1].x |= (pkt[0] & 0x20);
967 mt[1].y |= 0x000F;
968 break;
969 }
970
971 mt[0].y = 0x7FF - mt[0].y;
972 mt[1].y = 0x7FF - mt[1].y;
973}
974
975static int alps_get_mt_count(struct input_mt_pos *mt)
976{
Hans de Goede7091c442014-12-18 09:53:34 -0800977 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700978
Hans de Goede7091c442014-12-18 09:53:34 -0800979 for (i = 0; i < MAX_TOUCHES; i++) {
980 if (mt[i].x != 0 || mt[i].y != 0)
981 fingers++;
982 }
Yunkang Tang38088432014-07-26 13:51:41 -0700983
Hans de Goede7091c442014-12-18 09:53:34 -0800984 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700985}
986
987static int alps_decode_packet_v7(struct alps_fields *f,
988 unsigned char *p,
989 struct psmouse *psmouse)
990{
Hans de Goeded27eb792014-12-18 09:55:14 -0800991 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700992 unsigned char pkt_id;
993
994 pkt_id = alps_get_packet_id_v7(p);
995 if (pkt_id == V7_PACKET_ID_IDLE)
996 return 0;
997 if (pkt_id == V7_PACKET_ID_UNKNOWN)
998 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -0800999 /*
1000 * NEW packets are send to indicate a discontinuity in the finger
1001 * coordinate reporting. Specifically a finger may have moved from
1002 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1003 * us.
1004 *
1005 * NEW packets have 3 problems:
1006 * 1) They do not contain middle / right button info (on non clickpads)
1007 * this can be worked around by preserving the old button state
1008 * 2) They do not contain an accurate fingercount, and they are
1009 * typically send when the number of fingers changes. We cannot use
1010 * the old finger count as that may mismatch with the amount of
1011 * touch coordinates we've available in the NEW packet
1012 * 3) Their x data for the second touch is inaccurate leading to
1013 * a possible jump of the x coordinate by 16 units when the first
1014 * non NEW packet comes in
1015 * Since problems 2 & 3 cannot be worked around, just ignore them.
1016 */
1017 if (pkt_id == V7_PACKET_ID_NEW)
1018 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001019
1020 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1021
Yunkang Tang38088432014-07-26 13:51:41 -07001022 if (pkt_id == V7_PACKET_ID_TWO)
1023 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001024 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001025 f->fingers = 3 + (p[5] & 0x03);
1026
Hans de Goeded27eb792014-12-18 09:55:14 -08001027 f->left = (p[0] & 0x80) >> 7;
1028 if (priv->flags & ALPS_BUTTONPAD) {
1029 if (p[0] & 0x20)
1030 f->fingers++;
1031 if (p[0] & 0x10)
1032 f->fingers++;
1033 } else {
1034 f->right = (p[0] & 0x20) >> 5;
1035 f->middle = (p[0] & 0x10) >> 4;
1036 }
1037
Hans de Goede7091c442014-12-18 09:53:34 -08001038 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1039 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1040 f->mt[0].x = f->mt[1].x;
1041 f->mt[0].y = f->mt[1].y;
1042 f->mt[1].x = 0;
1043 f->mt[1].y = 0;
1044 }
1045
Yunkang Tang38088432014-07-26 13:51:41 -07001046 return 0;
1047}
1048
1049static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1050{
1051 struct alps_data *priv = psmouse->private;
1052 unsigned char *packet = psmouse->packet;
1053 struct input_dev *dev2 = priv->dev2;
1054 int x, y, z, left, right, middle;
1055
Pali Rohár34412ba2015-01-09 12:48:58 -08001056 /* It should be a DualPoint when received trackstick packet */
1057 if (!(priv->flags & ALPS_DUALPOINT)) {
1058 psmouse_warn(psmouse,
1059 "Rejected trackstick packet from non DualPoint device");
1060 return;
1061 }
1062
Yunkang Tang38088432014-07-26 13:51:41 -07001063 /*
1064 * b7 b6 b5 b4 b3 b2 b1 b0
1065 * Byte0 0 1 0 0 1 0 0 0
1066 * Byte1 1 1 * * 1 M R L
1067 * Byte2 X7 1 X5 X4 X3 X2 X1 X0
1068 * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
1069 * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
1070 * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
1071 * M / R / L: Middle / Right / Left button
1072 */
1073
1074 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1075 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1076 ((packet[3] & 0x20) << 1);
1077 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1078
1079 left = (packet[1] & 0x01);
1080 right = (packet[1] & 0x02) >> 1;
1081 middle = (packet[1] & 0x04) >> 2;
1082
1083 /* Divide 2 since trackpoint's speed is too fast */
1084 input_report_rel(dev2, REL_X, (char)x / 2);
1085 input_report_rel(dev2, REL_Y, -((char)y / 2));
1086
1087 input_report_key(dev2, BTN_LEFT, left);
1088 input_report_key(dev2, BTN_RIGHT, right);
1089 input_report_key(dev2, BTN_MIDDLE, middle);
1090
1091 input_sync(dev2);
1092}
1093
1094static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1095{
1096 struct alps_data *priv = psmouse->private;
1097 struct input_dev *dev = psmouse->dev;
1098 struct alps_fields *f = &priv->f;
1099
1100 memset(f, 0, sizeof(*f));
1101
1102 if (priv->decode_fields(f, psmouse->packet, psmouse))
1103 return;
1104
1105 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1106
1107 input_mt_report_finger_count(dev, f->fingers);
1108
1109 input_report_key(dev, BTN_LEFT, f->left);
1110 input_report_key(dev, BTN_RIGHT, f->right);
1111 input_report_key(dev, BTN_MIDDLE, f->middle);
1112
1113 input_sync(dev);
1114}
1115
1116static void alps_process_packet_v7(struct psmouse *psmouse)
1117{
1118 unsigned char *packet = psmouse->packet;
1119
1120 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1121 alps_process_trackstick_packet_v7(psmouse);
1122 else
1123 alps_process_touchpad_packet_v7(psmouse);
1124}
1125
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001126static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1127 unsigned char packet[],
1128 bool report_buttons)
1129{
1130 struct alps_data *priv = psmouse->private;
1131 struct input_dev *dev2 = priv->dev2;
1132
1133 if (report_buttons)
1134 alps_report_buttons(psmouse, dev2, psmouse->dev,
1135 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1136
1137 input_report_rel(dev2, REL_X,
1138 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1139 input_report_rel(dev2, REL_Y,
1140 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1141
1142 input_sync(dev2);
1143}
1144
1145static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct alps_data *priv = psmouse->private;
1148
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001149 if (psmouse->pktcnt < 6)
1150 return PSMOUSE_GOOD_DATA;
1151
1152 if (psmouse->pktcnt == 6) {
1153 /*
1154 * Start a timer to flush the packet if it ends up last
1155 * 6-byte packet in the stream. Timer needs to fire
1156 * psmouse core times out itself. 20 ms should be enough
1157 * to decide if we are getting more data or not.
1158 */
1159 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1160 return PSMOUSE_GOOD_DATA;
1161 }
1162
1163 del_timer(&priv->timer);
1164
1165 if (psmouse->packet[6] & 0x80) {
1166
1167 /*
1168 * Highest bit is set - that means we either had
1169 * complete ALPS packet and this is start of the
1170 * next packet or we got garbage.
1171 */
1172
1173 if (((psmouse->packet[3] |
1174 psmouse->packet[4] |
1175 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001176 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001177 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001178 "refusing packet %4ph (suspected interleaved ps/2)\n",
1179 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001180 return PSMOUSE_BAD_DATA;
1181 }
1182
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001183 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001184
1185 /* Continue with the next packet */
1186 psmouse->packet[0] = psmouse->packet[6];
1187 psmouse->pktcnt = 1;
1188
1189 } else {
1190
1191 /*
1192 * High bit is 0 - that means that we indeed got a PS/2
1193 * packet in the middle of ALPS packet.
1194 *
1195 * There is also possibility that we got 6-byte ALPS
1196 * packet followed by 3-byte packet from trackpoint. We
1197 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001198 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001199 * normal operation (user would need to press all
1200 * buttons on the pad and start moving trackpoint
1201 * without touching the pad surface) we assume former.
1202 * Even if we are wrong the wost thing that would happen
1203 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001204 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001205 */
1206
1207 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1208 false);
1209
1210 /*
1211 * Continue with the standard ALPS protocol handling,
1212 * but make sure we won't process it as an interleaved
1213 * packet again, which may happen if all buttons are
1214 * pressed. To avoid this let's reset the 4th bit which
1215 * is normally 1.
1216 */
1217 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1218 psmouse->pktcnt = 4;
1219 }
1220
1221 return PSMOUSE_GOOD_DATA;
1222}
1223
1224static void alps_flush_packet(unsigned long data)
1225{
1226 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001227 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001228
1229 serio_pause_rx(psmouse->ps2dev.serio);
1230
Seth Forsheeb46615f2011-11-07 19:53:30 -08001231 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001232
1233 /*
1234 * We did not any more data in reasonable amount of time.
1235 * Validate the last 3 bytes and process as a standard
1236 * ALPS packet.
1237 */
1238 if ((psmouse->packet[3] |
1239 psmouse->packet[4] |
1240 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001241 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001242 "refusing packet %3ph (suspected interleaved ps/2)\n",
1243 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001244 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001245 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001246 }
1247 psmouse->pktcnt = 0;
1248 }
1249
1250 serio_continue_rx(psmouse->ps2dev.serio);
1251}
1252
1253static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1254{
1255 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001256
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001257 /*
1258 * Check if we are dealing with a bare PS/2 packet, presumably from
1259 * a device connected to the external PS/2 port. Because bare PS/2
1260 * protocol does not have enough constant bits to self-synchronize
1261 * properly we only do this if the device is fully synchronized.
1262 */
1263 if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (psmouse->pktcnt == 3) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001265 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1266 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return PSMOUSE_FULL_PACKET;
1268 }
1269 return PSMOUSE_GOOD_DATA;
1270 }
1271
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001272 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1273
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001274 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001275 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1276 return alps_handle_interleaved_ps2(psmouse);
1277 }
1278
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001279 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001280 psmouse_dbg(psmouse,
1281 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001282 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Seth Forsheeb46615f2011-11-07 19:53:30 -08001286 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001287 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001288 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001289 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001290 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1291 psmouse->pktcnt - 1,
1292 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001293
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001294 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001295 psmouse->pktcnt == psmouse->pktsize) {
1296 /*
1297 * Some Dell boxes, such as Latitude E6440 or E7440
1298 * with closed lid, quite often smash last byte of
1299 * otherwise valid packet with 0xff. Given that the
1300 * next packet is very likely to be valid let's
1301 * report PSMOUSE_FULL_PACKET but not process data,
1302 * rather than reporting PSMOUSE_BAD_DATA and
1303 * filling the logs.
1304 */
1305 return PSMOUSE_FULL_PACKET;
1306 }
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Yunkang Tang38088432014-07-26 13:51:41 -07001311 if (priv->proto_version == ALPS_PROTO_V7 &&
1312 !alps_is_valid_package_v7(psmouse)) {
1313 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1314 psmouse->pktcnt - 1,
1315 psmouse->packet[psmouse->pktcnt - 1]);
1316 return PSMOUSE_BAD_DATA;
1317 }
1318
Seth Forsheeb46615f2011-11-07 19:53:30 -08001319 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001320 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return PSMOUSE_FULL_PACKET;
1322 }
1323
1324 return PSMOUSE_GOOD_DATA;
1325}
1326
Seth Forshee25bded72011-11-07 19:53:36 -08001327static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1328{
1329 struct ps2dev *ps2dev = &psmouse->ps2dev;
1330 struct alps_data *priv = psmouse->private;
1331 int command;
1332 unsigned char *param;
1333 unsigned char dummy[4];
1334
1335 BUG_ON(nibble > 0xf);
1336
1337 command = priv->nibble_commands[nibble].command;
1338 param = (command & 0x0f00) ?
1339 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1340
1341 if (ps2_command(ps2dev, param, command))
1342 return -1;
1343
1344 return 0;
1345}
1346
1347static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1348{
1349 struct ps2dev *ps2dev = &psmouse->ps2dev;
1350 struct alps_data *priv = psmouse->private;
1351 int i, nibble;
1352
1353 if (ps2_command(ps2dev, NULL, priv->addr_command))
1354 return -1;
1355
1356 for (i = 12; i >= 0; i -= 4) {
1357 nibble = (addr >> i) & 0xf;
1358 if (alps_command_mode_send_nibble(psmouse, nibble))
1359 return -1;
1360 }
1361
1362 return 0;
1363}
1364
1365static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1366{
1367 struct ps2dev *ps2dev = &psmouse->ps2dev;
1368 unsigned char param[4];
1369
1370 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1371 return -1;
1372
1373 /*
1374 * The address being read is returned in the first two bytes
1375 * of the result. Check that this address matches the expected
1376 * address.
1377 */
1378 if (addr != ((param[0] << 8) | param[1]))
1379 return -1;
1380
1381 return param[2];
1382}
1383
1384static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1385{
1386 if (alps_command_mode_set_addr(psmouse, addr))
1387 return -1;
1388 return __alps_command_mode_read_reg(psmouse, addr);
1389}
1390
1391static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1392{
1393 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1394 return -1;
1395 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1396 return -1;
1397 return 0;
1398}
1399
1400static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1401 u8 value)
1402{
1403 if (alps_command_mode_set_addr(psmouse, addr))
1404 return -1;
1405 return __alps_command_mode_write_reg(psmouse, value);
1406}
1407
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001408static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1409 int repeated_command, unsigned char *param)
1410{
1411 struct ps2dev *ps2dev = &psmouse->ps2dev;
1412
1413 param[0] = 0;
1414 if (init_command && ps2_command(ps2dev, param, init_command))
1415 return -EIO;
1416
1417 if (ps2_command(ps2dev, NULL, repeated_command) ||
1418 ps2_command(ps2dev, NULL, repeated_command) ||
1419 ps2_command(ps2dev, NULL, repeated_command))
1420 return -EIO;
1421
1422 param[0] = param[1] = param[2] = 0xff;
1423 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1424 return -EIO;
1425
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001426 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1427 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001428 return 0;
1429}
1430
Yunkang Tang38088432014-07-26 13:51:41 -07001431static bool alps_check_valid_firmware_id(unsigned char id[])
1432{
1433 if (id[0] == 0x73)
1434 return true;
1435
1436 if (id[0] == 0x88 &&
1437 (id[1] == 0x07 ||
1438 id[1] == 0x08 ||
1439 (id[1] & 0xf0) == 0xb0 ||
1440 (id[1] & 0xf0) == 0xc0)) {
1441 return true;
1442 }
1443
1444 return false;
1445}
1446
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001447static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001448{
1449 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001450
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001451 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001452 psmouse_err(psmouse, "failed to enter command mode\n");
1453 return -1;
1454 }
1455
Yunkang Tang38088432014-07-26 13:51:41 -07001456 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001457 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001458 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001459 return -1;
1460 }
Seth Forshee25bded72011-11-07 19:53:36 -08001461 return 0;
1462}
1463
1464static inline int alps_exit_command_mode(struct psmouse *psmouse)
1465{
1466 struct ps2dev *ps2dev = &psmouse->ps2dev;
1467 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1468 return -1;
1469 return 0;
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/*
1473 * For DualPoint devices select the device that should respond to
1474 * subsequent commands. It looks like glidepad is behind stickpointer,
1475 * I'd thought it would be other way around...
1476 */
Seth Forshee25bded72011-11-07 19:53:36 -08001477static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1481
1482 if (ps2_command(ps2dev, NULL, cmd) ||
1483 ps2_command(ps2dev, NULL, cmd) ||
1484 ps2_command(ps2dev, NULL, cmd) ||
1485 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1486 return -1;
1487
1488 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001489 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 return 0;
1492}
1493
Seth Forshee25bded72011-11-07 19:53:36 -08001494static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 struct ps2dev *ps2dev = &psmouse->ps2dev;
1497
1498 /* Try ALPS magic knock - 4 disable before enable */
1499 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1500 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1501 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1502 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1503 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1504 return -1;
1505
1506 /*
1507 * Switch mouse to poll (remote) mode so motion data will not
1508 * get in our way
1509 */
1510 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1511}
1512
Yunkang Tang95f75e92013-12-01 22:33:52 -08001513static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1514{
1515 int i, nibble;
1516
1517 /*
1518 * b0-b11 are valid bits, send sequence is inverse.
1519 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1520 */
1521 for (i = 0; i <= 8; i += 4) {
1522 nibble = (word >> i) & 0xf;
1523 if (alps_command_mode_send_nibble(psmouse, nibble))
1524 return -1;
1525 }
1526
1527 return 0;
1528}
1529
1530static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1531 u16 addr, u16 value)
1532{
1533 struct ps2dev *ps2dev = &psmouse->ps2dev;
1534
1535 /* 0x0A0 is the command to write the word */
1536 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1537 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1538 alps_monitor_mode_send_word(psmouse, addr) ||
1539 alps_monitor_mode_send_word(psmouse, value) ||
1540 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1541 return -1;
1542
1543 return 0;
1544}
1545
1546static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1547{
1548 struct ps2dev *ps2dev = &psmouse->ps2dev;
1549
1550 if (enable) {
1551 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1552 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1553 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1554 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1555 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1556 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1557 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1558 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1559 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1560 return -1;
1561 } else {
1562 /* EC to exit monitor mode */
1563 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1564 return -1;
1565 }
1566
1567 return 0;
1568}
1569
1570static int alps_absolute_mode_v6(struct psmouse *psmouse)
1571{
1572 u16 reg_val = 0x181;
1573 int ret = -1;
1574
1575 /* enter monitor mode, to write the register */
1576 if (alps_monitor_mode(psmouse, true))
1577 return -1;
1578
1579 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1580
1581 if (alps_monitor_mode(psmouse, false))
1582 ret = -1;
1583
1584 return ret;
1585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587static int alps_get_status(struct psmouse *psmouse, char *param)
1588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001590 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return -1;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return 0;
1594}
1595
1596/*
1597 * Turn touchpad tapping on or off. The sequences are:
1598 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1599 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1600 * My guess that 0xE9 (GetInfo) is here as a sync point.
1601 * For models that also have stickpointer (DualPoints) its tapping
1602 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1603 * we don't fiddle with it.
1604 */
1605static int alps_tap_mode(struct psmouse *psmouse, int enable)
1606{
1607 struct ps2dev *ps2dev = &psmouse->ps2dev;
1608 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1609 unsigned char tap_arg = enable ? 0x0A : 0x00;
1610 unsigned char param[4];
1611
1612 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1613 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1614 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1615 ps2_command(ps2dev, &tap_arg, cmd))
1616 return -1;
1617
1618 if (alps_get_status(psmouse, param))
1619 return -1;
1620
1621 return 0;
1622}
1623
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001624/*
1625 * alps_poll() - poll the touchpad for current motion packet.
1626 * Used in resync.
1627 */
1628static int alps_poll(struct psmouse *psmouse)
1629{
1630 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001631 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001632 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001633
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001634 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001635 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001636
1637 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1638 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1639
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001640 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001641 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001642
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001643 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001644 return -1;
1645
1646 if ((psmouse->badbyte & 0xc8) == 0x08) {
1647/*
1648 * Poll the track stick ...
1649 */
1650 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1651 return -1;
1652 }
1653
1654 memcpy(psmouse->packet, buf, sizeof(buf));
1655 return 0;
1656}
1657
Seth Forshee25bded72011-11-07 19:53:36 -08001658static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001662 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001663 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001667 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001668 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Seth Forshee25bded72011-11-07 19:53:36 -08001672 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001673 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return -1;
1675 }
1676
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001677 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001678 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001682 /* ALPS needs stream mode, otherwise it won't report any data */
1683 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001684 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001685 return -1;
1686 }
1687
1688 return 0;
1689}
1690
Yunkang Tang95f75e92013-12-01 22:33:52 -08001691static int alps_hw_init_v6(struct psmouse *psmouse)
1692{
1693 unsigned char param[2] = {0xC8, 0x14};
1694
1695 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1696 if (alps_passthrough_mode_v2(psmouse, true))
1697 return -1;
1698
1699 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1700 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1701 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1702 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1703 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1704 return -1;
1705
1706 if (alps_passthrough_mode_v2(psmouse, false))
1707 return -1;
1708
1709 if (alps_absolute_mode_v6(psmouse)) {
1710 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1711 return -1;
1712 }
1713
1714 return 0;
1715}
1716
Seth Forshee25bded72011-11-07 19:53:36 -08001717/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001718 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001719 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001720static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1721 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001722{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001723 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001724
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001725 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001726 return -1;
1727
Kevin Cernekeecd401202013-02-13 22:28:07 -08001728 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1729 if (reg_val == -1)
1730 goto error;
1731
Seth Forshee25bded72011-11-07 19:53:36 -08001732 if (enable)
1733 reg_val |= 0x01;
1734 else
1735 reg_val &= ~0x01;
1736
Kevin Cernekeecd401202013-02-13 22:28:07 -08001737 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001738
Kevin Cernekeecd401202013-02-13 22:28:07 -08001739error:
1740 if (alps_exit_command_mode(psmouse))
1741 ret = -1;
1742 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001743}
1744
1745/* Must be in command mode when calling this function */
1746static int alps_absolute_mode_v3(struct psmouse *psmouse)
1747{
1748 int reg_val;
1749
1750 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1751 if (reg_val == -1)
1752 return -1;
1753
1754 reg_val |= 0x06;
1755 if (__alps_command_mode_write_reg(psmouse, reg_val))
1756 return -1;
1757
1758 return 0;
1759}
1760
Kevin Cernekeecd401202013-02-13 22:28:07 -08001761static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1762{
1763 int ret = -EIO, reg_val;
1764
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001765 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001766 goto error;
1767
1768 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1769 if (reg_val == -1)
1770 goto error;
1771
1772 /* bit 7: trackstick is present */
1773 ret = reg_val & 0x80 ? 0 : -ENODEV;
1774
1775error:
1776 alps_exit_command_mode(psmouse);
1777 return ret;
1778}
1779
1780static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1781{
1782 struct ps2dev *ps2dev = &psmouse->ps2dev;
1783 int ret = 0;
1784 unsigned char param[4];
1785
1786 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1787 return -EIO;
1788
1789 /*
1790 * E7 report for the trackstick
1791 *
1792 * There have been reports of failures to seem to trace back
1793 * to the above trackstick check failing. When these occur
1794 * this E7 report fails, so when that happens we continue
1795 * with the assumption that there isn't a trackstick after
1796 * all.
1797 */
1798 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1799 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1800 ret = -ENODEV;
1801 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001802 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001803
1804 /*
1805 * Not sure what this does, but it is absolutely
1806 * essential. Without it, the touchpad does not
1807 * work at all and the trackstick just emits normal
1808 * PS/2 packets.
1809 */
1810 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1811 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1812 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1813 alps_command_mode_send_nibble(psmouse, 0x9) ||
1814 alps_command_mode_send_nibble(psmouse, 0x4)) {
1815 psmouse_err(psmouse,
1816 "Error sending magic E6 sequence\n");
1817 ret = -EIO;
1818 goto error;
1819 }
1820
1821 /*
1822 * This ensures the trackstick packets are in the format
1823 * supported by this driver. If bit 1 isn't set the packet
1824 * format is different.
1825 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001826 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001827 alps_command_mode_write_reg(psmouse,
1828 reg_base + 0x08, 0x82) ||
1829 alps_exit_command_mode(psmouse))
1830 ret = -EIO;
1831 }
1832
1833error:
1834 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1835 ret = -EIO;
1836
1837 return ret;
1838}
1839
Seth Forshee25bded72011-11-07 19:53:36 -08001840static int alps_hw_init_v3(struct psmouse *psmouse)
1841{
Seth Forshee25bded72011-11-07 19:53:36 -08001842 struct ps2dev *ps2dev = &psmouse->ps2dev;
1843 int reg_val;
1844 unsigned char param[4];
1845
Kevin Cernekeecd401202013-02-13 22:28:07 -08001846 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1847 if (reg_val == -EIO)
1848 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001849
Kevin Cernekeecd401202013-02-13 22:28:07 -08001850 if (reg_val == 0 &&
1851 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001852 goto error;
1853
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001854 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001855 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001856 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1857 goto error;
1858 }
1859
1860 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1861 if (reg_val == -1)
1862 goto error;
1863 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1864 goto error;
1865
1866 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1867 if (reg_val == -1)
1868 goto error;
1869 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1870 goto error;
1871
1872 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1873 goto error;
1874 if (__alps_command_mode_write_reg(psmouse, 0x04))
1875 goto error;
1876
1877 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1878 goto error;
1879 if (__alps_command_mode_write_reg(psmouse, 0x03))
1880 goto error;
1881
1882 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1883 goto error;
1884 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1885 goto error;
1886
1887 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1888 goto error;
1889 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1890 goto error;
1891
Seth Forshee25bded72011-11-07 19:53:36 -08001892 alps_exit_command_mode(psmouse);
1893
1894 /* Set rate and enable data reporting */
1895 param[0] = 0x64;
1896 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1897 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1898 psmouse_err(psmouse, "Failed to enable data reporting\n");
1899 return -1;
1900 }
1901
1902 return 0;
1903
Seth Forshee25bded72011-11-07 19:53:36 -08001904error:
1905 /*
1906 * Leaving the touchpad in command mode will essentially render
1907 * it unusable until the machine reboots, so exit it here just
1908 * to be safe
1909 */
1910 alps_exit_command_mode(psmouse);
1911 return -1;
1912}
1913
Hans de Goedef3f33c62014-07-29 11:22:07 -07001914static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1915{
1916 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1917 struct alps_data *priv = psmouse->private;
1918
1919 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1920 if (reg < 0)
1921 return reg;
1922
1923 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1924 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1925
1926 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1927 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1928
1929 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1930 if (reg < 0)
1931 return reg;
1932
1933 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1934 x_electrode = 17 + x_electrode;
1935
1936 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1937 y_electrode = 13 + y_electrode;
1938
1939 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1940 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1941
1942 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1943 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1944
1945 psmouse_dbg(psmouse,
1946 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1947 x_pitch, y_pitch, x_electrode, y_electrode,
1948 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1949
1950 return 0;
1951}
1952
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001953static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1954{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001955 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001956 struct ps2dev *ps2dev = &psmouse->ps2dev;
1957 int reg_val, ret = -1;
1958
Kevin Cernekeecd401202013-02-13 22:28:07 -08001959 if (priv->flags & ALPS_DUALPOINT) {
1960 reg_val = alps_setup_trackstick_v3(psmouse,
1961 ALPS_REG_BASE_RUSHMORE);
1962 if (reg_val == -EIO)
1963 goto error;
1964 if (reg_val == -ENODEV)
1965 priv->flags &= ~ALPS_DUALPOINT;
1966 }
1967
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001968 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001969 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1970 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1971 goto error;
1972
Hans de Goedef3f33c62014-07-29 11:22:07 -07001973 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1974 goto error;
1975
Kevin Cernekee1302bac2013-02-13 22:27:08 -08001976 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1977 if (reg_val == -1)
1978 goto error;
1979 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1980 goto error;
1981
1982 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1983 goto error;
1984
1985 /* enter absolute mode */
1986 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1987 if (reg_val == -1)
1988 goto error;
1989 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1990 goto error;
1991
1992 alps_exit_command_mode(psmouse);
1993 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1994
1995error:
1996 alps_exit_command_mode(psmouse);
1997 return ret;
1998}
1999
Seth Forshee25bded72011-11-07 19:53:36 -08002000/* Must be in command mode when calling this function */
2001static int alps_absolute_mode_v4(struct psmouse *psmouse)
2002{
2003 int reg_val;
2004
2005 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2006 if (reg_val == -1)
2007 return -1;
2008
2009 reg_val |= 0x02;
2010 if (__alps_command_mode_write_reg(psmouse, reg_val))
2011 return -1;
2012
2013 return 0;
2014}
2015
2016static int alps_hw_init_v4(struct psmouse *psmouse)
2017{
Seth Forshee25bded72011-11-07 19:53:36 -08002018 struct ps2dev *ps2dev = &psmouse->ps2dev;
2019 unsigned char param[4];
2020
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002021 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002022 goto error;
2023
2024 if (alps_absolute_mode_v4(psmouse)) {
2025 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2026 goto error;
2027 }
2028
2029 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2030 goto error;
2031
2032 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2033 goto error;
2034
2035 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2036 goto error;
2037
2038 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2039 goto error;
2040
2041 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2042 goto error;
2043
2044 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2045 goto error;
2046
2047 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2048 goto error;
2049
2050 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2051 goto error;
2052
2053 alps_exit_command_mode(psmouse);
2054
2055 /*
2056 * This sequence changes the output from a 9-byte to an
2057 * 8-byte format. All the same data seems to be present,
2058 * just in a more compact format.
2059 */
2060 param[0] = 0xc8;
2061 param[1] = 0x64;
2062 param[2] = 0x50;
2063 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2064 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2065 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2066 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2067 return -1;
2068
2069 /* Set rate and enable data reporting */
2070 param[0] = 0x64;
2071 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2072 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2073 psmouse_err(psmouse, "Failed to enable data reporting\n");
2074 return -1;
2075 }
2076
2077 return 0;
2078
2079error:
2080 /*
2081 * Leaving the touchpad in command mode will essentially render
2082 * it unusable until the machine reboots, so exit it here just
2083 * to be safe
2084 */
2085 alps_exit_command_mode(psmouse);
2086 return -1;
2087}
2088
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002089static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2090 struct alps_data *priv)
2091{
2092 struct ps2dev *ps2dev = &psmouse->ps2dev;
2093 unsigned char param[4] = {0};
2094 int num_x_electrode, num_y_electrode;
2095
2096 if (alps_enter_command_mode(psmouse))
2097 return -1;
2098
2099 param[0] = 0x0a;
2100 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2101 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2102 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2103 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2104 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2105 return -1;
2106
2107 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2108 return -1;
2109
2110 /*
2111 * Dolphin's sensor line number is not fixed. It can be calculated
2112 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2113 * Further more, we can get device's x_max and y_max by multiplying
2114 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2115 *
2116 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2117 * real sensor line number X = 11 + 8 = 19, and
2118 * real sensor line number Y = 8 + 1 = 9.
2119 * So, x_max = (19 - 1) * 64 = 1152, and
2120 * y_max = (9 - 1) * 64 = 512.
2121 */
2122 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2123 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2124 priv->x_bits = num_x_electrode;
2125 priv->y_bits = num_y_electrode;
2126 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2127 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2128
2129 if (alps_exit_command_mode(psmouse))
2130 return -1;
2131
2132 return 0;
2133}
2134
Dave Turvene75af9e52013-02-21 22:58:28 -08002135static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2136{
2137 struct ps2dev *ps2dev = &psmouse->ps2dev;
2138 unsigned char param[2];
2139
2140 /* This is dolphin "v1" as empirically defined by florin9doi */
2141 param[0] = 0x64;
2142 param[1] = 0x28;
2143
2144 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2145 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2146 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2147 return -1;
2148
2149 return 0;
2150}
2151
Yunkang Tang38088432014-07-26 13:51:41 -07002152static int alps_hw_init_v7(struct psmouse *psmouse)
2153{
2154 struct ps2dev *ps2dev = &psmouse->ps2dev;
2155 int reg_val, ret = -1;
2156
2157 if (alps_enter_command_mode(psmouse) ||
2158 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2159 goto error;
2160
Hans de Goedef3f33c62014-07-29 11:22:07 -07002161 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2162 goto error;
2163
Yunkang Tang38088432014-07-26 13:51:41 -07002164 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2165 goto error;
2166
2167 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2168 if (reg_val == -1)
2169 goto error;
2170 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2171 goto error;
2172
2173 alps_exit_command_mode(psmouse);
2174 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2175
2176error:
2177 alps_exit_command_mode(psmouse);
2178 return ret;
2179}
2180
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002181static int alps_set_protocol(struct psmouse *psmouse,
2182 struct alps_data *priv,
2183 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002184{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002185 psmouse->private = priv;
2186
2187 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2188
2189 priv->proto_version = protocol->version;
2190 priv->byte0 = protocol->byte0;
2191 priv->mask0 = protocol->mask0;
2192 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002193
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002194 priv->x_max = 2000;
2195 priv->y_max = 1400;
2196 priv->x_bits = 15;
2197 priv->y_bits = 11;
2198
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002199 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002200 case ALPS_PROTO_V1:
2201 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002202 priv->hw_init = alps_hw_init_v1_v2;
2203 priv->process_packet = alps_process_packet_v1_v2;
2204 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002205 priv->x_max = 1023;
2206 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002207 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002208
Seth Forshee25bded72011-11-07 19:53:36 -08002209 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002210 priv->hw_init = alps_hw_init_v3;
2211 priv->process_packet = alps_process_packet_v3;
2212 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002213 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002214 priv->nibble_commands = alps_v3_nibble_commands;
2215 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002216 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002217
2218 case ALPS_PROTO_V3_RUSHMORE:
2219 priv->hw_init = alps_hw_init_rushmore_v3;
2220 priv->process_packet = alps_process_packet_v3;
2221 priv->set_abs_params = alps_set_abs_params_mt;
2222 priv->decode_fields = alps_decode_rushmore;
2223 priv->nibble_commands = alps_v3_nibble_commands;
2224 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2225 priv->x_bits = 16;
2226 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002227
2228 if (alps_probe_trackstick_v3(psmouse,
2229 ALPS_REG_BASE_RUSHMORE) < 0)
2230 priv->flags &= ~ALPS_DUALPOINT;
2231
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002232 break;
2233
Seth Forshee25bded72011-11-07 19:53:36 -08002234 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002235 priv->hw_init = alps_hw_init_v4;
2236 priv->process_packet = alps_process_packet_v4;
2237 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002238 priv->nibble_commands = alps_v4_nibble_commands;
2239 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002240 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002241
Dave Turvene75af9e52013-02-21 22:58:28 -08002242 case ALPS_PROTO_V5:
2243 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002244 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002245 priv->decode_fields = alps_decode_dolphin;
2246 priv->set_abs_params = alps_set_abs_params_mt;
2247 priv->nibble_commands = alps_v3_nibble_commands;
2248 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002249 priv->x_max = 1360;
2250 priv->y_max = 660;
2251 priv->x_bits = 23;
2252 priv->y_bits = 12;
2253 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002254
Yunkang Tang95f75e92013-12-01 22:33:52 -08002255 case ALPS_PROTO_V6:
2256 priv->hw_init = alps_hw_init_v6;
2257 priv->process_packet = alps_process_packet_v6;
2258 priv->set_abs_params = alps_set_abs_params_st;
2259 priv->nibble_commands = alps_v6_nibble_commands;
2260 priv->x_max = 2047;
2261 priv->y_max = 1535;
2262 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002263
Yunkang Tang38088432014-07-26 13:51:41 -07002264 case ALPS_PROTO_V7:
2265 priv->hw_init = alps_hw_init_v7;
2266 priv->process_packet = alps_process_packet_v7;
2267 priv->decode_fields = alps_decode_packet_v7;
2268 priv->set_abs_params = alps_set_abs_params_mt;
2269 priv->nibble_commands = alps_v3_nibble_commands;
2270 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002271
2272 if (alps_dolphin_get_device_area(psmouse, priv))
2273 return -EIO;
Yunkang Tang38088432014-07-26 13:51:41 -07002274
2275 if (priv->fw_ver[1] != 0xba)
2276 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002277
Yunkang Tang38088432014-07-26 13:51:41 -07002278 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002279 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002280
2281 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002282}
2283
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002284static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2285 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002286{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002287 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002288 int i;
2289
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002290 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2291 model = &alps_model_data[i];
2292
2293 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2294 (!model->command_mode_resp ||
2295 model->command_mode_resp == ec[2])) {
2296
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002297 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002298 }
2299 }
2300
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002301 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002302}
2303
2304static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2305{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002306 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002307 unsigned char e6[4], e7[4], ec[4];
2308
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002309 /*
2310 * First try "E6 report".
2311 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2312 * The bits 0-2 of the first byte will be 1s if some buttons are
2313 * pressed.
2314 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002315 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2316 PSMOUSE_CMD_SETSCALE11, e6))
2317 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002318
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002319 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2320 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002321
2322 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002323 * Now get the "E7" and "EC" reports. These will uniquely identify
2324 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002325 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002326 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2327 PSMOUSE_CMD_SETSCALE21, e7) ||
2328 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2329 PSMOUSE_CMD_RESET_WRAP, ec) ||
2330 alps_exit_command_mode(psmouse))
2331 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002332
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002333 protocol = alps_match_table(e7, ec);
2334 if (!protocol) {
2335 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2336 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2337 protocol = &alps_v5_protocol_data;
2338 } else if (ec[0] == 0x88 &&
2339 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2340 protocol = &alps_v7_protocol_data;
2341 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2342 protocol = &alps_v3_rushmore_data;
2343 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2344 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2345 protocol = &alps_v3_protocol_data;
2346 } else {
2347 psmouse_dbg(psmouse,
2348 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2349 return -EINVAL;
2350 }
2351 }
2352
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002353 /* Save the Firmware version */
2354 memcpy(priv->fw_ver, ec, 3);
2355
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002356 return alps_set_protocol(psmouse, priv, protocol);
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002357}
2358
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002359static int alps_reconnect(struct psmouse *psmouse)
2360{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002361 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002362
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002363 psmouse_reset(psmouse);
2364
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002365 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002366 return -1;
2367
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002368 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
2371static void alps_disconnect(struct psmouse *psmouse)
2372{
2373 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002376 del_timer_sync(&priv->timer);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002377 input_unregister_device(priv->dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 kfree(priv);
2379}
2380
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002381static void alps_set_abs_params_st(struct alps_data *priv,
2382 struct input_dev *dev1)
2383{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002384 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2385 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002386}
2387
2388static void alps_set_abs_params_mt(struct alps_data *priv,
2389 struct input_dev *dev1)
2390{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002391 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2392 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002393
Hans de Goedef3f33c62014-07-29 11:22:07 -07002394 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2395 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2396
Hans de Goedecdf333e2014-07-25 22:44:42 -07002397 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2398 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2399
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002400 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2401 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Yunkang Tang38088432014-07-26 13:51:41 -07002402
2403 /* V7 is real multi-touch */
2404 if (priv->proto_version == ALPS_PROTO_V7)
2405 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002406}
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408int alps_init(struct psmouse *psmouse)
2409{
2410 struct alps_data *priv;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002411 struct input_dev *dev1 = psmouse->dev, *dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002413 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002414 dev2 = input_allocate_device();
2415 if (!priv || !dev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 goto init_fail;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002417
2418 priv->dev2 = dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Seth Forshee25bded72011-11-07 19:53:36 -08002420 psmouse_reset(psmouse);
2421
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002422 if (alps_identify(psmouse, priv) < 0)
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002423 goto init_fail;
2424
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002425 if (priv->hw_init(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 goto init_fail;
2427
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002428 /*
2429 * Undo part of setup done for us by psmouse core since touchpad
2430 * is not a relative device.
2431 */
2432 __clear_bit(EV_REL, dev1->evbit);
2433 __clear_bit(REL_X, dev1->relbit);
2434 __clear_bit(REL_Y, dev1->relbit);
2435
2436 /*
2437 * Now set up our capabilities.
2438 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002439 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2440 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2441 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002442 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2443 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002445 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002446
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002447 priv->set_abs_params(priv, dev1);
Yunkang Tang38088432014-07-26 13:51:41 -07002448 /* No pressure on V7 */
2449 if (priv->proto_version != ALPS_PROTO_V7)
2450 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002452 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002453 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2454 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002457 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002458 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2459 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
2461
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002462 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002463 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2464 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2465 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2466 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002467 } else if (priv->flags & ALPS_BUTTONPAD) {
2468 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2469 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002470 } else {
2471 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2472 }
2473
Pali Rohárdfba8602015-01-09 12:48:44 -08002474 if (priv->flags & ALPS_DUALPOINT) {
2475 /*
2476 * format of input device name is: "protocol vendor name"
2477 * see function psmouse_switch_protocol() in psmouse-base.c
2478 */
2479 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2480 dev2->id.product = PSMOUSE_ALPS;
2481 dev2->id.version = priv->proto_version;
2482 } else {
2483 dev2->name = "PS/2 ALPS Mouse";
2484 dev2->id.product = PSMOUSE_PS2;
2485 dev2->id.version = 0x0000;
2486 }
2487
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04002488 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002489 dev2->phys = priv->phys;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002490 dev2->id.bustype = BUS_I8042;
2491 dev2->id.vendor = 0x0002;
Dmitry Torokhov1db3a342008-03-18 00:29:18 -04002492 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002493
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002494 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002495 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2496 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2497 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Hans de Goede01d4cd52014-09-08 14:44:05 -07002499 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002500 if (priv->flags & ALPS_DUALPOINT)
2501 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2502
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002503 if (input_register_device(priv->dev2))
2504 goto init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
2506 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002507 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 psmouse->disconnect = alps_disconnect;
2509 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002510 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002512 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2513 psmouse->resync_time = 0;
2514
Pali Rohár9d720b32014-11-08 12:58:57 -08002515 /* Allow 2 invalid packets without resetting device */
2516 psmouse->resetafter = psmouse->pktsize * 2;
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return 0;
2519
2520init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002521 psmouse_reset(psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002522 input_free_device(dev2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 kfree(priv);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002524 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return -1;
2526}
2527
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002528int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002530 struct alps_data dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002532 if (alps_identify(psmouse, &dummy) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return -1;
2534
2535 if (set_properties) {
2536 psmouse->vendor = "ALPS";
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002537 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002538 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhovd7c13d32015-01-13 20:53:22 -08002539 psmouse->model = dummy.proto_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541 return 0;
2542}
2543