blob: da3af8db697cd61ab4ee0c4440f76b3168b2d759 [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 */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800120
121 /*
122 * XXX This entry is suspicious. First byte has zero lower nibble,
123 * which is what a normal mouse would report. Also, the value 0x0e
124 * isn't valid per PS/2 spec.
125 */
126 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
127
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800128 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
129 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800130 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800131 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
132 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
133 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
134 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
135 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
137 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800140static const struct alps_protocol_info alps_v3_protocol_data = {
141 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
142};
143
144static const struct alps_protocol_info alps_v3_rushmore_data = {
145 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
146};
147
148static const struct alps_protocol_info alps_v5_protocol_data = {
149 ALPS_PROTO_V5, 0xc8, 0xd8, 0
150};
151
152static const struct alps_protocol_info alps_v7_protocol_data = {
153 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
154};
155
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800156static void alps_set_abs_params_st(struct alps_data *priv,
157 struct input_dev *dev1);
158static void alps_set_abs_params_mt(struct alps_data *priv,
159 struct input_dev *dev1);
160
Seth Forsheed4b347b2011-11-07 19:53:15 -0800161/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800163static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800164 unsigned char data)
165{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800166 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800167}
168
Pali Rohár04aae282015-01-14 13:18:30 -0800169static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800170 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 */
Pali Rohár04aae282015-01-14 13:18:30 -0800179 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800180 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800181
Pali Rohár04aae282015-01-14 13:18:30 -0800182 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800183 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800184
Pali Rohár04aae282015-01-14 13:18:30 -0800185 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800186 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 */
Pali Rohár04aae282015-01-14 13:18:30 -0800192 if (dev2)
193 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800194}
195
Seth Forshee25bded72011-11-07 19:53:36 -0800196static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 struct alps_data *priv = psmouse->private;
199 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500200 struct input_dev *dev = psmouse->dev;
201 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500203 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800205 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400206 left = packet[2] & 0x10;
207 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 middle = 0;
209 x = packet[1] | ((packet[0] & 0x07) << 7);
210 y = packet[4] | ((packet[3] & 0x07) << 7);
211 z = packet[5];
212 } else {
213 left = packet[3] & 1;
214 right = packet[3] & 2;
215 middle = packet[3] & 4;
216 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
217 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
218 z = packet[5];
219 }
220
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800221 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400222 back = packet[0] & 0x10;
223 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500224 }
225
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800226 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500227 back = packet[3] & 4;
228 forward = packet[2] & 4;
229 if ((middle = forward && back))
230 forward = back = 0;
231 }
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ges = packet[2] & 1;
234 fin = packet[2] & 2;
235
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800236 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
238 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700239
Pali Rohár04aae282015-01-14 13:18:30 -0800240 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 input_sync(dev2);
243 return;
244 }
245
Hans de Goede92bac832015-04-12 15:42:35 -0700246 /* Non interleaved V2 dualpoint has separate stick button bits */
247 if (priv->proto_version == ALPS_PROTO_V2 &&
248 priv->flags == (ALPS_PASS | ALPS_DUALPOINT)) {
249 left |= packet[0] & 1;
250 right |= packet[0] & 2;
251 middle |= packet[0] & 4;
252 }
253
Pali Rohár04aae282015-01-14 13:18:30 -0800254 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800257 if (ges && !fin)
258 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * A "tap and drag" operation is reported by the hardware as a transition
262 * from (!fin && ges) to (fin && ges). This should be translated to the
263 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
264 */
265 if (ges && fin && !priv->prev_fin) {
266 input_report_abs(dev, ABS_X, x);
267 input_report_abs(dev, ABS_Y, y);
268 input_report_abs(dev, ABS_PRESSURE, 0);
269 input_report_key(dev, BTN_TOOL_FINGER, 0);
270 input_sync(dev);
271 }
272 priv->prev_fin = fin;
273
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800274 if (z > 30)
275 input_report_key(dev, BTN_TOUCH, 1);
276 if (z < 25)
277 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (z > 0) {
280 input_report_abs(dev, ABS_X, x);
281 input_report_abs(dev, ABS_Y, y);
282 }
283
284 input_report_abs(dev, ABS_PRESSURE, z);
285 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
286
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800287 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500288 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800290 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500291 input_report_key(dev, BTN_FORWARD, forward);
292 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800295 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800296 input_report_key(dev, BTN_0, packet[2] & 4);
297 input_report_key(dev, BTN_1, packet[0] & 0x10);
298 input_report_key(dev, BTN_2, packet[3] & 4);
299 input_report_key(dev, BTN_3, packet[0] & 0x20);
300 }
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 input_sync(dev);
303}
304
Seth Forshee01ce6612011-11-07 19:54:13 -0800305/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800306 * Process bitmap data for V5 protocols. Return value is null.
307 *
308 * The bitmaps don't have enough data to track fingers, so this function
309 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700310 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800311 */
312static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700313 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800314{
315 int box_middle_x, box_middle_y;
316 unsigned int x_map, y_map;
317 unsigned char start_bit, end_bit;
318 unsigned char x_msb, x_lsb, y_msb, y_lsb;
319
320 x_map = fields->x_map;
321 y_map = fields->y_map;
322
323 if (!x_map || !y_map)
324 return;
325
326 /* Get Most-significant and Least-significant bit */
327 x_msb = fls(x_map);
328 x_lsb = ffs(x_map);
329 y_msb = fls(y_map);
330 y_lsb = ffs(y_map);
331
332 /* Most-significant bit should never exceed max sensor line number */
333 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
334 return;
335
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800336 if (fields->fingers > 1) {
337 start_bit = priv->x_bits - x_msb;
338 end_bit = priv->x_bits - x_lsb;
339 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
340 (2 * (priv->x_bits - 1));
341
342 start_bit = y_lsb - 1;
343 end_bit = y_msb - 1;
344 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
345 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700346 fields->mt[0] = fields->st;
347 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
348 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800349 }
350}
351
Hans de Goede036e6c72014-07-25 22:38:51 -0700352static void alps_get_bitmap_points(unsigned int map,
353 struct alps_bitmap_point *low,
354 struct alps_bitmap_point *high,
355 int *fingers)
356{
357 struct alps_bitmap_point *point;
358 int i, bit, prev_bit = 0;
359
360 point = low;
361 for (i = 0; map != 0; i++, map >>= 1) {
362 bit = map & 1;
363 if (bit) {
364 if (!prev_bit) {
365 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700366 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700367 (*fingers)++;
368 }
369 point->num_bits++;
370 } else {
371 if (prev_bit)
372 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700373 }
374 prev_bit = bit;
375 }
376}
377
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800378/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800379 * Process bitmap data from v3 and v4 protocols. Returns the number of
380 * fingers detected. A return value of 0 means at least one of the
381 * bitmaps was empty.
382 *
383 * The bitmaps don't have enough data to track fingers, so this function
384 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700385 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800386 * is greater than 0.
387 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800388static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700389 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800390{
Hans de Goede036e6c72014-07-25 22:38:51 -0700391 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800392 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
393 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800394
Hans de Goede02d04252014-07-25 22:43:35 -0700395 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800396 return 0;
397
Hans de Goede02d04252014-07-25 22:43:35 -0700398 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
399 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800400
401 /*
402 * Fingers can overlap, so we use the maximum count of fingers
403 * on either axis as the finger count.
404 */
405 fingers = max(fingers_x, fingers_y);
406
407 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700408 * If an axis reports only a single contact, we have overlapping or
409 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800410 */
Hans de Goede20bea682014-07-25 22:33:33 -0700411 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700412 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700413 x_low.num_bits = x_low.num_bits - i;
414 x_high.start_bit = x_low.start_bit + i;
415 x_high.num_bits = max(i, 1);
416 }
417 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700418 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700419 y_low.num_bits = y_low.num_bits - i;
420 y_high.start_bit = y_low.start_bit + i;
421 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800422 }
423
Hans de Goede02d04252014-07-25 22:43:35 -0700424 fields->mt[0].x =
425 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
426 (2 * (priv->x_bits - 1));
427 fields->mt[0].y =
428 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
429 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800430
Hans de Goede02d04252014-07-25 22:43:35 -0700431 fields->mt[1].x =
432 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
433 (2 * (priv->x_bits - 1));
434 fields->mt[1].y =
435 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
436 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800437
Hans de Goede40e8f532014-07-25 22:37:15 -0700438 /* y-bitmap order is reversed, except on rushmore */
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800439 if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700440 fields->mt[0].y = priv->y_max - fields->mt[0].y;
441 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700442 }
443
Seth Forshee01ce6612011-11-07 19:54:13 -0800444 return fingers;
445}
446
Hans de Goedecdf333e2014-07-25 22:44:42 -0700447static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800448{
449 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700450 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
451 input_report_abs(dev, ABS_MT_POSITION_X, x);
452 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800453}
454
Hans de Goedecdf333e2014-07-25 22:44:42 -0700455static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800456{
Hans de Goede02d04252014-07-25 22:43:35 -0700457 struct alps_data *priv = psmouse->private;
458 struct input_dev *dev = psmouse->dev;
459 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700460 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700461
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800462 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700463 for (i = 0; i < n; i++)
464 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
465
466 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800467}
468
Hans de Goede68c21872014-07-25 22:47:25 -0700469static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
470{
471 struct alps_data *priv = psmouse->private;
472 struct input_dev *dev = psmouse->dev;
473 struct alps_fields *f = &priv->f;
474
475 /* Use st data when we don't have mt data */
476 if (fingers < 2) {
477 f->mt[0].x = f->st.x;
478 f->mt[0].y = f->st.y;
479 fingers = f->pressure > 0 ? 1 : 0;
480 }
481
Hans de Goede99d99962014-07-25 22:48:02 -0700482 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
Hans de Goede68c21872014-07-25 22:47:25 -0700483
484 input_mt_report_finger_count(dev, fingers);
485
486 input_report_key(dev, BTN_LEFT, f->left);
487 input_report_key(dev, BTN_RIGHT, f->right);
488 input_report_key(dev, BTN_MIDDLE, f->middle);
489
490 input_report_abs(dev, ABS_PRESSURE, f->pressure);
491
492 input_sync(dev);
493}
494
Seth Forshee25bded72011-11-07 19:53:36 -0800495static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
496{
497 struct alps_data *priv = psmouse->private;
498 unsigned char *packet = psmouse->packet;
499 struct input_dev *dev = priv->dev2;
500 int x, y, z, left, right, middle;
501
Pali Rohár34412ba2015-01-09 12:48:58 -0800502 /* It should be a DualPoint when received trackstick packet */
503 if (!(priv->flags & ALPS_DUALPOINT)) {
504 psmouse_warn(psmouse,
505 "Rejected trackstick packet from non DualPoint device");
506 return;
507 }
508
Seth Forshee25bded72011-11-07 19:53:36 -0800509 /* Sanity check packet */
510 if (!(packet[0] & 0x40)) {
511 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
512 return;
513 }
514
515 /*
516 * There's a special packet that seems to indicate the end
517 * of a stream of trackstick data. Filter these out.
518 */
519 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
520 return;
521
522 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
523 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
524 z = (packet[4] & 0x7c) >> 2;
525
526 /*
527 * The x and y values tend to be quite large, and when used
528 * alone the trackstick is difficult to use. Scale them down
529 * to compensate.
530 */
531 x /= 8;
532 y /= 8;
533
534 input_report_rel(dev, REL_X, x);
535 input_report_rel(dev, REL_Y, -y);
536
537 /*
538 * Most ALPS models report the trackstick buttons in the touchpad
539 * packets, but a few report them here. No reliable way has been
540 * found to differentiate between the models upfront, so we enable
541 * the quirk in response to seeing a button press in the trackstick
542 * packet.
543 */
544 left = packet[3] & 0x01;
545 right = packet[3] & 0x02;
546 middle = packet[3] & 0x04;
547
548 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
549 (left || right || middle))
550 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
551
552 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
553 input_report_key(dev, BTN_LEFT, left);
554 input_report_key(dev, BTN_RIGHT, right);
555 input_report_key(dev, BTN_MIDDLE, middle);
556 }
557
558 input_sync(dev);
559 return;
560}
561
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800562static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
563{
564 f->left = !!(p[3] & 0x01);
565 f->right = !!(p[3] & 0x02);
566 f->middle = !!(p[3] & 0x04);
567
568 f->ts_left = !!(p[3] & 0x10);
569 f->ts_right = !!(p[3] & 0x20);
570 f->ts_middle = !!(p[3] & 0x40);
571}
572
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700573static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800574 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800575{
576 f->first_mp = !!(p[4] & 0x40);
577 f->is_mp = !!(p[0] & 0x40);
578
579 f->fingers = (p[5] & 0x3) + 1;
580 f->x_map = ((p[4] & 0x7e) << 8) |
581 ((p[1] & 0x7f) << 2) |
582 ((p[0] & 0x30) >> 4);
583 f->y_map = ((p[3] & 0x70) << 4) |
584 ((p[2] & 0x7f) << 1) |
585 (p[4] & 0x01);
586
Hans de Goede02d04252014-07-25 22:43:35 -0700587 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800588 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700589 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
590 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800591
592 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700593
594 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800595}
596
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700597static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800598 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800599{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800600 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800601
Yunkang Tangf105e342014-07-25 22:29:24 -0700602 /* Rushmore's packet decode has a bit difference with Pinnacle's */
603 f->is_mp = !!(p[5] & 0x40);
604 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800605 f->x_map |= (p[5] & 0x10) << 11;
606 f->y_map |= (p[5] & 0x20) << 6;
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700607
608 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800609}
610
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700611static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800612 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800613{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800614 u64 palm_data = 0;
615 struct alps_data *priv = psmouse->private;
616
Dave Turvene75af9e52013-02-21 22:58:28 -0800617 f->first_mp = !!(p[0] & 0x02);
618 f->is_mp = !!(p[0] & 0x20);
619
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800620 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700621 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
622 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
623 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800624 alps_decode_buttons_v3(f, p);
625 } else {
626 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800627 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800628
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800629 palm_data = (p[1] & 0x7f) |
630 ((p[2] & 0x7f) << 7) |
631 ((p[4] & 0x7f) << 14) |
632 ((p[5] & 0x7f) << 21) |
633 ((p[3] & 0x07) << 28) |
634 (((u64)p[3] & 0x70) << 27) |
635 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800636
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800637 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
638 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
639
640 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
641 f->x_map = (palm_data >> priv->y_bits) &
642 (BIT(priv->x_bits) - 1);
643 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700644
645 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800646}
647
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800648static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800649{
650 struct alps_data *priv = psmouse->private;
651 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800652 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700653 struct alps_fields *f = &priv->f;
654 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800655
Hans de Goede02d04252014-07-25 22:43:35 -0700656 memset(f, 0, sizeof(*f));
657
658 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800659
660 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800661 * There's no single feature of touchpad position and bitmap packets
662 * that can be used to distinguish between them. We rely on the fact
663 * that a bitmap packet should always follow a position packet with
664 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800665 */
666 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800667 /*
668 * Sometimes a position packet will indicate a multi-packet
669 * sequence, but then what follows is another position
670 * packet. Check for this, and when it happens process the
671 * position packet as usual.
672 */
Hans de Goede02d04252014-07-25 22:43:35 -0700673 if (f->is_mp) {
674 fingers = f->fingers;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800675 if (priv->proto_version == ALPS_PROTO_V3 ||
676 priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700677 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700678 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800679
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800680 /* Now process position packet */
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 } else {
684 /*
685 * Because Dolphin uses position packet's
686 * coordinate data as Pt1 and uses it to
687 * calculate Pt2, so we need to do position
688 * packet decode first.
689 */
Hans de Goede02d04252014-07-25 22:43:35 -0700690 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800691 psmouse);
692
693 /*
694 * Since Dolphin's finger number is reliable,
695 * there is no need to compare with bmap_fn.
696 */
Hans de Goede02d04252014-07-25 22:43:35 -0700697 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800698 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800699 } else {
700 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800701 }
702 }
703
Seth Forshee01ce6612011-11-07 19:54:13 -0800704 /*
705 * Bit 6 of byte 0 is not usually set in position packets. The only
706 * times it seems to be set is in situations where the data is
707 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
708 * this combined with the fact that this bit is useful for filtering
709 * out misidentified bitmap packets, we reject anything with this
710 * bit set.
711 */
Hans de Goede02d04252014-07-25 22:43:35 -0700712 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800713 return;
714
Hans de Goede02d04252014-07-25 22:43:35 -0700715 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800716 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800717 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
718 return;
719 }
720
721 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800722
Seth Forshee25bded72011-11-07 19:53:36 -0800723 /*
724 * Sometimes the hardware sends a single packet with z = 0
725 * in the middle of a stream. Real releases generate packets
726 * with x, y, and z all zero, so these seem to be flukes.
727 * Ignore them.
728 */
Hans de Goede02d04252014-07-25 22:43:35 -0700729 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800730 return;
731
Hans de Goede68c21872014-07-25 22:47:25 -0700732 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800733
Pali Rohár34412ba2015-01-09 12:48:58 -0800734 if ((priv->flags & ALPS_DUALPOINT) &&
735 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700736 input_report_key(dev2, BTN_LEFT, f->ts_left);
737 input_report_key(dev2, BTN_RIGHT, f->ts_right);
738 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800739 input_sync(dev2);
740 }
741}
742
743static void alps_process_packet_v3(struct psmouse *psmouse)
744{
745 unsigned char *packet = psmouse->packet;
746
747 /*
748 * v3 protocol packets come in three types, two representing
749 * touchpad data and one representing trackstick data.
750 * Trackstick packets seem to be distinguished by always
751 * having 0x3f in the last byte. This value has never been
752 * observed in the last byte of either of the other types
753 * of packets.
754 */
755 if (packet[5] == 0x3f) {
756 alps_process_trackstick_packet_v3(psmouse);
757 return;
758 }
759
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800760 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800761}
762
Yunkang Tang95f75e92013-12-01 22:33:52 -0800763static void alps_process_packet_v6(struct psmouse *psmouse)
764{
765 struct alps_data *priv = psmouse->private;
766 unsigned char *packet = psmouse->packet;
767 struct input_dev *dev = psmouse->dev;
768 struct input_dev *dev2 = priv->dev2;
769 int x, y, z, left, right, middle;
770
771 /*
772 * We can use Byte5 to distinguish if the packet is from Touchpad
773 * or Trackpoint.
774 * Touchpad: 0 - 0x7E
775 * Trackpoint: 0x7F
776 */
777 if (packet[5] == 0x7F) {
778 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800779 if (!(priv->flags & ALPS_DUALPOINT)) {
780 psmouse_warn(psmouse,
781 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800782 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800783 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800784
785 /* Trackpoint packet */
786 x = packet[1] | ((packet[3] & 0x20) << 2);
787 y = packet[2] | ((packet[3] & 0x40) << 1);
788 z = packet[4];
789 left = packet[3] & 0x01;
790 right = packet[3] & 0x02;
791 middle = packet[3] & 0x04;
792
793 /* To prevent the cursor jump when finger lifted */
794 if (x == 0x7F && y == 0x7F && z == 0x7F)
795 x = y = z = 0;
796
797 /* Divide 4 since trackpoint's speed is too fast */
798 input_report_rel(dev2, REL_X, (char)x / 4);
799 input_report_rel(dev2, REL_Y, -((char)y / 4));
800
801 input_report_key(dev2, BTN_LEFT, left);
802 input_report_key(dev2, BTN_RIGHT, right);
803 input_report_key(dev2, BTN_MIDDLE, middle);
804
805 input_sync(dev2);
806 return;
807 }
808
809 /* Touchpad packet */
810 x = packet[1] | ((packet[3] & 0x78) << 4);
811 y = packet[2] | ((packet[4] & 0x78) << 4);
812 z = packet[5];
813 left = packet[3] & 0x01;
814 right = packet[3] & 0x02;
815
816 if (z > 30)
817 input_report_key(dev, BTN_TOUCH, 1);
818 if (z < 25)
819 input_report_key(dev, BTN_TOUCH, 0);
820
821 if (z > 0) {
822 input_report_abs(dev, ABS_X, x);
823 input_report_abs(dev, ABS_Y, y);
824 }
825
826 input_report_abs(dev, ABS_PRESSURE, z);
827 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
828
829 /* v6 touchpad does not have middle button */
830 input_report_key(dev, BTN_LEFT, left);
831 input_report_key(dev, BTN_RIGHT, right);
832
833 input_sync(dev);
834}
835
Seth Forshee25bded72011-11-07 19:53:36 -0800836static void alps_process_packet_v4(struct psmouse *psmouse)
837{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700838 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800839 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700840 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700841 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700842
843 /*
844 * v4 has a 6-byte encoding for bitmap data, but this data is
845 * broken up between 3 normal packets. Use priv->multi_packet to
846 * track our position in the bitmap packet.
847 */
848 if (packet[6] & 0x40) {
849 /* sync, reset position */
850 priv->multi_packet = 0;
851 }
852
853 if (WARN_ON_ONCE(priv->multi_packet > 2))
854 return;
855
856 offset = 2 * priv->multi_packet;
857 priv->multi_data[offset] = packet[6];
858 priv->multi_data[offset + 1] = packet[7];
859
860 if (++priv->multi_packet > 2) {
861 priv->multi_packet = 0;
862
Hans de Goede02d04252014-07-25 22:43:35 -0700863 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700864 ((priv->multi_data[3] & 0x60) << 3) |
865 ((priv->multi_data[0] & 0x3f) << 2) |
866 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700867 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700868 ((priv->multi_data[3] & 0x1f) << 5) |
869 (priv->multi_data[1] & 0x1f);
870
Hans de Goede02d04252014-07-25 22:43:35 -0700871 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700872 }
Seth Forshee25bded72011-11-07 19:53:36 -0800873
Andreas Boschb0cfb792014-10-15 10:44:50 -0700874 f->left = !!(packet[4] & 0x01);
875 f->right = !!(packet[4] & 0x02);
Seth Forshee25bded72011-11-07 19:53:36 -0800876
Hans de Goede02d04252014-07-25 22:43:35 -0700877 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
878 ((packet[0] & 0x30) >> 4);
879 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
880 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800881
Hans de Goede68c21872014-07-25 22:47:25 -0700882 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800883}
884
Yunkang Tang38088432014-07-26 13:51:41 -0700885static bool alps_is_valid_package_v7(struct psmouse *psmouse)
886{
887 switch (psmouse->pktcnt) {
888 case 3:
889 return (psmouse->packet[2] & 0x40) == 0x40;
890 case 4:
891 return (psmouse->packet[3] & 0x48) == 0x48;
892 case 6:
893 return (psmouse->packet[5] & 0x40) == 0x00;
894 }
895 return true;
896}
897
898static unsigned char alps_get_packet_id_v7(char *byte)
899{
900 unsigned char packet_id;
901
902 if (byte[4] & 0x40)
903 packet_id = V7_PACKET_ID_TWO;
904 else if (byte[4] & 0x01)
905 packet_id = V7_PACKET_ID_MULTI;
906 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
907 packet_id = V7_PACKET_ID_NEW;
908 else if (byte[1] == 0x00 && byte[4] == 0x00)
909 packet_id = V7_PACKET_ID_IDLE;
910 else
911 packet_id = V7_PACKET_ID_UNKNOWN;
912
913 return packet_id;
914}
915
916static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
917 unsigned char *pkt,
918 unsigned char pkt_id)
919{
920 mt[0].x = ((pkt[2] & 0x80) << 4);
921 mt[0].x |= ((pkt[2] & 0x3F) << 5);
922 mt[0].x |= ((pkt[3] & 0x30) >> 1);
923 mt[0].x |= (pkt[3] & 0x07);
924 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
925
926 mt[1].x = ((pkt[3] & 0x80) << 4);
927 mt[1].x |= ((pkt[4] & 0x80) << 3);
928 mt[1].x |= ((pkt[4] & 0x3F) << 4);
929 mt[1].y = ((pkt[5] & 0x80) << 3);
930 mt[1].y |= ((pkt[5] & 0x3F) << 4);
931
932 switch (pkt_id) {
933 case V7_PACKET_ID_TWO:
934 mt[1].x &= ~0x000F;
935 mt[1].y |= 0x000F;
936 break;
937
938 case V7_PACKET_ID_MULTI:
939 mt[1].x &= ~0x003F;
940 mt[1].y &= ~0x0020;
941 mt[1].y |= ((pkt[4] & 0x02) << 4);
942 mt[1].y |= 0x001F;
943 break;
944
945 case V7_PACKET_ID_NEW:
946 mt[1].x &= ~0x003F;
947 mt[1].x |= (pkt[0] & 0x20);
948 mt[1].y |= 0x000F;
949 break;
950 }
951
952 mt[0].y = 0x7FF - mt[0].y;
953 mt[1].y = 0x7FF - mt[1].y;
954}
955
956static int alps_get_mt_count(struct input_mt_pos *mt)
957{
Hans de Goede7091c442014-12-18 09:53:34 -0800958 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700959
Hans de Goede7091c442014-12-18 09:53:34 -0800960 for (i = 0; i < MAX_TOUCHES; i++) {
961 if (mt[i].x != 0 || mt[i].y != 0)
962 fingers++;
963 }
Yunkang Tang38088432014-07-26 13:51:41 -0700964
Hans de Goede7091c442014-12-18 09:53:34 -0800965 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700966}
967
968static int alps_decode_packet_v7(struct alps_fields *f,
969 unsigned char *p,
970 struct psmouse *psmouse)
971{
Hans de Goeded27eb792014-12-18 09:55:14 -0800972 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700973 unsigned char pkt_id;
974
975 pkt_id = alps_get_packet_id_v7(p);
976 if (pkt_id == V7_PACKET_ID_IDLE)
977 return 0;
978 if (pkt_id == V7_PACKET_ID_UNKNOWN)
979 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -0800980 /*
981 * NEW packets are send to indicate a discontinuity in the finger
982 * coordinate reporting. Specifically a finger may have moved from
983 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
984 * us.
985 *
986 * NEW packets have 3 problems:
987 * 1) They do not contain middle / right button info (on non clickpads)
988 * this can be worked around by preserving the old button state
989 * 2) They do not contain an accurate fingercount, and they are
990 * typically send when the number of fingers changes. We cannot use
991 * the old finger count as that may mismatch with the amount of
992 * touch coordinates we've available in the NEW packet
993 * 3) Their x data for the second touch is inaccurate leading to
994 * a possible jump of the x coordinate by 16 units when the first
995 * non NEW packet comes in
996 * Since problems 2 & 3 cannot be worked around, just ignore them.
997 */
998 if (pkt_id == V7_PACKET_ID_NEW)
999 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001000
1001 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1002
Yunkang Tang38088432014-07-26 13:51:41 -07001003 if (pkt_id == V7_PACKET_ID_TWO)
1004 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001005 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001006 f->fingers = 3 + (p[5] & 0x03);
1007
Hans de Goeded27eb792014-12-18 09:55:14 -08001008 f->left = (p[0] & 0x80) >> 7;
1009 if (priv->flags & ALPS_BUTTONPAD) {
1010 if (p[0] & 0x20)
1011 f->fingers++;
1012 if (p[0] & 0x10)
1013 f->fingers++;
1014 } else {
1015 f->right = (p[0] & 0x20) >> 5;
1016 f->middle = (p[0] & 0x10) >> 4;
1017 }
1018
Hans de Goede7091c442014-12-18 09:53:34 -08001019 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1020 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1021 f->mt[0].x = f->mt[1].x;
1022 f->mt[0].y = f->mt[1].y;
1023 f->mt[1].x = 0;
1024 f->mt[1].y = 0;
1025 }
1026
Yunkang Tang38088432014-07-26 13:51:41 -07001027 return 0;
1028}
1029
1030static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1031{
1032 struct alps_data *priv = psmouse->private;
1033 unsigned char *packet = psmouse->packet;
1034 struct input_dev *dev2 = priv->dev2;
1035 int x, y, z, left, right, middle;
1036
Pali Rohár34412ba2015-01-09 12:48:58 -08001037 /* It should be a DualPoint when received trackstick packet */
1038 if (!(priv->flags & ALPS_DUALPOINT)) {
1039 psmouse_warn(psmouse,
1040 "Rejected trackstick packet from non DualPoint device");
1041 return;
1042 }
1043
Yunkang Tang38088432014-07-26 13:51:41 -07001044 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1045 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1046 ((packet[3] & 0x20) << 1);
1047 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1048
1049 left = (packet[1] & 0x01);
1050 right = (packet[1] & 0x02) >> 1;
1051 middle = (packet[1] & 0x04) >> 2;
1052
1053 /* Divide 2 since trackpoint's speed is too fast */
1054 input_report_rel(dev2, REL_X, (char)x / 2);
1055 input_report_rel(dev2, REL_Y, -((char)y / 2));
1056
1057 input_report_key(dev2, BTN_LEFT, left);
1058 input_report_key(dev2, BTN_RIGHT, right);
1059 input_report_key(dev2, BTN_MIDDLE, middle);
1060
1061 input_sync(dev2);
1062}
1063
1064static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1065{
1066 struct alps_data *priv = psmouse->private;
1067 struct input_dev *dev = psmouse->dev;
1068 struct alps_fields *f = &priv->f;
1069
1070 memset(f, 0, sizeof(*f));
1071
1072 if (priv->decode_fields(f, psmouse->packet, psmouse))
1073 return;
1074
1075 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1076
1077 input_mt_report_finger_count(dev, f->fingers);
1078
1079 input_report_key(dev, BTN_LEFT, f->left);
1080 input_report_key(dev, BTN_RIGHT, f->right);
1081 input_report_key(dev, BTN_MIDDLE, f->middle);
1082
1083 input_sync(dev);
1084}
1085
1086static void alps_process_packet_v7(struct psmouse *psmouse)
1087{
1088 unsigned char *packet = psmouse->packet;
1089
1090 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1091 alps_process_trackstick_packet_v7(psmouse);
1092 else
1093 alps_process_touchpad_packet_v7(psmouse);
1094}
1095
Pali Rohár04aae282015-01-14 13:18:30 -08001096static DEFINE_MUTEX(alps_mutex);
1097
1098static void alps_register_bare_ps2_mouse(struct work_struct *work)
1099{
1100 struct alps_data *priv =
1101 container_of(work, struct alps_data, dev3_register_work.work);
1102 struct psmouse *psmouse = priv->psmouse;
1103 struct input_dev *dev3;
1104 int error = 0;
1105
1106 mutex_lock(&alps_mutex);
1107
1108 if (priv->dev3)
1109 goto out;
1110
1111 dev3 = input_allocate_device();
1112 if (!dev3) {
1113 psmouse_err(psmouse, "failed to allocate secondary device\n");
1114 error = -ENOMEM;
1115 goto out;
1116 }
1117
1118 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1119 psmouse->ps2dev.serio->phys,
1120 (priv->dev2 ? "input2" : "input1"));
1121 dev3->phys = priv->phys3;
1122
1123 /*
1124 * format of input device name is: "protocol vendor name"
1125 * see function psmouse_switch_protocol() in psmouse-base.c
1126 */
1127 dev3->name = "PS/2 ALPS Mouse";
1128
1129 dev3->id.bustype = BUS_I8042;
1130 dev3->id.vendor = 0x0002;
1131 dev3->id.product = PSMOUSE_PS2;
1132 dev3->id.version = 0x0000;
1133 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1134
1135 input_set_capability(dev3, EV_REL, REL_X);
1136 input_set_capability(dev3, EV_REL, REL_Y);
1137 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1138 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1139 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1140
1141 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1142
1143 error = input_register_device(dev3);
1144 if (error) {
1145 psmouse_err(psmouse,
1146 "failed to register secondary device: %d\n",
1147 error);
1148 input_free_device(dev3);
1149 goto out;
1150 }
1151
1152 priv->dev3 = dev3;
1153
1154out:
1155 /*
1156 * Save the error code so that we can detect that we
1157 * already tried to create the device.
1158 */
1159 if (error)
1160 priv->dev3 = ERR_PTR(error);
1161
1162 mutex_unlock(&alps_mutex);
1163}
1164
Hans de Goede59c30af2015-04-03 17:14:40 -07001165static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001166 unsigned char packet[],
1167 bool report_buttons)
1168{
Hans de Goede59c30af2015-04-03 17:14:40 -07001169 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001170 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001171
Hans de Goedee3a79212015-04-03 17:20:05 -07001172 /* Figure out which device to use to report the bare packet */
1173 if (priv->proto_version == ALPS_PROTO_V2 &&
1174 (priv->flags & ALPS_DUALPOINT)) {
1175 /* On V2 devices the DualPoint Stick reports bare packets */
1176 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001177 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001178 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001179 /* Register dev3 mouse if we received PS/2 packet first time */
1180 if (!IS_ERR(priv->dev3))
1181 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1182 0);
1183 return;
1184 } else {
1185 dev = priv->dev3;
1186 }
1187
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001188 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001189 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001190 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1191
Pali Rohár04aae282015-01-14 13:18:30 -08001192 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001193 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001194 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001195 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1196
Pali Rohár04aae282015-01-14 13:18:30 -08001197 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001198}
1199
1200static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct alps_data *priv = psmouse->private;
1203
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001204 if (psmouse->pktcnt < 6)
1205 return PSMOUSE_GOOD_DATA;
1206
1207 if (psmouse->pktcnt == 6) {
1208 /*
1209 * Start a timer to flush the packet if it ends up last
1210 * 6-byte packet in the stream. Timer needs to fire
1211 * psmouse core times out itself. 20 ms should be enough
1212 * to decide if we are getting more data or not.
1213 */
1214 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1215 return PSMOUSE_GOOD_DATA;
1216 }
1217
1218 del_timer(&priv->timer);
1219
1220 if (psmouse->packet[6] & 0x80) {
1221
1222 /*
1223 * Highest bit is set - that means we either had
1224 * complete ALPS packet and this is start of the
1225 * next packet or we got garbage.
1226 */
1227
1228 if (((psmouse->packet[3] |
1229 psmouse->packet[4] |
1230 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001231 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001232 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001233 "refusing packet %4ph (suspected interleaved ps/2)\n",
1234 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001235 return PSMOUSE_BAD_DATA;
1236 }
1237
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001238 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001239
1240 /* Continue with the next packet */
1241 psmouse->packet[0] = psmouse->packet[6];
1242 psmouse->pktcnt = 1;
1243
1244 } else {
1245
1246 /*
1247 * High bit is 0 - that means that we indeed got a PS/2
1248 * packet in the middle of ALPS packet.
1249 *
1250 * There is also possibility that we got 6-byte ALPS
1251 * packet followed by 3-byte packet from trackpoint. We
1252 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001253 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001254 * normal operation (user would need to press all
1255 * buttons on the pad and start moving trackpoint
1256 * without touching the pad surface) we assume former.
1257 * Even if we are wrong the wost thing that would happen
1258 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001259 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001260 */
1261
Hans de Goede59c30af2015-04-03 17:14:40 -07001262 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1263 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001264
1265 /*
1266 * Continue with the standard ALPS protocol handling,
1267 * but make sure we won't process it as an interleaved
1268 * packet again, which may happen if all buttons are
1269 * pressed. To avoid this let's reset the 4th bit which
1270 * is normally 1.
1271 */
1272 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1273 psmouse->pktcnt = 4;
1274 }
1275
1276 return PSMOUSE_GOOD_DATA;
1277}
1278
1279static void alps_flush_packet(unsigned long data)
1280{
1281 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001282 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001283
1284 serio_pause_rx(psmouse->ps2dev.serio);
1285
Seth Forsheeb46615f2011-11-07 19:53:30 -08001286 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001287
1288 /*
1289 * We did not any more data in reasonable amount of time.
1290 * Validate the last 3 bytes and process as a standard
1291 * ALPS packet.
1292 */
1293 if ((psmouse->packet[3] |
1294 psmouse->packet[4] |
1295 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001296 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001297 "refusing packet %3ph (suspected interleaved ps/2)\n",
1298 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001299 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001300 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001301 }
1302 psmouse->pktcnt = 0;
1303 }
1304
1305 serio_continue_rx(psmouse->ps2dev.serio);
1306}
1307
1308static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1309{
1310 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001311
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001312 /*
1313 * Check if we are dealing with a bare PS/2 packet, presumably from
1314 * a device connected to the external PS/2 port. Because bare PS/2
1315 * protocol does not have enough constant bits to self-synchronize
1316 * properly we only do this if the device is fully synchronized.
1317 */
1318 if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001320 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1321 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return PSMOUSE_FULL_PACKET;
1323 }
1324 return PSMOUSE_GOOD_DATA;
1325 }
1326
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001327 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1328
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001329 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001330 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1331 return alps_handle_interleaved_ps2(psmouse);
1332 }
1333
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001334 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001335 psmouse_dbg(psmouse,
1336 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001337 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Seth Forsheeb46615f2011-11-07 19:53:30 -08001341 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001342 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001343 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001344 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001345 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1346 psmouse->pktcnt - 1,
1347 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001348
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001349 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001350 psmouse->pktcnt == psmouse->pktsize) {
1351 /*
1352 * Some Dell boxes, such as Latitude E6440 or E7440
1353 * with closed lid, quite often smash last byte of
1354 * otherwise valid packet with 0xff. Given that the
1355 * next packet is very likely to be valid let's
1356 * report PSMOUSE_FULL_PACKET but not process data,
1357 * rather than reporting PSMOUSE_BAD_DATA and
1358 * filling the logs.
1359 */
1360 return PSMOUSE_FULL_PACKET;
1361 }
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Yunkang Tang38088432014-07-26 13:51:41 -07001366 if (priv->proto_version == ALPS_PROTO_V7 &&
1367 !alps_is_valid_package_v7(psmouse)) {
1368 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1369 psmouse->pktcnt - 1,
1370 psmouse->packet[psmouse->pktcnt - 1]);
1371 return PSMOUSE_BAD_DATA;
1372 }
1373
Seth Forsheeb46615f2011-11-07 19:53:30 -08001374 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001375 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return PSMOUSE_FULL_PACKET;
1377 }
1378
1379 return PSMOUSE_GOOD_DATA;
1380}
1381
Seth Forshee25bded72011-11-07 19:53:36 -08001382static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1383{
1384 struct ps2dev *ps2dev = &psmouse->ps2dev;
1385 struct alps_data *priv = psmouse->private;
1386 int command;
1387 unsigned char *param;
1388 unsigned char dummy[4];
1389
1390 BUG_ON(nibble > 0xf);
1391
1392 command = priv->nibble_commands[nibble].command;
1393 param = (command & 0x0f00) ?
1394 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1395
1396 if (ps2_command(ps2dev, param, command))
1397 return -1;
1398
1399 return 0;
1400}
1401
1402static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1403{
1404 struct ps2dev *ps2dev = &psmouse->ps2dev;
1405 struct alps_data *priv = psmouse->private;
1406 int i, nibble;
1407
1408 if (ps2_command(ps2dev, NULL, priv->addr_command))
1409 return -1;
1410
1411 for (i = 12; i >= 0; i -= 4) {
1412 nibble = (addr >> i) & 0xf;
1413 if (alps_command_mode_send_nibble(psmouse, nibble))
1414 return -1;
1415 }
1416
1417 return 0;
1418}
1419
1420static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1421{
1422 struct ps2dev *ps2dev = &psmouse->ps2dev;
1423 unsigned char param[4];
1424
1425 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1426 return -1;
1427
1428 /*
1429 * The address being read is returned in the first two bytes
1430 * of the result. Check that this address matches the expected
1431 * address.
1432 */
1433 if (addr != ((param[0] << 8) | param[1]))
1434 return -1;
1435
1436 return param[2];
1437}
1438
1439static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1440{
1441 if (alps_command_mode_set_addr(psmouse, addr))
1442 return -1;
1443 return __alps_command_mode_read_reg(psmouse, addr);
1444}
1445
1446static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1447{
1448 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1449 return -1;
1450 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1451 return -1;
1452 return 0;
1453}
1454
1455static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1456 u8 value)
1457{
1458 if (alps_command_mode_set_addr(psmouse, addr))
1459 return -1;
1460 return __alps_command_mode_write_reg(psmouse, value);
1461}
1462
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001463static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1464 int repeated_command, unsigned char *param)
1465{
1466 struct ps2dev *ps2dev = &psmouse->ps2dev;
1467
1468 param[0] = 0;
1469 if (init_command && ps2_command(ps2dev, param, init_command))
1470 return -EIO;
1471
1472 if (ps2_command(ps2dev, NULL, repeated_command) ||
1473 ps2_command(ps2dev, NULL, repeated_command) ||
1474 ps2_command(ps2dev, NULL, repeated_command))
1475 return -EIO;
1476
1477 param[0] = param[1] = param[2] = 0xff;
1478 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1479 return -EIO;
1480
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001481 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1482 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001483 return 0;
1484}
1485
Yunkang Tang38088432014-07-26 13:51:41 -07001486static bool alps_check_valid_firmware_id(unsigned char id[])
1487{
1488 if (id[0] == 0x73)
1489 return true;
1490
1491 if (id[0] == 0x88 &&
1492 (id[1] == 0x07 ||
1493 id[1] == 0x08 ||
1494 (id[1] & 0xf0) == 0xb0 ||
1495 (id[1] & 0xf0) == 0xc0)) {
1496 return true;
1497 }
1498
1499 return false;
1500}
1501
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001502static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001503{
1504 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001505
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001506 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001507 psmouse_err(psmouse, "failed to enter command mode\n");
1508 return -1;
1509 }
1510
Yunkang Tang38088432014-07-26 13:51:41 -07001511 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001512 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001513 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001514 return -1;
1515 }
Seth Forshee25bded72011-11-07 19:53:36 -08001516 return 0;
1517}
1518
1519static inline int alps_exit_command_mode(struct psmouse *psmouse)
1520{
1521 struct ps2dev *ps2dev = &psmouse->ps2dev;
1522 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1523 return -1;
1524 return 0;
1525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527/*
1528 * For DualPoint devices select the device that should respond to
1529 * subsequent commands. It looks like glidepad is behind stickpointer,
1530 * I'd thought it would be other way around...
1531 */
Seth Forshee25bded72011-11-07 19:53:36 -08001532static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1536
1537 if (ps2_command(ps2dev, NULL, cmd) ||
1538 ps2_command(ps2dev, NULL, cmd) ||
1539 ps2_command(ps2dev, NULL, cmd) ||
1540 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1541 return -1;
1542
1543 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001544 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 return 0;
1547}
1548
Seth Forshee25bded72011-11-07 19:53:36 -08001549static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct ps2dev *ps2dev = &psmouse->ps2dev;
1552
1553 /* Try ALPS magic knock - 4 disable before enable */
1554 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1555 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1556 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1557 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1558 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1559 return -1;
1560
1561 /*
1562 * Switch mouse to poll (remote) mode so motion data will not
1563 * get in our way
1564 */
1565 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1566}
1567
Yunkang Tang95f75e92013-12-01 22:33:52 -08001568static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1569{
1570 int i, nibble;
1571
1572 /*
1573 * b0-b11 are valid bits, send sequence is inverse.
1574 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1575 */
1576 for (i = 0; i <= 8; i += 4) {
1577 nibble = (word >> i) & 0xf;
1578 if (alps_command_mode_send_nibble(psmouse, nibble))
1579 return -1;
1580 }
1581
1582 return 0;
1583}
1584
1585static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1586 u16 addr, u16 value)
1587{
1588 struct ps2dev *ps2dev = &psmouse->ps2dev;
1589
1590 /* 0x0A0 is the command to write the word */
1591 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1592 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1593 alps_monitor_mode_send_word(psmouse, addr) ||
1594 alps_monitor_mode_send_word(psmouse, value) ||
1595 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1596 return -1;
1597
1598 return 0;
1599}
1600
1601static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1602{
1603 struct ps2dev *ps2dev = &psmouse->ps2dev;
1604
1605 if (enable) {
1606 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1607 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1608 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1609 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1610 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1611 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1612 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1613 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1614 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1615 return -1;
1616 } else {
1617 /* EC to exit monitor mode */
1618 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1619 return -1;
1620 }
1621
1622 return 0;
1623}
1624
1625static int alps_absolute_mode_v6(struct psmouse *psmouse)
1626{
1627 u16 reg_val = 0x181;
1628 int ret = -1;
1629
1630 /* enter monitor mode, to write the register */
1631 if (alps_monitor_mode(psmouse, true))
1632 return -1;
1633
1634 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1635
1636 if (alps_monitor_mode(psmouse, false))
1637 ret = -1;
1638
1639 return ret;
1640}
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642static int alps_get_status(struct psmouse *psmouse, char *param)
1643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001645 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return -1;
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return 0;
1649}
1650
1651/*
1652 * Turn touchpad tapping on or off. The sequences are:
1653 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1654 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1655 * My guess that 0xE9 (GetInfo) is here as a sync point.
1656 * For models that also have stickpointer (DualPoints) its tapping
1657 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1658 * we don't fiddle with it.
1659 */
1660static int alps_tap_mode(struct psmouse *psmouse, int enable)
1661{
1662 struct ps2dev *ps2dev = &psmouse->ps2dev;
1663 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1664 unsigned char tap_arg = enable ? 0x0A : 0x00;
1665 unsigned char param[4];
1666
1667 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1668 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1669 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1670 ps2_command(ps2dev, &tap_arg, cmd))
1671 return -1;
1672
1673 if (alps_get_status(psmouse, param))
1674 return -1;
1675
1676 return 0;
1677}
1678
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001679/*
1680 * alps_poll() - poll the touchpad for current motion packet.
1681 * Used in resync.
1682 */
1683static int alps_poll(struct psmouse *psmouse)
1684{
1685 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001686 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001687 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001688
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001689 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001690 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001691
1692 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1693 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1694
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001695 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001696 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001697
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001698 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001699 return -1;
1700
1701 if ((psmouse->badbyte & 0xc8) == 0x08) {
1702/*
1703 * Poll the track stick ...
1704 */
1705 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1706 return -1;
1707 }
1708
1709 memcpy(psmouse->packet, buf, sizeof(buf));
1710 return 0;
1711}
1712
Seth Forshee25bded72011-11-07 19:53:36 -08001713static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001717 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001718 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001722 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001723 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Seth Forshee25bded72011-11-07 19:53:36 -08001727 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001728 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return -1;
1730 }
1731
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001732 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001733 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001737 /* ALPS needs stream mode, otherwise it won't report any data */
1738 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001739 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001740 return -1;
1741 }
1742
1743 return 0;
1744}
1745
Yunkang Tang95f75e92013-12-01 22:33:52 -08001746static int alps_hw_init_v6(struct psmouse *psmouse)
1747{
1748 unsigned char param[2] = {0xC8, 0x14};
1749
1750 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1751 if (alps_passthrough_mode_v2(psmouse, true))
1752 return -1;
1753
1754 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1755 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1756 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1757 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1758 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1759 return -1;
1760
1761 if (alps_passthrough_mode_v2(psmouse, false))
1762 return -1;
1763
1764 if (alps_absolute_mode_v6(psmouse)) {
1765 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1766 return -1;
1767 }
1768
1769 return 0;
1770}
1771
Seth Forshee25bded72011-11-07 19:53:36 -08001772/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001773 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001774 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001775static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1776 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001777{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001778 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001779
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001780 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001781 return -1;
1782
Kevin Cernekeecd401202013-02-13 22:28:07 -08001783 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1784 if (reg_val == -1)
1785 goto error;
1786
Seth Forshee25bded72011-11-07 19:53:36 -08001787 if (enable)
1788 reg_val |= 0x01;
1789 else
1790 reg_val &= ~0x01;
1791
Kevin Cernekeecd401202013-02-13 22:28:07 -08001792 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001793
Kevin Cernekeecd401202013-02-13 22:28:07 -08001794error:
1795 if (alps_exit_command_mode(psmouse))
1796 ret = -1;
1797 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001798}
1799
1800/* Must be in command mode when calling this function */
1801static int alps_absolute_mode_v3(struct psmouse *psmouse)
1802{
1803 int reg_val;
1804
1805 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1806 if (reg_val == -1)
1807 return -1;
1808
1809 reg_val |= 0x06;
1810 if (__alps_command_mode_write_reg(psmouse, reg_val))
1811 return -1;
1812
1813 return 0;
1814}
1815
Kevin Cernekeecd401202013-02-13 22:28:07 -08001816static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1817{
1818 int ret = -EIO, reg_val;
1819
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001820 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001821 goto error;
1822
1823 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1824 if (reg_val == -1)
1825 goto error;
1826
1827 /* bit 7: trackstick is present */
1828 ret = reg_val & 0x80 ? 0 : -ENODEV;
1829
1830error:
1831 alps_exit_command_mode(psmouse);
1832 return ret;
1833}
1834
1835static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1836{
1837 struct ps2dev *ps2dev = &psmouse->ps2dev;
1838 int ret = 0;
1839 unsigned char param[4];
1840
1841 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1842 return -EIO;
1843
1844 /*
1845 * E7 report for the trackstick
1846 *
1847 * There have been reports of failures to seem to trace back
1848 * to the above trackstick check failing. When these occur
1849 * this E7 report fails, so when that happens we continue
1850 * with the assumption that there isn't a trackstick after
1851 * all.
1852 */
1853 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08001854 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08001855 ret = -ENODEV;
1856 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001857 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001858
1859 /*
1860 * Not sure what this does, but it is absolutely
1861 * essential. Without it, the touchpad does not
1862 * work at all and the trackstick just emits normal
1863 * PS/2 packets.
1864 */
1865 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1866 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1867 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1868 alps_command_mode_send_nibble(psmouse, 0x9) ||
1869 alps_command_mode_send_nibble(psmouse, 0x4)) {
1870 psmouse_err(psmouse,
1871 "Error sending magic E6 sequence\n");
1872 ret = -EIO;
1873 goto error;
1874 }
1875
1876 /*
1877 * This ensures the trackstick packets are in the format
1878 * supported by this driver. If bit 1 isn't set the packet
1879 * format is different.
1880 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001881 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001882 alps_command_mode_write_reg(psmouse,
1883 reg_base + 0x08, 0x82) ||
1884 alps_exit_command_mode(psmouse))
1885 ret = -EIO;
1886 }
1887
1888error:
1889 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1890 ret = -EIO;
1891
1892 return ret;
1893}
1894
Seth Forshee25bded72011-11-07 19:53:36 -08001895static int alps_hw_init_v3(struct psmouse *psmouse)
1896{
Seth Forshee25bded72011-11-07 19:53:36 -08001897 struct ps2dev *ps2dev = &psmouse->ps2dev;
1898 int reg_val;
1899 unsigned char param[4];
1900
Kevin Cernekeecd401202013-02-13 22:28:07 -08001901 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1902 if (reg_val == -EIO)
1903 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001904
Kevin Cernekeecd401202013-02-13 22:28:07 -08001905 if (reg_val == 0 &&
1906 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001907 goto error;
1908
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001909 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001910 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001911 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1912 goto error;
1913 }
1914
1915 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1916 if (reg_val == -1)
1917 goto error;
1918 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1919 goto error;
1920
1921 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1922 if (reg_val == -1)
1923 goto error;
1924 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1925 goto error;
1926
1927 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1928 goto error;
1929 if (__alps_command_mode_write_reg(psmouse, 0x04))
1930 goto error;
1931
1932 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1933 goto error;
1934 if (__alps_command_mode_write_reg(psmouse, 0x03))
1935 goto error;
1936
1937 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1938 goto error;
1939 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1940 goto error;
1941
1942 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1943 goto error;
1944 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1945 goto error;
1946
Seth Forshee25bded72011-11-07 19:53:36 -08001947 alps_exit_command_mode(psmouse);
1948
1949 /* Set rate and enable data reporting */
1950 param[0] = 0x64;
1951 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1952 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1953 psmouse_err(psmouse, "Failed to enable data reporting\n");
1954 return -1;
1955 }
1956
1957 return 0;
1958
Seth Forshee25bded72011-11-07 19:53:36 -08001959error:
1960 /*
1961 * Leaving the touchpad in command mode will essentially render
1962 * it unusable until the machine reboots, so exit it here just
1963 * to be safe
1964 */
1965 alps_exit_command_mode(psmouse);
1966 return -1;
1967}
1968
Hans de Goedef3f33c62014-07-29 11:22:07 -07001969static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1970{
1971 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1972 struct alps_data *priv = psmouse->private;
1973
1974 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1975 if (reg < 0)
1976 return reg;
1977
1978 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1979 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1980
1981 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1982 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1983
1984 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1985 if (reg < 0)
1986 return reg;
1987
1988 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1989 x_electrode = 17 + x_electrode;
1990
1991 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1992 y_electrode = 13 + y_electrode;
1993
1994 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1995 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1996
1997 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1998 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1999
2000 psmouse_dbg(psmouse,
2001 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2002 x_pitch, y_pitch, x_electrode, y_electrode,
2003 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2004
2005 return 0;
2006}
2007
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002008static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2009{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002010 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002011 struct ps2dev *ps2dev = &psmouse->ps2dev;
2012 int reg_val, ret = -1;
2013
Kevin Cernekeecd401202013-02-13 22:28:07 -08002014 if (priv->flags & ALPS_DUALPOINT) {
2015 reg_val = alps_setup_trackstick_v3(psmouse,
2016 ALPS_REG_BASE_RUSHMORE);
2017 if (reg_val == -EIO)
2018 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002019 }
2020
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002021 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002022 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2023 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2024 goto error;
2025
Hans de Goedef3f33c62014-07-29 11:22:07 -07002026 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2027 goto error;
2028
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002029 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2030 if (reg_val == -1)
2031 goto error;
2032 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2033 goto error;
2034
2035 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2036 goto error;
2037
2038 /* enter absolute mode */
2039 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2040 if (reg_val == -1)
2041 goto error;
2042 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2043 goto error;
2044
2045 alps_exit_command_mode(psmouse);
2046 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2047
2048error:
2049 alps_exit_command_mode(psmouse);
2050 return ret;
2051}
2052
Seth Forshee25bded72011-11-07 19:53:36 -08002053/* Must be in command mode when calling this function */
2054static int alps_absolute_mode_v4(struct psmouse *psmouse)
2055{
2056 int reg_val;
2057
2058 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2059 if (reg_val == -1)
2060 return -1;
2061
2062 reg_val |= 0x02;
2063 if (__alps_command_mode_write_reg(psmouse, reg_val))
2064 return -1;
2065
2066 return 0;
2067}
2068
2069static int alps_hw_init_v4(struct psmouse *psmouse)
2070{
Seth Forshee25bded72011-11-07 19:53:36 -08002071 struct ps2dev *ps2dev = &psmouse->ps2dev;
2072 unsigned char param[4];
2073
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002074 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002075 goto error;
2076
2077 if (alps_absolute_mode_v4(psmouse)) {
2078 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2079 goto error;
2080 }
2081
2082 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2083 goto error;
2084
2085 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2086 goto error;
2087
2088 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2089 goto error;
2090
2091 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2092 goto error;
2093
2094 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2095 goto error;
2096
2097 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2098 goto error;
2099
2100 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2101 goto error;
2102
2103 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2104 goto error;
2105
2106 alps_exit_command_mode(psmouse);
2107
2108 /*
2109 * This sequence changes the output from a 9-byte to an
2110 * 8-byte format. All the same data seems to be present,
2111 * just in a more compact format.
2112 */
2113 param[0] = 0xc8;
2114 param[1] = 0x64;
2115 param[2] = 0x50;
2116 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2117 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2118 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2119 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2120 return -1;
2121
2122 /* Set rate and enable data reporting */
2123 param[0] = 0x64;
2124 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2125 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2126 psmouse_err(psmouse, "Failed to enable data reporting\n");
2127 return -1;
2128 }
2129
2130 return 0;
2131
2132error:
2133 /*
2134 * Leaving the touchpad in command mode will essentially render
2135 * it unusable until the machine reboots, so exit it here just
2136 * to be safe
2137 */
2138 alps_exit_command_mode(psmouse);
2139 return -1;
2140}
2141
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002142static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2143 struct alps_data *priv)
2144{
2145 struct ps2dev *ps2dev = &psmouse->ps2dev;
2146 unsigned char param[4] = {0};
2147 int num_x_electrode, num_y_electrode;
2148
2149 if (alps_enter_command_mode(psmouse))
2150 return -1;
2151
2152 param[0] = 0x0a;
2153 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2154 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2155 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2156 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2157 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2158 return -1;
2159
2160 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2161 return -1;
2162
2163 /*
2164 * Dolphin's sensor line number is not fixed. It can be calculated
2165 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2166 * Further more, we can get device's x_max and y_max by multiplying
2167 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2168 *
2169 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2170 * real sensor line number X = 11 + 8 = 19, and
2171 * real sensor line number Y = 8 + 1 = 9.
2172 * So, x_max = (19 - 1) * 64 = 1152, and
2173 * y_max = (9 - 1) * 64 = 512.
2174 */
2175 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2176 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2177 priv->x_bits = num_x_electrode;
2178 priv->y_bits = num_y_electrode;
2179 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2180 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2181
2182 if (alps_exit_command_mode(psmouse))
2183 return -1;
2184
2185 return 0;
2186}
2187
Dave Turvene75af9e52013-02-21 22:58:28 -08002188static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2189{
2190 struct ps2dev *ps2dev = &psmouse->ps2dev;
2191 unsigned char param[2];
2192
2193 /* This is dolphin "v1" as empirically defined by florin9doi */
2194 param[0] = 0x64;
2195 param[1] = 0x28;
2196
2197 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2198 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2199 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2200 return -1;
2201
2202 return 0;
2203}
2204
Yunkang Tang38088432014-07-26 13:51:41 -07002205static int alps_hw_init_v7(struct psmouse *psmouse)
2206{
2207 struct ps2dev *ps2dev = &psmouse->ps2dev;
2208 int reg_val, ret = -1;
2209
2210 if (alps_enter_command_mode(psmouse) ||
2211 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2212 goto error;
2213
Hans de Goedef3f33c62014-07-29 11:22:07 -07002214 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2215 goto error;
2216
Yunkang Tang38088432014-07-26 13:51:41 -07002217 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2218 goto error;
2219
2220 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2221 if (reg_val == -1)
2222 goto error;
2223 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2224 goto error;
2225
2226 alps_exit_command_mode(psmouse);
2227 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2228
2229error:
2230 alps_exit_command_mode(psmouse);
2231 return ret;
2232}
2233
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002234static int alps_set_protocol(struct psmouse *psmouse,
2235 struct alps_data *priv,
2236 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002237{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002238 psmouse->private = priv;
2239
2240 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2241
2242 priv->proto_version = protocol->version;
2243 priv->byte0 = protocol->byte0;
2244 priv->mask0 = protocol->mask0;
2245 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002246
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002247 priv->x_max = 2000;
2248 priv->y_max = 1400;
2249 priv->x_bits = 15;
2250 priv->y_bits = 11;
2251
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002252 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002253 case ALPS_PROTO_V1:
2254 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002255 priv->hw_init = alps_hw_init_v1_v2;
2256 priv->process_packet = alps_process_packet_v1_v2;
2257 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002258 priv->x_max = 1023;
2259 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002260 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002261
Seth Forshee25bded72011-11-07 19:53:36 -08002262 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002263 priv->hw_init = alps_hw_init_v3;
2264 priv->process_packet = alps_process_packet_v3;
2265 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002266 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002267 priv->nibble_commands = alps_v3_nibble_commands;
2268 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002269 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002270
2271 case ALPS_PROTO_V3_RUSHMORE:
2272 priv->hw_init = alps_hw_init_rushmore_v3;
2273 priv->process_packet = alps_process_packet_v3;
2274 priv->set_abs_params = alps_set_abs_params_mt;
2275 priv->decode_fields = alps_decode_rushmore;
2276 priv->nibble_commands = alps_v3_nibble_commands;
2277 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2278 priv->x_bits = 16;
2279 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002280
2281 if (alps_probe_trackstick_v3(psmouse,
2282 ALPS_REG_BASE_RUSHMORE) < 0)
2283 priv->flags &= ~ALPS_DUALPOINT;
2284
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002285 break;
2286
Seth Forshee25bded72011-11-07 19:53:36 -08002287 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002288 priv->hw_init = alps_hw_init_v4;
2289 priv->process_packet = alps_process_packet_v4;
2290 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002291 priv->nibble_commands = alps_v4_nibble_commands;
2292 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002293 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002294
Dave Turvene75af9e52013-02-21 22:58:28 -08002295 case ALPS_PROTO_V5:
2296 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002297 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002298 priv->decode_fields = alps_decode_dolphin;
2299 priv->set_abs_params = alps_set_abs_params_mt;
2300 priv->nibble_commands = alps_v3_nibble_commands;
2301 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002302 priv->x_bits = 23;
2303 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002304
2305 if (alps_dolphin_get_device_area(psmouse, priv))
2306 return -EIO;
2307
Dave Turvene75af9e52013-02-21 22:58:28 -08002308 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002309
Yunkang Tang95f75e92013-12-01 22:33:52 -08002310 case ALPS_PROTO_V6:
2311 priv->hw_init = alps_hw_init_v6;
2312 priv->process_packet = alps_process_packet_v6;
2313 priv->set_abs_params = alps_set_abs_params_st;
2314 priv->nibble_commands = alps_v6_nibble_commands;
2315 priv->x_max = 2047;
2316 priv->y_max = 1535;
2317 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002318
Yunkang Tang38088432014-07-26 13:51:41 -07002319 case ALPS_PROTO_V7:
2320 priv->hw_init = alps_hw_init_v7;
2321 priv->process_packet = alps_process_packet_v7;
2322 priv->decode_fields = alps_decode_packet_v7;
2323 priv->set_abs_params = alps_set_abs_params_mt;
2324 priv->nibble_commands = alps_v3_nibble_commands;
2325 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002326 priv->x_max = 0xfff;
2327 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002328
2329 if (priv->fw_ver[1] != 0xba)
2330 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002331
Yunkang Tang38088432014-07-26 13:51:41 -07002332 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002333 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002334
2335 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002336}
2337
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002338static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2339 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002340{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002341 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002342 int i;
2343
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002344 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2345 model = &alps_model_data[i];
2346
2347 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2348 (!model->command_mode_resp ||
2349 model->command_mode_resp == ec[2])) {
2350
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002351 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002352 }
2353 }
2354
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002355 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002356}
2357
2358static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2359{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002360 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002361 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002362 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002363
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002364 /*
2365 * First try "E6 report".
2366 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2367 * The bits 0-2 of the first byte will be 1s if some buttons are
2368 * pressed.
2369 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002370 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2371 PSMOUSE_CMD_SETSCALE11, e6))
2372 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002373
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002374 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2375 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002376
2377 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002378 * Now get the "E7" and "EC" reports. These will uniquely identify
2379 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002380 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002381 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2382 PSMOUSE_CMD_SETSCALE21, e7) ||
2383 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2384 PSMOUSE_CMD_RESET_WRAP, ec) ||
2385 alps_exit_command_mode(psmouse))
2386 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002387
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002388 protocol = alps_match_table(e7, ec);
2389 if (!protocol) {
2390 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2391 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2392 protocol = &alps_v5_protocol_data;
2393 } else if (ec[0] == 0x88 &&
2394 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2395 protocol = &alps_v7_protocol_data;
2396 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2397 protocol = &alps_v3_rushmore_data;
2398 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2399 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2400 protocol = &alps_v3_protocol_data;
2401 } else {
2402 psmouse_dbg(psmouse,
2403 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2404 return -EINVAL;
2405 }
2406 }
2407
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002408 if (priv) {
2409 /* Save the Firmware version */
2410 memcpy(priv->fw_ver, ec, 3);
2411 error = alps_set_protocol(psmouse, priv, protocol);
2412 if (error)
2413 return error;
2414 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002415
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002416 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002417}
2418
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002419static int alps_reconnect(struct psmouse *psmouse)
2420{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002421 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002422
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002423 psmouse_reset(psmouse);
2424
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002425 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002426 return -1;
2427
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002428 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
2431static void alps_disconnect(struct psmouse *psmouse)
2432{
2433 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002436 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002437 if (priv->dev2)
2438 input_unregister_device(priv->dev2);
2439 if (!IS_ERR_OR_NULL(priv->dev3))
2440 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 kfree(priv);
2442}
2443
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002444static void alps_set_abs_params_st(struct alps_data *priv,
2445 struct input_dev *dev1)
2446{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002447 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2448 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002449}
2450
2451static void alps_set_abs_params_mt(struct alps_data *priv,
2452 struct input_dev *dev1)
2453{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002454 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2455 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002456
Hans de Goedef3f33c62014-07-29 11:22:07 -07002457 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2458 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2459
Hans de Goedecdf333e2014-07-25 22:44:42 -07002460 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2461 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2462
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002463 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2464 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Yunkang Tang38088432014-07-26 13:51:41 -07002465
2466 /* V7 is real multi-touch */
2467 if (priv->proto_version == ALPS_PROTO_V7)
2468 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002469}
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471int alps_init(struct psmouse *psmouse)
2472{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002473 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002474 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002475 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002477 error = priv->hw_init(psmouse);
2478 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 goto init_fail;
2480
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002481 /*
2482 * Undo part of setup done for us by psmouse core since touchpad
2483 * is not a relative device.
2484 */
2485 __clear_bit(EV_REL, dev1->evbit);
2486 __clear_bit(REL_X, dev1->relbit);
2487 __clear_bit(REL_Y, dev1->relbit);
2488
2489 /*
2490 * Now set up our capabilities.
2491 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002492 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2493 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2494 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002495 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2496 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002498 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002499
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002500 priv->set_abs_params(priv, dev1);
Yunkang Tang38088432014-07-26 13:51:41 -07002501 /* No pressure on V7 */
2502 if (priv->proto_version != ALPS_PROTO_V7)
2503 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002505 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002506 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2507 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
2509
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002510 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002511 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2512 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002515 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002516 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2517 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2518 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2519 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002520 } else if (priv->flags & ALPS_BUTTONPAD) {
2521 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2522 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002523 } else {
2524 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2525 }
2526
Pali Rohárdfba8602015-01-09 12:48:44 -08002527 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002528 struct input_dev *dev2;
2529
2530 dev2 = input_allocate_device();
2531 if (!dev2) {
2532 psmouse_err(psmouse,
2533 "failed to allocate trackstick device\n");
2534 error = -ENOMEM;
2535 goto init_fail;
2536 }
2537
2538 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2539 psmouse->ps2dev.serio->phys);
2540 dev2->phys = priv->phys2;
2541
Pali Rohárdfba8602015-01-09 12:48:44 -08002542 /*
2543 * format of input device name is: "protocol vendor name"
2544 * see function psmouse_switch_protocol() in psmouse-base.c
2545 */
2546 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002547
2548 dev2->id.bustype = BUS_I8042;
2549 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002550 dev2->id.product = PSMOUSE_ALPS;
2551 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002552 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002553
Pali Rohár04aae282015-01-14 13:18:30 -08002554 input_set_capability(dev2, EV_REL, REL_X);
2555 input_set_capability(dev2, EV_REL, REL_Y);
2556 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2557 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2558 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002559
Pali Rohár04aae282015-01-14 13:18:30 -08002560 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002561 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2562
Pali Rohár04aae282015-01-14 13:18:30 -08002563 error = input_register_device(dev2);
2564 if (error) {
2565 psmouse_err(psmouse,
2566 "failed to register trackstick device: %d\n",
2567 error);
2568 input_free_device(dev2);
2569 goto init_fail;
2570 }
2571
2572 priv->dev2 = dev2;
2573 }
2574
2575 priv->psmouse = psmouse;
2576
2577 INIT_DELAYED_WORK(&priv->dev3_register_work,
2578 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002581 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 psmouse->disconnect = alps_disconnect;
2583 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002584 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002586 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2587 psmouse->resync_time = 0;
2588
Pali Rohár9d720b32014-11-08 12:58:57 -08002589 /* Allow 2 invalid packets without resetting device */
2590 psmouse->resetafter = psmouse->pktsize * 2;
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return 0;
2593
2594init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002595 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002596 /*
2597 * Even though we did not allocate psmouse->private we do free
2598 * it here.
2599 */
2600 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002601 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002602 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002605int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002607 struct alps_data *priv;
2608 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002610 error = alps_identify(psmouse, NULL);
2611 if (error)
2612 return error;
2613
2614 /*
2615 * Reset the device to make sure it is fully operational:
2616 * on some laptops, like certain Dell Latitudes, we may
2617 * fail to properly detect presence of trackstick if device
2618 * has not been reset.
2619 */
2620 psmouse_reset(psmouse);
2621
2622 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2623 if (!priv)
2624 return -ENOMEM;
2625
2626 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08002627 if (error) {
2628 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002629 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08002630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 if (set_properties) {
2633 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002634 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002635 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002636 psmouse->model = priv->proto_version;
2637 } else {
2638 /*
2639 * Destroy alps_data structure we allocated earlier since
2640 * this was just a "trial run". Otherwise we'll keep it
2641 * to be used by alps_init() which has to be called if
2642 * we succeed and set_properties is true.
2643 */
2644 kfree(priv);
2645 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return 0;
2649}
2650